nfs_subs.c revision 1.98 1 /* $NetBSD: nfs_subs.c,v 1.98 2001/09/27 21:44:56 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
39 */
40
41 /*
42 * Copyright 2000 Wasabi Systems, Inc.
43 * All rights reserved.
44 *
45 * Written by Frank van der Linden for Wasabi Systems, Inc.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed for the NetBSD Project by
58 * Wasabi Systems, Inc.
59 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
60 * or promote products derived from this software without specific prior
61 * written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
65 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
66 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
67 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
68 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
69 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
70 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
71 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
72 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
73 * POSSIBILITY OF SUCH DAMAGE.
74 */
75
76 #include "fs_nfs.h"
77 #include "opt_nfs.h"
78 #include "opt_nfsserver.h"
79 #include "opt_iso.h"
80 #include "opt_inet.h"
81
82 /*
83 * These functions support the macros and help fiddle mbuf chains for
84 * the nfs op functions. They do things like create the rpc header and
85 * copy data between mbuf chains and uio lists.
86 */
87 #include <sys/param.h>
88 #include <sys/proc.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/mount.h>
92 #include <sys/vnode.h>
93 #include <sys/namei.h>
94 #include <sys/mbuf.h>
95 #include <sys/socket.h>
96 #include <sys/stat.h>
97 #include <sys/malloc.h>
98 #include <sys/filedesc.h>
99 #include <sys/time.h>
100 #include <sys/dirent.h>
101
102 #include <uvm/uvm_extern.h>
103
104 #include <nfs/rpcv2.h>
105 #include <nfs/nfsproto.h>
106 #include <nfs/nfsnode.h>
107 #include <nfs/nfs.h>
108 #include <nfs/xdr_subs.h>
109 #include <nfs/nfsm_subs.h>
110 #include <nfs/nfsmount.h>
111 #include <nfs/nqnfs.h>
112 #include <nfs/nfsrtt.h>
113 #include <nfs/nfs_var.h>
114
115 #include <miscfs/specfs/specdev.h>
116
117 #include <netinet/in.h>
118 #ifdef ISO
119 #include <netiso/iso.h>
120 #endif
121
122 /*
123 * Data items converted to xdr at startup, since they are constant
124 * This is kinda hokey, but may save a little time doing byte swaps
125 */
126 u_int32_t nfs_xdrneg1;
127 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
128 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
129 rpc_auth_kerb;
130 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
131
132 /* And other global data */
133 static u_int32_t nfs_xid = 0;
134 const nfstype nfsv2_type[9] =
135 { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON, NFCHR, NFNON };
136 const nfstype nfsv3_type[9] =
137 { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON };
138 const enum vtype nv2tov_type[8] =
139 { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
140 const enum vtype nv3tov_type[8] =
141 { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
142 int nfs_ticks;
143
144 /* NFS client/server stats. */
145 struct nfsstats nfsstats;
146
147 /*
148 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
149 */
150 const int nfsv3_procid[NFS_NPROCS] = {
151 NFSPROC_NULL,
152 NFSPROC_GETATTR,
153 NFSPROC_SETATTR,
154 NFSPROC_NOOP,
155 NFSPROC_LOOKUP,
156 NFSPROC_READLINK,
157 NFSPROC_READ,
158 NFSPROC_NOOP,
159 NFSPROC_WRITE,
160 NFSPROC_CREATE,
161 NFSPROC_REMOVE,
162 NFSPROC_RENAME,
163 NFSPROC_LINK,
164 NFSPROC_SYMLINK,
165 NFSPROC_MKDIR,
166 NFSPROC_RMDIR,
167 NFSPROC_READDIR,
168 NFSPROC_FSSTAT,
169 NFSPROC_NOOP,
170 NFSPROC_NOOP,
171 NFSPROC_NOOP,
172 NFSPROC_NOOP,
173 NFSPROC_NOOP,
174 NFSPROC_NOOP,
175 NFSPROC_NOOP,
176 NFSPROC_NOOP
177 };
178
179 /*
180 * and the reverse mapping from generic to Version 2 procedure numbers
181 */
182 const int nfsv2_procid[NFS_NPROCS] = {
183 NFSV2PROC_NULL,
184 NFSV2PROC_GETATTR,
185 NFSV2PROC_SETATTR,
186 NFSV2PROC_LOOKUP,
187 NFSV2PROC_NOOP,
188 NFSV2PROC_READLINK,
189 NFSV2PROC_READ,
190 NFSV2PROC_WRITE,
191 NFSV2PROC_CREATE,
192 NFSV2PROC_MKDIR,
193 NFSV2PROC_SYMLINK,
194 NFSV2PROC_CREATE,
195 NFSV2PROC_REMOVE,
196 NFSV2PROC_RMDIR,
197 NFSV2PROC_RENAME,
198 NFSV2PROC_LINK,
199 NFSV2PROC_READDIR,
200 NFSV2PROC_NOOP,
201 NFSV2PROC_STATFS,
202 NFSV2PROC_NOOP,
203 NFSV2PROC_NOOP,
204 NFSV2PROC_NOOP,
205 NFSV2PROC_NOOP,
206 NFSV2PROC_NOOP,
207 NFSV2PROC_NOOP,
208 NFSV2PROC_NOOP,
209 };
210
211 /*
212 * Maps errno values to nfs error numbers.
213 * Use NFSERR_IO as the catch all for ones not specifically defined in
214 * RFC 1094.
215 */
216 static const u_char nfsrv_v2errmap[ELAST] = {
217 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
218 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
219 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
220 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
221 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
222 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
223 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
224 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
225 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
226 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
227 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
228 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
229 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
230 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
231 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
232 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
233 NFSERR_IO, NFSERR_IO,
234 };
235
236 /*
237 * Maps errno values to nfs error numbers.
238 * Although it is not obvious whether or not NFS clients really care if
239 * a returned error value is in the specified list for the procedure, the
240 * safest thing to do is filter them appropriately. For Version 2, the
241 * X/Open XNFS document is the only specification that defines error values
242 * for each RPC (The RFC simply lists all possible error values for all RPCs),
243 * so I have decided to not do this for Version 2.
244 * The first entry is the default error return and the rest are the valid
245 * errors for that RPC in increasing numeric order.
246 */
247 static const short nfsv3err_null[] = {
248 0,
249 0,
250 };
251
252 static const short nfsv3err_getattr[] = {
253 NFSERR_IO,
254 NFSERR_IO,
255 NFSERR_STALE,
256 NFSERR_BADHANDLE,
257 NFSERR_SERVERFAULT,
258 0,
259 };
260
261 static const short nfsv3err_setattr[] = {
262 NFSERR_IO,
263 NFSERR_PERM,
264 NFSERR_IO,
265 NFSERR_ACCES,
266 NFSERR_INVAL,
267 NFSERR_NOSPC,
268 NFSERR_ROFS,
269 NFSERR_DQUOT,
270 NFSERR_STALE,
271 NFSERR_BADHANDLE,
272 NFSERR_NOT_SYNC,
273 NFSERR_SERVERFAULT,
274 0,
275 };
276
277 static const short nfsv3err_lookup[] = {
278 NFSERR_IO,
279 NFSERR_NOENT,
280 NFSERR_IO,
281 NFSERR_ACCES,
282 NFSERR_NOTDIR,
283 NFSERR_NAMETOL,
284 NFSERR_STALE,
285 NFSERR_BADHANDLE,
286 NFSERR_SERVERFAULT,
287 0,
288 };
289
290 static const short nfsv3err_access[] = {
291 NFSERR_IO,
292 NFSERR_IO,
293 NFSERR_STALE,
294 NFSERR_BADHANDLE,
295 NFSERR_SERVERFAULT,
296 0,
297 };
298
299 static const short nfsv3err_readlink[] = {
300 NFSERR_IO,
301 NFSERR_IO,
302 NFSERR_ACCES,
303 NFSERR_INVAL,
304 NFSERR_STALE,
305 NFSERR_BADHANDLE,
306 NFSERR_NOTSUPP,
307 NFSERR_SERVERFAULT,
308 0,
309 };
310
311 static const short nfsv3err_read[] = {
312 NFSERR_IO,
313 NFSERR_IO,
314 NFSERR_NXIO,
315 NFSERR_ACCES,
316 NFSERR_INVAL,
317 NFSERR_STALE,
318 NFSERR_BADHANDLE,
319 NFSERR_SERVERFAULT,
320 NFSERR_JUKEBOX,
321 0,
322 };
323
324 static const short nfsv3err_write[] = {
325 NFSERR_IO,
326 NFSERR_IO,
327 NFSERR_ACCES,
328 NFSERR_INVAL,
329 NFSERR_FBIG,
330 NFSERR_NOSPC,
331 NFSERR_ROFS,
332 NFSERR_DQUOT,
333 NFSERR_STALE,
334 NFSERR_BADHANDLE,
335 NFSERR_SERVERFAULT,
336 NFSERR_JUKEBOX,
337 0,
338 };
339
340 static const short nfsv3err_create[] = {
341 NFSERR_IO,
342 NFSERR_IO,
343 NFSERR_ACCES,
344 NFSERR_EXIST,
345 NFSERR_NOTDIR,
346 NFSERR_NOSPC,
347 NFSERR_ROFS,
348 NFSERR_NAMETOL,
349 NFSERR_DQUOT,
350 NFSERR_STALE,
351 NFSERR_BADHANDLE,
352 NFSERR_NOTSUPP,
353 NFSERR_SERVERFAULT,
354 0,
355 };
356
357 static const short nfsv3err_mkdir[] = {
358 NFSERR_IO,
359 NFSERR_IO,
360 NFSERR_ACCES,
361 NFSERR_EXIST,
362 NFSERR_NOTDIR,
363 NFSERR_NOSPC,
364 NFSERR_ROFS,
365 NFSERR_NAMETOL,
366 NFSERR_DQUOT,
367 NFSERR_STALE,
368 NFSERR_BADHANDLE,
369 NFSERR_NOTSUPP,
370 NFSERR_SERVERFAULT,
371 0,
372 };
373
374 static const short nfsv3err_symlink[] = {
375 NFSERR_IO,
376 NFSERR_IO,
377 NFSERR_ACCES,
378 NFSERR_EXIST,
379 NFSERR_NOTDIR,
380 NFSERR_NOSPC,
381 NFSERR_ROFS,
382 NFSERR_NAMETOL,
383 NFSERR_DQUOT,
384 NFSERR_STALE,
385 NFSERR_BADHANDLE,
386 NFSERR_NOTSUPP,
387 NFSERR_SERVERFAULT,
388 0,
389 };
390
391 static const short nfsv3err_mknod[] = {
392 NFSERR_IO,
393 NFSERR_IO,
394 NFSERR_ACCES,
395 NFSERR_EXIST,
396 NFSERR_NOTDIR,
397 NFSERR_NOSPC,
398 NFSERR_ROFS,
399 NFSERR_NAMETOL,
400 NFSERR_DQUOT,
401 NFSERR_STALE,
402 NFSERR_BADHANDLE,
403 NFSERR_NOTSUPP,
404 NFSERR_SERVERFAULT,
405 NFSERR_BADTYPE,
406 0,
407 };
408
409 static const short nfsv3err_remove[] = {
410 NFSERR_IO,
411 NFSERR_NOENT,
412 NFSERR_IO,
413 NFSERR_ACCES,
414 NFSERR_NOTDIR,
415 NFSERR_ROFS,
416 NFSERR_NAMETOL,
417 NFSERR_STALE,
418 NFSERR_BADHANDLE,
419 NFSERR_SERVERFAULT,
420 0,
421 };
422
423 static const short nfsv3err_rmdir[] = {
424 NFSERR_IO,
425 NFSERR_NOENT,
426 NFSERR_IO,
427 NFSERR_ACCES,
428 NFSERR_EXIST,
429 NFSERR_NOTDIR,
430 NFSERR_INVAL,
431 NFSERR_ROFS,
432 NFSERR_NAMETOL,
433 NFSERR_NOTEMPTY,
434 NFSERR_STALE,
435 NFSERR_BADHANDLE,
436 NFSERR_NOTSUPP,
437 NFSERR_SERVERFAULT,
438 0,
439 };
440
441 static const short nfsv3err_rename[] = {
442 NFSERR_IO,
443 NFSERR_NOENT,
444 NFSERR_IO,
445 NFSERR_ACCES,
446 NFSERR_EXIST,
447 NFSERR_XDEV,
448 NFSERR_NOTDIR,
449 NFSERR_ISDIR,
450 NFSERR_INVAL,
451 NFSERR_NOSPC,
452 NFSERR_ROFS,
453 NFSERR_MLINK,
454 NFSERR_NAMETOL,
455 NFSERR_NOTEMPTY,
456 NFSERR_DQUOT,
457 NFSERR_STALE,
458 NFSERR_BADHANDLE,
459 NFSERR_NOTSUPP,
460 NFSERR_SERVERFAULT,
461 0,
462 };
463
464 static const short nfsv3err_link[] = {
465 NFSERR_IO,
466 NFSERR_IO,
467 NFSERR_ACCES,
468 NFSERR_EXIST,
469 NFSERR_XDEV,
470 NFSERR_NOTDIR,
471 NFSERR_INVAL,
472 NFSERR_NOSPC,
473 NFSERR_ROFS,
474 NFSERR_MLINK,
475 NFSERR_NAMETOL,
476 NFSERR_DQUOT,
477 NFSERR_STALE,
478 NFSERR_BADHANDLE,
479 NFSERR_NOTSUPP,
480 NFSERR_SERVERFAULT,
481 0,
482 };
483
484 static const short nfsv3err_readdir[] = {
485 NFSERR_IO,
486 NFSERR_IO,
487 NFSERR_ACCES,
488 NFSERR_NOTDIR,
489 NFSERR_STALE,
490 NFSERR_BADHANDLE,
491 NFSERR_BAD_COOKIE,
492 NFSERR_TOOSMALL,
493 NFSERR_SERVERFAULT,
494 0,
495 };
496
497 static const short nfsv3err_readdirplus[] = {
498 NFSERR_IO,
499 NFSERR_IO,
500 NFSERR_ACCES,
501 NFSERR_NOTDIR,
502 NFSERR_STALE,
503 NFSERR_BADHANDLE,
504 NFSERR_BAD_COOKIE,
505 NFSERR_NOTSUPP,
506 NFSERR_TOOSMALL,
507 NFSERR_SERVERFAULT,
508 0,
509 };
510
511 static const short nfsv3err_fsstat[] = {
512 NFSERR_IO,
513 NFSERR_IO,
514 NFSERR_STALE,
515 NFSERR_BADHANDLE,
516 NFSERR_SERVERFAULT,
517 0,
518 };
519
520 static const short nfsv3err_fsinfo[] = {
521 NFSERR_STALE,
522 NFSERR_STALE,
523 NFSERR_BADHANDLE,
524 NFSERR_SERVERFAULT,
525 0,
526 };
527
528 static const short nfsv3err_pathconf[] = {
529 NFSERR_STALE,
530 NFSERR_STALE,
531 NFSERR_BADHANDLE,
532 NFSERR_SERVERFAULT,
533 0,
534 };
535
536 static const short nfsv3err_commit[] = {
537 NFSERR_IO,
538 NFSERR_IO,
539 NFSERR_STALE,
540 NFSERR_BADHANDLE,
541 NFSERR_SERVERFAULT,
542 0,
543 };
544
545 static const short * const nfsrv_v3errmap[] = {
546 nfsv3err_null,
547 nfsv3err_getattr,
548 nfsv3err_setattr,
549 nfsv3err_lookup,
550 nfsv3err_access,
551 nfsv3err_readlink,
552 nfsv3err_read,
553 nfsv3err_write,
554 nfsv3err_create,
555 nfsv3err_mkdir,
556 nfsv3err_symlink,
557 nfsv3err_mknod,
558 nfsv3err_remove,
559 nfsv3err_rmdir,
560 nfsv3err_rename,
561 nfsv3err_link,
562 nfsv3err_readdir,
563 nfsv3err_readdirplus,
564 nfsv3err_fsstat,
565 nfsv3err_fsinfo,
566 nfsv3err_pathconf,
567 nfsv3err_commit,
568 };
569
570 extern struct nfsrtt nfsrtt;
571 extern time_t nqnfsstarttime;
572 extern int nqsrv_clockskew;
573 extern int nqsrv_writeslack;
574 extern int nqsrv_maxlease;
575 extern const int nqnfs_piggy[NFS_NPROCS];
576 extern struct nfsnodehashhead *nfsnodehashtbl;
577 extern u_long nfsnodehash;
578
579 LIST_HEAD(nfsnodehashhead, nfsnode);
580 u_long nfsdirhashmask;
581
582 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
583
584 /*
585 * Create the header for an rpc request packet
586 * The hsiz is the size of the rest of the nfs request header.
587 * (just used to decide if a cluster is a good idea)
588 */
589 struct mbuf *
590 nfsm_reqh(vp, procid, hsiz, bposp)
591 struct vnode *vp;
592 u_long procid;
593 int hsiz;
594 caddr_t *bposp;
595 {
596 struct mbuf *mb;
597 caddr_t bpos;
598 struct nfsmount *nmp;
599 #ifndef NFS_V2_ONLY
600 u_int32_t *tl;
601 struct mbuf *mb2;
602 int nqflag;
603 #endif
604
605 MGET(mb, M_WAIT, MT_DATA);
606 if (hsiz >= MINCLSIZE)
607 MCLGET(mb, M_WAIT);
608 mb->m_len = 0;
609 bpos = mtod(mb, caddr_t);
610
611 /*
612 * For NQNFS, add lease request.
613 */
614 if (vp) {
615 nmp = VFSTONFS(vp->v_mount);
616 #ifndef NFS_V2_ONLY
617 if (nmp->nm_flag & NFSMNT_NQNFS) {
618 nqflag = NQNFS_NEEDLEASE(vp, procid);
619 if (nqflag) {
620 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
621 *tl++ = txdr_unsigned(nqflag);
622 *tl = txdr_unsigned(nmp->nm_leaseterm);
623 } else {
624 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
625 *tl = 0;
626 }
627 }
628 #endif
629 }
630 /* Finally, return values */
631 *bposp = bpos;
632 return (mb);
633 }
634
635 /*
636 * Build the RPC header and fill in the authorization info.
637 * The authorization string argument is only used when the credentials
638 * come from outside of the kernel.
639 * Returns the head of the mbuf list.
640 */
641 struct mbuf *
642 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
643 verf_str, mrest, mrest_len, mbp, xidp)
644 struct ucred *cr;
645 int nmflag;
646 int procid;
647 int auth_type;
648 int auth_len;
649 char *auth_str;
650 int verf_len;
651 char *verf_str;
652 struct mbuf *mrest;
653 int mrest_len;
654 struct mbuf **mbp;
655 u_int32_t *xidp;
656 {
657 struct mbuf *mb;
658 u_int32_t *tl;
659 caddr_t bpos;
660 int i;
661 struct mbuf *mreq, *mb2;
662 int siz, grpsiz, authsiz;
663 struct timeval tv;
664 static u_int32_t base;
665
666 authsiz = nfsm_rndup(auth_len);
667 MGETHDR(mb, M_WAIT, MT_DATA);
668 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
669 MCLGET(mb, M_WAIT);
670 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
671 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
672 } else {
673 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
674 }
675 mb->m_len = 0;
676 mreq = mb;
677 bpos = mtod(mb, caddr_t);
678
679 /*
680 * First the RPC header.
681 */
682 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
683
684 /*
685 * derive initial xid from system time
686 * XXX time is invalid if root not yet mounted
687 */
688 if (!base && (rootvp)) {
689 microtime(&tv);
690 base = tv.tv_sec << 12;
691 nfs_xid = base;
692 }
693 /*
694 * Skip zero xid if it should ever happen.
695 */
696 if (++nfs_xid == 0)
697 nfs_xid++;
698
699 *tl++ = *xidp = txdr_unsigned(nfs_xid);
700 *tl++ = rpc_call;
701 *tl++ = rpc_vers;
702 if (nmflag & NFSMNT_NQNFS) {
703 *tl++ = txdr_unsigned(NQNFS_PROG);
704 *tl++ = txdr_unsigned(NQNFS_VER3);
705 } else {
706 *tl++ = txdr_unsigned(NFS_PROG);
707 if (nmflag & NFSMNT_NFSV3)
708 *tl++ = txdr_unsigned(NFS_VER3);
709 else
710 *tl++ = txdr_unsigned(NFS_VER2);
711 }
712 if (nmflag & NFSMNT_NFSV3)
713 *tl++ = txdr_unsigned(procid);
714 else
715 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
716
717 /*
718 * And then the authorization cred.
719 */
720 *tl++ = txdr_unsigned(auth_type);
721 *tl = txdr_unsigned(authsiz);
722 switch (auth_type) {
723 case RPCAUTH_UNIX:
724 nfsm_build(tl, u_int32_t *, auth_len);
725 *tl++ = 0; /* stamp ?? */
726 *tl++ = 0; /* NULL hostname */
727 *tl++ = txdr_unsigned(cr->cr_uid);
728 *tl++ = txdr_unsigned(cr->cr_gid);
729 grpsiz = (auth_len >> 2) - 5;
730 *tl++ = txdr_unsigned(grpsiz);
731 for (i = 0; i < grpsiz; i++)
732 *tl++ = txdr_unsigned(cr->cr_groups[i]);
733 break;
734 case RPCAUTH_KERB4:
735 siz = auth_len;
736 while (siz > 0) {
737 if (M_TRAILINGSPACE(mb) == 0) {
738 MGET(mb2, M_WAIT, MT_DATA);
739 if (siz >= MINCLSIZE)
740 MCLGET(mb2, M_WAIT);
741 mb->m_next = mb2;
742 mb = mb2;
743 mb->m_len = 0;
744 bpos = mtod(mb, caddr_t);
745 }
746 i = min(siz, M_TRAILINGSPACE(mb));
747 memcpy(bpos, auth_str, i);
748 mb->m_len += i;
749 auth_str += i;
750 bpos += i;
751 siz -= i;
752 }
753 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
754 for (i = 0; i < siz; i++)
755 *bpos++ = '\0';
756 mb->m_len += siz;
757 }
758 break;
759 };
760
761 /*
762 * And the verifier...
763 */
764 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
765 if (verf_str) {
766 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
767 *tl = txdr_unsigned(verf_len);
768 siz = verf_len;
769 while (siz > 0) {
770 if (M_TRAILINGSPACE(mb) == 0) {
771 MGET(mb2, M_WAIT, MT_DATA);
772 if (siz >= MINCLSIZE)
773 MCLGET(mb2, M_WAIT);
774 mb->m_next = mb2;
775 mb = mb2;
776 mb->m_len = 0;
777 bpos = mtod(mb, caddr_t);
778 }
779 i = min(siz, M_TRAILINGSPACE(mb));
780 memcpy(bpos, verf_str, i);
781 mb->m_len += i;
782 verf_str += i;
783 bpos += i;
784 siz -= i;
785 }
786 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
787 for (i = 0; i < siz; i++)
788 *bpos++ = '\0';
789 mb->m_len += siz;
790 }
791 } else {
792 *tl++ = txdr_unsigned(RPCAUTH_NULL);
793 *tl = 0;
794 }
795 mb->m_next = mrest;
796 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
797 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
798 *mbp = mb;
799 return (mreq);
800 }
801
802 /*
803 * copies mbuf chain to the uio scatter/gather list
804 */
805 int
806 nfsm_mbuftouio(mrep, uiop, siz, dpos)
807 struct mbuf **mrep;
808 struct uio *uiop;
809 int siz;
810 caddr_t *dpos;
811 {
812 char *mbufcp, *uiocp;
813 int xfer, left, len;
814 struct mbuf *mp;
815 long uiosiz, rem;
816 int error = 0;
817
818 mp = *mrep;
819 mbufcp = *dpos;
820 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
821 rem = nfsm_rndup(siz)-siz;
822 while (siz > 0) {
823 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
824 return (EFBIG);
825 left = uiop->uio_iov->iov_len;
826 uiocp = uiop->uio_iov->iov_base;
827 if (left > siz)
828 left = siz;
829 uiosiz = left;
830 while (left > 0) {
831 while (len == 0) {
832 mp = mp->m_next;
833 if (mp == NULL)
834 return (EBADRPC);
835 mbufcp = mtod(mp, caddr_t);
836 len = mp->m_len;
837 }
838 xfer = (left > len) ? len : left;
839 #ifdef notdef
840 /* Not Yet.. */
841 if (uiop->uio_iov->iov_op != NULL)
842 (*(uiop->uio_iov->iov_op))
843 (mbufcp, uiocp, xfer);
844 else
845 #endif
846 if (uiop->uio_segflg == UIO_SYSSPACE)
847 memcpy(uiocp, mbufcp, xfer);
848 else
849 copyout(mbufcp, uiocp, xfer);
850 left -= xfer;
851 len -= xfer;
852 mbufcp += xfer;
853 uiocp += xfer;
854 uiop->uio_offset += xfer;
855 uiop->uio_resid -= xfer;
856 }
857 if (uiop->uio_iov->iov_len <= siz) {
858 uiop->uio_iovcnt--;
859 uiop->uio_iov++;
860 } else {
861 uiop->uio_iov->iov_base =
862 (caddr_t)uiop->uio_iov->iov_base + uiosiz;
863 uiop->uio_iov->iov_len -= uiosiz;
864 }
865 siz -= uiosiz;
866 }
867 *dpos = mbufcp;
868 *mrep = mp;
869 if (rem > 0) {
870 if (len < rem)
871 error = nfs_adv(mrep, dpos, rem, len);
872 else
873 *dpos += rem;
874 }
875 return (error);
876 }
877
878 /*
879 * copies a uio scatter/gather list to an mbuf chain.
880 * NOTE: can ony handle iovcnt == 1
881 */
882 int
883 nfsm_uiotombuf(uiop, mq, siz, bpos)
884 struct uio *uiop;
885 struct mbuf **mq;
886 int siz;
887 caddr_t *bpos;
888 {
889 char *uiocp;
890 struct mbuf *mp, *mp2;
891 int xfer, left, mlen;
892 int uiosiz, clflg, rem;
893 char *cp;
894
895 #ifdef DIAGNOSTIC
896 if (uiop->uio_iovcnt != 1)
897 panic("nfsm_uiotombuf: iovcnt != 1");
898 #endif
899
900 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
901 clflg = 1;
902 else
903 clflg = 0;
904 rem = nfsm_rndup(siz)-siz;
905 mp = mp2 = *mq;
906 while (siz > 0) {
907 left = uiop->uio_iov->iov_len;
908 uiocp = uiop->uio_iov->iov_base;
909 if (left > siz)
910 left = siz;
911 uiosiz = left;
912 while (left > 0) {
913 mlen = M_TRAILINGSPACE(mp);
914 if (mlen == 0) {
915 MGET(mp, M_WAIT, MT_DATA);
916 if (clflg)
917 MCLGET(mp, M_WAIT);
918 mp->m_len = 0;
919 mp2->m_next = mp;
920 mp2 = mp;
921 mlen = M_TRAILINGSPACE(mp);
922 }
923 xfer = (left > mlen) ? mlen : left;
924 #ifdef notdef
925 /* Not Yet.. */
926 if (uiop->uio_iov->iov_op != NULL)
927 (*(uiop->uio_iov->iov_op))
928 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
929 else
930 #endif
931 if (uiop->uio_segflg == UIO_SYSSPACE)
932 memcpy(mtod(mp, caddr_t)+mp->m_len, uiocp, xfer);
933 else
934 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
935 mp->m_len += xfer;
936 left -= xfer;
937 uiocp += xfer;
938 uiop->uio_offset += xfer;
939 uiop->uio_resid -= xfer;
940 }
941 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
942 uiosiz;
943 uiop->uio_iov->iov_len -= uiosiz;
944 siz -= uiosiz;
945 }
946 if (rem > 0) {
947 if (rem > M_TRAILINGSPACE(mp)) {
948 MGET(mp, M_WAIT, MT_DATA);
949 mp->m_len = 0;
950 mp2->m_next = mp;
951 }
952 cp = mtod(mp, caddr_t)+mp->m_len;
953 for (left = 0; left < rem; left++)
954 *cp++ = '\0';
955 mp->m_len += rem;
956 *bpos = cp;
957 } else
958 *bpos = mtod(mp, caddr_t)+mp->m_len;
959 *mq = mp;
960 return (0);
961 }
962
963 /*
964 * Get at least "siz" bytes of correctly aligned data.
965 * When called the mbuf pointers are not necessarily correct,
966 * dsosp points to what ought to be in m_data and left contains
967 * what ought to be in m_len.
968 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
969 * cases. (The macros use the vars. dpos and dpos2)
970 */
971 int
972 nfsm_disct(mdp, dposp, siz, left, cp2)
973 struct mbuf **mdp;
974 caddr_t *dposp;
975 int siz;
976 int left;
977 caddr_t *cp2;
978 {
979 struct mbuf *m1, *m2;
980 struct mbuf *havebuf = NULL;
981 caddr_t src = *dposp;
982 caddr_t dst;
983 int len;
984
985 #ifdef DEBUG
986 if (left < 0)
987 panic("nfsm_disct: left < 0");
988 #endif
989 m1 = *mdp;
990 /*
991 * Skip through the mbuf chain looking for an mbuf with
992 * some data. If the first mbuf found has enough data
993 * and it is correctly aligned return it.
994 */
995 while (left == 0) {
996 havebuf = m1;
997 *mdp = m1 = m1->m_next;
998 if (m1 == NULL)
999 return (EBADRPC);
1000 src = mtod(m1, caddr_t);
1001 left = m1->m_len;
1002 /*
1003 * If we start a new mbuf and it is big enough
1004 * and correctly aligned just return it, don't
1005 * do any pull up.
1006 */
1007 if (left >= siz && nfsm_aligned(src)) {
1008 *cp2 = src;
1009 *dposp = src + siz;
1010 return (0);
1011 }
1012 }
1013 if (m1->m_flags & M_EXT) {
1014 if (havebuf) {
1015 /* If the first mbuf with data has external data
1016 * and there is a previous empty mbuf use it
1017 * to move the data into.
1018 */
1019 m2 = m1;
1020 *mdp = m1 = havebuf;
1021 if (m1->m_flags & M_EXT) {
1022 MEXTREMOVE(m1);
1023 }
1024 } else {
1025 /*
1026 * If the first mbuf has a external data
1027 * and there is no previous empty mbuf
1028 * allocate a new mbuf and move the external
1029 * data to the new mbuf. Also make the first
1030 * mbuf look empty.
1031 */
1032 m2 = m_get(M_WAIT, MT_DATA);
1033 m2->m_ext = m1->m_ext;
1034 m2->m_data = src;
1035 m2->m_len = left;
1036 MCLADDREFERENCE(m1, m2);
1037 MEXTREMOVE(m1);
1038 m2->m_next = m1->m_next;
1039 m1->m_next = m2;
1040 }
1041 m1->m_len = 0;
1042 dst = m1->m_dat;
1043 } else {
1044 /*
1045 * If the first mbuf has no external data
1046 * move the data to the front of the mbuf.
1047 */
1048 if ((dst = m1->m_dat) != src)
1049 memmove(dst, src, left);
1050 dst += left;
1051 m1->m_len = left;
1052 m2 = m1->m_next;
1053 }
1054 m1->m_flags &= ~M_PKTHDR;
1055 *cp2 = m1->m_data = m1->m_dat; /* data is at beginning of buffer */
1056 *dposp = mtod(m1, caddr_t) + siz;
1057 /*
1058 * Loop through mbufs pulling data up into first mbuf until
1059 * the first mbuf is full or there is no more data to
1060 * pullup.
1061 */
1062 while ((len = (MLEN - m1->m_len)) != 0 && m2) {
1063 if ((len = min(len, m2->m_len)) != 0)
1064 memcpy(dst, m2->m_data, len);
1065 m1->m_len += len;
1066 dst += len;
1067 m2->m_data += len;
1068 m2->m_len -= len;
1069 m2 = m2->m_next;
1070 }
1071 if (m1->m_len < siz)
1072 return (EBADRPC);
1073 return (0);
1074 }
1075
1076 /*
1077 * Advance the position in the mbuf chain.
1078 */
1079 int
1080 nfs_adv(mdp, dposp, offs, left)
1081 struct mbuf **mdp;
1082 caddr_t *dposp;
1083 int offs;
1084 int left;
1085 {
1086 struct mbuf *m;
1087 int s;
1088
1089 m = *mdp;
1090 s = left;
1091 while (s < offs) {
1092 offs -= s;
1093 m = m->m_next;
1094 if (m == NULL)
1095 return (EBADRPC);
1096 s = m->m_len;
1097 }
1098 *mdp = m;
1099 *dposp = mtod(m, caddr_t)+offs;
1100 return (0);
1101 }
1102
1103 /*
1104 * Copy a string into mbufs for the hard cases...
1105 */
1106 int
1107 nfsm_strtmbuf(mb, bpos, cp, siz)
1108 struct mbuf **mb;
1109 char **bpos;
1110 const char *cp;
1111 long siz;
1112 {
1113 struct mbuf *m1 = NULL, *m2;
1114 long left, xfer, len, tlen;
1115 u_int32_t *tl;
1116 int putsize;
1117
1118 putsize = 1;
1119 m2 = *mb;
1120 left = M_TRAILINGSPACE(m2);
1121 if (left > 0) {
1122 tl = ((u_int32_t *)(*bpos));
1123 *tl++ = txdr_unsigned(siz);
1124 putsize = 0;
1125 left -= NFSX_UNSIGNED;
1126 m2->m_len += NFSX_UNSIGNED;
1127 if (left > 0) {
1128 memcpy((caddr_t) tl, cp, left);
1129 siz -= left;
1130 cp += left;
1131 m2->m_len += left;
1132 left = 0;
1133 }
1134 }
1135 /* Loop around adding mbufs */
1136 while (siz > 0) {
1137 MGET(m1, M_WAIT, MT_DATA);
1138 if (siz > MLEN)
1139 MCLGET(m1, M_WAIT);
1140 m1->m_len = NFSMSIZ(m1);
1141 m2->m_next = m1;
1142 m2 = m1;
1143 tl = mtod(m1, u_int32_t *);
1144 tlen = 0;
1145 if (putsize) {
1146 *tl++ = txdr_unsigned(siz);
1147 m1->m_len -= NFSX_UNSIGNED;
1148 tlen = NFSX_UNSIGNED;
1149 putsize = 0;
1150 }
1151 if (siz < m1->m_len) {
1152 len = nfsm_rndup(siz);
1153 xfer = siz;
1154 if (xfer < len)
1155 *(tl+(xfer>>2)) = 0;
1156 } else {
1157 xfer = len = m1->m_len;
1158 }
1159 memcpy((caddr_t) tl, cp, xfer);
1160 m1->m_len = len+tlen;
1161 siz -= xfer;
1162 cp += xfer;
1163 }
1164 *mb = m1;
1165 *bpos = mtod(m1, caddr_t)+m1->m_len;
1166 return (0);
1167 }
1168
1169 /*
1170 * Directory caching routines. They work as follows:
1171 * - a cache is maintained per VDIR nfsnode.
1172 * - for each offset cookie that is exported to userspace, and can
1173 * thus be thrown back at us as an offset to VOP_READDIR, store
1174 * information in the cache.
1175 * - cached are:
1176 * - cookie itself
1177 * - blocknumber (essentially just a search key in the buffer cache)
1178 * - entry number in block.
1179 * - offset cookie of block in which this entry is stored
1180 * - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
1181 * - entries are looked up in a hash table
1182 * - also maintained is an LRU list of entries, used to determine
1183 * which ones to delete if the cache grows too large.
1184 * - if 32 <-> 64 translation mode is requested for a filesystem,
1185 * the cache also functions as a translation table
1186 * - in the translation case, invalidating the cache does not mean
1187 * flushing it, but just marking entries as invalid, except for
1188 * the <64bit cookie, 32bitcookie> pair which is still valid, to
1189 * still be able to use the cache as a translation table.
1190 * - 32 bit cookies are uniquely created by combining the hash table
1191 * entry value, and one generation count per hash table entry,
1192 * incremented each time an entry is appended to the chain.
1193 * - the cache is invalidated each time a direcory is modified
1194 * - sanity checks are also done; if an entry in a block turns
1195 * out not to have a matching cookie, the cache is invalidated
1196 * and a new block starting from the wanted offset is fetched from
1197 * the server.
1198 * - directory entries as read from the server are extended to contain
1199 * the 64bit and, optionally, the 32bit cookies, for sanity checking
1200 * the cache and exporting them to userspace through the cookie
1201 * argument to VOP_READDIR.
1202 */
1203
1204 u_long
1205 nfs_dirhash(off)
1206 off_t off;
1207 {
1208 int i;
1209 char *cp = (char *)&off;
1210 u_long sum = 0L;
1211
1212 for (i = 0 ; i < sizeof (off); i++)
1213 sum += *cp++;
1214
1215 return sum;
1216 }
1217
1218 void
1219 nfs_initdircache(vp)
1220 struct vnode *vp;
1221 {
1222 struct nfsnode *np = VTONFS(vp);
1223 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1224
1225 np->n_dircachesize = 0;
1226 np->n_dblkno = 1;
1227 np->n_dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, M_NFSDIROFF,
1228 M_WAITOK, &nfsdirhashmask);
1229 TAILQ_INIT(&np->n_dirchain);
1230 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1231 MALLOC(np->n_dirgens, unsigned *,
1232 NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF,
1233 M_WAITOK);
1234 memset((caddr_t)np->n_dirgens, 0,
1235 NFS_DIRHASHSIZ * sizeof (unsigned));
1236 }
1237 }
1238
1239 static struct nfsdircache dzero = {0, 0, {0, 0}, {0, 0}, 0, 0, 0};
1240
1241 struct nfsdircache *
1242 nfs_searchdircache(vp, off, do32, hashent)
1243 struct vnode *vp;
1244 off_t off;
1245 int do32;
1246 int *hashent;
1247 {
1248 struct nfsdirhashhead *ndhp;
1249 struct nfsdircache *ndp = NULL;
1250 struct nfsnode *np = VTONFS(vp);
1251 unsigned ent;
1252
1253 /*
1254 * Zero is always a valid cookie.
1255 */
1256 if (off == 0)
1257 return &dzero;
1258
1259 /*
1260 * We use a 32bit cookie as search key, directly reconstruct
1261 * the hashentry. Else use the hashfunction.
1262 */
1263 if (do32) {
1264 ent = (u_int32_t)off >> 24;
1265 if (ent >= NFS_DIRHASHSIZ)
1266 return NULL;
1267 ndhp = &np->n_dircache[ent];
1268 } else {
1269 ndhp = NFSDIRHASH(np, off);
1270 }
1271
1272 if (hashent)
1273 *hashent = (int)(ndhp - np->n_dircache);
1274 if (do32) {
1275 for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next) {
1276 if (ndp->dc_cookie32 == (u_int32_t)off) {
1277 /*
1278 * An invalidated entry will become the
1279 * start of a new block fetched from
1280 * the server.
1281 */
1282 if (ndp->dc_blkno == -1) {
1283 ndp->dc_blkcookie = ndp->dc_cookie;
1284 ndp->dc_blkno = np->n_dblkno++;
1285 ndp->dc_entry = 0;
1286 }
1287 break;
1288 }
1289 }
1290 } else {
1291 for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next)
1292 if (ndp->dc_cookie == off)
1293 break;
1294 }
1295 return ndp;
1296 }
1297
1298
1299 struct nfsdircache *
1300 nfs_enterdircache(vp, off, blkoff, en, blkno)
1301 struct vnode *vp;
1302 off_t off, blkoff;
1303 daddr_t blkno;
1304 int en;
1305 {
1306 struct nfsnode *np = VTONFS(vp);
1307 struct nfsdirhashhead *ndhp;
1308 struct nfsdircache *ndp = NULL, *first;
1309 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1310 int hashent, gen, overwrite;
1311
1312 if (!np->n_dircache)
1313 /*
1314 * XXX would like to do this in nfs_nget but vtype
1315 * isn't known at that time.
1316 */
1317 nfs_initdircache(vp);
1318
1319 /*
1320 * XXX refuse entries for offset 0. amd(8) erroneously sets
1321 * cookie 0 for the '.' entry, making this necessary. This
1322 * isn't so bad, as 0 is a special case anyway.
1323 */
1324 if (off == 0)
1325 return &dzero;
1326
1327 ndp = nfs_searchdircache(vp, off, 0, &hashent);
1328
1329 if (ndp && ndp->dc_blkno != -1) {
1330 /*
1331 * Overwriting an old entry. Check if it's the same.
1332 * If so, just return. If not, remove the old entry.
1333 */
1334 if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
1335 return ndp;
1336 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1337 LIST_REMOVE(ndp, dc_hash);
1338 FREE(ndp, M_NFSDIROFF);
1339 ndp = 0;
1340 }
1341
1342 ndhp = &np->n_dircache[hashent];
1343
1344 if (!ndp) {
1345 MALLOC(ndp, struct nfsdircache *, sizeof (*ndp), M_NFSDIROFF,
1346 M_WAITOK);
1347 overwrite = 0;
1348 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1349 /*
1350 * We're allocating a new entry, so bump the
1351 * generation number.
1352 */
1353 gen = ++np->n_dirgens[hashent];
1354 if (gen == 0) {
1355 np->n_dirgens[hashent]++;
1356 gen++;
1357 }
1358 ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
1359 }
1360 } else
1361 overwrite = 1;
1362
1363 /*
1364 * If the entry number is 0, we are at the start of a new block, so
1365 * allocate a new blocknumber.
1366 */
1367 if (en == 0)
1368 ndp->dc_blkno = np->n_dblkno++;
1369 else
1370 ndp->dc_blkno = blkno;
1371
1372 ndp->dc_cookie = off;
1373 ndp->dc_blkcookie = blkoff;
1374 ndp->dc_entry = en;
1375
1376 if (overwrite)
1377 return ndp;
1378
1379 /*
1380 * If the maximum directory cookie cache size has been reached
1381 * for this node, take one off the front. The idea is that
1382 * directories are typically read front-to-back once, so that
1383 * the oldest entries can be thrown away without much performance
1384 * loss.
1385 */
1386 if (np->n_dircachesize == NFS_MAXDIRCACHE) {
1387 first = np->n_dirchain.tqh_first;
1388 TAILQ_REMOVE(&np->n_dirchain, first, dc_chain);
1389 LIST_REMOVE(first, dc_hash);
1390 FREE(first, M_NFSDIROFF);
1391 } else
1392 np->n_dircachesize++;
1393
1394 LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
1395 TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
1396 return ndp;
1397 }
1398
1399 void
1400 nfs_invaldircache(vp, forcefree)
1401 struct vnode *vp;
1402 int forcefree;
1403 {
1404 struct nfsnode *np = VTONFS(vp);
1405 struct nfsdircache *ndp = NULL;
1406 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1407
1408 #ifdef DIAGNOSTIC
1409 if (vp->v_type != VDIR)
1410 panic("nfs: invaldircache: not dir");
1411 #endif
1412
1413 if (!np->n_dircache)
1414 return;
1415
1416 if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
1417 while ((ndp = np->n_dirchain.tqh_first)) {
1418 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1419 LIST_REMOVE(ndp, dc_hash);
1420 FREE(ndp, M_NFSDIROFF);
1421 }
1422 np->n_dircachesize = 0;
1423 if (forcefree && np->n_dirgens) {
1424 FREE(np->n_dirgens, M_NFSDIROFF);
1425 }
1426 } else {
1427 for (ndp = np->n_dirchain.tqh_first; ndp;
1428 ndp = ndp->dc_chain.tqe_next)
1429 ndp->dc_blkno = -1;
1430 }
1431
1432 np->n_dblkno = 1;
1433 }
1434
1435 /*
1436 * Called once before VFS init to initialize shared and
1437 * server-specific data structures.
1438 */
1439 void
1440 nfs_init()
1441 {
1442 nfsrtt.pos = 0;
1443 rpc_vers = txdr_unsigned(RPC_VER2);
1444 rpc_call = txdr_unsigned(RPC_CALL);
1445 rpc_reply = txdr_unsigned(RPC_REPLY);
1446 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1447 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1448 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1449 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1450 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1451 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1452 nfs_prog = txdr_unsigned(NFS_PROG);
1453 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1454 nfs_true = txdr_unsigned(TRUE);
1455 nfs_false = txdr_unsigned(FALSE);
1456 nfs_xdrneg1 = txdr_unsigned(-1);
1457 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1458 if (nfs_ticks < 1)
1459 nfs_ticks = 1;
1460 #ifdef NFSSERVER
1461 nfsrv_init(0); /* Init server data structures */
1462 nfsrv_initcache(); /* Init the server request cache */
1463 #endif /* NFSSERVER */
1464
1465 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY)
1466 /*
1467 * Initialize the nqnfs data structures.
1468 */
1469 if (nqnfsstarttime == 0) {
1470 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1471 + nqsrv_clockskew + nqsrv_writeslack;
1472 NQLOADNOVRAM(nqnfsstarttime);
1473 CIRCLEQ_INIT(&nqtimerhead);
1474 nqfhhashtbl = hashinit(NQLCHSZ, HASH_LIST, M_NQLEASE,
1475 M_WAITOK, &nqfhhash);
1476 }
1477 #endif
1478
1479 /*
1480 * Initialize reply list and start timer
1481 */
1482 TAILQ_INIT(&nfs_reqq);
1483 nfs_timer(NULL);
1484 }
1485
1486 #ifdef NFS
1487 /*
1488 * Called once at VFS init to initialize client-specific data structures.
1489 */
1490 void
1491 nfs_vfs_init()
1492 {
1493 nfs_nhinit(); /* Init the nfsnode table */
1494 }
1495
1496 void
1497 nfs_vfs_reinit()
1498 {
1499 nfs_nhreinit();
1500 }
1501
1502 void
1503 nfs_vfs_done()
1504 {
1505 nfs_nhdone();
1506 }
1507
1508 /*
1509 * Attribute cache routines.
1510 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1511 * that are on the mbuf list
1512 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1513 * error otherwise
1514 */
1515
1516 /*
1517 * Load the attribute cache (that lives in the nfsnode entry) with
1518 * the values on the mbuf list and
1519 * Iff vap not NULL
1520 * copy the attributes to *vaper
1521 */
1522 int
1523 nfsm_loadattrcache(vpp, mdp, dposp, vaper)
1524 struct vnode **vpp;
1525 struct mbuf **mdp;
1526 caddr_t *dposp;
1527 struct vattr *vaper;
1528 {
1529 int32_t t1;
1530 caddr_t cp2;
1531 int error = 0;
1532 struct mbuf *md;
1533 int v3 = NFS_ISV3(*vpp);
1534
1535 md = *mdp;
1536 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1537 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1538 if (error)
1539 return (error);
1540 return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
1541 }
1542
1543 int
1544 nfs_loadattrcache(vpp, fp, vaper)
1545 struct vnode **vpp;
1546 struct nfs_fattr *fp;
1547 struct vattr *vaper;
1548 {
1549 struct vnode *vp = *vpp;
1550 struct vattr *vap;
1551 int v3 = NFS_ISV3(vp);
1552 enum vtype vtyp;
1553 u_short vmode;
1554 struct timespec mtime;
1555 struct vnode *nvp;
1556 int32_t rdev;
1557 struct nfsnode *np;
1558 extern int (**spec_nfsv2nodeop_p) __P((void *));
1559
1560 if (v3) {
1561 vtyp = nfsv3tov_type(fp->fa_type);
1562 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1563 rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
1564 fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
1565 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1566 } else {
1567 vtyp = nfsv2tov_type(fp->fa_type);
1568 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1569 if (vtyp == VNON || vtyp == VREG)
1570 vtyp = IFTOVT(vmode);
1571 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1572 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1573
1574 /*
1575 * Really ugly NFSv2 kludge.
1576 */
1577 if (vtyp == VCHR && rdev == 0xffffffff)
1578 vtyp = VFIFO;
1579 }
1580
1581 /*
1582 * If v_type == VNON it is a new node, so fill in the v_type,
1583 * n_mtime fields. Check to see if it represents a special
1584 * device, and if so, check for a possible alias. Once the
1585 * correct vnode has been obtained, fill in the rest of the
1586 * information.
1587 */
1588 np = VTONFS(vp);
1589 if (vp->v_type == VNON) {
1590 vp->v_type = vtyp;
1591 if (vp->v_type == VFIFO) {
1592 extern int (**fifo_nfsv2nodeop_p) __P((void *));
1593 vp->v_op = fifo_nfsv2nodeop_p;
1594 }
1595 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1596 vp->v_op = spec_nfsv2nodeop_p;
1597 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1598 if (nvp) {
1599 /*
1600 * Discard unneeded vnode, but save its nfsnode.
1601 * Since the nfsnode does not have a lock, its
1602 * vnode lock has to be carried over.
1603 */
1604 /*
1605 * XXX is the old node sure to be locked here?
1606 */
1607 KASSERT(lockstatus(&vp->v_lock) ==
1608 LK_EXCLUSIVE);
1609 nvp->v_data = vp->v_data;
1610 vp->v_data = NULL;
1611 VOP_UNLOCK(vp, 0);
1612 vp->v_op = spec_vnodeop_p;
1613 vrele(vp);
1614 vgone(vp);
1615 lockmgr(&nvp->v_lock, LK_EXCLUSIVE,
1616 &nvp->v_interlock);
1617 /*
1618 * Reinitialize aliased node.
1619 */
1620 np->n_vnode = nvp;
1621 *vpp = vp = nvp;
1622 }
1623 }
1624 np->n_mtime = mtime.tv_sec;
1625 }
1626 vap = np->n_vattr;
1627 vap->va_type = vtyp;
1628 vap->va_mode = vmode & ALLPERMS;
1629 vap->va_rdev = (dev_t)rdev;
1630 vap->va_mtime = mtime;
1631 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1632 switch (vtyp) {
1633 case VDIR:
1634 vap->va_blocksize = NFS_DIRFRAGSIZ;
1635 break;
1636 case VBLK:
1637 vap->va_blocksize = BLKDEV_IOSIZE;
1638 break;
1639 case VCHR:
1640 vap->va_blocksize = MAXBSIZE;
1641 break;
1642 default:
1643 vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
1644 fxdr_unsigned(int32_t, fp->fa2_blocksize);
1645 break;
1646 }
1647 if (v3) {
1648 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1649 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1650 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1651 vap->va_size = fxdr_hyper(&fp->fa3_size);
1652 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1653 vap->va_fileid = fxdr_unsigned(int32_t,
1654 fp->fa3_fileid.nfsuquad[1]);
1655 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1656 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1657 vap->va_flags = 0;
1658 vap->va_filerev = 0;
1659 } else {
1660 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1661 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1662 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1663 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1664 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1665 * NFS_FABLKSIZE;
1666 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1667 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1668 vap->va_flags = 0;
1669 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1670 fp->fa2_ctime.nfsv2_sec);
1671 vap->va_ctime.tv_nsec = 0;
1672 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1673 vap->va_filerev = 0;
1674 }
1675 if (vap->va_size != np->n_size) {
1676 if ((np->n_flag & NMODIFIED) && vap->va_size < np->n_size) {
1677 vap->va_size = np->n_size;
1678 } else {
1679 np->n_size = vap->va_size;
1680 if (vap->va_type == VREG) {
1681 uvm_vnp_setsize(vp, np->n_size);
1682 }
1683 }
1684 }
1685 np->n_attrstamp = time.tv_sec;
1686 if (vaper != NULL) {
1687 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
1688 if (np->n_flag & NCHG) {
1689 if (np->n_flag & NACC)
1690 vaper->va_atime = np->n_atim;
1691 if (np->n_flag & NUPD)
1692 vaper->va_mtime = np->n_mtim;
1693 }
1694 }
1695 return (0);
1696 }
1697
1698 /*
1699 * Check the time stamp
1700 * If the cache is valid, copy contents to *vap and return 0
1701 * otherwise return an error
1702 */
1703 int
1704 nfs_getattrcache(vp, vaper)
1705 struct vnode *vp;
1706 struct vattr *vaper;
1707 {
1708 struct nfsnode *np = VTONFS(vp);
1709 struct vattr *vap;
1710
1711 if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1712 nfsstats.attrcache_misses++;
1713 return (ENOENT);
1714 }
1715 nfsstats.attrcache_hits++;
1716 vap = np->n_vattr;
1717 if (vap->va_size != np->n_size) {
1718 if (vap->va_type == VREG) {
1719 if (np->n_flag & NMODIFIED) {
1720 if (vap->va_size < np->n_size)
1721 vap->va_size = np->n_size;
1722 else
1723 np->n_size = vap->va_size;
1724 } else
1725 np->n_size = vap->va_size;
1726 uvm_vnp_setsize(vp, np->n_size);
1727 } else
1728 np->n_size = vap->va_size;
1729 }
1730 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
1731 if (np->n_flag & NCHG) {
1732 if (np->n_flag & NACC)
1733 vaper->va_atime = np->n_atim;
1734 if (np->n_flag & NUPD)
1735 vaper->va_mtime = np->n_mtim;
1736 }
1737 return (0);
1738 }
1739
1740 /*
1741 * Heuristic to see if the server XDR encodes directory cookies or not.
1742 * it is not supposed to, but a lot of servers may do this. Also, since
1743 * most/all servers will implement V2 as well, it is expected that they
1744 * may return just 32 bits worth of cookie information, so we need to
1745 * find out in which 32 bits this information is available. We do this
1746 * to avoid trouble with emulated binaries that can't handle 64 bit
1747 * directory offsets.
1748 */
1749
1750 void
1751 nfs_cookieheuristic(vp, flagp, p, cred)
1752 struct vnode *vp;
1753 int *flagp;
1754 struct proc *p;
1755 struct ucred *cred;
1756 {
1757 struct uio auio;
1758 struct iovec aiov;
1759 caddr_t buf, cp;
1760 struct dirent *dp;
1761 off_t *cookies = NULL, *cop;
1762 int error, eof, nc, len;
1763
1764 MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
1765
1766 aiov.iov_base = buf;
1767 aiov.iov_len = NFS_DIRFRAGSIZ;
1768 auio.uio_iov = &aiov;
1769 auio.uio_iovcnt = 1;
1770 auio.uio_rw = UIO_READ;
1771 auio.uio_segflg = UIO_SYSSPACE;
1772 auio.uio_procp = p;
1773 auio.uio_resid = NFS_DIRFRAGSIZ;
1774 auio.uio_offset = 0;
1775
1776 error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
1777
1778 len = NFS_DIRFRAGSIZ - auio.uio_resid;
1779 if (error || len == 0) {
1780 FREE(buf, M_TEMP);
1781 if (cookies)
1782 free(cookies, M_TEMP);
1783 return;
1784 }
1785
1786 /*
1787 * Find the first valid entry and look at its offset cookie.
1788 */
1789
1790 cp = buf;
1791 for (cop = cookies; len > 0; len -= dp->d_reclen) {
1792 dp = (struct dirent *)cp;
1793 if (dp->d_fileno != 0 && len >= dp->d_reclen) {
1794 if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
1795 *flagp |= NFSMNT_SWAPCOOKIE;
1796 nfs_invaldircache(vp, 0);
1797 nfs_vinvalbuf(vp, 0, cred, p, 1);
1798 }
1799 break;
1800 }
1801 cop++;
1802 cp += dp->d_reclen;
1803 }
1804
1805 FREE(buf, M_TEMP);
1806 free(cookies, M_TEMP);
1807 }
1808 #endif /* NFS */
1809
1810 /*
1811 * Set up nameidata for a lookup() call and do it.
1812 *
1813 * If pubflag is set, this call is done for a lookup operation on the
1814 * public filehandle. In that case we allow crossing mountpoints and
1815 * absolute pathnames. However, the caller is expected to check that
1816 * the lookup result is within the public fs, and deny access if
1817 * it is not.
1818 */
1819 int
1820 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1821 struct nameidata *ndp;
1822 fhandle_t *fhp;
1823 int len;
1824 struct nfssvc_sock *slp;
1825 struct mbuf *nam;
1826 struct mbuf **mdp;
1827 caddr_t *dposp;
1828 struct vnode **retdirp;
1829 struct proc *p;
1830 int kerbflag, pubflag;
1831 {
1832 int i, rem;
1833 struct mbuf *md;
1834 char *fromcp, *tocp, *cp;
1835 struct iovec aiov;
1836 struct uio auio;
1837 struct vnode *dp;
1838 int error, rdonly, linklen;
1839 struct componentname *cnp = &ndp->ni_cnd;
1840
1841 *retdirp = (struct vnode *)0;
1842
1843 if ((len + 1) > MAXPATHLEN)
1844 return (ENAMETOOLONG);
1845 cnp->cn_pnbuf = PNBUF_GET();
1846
1847 /*
1848 * Copy the name from the mbuf list to ndp->ni_pnbuf
1849 * and set the various ndp fields appropriately.
1850 */
1851 fromcp = *dposp;
1852 tocp = cnp->cn_pnbuf;
1853 md = *mdp;
1854 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1855 for (i = 0; i < len; i++) {
1856 while (rem == 0) {
1857 md = md->m_next;
1858 if (md == NULL) {
1859 error = EBADRPC;
1860 goto out;
1861 }
1862 fromcp = mtod(md, caddr_t);
1863 rem = md->m_len;
1864 }
1865 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1866 error = EACCES;
1867 goto out;
1868 }
1869 *tocp++ = *fromcp++;
1870 rem--;
1871 }
1872 *tocp = '\0';
1873 *mdp = md;
1874 *dposp = fromcp;
1875 len = nfsm_rndup(len)-len;
1876 if (len > 0) {
1877 if (rem >= len)
1878 *dposp += len;
1879 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1880 goto out;
1881 }
1882
1883 /*
1884 * Extract and set starting directory.
1885 */
1886 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1887 nam, &rdonly, kerbflag, pubflag);
1888 if (error)
1889 goto out;
1890 if (dp->v_type != VDIR) {
1891 vrele(dp);
1892 error = ENOTDIR;
1893 goto out;
1894 }
1895
1896 if (rdonly)
1897 cnp->cn_flags |= RDONLY;
1898
1899 *retdirp = dp;
1900
1901 if (pubflag) {
1902 /*
1903 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1904 * and the 'native path' indicator.
1905 */
1906 cp = PNBUF_GET();
1907 fromcp = cnp->cn_pnbuf;
1908 tocp = cp;
1909 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1910 switch ((unsigned char)*fromcp) {
1911 case WEBNFS_NATIVE_CHAR:
1912 /*
1913 * 'Native' path for us is the same
1914 * as a path according to the NFS spec,
1915 * just skip the escape char.
1916 */
1917 fromcp++;
1918 break;
1919 /*
1920 * More may be added in the future, range 0x80-0xff
1921 */
1922 default:
1923 error = EIO;
1924 FREE(cp, M_NAMEI);
1925 goto out;
1926 }
1927 }
1928 /*
1929 * Translate the '%' escapes, URL-style.
1930 */
1931 while (*fromcp != '\0') {
1932 if (*fromcp == WEBNFS_ESC_CHAR) {
1933 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1934 fromcp++;
1935 *tocp++ = HEXSTRTOI(fromcp);
1936 fromcp += 2;
1937 continue;
1938 } else {
1939 error = ENOENT;
1940 FREE(cp, M_NAMEI);
1941 goto out;
1942 }
1943 } else
1944 *tocp++ = *fromcp++;
1945 }
1946 *tocp = '\0';
1947 PNBUF_PUT(cnp->cn_pnbuf);
1948 cnp->cn_pnbuf = cp;
1949 }
1950
1951 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1952 ndp->ni_segflg = UIO_SYSSPACE;
1953 ndp->ni_rootdir = rootvnode;
1954
1955 if (pubflag) {
1956 ndp->ni_loopcnt = 0;
1957 if (cnp->cn_pnbuf[0] == '/')
1958 dp = rootvnode;
1959 } else {
1960 cnp->cn_flags |= NOCROSSMOUNT;
1961 }
1962
1963 cnp->cn_proc = p;
1964 VREF(dp);
1965
1966 for (;;) {
1967 cnp->cn_nameptr = cnp->cn_pnbuf;
1968 ndp->ni_startdir = dp;
1969 /*
1970 * And call lookup() to do the real work
1971 */
1972 error = lookup(ndp);
1973 if (error) {
1974 PNBUF_PUT(cnp->cn_pnbuf);
1975 return (error);
1976 }
1977 /*
1978 * Check for encountering a symbolic link
1979 */
1980 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1981 if (cnp->cn_flags & (SAVENAME | SAVESTART))
1982 cnp->cn_flags |= HASBUF;
1983 else
1984 PNBUF_PUT(cnp->cn_pnbuf);
1985 return (0);
1986 } else {
1987 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
1988 VOP_UNLOCK(ndp->ni_dvp, 0);
1989 if (!pubflag) {
1990 error = EINVAL;
1991 break;
1992 }
1993
1994 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1995 error = ELOOP;
1996 break;
1997 }
1998 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
1999 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
2000 cnp->cn_proc);
2001 if (error != 0)
2002 break;
2003 }
2004 if (ndp->ni_pathlen > 1)
2005 cp = PNBUF_GET();
2006 else
2007 cp = cnp->cn_pnbuf;
2008 aiov.iov_base = cp;
2009 aiov.iov_len = MAXPATHLEN;
2010 auio.uio_iov = &aiov;
2011 auio.uio_iovcnt = 1;
2012 auio.uio_offset = 0;
2013 auio.uio_rw = UIO_READ;
2014 auio.uio_segflg = UIO_SYSSPACE;
2015 auio.uio_procp = (struct proc *)0;
2016 auio.uio_resid = MAXPATHLEN;
2017 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
2018 if (error) {
2019 badlink:
2020 if (ndp->ni_pathlen > 1)
2021 PNBUF_PUT(cp);
2022 break;
2023 }
2024 linklen = MAXPATHLEN - auio.uio_resid;
2025 if (linklen == 0) {
2026 error = ENOENT;
2027 goto badlink;
2028 }
2029 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2030 error = ENAMETOOLONG;
2031 goto badlink;
2032 }
2033 if (ndp->ni_pathlen > 1) {
2034 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
2035 PNBUF_PUT(cnp->cn_pnbuf);
2036 cnp->cn_pnbuf = cp;
2037 } else
2038 cnp->cn_pnbuf[linklen] = '\0';
2039 ndp->ni_pathlen += linklen;
2040 vput(ndp->ni_vp);
2041 dp = ndp->ni_dvp;
2042 /*
2043 * Check if root directory should replace current directory.
2044 */
2045 if (cnp->cn_pnbuf[0] == '/') {
2046 vrele(dp);
2047 dp = ndp->ni_rootdir;
2048 VREF(dp);
2049 }
2050 }
2051 }
2052 vrele(ndp->ni_dvp);
2053 vput(ndp->ni_vp);
2054 ndp->ni_vp = NULL;
2055 out:
2056 PNBUF_PUT(cnp->cn_pnbuf);
2057 return (error);
2058 }
2059
2060 /*
2061 * A fiddled version of m_adj() that ensures null fill to a long
2062 * boundary and only trims off the back end
2063 */
2064 void
2065 nfsm_adj(mp, len, nul)
2066 struct mbuf *mp;
2067 int len;
2068 int nul;
2069 {
2070 struct mbuf *m;
2071 int count, i;
2072 char *cp;
2073
2074 /*
2075 * Trim from tail. Scan the mbuf chain,
2076 * calculating its length and finding the last mbuf.
2077 * If the adjustment only affects this mbuf, then just
2078 * adjust and return. Otherwise, rescan and truncate
2079 * after the remaining size.
2080 */
2081 count = 0;
2082 m = mp;
2083 for (;;) {
2084 count += m->m_len;
2085 if (m->m_next == (struct mbuf *)0)
2086 break;
2087 m = m->m_next;
2088 }
2089 if (m->m_len > len) {
2090 m->m_len -= len;
2091 if (nul > 0) {
2092 cp = mtod(m, caddr_t)+m->m_len-nul;
2093 for (i = 0; i < nul; i++)
2094 *cp++ = '\0';
2095 }
2096 return;
2097 }
2098 count -= len;
2099 if (count < 0)
2100 count = 0;
2101 /*
2102 * Correct length for chain is "count".
2103 * Find the mbuf with last data, adjust its length,
2104 * and toss data from remaining mbufs on chain.
2105 */
2106 for (m = mp; m; m = m->m_next) {
2107 if (m->m_len >= count) {
2108 m->m_len = count;
2109 if (nul > 0) {
2110 cp = mtod(m, caddr_t)+m->m_len-nul;
2111 for (i = 0; i < nul; i++)
2112 *cp++ = '\0';
2113 }
2114 break;
2115 }
2116 count -= m->m_len;
2117 }
2118 for (m = m->m_next;m;m = m->m_next)
2119 m->m_len = 0;
2120 }
2121
2122 /*
2123 * Make these functions instead of macros, so that the kernel text size
2124 * doesn't get too big...
2125 */
2126 void
2127 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2128 struct nfsrv_descript *nfsd;
2129 int before_ret;
2130 struct vattr *before_vap;
2131 int after_ret;
2132 struct vattr *after_vap;
2133 struct mbuf **mbp;
2134 char **bposp;
2135 {
2136 struct mbuf *mb = *mbp, *mb2;
2137 char *bpos = *bposp;
2138 u_int32_t *tl;
2139
2140 if (before_ret) {
2141 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2142 *tl = nfs_false;
2143 } else {
2144 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2145 *tl++ = nfs_true;
2146 txdr_hyper(before_vap->va_size, tl);
2147 tl += 2;
2148 txdr_nfsv3time(&(before_vap->va_mtime), tl);
2149 tl += 2;
2150 txdr_nfsv3time(&(before_vap->va_ctime), tl);
2151 }
2152 *bposp = bpos;
2153 *mbp = mb;
2154 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2155 }
2156
2157 void
2158 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2159 struct nfsrv_descript *nfsd;
2160 int after_ret;
2161 struct vattr *after_vap;
2162 struct mbuf **mbp;
2163 char **bposp;
2164 {
2165 struct mbuf *mb = *mbp, *mb2;
2166 char *bpos = *bposp;
2167 u_int32_t *tl;
2168 struct nfs_fattr *fp;
2169
2170 if (after_ret) {
2171 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2172 *tl = nfs_false;
2173 } else {
2174 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
2175 *tl++ = nfs_true;
2176 fp = (struct nfs_fattr *)tl;
2177 nfsm_srvfattr(nfsd, after_vap, fp);
2178 }
2179 *mbp = mb;
2180 *bposp = bpos;
2181 }
2182
2183 void
2184 nfsm_srvfattr(nfsd, vap, fp)
2185 struct nfsrv_descript *nfsd;
2186 struct vattr *vap;
2187 struct nfs_fattr *fp;
2188 {
2189
2190 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2191 fp->fa_uid = txdr_unsigned(vap->va_uid);
2192 fp->fa_gid = txdr_unsigned(vap->va_gid);
2193 if (nfsd->nd_flag & ND_NFSV3) {
2194 fp->fa_type = vtonfsv3_type(vap->va_type);
2195 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2196 txdr_hyper(vap->va_size, &fp->fa3_size);
2197 txdr_hyper(vap->va_bytes, &fp->fa3_used);
2198 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2199 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2200 fp->fa3_fsid.nfsuquad[0] = 0;
2201 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2202 fp->fa3_fileid.nfsuquad[0] = 0;
2203 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
2204 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
2205 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
2206 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
2207 } else {
2208 fp->fa_type = vtonfsv2_type(vap->va_type);
2209 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2210 fp->fa2_size = txdr_unsigned(vap->va_size);
2211 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
2212 if (vap->va_type == VFIFO)
2213 fp->fa2_rdev = 0xffffffff;
2214 else
2215 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2216 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
2217 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2218 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2219 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
2220 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
2221 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
2222 }
2223 }
2224
2225 /*
2226 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
2227 * - look up fsid in mount list (if not found ret error)
2228 * - get vp and export rights by calling VFS_FHTOVP()
2229 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2230 * - if not lockflag unlock it with VOP_UNLOCK()
2231 */
2232 int
2233 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
2234 fhandle_t *fhp;
2235 int lockflag;
2236 struct vnode **vpp;
2237 struct ucred *cred;
2238 struct nfssvc_sock *slp;
2239 struct mbuf *nam;
2240 int *rdonlyp;
2241 int kerbflag;
2242 {
2243 struct mount *mp;
2244 int i;
2245 struct ucred *credanon;
2246 int error, exflags;
2247 struct sockaddr_in *saddr;
2248
2249 *vpp = (struct vnode *)0;
2250
2251 if (nfs_ispublicfh(fhp)) {
2252 if (!pubflag || !nfs_pub.np_valid)
2253 return (ESTALE);
2254 fhp = &nfs_pub.np_handle;
2255 }
2256
2257 mp = vfs_getvfs(&fhp->fh_fsid);
2258 if (!mp)
2259 return (ESTALE);
2260 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
2261 if (error)
2262 return (error);
2263 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2264 if (error)
2265 return (error);
2266
2267 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
2268 saddr = mtod(nam, struct sockaddr_in *);
2269 if ((saddr->sin_family == AF_INET) &&
2270 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
2271 vput(*vpp);
2272 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2273 }
2274 #ifdef INET6
2275 if ((saddr->sin_family == AF_INET6) &&
2276 ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
2277 vput(*vpp);
2278 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2279 }
2280 #endif
2281 }
2282 /*
2283 * Check/setup credentials.
2284 */
2285 if (exflags & MNT_EXKERB) {
2286 if (!kerbflag) {
2287 vput(*vpp);
2288 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2289 }
2290 } else if (kerbflag) {
2291 vput(*vpp);
2292 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2293 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
2294 cred->cr_uid = credanon->cr_uid;
2295 cred->cr_gid = credanon->cr_gid;
2296 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
2297 cred->cr_groups[i] = credanon->cr_groups[i];
2298 cred->cr_ngroups = i;
2299 }
2300 if (exflags & MNT_EXRDONLY)
2301 *rdonlyp = 1;
2302 else
2303 *rdonlyp = 0;
2304 if (!lockflag)
2305 VOP_UNLOCK(*vpp, 0);
2306 return (0);
2307 }
2308
2309 /*
2310 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2311 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2312 * transformed this to all zeroes in both cases, so check for it.
2313 */
2314 int
2315 nfs_ispublicfh(fhp)
2316 fhandle_t *fhp;
2317 {
2318 char *cp = (char *)fhp;
2319 int i;
2320
2321 for (i = 0; i < NFSX_V3FH; i++)
2322 if (*cp++ != 0)
2323 return (FALSE);
2324 return (TRUE);
2325 }
2326
2327 /*
2328 * This function compares two net addresses by family and returns TRUE
2329 * if they are the same host.
2330 * If there is any doubt, return FALSE.
2331 * The AF_INET family is handled as a special case so that address mbufs
2332 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2333 */
2334 int
2335 netaddr_match(family, haddr, nam)
2336 int family;
2337 union nethostaddr *haddr;
2338 struct mbuf *nam;
2339 {
2340 struct sockaddr_in *inetaddr;
2341
2342 switch (family) {
2343 case AF_INET:
2344 inetaddr = mtod(nam, struct sockaddr_in *);
2345 if (inetaddr->sin_family == AF_INET &&
2346 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2347 return (1);
2348 break;
2349 #ifdef INET6
2350 case AF_INET6:
2351 {
2352 struct sockaddr_in6 *sin6_1, *sin6_2;
2353
2354 sin6_1 = mtod(nam, struct sockaddr_in6 *);
2355 sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
2356 if (sin6_1->sin6_family == AF_INET6 &&
2357 IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
2358 return 1;
2359 }
2360 #endif
2361 #ifdef ISO
2362 case AF_ISO:
2363 {
2364 struct sockaddr_iso *isoaddr1, *isoaddr2;
2365
2366 isoaddr1 = mtod(nam, struct sockaddr_iso *);
2367 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2368 if (isoaddr1->siso_family == AF_ISO &&
2369 isoaddr1->siso_nlen > 0 &&
2370 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2371 SAME_ISOADDR(isoaddr1, isoaddr2))
2372 return (1);
2373 break;
2374 }
2375 #endif /* ISO */
2376 default:
2377 break;
2378 };
2379 return (0);
2380 }
2381
2382 /*
2383 * The write verifier has changed (probably due to a server reboot), so all
2384 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2385 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2386 * flag. Once done the new write verifier can be set for the mount point.
2387 */
2388 void
2389 nfs_clearcommit(mp)
2390 struct mount *mp;
2391 {
2392 struct vnode *vp;
2393 struct nfsnode *np;
2394 struct vm_page *pg;
2395 int s;
2396
2397 s = splbio();
2398 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2399 KASSERT(vp->v_mount == mp);
2400 if (vp->v_type == VNON)
2401 continue;
2402 np = VTONFS(vp);
2403 np->n_pushlo = np->n_pushhi = np->n_pushedlo =
2404 np->n_pushedhi = 0;
2405 np->n_commitflags &=
2406 ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
2407 simple_lock(&vp->v_uobj.vmobjlock);
2408 TAILQ_FOREACH(pg, &vp->v_uobj.memq, listq) {
2409 pg->flags &= ~PG_NEEDCOMMIT;
2410 }
2411 simple_unlock(&vp->v_uobj.vmobjlock);
2412 }
2413 splx(s);
2414 }
2415
2416 void
2417 nfs_merge_commit_ranges(vp)
2418 struct vnode *vp;
2419 {
2420 struct nfsnode *np = VTONFS(vp);
2421
2422 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2423 np->n_pushedlo = np->n_pushlo;
2424 np->n_pushedhi = np->n_pushhi;
2425 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2426 } else {
2427 if (np->n_pushlo < np->n_pushedlo)
2428 np->n_pushedlo = np->n_pushlo;
2429 if (np->n_pushhi > np->n_pushedhi)
2430 np->n_pushedhi = np->n_pushhi;
2431 }
2432
2433 np->n_pushlo = np->n_pushhi = 0;
2434 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
2435
2436 #ifdef fvdl_debug
2437 printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2438 (unsigned)np->n_pushedhi);
2439 #endif
2440 }
2441
2442 int
2443 nfs_in_committed_range(vp, off, len)
2444 struct vnode *vp;
2445 off_t off, len;
2446 {
2447 struct nfsnode *np = VTONFS(vp);
2448 off_t lo, hi;
2449
2450 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2451 return 0;
2452 lo = off;
2453 hi = lo + len;
2454
2455 return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
2456 }
2457
2458 int
2459 nfs_in_tobecommitted_range(vp, off, len)
2460 struct vnode *vp;
2461 off_t off, len;
2462 {
2463 struct nfsnode *np = VTONFS(vp);
2464 off_t lo, hi;
2465
2466 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2467 return 0;
2468 lo = off;
2469 hi = lo + len;
2470
2471 return (lo >= np->n_pushlo && hi <= np->n_pushhi);
2472 }
2473
2474 void
2475 nfs_add_committed_range(vp, off, len)
2476 struct vnode *vp;
2477 off_t off, len;
2478 {
2479 struct nfsnode *np = VTONFS(vp);
2480 off_t lo, hi;
2481
2482 lo = off;
2483 hi = lo + len;
2484
2485 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2486 np->n_pushedlo = lo;
2487 np->n_pushedhi = hi;
2488 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2489 } else {
2490 if (hi > np->n_pushedhi)
2491 np->n_pushedhi = hi;
2492 if (lo < np->n_pushedlo)
2493 np->n_pushedlo = lo;
2494 }
2495 #ifdef fvdl_debug
2496 printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2497 (unsigned)np->n_pushedhi);
2498 #endif
2499 }
2500
2501 void
2502 nfs_del_committed_range(vp, off, len)
2503 struct vnode *vp;
2504 off_t off, len;
2505 {
2506 struct nfsnode *np = VTONFS(vp);
2507 off_t lo, hi;
2508
2509 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2510 return;
2511
2512 lo = off;
2513 hi = lo + len;
2514
2515 if (lo > np->n_pushedhi || hi < np->n_pushedlo)
2516 return;
2517 if (lo <= np->n_pushedlo)
2518 np->n_pushedlo = hi;
2519 else if (hi >= np->n_pushedhi)
2520 np->n_pushedhi = lo;
2521 else {
2522 /*
2523 * XXX There's only one range. If the deleted range
2524 * is in the middle, pick the largest of the
2525 * contiguous ranges that it leaves.
2526 */
2527 if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
2528 np->n_pushedhi = lo;
2529 else
2530 np->n_pushedlo = hi;
2531 }
2532 #ifdef fvdl_debug
2533 printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2534 (unsigned)np->n_pushedhi);
2535 #endif
2536 }
2537
2538 void
2539 nfs_add_tobecommitted_range(vp, off, len)
2540 struct vnode *vp;
2541 off_t off, len;
2542 {
2543 struct nfsnode *np = VTONFS(vp);
2544 off_t lo, hi;
2545
2546 lo = off;
2547 hi = lo + len;
2548
2549 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
2550 np->n_pushlo = lo;
2551 np->n_pushhi = hi;
2552 np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
2553 } else {
2554 if (lo < np->n_pushlo)
2555 np->n_pushlo = lo;
2556 if (hi > np->n_pushhi)
2557 np->n_pushhi = hi;
2558 }
2559 #ifdef fvdl_debug
2560 printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2561 (unsigned)np->n_pushhi);
2562 #endif
2563 }
2564
2565 void
2566 nfs_del_tobecommitted_range(vp, off, len)
2567 struct vnode *vp;
2568 off_t off, len;
2569 {
2570 struct nfsnode *np = VTONFS(vp);
2571 off_t lo, hi;
2572
2573 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2574 return;
2575
2576 lo = off;
2577 hi = lo + len;
2578
2579 if (lo > np->n_pushhi || hi < np->n_pushlo)
2580 return;
2581
2582 if (lo <= np->n_pushlo)
2583 np->n_pushlo = hi;
2584 else if (hi >= np->n_pushhi)
2585 np->n_pushhi = lo;
2586 else {
2587 /*
2588 * XXX There's only one range. If the deleted range
2589 * is in the middle, pick the largest of the
2590 * contiguous ranges that it leaves.
2591 */
2592 if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
2593 np->n_pushhi = lo;
2594 else
2595 np->n_pushlo = hi;
2596 }
2597 #ifdef fvdl_debug
2598 printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2599 (unsigned)np->n_pushhi);
2600 #endif
2601 }
2602
2603 /*
2604 * Map errnos to NFS error numbers. For Version 3 also filter out error
2605 * numbers not specified for the associated procedure.
2606 */
2607 int
2608 nfsrv_errmap(nd, err)
2609 struct nfsrv_descript *nd;
2610 int err;
2611 {
2612 const short *defaulterrp, *errp;
2613
2614 if (nd->nd_flag & ND_NFSV3) {
2615 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2616 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2617 while (*++errp) {
2618 if (*errp == err)
2619 return (err);
2620 else if (*errp > err)
2621 break;
2622 }
2623 return ((int)*defaulterrp);
2624 } else
2625 return (err & 0xffff);
2626 }
2627 if (err <= ELAST)
2628 return ((int)nfsrv_v2errmap[err - 1]);
2629 return (NFSERR_IO);
2630 }
2631
2632 /*
2633 * Sort the group list in increasing numerical order.
2634 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2635 * that used to be here.)
2636 */
2637 void
2638 nfsrvw_sort(list, num)
2639 gid_t *list;
2640 int num;
2641 {
2642 int i, j;
2643 gid_t v;
2644
2645 /* Insertion sort. */
2646 for (i = 1; i < num; i++) {
2647 v = list[i];
2648 /* find correct slot for value v, moving others up */
2649 for (j = i; --j >= 0 && v < list[j];)
2650 list[j + 1] = list[j];
2651 list[j + 1] = v;
2652 }
2653 }
2654
2655 /*
2656 * copy credentials making sure that the result can be compared with memcmp().
2657 */
2658 void
2659 nfsrv_setcred(incred, outcred)
2660 struct ucred *incred, *outcred;
2661 {
2662 int i;
2663
2664 memset((caddr_t)outcred, 0, sizeof (struct ucred));
2665 outcred->cr_ref = 1;
2666 outcred->cr_uid = incred->cr_uid;
2667 outcred->cr_gid = incred->cr_gid;
2668 outcred->cr_ngroups = incred->cr_ngroups;
2669 for (i = 0; i < incred->cr_ngroups; i++)
2670 outcred->cr_groups[i] = incred->cr_groups[i];
2671 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2672 }
2673