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