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