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