xref: /openbsd/sys/nfs/nfs_subs.c (revision 133306f0)
1 /*	$OpenBSD: nfs_subs.c,v 1.29 2000/06/26 22:48:15 art Exp $	*/
2 /*	$NetBSD: nfs_subs.c,v 1.27.4.3 1996/07/08 20:34:24 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
40  */
41 
42 
43 /*
44  * These functions support the macros and help fiddle mbuf chains for
45  * the nfs op functions. They do things like create the rpc header and
46  * copy data between mbuf chains and uio lists.
47  */
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/stat.h>
59 #include <sys/malloc.h>
60 #include <sys/time.h>
61 
62 #include <vm/vm.h>
63 
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfsnode.h>
67 #include <nfs/nfs.h>
68 #include <nfs/xdr_subs.h>
69 #include <nfs/nfsm_subs.h>
70 #include <nfs/nfsmount.h>
71 #include <nfs/nqnfs.h>
72 #include <nfs/nfsrtt.h>
73 #include <nfs/nfs_var.h>
74 
75 #include <miscfs/specfs/specdev.h>
76 
77 #include <vm/vm.h>
78 
79 #include <netinet/in.h>
80 #ifdef ISO
81 #include <netiso/iso.h>
82 #endif
83 
84 #include <dev/rndvar.h>
85 
86 #ifdef __GNUC__
87 #define INLINE __inline
88 #else
89 #define INLINE
90 #endif
91 
92 int	nfs_attrtimeo __P((struct nfsnode *np));
93 
94 /*
95  * Data items converted to xdr at startup, since they are constant
96  * This is kinda hokey, but may save a little time doing byte swaps
97  */
98 u_int32_t nfs_xdrneg1;
99 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
100 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
101 	rpc_auth_kerb;
102 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
103 
104 /* And other global data */
105 static u_int32_t nfs_xid = 0;
106 static u_int32_t nfs_xid_touched = 0;
107 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
108 		      NFCHR, NFNON };
109 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
110 		      NFFIFO, NFNON };
111 enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
112 enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
113 int nfs_ticks;
114 
115 /*
116  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
117  */
118 int nfsv3_procid[NFS_NPROCS] = {
119 	NFSPROC_NULL,
120 	NFSPROC_GETATTR,
121 	NFSPROC_SETATTR,
122 	NFSPROC_NOOP,
123 	NFSPROC_LOOKUP,
124 	NFSPROC_READLINK,
125 	NFSPROC_READ,
126 	NFSPROC_NOOP,
127 	NFSPROC_WRITE,
128 	NFSPROC_CREATE,
129 	NFSPROC_REMOVE,
130 	NFSPROC_RENAME,
131 	NFSPROC_LINK,
132 	NFSPROC_SYMLINK,
133 	NFSPROC_MKDIR,
134 	NFSPROC_RMDIR,
135 	NFSPROC_READDIR,
136 	NFSPROC_FSSTAT,
137 	NFSPROC_NOOP,
138 	NFSPROC_NOOP,
139 	NFSPROC_NOOP,
140 	NFSPROC_NOOP,
141 	NFSPROC_NOOP,
142 	NFSPROC_NOOP,
143 	NFSPROC_NOOP,
144 	NFSPROC_NOOP
145 };
146 
147 /*
148  * and the reverse mapping from generic to Version 2 procedure numbers
149  */
150 int nfsv2_procid[NFS_NPROCS] = {
151 	NFSV2PROC_NULL,
152 	NFSV2PROC_GETATTR,
153 	NFSV2PROC_SETATTR,
154 	NFSV2PROC_LOOKUP,
155 	NFSV2PROC_NOOP,
156 	NFSV2PROC_READLINK,
157 	NFSV2PROC_READ,
158 	NFSV2PROC_WRITE,
159 	NFSV2PROC_CREATE,
160 	NFSV2PROC_MKDIR,
161 	NFSV2PROC_SYMLINK,
162 	NFSV2PROC_CREATE,
163 	NFSV2PROC_REMOVE,
164 	NFSV2PROC_RMDIR,
165 	NFSV2PROC_RENAME,
166 	NFSV2PROC_LINK,
167 	NFSV2PROC_READDIR,
168 	NFSV2PROC_NOOP,
169 	NFSV2PROC_STATFS,
170 	NFSV2PROC_NOOP,
171 	NFSV2PROC_NOOP,
172 	NFSV2PROC_NOOP,
173 	NFSV2PROC_NOOP,
174 	NFSV2PROC_NOOP,
175 	NFSV2PROC_NOOP,
176 	NFSV2PROC_NOOP,
177 };
178 
179 /*
180  * Maps errno values to nfs error numbers.
181  * Use NFSERR_IO as the catch all for ones not specifically defined in
182  * RFC 1094.
183  */
184 static u_char nfsrv_v2errmap[ELAST] = {
185   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
186   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
187   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
188   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
189   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
190   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
191   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
192   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
193   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
194   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
195   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
196   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
197   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
198   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
199   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
200   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
201   NFSERR_IO,
202 };
203 
204 /*
205  * Maps errno values to nfs error numbers.
206  * Although it is not obvious whether or not NFS clients really care if
207  * a returned error value is in the specified list for the procedure, the
208  * safest thing to do is filter them appropriately. For Version 2, the
209  * X/Open XNFS document is the only specification that defines error values
210  * for each RPC (The RFC simply lists all possible error values for all RPCs),
211  * so I have decided to not do this for Version 2.
212  * The first entry is the default error return and the rest are the valid
213  * errors for that RPC in increasing numeric order.
214  */
215 static short nfsv3err_null[] = {
216 	0,
217 	0,
218 };
219 
220 static short nfsv3err_getattr[] = {
221 	NFSERR_IO,
222 	NFSERR_IO,
223 	NFSERR_STALE,
224 	NFSERR_BADHANDLE,
225 	NFSERR_SERVERFAULT,
226 	0,
227 };
228 
229 static short nfsv3err_setattr[] = {
230 	NFSERR_IO,
231 	NFSERR_PERM,
232 	NFSERR_IO,
233 	NFSERR_ACCES,
234 	NFSERR_INVAL,
235 	NFSERR_NOSPC,
236 	NFSERR_ROFS,
237 	NFSERR_DQUOT,
238 	NFSERR_STALE,
239 	NFSERR_BADHANDLE,
240 	NFSERR_NOT_SYNC,
241 	NFSERR_SERVERFAULT,
242 	0,
243 };
244 
245 static short nfsv3err_lookup[] = {
246 	NFSERR_IO,
247 	NFSERR_NOENT,
248 	NFSERR_IO,
249 	NFSERR_ACCES,
250 	NFSERR_NOTDIR,
251 	NFSERR_NAMETOL,
252 	NFSERR_STALE,
253 	NFSERR_BADHANDLE,
254 	NFSERR_SERVERFAULT,
255 	0,
256 };
257 
258 static short nfsv3err_access[] = {
259 	NFSERR_IO,
260 	NFSERR_IO,
261 	NFSERR_STALE,
262 	NFSERR_BADHANDLE,
263 	NFSERR_SERVERFAULT,
264 	0,
265 };
266 
267 static short nfsv3err_readlink[] = {
268 	NFSERR_IO,
269 	NFSERR_IO,
270 	NFSERR_ACCES,
271 	NFSERR_INVAL,
272 	NFSERR_STALE,
273 	NFSERR_BADHANDLE,
274 	NFSERR_NOTSUPP,
275 	NFSERR_SERVERFAULT,
276 	0,
277 };
278 
279 static short nfsv3err_read[] = {
280 	NFSERR_IO,
281 	NFSERR_IO,
282 	NFSERR_NXIO,
283 	NFSERR_ACCES,
284 	NFSERR_INVAL,
285 	NFSERR_STALE,
286 	NFSERR_BADHANDLE,
287 	NFSERR_SERVERFAULT,
288 	0,
289 };
290 
291 static short nfsv3err_write[] = {
292 	NFSERR_IO,
293 	NFSERR_IO,
294 	NFSERR_ACCES,
295 	NFSERR_INVAL,
296 	NFSERR_FBIG,
297 	NFSERR_NOSPC,
298 	NFSERR_ROFS,
299 	NFSERR_DQUOT,
300 	NFSERR_STALE,
301 	NFSERR_BADHANDLE,
302 	NFSERR_SERVERFAULT,
303 	0,
304 };
305 
306 static short nfsv3err_create[] = {
307 	NFSERR_IO,
308 	NFSERR_IO,
309 	NFSERR_ACCES,
310 	NFSERR_EXIST,
311 	NFSERR_NOTDIR,
312 	NFSERR_NOSPC,
313 	NFSERR_ROFS,
314 	NFSERR_NAMETOL,
315 	NFSERR_DQUOT,
316 	NFSERR_STALE,
317 	NFSERR_BADHANDLE,
318 	NFSERR_NOTSUPP,
319 	NFSERR_SERVERFAULT,
320 	0,
321 };
322 
323 static short nfsv3err_mkdir[] = {
324 	NFSERR_IO,
325 	NFSERR_IO,
326 	NFSERR_ACCES,
327 	NFSERR_EXIST,
328 	NFSERR_NOTDIR,
329 	NFSERR_NOSPC,
330 	NFSERR_ROFS,
331 	NFSERR_NAMETOL,
332 	NFSERR_DQUOT,
333 	NFSERR_STALE,
334 	NFSERR_BADHANDLE,
335 	NFSERR_NOTSUPP,
336 	NFSERR_SERVERFAULT,
337 	0,
338 };
339 
340 static short nfsv3err_symlink[] = {
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 short nfsv3err_mknod[] = {
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 	NFSERR_BADTYPE,
372 	0,
373 };
374 
375 static short nfsv3err_remove[] = {
376 	NFSERR_IO,
377 	NFSERR_NOENT,
378 	NFSERR_IO,
379 	NFSERR_ACCES,
380 	NFSERR_NOTDIR,
381 	NFSERR_ROFS,
382 	NFSERR_NAMETOL,
383 	NFSERR_STALE,
384 	NFSERR_BADHANDLE,
385 	NFSERR_SERVERFAULT,
386 	0,
387 };
388 
389 static short nfsv3err_rmdir[] = {
390 	NFSERR_IO,
391 	NFSERR_NOENT,
392 	NFSERR_IO,
393 	NFSERR_ACCES,
394 	NFSERR_EXIST,
395 	NFSERR_NOTDIR,
396 	NFSERR_INVAL,
397 	NFSERR_ROFS,
398 	NFSERR_NAMETOL,
399 	NFSERR_NOTEMPTY,
400 	NFSERR_STALE,
401 	NFSERR_BADHANDLE,
402 	NFSERR_NOTSUPP,
403 	NFSERR_SERVERFAULT,
404 	0,
405 };
406 
407 static short nfsv3err_rename[] = {
408 	NFSERR_IO,
409 	NFSERR_NOENT,
410 	NFSERR_IO,
411 	NFSERR_ACCES,
412 	NFSERR_EXIST,
413 	NFSERR_XDEV,
414 	NFSERR_NOTDIR,
415 	NFSERR_ISDIR,
416 	NFSERR_INVAL,
417 	NFSERR_NOSPC,
418 	NFSERR_ROFS,
419 	NFSERR_MLINK,
420 	NFSERR_NAMETOL,
421 	NFSERR_NOTEMPTY,
422 	NFSERR_DQUOT,
423 	NFSERR_STALE,
424 	NFSERR_BADHANDLE,
425 	NFSERR_NOTSUPP,
426 	NFSERR_SERVERFAULT,
427 	0,
428 };
429 
430 static short nfsv3err_link[] = {
431 	NFSERR_IO,
432 	NFSERR_IO,
433 	NFSERR_ACCES,
434 	NFSERR_EXIST,
435 	NFSERR_XDEV,
436 	NFSERR_NOTDIR,
437 	NFSERR_INVAL,
438 	NFSERR_NOSPC,
439 	NFSERR_ROFS,
440 	NFSERR_MLINK,
441 	NFSERR_NAMETOL,
442 	NFSERR_DQUOT,
443 	NFSERR_STALE,
444 	NFSERR_BADHANDLE,
445 	NFSERR_NOTSUPP,
446 	NFSERR_SERVERFAULT,
447 	0,
448 };
449 
450 static short nfsv3err_readdir[] = {
451 	NFSERR_IO,
452 	NFSERR_IO,
453 	NFSERR_ACCES,
454 	NFSERR_NOTDIR,
455 	NFSERR_STALE,
456 	NFSERR_BADHANDLE,
457 	NFSERR_BAD_COOKIE,
458 	NFSERR_TOOSMALL,
459 	NFSERR_SERVERFAULT,
460 	0,
461 };
462 
463 static short nfsv3err_readdirplus[] = {
464 	NFSERR_IO,
465 	NFSERR_IO,
466 	NFSERR_ACCES,
467 	NFSERR_NOTDIR,
468 	NFSERR_STALE,
469 	NFSERR_BADHANDLE,
470 	NFSERR_BAD_COOKIE,
471 	NFSERR_NOTSUPP,
472 	NFSERR_TOOSMALL,
473 	NFSERR_SERVERFAULT,
474 	0,
475 };
476 
477 static short nfsv3err_fsstat[] = {
478 	NFSERR_IO,
479 	NFSERR_IO,
480 	NFSERR_STALE,
481 	NFSERR_BADHANDLE,
482 	NFSERR_SERVERFAULT,
483 	0,
484 };
485 
486 static short nfsv3err_fsinfo[] = {
487 	NFSERR_STALE,
488 	NFSERR_STALE,
489 	NFSERR_BADHANDLE,
490 	NFSERR_SERVERFAULT,
491 	0,
492 };
493 
494 static short nfsv3err_pathconf[] = {
495 	NFSERR_STALE,
496 	NFSERR_STALE,
497 	NFSERR_BADHANDLE,
498 	NFSERR_SERVERFAULT,
499 	0,
500 };
501 
502 static short nfsv3err_commit[] = {
503 	NFSERR_IO,
504 	NFSERR_IO,
505 	NFSERR_STALE,
506 	NFSERR_BADHANDLE,
507 	NFSERR_SERVERFAULT,
508 	0,
509 };
510 
511 static short *nfsrv_v3errmap[] = {
512 	nfsv3err_null,
513 	nfsv3err_getattr,
514 	nfsv3err_setattr,
515 	nfsv3err_lookup,
516 	nfsv3err_access,
517 	nfsv3err_readlink,
518 	nfsv3err_read,
519 	nfsv3err_write,
520 	nfsv3err_create,
521 	nfsv3err_mkdir,
522 	nfsv3err_symlink,
523 	nfsv3err_mknod,
524 	nfsv3err_remove,
525 	nfsv3err_rmdir,
526 	nfsv3err_rename,
527 	nfsv3err_link,
528 	nfsv3err_readdir,
529 	nfsv3err_readdirplus,
530 	nfsv3err_fsstat,
531 	nfsv3err_fsinfo,
532 	nfsv3err_pathconf,
533 	nfsv3err_commit,
534 };
535 
536 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
537 extern struct nfsrtt nfsrtt;
538 extern time_t nqnfsstarttime;
539 extern int nqsrv_clockskew;
540 extern int nqsrv_writeslack;
541 extern int nqsrv_maxlease;
542 extern struct nfsstats nfsstats;
543 extern int nqnfs_piggy[NFS_NPROCS];
544 extern nfstype nfsv2_type[9];
545 extern nfstype nfsv3_type[9];
546 extern struct nfsnodehashhead *nfsnodehashtbl;
547 extern u_long nfsnodehash;
548 
549 LIST_HEAD(nfsnodehashhead, nfsnode);
550 
551 /*
552  * Create the header for an rpc request packet
553  * The hsiz is the size of the rest of the nfs request header.
554  * (just used to decide if a cluster is a good idea)
555  */
556 struct mbuf *
557 nfsm_reqh(vp, procid, hsiz, bposp)
558 	struct vnode *vp;
559 	u_long procid;
560 	int hsiz;
561 	caddr_t *bposp;
562 {
563 	register struct mbuf *mb;
564 	register u_int32_t *tl;
565 	register caddr_t bpos;
566 	struct mbuf *mb2;
567 	struct nfsmount *nmp;
568 	int nqflag;
569 
570 	MGET(mb, M_WAIT, MT_DATA);
571 	if (hsiz >= MINCLSIZE)
572 		MCLGET(mb, M_WAIT);
573 	mb->m_len = 0;
574 	bpos = mtod(mb, caddr_t);
575 
576 	/*
577 	 * For NQNFS, add lease request.
578 	 */
579 	if (vp) {
580 		nmp = VFSTONFS(vp->v_mount);
581 		if (nmp->nm_flag & NFSMNT_NQNFS) {
582 			nqflag = NQNFS_NEEDLEASE(vp, procid);
583 			if (nqflag) {
584 				nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
585 				*tl++ = txdr_unsigned(nqflag);
586 				*tl = txdr_unsigned(nmp->nm_leaseterm);
587 			} else {
588 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
589 				*tl = 0;
590 			}
591 		}
592 	}
593 	/* Finally, return values */
594 	*bposp = bpos;
595 	return (mb);
596 }
597 
598 /*
599  * Build the RPC header and fill in the authorization info.
600  * The authorization string argument is only used when the credentials
601  * come from outside of the kernel.
602  * Returns the head of the mbuf list.
603  */
604 struct mbuf *
605 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
606 	verf_str, mrest, mrest_len, mbp, xidp)
607 	register struct ucred *cr;
608 	int nmflag;
609 	int procid;
610 	int auth_type;
611 	int auth_len;
612 	char *auth_str;
613 	int verf_len;
614 	char *verf_str;
615 	struct mbuf *mrest;
616 	int mrest_len;
617 	struct mbuf **mbp;
618 	u_int32_t *xidp;
619 {
620 	register struct mbuf *mb;
621 	register u_int32_t *tl;
622 	register caddr_t bpos;
623 	register int i;
624 	struct mbuf *mreq, *mb2;
625 	int siz, grpsiz, authsiz;
626 
627 	authsiz = nfsm_rndup(auth_len);
628 	MGETHDR(mb, M_WAIT, MT_DATA);
629 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
630 		MCLGET(mb, M_WAIT);
631 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
632 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
633 	} else {
634 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
635 	}
636 	mb->m_len = 0;
637 	mreq = mb;
638 	bpos = mtod(mb, caddr_t);
639 
640 	/*
641 	 * First the RPC header.
642 	 */
643 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
644 
645 	/* Get a new (non-zero) xid */
646 
647 	if ((nfs_xid == 0) && (nfs_xid_touched == 0)) {
648 		nfs_xid = arc4random();
649 		nfs_xid_touched = 1;
650 	} else {
651 		while ((*xidp = arc4random() % 256) == 0)
652 			;
653 		nfs_xid += *xidp;
654 	}
655 
656 	*tl++ = *xidp = txdr_unsigned(nfs_xid);
657 	*tl++ = rpc_call;
658 	*tl++ = rpc_vers;
659 	if (nmflag & NFSMNT_NQNFS) {
660 		*tl++ = txdr_unsigned(NQNFS_PROG);
661 		*tl++ = txdr_unsigned(NQNFS_VER3);
662 	} else {
663 		*tl++ = txdr_unsigned(NFS_PROG);
664 		if (nmflag & NFSMNT_NFSV3)
665 			*tl++ = txdr_unsigned(NFS_VER3);
666 		else
667 			*tl++ = txdr_unsigned(NFS_VER2);
668 	}
669 	if (nmflag & NFSMNT_NFSV3)
670 		*tl++ = txdr_unsigned(procid);
671 	else
672 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
673 
674 	/*
675 	 * And then the authorization cred.
676 	 */
677 	*tl++ = txdr_unsigned(auth_type);
678 	*tl = txdr_unsigned(authsiz);
679 	switch (auth_type) {
680 	case RPCAUTH_UNIX:
681 		nfsm_build(tl, u_int32_t *, auth_len);
682 		*tl++ = 0;		/* stamp ?? */
683 		*tl++ = 0;		/* NULL hostname */
684 		*tl++ = txdr_unsigned(cr->cr_uid);
685 		*tl++ = txdr_unsigned(cr->cr_gid);
686 		grpsiz = (auth_len >> 2) - 5;
687 		*tl++ = txdr_unsigned(grpsiz);
688 		for (i = 0; i < grpsiz; i++)
689 			*tl++ = txdr_unsigned(cr->cr_groups[i]);
690 		break;
691 	case RPCAUTH_KERB4:
692 		siz = auth_len;
693 		while (siz > 0) {
694 			if (M_TRAILINGSPACE(mb) == 0) {
695 				MGET(mb2, M_WAIT, MT_DATA);
696 				if (siz >= MINCLSIZE)
697 					MCLGET(mb2, M_WAIT);
698 				mb->m_next = mb2;
699 				mb = mb2;
700 				mb->m_len = 0;
701 				bpos = mtod(mb, caddr_t);
702 			}
703 			i = min(siz, M_TRAILINGSPACE(mb));
704 			bcopy(auth_str, bpos, i);
705 			mb->m_len += i;
706 			auth_str += i;
707 			bpos += i;
708 			siz -= i;
709 		}
710 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
711 			for (i = 0; i < siz; i++)
712 				*bpos++ = '\0';
713 			mb->m_len += siz;
714 		}
715 		break;
716 	};
717 
718 	/*
719 	 * And the verifier...
720 	 */
721 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
722 	if (verf_str) {
723 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
724 		*tl = txdr_unsigned(verf_len);
725 		siz = verf_len;
726 		while (siz > 0) {
727 			if (M_TRAILINGSPACE(mb) == 0) {
728 				MGET(mb2, M_WAIT, MT_DATA);
729 				if (siz >= MINCLSIZE)
730 					MCLGET(mb2, M_WAIT);
731 				mb->m_next = mb2;
732 				mb = mb2;
733 				mb->m_len = 0;
734 				bpos = mtod(mb, caddr_t);
735 			}
736 			i = min(siz, M_TRAILINGSPACE(mb));
737 			bcopy(verf_str, bpos, i);
738 			mb->m_len += i;
739 			verf_str += i;
740 			bpos += i;
741 			siz -= i;
742 		}
743 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
744 			for (i = 0; i < siz; i++)
745 				*bpos++ = '\0';
746 			mb->m_len += siz;
747 		}
748 	} else {
749 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
750 		*tl = 0;
751 	}
752 	mb->m_next = mrest;
753 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
754 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
755 	*mbp = mb;
756 	return (mreq);
757 }
758 
759 /*
760  * copies mbuf chain to the uio scatter/gather list
761  */
762 int
763 nfsm_mbuftouio(mrep, uiop, siz, dpos)
764 	struct mbuf **mrep;
765 	register struct uio *uiop;
766 	int siz;
767 	caddr_t *dpos;
768 {
769 	register char *mbufcp, *uiocp;
770 	register int xfer, left, len;
771 	register struct mbuf *mp;
772 	long uiosiz, rem;
773 	int error = 0;
774 
775 	mp = *mrep;
776 	mbufcp = *dpos;
777 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
778 	rem = nfsm_rndup(siz)-siz;
779 	while (siz > 0) {
780 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
781 			return (EFBIG);
782 		left = uiop->uio_iov->iov_len;
783 		uiocp = uiop->uio_iov->iov_base;
784 		if (left > siz)
785 			left = siz;
786 		uiosiz = left;
787 		while (left > 0) {
788 			while (len == 0) {
789 				mp = mp->m_next;
790 				if (mp == NULL)
791 					return (EBADRPC);
792 				mbufcp = mtod(mp, caddr_t);
793 				len = mp->m_len;
794 			}
795 			xfer = (left > len) ? len : left;
796 #ifdef notdef
797 			/* Not Yet.. */
798 			if (uiop->uio_iov->iov_op != NULL)
799 				(*(uiop->uio_iov->iov_op))
800 				(mbufcp, uiocp, xfer);
801 			else
802 #endif
803 			if (uiop->uio_segflg == UIO_SYSSPACE)
804 				bcopy(mbufcp, uiocp, xfer);
805 			else
806 				copyout(mbufcp, uiocp, xfer);
807 			left -= xfer;
808 			len -= xfer;
809 			mbufcp += xfer;
810 			uiocp += xfer;
811 			uiop->uio_offset += xfer;
812 			uiop->uio_resid -= xfer;
813 		}
814 		if (uiop->uio_iov->iov_len <= siz) {
815 			uiop->uio_iovcnt--;
816 			uiop->uio_iov++;
817 		} else {
818 			uiop->uio_iov->iov_base += uiosiz;
819 			uiop->uio_iov->iov_len -= uiosiz;
820 		}
821 		siz -= uiosiz;
822 	}
823 	*dpos = mbufcp;
824 	*mrep = mp;
825 	if (rem > 0) {
826 		if (len < rem)
827 			error = nfs_adv(mrep, dpos, rem, len);
828 		else
829 			*dpos += rem;
830 	}
831 	return (error);
832 }
833 
834 /*
835  * copies a uio scatter/gather list to an mbuf chain.
836  * NOTE: can ony handle iovcnt == 1
837  */
838 int
839 nfsm_uiotombuf(uiop, mq, siz, bpos)
840 	register struct uio *uiop;
841 	struct mbuf **mq;
842 	int siz;
843 	caddr_t *bpos;
844 {
845 	register char *uiocp;
846 	register struct mbuf *mp, *mp2;
847 	register int xfer, left, mlen;
848 	int uiosiz, clflg, rem;
849 	char *cp;
850 
851 #ifdef DIAGNOSTIC
852 	if (uiop->uio_iovcnt != 1)
853 		panic("nfsm_uiotombuf: iovcnt != 1");
854 #endif
855 
856 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
857 		clflg = 1;
858 	else
859 		clflg = 0;
860 	rem = nfsm_rndup(siz)-siz;
861 	mp = mp2 = *mq;
862 	while (siz > 0) {
863 		left = uiop->uio_iov->iov_len;
864 		uiocp = uiop->uio_iov->iov_base;
865 		if (left > siz)
866 			left = siz;
867 		uiosiz = left;
868 		while (left > 0) {
869 			mlen = M_TRAILINGSPACE(mp);
870 			if (mlen == 0) {
871 				MGET(mp, M_WAIT, MT_DATA);
872 				if (clflg)
873 					MCLGET(mp, M_WAIT);
874 				mp->m_len = 0;
875 				mp2->m_next = mp;
876 				mp2 = mp;
877 				mlen = M_TRAILINGSPACE(mp);
878 			}
879 			xfer = (left > mlen) ? mlen : left;
880 #ifdef notdef
881 			/* Not Yet.. */
882 			if (uiop->uio_iov->iov_op != NULL)
883 				(*(uiop->uio_iov->iov_op))
884 				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
885 			else
886 #endif
887 			if (uiop->uio_segflg == UIO_SYSSPACE)
888 				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
889 			else
890 				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
891 			mp->m_len += xfer;
892 			left -= xfer;
893 			uiocp += xfer;
894 			uiop->uio_offset += xfer;
895 			uiop->uio_resid -= xfer;
896 		}
897 		uiop->uio_iov->iov_base += uiosiz;
898 		uiop->uio_iov->iov_len -= uiosiz;
899 		siz -= uiosiz;
900 	}
901 	if (rem > 0) {
902 		if (rem > M_TRAILINGSPACE(mp)) {
903 			MGET(mp, M_WAIT, MT_DATA);
904 			mp->m_len = 0;
905 			mp2->m_next = mp;
906 		}
907 		cp = mtod(mp, caddr_t)+mp->m_len;
908 		for (left = 0; left < rem; left++)
909 			*cp++ = '\0';
910 		mp->m_len += rem;
911 		*bpos = cp;
912 	} else
913 		*bpos = mtod(mp, caddr_t)+mp->m_len;
914 	*mq = mp;
915 	return (0);
916 }
917 
918 /*
919  * Help break down an mbuf chain by setting the first siz bytes contiguous
920  * pointed to by returned val.
921  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
922  * cases. (The macros use the vars. dpos and dpos2)
923  */
924 int
925 nfsm_disct(mdp, dposp, siz, left, cp2)
926 	struct mbuf **mdp;
927 	caddr_t *dposp;
928 	int siz;
929 	int left;
930 	caddr_t *cp2;
931 {
932 	register struct mbuf *mp, *mp2;
933 	register int siz2, xfer;
934 	register caddr_t p;
935 
936 	mp = *mdp;
937 	while (left == 0) {
938 		*mdp = mp = mp->m_next;
939 		if (mp == NULL)
940 			return (EBADRPC);
941 		left = mp->m_len;
942 		*dposp = mtod(mp, caddr_t);
943 	}
944 	if (left >= siz) {
945 		*cp2 = *dposp;
946 		*dposp += siz;
947 	} else if (mp->m_next == NULL) {
948 		return (EBADRPC);
949 	} else if (siz > MHLEN) {
950 		panic("nfs S too big");
951 	} else {
952 		MGET(mp2, M_WAIT, MT_DATA);
953 		mp2->m_next = mp->m_next;
954 		mp->m_next = mp2;
955 		mp->m_len -= left;
956 		mp = mp2;
957 		*cp2 = p = mtod(mp, caddr_t);
958 		bcopy(*dposp, p, left);		/* Copy what was left */
959 		siz2 = siz-left;
960 		p += left;
961 		mp2 = mp->m_next;
962 		/* Loop around copying up the siz2 bytes */
963 		while (siz2 > 0) {
964 			if (mp2 == NULL)
965 				return (EBADRPC);
966 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
967 			if (xfer > 0) {
968 				bcopy(mtod(mp2, caddr_t), p, xfer);
969 				NFSMADV(mp2, xfer);
970 				mp2->m_len -= xfer;
971 				p += xfer;
972 				siz2 -= xfer;
973 			}
974 			if (siz2 > 0)
975 				mp2 = mp2->m_next;
976 		}
977 		mp->m_len = siz;
978 		*mdp = mp2;
979 		*dposp = mtod(mp2, caddr_t);
980 	}
981 	return (0);
982 }
983 
984 /*
985  * Advance the position in the mbuf chain.
986  */
987 int
988 nfs_adv(mdp, dposp, offs, left)
989 	struct mbuf **mdp;
990 	caddr_t *dposp;
991 	int offs;
992 	int left;
993 {
994 	register struct mbuf *m;
995 	register int s;
996 
997 	m = *mdp;
998 	s = left;
999 	while (s < offs) {
1000 		offs -= s;
1001 		m = m->m_next;
1002 		if (m == NULL)
1003 			return (EBADRPC);
1004 		s = m->m_len;
1005 	}
1006 	*mdp = m;
1007 	*dposp = mtod(m, caddr_t)+offs;
1008 	return (0);
1009 }
1010 
1011 /*
1012  * Copy a string into mbufs for the hard cases...
1013  */
1014 int
1015 nfsm_strtmbuf(mb, bpos, cp, siz)
1016 	struct mbuf **mb;
1017 	char **bpos;
1018 	char *cp;
1019 	long siz;
1020 {
1021 	register struct mbuf *m1 = NULL, *m2;
1022 	long left, xfer, len, tlen;
1023 	u_int32_t *tl;
1024 	int putsize;
1025 
1026 	putsize = 1;
1027 	m2 = *mb;
1028 	left = M_TRAILINGSPACE(m2);
1029 	if (left > 0) {
1030 		tl = ((u_int32_t *)(*bpos));
1031 		*tl++ = txdr_unsigned(siz);
1032 		putsize = 0;
1033 		left -= NFSX_UNSIGNED;
1034 		m2->m_len += NFSX_UNSIGNED;
1035 		if (left > 0) {
1036 			bcopy(cp, (caddr_t) tl, left);
1037 			siz -= left;
1038 			cp += left;
1039 			m2->m_len += left;
1040 			left = 0;
1041 		}
1042 	}
1043 	/* Loop around adding mbufs */
1044 	while (siz > 0) {
1045 		MGET(m1, M_WAIT, MT_DATA);
1046 		if (siz > MLEN)
1047 			MCLGET(m1, M_WAIT);
1048 		m1->m_len = NFSMSIZ(m1);
1049 		m2->m_next = m1;
1050 		m2 = m1;
1051 		tl = mtod(m1, u_int32_t *);
1052 		tlen = 0;
1053 		if (putsize) {
1054 			*tl++ = txdr_unsigned(siz);
1055 			m1->m_len -= NFSX_UNSIGNED;
1056 			tlen = NFSX_UNSIGNED;
1057 			putsize = 0;
1058 		}
1059 		if (siz < m1->m_len) {
1060 			len = nfsm_rndup(siz);
1061 			xfer = siz;
1062 			if (xfer < len)
1063 				*(tl+(xfer>>2)) = 0;
1064 		} else {
1065 			xfer = len = m1->m_len;
1066 		}
1067 		bcopy(cp, (caddr_t) tl, xfer);
1068 		m1->m_len = len+tlen;
1069 		siz -= xfer;
1070 		cp += xfer;
1071 	}
1072 	*mb = m1;
1073 	*bpos = mtod(m1, caddr_t)+m1->m_len;
1074 	return (0);
1075 }
1076 
1077 /*
1078  * Called once to initialize data structures...
1079  */
1080 void
1081 nfs_init()
1082 {
1083 	static struct timeout nfs_timer_to;
1084 
1085 #if !defined(alpha) && defined(DIAGNOSTIC)
1086 	/*
1087 	 * Check to see if major data structures haven't bloated.
1088 	 */
1089 	if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1090 		printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1091 		printf("Try reducing NFS_SMALLFH\n");
1092 	}
1093 	if (sizeof (struct nfsmount) > NFS_MNTALLOC) {
1094 		printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
1095 		printf("Try reducing NFS_MUIDHASHSIZ\n");
1096 	}
1097 	if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1098 		printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1099 		printf("Try reducing NFS_UIDHASHSIZ\n");
1100 	}
1101 	if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1102 		printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1103 		printf("Try unionizing the nu_nickname and nu_flag fields\n");
1104 	}
1105 #endif
1106 
1107 	nfsrtt.pos = 0;
1108 	rpc_vers = txdr_unsigned(RPC_VER2);
1109 	rpc_call = txdr_unsigned(RPC_CALL);
1110 	rpc_reply = txdr_unsigned(RPC_REPLY);
1111 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1112 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1113 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1114 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1115 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1116 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1117 	nfs_prog = txdr_unsigned(NFS_PROG);
1118 	nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1119 	nfs_true = txdr_unsigned(TRUE);
1120 	nfs_false = txdr_unsigned(FALSE);
1121 	nfs_xdrneg1 = txdr_unsigned(-1);
1122 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1123 	if (nfs_ticks < 1)
1124 		nfs_ticks = 1;
1125 #ifdef NFSSERVER
1126 	nfsrv_init(0);			/* Init server data structures */
1127 	nfsrv_initcache();		/* Init the server request cache */
1128 #endif /* NFSSERVER */
1129 
1130 	/*
1131 	 * Initialize the nqnfs client/server stuff.
1132 	 */
1133 	if (nqnfsstarttime == 0) {
1134 		nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1135 			+ nqsrv_clockskew + nqsrv_writeslack;
1136 		NQLOADNOVRAM(nqnfsstarttime);
1137 		CIRCLEQ_INIT(&nqtimerhead);
1138 		nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, M_WAITOK, &nqfhhash);
1139 	}
1140 
1141 	/*
1142 	 * Initialize reply list and start timer
1143 	 */
1144 	TAILQ_INIT(&nfs_reqq);
1145 
1146 	timeout_set(&nfs_timer_to, nfs_timer, &nfs_timer_to);
1147 	nfs_timer(&nfs_timer_to);
1148 }
1149 
1150 #ifdef NFSCLIENT
1151 int
1152 nfs_vfs_init(vfsp)
1153 	struct vfsconf *vfsp;
1154 {
1155 	register int i;
1156 
1157 	/* Ensure async daemons disabled */
1158 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1159 		nfs_iodwant[i] = (struct proc *)0;
1160 	TAILQ_INIT(&nfs_bufq);
1161 	nfs_nhinit();			/* Init the nfsnode table */
1162 
1163 	return (0);
1164 }
1165 
1166 /*
1167  * Attribute cache routines.
1168  * nfs_loadattrcache() - loads or updates the cache contents from attributes
1169  *	that are on the mbuf list
1170  * nfs_getattrcache() - returns valid attributes if found in cache, returns
1171  *	error otherwise
1172  */
1173 
1174 /*
1175  * Load the attribute cache (that lives in the nfsnode entry) with
1176  * the values on the mbuf list and
1177  * Iff vap not NULL
1178  *    copy the attributes to *vaper
1179  */
1180 int
1181 nfs_loadattrcache(vpp, mdp, dposp, vaper)
1182 	struct vnode **vpp;
1183 	struct mbuf **mdp;
1184 	caddr_t *dposp;
1185 	struct vattr *vaper;
1186 {
1187 	register struct vnode *vp = *vpp;
1188 	register struct vattr *vap;
1189 	register struct nfs_fattr *fp;
1190 	extern int (**spec_nfsv2nodeop_p) __P((void *));
1191 	register struct nfsnode *np;
1192 	register int32_t t1;
1193 	caddr_t cp2;
1194 	int error = 0;
1195 	int32_t rdev;
1196 	struct mbuf *md;
1197 	enum vtype vtyp;
1198 	u_short vmode;
1199 	struct timespec mtime;
1200 	struct vnode *nvp;
1201 	int v3 = NFS_ISV3(vp);
1202 
1203 	md = *mdp;
1204 	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1205 	error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1206 	if (error)
1207 		return (error);
1208 	fp = (struct nfs_fattr *)cp2;
1209 	if (v3) {
1210 		vtyp = nfsv3tov_type(fp->fa_type);
1211 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1212 		rdev = makedev(fxdr_unsigned(u_char, fp->fa3_rdev.specdata1),
1213 			fxdr_unsigned(u_char, fp->fa3_rdev.specdata2));
1214 		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1215 	} else {
1216 		vtyp = nfsv2tov_type(fp->fa_type);
1217 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1218 		if (vtyp == VNON || vtyp == VREG)
1219 			vtyp = IFTOVT(vmode);
1220 		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1221 		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1222 
1223 		/*
1224 		 * Really ugly NFSv2 kludge.
1225 		 */
1226 		if (vtyp == VCHR && rdev == 0xffffffff)
1227 			vtyp = VFIFO;
1228 	}
1229 
1230 	/*
1231 	 * If v_type == VNON it is a new node, so fill in the v_type,
1232 	 * n_mtime fields. Check to see if it represents a special
1233 	 * device, and if so, check for a possible alias. Once the
1234 	 * correct vnode has been obtained, fill in the rest of the
1235 	 * information.
1236 	 */
1237 	np = VTONFS(vp);
1238 	if (vp->v_type != vtyp) {
1239 		vp->v_type = vtyp;
1240 		if (vp->v_type == VFIFO) {
1241 #ifndef FIFO
1242 			return (EOPNOTSUPP);
1243 #else
1244 			extern int (**fifo_nfsv2nodeop_p) __P((void *));
1245 			vp->v_op = fifo_nfsv2nodeop_p;
1246 #endif /* FIFO */
1247 		}
1248 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
1249 			vp->v_op = spec_nfsv2nodeop_p;
1250 			nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1251 			if (nvp) {
1252 				/*
1253 				 * Discard unneeded vnode, but save its nfsnode.
1254 				 * Since the nfsnode does not have a lock, its
1255 				 * vnode lock has to be carried over.
1256 				 */
1257 
1258 				nvp->v_vnlock = vp->v_vnlock;
1259 				vp->v_vnlock = NULL;
1260 				nvp->v_data = vp->v_data;
1261 				vp->v_data = NULL;
1262 				vp->v_op = spec_vnodeop_p;
1263 				vrele(vp);
1264 				vgone(vp);
1265 				/*
1266 				 * Reinitialize aliased node.
1267 				 */
1268 				np->n_vnode = nvp;
1269 				*vpp = vp = nvp;
1270 			}
1271 		}
1272 		np->n_mtime = mtime.tv_sec;
1273 	}
1274 	vap = &np->n_vattr;
1275 	vap->va_type = vtyp;
1276 	vap->va_mode = (vmode & 07777);
1277 	vap->va_rdev = (dev_t)rdev;
1278 	vap->va_mtime = mtime;
1279 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1280 	if (v3) {
1281 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1282 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1283 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1284 		vap->va_size = fxdr_hyper(&fp->fa3_size);
1285 		vap->va_blocksize = NFS_FABLKSIZE;
1286 		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1287 		vap->va_fileid = fxdr_unsigned(int32_t,
1288 		    fp->fa3_fileid.nfsuquad[1]);
1289 		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1290 		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1291 		vap->va_flags = 0;
1292 		vap->va_filerev = 0;
1293 	} else {
1294 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1295 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1296 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1297 		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1298 		vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1299 		vap->va_bytes =
1300 		    (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
1301 		    NFS_FABLKSIZE;
1302 		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1303 		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1304 		vap->va_flags = 0;
1305 		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1306 		    fp->fa2_ctime.nfsv2_sec);
1307 		vap->va_ctime.tv_nsec = 0;
1308 		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1309 		vap->va_filerev = 0;
1310 	}
1311 	if (vap->va_size != np->n_size) {
1312 		if (vap->va_type == VREG) {
1313 			if (np->n_flag & NMODIFIED) {
1314 				if (vap->va_size < np->n_size)
1315 					vap->va_size = np->n_size;
1316 				else
1317 					np->n_size = vap->va_size;
1318 			} else
1319 				np->n_size = vap->va_size;
1320 #if defined(UVM)
1321 			uvm_vnp_setsize(vp, np->n_size);
1322 #else
1323 			vnode_pager_setsize(vp, (u_long)np->n_size);
1324 #endif
1325 		} else
1326 			np->n_size = vap->va_size;
1327 	}
1328 	np->n_attrstamp = time.tv_sec;
1329 	if (vaper != NULL) {
1330 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1331 		if (np->n_flag & NCHG) {
1332 			if (np->n_flag & NACC)
1333 				vaper->va_atime = np->n_atim;
1334 			if (np->n_flag & NUPD)
1335 				vaper->va_mtime = np->n_mtim;
1336 		}
1337 	}
1338 	return (0);
1339 }
1340 
1341 INLINE int
1342 nfs_attrtimeo (np)
1343 	struct nfsnode *np;
1344 {
1345 	struct vnode *vp = np->n_vnode;
1346 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1347 	int tenthage = (time.tv_sec - np->n_mtime) / 10;
1348 	int minto, maxto;
1349 
1350 	if (vp->v_type == VDIR) {
1351 		maxto = nmp->nm_acdirmax;
1352 		minto = nmp->nm_acdirmin;
1353 	}
1354 	else {
1355 		maxto = nmp->nm_acregmax;
1356 		minto = nmp->nm_acregmin;
1357 	}
1358 
1359 	if (np->n_flag & NMODIFIED || tenthage < minto)
1360 		return minto;
1361 	else if (tenthage < maxto)
1362 		return tenthage;
1363 	else
1364 		return maxto;
1365 }
1366 
1367 /*
1368  * Check the time stamp
1369  * If the cache is valid, copy contents to *vap and return 0
1370  * otherwise return an error
1371  */
1372 int
1373 nfs_getattrcache(vp, vaper)
1374 	register struct vnode *vp;
1375 	struct vattr *vaper;
1376 {
1377 	register struct nfsnode *np = VTONFS(vp);
1378 	register struct vattr *vap;
1379 
1380 	if ((time.tv_sec - np->n_attrstamp) >= nfs_attrtimeo(np)) {
1381 		nfsstats.attrcache_misses++;
1382 		return (ENOENT);
1383 	}
1384 	nfsstats.attrcache_hits++;
1385 	vap = &np->n_vattr;
1386 	if (vap->va_size != np->n_size) {
1387 		if (vap->va_type == VREG) {
1388 			if (np->n_flag & NMODIFIED) {
1389 				if (vap->va_size < np->n_size)
1390 					vap->va_size = np->n_size;
1391 				else
1392 					np->n_size = vap->va_size;
1393 			} else
1394 				np->n_size = vap->va_size;
1395 #if defined(UVM)
1396 			uvm_vnp_setsize(vp, np->n_size);
1397 #else
1398 			vnode_pager_setsize(vp, (u_long)np->n_size);
1399 #endif
1400 		} else
1401 			np->n_size = vap->va_size;
1402 	}
1403 	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1404 	if (np->n_flag & NCHG) {
1405 		if (np->n_flag & NACC)
1406 			vaper->va_atime = np->n_atim;
1407 		if (np->n_flag & NUPD)
1408 			vaper->va_mtime = np->n_mtim;
1409 	}
1410 	return (0);
1411 }
1412 #endif /* NFSCLIENT */
1413 
1414 /*
1415  * Set up nameidata for a lookup() call and do it
1416  */
1417 int
1418 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag)
1419 	register struct nameidata *ndp;
1420 	fhandle_t *fhp;
1421 	int len;
1422 	struct nfssvc_sock *slp;
1423 	struct mbuf *nam;
1424 	struct mbuf **mdp;
1425 	caddr_t *dposp;
1426 	struct vnode **retdirp;
1427 	struct proc *p;
1428 	int kerbflag;
1429 {
1430 	register int i, rem;
1431 	register struct mbuf *md;
1432 	register char *fromcp, *tocp;
1433 	struct vnode *dp;
1434 	int error, rdonly;
1435 	struct componentname *cnp = &ndp->ni_cnd;
1436 
1437 	*retdirp = (struct vnode *)0;
1438 	MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
1439 	/*
1440 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
1441 	 * and set the various ndp fields appropriately.
1442 	 */
1443 	fromcp = *dposp;
1444 	tocp = cnp->cn_pnbuf;
1445 	md = *mdp;
1446 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
1447 	cnp->cn_hash = 0;
1448 	for (i = 0; i < len; i++) {
1449 		while (rem == 0) {
1450 			md = md->m_next;
1451 			if (md == NULL) {
1452 				error = EBADRPC;
1453 				goto out;
1454 			}
1455 			fromcp = mtod(md, caddr_t);
1456 			rem = md->m_len;
1457 		}
1458 		if (*fromcp == '\0' || *fromcp == '/') {
1459 			error = EACCES;
1460 			goto out;
1461 		}
1462 		cnp->cn_hash += (u_char)*fromcp;
1463 		*tocp++ = *fromcp++;
1464 		rem--;
1465 	}
1466 	*tocp = '\0';
1467 	*mdp = md;
1468 	*dposp = fromcp;
1469 	len = nfsm_rndup(len)-len;
1470 	if (len > 0) {
1471 		if (rem >= len)
1472 			*dposp += len;
1473 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1474 			goto out;
1475 	}
1476 	ndp->ni_pathlen = tocp - cnp->cn_pnbuf;
1477 	cnp->cn_nameptr = cnp->cn_pnbuf;
1478 	/*
1479 	 * Extract and set starting directory.
1480 	 */
1481 	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1482 	    nam, &rdonly, kerbflag);
1483 	if (error)
1484 		goto out;
1485 	if (dp->v_type != VDIR) {
1486 		vrele(dp);
1487 		error = ENOTDIR;
1488 		goto out;
1489 	}
1490 	VREF(dp);
1491 	*retdirp = dp;
1492 	ndp->ni_startdir = dp;
1493 	if (rdonly)
1494 		cnp->cn_flags |= (NOCROSSMOUNT | RDONLY);
1495 	else
1496 		cnp->cn_flags |= NOCROSSMOUNT;
1497 	/*
1498 	 * And call lookup() to do the real work
1499 	 */
1500 	cnp->cn_proc = p;
1501 	error = lookup(ndp);
1502 	if (error)
1503 		goto out;
1504 	/*
1505 	 * Check for encountering a symbolic link
1506 	 */
1507 	if (cnp->cn_flags & ISSYMLINK) {
1508 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1509 			vput(ndp->ni_dvp);
1510 		else
1511 			vrele(ndp->ni_dvp);
1512 		vput(ndp->ni_vp);
1513 		ndp->ni_vp = NULL;
1514 		error = EINVAL;
1515 		goto out;
1516 	}
1517 	/*
1518 	 * Check for saved name request
1519 	 */
1520 	if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1521 		cnp->cn_flags |= HASBUF;
1522 		return (0);
1523 	}
1524 out:
1525 	FREE(cnp->cn_pnbuf, M_NAMEI);
1526 	return (error);
1527 }
1528 
1529 /*
1530  * A fiddled version of m_adj() that ensures null fill to a long
1531  * boundary and only trims off the back end
1532  */
1533 void
1534 nfsm_adj(mp, len, nul)
1535 	struct mbuf *mp;
1536 	register int len;
1537 	int nul;
1538 {
1539 	register struct mbuf *m;
1540 	register int count, i;
1541 	register char *cp;
1542 
1543 	/*
1544 	 * Trim from tail.  Scan the mbuf chain,
1545 	 * calculating its length and finding the last mbuf.
1546 	 * If the adjustment only affects this mbuf, then just
1547 	 * adjust and return.  Otherwise, rescan and truncate
1548 	 * after the remaining size.
1549 	 */
1550 	count = 0;
1551 	m = mp;
1552 	for (;;) {
1553 		count += m->m_len;
1554 		if (m->m_next == (struct mbuf *)0)
1555 			break;
1556 		m = m->m_next;
1557 	}
1558 	if (m->m_len > len) {
1559 		m->m_len -= len;
1560 		if (nul > 0) {
1561 			cp = mtod(m, caddr_t)+m->m_len-nul;
1562 			for (i = 0; i < nul; i++)
1563 				*cp++ = '\0';
1564 		}
1565 		return;
1566 	}
1567 	count -= len;
1568 	if (count < 0)
1569 		count = 0;
1570 	/*
1571 	 * Correct length for chain is "count".
1572 	 * Find the mbuf with last data, adjust its length,
1573 	 * and toss data from remaining mbufs on chain.
1574 	 */
1575 	for (m = mp; m; m = m->m_next) {
1576 		if (m->m_len >= count) {
1577 			m->m_len = count;
1578 			if (nul > 0) {
1579 				cp = mtod(m, caddr_t)+m->m_len-nul;
1580 				for (i = 0; i < nul; i++)
1581 					*cp++ = '\0';
1582 			}
1583 			break;
1584 		}
1585 		count -= m->m_len;
1586 	}
1587 	for (m = m->m_next;m;m = m->m_next)
1588 		m->m_len = 0;
1589 }
1590 
1591 /*
1592  * Make these functions instead of macros, so that the kernel text size
1593  * doesn't get too big...
1594  */
1595 void
1596 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1597 	struct nfsrv_descript *nfsd;
1598 	int before_ret;
1599 	register struct vattr *before_vap;
1600 	int after_ret;
1601 	struct vattr *after_vap;
1602 	struct mbuf **mbp;
1603 	char **bposp;
1604 {
1605 	register struct mbuf *mb = *mbp, *mb2;
1606 	register char *bpos = *bposp;
1607 	register u_int32_t *tl;
1608 
1609 	if (before_ret) {
1610 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1611 		*tl = nfs_false;
1612 	} else {
1613 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1614 		*tl++ = nfs_true;
1615 		txdr_hyper(before_vap->va_size, tl);
1616 		tl += 2;
1617 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
1618 		tl += 2;
1619 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
1620 	}
1621 	*bposp = bpos;
1622 	*mbp = mb;
1623 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1624 }
1625 
1626 void
1627 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1628 	struct nfsrv_descript *nfsd;
1629 	int after_ret;
1630 	struct vattr *after_vap;
1631 	struct mbuf **mbp;
1632 	char **bposp;
1633 {
1634 	register struct mbuf *mb = *mbp, *mb2;
1635 	register char *bpos = *bposp;
1636 	register u_int32_t *tl;
1637 	register struct nfs_fattr *fp;
1638 
1639 	if (after_ret) {
1640 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1641 		*tl = nfs_false;
1642 	} else {
1643 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1644 		*tl++ = nfs_true;
1645 		fp = (struct nfs_fattr *)tl;
1646 		nfsm_srvfattr(nfsd, after_vap, fp);
1647 	}
1648 	*mbp = mb;
1649 	*bposp = bpos;
1650 }
1651 
1652 void
1653 nfsm_srvfattr(nfsd, vap, fp)
1654 	register struct nfsrv_descript *nfsd;
1655 	register struct vattr *vap;
1656 	register struct nfs_fattr *fp;
1657 {
1658 
1659 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1660 	fp->fa_uid = txdr_unsigned(vap->va_uid);
1661 	fp->fa_gid = txdr_unsigned(vap->va_gid);
1662 	if (nfsd->nd_flag & ND_NFSV3) {
1663 		fp->fa_type = vtonfsv3_type(vap->va_type);
1664 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1665 		txdr_hyper(vap->va_size, &fp->fa3_size);
1666 		txdr_hyper(vap->va_bytes, &fp->fa3_used);
1667 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1668 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1669 		fp->fa3_fsid.nfsuquad[0] = 0;
1670 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1671 		fp->fa3_fileid.nfsuquad[0] = 0;
1672 		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1673 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1674 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1675 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1676 	} else {
1677 		fp->fa_type = vtonfsv2_type(vap->va_type);
1678 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1679 		fp->fa2_size = txdr_unsigned(vap->va_size);
1680 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1681 		if (vap->va_type == VFIFO)
1682 			fp->fa2_rdev = 0xffffffff;
1683 		else
1684 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1685 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1686 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1687 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1688 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1689 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1690 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1691 	}
1692 }
1693 
1694 /*
1695  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1696  * 	- look up fsid in mount list (if not found ret error)
1697  *	- get vp and export rights by calling VFS_FHTOVP() and VFS_CHECKEXP()
1698  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1699  *	- if not lockflag unlock it with VOP_UNLOCK()
1700  */
1701 int
1702 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag)
1703 	fhandle_t *fhp;
1704 	int lockflag;
1705 	struct vnode **vpp;
1706 	struct ucred *cred;
1707 	struct nfssvc_sock *slp;
1708 	struct mbuf *nam;
1709 	int *rdonlyp;
1710 	int kerbflag;
1711 {
1712 	struct proc *p = curproc;	/* XXX */
1713 	register struct mount *mp;
1714 	register int i;
1715 	struct ucred *credanon;
1716 	int error, exflags;
1717 	struct sockaddr_in *saddr;
1718 
1719 	*vpp = (struct vnode *)0;
1720 	mp = vfs_getvfs(&fhp->fh_fsid);
1721 
1722 	if (!mp)
1723 		return (ESTALE);
1724 	error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1725 	if (error)
1726 		return (error);
1727 	error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1728 	if (error)
1729 		return (error);
1730 
1731 	saddr = mtod(nam, struct sockaddr_in *);
1732 	if (saddr->sin_family == AF_INET &&
1733 	    (ntohs(saddr->sin_port) >= IPPORT_RESERVED ||
1734 	    (slp->ns_so->so_type == SOCK_STREAM && ntohs(saddr->sin_port) == 20))) {
1735 		vput(*vpp);
1736 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1737 	}
1738 
1739 	/*
1740 	 * Check/setup credentials.
1741 	 */
1742 	if (exflags & MNT_EXKERB) {
1743 		if (!kerbflag) {
1744 			vput(*vpp);
1745 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1746 		}
1747 	} else if (kerbflag) {
1748 		vput(*vpp);
1749 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1750 	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1751 		cred->cr_uid = credanon->cr_uid;
1752 		cred->cr_gid = credanon->cr_gid;
1753 		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1754 			cred->cr_groups[i] = credanon->cr_groups[i];
1755 		cred->cr_ngroups = i;
1756 	}
1757 	if (exflags & MNT_EXRDONLY)
1758 		*rdonlyp = 1;
1759 	else
1760 		*rdonlyp = 0;
1761 	if (!lockflag)
1762 		VOP_UNLOCK(*vpp, 0, p);
1763 
1764 	return (0);
1765 }
1766 
1767 /*
1768  * This function compares two net addresses by family and returns TRUE
1769  * if they are the same host.
1770  * If there is any doubt, return FALSE.
1771  * The AF_INET family is handled as a special case so that address mbufs
1772  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1773  */
1774 int
1775 netaddr_match(family, haddr, nam)
1776 	int family;
1777 	union nethostaddr *haddr;
1778 	struct mbuf *nam;
1779 {
1780 	register struct sockaddr_in *inetaddr;
1781 
1782 	switch (family) {
1783 	case AF_INET:
1784 		inetaddr = mtod(nam, struct sockaddr_in *);
1785 		if (inetaddr->sin_family == AF_INET &&
1786 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1787 			return (1);
1788 		break;
1789 #ifdef ISO
1790 	case AF_ISO:
1791 	    {
1792 		register struct sockaddr_iso *isoaddr1, *isoaddr2;
1793 
1794 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
1795 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
1796 		if (isoaddr1->siso_family == AF_ISO &&
1797 		    isoaddr1->siso_nlen > 0 &&
1798 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1799 		    SAME_ISOADDR(isoaddr1, isoaddr2))
1800 			return (1);
1801 		break;
1802 	    }
1803 #endif	/* ISO */
1804 	default:
1805 		break;
1806 	};
1807 	return (0);
1808 }
1809 
1810 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
1811 /*
1812  * This function finds the directory cookie that corresponds to the
1813  * logical byte offset given.
1814  */
1815 nfsuint64 *
1816 nfs_getcookie(np, off, add)
1817 	register struct nfsnode *np;
1818 	off_t off;
1819 	int add;
1820 {
1821 	register struct nfsdmap *dp, *dp2;
1822 	register int pos;
1823 
1824 	pos = off / NFS_DIRBLKSIZ;
1825 	if (pos == 0) {
1826 #ifdef DIAGNOSTIC
1827 		if (add)
1828 			panic("nfs getcookie add at 0");
1829 #endif
1830 		return (&nfs_nullcookie);
1831 	}
1832 	pos--;
1833 	dp = np->n_cookies.lh_first;
1834 	if (!dp) {
1835 		if (add) {
1836 			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
1837 				M_NFSDIROFF, M_WAITOK);
1838 			dp->ndm_eocookie = 0;
1839 			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1840 		} else
1841 			return ((nfsuint64 *)0);
1842 	}
1843 	while (pos >= NFSNUMCOOKIES) {
1844 		pos -= NFSNUMCOOKIES;
1845 		if (dp->ndm_list.le_next) {
1846 			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
1847 				pos >= dp->ndm_eocookie)
1848 				return ((nfsuint64 *)0);
1849 			dp = dp->ndm_list.le_next;
1850 		} else if (add) {
1851 			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
1852 				M_NFSDIROFF, M_WAITOK);
1853 			dp2->ndm_eocookie = 0;
1854 			LIST_INSERT_AFTER(dp, dp2, ndm_list);
1855 			dp = dp2;
1856 		} else
1857 			return ((nfsuint64 *)0);
1858 	}
1859 	if (pos >= dp->ndm_eocookie) {
1860 		if (add)
1861 			dp->ndm_eocookie = pos + 1;
1862 		else
1863 			return ((nfsuint64 *)0);
1864 	}
1865 	return (&dp->ndm_cookies[pos]);
1866 }
1867 
1868 /*
1869  * Invalidate cached directory information, except for the actual directory
1870  * blocks (which are invalidated separately).
1871  * Done mainly to avoid the use of stale offset cookies.
1872  */
1873 void
1874 nfs_invaldir(vp)
1875 	register struct vnode *vp;
1876 {
1877 #ifdef notdef /* XXX */
1878 	register struct nfsnode *np = VTONFS(vp);
1879 
1880 #ifdef DIAGNOSTIC
1881 	if (vp->v_type != VDIR)
1882 		panic("nfs: invaldir not dir");
1883 #endif
1884 	np->n_direofoffset = 0;
1885 	np->n_cookieverf.nfsuquad[0] = 0;
1886 	np->n_cookieverf.nfsuquad[1] = 0;
1887 	if (np->n_cookies.lh_first)
1888 		np->n_cookies.lh_first->ndm_eocookie = 0;
1889 #endif
1890 }
1891 
1892 /*
1893  * The write verifier has changed (probably due to a server reboot), so all
1894  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
1895  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
1896  * flag. Once done the new write verifier can be set for the mount point.
1897  */
1898 void
1899 nfs_clearcommit(mp)
1900 	struct mount *mp;
1901 {
1902 	register struct vnode *vp, *nvp;
1903 	register struct buf *bp, *nbp;
1904 	int s;
1905 
1906 	s = splbio();
1907 loop:
1908 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1909 		if (vp->v_mount != mp)	/* Paranoia */
1910 			goto loop;
1911 		nvp = vp->v_mntvnodes.le_next;
1912 		for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
1913 			nbp = bp->b_vnbufs.le_next;
1914 			if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
1915 				== (B_DELWRI | B_NEEDCOMMIT))
1916 				bp->b_flags &= ~B_NEEDCOMMIT;
1917 		}
1918 	}
1919 	splx(s);
1920 }
1921 
1922 /*
1923  * Map errnos to NFS error numbers. For Version 3 also filter out error
1924  * numbers not specified for the associated procedure.
1925  */
1926 int
1927 nfsrv_errmap(nd, err)
1928 	struct nfsrv_descript *nd;
1929 	register int err;
1930 {
1931 	register short *defaulterrp, *errp;
1932 
1933 	if (nd->nd_flag & ND_NFSV3) {
1934 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
1935 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1936 		while (*++errp) {
1937 			if (*errp == err)
1938 				return (err);
1939 			else if (*errp > err)
1940 				break;
1941 		}
1942 		return ((int)*defaulterrp);
1943 	    } else
1944 		return (err & 0xffff);
1945 	}
1946 	if (err <= ELAST)
1947 		return ((int)nfsrv_v2errmap[err - 1]);
1948 	return (NFSERR_IO);
1949 }
1950 
1951 /*
1952  * Sort the group list in increasing numerical order.
1953  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1954  *  that used to be here.)
1955  */
1956 void
1957 nfsrvw_sort(list, num)
1958         register gid_t *list;
1959         register int num;
1960 {
1961 	register int i, j;
1962 	gid_t v;
1963 
1964 	/* Insertion sort. */
1965 	for (i = 1; i < num; i++) {
1966 		v = list[i];
1967 		/* find correct slot for value v, moving others up */
1968 		for (j = i; --j >= 0 && v < list[j];)
1969 			list[j + 1] = list[j];
1970 		list[j + 1] = v;
1971 	}
1972 }
1973 
1974 /*
1975  * copy credentials making sure that the result can be compared with bcmp().
1976  */
1977 void
1978 nfsrv_setcred(incred, outcred)
1979 	register struct ucred *incred, *outcred;
1980 {
1981 	register int i;
1982 
1983 	bzero((caddr_t)outcred, sizeof (struct ucred));
1984 	outcred->cr_ref = 1;
1985 	outcred->cr_uid = incred->cr_uid;
1986 	outcred->cr_gid = incred->cr_gid;
1987 	outcred->cr_ngroups = incred->cr_ngroups;
1988 	for (i = 0; i < incred->cr_ngroups; i++)
1989 		outcred->cr_groups[i] = incred->cr_groups[i];
1990 	nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
1991 }
1992