xref: /freebsd/sys/fs/nfsserver/nfs_nfsdport.c (revision f37852c17391fdf0e8309bcf684384dd0d854e43)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/capsicum.h>
38 
39 /*
40  * Functions that perform the vfs operations required by the routines in
41  * nfsd_serv.c. It is hoped that this change will make the server more
42  * portable.
43  */
44 
45 #include <fs/nfs/nfsport.h>
46 #include <sys/hash.h>
47 #include <sys/sysctl.h>
48 #include <nlm/nlm_prot.h>
49 #include <nlm/nlm.h>
50 
51 FEATURE(nfsd, "NFSv4 server");
52 
53 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54 extern int nfsrv_useacl;
55 extern int newnfs_numnfsd;
56 extern struct mount nfsv4root_mnt;
57 extern struct nfsrv_stablefirst nfsrv_stablefirst;
58 extern void (*nfsd_call_servertimer)(void);
59 extern SVCPOOL	*nfsrvd_pool;
60 extern struct nfsv4lock nfsd_suspend_lock;
61 extern struct nfsclienthashhead *nfsclienthash;
62 extern struct nfslockhashhead *nfslockhash;
63 extern struct nfssessionhash *nfssessionhash;
64 extern int nfsrv_sessionhashsize;
65 extern struct nfsstatsv1 nfsstatsv1;
66 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
67 NFSDLOCKMUTEX;
68 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
69 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
70 struct mtx nfsrc_udpmtx;
71 struct mtx nfs_v4root_mutex;
72 struct nfsrvfh nfs_rootfh, nfs_pubfh;
73 int nfs_pubfhset = 0, nfs_rootfhset = 0;
74 struct proc *nfsd_master_proc = NULL;
75 int nfsd_debuglevel = 0;
76 static pid_t nfsd_master_pid = (pid_t)-1;
77 static char nfsd_master_comm[MAXCOMLEN + 1];
78 static struct timeval nfsd_master_start;
79 static uint32_t nfsv4_sysid = 0;
80 
81 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
82     struct ucred *);
83 
84 int nfsrv_enable_crossmntpt = 1;
85 static int nfs_commit_blks;
86 static int nfs_commit_miss;
87 extern int nfsrv_issuedelegs;
88 extern int nfsrv_dolocallocks;
89 extern int nfsd_enable_stringtouid;
90 extern int nfsd_enable_uidtostring;
91 
92 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server");
93 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
94     &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
95 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
96     0, "");
97 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
98     0, "");
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
100     &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
101 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
102     &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
103 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
104     0, "Debug level for NFS server");
105 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
106     &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
107 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_uidtostring, CTLFLAG_RW,
108     &nfsd_enable_uidtostring, 0, "Make nfsd always send numeric owner_names");
109 
110 #define	MAX_REORDERED_RPC	16
111 #define	NUM_HEURISTIC		1031
112 #define	NHUSE_INIT		64
113 #define	NHUSE_INC		16
114 #define	NHUSE_MAX		2048
115 
116 static struct nfsheur {
117 	struct vnode *nh_vp;	/* vp to match (unreferenced pointer) */
118 	off_t nh_nextoff;	/* next offset for sequential detection */
119 	int nh_use;		/* use count for selection */
120 	int nh_seqcount;	/* heuristic */
121 } nfsheur[NUM_HEURISTIC];
122 
123 
124 /*
125  * Heuristic to detect sequential operation.
126  */
127 static struct nfsheur *
128 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
129 {
130 	struct nfsheur *nh;
131 	int hi, try;
132 
133 	/* Locate best candidate. */
134 	try = 32;
135 	hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
136 	nh = &nfsheur[hi];
137 	while (try--) {
138 		if (nfsheur[hi].nh_vp == vp) {
139 			nh = &nfsheur[hi];
140 			break;
141 		}
142 		if (nfsheur[hi].nh_use > 0)
143 			--nfsheur[hi].nh_use;
144 		hi = (hi + 1) % NUM_HEURISTIC;
145 		if (nfsheur[hi].nh_use < nh->nh_use)
146 			nh = &nfsheur[hi];
147 	}
148 
149 	/* Initialize hint if this is a new file. */
150 	if (nh->nh_vp != vp) {
151 		nh->nh_vp = vp;
152 		nh->nh_nextoff = uio->uio_offset;
153 		nh->nh_use = NHUSE_INIT;
154 		if (uio->uio_offset == 0)
155 			nh->nh_seqcount = 4;
156 		else
157 			nh->nh_seqcount = 1;
158 	}
159 
160 	/* Calculate heuristic. */
161 	if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
162 	    uio->uio_offset == nh->nh_nextoff) {
163 		/* See comments in vfs_vnops.c:sequential_heuristic(). */
164 		nh->nh_seqcount += howmany(uio->uio_resid, 16384);
165 		if (nh->nh_seqcount > IO_SEQMAX)
166 			nh->nh_seqcount = IO_SEQMAX;
167 	} else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
168 	    imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
169 		/* Probably a reordered RPC, leave seqcount alone. */
170 	} else if (nh->nh_seqcount > 1) {
171 		nh->nh_seqcount /= 2;
172 	} else {
173 		nh->nh_seqcount = 0;
174 	}
175 	nh->nh_use += NHUSE_INC;
176 	if (nh->nh_use > NHUSE_MAX)
177 		nh->nh_use = NHUSE_MAX;
178 	return (nh);
179 }
180 
181 /*
182  * Get attributes into nfsvattr structure.
183  */
184 int
185 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
186     struct thread *p, int vpislocked)
187 {
188 	int error, lockedit = 0;
189 
190 	if (vpislocked == 0) {
191 		/*
192 		 * When vpislocked == 0, the vnode is either exclusively
193 		 * locked by this thread or not locked by this thread.
194 		 * As such, shared lock it, if not exclusively locked.
195 		 */
196 		if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
197 			lockedit = 1;
198 			NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
199 		}
200 	}
201 	error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
202 	if (lockedit != 0)
203 		NFSVOPUNLOCK(vp, 0);
204 
205 	NFSEXITCODE(error);
206 	return (error);
207 }
208 
209 /*
210  * Get a file handle for a vnode.
211  */
212 int
213 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
214 {
215 	int error;
216 
217 	NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
218 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
219 	error = VOP_VPTOFH(vp, &fhp->fh_fid);
220 
221 	NFSEXITCODE(error);
222 	return (error);
223 }
224 
225 /*
226  * Perform access checking for vnodes obtained from file handles that would
227  * refer to files already opened by a Unix client. You cannot just use
228  * vn_writechk() and VOP_ACCESSX() for two reasons.
229  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
230  *     case.
231  * 2 - The owner is to be given access irrespective of mode bits for some
232  *     operations, so that processes that chmod after opening a file don't
233  *     break.
234  */
235 int
236 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
237     struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
238     u_int32_t *supportedtypep)
239 {
240 	struct vattr vattr;
241 	int error = 0, getret = 0;
242 
243 	if (vpislocked == 0) {
244 		if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
245 			error = EPERM;
246 			goto out;
247 		}
248 	}
249 	if (accmode & VWRITE) {
250 		/* Just vn_writechk() changed to check rdonly */
251 		/*
252 		 * Disallow write attempts on read-only file systems;
253 		 * unless the file is a socket or a block or character
254 		 * device resident on the file system.
255 		 */
256 		if (NFSVNO_EXRDONLY(exp) ||
257 		    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
258 			switch (vp->v_type) {
259 			case VREG:
260 			case VDIR:
261 			case VLNK:
262 				error = EROFS;
263 			default:
264 				break;
265 			}
266 		}
267 		/*
268 		 * If there's shared text associated with
269 		 * the inode, try to free it up once.  If
270 		 * we fail, we can't allow writing.
271 		 */
272 		if (VOP_IS_TEXT(vp) && error == 0)
273 			error = ETXTBSY;
274 	}
275 	if (error != 0) {
276 		if (vpislocked == 0)
277 			NFSVOPUNLOCK(vp, 0);
278 		goto out;
279 	}
280 
281 	/*
282 	 * Should the override still be applied when ACLs are enabled?
283 	 */
284 	error = VOP_ACCESSX(vp, accmode, cred, p);
285 	if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
286 		/*
287 		 * Try again with VEXPLICIT_DENY, to see if the test for
288 		 * deletion is supported.
289 		 */
290 		error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
291 		if (error == 0) {
292 			if (vp->v_type == VDIR) {
293 				accmode &= ~(VDELETE | VDELETE_CHILD);
294 				accmode |= VWRITE;
295 				error = VOP_ACCESSX(vp, accmode, cred, p);
296 			} else if (supportedtypep != NULL) {
297 				*supportedtypep &= ~NFSACCESS_DELETE;
298 			}
299 		}
300 	}
301 
302 	/*
303 	 * Allow certain operations for the owner (reads and writes
304 	 * on files that are already open).
305 	 */
306 	if (override != NFSACCCHK_NOOVERRIDE &&
307 	    (error == EPERM || error == EACCES)) {
308 		if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
309 			error = 0;
310 		else if (override & NFSACCCHK_ALLOWOWNER) {
311 			getret = VOP_GETATTR(vp, &vattr, cred);
312 			if (getret == 0 && cred->cr_uid == vattr.va_uid)
313 				error = 0;
314 		}
315 	}
316 	if (vpislocked == 0)
317 		NFSVOPUNLOCK(vp, 0);
318 
319 out:
320 	NFSEXITCODE(error);
321 	return (error);
322 }
323 
324 /*
325  * Set attribute(s) vnop.
326  */
327 int
328 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
329     struct thread *p, struct nfsexstuff *exp)
330 {
331 	int error;
332 
333 	error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
334 	NFSEXITCODE(error);
335 	return (error);
336 }
337 
338 /*
339  * Set up nameidata for a lookup() call and do it.
340  */
341 int
342 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
343     struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
344     struct vnode **retdirp)
345 {
346 	struct componentname *cnp = &ndp->ni_cnd;
347 	int i;
348 	struct iovec aiov;
349 	struct uio auio;
350 	int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
351 	int error = 0, crossmnt;
352 	char *cp;
353 
354 	*retdirp = NULL;
355 	cnp->cn_nameptr = cnp->cn_pnbuf;
356 	ndp->ni_lcf = 0;
357 	/*
358 	 * Extract and set starting directory.
359 	 */
360 	if (dp->v_type != VDIR) {
361 		if (islocked)
362 			vput(dp);
363 		else
364 			vrele(dp);
365 		nfsvno_relpathbuf(ndp);
366 		error = ENOTDIR;
367 		goto out1;
368 	}
369 	if (islocked)
370 		NFSVOPUNLOCK(dp, 0);
371 	VREF(dp);
372 	*retdirp = dp;
373 	if (NFSVNO_EXRDONLY(exp))
374 		cnp->cn_flags |= RDONLY;
375 	ndp->ni_segflg = UIO_SYSSPACE;
376 	crossmnt = 1;
377 
378 	if (nd->nd_flag & ND_PUBLOOKUP) {
379 		ndp->ni_loopcnt = 0;
380 		if (cnp->cn_pnbuf[0] == '/') {
381 			vrele(dp);
382 			/*
383 			 * Check for degenerate pathnames here, since lookup()
384 			 * panics on them.
385 			 */
386 			for (i = 1; i < ndp->ni_pathlen; i++)
387 				if (cnp->cn_pnbuf[i] != '/')
388 					break;
389 			if (i == ndp->ni_pathlen) {
390 				error = NFSERR_ACCES;
391 				goto out;
392 			}
393 			dp = rootvnode;
394 			VREF(dp);
395 		}
396 	} else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
397 	    (nd->nd_flag & ND_NFSV4) == 0) {
398 		/*
399 		 * Only cross mount points for NFSv4 when doing a
400 		 * mount while traversing the file system above
401 		 * the mount point, unless nfsrv_enable_crossmntpt is set.
402 		 */
403 		cnp->cn_flags |= NOCROSSMOUNT;
404 		crossmnt = 0;
405 	}
406 
407 	/*
408 	 * Initialize for scan, set ni_startdir and bump ref on dp again
409 	 * because lookup() will dereference ni_startdir.
410 	 */
411 
412 	cnp->cn_thread = p;
413 	ndp->ni_startdir = dp;
414 	ndp->ni_rootdir = rootvnode;
415 	ndp->ni_topdir = NULL;
416 
417 	if (!lockleaf)
418 		cnp->cn_flags |= LOCKLEAF;
419 	for (;;) {
420 		cnp->cn_nameptr = cnp->cn_pnbuf;
421 		/*
422 		 * Call lookup() to do the real work.  If an error occurs,
423 		 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
424 		 * we do not have to dereference anything before returning.
425 		 * In either case ni_startdir will be dereferenced and NULLed
426 		 * out.
427 		 */
428 		error = lookup(ndp);
429 		if (error)
430 			break;
431 
432 		/*
433 		 * Check for encountering a symbolic link.  Trivial
434 		 * termination occurs if no symlink encountered.
435 		 */
436 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
437 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
438 				nfsvno_relpathbuf(ndp);
439 			if (ndp->ni_vp && !lockleaf)
440 				NFSVOPUNLOCK(ndp->ni_vp, 0);
441 			break;
442 		}
443 
444 		/*
445 		 * Validate symlink
446 		 */
447 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
448 			NFSVOPUNLOCK(ndp->ni_dvp, 0);
449 		if (!(nd->nd_flag & ND_PUBLOOKUP)) {
450 			error = EINVAL;
451 			goto badlink2;
452 		}
453 
454 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
455 			error = ELOOP;
456 			goto badlink2;
457 		}
458 		if (ndp->ni_pathlen > 1)
459 			cp = uma_zalloc(namei_zone, M_WAITOK);
460 		else
461 			cp = cnp->cn_pnbuf;
462 		aiov.iov_base = cp;
463 		aiov.iov_len = MAXPATHLEN;
464 		auio.uio_iov = &aiov;
465 		auio.uio_iovcnt = 1;
466 		auio.uio_offset = 0;
467 		auio.uio_rw = UIO_READ;
468 		auio.uio_segflg = UIO_SYSSPACE;
469 		auio.uio_td = NULL;
470 		auio.uio_resid = MAXPATHLEN;
471 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
472 		if (error) {
473 		badlink1:
474 			if (ndp->ni_pathlen > 1)
475 				uma_zfree(namei_zone, cp);
476 		badlink2:
477 			vrele(ndp->ni_dvp);
478 			vput(ndp->ni_vp);
479 			break;
480 		}
481 		linklen = MAXPATHLEN - auio.uio_resid;
482 		if (linklen == 0) {
483 			error = ENOENT;
484 			goto badlink1;
485 		}
486 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
487 			error = ENAMETOOLONG;
488 			goto badlink1;
489 		}
490 
491 		/*
492 		 * Adjust or replace path
493 		 */
494 		if (ndp->ni_pathlen > 1) {
495 			NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
496 			uma_zfree(namei_zone, cnp->cn_pnbuf);
497 			cnp->cn_pnbuf = cp;
498 		} else
499 			cnp->cn_pnbuf[linklen] = '\0';
500 		ndp->ni_pathlen += linklen;
501 
502 		/*
503 		 * Cleanup refs for next loop and check if root directory
504 		 * should replace current directory.  Normally ni_dvp
505 		 * becomes the new base directory and is cleaned up when
506 		 * we loop.  Explicitly null pointers after invalidation
507 		 * to clarify operation.
508 		 */
509 		vput(ndp->ni_vp);
510 		ndp->ni_vp = NULL;
511 
512 		if (cnp->cn_pnbuf[0] == '/') {
513 			vrele(ndp->ni_dvp);
514 			ndp->ni_dvp = ndp->ni_rootdir;
515 			VREF(ndp->ni_dvp);
516 		}
517 		ndp->ni_startdir = ndp->ni_dvp;
518 		ndp->ni_dvp = NULL;
519 	}
520 	if (!lockleaf)
521 		cnp->cn_flags &= ~LOCKLEAF;
522 
523 out:
524 	if (error) {
525 		nfsvno_relpathbuf(ndp);
526 		ndp->ni_vp = NULL;
527 		ndp->ni_dvp = NULL;
528 		ndp->ni_startdir = NULL;
529 	} else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
530 		ndp->ni_dvp = NULL;
531 	}
532 
533 out1:
534 	NFSEXITCODE2(error, nd);
535 	return (error);
536 }
537 
538 /*
539  * Set up a pathname buffer and return a pointer to it and, optionally
540  * set a hash pointer.
541  */
542 void
543 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
544 {
545 	struct componentname *cnp = &ndp->ni_cnd;
546 
547 	cnp->cn_flags |= (NOMACCHECK | HASBUF);
548 	cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
549 	if (hashpp != NULL)
550 		*hashpp = NULL;
551 	*bufpp = cnp->cn_pnbuf;
552 }
553 
554 /*
555  * Release the above path buffer, if not released by nfsvno_namei().
556  */
557 void
558 nfsvno_relpathbuf(struct nameidata *ndp)
559 {
560 
561 	if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
562 		panic("nfsrelpath");
563 	uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
564 	ndp->ni_cnd.cn_flags &= ~HASBUF;
565 }
566 
567 /*
568  * Readlink vnode op into an mbuf list.
569  */
570 int
571 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
572     struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
573 {
574 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
575 	struct iovec *ivp = iv;
576 	struct uio io, *uiop = &io;
577 	struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
578 	int i, len, tlen, error = 0;
579 
580 	len = 0;
581 	i = 0;
582 	while (len < NFS_MAXPATHLEN) {
583 		NFSMGET(mp);
584 		MCLGET(mp, M_WAITOK);
585 		mp->m_len = M_SIZE(mp);
586 		if (len == 0) {
587 			mp3 = mp2 = mp;
588 		} else {
589 			mp2->m_next = mp;
590 			mp2 = mp;
591 		}
592 		if ((len + mp->m_len) > NFS_MAXPATHLEN) {
593 			mp->m_len = NFS_MAXPATHLEN - len;
594 			len = NFS_MAXPATHLEN;
595 		} else {
596 			len += mp->m_len;
597 		}
598 		ivp->iov_base = mtod(mp, caddr_t);
599 		ivp->iov_len = mp->m_len;
600 		i++;
601 		ivp++;
602 	}
603 	uiop->uio_iov = iv;
604 	uiop->uio_iovcnt = i;
605 	uiop->uio_offset = 0;
606 	uiop->uio_resid = len;
607 	uiop->uio_rw = UIO_READ;
608 	uiop->uio_segflg = UIO_SYSSPACE;
609 	uiop->uio_td = NULL;
610 	error = VOP_READLINK(vp, uiop, cred);
611 	if (error) {
612 		m_freem(mp3);
613 		*lenp = 0;
614 		goto out;
615 	}
616 	if (uiop->uio_resid > 0) {
617 		len -= uiop->uio_resid;
618 		tlen = NFSM_RNDUP(len);
619 		nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
620 	}
621 	*lenp = len;
622 	*mpp = mp3;
623 	*mpendp = mp;
624 
625 out:
626 	NFSEXITCODE(error);
627 	return (error);
628 }
629 
630 /*
631  * Read vnode op call into mbuf list.
632  */
633 int
634 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
635     struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
636 {
637 	struct mbuf *m;
638 	int i;
639 	struct iovec *iv;
640 	struct iovec *iv2;
641 	int error = 0, len, left, siz, tlen, ioflag = 0;
642 	struct mbuf *m2 = NULL, *m3;
643 	struct uio io, *uiop = &io;
644 	struct nfsheur *nh;
645 
646 	len = left = NFSM_RNDUP(cnt);
647 	m3 = NULL;
648 	/*
649 	 * Generate the mbuf list with the uio_iov ref. to it.
650 	 */
651 	i = 0;
652 	while (left > 0) {
653 		NFSMGET(m);
654 		MCLGET(m, M_WAITOK);
655 		m->m_len = 0;
656 		siz = min(M_TRAILINGSPACE(m), left);
657 		left -= siz;
658 		i++;
659 		if (m3)
660 			m2->m_next = m;
661 		else
662 			m3 = m;
663 		m2 = m;
664 	}
665 	MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
666 	    M_TEMP, M_WAITOK);
667 	uiop->uio_iov = iv2 = iv;
668 	m = m3;
669 	left = len;
670 	i = 0;
671 	while (left > 0) {
672 		if (m == NULL)
673 			panic("nfsvno_read iov");
674 		siz = min(M_TRAILINGSPACE(m), left);
675 		if (siz > 0) {
676 			iv->iov_base = mtod(m, caddr_t) + m->m_len;
677 			iv->iov_len = siz;
678 			m->m_len += siz;
679 			left -= siz;
680 			iv++;
681 			i++;
682 		}
683 		m = m->m_next;
684 	}
685 	uiop->uio_iovcnt = i;
686 	uiop->uio_offset = off;
687 	uiop->uio_resid = len;
688 	uiop->uio_rw = UIO_READ;
689 	uiop->uio_segflg = UIO_SYSSPACE;
690 	uiop->uio_td = NULL;
691 	nh = nfsrv_sequential_heuristic(uiop, vp);
692 	ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
693 	/* XXX KDM make this more systematic? */
694 	nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
695 	error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
696 	FREE((caddr_t)iv2, M_TEMP);
697 	if (error) {
698 		m_freem(m3);
699 		*mpp = NULL;
700 		goto out;
701 	}
702 	nh->nh_nextoff = uiop->uio_offset;
703 	tlen = len - uiop->uio_resid;
704 	cnt = cnt < tlen ? cnt : tlen;
705 	tlen = NFSM_RNDUP(cnt);
706 	if (tlen == 0) {
707 		m_freem(m3);
708 		m3 = NULL;
709 	} else if (len != tlen || tlen != cnt)
710 		nfsrv_adj(m3, len - tlen, tlen - cnt);
711 	*mpp = m3;
712 	*mpendp = m2;
713 
714 out:
715 	NFSEXITCODE(error);
716 	return (error);
717 }
718 
719 /*
720  * Write vnode op from an mbuf list.
721  */
722 int
723 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
724     struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
725 {
726 	struct iovec *ivp;
727 	int i, len;
728 	struct iovec *iv;
729 	int ioflags, error;
730 	struct uio io, *uiop = &io;
731 	struct nfsheur *nh;
732 
733 	MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
734 	    M_WAITOK);
735 	uiop->uio_iov = iv = ivp;
736 	uiop->uio_iovcnt = cnt;
737 	i = mtod(mp, caddr_t) + mp->m_len - cp;
738 	len = retlen;
739 	while (len > 0) {
740 		if (mp == NULL)
741 			panic("nfsvno_write");
742 		if (i > 0) {
743 			i = min(i, len);
744 			ivp->iov_base = cp;
745 			ivp->iov_len = i;
746 			ivp++;
747 			len -= i;
748 		}
749 		mp = mp->m_next;
750 		if (mp) {
751 			i = mp->m_len;
752 			cp = mtod(mp, caddr_t);
753 		}
754 	}
755 
756 	if (stable == NFSWRITE_UNSTABLE)
757 		ioflags = IO_NODELOCKED;
758 	else
759 		ioflags = (IO_SYNC | IO_NODELOCKED);
760 	uiop->uio_resid = retlen;
761 	uiop->uio_rw = UIO_WRITE;
762 	uiop->uio_segflg = UIO_SYSSPACE;
763 	NFSUIOPROC(uiop, p);
764 	uiop->uio_offset = off;
765 	nh = nfsrv_sequential_heuristic(uiop, vp);
766 	ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
767 	/* XXX KDM make this more systematic? */
768 	nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
769 	error = VOP_WRITE(vp, uiop, ioflags, cred);
770 	if (error == 0)
771 		nh->nh_nextoff = uiop->uio_offset;
772 	FREE((caddr_t)iv, M_TEMP);
773 
774 	NFSEXITCODE(error);
775 	return (error);
776 }
777 
778 /*
779  * Common code for creating a regular file (plus special files for V2).
780  */
781 int
782 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
783     struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
784     int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
785 {
786 	u_quad_t tempsize;
787 	int error;
788 
789 	error = nd->nd_repstat;
790 	if (!error && ndp->ni_vp == NULL) {
791 		if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
792 			vrele(ndp->ni_startdir);
793 			error = VOP_CREATE(ndp->ni_dvp,
794 			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
795 			vput(ndp->ni_dvp);
796 			nfsvno_relpathbuf(ndp);
797 			if (!error) {
798 				if (*exclusive_flagp) {
799 					*exclusive_flagp = 0;
800 					NFSVNO_ATTRINIT(nvap);
801 					nvap->na_atime.tv_sec = cverf[0];
802 					nvap->na_atime.tv_nsec = cverf[1];
803 					error = VOP_SETATTR(ndp->ni_vp,
804 					    &nvap->na_vattr, nd->nd_cred);
805 					if (error != 0) {
806 						vput(ndp->ni_vp);
807 						ndp->ni_vp = NULL;
808 						error = NFSERR_NOTSUPP;
809 					}
810 				}
811 			}
812 		/*
813 		 * NFS V2 Only. nfsrvd_mknod() does this for V3.
814 		 * (This implies, just get out on an error.)
815 		 */
816 		} else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
817 			nvap->na_type == VFIFO) {
818 			if (nvap->na_type == VCHR && rdev == 0xffffffff)
819 				nvap->na_type = VFIFO;
820                         if (nvap->na_type != VFIFO &&
821 			    (error = priv_check_cred(nd->nd_cred,
822 			     PRIV_VFS_MKNOD_DEV, 0))) {
823 				vrele(ndp->ni_startdir);
824 				nfsvno_relpathbuf(ndp);
825 				vput(ndp->ni_dvp);
826 				goto out;
827 			}
828 			nvap->na_rdev = rdev;
829 			error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
830 			    &ndp->ni_cnd, &nvap->na_vattr);
831 			vput(ndp->ni_dvp);
832 			nfsvno_relpathbuf(ndp);
833 			vrele(ndp->ni_startdir);
834 			if (error)
835 				goto out;
836 		} else {
837 			vrele(ndp->ni_startdir);
838 			nfsvno_relpathbuf(ndp);
839 			vput(ndp->ni_dvp);
840 			error = ENXIO;
841 			goto out;
842 		}
843 		*vpp = ndp->ni_vp;
844 	} else {
845 		/*
846 		 * Handle cases where error is already set and/or
847 		 * the file exists.
848 		 * 1 - clean up the lookup
849 		 * 2 - iff !error and na_size set, truncate it
850 		 */
851 		vrele(ndp->ni_startdir);
852 		nfsvno_relpathbuf(ndp);
853 		*vpp = ndp->ni_vp;
854 		if (ndp->ni_dvp == *vpp)
855 			vrele(ndp->ni_dvp);
856 		else
857 			vput(ndp->ni_dvp);
858 		if (!error && nvap->na_size != VNOVAL) {
859 			error = nfsvno_accchk(*vpp, VWRITE,
860 			    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
861 			    NFSACCCHK_VPISLOCKED, NULL);
862 			if (!error) {
863 				tempsize = nvap->na_size;
864 				NFSVNO_ATTRINIT(nvap);
865 				nvap->na_size = tempsize;
866 				error = VOP_SETATTR(*vpp,
867 				    &nvap->na_vattr, nd->nd_cred);
868 			}
869 		}
870 		if (error)
871 			vput(*vpp);
872 	}
873 
874 out:
875 	NFSEXITCODE(error);
876 	return (error);
877 }
878 
879 /*
880  * Do a mknod vnode op.
881  */
882 int
883 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
884     struct thread *p)
885 {
886 	int error = 0;
887 	enum vtype vtyp;
888 
889 	vtyp = nvap->na_type;
890 	/*
891 	 * Iff doesn't exist, create it.
892 	 */
893 	if (ndp->ni_vp) {
894 		vrele(ndp->ni_startdir);
895 		nfsvno_relpathbuf(ndp);
896 		vput(ndp->ni_dvp);
897 		vrele(ndp->ni_vp);
898 		error = EEXIST;
899 		goto out;
900 	}
901 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
902 		vrele(ndp->ni_startdir);
903 		nfsvno_relpathbuf(ndp);
904 		vput(ndp->ni_dvp);
905 		error = NFSERR_BADTYPE;
906 		goto out;
907 	}
908 	if (vtyp == VSOCK) {
909 		vrele(ndp->ni_startdir);
910 		error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
911 		    &ndp->ni_cnd, &nvap->na_vattr);
912 		vput(ndp->ni_dvp);
913 		nfsvno_relpathbuf(ndp);
914 	} else {
915 		if (nvap->na_type != VFIFO &&
916 		    (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
917 			vrele(ndp->ni_startdir);
918 			nfsvno_relpathbuf(ndp);
919 			vput(ndp->ni_dvp);
920 			goto out;
921 		}
922 		error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
923 		    &ndp->ni_cnd, &nvap->na_vattr);
924 		vput(ndp->ni_dvp);
925 		nfsvno_relpathbuf(ndp);
926 		vrele(ndp->ni_startdir);
927 		/*
928 		 * Since VOP_MKNOD returns the ni_vp, I can't
929 		 * see any reason to do the lookup.
930 		 */
931 	}
932 
933 out:
934 	NFSEXITCODE(error);
935 	return (error);
936 }
937 
938 /*
939  * Mkdir vnode op.
940  */
941 int
942 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
943     struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
944 {
945 	int error = 0;
946 
947 	if (ndp->ni_vp != NULL) {
948 		if (ndp->ni_dvp == ndp->ni_vp)
949 			vrele(ndp->ni_dvp);
950 		else
951 			vput(ndp->ni_dvp);
952 		vrele(ndp->ni_vp);
953 		nfsvno_relpathbuf(ndp);
954 		error = EEXIST;
955 		goto out;
956 	}
957 	error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
958 	    &nvap->na_vattr);
959 	vput(ndp->ni_dvp);
960 	nfsvno_relpathbuf(ndp);
961 
962 out:
963 	NFSEXITCODE(error);
964 	return (error);
965 }
966 
967 /*
968  * symlink vnode op.
969  */
970 int
971 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
972     int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
973     struct nfsexstuff *exp)
974 {
975 	int error = 0;
976 
977 	if (ndp->ni_vp) {
978 		vrele(ndp->ni_startdir);
979 		nfsvno_relpathbuf(ndp);
980 		if (ndp->ni_dvp == ndp->ni_vp)
981 			vrele(ndp->ni_dvp);
982 		else
983 			vput(ndp->ni_dvp);
984 		vrele(ndp->ni_vp);
985 		error = EEXIST;
986 		goto out;
987 	}
988 
989 	error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
990 	    &nvap->na_vattr, pathcp);
991 	vput(ndp->ni_dvp);
992 	vrele(ndp->ni_startdir);
993 	nfsvno_relpathbuf(ndp);
994 	/*
995 	 * Although FreeBSD still had the lookup code in
996 	 * it for 7/current, there doesn't seem to be any
997 	 * point, since VOP_SYMLINK() returns the ni_vp.
998 	 * Just vput it for v2.
999 	 */
1000 	if (!not_v2 && !error)
1001 		vput(ndp->ni_vp);
1002 
1003 out:
1004 	NFSEXITCODE(error);
1005 	return (error);
1006 }
1007 
1008 /*
1009  * Parse symbolic link arguments.
1010  * This function has an ugly side effect. It will MALLOC() an area for
1011  * the symlink and set iov_base to point to it, only if it succeeds.
1012  * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1013  * be FREE'd later.
1014  */
1015 int
1016 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1017     struct thread *p, char **pathcpp, int *lenp)
1018 {
1019 	u_int32_t *tl;
1020 	char *pathcp = NULL;
1021 	int error = 0, len;
1022 	struct nfsv2_sattr *sp;
1023 
1024 	*pathcpp = NULL;
1025 	*lenp = 0;
1026 	if ((nd->nd_flag & ND_NFSV3) &&
1027 	    (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1028 		goto nfsmout;
1029 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1030 	len = fxdr_unsigned(int, *tl);
1031 	if (len > NFS_MAXPATHLEN || len <= 0) {
1032 		error = EBADRPC;
1033 		goto nfsmout;
1034 	}
1035 	MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1036 	error = nfsrv_mtostr(nd, pathcp, len);
1037 	if (error)
1038 		goto nfsmout;
1039 	if (nd->nd_flag & ND_NFSV2) {
1040 		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1041 		nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1042 	}
1043 	*pathcpp = pathcp;
1044 	*lenp = len;
1045 	NFSEXITCODE2(0, nd);
1046 	return (0);
1047 nfsmout:
1048 	if (pathcp)
1049 		free(pathcp, M_TEMP);
1050 	NFSEXITCODE2(error, nd);
1051 	return (error);
1052 }
1053 
1054 /*
1055  * Remove a non-directory object.
1056  */
1057 int
1058 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1059     struct thread *p, struct nfsexstuff *exp)
1060 {
1061 	struct vnode *vp;
1062 	int error = 0;
1063 
1064 	vp = ndp->ni_vp;
1065 	if (vp->v_type == VDIR)
1066 		error = NFSERR_ISDIR;
1067 	else if (is_v4)
1068 		error = nfsrv_checkremove(vp, 1, p);
1069 	if (!error)
1070 		error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1071 	if (ndp->ni_dvp == vp)
1072 		vrele(ndp->ni_dvp);
1073 	else
1074 		vput(ndp->ni_dvp);
1075 	vput(vp);
1076 	if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1077 		nfsvno_relpathbuf(ndp);
1078 	NFSEXITCODE(error);
1079 	return (error);
1080 }
1081 
1082 /*
1083  * Remove a directory.
1084  */
1085 int
1086 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1087     struct thread *p, struct nfsexstuff *exp)
1088 {
1089 	struct vnode *vp;
1090 	int error = 0;
1091 
1092 	vp = ndp->ni_vp;
1093 	if (vp->v_type != VDIR) {
1094 		error = ENOTDIR;
1095 		goto out;
1096 	}
1097 	/*
1098 	 * No rmdir "." please.
1099 	 */
1100 	if (ndp->ni_dvp == vp) {
1101 		error = EINVAL;
1102 		goto out;
1103 	}
1104 	/*
1105 	 * The root of a mounted filesystem cannot be deleted.
1106 	 */
1107 	if (vp->v_vflag & VV_ROOT)
1108 		error = EBUSY;
1109 out:
1110 	if (!error)
1111 		error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1112 	if (ndp->ni_dvp == vp)
1113 		vrele(ndp->ni_dvp);
1114 	else
1115 		vput(ndp->ni_dvp);
1116 	vput(vp);
1117 	if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1118 		nfsvno_relpathbuf(ndp);
1119 	NFSEXITCODE(error);
1120 	return (error);
1121 }
1122 
1123 /*
1124  * Rename vnode op.
1125  */
1126 int
1127 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1128     u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1129 {
1130 	struct vnode *fvp, *tvp, *tdvp;
1131 	int error = 0;
1132 
1133 	fvp = fromndp->ni_vp;
1134 	if (ndstat) {
1135 		vrele(fromndp->ni_dvp);
1136 		vrele(fvp);
1137 		error = ndstat;
1138 		goto out1;
1139 	}
1140 	tdvp = tondp->ni_dvp;
1141 	tvp = tondp->ni_vp;
1142 	if (tvp != NULL) {
1143 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1144 			error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1145 			goto out;
1146 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1147 			error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1148 			goto out;
1149 		}
1150 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1151 			error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1152 			goto out;
1153 		}
1154 
1155 		/*
1156 		 * A rename to '.' or '..' results in a prematurely
1157 		 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1158 		 * here.
1159 		 */
1160 		if ((tondp->ni_cnd.cn_namelen == 1 &&
1161 		     tondp->ni_cnd.cn_nameptr[0] == '.') ||
1162 		    (tondp->ni_cnd.cn_namelen == 2 &&
1163 		     tondp->ni_cnd.cn_nameptr[0] == '.' &&
1164 		     tondp->ni_cnd.cn_nameptr[1] == '.')) {
1165 			error = EINVAL;
1166 			goto out;
1167 		}
1168 	}
1169 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1170 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1171 		goto out;
1172 	}
1173 	if (fvp->v_mount != tdvp->v_mount) {
1174 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1175 		goto out;
1176 	}
1177 	if (fvp == tdvp) {
1178 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1179 		goto out;
1180 	}
1181 	if (fvp == tvp) {
1182 		/*
1183 		 * If source and destination are the same, there is nothing to
1184 		 * do. Set error to -1 to indicate this.
1185 		 */
1186 		error = -1;
1187 		goto out;
1188 	}
1189 	if (ndflag & ND_NFSV4) {
1190 		if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1191 			error = nfsrv_checkremove(fvp, 0, p);
1192 			NFSVOPUNLOCK(fvp, 0);
1193 		} else
1194 			error = EPERM;
1195 		if (tvp && !error)
1196 			error = nfsrv_checkremove(tvp, 1, p);
1197 	} else {
1198 		/*
1199 		 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1200 		 * that the NFSv4 client won't be confused by the rename.
1201 		 * Since nfsd_recalldelegation() can only be called on an
1202 		 * unlocked vnode at this point and fvp is the file that will
1203 		 * still exist after the rename, just do fvp.
1204 		 */
1205 		nfsd_recalldelegation(fvp, p);
1206 	}
1207 out:
1208 	if (!error) {
1209 		error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1210 		    &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1211 		    &tondp->ni_cnd);
1212 	} else {
1213 		if (tdvp == tvp)
1214 			vrele(tdvp);
1215 		else
1216 			vput(tdvp);
1217 		if (tvp)
1218 			vput(tvp);
1219 		vrele(fromndp->ni_dvp);
1220 		vrele(fvp);
1221 		if (error == -1)
1222 			error = 0;
1223 	}
1224 	vrele(tondp->ni_startdir);
1225 	nfsvno_relpathbuf(tondp);
1226 out1:
1227 	vrele(fromndp->ni_startdir);
1228 	nfsvno_relpathbuf(fromndp);
1229 	NFSEXITCODE(error);
1230 	return (error);
1231 }
1232 
1233 /*
1234  * Link vnode op.
1235  */
1236 int
1237 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1238     struct thread *p, struct nfsexstuff *exp)
1239 {
1240 	struct vnode *xp;
1241 	int error = 0;
1242 
1243 	xp = ndp->ni_vp;
1244 	if (xp != NULL) {
1245 		error = EEXIST;
1246 	} else {
1247 		xp = ndp->ni_dvp;
1248 		if (vp->v_mount != xp->v_mount)
1249 			error = EXDEV;
1250 	}
1251 	if (!error) {
1252 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1253 		if ((vp->v_iflag & VI_DOOMED) == 0)
1254 			error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1255 		else
1256 			error = EPERM;
1257 		if (ndp->ni_dvp == vp)
1258 			vrele(ndp->ni_dvp);
1259 		else
1260 			vput(ndp->ni_dvp);
1261 		NFSVOPUNLOCK(vp, 0);
1262 	} else {
1263 		if (ndp->ni_dvp == ndp->ni_vp)
1264 			vrele(ndp->ni_dvp);
1265 		else
1266 			vput(ndp->ni_dvp);
1267 		if (ndp->ni_vp)
1268 			vrele(ndp->ni_vp);
1269 	}
1270 	nfsvno_relpathbuf(ndp);
1271 	NFSEXITCODE(error);
1272 	return (error);
1273 }
1274 
1275 /*
1276  * Do the fsync() appropriate for the commit.
1277  */
1278 int
1279 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1280     struct thread *td)
1281 {
1282 	int error = 0;
1283 
1284 	/*
1285 	 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1286 	 * file is done.  At this time VOP_FSYNC does not accept offset and
1287 	 * byte count parameters so call VOP_FSYNC the whole file for now.
1288 	 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1289 	 * File systems that do not use the buffer cache (as indicated
1290 	 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1291 	 */
1292 	if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1293 	    (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1294 		/*
1295 		 * Give up and do the whole thing
1296 		 */
1297 		if (vp->v_object &&
1298 		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1299 			VM_OBJECT_WLOCK(vp->v_object);
1300 			vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1301 			VM_OBJECT_WUNLOCK(vp->v_object);
1302 		}
1303 		error = VOP_FSYNC(vp, MNT_WAIT, td);
1304 	} else {
1305 		/*
1306 		 * Locate and synchronously write any buffers that fall
1307 		 * into the requested range.  Note:  we are assuming that
1308 		 * f_iosize is a power of 2.
1309 		 */
1310 		int iosize = vp->v_mount->mnt_stat.f_iosize;
1311 		int iomask = iosize - 1;
1312 		struct bufobj *bo;
1313 		daddr_t lblkno;
1314 
1315 		/*
1316 		 * Align to iosize boundary, super-align to page boundary.
1317 		 */
1318 		if (off & iomask) {
1319 			cnt += off & iomask;
1320 			off &= ~(u_quad_t)iomask;
1321 		}
1322 		if (off & PAGE_MASK) {
1323 			cnt += off & PAGE_MASK;
1324 			off &= ~(u_quad_t)PAGE_MASK;
1325 		}
1326 		lblkno = off / iosize;
1327 
1328 		if (vp->v_object &&
1329 		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1330 			VM_OBJECT_WLOCK(vp->v_object);
1331 			vm_object_page_clean(vp->v_object, off, off + cnt,
1332 			    OBJPC_SYNC);
1333 			VM_OBJECT_WUNLOCK(vp->v_object);
1334 		}
1335 
1336 		bo = &vp->v_bufobj;
1337 		BO_LOCK(bo);
1338 		while (cnt > 0) {
1339 			struct buf *bp;
1340 
1341 			/*
1342 			 * If we have a buffer and it is marked B_DELWRI we
1343 			 * have to lock and write it.  Otherwise the prior
1344 			 * write is assumed to have already been committed.
1345 			 *
1346 			 * gbincore() can return invalid buffers now so we
1347 			 * have to check that bit as well (though B_DELWRI
1348 			 * should not be set if B_INVAL is set there could be
1349 			 * a race here since we haven't locked the buffer).
1350 			 */
1351 			if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1352 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1353 				    LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1354 					BO_LOCK(bo);
1355 					continue; /* retry */
1356 				}
1357 			    	if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1358 				    B_DELWRI) {
1359 					bremfree(bp);
1360 					bp->b_flags &= ~B_ASYNC;
1361 					bwrite(bp);
1362 					++nfs_commit_miss;
1363 				} else
1364 					BUF_UNLOCK(bp);
1365 				BO_LOCK(bo);
1366 			}
1367 			++nfs_commit_blks;
1368 			if (cnt < iosize)
1369 				break;
1370 			cnt -= iosize;
1371 			++lblkno;
1372 		}
1373 		BO_UNLOCK(bo);
1374 	}
1375 	NFSEXITCODE(error);
1376 	return (error);
1377 }
1378 
1379 /*
1380  * Statfs vnode op.
1381  */
1382 int
1383 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1384 {
1385 	int error;
1386 
1387 	error = VFS_STATFS(vp->v_mount, sf);
1388 	if (error == 0) {
1389 		/*
1390 		 * Since NFS handles these values as unsigned on the
1391 		 * wire, there is no way to represent negative values,
1392 		 * so set them to 0. Without this, they will appear
1393 		 * to be very large positive values for clients like
1394 		 * Solaris10.
1395 		 */
1396 		if (sf->f_bavail < 0)
1397 			sf->f_bavail = 0;
1398 		if (sf->f_ffree < 0)
1399 			sf->f_ffree = 0;
1400 	}
1401 	NFSEXITCODE(error);
1402 	return (error);
1403 }
1404 
1405 /*
1406  * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1407  * must handle nfsrv_opencheck() calls after any other access checks.
1408  */
1409 void
1410 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1411     nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1412     int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1413     NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1414     struct nfsexstuff *exp, struct vnode **vpp)
1415 {
1416 	struct vnode *vp = NULL;
1417 	u_quad_t tempsize;
1418 	struct nfsexstuff nes;
1419 
1420 	if (ndp->ni_vp == NULL)
1421 		nd->nd_repstat = nfsrv_opencheck(clientid,
1422 		    stateidp, stp, NULL, nd, p, nd->nd_repstat);
1423 	if (!nd->nd_repstat) {
1424 		if (ndp->ni_vp == NULL) {
1425 			vrele(ndp->ni_startdir);
1426 			nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1427 			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1428 			vput(ndp->ni_dvp);
1429 			nfsvno_relpathbuf(ndp);
1430 			if (!nd->nd_repstat) {
1431 				if (*exclusive_flagp) {
1432 					*exclusive_flagp = 0;
1433 					NFSVNO_ATTRINIT(nvap);
1434 					nvap->na_atime.tv_sec = cverf[0];
1435 					nvap->na_atime.tv_nsec = cverf[1];
1436 					nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1437 					    &nvap->na_vattr, cred);
1438 					if (nd->nd_repstat != 0) {
1439 						vput(ndp->ni_vp);
1440 						ndp->ni_vp = NULL;
1441 						nd->nd_repstat = NFSERR_NOTSUPP;
1442 					} else
1443 						NFSSETBIT_ATTRBIT(attrbitp,
1444 						    NFSATTRBIT_TIMEACCESS);
1445 				} else {
1446 					nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1447 					    aclp, p, attrbitp, exp);
1448 				}
1449 			}
1450 			vp = ndp->ni_vp;
1451 		} else {
1452 			if (ndp->ni_startdir)
1453 				vrele(ndp->ni_startdir);
1454 			nfsvno_relpathbuf(ndp);
1455 			vp = ndp->ni_vp;
1456 			if (create == NFSV4OPEN_CREATE) {
1457 				if (ndp->ni_dvp == vp)
1458 					vrele(ndp->ni_dvp);
1459 				else
1460 					vput(ndp->ni_dvp);
1461 			}
1462 			if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1463 				if (ndp->ni_cnd.cn_flags & RDONLY)
1464 					NFSVNO_SETEXRDONLY(&nes);
1465 				else
1466 					NFSVNO_EXINIT(&nes);
1467 				nd->nd_repstat = nfsvno_accchk(vp,
1468 				    VWRITE, cred, &nes, p,
1469 				    NFSACCCHK_NOOVERRIDE,
1470 				    NFSACCCHK_VPISLOCKED, NULL);
1471 				nd->nd_repstat = nfsrv_opencheck(clientid,
1472 				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1473 				if (!nd->nd_repstat) {
1474 					tempsize = nvap->na_size;
1475 					NFSVNO_ATTRINIT(nvap);
1476 					nvap->na_size = tempsize;
1477 					nd->nd_repstat = VOP_SETATTR(vp,
1478 					    &nvap->na_vattr, cred);
1479 				}
1480 			} else if (vp->v_type == VREG) {
1481 				nd->nd_repstat = nfsrv_opencheck(clientid,
1482 				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1483 			}
1484 		}
1485 	} else {
1486 		if (ndp->ni_cnd.cn_flags & HASBUF)
1487 			nfsvno_relpathbuf(ndp);
1488 		if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1489 			vrele(ndp->ni_startdir);
1490 			if (ndp->ni_dvp == ndp->ni_vp)
1491 				vrele(ndp->ni_dvp);
1492 			else
1493 				vput(ndp->ni_dvp);
1494 			if (ndp->ni_vp)
1495 				vput(ndp->ni_vp);
1496 		}
1497 	}
1498 	*vpp = vp;
1499 
1500 	NFSEXITCODE2(0, nd);
1501 }
1502 
1503 /*
1504  * Updates the file rev and sets the mtime and ctime
1505  * to the current clock time, returning the va_filerev and va_Xtime
1506  * values.
1507  * Return ESTALE to indicate the vnode is VI_DOOMED.
1508  */
1509 int
1510 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1511     struct ucred *cred, struct thread *p)
1512 {
1513 	struct vattr va;
1514 
1515 	VATTR_NULL(&va);
1516 	vfs_timestamp(&va.va_mtime);
1517 	if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1518 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1519 		if ((vp->v_iflag & VI_DOOMED) != 0)
1520 			return (ESTALE);
1521 	}
1522 	(void) VOP_SETATTR(vp, &va, cred);
1523 	(void) nfsvno_getattr(vp, nvap, cred, p, 1);
1524 	return (0);
1525 }
1526 
1527 /*
1528  * Glue routine to nfsv4_fillattr().
1529  */
1530 int
1531 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1532     struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1533     struct ucred *cred, struct thread *p, int isdgram, int reterr,
1534     int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1535 {
1536 	int error;
1537 
1538 	error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1539 	    attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1540 	    mounted_on_fileno);
1541 	NFSEXITCODE2(0, nd);
1542 	return (error);
1543 }
1544 
1545 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1546 /*
1547  * nfs readdir service
1548  * - mallocs what it thinks is enough to read
1549  *	count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1550  * - calls VOP_READDIR()
1551  * - loops around building the reply
1552  *	if the output generated exceeds count break out of loop
1553  *	The NFSM_CLGET macro is used here so that the reply will be packed
1554  *	tightly in mbuf clusters.
1555  * - it trims out records with d_fileno == 0
1556  *	this doesn't matter for Unix clients, but they might confuse clients
1557  *	for other os'.
1558  * - it trims out records with d_type == DT_WHT
1559  *	these cannot be seen through NFS (unless we extend the protocol)
1560  *     The alternate call nfsrvd_readdirplus() does lookups as well.
1561  * PS: The NFS protocol spec. does not clarify what the "count" byte
1562  *	argument is a count of.. just name strings and file id's or the
1563  *	entire reply rpc or ...
1564  *	I tried just file name and id sizes and it confused the Sun client,
1565  *	so I am using the full rpc size now. The "paranoia.." comment refers
1566  *	to including the status longwords that are not a part of the dir.
1567  *	"entry" structures, but are in the rpc.
1568  */
1569 int
1570 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1571     struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1572 {
1573 	struct dirent *dp;
1574 	u_int32_t *tl;
1575 	int dirlen;
1576 	char *cpos, *cend, *rbuf;
1577 	struct nfsvattr at;
1578 	int nlen, error = 0, getret = 1;
1579 	int siz, cnt, fullsiz, eofflag, ncookies;
1580 	u_int64_t off, toff, verf;
1581 	u_long *cookies = NULL, *cookiep;
1582 	struct uio io;
1583 	struct iovec iv;
1584 	int is_ufs;
1585 
1586 	if (nd->nd_repstat) {
1587 		nfsrv_postopattr(nd, getret, &at);
1588 		goto out;
1589 	}
1590 	if (nd->nd_flag & ND_NFSV2) {
1591 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1592 		off = fxdr_unsigned(u_quad_t, *tl++);
1593 	} else {
1594 		NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1595 		off = fxdr_hyper(tl);
1596 		tl += 2;
1597 		verf = fxdr_hyper(tl);
1598 		tl += 2;
1599 	}
1600 	toff = off;
1601 	cnt = fxdr_unsigned(int, *tl);
1602 	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1603 		cnt = NFS_SRVMAXDATA(nd);
1604 	siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1605 	fullsiz = siz;
1606 	if (nd->nd_flag & ND_NFSV3) {
1607 		nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1608 		    p, 1);
1609 #if 0
1610 		/*
1611 		 * va_filerev is not sufficient as a cookie verifier,
1612 		 * since it is not supposed to change when entries are
1613 		 * removed/added unless that offset cookies returned to
1614 		 * the client are no longer valid.
1615 		 */
1616 		if (!nd->nd_repstat && toff && verf != at.na_filerev)
1617 			nd->nd_repstat = NFSERR_BAD_COOKIE;
1618 #endif
1619 	}
1620 	if (!nd->nd_repstat && vp->v_type != VDIR)
1621 		nd->nd_repstat = NFSERR_NOTDIR;
1622 	if (nd->nd_repstat == 0 && cnt == 0) {
1623 		if (nd->nd_flag & ND_NFSV2)
1624 			/* NFSv2 does not have NFSERR_TOOSMALL */
1625 			nd->nd_repstat = EPERM;
1626 		else
1627 			nd->nd_repstat = NFSERR_TOOSMALL;
1628 	}
1629 	if (!nd->nd_repstat)
1630 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1631 		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1632 		    NFSACCCHK_VPISLOCKED, NULL);
1633 	if (nd->nd_repstat) {
1634 		vput(vp);
1635 		if (nd->nd_flag & ND_NFSV3)
1636 			nfsrv_postopattr(nd, getret, &at);
1637 		goto out;
1638 	}
1639 	is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1640 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1641 again:
1642 	eofflag = 0;
1643 	if (cookies) {
1644 		free((caddr_t)cookies, M_TEMP);
1645 		cookies = NULL;
1646 	}
1647 
1648 	iv.iov_base = rbuf;
1649 	iv.iov_len = siz;
1650 	io.uio_iov = &iv;
1651 	io.uio_iovcnt = 1;
1652 	io.uio_offset = (off_t)off;
1653 	io.uio_resid = siz;
1654 	io.uio_segflg = UIO_SYSSPACE;
1655 	io.uio_rw = UIO_READ;
1656 	io.uio_td = NULL;
1657 	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1658 	    &cookies);
1659 	off = (u_int64_t)io.uio_offset;
1660 	if (io.uio_resid)
1661 		siz -= io.uio_resid;
1662 
1663 	if (!cookies && !nd->nd_repstat)
1664 		nd->nd_repstat = NFSERR_PERM;
1665 	if (nd->nd_flag & ND_NFSV3) {
1666 		getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1667 		if (!nd->nd_repstat)
1668 			nd->nd_repstat = getret;
1669 	}
1670 
1671 	/*
1672 	 * Handles the failed cases. nd->nd_repstat == 0 past here.
1673 	 */
1674 	if (nd->nd_repstat) {
1675 		vput(vp);
1676 		free((caddr_t)rbuf, M_TEMP);
1677 		if (cookies)
1678 			free((caddr_t)cookies, M_TEMP);
1679 		if (nd->nd_flag & ND_NFSV3)
1680 			nfsrv_postopattr(nd, getret, &at);
1681 		goto out;
1682 	}
1683 	/*
1684 	 * If nothing read, return eof
1685 	 * rpc reply
1686 	 */
1687 	if (siz == 0) {
1688 		vput(vp);
1689 		if (nd->nd_flag & ND_NFSV2) {
1690 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1691 		} else {
1692 			nfsrv_postopattr(nd, getret, &at);
1693 			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1694 			txdr_hyper(at.na_filerev, tl);
1695 			tl += 2;
1696 		}
1697 		*tl++ = newnfs_false;
1698 		*tl = newnfs_true;
1699 		FREE((caddr_t)rbuf, M_TEMP);
1700 		FREE((caddr_t)cookies, M_TEMP);
1701 		goto out;
1702 	}
1703 
1704 	/*
1705 	 * Check for degenerate cases of nothing useful read.
1706 	 * If so go try again
1707 	 */
1708 	cpos = rbuf;
1709 	cend = rbuf + siz;
1710 	dp = (struct dirent *)cpos;
1711 	cookiep = cookies;
1712 
1713 	/*
1714 	 * For some reason FreeBSD's ufs_readdir() chooses to back the
1715 	 * directory offset up to a block boundary, so it is necessary to
1716 	 * skip over the records that precede the requested offset. This
1717 	 * requires the assumption that file offset cookies monotonically
1718 	 * increase.
1719 	 */
1720 	while (cpos < cend && ncookies > 0 &&
1721 	    (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1722 	     (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1723 		cpos += dp->d_reclen;
1724 		dp = (struct dirent *)cpos;
1725 		cookiep++;
1726 		ncookies--;
1727 	}
1728 	if (cpos >= cend || ncookies == 0) {
1729 		siz = fullsiz;
1730 		toff = off;
1731 		goto again;
1732 	}
1733 	vput(vp);
1734 
1735 	/*
1736 	 * dirlen is the size of the reply, including all XDR and must
1737 	 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1738 	 * if the XDR should be included in "count", but to be safe, we do.
1739 	 * (Include the two booleans at the end of the reply in dirlen now.)
1740 	 */
1741 	if (nd->nd_flag & ND_NFSV3) {
1742 		nfsrv_postopattr(nd, getret, &at);
1743 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1744 		txdr_hyper(at.na_filerev, tl);
1745 		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1746 	} else {
1747 		dirlen = 2 * NFSX_UNSIGNED;
1748 	}
1749 
1750 	/* Loop through the records and build reply */
1751 	while (cpos < cend && ncookies > 0) {
1752 		nlen = dp->d_namlen;
1753 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1754 			nlen <= NFS_MAXNAMLEN) {
1755 			if (nd->nd_flag & ND_NFSV3)
1756 				dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1757 			else
1758 				dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1759 			if (dirlen > cnt) {
1760 				eofflag = 0;
1761 				break;
1762 			}
1763 
1764 			/*
1765 			 * Build the directory record xdr from
1766 			 * the dirent entry.
1767 			 */
1768 			if (nd->nd_flag & ND_NFSV3) {
1769 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1770 				*tl++ = newnfs_true;
1771 				*tl++ = 0;
1772 			} else {
1773 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1774 				*tl++ = newnfs_true;
1775 			}
1776 			*tl = txdr_unsigned(dp->d_fileno);
1777 			(void) nfsm_strtom(nd, dp->d_name, nlen);
1778 			if (nd->nd_flag & ND_NFSV3) {
1779 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1780 				*tl++ = 0;
1781 			} else
1782 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1783 			*tl = txdr_unsigned(*cookiep);
1784 		}
1785 		cpos += dp->d_reclen;
1786 		dp = (struct dirent *)cpos;
1787 		cookiep++;
1788 		ncookies--;
1789 	}
1790 	if (cpos < cend)
1791 		eofflag = 0;
1792 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1793 	*tl++ = newnfs_false;
1794 	if (eofflag)
1795 		*tl = newnfs_true;
1796 	else
1797 		*tl = newnfs_false;
1798 	FREE((caddr_t)rbuf, M_TEMP);
1799 	FREE((caddr_t)cookies, M_TEMP);
1800 
1801 out:
1802 	NFSEXITCODE2(0, nd);
1803 	return (0);
1804 nfsmout:
1805 	vput(vp);
1806 	NFSEXITCODE2(error, nd);
1807 	return (error);
1808 }
1809 
1810 /*
1811  * Readdirplus for V3 and Readdir for V4.
1812  */
1813 int
1814 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1815     struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1816 {
1817 	struct dirent *dp;
1818 	u_int32_t *tl;
1819 	int dirlen;
1820 	char *cpos, *cend, *rbuf;
1821 	struct vnode *nvp;
1822 	fhandle_t nfh;
1823 	struct nfsvattr nva, at, *nvap = &nva;
1824 	struct mbuf *mb0, *mb1;
1825 	struct nfsreferral *refp;
1826 	int nlen, r, error = 0, getret = 1, usevget = 1;
1827 	int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1828 	caddr_t bpos0, bpos1;
1829 	u_int64_t off, toff, verf;
1830 	u_long *cookies = NULL, *cookiep;
1831 	nfsattrbit_t attrbits, rderrbits, savbits;
1832 	struct uio io;
1833 	struct iovec iv;
1834 	struct componentname cn;
1835 	int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
1836 	struct mount *mp, *new_mp;
1837 	uint64_t mounted_on_fileno;
1838 
1839 	if (nd->nd_repstat) {
1840 		nfsrv_postopattr(nd, getret, &at);
1841 		goto out;
1842 	}
1843 	NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1844 	off = fxdr_hyper(tl);
1845 	toff = off;
1846 	tl += 2;
1847 	verf = fxdr_hyper(tl);
1848 	tl += 2;
1849 	siz = fxdr_unsigned(int, *tl++);
1850 	cnt = fxdr_unsigned(int, *tl);
1851 
1852 	/*
1853 	 * Use the server's maximum data transfer size as the upper bound
1854 	 * on reply datalen.
1855 	 */
1856 	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1857 		cnt = NFS_SRVMAXDATA(nd);
1858 
1859 	/*
1860 	 * siz is a "hint" of how much directory information (name, fileid,
1861 	 * cookie) should be in the reply. At least one client "hints" 0,
1862 	 * so I set it to cnt for that case. I also round it up to the
1863 	 * next multiple of DIRBLKSIZ.
1864 	 */
1865 	if (siz <= 0)
1866 		siz = cnt;
1867 	siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1868 
1869 	if (nd->nd_flag & ND_NFSV4) {
1870 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1871 		if (error)
1872 			goto nfsmout;
1873 		NFSSET_ATTRBIT(&savbits, &attrbits);
1874 		NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1875 		NFSZERO_ATTRBIT(&rderrbits);
1876 		NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1877 	} else {
1878 		NFSZERO_ATTRBIT(&attrbits);
1879 	}
1880 	fullsiz = siz;
1881 	nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1882 	if (!nd->nd_repstat) {
1883 	    if (off && verf != at.na_filerev) {
1884 		/*
1885 		 * va_filerev is not sufficient as a cookie verifier,
1886 		 * since it is not supposed to change when entries are
1887 		 * removed/added unless that offset cookies returned to
1888 		 * the client are no longer valid.
1889 		 */
1890 #if 0
1891 		if (nd->nd_flag & ND_NFSV4) {
1892 			nd->nd_repstat = NFSERR_NOTSAME;
1893 		} else {
1894 			nd->nd_repstat = NFSERR_BAD_COOKIE;
1895 		}
1896 #endif
1897 	    } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1898 		nd->nd_repstat = NFSERR_BAD_COOKIE;
1899 	    }
1900 	}
1901 	if (!nd->nd_repstat && vp->v_type != VDIR)
1902 		nd->nd_repstat = NFSERR_NOTDIR;
1903 	if (!nd->nd_repstat && cnt == 0)
1904 		nd->nd_repstat = NFSERR_TOOSMALL;
1905 	if (!nd->nd_repstat)
1906 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1907 		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1908 		    NFSACCCHK_VPISLOCKED, NULL);
1909 	if (nd->nd_repstat) {
1910 		vput(vp);
1911 		if (nd->nd_flag & ND_NFSV3)
1912 			nfsrv_postopattr(nd, getret, &at);
1913 		goto out;
1914 	}
1915 	is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1916 	is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
1917 
1918 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1919 again:
1920 	eofflag = 0;
1921 	if (cookies) {
1922 		free((caddr_t)cookies, M_TEMP);
1923 		cookies = NULL;
1924 	}
1925 
1926 	iv.iov_base = rbuf;
1927 	iv.iov_len = siz;
1928 	io.uio_iov = &iv;
1929 	io.uio_iovcnt = 1;
1930 	io.uio_offset = (off_t)off;
1931 	io.uio_resid = siz;
1932 	io.uio_segflg = UIO_SYSSPACE;
1933 	io.uio_rw = UIO_READ;
1934 	io.uio_td = NULL;
1935 	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1936 	    &cookies);
1937 	off = (u_int64_t)io.uio_offset;
1938 	if (io.uio_resid)
1939 		siz -= io.uio_resid;
1940 
1941 	getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1942 
1943 	if (!cookies && !nd->nd_repstat)
1944 		nd->nd_repstat = NFSERR_PERM;
1945 	if (!nd->nd_repstat)
1946 		nd->nd_repstat = getret;
1947 	if (nd->nd_repstat) {
1948 		vput(vp);
1949 		if (cookies)
1950 			free((caddr_t)cookies, M_TEMP);
1951 		free((caddr_t)rbuf, M_TEMP);
1952 		if (nd->nd_flag & ND_NFSV3)
1953 			nfsrv_postopattr(nd, getret, &at);
1954 		goto out;
1955 	}
1956 	/*
1957 	 * If nothing read, return eof
1958 	 * rpc reply
1959 	 */
1960 	if (siz == 0) {
1961 		vput(vp);
1962 		if (nd->nd_flag & ND_NFSV3)
1963 			nfsrv_postopattr(nd, getret, &at);
1964 		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1965 		txdr_hyper(at.na_filerev, tl);
1966 		tl += 2;
1967 		*tl++ = newnfs_false;
1968 		*tl = newnfs_true;
1969 		free((caddr_t)cookies, M_TEMP);
1970 		free((caddr_t)rbuf, M_TEMP);
1971 		goto out;
1972 	}
1973 
1974 	/*
1975 	 * Check for degenerate cases of nothing useful read.
1976 	 * If so go try again
1977 	 */
1978 	cpos = rbuf;
1979 	cend = rbuf + siz;
1980 	dp = (struct dirent *)cpos;
1981 	cookiep = cookies;
1982 
1983 	/*
1984 	 * For some reason FreeBSD's ufs_readdir() chooses to back the
1985 	 * directory offset up to a block boundary, so it is necessary to
1986 	 * skip over the records that precede the requested offset. This
1987 	 * requires the assumption that file offset cookies monotonically
1988 	 * increase.
1989 	 */
1990 	while (cpos < cend && ncookies > 0 &&
1991 	  (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1992 	   (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
1993 	   ((nd->nd_flag & ND_NFSV4) &&
1994 	    ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1995 	     (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1996 		cpos += dp->d_reclen;
1997 		dp = (struct dirent *)cpos;
1998 		cookiep++;
1999 		ncookies--;
2000 	}
2001 	if (cpos >= cend || ncookies == 0) {
2002 		siz = fullsiz;
2003 		toff = off;
2004 		goto again;
2005 	}
2006 
2007 	/*
2008 	 * Busy the file system so that the mount point won't go away
2009 	 * and, as such, VFS_VGET() can be used safely.
2010 	 */
2011 	mp = vp->v_mount;
2012 	vfs_ref(mp);
2013 	NFSVOPUNLOCK(vp, 0);
2014 	nd->nd_repstat = vfs_busy(mp, 0);
2015 	vfs_rel(mp);
2016 	if (nd->nd_repstat != 0) {
2017 		vrele(vp);
2018 		free(cookies, M_TEMP);
2019 		free(rbuf, M_TEMP);
2020 		if (nd->nd_flag & ND_NFSV3)
2021 			nfsrv_postopattr(nd, getret, &at);
2022 		goto out;
2023 	}
2024 
2025 	/*
2026 	 * Check to see if entries in this directory can be safely acquired
2027 	 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2028 	 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2029 	 * automount of the snapshot directory that is required will
2030 	 * be done.
2031 	 * This needs to be done here for NFSv4, since NFSv4 never does
2032 	 * a VFS_VGET() for "." or "..".
2033 	 */
2034 	if (is_zfs == 1) {
2035 		r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2036 		if (r == EOPNOTSUPP) {
2037 			usevget = 0;
2038 			cn.cn_nameiop = LOOKUP;
2039 			cn.cn_lkflags = LK_SHARED | LK_RETRY;
2040 			cn.cn_cred = nd->nd_cred;
2041 			cn.cn_thread = p;
2042 		} else if (r == 0)
2043 			vput(nvp);
2044 	}
2045 
2046 	/*
2047 	 * Save this position, in case there is an error before one entry
2048 	 * is created.
2049 	 */
2050 	mb0 = nd->nd_mb;
2051 	bpos0 = nd->nd_bpos;
2052 
2053 	/*
2054 	 * Fill in the first part of the reply.
2055 	 * dirlen is the reply length in bytes and cannot exceed cnt.
2056 	 * (Include the two booleans at the end of the reply in dirlen now,
2057 	 *  so we recognize when we have exceeded cnt.)
2058 	 */
2059 	if (nd->nd_flag & ND_NFSV3) {
2060 		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2061 		nfsrv_postopattr(nd, getret, &at);
2062 	} else {
2063 		dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2064 	}
2065 	NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2066 	txdr_hyper(at.na_filerev, tl);
2067 
2068 	/*
2069 	 * Save this position, in case there is an empty reply needed.
2070 	 */
2071 	mb1 = nd->nd_mb;
2072 	bpos1 = nd->nd_bpos;
2073 
2074 	/* Loop through the records and build reply */
2075 	entrycnt = 0;
2076 	while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2077 		nlen = dp->d_namlen;
2078 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2079 		    nlen <= NFS_MAXNAMLEN &&
2080 		    ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2081 		     (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2082 		      || (nlen == 1 && dp->d_name[0] != '.'))) {
2083 			/*
2084 			 * Save the current position in the reply, in case
2085 			 * this entry exceeds cnt.
2086 			 */
2087 			mb1 = nd->nd_mb;
2088 			bpos1 = nd->nd_bpos;
2089 
2090 			/*
2091 			 * For readdir_and_lookup get the vnode using
2092 			 * the file number.
2093 			 */
2094 			nvp = NULL;
2095 			refp = NULL;
2096 			r = 0;
2097 			at_root = 0;
2098 			needs_unbusy = 0;
2099 			new_mp = mp;
2100 			mounted_on_fileno = (uint64_t)dp->d_fileno;
2101 			if ((nd->nd_flag & ND_NFSV3) ||
2102 			    NFSNONZERO_ATTRBIT(&savbits)) {
2103 				if (nd->nd_flag & ND_NFSV4)
2104 					refp = nfsv4root_getreferral(NULL,
2105 					    vp, dp->d_fileno);
2106 				if (refp == NULL) {
2107 					if (usevget)
2108 						r = VFS_VGET(mp, dp->d_fileno,
2109 						    LK_SHARED, &nvp);
2110 					else
2111 						r = EOPNOTSUPP;
2112 					if (r == EOPNOTSUPP) {
2113 						if (usevget) {
2114 							usevget = 0;
2115 							cn.cn_nameiop = LOOKUP;
2116 							cn.cn_lkflags =
2117 							    LK_SHARED |
2118 							    LK_RETRY;
2119 							cn.cn_cred =
2120 							    nd->nd_cred;
2121 							cn.cn_thread = p;
2122 						}
2123 						cn.cn_nameptr = dp->d_name;
2124 						cn.cn_namelen = nlen;
2125 						cn.cn_flags = ISLASTCN |
2126 						    NOFOLLOW | LOCKLEAF;
2127 						if (nlen == 2 &&
2128 						    dp->d_name[0] == '.' &&
2129 						    dp->d_name[1] == '.')
2130 							cn.cn_flags |=
2131 							    ISDOTDOT;
2132 						if (NFSVOPLOCK(vp, LK_SHARED)
2133 						    != 0) {
2134 							nd->nd_repstat = EPERM;
2135 							break;
2136 						}
2137 						if ((vp->v_vflag & VV_ROOT) != 0
2138 						    && (cn.cn_flags & ISDOTDOT)
2139 						    != 0) {
2140 							vref(vp);
2141 							nvp = vp;
2142 							r = 0;
2143 						} else {
2144 							r = VOP_LOOKUP(vp, &nvp,
2145 							    &cn);
2146 							if (vp != nvp)
2147 								NFSVOPUNLOCK(vp,
2148 								    0);
2149 						}
2150 					}
2151 
2152 					/*
2153 					 * For NFSv4, check to see if nvp is
2154 					 * a mount point and get the mount
2155 					 * point vnode, as required.
2156 					 */
2157 					if (r == 0 &&
2158 					    nfsrv_enable_crossmntpt != 0 &&
2159 					    (nd->nd_flag & ND_NFSV4) != 0 &&
2160 					    nvp->v_type == VDIR &&
2161 					    nvp->v_mountedhere != NULL) {
2162 						new_mp = nvp->v_mountedhere;
2163 						r = vfs_busy(new_mp, 0);
2164 						vput(nvp);
2165 						nvp = NULL;
2166 						if (r == 0) {
2167 							r = VFS_ROOT(new_mp,
2168 							    LK_SHARED, &nvp);
2169 							needs_unbusy = 1;
2170 							if (r == 0)
2171 								at_root = 1;
2172 						}
2173 					}
2174 				}
2175 				if (!r) {
2176 				    if (refp == NULL &&
2177 					((nd->nd_flag & ND_NFSV3) ||
2178 					 NFSNONZERO_ATTRBIT(&attrbits))) {
2179 					r = nfsvno_getfh(nvp, &nfh, p);
2180 					if (!r)
2181 					    r = nfsvno_getattr(nvp, nvap,
2182 						nd->nd_cred, p, 1);
2183 					if (r == 0 && is_zfs == 1 &&
2184 					    nfsrv_enable_crossmntpt != 0 &&
2185 					    (nd->nd_flag & ND_NFSV4) != 0 &&
2186 					    nvp->v_type == VDIR &&
2187 					    vp->v_mount != nvp->v_mount) {
2188 					    /*
2189 					     * For a ZFS snapshot, there is a
2190 					     * pseudo mount that does not set
2191 					     * v_mountedhere, so it needs to
2192 					     * be detected via a different
2193 					     * mount structure.
2194 					     */
2195 					    at_root = 1;
2196 					    if (new_mp == mp)
2197 						new_mp = nvp->v_mount;
2198 					}
2199 				    }
2200 				} else {
2201 				    nvp = NULL;
2202 				}
2203 				if (r) {
2204 					if (!NFSISSET_ATTRBIT(&attrbits,
2205 					    NFSATTRBIT_RDATTRERROR)) {
2206 						if (nvp != NULL)
2207 							vput(nvp);
2208 						if (needs_unbusy != 0)
2209 							vfs_unbusy(new_mp);
2210 						nd->nd_repstat = r;
2211 						break;
2212 					}
2213 				}
2214 			}
2215 
2216 			/*
2217 			 * Build the directory record xdr
2218 			 */
2219 			if (nd->nd_flag & ND_NFSV3) {
2220 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2221 				*tl++ = newnfs_true;
2222 				*tl++ = 0;
2223 				*tl = txdr_unsigned(dp->d_fileno);
2224 				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2225 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2226 				*tl++ = 0;
2227 				*tl = txdr_unsigned(*cookiep);
2228 				nfsrv_postopattr(nd, 0, nvap);
2229 				dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2230 				dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2231 				if (nvp != NULL)
2232 					vput(nvp);
2233 			} else {
2234 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2235 				*tl++ = newnfs_true;
2236 				*tl++ = 0;
2237 				*tl = txdr_unsigned(*cookiep);
2238 				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2239 				if (nvp != NULL) {
2240 					supports_nfsv4acls =
2241 					    nfs_supportsnfsv4acls(nvp);
2242 					NFSVOPUNLOCK(nvp, 0);
2243 				} else
2244 					supports_nfsv4acls = 0;
2245 				if (refp != NULL) {
2246 					dirlen += nfsrv_putreferralattr(nd,
2247 					    &savbits, refp, 0,
2248 					    &nd->nd_repstat);
2249 					if (nd->nd_repstat) {
2250 						if (nvp != NULL)
2251 							vrele(nvp);
2252 						if (needs_unbusy != 0)
2253 							vfs_unbusy(new_mp);
2254 						break;
2255 					}
2256 				} else if (r) {
2257 					dirlen += nfsvno_fillattr(nd, new_mp,
2258 					    nvp, nvap, &nfh, r, &rderrbits,
2259 					    nd->nd_cred, p, isdgram, 0,
2260 					    supports_nfsv4acls, at_root,
2261 					    mounted_on_fileno);
2262 				} else {
2263 					dirlen += nfsvno_fillattr(nd, new_mp,
2264 					    nvp, nvap, &nfh, r, &attrbits,
2265 					    nd->nd_cred, p, isdgram, 0,
2266 					    supports_nfsv4acls, at_root,
2267 					    mounted_on_fileno);
2268 				}
2269 				if (nvp != NULL)
2270 					vrele(nvp);
2271 				dirlen += (3 * NFSX_UNSIGNED);
2272 			}
2273 			if (needs_unbusy != 0)
2274 				vfs_unbusy(new_mp);
2275 			if (dirlen <= cnt)
2276 				entrycnt++;
2277 		}
2278 		cpos += dp->d_reclen;
2279 		dp = (struct dirent *)cpos;
2280 		cookiep++;
2281 		ncookies--;
2282 	}
2283 	vrele(vp);
2284 	vfs_unbusy(mp);
2285 
2286 	/*
2287 	 * If dirlen > cnt, we must strip off the last entry. If that
2288 	 * results in an empty reply, report NFSERR_TOOSMALL.
2289 	 */
2290 	if (dirlen > cnt || nd->nd_repstat) {
2291 		if (!nd->nd_repstat && entrycnt == 0)
2292 			nd->nd_repstat = NFSERR_TOOSMALL;
2293 		if (nd->nd_repstat) {
2294 			newnfs_trimtrailing(nd, mb0, bpos0);
2295 			if (nd->nd_flag & ND_NFSV3)
2296 				nfsrv_postopattr(nd, getret, &at);
2297 		} else
2298 			newnfs_trimtrailing(nd, mb1, bpos1);
2299 		eofflag = 0;
2300 	} else if (cpos < cend)
2301 		eofflag = 0;
2302 	if (!nd->nd_repstat) {
2303 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2304 		*tl++ = newnfs_false;
2305 		if (eofflag)
2306 			*tl = newnfs_true;
2307 		else
2308 			*tl = newnfs_false;
2309 	}
2310 	FREE((caddr_t)cookies, M_TEMP);
2311 	FREE((caddr_t)rbuf, M_TEMP);
2312 
2313 out:
2314 	NFSEXITCODE2(0, nd);
2315 	return (0);
2316 nfsmout:
2317 	vput(vp);
2318 	NFSEXITCODE2(error, nd);
2319 	return (error);
2320 }
2321 
2322 /*
2323  * Get the settable attributes out of the mbuf list.
2324  * (Return 0 or EBADRPC)
2325  */
2326 int
2327 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2328     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2329 {
2330 	u_int32_t *tl;
2331 	struct nfsv2_sattr *sp;
2332 	int error = 0, toclient = 0;
2333 
2334 	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2335 	case ND_NFSV2:
2336 		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2337 		/*
2338 		 * Some old clients didn't fill in the high order 16bits.
2339 		 * --> check the low order 2 bytes for 0xffff
2340 		 */
2341 		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2342 			nvap->na_mode = nfstov_mode(sp->sa_mode);
2343 		if (sp->sa_uid != newnfs_xdrneg1)
2344 			nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2345 		if (sp->sa_gid != newnfs_xdrneg1)
2346 			nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2347 		if (sp->sa_size != newnfs_xdrneg1)
2348 			nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2349 		if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2350 #ifdef notyet
2351 			fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2352 #else
2353 			nvap->na_atime.tv_sec =
2354 				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2355 			nvap->na_atime.tv_nsec = 0;
2356 #endif
2357 		}
2358 		if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2359 			fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2360 		break;
2361 	case ND_NFSV3:
2362 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2363 		if (*tl == newnfs_true) {
2364 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2365 			nvap->na_mode = nfstov_mode(*tl);
2366 		}
2367 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2368 		if (*tl == newnfs_true) {
2369 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2370 			nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2371 		}
2372 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2373 		if (*tl == newnfs_true) {
2374 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2375 			nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2376 		}
2377 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2378 		if (*tl == newnfs_true) {
2379 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2380 			nvap->na_size = fxdr_hyper(tl);
2381 		}
2382 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2383 		switch (fxdr_unsigned(int, *tl)) {
2384 		case NFSV3SATTRTIME_TOCLIENT:
2385 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2386 			fxdr_nfsv3time(tl, &nvap->na_atime);
2387 			toclient = 1;
2388 			break;
2389 		case NFSV3SATTRTIME_TOSERVER:
2390 			vfs_timestamp(&nvap->na_atime);
2391 			nvap->na_vaflags |= VA_UTIMES_NULL;
2392 			break;
2393 		}
2394 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2395 		switch (fxdr_unsigned(int, *tl)) {
2396 		case NFSV3SATTRTIME_TOCLIENT:
2397 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2398 			fxdr_nfsv3time(tl, &nvap->na_mtime);
2399 			nvap->na_vaflags &= ~VA_UTIMES_NULL;
2400 			break;
2401 		case NFSV3SATTRTIME_TOSERVER:
2402 			vfs_timestamp(&nvap->na_mtime);
2403 			if (!toclient)
2404 				nvap->na_vaflags |= VA_UTIMES_NULL;
2405 			break;
2406 		}
2407 		break;
2408 	case ND_NFSV4:
2409 		error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2410 	}
2411 nfsmout:
2412 	NFSEXITCODE2(error, nd);
2413 	return (error);
2414 }
2415 
2416 /*
2417  * Handle the setable attributes for V4.
2418  * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2419  */
2420 int
2421 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2422     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2423 {
2424 	u_int32_t *tl;
2425 	int attrsum = 0;
2426 	int i, j;
2427 	int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2428 	int toclient = 0;
2429 	u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2430 	uid_t uid;
2431 	gid_t gid;
2432 
2433 	error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2434 	if (error)
2435 		goto nfsmout;
2436 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2437 	attrsize = fxdr_unsigned(int, *tl);
2438 
2439 	/*
2440 	 * Loop around getting the setable attributes. If an unsupported
2441 	 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2442 	 */
2443 	if (retnotsup) {
2444 		nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2445 		bitpos = NFSATTRBIT_MAX;
2446 	} else {
2447 		bitpos = 0;
2448 	}
2449 	for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2450 	    if (attrsum > attrsize) {
2451 		error = NFSERR_BADXDR;
2452 		goto nfsmout;
2453 	    }
2454 	    if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2455 		switch (bitpos) {
2456 		case NFSATTRBIT_SIZE:
2457 			NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2458                      if (vp != NULL && vp->v_type != VREG) {
2459                             error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2460                                 NFSERR_INVAL;
2461                             goto nfsmout;
2462 			}
2463 			nvap->na_size = fxdr_hyper(tl);
2464 			attrsum += NFSX_HYPER;
2465 			break;
2466 		case NFSATTRBIT_ACL:
2467 			error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2468 			    p);
2469 			if (error)
2470 				goto nfsmout;
2471 			if (aceerr && !nd->nd_repstat)
2472 				nd->nd_repstat = aceerr;
2473 			attrsum += aclsize;
2474 			break;
2475 		case NFSATTRBIT_ARCHIVE:
2476 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2477 			if (!nd->nd_repstat)
2478 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2479 			attrsum += NFSX_UNSIGNED;
2480 			break;
2481 		case NFSATTRBIT_HIDDEN:
2482 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2483 			if (!nd->nd_repstat)
2484 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2485 			attrsum += NFSX_UNSIGNED;
2486 			break;
2487 		case NFSATTRBIT_MIMETYPE:
2488 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2489 			i = fxdr_unsigned(int, *tl);
2490 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2491 			if (error)
2492 				goto nfsmout;
2493 			if (!nd->nd_repstat)
2494 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2495 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2496 			break;
2497 		case NFSATTRBIT_MODE:
2498 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2499 			nvap->na_mode = nfstov_mode(*tl);
2500 			attrsum += NFSX_UNSIGNED;
2501 			break;
2502 		case NFSATTRBIT_OWNER:
2503 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2504 			j = fxdr_unsigned(int, *tl);
2505 			if (j < 0) {
2506 				error = NFSERR_BADXDR;
2507 				goto nfsmout;
2508 			}
2509 			if (j > NFSV4_SMALLSTR)
2510 				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2511 			else
2512 				cp = namestr;
2513 			error = nfsrv_mtostr(nd, cp, j);
2514 			if (error) {
2515 				if (j > NFSV4_SMALLSTR)
2516 					free(cp, M_NFSSTRING);
2517 				goto nfsmout;
2518 			}
2519 			if (!nd->nd_repstat) {
2520 				nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2521 				    p);
2522 				if (!nd->nd_repstat)
2523 					nvap->na_uid = uid;
2524 			}
2525 			if (j > NFSV4_SMALLSTR)
2526 				free(cp, M_NFSSTRING);
2527 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2528 			break;
2529 		case NFSATTRBIT_OWNERGROUP:
2530 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2531 			j = fxdr_unsigned(int, *tl);
2532 			if (j < 0) {
2533 				error = NFSERR_BADXDR;
2534 				goto nfsmout;
2535 			}
2536 			if (j > NFSV4_SMALLSTR)
2537 				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2538 			else
2539 				cp = namestr;
2540 			error = nfsrv_mtostr(nd, cp, j);
2541 			if (error) {
2542 				if (j > NFSV4_SMALLSTR)
2543 					free(cp, M_NFSSTRING);
2544 				goto nfsmout;
2545 			}
2546 			if (!nd->nd_repstat) {
2547 				nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2548 				    p);
2549 				if (!nd->nd_repstat)
2550 					nvap->na_gid = gid;
2551 			}
2552 			if (j > NFSV4_SMALLSTR)
2553 				free(cp, M_NFSSTRING);
2554 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2555 			break;
2556 		case NFSATTRBIT_SYSTEM:
2557 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2558 			if (!nd->nd_repstat)
2559 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2560 			attrsum += NFSX_UNSIGNED;
2561 			break;
2562 		case NFSATTRBIT_TIMEACCESSSET:
2563 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2564 			attrsum += NFSX_UNSIGNED;
2565 			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2566 			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2567 			    fxdr_nfsv4time(tl, &nvap->na_atime);
2568 			    toclient = 1;
2569 			    attrsum += NFSX_V4TIME;
2570 			} else {
2571 			    vfs_timestamp(&nvap->na_atime);
2572 			    nvap->na_vaflags |= VA_UTIMES_NULL;
2573 			}
2574 			break;
2575 		case NFSATTRBIT_TIMEBACKUP:
2576 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2577 			if (!nd->nd_repstat)
2578 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2579 			attrsum += NFSX_V4TIME;
2580 			break;
2581 		case NFSATTRBIT_TIMECREATE:
2582 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2583 			if (!nd->nd_repstat)
2584 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2585 			attrsum += NFSX_V4TIME;
2586 			break;
2587 		case NFSATTRBIT_TIMEMODIFYSET:
2588 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2589 			attrsum += NFSX_UNSIGNED;
2590 			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2591 			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2592 			    fxdr_nfsv4time(tl, &nvap->na_mtime);
2593 			    nvap->na_vaflags &= ~VA_UTIMES_NULL;
2594 			    attrsum += NFSX_V4TIME;
2595 			} else {
2596 			    vfs_timestamp(&nvap->na_mtime);
2597 			    if (!toclient)
2598 				nvap->na_vaflags |= VA_UTIMES_NULL;
2599 			}
2600 			break;
2601 		default:
2602 			nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2603 			/*
2604 			 * set bitpos so we drop out of the loop.
2605 			 */
2606 			bitpos = NFSATTRBIT_MAX;
2607 			break;
2608 		}
2609 	}
2610 
2611 	/*
2612 	 * some clients pad the attrlist, so we need to skip over the
2613 	 * padding.
2614 	 */
2615 	if (attrsum > attrsize) {
2616 		error = NFSERR_BADXDR;
2617 	} else {
2618 		attrsize = NFSM_RNDUP(attrsize);
2619 		if (attrsum < attrsize)
2620 			error = nfsm_advance(nd, attrsize - attrsum, -1);
2621 	}
2622 nfsmout:
2623 	NFSEXITCODE2(error, nd);
2624 	return (error);
2625 }
2626 
2627 /*
2628  * Check/setup export credentials.
2629  */
2630 int
2631 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2632     struct ucred *credanon)
2633 {
2634 	int error = 0;
2635 
2636 	/*
2637 	 * Check/setup credentials.
2638 	 */
2639 	if (nd->nd_flag & ND_GSS)
2640 		exp->nes_exflag &= ~MNT_EXPORTANON;
2641 
2642 	/*
2643 	 * Check to see if the operation is allowed for this security flavor.
2644 	 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2645 	 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2646 	 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2647 	 */
2648 	if (nfsvno_testexp(nd, exp) &&
2649 	    nd->nd_procnum != NFSV4OP_SECINFO &&
2650 	    nd->nd_procnum != NFSPROC_FSINFO) {
2651 		if (nd->nd_flag & ND_NFSV4)
2652 			error = NFSERR_WRONGSEC;
2653 		else
2654 			error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2655 		goto out;
2656 	}
2657 
2658 	/*
2659 	 * Check to see if the file system is exported V4 only.
2660 	 */
2661 	if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2662 		error = NFSERR_PROGNOTV4;
2663 		goto out;
2664 	}
2665 
2666 	/*
2667 	 * Now, map the user credentials.
2668 	 * (Note that ND_AUTHNONE will only be set for an NFSv3
2669 	 *  Fsinfo RPC. If set for anything else, this code might need
2670 	 *  to change.)
2671 	 */
2672 	if (NFSVNO_EXPORTED(exp)) {
2673 		if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2674 		     NFSVNO_EXPORTANON(exp) ||
2675 		     (nd->nd_flag & ND_AUTHNONE) != 0) {
2676 			nd->nd_cred->cr_uid = credanon->cr_uid;
2677 			nd->nd_cred->cr_gid = credanon->cr_gid;
2678 			crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2679 			    credanon->cr_groups);
2680 		} else if ((nd->nd_flag & ND_GSS) == 0) {
2681 			/*
2682 			 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2683 			 * if there is a replacement credential with a group
2684 			 * list set up by "nfsuserd -manage-gids".
2685 			 * If there is no replacement, nfsrv_getgrpscred()
2686 			 * simply returns its argument.
2687 			 */
2688 			nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2689 		}
2690 	}
2691 
2692 out:
2693 	NFSEXITCODE2(error, nd);
2694 	return (error);
2695 }
2696 
2697 /*
2698  * Check exports.
2699  */
2700 int
2701 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2702     struct ucred **credp)
2703 {
2704 	int i, error, *secflavors;
2705 
2706 	error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2707 	    &exp->nes_numsecflavor, &secflavors);
2708 	if (error) {
2709 		if (nfs_rootfhset) {
2710 			exp->nes_exflag = 0;
2711 			exp->nes_numsecflavor = 0;
2712 			error = 0;
2713 		}
2714 	} else {
2715 		/* Copy the security flavors. */
2716 		for (i = 0; i < exp->nes_numsecflavor; i++)
2717 			exp->nes_secflavors[i] = secflavors[i];
2718 	}
2719 	NFSEXITCODE(error);
2720 	return (error);
2721 }
2722 
2723 /*
2724  * Get a vnode for a file handle and export stuff.
2725  */
2726 int
2727 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2728     int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2729     struct ucred **credp)
2730 {
2731 	int i, error, *secflavors;
2732 
2733 	*credp = NULL;
2734 	exp->nes_numsecflavor = 0;
2735 	error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2736 	if (error != 0)
2737 		/* Make sure the server replies ESTALE to the client. */
2738 		error = ESTALE;
2739 	if (nam && !error) {
2740 		error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2741 		    &exp->nes_numsecflavor, &secflavors);
2742 		if (error) {
2743 			if (nfs_rootfhset) {
2744 				exp->nes_exflag = 0;
2745 				exp->nes_numsecflavor = 0;
2746 				error = 0;
2747 			} else {
2748 				vput(*vpp);
2749 			}
2750 		} else {
2751 			/* Copy the security flavors. */
2752 			for (i = 0; i < exp->nes_numsecflavor; i++)
2753 				exp->nes_secflavors[i] = secflavors[i];
2754 		}
2755 	}
2756 	NFSEXITCODE(error);
2757 	return (error);
2758 }
2759 
2760 /*
2761  * nfsd_fhtovp() - convert a fh to a vnode ptr
2762  * 	- look up fsid in mount list (if not found ret error)
2763  *	- get vp and export rights by calling nfsvno_fhtovp()
2764  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2765  *	  for AUTH_SYS
2766  *	- if mpp != NULL, return the mount point so that it can
2767  *	  be used for vn_finished_write() by the caller
2768  */
2769 void
2770 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2771     struct vnode **vpp, struct nfsexstuff *exp,
2772     struct mount **mpp, int startwrite, struct thread *p)
2773 {
2774 	struct mount *mp;
2775 	struct ucred *credanon;
2776 	fhandle_t *fhp;
2777 
2778 	fhp = (fhandle_t *)nfp->nfsrvfh_data;
2779 	/*
2780 	 * Check for the special case of the nfsv4root_fh.
2781 	 */
2782 	mp = vfs_busyfs(&fhp->fh_fsid);
2783 	if (mpp != NULL)
2784 		*mpp = mp;
2785 	if (mp == NULL) {
2786 		*vpp = NULL;
2787 		nd->nd_repstat = ESTALE;
2788 		goto out;
2789 	}
2790 
2791 	if (startwrite) {
2792 		vn_start_write(NULL, mpp, V_WAIT);
2793 		if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2794 			lktype = LK_EXCLUSIVE;
2795 	}
2796 	nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2797 	    &credanon);
2798 	vfs_unbusy(mp);
2799 
2800 	/*
2801 	 * For NFSv4 without a pseudo root fs, unexported file handles
2802 	 * can be returned, so that Lookup works everywhere.
2803 	 */
2804 	if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2805 	    !(nd->nd_flag & ND_NFSV4)) {
2806 		vput(*vpp);
2807 		nd->nd_repstat = EACCES;
2808 	}
2809 
2810 	/*
2811 	 * Personally, I've never seen any point in requiring a
2812 	 * reserved port#, since only in the rare case where the
2813 	 * clients are all boxes with secure system privileges,
2814 	 * does it provide any enhanced security, but... some people
2815 	 * believe it to be useful and keep putting this code back in.
2816 	 * (There is also some "security checker" out there that
2817 	 *  complains if the nfs server doesn't enforce this.)
2818 	 * However, note the following:
2819 	 * RFC3530 (NFSv4) specifies that a reserved port# not be
2820 	 *	required.
2821 	 * RFC2623 recommends that, if a reserved port# is checked for,
2822 	 *	that there be a way to turn that off--> ifdef'd.
2823 	 */
2824 #ifdef NFS_REQRSVPORT
2825 	if (!nd->nd_repstat) {
2826 		struct sockaddr_in *saddr;
2827 		struct sockaddr_in6 *saddr6;
2828 
2829 		saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2830 		saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2831 		if (!(nd->nd_flag & ND_NFSV4) &&
2832 		    ((saddr->sin_family == AF_INET &&
2833 		      ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2834 		     (saddr6->sin6_family == AF_INET6 &&
2835 		      ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2836 			vput(*vpp);
2837 			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2838 		}
2839 	}
2840 #endif	/* NFS_REQRSVPORT */
2841 
2842 	/*
2843 	 * Check/setup credentials.
2844 	 */
2845 	if (!nd->nd_repstat) {
2846 		nd->nd_saveduid = nd->nd_cred->cr_uid;
2847 		nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2848 		if (nd->nd_repstat)
2849 			vput(*vpp);
2850 	}
2851 	if (credanon != NULL)
2852 		crfree(credanon);
2853 	if (nd->nd_repstat) {
2854 		if (startwrite)
2855 			vn_finished_write(mp);
2856 		*vpp = NULL;
2857 		if (mpp != NULL)
2858 			*mpp = NULL;
2859 	}
2860 
2861 out:
2862 	NFSEXITCODE2(0, nd);
2863 }
2864 
2865 /*
2866  * glue for fp.
2867  */
2868 static int
2869 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2870 {
2871 	struct filedesc *fdp;
2872 	struct file *fp;
2873 	int error = 0;
2874 
2875 	fdp = p->td_proc->p_fd;
2876 	if (fd < 0 || fd >= fdp->fd_nfiles ||
2877 	    (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2878 		error = EBADF;
2879 		goto out;
2880 	}
2881 	*fpp = fp;
2882 
2883 out:
2884 	NFSEXITCODE(error);
2885 	return (error);
2886 }
2887 
2888 /*
2889  * Called from nfssvc() to update the exports list. Just call
2890  * vfs_export(). This has to be done, since the v4 root fake fs isn't
2891  * in the mount list.
2892  */
2893 int
2894 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2895 {
2896 	struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2897 	int error = 0;
2898 	struct nameidata nd;
2899 	fhandle_t fh;
2900 
2901 	error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2902 	if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2903 		nfs_rootfhset = 0;
2904 	else if (error == 0) {
2905 		if (nfsexargp->fspec == NULL) {
2906 			error = EPERM;
2907 			goto out;
2908 		}
2909 		/*
2910 		 * If fspec != NULL, this is the v4root path.
2911 		 */
2912 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2913 		    nfsexargp->fspec, p);
2914 		if ((error = namei(&nd)) != 0)
2915 			goto out;
2916 		error = nfsvno_getfh(nd.ni_vp, &fh, p);
2917 		vrele(nd.ni_vp);
2918 		if (!error) {
2919 			nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2920 			NFSBCOPY((caddr_t)&fh,
2921 			    nfs_rootfh.nfsrvfh_data,
2922 			    sizeof (fhandle_t));
2923 			nfs_rootfhset = 1;
2924 		}
2925 	}
2926 
2927 out:
2928 	NFSEXITCODE(error);
2929 	return (error);
2930 }
2931 
2932 /*
2933  * This function needs to test to see if the system is near its limit
2934  * for memory allocation via malloc() or mget() and return True iff
2935  * either of these resources are near their limit.
2936  * XXX (For now, this is just a stub.)
2937  */
2938 int nfsrv_testmalloclimit = 0;
2939 int
2940 nfsrv_mallocmget_limit(void)
2941 {
2942 	static int printmesg = 0;
2943 	static int testval = 1;
2944 
2945 	if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2946 		if ((printmesg++ % 100) == 0)
2947 			printf("nfsd: malloc/mget near limit\n");
2948 		return (1);
2949 	}
2950 	return (0);
2951 }
2952 
2953 /*
2954  * BSD specific initialization of a mount point.
2955  */
2956 void
2957 nfsd_mntinit(void)
2958 {
2959 	static int inited = 0;
2960 
2961 	if (inited)
2962 		return;
2963 	inited = 1;
2964 	nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2965 	TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2966 	TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2967 	nfsv4root_mnt.mnt_export = NULL;
2968 	TAILQ_INIT(&nfsv4root_opt);
2969 	TAILQ_INIT(&nfsv4root_newopt);
2970 	nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2971 	nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2972 	nfsv4root_mnt.mnt_nvnodelistsize = 0;
2973 	nfsv4root_mnt.mnt_activevnodelistsize = 0;
2974 }
2975 
2976 /*
2977  * Get a vnode for a file handle, without checking exports, etc.
2978  */
2979 struct vnode *
2980 nfsvno_getvp(fhandle_t *fhp)
2981 {
2982 	struct mount *mp;
2983 	struct vnode *vp;
2984 	int error;
2985 
2986 	mp = vfs_busyfs(&fhp->fh_fsid);
2987 	if (mp == NULL)
2988 		return (NULL);
2989 	error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2990 	vfs_unbusy(mp);
2991 	if (error)
2992 		return (NULL);
2993 	return (vp);
2994 }
2995 
2996 /*
2997  * Do a local VOP_ADVLOCK().
2998  */
2999 int
3000 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3001     u_int64_t end, struct thread *td)
3002 {
3003 	int error = 0;
3004 	struct flock fl;
3005 	u_int64_t tlen;
3006 
3007 	if (nfsrv_dolocallocks == 0)
3008 		goto out;
3009 	ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3010 
3011 	fl.l_whence = SEEK_SET;
3012 	fl.l_type = ftype;
3013 	fl.l_start = (off_t)first;
3014 	if (end == NFS64BITSSET) {
3015 		fl.l_len = 0;
3016 	} else {
3017 		tlen = end - first;
3018 		fl.l_len = (off_t)tlen;
3019 	}
3020 	/*
3021 	 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3022 	 * values for all calls, so that all locks will be held by the
3023 	 * nfsd server. (The nfsd server handles conflicts between the
3024 	 * various clients.)
3025 	 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3026 	 * bytes, so it can't be put in l_sysid.
3027 	 */
3028 	if (nfsv4_sysid == 0)
3029 		nfsv4_sysid = nlm_acquire_next_sysid();
3030 	fl.l_pid = (pid_t)0;
3031 	fl.l_sysid = (int)nfsv4_sysid;
3032 
3033 	if (ftype == F_UNLCK)
3034 		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3035 		    (F_POSIX | F_REMOTE));
3036 	else
3037 		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3038 		    (F_POSIX | F_REMOTE));
3039 
3040 out:
3041 	NFSEXITCODE(error);
3042 	return (error);
3043 }
3044 
3045 /*
3046  * Check the nfsv4 root exports.
3047  */
3048 int
3049 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3050 {
3051 	struct ucred *credanon;
3052 	int exflags, error = 0, numsecflavor, *secflavors, i;
3053 
3054 	error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3055 	    &credanon, &numsecflavor, &secflavors);
3056 	if (error) {
3057 		error = NFSERR_PROGUNAVAIL;
3058 		goto out;
3059 	}
3060 	if (credanon != NULL)
3061 		crfree(credanon);
3062 	for (i = 0; i < numsecflavor; i++) {
3063 		if (secflavors[i] == AUTH_SYS)
3064 			nd->nd_flag |= ND_EXAUTHSYS;
3065 		else if (secflavors[i] == RPCSEC_GSS_KRB5)
3066 			nd->nd_flag |= ND_EXGSS;
3067 		else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3068 			nd->nd_flag |= ND_EXGSSINTEGRITY;
3069 		else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3070 			nd->nd_flag |= ND_EXGSSPRIVACY;
3071 	}
3072 
3073 out:
3074 	NFSEXITCODE(error);
3075 	return (error);
3076 }
3077 
3078 /*
3079  * Nfs server pseudo system call for the nfsd's
3080  */
3081 /*
3082  * MPSAFE
3083  */
3084 static int
3085 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3086 {
3087 	struct file *fp;
3088 	struct nfsd_addsock_args sockarg;
3089 	struct nfsd_nfsd_args nfsdarg;
3090 	cap_rights_t rights;
3091 	int error;
3092 
3093 	if (uap->flag & NFSSVC_NFSDADDSOCK) {
3094 		error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3095 		if (error)
3096 			goto out;
3097 		/*
3098 		 * Since we don't know what rights might be required,
3099 		 * pretend that we need them all. It is better to be too
3100 		 * careful than too reckless.
3101 		 */
3102 		error = fget(td, sockarg.sock,
3103 		    cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3104 		if (error != 0)
3105 			goto out;
3106 		if (fp->f_type != DTYPE_SOCKET) {
3107 			fdrop(fp, td);
3108 			error = EPERM;
3109 			goto out;
3110 		}
3111 		error = nfsrvd_addsock(fp);
3112 		fdrop(fp, td);
3113 	} else if (uap->flag & NFSSVC_NFSDNFSD) {
3114 		if (uap->argp == NULL) {
3115 			error = EINVAL;
3116 			goto out;
3117 		}
3118 		error = copyin(uap->argp, (caddr_t)&nfsdarg,
3119 		    sizeof (nfsdarg));
3120 		if (error)
3121 			goto out;
3122 		error = nfsrvd_nfsd(td, &nfsdarg);
3123 	} else {
3124 		error = nfssvc_srvcall(td, uap, td->td_ucred);
3125 	}
3126 
3127 out:
3128 	NFSEXITCODE(error);
3129 	return (error);
3130 }
3131 
3132 static int
3133 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3134 {
3135 	struct nfsex_args export;
3136 	struct file *fp = NULL;
3137 	int stablefd, len;
3138 	struct nfsd_clid adminrevoke;
3139 	struct nfsd_dumplist dumplist;
3140 	struct nfsd_dumpclients *dumpclients;
3141 	struct nfsd_dumplocklist dumplocklist;
3142 	struct nfsd_dumplocks *dumplocks;
3143 	struct nameidata nd;
3144 	vnode_t vp;
3145 	int error = EINVAL, igotlock;
3146 	struct proc *procp;
3147 	static int suspend_nfsd = 0;
3148 
3149 	if (uap->flag & NFSSVC_PUBLICFH) {
3150 		NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3151 		    sizeof (fhandle_t));
3152 		error = copyin(uap->argp,
3153 		    &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3154 		if (!error)
3155 			nfs_pubfhset = 1;
3156 	} else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3157 		error = copyin(uap->argp,(caddr_t)&export,
3158 		    sizeof (struct nfsex_args));
3159 		if (!error)
3160 			error = nfsrv_v4rootexport(&export, cred, p);
3161 	} else if (uap->flag & NFSSVC_NOPUBLICFH) {
3162 		nfs_pubfhset = 0;
3163 		error = 0;
3164 	} else if (uap->flag & NFSSVC_STABLERESTART) {
3165 		error = copyin(uap->argp, (caddr_t)&stablefd,
3166 		    sizeof (int));
3167 		if (!error)
3168 			error = fp_getfvp(p, stablefd, &fp, &vp);
3169 		if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3170 			error = EBADF;
3171 		if (!error && newnfs_numnfsd != 0)
3172 			error = EPERM;
3173 		if (!error) {
3174 			nfsrv_stablefirst.nsf_fp = fp;
3175 			nfsrv_setupstable(p);
3176 		}
3177 	} else if (uap->flag & NFSSVC_ADMINREVOKE) {
3178 		error = copyin(uap->argp, (caddr_t)&adminrevoke,
3179 		    sizeof (struct nfsd_clid));
3180 		if (!error)
3181 			error = nfsrv_adminrevoke(&adminrevoke, p);
3182 	} else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3183 		error = copyin(uap->argp, (caddr_t)&dumplist,
3184 		    sizeof (struct nfsd_dumplist));
3185 		if (!error && (dumplist.ndl_size < 1 ||
3186 			dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3187 			error = EPERM;
3188 		if (!error) {
3189 		    len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3190 		    dumpclients = (struct nfsd_dumpclients *)malloc(len,
3191 			M_TEMP, M_WAITOK);
3192 		    nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3193 		    error = copyout(dumpclients,
3194 			CAST_USER_ADDR_T(dumplist.ndl_list), len);
3195 		    free((caddr_t)dumpclients, M_TEMP);
3196 		}
3197 	} else if (uap->flag & NFSSVC_DUMPLOCKS) {
3198 		error = copyin(uap->argp, (caddr_t)&dumplocklist,
3199 		    sizeof (struct nfsd_dumplocklist));
3200 		if (!error && (dumplocklist.ndllck_size < 1 ||
3201 			dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3202 			error = EPERM;
3203 		if (!error)
3204 			error = nfsrv_lookupfilename(&nd,
3205 				dumplocklist.ndllck_fname, p);
3206 		if (!error) {
3207 			len = sizeof (struct nfsd_dumplocks) *
3208 				dumplocklist.ndllck_size;
3209 			dumplocks = (struct nfsd_dumplocks *)malloc(len,
3210 				M_TEMP, M_WAITOK);
3211 			nfsrv_dumplocks(nd.ni_vp, dumplocks,
3212 			    dumplocklist.ndllck_size, p);
3213 			vput(nd.ni_vp);
3214 			error = copyout(dumplocks,
3215 			    CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3216 			free((caddr_t)dumplocks, M_TEMP);
3217 		}
3218 	} else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3219 		procp = p->td_proc;
3220 		PROC_LOCK(procp);
3221 		nfsd_master_pid = procp->p_pid;
3222 		bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3223 		nfsd_master_start = procp->p_stats->p_start;
3224 		nfsd_master_proc = procp;
3225 		PROC_UNLOCK(procp);
3226 	} else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3227 		NFSLOCKV4ROOTMUTEX();
3228 		if (suspend_nfsd == 0) {
3229 			/* Lock out all nfsd threads */
3230 			do {
3231 				igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3232 				    NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3233 			} while (igotlock == 0 && suspend_nfsd == 0);
3234 			suspend_nfsd = 1;
3235 		}
3236 		NFSUNLOCKV4ROOTMUTEX();
3237 		error = 0;
3238 	} else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3239 		NFSLOCKV4ROOTMUTEX();
3240 		if (suspend_nfsd != 0) {
3241 			nfsv4_unlock(&nfsd_suspend_lock, 0);
3242 			suspend_nfsd = 0;
3243 		}
3244 		NFSUNLOCKV4ROOTMUTEX();
3245 		error = 0;
3246 	}
3247 
3248 	NFSEXITCODE(error);
3249 	return (error);
3250 }
3251 
3252 /*
3253  * Check exports.
3254  * Returns 0 if ok, 1 otherwise.
3255  */
3256 int
3257 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3258 {
3259 	int i;
3260 
3261 	/*
3262 	 * This seems odd, but allow the case where the security flavor
3263 	 * list is empty. This happens when NFSv4 is traversing non-exported
3264 	 * file systems. Exported file systems should always have a non-empty
3265 	 * security flavor list.
3266 	 */
3267 	if (exp->nes_numsecflavor == 0)
3268 		return (0);
3269 
3270 	for (i = 0; i < exp->nes_numsecflavor; i++) {
3271 		/*
3272 		 * The tests for privacy and integrity must be first,
3273 		 * since ND_GSS is set for everything but AUTH_SYS.
3274 		 */
3275 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3276 		    (nd->nd_flag & ND_GSSPRIVACY))
3277 			return (0);
3278 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3279 		    (nd->nd_flag & ND_GSSINTEGRITY))
3280 			return (0);
3281 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3282 		    (nd->nd_flag & ND_GSS))
3283 			return (0);
3284 		if (exp->nes_secflavors[i] == AUTH_SYS &&
3285 		    (nd->nd_flag & ND_GSS) == 0)
3286 			return (0);
3287 	}
3288 	return (1);
3289 }
3290 
3291 /*
3292  * Calculate a hash value for the fid in a file handle.
3293  */
3294 uint32_t
3295 nfsrv_hashfh(fhandle_t *fhp)
3296 {
3297 	uint32_t hashval;
3298 
3299 	hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3300 	return (hashval);
3301 }
3302 
3303 /*
3304  * Calculate a hash value for the sessionid.
3305  */
3306 uint32_t
3307 nfsrv_hashsessionid(uint8_t *sessionid)
3308 {
3309 	uint32_t hashval;
3310 
3311 	hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3312 	return (hashval);
3313 }
3314 
3315 /*
3316  * Signal the userland master nfsd to backup the stable restart file.
3317  */
3318 void
3319 nfsrv_backupstable(void)
3320 {
3321 	struct proc *procp;
3322 
3323 	if (nfsd_master_proc != NULL) {
3324 		procp = pfind(nfsd_master_pid);
3325 		/* Try to make sure it is the correct process. */
3326 		if (procp == nfsd_master_proc &&
3327 		    procp->p_stats->p_start.tv_sec ==
3328 		    nfsd_master_start.tv_sec &&
3329 		    procp->p_stats->p_start.tv_usec ==
3330 		    nfsd_master_start.tv_usec &&
3331 		    strcmp(procp->p_comm, nfsd_master_comm) == 0)
3332 			kern_psignal(procp, SIGUSR2);
3333 		else
3334 			nfsd_master_proc = NULL;
3335 
3336 		if (procp != NULL)
3337 			PROC_UNLOCK(procp);
3338 	}
3339 }
3340 
3341 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3342 
3343 /*
3344  * Called once to initialize data structures...
3345  */
3346 static int
3347 nfsd_modevent(module_t mod, int type, void *data)
3348 {
3349 	int error = 0, i;
3350 	static int loaded = 0;
3351 
3352 	switch (type) {
3353 	case MOD_LOAD:
3354 		if (loaded)
3355 			goto out;
3356 		newnfs_portinit();
3357 		for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3358 			mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
3359 			    MTX_DEF);
3360 			mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
3361 			    MTX_DEF);
3362 		}
3363 		mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
3364 		mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
3365 		mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3366 		lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3367 		nfsrvd_initcache();
3368 		nfsd_init();
3369 		NFSD_LOCK();
3370 		nfsrvd_init(0);
3371 		NFSD_UNLOCK();
3372 		nfsd_mntinit();
3373 #ifdef VV_DISABLEDELEG
3374 		vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3375 		vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3376 #endif
3377 		nfsd_call_servertimer = nfsrv_servertimer;
3378 		nfsd_call_nfsd = nfssvc_nfsd;
3379 		loaded = 1;
3380 		break;
3381 
3382 	case MOD_UNLOAD:
3383 		if (newnfs_numnfsd != 0) {
3384 			error = EBUSY;
3385 			break;
3386 		}
3387 
3388 #ifdef VV_DISABLEDELEG
3389 		vn_deleg_ops.vndeleg_recall = NULL;
3390 		vn_deleg_ops.vndeleg_disable = NULL;
3391 #endif
3392 		nfsd_call_servertimer = NULL;
3393 		nfsd_call_nfsd = NULL;
3394 
3395 		/* Clean out all NFSv4 state. */
3396 		nfsrv_throwawayallstate(curthread);
3397 
3398 		/* Clean the NFS server reply cache */
3399 		nfsrvd_cleancache();
3400 
3401 		/* Free up the krpc server pool. */
3402 		if (nfsrvd_pool != NULL)
3403 			svcpool_destroy(nfsrvd_pool);
3404 
3405 		/* and get rid of the locks */
3406 		for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3407 			mtx_destroy(&nfsrchash_table[i].mtx);
3408 			mtx_destroy(&nfsrcahash_table[i].mtx);
3409 		}
3410 		mtx_destroy(&nfsrc_udpmtx);
3411 		mtx_destroy(&nfs_v4root_mutex);
3412 		mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3413 		for (i = 0; i < nfsrv_sessionhashsize; i++)
3414 			mtx_destroy(&nfssessionhash[i].mtx);
3415 		lockdestroy(&nfsv4root_mnt.mnt_explock);
3416 		free(nfsclienthash, M_NFSDCLIENT);
3417 		free(nfslockhash, M_NFSDLOCKFILE);
3418 		free(nfssessionhash, M_NFSDSESSION);
3419 		loaded = 0;
3420 		break;
3421 	default:
3422 		error = EOPNOTSUPP;
3423 		break;
3424 	}
3425 
3426 out:
3427 	NFSEXITCODE(error);
3428 	return (error);
3429 }
3430 static moduledata_t nfsd_mod = {
3431 	"nfsd",
3432 	nfsd_modevent,
3433 	NULL,
3434 };
3435 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3436 
3437 /* So that loader and kldload(2) can find us, wherever we are.. */
3438 MODULE_VERSION(nfsd, 1);
3439 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3440 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3441 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3442 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3443 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
3444 
3445