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