nfs_nfsdport.c revision 232683
1191783Srmacklem/*-
2191783Srmacklem * Copyright (c) 1989, 1993
3191783Srmacklem *	The Regents of the University of California.  All rights reserved.
4191783Srmacklem *
5191783Srmacklem * This code is derived from software contributed to Berkeley by
6191783Srmacklem * Rick Macklem at The University of Guelph.
7191783Srmacklem *
8191783Srmacklem * Redistribution and use in source and binary forms, with or without
9191783Srmacklem * modification, are permitted provided that the following conditions
10191783Srmacklem * are met:
11191783Srmacklem * 1. Redistributions of source code must retain the above copyright
12191783Srmacklem *    notice, this list of conditions and the following disclaimer.
13191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
14191783Srmacklem *    notice, this list of conditions and the following disclaimer in the
15191783Srmacklem *    documentation and/or other materials provided with the distribution.
16191783Srmacklem * 4. Neither the name of the University nor the names of its contributors
17191783Srmacklem *    may be used to endorse or promote products derived from this software
18191783Srmacklem *    without specific prior written permission.
19191783Srmacklem *
20191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23191783Srmacklem * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30191783Srmacklem * SUCH DAMAGE.
31191783Srmacklem *
32191783Srmacklem */
33191783Srmacklem
34191783Srmacklem#include <sys/cdefs.h>
35191783Srmacklem__FBSDID("$FreeBSD: stable/9/sys/fs/nfsserver/nfs_nfsdport.c 232683 2012-03-08 03:02:48Z rmacklem $");
36191783Srmacklem
37224778Srwatson#include <sys/capability.h>
38224778Srwatson
39191783Srmacklem/*
40191783Srmacklem * Functions that perform the vfs operations required by the routines in
41191783Srmacklem * nfsd_serv.c. It is hoped that this change will make the server more
42191783Srmacklem * portable.
43191783Srmacklem */
44191783Srmacklem
45191783Srmacklem#include <fs/nfs/nfsport.h>
46214255Srmacklem#include <sys/hash.h>
47191783Srmacklem#include <sys/sysctl.h>
48192503Srmacklem#include <nlm/nlm_prot.h>
49192503Srmacklem#include <nlm/nlm.h>
50191783Srmacklem
51219028SnetchildFEATURE(nfsd, "NFSv4 server");
52219028Snetchild
53191783Srmacklemextern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54191783Srmacklemextern int nfsrv_useacl;
55191783Srmacklemextern int newnfs_numnfsd;
56191783Srmacklemextern struct mount nfsv4root_mnt;
57191783Srmacklemextern struct nfsrv_stablefirst nfsrv_stablefirst;
58191783Srmacklemextern void (*nfsd_call_servertimer)(void);
59220530Srmacklemextern SVCPOOL	*nfsrvd_pool;
60191783Srmacklemstruct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
61191783SrmacklemNFSDLOCKMUTEX;
62191783Srmacklemstruct mtx nfs_cache_mutex;
63191783Srmacklemstruct mtx nfs_v4root_mutex;
64191783Srmacklemstruct nfsrvfh nfs_rootfh, nfs_pubfh;
65191783Srmacklemint nfs_pubfhset = 0, nfs_rootfhset = 0;
66217432Srmacklemstruct proc *nfsd_master_proc = NULL;
67217432Srmacklemstatic pid_t nfsd_master_pid = (pid_t)-1;
68217432Srmacklemstatic char nfsd_master_comm[MAXCOMLEN + 1];
69217432Srmacklemstatic struct timeval nfsd_master_start;
70192503Srmacklemstatic uint32_t nfsv4_sysid = 0;
71191783Srmacklem
72192255Srmacklemstatic int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
73192255Srmacklem    struct ucred *);
74191783Srmacklem
75220645Srmacklemint nfsrv_enable_crossmntpt = 1;
76191783Srmacklemstatic int nfs_commit_blks;
77191783Srmacklemstatic int nfs_commit_miss;
78191783Srmacklemextern int nfsrv_issuedelegs;
79192255Srmacklemextern int nfsrv_dolocallocks;
80192255Srmacklem
81221615SrmacklemSYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
82221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
83220645Srmacklem    &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
84221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
85192255Srmacklem    0, "");
86221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
87192255Srmacklem    0, "");
88221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
89192255Srmacklem    &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
90221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
91192255Srmacklem    &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
92191783Srmacklem
93229617Sjhb#define	MAX_REORDERED_RPC	16
94229617Sjhb#define	NUM_HEURISTIC		1031
95191783Srmacklem#define	NHUSE_INIT		64
96191783Srmacklem#define	NHUSE_INC		16
97191783Srmacklem#define	NHUSE_MAX		2048
98191783Srmacklem
99191783Srmacklemstatic struct nfsheur {
100191783Srmacklem	struct vnode *nh_vp;	/* vp to match (unreferenced pointer) */
101229617Sjhb	off_t nh_nextoff;	/* next offset for sequential detection */
102191783Srmacklem	int nh_use;		/* use count for selection */
103191783Srmacklem	int nh_seqcount;	/* heuristic */
104191783Srmacklem} nfsheur[NUM_HEURISTIC];
105191783Srmacklem
106191783Srmacklem
107191783Srmacklem/*
108229617Sjhb * Heuristic to detect sequential operation.
109229617Sjhb */
110229617Sjhbstatic struct nfsheur *
111229617Sjhbnfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
112229617Sjhb{
113229617Sjhb	struct nfsheur *nh;
114229617Sjhb	int hi, try;
115229617Sjhb
116229617Sjhb	/* Locate best candidate. */
117229617Sjhb	try = 32;
118229617Sjhb	hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
119229617Sjhb	nh = &nfsheur[hi];
120229617Sjhb	while (try--) {
121229617Sjhb		if (nfsheur[hi].nh_vp == vp) {
122229617Sjhb			nh = &nfsheur[hi];
123229617Sjhb			break;
124229617Sjhb		}
125229617Sjhb		if (nfsheur[hi].nh_use > 0)
126229617Sjhb			--nfsheur[hi].nh_use;
127229617Sjhb		hi = (hi + 1) % NUM_HEURISTIC;
128229617Sjhb		if (nfsheur[hi].nh_use < nh->nh_use)
129229617Sjhb			nh = &nfsheur[hi];
130229617Sjhb	}
131229617Sjhb
132229617Sjhb	/* Initialize hint if this is a new file. */
133229617Sjhb	if (nh->nh_vp != vp) {
134229617Sjhb		nh->nh_vp = vp;
135229617Sjhb		nh->nh_nextoff = uio->uio_offset;
136229617Sjhb		nh->nh_use = NHUSE_INIT;
137229617Sjhb		if (uio->uio_offset == 0)
138229617Sjhb			nh->nh_seqcount = 4;
139229617Sjhb		else
140229617Sjhb			nh->nh_seqcount = 1;
141229617Sjhb	}
142229617Sjhb
143229617Sjhb	/* Calculate heuristic. */
144229617Sjhb	if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
145229617Sjhb	    uio->uio_offset == nh->nh_nextoff) {
146229617Sjhb		/* See comments in vfs_vnops.c:sequential_heuristic(). */
147229617Sjhb		nh->nh_seqcount += howmany(uio->uio_resid, 16384);
148229617Sjhb		if (nh->nh_seqcount > IO_SEQMAX)
149229617Sjhb			nh->nh_seqcount = IO_SEQMAX;
150229617Sjhb	} else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
151229617Sjhb	    imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
152229617Sjhb		/* Probably a reordered RPC, leave seqcount alone. */
153229617Sjhb	} else if (nh->nh_seqcount > 1) {
154229617Sjhb		nh->nh_seqcount /= 2;
155229617Sjhb	} else {
156229617Sjhb		nh->nh_seqcount = 0;
157229617Sjhb	}
158229617Sjhb	nh->nh_use += NHUSE_INC;
159229617Sjhb	if (nh->nh_use > NHUSE_MAX)
160229617Sjhb		nh->nh_use = NHUSE_MAX;
161229617Sjhb	return (nh);
162229617Sjhb}
163229617Sjhb
164229617Sjhb/*
165191783Srmacklem * Get attributes into nfsvattr structure.
166191783Srmacklem */
167191783Srmacklemint
168191783Srmacklemnfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
169216693Srmacklem    struct thread *p, int vpislocked)
170191783Srmacklem{
171191783Srmacklem	int error, lockedit = 0;
172191783Srmacklem
173216693Srmacklem	if (vpislocked == 0) {
174216693Srmacklem		/*
175216693Srmacklem		 * When vpislocked == 0, the vnode is either exclusively
176216693Srmacklem		 * locked by this thread or not locked by this thread.
177216693Srmacklem		 * As such, shared lock it, if not exclusively locked.
178216693Srmacklem		 */
179224083Szack		if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
180216693Srmacklem			lockedit = 1;
181224081Szack			NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
182216693Srmacklem		}
183191783Srmacklem	}
184191783Srmacklem	error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
185216693Srmacklem	if (lockedit != 0)
186224082Szack		NFSVOPUNLOCK(vp, 0);
187224086Szack
188224086Szack	NFSEXITCODE(error);
189191783Srmacklem	return (error);
190191783Srmacklem}
191191783Srmacklem
192191783Srmacklem/*
193191783Srmacklem * Get a file handle for a vnode.
194191783Srmacklem */
195191783Srmacklemint
196191783Srmacklemnfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
197191783Srmacklem{
198191783Srmacklem	int error;
199191783Srmacklem
200191783Srmacklem	NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
201191783Srmacklem	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
202191783Srmacklem	error = VOP_VPTOFH(vp, &fhp->fh_fid);
203224086Szack
204224086Szack	NFSEXITCODE(error);
205191783Srmacklem	return (error);
206191783Srmacklem}
207191783Srmacklem
208191783Srmacklem/*
209191783Srmacklem * Perform access checking for vnodes obtained from file handles that would
210191783Srmacklem * refer to files already opened by a Unix client. You cannot just use
211200999Srmacklem * vn_writechk() and VOP_ACCESSX() for two reasons.
212200999Srmacklem * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
213200999Srmacklem *     case.
214191783Srmacklem * 2 - The owner is to be given access irrespective of mode bits for some
215191783Srmacklem *     operations, so that processes that chmod after opening a file don't
216191783Srmacklem *     break.
217191783Srmacklem */
218191783Srmacklemint
219200999Srmacklemnfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
220200999Srmacklem    struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
221200999Srmacklem    u_int32_t *supportedtypep)
222191783Srmacklem{
223191783Srmacklem	struct vattr vattr;
224191783Srmacklem	int error = 0, getret = 0;
225191783Srmacklem
226216893Srmacklem	if (vpislocked == 0) {
227224086Szack		if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
228224086Szack			error = EPERM;
229224086Szack			goto out;
230224086Szack		}
231216893Srmacklem	}
232191783Srmacklem	if (accmode & VWRITE) {
233191783Srmacklem		/* Just vn_writechk() changed to check rdonly */
234191783Srmacklem		/*
235191783Srmacklem		 * Disallow write attempts on read-only file systems;
236191783Srmacklem		 * unless the file is a socket or a block or character
237191783Srmacklem		 * device resident on the file system.
238191783Srmacklem		 */
239191783Srmacklem		if (NFSVNO_EXRDONLY(exp) ||
240200999Srmacklem		    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
241191783Srmacklem			switch (vp->v_type) {
242191783Srmacklem			case VREG:
243191783Srmacklem			case VDIR:
244191783Srmacklem			case VLNK:
245216893Srmacklem				error = EROFS;
246191783Srmacklem			default:
247191783Srmacklem				break;
248191783Srmacklem			}
249191783Srmacklem		}
250191783Srmacklem		/*
251191783Srmacklem		 * If there's shared text associated with
252191783Srmacklem		 * the inode, try to free it up once.  If
253191783Srmacklem		 * we fail, we can't allow writing.
254191783Srmacklem		 */
255216893Srmacklem		if ((vp->v_vflag & VV_TEXT) != 0 && error == 0)
256216893Srmacklem			error = ETXTBSY;
257191783Srmacklem	}
258216893Srmacklem	if (error != 0) {
259216893Srmacklem		if (vpislocked == 0)
260224082Szack			NFSVOPUNLOCK(vp, 0);
261224086Szack		goto out;
262216893Srmacklem	}
263191783Srmacklem
264191783Srmacklem	/*
265191783Srmacklem	 * Should the override still be applied when ACLs are enabled?
266191783Srmacklem	 */
267200999Srmacklem	error = VOP_ACCESSX(vp, accmode, cred, p);
268200999Srmacklem	if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
269200999Srmacklem		/*
270200999Srmacklem		 * Try again with VEXPLICIT_DENY, to see if the test for
271200999Srmacklem		 * deletion is supported.
272200999Srmacklem		 */
273200999Srmacklem		error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
274200999Srmacklem		if (error == 0) {
275200999Srmacklem			if (vp->v_type == VDIR) {
276200999Srmacklem				accmode &= ~(VDELETE | VDELETE_CHILD);
277200999Srmacklem				accmode |= VWRITE;
278200999Srmacklem				error = VOP_ACCESSX(vp, accmode, cred, p);
279200999Srmacklem			} else if (supportedtypep != NULL) {
280200999Srmacklem				*supportedtypep &= ~NFSACCESS_DELETE;
281200999Srmacklem			}
282200999Srmacklem		}
283200999Srmacklem	}
284191783Srmacklem
285191783Srmacklem	/*
286191783Srmacklem	 * Allow certain operations for the owner (reads and writes
287191783Srmacklem	 * on files that are already open).
288191783Srmacklem	 */
289191783Srmacklem	if (override != NFSACCCHK_NOOVERRIDE &&
290191783Srmacklem	    (error == EPERM || error == EACCES)) {
291191783Srmacklem		if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
292191783Srmacklem			error = 0;
293191783Srmacklem		else if (override & NFSACCCHK_ALLOWOWNER) {
294191783Srmacklem			getret = VOP_GETATTR(vp, &vattr, cred);
295191783Srmacklem			if (getret == 0 && cred->cr_uid == vattr.va_uid)
296191783Srmacklem				error = 0;
297191783Srmacklem		}
298191783Srmacklem	}
299191783Srmacklem	if (vpislocked == 0)
300224082Szack		NFSVOPUNLOCK(vp, 0);
301224086Szack
302224086Szackout:
303224086Szack	NFSEXITCODE(error);
304191783Srmacklem	return (error);
305191783Srmacklem}
306191783Srmacklem
307191783Srmacklem/*
308191783Srmacklem * Set attribute(s) vnop.
309191783Srmacklem */
310191783Srmacklemint
311191783Srmacklemnfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
312191783Srmacklem    struct thread *p, struct nfsexstuff *exp)
313191783Srmacklem{
314191783Srmacklem	int error;
315191783Srmacklem
316191783Srmacklem	error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
317224086Szack	NFSEXITCODE(error);
318191783Srmacklem	return (error);
319191783Srmacklem}
320191783Srmacklem
321191783Srmacklem/*
322232018Srmacklem * Set up nameidata for a lookup() call and do it.
323191783Srmacklem */
324191783Srmacklemint
325191783Srmacklemnfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
326191783Srmacklem    struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
327191783Srmacklem    struct vnode **retdirp)
328191783Srmacklem{
329191783Srmacklem	struct componentname *cnp = &ndp->ni_cnd;
330191783Srmacklem	int i;
331191783Srmacklem	struct iovec aiov;
332191783Srmacklem	struct uio auio;
333191783Srmacklem	int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
334191783Srmacklem	int error = 0, crossmnt;
335191783Srmacklem	char *cp;
336191783Srmacklem
337191783Srmacklem	*retdirp = NULL;
338191783Srmacklem	cnp->cn_nameptr = cnp->cn_pnbuf;
339225356Srmacklem	ndp->ni_strictrelative = 0;
340191783Srmacklem	/*
341191783Srmacklem	 * Extract and set starting directory.
342191783Srmacklem	 */
343191783Srmacklem	if (dp->v_type != VDIR) {
344191783Srmacklem		if (islocked)
345191783Srmacklem			vput(dp);
346191783Srmacklem		else
347191783Srmacklem			vrele(dp);
348191783Srmacklem		nfsvno_relpathbuf(ndp);
349224086Szack		error = ENOTDIR;
350224086Szack		goto out1;
351191783Srmacklem	}
352191783Srmacklem	if (islocked)
353224080Szack		NFSVOPUNLOCK(dp, 0);
354191783Srmacklem	VREF(dp);
355191783Srmacklem	*retdirp = dp;
356191783Srmacklem	if (NFSVNO_EXRDONLY(exp))
357191783Srmacklem		cnp->cn_flags |= RDONLY;
358191783Srmacklem	ndp->ni_segflg = UIO_SYSSPACE;
359191783Srmacklem	crossmnt = 1;
360191783Srmacklem
361191783Srmacklem	if (nd->nd_flag & ND_PUBLOOKUP) {
362191783Srmacklem		ndp->ni_loopcnt = 0;
363191783Srmacklem		if (cnp->cn_pnbuf[0] == '/') {
364191783Srmacklem			vrele(dp);
365191783Srmacklem			/*
366191783Srmacklem			 * Check for degenerate pathnames here, since lookup()
367191783Srmacklem			 * panics on them.
368191783Srmacklem			 */
369191783Srmacklem			for (i = 1; i < ndp->ni_pathlen; i++)
370191783Srmacklem				if (cnp->cn_pnbuf[i] != '/')
371191783Srmacklem					break;
372191783Srmacklem			if (i == ndp->ni_pathlen) {
373191783Srmacklem				error = NFSERR_ACCES;
374191783Srmacklem				goto out;
375191783Srmacklem			}
376191783Srmacklem			dp = rootvnode;
377191783Srmacklem			VREF(dp);
378191783Srmacklem		}
379220645Srmacklem	} else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
380191783Srmacklem	    (nd->nd_flag & ND_NFSV4) == 0) {
381191783Srmacklem		/*
382191783Srmacklem		 * Only cross mount points for NFSv4 when doing a
383191783Srmacklem		 * mount while traversing the file system above
384220645Srmacklem		 * the mount point, unless nfsrv_enable_crossmntpt is set.
385191783Srmacklem		 */
386191783Srmacklem		cnp->cn_flags |= NOCROSSMOUNT;
387191783Srmacklem		crossmnt = 0;
388191783Srmacklem	}
389191783Srmacklem
390191783Srmacklem	/*
391191783Srmacklem	 * Initialize for scan, set ni_startdir and bump ref on dp again
392191783Srmacklem	 * becuase lookup() will dereference ni_startdir.
393191783Srmacklem	 */
394191783Srmacklem
395191783Srmacklem	cnp->cn_thread = p;
396191783Srmacklem	ndp->ni_startdir = dp;
397191783Srmacklem	ndp->ni_rootdir = rootvnode;
398232683Srmacklem	ndp->ni_topdir = NULL;
399191783Srmacklem
400191783Srmacklem	if (!lockleaf)
401191783Srmacklem		cnp->cn_flags |= LOCKLEAF;
402191783Srmacklem	for (;;) {
403191783Srmacklem		cnp->cn_nameptr = cnp->cn_pnbuf;
404191783Srmacklem		/*
405191783Srmacklem		 * Call lookup() to do the real work.  If an error occurs,
406191783Srmacklem		 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
407191783Srmacklem		 * we do not have to dereference anything before returning.
408191783Srmacklem		 * In either case ni_startdir will be dereferenced and NULLed
409191783Srmacklem		 * out.
410191783Srmacklem		 */
411191783Srmacklem		error = lookup(ndp);
412191783Srmacklem		if (error)
413191783Srmacklem			break;
414191783Srmacklem
415191783Srmacklem		/*
416191783Srmacklem		 * Check for encountering a symbolic link.  Trivial
417191783Srmacklem		 * termination occurs if no symlink encountered.
418191783Srmacklem		 */
419191783Srmacklem		if ((cnp->cn_flags & ISSYMLINK) == 0) {
420191783Srmacklem			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
421191783Srmacklem				nfsvno_relpathbuf(ndp);
422191783Srmacklem			if (ndp->ni_vp && !lockleaf)
423224080Szack				NFSVOPUNLOCK(ndp->ni_vp, 0);
424191783Srmacklem			break;
425191783Srmacklem		}
426191783Srmacklem
427191783Srmacklem		/*
428191783Srmacklem		 * Validate symlink
429191783Srmacklem		 */
430191783Srmacklem		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
431224080Szack			NFSVOPUNLOCK(ndp->ni_dvp, 0);
432191783Srmacklem		if (!(nd->nd_flag & ND_PUBLOOKUP)) {
433191783Srmacklem			error = EINVAL;
434191783Srmacklem			goto badlink2;
435191783Srmacklem		}
436191783Srmacklem
437191783Srmacklem		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
438191783Srmacklem			error = ELOOP;
439191783Srmacklem			goto badlink2;
440191783Srmacklem		}
441191783Srmacklem		if (ndp->ni_pathlen > 1)
442191783Srmacklem			cp = uma_zalloc(namei_zone, M_WAITOK);
443191783Srmacklem		else
444191783Srmacklem			cp = cnp->cn_pnbuf;
445191783Srmacklem		aiov.iov_base = cp;
446191783Srmacklem		aiov.iov_len = MAXPATHLEN;
447191783Srmacklem		auio.uio_iov = &aiov;
448191783Srmacklem		auio.uio_iovcnt = 1;
449191783Srmacklem		auio.uio_offset = 0;
450191783Srmacklem		auio.uio_rw = UIO_READ;
451191783Srmacklem		auio.uio_segflg = UIO_SYSSPACE;
452191783Srmacklem		auio.uio_td = NULL;
453191783Srmacklem		auio.uio_resid = MAXPATHLEN;
454191783Srmacklem		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
455191783Srmacklem		if (error) {
456191783Srmacklem		badlink1:
457191783Srmacklem			if (ndp->ni_pathlen > 1)
458191783Srmacklem				uma_zfree(namei_zone, cp);
459191783Srmacklem		badlink2:
460191783Srmacklem			vrele(ndp->ni_dvp);
461191783Srmacklem			vput(ndp->ni_vp);
462191783Srmacklem			break;
463191783Srmacklem		}
464191783Srmacklem		linklen = MAXPATHLEN - auio.uio_resid;
465191783Srmacklem		if (linklen == 0) {
466191783Srmacklem			error = ENOENT;
467191783Srmacklem			goto badlink1;
468191783Srmacklem		}
469191783Srmacklem		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
470191783Srmacklem			error = ENAMETOOLONG;
471191783Srmacklem			goto badlink1;
472191783Srmacklem		}
473191783Srmacklem
474191783Srmacklem		/*
475191783Srmacklem		 * Adjust or replace path
476191783Srmacklem		 */
477191783Srmacklem		if (ndp->ni_pathlen > 1) {
478191783Srmacklem			NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
479191783Srmacklem			uma_zfree(namei_zone, cnp->cn_pnbuf);
480191783Srmacklem			cnp->cn_pnbuf = cp;
481191783Srmacklem		} else
482191783Srmacklem			cnp->cn_pnbuf[linklen] = '\0';
483191783Srmacklem		ndp->ni_pathlen += linklen;
484191783Srmacklem
485191783Srmacklem		/*
486191783Srmacklem		 * Cleanup refs for next loop and check if root directory
487191783Srmacklem		 * should replace current directory.  Normally ni_dvp
488191783Srmacklem		 * becomes the new base directory and is cleaned up when
489191783Srmacklem		 * we loop.  Explicitly null pointers after invalidation
490191783Srmacklem		 * to clarify operation.
491191783Srmacklem		 */
492191783Srmacklem		vput(ndp->ni_vp);
493191783Srmacklem		ndp->ni_vp = NULL;
494191783Srmacklem
495191783Srmacklem		if (cnp->cn_pnbuf[0] == '/') {
496191783Srmacklem			vrele(ndp->ni_dvp);
497191783Srmacklem			ndp->ni_dvp = ndp->ni_rootdir;
498191783Srmacklem			VREF(ndp->ni_dvp);
499191783Srmacklem		}
500191783Srmacklem		ndp->ni_startdir = ndp->ni_dvp;
501191783Srmacklem		ndp->ni_dvp = NULL;
502191783Srmacklem	}
503191783Srmacklem	if (!lockleaf)
504191783Srmacklem		cnp->cn_flags &= ~LOCKLEAF;
505191783Srmacklem
506191783Srmacklemout:
507191783Srmacklem	if (error) {
508191783Srmacklem		uma_zfree(namei_zone, cnp->cn_pnbuf);
509191783Srmacklem		ndp->ni_vp = NULL;
510191783Srmacklem		ndp->ni_dvp = NULL;
511191783Srmacklem		ndp->ni_startdir = NULL;
512191783Srmacklem		cnp->cn_flags &= ~HASBUF;
513191783Srmacklem	} else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
514191783Srmacklem		ndp->ni_dvp = NULL;
515191783Srmacklem	}
516224086Szack
517224086Szackout1:
518224086Szack	NFSEXITCODE2(error, nd);
519191783Srmacklem	return (error);
520191783Srmacklem}
521191783Srmacklem
522191783Srmacklem/*
523191783Srmacklem * Set up a pathname buffer and return a pointer to it and, optionally
524191783Srmacklem * set a hash pointer.
525191783Srmacklem */
526191783Srmacklemvoid
527191783Srmacklemnfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
528191783Srmacklem{
529191783Srmacklem	struct componentname *cnp = &ndp->ni_cnd;
530191783Srmacklem
531191783Srmacklem	cnp->cn_flags |= (NOMACCHECK | HASBUF);
532191783Srmacklem	cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
533191783Srmacklem	if (hashpp != NULL)
534191783Srmacklem		*hashpp = NULL;
535191783Srmacklem	*bufpp = cnp->cn_pnbuf;
536191783Srmacklem}
537191783Srmacklem
538191783Srmacklem/*
539191783Srmacklem * Release the above path buffer, if not released by nfsvno_namei().
540191783Srmacklem */
541191783Srmacklemvoid
542191783Srmacklemnfsvno_relpathbuf(struct nameidata *ndp)
543191783Srmacklem{
544191783Srmacklem
545191783Srmacklem	if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
546191783Srmacklem		panic("nfsrelpath");
547191783Srmacklem	uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
548191783Srmacklem	ndp->ni_cnd.cn_flags &= ~HASBUF;
549191783Srmacklem}
550191783Srmacklem
551191783Srmacklem/*
552191783Srmacklem * Readlink vnode op into an mbuf list.
553191783Srmacklem */
554191783Srmacklemint
555191783Srmacklemnfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
556191783Srmacklem    struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
557191783Srmacklem{
558191783Srmacklem	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
559191783Srmacklem	struct iovec *ivp = iv;
560191783Srmacklem	struct uio io, *uiop = &io;
561191783Srmacklem	struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
562224086Szack	int i, len, tlen, error = 0;
563191783Srmacklem
564191783Srmacklem	len = 0;
565191783Srmacklem	i = 0;
566191783Srmacklem	while (len < NFS_MAXPATHLEN) {
567191783Srmacklem		NFSMGET(mp);
568191783Srmacklem		MCLGET(mp, M_WAIT);
569191783Srmacklem		mp->m_len = NFSMSIZ(mp);
570191783Srmacklem		if (len == 0) {
571191783Srmacklem			mp3 = mp2 = mp;
572191783Srmacklem		} else {
573191783Srmacklem			mp2->m_next = mp;
574191783Srmacklem			mp2 = mp;
575191783Srmacklem		}
576191783Srmacklem		if ((len + mp->m_len) > NFS_MAXPATHLEN) {
577191783Srmacklem			mp->m_len = NFS_MAXPATHLEN - len;
578191783Srmacklem			len = NFS_MAXPATHLEN;
579191783Srmacklem		} else {
580191783Srmacklem			len += mp->m_len;
581191783Srmacklem		}
582191783Srmacklem		ivp->iov_base = mtod(mp, caddr_t);
583191783Srmacklem		ivp->iov_len = mp->m_len;
584191783Srmacklem		i++;
585191783Srmacklem		ivp++;
586191783Srmacklem	}
587191783Srmacklem	uiop->uio_iov = iv;
588191783Srmacklem	uiop->uio_iovcnt = i;
589191783Srmacklem	uiop->uio_offset = 0;
590191783Srmacklem	uiop->uio_resid = len;
591191783Srmacklem	uiop->uio_rw = UIO_READ;
592191783Srmacklem	uiop->uio_segflg = UIO_SYSSPACE;
593191783Srmacklem	uiop->uio_td = NULL;
594191783Srmacklem	error = VOP_READLINK(vp, uiop, cred);
595191783Srmacklem	if (error) {
596191783Srmacklem		m_freem(mp3);
597191783Srmacklem		*lenp = 0;
598224086Szack		goto out;
599191783Srmacklem	}
600191783Srmacklem	if (uiop->uio_resid > 0) {
601191783Srmacklem		len -= uiop->uio_resid;
602191783Srmacklem		tlen = NFSM_RNDUP(len);
603191783Srmacklem		nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
604191783Srmacklem	}
605191783Srmacklem	*lenp = len;
606191783Srmacklem	*mpp = mp3;
607191783Srmacklem	*mpendp = mp;
608224086Szack
609224086Szackout:
610224086Szack	NFSEXITCODE(error);
611224086Szack	return (error);
612191783Srmacklem}
613191783Srmacklem
614191783Srmacklem/*
615191783Srmacklem * Read vnode op call into mbuf list.
616191783Srmacklem */
617191783Srmacklemint
618191783Srmacklemnfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
619191783Srmacklem    struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
620191783Srmacklem{
621191783Srmacklem	struct mbuf *m;
622191783Srmacklem	int i;
623191783Srmacklem	struct iovec *iv;
624191783Srmacklem	struct iovec *iv2;
625229617Sjhb	int error = 0, len, left, siz, tlen, ioflag = 0;
626191783Srmacklem	struct mbuf *m2 = NULL, *m3;
627191783Srmacklem	struct uio io, *uiop = &io;
628191783Srmacklem	struct nfsheur *nh;
629191783Srmacklem
630191783Srmacklem	len = left = NFSM_RNDUP(cnt);
631191783Srmacklem	m3 = NULL;
632191783Srmacklem	/*
633191783Srmacklem	 * Generate the mbuf list with the uio_iov ref. to it.
634191783Srmacklem	 */
635191783Srmacklem	i = 0;
636191783Srmacklem	while (left > 0) {
637191783Srmacklem		NFSMGET(m);
638191783Srmacklem		MCLGET(m, M_WAIT);
639191783Srmacklem		m->m_len = 0;
640191783Srmacklem		siz = min(M_TRAILINGSPACE(m), left);
641191783Srmacklem		left -= siz;
642191783Srmacklem		i++;
643191783Srmacklem		if (m3)
644191783Srmacklem			m2->m_next = m;
645191783Srmacklem		else
646191783Srmacklem			m3 = m;
647191783Srmacklem		m2 = m;
648191783Srmacklem	}
649191783Srmacklem	MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
650191783Srmacklem	    M_TEMP, M_WAITOK);
651191783Srmacklem	uiop->uio_iov = iv2 = iv;
652191783Srmacklem	m = m3;
653191783Srmacklem	left = len;
654191783Srmacklem	i = 0;
655191783Srmacklem	while (left > 0) {
656191783Srmacklem		if (m == NULL)
657191783Srmacklem			panic("nfsvno_read iov");
658191783Srmacklem		siz = min(M_TRAILINGSPACE(m), left);
659191783Srmacklem		if (siz > 0) {
660191783Srmacklem			iv->iov_base = mtod(m, caddr_t) + m->m_len;
661191783Srmacklem			iv->iov_len = siz;
662191783Srmacklem			m->m_len += siz;
663191783Srmacklem			left -= siz;
664191783Srmacklem			iv++;
665191783Srmacklem			i++;
666191783Srmacklem		}
667191783Srmacklem		m = m->m_next;
668191783Srmacklem	}
669191783Srmacklem	uiop->uio_iovcnt = i;
670191783Srmacklem	uiop->uio_offset = off;
671191783Srmacklem	uiop->uio_resid = len;
672191783Srmacklem	uiop->uio_rw = UIO_READ;
673191783Srmacklem	uiop->uio_segflg = UIO_SYSSPACE;
674229617Sjhb	nh = nfsrv_sequential_heuristic(uiop, vp);
675229617Sjhb	ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
676191783Srmacklem	error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
677191783Srmacklem	FREE((caddr_t)iv2, M_TEMP);
678191783Srmacklem	if (error) {
679191783Srmacklem		m_freem(m3);
680191783Srmacklem		*mpp = NULL;
681224086Szack		goto out;
682191783Srmacklem	}
683229617Sjhb	nh->nh_nextoff = uiop->uio_offset;
684191783Srmacklem	tlen = len - uiop->uio_resid;
685191783Srmacklem	cnt = cnt < tlen ? cnt : tlen;
686191783Srmacklem	tlen = NFSM_RNDUP(cnt);
687191783Srmacklem	if (tlen == 0) {
688191783Srmacklem		m_freem(m3);
689191783Srmacklem		m3 = NULL;
690191783Srmacklem	} else if (len != tlen || tlen != cnt)
691191783Srmacklem		nfsrv_adj(m3, len - tlen, tlen - cnt);
692191783Srmacklem	*mpp = m3;
693191783Srmacklem	*mpendp = m2;
694224086Szack
695224086Szackout:
696224086Szack	NFSEXITCODE(error);
697224086Szack	return (error);
698191783Srmacklem}
699191783Srmacklem
700191783Srmacklem/*
701191783Srmacklem * Write vnode op from an mbuf list.
702191783Srmacklem */
703191783Srmacklemint
704191783Srmacklemnfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
705191783Srmacklem    struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
706191783Srmacklem{
707191783Srmacklem	struct iovec *ivp;
708191783Srmacklem	int i, len;
709191783Srmacklem	struct iovec *iv;
710191783Srmacklem	int ioflags, error;
711191783Srmacklem	struct uio io, *uiop = &io;
712229617Sjhb	struct nfsheur *nh;
713191783Srmacklem
714191783Srmacklem	MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
715191783Srmacklem	    M_WAITOK);
716191783Srmacklem	uiop->uio_iov = iv = ivp;
717191783Srmacklem	uiop->uio_iovcnt = cnt;
718191783Srmacklem	i = mtod(mp, caddr_t) + mp->m_len - cp;
719191783Srmacklem	len = retlen;
720191783Srmacklem	while (len > 0) {
721191783Srmacklem		if (mp == NULL)
722191783Srmacklem			panic("nfsvno_write");
723191783Srmacklem		if (i > 0) {
724191783Srmacklem			i = min(i, len);
725191783Srmacklem			ivp->iov_base = cp;
726191783Srmacklem			ivp->iov_len = i;
727191783Srmacklem			ivp++;
728191783Srmacklem			len -= i;
729191783Srmacklem		}
730191783Srmacklem		mp = mp->m_next;
731191783Srmacklem		if (mp) {
732191783Srmacklem			i = mp->m_len;
733191783Srmacklem			cp = mtod(mp, caddr_t);
734191783Srmacklem		}
735191783Srmacklem	}
736191783Srmacklem
737191783Srmacklem	if (stable == NFSWRITE_UNSTABLE)
738191783Srmacklem		ioflags = IO_NODELOCKED;
739191783Srmacklem	else
740191783Srmacklem		ioflags = (IO_SYNC | IO_NODELOCKED);
741191783Srmacklem	uiop->uio_resid = retlen;
742191783Srmacklem	uiop->uio_rw = UIO_WRITE;
743191783Srmacklem	uiop->uio_segflg = UIO_SYSSPACE;
744191783Srmacklem	NFSUIOPROC(uiop, p);
745191783Srmacklem	uiop->uio_offset = off;
746229617Sjhb	nh = nfsrv_sequential_heuristic(uiop, vp);
747229617Sjhb	ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
748191783Srmacklem	error = VOP_WRITE(vp, uiop, ioflags, cred);
749229617Sjhb	if (error == 0)
750229617Sjhb		nh->nh_nextoff = uiop->uio_offset;
751191783Srmacklem	FREE((caddr_t)iv, M_TEMP);
752224086Szack
753224086Szack	NFSEXITCODE(error);
754191783Srmacklem	return (error);
755191783Srmacklem}
756191783Srmacklem
757191783Srmacklem/*
758191783Srmacklem * Common code for creating a regular file (plus special files for V2).
759191783Srmacklem */
760191783Srmacklemint
761191783Srmacklemnfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
762191783Srmacklem    struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
763199616Srmacklem    int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
764191783Srmacklem{
765191783Srmacklem	u_quad_t tempsize;
766191783Srmacklem	int error;
767191783Srmacklem
768191783Srmacklem	error = nd->nd_repstat;
769191783Srmacklem	if (!error && ndp->ni_vp == NULL) {
770191783Srmacklem		if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
771191783Srmacklem			vrele(ndp->ni_startdir);
772191783Srmacklem			error = VOP_CREATE(ndp->ni_dvp,
773191783Srmacklem			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
774191783Srmacklem			vput(ndp->ni_dvp);
775191783Srmacklem			nfsvno_relpathbuf(ndp);
776191783Srmacklem			if (!error) {
777191783Srmacklem				if (*exclusive_flagp) {
778191783Srmacklem					*exclusive_flagp = 0;
779191783Srmacklem					NFSVNO_ATTRINIT(nvap);
780199616Srmacklem					nvap->na_atime.tv_sec = cverf[0];
781199616Srmacklem					nvap->na_atime.tv_nsec = cverf[1];
782191783Srmacklem					error = VOP_SETATTR(ndp->ni_vp,
783191783Srmacklem					    &nvap->na_vattr, nd->nd_cred);
784191783Srmacklem				}
785191783Srmacklem			}
786191783Srmacklem		/*
787191783Srmacklem		 * NFS V2 Only. nfsrvd_mknod() does this for V3.
788191783Srmacklem		 * (This implies, just get out on an error.)
789191783Srmacklem		 */
790191783Srmacklem		} else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
791191783Srmacklem			nvap->na_type == VFIFO) {
792191783Srmacklem			if (nvap->na_type == VCHR && rdev == 0xffffffff)
793191783Srmacklem				nvap->na_type = VFIFO;
794191783Srmacklem                        if (nvap->na_type != VFIFO &&
795191783Srmacklem			    (error = priv_check_cred(nd->nd_cred,
796191783Srmacklem			     PRIV_VFS_MKNOD_DEV, 0))) {
797191783Srmacklem				vrele(ndp->ni_startdir);
798191783Srmacklem				nfsvno_relpathbuf(ndp);
799191783Srmacklem				vput(ndp->ni_dvp);
800224086Szack				goto out;
801191783Srmacklem			}
802191783Srmacklem			nvap->na_rdev = rdev;
803191783Srmacklem			error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
804191783Srmacklem			    &ndp->ni_cnd, &nvap->na_vattr);
805191783Srmacklem			vput(ndp->ni_dvp);
806191783Srmacklem			nfsvno_relpathbuf(ndp);
807220546Srmacklem			vrele(ndp->ni_startdir);
808220546Srmacklem			if (error)
809224086Szack				goto out;
810191783Srmacklem		} else {
811191783Srmacklem			vrele(ndp->ni_startdir);
812191783Srmacklem			nfsvno_relpathbuf(ndp);
813191783Srmacklem			vput(ndp->ni_dvp);
814224086Szack			error = ENXIO;
815224086Szack			goto out;
816191783Srmacklem		}
817191783Srmacklem		*vpp = ndp->ni_vp;
818191783Srmacklem	} else {
819191783Srmacklem		/*
820191783Srmacklem		 * Handle cases where error is already set and/or
821191783Srmacklem		 * the file exists.
822191783Srmacklem		 * 1 - clean up the lookup
823191783Srmacklem		 * 2 - iff !error and na_size set, truncate it
824191783Srmacklem		 */
825191783Srmacklem		vrele(ndp->ni_startdir);
826191783Srmacklem		nfsvno_relpathbuf(ndp);
827191783Srmacklem		*vpp = ndp->ni_vp;
828191783Srmacklem		if (ndp->ni_dvp == *vpp)
829191783Srmacklem			vrele(ndp->ni_dvp);
830191783Srmacklem		else
831191783Srmacklem			vput(ndp->ni_dvp);
832191783Srmacklem		if (!error && nvap->na_size != VNOVAL) {
833200999Srmacklem			error = nfsvno_accchk(*vpp, VWRITE,
834191783Srmacklem			    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
835200999Srmacklem			    NFSACCCHK_VPISLOCKED, NULL);
836191783Srmacklem			if (!error) {
837191783Srmacklem				tempsize = nvap->na_size;
838191783Srmacklem				NFSVNO_ATTRINIT(nvap);
839191783Srmacklem				nvap->na_size = tempsize;
840191783Srmacklem				error = VOP_SETATTR(*vpp,
841191783Srmacklem				    &nvap->na_vattr, nd->nd_cred);
842191783Srmacklem			}
843191783Srmacklem		}
844191783Srmacklem		if (error)
845191783Srmacklem			vput(*vpp);
846191783Srmacklem	}
847224086Szack
848224086Szackout:
849224086Szack	NFSEXITCODE(error);
850191783Srmacklem	return (error);
851191783Srmacklem}
852191783Srmacklem
853191783Srmacklem/*
854191783Srmacklem * Do a mknod vnode op.
855191783Srmacklem */
856191783Srmacklemint
857191783Srmacklemnfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
858191783Srmacklem    struct thread *p)
859191783Srmacklem{
860191783Srmacklem	int error = 0;
861191783Srmacklem	enum vtype vtyp;
862191783Srmacklem
863191783Srmacklem	vtyp = nvap->na_type;
864191783Srmacklem	/*
865191783Srmacklem	 * Iff doesn't exist, create it.
866191783Srmacklem	 */
867191783Srmacklem	if (ndp->ni_vp) {
868191783Srmacklem		vrele(ndp->ni_startdir);
869191783Srmacklem		nfsvno_relpathbuf(ndp);
870191783Srmacklem		vput(ndp->ni_dvp);
871191783Srmacklem		vrele(ndp->ni_vp);
872224086Szack		error = EEXIST;
873224086Szack		goto out;
874191783Srmacklem	}
875191783Srmacklem	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
876191783Srmacklem		vrele(ndp->ni_startdir);
877191783Srmacklem		nfsvno_relpathbuf(ndp);
878191783Srmacklem		vput(ndp->ni_dvp);
879224086Szack		error = NFSERR_BADTYPE;
880224086Szack		goto out;
881191783Srmacklem	}
882191783Srmacklem	if (vtyp == VSOCK) {
883191783Srmacklem		vrele(ndp->ni_startdir);
884191783Srmacklem		error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
885191783Srmacklem		    &ndp->ni_cnd, &nvap->na_vattr);
886191783Srmacklem		vput(ndp->ni_dvp);
887191783Srmacklem		nfsvno_relpathbuf(ndp);
888191783Srmacklem	} else {
889191783Srmacklem		if (nvap->na_type != VFIFO &&
890191783Srmacklem		    (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
891191783Srmacklem			vrele(ndp->ni_startdir);
892191783Srmacklem			nfsvno_relpathbuf(ndp);
893191783Srmacklem			vput(ndp->ni_dvp);
894224086Szack			goto out;
895191783Srmacklem		}
896191783Srmacklem		error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
897191783Srmacklem		    &ndp->ni_cnd, &nvap->na_vattr);
898191783Srmacklem		vput(ndp->ni_dvp);
899191783Srmacklem		nfsvno_relpathbuf(ndp);
900216898Srmacklem		vrele(ndp->ni_startdir);
901191783Srmacklem		/*
902191783Srmacklem		 * Since VOP_MKNOD returns the ni_vp, I can't
903191783Srmacklem		 * see any reason to do the lookup.
904191783Srmacklem		 */
905191783Srmacklem	}
906224086Szack
907224086Szackout:
908224086Szack	NFSEXITCODE(error);
909191783Srmacklem	return (error);
910191783Srmacklem}
911191783Srmacklem
912191783Srmacklem/*
913191783Srmacklem * Mkdir vnode op.
914191783Srmacklem */
915191783Srmacklemint
916191783Srmacklemnfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
917191783Srmacklem    struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
918191783Srmacklem{
919191783Srmacklem	int error = 0;
920191783Srmacklem
921191783Srmacklem	if (ndp->ni_vp != NULL) {
922191783Srmacklem		if (ndp->ni_dvp == ndp->ni_vp)
923191783Srmacklem			vrele(ndp->ni_dvp);
924191783Srmacklem		else
925191783Srmacklem			vput(ndp->ni_dvp);
926191783Srmacklem		vrele(ndp->ni_vp);
927206063Srmacklem		nfsvno_relpathbuf(ndp);
928224086Szack		error = EEXIST;
929224086Szack		goto out;
930191783Srmacklem	}
931191783Srmacklem	error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
932191783Srmacklem	    &nvap->na_vattr);
933191783Srmacklem	vput(ndp->ni_dvp);
934191783Srmacklem	nfsvno_relpathbuf(ndp);
935224086Szack
936224086Szackout:
937224086Szack	NFSEXITCODE(error);
938191783Srmacklem	return (error);
939191783Srmacklem}
940191783Srmacklem
941191783Srmacklem/*
942191783Srmacklem * symlink vnode op.
943191783Srmacklem */
944191783Srmacklemint
945191783Srmacklemnfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
946191783Srmacklem    int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
947191783Srmacklem    struct nfsexstuff *exp)
948191783Srmacklem{
949191783Srmacklem	int error = 0;
950191783Srmacklem
951191783Srmacklem	if (ndp->ni_vp) {
952191783Srmacklem		vrele(ndp->ni_startdir);
953191783Srmacklem		nfsvno_relpathbuf(ndp);
954191783Srmacklem		if (ndp->ni_dvp == ndp->ni_vp)
955191783Srmacklem			vrele(ndp->ni_dvp);
956191783Srmacklem		else
957191783Srmacklem			vput(ndp->ni_dvp);
958191783Srmacklem		vrele(ndp->ni_vp);
959224086Szack		error = EEXIST;
960224086Szack		goto out;
961191783Srmacklem	}
962191783Srmacklem
963191783Srmacklem	error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
964191783Srmacklem	    &nvap->na_vattr, pathcp);
965191783Srmacklem	vput(ndp->ni_dvp);
966191783Srmacklem	vrele(ndp->ni_startdir);
967191783Srmacklem	nfsvno_relpathbuf(ndp);
968191783Srmacklem	/*
969191783Srmacklem	 * Although FreeBSD still had the lookup code in
970191783Srmacklem	 * it for 7/current, there doesn't seem to be any
971191783Srmacklem	 * point, since VOP_SYMLINK() returns the ni_vp.
972191783Srmacklem	 * Just vput it for v2.
973191783Srmacklem	 */
974191783Srmacklem	if (!not_v2 && !error)
975191783Srmacklem		vput(ndp->ni_vp);
976224086Szack
977224086Szackout:
978224086Szack	NFSEXITCODE(error);
979191783Srmacklem	return (error);
980191783Srmacklem}
981191783Srmacklem
982191783Srmacklem/*
983191783Srmacklem * Parse symbolic link arguments.
984191783Srmacklem * This function has an ugly side effect. It will MALLOC() an area for
985191783Srmacklem * the symlink and set iov_base to point to it, only if it succeeds.
986191783Srmacklem * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
987191783Srmacklem * be FREE'd later.
988191783Srmacklem */
989191783Srmacklemint
990191783Srmacklemnfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
991191783Srmacklem    struct thread *p, char **pathcpp, int *lenp)
992191783Srmacklem{
993191783Srmacklem	u_int32_t *tl;
994191783Srmacklem	char *pathcp = NULL;
995191783Srmacklem	int error = 0, len;
996191783Srmacklem	struct nfsv2_sattr *sp;
997191783Srmacklem
998191783Srmacklem	*pathcpp = NULL;
999191783Srmacklem	*lenp = 0;
1000191783Srmacklem	if ((nd->nd_flag & ND_NFSV3) &&
1001191783Srmacklem	    (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1002191783Srmacklem		goto nfsmout;
1003191783Srmacklem	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1004191783Srmacklem	len = fxdr_unsigned(int, *tl);
1005191783Srmacklem	if (len > NFS_MAXPATHLEN || len <= 0) {
1006191783Srmacklem		error = EBADRPC;
1007191783Srmacklem		goto nfsmout;
1008191783Srmacklem	}
1009191783Srmacklem	MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1010191783Srmacklem	error = nfsrv_mtostr(nd, pathcp, len);
1011191783Srmacklem	if (error)
1012191783Srmacklem		goto nfsmout;
1013191783Srmacklem	if (nd->nd_flag & ND_NFSV2) {
1014191783Srmacklem		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1015191783Srmacklem		nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1016191783Srmacklem	}
1017191783Srmacklem	*pathcpp = pathcp;
1018191783Srmacklem	*lenp = len;
1019224086Szack	NFSEXITCODE2(0, nd);
1020191783Srmacklem	return (0);
1021191783Srmacklemnfsmout:
1022191783Srmacklem	if (pathcp)
1023191783Srmacklem		free(pathcp, M_TEMP);
1024224086Szack	NFSEXITCODE2(error, nd);
1025191783Srmacklem	return (error);
1026191783Srmacklem}
1027191783Srmacklem
1028191783Srmacklem/*
1029191783Srmacklem * Remove a non-directory object.
1030191783Srmacklem */
1031191783Srmacklemint
1032191783Srmacklemnfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1033191783Srmacklem    struct thread *p, struct nfsexstuff *exp)
1034191783Srmacklem{
1035191783Srmacklem	struct vnode *vp;
1036191783Srmacklem	int error = 0;
1037191783Srmacklem
1038191783Srmacklem	vp = ndp->ni_vp;
1039191783Srmacklem	if (vp->v_type == VDIR)
1040191783Srmacklem		error = NFSERR_ISDIR;
1041191783Srmacklem	else if (is_v4)
1042191783Srmacklem		error = nfsrv_checkremove(vp, 1, p);
1043191783Srmacklem	if (!error)
1044191783Srmacklem		error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1045191783Srmacklem	if (ndp->ni_dvp == vp)
1046191783Srmacklem		vrele(ndp->ni_dvp);
1047191783Srmacklem	else
1048191783Srmacklem		vput(ndp->ni_dvp);
1049191783Srmacklem	vput(vp);
1050224086Szack	NFSEXITCODE(error);
1051191783Srmacklem	return (error);
1052191783Srmacklem}
1053191783Srmacklem
1054191783Srmacklem/*
1055191783Srmacklem * Remove a directory.
1056191783Srmacklem */
1057191783Srmacklemint
1058191783Srmacklemnfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1059191783Srmacklem    struct thread *p, struct nfsexstuff *exp)
1060191783Srmacklem{
1061191783Srmacklem	struct vnode *vp;
1062191783Srmacklem	int error = 0;
1063191783Srmacklem
1064191783Srmacklem	vp = ndp->ni_vp;
1065191783Srmacklem	if (vp->v_type != VDIR) {
1066191783Srmacklem		error = ENOTDIR;
1067191783Srmacklem		goto out;
1068191783Srmacklem	}
1069191783Srmacklem	/*
1070191783Srmacklem	 * No rmdir "." please.
1071191783Srmacklem	 */
1072191783Srmacklem	if (ndp->ni_dvp == vp) {
1073191783Srmacklem		error = EINVAL;
1074191783Srmacklem		goto out;
1075191783Srmacklem	}
1076191783Srmacklem	/*
1077191783Srmacklem	 * The root of a mounted filesystem cannot be deleted.
1078191783Srmacklem	 */
1079191783Srmacklem	if (vp->v_vflag & VV_ROOT)
1080191783Srmacklem		error = EBUSY;
1081191783Srmacklemout:
1082191783Srmacklem	if (!error)
1083191783Srmacklem		error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1084191783Srmacklem	if (ndp->ni_dvp == vp)
1085191783Srmacklem		vrele(ndp->ni_dvp);
1086191783Srmacklem	else
1087191783Srmacklem		vput(ndp->ni_dvp);
1088191783Srmacklem	vput(vp);
1089224086Szack	NFSEXITCODE(error);
1090191783Srmacklem	return (error);
1091191783Srmacklem}
1092191783Srmacklem
1093191783Srmacklem/*
1094191783Srmacklem * Rename vnode op.
1095191783Srmacklem */
1096191783Srmacklemint
1097191783Srmacklemnfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1098191783Srmacklem    u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1099191783Srmacklem{
1100191783Srmacklem	struct vnode *fvp, *tvp, *tdvp;
1101191783Srmacklem	int error = 0;
1102191783Srmacklem
1103191783Srmacklem	fvp = fromndp->ni_vp;
1104191783Srmacklem	if (ndstat) {
1105191783Srmacklem		vrele(fromndp->ni_dvp);
1106191783Srmacklem		vrele(fvp);
1107191783Srmacklem		error = ndstat;
1108191783Srmacklem		goto out1;
1109191783Srmacklem	}
1110191783Srmacklem	tdvp = tondp->ni_dvp;
1111191783Srmacklem	tvp = tondp->ni_vp;
1112191783Srmacklem	if (tvp != NULL) {
1113191783Srmacklem		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1114191783Srmacklem			error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1115191783Srmacklem			goto out;
1116191783Srmacklem		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1117191783Srmacklem			error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1118191783Srmacklem			goto out;
1119191783Srmacklem		}
1120191783Srmacklem		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1121191783Srmacklem			error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1122191783Srmacklem			goto out;
1123191783Srmacklem		}
1124191783Srmacklem
1125191783Srmacklem		/*
1126191783Srmacklem		 * A rename to '.' or '..' results in a prematurely
1127191783Srmacklem		 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1128191783Srmacklem		 * here.
1129191783Srmacklem		 */
1130191783Srmacklem		if ((tondp->ni_cnd.cn_namelen == 1 &&
1131191783Srmacklem		     tondp->ni_cnd.cn_nameptr[0] == '.') ||
1132191783Srmacklem		    (tondp->ni_cnd.cn_namelen == 2 &&
1133191783Srmacklem		     tondp->ni_cnd.cn_nameptr[0] == '.' &&
1134191783Srmacklem		     tondp->ni_cnd.cn_nameptr[1] == '.')) {
1135191783Srmacklem			error = EINVAL;
1136191783Srmacklem			goto out;
1137191783Srmacklem		}
1138191783Srmacklem	}
1139191783Srmacklem	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1140191783Srmacklem		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1141191783Srmacklem		goto out;
1142191783Srmacklem	}
1143191783Srmacklem	if (fvp->v_mount != tdvp->v_mount) {
1144191783Srmacklem		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1145191783Srmacklem		goto out;
1146191783Srmacklem	}
1147191783Srmacklem	if (fvp == tdvp) {
1148191783Srmacklem		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1149191783Srmacklem		goto out;
1150191783Srmacklem	}
1151191783Srmacklem	if (fvp == tvp) {
1152191783Srmacklem		/*
1153191783Srmacklem		 * If source and destination are the same, there is nothing to
1154191783Srmacklem		 * do. Set error to -1 to indicate this.
1155191783Srmacklem		 */
1156191783Srmacklem		error = -1;
1157191783Srmacklem		goto out;
1158191783Srmacklem	}
1159191783Srmacklem	if (ndflag & ND_NFSV4) {
1160224081Szack		if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1161216893Srmacklem			error = nfsrv_checkremove(fvp, 0, p);
1162224082Szack			NFSVOPUNLOCK(fvp, 0);
1163216893Srmacklem		} else
1164216893Srmacklem			error = EPERM;
1165191783Srmacklem		if (tvp && !error)
1166191783Srmacklem			error = nfsrv_checkremove(tvp, 1, p);
1167191783Srmacklem	} else {
1168191783Srmacklem		/*
1169191783Srmacklem		 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1170191783Srmacklem		 * that the NFSv4 client won't be confused by the rename.
1171191783Srmacklem		 * Since nfsd_recalldelegation() can only be called on an
1172191783Srmacklem		 * unlocked vnode at this point and fvp is the file that will
1173191783Srmacklem		 * still exist after the rename, just do fvp.
1174191783Srmacklem		 */
1175191783Srmacklem		nfsd_recalldelegation(fvp, p);
1176191783Srmacklem	}
1177191783Srmacklemout:
1178191783Srmacklem	if (!error) {
1179191783Srmacklem		error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1180191783Srmacklem		    &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1181191783Srmacklem		    &tondp->ni_cnd);
1182191783Srmacklem	} else {
1183191783Srmacklem		if (tdvp == tvp)
1184191783Srmacklem			vrele(tdvp);
1185191783Srmacklem		else
1186191783Srmacklem			vput(tdvp);
1187191783Srmacklem		if (tvp)
1188191783Srmacklem			vput(tvp);
1189191783Srmacklem		vrele(fromndp->ni_dvp);
1190191783Srmacklem		vrele(fvp);
1191191783Srmacklem		if (error == -1)
1192191783Srmacklem			error = 0;
1193191783Srmacklem	}
1194191783Srmacklem	vrele(tondp->ni_startdir);
1195191783Srmacklem	nfsvno_relpathbuf(tondp);
1196191783Srmacklemout1:
1197191783Srmacklem	vrele(fromndp->ni_startdir);
1198191783Srmacklem	nfsvno_relpathbuf(fromndp);
1199224086Szack	NFSEXITCODE(error);
1200191783Srmacklem	return (error);
1201191783Srmacklem}
1202191783Srmacklem
1203191783Srmacklem/*
1204191783Srmacklem * Link vnode op.
1205191783Srmacklem */
1206191783Srmacklemint
1207191783Srmacklemnfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1208191783Srmacklem    struct thread *p, struct nfsexstuff *exp)
1209191783Srmacklem{
1210191783Srmacklem	struct vnode *xp;
1211191783Srmacklem	int error = 0;
1212191783Srmacklem
1213191783Srmacklem	xp = ndp->ni_vp;
1214191783Srmacklem	if (xp != NULL) {
1215191783Srmacklem		error = EEXIST;
1216191783Srmacklem	} else {
1217191783Srmacklem		xp = ndp->ni_dvp;
1218191783Srmacklem		if (vp->v_mount != xp->v_mount)
1219191783Srmacklem			error = EXDEV;
1220191783Srmacklem	}
1221191783Srmacklem	if (!error) {
1222224081Szack		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1223216893Srmacklem		if ((vp->v_iflag & VI_DOOMED) == 0)
1224216893Srmacklem			error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1225216893Srmacklem		else
1226216893Srmacklem			error = EPERM;
1227191783Srmacklem		if (ndp->ni_dvp == vp)
1228191783Srmacklem			vrele(ndp->ni_dvp);
1229191783Srmacklem		else
1230191783Srmacklem			vput(ndp->ni_dvp);
1231224082Szack		NFSVOPUNLOCK(vp, 0);
1232191783Srmacklem	} else {
1233191783Srmacklem		if (ndp->ni_dvp == ndp->ni_vp)
1234191783Srmacklem			vrele(ndp->ni_dvp);
1235191783Srmacklem		else
1236191783Srmacklem			vput(ndp->ni_dvp);
1237191783Srmacklem		if (ndp->ni_vp)
1238191783Srmacklem			vrele(ndp->ni_vp);
1239191783Srmacklem	}
1240191783Srmacklem	nfsvno_relpathbuf(ndp);
1241224086Szack	NFSEXITCODE(error);
1242191783Srmacklem	return (error);
1243191783Srmacklem}
1244191783Srmacklem
1245191783Srmacklem/*
1246191783Srmacklem * Do the fsync() appropriate for the commit.
1247191783Srmacklem */
1248191783Srmacklemint
1249191783Srmacklemnfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1250191783Srmacklem    struct thread *td)
1251191783Srmacklem{
1252191783Srmacklem	int error = 0;
1253191783Srmacklem
1254229827Srmacklem	/*
1255229827Srmacklem	 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1256229827Srmacklem	 * file is done.  At this time VOP_FSYNC does not accept offset and
1257229827Srmacklem	 * byte count parameters so call VOP_FSYNC the whole file for now.
1258229827Srmacklem	 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1259229827Srmacklem	 */
1260229827Srmacklem	if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
1261191783Srmacklem		/*
1262191783Srmacklem		 * Give up and do the whole thing
1263191783Srmacklem		 */
1264191783Srmacklem		if (vp->v_object &&
1265191783Srmacklem		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1266191783Srmacklem			VM_OBJECT_LOCK(vp->v_object);
1267191783Srmacklem			vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1268191783Srmacklem			VM_OBJECT_UNLOCK(vp->v_object);
1269191783Srmacklem		}
1270191783Srmacklem		error = VOP_FSYNC(vp, MNT_WAIT, td);
1271191783Srmacklem	} else {
1272191783Srmacklem		/*
1273191783Srmacklem		 * Locate and synchronously write any buffers that fall
1274191783Srmacklem		 * into the requested range.  Note:  we are assuming that
1275191783Srmacklem		 * f_iosize is a power of 2.
1276191783Srmacklem		 */
1277191783Srmacklem		int iosize = vp->v_mount->mnt_stat.f_iosize;
1278191783Srmacklem		int iomask = iosize - 1;
1279191783Srmacklem		struct bufobj *bo;
1280191783Srmacklem		daddr_t lblkno;
1281191783Srmacklem
1282191783Srmacklem		/*
1283191783Srmacklem		 * Align to iosize boundry, super-align to page boundry.
1284191783Srmacklem		 */
1285191783Srmacklem		if (off & iomask) {
1286191783Srmacklem			cnt += off & iomask;
1287191783Srmacklem			off &= ~(u_quad_t)iomask;
1288191783Srmacklem		}
1289191783Srmacklem		if (off & PAGE_MASK) {
1290191783Srmacklem			cnt += off & PAGE_MASK;
1291191783Srmacklem			off &= ~(u_quad_t)PAGE_MASK;
1292191783Srmacklem		}
1293191783Srmacklem		lblkno = off / iosize;
1294191783Srmacklem
1295191783Srmacklem		if (vp->v_object &&
1296191783Srmacklem		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1297191783Srmacklem			VM_OBJECT_LOCK(vp->v_object);
1298218345Salc			vm_object_page_clean(vp->v_object, off, off + cnt,
1299218345Salc			    OBJPC_SYNC);
1300191783Srmacklem			VM_OBJECT_UNLOCK(vp->v_object);
1301191783Srmacklem		}
1302191783Srmacklem
1303191783Srmacklem		bo = &vp->v_bufobj;
1304191783Srmacklem		BO_LOCK(bo);
1305191783Srmacklem		while (cnt > 0) {
1306191783Srmacklem			struct buf *bp;
1307191783Srmacklem
1308191783Srmacklem			/*
1309191783Srmacklem			 * If we have a buffer and it is marked B_DELWRI we
1310191783Srmacklem			 * have to lock and write it.  Otherwise the prior
1311191783Srmacklem			 * write is assumed to have already been committed.
1312191783Srmacklem			 *
1313191783Srmacklem			 * gbincore() can return invalid buffers now so we
1314191783Srmacklem			 * have to check that bit as well (though B_DELWRI
1315191783Srmacklem			 * should not be set if B_INVAL is set there could be
1316191783Srmacklem			 * a race here since we haven't locked the buffer).
1317191783Srmacklem			 */
1318191783Srmacklem			if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1319191783Srmacklem				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1320191783Srmacklem				    LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1321191783Srmacklem					BO_LOCK(bo);
1322191783Srmacklem					continue; /* retry */
1323191783Srmacklem				}
1324191783Srmacklem			    	if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1325191783Srmacklem				    B_DELWRI) {
1326191783Srmacklem					bremfree(bp);
1327191783Srmacklem					bp->b_flags &= ~B_ASYNC;
1328191783Srmacklem					bwrite(bp);
1329191783Srmacklem					++nfs_commit_miss;
1330191783Srmacklem				} else
1331191783Srmacklem					BUF_UNLOCK(bp);
1332191783Srmacklem				BO_LOCK(bo);
1333191783Srmacklem			}
1334191783Srmacklem			++nfs_commit_blks;
1335191783Srmacklem			if (cnt < iosize)
1336191783Srmacklem				break;
1337191783Srmacklem			cnt -= iosize;
1338191783Srmacklem			++lblkno;
1339191783Srmacklem		}
1340191783Srmacklem		BO_UNLOCK(bo);
1341191783Srmacklem	}
1342224086Szack	NFSEXITCODE(error);
1343191783Srmacklem	return (error);
1344191783Srmacklem}
1345191783Srmacklem
1346191783Srmacklem/*
1347191783Srmacklem * Statfs vnode op.
1348191783Srmacklem */
1349191783Srmacklemint
1350191990Sattilionfsvno_statfs(struct vnode *vp, struct statfs *sf)
1351191783Srmacklem{
1352221517Srmacklem	int error;
1353191783Srmacklem
1354221517Srmacklem	error = VFS_STATFS(vp->v_mount, sf);
1355221517Srmacklem	if (error == 0) {
1356221517Srmacklem		/*
1357221517Srmacklem		 * Since NFS handles these values as unsigned on the
1358221517Srmacklem		 * wire, there is no way to represent negative values,
1359221517Srmacklem		 * so set them to 0. Without this, they will appear
1360221517Srmacklem		 * to be very large positive values for clients like
1361221517Srmacklem		 * Solaris10.
1362221517Srmacklem		 */
1363221517Srmacklem		if (sf->f_bavail < 0)
1364221517Srmacklem			sf->f_bavail = 0;
1365221517Srmacklem		if (sf->f_ffree < 0)
1366221517Srmacklem			sf->f_ffree = 0;
1367221517Srmacklem	}
1368224086Szack	NFSEXITCODE(error);
1369221517Srmacklem	return (error);
1370191783Srmacklem}
1371191783Srmacklem
1372191783Srmacklem/*
1373191783Srmacklem * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1374191783Srmacklem * must handle nfsrv_opencheck() calls after any other access checks.
1375191783Srmacklem */
1376191783Srmacklemvoid
1377191783Srmacklemnfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1378191783Srmacklem    nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1379199616Srmacklem    int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1380191783Srmacklem    NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1381191783Srmacklem    struct nfsexstuff *exp, struct vnode **vpp)
1382191783Srmacklem{
1383191783Srmacklem	struct vnode *vp = NULL;
1384191783Srmacklem	u_quad_t tempsize;
1385191783Srmacklem	struct nfsexstuff nes;
1386191783Srmacklem
1387191783Srmacklem	if (ndp->ni_vp == NULL)
1388191783Srmacklem		nd->nd_repstat = nfsrv_opencheck(clientid,
1389191783Srmacklem		    stateidp, stp, NULL, nd, p, nd->nd_repstat);
1390191783Srmacklem	if (!nd->nd_repstat) {
1391191783Srmacklem		if (ndp->ni_vp == NULL) {
1392191783Srmacklem			vrele(ndp->ni_startdir);
1393191783Srmacklem			nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1394191783Srmacklem			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1395191783Srmacklem			vput(ndp->ni_dvp);
1396191783Srmacklem			nfsvno_relpathbuf(ndp);
1397191783Srmacklem			if (!nd->nd_repstat) {
1398191783Srmacklem				if (*exclusive_flagp) {
1399191783Srmacklem					*exclusive_flagp = 0;
1400191783Srmacklem					NFSVNO_ATTRINIT(nvap);
1401199616Srmacklem					nvap->na_atime.tv_sec = cverf[0];
1402199616Srmacklem					nvap->na_atime.tv_nsec = cverf[1];
1403191783Srmacklem					nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1404191783Srmacklem					    &nvap->na_vattr, cred);
1405191783Srmacklem				} else {
1406191783Srmacklem					nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1407191783Srmacklem					    aclp, p, attrbitp, exp);
1408191783Srmacklem				}
1409191783Srmacklem			}
1410191783Srmacklem			vp = ndp->ni_vp;
1411191783Srmacklem		} else {
1412191783Srmacklem			if (ndp->ni_startdir)
1413191783Srmacklem				vrele(ndp->ni_startdir);
1414191783Srmacklem			nfsvno_relpathbuf(ndp);
1415191783Srmacklem			vp = ndp->ni_vp;
1416191783Srmacklem			if (create == NFSV4OPEN_CREATE) {
1417191783Srmacklem				if (ndp->ni_dvp == vp)
1418191783Srmacklem					vrele(ndp->ni_dvp);
1419191783Srmacklem				else
1420191783Srmacklem					vput(ndp->ni_dvp);
1421191783Srmacklem			}
1422191783Srmacklem			if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1423191783Srmacklem				if (ndp->ni_cnd.cn_flags & RDONLY)
1424191783Srmacklem					NFSVNO_SETEXRDONLY(&nes);
1425191783Srmacklem				else
1426191783Srmacklem					NFSVNO_EXINIT(&nes);
1427191783Srmacklem				nd->nd_repstat = nfsvno_accchk(vp,
1428200999Srmacklem				    VWRITE, cred, &nes, p,
1429200999Srmacklem				    NFSACCCHK_NOOVERRIDE,
1430200999Srmacklem				    NFSACCCHK_VPISLOCKED, NULL);
1431191783Srmacklem				nd->nd_repstat = nfsrv_opencheck(clientid,
1432191783Srmacklem				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1433191783Srmacklem				if (!nd->nd_repstat) {
1434191783Srmacklem					tempsize = nvap->na_size;
1435191783Srmacklem					NFSVNO_ATTRINIT(nvap);
1436191783Srmacklem					nvap->na_size = tempsize;
1437191783Srmacklem					nd->nd_repstat = VOP_SETATTR(vp,
1438191783Srmacklem					    &nvap->na_vattr, cred);
1439191783Srmacklem				}
1440191783Srmacklem			} else if (vp->v_type == VREG) {
1441191783Srmacklem				nd->nd_repstat = nfsrv_opencheck(clientid,
1442191783Srmacklem				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1443191783Srmacklem			}
1444191783Srmacklem		}
1445191783Srmacklem	} else {
1446191783Srmacklem		if (ndp->ni_cnd.cn_flags & HASBUF)
1447191783Srmacklem			nfsvno_relpathbuf(ndp);
1448191783Srmacklem		if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1449191783Srmacklem			vrele(ndp->ni_startdir);
1450191783Srmacklem			if (ndp->ni_dvp == ndp->ni_vp)
1451191783Srmacklem				vrele(ndp->ni_dvp);
1452191783Srmacklem			else
1453191783Srmacklem				vput(ndp->ni_dvp);
1454191783Srmacklem			if (ndp->ni_vp)
1455191783Srmacklem				vput(ndp->ni_vp);
1456191783Srmacklem		}
1457191783Srmacklem	}
1458191783Srmacklem	*vpp = vp;
1459224086Szack
1460224086Szack	NFSEXITCODE2(0, nd);
1461191783Srmacklem}
1462191783Srmacklem
1463191783Srmacklem/*
1464191783Srmacklem * Updates the file rev and sets the mtime and ctime
1465191783Srmacklem * to the current clock time, returning the va_filerev and va_Xtime
1466191783Srmacklem * values.
1467191783Srmacklem */
1468191783Srmacklemvoid
1469191783Srmacklemnfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1470191783Srmacklem    struct ucred *cred, struct thread *p)
1471191783Srmacklem{
1472191783Srmacklem	struct vattr va;
1473191783Srmacklem
1474191783Srmacklem	VATTR_NULL(&va);
1475191783Srmacklem	getnanotime(&va.va_mtime);
1476191783Srmacklem	(void) VOP_SETATTR(vp, &va, cred);
1477216693Srmacklem	(void) nfsvno_getattr(vp, nvap, cred, p, 1);
1478191783Srmacklem}
1479191783Srmacklem
1480191783Srmacklem/*
1481191783Srmacklem * Glue routine to nfsv4_fillattr().
1482191783Srmacklem */
1483191783Srmacklemint
1484220645Srmacklemnfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1485191783Srmacklem    struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1486220648Srmacklem    struct ucred *cred, struct thread *p, int isdgram, int reterr,
1487220648Srmacklem    int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1488191783Srmacklem{
1489191783Srmacklem	int error;
1490191783Srmacklem
1491220645Srmacklem	error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1492220648Srmacklem	    attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1493220648Srmacklem	    mounted_on_fileno);
1494224086Szack	NFSEXITCODE2(0, nd);
1495191783Srmacklem	return (error);
1496191783Srmacklem}
1497191783Srmacklem
1498191783Srmacklem/* Since the Readdir vnode ops vary, put the entire functions in here. */
1499191783Srmacklem/*
1500191783Srmacklem * nfs readdir service
1501191783Srmacklem * - mallocs what it thinks is enough to read
1502191783Srmacklem *	count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1503206170Srmacklem * - calls VOP_READDIR()
1504191783Srmacklem * - loops around building the reply
1505191783Srmacklem *	if the output generated exceeds count break out of loop
1506191783Srmacklem *	The NFSM_CLGET macro is used here so that the reply will be packed
1507191783Srmacklem *	tightly in mbuf clusters.
1508191783Srmacklem * - it trims out records with d_fileno == 0
1509191783Srmacklem *	this doesn't matter for Unix clients, but they might confuse clients
1510191783Srmacklem *	for other os'.
1511191783Srmacklem * - it trims out records with d_type == DT_WHT
1512191783Srmacklem *	these cannot be seen through NFS (unless we extend the protocol)
1513191783Srmacklem *     The alternate call nfsrvd_readdirplus() does lookups as well.
1514191783Srmacklem * PS: The NFS protocol spec. does not clarify what the "count" byte
1515191783Srmacklem *	argument is a count of.. just name strings and file id's or the
1516191783Srmacklem *	entire reply rpc or ...
1517191783Srmacklem *	I tried just file name and id sizes and it confused the Sun client,
1518191783Srmacklem *	so I am using the full rpc size now. The "paranoia.." comment refers
1519191783Srmacklem *	to including the status longwords that are not a part of the dir.
1520191783Srmacklem *	"entry" structures, but are in the rpc.
1521191783Srmacklem */
1522191783Srmacklemint
1523191783Srmacklemnfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1524191783Srmacklem    struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1525191783Srmacklem{
1526191783Srmacklem	struct dirent *dp;
1527191783Srmacklem	u_int32_t *tl;
1528191783Srmacklem	int dirlen;
1529191783Srmacklem	char *cpos, *cend, *rbuf;
1530191783Srmacklem	struct nfsvattr at;
1531191783Srmacklem	int nlen, error = 0, getret = 1;
1532191783Srmacklem	int siz, cnt, fullsiz, eofflag, ncookies;
1533191783Srmacklem	u_int64_t off, toff, verf;
1534191783Srmacklem	u_long *cookies = NULL, *cookiep;
1535191783Srmacklem	struct uio io;
1536191783Srmacklem	struct iovec iv;
1537216691Srmacklem	int not_zfs;
1538191783Srmacklem
1539191783Srmacklem	if (nd->nd_repstat) {
1540191783Srmacklem		nfsrv_postopattr(nd, getret, &at);
1541224086Szack		goto out;
1542191783Srmacklem	}
1543191783Srmacklem	if (nd->nd_flag & ND_NFSV2) {
1544191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1545191783Srmacklem		off = fxdr_unsigned(u_quad_t, *tl++);
1546191783Srmacklem	} else {
1547191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1548191783Srmacklem		off = fxdr_hyper(tl);
1549191783Srmacklem		tl += 2;
1550191783Srmacklem		verf = fxdr_hyper(tl);
1551191783Srmacklem		tl += 2;
1552191783Srmacklem	}
1553191783Srmacklem	toff = off;
1554191783Srmacklem	cnt = fxdr_unsigned(int, *tl);
1555206170Srmacklem	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1556191783Srmacklem		cnt = NFS_SRVMAXDATA(nd);
1557191783Srmacklem	siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1558191783Srmacklem	fullsiz = siz;
1559191783Srmacklem	if (nd->nd_flag & ND_NFSV3) {
1560191783Srmacklem		nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1561216693Srmacklem		    p, 1);
1562191783Srmacklem#if 0
1563191783Srmacklem		/*
1564191783Srmacklem		 * va_filerev is not sufficient as a cookie verifier,
1565191783Srmacklem		 * since it is not supposed to change when entries are
1566191783Srmacklem		 * removed/added unless that offset cookies returned to
1567191783Srmacklem		 * the client are no longer valid.
1568191783Srmacklem		 */
1569191783Srmacklem		if (!nd->nd_repstat && toff && verf != at.na_filerev)
1570191783Srmacklem			nd->nd_repstat = NFSERR_BAD_COOKIE;
1571191783Srmacklem#endif
1572191783Srmacklem	}
1573206170Srmacklem	if (nd->nd_repstat == 0 && cnt == 0) {
1574206170Srmacklem		if (nd->nd_flag & ND_NFSV2)
1575206170Srmacklem			/* NFSv2 does not have NFSERR_TOOSMALL */
1576206170Srmacklem			nd->nd_repstat = EPERM;
1577206170Srmacklem		else
1578206170Srmacklem			nd->nd_repstat = NFSERR_TOOSMALL;
1579206170Srmacklem	}
1580191783Srmacklem	if (!nd->nd_repstat)
1581200999Srmacklem		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1582191783Srmacklem		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1583200999Srmacklem		    NFSACCCHK_VPISLOCKED, NULL);
1584191783Srmacklem	if (nd->nd_repstat) {
1585191783Srmacklem		vput(vp);
1586191783Srmacklem		if (nd->nd_flag & ND_NFSV3)
1587191783Srmacklem			nfsrv_postopattr(nd, getret, &at);
1588224086Szack		goto out;
1589191783Srmacklem	}
1590216691Srmacklem	not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1591191783Srmacklem	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1592191783Srmacklemagain:
1593191783Srmacklem	eofflag = 0;
1594191783Srmacklem	if (cookies) {
1595191783Srmacklem		free((caddr_t)cookies, M_TEMP);
1596191783Srmacklem		cookies = NULL;
1597191783Srmacklem	}
1598191783Srmacklem
1599191783Srmacklem	iv.iov_base = rbuf;
1600191783Srmacklem	iv.iov_len = siz;
1601191783Srmacklem	io.uio_iov = &iv;
1602191783Srmacklem	io.uio_iovcnt = 1;
1603191783Srmacklem	io.uio_offset = (off_t)off;
1604191783Srmacklem	io.uio_resid = siz;
1605191783Srmacklem	io.uio_segflg = UIO_SYSSPACE;
1606191783Srmacklem	io.uio_rw = UIO_READ;
1607191783Srmacklem	io.uio_td = NULL;
1608191783Srmacklem	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1609191783Srmacklem	    &cookies);
1610191783Srmacklem	off = (u_int64_t)io.uio_offset;
1611191783Srmacklem	if (io.uio_resid)
1612191783Srmacklem		siz -= io.uio_resid;
1613191783Srmacklem
1614191783Srmacklem	if (!cookies && !nd->nd_repstat)
1615191783Srmacklem		nd->nd_repstat = NFSERR_PERM;
1616191783Srmacklem	if (nd->nd_flag & ND_NFSV3) {
1617216693Srmacklem		getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1618191783Srmacklem		if (!nd->nd_repstat)
1619191783Srmacklem			nd->nd_repstat = getret;
1620191783Srmacklem	}
1621191783Srmacklem
1622191783Srmacklem	/*
1623191783Srmacklem	 * Handles the failed cases. nd->nd_repstat == 0 past here.
1624191783Srmacklem	 */
1625191783Srmacklem	if (nd->nd_repstat) {
1626216692Srmacklem		vput(vp);
1627191783Srmacklem		free((caddr_t)rbuf, M_TEMP);
1628191783Srmacklem		if (cookies)
1629191783Srmacklem			free((caddr_t)cookies, M_TEMP);
1630191783Srmacklem		if (nd->nd_flag & ND_NFSV3)
1631191783Srmacklem			nfsrv_postopattr(nd, getret, &at);
1632224086Szack		goto out;
1633191783Srmacklem	}
1634191783Srmacklem	/*
1635191783Srmacklem	 * If nothing read, return eof
1636191783Srmacklem	 * rpc reply
1637191783Srmacklem	 */
1638191783Srmacklem	if (siz == 0) {
1639216692Srmacklem		vput(vp);
1640191783Srmacklem		if (nd->nd_flag & ND_NFSV2) {
1641191783Srmacklem			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1642191783Srmacklem		} else {
1643191783Srmacklem			nfsrv_postopattr(nd, getret, &at);
1644191783Srmacklem			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1645191783Srmacklem			txdr_hyper(at.na_filerev, tl);
1646191783Srmacklem			tl += 2;
1647191783Srmacklem		}
1648191783Srmacklem		*tl++ = newnfs_false;
1649191783Srmacklem		*tl = newnfs_true;
1650191783Srmacklem		FREE((caddr_t)rbuf, M_TEMP);
1651191783Srmacklem		FREE((caddr_t)cookies, M_TEMP);
1652224086Szack		goto out;
1653191783Srmacklem	}
1654191783Srmacklem
1655191783Srmacklem	/*
1656191783Srmacklem	 * Check for degenerate cases of nothing useful read.
1657191783Srmacklem	 * If so go try again
1658191783Srmacklem	 */
1659191783Srmacklem	cpos = rbuf;
1660191783Srmacklem	cend = rbuf + siz;
1661191783Srmacklem	dp = (struct dirent *)cpos;
1662191783Srmacklem	cookiep = cookies;
1663191783Srmacklem
1664191783Srmacklem	/*
1665191783Srmacklem	 * For some reason FreeBSD's ufs_readdir() chooses to back the
1666191783Srmacklem	 * directory offset up to a block boundary, so it is necessary to
1667191783Srmacklem	 * skip over the records that precede the requested offset. This
1668191783Srmacklem	 * requires the assumption that file offset cookies monotonically
1669191783Srmacklem	 * increase.
1670216691Srmacklem	 * Since the offset cookies don't monotonically increase for ZFS,
1671216691Srmacklem	 * this is not done when ZFS is the file system.
1672191783Srmacklem	 */
1673191783Srmacklem	while (cpos < cend && ncookies > 0 &&
1674191783Srmacklem	    (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1675216691Srmacklem	     (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1676191783Srmacklem		cpos += dp->d_reclen;
1677191783Srmacklem		dp = (struct dirent *)cpos;
1678191783Srmacklem		cookiep++;
1679191783Srmacklem		ncookies--;
1680191783Srmacklem	}
1681191783Srmacklem	if (cpos >= cend || ncookies == 0) {
1682191783Srmacklem		siz = fullsiz;
1683191783Srmacklem		toff = off;
1684191783Srmacklem		goto again;
1685191783Srmacklem	}
1686216692Srmacklem	vput(vp);
1687191783Srmacklem
1688191783Srmacklem	/*
1689191783Srmacklem	 * dirlen is the size of the reply, including all XDR and must
1690191783Srmacklem	 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1691191783Srmacklem	 * if the XDR should be included in "count", but to be safe, we do.
1692191783Srmacklem	 * (Include the two booleans at the end of the reply in dirlen now.)
1693191783Srmacklem	 */
1694191783Srmacklem	if (nd->nd_flag & ND_NFSV3) {
1695191783Srmacklem		nfsrv_postopattr(nd, getret, &at);
1696191783Srmacklem		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1697191783Srmacklem		txdr_hyper(at.na_filerev, tl);
1698191783Srmacklem		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1699191783Srmacklem	} else {
1700191783Srmacklem		dirlen = 2 * NFSX_UNSIGNED;
1701191783Srmacklem	}
1702191783Srmacklem
1703191783Srmacklem	/* Loop through the records and build reply */
1704191783Srmacklem	while (cpos < cend && ncookies > 0) {
1705191783Srmacklem		nlen = dp->d_namlen;
1706191783Srmacklem		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1707191783Srmacklem			nlen <= NFS_MAXNAMLEN) {
1708191783Srmacklem			if (nd->nd_flag & ND_NFSV3)
1709191783Srmacklem				dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1710191783Srmacklem			else
1711191783Srmacklem				dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1712191783Srmacklem			if (dirlen > cnt) {
1713191783Srmacklem				eofflag = 0;
1714191783Srmacklem				break;
1715191783Srmacklem			}
1716191783Srmacklem
1717191783Srmacklem			/*
1718191783Srmacklem			 * Build the directory record xdr from
1719191783Srmacklem			 * the dirent entry.
1720191783Srmacklem			 */
1721191783Srmacklem			if (nd->nd_flag & ND_NFSV3) {
1722191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1723191783Srmacklem				*tl++ = newnfs_true;
1724191783Srmacklem				*tl++ = 0;
1725191783Srmacklem			} else {
1726191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1727191783Srmacklem				*tl++ = newnfs_true;
1728191783Srmacklem			}
1729191783Srmacklem			*tl = txdr_unsigned(dp->d_fileno);
1730191783Srmacklem			(void) nfsm_strtom(nd, dp->d_name, nlen);
1731191783Srmacklem			if (nd->nd_flag & ND_NFSV3) {
1732191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1733191783Srmacklem				*tl++ = 0;
1734191783Srmacklem			} else
1735191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1736191783Srmacklem			*tl = txdr_unsigned(*cookiep);
1737191783Srmacklem		}
1738191783Srmacklem		cpos += dp->d_reclen;
1739191783Srmacklem		dp = (struct dirent *)cpos;
1740191783Srmacklem		cookiep++;
1741191783Srmacklem		ncookies--;
1742191783Srmacklem	}
1743191783Srmacklem	if (cpos < cend)
1744191783Srmacklem		eofflag = 0;
1745191783Srmacklem	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1746191783Srmacklem	*tl++ = newnfs_false;
1747191783Srmacklem	if (eofflag)
1748191783Srmacklem		*tl = newnfs_true;
1749191783Srmacklem	else
1750191783Srmacklem		*tl = newnfs_false;
1751191783Srmacklem	FREE((caddr_t)rbuf, M_TEMP);
1752191783Srmacklem	FREE((caddr_t)cookies, M_TEMP);
1753224086Szack
1754224086Szackout:
1755224086Szack	NFSEXITCODE2(0, nd);
1756191783Srmacklem	return (0);
1757191783Srmacklemnfsmout:
1758191783Srmacklem	vput(vp);
1759224086Szack	NFSEXITCODE2(error, nd);
1760191783Srmacklem	return (error);
1761191783Srmacklem}
1762191783Srmacklem
1763191783Srmacklem/*
1764191783Srmacklem * Readdirplus for V3 and Readdir for V4.
1765191783Srmacklem */
1766191783Srmacklemint
1767191783Srmacklemnfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1768191783Srmacklem    struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1769191783Srmacklem{
1770191783Srmacklem	struct dirent *dp;
1771191783Srmacklem	u_int32_t *tl;
1772191783Srmacklem	int dirlen;
1773191783Srmacklem	char *cpos, *cend, *rbuf;
1774191783Srmacklem	struct vnode *nvp;
1775191783Srmacklem	fhandle_t nfh;
1776191783Srmacklem	struct nfsvattr nva, at, *nvap = &nva;
1777191783Srmacklem	struct mbuf *mb0, *mb1;
1778191783Srmacklem	struct nfsreferral *refp;
1779199715Srmacklem	int nlen, r, error = 0, getret = 1, usevget = 1;
1780191783Srmacklem	int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1781191783Srmacklem	caddr_t bpos0, bpos1;
1782191783Srmacklem	u_int64_t off, toff, verf;
1783191783Srmacklem	u_long *cookies = NULL, *cookiep;
1784191783Srmacklem	nfsattrbit_t attrbits, rderrbits, savbits;
1785191783Srmacklem	struct uio io;
1786191783Srmacklem	struct iovec iv;
1787199715Srmacklem	struct componentname cn;
1788220648Srmacklem	int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1789220645Srmacklem	struct mount *mp, *new_mp;
1790220645Srmacklem	uint64_t mounted_on_fileno;
1791191783Srmacklem
1792191783Srmacklem	if (nd->nd_repstat) {
1793191783Srmacklem		nfsrv_postopattr(nd, getret, &at);
1794224086Szack		goto out;
1795191783Srmacklem	}
1796191783Srmacklem	NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1797191783Srmacklem	off = fxdr_hyper(tl);
1798191783Srmacklem	toff = off;
1799191783Srmacklem	tl += 2;
1800191783Srmacklem	verf = fxdr_hyper(tl);
1801191783Srmacklem	tl += 2;
1802191783Srmacklem	siz = fxdr_unsigned(int, *tl++);
1803191783Srmacklem	cnt = fxdr_unsigned(int, *tl);
1804191783Srmacklem
1805191783Srmacklem	/*
1806191783Srmacklem	 * Use the server's maximum data transfer size as the upper bound
1807191783Srmacklem	 * on reply datalen.
1808191783Srmacklem	 */
1809206170Srmacklem	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1810191783Srmacklem		cnt = NFS_SRVMAXDATA(nd);
1811191783Srmacklem
1812191783Srmacklem	/*
1813191783Srmacklem	 * siz is a "hint" of how much directory information (name, fileid,
1814191783Srmacklem	 * cookie) should be in the reply. At least one client "hints" 0,
1815191783Srmacklem	 * so I set it to cnt for that case. I also round it up to the
1816191783Srmacklem	 * next multiple of DIRBLKSIZ.
1817191783Srmacklem	 */
1818206170Srmacklem	if (siz <= 0)
1819191783Srmacklem		siz = cnt;
1820191783Srmacklem	siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1821191783Srmacklem
1822191783Srmacklem	if (nd->nd_flag & ND_NFSV4) {
1823191783Srmacklem		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1824191783Srmacklem		if (error)
1825191783Srmacklem			goto nfsmout;
1826191783Srmacklem		NFSSET_ATTRBIT(&savbits, &attrbits);
1827191783Srmacklem		NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1828191783Srmacklem		NFSZERO_ATTRBIT(&rderrbits);
1829191783Srmacklem		NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1830191783Srmacklem	} else {
1831191783Srmacklem		NFSZERO_ATTRBIT(&attrbits);
1832191783Srmacklem	}
1833191783Srmacklem	fullsiz = siz;
1834216693Srmacklem	nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1835191783Srmacklem	if (!nd->nd_repstat) {
1836191783Srmacklem	    if (off && verf != at.na_filerev) {
1837191783Srmacklem		/*
1838191783Srmacklem		 * va_filerev is not sufficient as a cookie verifier,
1839191783Srmacklem		 * since it is not supposed to change when entries are
1840191783Srmacklem		 * removed/added unless that offset cookies returned to
1841191783Srmacklem		 * the client are no longer valid.
1842191783Srmacklem		 */
1843191783Srmacklem#if 0
1844191783Srmacklem		if (nd->nd_flag & ND_NFSV4) {
1845191783Srmacklem			nd->nd_repstat = NFSERR_NOTSAME;
1846191783Srmacklem		} else {
1847191783Srmacklem			nd->nd_repstat = NFSERR_BAD_COOKIE;
1848191783Srmacklem		}
1849191783Srmacklem#endif
1850191783Srmacklem	    } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1851191783Srmacklem		nd->nd_repstat = NFSERR_BAD_COOKIE;
1852191783Srmacklem	    }
1853191783Srmacklem	}
1854191783Srmacklem	if (!nd->nd_repstat && vp->v_type != VDIR)
1855191783Srmacklem		nd->nd_repstat = NFSERR_NOTDIR;
1856191783Srmacklem	if (!nd->nd_repstat && cnt == 0)
1857191783Srmacklem		nd->nd_repstat = NFSERR_TOOSMALL;
1858191783Srmacklem	if (!nd->nd_repstat)
1859200999Srmacklem		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1860191783Srmacklem		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1861200999Srmacklem		    NFSACCCHK_VPISLOCKED, NULL);
1862191783Srmacklem	if (nd->nd_repstat) {
1863191783Srmacklem		vput(vp);
1864191783Srmacklem		if (nd->nd_flag & ND_NFSV3)
1865191783Srmacklem			nfsrv_postopattr(nd, getret, &at);
1866224086Szack		goto out;
1867191783Srmacklem	}
1868216691Srmacklem	not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1869191783Srmacklem
1870191783Srmacklem	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1871191783Srmacklemagain:
1872191783Srmacklem	eofflag = 0;
1873191783Srmacklem	if (cookies) {
1874191783Srmacklem		free((caddr_t)cookies, M_TEMP);
1875191783Srmacklem		cookies = NULL;
1876191783Srmacklem	}
1877191783Srmacklem
1878191783Srmacklem	iv.iov_base = rbuf;
1879191783Srmacklem	iv.iov_len = siz;
1880191783Srmacklem	io.uio_iov = &iv;
1881191783Srmacklem	io.uio_iovcnt = 1;
1882191783Srmacklem	io.uio_offset = (off_t)off;
1883191783Srmacklem	io.uio_resid = siz;
1884191783Srmacklem	io.uio_segflg = UIO_SYSSPACE;
1885191783Srmacklem	io.uio_rw = UIO_READ;
1886191783Srmacklem	io.uio_td = NULL;
1887191783Srmacklem	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1888191783Srmacklem	    &cookies);
1889191783Srmacklem	off = (u_int64_t)io.uio_offset;
1890191783Srmacklem	if (io.uio_resid)
1891191783Srmacklem		siz -= io.uio_resid;
1892191783Srmacklem
1893216693Srmacklem	getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1894191783Srmacklem
1895191783Srmacklem	if (!cookies && !nd->nd_repstat)
1896191783Srmacklem		nd->nd_repstat = NFSERR_PERM;
1897191783Srmacklem	if (!nd->nd_repstat)
1898191783Srmacklem		nd->nd_repstat = getret;
1899191783Srmacklem	if (nd->nd_repstat) {
1900199715Srmacklem		vput(vp);
1901191783Srmacklem		if (cookies)
1902191783Srmacklem			free((caddr_t)cookies, M_TEMP);
1903191783Srmacklem		free((caddr_t)rbuf, M_TEMP);
1904191783Srmacklem		if (nd->nd_flag & ND_NFSV3)
1905191783Srmacklem			nfsrv_postopattr(nd, getret, &at);
1906224086Szack		goto out;
1907191783Srmacklem	}
1908191783Srmacklem	/*
1909191783Srmacklem	 * If nothing read, return eof
1910191783Srmacklem	 * rpc reply
1911191783Srmacklem	 */
1912191783Srmacklem	if (siz == 0) {
1913199715Srmacklem		vput(vp);
1914191783Srmacklem		if (nd->nd_flag & ND_NFSV3)
1915191783Srmacklem			nfsrv_postopattr(nd, getret, &at);
1916191783Srmacklem		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1917191783Srmacklem		txdr_hyper(at.na_filerev, tl);
1918191783Srmacklem		tl += 2;
1919191783Srmacklem		*tl++ = newnfs_false;
1920191783Srmacklem		*tl = newnfs_true;
1921191783Srmacklem		free((caddr_t)cookies, M_TEMP);
1922191783Srmacklem		free((caddr_t)rbuf, M_TEMP);
1923224086Szack		goto out;
1924191783Srmacklem	}
1925191783Srmacklem
1926191783Srmacklem	/*
1927191783Srmacklem	 * Check for degenerate cases of nothing useful read.
1928191783Srmacklem	 * If so go try again
1929191783Srmacklem	 */
1930191783Srmacklem	cpos = rbuf;
1931191783Srmacklem	cend = rbuf + siz;
1932191783Srmacklem	dp = (struct dirent *)cpos;
1933191783Srmacklem	cookiep = cookies;
1934191783Srmacklem
1935191783Srmacklem	/*
1936191783Srmacklem	 * For some reason FreeBSD's ufs_readdir() chooses to back the
1937191783Srmacklem	 * directory offset up to a block boundary, so it is necessary to
1938191783Srmacklem	 * skip over the records that precede the requested offset. This
1939191783Srmacklem	 * requires the assumption that file offset cookies monotonically
1940191783Srmacklem	 * increase.
1941216691Srmacklem	 * Since the offset cookies don't monotonically increase for ZFS,
1942216691Srmacklem	 * this is not done when ZFS is the file system.
1943191783Srmacklem	 */
1944191783Srmacklem	while (cpos < cend && ncookies > 0 &&
1945191783Srmacklem	  (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1946216691Srmacklem	   (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1947191783Srmacklem	   ((nd->nd_flag & ND_NFSV4) &&
1948191783Srmacklem	    ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1949191783Srmacklem	     (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1950191783Srmacklem		cpos += dp->d_reclen;
1951191783Srmacklem		dp = (struct dirent *)cpos;
1952191783Srmacklem		cookiep++;
1953191783Srmacklem		ncookies--;
1954191783Srmacklem	}
1955191783Srmacklem	if (cpos >= cend || ncookies == 0) {
1956191783Srmacklem		siz = fullsiz;
1957191783Srmacklem		toff = off;
1958191783Srmacklem		goto again;
1959191783Srmacklem	}
1960217176Srmacklem
1961217176Srmacklem	/*
1962217176Srmacklem	 * Busy the file system so that the mount point won't go away
1963217176Srmacklem	 * and, as such, VFS_VGET() can be used safely.
1964217176Srmacklem	 */
1965217176Srmacklem	mp = vp->v_mount;
1966217176Srmacklem	vfs_ref(mp);
1967224082Szack	NFSVOPUNLOCK(vp, 0);
1968217176Srmacklem	nd->nd_repstat = vfs_busy(mp, 0);
1969217176Srmacklem	vfs_rel(mp);
1970217176Srmacklem	if (nd->nd_repstat != 0) {
1971217176Srmacklem		vrele(vp);
1972217176Srmacklem		free(cookies, M_TEMP);
1973217176Srmacklem		free(rbuf, M_TEMP);
1974217176Srmacklem		if (nd->nd_flag & ND_NFSV3)
1975217176Srmacklem			nfsrv_postopattr(nd, getret, &at);
1976224086Szack		goto out;
1977217176Srmacklem	}
1978191783Srmacklem
1979191783Srmacklem	/*
1980191783Srmacklem	 * Save this position, in case there is an error before one entry
1981191783Srmacklem	 * is created.
1982191783Srmacklem	 */
1983191783Srmacklem	mb0 = nd->nd_mb;
1984191783Srmacklem	bpos0 = nd->nd_bpos;
1985191783Srmacklem
1986191783Srmacklem	/*
1987191783Srmacklem	 * Fill in the first part of the reply.
1988191783Srmacklem	 * dirlen is the reply length in bytes and cannot exceed cnt.
1989191783Srmacklem	 * (Include the two booleans at the end of the reply in dirlen now,
1990191783Srmacklem	 *  so we recognize when we have exceeded cnt.)
1991191783Srmacklem	 */
1992191783Srmacklem	if (nd->nd_flag & ND_NFSV3) {
1993191783Srmacklem		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1994191783Srmacklem		nfsrv_postopattr(nd, getret, &at);
1995191783Srmacklem	} else {
1996191783Srmacklem		dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
1997191783Srmacklem	}
1998191783Srmacklem	NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1999191783Srmacklem	txdr_hyper(at.na_filerev, tl);
2000191783Srmacklem
2001191783Srmacklem	/*
2002191783Srmacklem	 * Save this position, in case there is an empty reply needed.
2003191783Srmacklem	 */
2004191783Srmacklem	mb1 = nd->nd_mb;
2005191783Srmacklem	bpos1 = nd->nd_bpos;
2006191783Srmacklem
2007191783Srmacklem	/* Loop through the records and build reply */
2008191783Srmacklem	entrycnt = 0;
2009191783Srmacklem	while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2010191783Srmacklem		nlen = dp->d_namlen;
2011191783Srmacklem		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2012191783Srmacklem		    nlen <= NFS_MAXNAMLEN &&
2013191783Srmacklem		    ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2014191783Srmacklem		     (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2015191783Srmacklem		      || (nlen == 1 && dp->d_name[0] != '.'))) {
2016191783Srmacklem			/*
2017191783Srmacklem			 * Save the current position in the reply, in case
2018191783Srmacklem			 * this entry exceeds cnt.
2019191783Srmacklem			 */
2020191783Srmacklem			mb1 = nd->nd_mb;
2021191783Srmacklem			bpos1 = nd->nd_bpos;
2022191783Srmacklem
2023191783Srmacklem			/*
2024191783Srmacklem			 * For readdir_and_lookup get the vnode using
2025191783Srmacklem			 * the file number.
2026191783Srmacklem			 */
2027191783Srmacklem			nvp = NULL;
2028191783Srmacklem			refp = NULL;
2029191783Srmacklem			r = 0;
2030220645Srmacklem			at_root = 0;
2031220645Srmacklem			needs_unbusy = 0;
2032220645Srmacklem			new_mp = mp;
2033220645Srmacklem			mounted_on_fileno = (uint64_t)dp->d_fileno;
2034191783Srmacklem			if ((nd->nd_flag & ND_NFSV3) ||
2035191783Srmacklem			    NFSNONZERO_ATTRBIT(&savbits)) {
2036191783Srmacklem				if (nd->nd_flag & ND_NFSV4)
2037191783Srmacklem					refp = nfsv4root_getreferral(NULL,
2038191783Srmacklem					    vp, dp->d_fileno);
2039199715Srmacklem				if (refp == NULL) {
2040199715Srmacklem					if (usevget)
2041217176Srmacklem						r = VFS_VGET(mp, dp->d_fileno,
2042217176Srmacklem						    LK_SHARED, &nvp);
2043199715Srmacklem					else
2044199715Srmacklem						r = EOPNOTSUPP;
2045199715Srmacklem					if (r == EOPNOTSUPP) {
2046199715Srmacklem						if (usevget) {
2047199715Srmacklem							usevget = 0;
2048199715Srmacklem							cn.cn_nameiop = LOOKUP;
2049199715Srmacklem							cn.cn_lkflags =
2050216700Srmacklem							    LK_SHARED |
2051199715Srmacklem							    LK_RETRY;
2052199715Srmacklem							cn.cn_cred =
2053199715Srmacklem							    nd->nd_cred;
2054199715Srmacklem							cn.cn_thread = p;
2055199715Srmacklem						}
2056199715Srmacklem						cn.cn_nameptr = dp->d_name;
2057199715Srmacklem						cn.cn_namelen = nlen;
2058199715Srmacklem						cn.cn_flags = ISLASTCN |
2059199715Srmacklem						    NOFOLLOW | LOCKLEAF |
2060199715Srmacklem						    MPSAFE;
2061199715Srmacklem						if (nlen == 2 &&
2062199715Srmacklem						    dp->d_name[0] == '.' &&
2063199715Srmacklem						    dp->d_name[1] == '.')
2064199715Srmacklem							cn.cn_flags |=
2065199715Srmacklem							    ISDOTDOT;
2066224081Szack						if (NFSVOPLOCK(vp, LK_SHARED)
2067216692Srmacklem						    != 0) {
2068216692Srmacklem							nd->nd_repstat = EPERM;
2069216692Srmacklem							break;
2070216692Srmacklem						}
2071214149Srmacklem						if ((vp->v_vflag & VV_ROOT) != 0
2072214149Srmacklem						    && (cn.cn_flags & ISDOTDOT)
2073214149Srmacklem						    != 0) {
2074214149Srmacklem							vref(vp);
2075214149Srmacklem							nvp = vp;
2076214149Srmacklem							r = 0;
2077220507Srmacklem						} else {
2078214149Srmacklem							r = VOP_LOOKUP(vp, &nvp,
2079214149Srmacklem							    &cn);
2080220507Srmacklem							if (vp != nvp)
2081224082Szack								NFSVOPUNLOCK(vp,
2082220507Srmacklem								    0);
2083220507Srmacklem						}
2084199715Srmacklem					}
2085220645Srmacklem
2086220645Srmacklem					/*
2087220645Srmacklem					 * For NFSv4, check to see if nvp is
2088220645Srmacklem					 * a mount point and get the mount
2089220645Srmacklem					 * point vnode, as required.
2090220645Srmacklem					 */
2091220645Srmacklem					if (r == 0 &&
2092220645Srmacklem					    nfsrv_enable_crossmntpt != 0 &&
2093220645Srmacklem					    (nd->nd_flag & ND_NFSV4) != 0 &&
2094220645Srmacklem					    nvp->v_type == VDIR &&
2095220645Srmacklem					    nvp->v_mountedhere != NULL) {
2096220645Srmacklem						new_mp = nvp->v_mountedhere;
2097220645Srmacklem						r = vfs_busy(new_mp, 0);
2098220645Srmacklem						vput(nvp);
2099220645Srmacklem						nvp = NULL;
2100220645Srmacklem						if (r == 0) {
2101220645Srmacklem							r = VFS_ROOT(new_mp,
2102220645Srmacklem							    LK_SHARED, &nvp);
2103220645Srmacklem							needs_unbusy = 1;
2104220645Srmacklem							if (r == 0)
2105220645Srmacklem								at_root = 1;
2106220645Srmacklem						}
2107220645Srmacklem					}
2108199715Srmacklem				}
2109191783Srmacklem				if (!r) {
2110191783Srmacklem				    if (refp == NULL &&
2111191783Srmacklem					((nd->nd_flag & ND_NFSV3) ||
2112191783Srmacklem					 NFSNONZERO_ATTRBIT(&attrbits))) {
2113191783Srmacklem					r = nfsvno_getfh(nvp, &nfh, p);
2114191783Srmacklem					if (!r)
2115191783Srmacklem					    r = nfsvno_getattr(nvp, nvap,
2116216693Srmacklem						nd->nd_cred, p, 1);
2117191783Srmacklem				    }
2118191783Srmacklem				} else {
2119191783Srmacklem				    nvp = NULL;
2120191783Srmacklem				}
2121191783Srmacklem				if (r) {
2122191783Srmacklem					if (!NFSISSET_ATTRBIT(&attrbits,
2123191783Srmacklem					    NFSATTRBIT_RDATTRERROR)) {
2124191783Srmacklem						if (nvp != NULL)
2125191783Srmacklem							vput(nvp);
2126220645Srmacklem						if (needs_unbusy != 0)
2127220645Srmacklem							vfs_unbusy(new_mp);
2128191783Srmacklem						nd->nd_repstat = r;
2129191783Srmacklem						break;
2130191783Srmacklem					}
2131191783Srmacklem				}
2132191783Srmacklem			}
2133191783Srmacklem
2134191783Srmacklem			/*
2135191783Srmacklem			 * Build the directory record xdr
2136191783Srmacklem			 */
2137191783Srmacklem			if (nd->nd_flag & ND_NFSV3) {
2138191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2139191783Srmacklem				*tl++ = newnfs_true;
2140191783Srmacklem				*tl++ = 0;
2141191783Srmacklem				*tl = txdr_unsigned(dp->d_fileno);
2142191783Srmacklem				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2143191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2144191783Srmacklem				*tl++ = 0;
2145191783Srmacklem				*tl = txdr_unsigned(*cookiep);
2146191783Srmacklem				nfsrv_postopattr(nd, 0, nvap);
2147191783Srmacklem				dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2148191783Srmacklem				dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2149191783Srmacklem				if (nvp != NULL)
2150191783Srmacklem					vput(nvp);
2151191783Srmacklem			} else {
2152191783Srmacklem				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2153191783Srmacklem				*tl++ = newnfs_true;
2154191783Srmacklem				*tl++ = 0;
2155191783Srmacklem				*tl = txdr_unsigned(*cookiep);
2156191783Srmacklem				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2157220648Srmacklem				if (nvp != NULL) {
2158220648Srmacklem					supports_nfsv4acls =
2159220648Srmacklem					    nfs_supportsnfsv4acls(nvp);
2160224082Szack					NFSVOPUNLOCK(nvp, 0);
2161220648Srmacklem				} else
2162220648Srmacklem					supports_nfsv4acls = 0;
2163191783Srmacklem				if (refp != NULL) {
2164191783Srmacklem					dirlen += nfsrv_putreferralattr(nd,
2165191783Srmacklem					    &savbits, refp, 0,
2166191783Srmacklem					    &nd->nd_repstat);
2167191783Srmacklem					if (nd->nd_repstat) {
2168191783Srmacklem						if (nvp != NULL)
2169191783Srmacklem							vrele(nvp);
2170220645Srmacklem						if (needs_unbusy != 0)
2171220645Srmacklem							vfs_unbusy(new_mp);
2172191783Srmacklem						break;
2173191783Srmacklem					}
2174191783Srmacklem				} else if (r) {
2175220645Srmacklem					dirlen += nfsvno_fillattr(nd, new_mp,
2176220645Srmacklem					    nvp, nvap, &nfh, r, &rderrbits,
2177220648Srmacklem					    nd->nd_cred, p, isdgram, 0,
2178220648Srmacklem					    supports_nfsv4acls, at_root,
2179220645Srmacklem					    mounted_on_fileno);
2180191783Srmacklem				} else {
2181220645Srmacklem					dirlen += nfsvno_fillattr(nd, new_mp,
2182220645Srmacklem					    nvp, nvap, &nfh, r, &attrbits,
2183220648Srmacklem					    nd->nd_cred, p, isdgram, 0,
2184220648Srmacklem					    supports_nfsv4acls, at_root,
2185220645Srmacklem					    mounted_on_fileno);
2186191783Srmacklem				}
2187191783Srmacklem				if (nvp != NULL)
2188191783Srmacklem					vrele(nvp);
2189191783Srmacklem				dirlen += (3 * NFSX_UNSIGNED);
2190191783Srmacklem			}
2191220645Srmacklem			if (needs_unbusy != 0)
2192220645Srmacklem				vfs_unbusy(new_mp);
2193191783Srmacklem			if (dirlen <= cnt)
2194191783Srmacklem				entrycnt++;
2195191783Srmacklem		}
2196191783Srmacklem		cpos += dp->d_reclen;
2197191783Srmacklem		dp = (struct dirent *)cpos;
2198191783Srmacklem		cookiep++;
2199191783Srmacklem		ncookies--;
2200191783Srmacklem	}
2201216692Srmacklem	vrele(vp);
2202217176Srmacklem	vfs_unbusy(mp);
2203191783Srmacklem
2204191783Srmacklem	/*
2205191783Srmacklem	 * If dirlen > cnt, we must strip off the last entry. If that
2206191783Srmacklem	 * results in an empty reply, report NFSERR_TOOSMALL.
2207191783Srmacklem	 */
2208191783Srmacklem	if (dirlen > cnt || nd->nd_repstat) {
2209191783Srmacklem		if (!nd->nd_repstat && entrycnt == 0)
2210191783Srmacklem			nd->nd_repstat = NFSERR_TOOSMALL;
2211191783Srmacklem		if (nd->nd_repstat)
2212191783Srmacklem			newnfs_trimtrailing(nd, mb0, bpos0);
2213191783Srmacklem		else
2214191783Srmacklem			newnfs_trimtrailing(nd, mb1, bpos1);
2215191783Srmacklem		eofflag = 0;
2216191783Srmacklem	} else if (cpos < cend)
2217191783Srmacklem		eofflag = 0;
2218191783Srmacklem	if (!nd->nd_repstat) {
2219191783Srmacklem		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2220191783Srmacklem		*tl++ = newnfs_false;
2221191783Srmacklem		if (eofflag)
2222191783Srmacklem			*tl = newnfs_true;
2223191783Srmacklem		else
2224191783Srmacklem			*tl = newnfs_false;
2225191783Srmacklem	}
2226191783Srmacklem	FREE((caddr_t)cookies, M_TEMP);
2227191783Srmacklem	FREE((caddr_t)rbuf, M_TEMP);
2228224086Szack
2229224086Szackout:
2230224086Szack	NFSEXITCODE2(0, nd);
2231191783Srmacklem	return (0);
2232191783Srmacklemnfsmout:
2233191783Srmacklem	vput(vp);
2234224086Szack	NFSEXITCODE2(error, nd);
2235191783Srmacklem	return (error);
2236191783Srmacklem}
2237191783Srmacklem
2238191783Srmacklem/*
2239191783Srmacklem * Get the settable attributes out of the mbuf list.
2240191783Srmacklem * (Return 0 or EBADRPC)
2241191783Srmacklem */
2242191783Srmacklemint
2243191783Srmacklemnfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2244191783Srmacklem    nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2245191783Srmacklem{
2246191783Srmacklem	u_int32_t *tl;
2247191783Srmacklem	struct nfsv2_sattr *sp;
2248191783Srmacklem	struct timeval curtime;
2249191783Srmacklem	int error = 0, toclient = 0;
2250191783Srmacklem
2251191783Srmacklem	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2252191783Srmacklem	case ND_NFSV2:
2253191783Srmacklem		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2254191783Srmacklem		/*
2255191783Srmacklem		 * Some old clients didn't fill in the high order 16bits.
2256191783Srmacklem		 * --> check the low order 2 bytes for 0xffff
2257191783Srmacklem		 */
2258191783Srmacklem		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2259191783Srmacklem			nvap->na_mode = nfstov_mode(sp->sa_mode);
2260191783Srmacklem		if (sp->sa_uid != newnfs_xdrneg1)
2261191783Srmacklem			nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2262191783Srmacklem		if (sp->sa_gid != newnfs_xdrneg1)
2263191783Srmacklem			nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2264191783Srmacklem		if (sp->sa_size != newnfs_xdrneg1)
2265191783Srmacklem			nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2266191783Srmacklem		if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2267191783Srmacklem#ifdef notyet
2268191783Srmacklem			fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2269191783Srmacklem#else
2270191783Srmacklem			nvap->na_atime.tv_sec =
2271191783Srmacklem				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2272191783Srmacklem			nvap->na_atime.tv_nsec = 0;
2273191783Srmacklem#endif
2274191783Srmacklem		}
2275191783Srmacklem		if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2276191783Srmacklem			fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2277191783Srmacklem		break;
2278191783Srmacklem	case ND_NFSV3:
2279191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2280191783Srmacklem		if (*tl == newnfs_true) {
2281191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2282191783Srmacklem			nvap->na_mode = nfstov_mode(*tl);
2283191783Srmacklem		}
2284191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2285191783Srmacklem		if (*tl == newnfs_true) {
2286191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2287191783Srmacklem			nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2288191783Srmacklem		}
2289191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2290191783Srmacklem		if (*tl == newnfs_true) {
2291191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2292191783Srmacklem			nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2293191783Srmacklem		}
2294191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2295191783Srmacklem		if (*tl == newnfs_true) {
2296191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2297191783Srmacklem			nvap->na_size = fxdr_hyper(tl);
2298191783Srmacklem		}
2299191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2300191783Srmacklem		switch (fxdr_unsigned(int, *tl)) {
2301191783Srmacklem		case NFSV3SATTRTIME_TOCLIENT:
2302191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2303191783Srmacklem			fxdr_nfsv3time(tl, &nvap->na_atime);
2304191783Srmacklem			toclient = 1;
2305191783Srmacklem			break;
2306191783Srmacklem		case NFSV3SATTRTIME_TOSERVER:
2307191783Srmacklem			NFSGETTIME(&curtime);
2308191783Srmacklem			nvap->na_atime.tv_sec = curtime.tv_sec;
2309191783Srmacklem			nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2310191783Srmacklem			nvap->na_vaflags |= VA_UTIMES_NULL;
2311191783Srmacklem			break;
2312191783Srmacklem		};
2313191783Srmacklem		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2314191783Srmacklem		switch (fxdr_unsigned(int, *tl)) {
2315191783Srmacklem		case NFSV3SATTRTIME_TOCLIENT:
2316191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2317191783Srmacklem			fxdr_nfsv3time(tl, &nvap->na_mtime);
2318191783Srmacklem			nvap->na_vaflags &= ~VA_UTIMES_NULL;
2319191783Srmacklem			break;
2320191783Srmacklem		case NFSV3SATTRTIME_TOSERVER:
2321191783Srmacklem			NFSGETTIME(&curtime);
2322191783Srmacklem			nvap->na_mtime.tv_sec = curtime.tv_sec;
2323191783Srmacklem			nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2324191783Srmacklem			if (!toclient)
2325191783Srmacklem				nvap->na_vaflags |= VA_UTIMES_NULL;
2326191783Srmacklem			break;
2327191783Srmacklem		};
2328191783Srmacklem		break;
2329191783Srmacklem	case ND_NFSV4:
2330191783Srmacklem		error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2331191783Srmacklem	};
2332191783Srmacklemnfsmout:
2333224086Szack	NFSEXITCODE2(error, nd);
2334191783Srmacklem	return (error);
2335191783Srmacklem}
2336191783Srmacklem
2337191783Srmacklem/*
2338191783Srmacklem * Handle the setable attributes for V4.
2339191783Srmacklem * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2340191783Srmacklem */
2341191783Srmacklemint
2342191783Srmacklemnfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2343191783Srmacklem    nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2344191783Srmacklem{
2345191783Srmacklem	u_int32_t *tl;
2346191783Srmacklem	int attrsum = 0;
2347191783Srmacklem	int i, j;
2348191783Srmacklem	int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2349191783Srmacklem	int toclient = 0;
2350191783Srmacklem	u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2351191783Srmacklem	uid_t uid;
2352191783Srmacklem	gid_t gid;
2353191783Srmacklem	struct timeval curtime;
2354191783Srmacklem
2355191783Srmacklem	error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2356191783Srmacklem	if (error)
2357224086Szack		goto nfsmout;
2358191783Srmacklem	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2359191783Srmacklem	attrsize = fxdr_unsigned(int, *tl);
2360191783Srmacklem
2361191783Srmacklem	/*
2362191783Srmacklem	 * Loop around getting the setable attributes. If an unsupported
2363191783Srmacklem	 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2364191783Srmacklem	 */
2365191783Srmacklem	if (retnotsup) {
2366191783Srmacklem		nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2367191783Srmacklem		bitpos = NFSATTRBIT_MAX;
2368191783Srmacklem	} else {
2369191783Srmacklem		bitpos = 0;
2370191783Srmacklem	}
2371191783Srmacklem	for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2372191783Srmacklem	    if (attrsum > attrsize) {
2373191783Srmacklem		error = NFSERR_BADXDR;
2374191783Srmacklem		goto nfsmout;
2375191783Srmacklem	    }
2376191783Srmacklem	    if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2377191783Srmacklem		switch (bitpos) {
2378191783Srmacklem		case NFSATTRBIT_SIZE:
2379191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2380191783Srmacklem			nvap->na_size = fxdr_hyper(tl);
2381191783Srmacklem			attrsum += NFSX_HYPER;
2382191783Srmacklem			break;
2383191783Srmacklem		case NFSATTRBIT_ACL:
2384191783Srmacklem			error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2385191783Srmacklem			    p);
2386191783Srmacklem			if (error)
2387191783Srmacklem				goto nfsmout;
2388191783Srmacklem			if (aceerr && !nd->nd_repstat)
2389224077Szack				nd->nd_repstat = aceerr;
2390191783Srmacklem			attrsum += aclsize;
2391191783Srmacklem			break;
2392191783Srmacklem		case NFSATTRBIT_ARCHIVE:
2393191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2394191783Srmacklem			if (!nd->nd_repstat)
2395191783Srmacklem				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2396191783Srmacklem			attrsum += NFSX_UNSIGNED;
2397191783Srmacklem			break;
2398191783Srmacklem		case NFSATTRBIT_HIDDEN:
2399191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2400191783Srmacklem			if (!nd->nd_repstat)
2401191783Srmacklem				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2402191783Srmacklem			attrsum += NFSX_UNSIGNED;
2403191783Srmacklem			break;
2404191783Srmacklem		case NFSATTRBIT_MIMETYPE:
2405191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2406191783Srmacklem			i = fxdr_unsigned(int, *tl);
2407191783Srmacklem			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2408191783Srmacklem			if (error)
2409191783Srmacklem				goto nfsmout;
2410191783Srmacklem			if (!nd->nd_repstat)
2411191783Srmacklem				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2412191783Srmacklem			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2413191783Srmacklem			break;
2414191783Srmacklem		case NFSATTRBIT_MODE:
2415191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2416191783Srmacklem			nvap->na_mode = nfstov_mode(*tl);
2417191783Srmacklem			attrsum += NFSX_UNSIGNED;
2418191783Srmacklem			break;
2419191783Srmacklem		case NFSATTRBIT_OWNER:
2420191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2421191783Srmacklem			j = fxdr_unsigned(int, *tl);
2422224086Szack			if (j < 0) {
2423224086Szack				error = NFSERR_BADXDR;
2424224086Szack				goto nfsmout;
2425224086Szack			}
2426191783Srmacklem			if (j > NFSV4_SMALLSTR)
2427191783Srmacklem				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2428191783Srmacklem			else
2429191783Srmacklem				cp = namestr;
2430191783Srmacklem			error = nfsrv_mtostr(nd, cp, j);
2431191783Srmacklem			if (error) {
2432191783Srmacklem				if (j > NFSV4_SMALLSTR)
2433191783Srmacklem					free(cp, M_NFSSTRING);
2434224086Szack				goto nfsmout;
2435191783Srmacklem			}
2436191783Srmacklem			if (!nd->nd_repstat) {
2437191783Srmacklem				nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p);
2438191783Srmacklem				if (!nd->nd_repstat)
2439191783Srmacklem					nvap->na_uid = uid;
2440191783Srmacklem			}
2441191783Srmacklem			if (j > NFSV4_SMALLSTR)
2442191783Srmacklem				free(cp, M_NFSSTRING);
2443191783Srmacklem			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2444191783Srmacklem			break;
2445191783Srmacklem		case NFSATTRBIT_OWNERGROUP:
2446191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2447191783Srmacklem			j = fxdr_unsigned(int, *tl);
2448224086Szack			if (j < 0) {
2449224086Szack				error = NFSERR_BADXDR;
2450224086Szack				goto nfsmout;
2451224086Szack			}
2452191783Srmacklem			if (j > NFSV4_SMALLSTR)
2453191783Srmacklem				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2454191783Srmacklem			else
2455191783Srmacklem				cp = namestr;
2456191783Srmacklem			error = nfsrv_mtostr(nd, cp, j);
2457191783Srmacklem			if (error) {
2458191783Srmacklem				if (j > NFSV4_SMALLSTR)
2459191783Srmacklem					free(cp, M_NFSSTRING);
2460224086Szack				goto nfsmout;
2461191783Srmacklem			}
2462191783Srmacklem			if (!nd->nd_repstat) {
2463191783Srmacklem				nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p);
2464191783Srmacklem				if (!nd->nd_repstat)
2465191783Srmacklem					nvap->na_gid = gid;
2466191783Srmacklem			}
2467191783Srmacklem			if (j > NFSV4_SMALLSTR)
2468191783Srmacklem				free(cp, M_NFSSTRING);
2469191783Srmacklem			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2470191783Srmacklem			break;
2471191783Srmacklem		case NFSATTRBIT_SYSTEM:
2472191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2473191783Srmacklem			if (!nd->nd_repstat)
2474191783Srmacklem				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2475191783Srmacklem			attrsum += NFSX_UNSIGNED;
2476191783Srmacklem			break;
2477191783Srmacklem		case NFSATTRBIT_TIMEACCESSSET:
2478191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2479191783Srmacklem			attrsum += NFSX_UNSIGNED;
2480191783Srmacklem			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2481191783Srmacklem			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2482191783Srmacklem			    fxdr_nfsv4time(tl, &nvap->na_atime);
2483191783Srmacklem			    toclient = 1;
2484191783Srmacklem			    attrsum += NFSX_V4TIME;
2485191783Srmacklem			} else {
2486191783Srmacklem			    NFSGETTIME(&curtime);
2487191783Srmacklem			    nvap->na_atime.tv_sec = curtime.tv_sec;
2488191783Srmacklem			    nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2489191783Srmacklem			    nvap->na_vaflags |= VA_UTIMES_NULL;
2490191783Srmacklem			}
2491191783Srmacklem			break;
2492191783Srmacklem		case NFSATTRBIT_TIMEBACKUP:
2493191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2494191783Srmacklem			if (!nd->nd_repstat)
2495191783Srmacklem				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2496191783Srmacklem			attrsum += NFSX_V4TIME;
2497191783Srmacklem			break;
2498191783Srmacklem		case NFSATTRBIT_TIMECREATE:
2499191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2500191783Srmacklem			if (!nd->nd_repstat)
2501191783Srmacklem				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2502191783Srmacklem			attrsum += NFSX_V4TIME;
2503191783Srmacklem			break;
2504191783Srmacklem		case NFSATTRBIT_TIMEMODIFYSET:
2505191783Srmacklem			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2506191783Srmacklem			attrsum += NFSX_UNSIGNED;
2507191783Srmacklem			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2508191783Srmacklem			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2509191783Srmacklem			    fxdr_nfsv4time(tl, &nvap->na_mtime);
2510191783Srmacklem			    nvap->na_vaflags &= ~VA_UTIMES_NULL;
2511191783Srmacklem			    attrsum += NFSX_V4TIME;
2512191783Srmacklem			} else {
2513191783Srmacklem			    NFSGETTIME(&curtime);
2514191783Srmacklem			    nvap->na_mtime.tv_sec = curtime.tv_sec;
2515191783Srmacklem			    nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2516191783Srmacklem			    if (!toclient)
2517191783Srmacklem				nvap->na_vaflags |= VA_UTIMES_NULL;
2518191783Srmacklem			}
2519191783Srmacklem			break;
2520191783Srmacklem		default:
2521191783Srmacklem			nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2522191783Srmacklem			/*
2523191783Srmacklem			 * set bitpos so we drop out of the loop.
2524191783Srmacklem			 */
2525191783Srmacklem			bitpos = NFSATTRBIT_MAX;
2526191783Srmacklem			break;
2527191783Srmacklem		};
2528191783Srmacklem	}
2529191783Srmacklem
2530191783Srmacklem	/*
2531191783Srmacklem	 * some clients pad the attrlist, so we need to skip over the
2532191783Srmacklem	 * padding.
2533191783Srmacklem	 */
2534191783Srmacklem	if (attrsum > attrsize) {
2535191783Srmacklem		error = NFSERR_BADXDR;
2536191783Srmacklem	} else {
2537191783Srmacklem		attrsize = NFSM_RNDUP(attrsize);
2538191783Srmacklem		if (attrsum < attrsize)
2539191783Srmacklem			error = nfsm_advance(nd, attrsize - attrsum, -1);
2540191783Srmacklem	}
2541191783Srmacklemnfsmout:
2542224086Szack	NFSEXITCODE2(error, nd);
2543191783Srmacklem	return (error);
2544191783Srmacklem}
2545191783Srmacklem
2546191783Srmacklem/*
2547191783Srmacklem * Check/setup export credentials.
2548191783Srmacklem */
2549191783Srmacklemint
2550191783Srmacklemnfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2551191783Srmacklem    struct ucred *credanon)
2552191783Srmacklem{
2553191783Srmacklem	int error = 0;
2554191783Srmacklem
2555191783Srmacklem	/*
2556191783Srmacklem	 * Check/setup credentials.
2557191783Srmacklem	 */
2558191783Srmacklem	if (nd->nd_flag & ND_GSS)
2559192121Srmacklem		exp->nes_exflag &= ~MNT_EXPORTANON;
2560191783Srmacklem
2561191783Srmacklem	/*
2562192121Srmacklem	 * Check to see if the operation is allowed for this security flavor.
2563191783Srmacklem	 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2564191783Srmacklem	 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2565192121Srmacklem	 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2566191783Srmacklem	 */
2567192121Srmacklem	if (nfsvno_testexp(nd, exp) &&
2568192121Srmacklem	    nd->nd_procnum != NFSV4OP_SECINFO &&
2569191783Srmacklem	    nd->nd_procnum != NFSPROC_FSINFO) {
2570191783Srmacklem		if (nd->nd_flag & ND_NFSV4)
2571191783Srmacklem			error = NFSERR_WRONGSEC;
2572191783Srmacklem		else
2573191783Srmacklem			error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2574224086Szack		goto out;
2575191783Srmacklem	}
2576191783Srmacklem
2577191783Srmacklem	/*
2578191783Srmacklem	 * Check to see if the file system is exported V4 only.
2579191783Srmacklem	 */
2580224086Szack	if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2581224086Szack		error = NFSERR_PROGNOTV4;
2582224086Szack		goto out;
2583224086Szack	}
2584191783Srmacklem
2585191783Srmacklem	/*
2586191783Srmacklem	 * Now, map the user credentials.
2587191783Srmacklem	 * (Note that ND_AUTHNONE will only be set for an NFSv3
2588191783Srmacklem	 *  Fsinfo RPC. If set for anything else, this code might need
2589191783Srmacklem	 *  to change.)
2590191783Srmacklem	 */
2591191783Srmacklem	if (NFSVNO_EXPORTED(exp) &&
2592191783Srmacklem	    ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2593191783Srmacklem	     NFSVNO_EXPORTANON(exp) ||
2594191783Srmacklem	     (nd->nd_flag & ND_AUTHNONE))) {
2595191783Srmacklem		nd->nd_cred->cr_uid = credanon->cr_uid;
2596191783Srmacklem		nd->nd_cred->cr_gid = credanon->cr_gid;
2597194498Sbrooks		crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2598194498Sbrooks		    credanon->cr_groups);
2599191783Srmacklem	}
2600224086Szack
2601224086Szackout:
2602224086Szack	NFSEXITCODE2(error, nd);
2603224086Szack	return (error);
2604191783Srmacklem}
2605191783Srmacklem
2606191783Srmacklem/*
2607191783Srmacklem * Check exports.
2608191783Srmacklem */
2609191783Srmacklemint
2610191783Srmacklemnfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2611191783Srmacklem    struct ucred **credp)
2612191783Srmacklem{
2613192121Srmacklem	int i, error, *secflavors;
2614191783Srmacklem
2615191783Srmacklem	error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2616192121Srmacklem	    &exp->nes_numsecflavor, &secflavors);
2617192121Srmacklem	if (error) {
2618192121Srmacklem		if (nfs_rootfhset) {
2619192121Srmacklem			exp->nes_exflag = 0;
2620192121Srmacklem			exp->nes_numsecflavor = 0;
2621192121Srmacklem			error = 0;
2622192121Srmacklem		}
2623192121Srmacklem	} else {
2624192121Srmacklem		/* Copy the security flavors. */
2625192121Srmacklem		for (i = 0; i < exp->nes_numsecflavor; i++)
2626192121Srmacklem			exp->nes_secflavors[i] = secflavors[i];
2627191783Srmacklem	}
2628224086Szack	NFSEXITCODE(error);
2629191783Srmacklem	return (error);
2630191783Srmacklem}
2631191783Srmacklem
2632191783Srmacklem/*
2633191783Srmacklem * Get a vnode for a file handle and export stuff.
2634191783Srmacklem */
2635191783Srmacklemint
2636191783Srmacklemnfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2637216700Srmacklem    int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2638216700Srmacklem    struct ucred **credp)
2639191783Srmacklem{
2640192121Srmacklem	int i, error, *secflavors;
2641191783Srmacklem
2642191998Srmacklem	*credp = NULL;
2643192121Srmacklem	exp->nes_numsecflavor = 0;
2644217063Srmacklem	if (VFS_NEEDSGIANT(mp))
2645217063Srmacklem		error = ESTALE;
2646217063Srmacklem	else
2647222167Srmacklem		error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp);
2648205663Srmacklem	if (error != 0)
2649205663Srmacklem		/* Make sure the server replies ESTALE to the client. */
2650205663Srmacklem		error = ESTALE;
2651191783Srmacklem	if (nam && !error) {
2652191783Srmacklem		error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2653192121Srmacklem		    &exp->nes_numsecflavor, &secflavors);
2654191783Srmacklem		if (error) {
2655191783Srmacklem			if (nfs_rootfhset) {
2656191783Srmacklem				exp->nes_exflag = 0;
2657192121Srmacklem				exp->nes_numsecflavor = 0;
2658191783Srmacklem				error = 0;
2659191783Srmacklem			} else {
2660191783Srmacklem				vput(*vpp);
2661191783Srmacklem			}
2662192121Srmacklem		} else {
2663192121Srmacklem			/* Copy the security flavors. */
2664192121Srmacklem			for (i = 0; i < exp->nes_numsecflavor; i++)
2665192121Srmacklem				exp->nes_secflavors[i] = secflavors[i];
2666191783Srmacklem		}
2667191783Srmacklem	}
2668216700Srmacklem	if (error == 0 && lktype == LK_SHARED)
2669216700Srmacklem		/*
2670216700Srmacklem		 * It would be much better to pass lktype to VFS_FHTOVP(),
2671216700Srmacklem		 * but this will have to do until VFS_FHTOVP() has a lock
2672216700Srmacklem		 * type argument like VFS_VGET().
2673216700Srmacklem		 */
2674224081Szack		NFSVOPLOCK(*vpp, LK_DOWNGRADE | LK_RETRY);
2675224086Szack
2676224086Szack	NFSEXITCODE(error);
2677191783Srmacklem	return (error);
2678191783Srmacklem}
2679191783Srmacklem
2680191783Srmacklem/*
2681191783Srmacklem * nfsd_fhtovp() - convert a fh to a vnode ptr
2682191783Srmacklem * 	- look up fsid in mount list (if not found ret error)
2683191783Srmacklem *	- get vp and export rights by calling nfsvno_fhtovp()
2684191783Srmacklem *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2685191783Srmacklem *	  for AUTH_SYS
2686217063Srmacklem *	- if mpp != NULL, return the mount point so that it can
2687217063Srmacklem *	  be used for vn_finished_write() by the caller
2688191783Srmacklem */
2689191783Srmacklemvoid
2690216700Srmacklemnfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2691191783Srmacklem    struct vnode **vpp, struct nfsexstuff *exp,
2692191783Srmacklem    struct mount **mpp, int startwrite, struct thread *p)
2693191783Srmacklem{
2694191783Srmacklem	struct mount *mp;
2695191783Srmacklem	struct ucred *credanon;
2696191783Srmacklem	fhandle_t *fhp;
2697191783Srmacklem
2698191783Srmacklem	fhp = (fhandle_t *)nfp->nfsrvfh_data;
2699191783Srmacklem	/*
2700191783Srmacklem	 * Check for the special case of the nfsv4root_fh.
2701191783Srmacklem	 */
2702217017Srmacklem	mp = vfs_busyfs(&fhp->fh_fsid);
2703217063Srmacklem	if (mpp != NULL)
2704217063Srmacklem		*mpp = mp;
2705217017Srmacklem	if (mp == NULL) {
2706191783Srmacklem		*vpp = NULL;
2707191783Srmacklem		nd->nd_repstat = ESTALE;
2708224086Szack		goto out;
2709191783Srmacklem	}
2710191783Srmacklem
2711191783Srmacklem	if (startwrite)
2712191783Srmacklem		vn_start_write(NULL, mpp, V_WAIT);
2713191783Srmacklem
2714216700Srmacklem	nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2715191783Srmacklem	    &credanon);
2716217017Srmacklem	vfs_unbusy(mp);
2717191783Srmacklem
2718191783Srmacklem	/*
2719191783Srmacklem	 * For NFSv4 without a pseudo root fs, unexported file handles
2720191783Srmacklem	 * can be returned, so that Lookup works everywhere.
2721191783Srmacklem	 */
2722191783Srmacklem	if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2723191783Srmacklem	    !(nd->nd_flag & ND_NFSV4)) {
2724191783Srmacklem		vput(*vpp);
2725191783Srmacklem		nd->nd_repstat = EACCES;
2726191783Srmacklem	}
2727191783Srmacklem
2728191783Srmacklem	/*
2729191783Srmacklem	 * Personally, I've never seen any point in requiring a
2730191783Srmacklem	 * reserved port#, since only in the rare case where the
2731191783Srmacklem	 * clients are all boxes with secure system priviledges,
2732191783Srmacklem	 * does it provide any enhanced security, but... some people
2733191783Srmacklem	 * believe it to be useful and keep putting this code back in.
2734191783Srmacklem	 * (There is also some "security checker" out there that
2735191783Srmacklem	 *  complains if the nfs server doesn't enforce this.)
2736191783Srmacklem	 * However, note the following:
2737191783Srmacklem	 * RFC3530 (NFSv4) specifies that a reserved port# not be
2738191783Srmacklem	 *	required.
2739191783Srmacklem	 * RFC2623 recommends that, if a reserved port# is checked for,
2740191783Srmacklem	 *	that there be a way to turn that off--> ifdef'd.
2741191783Srmacklem	 */
2742191783Srmacklem#ifdef NFS_REQRSVPORT
2743191783Srmacklem	if (!nd->nd_repstat) {
2744192121Srmacklem		struct sockaddr_in *saddr;
2745192121Srmacklem		struct sockaddr_in6 *saddr6;
2746192121Srmacklem
2747192121Srmacklem		saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2748192121Srmacklem		saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2749192121Srmacklem		if (!(nd->nd_flag & ND_NFSV4) &&
2750192121Srmacklem		    ((saddr->sin_family == AF_INET &&
2751192121Srmacklem		      ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2752192121Srmacklem		     (saddr6->sin6_family == AF_INET6 &&
2753192121Srmacklem		      ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2754192121Srmacklem			vput(*vpp);
2755192121Srmacklem			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2756192121Srmacklem		}
2757191783Srmacklem	}
2758191783Srmacklem#endif	/* NFS_REQRSVPORT */
2759191783Srmacklem
2760191783Srmacklem	/*
2761191783Srmacklem	 * Check/setup credentials.
2762191783Srmacklem	 */
2763191783Srmacklem	if (!nd->nd_repstat) {
2764191783Srmacklem		nd->nd_saveduid = nd->nd_cred->cr_uid;
2765191783Srmacklem		nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2766191783Srmacklem		if (nd->nd_repstat)
2767191783Srmacklem			vput(*vpp);
2768191783Srmacklem	}
2769191940Skan	if (credanon != NULL)
2770191940Skan		crfree(credanon);
2771191783Srmacklem	if (nd->nd_repstat) {
2772191783Srmacklem		if (startwrite)
2773191783Srmacklem			vn_finished_write(mp);
2774191783Srmacklem		*vpp = NULL;
2775217063Srmacklem		if (mpp != NULL)
2776217063Srmacklem			*mpp = NULL;
2777191783Srmacklem	}
2778224086Szack
2779224086Szackout:
2780224086Szack	NFSEXITCODE2(0, nd);
2781191783Srmacklem}
2782191783Srmacklem
2783191783Srmacklem/*
2784191783Srmacklem * glue for fp.
2785191783Srmacklem */
2786191783Srmacklemint
2787191783Srmacklemfp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2788191783Srmacklem{
2789191783Srmacklem	struct filedesc *fdp;
2790191783Srmacklem	struct file *fp;
2791224086Szack	int error = 0;
2792191783Srmacklem
2793191783Srmacklem	fdp = p->td_proc->p_fd;
2794191783Srmacklem	if (fd >= fdp->fd_nfiles ||
2795224086Szack	    (fp = fdp->fd_ofiles[fd]) == NULL) {
2796224086Szack		error = EBADF;
2797224086Szack		goto out;
2798224086Szack	}
2799191783Srmacklem	*fpp = fp;
2800224086Szack
2801224086Szackout:
2802224086Szack	NFSEXITCODE(error);
2803224086Szack	return (error);
2804191783Srmacklem}
2805191783Srmacklem
2806191783Srmacklem/*
2807192121Srmacklem * Called from nfssvc() to update the exports list. Just call
2808191783Srmacklem * vfs_export(). This has to be done, since the v4 root fake fs isn't
2809191783Srmacklem * in the mount list.
2810191783Srmacklem */
2811191783Srmacklemint
2812191783Srmacklemnfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2813191783Srmacklem{
2814191783Srmacklem	struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2815224086Szack	int error = 0;
2816191783Srmacklem	struct nameidata nd;
2817191783Srmacklem	fhandle_t fh;
2818191783Srmacklem
2819191783Srmacklem	error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2820216894Srmacklem	if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2821191783Srmacklem		nfs_rootfhset = 0;
2822216894Srmacklem	else if (error == 0) {
2823224086Szack		if (nfsexargp->fspec == NULL) {
2824224086Szack			error = EPERM;
2825224086Szack			goto out;
2826224086Szack		}
2827191783Srmacklem		/*
2828191783Srmacklem		 * If fspec != NULL, this is the v4root path.
2829191783Srmacklem		 */
2830191783Srmacklem		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2831191783Srmacklem		    nfsexargp->fspec, p);
2832191783Srmacklem		if ((error = namei(&nd)) != 0)
2833224086Szack			goto out;
2834191783Srmacklem		error = nfsvno_getfh(nd.ni_vp, &fh, p);
2835191783Srmacklem		vrele(nd.ni_vp);
2836191783Srmacklem		if (!error) {
2837191783Srmacklem			nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2838191783Srmacklem			NFSBCOPY((caddr_t)&fh,
2839191783Srmacklem			    nfs_rootfh.nfsrvfh_data,
2840191783Srmacklem			    sizeof (fhandle_t));
2841191783Srmacklem			nfs_rootfhset = 1;
2842191783Srmacklem		}
2843191783Srmacklem	}
2844224086Szack
2845224086Szackout:
2846224086Szack	NFSEXITCODE(error);
2847191783Srmacklem	return (error);
2848191783Srmacklem}
2849191783Srmacklem
2850191783Srmacklem/*
2851191783Srmacklem * Get the tcp socket sequence numbers we need.
2852191783Srmacklem * (Maybe this should be moved to the tcp sources?)
2853191783Srmacklem */
2854191783Srmacklemint
2855191783Srmacklemnfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2856191783Srmacklem{
2857191783Srmacklem	struct inpcb *inp;
2858191783Srmacklem	struct tcpcb *tp;
2859224086Szack	int error = 0;
2860191783Srmacklem
2861191783Srmacklem	inp = sotoinpcb(so);
2862205010Srwatson	KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2863205010Srwatson	INP_RLOCK(inp);
2864205010Srwatson	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2865205010Srwatson		INP_RUNLOCK(inp);
2866224086Szack		error = EPIPE;
2867224086Szack		goto out;
2868191783Srmacklem	}
2869191783Srmacklem	tp = intotcpcb(inp);
2870205010Srwatson	if (tp->t_state != TCPS_ESTABLISHED) {
2871205010Srwatson		INP_RUNLOCK(inp);
2872224086Szack		error = EPIPE;
2873224086Szack		goto out;
2874191783Srmacklem	}
2875205010Srwatson	*maxp = tp->snd_max;
2876205010Srwatson	*unap = tp->snd_una;
2877191783Srmacklem	INP_RUNLOCK(inp);
2878224086Szack
2879224086Szackout:
2880224086Szack	NFSEXITCODE(error);
2881224086Szack	return (error);
2882191783Srmacklem}
2883191783Srmacklem
2884191783Srmacklem/*
2885191783Srmacklem * This function needs to test to see if the system is near its limit
2886191783Srmacklem * for memory allocation via malloc() or mget() and return True iff
2887191783Srmacklem * either of these resources are near their limit.
2888191783Srmacklem * XXX (For now, this is just a stub.)
2889191783Srmacklem */
2890191783Srmacklemint nfsrv_testmalloclimit = 0;
2891191783Srmacklemint
2892191783Srmacklemnfsrv_mallocmget_limit(void)
2893191783Srmacklem{
2894191783Srmacklem	static int printmesg = 0;
2895191783Srmacklem	static int testval = 1;
2896191783Srmacklem
2897191783Srmacklem	if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2898191783Srmacklem		if ((printmesg++ % 100) == 0)
2899191783Srmacklem			printf("nfsd: malloc/mget near limit\n");
2900191783Srmacklem		return (1);
2901191783Srmacklem	}
2902191783Srmacklem	return (0);
2903191783Srmacklem}
2904191783Srmacklem
2905191783Srmacklem/*
2906191783Srmacklem * BSD specific initialization of a mount point.
2907191783Srmacklem */
2908191783Srmacklemvoid
2909191783Srmacklemnfsd_mntinit(void)
2910191783Srmacklem{
2911191783Srmacklem	static int inited = 0;
2912191783Srmacklem
2913191783Srmacklem	if (inited)
2914191783Srmacklem		return;
2915191783Srmacklem	inited = 1;
2916191783Srmacklem	nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2917191783Srmacklem	TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2918191783Srmacklem	nfsv4root_mnt.mnt_export = NULL;
2919191783Srmacklem	TAILQ_INIT(&nfsv4root_opt);
2920191783Srmacklem	TAILQ_INIT(&nfsv4root_newopt);
2921191783Srmacklem	nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2922191783Srmacklem	nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2923191783Srmacklem	nfsv4root_mnt.mnt_nvnodelistsize = 0;
2924191783Srmacklem}
2925191783Srmacklem
2926191783Srmacklem/*
2927191783Srmacklem * Get a vnode for a file handle, without checking exports, etc.
2928191783Srmacklem */
2929191783Srmacklemstruct vnode *
2930191783Srmacklemnfsvno_getvp(fhandle_t *fhp)
2931191783Srmacklem{
2932191783Srmacklem	struct mount *mp;
2933191783Srmacklem	struct vnode *vp;
2934191783Srmacklem	int error;
2935191783Srmacklem
2936217017Srmacklem	mp = vfs_busyfs(&fhp->fh_fsid);
2937191783Srmacklem	if (mp == NULL)
2938191783Srmacklem		return (NULL);
2939222167Srmacklem	error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2940217017Srmacklem	vfs_unbusy(mp);
2941191783Srmacklem	if (error)
2942191783Srmacklem		return (NULL);
2943191783Srmacklem	return (vp);
2944191783Srmacklem}
2945191783Srmacklem
2946191783Srmacklem/*
2947191783Srmacklem * Do a local VOP_ADVLOCK().
2948191783Srmacklem */
2949191783Srmacklemint
2950191783Srmacklemnfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2951192256Srmacklem    u_int64_t end, struct thread *td)
2952191783Srmacklem{
2953224086Szack	int error = 0;
2954191783Srmacklem	struct flock fl;
2955191783Srmacklem	u_int64_t tlen;
2956191783Srmacklem
2957212833Srmacklem	if (nfsrv_dolocallocks == 0)
2958224086Szack		goto out;
2959216893Srmacklem
2960216893Srmacklem	/* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2961224086Szack	if ((vp->v_iflag & VI_DOOMED) != 0) {
2962224086Szack		error = EPERM;
2963224086Szack		goto out;
2964224086Szack	}
2965216893Srmacklem
2966191783Srmacklem	fl.l_whence = SEEK_SET;
2967191783Srmacklem	fl.l_type = ftype;
2968191783Srmacklem	fl.l_start = (off_t)first;
2969191783Srmacklem	if (end == NFS64BITSSET) {
2970191783Srmacklem		fl.l_len = 0;
2971191783Srmacklem	} else {
2972191783Srmacklem		tlen = end - first;
2973191783Srmacklem		fl.l_len = (off_t)tlen;
2974191783Srmacklem	}
2975191783Srmacklem	/*
2976192256Srmacklem	 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2977192256Srmacklem	 * values for all calls, so that all locks will be held by the
2978192256Srmacklem	 * nfsd server. (The nfsd server handles conflicts between the
2979192256Srmacklem	 * various clients.)
2980192256Srmacklem	 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2981192256Srmacklem	 * bytes, so it can't be put in l_sysid.
2982191783Srmacklem	 */
2983192503Srmacklem	if (nfsv4_sysid == 0)
2984192503Srmacklem		nfsv4_sysid = nlm_acquire_next_sysid();
2985192256Srmacklem	fl.l_pid = (pid_t)0;
2986192503Srmacklem	fl.l_sysid = (int)nfsv4_sysid;
2987192256Srmacklem
2988224080Szack	NFSVOPUNLOCK(vp, 0);
2989212833Srmacklem	if (ftype == F_UNLCK)
2990212833Srmacklem		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2991212833Srmacklem		    (F_POSIX | F_REMOTE));
2992212833Srmacklem	else
2993212833Srmacklem		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2994212833Srmacklem		    (F_POSIX | F_REMOTE));
2995224080Szack	NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2996224086Szack
2997224086Szackout:
2998224086Szack	NFSEXITCODE(error);
2999191783Srmacklem	return (error);
3000191783Srmacklem}
3001191783Srmacklem
3002191783Srmacklem/*
3003191783Srmacklem * Check the nfsv4 root exports.
3004191783Srmacklem */
3005191783Srmacklemint
3006191783Srmacklemnfsvno_v4rootexport(struct nfsrv_descript *nd)
3007191783Srmacklem{
3008191783Srmacklem	struct ucred *credanon;
3009224086Szack	int exflags, error = 0, numsecflavor, *secflavors, i;
3010191783Srmacklem
3011191783Srmacklem	error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3012192121Srmacklem	    &credanon, &numsecflavor, &secflavors);
3013224086Szack	if (error) {
3014224086Szack		error = NFSERR_PROGUNAVAIL;
3015224086Szack		goto out;
3016224086Szack	}
3017191940Skan	if (credanon != NULL)
3018191940Skan		crfree(credanon);
3019192121Srmacklem	for (i = 0; i < numsecflavor; i++) {
3020192121Srmacklem		if (secflavors[i] == AUTH_SYS)
3021192121Srmacklem			nd->nd_flag |= ND_EXAUTHSYS;
3022192121Srmacklem		else if (secflavors[i] == RPCSEC_GSS_KRB5)
3023192121Srmacklem			nd->nd_flag |= ND_EXGSS;
3024192121Srmacklem		else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3025192121Srmacklem			nd->nd_flag |= ND_EXGSSINTEGRITY;
3026192121Srmacklem		else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3027192121Srmacklem			nd->nd_flag |= ND_EXGSSPRIVACY;
3028192121Srmacklem	}
3029224086Szack
3030224086Szackout:
3031224086Szack	NFSEXITCODE(error);
3032224086Szack	return (error);
3033191783Srmacklem}
3034191783Srmacklem
3035191783Srmacklem/*
3036191783Srmacklem * Nfs server psuedo system call for the nfsd's
3037191783Srmacklem */
3038191783Srmacklem/*
3039191783Srmacklem * MPSAFE
3040191783Srmacklem */
3041191783Srmacklemstatic int
3042191783Srmacklemnfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3043191783Srmacklem{
3044191783Srmacklem	struct file *fp;
3045192017Srmacklem	struct nfsd_addsock_args sockarg;
3046192017Srmacklem	struct nfsd_nfsd_args nfsdarg;
3047191783Srmacklem	int error;
3048191783Srmacklem
3049191783Srmacklem	if (uap->flag & NFSSVC_NFSDADDSOCK) {
3050192017Srmacklem		error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3051191783Srmacklem		if (error)
3052224086Szack			goto out;
3053224778Srwatson		/*
3054224778Srwatson		 * Since we don't know what rights might be required,
3055224778Srwatson		 * pretend that we need them all. It is better to be too
3056224778Srwatson		 * careful than too reckless.
3057224778Srwatson		 */
3058224778Srwatson		if ((error = fget(td, sockarg.sock, CAP_SOCK_ALL, &fp)) != 0)
3059224086Szack			goto out;
3060191783Srmacklem		if (fp->f_type != DTYPE_SOCKET) {
3061191783Srmacklem			fdrop(fp, td);
3062224086Szack			error = EPERM;
3063224086Szack			goto out;
3064191783Srmacklem		}
3065191783Srmacklem		error = nfsrvd_addsock(fp);
3066191783Srmacklem		fdrop(fp, td);
3067191783Srmacklem	} else if (uap->flag & NFSSVC_NFSDNFSD) {
3068224086Szack		if (uap->argp == NULL) {
3069224086Szack			error = EINVAL;
3070224086Szack			goto out;
3071224086Szack		}
3072192017Srmacklem		error = copyin(uap->argp, (caddr_t)&nfsdarg,
3073192017Srmacklem		    sizeof (nfsdarg));
3074192017Srmacklem		if (error)
3075224086Szack			goto out;
3076192017Srmacklem		error = nfsrvd_nfsd(td, &nfsdarg);
3077191783Srmacklem	} else {
3078191783Srmacklem		error = nfssvc_srvcall(td, uap, td->td_ucred);
3079191783Srmacklem	}
3080224086Szack
3081224086Szackout:
3082224086Szack	NFSEXITCODE(error);
3083191783Srmacklem	return (error);
3084191783Srmacklem}
3085191783Srmacklem
3086191783Srmacklemstatic int
3087191783Srmacklemnfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3088191783Srmacklem{
3089191783Srmacklem	struct nfsex_args export;
3090191783Srmacklem	struct file *fp = NULL;
3091191783Srmacklem	int stablefd, len;
3092191783Srmacklem	struct nfsd_clid adminrevoke;
3093191783Srmacklem	struct nfsd_dumplist dumplist;
3094191783Srmacklem	struct nfsd_dumpclients *dumpclients;
3095191783Srmacklem	struct nfsd_dumplocklist dumplocklist;
3096191783Srmacklem	struct nfsd_dumplocks *dumplocks;
3097191783Srmacklem	struct nameidata nd;
3098191783Srmacklem	vnode_t vp;
3099191783Srmacklem	int error = EINVAL;
3100217432Srmacklem	struct proc *procp;
3101191783Srmacklem
3102191783Srmacklem	if (uap->flag & NFSSVC_PUBLICFH) {
3103191783Srmacklem		NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3104191783Srmacklem		    sizeof (fhandle_t));
3105191783Srmacklem		error = copyin(uap->argp,
3106191783Srmacklem		    &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3107191783Srmacklem		if (!error)
3108191783Srmacklem			nfs_pubfhset = 1;
3109191783Srmacklem	} else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3110191783Srmacklem		error = copyin(uap->argp,(caddr_t)&export,
3111191783Srmacklem		    sizeof (struct nfsex_args));
3112191783Srmacklem		if (!error)
3113191783Srmacklem			error = nfsrv_v4rootexport(&export, cred, p);
3114191783Srmacklem	} else if (uap->flag & NFSSVC_NOPUBLICFH) {
3115191783Srmacklem		nfs_pubfhset = 0;
3116191783Srmacklem		error = 0;
3117191783Srmacklem	} else if (uap->flag & NFSSVC_STABLERESTART) {
3118191783Srmacklem		error = copyin(uap->argp, (caddr_t)&stablefd,
3119191783Srmacklem		    sizeof (int));
3120191783Srmacklem		if (!error)
3121191783Srmacklem			error = fp_getfvp(p, stablefd, &fp, &vp);
3122191783Srmacklem		if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3123191783Srmacklem			error = EBADF;
3124191783Srmacklem		if (!error && newnfs_numnfsd != 0)
3125191783Srmacklem			error = EPERM;
3126191783Srmacklem		if (!error) {
3127191783Srmacklem			nfsrv_stablefirst.nsf_fp = fp;
3128191783Srmacklem			nfsrv_setupstable(p);
3129191783Srmacklem		}
3130191783Srmacklem	} else if (uap->flag & NFSSVC_ADMINREVOKE) {
3131191783Srmacklem		error = copyin(uap->argp, (caddr_t)&adminrevoke,
3132191783Srmacklem		    sizeof (struct nfsd_clid));
3133191783Srmacklem		if (!error)
3134191783Srmacklem			error = nfsrv_adminrevoke(&adminrevoke, p);
3135191783Srmacklem	} else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3136191783Srmacklem		error = copyin(uap->argp, (caddr_t)&dumplist,
3137191783Srmacklem		    sizeof (struct nfsd_dumplist));
3138191783Srmacklem		if (!error && (dumplist.ndl_size < 1 ||
3139191783Srmacklem			dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3140191783Srmacklem			error = EPERM;
3141191783Srmacklem		if (!error) {
3142191783Srmacklem		    len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3143191783Srmacklem		    dumpclients = (struct nfsd_dumpclients *)malloc(len,
3144191783Srmacklem			M_TEMP, M_WAITOK);
3145191783Srmacklem		    nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3146191783Srmacklem		    error = copyout(dumpclients,
3147191783Srmacklem			CAST_USER_ADDR_T(dumplist.ndl_list), len);
3148191783Srmacklem		    free((caddr_t)dumpclients, M_TEMP);
3149191783Srmacklem		}
3150191783Srmacklem	} else if (uap->flag & NFSSVC_DUMPLOCKS) {
3151191783Srmacklem		error = copyin(uap->argp, (caddr_t)&dumplocklist,
3152191783Srmacklem		    sizeof (struct nfsd_dumplocklist));
3153191783Srmacklem		if (!error && (dumplocklist.ndllck_size < 1 ||
3154191783Srmacklem			dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3155191783Srmacklem			error = EPERM;
3156191783Srmacklem		if (!error)
3157191783Srmacklem			error = nfsrv_lookupfilename(&nd,
3158191783Srmacklem				dumplocklist.ndllck_fname, p);
3159191783Srmacklem		if (!error) {
3160191783Srmacklem			len = sizeof (struct nfsd_dumplocks) *
3161191783Srmacklem				dumplocklist.ndllck_size;
3162191783Srmacklem			dumplocks = (struct nfsd_dumplocks *)malloc(len,
3163191783Srmacklem				M_TEMP, M_WAITOK);
3164191783Srmacklem			nfsrv_dumplocks(nd.ni_vp, dumplocks,
3165191783Srmacklem			    dumplocklist.ndllck_size, p);
3166191783Srmacklem			vput(nd.ni_vp);
3167191783Srmacklem			error = copyout(dumplocks,
3168191783Srmacklem			    CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3169191783Srmacklem			free((caddr_t)dumplocks, M_TEMP);
3170191783Srmacklem		}
3171217432Srmacklem	} else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3172217432Srmacklem		procp = p->td_proc;
3173217432Srmacklem		PROC_LOCK(procp);
3174217432Srmacklem		nfsd_master_pid = procp->p_pid;
3175217432Srmacklem		bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3176217432Srmacklem		nfsd_master_start = procp->p_stats->p_start;
3177217432Srmacklem		nfsd_master_proc = procp;
3178217432Srmacklem		PROC_UNLOCK(procp);
3179191783Srmacklem	}
3180224086Szack
3181224086Szack	NFSEXITCODE(error);
3182191783Srmacklem	return (error);
3183191783Srmacklem}
3184191783Srmacklem
3185192121Srmacklem/*
3186192121Srmacklem * Check exports.
3187192121Srmacklem * Returns 0 if ok, 1 otherwise.
3188192121Srmacklem */
3189192121Srmacklemint
3190192121Srmacklemnfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3191192121Srmacklem{
3192192121Srmacklem	int i;
3193192121Srmacklem
3194192121Srmacklem	/*
3195192121Srmacklem	 * This seems odd, but allow the case where the security flavor
3196192121Srmacklem	 * list is empty. This happens when NFSv4 is traversing non-exported
3197192121Srmacklem	 * file systems. Exported file systems should always have a non-empty
3198192121Srmacklem	 * security flavor list.
3199192121Srmacklem	 */
3200192121Srmacklem	if (exp->nes_numsecflavor == 0)
3201192121Srmacklem		return (0);
3202192121Srmacklem
3203192121Srmacklem	for (i = 0; i < exp->nes_numsecflavor; i++) {
3204192121Srmacklem		/*
3205192121Srmacklem		 * The tests for privacy and integrity must be first,
3206192121Srmacklem		 * since ND_GSS is set for everything but AUTH_SYS.
3207192121Srmacklem		 */
3208192121Srmacklem		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3209192121Srmacklem		    (nd->nd_flag & ND_GSSPRIVACY))
3210192121Srmacklem			return (0);
3211192121Srmacklem		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3212192121Srmacklem		    (nd->nd_flag & ND_GSSINTEGRITY))
3213192121Srmacklem			return (0);
3214192121Srmacklem		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3215192121Srmacklem		    (nd->nd_flag & ND_GSS))
3216192121Srmacklem			return (0);
3217192121Srmacklem		if (exp->nes_secflavors[i] == AUTH_SYS &&
3218192121Srmacklem		    (nd->nd_flag & ND_GSS) == 0)
3219192121Srmacklem			return (0);
3220192121Srmacklem	}
3221192121Srmacklem	return (1);
3222192121Srmacklem}
3223192121Srmacklem
3224214224Srmacklem/*
3225214224Srmacklem * Calculate a hash value for the fid in a file handle.
3226214224Srmacklem */
3227214255Srmacklemuint32_t
3228214224Srmacklemnfsrv_hashfh(fhandle_t *fhp)
3229214224Srmacklem{
3230214255Srmacklem	uint32_t hashval;
3231214224Srmacklem
3232214255Srmacklem	hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3233214224Srmacklem	return (hashval);
3234214224Srmacklem}
3235214224Srmacklem
3236217432Srmacklem/*
3237217432Srmacklem * Signal the userland master nfsd to backup the stable restart file.
3238217432Srmacklem */
3239217432Srmacklemvoid
3240217432Srmacklemnfsrv_backupstable(void)
3241217432Srmacklem{
3242217432Srmacklem	struct proc *procp;
3243217432Srmacklem
3244217432Srmacklem	if (nfsd_master_proc != NULL) {
3245217432Srmacklem		procp = pfind(nfsd_master_pid);
3246217432Srmacklem		/* Try to make sure it is the correct process. */
3247217432Srmacklem		if (procp == nfsd_master_proc &&
3248217432Srmacklem		    procp->p_stats->p_start.tv_sec ==
3249217432Srmacklem		    nfsd_master_start.tv_sec &&
3250217432Srmacklem		    procp->p_stats->p_start.tv_usec ==
3251217432Srmacklem		    nfsd_master_start.tv_usec &&
3252217432Srmacklem		    strcmp(procp->p_comm, nfsd_master_comm) == 0)
3253225617Skmacy			kern_psignal(procp, SIGUSR2);
3254217432Srmacklem		else
3255217432Srmacklem			nfsd_master_proc = NULL;
3256217432Srmacklem
3257217432Srmacklem		if (procp != NULL)
3258217432Srmacklem			PROC_UNLOCK(procp);
3259217432Srmacklem	}
3260217432Srmacklem}
3261217432Srmacklem
3262191783Srmacklemextern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3263191783Srmacklem
3264191783Srmacklem/*
3265191783Srmacklem * Called once to initialize data structures...
3266191783Srmacklem */
3267191783Srmacklemstatic int
3268191783Srmacklemnfsd_modevent(module_t mod, int type, void *data)
3269191783Srmacklem{
3270191783Srmacklem	int error = 0;
3271191783Srmacklem	static int loaded = 0;
3272191783Srmacklem
3273191783Srmacklem	switch (type) {
3274191783Srmacklem	case MOD_LOAD:
3275191783Srmacklem		if (loaded)
3276224086Szack			goto out;
3277191783Srmacklem		newnfs_portinit();
3278191783Srmacklem		mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF);
3279191783Srmacklem		mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3280191783Srmacklem		mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3281191783Srmacklem		    MTX_DEF);
3282191783Srmacklem		lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3283191783Srmacklem		nfsrvd_initcache();
3284191783Srmacklem		nfsd_init();
3285191783Srmacklem		NFSD_LOCK();
3286191783Srmacklem		nfsrvd_init(0);
3287191783Srmacklem		NFSD_UNLOCK();
3288191783Srmacklem		nfsd_mntinit();
3289191783Srmacklem#ifdef VV_DISABLEDELEG
3290191783Srmacklem		vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3291191783Srmacklem		vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3292191783Srmacklem#endif
3293191783Srmacklem		nfsd_call_servertimer = nfsrv_servertimer;
3294191783Srmacklem		nfsd_call_nfsd = nfssvc_nfsd;
3295191783Srmacklem		loaded = 1;
3296191783Srmacklem		break;
3297191783Srmacklem
3298191783Srmacklem	case MOD_UNLOAD:
3299191783Srmacklem		if (newnfs_numnfsd != 0) {
3300191783Srmacklem			error = EBUSY;
3301191783Srmacklem			break;
3302191783Srmacklem		}
3303191783Srmacklem
3304191783Srmacklem#ifdef VV_DISABLEDELEG
3305191783Srmacklem		vn_deleg_ops.vndeleg_recall = NULL;
3306191783Srmacklem		vn_deleg_ops.vndeleg_disable = NULL;
3307191783Srmacklem#endif
3308191783Srmacklem		nfsd_call_servertimer = NULL;
3309191783Srmacklem		nfsd_call_nfsd = NULL;
3310217335Szack
3311220530Srmacklem		/* Clean out all NFSv4 state. */
3312220530Srmacklem		nfsrv_throwawayallstate(curthread);
3313220530Srmacklem
3314217335Szack		/* Clean the NFS server reply cache */
3315217335Szack		nfsrvd_cleancache();
3316217335Szack
3317220530Srmacklem		/* Free up the krpc server pool. */
3318220530Srmacklem		if (nfsrvd_pool != NULL)
3319220530Srmacklem			svcpool_destroy(nfsrvd_pool);
3320220530Srmacklem
3321191783Srmacklem		/* and get rid of the locks */
3322191783Srmacklem		mtx_destroy(&nfs_cache_mutex);
3323191783Srmacklem		mtx_destroy(&nfs_v4root_mutex);
3324191783Srmacklem		mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3325191783Srmacklem		lockdestroy(&nfsv4root_mnt.mnt_explock);
3326191783Srmacklem		loaded = 0;
3327191783Srmacklem		break;
3328191783Srmacklem	default:
3329191783Srmacklem		error = EOPNOTSUPP;
3330191783Srmacklem		break;
3331191783Srmacklem	}
3332224086Szack
3333224086Szackout:
3334224086Szack	NFSEXITCODE(error);
3335224086Szack	return (error);
3336191783Srmacklem}
3337191783Srmacklemstatic moduledata_t nfsd_mod = {
3338191783Srmacklem	"nfsd",
3339191783Srmacklem	nfsd_modevent,
3340191783Srmacklem	NULL,
3341191783Srmacklem};
3342191783SrmacklemDECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3343191783Srmacklem
3344191783Srmacklem/* So that loader and kldload(2) can find us, wherever we are.. */
3345191783SrmacklemMODULE_VERSION(nfsd, 1);
3346191783SrmacklemMODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3347216931SrmacklemMODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3348192574SrmacklemMODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3349209191SrmacklemMODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3350209191SrmacklemMODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
3351191783Srmacklem
3352