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