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