xref: /freebsd/sys/fs/nfsserver/nfs_nfsdport.c (revision 3e230e0cc4ad822554afaaa07369ca5ccb62054d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/capsicum.h>
40 #include <sys/extattr.h>
41 
42 /*
43  * Functions that perform the vfs operations required by the routines in
44  * nfsd_serv.c. It is hoped that this change will make the server more
45  * portable.
46  */
47 
48 #include <fs/nfs/nfsport.h>
49 #include <security/mac/mac_framework.h>
50 #include <sys/callout.h>
51 #include <sys/filio.h>
52 #include <sys/hash.h>
53 #include <sys/sysctl.h>
54 #include <nlm/nlm_prot.h>
55 #include <nlm/nlm.h>
56 
57 FEATURE(nfsd, "NFSv4 server");
58 
59 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
60 extern int nfsrv_useacl;
61 extern int newnfs_numnfsd;
62 extern struct mount nfsv4root_mnt;
63 extern struct nfsrv_stablefirst nfsrv_stablefirst;
64 extern SVCPOOL	*nfsrvd_pool;
65 extern struct nfsv4lock nfsd_suspend_lock;
66 extern struct nfsclienthashhead *nfsclienthash;
67 extern struct nfslockhashhead *nfslockhash;
68 extern struct nfssessionhash *nfssessionhash;
69 extern int nfsrv_sessionhashsize;
70 extern struct nfsstatsv1 nfsstatsv1;
71 extern struct nfslayouthash *nfslayouthash;
72 extern int nfsrv_layouthashsize;
73 extern struct mtx nfsrv_dslock_mtx;
74 extern int nfs_pnfsiothreads;
75 extern struct nfsdontlisthead nfsrv_dontlisthead;
76 extern volatile int nfsrv_dontlistlen;
77 extern volatile int nfsrv_devidcnt;
78 extern int nfsrv_maxpnfsmirror;
79 extern uint32_t nfs_srvmaxio;
80 extern int nfs_bufpackets;
81 extern u_long sb_max_adj;
82 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
83 NFSDLOCKMUTEX;
84 NFSSTATESPINLOCK;
85 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
86 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
87 struct mtx nfsrc_udpmtx;
88 struct mtx nfs_v4root_mutex;
89 struct mtx nfsrv_dontlistlock_mtx;
90 struct mtx nfsrv_recalllock_mtx;
91 struct nfsrvfh nfs_rootfh, nfs_pubfh;
92 int nfs_pubfhset = 0, nfs_rootfhset = 0;
93 struct proc *nfsd_master_proc = NULL;
94 int nfsd_debuglevel = 0;
95 static pid_t nfsd_master_pid = (pid_t)-1;
96 static char nfsd_master_comm[MAXCOMLEN + 1];
97 static struct timeval nfsd_master_start;
98 static uint32_t nfsv4_sysid = 0;
99 static fhandle_t zerofh;
100 struct callout nfsd_callout;
101 
102 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
103     struct ucred *);
104 static void nfsvno_updateds(struct vnode *, struct ucred *, struct thread *);
105 
106 int nfsrv_enable_crossmntpt = 1;
107 static int nfs_commit_blks;
108 static int nfs_commit_miss;
109 extern int nfsrv_issuedelegs;
110 extern int nfsrv_dolocallocks;
111 extern int nfsd_enable_stringtouid;
112 extern struct nfsdevicehead nfsrv_devidhead;
113 
114 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
115     struct iovec **);
116 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
117     struct mbuf **, struct iovec **);
118 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
119     int *);
120 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
121     NFSPROC_T *);
122 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
123     int *, char *, fhandle_t *);
124 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
125     NFSPROC_T *);
126 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
127     struct thread *, int, struct mbuf **, char *, struct mbuf **,
128     struct nfsvattr *, struct acl *, off_t *, int, bool *);
129 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
130 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
131     NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
132 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
133     NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
134     char *, int *);
135 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
136     NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
137 static int nfsrv_deallocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
138     NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
139 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
140     struct vnode *, struct nfsmount **, int, struct acl *, int *);
141 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
142     struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
143 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
144     struct vnode *, struct nfsmount *, struct nfsvattr *);
145 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
146     NFSPROC_T *, struct nfsmount *);
147 static int nfsrv_putfhname(fhandle_t *, char *);
148 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
149     struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
150 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
151     struct vnode *, NFSPROC_T *);
152 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
153 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
154     NFSPROC_T *);
155 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
156 
157 int nfs_pnfsio(task_fn_t *, void *);
158 
159 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
160     "NFS server");
161 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
162     &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
163 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
164     0, "");
165 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
166     0, "");
167 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
168     &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
169 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
170     &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
171 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
172     0, "Debug level for NFS server");
173 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
174     &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
175 static int nfsrv_pnfsgetdsattr = 1;
176 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
177     &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
178 
179 /*
180  * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
181  * not running.
182  * The dsN subdirectories for the increased values must have been created
183  * on all DS servers before this increase is done.
184  */
185 u_int	nfsrv_dsdirsize = 20;
186 static int
187 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
188 {
189 	int error, newdsdirsize;
190 
191 	newdsdirsize = nfsrv_dsdirsize;
192 	error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
193 	if (error != 0 || req->newptr == NULL)
194 		return (error);
195 	if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
196 	    newnfs_numnfsd != 0)
197 		return (EINVAL);
198 	nfsrv_dsdirsize = newdsdirsize;
199 	return (0);
200 }
201 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
202     CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
203     sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
204 
205 /*
206  * nfs_srvmaxio can only be increased and only when the nfsd threads are
207  * not running.  The setting must be a power of 2, with the current limit of
208  * 1Mbyte.
209  */
210 static int
211 sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)
212 {
213 	int error;
214 	u_int newsrvmaxio;
215 	uint64_t tval;
216 
217 	newsrvmaxio = nfs_srvmaxio;
218 	error = sysctl_handle_int(oidp, &newsrvmaxio, 0, req);
219 	if (error != 0 || req->newptr == NULL)
220 		return (error);
221 	if (newsrvmaxio == nfs_srvmaxio)
222 		return (0);
223 	if (newsrvmaxio < nfs_srvmaxio) {
224 		printf("nfsd: vfs.nfsd.srvmaxio can only be increased\n");
225 		return (EINVAL);
226 	}
227 	if (newsrvmaxio > 1048576) {
228 		printf("nfsd: vfs.nfsd.srvmaxio cannot be > 1Mbyte\n");
229 		return (EINVAL);
230 	}
231 	if ((newsrvmaxio & (newsrvmaxio - 1)) != 0) {
232 		printf("nfsd: vfs.nfsd.srvmaxio must be a power of 2\n");
233 		return (EINVAL);
234 	}
235 
236 	/*
237 	 * Check that kern.ipc.maxsockbuf is large enough for
238 	 * newsrviomax, given the setting of vfs.nfs.bufpackets.
239 	 */
240 	if ((newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets >
241 	    sb_max_adj) {
242 		/*
243 		 * Suggest vfs.nfs.bufpackets * maximum RPC message for
244 		 * sb_max_adj.
245 		 */
246 		tval = (newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets;
247 
248 		/*
249 		 * Convert suggested sb_max_adj value to a suggested
250 		 * sb_max value, which is what is set via kern.ipc.maxsockbuf.
251 		 * Perform the inverse calculation of (from uipc_sockbuf.c):
252 		 * sb_max_adj = (u_quad_t)sb_max * MCLBYTES /
253 		 *     (MSIZE + MCLBYTES);
254 		 * XXX If the calculation of sb_max_adj from sb_max changes,
255 		 *     this calculation must be changed as well.
256 		 */
257 		tval *= (MSIZE + MCLBYTES);  /* Brackets for readability. */
258 		tval += MCLBYTES - 1;        /* Round up divide. */
259 		tval /= MCLBYTES;
260 		printf("nfsd: set kern.ipc.maxsockbuf to a minimum of "
261 		    "%ju to support %ubyte NFS I/O\n", (uintmax_t)tval,
262 		    newsrvmaxio);
263 		return (EINVAL);
264 	}
265 
266 	NFSD_LOCK();
267 	if (newnfs_numnfsd != 0) {
268 		NFSD_UNLOCK();
269 		printf("nfsd: cannot set vfs.nfsd.srvmaxio when nfsd "
270 		    "threads are running\n");
271 		return (EINVAL);
272 	}
273 
274 
275 	nfs_srvmaxio = newsrvmaxio;
276 	NFSD_UNLOCK();
277 	return (0);
278 }
279 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, srvmaxio,
280     CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
281     sysctl_srvmaxio, "IU", "Maximum I/O size in bytes");
282 
283 #define	MAX_REORDERED_RPC	16
284 #define	NUM_HEURISTIC		1031
285 #define	NHUSE_INIT		64
286 #define	NHUSE_INC		16
287 #define	NHUSE_MAX		2048
288 
289 static struct nfsheur {
290 	struct vnode *nh_vp;	/* vp to match (unreferenced pointer) */
291 	off_t nh_nextoff;	/* next offset for sequential detection */
292 	int nh_use;		/* use count for selection */
293 	int nh_seqcount;	/* heuristic */
294 } nfsheur[NUM_HEURISTIC];
295 
296 /*
297  * Heuristic to detect sequential operation.
298  */
299 static struct nfsheur *
300 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
301 {
302 	struct nfsheur *nh;
303 	int hi, try;
304 
305 	/* Locate best candidate. */
306 	try = 32;
307 	hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
308 	nh = &nfsheur[hi];
309 	while (try--) {
310 		if (nfsheur[hi].nh_vp == vp) {
311 			nh = &nfsheur[hi];
312 			break;
313 		}
314 		if (nfsheur[hi].nh_use > 0)
315 			--nfsheur[hi].nh_use;
316 		hi = (hi + 1) % NUM_HEURISTIC;
317 		if (nfsheur[hi].nh_use < nh->nh_use)
318 			nh = &nfsheur[hi];
319 	}
320 
321 	/* Initialize hint if this is a new file. */
322 	if (nh->nh_vp != vp) {
323 		nh->nh_vp = vp;
324 		nh->nh_nextoff = uio->uio_offset;
325 		nh->nh_use = NHUSE_INIT;
326 		if (uio->uio_offset == 0)
327 			nh->nh_seqcount = 4;
328 		else
329 			nh->nh_seqcount = 1;
330 	}
331 
332 	/* Calculate heuristic. */
333 	if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
334 	    uio->uio_offset == nh->nh_nextoff) {
335 		/* See comments in vfs_vnops.c:sequential_heuristic(). */
336 		nh->nh_seqcount += howmany(uio->uio_resid, 16384);
337 		if (nh->nh_seqcount > IO_SEQMAX)
338 			nh->nh_seqcount = IO_SEQMAX;
339 	} else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
340 	    imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
341 		/* Probably a reordered RPC, leave seqcount alone. */
342 	} else if (nh->nh_seqcount > 1) {
343 		nh->nh_seqcount /= 2;
344 	} else {
345 		nh->nh_seqcount = 0;
346 	}
347 	nh->nh_use += NHUSE_INC;
348 	if (nh->nh_use > NHUSE_MAX)
349 		nh->nh_use = NHUSE_MAX;
350 	return (nh);
351 }
352 
353 /*
354  * Get attributes into nfsvattr structure.
355  */
356 int
357 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
358     struct nfsrv_descript *nd, struct thread *p, int vpislocked,
359     nfsattrbit_t *attrbitp)
360 {
361 	int error, gotattr, lockedit = 0;
362 	struct nfsvattr na;
363 
364 	if (vpislocked == 0) {
365 		/*
366 		 * When vpislocked == 0, the vnode is either exclusively
367 		 * locked by this thread or not locked by this thread.
368 		 * As such, shared lock it, if not exclusively locked.
369 		 */
370 		if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
371 			lockedit = 1;
372 			NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
373 		}
374 	}
375 
376 	/*
377 	 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
378 	 * attributes, as required.
379 	 * This needs to be done for regular files if:
380 	 * - non-NFSv4 RPCs or
381 	 * - when attrbitp == NULL or
382 	 * - an NFSv4 RPC with any of the above attributes in attrbitp.
383 	 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
384 	 * these attributes.  nfsrv_proxyds() will return an error if the
385 	 * server is not a pNFS one.
386 	 */
387 	gotattr = 0;
388 	if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
389 	    (nd->nd_flag & ND_NFSV4) == 0 ||
390 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
391 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
392 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
393 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
394 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
395 		error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
396 		    NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
397 		    NULL);
398 		if (error == 0)
399 			gotattr = 1;
400 	}
401 
402 	error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
403 	if (lockedit != 0)
404 		NFSVOPUNLOCK(vp);
405 
406 	/*
407 	 * If we got the Change, Size and Modify Time from the DS,
408 	 * replace them.
409 	 */
410 	if (gotattr != 0) {
411 		nvap->na_atime = na.na_atime;
412 		nvap->na_mtime = na.na_mtime;
413 		nvap->na_filerev = na.na_filerev;
414 		nvap->na_size = na.na_size;
415 		nvap->na_bytes = na.na_bytes;
416 	}
417 	NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
418 	    error, (uintmax_t)na.na_filerev);
419 
420 	NFSEXITCODE(error);
421 	return (error);
422 }
423 
424 /*
425  * Get a file handle for a vnode.
426  */
427 int
428 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
429 {
430 	int error;
431 
432 	NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
433 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
434 	error = VOP_VPTOFH(vp, &fhp->fh_fid);
435 
436 	NFSEXITCODE(error);
437 	return (error);
438 }
439 
440 /*
441  * Perform access checking for vnodes obtained from file handles that would
442  * refer to files already opened by a Unix client. You cannot just use
443  * vn_writechk() and VOP_ACCESSX() for two reasons.
444  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
445  *     case.
446  * 2 - The owner is to be given access irrespective of mode bits for some
447  *     operations, so that processes that chmod after opening a file don't
448  *     break.
449  */
450 int
451 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
452     struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
453     u_int32_t *supportedtypep)
454 {
455 	struct vattr vattr;
456 	int error = 0, getret = 0;
457 
458 	if (vpislocked == 0) {
459 		if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
460 			error = EPERM;
461 			goto out;
462 		}
463 	}
464 	if (accmode & VWRITE) {
465 		/* Just vn_writechk() changed to check rdonly */
466 		/*
467 		 * Disallow write attempts on read-only file systems;
468 		 * unless the file is a socket or a block or character
469 		 * device resident on the file system.
470 		 */
471 		if (NFSVNO_EXRDONLY(exp) ||
472 		    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
473 			switch (vp->v_type) {
474 			case VREG:
475 			case VDIR:
476 			case VLNK:
477 				error = EROFS;
478 			default:
479 				break;
480 			}
481 		}
482 		/*
483 		 * If there's shared text associated with
484 		 * the inode, try to free it up once.  If
485 		 * we fail, we can't allow writing.
486 		 */
487 		if (VOP_IS_TEXT(vp) && error == 0)
488 			error = ETXTBSY;
489 	}
490 	if (error != 0) {
491 		if (vpislocked == 0)
492 			NFSVOPUNLOCK(vp);
493 		goto out;
494 	}
495 
496 	/*
497 	 * Should the override still be applied when ACLs are enabled?
498 	 */
499 	error = VOP_ACCESSX(vp, accmode, cred, p);
500 	if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
501 		/*
502 		 * Try again with VEXPLICIT_DENY, to see if the test for
503 		 * deletion is supported.
504 		 */
505 		error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
506 		if (error == 0) {
507 			if (vp->v_type == VDIR) {
508 				accmode &= ~(VDELETE | VDELETE_CHILD);
509 				accmode |= VWRITE;
510 				error = VOP_ACCESSX(vp, accmode, cred, p);
511 			} else if (supportedtypep != NULL) {
512 				*supportedtypep &= ~NFSACCESS_DELETE;
513 			}
514 		}
515 	}
516 
517 	/*
518 	 * Allow certain operations for the owner (reads and writes
519 	 * on files that are already open).
520 	 */
521 	if (override != NFSACCCHK_NOOVERRIDE &&
522 	    (error == EPERM || error == EACCES)) {
523 		if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
524 			error = 0;
525 		else if (override & NFSACCCHK_ALLOWOWNER) {
526 			getret = VOP_GETATTR(vp, &vattr, cred);
527 			if (getret == 0 && cred->cr_uid == vattr.va_uid)
528 				error = 0;
529 		}
530 	}
531 	if (vpislocked == 0)
532 		NFSVOPUNLOCK(vp);
533 
534 out:
535 	NFSEXITCODE(error);
536 	return (error);
537 }
538 
539 /*
540  * Set attribute(s) vnop.
541  */
542 int
543 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
544     struct thread *p, struct nfsexstuff *exp)
545 {
546 	u_quad_t savsize = 0;
547 	int error, savedit;
548 	time_t savbtime;
549 
550 	/*
551 	 * If this is an exported file system and a pNFS service is running,
552 	 * don't VOP_SETATTR() of size for the MDS file system.
553 	 */
554 	savedit = 0;
555 	error = 0;
556 	if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
557 	    nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
558 	    nvap->na_vattr.va_size > 0) {
559 		savsize = nvap->na_vattr.va_size;
560 		nvap->na_vattr.va_size = VNOVAL;
561 		if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
562 		    nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
563 		    nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
564 		    nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
565 		    nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
566 			savedit = 1;
567 		else
568 			savedit = 2;
569 	}
570 	if (savedit != 2)
571 		error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
572 	if (savedit != 0)
573 		nvap->na_vattr.va_size = savsize;
574 	if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
575 	    nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
576 	    nvap->na_vattr.va_size != VNOVAL ||
577 	    nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
578 	    nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
579 	    nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
580 		/* Never modify birthtime on a DS file. */
581 		savbtime = nvap->na_vattr.va_birthtime.tv_sec;
582 		nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
583 		/* For a pNFS server, set the attributes on the DS file. */
584 		error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
585 		    NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
586 		nvap->na_vattr.va_birthtime.tv_sec = savbtime;
587 		if (error == ENOENT)
588 			error = 0;
589 	}
590 	NFSEXITCODE(error);
591 	return (error);
592 }
593 
594 /*
595  * Set up nameidata for a lookup() call and do it.
596  */
597 int
598 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
599     struct vnode *dp, int islocked, struct nfsexstuff *exp,
600     struct vnode **retdirp)
601 {
602 	struct componentname *cnp = &ndp->ni_cnd;
603 	int i;
604 	struct iovec aiov;
605 	struct uio auio;
606 	int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
607 	int error = 0;
608 	char *cp;
609 
610 	*retdirp = NULL;
611 	cnp->cn_nameptr = cnp->cn_pnbuf;
612 	ndp->ni_lcf = 0;
613 	/*
614 	 * Extract and set starting directory.
615 	 */
616 	if (dp->v_type != VDIR) {
617 		if (islocked)
618 			vput(dp);
619 		else
620 			vrele(dp);
621 		nfsvno_relpathbuf(ndp);
622 		error = ENOTDIR;
623 		goto out1;
624 	}
625 	if (islocked)
626 		NFSVOPUNLOCK(dp);
627 	VREF(dp);
628 	*retdirp = dp;
629 	if (NFSVNO_EXRDONLY(exp))
630 		cnp->cn_flags |= RDONLY;
631 	ndp->ni_segflg = UIO_SYSSPACE;
632 
633 	if (nd->nd_flag & ND_PUBLOOKUP) {
634 		ndp->ni_loopcnt = 0;
635 		if (cnp->cn_pnbuf[0] == '/') {
636 			vrele(dp);
637 			/*
638 			 * Check for degenerate pathnames here, since lookup()
639 			 * panics on them.
640 			 */
641 			for (i = 1; i < ndp->ni_pathlen; i++)
642 				if (cnp->cn_pnbuf[i] != '/')
643 					break;
644 			if (i == ndp->ni_pathlen) {
645 				error = NFSERR_ACCES;
646 				goto out;
647 			}
648 			dp = rootvnode;
649 			VREF(dp);
650 		}
651 	} else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
652 	    (nd->nd_flag & ND_NFSV4) == 0) {
653 		/*
654 		 * Only cross mount points for NFSv4 when doing a
655 		 * mount while traversing the file system above
656 		 * the mount point, unless nfsrv_enable_crossmntpt is set.
657 		 */
658 		cnp->cn_flags |= NOCROSSMOUNT;
659 	}
660 
661 	/*
662 	 * Initialize for scan, set ni_startdir and bump ref on dp again
663 	 * because lookup() will dereference ni_startdir.
664 	 */
665 
666 	ndp->ni_startdir = dp;
667 	ndp->ni_rootdir = rootvnode;
668 	ndp->ni_topdir = NULL;
669 
670 	if (!lockleaf)
671 		cnp->cn_flags |= LOCKLEAF;
672 	for (;;) {
673 		cnp->cn_nameptr = cnp->cn_pnbuf;
674 		/*
675 		 * Call lookup() to do the real work.  If an error occurs,
676 		 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
677 		 * we do not have to dereference anything before returning.
678 		 * In either case ni_startdir will be dereferenced and NULLed
679 		 * out.
680 		 */
681 		error = vfs_lookup(ndp);
682 		if (error)
683 			break;
684 
685 		/*
686 		 * Check for encountering a symbolic link.  Trivial
687 		 * termination occurs if no symlink encountered.
688 		 */
689 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
690 			if (ndp->ni_vp && !lockleaf)
691 				NFSVOPUNLOCK(ndp->ni_vp);
692 			break;
693 		}
694 
695 		/*
696 		 * Validate symlink
697 		 */
698 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
699 			NFSVOPUNLOCK(ndp->ni_dvp);
700 		if (!(nd->nd_flag & ND_PUBLOOKUP)) {
701 			error = EINVAL;
702 			goto badlink2;
703 		}
704 
705 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
706 			error = ELOOP;
707 			goto badlink2;
708 		}
709 		if (ndp->ni_pathlen > 1)
710 			cp = uma_zalloc(namei_zone, M_WAITOK);
711 		else
712 			cp = cnp->cn_pnbuf;
713 		aiov.iov_base = cp;
714 		aiov.iov_len = MAXPATHLEN;
715 		auio.uio_iov = &aiov;
716 		auio.uio_iovcnt = 1;
717 		auio.uio_offset = 0;
718 		auio.uio_rw = UIO_READ;
719 		auio.uio_segflg = UIO_SYSSPACE;
720 		auio.uio_td = NULL;
721 		auio.uio_resid = MAXPATHLEN;
722 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
723 		if (error) {
724 		badlink1:
725 			if (ndp->ni_pathlen > 1)
726 				uma_zfree(namei_zone, cp);
727 		badlink2:
728 			vrele(ndp->ni_dvp);
729 			vput(ndp->ni_vp);
730 			break;
731 		}
732 		linklen = MAXPATHLEN - auio.uio_resid;
733 		if (linklen == 0) {
734 			error = ENOENT;
735 			goto badlink1;
736 		}
737 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
738 			error = ENAMETOOLONG;
739 			goto badlink1;
740 		}
741 
742 		/*
743 		 * Adjust or replace path
744 		 */
745 		if (ndp->ni_pathlen > 1) {
746 			NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
747 			uma_zfree(namei_zone, cnp->cn_pnbuf);
748 			cnp->cn_pnbuf = cp;
749 		} else
750 			cnp->cn_pnbuf[linklen] = '\0';
751 		ndp->ni_pathlen += linklen;
752 
753 		/*
754 		 * Cleanup refs for next loop and check if root directory
755 		 * should replace current directory.  Normally ni_dvp
756 		 * becomes the new base directory and is cleaned up when
757 		 * we loop.  Explicitly null pointers after invalidation
758 		 * to clarify operation.
759 		 */
760 		vput(ndp->ni_vp);
761 		ndp->ni_vp = NULL;
762 
763 		if (cnp->cn_pnbuf[0] == '/') {
764 			vrele(ndp->ni_dvp);
765 			ndp->ni_dvp = ndp->ni_rootdir;
766 			VREF(ndp->ni_dvp);
767 		}
768 		ndp->ni_startdir = ndp->ni_dvp;
769 		ndp->ni_dvp = NULL;
770 	}
771 	if (!lockleaf)
772 		cnp->cn_flags &= ~LOCKLEAF;
773 
774 out:
775 	if (error) {
776 		nfsvno_relpathbuf(ndp);
777 		ndp->ni_vp = NULL;
778 		ndp->ni_dvp = NULL;
779 		ndp->ni_startdir = NULL;
780 	} else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
781 		ndp->ni_dvp = NULL;
782 	}
783 
784 out1:
785 	NFSEXITCODE2(error, nd);
786 	return (error);
787 }
788 
789 /*
790  * Set up a pathname buffer and return a pointer to it and, optionally
791  * set a hash pointer.
792  */
793 void
794 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
795 {
796 	struct componentname *cnp = &ndp->ni_cnd;
797 
798 	cnp->cn_flags |= (NOMACCHECK);
799 	cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
800 	if (hashpp != NULL)
801 		*hashpp = NULL;
802 	*bufpp = cnp->cn_pnbuf;
803 }
804 
805 /*
806  * Release the above path buffer, if not released by nfsvno_namei().
807  */
808 void
809 nfsvno_relpathbuf(struct nameidata *ndp)
810 {
811 
812 	uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
813 	ndp->ni_cnd.cn_pnbuf = NULL;
814 }
815 
816 /*
817  * Readlink vnode op into an mbuf list.
818  */
819 int
820 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
821     struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
822 {
823 	struct iovec *iv;
824 	struct uio io, *uiop = &io;
825 	struct mbuf *mp, *mp3;
826 	int len, tlen, error = 0;
827 
828 	len = NFS_MAXPATHLEN;
829 	if (maxextsiz > 0)
830 		uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
831 		    &mp3, &mp, &iv);
832 	else
833 		uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
834 	uiop->uio_iov = iv;
835 	uiop->uio_offset = 0;
836 	uiop->uio_resid = len;
837 	uiop->uio_rw = UIO_READ;
838 	uiop->uio_segflg = UIO_SYSSPACE;
839 	uiop->uio_td = NULL;
840 	error = VOP_READLINK(vp, uiop, cred);
841 	free(iv, M_TEMP);
842 	if (error) {
843 		m_freem(mp3);
844 		*lenp = 0;
845 		goto out;
846 	}
847 	if (uiop->uio_resid > 0) {
848 		len -= uiop->uio_resid;
849 		tlen = NFSM_RNDUP(len);
850 		if (tlen == 0) {
851 			m_freem(mp3);
852 			mp3 = mp = NULL;
853 		} else if (tlen != NFS_MAXPATHLEN || tlen != len)
854 			mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
855 			    tlen - len);
856 	}
857 	*lenp = len;
858 	*mpp = mp3;
859 	*mpendp = mp;
860 
861 out:
862 	NFSEXITCODE(error);
863 	return (error);
864 }
865 
866 /*
867  * Create an mbuf chain and an associated iovec that can be used to Read
868  * or Getextattr of data.
869  * Upon success, return pointers to the first and last mbufs in the chain
870  * plus the malloc'd iovec and its iovlen.
871  */
872 static int
873 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
874     struct iovec **ivp)
875 {
876 	struct mbuf *m, *m2 = NULL, *m3;
877 	struct iovec *iv;
878 	int i, left, siz;
879 
880 	left = len;
881 	m3 = NULL;
882 	/*
883 	 * Generate the mbuf list with the uio_iov ref. to it.
884 	 */
885 	i = 0;
886 	while (left > 0) {
887 		NFSMGET(m);
888 		MCLGET(m, M_WAITOK);
889 		m->m_len = 0;
890 		siz = min(M_TRAILINGSPACE(m), left);
891 		left -= siz;
892 		i++;
893 		if (m3)
894 			m2->m_next = m;
895 		else
896 			m3 = m;
897 		m2 = m;
898 	}
899 	*ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
900 	m = m3;
901 	left = len;
902 	i = 0;
903 	while (left > 0) {
904 		if (m == NULL)
905 			panic("nfsrv_createiovec iov");
906 		siz = min(M_TRAILINGSPACE(m), left);
907 		if (siz > 0) {
908 			iv->iov_base = mtod(m, caddr_t) + m->m_len;
909 			iv->iov_len = siz;
910 			m->m_len += siz;
911 			left -= siz;
912 			iv++;
913 			i++;
914 		}
915 		m = m->m_next;
916 	}
917 	*mpp = m3;
918 	*mpendp = m2;
919 	return (i);
920 }
921 
922 /*
923  * Create an mbuf chain and an associated iovec that can be used to Read
924  * or Getextattr of data.
925  * Upon success, return pointers to the first and last mbufs in the chain
926  * plus the malloc'd iovec and its iovlen.
927  * Same as above, but creates ext_pgs mbuf(s).
928  */
929 static int
930 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
931     struct mbuf **mpendp, struct iovec **ivp)
932 {
933 	struct mbuf *m, *m2 = NULL, *m3;
934 	struct iovec *iv;
935 	int i, left, pgno, siz;
936 
937 	left = len;
938 	m3 = NULL;
939 	/*
940 	 * Generate the mbuf list with the uio_iov ref. to it.
941 	 */
942 	i = 0;
943 	while (left > 0) {
944 		siz = min(left, maxextsiz);
945 		m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
946 		left -= siz;
947 		i += m->m_epg_npgs;
948 		if (m3 != NULL)
949 			m2->m_next = m;
950 		else
951 			m3 = m;
952 		m2 = m;
953 	}
954 	*ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
955 	m = m3;
956 	left = len;
957 	i = 0;
958 	pgno = 0;
959 	while (left > 0) {
960 		if (m == NULL)
961 			panic("nfsvno_createiovec_extpgs iov");
962 		siz = min(PAGE_SIZE, left);
963 		if (siz > 0) {
964 			iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
965 			iv->iov_len = siz;
966 			m->m_len += siz;
967 			if (pgno == m->m_epg_npgs - 1)
968 				m->m_epg_last_len = siz;
969 			left -= siz;
970 			iv++;
971 			i++;
972 			pgno++;
973 		}
974 		if (pgno == m->m_epg_npgs && left > 0) {
975 			m = m->m_next;
976 			if (m == NULL)
977 				panic("nfsvno_createiovec_extpgs iov");
978 			pgno = 0;
979 		}
980 	}
981 	*mpp = m3;
982 	*mpendp = m2;
983 	return (i);
984 }
985 
986 /*
987  * Read vnode op call into mbuf list.
988  */
989 int
990 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
991     int maxextsiz, struct thread *p, struct mbuf **mpp,
992     struct mbuf **mpendp)
993 {
994 	struct mbuf *m;
995 	struct iovec *iv;
996 	int error = 0, len, tlen, ioflag = 0;
997 	struct mbuf *m3;
998 	struct uio io, *uiop = &io;
999 	struct nfsheur *nh;
1000 
1001 	/*
1002 	 * Attempt to read from a DS file. A return of ENOENT implies
1003 	 * there is no DS file to read.
1004 	 */
1005 	error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
1006 	    NULL, mpendp, NULL, NULL, NULL, 0, NULL);
1007 	if (error != ENOENT)
1008 		return (error);
1009 
1010 	len = NFSM_RNDUP(cnt);
1011 	if (maxextsiz > 0)
1012 		uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
1013 		    &m3, &m, &iv);
1014 	else
1015 		uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
1016 	uiop->uio_iov = iv;
1017 	uiop->uio_offset = off;
1018 	uiop->uio_resid = len;
1019 	uiop->uio_rw = UIO_READ;
1020 	uiop->uio_segflg = UIO_SYSSPACE;
1021 	uiop->uio_td = NULL;
1022 	nh = nfsrv_sequential_heuristic(uiop, vp);
1023 	ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
1024 	/* XXX KDM make this more systematic? */
1025 	nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
1026 	error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
1027 	free(iv, M_TEMP);
1028 	if (error) {
1029 		m_freem(m3);
1030 		*mpp = NULL;
1031 		goto out;
1032 	}
1033 	nh->nh_nextoff = uiop->uio_offset;
1034 	tlen = len - uiop->uio_resid;
1035 	cnt = cnt < tlen ? cnt : tlen;
1036 	tlen = NFSM_RNDUP(cnt);
1037 	if (tlen == 0) {
1038 		m_freem(m3);
1039 		m3 = m = NULL;
1040 	} else if (len != tlen || tlen != cnt)
1041 		m = nfsrv_adj(m3, len - tlen, tlen - cnt);
1042 	*mpp = m3;
1043 	*mpendp = m;
1044 
1045 out:
1046 	NFSEXITCODE(error);
1047 	return (error);
1048 }
1049 
1050 /*
1051  * Create the iovec for the mbuf chain passed in as an argument.
1052  * The "cp" argument is where the data starts within the first mbuf in
1053  * the chain. It returns the iovec and the iovcnt.
1054  */
1055 static int
1056 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
1057     int *iovcntp)
1058 {
1059 	struct mbuf *mp;
1060 	struct iovec *ivp;
1061 	int cnt, i, len;
1062 
1063 	/*
1064 	 * Loop through the mbuf chain, counting how many mbufs are a
1065 	 * part of this write operation, so the iovec size is known.
1066 	 */
1067 	cnt = 0;
1068 	len = retlen;
1069 	mp = m;
1070 	i = mtod(mp, caddr_t) + mp->m_len - cp;
1071 	while (len > 0) {
1072 		if (i > 0) {
1073 			len -= i;
1074 			cnt++;
1075 		}
1076 		mp = mp->m_next;
1077 		if (!mp) {
1078 			if (len > 0)
1079 				return (EBADRPC);
1080 		} else
1081 			i = mp->m_len;
1082 	}
1083 
1084 	/* Now, create the iovec. */
1085 	mp = m;
1086 	*ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1087 	    M_WAITOK);
1088 	*iovcntp = cnt;
1089 	i = mtod(mp, caddr_t) + mp->m_len - cp;
1090 	len = retlen;
1091 	while (len > 0) {
1092 		if (mp == NULL)
1093 			panic("nfsrv_createiovecw");
1094 		if (i > 0) {
1095 			i = min(i, len);
1096 			ivp->iov_base = cp;
1097 			ivp->iov_len = i;
1098 			ivp++;
1099 			len -= i;
1100 		}
1101 		mp = mp->m_next;
1102 		if (mp) {
1103 			i = mp->m_len;
1104 			cp = mtod(mp, caddr_t);
1105 		}
1106 	}
1107 	return (0);
1108 }
1109 
1110 /*
1111  * Write vnode op from an mbuf list.
1112  */
1113 int
1114 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1115     struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1116 {
1117 	struct iovec *iv;
1118 	int cnt, ioflags, error;
1119 	struct uio io, *uiop = &io;
1120 	struct nfsheur *nh;
1121 
1122 	/*
1123 	 * Attempt to write to a DS file. A return of ENOENT implies
1124 	 * there is no DS file to write.
1125 	 */
1126 	error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1127 	    &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1128 	if (error != ENOENT) {
1129 		*stable = NFSWRITE_FILESYNC;
1130 		return (error);
1131 	}
1132 
1133 	if (*stable == NFSWRITE_UNSTABLE)
1134 		ioflags = IO_NODELOCKED;
1135 	else
1136 		ioflags = (IO_SYNC | IO_NODELOCKED);
1137 	error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1138 	if (error != 0)
1139 		return (error);
1140 	uiop->uio_iov = iv;
1141 	uiop->uio_iovcnt = cnt;
1142 	uiop->uio_resid = retlen;
1143 	uiop->uio_rw = UIO_WRITE;
1144 	uiop->uio_segflg = UIO_SYSSPACE;
1145 	NFSUIOPROC(uiop, p);
1146 	uiop->uio_offset = off;
1147 	nh = nfsrv_sequential_heuristic(uiop, vp);
1148 	ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1149 	/* XXX KDM make this more systematic? */
1150 	nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1151 	error = VOP_WRITE(vp, uiop, ioflags, cred);
1152 	if (error == 0)
1153 		nh->nh_nextoff = uiop->uio_offset;
1154 	free(iv, M_TEMP);
1155 
1156 	NFSEXITCODE(error);
1157 	return (error);
1158 }
1159 
1160 /*
1161  * Common code for creating a regular file (plus special files for V2).
1162  */
1163 int
1164 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1165     struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1166     int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1167 {
1168 	u_quad_t tempsize;
1169 	int error;
1170 	struct thread *p = curthread;
1171 
1172 	error = nd->nd_repstat;
1173 	if (!error && ndp->ni_vp == NULL) {
1174 		if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1175 			error = VOP_CREATE(ndp->ni_dvp,
1176 			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1177 			/* For a pNFS server, create the data file on a DS. */
1178 			if (error == 0 && nvap->na_type == VREG) {
1179 				/*
1180 				 * Create a data file on a DS for a pNFS server.
1181 				 * This function just returns if not
1182 				 * running a pNFS DS or the creation fails.
1183 				 */
1184 				nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1185 				    nd->nd_cred, p);
1186 			}
1187 			VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1188 			    NULL, false);
1189 			nfsvno_relpathbuf(ndp);
1190 			if (!error) {
1191 				if (*exclusive_flagp) {
1192 					*exclusive_flagp = 0;
1193 					NFSVNO_ATTRINIT(nvap);
1194 					nvap->na_atime.tv_sec = cverf[0];
1195 					nvap->na_atime.tv_nsec = cverf[1];
1196 					error = VOP_SETATTR(ndp->ni_vp,
1197 					    &nvap->na_vattr, nd->nd_cred);
1198 					if (error != 0) {
1199 						vput(ndp->ni_vp);
1200 						ndp->ni_vp = NULL;
1201 						error = NFSERR_NOTSUPP;
1202 					}
1203 				}
1204 			}
1205 		/*
1206 		 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1207 		 * (This implies, just get out on an error.)
1208 		 */
1209 		} else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1210 			nvap->na_type == VFIFO) {
1211 			if (nvap->na_type == VCHR && rdev == 0xffffffff)
1212 				nvap->na_type = VFIFO;
1213                         if (nvap->na_type != VFIFO &&
1214 			    (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1215 				nfsvno_relpathbuf(ndp);
1216 				vput(ndp->ni_dvp);
1217 				goto out;
1218 			}
1219 			nvap->na_rdev = rdev;
1220 			error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1221 			    &ndp->ni_cnd, &nvap->na_vattr);
1222 			VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1223 			    NULL, false);
1224 			nfsvno_relpathbuf(ndp);
1225 			if (error)
1226 				goto out;
1227 		} else {
1228 			nfsvno_relpathbuf(ndp);
1229 			vput(ndp->ni_dvp);
1230 			error = ENXIO;
1231 			goto out;
1232 		}
1233 		*vpp = ndp->ni_vp;
1234 	} else {
1235 		/*
1236 		 * Handle cases where error is already set and/or
1237 		 * the file exists.
1238 		 * 1 - clean up the lookup
1239 		 * 2 - iff !error and na_size set, truncate it
1240 		 */
1241 		nfsvno_relpathbuf(ndp);
1242 		*vpp = ndp->ni_vp;
1243 		if (ndp->ni_dvp == *vpp)
1244 			vrele(ndp->ni_dvp);
1245 		else
1246 			vput(ndp->ni_dvp);
1247 		if (!error && nvap->na_size != VNOVAL) {
1248 			error = nfsvno_accchk(*vpp, VWRITE,
1249 			    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1250 			    NFSACCCHK_VPISLOCKED, NULL);
1251 			if (!error) {
1252 				tempsize = nvap->na_size;
1253 				NFSVNO_ATTRINIT(nvap);
1254 				nvap->na_size = tempsize;
1255 				error = nfsvno_setattr(*vpp, nvap,
1256 				    nd->nd_cred, p, exp);
1257 			}
1258 		}
1259 		if (error)
1260 			vput(*vpp);
1261 	}
1262 
1263 out:
1264 	NFSEXITCODE(error);
1265 	return (error);
1266 }
1267 
1268 /*
1269  * Do a mknod vnode op.
1270  */
1271 int
1272 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1273     struct thread *p)
1274 {
1275 	int error = 0;
1276 	enum vtype vtyp;
1277 
1278 	vtyp = nvap->na_type;
1279 	/*
1280 	 * Iff doesn't exist, create it.
1281 	 */
1282 	if (ndp->ni_vp) {
1283 		nfsvno_relpathbuf(ndp);
1284 		vput(ndp->ni_dvp);
1285 		vrele(ndp->ni_vp);
1286 		error = EEXIST;
1287 		goto out;
1288 	}
1289 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1290 		nfsvno_relpathbuf(ndp);
1291 		vput(ndp->ni_dvp);
1292 		error = NFSERR_BADTYPE;
1293 		goto out;
1294 	}
1295 	if (vtyp == VSOCK) {
1296 		error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1297 		    &ndp->ni_cnd, &nvap->na_vattr);
1298 		VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1299 		    false);
1300 		nfsvno_relpathbuf(ndp);
1301 	} else {
1302 		if (nvap->na_type != VFIFO &&
1303 		    (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1304 			nfsvno_relpathbuf(ndp);
1305 			vput(ndp->ni_dvp);
1306 			goto out;
1307 		}
1308 		error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1309 		    &ndp->ni_cnd, &nvap->na_vattr);
1310 		VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1311 		    false);
1312 		nfsvno_relpathbuf(ndp);
1313 		/*
1314 		 * Since VOP_MKNOD returns the ni_vp, I can't
1315 		 * see any reason to do the lookup.
1316 		 */
1317 	}
1318 
1319 out:
1320 	NFSEXITCODE(error);
1321 	return (error);
1322 }
1323 
1324 /*
1325  * Mkdir vnode op.
1326  */
1327 int
1328 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1329     struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1330 {
1331 	int error = 0;
1332 
1333 	if (ndp->ni_vp != NULL) {
1334 		if (ndp->ni_dvp == ndp->ni_vp)
1335 			vrele(ndp->ni_dvp);
1336 		else
1337 			vput(ndp->ni_dvp);
1338 		vrele(ndp->ni_vp);
1339 		nfsvno_relpathbuf(ndp);
1340 		error = EEXIST;
1341 		goto out;
1342 	}
1343 	error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1344 	    &nvap->na_vattr);
1345 	VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL, false);
1346 	nfsvno_relpathbuf(ndp);
1347 
1348 out:
1349 	NFSEXITCODE(error);
1350 	return (error);
1351 }
1352 
1353 /*
1354  * symlink vnode op.
1355  */
1356 int
1357 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1358     int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1359     struct nfsexstuff *exp)
1360 {
1361 	int error = 0;
1362 
1363 	if (ndp->ni_vp) {
1364 		nfsvno_relpathbuf(ndp);
1365 		if (ndp->ni_dvp == ndp->ni_vp)
1366 			vrele(ndp->ni_dvp);
1367 		else
1368 			vput(ndp->ni_dvp);
1369 		vrele(ndp->ni_vp);
1370 		error = EEXIST;
1371 		goto out;
1372 	}
1373 
1374 	error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1375 	    &nvap->na_vattr, pathcp);
1376 	/*
1377 	 * Although FreeBSD still had the lookup code in
1378 	 * it for 7/current, there doesn't seem to be any
1379 	 * point, since VOP_SYMLINK() returns the ni_vp.
1380 	 * Just vput it for v2.
1381 	 */
1382 	VOP_VPUT_PAIR(ndp->ni_dvp, &ndp->ni_vp, !not_v2 && error == 0);
1383 	nfsvno_relpathbuf(ndp);
1384 
1385 out:
1386 	NFSEXITCODE(error);
1387 	return (error);
1388 }
1389 
1390 /*
1391  * Parse symbolic link arguments.
1392  * This function has an ugly side effect. It will malloc() an area for
1393  * the symlink and set iov_base to point to it, only if it succeeds.
1394  * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1395  * be FREE'd later.
1396  */
1397 int
1398 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1399     struct thread *p, char **pathcpp, int *lenp)
1400 {
1401 	u_int32_t *tl;
1402 	char *pathcp = NULL;
1403 	int error = 0, len;
1404 	struct nfsv2_sattr *sp;
1405 
1406 	*pathcpp = NULL;
1407 	*lenp = 0;
1408 	if ((nd->nd_flag & ND_NFSV3) &&
1409 	    (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1410 		goto nfsmout;
1411 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1412 	len = fxdr_unsigned(int, *tl);
1413 	if (len > NFS_MAXPATHLEN || len <= 0) {
1414 		error = EBADRPC;
1415 		goto nfsmout;
1416 	}
1417 	pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1418 	error = nfsrv_mtostr(nd, pathcp, len);
1419 	if (error)
1420 		goto nfsmout;
1421 	if (nd->nd_flag & ND_NFSV2) {
1422 		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1423 		nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1424 	}
1425 	*pathcpp = pathcp;
1426 	*lenp = len;
1427 	NFSEXITCODE2(0, nd);
1428 	return (0);
1429 nfsmout:
1430 	if (pathcp)
1431 		free(pathcp, M_TEMP);
1432 	NFSEXITCODE2(error, nd);
1433 	return (error);
1434 }
1435 
1436 /*
1437  * Remove a non-directory object.
1438  */
1439 int
1440 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1441     struct thread *p, struct nfsexstuff *exp)
1442 {
1443 	struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1444 	int error = 0, mirrorcnt;
1445 	char fname[PNFS_FILENAME_LEN + 1];
1446 	fhandle_t fh;
1447 
1448 	vp = ndp->ni_vp;
1449 	dsdvp[0] = NULL;
1450 	if (vp->v_type == VDIR)
1451 		error = NFSERR_ISDIR;
1452 	else if (is_v4)
1453 		error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1454 		    p);
1455 	if (error == 0)
1456 		nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1457 	if (!error)
1458 		error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1459 	if (error == 0 && dsdvp[0] != NULL)
1460 		nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1461 	if (ndp->ni_dvp == vp)
1462 		vrele(ndp->ni_dvp);
1463 	else
1464 		vput(ndp->ni_dvp);
1465 	vput(vp);
1466 	nfsvno_relpathbuf(ndp);
1467 	NFSEXITCODE(error);
1468 	return (error);
1469 }
1470 
1471 /*
1472  * Remove a directory.
1473  */
1474 int
1475 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1476     struct thread *p, struct nfsexstuff *exp)
1477 {
1478 	struct vnode *vp;
1479 	int error = 0;
1480 
1481 	vp = ndp->ni_vp;
1482 	if (vp->v_type != VDIR) {
1483 		error = ENOTDIR;
1484 		goto out;
1485 	}
1486 	/*
1487 	 * No rmdir "." please.
1488 	 */
1489 	if (ndp->ni_dvp == vp) {
1490 		error = EINVAL;
1491 		goto out;
1492 	}
1493 	/*
1494 	 * The root of a mounted filesystem cannot be deleted.
1495 	 */
1496 	if (vp->v_vflag & VV_ROOT)
1497 		error = EBUSY;
1498 out:
1499 	if (!error)
1500 		error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1501 	if (ndp->ni_dvp == vp)
1502 		vrele(ndp->ni_dvp);
1503 	else
1504 		vput(ndp->ni_dvp);
1505 	vput(vp);
1506 	nfsvno_relpathbuf(ndp);
1507 	NFSEXITCODE(error);
1508 	return (error);
1509 }
1510 
1511 /*
1512  * Rename vnode op.
1513  */
1514 int
1515 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1516     u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1517 {
1518 	struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1519 	int error = 0, mirrorcnt;
1520 	char fname[PNFS_FILENAME_LEN + 1];
1521 	fhandle_t fh;
1522 
1523 	dsdvp[0] = NULL;
1524 	fvp = fromndp->ni_vp;
1525 	if (ndstat) {
1526 		vrele(fromndp->ni_dvp);
1527 		vrele(fvp);
1528 		error = ndstat;
1529 		goto out1;
1530 	}
1531 	tdvp = tondp->ni_dvp;
1532 	tvp = tondp->ni_vp;
1533 	if (tvp != NULL) {
1534 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1535 			error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1536 			goto out;
1537 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1538 			error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1539 			goto out;
1540 		}
1541 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1542 			error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1543 			goto out;
1544 		}
1545 
1546 		/*
1547 		 * A rename to '.' or '..' results in a prematurely
1548 		 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1549 		 * here.
1550 		 */
1551 		if ((tondp->ni_cnd.cn_namelen == 1 &&
1552 		     tondp->ni_cnd.cn_nameptr[0] == '.') ||
1553 		    (tondp->ni_cnd.cn_namelen == 2 &&
1554 		     tondp->ni_cnd.cn_nameptr[0] == '.' &&
1555 		     tondp->ni_cnd.cn_nameptr[1] == '.')) {
1556 			error = EINVAL;
1557 			goto out;
1558 		}
1559 	}
1560 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1561 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1562 		goto out;
1563 	}
1564 	if (fvp->v_mount != tdvp->v_mount) {
1565 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1566 		goto out;
1567 	}
1568 	if (fvp == tdvp) {
1569 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1570 		goto out;
1571 	}
1572 	if (fvp == tvp) {
1573 		/*
1574 		 * If source and destination are the same, there is nothing to
1575 		 * do. Set error to -1 to indicate this.
1576 		 */
1577 		error = -1;
1578 		goto out;
1579 	}
1580 	if (ndflag & ND_NFSV4) {
1581 		if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1582 			error = nfsrv_checkremove(fvp, 0, NULL,
1583 			    (nfsquad_t)((u_quad_t)0), p);
1584 			NFSVOPUNLOCK(fvp);
1585 		} else
1586 			error = EPERM;
1587 		if (tvp && !error)
1588 			error = nfsrv_checkremove(tvp, 1, NULL,
1589 			    (nfsquad_t)((u_quad_t)0), p);
1590 	} else {
1591 		/*
1592 		 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1593 		 * that the NFSv4 client won't be confused by the rename.
1594 		 * Since nfsd_recalldelegation() can only be called on an
1595 		 * unlocked vnode at this point and fvp is the file that will
1596 		 * still exist after the rename, just do fvp.
1597 		 */
1598 		nfsd_recalldelegation(fvp, p);
1599 	}
1600 	if (error == 0 && tvp != NULL) {
1601 		nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1602 		NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1603 		    " dsdvp=%p\n", dsdvp[0]);
1604 	}
1605 out:
1606 	if (!error) {
1607 		error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1608 		    &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1609 		    &tondp->ni_cnd);
1610 	} else {
1611 		if (tdvp == tvp)
1612 			vrele(tdvp);
1613 		else
1614 			vput(tdvp);
1615 		if (tvp)
1616 			vput(tvp);
1617 		vrele(fromndp->ni_dvp);
1618 		vrele(fvp);
1619 		if (error == -1)
1620 			error = 0;
1621 	}
1622 
1623 	/*
1624 	 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1625 	 * if the rename succeeded, the DS file for the tvp needs to be
1626 	 * removed.
1627 	 */
1628 	if (error == 0 && dsdvp[0] != NULL) {
1629 		nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1630 		NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1631 	}
1632 
1633 	nfsvno_relpathbuf(tondp);
1634 out1:
1635 	nfsvno_relpathbuf(fromndp);
1636 	NFSEXITCODE(error);
1637 	return (error);
1638 }
1639 
1640 /*
1641  * Link vnode op.
1642  */
1643 int
1644 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1645     struct thread *p, struct nfsexstuff *exp)
1646 {
1647 	struct vnode *xp;
1648 	int error = 0;
1649 
1650 	xp = ndp->ni_vp;
1651 	if (xp != NULL) {
1652 		error = EEXIST;
1653 	} else {
1654 		xp = ndp->ni_dvp;
1655 		if (vp->v_mount != xp->v_mount)
1656 			error = EXDEV;
1657 	}
1658 	if (!error) {
1659 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1660 		if (!VN_IS_DOOMED(vp))
1661 			error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1662 		else
1663 			error = EPERM;
1664 		if (ndp->ni_dvp == vp) {
1665 			vrele(ndp->ni_dvp);
1666 			NFSVOPUNLOCK(vp);
1667 		} else {
1668 			vref(vp);
1669 			VOP_VPUT_PAIR(ndp->ni_dvp, &vp, true);
1670 		}
1671 	} else {
1672 		if (ndp->ni_dvp == ndp->ni_vp)
1673 			vrele(ndp->ni_dvp);
1674 		else
1675 			vput(ndp->ni_dvp);
1676 		if (ndp->ni_vp)
1677 			vrele(ndp->ni_vp);
1678 	}
1679 	nfsvno_relpathbuf(ndp);
1680 	NFSEXITCODE(error);
1681 	return (error);
1682 }
1683 
1684 /*
1685  * Do the fsync() appropriate for the commit.
1686  */
1687 int
1688 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1689     struct thread *td)
1690 {
1691 	int error = 0;
1692 
1693 	/*
1694 	 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1695 	 * file is done.  At this time VOP_FSYNC does not accept offset and
1696 	 * byte count parameters so call VOP_FSYNC the whole file for now.
1697 	 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1698 	 * File systems that do not use the buffer cache (as indicated
1699 	 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1700 	 */
1701 	if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1702 	    (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1703 		/*
1704 		 * Give up and do the whole thing
1705 		 */
1706 		if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1707 			VM_OBJECT_WLOCK(vp->v_object);
1708 			vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1709 			VM_OBJECT_WUNLOCK(vp->v_object);
1710 		}
1711 		error = VOP_FSYNC(vp, MNT_WAIT, td);
1712 	} else {
1713 		/*
1714 		 * Locate and synchronously write any buffers that fall
1715 		 * into the requested range.  Note:  we are assuming that
1716 		 * f_iosize is a power of 2.
1717 		 */
1718 		int iosize = vp->v_mount->mnt_stat.f_iosize;
1719 		int iomask = iosize - 1;
1720 		struct bufobj *bo;
1721 		daddr_t lblkno;
1722 
1723 		/*
1724 		 * Align to iosize boundary, super-align to page boundary.
1725 		 */
1726 		if (off & iomask) {
1727 			cnt += off & iomask;
1728 			off &= ~(u_quad_t)iomask;
1729 		}
1730 		if (off & PAGE_MASK) {
1731 			cnt += off & PAGE_MASK;
1732 			off &= ~(u_quad_t)PAGE_MASK;
1733 		}
1734 		lblkno = off / iosize;
1735 
1736 		if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1737 			VM_OBJECT_WLOCK(vp->v_object);
1738 			vm_object_page_clean(vp->v_object, off, off + cnt,
1739 			    OBJPC_SYNC);
1740 			VM_OBJECT_WUNLOCK(vp->v_object);
1741 		}
1742 
1743 		bo = &vp->v_bufobj;
1744 		BO_LOCK(bo);
1745 		while (cnt > 0) {
1746 			struct buf *bp;
1747 
1748 			/*
1749 			 * If we have a buffer and it is marked B_DELWRI we
1750 			 * have to lock and write it.  Otherwise the prior
1751 			 * write is assumed to have already been committed.
1752 			 *
1753 			 * gbincore() can return invalid buffers now so we
1754 			 * have to check that bit as well (though B_DELWRI
1755 			 * should not be set if B_INVAL is set there could be
1756 			 * a race here since we haven't locked the buffer).
1757 			 */
1758 			if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1759 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1760 				    LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1761 					BO_LOCK(bo);
1762 					continue; /* retry */
1763 				}
1764 			    	if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1765 				    B_DELWRI) {
1766 					bremfree(bp);
1767 					bp->b_flags &= ~B_ASYNC;
1768 					bwrite(bp);
1769 					++nfs_commit_miss;
1770 				} else
1771 					BUF_UNLOCK(bp);
1772 				BO_LOCK(bo);
1773 			}
1774 			++nfs_commit_blks;
1775 			if (cnt < iosize)
1776 				break;
1777 			cnt -= iosize;
1778 			++lblkno;
1779 		}
1780 		BO_UNLOCK(bo);
1781 	}
1782 	NFSEXITCODE(error);
1783 	return (error);
1784 }
1785 
1786 /*
1787  * Statfs vnode op.
1788  */
1789 int
1790 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1791 {
1792 	struct statfs *tsf;
1793 	int error;
1794 
1795 	tsf = NULL;
1796 	if (nfsrv_devidcnt > 0) {
1797 		/* For a pNFS service, get the DS numbers. */
1798 		tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1799 		error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1800 		if (error != 0) {
1801 			free(tsf, M_TEMP);
1802 			tsf = NULL;
1803 		}
1804 	}
1805 	error = VFS_STATFS(vp->v_mount, sf);
1806 	if (error == 0) {
1807 		if (tsf != NULL) {
1808 			sf->f_blocks = tsf->f_blocks;
1809 			sf->f_bavail = tsf->f_bavail;
1810 			sf->f_bfree = tsf->f_bfree;
1811 			sf->f_bsize = tsf->f_bsize;
1812 		}
1813 		/*
1814 		 * Since NFS handles these values as unsigned on the
1815 		 * wire, there is no way to represent negative values,
1816 		 * so set them to 0. Without this, they will appear
1817 		 * to be very large positive values for clients like
1818 		 * Solaris10.
1819 		 */
1820 		if (sf->f_bavail < 0)
1821 			sf->f_bavail = 0;
1822 		if (sf->f_ffree < 0)
1823 			sf->f_ffree = 0;
1824 	}
1825 	free(tsf, M_TEMP);
1826 	NFSEXITCODE(error);
1827 	return (error);
1828 }
1829 
1830 /*
1831  * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1832  * must handle nfsrv_opencheck() calls after any other access checks.
1833  */
1834 void
1835 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1836     nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1837     int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1838     NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, bool done_namei,
1839     struct nfsexstuff *exp, struct vnode **vpp)
1840 {
1841 	struct vnode *vp = NULL;
1842 	u_quad_t tempsize;
1843 	struct nfsexstuff nes;
1844 	struct thread *p = curthread;
1845 	uint32_t oldrepstat;
1846 
1847 	if (ndp->ni_vp == NULL) {
1848 		/*
1849 		 * If nfsrv_opencheck() sets nd_repstat, done_namei needs to be
1850 		 * set true, since cleanup after nfsvno_namei() is needed.
1851 		 */
1852 		oldrepstat = nd->nd_repstat;
1853 		nd->nd_repstat = nfsrv_opencheck(clientid,
1854 		    stateidp, stp, NULL, nd, p, nd->nd_repstat);
1855 		if (nd->nd_repstat != 0 && oldrepstat == 0)
1856 			done_namei = true;
1857 	}
1858 	if (!nd->nd_repstat) {
1859 		if (ndp->ni_vp == NULL) {
1860 			nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1861 			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1862 			/* For a pNFS server, create the data file on a DS. */
1863 			if (nd->nd_repstat == 0) {
1864 				/*
1865 				 * Create a data file on a DS for a pNFS server.
1866 				 * This function just returns if not
1867 				 * running a pNFS DS or the creation fails.
1868 				 */
1869 				nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1870 				    cred, p);
1871 			}
1872 			VOP_VPUT_PAIR(ndp->ni_dvp, nd->nd_repstat == 0 ?
1873 			    &ndp->ni_vp : NULL, false);
1874 			nfsvno_relpathbuf(ndp);
1875 			if (!nd->nd_repstat) {
1876 				if (*exclusive_flagp) {
1877 					*exclusive_flagp = 0;
1878 					NFSVNO_ATTRINIT(nvap);
1879 					nvap->na_atime.tv_sec = cverf[0];
1880 					nvap->na_atime.tv_nsec = cverf[1];
1881 					nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1882 					    &nvap->na_vattr, cred);
1883 					if (nd->nd_repstat != 0) {
1884 						vput(ndp->ni_vp);
1885 						ndp->ni_vp = NULL;
1886 						nd->nd_repstat = NFSERR_NOTSUPP;
1887 					} else
1888 						NFSSETBIT_ATTRBIT(attrbitp,
1889 						    NFSATTRBIT_TIMEACCESS);
1890 				} else {
1891 					nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1892 					    aclp, p, attrbitp, exp);
1893 				}
1894 			}
1895 			vp = ndp->ni_vp;
1896 		} else {
1897 			nfsvno_relpathbuf(ndp);
1898 			vp = ndp->ni_vp;
1899 			if (create == NFSV4OPEN_CREATE) {
1900 				if (ndp->ni_dvp == vp)
1901 					vrele(ndp->ni_dvp);
1902 				else
1903 					vput(ndp->ni_dvp);
1904 			}
1905 			if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1906 				if (ndp->ni_cnd.cn_flags & RDONLY)
1907 					NFSVNO_SETEXRDONLY(&nes);
1908 				else
1909 					NFSVNO_EXINIT(&nes);
1910 				nd->nd_repstat = nfsvno_accchk(vp,
1911 				    VWRITE, cred, &nes, p,
1912 				    NFSACCCHK_NOOVERRIDE,
1913 				    NFSACCCHK_VPISLOCKED, NULL);
1914 				nd->nd_repstat = nfsrv_opencheck(clientid,
1915 				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1916 				if (!nd->nd_repstat) {
1917 					tempsize = nvap->na_size;
1918 					NFSVNO_ATTRINIT(nvap);
1919 					nvap->na_size = tempsize;
1920 					nd->nd_repstat = nfsvno_setattr(vp,
1921 					    nvap, cred, p, exp);
1922 				}
1923 			} else if (vp->v_type == VREG) {
1924 				nd->nd_repstat = nfsrv_opencheck(clientid,
1925 				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1926 			}
1927 		}
1928 	} else if (done_namei) {
1929 		/*
1930 		 * done_namei is set when nfsvno_namei() has completed
1931 		 * successfully, but a subsequent error was set in
1932 		 * nd_repstat.  As such, cleanup of the nfsvno_namei()
1933 		 * results is required.
1934 		 */
1935 		nfsvno_relpathbuf(ndp);
1936 		if (ndp->ni_dvp == ndp->ni_vp)
1937 			vrele(ndp->ni_dvp);
1938 		else
1939 			vput(ndp->ni_dvp);
1940 		if (ndp->ni_vp)
1941 			vput(ndp->ni_vp);
1942 	}
1943 	*vpp = vp;
1944 
1945 	NFSEXITCODE2(0, nd);
1946 }
1947 
1948 /*
1949  * Updates the file rev and sets the mtime and ctime
1950  * to the current clock time, returning the va_filerev and va_Xtime
1951  * values.
1952  * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1953  */
1954 int
1955 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1956     struct nfsrv_descript *nd, struct thread *p)
1957 {
1958 	struct vattr va;
1959 
1960 	VATTR_NULL(&va);
1961 	vfs_timestamp(&va.va_mtime);
1962 	if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1963 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1964 		if (VN_IS_DOOMED(vp))
1965 			return (ESTALE);
1966 	}
1967 	(void) VOP_SETATTR(vp, &va, nd->nd_cred);
1968 	(void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1969 	return (0);
1970 }
1971 
1972 /*
1973  * Glue routine to nfsv4_fillattr().
1974  */
1975 int
1976 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1977     struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1978     struct ucred *cred, struct thread *p, int isdgram, int reterr,
1979     int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1980 {
1981 	struct statfs *sf;
1982 	int error;
1983 
1984 	sf = NULL;
1985 	if (nfsrv_devidcnt > 0 &&
1986 	    (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1987 	     NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1988 	     NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1989 		sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1990 		error = nfsrv_pnfsstatfs(sf, mp);
1991 		if (error != 0) {
1992 			free(sf, M_TEMP);
1993 			sf = NULL;
1994 		}
1995 	}
1996 	error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1997 	    attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1998 	    mounted_on_fileno, sf);
1999 	free(sf, M_TEMP);
2000 	NFSEXITCODE2(0, nd);
2001 	return (error);
2002 }
2003 
2004 /* Since the Readdir vnode ops vary, put the entire functions in here. */
2005 /*
2006  * nfs readdir service
2007  * - mallocs what it thinks is enough to read
2008  *	count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
2009  * - calls VOP_READDIR()
2010  * - loops around building the reply
2011  *	if the output generated exceeds count break out of loop
2012  *	The NFSM_CLGET macro is used here so that the reply will be packed
2013  *	tightly in mbuf clusters.
2014  * - it trims out records with d_fileno == 0
2015  *	this doesn't matter for Unix clients, but they might confuse clients
2016  *	for other os'.
2017  * - it trims out records with d_type == DT_WHT
2018  *	these cannot be seen through NFS (unless we extend the protocol)
2019  *     The alternate call nfsrvd_readdirplus() does lookups as well.
2020  * PS: The NFS protocol spec. does not clarify what the "count" byte
2021  *	argument is a count of.. just name strings and file id's or the
2022  *	entire reply rpc or ...
2023  *	I tried just file name and id sizes and it confused the Sun client,
2024  *	so I am using the full rpc size now. The "paranoia.." comment refers
2025  *	to including the status longwords that are not a part of the dir.
2026  *	"entry" structures, but are in the rpc.
2027  */
2028 int
2029 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2030     struct vnode *vp, struct nfsexstuff *exp)
2031 {
2032 	struct dirent *dp;
2033 	u_int32_t *tl;
2034 	int dirlen;
2035 	char *cpos, *cend, *rbuf;
2036 	struct nfsvattr at;
2037 	int nlen, error = 0, getret = 1;
2038 	int siz, cnt, fullsiz, eofflag, ncookies;
2039 	u_int64_t off, toff, verf __unused;
2040 	uint64_t *cookies = NULL, *cookiep;
2041 	struct uio io;
2042 	struct iovec iv;
2043 	int is_ufs;
2044 	struct thread *p = curthread;
2045 
2046 	if (nd->nd_repstat) {
2047 		nfsrv_postopattr(nd, getret, &at);
2048 		goto out;
2049 	}
2050 	if (nd->nd_flag & ND_NFSV2) {
2051 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2052 		off = fxdr_unsigned(u_quad_t, *tl++);
2053 	} else {
2054 		NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2055 		off = fxdr_hyper(tl);
2056 		tl += 2;
2057 		verf = fxdr_hyper(tl);
2058 		tl += 2;
2059 	}
2060 	toff = off;
2061 	cnt = fxdr_unsigned(int, *tl);
2062 	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2063 		cnt = NFS_SRVMAXDATA(nd);
2064 	siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2065 	fullsiz = siz;
2066 	if (nd->nd_flag & ND_NFSV3) {
2067 		nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2068 		    NULL);
2069 #if 0
2070 		/*
2071 		 * va_filerev is not sufficient as a cookie verifier,
2072 		 * since it is not supposed to change when entries are
2073 		 * removed/added unless that offset cookies returned to
2074 		 * the client are no longer valid.
2075 		 */
2076 		if (!nd->nd_repstat && toff && verf != at.na_filerev)
2077 			nd->nd_repstat = NFSERR_BAD_COOKIE;
2078 #endif
2079 	}
2080 	if (!nd->nd_repstat && vp->v_type != VDIR)
2081 		nd->nd_repstat = NFSERR_NOTDIR;
2082 	if (nd->nd_repstat == 0 && cnt == 0) {
2083 		if (nd->nd_flag & ND_NFSV2)
2084 			/* NFSv2 does not have NFSERR_TOOSMALL */
2085 			nd->nd_repstat = EPERM;
2086 		else
2087 			nd->nd_repstat = NFSERR_TOOSMALL;
2088 	}
2089 	if (!nd->nd_repstat)
2090 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2091 		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2092 		    NFSACCCHK_VPISLOCKED, NULL);
2093 	if (nd->nd_repstat) {
2094 		vput(vp);
2095 		if (nd->nd_flag & ND_NFSV3)
2096 			nfsrv_postopattr(nd, getret, &at);
2097 		goto out;
2098 	}
2099 	is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2100 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
2101 again:
2102 	eofflag = 0;
2103 	if (cookies) {
2104 		free(cookies, M_TEMP);
2105 		cookies = NULL;
2106 	}
2107 
2108 	iv.iov_base = rbuf;
2109 	iv.iov_len = siz;
2110 	io.uio_iov = &iv;
2111 	io.uio_iovcnt = 1;
2112 	io.uio_offset = (off_t)off;
2113 	io.uio_resid = siz;
2114 	io.uio_segflg = UIO_SYSSPACE;
2115 	io.uio_rw = UIO_READ;
2116 	io.uio_td = NULL;
2117 	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2118 	    &cookies);
2119 	off = (u_int64_t)io.uio_offset;
2120 	if (io.uio_resid)
2121 		siz -= io.uio_resid;
2122 
2123 	if (!cookies && !nd->nd_repstat)
2124 		nd->nd_repstat = NFSERR_PERM;
2125 	if (nd->nd_flag & ND_NFSV3) {
2126 		getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2127 		if (!nd->nd_repstat)
2128 			nd->nd_repstat = getret;
2129 	}
2130 
2131 	/*
2132 	 * Handles the failed cases. nd->nd_repstat == 0 past here.
2133 	 */
2134 	if (nd->nd_repstat) {
2135 		vput(vp);
2136 		free(rbuf, M_TEMP);
2137 		if (cookies)
2138 			free(cookies, M_TEMP);
2139 		if (nd->nd_flag & ND_NFSV3)
2140 			nfsrv_postopattr(nd, getret, &at);
2141 		goto out;
2142 	}
2143 	/*
2144 	 * If nothing read, return eof
2145 	 * rpc reply
2146 	 */
2147 	if (siz == 0) {
2148 		vput(vp);
2149 		if (nd->nd_flag & ND_NFSV2) {
2150 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2151 		} else {
2152 			nfsrv_postopattr(nd, getret, &at);
2153 			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2154 			txdr_hyper(at.na_filerev, tl);
2155 			tl += 2;
2156 		}
2157 		*tl++ = newnfs_false;
2158 		*tl = newnfs_true;
2159 		free(rbuf, M_TEMP);
2160 		free(cookies, M_TEMP);
2161 		goto out;
2162 	}
2163 
2164 	/*
2165 	 * Check for degenerate cases of nothing useful read.
2166 	 * If so go try again
2167 	 */
2168 	cpos = rbuf;
2169 	cend = rbuf + siz;
2170 	dp = (struct dirent *)cpos;
2171 	cookiep = cookies;
2172 
2173 	/*
2174 	 * For some reason FreeBSD's ufs_readdir() chooses to back the
2175 	 * directory offset up to a block boundary, so it is necessary to
2176 	 * skip over the records that precede the requested offset. This
2177 	 * requires the assumption that file offset cookies monotonically
2178 	 * increase.
2179 	 */
2180 	while (cpos < cend && ncookies > 0 &&
2181 	    (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2182 	     (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2183 		cpos += dp->d_reclen;
2184 		dp = (struct dirent *)cpos;
2185 		cookiep++;
2186 		ncookies--;
2187 	}
2188 	if (cpos >= cend || ncookies == 0) {
2189 		siz = fullsiz;
2190 		toff = off;
2191 		goto again;
2192 	}
2193 	vput(vp);
2194 
2195 	/*
2196 	 * If cnt > MCLBYTES and the reply will not be saved, use
2197 	 * ext_pgs mbufs for TLS.
2198 	 * For NFSv4.0, we do not know for sure if the reply will
2199 	 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2200 	 */
2201 	if (cnt > MCLBYTES && siz > MCLBYTES &&
2202 	    (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2203 	    (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2204 		nd->nd_flag |= ND_EXTPG;
2205 
2206 	/*
2207 	 * dirlen is the size of the reply, including all XDR and must
2208 	 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2209 	 * if the XDR should be included in "count", but to be safe, we do.
2210 	 * (Include the two booleans at the end of the reply in dirlen now.)
2211 	 */
2212 	if (nd->nd_flag & ND_NFSV3) {
2213 		nfsrv_postopattr(nd, getret, &at);
2214 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2215 		txdr_hyper(at.na_filerev, tl);
2216 		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2217 	} else {
2218 		dirlen = 2 * NFSX_UNSIGNED;
2219 	}
2220 
2221 	/* Loop through the records and build reply */
2222 	while (cpos < cend && ncookies > 0) {
2223 		nlen = dp->d_namlen;
2224 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2225 			nlen <= NFS_MAXNAMLEN) {
2226 			if (nd->nd_flag & ND_NFSV3)
2227 				dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2228 			else
2229 				dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2230 			if (dirlen > cnt) {
2231 				eofflag = 0;
2232 				break;
2233 			}
2234 
2235 			/*
2236 			 * Build the directory record xdr from
2237 			 * the dirent entry.
2238 			 */
2239 			if (nd->nd_flag & ND_NFSV3) {
2240 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2241 				*tl++ = newnfs_true;
2242 				*tl++ = 0;
2243 			} else {
2244 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2245 				*tl++ = newnfs_true;
2246 			}
2247 			*tl = txdr_unsigned(dp->d_fileno);
2248 			(void) nfsm_strtom(nd, dp->d_name, nlen);
2249 			if (nd->nd_flag & ND_NFSV3) {
2250 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2251 				txdr_hyper(*cookiep, tl);
2252 			} else {
2253 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2254 				*tl = txdr_unsigned(*cookiep);
2255 			}
2256 		}
2257 		cpos += dp->d_reclen;
2258 		dp = (struct dirent *)cpos;
2259 		cookiep++;
2260 		ncookies--;
2261 	}
2262 	if (cpos < cend)
2263 		eofflag = 0;
2264 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2265 	*tl++ = newnfs_false;
2266 	if (eofflag)
2267 		*tl = newnfs_true;
2268 	else
2269 		*tl = newnfs_false;
2270 	free(rbuf, M_TEMP);
2271 	free(cookies, M_TEMP);
2272 
2273 out:
2274 	NFSEXITCODE2(0, nd);
2275 	return (0);
2276 nfsmout:
2277 	vput(vp);
2278 	NFSEXITCODE2(error, nd);
2279 	return (error);
2280 }
2281 
2282 /*
2283  * Readdirplus for V3 and Readdir for V4.
2284  */
2285 int
2286 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2287     struct vnode *vp, struct nfsexstuff *exp)
2288 {
2289 	struct dirent *dp;
2290 	u_int32_t *tl;
2291 	int dirlen;
2292 	char *cpos, *cend, *rbuf;
2293 	struct vnode *nvp;
2294 	fhandle_t nfh;
2295 	struct nfsvattr nva, at, *nvap = &nva;
2296 	struct mbuf *mb0, *mb1;
2297 	struct nfsreferral *refp;
2298 	int nlen, r, error = 0, getret = 1, usevget = 1;
2299 	int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2300 	caddr_t bpos0, bpos1;
2301 	u_int64_t off, toff, verf __unused;
2302 	uint64_t *cookies = NULL, *cookiep;
2303 	nfsattrbit_t attrbits, rderrbits, savbits;
2304 	struct uio io;
2305 	struct iovec iv;
2306 	struct componentname cn;
2307 	int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2308 	struct mount *mp, *new_mp;
2309 	uint64_t mounted_on_fileno;
2310 	struct thread *p = curthread;
2311 	int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2312 
2313 	if (nd->nd_repstat) {
2314 		nfsrv_postopattr(nd, getret, &at);
2315 		goto out;
2316 	}
2317 	NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2318 	off = fxdr_hyper(tl);
2319 	toff = off;
2320 	tl += 2;
2321 	verf = fxdr_hyper(tl);
2322 	tl += 2;
2323 	siz = fxdr_unsigned(int, *tl++);
2324 	cnt = fxdr_unsigned(int, *tl);
2325 
2326 	/*
2327 	 * Use the server's maximum data transfer size as the upper bound
2328 	 * on reply datalen.
2329 	 */
2330 	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2331 		cnt = NFS_SRVMAXDATA(nd);
2332 
2333 	/*
2334 	 * siz is a "hint" of how much directory information (name, fileid,
2335 	 * cookie) should be in the reply. At least one client "hints" 0,
2336 	 * so I set it to cnt for that case. I also round it up to the
2337 	 * next multiple of DIRBLKSIZ.
2338 	 * Since the size of a Readdirplus directory entry reply will always
2339 	 * be greater than a directory entry returned by VOP_READDIR(), it
2340 	 * does not make sense to read more than NFS_SRVMAXDATA() via
2341 	 * VOP_READDIR().
2342 	 */
2343 	if (siz <= 0)
2344 		siz = cnt;
2345 	else if (siz > NFS_SRVMAXDATA(nd))
2346 		siz = NFS_SRVMAXDATA(nd);
2347 	siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2348 
2349 	if (nd->nd_flag & ND_NFSV4) {
2350 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2351 		if (error)
2352 			goto nfsmout;
2353 		NFSSET_ATTRBIT(&savbits, &attrbits);
2354 		NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2355 		NFSZERO_ATTRBIT(&rderrbits);
2356 		NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2357 	} else {
2358 		NFSZERO_ATTRBIT(&attrbits);
2359 	}
2360 	fullsiz = siz;
2361 	nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2362 #if 0
2363 	if (!nd->nd_repstat) {
2364 	    if (off && verf != at.na_filerev) {
2365 		/*
2366 		 * va_filerev is not sufficient as a cookie verifier,
2367 		 * since it is not supposed to change when entries are
2368 		 * removed/added unless that offset cookies returned to
2369 		 * the client are no longer valid.
2370 		 */
2371 		if (nd->nd_flag & ND_NFSV4) {
2372 			nd->nd_repstat = NFSERR_NOTSAME;
2373 		} else {
2374 			nd->nd_repstat = NFSERR_BAD_COOKIE;
2375 		}
2376 	    }
2377 	}
2378 #endif
2379 	if (!nd->nd_repstat && vp->v_type != VDIR)
2380 		nd->nd_repstat = NFSERR_NOTDIR;
2381 	if (!nd->nd_repstat && cnt == 0)
2382 		nd->nd_repstat = NFSERR_TOOSMALL;
2383 	if (!nd->nd_repstat)
2384 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2385 		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2386 		    NFSACCCHK_VPISLOCKED, NULL);
2387 	if (nd->nd_repstat) {
2388 		vput(vp);
2389 		if (nd->nd_flag & ND_NFSV3)
2390 			nfsrv_postopattr(nd, getret, &at);
2391 		goto out;
2392 	}
2393 	is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2394 	is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2395 
2396 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
2397 again:
2398 	eofflag = 0;
2399 	if (cookies) {
2400 		free(cookies, M_TEMP);
2401 		cookies = NULL;
2402 	}
2403 
2404 	iv.iov_base = rbuf;
2405 	iv.iov_len = siz;
2406 	io.uio_iov = &iv;
2407 	io.uio_iovcnt = 1;
2408 	io.uio_offset = (off_t)off;
2409 	io.uio_resid = siz;
2410 	io.uio_segflg = UIO_SYSSPACE;
2411 	io.uio_rw = UIO_READ;
2412 	io.uio_td = NULL;
2413 	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2414 	    &cookies);
2415 	off = (u_int64_t)io.uio_offset;
2416 	if (io.uio_resid)
2417 		siz -= io.uio_resid;
2418 
2419 	getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2420 
2421 	if (!cookies && !nd->nd_repstat)
2422 		nd->nd_repstat = NFSERR_PERM;
2423 	if (!nd->nd_repstat)
2424 		nd->nd_repstat = getret;
2425 	if (nd->nd_repstat) {
2426 		vput(vp);
2427 		if (cookies)
2428 			free(cookies, M_TEMP);
2429 		free(rbuf, M_TEMP);
2430 		if (nd->nd_flag & ND_NFSV3)
2431 			nfsrv_postopattr(nd, getret, &at);
2432 		goto out;
2433 	}
2434 	/*
2435 	 * If nothing read, return eof
2436 	 * rpc reply
2437 	 */
2438 	if (siz == 0) {
2439 		vput(vp);
2440 		if (nd->nd_flag & ND_NFSV3)
2441 			nfsrv_postopattr(nd, getret, &at);
2442 		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2443 		txdr_hyper(at.na_filerev, tl);
2444 		tl += 2;
2445 		*tl++ = newnfs_false;
2446 		*tl = newnfs_true;
2447 		free(cookies, M_TEMP);
2448 		free(rbuf, M_TEMP);
2449 		goto out;
2450 	}
2451 
2452 	/*
2453 	 * Check for degenerate cases of nothing useful read.
2454 	 * If so go try again
2455 	 */
2456 	cpos = rbuf;
2457 	cend = rbuf + siz;
2458 	dp = (struct dirent *)cpos;
2459 	cookiep = cookies;
2460 
2461 	/*
2462 	 * For some reason FreeBSD's ufs_readdir() chooses to back the
2463 	 * directory offset up to a block boundary, so it is necessary to
2464 	 * skip over the records that precede the requested offset. This
2465 	 * requires the assumption that file offset cookies monotonically
2466 	 * increase.
2467 	 */
2468 	while (cpos < cend && ncookies > 0 &&
2469 	  (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2470 	   (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2471 	   ((nd->nd_flag & ND_NFSV4) &&
2472 	    ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2473 	     (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2474 		cpos += dp->d_reclen;
2475 		dp = (struct dirent *)cpos;
2476 		cookiep++;
2477 		ncookies--;
2478 	}
2479 	if (cpos >= cend || ncookies == 0) {
2480 		siz = fullsiz;
2481 		toff = off;
2482 		goto again;
2483 	}
2484 
2485 	/*
2486 	 * Busy the file system so that the mount point won't go away
2487 	 * and, as such, VFS_VGET() can be used safely.
2488 	 */
2489 	mp = vp->v_mount;
2490 	vfs_ref(mp);
2491 	NFSVOPUNLOCK(vp);
2492 	nd->nd_repstat = vfs_busy(mp, 0);
2493 	vfs_rel(mp);
2494 	if (nd->nd_repstat != 0) {
2495 		vrele(vp);
2496 		free(cookies, M_TEMP);
2497 		free(rbuf, M_TEMP);
2498 		if (nd->nd_flag & ND_NFSV3)
2499 			nfsrv_postopattr(nd, getret, &at);
2500 		goto out;
2501 	}
2502 
2503 	/*
2504 	 * Check to see if entries in this directory can be safely acquired
2505 	 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2506 	 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2507 	 * automount of the snapshot directory that is required will
2508 	 * be done.
2509 	 * This needs to be done here for NFSv4, since NFSv4 never does
2510 	 * a VFS_VGET() for "." or "..".
2511 	 */
2512 	if (is_zfs == 1) {
2513 		r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2514 		if (r == EOPNOTSUPP) {
2515 			usevget = 0;
2516 			cn.cn_nameiop = LOOKUP;
2517 			cn.cn_lkflags = LK_SHARED | LK_RETRY;
2518 			cn.cn_cred = nd->nd_cred;
2519 		} else if (r == 0)
2520 			vput(nvp);
2521 	}
2522 
2523 	/*
2524 	 * If the reply is likely to exceed MCLBYTES and the reply will
2525 	 * not be saved, use ext_pgs mbufs for TLS.
2526 	 * It is difficult to predict how large each entry will be and
2527 	 * how many entries have been read, so just assume the directory
2528 	 * entries grow by a factor of 4 when attributes are included.
2529 	 * For NFSv4.0, we do not know for sure if the reply will
2530 	 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2531 	 */
2532 	if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2533 	    (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2534 	    (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2535 		nd->nd_flag |= ND_EXTPG;
2536 
2537 	/*
2538 	 * Save this position, in case there is an error before one entry
2539 	 * is created.
2540 	 */
2541 	mb0 = nd->nd_mb;
2542 	bpos0 = nd->nd_bpos;
2543 	bextpg0 = nd->nd_bextpg;
2544 	bextpgsiz0 = nd->nd_bextpgsiz;
2545 
2546 	/*
2547 	 * Fill in the first part of the reply.
2548 	 * dirlen is the reply length in bytes and cannot exceed cnt.
2549 	 * (Include the two booleans at the end of the reply in dirlen now,
2550 	 *  so we recognize when we have exceeded cnt.)
2551 	 */
2552 	if (nd->nd_flag & ND_NFSV3) {
2553 		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2554 		nfsrv_postopattr(nd, getret, &at);
2555 	} else {
2556 		dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2557 	}
2558 	NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2559 	txdr_hyper(at.na_filerev, tl);
2560 
2561 	/*
2562 	 * Save this position, in case there is an empty reply needed.
2563 	 */
2564 	mb1 = nd->nd_mb;
2565 	bpos1 = nd->nd_bpos;
2566 	bextpg1 = nd->nd_bextpg;
2567 	bextpgsiz1 = nd->nd_bextpgsiz;
2568 
2569 	/* Loop through the records and build reply */
2570 	entrycnt = 0;
2571 	while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2572 		nlen = dp->d_namlen;
2573 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2574 		    nlen <= NFS_MAXNAMLEN &&
2575 		    ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2576 		     (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2577 		      || (nlen == 1 && dp->d_name[0] != '.'))) {
2578 			/*
2579 			 * Save the current position in the reply, in case
2580 			 * this entry exceeds cnt.
2581 			 */
2582 			mb1 = nd->nd_mb;
2583 			bpos1 = nd->nd_bpos;
2584 			bextpg1 = nd->nd_bextpg;
2585 			bextpgsiz1 = nd->nd_bextpgsiz;
2586 
2587 			/*
2588 			 * For readdir_and_lookup get the vnode using
2589 			 * the file number.
2590 			 */
2591 			nvp = NULL;
2592 			refp = NULL;
2593 			r = 0;
2594 			at_root = 0;
2595 			needs_unbusy = 0;
2596 			new_mp = mp;
2597 			mounted_on_fileno = (uint64_t)dp->d_fileno;
2598 			if ((nd->nd_flag & ND_NFSV3) ||
2599 			    NFSNONZERO_ATTRBIT(&savbits)) {
2600 				if (nd->nd_flag & ND_NFSV4)
2601 					refp = nfsv4root_getreferral(NULL,
2602 					    vp, dp->d_fileno);
2603 				if (refp == NULL) {
2604 					if (usevget)
2605 						r = VFS_VGET(mp, dp->d_fileno,
2606 						    LK_SHARED, &nvp);
2607 					else
2608 						r = EOPNOTSUPP;
2609 					if (r == EOPNOTSUPP) {
2610 						if (usevget) {
2611 							usevget = 0;
2612 							cn.cn_nameiop = LOOKUP;
2613 							cn.cn_lkflags =
2614 							    LK_SHARED |
2615 							    LK_RETRY;
2616 							cn.cn_cred =
2617 							    nd->nd_cred;
2618 						}
2619 						cn.cn_nameptr = dp->d_name;
2620 						cn.cn_namelen = nlen;
2621 						cn.cn_flags = ISLASTCN |
2622 						    NOFOLLOW | LOCKLEAF;
2623 						if (nlen == 2 &&
2624 						    dp->d_name[0] == '.' &&
2625 						    dp->d_name[1] == '.')
2626 							cn.cn_flags |=
2627 							    ISDOTDOT;
2628 						if (NFSVOPLOCK(vp, LK_SHARED)
2629 						    != 0) {
2630 							nd->nd_repstat = EPERM;
2631 							break;
2632 						}
2633 						if ((vp->v_vflag & VV_ROOT) != 0
2634 						    && (cn.cn_flags & ISDOTDOT)
2635 						    != 0) {
2636 							vref(vp);
2637 							nvp = vp;
2638 							r = 0;
2639 						} else {
2640 							r = VOP_LOOKUP(vp, &nvp,
2641 							    &cn);
2642 							if (vp != nvp)
2643 								NFSVOPUNLOCK(vp);
2644 						}
2645 					}
2646 
2647 					/*
2648 					 * For NFSv4, check to see if nvp is
2649 					 * a mount point and get the mount
2650 					 * point vnode, as required.
2651 					 */
2652 					if (r == 0 &&
2653 					    nfsrv_enable_crossmntpt != 0 &&
2654 					    (nd->nd_flag & ND_NFSV4) != 0 &&
2655 					    nvp->v_type == VDIR &&
2656 					    nvp->v_mountedhere != NULL) {
2657 						new_mp = nvp->v_mountedhere;
2658 						r = vfs_busy(new_mp, 0);
2659 						vput(nvp);
2660 						nvp = NULL;
2661 						if (r == 0) {
2662 							r = VFS_ROOT(new_mp,
2663 							    LK_SHARED, &nvp);
2664 							needs_unbusy = 1;
2665 							if (r == 0)
2666 								at_root = 1;
2667 						}
2668 					}
2669 				}
2670 
2671 				/*
2672 				 * If we failed to look up the entry, then it
2673 				 * has become invalid, most likely removed.
2674 				 */
2675 				if (r != 0) {
2676 					if (needs_unbusy)
2677 						vfs_unbusy(new_mp);
2678 					goto invalid;
2679 				}
2680 				KASSERT(refp != NULL || nvp != NULL,
2681 				    ("%s: undetected lookup error", __func__));
2682 
2683 				if (refp == NULL &&
2684 				    ((nd->nd_flag & ND_NFSV3) ||
2685 				     NFSNONZERO_ATTRBIT(&attrbits))) {
2686 					r = nfsvno_getfh(nvp, &nfh, p);
2687 					if (!r)
2688 					    r = nfsvno_getattr(nvp, nvap, nd, p,
2689 						1, &attrbits);
2690 					if (r == 0 && is_zfs == 1 &&
2691 					    nfsrv_enable_crossmntpt != 0 &&
2692 					    (nd->nd_flag & ND_NFSV4) != 0 &&
2693 					    nvp->v_type == VDIR &&
2694 					    vp->v_mount != nvp->v_mount) {
2695 					    /*
2696 					     * For a ZFS snapshot, there is a
2697 					     * pseudo mount that does not set
2698 					     * v_mountedhere, so it needs to
2699 					     * be detected via a different
2700 					     * mount structure.
2701 					     */
2702 					    at_root = 1;
2703 					    if (new_mp == mp)
2704 						new_mp = nvp->v_mount;
2705 					}
2706 				}
2707 
2708 				/*
2709 				 * If we failed to get attributes of the entry,
2710 				 * then just skip it for NFSv3 (the traditional
2711 				 * behavior in the old NFS server).
2712 				 * For NFSv4 the behavior is controlled by
2713 				 * RDATTRERROR: we either ignore the error or
2714 				 * fail the request.
2715 				 * The exception is EOPNOTSUPP, which can be
2716 				 * returned by nfsvno_getfh() for certain
2717 				 * file systems, such as devfs.  This indicates
2718 				 * that the file system cannot be exported,
2719 				 * so just skip over the entry.
2720 				 * Note that RDATTRERROR is never set for NFSv3.
2721 				 */
2722 				if (r != 0) {
2723 					if (!NFSISSET_ATTRBIT(&attrbits,
2724 					    NFSATTRBIT_RDATTRERROR) ||
2725 					    r == EOPNOTSUPP) {
2726 						vput(nvp);
2727 						if (needs_unbusy != 0)
2728 							vfs_unbusy(new_mp);
2729 						if ((nd->nd_flag & ND_NFSV3) ||
2730 						    r == EOPNOTSUPP)
2731 							goto invalid;
2732 						nd->nd_repstat = r;
2733 						break;
2734 					}
2735 				}
2736 			}
2737 
2738 			/*
2739 			 * Build the directory record xdr
2740 			 */
2741 			if (nd->nd_flag & ND_NFSV3) {
2742 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2743 				*tl++ = newnfs_true;
2744 				*tl++ = 0;
2745 				*tl = txdr_unsigned(dp->d_fileno);
2746 				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2747 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2748 				txdr_hyper(*cookiep, tl);
2749 				nfsrv_postopattr(nd, 0, nvap);
2750 				dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2751 				dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2752 				if (nvp != NULL)
2753 					vput(nvp);
2754 			} else {
2755 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2756 				*tl++ = newnfs_true;
2757 				txdr_hyper(*cookiep, tl);
2758 				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2759 				if (nvp != NULL) {
2760 					supports_nfsv4acls =
2761 					    nfs_supportsnfsv4acls(nvp);
2762 					NFSVOPUNLOCK(nvp);
2763 				} else
2764 					supports_nfsv4acls = 0;
2765 				if (refp != NULL) {
2766 					dirlen += nfsrv_putreferralattr(nd,
2767 					    &savbits, refp, 0,
2768 					    &nd->nd_repstat);
2769 					if (nd->nd_repstat) {
2770 						if (nvp != NULL)
2771 							vrele(nvp);
2772 						if (needs_unbusy != 0)
2773 							vfs_unbusy(new_mp);
2774 						break;
2775 					}
2776 				} else if (r) {
2777 					dirlen += nfsvno_fillattr(nd, new_mp,
2778 					    nvp, nvap, &nfh, r, &rderrbits,
2779 					    nd->nd_cred, p, isdgram, 0,
2780 					    supports_nfsv4acls, at_root,
2781 					    mounted_on_fileno);
2782 				} else {
2783 					dirlen += nfsvno_fillattr(nd, new_mp,
2784 					    nvp, nvap, &nfh, r, &attrbits,
2785 					    nd->nd_cred, p, isdgram, 0,
2786 					    supports_nfsv4acls, at_root,
2787 					    mounted_on_fileno);
2788 				}
2789 				if (nvp != NULL)
2790 					vrele(nvp);
2791 				dirlen += (3 * NFSX_UNSIGNED);
2792 			}
2793 			if (needs_unbusy != 0)
2794 				vfs_unbusy(new_mp);
2795 			if (dirlen <= cnt)
2796 				entrycnt++;
2797 		}
2798 invalid:
2799 		cpos += dp->d_reclen;
2800 		dp = (struct dirent *)cpos;
2801 		cookiep++;
2802 		ncookies--;
2803 	}
2804 	vrele(vp);
2805 	vfs_unbusy(mp);
2806 
2807 	/*
2808 	 * If dirlen > cnt, we must strip off the last entry. If that
2809 	 * results in an empty reply, report NFSERR_TOOSMALL.
2810 	 */
2811 	if (dirlen > cnt || nd->nd_repstat) {
2812 		if (!nd->nd_repstat && entrycnt == 0)
2813 			nd->nd_repstat = NFSERR_TOOSMALL;
2814 		if (nd->nd_repstat) {
2815 			nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2816 			if (nd->nd_flag & ND_NFSV3)
2817 				nfsrv_postopattr(nd, getret, &at);
2818 		} else
2819 			nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2820 		eofflag = 0;
2821 	} else if (cpos < cend)
2822 		eofflag = 0;
2823 	if (!nd->nd_repstat) {
2824 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2825 		*tl++ = newnfs_false;
2826 		if (eofflag)
2827 			*tl = newnfs_true;
2828 		else
2829 			*tl = newnfs_false;
2830 	}
2831 	free(cookies, M_TEMP);
2832 	free(rbuf, M_TEMP);
2833 
2834 out:
2835 	NFSEXITCODE2(0, nd);
2836 	return (0);
2837 nfsmout:
2838 	vput(vp);
2839 	NFSEXITCODE2(error, nd);
2840 	return (error);
2841 }
2842 
2843 /*
2844  * Get the settable attributes out of the mbuf list.
2845  * (Return 0 or EBADRPC)
2846  */
2847 int
2848 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2849     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2850 {
2851 	u_int32_t *tl;
2852 	struct nfsv2_sattr *sp;
2853 	int error = 0, toclient = 0;
2854 
2855 	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2856 	case ND_NFSV2:
2857 		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2858 		/*
2859 		 * Some old clients didn't fill in the high order 16bits.
2860 		 * --> check the low order 2 bytes for 0xffff
2861 		 */
2862 		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2863 			nvap->na_mode = nfstov_mode(sp->sa_mode);
2864 		if (sp->sa_uid != newnfs_xdrneg1)
2865 			nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2866 		if (sp->sa_gid != newnfs_xdrneg1)
2867 			nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2868 		if (sp->sa_size != newnfs_xdrneg1)
2869 			nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2870 		if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2871 #ifdef notyet
2872 			fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2873 #else
2874 			nvap->na_atime.tv_sec =
2875 				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2876 			nvap->na_atime.tv_nsec = 0;
2877 #endif
2878 		}
2879 		if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2880 			fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2881 		break;
2882 	case ND_NFSV3:
2883 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2884 		if (*tl == newnfs_true) {
2885 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2886 			nvap->na_mode = nfstov_mode(*tl);
2887 		}
2888 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2889 		if (*tl == newnfs_true) {
2890 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2891 			nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2892 		}
2893 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2894 		if (*tl == newnfs_true) {
2895 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2896 			nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2897 		}
2898 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2899 		if (*tl == newnfs_true) {
2900 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2901 			nvap->na_size = fxdr_hyper(tl);
2902 		}
2903 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2904 		switch (fxdr_unsigned(int, *tl)) {
2905 		case NFSV3SATTRTIME_TOCLIENT:
2906 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2907 			fxdr_nfsv3time(tl, &nvap->na_atime);
2908 			toclient = 1;
2909 			break;
2910 		case NFSV3SATTRTIME_TOSERVER:
2911 			vfs_timestamp(&nvap->na_atime);
2912 			nvap->na_vaflags |= VA_UTIMES_NULL;
2913 			break;
2914 		}
2915 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2916 		switch (fxdr_unsigned(int, *tl)) {
2917 		case NFSV3SATTRTIME_TOCLIENT:
2918 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2919 			fxdr_nfsv3time(tl, &nvap->na_mtime);
2920 			nvap->na_vaflags &= ~VA_UTIMES_NULL;
2921 			break;
2922 		case NFSV3SATTRTIME_TOSERVER:
2923 			vfs_timestamp(&nvap->na_mtime);
2924 			if (!toclient)
2925 				nvap->na_vaflags |= VA_UTIMES_NULL;
2926 			break;
2927 		}
2928 		break;
2929 	case ND_NFSV4:
2930 		error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2931 	}
2932 nfsmout:
2933 	NFSEXITCODE2(error, nd);
2934 	return (error);
2935 }
2936 
2937 /*
2938  * Handle the setable attributes for V4.
2939  * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2940  */
2941 int
2942 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2943     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2944 {
2945 	u_int32_t *tl;
2946 	int attrsum = 0;
2947 	int i, j;
2948 	int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2949 	int moderet, toclient = 0;
2950 	u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2951 	uid_t uid;
2952 	gid_t gid;
2953 	u_short mode, mask;		/* Same type as va_mode. */
2954 	struct vattr va;
2955 
2956 	error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2957 	if (error)
2958 		goto nfsmout;
2959 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2960 	attrsize = fxdr_unsigned(int, *tl);
2961 
2962 	/*
2963 	 * Loop around getting the setable attributes. If an unsupported
2964 	 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2965 	 */
2966 	if (retnotsup) {
2967 		nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2968 		bitpos = NFSATTRBIT_MAX;
2969 	} else {
2970 		bitpos = 0;
2971 	}
2972 	moderet = 0;
2973 	for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2974 	    if (attrsum > attrsize) {
2975 		error = NFSERR_BADXDR;
2976 		goto nfsmout;
2977 	    }
2978 	    if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2979 		switch (bitpos) {
2980 		case NFSATTRBIT_SIZE:
2981 			NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2982                      if (vp != NULL && vp->v_type != VREG) {
2983                             error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2984                                 NFSERR_INVAL;
2985                             goto nfsmout;
2986 			}
2987 			nvap->na_size = fxdr_hyper(tl);
2988 			attrsum += NFSX_HYPER;
2989 			break;
2990 		case NFSATTRBIT_ACL:
2991 			error = nfsrv_dissectacl(nd, aclp, true, &aceerr,
2992 			    &aclsize, p);
2993 			if (error)
2994 				goto nfsmout;
2995 			if (aceerr && !nd->nd_repstat)
2996 				nd->nd_repstat = aceerr;
2997 			attrsum += aclsize;
2998 			break;
2999 		case NFSATTRBIT_ARCHIVE:
3000 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3001 			if (!nd->nd_repstat)
3002 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3003 			attrsum += NFSX_UNSIGNED;
3004 			break;
3005 		case NFSATTRBIT_HIDDEN:
3006 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3007 			if (!nd->nd_repstat)
3008 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3009 			attrsum += NFSX_UNSIGNED;
3010 			break;
3011 		case NFSATTRBIT_MIMETYPE:
3012 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3013 			i = fxdr_unsigned(int, *tl);
3014 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3015 			if (error)
3016 				goto nfsmout;
3017 			if (!nd->nd_repstat)
3018 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3019 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3020 			break;
3021 		case NFSATTRBIT_MODE:
3022 			moderet = NFSERR_INVAL;	/* Can't do MODESETMASKED. */
3023 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3024 			nvap->na_mode = nfstov_mode(*tl);
3025 			attrsum += NFSX_UNSIGNED;
3026 			break;
3027 		case NFSATTRBIT_OWNER:
3028 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3029 			j = fxdr_unsigned(int, *tl);
3030 			if (j < 0) {
3031 				error = NFSERR_BADXDR;
3032 				goto nfsmout;
3033 			}
3034 			if (j > NFSV4_SMALLSTR)
3035 				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3036 			else
3037 				cp = namestr;
3038 			error = nfsrv_mtostr(nd, cp, j);
3039 			if (error) {
3040 				if (j > NFSV4_SMALLSTR)
3041 					free(cp, M_NFSSTRING);
3042 				goto nfsmout;
3043 			}
3044 			if (!nd->nd_repstat) {
3045 				nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3046 				    &uid);
3047 				if (!nd->nd_repstat)
3048 					nvap->na_uid = uid;
3049 			}
3050 			if (j > NFSV4_SMALLSTR)
3051 				free(cp, M_NFSSTRING);
3052 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3053 			break;
3054 		case NFSATTRBIT_OWNERGROUP:
3055 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3056 			j = fxdr_unsigned(int, *tl);
3057 			if (j < 0) {
3058 				error = NFSERR_BADXDR;
3059 				goto nfsmout;
3060 			}
3061 			if (j > NFSV4_SMALLSTR)
3062 				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3063 			else
3064 				cp = namestr;
3065 			error = nfsrv_mtostr(nd, cp, j);
3066 			if (error) {
3067 				if (j > NFSV4_SMALLSTR)
3068 					free(cp, M_NFSSTRING);
3069 				goto nfsmout;
3070 			}
3071 			if (!nd->nd_repstat) {
3072 				nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3073 				    &gid);
3074 				if (!nd->nd_repstat)
3075 					nvap->na_gid = gid;
3076 			}
3077 			if (j > NFSV4_SMALLSTR)
3078 				free(cp, M_NFSSTRING);
3079 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3080 			break;
3081 		case NFSATTRBIT_SYSTEM:
3082 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3083 			if (!nd->nd_repstat)
3084 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3085 			attrsum += NFSX_UNSIGNED;
3086 			break;
3087 		case NFSATTRBIT_TIMEACCESSSET:
3088 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3089 			attrsum += NFSX_UNSIGNED;
3090 			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3091 			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3092 			    fxdr_nfsv4time(tl, &nvap->na_atime);
3093 			    toclient = 1;
3094 			    attrsum += NFSX_V4TIME;
3095 			} else {
3096 			    vfs_timestamp(&nvap->na_atime);
3097 			    nvap->na_vaflags |= VA_UTIMES_NULL;
3098 			}
3099 			break;
3100 		case NFSATTRBIT_TIMEBACKUP:
3101 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3102 			if (!nd->nd_repstat)
3103 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3104 			attrsum += NFSX_V4TIME;
3105 			break;
3106 		case NFSATTRBIT_TIMECREATE:
3107 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3108 			fxdr_nfsv4time(tl, &nvap->na_btime);
3109 			attrsum += NFSX_V4TIME;
3110 			break;
3111 		case NFSATTRBIT_TIMEMODIFYSET:
3112 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3113 			attrsum += NFSX_UNSIGNED;
3114 			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3115 			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3116 			    fxdr_nfsv4time(tl, &nvap->na_mtime);
3117 			    nvap->na_vaflags &= ~VA_UTIMES_NULL;
3118 			    attrsum += NFSX_V4TIME;
3119 			} else {
3120 			    vfs_timestamp(&nvap->na_mtime);
3121 			    if (!toclient)
3122 				nvap->na_vaflags |= VA_UTIMES_NULL;
3123 			}
3124 			break;
3125 		case NFSATTRBIT_MODESETMASKED:
3126 			NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3127 			mode = fxdr_unsigned(u_short, *tl++);
3128 			mask = fxdr_unsigned(u_short, *tl);
3129 			/*
3130 			 * vp == NULL implies an Open/Create operation.
3131 			 * This attribute can only be used for Setattr and
3132 			 * only for NFSv4.1 or higher.
3133 			 * If moderet != 0, a mode attribute has also been
3134 			 * specified and this attribute cannot be done in the
3135 			 * same Setattr operation.
3136 			 */
3137 			if ((nd->nd_flag & ND_NFSV41) == 0)
3138 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3139 			else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3140 			    vp == NULL)
3141 				nd->nd_repstat = NFSERR_INVAL;
3142 			else if (moderet == 0)
3143 				moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3144 			if (moderet == 0)
3145 				nvap->na_mode = (mode & mask) |
3146 				    (va.va_mode & ~mask);
3147 			else
3148 				nd->nd_repstat = moderet;
3149 			attrsum += 2 * NFSX_UNSIGNED;
3150 			break;
3151 		default:
3152 			nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3153 			/*
3154 			 * set bitpos so we drop out of the loop.
3155 			 */
3156 			bitpos = NFSATTRBIT_MAX;
3157 			break;
3158 		}
3159 	}
3160 
3161 	/*
3162 	 * some clients pad the attrlist, so we need to skip over the
3163 	 * padding.
3164 	 */
3165 	if (attrsum > attrsize) {
3166 		error = NFSERR_BADXDR;
3167 	} else {
3168 		attrsize = NFSM_RNDUP(attrsize);
3169 		if (attrsum < attrsize)
3170 			error = nfsm_advance(nd, attrsize - attrsum, -1);
3171 	}
3172 nfsmout:
3173 	NFSEXITCODE2(error, nd);
3174 	return (error);
3175 }
3176 
3177 /*
3178  * Check/setup export credentials.
3179  */
3180 int
3181 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3182     struct ucred *credanon, bool testsec)
3183 {
3184 	int error;
3185 
3186 	/*
3187 	 * Check/setup credentials.
3188 	 */
3189 	if (nd->nd_flag & ND_GSS)
3190 		exp->nes_exflag &= ~MNT_EXPORTANON;
3191 
3192 	/*
3193 	 * Check to see if the operation is allowed for this security flavor.
3194 	 */
3195 	error = 0;
3196 	if (testsec) {
3197 		error = nfsvno_testexp(nd, exp);
3198 		if (error != 0)
3199 			goto out;
3200 	}
3201 
3202 	/*
3203 	 * Check to see if the file system is exported V4 only.
3204 	 */
3205 	if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3206 		error = NFSERR_PROGNOTV4;
3207 		goto out;
3208 	}
3209 
3210 	/*
3211 	 * Now, map the user credentials.
3212 	 * (Note that ND_AUTHNONE will only be set for an NFSv3
3213 	 *  Fsinfo RPC. If set for anything else, this code might need
3214 	 *  to change.)
3215 	 */
3216 	if (NFSVNO_EXPORTED(exp)) {
3217 		if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3218 		     NFSVNO_EXPORTANON(exp) ||
3219 		     (nd->nd_flag & ND_AUTHNONE) != 0) {
3220 			nd->nd_cred->cr_uid = credanon->cr_uid;
3221 			nd->nd_cred->cr_gid = credanon->cr_gid;
3222 			crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3223 			    credanon->cr_groups);
3224 		} else if ((nd->nd_flag & ND_GSS) == 0) {
3225 			/*
3226 			 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3227 			 * if there is a replacement credential with a group
3228 			 * list set up by "nfsuserd -manage-gids".
3229 			 * If there is no replacement, nfsrv_getgrpscred()
3230 			 * simply returns its argument.
3231 			 */
3232 			nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3233 		}
3234 	}
3235 
3236 out:
3237 	NFSEXITCODE2(error, nd);
3238 	return (error);
3239 }
3240 
3241 /*
3242  * Check exports.
3243  */
3244 int
3245 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3246     struct ucred **credp)
3247 {
3248 	int error;
3249 
3250 	error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3251 	    &exp->nes_numsecflavor, exp->nes_secflavors);
3252 	if (error) {
3253 		if (nfs_rootfhset) {
3254 			exp->nes_exflag = 0;
3255 			exp->nes_numsecflavor = 0;
3256 			error = 0;
3257 		}
3258 	} else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3259 	    MAXSECFLAVORS) {
3260 		printf("nfsvno_checkexp: numsecflavors out of range\n");
3261 		exp->nes_numsecflavor = 0;
3262 		error = EACCES;
3263 	}
3264 	NFSEXITCODE(error);
3265 	return (error);
3266 }
3267 
3268 /*
3269  * Get a vnode for a file handle and export stuff.
3270  */
3271 int
3272 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3273     int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3274     struct ucred **credp)
3275 {
3276 	int error;
3277 
3278 	*credp = NULL;
3279 	exp->nes_numsecflavor = 0;
3280 	error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3281 	if (error != 0)
3282 		/* Make sure the server replies ESTALE to the client. */
3283 		error = ESTALE;
3284 	if (nam && !error) {
3285 		error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3286 		    &exp->nes_numsecflavor, exp->nes_secflavors);
3287 		if (error) {
3288 			if (nfs_rootfhset) {
3289 				exp->nes_exflag = 0;
3290 				exp->nes_numsecflavor = 0;
3291 				error = 0;
3292 			} else {
3293 				vput(*vpp);
3294 			}
3295 		} else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3296 		    MAXSECFLAVORS) {
3297 			printf("nfsvno_fhtovp: numsecflavors out of range\n");
3298 			exp->nes_numsecflavor = 0;
3299 			error = EACCES;
3300 			vput(*vpp);
3301 		}
3302 	}
3303 	NFSEXITCODE(error);
3304 	return (error);
3305 }
3306 
3307 /*
3308  * nfsd_fhtovp() - convert a fh to a vnode ptr
3309  * 	- look up fsid in mount list (if not found ret error)
3310  *	- get vp and export rights by calling nfsvno_fhtovp()
3311  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3312  *	  for AUTH_SYS
3313  *	- if mpp != NULL, return the mount point so that it can
3314  *	  be used for vn_finished_write() by the caller
3315  */
3316 void
3317 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3318     struct vnode **vpp, struct nfsexstuff *exp,
3319     struct mount **mpp, int startwrite, int nextop)
3320 {
3321 	struct mount *mp, *mpw;
3322 	struct ucred *credanon;
3323 	fhandle_t *fhp;
3324 	int error;
3325 
3326 	if (mpp != NULL)
3327 		*mpp = NULL;
3328 	*vpp = NULL;
3329 	fhp = (fhandle_t *)nfp->nfsrvfh_data;
3330 	mp = vfs_busyfs(&fhp->fh_fsid);
3331 	if (mp == NULL) {
3332 		nd->nd_repstat = ESTALE;
3333 		goto out;
3334 	}
3335 
3336 	if (startwrite) {
3337 		mpw = mp;
3338 		error = vn_start_write(NULL, &mpw, V_WAIT);
3339 		if (error != 0) {
3340 			mpw = NULL;
3341 			vfs_unbusy(mp);
3342 			nd->nd_repstat = ESTALE;
3343 			goto out;
3344 		}
3345 		if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3346 			lktype = LK_EXCLUSIVE;
3347 	} else
3348 		mpw = NULL;
3349 
3350 	nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3351 	    &credanon);
3352 	vfs_unbusy(mp);
3353 
3354 	/*
3355 	 * For NFSv4 without a pseudo root fs, unexported file handles
3356 	 * can be returned, so that Lookup works everywhere.
3357 	 */
3358 	if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3359 	    !(nd->nd_flag & ND_NFSV4)) {
3360 		vput(*vpp);
3361 		*vpp = NULL;
3362 		nd->nd_repstat = EACCES;
3363 	}
3364 
3365 	/*
3366 	 * Personally, I've never seen any point in requiring a
3367 	 * reserved port#, since only in the rare case where the
3368 	 * clients are all boxes with secure system privileges,
3369 	 * does it provide any enhanced security, but... some people
3370 	 * believe it to be useful and keep putting this code back in.
3371 	 * (There is also some "security checker" out there that
3372 	 *  complains if the nfs server doesn't enforce this.)
3373 	 * However, note the following:
3374 	 * RFC3530 (NFSv4) specifies that a reserved port# not be
3375 	 *	required.
3376 	 * RFC2623 recommends that, if a reserved port# is checked for,
3377 	 *	that there be a way to turn that off--> ifdef'd.
3378 	 */
3379 #ifdef NFS_REQRSVPORT
3380 	if (!nd->nd_repstat) {
3381 		struct sockaddr_in *saddr;
3382 		struct sockaddr_in6 *saddr6;
3383 
3384 		saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3385 		saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3386 		if (!(nd->nd_flag & ND_NFSV4) &&
3387 		    ((saddr->sin_family == AF_INET &&
3388 		      ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3389 		     (saddr6->sin6_family == AF_INET6 &&
3390 		      ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3391 			vput(*vpp);
3392 			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3393 		}
3394 	}
3395 #endif	/* NFS_REQRSVPORT */
3396 
3397 	/*
3398 	 * Check/setup credentials.
3399 	 */
3400 	if (!nd->nd_repstat) {
3401 		nd->nd_saveduid = nd->nd_cred->cr_uid;
3402 		nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3403 		    nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3404 		if (nd->nd_repstat)
3405 			vput(*vpp);
3406 	}
3407 	if (credanon != NULL)
3408 		crfree(credanon);
3409 	if (nd->nd_repstat) {
3410 		vn_finished_write(mpw);
3411 		*vpp = NULL;
3412 	} else if (mpp != NULL) {
3413 		*mpp = mpw;
3414 	}
3415 
3416 out:
3417 	NFSEXITCODE2(0, nd);
3418 }
3419 
3420 /*
3421  * glue for fp.
3422  */
3423 static int
3424 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3425 {
3426 	struct filedesc *fdp;
3427 	struct file *fp;
3428 	int error = 0;
3429 
3430 	fdp = p->td_proc->p_fd;
3431 	if (fd < 0 || fd >= fdp->fd_nfiles ||
3432 	    (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3433 		error = EBADF;
3434 		goto out;
3435 	}
3436 	*fpp = fp;
3437 
3438 out:
3439 	NFSEXITCODE(error);
3440 	return (error);
3441 }
3442 
3443 /*
3444  * Called from nfssvc() to update the exports list. Just call
3445  * vfs_export(). This has to be done, since the v4 root fake fs isn't
3446  * in the mount list.
3447  */
3448 int
3449 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3450 {
3451 	struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3452 	int error = 0;
3453 	struct nameidata nd;
3454 	fhandle_t fh;
3455 
3456 	error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3457 	if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3458 		nfs_rootfhset = 0;
3459 	else if (error == 0) {
3460 		if (nfsexargp->fspec == NULL) {
3461 			error = EPERM;
3462 			goto out;
3463 		}
3464 		/*
3465 		 * If fspec != NULL, this is the v4root path.
3466 		 */
3467 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec);
3468 		if ((error = namei(&nd)) != 0)
3469 			goto out;
3470 		error = nfsvno_getfh(nd.ni_vp, &fh, p);
3471 		vrele(nd.ni_vp);
3472 		if (!error) {
3473 			nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3474 			NFSBCOPY((caddr_t)&fh,
3475 			    nfs_rootfh.nfsrvfh_data,
3476 			    sizeof (fhandle_t));
3477 			nfs_rootfhset = 1;
3478 		}
3479 	}
3480 
3481 out:
3482 	NFSEXITCODE(error);
3483 	return (error);
3484 }
3485 
3486 /*
3487  * This function needs to test to see if the system is near its limit
3488  * for memory allocation via malloc() or mget() and return True iff
3489  * either of these resources are near their limit.
3490  * XXX (For now, this is just a stub.)
3491  */
3492 int nfsrv_testmalloclimit = 0;
3493 int
3494 nfsrv_mallocmget_limit(void)
3495 {
3496 	static int printmesg = 0;
3497 	static int testval = 1;
3498 
3499 	if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3500 		if ((printmesg++ % 100) == 0)
3501 			printf("nfsd: malloc/mget near limit\n");
3502 		return (1);
3503 	}
3504 	return (0);
3505 }
3506 
3507 /*
3508  * BSD specific initialization of a mount point.
3509  */
3510 void
3511 nfsd_mntinit(void)
3512 {
3513 	static int inited = 0;
3514 
3515 	if (inited)
3516 		return;
3517 	inited = 1;
3518 	nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3519 	TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3520 	TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3521 	nfsv4root_mnt.mnt_export = NULL;
3522 	TAILQ_INIT(&nfsv4root_opt);
3523 	TAILQ_INIT(&nfsv4root_newopt);
3524 	nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3525 	nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3526 	nfsv4root_mnt.mnt_nvnodelistsize = 0;
3527 	nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3528 }
3529 
3530 static void
3531 nfsd_timer(void *arg)
3532 {
3533 
3534 	nfsrv_servertimer();
3535 	callout_reset_sbt(&nfsd_callout, SBT_1S, SBT_1S, nfsd_timer, NULL, 0);
3536 }
3537 
3538 /*
3539  * Get a vnode for a file handle, without checking exports, etc.
3540  */
3541 struct vnode *
3542 nfsvno_getvp(fhandle_t *fhp)
3543 {
3544 	struct mount *mp;
3545 	struct vnode *vp;
3546 	int error;
3547 
3548 	mp = vfs_busyfs(&fhp->fh_fsid);
3549 	if (mp == NULL)
3550 		return (NULL);
3551 	error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3552 	vfs_unbusy(mp);
3553 	if (error)
3554 		return (NULL);
3555 	return (vp);
3556 }
3557 
3558 /*
3559  * Do a local VOP_ADVLOCK().
3560  */
3561 int
3562 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3563     u_int64_t end, struct thread *td)
3564 {
3565 	int error = 0;
3566 	struct flock fl;
3567 	u_int64_t tlen;
3568 
3569 	if (nfsrv_dolocallocks == 0)
3570 		goto out;
3571 	ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3572 
3573 	fl.l_whence = SEEK_SET;
3574 	fl.l_type = ftype;
3575 	fl.l_start = (off_t)first;
3576 	if (end == NFS64BITSSET) {
3577 		fl.l_len = 0;
3578 	} else {
3579 		tlen = end - first;
3580 		fl.l_len = (off_t)tlen;
3581 	}
3582 	/*
3583 	 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3584 	 * values for all calls, so that all locks will be held by the
3585 	 * nfsd server. (The nfsd server handles conflicts between the
3586 	 * various clients.)
3587 	 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3588 	 * bytes, so it can't be put in l_sysid.
3589 	 */
3590 	if (nfsv4_sysid == 0)
3591 		nfsv4_sysid = nlm_acquire_next_sysid();
3592 	fl.l_pid = (pid_t)0;
3593 	fl.l_sysid = (int)nfsv4_sysid;
3594 
3595 	if (ftype == F_UNLCK)
3596 		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3597 		    (F_POSIX | F_REMOTE));
3598 	else
3599 		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3600 		    (F_POSIX | F_REMOTE));
3601 
3602 out:
3603 	NFSEXITCODE(error);
3604 	return (error);
3605 }
3606 
3607 /*
3608  * Check the nfsv4 root exports.
3609  */
3610 int
3611 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3612 {
3613 	struct ucred *credanon;
3614 	int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3615 	uint64_t exflags;
3616 
3617 	error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3618 	    &credanon, &numsecflavor, secflavors);
3619 	if (error) {
3620 		error = NFSERR_PROGUNAVAIL;
3621 		goto out;
3622 	}
3623 	if (credanon != NULL)
3624 		crfree(credanon);
3625 	for (i = 0; i < numsecflavor; i++) {
3626 		if (secflavors[i] == AUTH_SYS)
3627 			nd->nd_flag |= ND_EXAUTHSYS;
3628 		else if (secflavors[i] == RPCSEC_GSS_KRB5)
3629 			nd->nd_flag |= ND_EXGSS;
3630 		else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3631 			nd->nd_flag |= ND_EXGSSINTEGRITY;
3632 		else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3633 			nd->nd_flag |= ND_EXGSSPRIVACY;
3634 	}
3635 
3636 	/* And set ND_EXxx flags for TLS. */
3637 	if ((exflags & MNT_EXTLS) != 0) {
3638 		nd->nd_flag |= ND_EXTLS;
3639 		if ((exflags & MNT_EXTLSCERT) != 0)
3640 			nd->nd_flag |= ND_EXTLSCERT;
3641 		if ((exflags & MNT_EXTLSCERTUSER) != 0)
3642 			nd->nd_flag |= ND_EXTLSCERTUSER;
3643 	}
3644 
3645 out:
3646 	NFSEXITCODE(error);
3647 	return (error);
3648 }
3649 
3650 /*
3651  * Nfs server pseudo system call for the nfsd's
3652  */
3653 /*
3654  * MPSAFE
3655  */
3656 static int
3657 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3658 {
3659 	struct file *fp;
3660 	struct nfsd_addsock_args sockarg;
3661 	struct nfsd_nfsd_args nfsdarg;
3662 	struct nfsd_nfsd_oargs onfsdarg;
3663 	struct nfsd_pnfsd_args pnfsdarg;
3664 	struct vnode *vp, *nvp, *curdvp;
3665 	struct pnfsdsfile *pf;
3666 	struct nfsdevice *ds, *fds;
3667 	cap_rights_t rights;
3668 	int buflen, error, ret;
3669 	char *buf, *cp, *cp2, *cp3;
3670 	char fname[PNFS_FILENAME_LEN + 1];
3671 
3672 	if (uap->flag & NFSSVC_NFSDADDSOCK) {
3673 		error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3674 		if (error)
3675 			goto out;
3676 		/*
3677 		 * Since we don't know what rights might be required,
3678 		 * pretend that we need them all. It is better to be too
3679 		 * careful than too reckless.
3680 		 */
3681 		error = fget(td, sockarg.sock,
3682 		    cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
3683 		if (error != 0)
3684 			goto out;
3685 		if (fp->f_type != DTYPE_SOCKET) {
3686 			fdrop(fp, td);
3687 			error = EPERM;
3688 			goto out;
3689 		}
3690 		error = nfsrvd_addsock(fp);
3691 		fdrop(fp, td);
3692 	} else if (uap->flag & NFSSVC_NFSDNFSD) {
3693 		if (uap->argp == NULL) {
3694 			error = EINVAL;
3695 			goto out;
3696 		}
3697 		if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3698 			error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3699 			if (error == 0) {
3700 				nfsdarg.principal = onfsdarg.principal;
3701 				nfsdarg.minthreads = onfsdarg.minthreads;
3702 				nfsdarg.maxthreads = onfsdarg.maxthreads;
3703 				nfsdarg.version = 1;
3704 				nfsdarg.addr = NULL;
3705 				nfsdarg.addrlen = 0;
3706 				nfsdarg.dnshost = NULL;
3707 				nfsdarg.dnshostlen = 0;
3708 				nfsdarg.dspath = NULL;
3709 				nfsdarg.dspathlen = 0;
3710 				nfsdarg.mdspath = NULL;
3711 				nfsdarg.mdspathlen = 0;
3712 				nfsdarg.mirrorcnt = 1;
3713 			}
3714 		} else
3715 			error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3716 		if (error)
3717 			goto out;
3718 		if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3719 		    nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3720 		    nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3721 		    nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3722 		    nfsdarg.mirrorcnt >= 1 &&
3723 		    nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3724 		    nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3725 		    nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3726 			NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3727 			    " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3728 			    nfsdarg.dspathlen, nfsdarg.dnshostlen,
3729 			    nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3730 			cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3731 			error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3732 			if (error != 0) {
3733 				free(cp, M_TEMP);
3734 				goto out;
3735 			}
3736 			cp[nfsdarg.addrlen] = '\0';	/* Ensure nul term. */
3737 			nfsdarg.addr = cp;
3738 			cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3739 			error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3740 			if (error != 0) {
3741 				free(nfsdarg.addr, M_TEMP);
3742 				free(cp, M_TEMP);
3743 				goto out;
3744 			}
3745 			cp[nfsdarg.dnshostlen] = '\0';	/* Ensure nul term. */
3746 			nfsdarg.dnshost = cp;
3747 			cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3748 			error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3749 			if (error != 0) {
3750 				free(nfsdarg.addr, M_TEMP);
3751 				free(nfsdarg.dnshost, M_TEMP);
3752 				free(cp, M_TEMP);
3753 				goto out;
3754 			}
3755 			cp[nfsdarg.dspathlen] = '\0';	/* Ensure nul term. */
3756 			nfsdarg.dspath = cp;
3757 			cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3758 			error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3759 			if (error != 0) {
3760 				free(nfsdarg.addr, M_TEMP);
3761 				free(nfsdarg.dnshost, M_TEMP);
3762 				free(nfsdarg.dspath, M_TEMP);
3763 				free(cp, M_TEMP);
3764 				goto out;
3765 			}
3766 			cp[nfsdarg.mdspathlen] = '\0';	/* Ensure nul term. */
3767 			nfsdarg.mdspath = cp;
3768 		} else {
3769 			nfsdarg.addr = NULL;
3770 			nfsdarg.addrlen = 0;
3771 			nfsdarg.dnshost = NULL;
3772 			nfsdarg.dnshostlen = 0;
3773 			nfsdarg.dspath = NULL;
3774 			nfsdarg.dspathlen = 0;
3775 			nfsdarg.mdspath = NULL;
3776 			nfsdarg.mdspathlen = 0;
3777 			nfsdarg.mirrorcnt = 1;
3778 		}
3779 		nfsd_timer(NULL);
3780 		error = nfsrvd_nfsd(td, &nfsdarg);
3781 		free(nfsdarg.addr, M_TEMP);
3782 		free(nfsdarg.dnshost, M_TEMP);
3783 		free(nfsdarg.dspath, M_TEMP);
3784 		free(nfsdarg.mdspath, M_TEMP);
3785 	} else if (uap->flag & NFSSVC_PNFSDS) {
3786 		error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3787 		if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3788 		    pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3789 			cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3790 			error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3791 			    NULL);
3792 			if (error == 0)
3793 				error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3794 			free(cp, M_TEMP);
3795 		} else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3796 			cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3797 			buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3798 			buf = malloc(buflen, M_TEMP, M_WAITOK);
3799 			error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3800 			    NULL);
3801 			NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3802 			if (error == 0 && pnfsdarg.dspath != NULL) {
3803 				cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3804 				error = copyinstr(pnfsdarg.dspath, cp2,
3805 				    PATH_MAX + 1, NULL);
3806 				NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3807 				    error);
3808 			} else
3809 				cp2 = NULL;
3810 			if (error == 0 && pnfsdarg.curdspath != NULL) {
3811 				cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3812 				error = copyinstr(pnfsdarg.curdspath, cp3,
3813 				    PATH_MAX + 1, NULL);
3814 				NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3815 				    error);
3816 			} else
3817 				cp3 = NULL;
3818 			curdvp = NULL;
3819 			fds = NULL;
3820 			if (error == 0)
3821 				error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3822 				    &buflen, fname, td, &vp, &nvp, &pf, &ds,
3823 				    &fds);
3824 			NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3825 			if (error == 0) {
3826 				if (pf->dsf_dir >= nfsrv_dsdirsize) {
3827 					printf("copymr: dsdir out of range\n");
3828 					pf->dsf_dir = 0;
3829 				}
3830 				NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3831 				error = nfsrv_copymr(vp, nvp,
3832 				    ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3833 				    (struct pnfsdsfile *)buf,
3834 				    buflen / sizeof(*pf), td->td_ucred, td);
3835 				vput(vp);
3836 				vput(nvp);
3837 				if (fds != NULL && error == 0) {
3838 					curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3839 					ret = vn_lock(curdvp, LK_EXCLUSIVE);
3840 					if (ret == 0) {
3841 						nfsrv_dsremove(curdvp, fname,
3842 						    td->td_ucred, td);
3843 						NFSVOPUNLOCK(curdvp);
3844 					}
3845 				}
3846 				NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3847 			}
3848 			free(cp, M_TEMP);
3849 			free(cp2, M_TEMP);
3850 			free(cp3, M_TEMP);
3851 			free(buf, M_TEMP);
3852 		}
3853 	} else {
3854 		error = nfssvc_srvcall(td, uap, td->td_ucred);
3855 	}
3856 
3857 out:
3858 	NFSEXITCODE(error);
3859 	return (error);
3860 }
3861 
3862 static int
3863 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3864 {
3865 	struct nfsex_args export;
3866 	struct nfsex_oldargs oexp;
3867 	struct file *fp = NULL;
3868 	int stablefd, i, len;
3869 	struct nfsd_clid adminrevoke;
3870 	struct nfsd_dumplist dumplist;
3871 	struct nfsd_dumpclients *dumpclients;
3872 	struct nfsd_dumplocklist dumplocklist;
3873 	struct nfsd_dumplocks *dumplocks;
3874 	struct nameidata nd;
3875 	vnode_t vp;
3876 	int error = EINVAL, igotlock;
3877 	struct proc *procp;
3878 	gid_t *grps;
3879 	static int suspend_nfsd = 0;
3880 
3881 	if (uap->flag & NFSSVC_PUBLICFH) {
3882 		NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3883 		    sizeof (fhandle_t));
3884 		error = copyin(uap->argp,
3885 		    &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3886 		if (!error)
3887 			nfs_pubfhset = 1;
3888 	} else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3889 	    (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3890 		error = copyin(uap->argp,(caddr_t)&export,
3891 		    sizeof (struct nfsex_args));
3892 		if (!error) {
3893 			grps = NULL;
3894 			if (export.export.ex_ngroups > NGROUPS_MAX ||
3895 			    export.export.ex_ngroups < 0)
3896 				error = EINVAL;
3897 			else if (export.export.ex_ngroups > 0) {
3898 				grps = malloc(export.export.ex_ngroups *
3899 				    sizeof(gid_t), M_TEMP, M_WAITOK);
3900 				error = copyin(export.export.ex_groups, grps,
3901 				    export.export.ex_ngroups * sizeof(gid_t));
3902 				export.export.ex_groups = grps;
3903 			} else
3904 				export.export.ex_groups = NULL;
3905 			if (!error)
3906 				error = nfsrv_v4rootexport(&export, cred, p);
3907 			free(grps, M_TEMP);
3908 		}
3909 	} else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3910 	    NFSSVC_V4ROOTEXPORT) {
3911 		error = copyin(uap->argp,(caddr_t)&oexp,
3912 		    sizeof (struct nfsex_oldargs));
3913 		if (!error) {
3914 			memset(&export.export, 0, sizeof(export.export));
3915 			export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3916 			export.export.ex_root = oexp.export.ex_root;
3917 			export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3918 			export.export.ex_ngroups =
3919 			    oexp.export.ex_anon.cr_ngroups;
3920 			export.export.ex_groups = NULL;
3921 			if (export.export.ex_ngroups > XU_NGROUPS ||
3922 			    export.export.ex_ngroups < 0)
3923 				error = EINVAL;
3924 			else if (export.export.ex_ngroups > 0) {
3925 				export.export.ex_groups = malloc(
3926 				    export.export.ex_ngroups * sizeof(gid_t),
3927 				    M_TEMP, M_WAITOK);
3928 				for (i = 0; i < export.export.ex_ngroups; i++)
3929 					export.export.ex_groups[i] =
3930 					    oexp.export.ex_anon.cr_groups[i];
3931 			}
3932 			export.export.ex_addr = oexp.export.ex_addr;
3933 			export.export.ex_addrlen = oexp.export.ex_addrlen;
3934 			export.export.ex_mask = oexp.export.ex_mask;
3935 			export.export.ex_masklen = oexp.export.ex_masklen;
3936 			export.export.ex_indexfile = oexp.export.ex_indexfile;
3937 			export.export.ex_numsecflavors =
3938 			    oexp.export.ex_numsecflavors;
3939 			if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3940 			    export.export.ex_numsecflavors < 0)
3941 				error = EINVAL;
3942 			else {
3943 				for (i = 0; i < export.export.ex_numsecflavors;
3944 				    i++)
3945 					export.export.ex_secflavors[i] =
3946 					    oexp.export.ex_secflavors[i];
3947 			}
3948 			export.fspec = oexp.fspec;
3949 			if (error == 0)
3950 				error = nfsrv_v4rootexport(&export, cred, p);
3951 			free(export.export.ex_groups, M_TEMP);
3952 		}
3953 	} else if (uap->flag & NFSSVC_NOPUBLICFH) {
3954 		nfs_pubfhset = 0;
3955 		error = 0;
3956 	} else if (uap->flag & NFSSVC_STABLERESTART) {
3957 		error = copyin(uap->argp, (caddr_t)&stablefd,
3958 		    sizeof (int));
3959 		if (!error)
3960 			error = fp_getfvp(p, stablefd, &fp, &vp);
3961 		if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3962 			error = EBADF;
3963 		if (!error && newnfs_numnfsd != 0)
3964 			error = EPERM;
3965 		if (!error) {
3966 			nfsrv_stablefirst.nsf_fp = fp;
3967 			nfsrv_setupstable(p);
3968 		}
3969 	} else if (uap->flag & NFSSVC_ADMINREVOKE) {
3970 		error = copyin(uap->argp, (caddr_t)&adminrevoke,
3971 		    sizeof (struct nfsd_clid));
3972 		if (!error)
3973 			error = nfsrv_adminrevoke(&adminrevoke, p);
3974 	} else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3975 		error = copyin(uap->argp, (caddr_t)&dumplist,
3976 		    sizeof (struct nfsd_dumplist));
3977 		if (!error && (dumplist.ndl_size < 1 ||
3978 			dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3979 			error = EPERM;
3980 		if (!error) {
3981 		    len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3982 		    dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3983 		    nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3984 		    error = copyout(dumpclients, dumplist.ndl_list, len);
3985 		    free(dumpclients, M_TEMP);
3986 		}
3987 	} else if (uap->flag & NFSSVC_DUMPLOCKS) {
3988 		error = copyin(uap->argp, (caddr_t)&dumplocklist,
3989 		    sizeof (struct nfsd_dumplocklist));
3990 		if (!error && (dumplocklist.ndllck_size < 1 ||
3991 			dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3992 			error = EPERM;
3993 		if (!error)
3994 			error = nfsrv_lookupfilename(&nd,
3995 				dumplocklist.ndllck_fname, p);
3996 		if (!error) {
3997 			len = sizeof (struct nfsd_dumplocks) *
3998 				dumplocklist.ndllck_size;
3999 			dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4000 			nfsrv_dumplocks(nd.ni_vp, dumplocks,
4001 			    dumplocklist.ndllck_size, p);
4002 			vput(nd.ni_vp);
4003 			error = copyout(dumplocks, dumplocklist.ndllck_list,
4004 			    len);
4005 			free(dumplocks, M_TEMP);
4006 		}
4007 	} else if (uap->flag & NFSSVC_BACKUPSTABLE) {
4008 		procp = p->td_proc;
4009 		PROC_LOCK(procp);
4010 		nfsd_master_pid = procp->p_pid;
4011 		bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
4012 		nfsd_master_start = procp->p_stats->p_start;
4013 		nfsd_master_proc = procp;
4014 		PROC_UNLOCK(procp);
4015 	} else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4016 		NFSLOCKV4ROOTMUTEX();
4017 		if (suspend_nfsd == 0) {
4018 			/* Lock out all nfsd threads */
4019 			do {
4020 				igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
4021 				    NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
4022 			} while (igotlock == 0 && suspend_nfsd == 0);
4023 			suspend_nfsd = 1;
4024 		}
4025 		NFSUNLOCKV4ROOTMUTEX();
4026 		error = 0;
4027 	} else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4028 		NFSLOCKV4ROOTMUTEX();
4029 		if (suspend_nfsd != 0) {
4030 			nfsv4_unlock(&nfsd_suspend_lock, 0);
4031 			suspend_nfsd = 0;
4032 		}
4033 		NFSUNLOCKV4ROOTMUTEX();
4034 		error = 0;
4035 	}
4036 
4037 	NFSEXITCODE(error);
4038 	return (error);
4039 }
4040 
4041 /*
4042  * Check exports.
4043  * Returns 0 if ok, 1 otherwise.
4044  */
4045 int
4046 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4047 {
4048 	int i;
4049 
4050 	if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4051 	    (NFSVNO_EXTLSCERT(exp) &&
4052 	     (nd->nd_flag & ND_TLSCERT) == 0) ||
4053 	    (NFSVNO_EXTLSCERTUSER(exp) &&
4054 	     (nd->nd_flag & ND_TLSCERTUSER) == 0)) {
4055 		if ((nd->nd_flag & ND_NFSV4) != 0)
4056 			return (NFSERR_WRONGSEC);
4057 #ifdef notnow
4058 		/* There is currently no auth_stat for this. */
4059 		else if ((nd->nd_flag & ND_TLS) == 0)
4060 			return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4061 		else
4062 			return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4063 #endif
4064 		else
4065 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4066 	}
4067 
4068 	/*
4069 	 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use
4070 	 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
4071 	 */
4072 	if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO)
4073 		return (0);
4074 
4075 	/*
4076 	 * This seems odd, but allow the case where the security flavor
4077 	 * list is empty. This happens when NFSv4 is traversing non-exported
4078 	 * file systems. Exported file systems should always have a non-empty
4079 	 * security flavor list.
4080 	 */
4081 	if (exp->nes_numsecflavor == 0)
4082 		return (0);
4083 
4084 	for (i = 0; i < exp->nes_numsecflavor; i++) {
4085 		/*
4086 		 * The tests for privacy and integrity must be first,
4087 		 * since ND_GSS is set for everything but AUTH_SYS.
4088 		 */
4089 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4090 		    (nd->nd_flag & ND_GSSPRIVACY))
4091 			return (0);
4092 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4093 		    (nd->nd_flag & ND_GSSINTEGRITY))
4094 			return (0);
4095 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4096 		    (nd->nd_flag & ND_GSS))
4097 			return (0);
4098 		if (exp->nes_secflavors[i] == AUTH_SYS &&
4099 		    (nd->nd_flag & ND_GSS) == 0)
4100 			return (0);
4101 	}
4102 	if ((nd->nd_flag & ND_NFSV4) != 0)
4103 		return (NFSERR_WRONGSEC);
4104 	return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4105 }
4106 
4107 /*
4108  * Calculate a hash value for the fid in a file handle.
4109  */
4110 uint32_t
4111 nfsrv_hashfh(fhandle_t *fhp)
4112 {
4113 	uint32_t hashval;
4114 
4115 	hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4116 	return (hashval);
4117 }
4118 
4119 /*
4120  * Calculate a hash value for the sessionid.
4121  */
4122 uint32_t
4123 nfsrv_hashsessionid(uint8_t *sessionid)
4124 {
4125 	uint32_t hashval;
4126 
4127 	hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4128 	return (hashval);
4129 }
4130 
4131 /*
4132  * Signal the userland master nfsd to backup the stable restart file.
4133  */
4134 void
4135 nfsrv_backupstable(void)
4136 {
4137 	struct proc *procp;
4138 
4139 	if (nfsd_master_proc != NULL) {
4140 		procp = pfind(nfsd_master_pid);
4141 		/* Try to make sure it is the correct process. */
4142 		if (procp == nfsd_master_proc &&
4143 		    procp->p_stats->p_start.tv_sec ==
4144 		    nfsd_master_start.tv_sec &&
4145 		    procp->p_stats->p_start.tv_usec ==
4146 		    nfsd_master_start.tv_usec &&
4147 		    strcmp(procp->p_comm, nfsd_master_comm) == 0)
4148 			kern_psignal(procp, SIGUSR2);
4149 		else
4150 			nfsd_master_proc = NULL;
4151 
4152 		if (procp != NULL)
4153 			PROC_UNLOCK(procp);
4154 	}
4155 }
4156 
4157 /*
4158  * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4159  * The arguments are in a structure, so that they can be passed through
4160  * taskqueue for a kernel process to execute this function.
4161  */
4162 struct nfsrvdscreate {
4163 	int			done;
4164 	int			inprog;
4165 	struct task		tsk;
4166 	struct ucred		*tcred;
4167 	struct vnode		*dvp;
4168 	NFSPROC_T		*p;
4169 	struct pnfsdsfile	*pf;
4170 	int			err;
4171 	fhandle_t		fh;
4172 	struct vattr		va;
4173 	struct vattr		createva;
4174 };
4175 
4176 int
4177 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4178     fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4179     char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4180 {
4181 	struct vnode *nvp;
4182 	struct nameidata named;
4183 	struct vattr va;
4184 	char *bufp;
4185 	u_long *hashp;
4186 	struct nfsnode *np;
4187 	struct nfsmount *nmp;
4188 	int error;
4189 
4190 	NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4191 	    LOCKPARENT | LOCKLEAF | NOCACHE);
4192 	nfsvno_setpathbuf(&named, &bufp, &hashp);
4193 	named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4194 	named.ni_cnd.cn_nameptr = bufp;
4195 	if (fnamep != NULL) {
4196 		strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4197 		named.ni_cnd.cn_namelen = strlen(bufp);
4198 	} else
4199 		named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4200 	NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4201 
4202 	/* Create the date file in the DS mount. */
4203 	error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4204 	if (error == 0) {
4205 		error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4206 		vref(dvp);
4207 		VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4208 		if (error == 0) {
4209 			/* Set the ownership of the file. */
4210 			error = VOP_SETATTR(nvp, nvap, tcred);
4211 			NFSD_DEBUG(4, "nfsrv_dscreate:"
4212 			    " setattr-uid=%d\n", error);
4213 			if (error != 0)
4214 				vput(nvp);
4215 		}
4216 		if (error != 0)
4217 			printf("pNFS: pnfscreate failed=%d\n", error);
4218 	} else
4219 		printf("pNFS: pnfscreate vnlock=%d\n", error);
4220 	if (error == 0) {
4221 		np = VTONFS(nvp);
4222 		nmp = VFSTONFS(nvp->v_mount);
4223 		if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4224 		    != 0 || nmp->nm_nam->sa_len > sizeof(
4225 		    struct sockaddr_in6) ||
4226 		    np->n_fhp->nfh_len != NFSX_MYFH) {
4227 			printf("Bad DS file: fstype=%s salen=%d"
4228 			    " fhlen=%d\n",
4229 			    nvp->v_mount->mnt_vfc->vfc_name,
4230 			    nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4231 			error = ENOENT;
4232 		}
4233 
4234 		/* Set extattrs for the DS on the MDS file. */
4235 		if (error == 0) {
4236 			if (dsa != NULL) {
4237 				error = VOP_GETATTR(nvp, &va, tcred);
4238 				if (error == 0) {
4239 					dsa->dsa_filerev = va.va_filerev;
4240 					dsa->dsa_size = va.va_size;
4241 					dsa->dsa_atime = va.va_atime;
4242 					dsa->dsa_mtime = va.va_mtime;
4243 					dsa->dsa_bytes = va.va_bytes;
4244 				}
4245 			}
4246 			if (error == 0) {
4247 				NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4248 				    NFSX_MYFH);
4249 				NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4250 				    nmp->nm_nam->sa_len);
4251 				NFSBCOPY(named.ni_cnd.cn_nameptr,
4252 				    pf->dsf_filename,
4253 				    sizeof(pf->dsf_filename));
4254 			}
4255 		} else
4256 			printf("pNFS: pnfscreate can't get DS"
4257 			    " attr=%d\n", error);
4258 		if (nvpp != NULL && error == 0)
4259 			*nvpp = nvp;
4260 		else
4261 			vput(nvp);
4262 	}
4263 	nfsvno_relpathbuf(&named);
4264 	return (error);
4265 }
4266 
4267 /*
4268  * Start up the thread that will execute nfsrv_dscreate().
4269  */
4270 static void
4271 start_dscreate(void *arg, int pending)
4272 {
4273 	struct nfsrvdscreate *dsc;
4274 
4275 	dsc = (struct nfsrvdscreate *)arg;
4276 	dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4277 	    dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4278 	dsc->done = 1;
4279 	NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4280 }
4281 
4282 /*
4283  * Create a pNFS data file on the Data Server(s).
4284  */
4285 static void
4286 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4287     NFSPROC_T *p)
4288 {
4289 	struct nfsrvdscreate *dsc, *tdsc = NULL;
4290 	struct nfsdevice *ds, *tds, *fds;
4291 	struct mount *mp;
4292 	struct pnfsdsfile *pf, *tpf;
4293 	struct pnfsdsattr dsattr;
4294 	struct vattr va;
4295 	struct vnode *dvp[NFSDEV_MAXMIRRORS];
4296 	struct nfsmount *nmp;
4297 	fhandle_t fh;
4298 	uid_t vauid;
4299 	gid_t vagid;
4300 	u_short vamode;
4301 	struct ucred *tcred;
4302 	int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4303 	int failpos, timo;
4304 
4305 	/* Get a DS server directory in a round-robin order. */
4306 	mirrorcnt = 1;
4307 	mp = vp->v_mount;
4308 	ds = fds = NULL;
4309 	NFSDDSLOCK();
4310 	/*
4311 	 * Search for the first entry that handles this MDS fs, but use the
4312 	 * first entry for all MDS fs's otherwise.
4313 	 */
4314 	TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4315 		if (tds->nfsdev_nmp != NULL) {
4316 			if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4317 				ds = tds;
4318 			else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4319 			    &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4320 				ds = fds = tds;
4321 				break;
4322 			}
4323 		}
4324 	}
4325 	if (ds == NULL) {
4326 		NFSDDSUNLOCK();
4327 		NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4328 		return;
4329 	}
4330 	i = dsdir[0] = ds->nfsdev_nextdir;
4331 	ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4332 	dvp[0] = ds->nfsdev_dsdir[i];
4333 	tds = TAILQ_NEXT(ds, nfsdev_list);
4334 	if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4335 		TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4336 			if (tds->nfsdev_nmp != NULL &&
4337 			    ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4338 			     (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4339 			      fsidcmp(&mp->mnt_stat.f_fsid,
4340 			      &tds->nfsdev_mdsfsid) == 0))) {
4341 				dsdir[mirrorcnt] = i;
4342 				dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4343 				mirrorcnt++;
4344 				if (mirrorcnt >= nfsrv_maxpnfsmirror)
4345 					break;
4346 			}
4347 		}
4348 	}
4349 	/* Put at end of list to implement round-robin usage. */
4350 	TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4351 	TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4352 	NFSDDSUNLOCK();
4353 	dsc = NULL;
4354 	if (mirrorcnt > 1)
4355 		tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4356 		    M_WAITOK | M_ZERO);
4357 	tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4358 	    M_ZERO);
4359 
4360 	error = nfsvno_getfh(vp, &fh, p);
4361 	if (error == 0)
4362 		error = VOP_GETATTR(vp, &va, cred);
4363 	if (error == 0) {
4364 		/* Set the attributes for "vp" to Setattr the DS vp. */
4365 		vauid = va.va_uid;
4366 		vagid = va.va_gid;
4367 		vamode = va.va_mode;
4368 		VATTR_NULL(&va);
4369 		va.va_uid = vauid;
4370 		va.va_gid = vagid;
4371 		va.va_mode = vamode;
4372 		va.va_size = 0;
4373 	} else
4374 		printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4375 
4376 	NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4377 	    cred->cr_gid);
4378 	/* Make data file name based on FH. */
4379 	tcred = newnfs_getcred();
4380 
4381 	/*
4382 	 * Create the file on each DS mirror, using kernel process(es) for the
4383 	 * additional mirrors.
4384 	 */
4385 	failpos = -1;
4386 	for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4387 		tpf->dsf_dir = dsdir[i];
4388 		tdsc->tcred = tcred;
4389 		tdsc->p = p;
4390 		tdsc->pf = tpf;
4391 		tdsc->createva = *vap;
4392 		NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4393 		tdsc->va = va;
4394 		tdsc->dvp = dvp[i];
4395 		tdsc->done = 0;
4396 		tdsc->inprog = 0;
4397 		tdsc->err = 0;
4398 		ret = EIO;
4399 		if (nfs_pnfsiothreads != 0) {
4400 			ret = nfs_pnfsio(start_dscreate, tdsc);
4401 			NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4402 		}
4403 		if (ret != 0) {
4404 			ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4405 			    NULL, tcred, p, NULL);
4406 			if (ret != 0) {
4407 				KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4408 				    error));
4409 				if (failpos == -1 && nfsds_failerr(ret))
4410 					failpos = i;
4411 				else
4412 					error = ret;
4413 			}
4414 		}
4415 	}
4416 	if (error == 0) {
4417 		tpf->dsf_dir = dsdir[mirrorcnt - 1];
4418 		error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4419 		    &dsattr, NULL, tcred, p, NULL);
4420 		if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4421 			failpos = mirrorcnt - 1;
4422 			error = 0;
4423 		}
4424 	}
4425 	timo = hz / 50;		/* Wait for 20msec. */
4426 	if (timo < 1)
4427 		timo = 1;
4428 	/* Wait for kernel task(s) to complete. */
4429 	for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4430 		while (tdsc->inprog != 0 && tdsc->done == 0)
4431 			tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4432 		if (tdsc->err != 0) {
4433 			if (failpos == -1 && nfsds_failerr(tdsc->err))
4434 				failpos = i;
4435 			else if (error == 0)
4436 				error = tdsc->err;
4437 		}
4438 	}
4439 
4440 	/*
4441 	 * If failpos has been set, that mirror has failed, so it needs
4442 	 * to be disabled.
4443 	 */
4444 	if (failpos >= 0) {
4445 		nmp = VFSTONFS(dvp[failpos]->v_mount);
4446 		NFSLOCKMNT(nmp);
4447 		if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4448 		     NFSMNTP_CANCELRPCS)) == 0) {
4449 			nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4450 			NFSUNLOCKMNT(nmp);
4451 			ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4452 			NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4453 			    ds);
4454 			if (ds != NULL)
4455 				nfsrv_killrpcs(nmp);
4456 			NFSLOCKMNT(nmp);
4457 			nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4458 			wakeup(nmp);
4459 		}
4460 		NFSUNLOCKMNT(nmp);
4461 	}
4462 
4463 	NFSFREECRED(tcred);
4464 	if (error == 0) {
4465 		ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4466 
4467 		NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4468 		    mirrorcnt, nfsrv_maxpnfsmirror);
4469 		/*
4470 		 * For all mirrors that couldn't be created, fill in the
4471 		 * *pf structure, but with an IP address == 0.0.0.0.
4472 		 */
4473 		tpf = pf + mirrorcnt;
4474 		for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4475 			*tpf = *pf;
4476 			tpf->dsf_sin.sin_family = AF_INET;
4477 			tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4478 			tpf->dsf_sin.sin_addr.s_addr = 0;
4479 			tpf->dsf_sin.sin_port = 0;
4480 		}
4481 
4482 		error = vn_extattr_set(vp, IO_NODELOCKED,
4483 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4484 		    sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4485 		if (error == 0)
4486 			error = vn_extattr_set(vp, IO_NODELOCKED,
4487 			    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4488 			    sizeof(dsattr), (char *)&dsattr, p);
4489 		if (error != 0)
4490 			printf("pNFS: pnfscreate setextattr=%d\n",
4491 			    error);
4492 	} else
4493 		printf("pNFS: pnfscreate=%d\n", error);
4494 	free(pf, M_TEMP);
4495 	free(dsc, M_TEMP);
4496 }
4497 
4498 /*
4499  * Get the information needed to remove the pNFS Data Server file from the
4500  * Metadata file.  Upon success, ddvp is set non-NULL to the locked
4501  * DS directory vnode.  The caller must unlock *ddvp when done with it.
4502  */
4503 static void
4504 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4505     int *mirrorcntp, char *fname, fhandle_t *fhp)
4506 {
4507 	struct vattr va;
4508 	struct ucred *tcred;
4509 	char *buf;
4510 	int buflen, error;
4511 
4512 	dvpp[0] = NULL;
4513 	/* If not an exported regular file or not a pNFS server, just return. */
4514 	if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4515 	    nfsrv_devidcnt == 0)
4516 		return;
4517 
4518 	/* Check to see if this is the last hard link. */
4519 	tcred = newnfs_getcred();
4520 	error = VOP_GETATTR(vp, &va, tcred);
4521 	NFSFREECRED(tcred);
4522 	if (error != 0) {
4523 		printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4524 		return;
4525 	}
4526 	if (va.va_nlink > 1)
4527 		return;
4528 
4529 	error = nfsvno_getfh(vp, fhp, p);
4530 	if (error != 0) {
4531 		printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4532 		return;
4533 	}
4534 
4535 	buflen = 1024;
4536 	buf = malloc(buflen, M_TEMP, M_WAITOK);
4537 	/* Get the directory vnode for the DS mount and the file handle. */
4538 	error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4539 	    NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4540 	free(buf, M_TEMP);
4541 	if (error != 0)
4542 		printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4543 }
4544 
4545 /*
4546  * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4547  * The arguments are in a structure, so that they can be passed through
4548  * taskqueue for a kernel process to execute this function.
4549  */
4550 struct nfsrvdsremove {
4551 	int			done;
4552 	int			inprog;
4553 	struct task		tsk;
4554 	struct ucred		*tcred;
4555 	struct vnode		*dvp;
4556 	NFSPROC_T		*p;
4557 	int			err;
4558 	char			fname[PNFS_FILENAME_LEN + 1];
4559 };
4560 
4561 static int
4562 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4563     NFSPROC_T *p)
4564 {
4565 	struct nameidata named;
4566 	struct vnode *nvp;
4567 	char *bufp;
4568 	u_long *hashp;
4569 	int error;
4570 
4571 	error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4572 	if (error != 0)
4573 		return (error);
4574 	named.ni_cnd.cn_nameiop = DELETE;
4575 	named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4576 	named.ni_cnd.cn_cred = tcred;
4577 	named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
4578 	nfsvno_setpathbuf(&named, &bufp, &hashp);
4579 	named.ni_cnd.cn_nameptr = bufp;
4580 	named.ni_cnd.cn_namelen = strlen(fname);
4581 	strlcpy(bufp, fname, NAME_MAX);
4582 	NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4583 	error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4584 	NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4585 	if (error == 0) {
4586 		error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4587 		vput(nvp);
4588 	}
4589 	NFSVOPUNLOCK(dvp);
4590 	nfsvno_relpathbuf(&named);
4591 	if (error != 0)
4592 		printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4593 	return (error);
4594 }
4595 
4596 /*
4597  * Start up the thread that will execute nfsrv_dsremove().
4598  */
4599 static void
4600 start_dsremove(void *arg, int pending)
4601 {
4602 	struct nfsrvdsremove *dsrm;
4603 
4604 	dsrm = (struct nfsrvdsremove *)arg;
4605 	dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4606 	    dsrm->p);
4607 	dsrm->done = 1;
4608 	NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4609 }
4610 
4611 /*
4612  * Remove a pNFS data file from a Data Server.
4613  * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4614  * removed to set up the dvp and fill in the FH.
4615  */
4616 static void
4617 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4618     NFSPROC_T *p)
4619 {
4620 	struct ucred *tcred;
4621 	struct nfsrvdsremove *dsrm, *tdsrm;
4622 	struct nfsdevice *ds;
4623 	struct nfsmount *nmp;
4624 	int failpos, i, ret, timo;
4625 
4626 	tcred = newnfs_getcred();
4627 	dsrm = NULL;
4628 	if (mirrorcnt > 1)
4629 		dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4630 	/*
4631 	 * Remove the file on each DS mirror, using kernel process(es) for the
4632 	 * additional mirrors.
4633 	 */
4634 	failpos = -1;
4635 	for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4636 		tdsrm->tcred = tcred;
4637 		tdsrm->p = p;
4638 		tdsrm->dvp = dvp[i];
4639 		strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4640 		tdsrm->inprog = 0;
4641 		tdsrm->done = 0;
4642 		tdsrm->err = 0;
4643 		ret = EIO;
4644 		if (nfs_pnfsiothreads != 0) {
4645 			ret = nfs_pnfsio(start_dsremove, tdsrm);
4646 			NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4647 		}
4648 		if (ret != 0) {
4649 			ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4650 			if (failpos == -1 && nfsds_failerr(ret))
4651 				failpos = i;
4652 		}
4653 	}
4654 	ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4655 	if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4656 		failpos = mirrorcnt - 1;
4657 	timo = hz / 50;		/* Wait for 20msec. */
4658 	if (timo < 1)
4659 		timo = 1;
4660 	/* Wait for kernel task(s) to complete. */
4661 	for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4662 		while (tdsrm->inprog != 0 && tdsrm->done == 0)
4663 			tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4664 		if (failpos == -1 && nfsds_failerr(tdsrm->err))
4665 			failpos = i;
4666 	}
4667 
4668 	/*
4669 	 * If failpos has been set, that mirror has failed, so it needs
4670 	 * to be disabled.
4671 	 */
4672 	if (failpos >= 0) {
4673 		nmp = VFSTONFS(dvp[failpos]->v_mount);
4674 		NFSLOCKMNT(nmp);
4675 		if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4676 		     NFSMNTP_CANCELRPCS)) == 0) {
4677 			nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4678 			NFSUNLOCKMNT(nmp);
4679 			ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4680 			NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4681 			    ds);
4682 			if (ds != NULL)
4683 				nfsrv_killrpcs(nmp);
4684 			NFSLOCKMNT(nmp);
4685 			nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4686 			wakeup(nmp);
4687 		}
4688 		NFSUNLOCKMNT(nmp);
4689 	}
4690 
4691 	/* Get rid all layouts for the file. */
4692 	nfsrv_freefilelayouts(fhp);
4693 
4694 	NFSFREECRED(tcred);
4695 	free(dsrm, M_TEMP);
4696 }
4697 
4698 /*
4699  * Generate a file name based on the file handle and put it in *bufp.
4700  * Return the number of bytes generated.
4701  */
4702 static int
4703 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4704 {
4705 	int i;
4706 	uint8_t *cp;
4707 	const uint8_t *hexdigits = "0123456789abcdef";
4708 
4709 	cp = (uint8_t *)fhp;
4710 	for (i = 0; i < sizeof(*fhp); i++) {
4711 		bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4712 		bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4713 	}
4714 	bufp[2 * i] = '\0';
4715 	return (2 * i);
4716 }
4717 
4718 /*
4719  * Update the Metadata file's attributes from the DS file when a Read/Write
4720  * layout is returned.
4721  * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4722  * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4723  */
4724 int
4725 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4726 {
4727 	struct ucred *tcred;
4728 	int error;
4729 
4730 	/* Do this as root so that it won't fail with EACCES. */
4731 	tcred = newnfs_getcred();
4732 	error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4733 	    NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4734 	NFSFREECRED(tcred);
4735 	return (error);
4736 }
4737 
4738 /*
4739  * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4740  */
4741 static int
4742 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4743     NFSPROC_T *p)
4744 {
4745 	int error;
4746 
4747 	error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4748 	    NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4749 	return (error);
4750 }
4751 
4752 static int
4753 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4754     struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4755     struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4756     off_t *offp, int content, bool *eofp)
4757 {
4758 	struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4759 	fhandle_t fh[NFSDEV_MAXMIRRORS];
4760 	struct vnode *dvp[NFSDEV_MAXMIRRORS];
4761 	struct nfsdevice *ds;
4762 	struct pnfsdsattr dsattr;
4763 	struct opnfsdsattr odsattr;
4764 	char *buf;
4765 	int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4766 
4767 	NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4768 	/*
4769 	 * If not a regular file, not exported or not a pNFS server,
4770 	 * just return ENOENT.
4771 	 */
4772 	if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4773 	    nfsrv_devidcnt == 0)
4774 		return (ENOENT);
4775 
4776 	buflen = 1024;
4777 	buf = malloc(buflen, M_TEMP, M_WAITOK);
4778 	error = 0;
4779 
4780 	/*
4781 	 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4782 	 * from the MetaData file's extended attribute.
4783 	 */
4784 	if (ioproc == NFSPROC_GETATTR) {
4785 		error = vn_extattr_get(vp, IO_NODELOCKED,
4786 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4787 		    p);
4788 		if (error == 0) {
4789 			if (buflen == sizeof(odsattr)) {
4790 				NFSBCOPY(buf, &odsattr, buflen);
4791 				nap->na_filerev = odsattr.dsa_filerev;
4792 				nap->na_size = odsattr.dsa_size;
4793 				nap->na_atime = odsattr.dsa_atime;
4794 				nap->na_mtime = odsattr.dsa_mtime;
4795 				/*
4796 				 * Fake na_bytes by rounding up na_size.
4797 				 * Since we don't know the block size, just
4798 				 * use BLKDEV_IOSIZE.
4799 				 */
4800 				nap->na_bytes = (odsattr.dsa_size +
4801 				    BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4802 			} else if (buflen == sizeof(dsattr)) {
4803 				NFSBCOPY(buf, &dsattr, buflen);
4804 				nap->na_filerev = dsattr.dsa_filerev;
4805 				nap->na_size = dsattr.dsa_size;
4806 				nap->na_atime = dsattr.dsa_atime;
4807 				nap->na_mtime = dsattr.dsa_mtime;
4808 				nap->na_bytes = dsattr.dsa_bytes;
4809 			} else
4810 				error = ENXIO;
4811 		}
4812 		if (error == 0) {
4813 			/*
4814 			 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4815 			 * returns 0, just return now.  nfsrv_checkdsattr()
4816 			 * returns 0 if there is no Read/Write layout
4817 			 * plus either an Open/Write_access or Write
4818 			 * delegation issued to a client for the file.
4819 			 */
4820 			if (nfsrv_pnfsgetdsattr == 0 ||
4821 			    nfsrv_checkdsattr(vp, p) == 0) {
4822 				free(buf, M_TEMP);
4823 				return (error);
4824 			}
4825 		}
4826 
4827 		/*
4828 		 * Clear ENOATTR so the code below will attempt to do a
4829 		 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4830 		 * the extended attribute.
4831 		 */
4832 		if (error == ENOATTR)
4833 			error = 0;
4834 	}
4835 
4836 	origmircnt = -1;
4837 	trycnt = 0;
4838 tryagain:
4839 	if (error == 0) {
4840 		buflen = 1024;
4841 		if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4842 		    LK_EXCLUSIVE)
4843 			printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4844 		error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4845 		    &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4846 		    NULL, NULL);
4847 		if (error == 0) {
4848 			for (i = 0; i < mirrorcnt; i++)
4849 				nmp[i] = VFSTONFS(dvp[i]->v_mount);
4850 		} else
4851 			printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4852 	} else
4853 		printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4854 	if (error == 0) {
4855 		failpos = -1;
4856 		if (origmircnt == -1)
4857 			origmircnt = mirrorcnt;
4858 		/*
4859 		 * If failpos is set to a mirror#, then that mirror has
4860 		 * failed and will be disabled. For Read, Getattr and Seek, the
4861 		 * function only tries one mirror, so if that mirror has
4862 		 * failed, it will need to be retried. As such, increment
4863 		 * tryitagain for these cases.
4864 		 * For Write, Setattr and Setacl, the function tries all
4865 		 * mirrors and will not return an error for the case where
4866 		 * one mirror has failed. For these cases, the functioning
4867 		 * mirror(s) will have been modified, so a retry isn't
4868 		 * necessary. These functions will set failpos for the
4869 		 * failed mirror#.
4870 		 */
4871 		if (ioproc == NFSPROC_READDS) {
4872 			error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4873 			    mpp, mpp2);
4874 			if (nfsds_failerr(error) && mirrorcnt > 1) {
4875 				/*
4876 				 * Setting failpos will cause the mirror
4877 				 * to be disabled and then a retry of this
4878 				 * read is required.
4879 				 */
4880 				failpos = 0;
4881 				error = 0;
4882 				trycnt++;
4883 			}
4884 		} else if (ioproc == NFSPROC_WRITEDS)
4885 			error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4886 			    &nmp[0], mirrorcnt, mpp, cp, &failpos);
4887 		else if (ioproc == NFSPROC_SETATTR)
4888 			error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4889 			    mirrorcnt, nap, &failpos);
4890 		else if (ioproc == NFSPROC_SETACL)
4891 			error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4892 			    mirrorcnt, aclp, &failpos);
4893 		else if (ioproc == NFSPROC_SEEKDS) {
4894 			error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4895 			    p, nmp[0]);
4896 			if (nfsds_failerr(error) && mirrorcnt > 1) {
4897 				/*
4898 				 * Setting failpos will cause the mirror
4899 				 * to be disabled and then a retry of this
4900 				 * read is required.
4901 				 */
4902 				failpos = 0;
4903 				error = 0;
4904 				trycnt++;
4905 			}
4906 		} else if (ioproc == NFSPROC_ALLOCATE)
4907 			error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4908 			    &nmp[0], mirrorcnt, &failpos);
4909 		else if (ioproc == NFSPROC_DEALLOCATE)
4910 			error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p,
4911 			    vp, &nmp[0], mirrorcnt, &failpos);
4912 		else {
4913 			error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4914 			    vp, nmp[mirrorcnt - 1], nap);
4915 			if (nfsds_failerr(error) && mirrorcnt > 1) {
4916 				/*
4917 				 * Setting failpos will cause the mirror
4918 				 * to be disabled and then a retry of this
4919 				 * getattr is required.
4920 				 */
4921 				failpos = mirrorcnt - 1;
4922 				error = 0;
4923 				trycnt++;
4924 			}
4925 		}
4926 		ds = NULL;
4927 		if (failpos >= 0) {
4928 			failnmp = nmp[failpos];
4929 			NFSLOCKMNT(failnmp);
4930 			if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4931 			     NFSMNTP_CANCELRPCS)) == 0) {
4932 				failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4933 				NFSUNLOCKMNT(failnmp);
4934 				ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4935 				    failnmp, p);
4936 				NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4937 				    failpos, ds);
4938 				if (ds != NULL)
4939 					nfsrv_killrpcs(failnmp);
4940 				NFSLOCKMNT(failnmp);
4941 				failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4942 				wakeup(failnmp);
4943 			}
4944 			NFSUNLOCKMNT(failnmp);
4945 		}
4946 		for (i = 0; i < mirrorcnt; i++)
4947 			NFSVOPUNLOCK(dvp[i]);
4948 		NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4949 		    trycnt);
4950 		/* Try the Read/Getattr again if a mirror was deleted. */
4951 		if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4952 			goto tryagain;
4953 	} else {
4954 		/* Return ENOENT for any Extended Attribute error. */
4955 		error = ENOENT;
4956 	}
4957 	free(buf, M_TEMP);
4958 	NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4959 	return (error);
4960 }
4961 
4962 /*
4963  * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4964  * attribute.
4965  * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4966  *           to be checked.  If it points to a NULL nmp, then it returns
4967  *           a suitable destination.
4968  * curnmp - If non-NULL, it is the source mount for the copy.
4969  */
4970 int
4971 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4972     int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4973     char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4974     struct nfsmount *curnmp, int *ippos, int *dsdirp)
4975 {
4976 	struct vnode *dvp, *nvp = NULL, **tdvpp;
4977 	struct mount *mp;
4978 	struct nfsmount *nmp, *newnmp;
4979 	struct sockaddr *sad;
4980 	struct sockaddr_in *sin;
4981 	struct nfsdevice *ds, *tds, *fndds;
4982 	struct pnfsdsfile *pf;
4983 	uint32_t dsdir;
4984 	int error, fhiszero, fnd, gotone, i, mirrorcnt;
4985 
4986 	ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4987 	*mirrorcntp = 1;
4988 	tdvpp = dvpp;
4989 	if (nvpp != NULL)
4990 		*nvpp = NULL;
4991 	if (dvpp != NULL)
4992 		*dvpp = NULL;
4993 	if (ippos != NULL)
4994 		*ippos = -1;
4995 	if (newnmpp != NULL)
4996 		newnmp = *newnmpp;
4997 	else
4998 		newnmp = NULL;
4999 	mp = vp->v_mount;
5000 	error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5001 	    "pnfsd.dsfile", buflenp, buf, p);
5002 	mirrorcnt = *buflenp / sizeof(*pf);
5003 	if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
5004 	    *buflenp != sizeof(*pf) * mirrorcnt))
5005 		error = ENOATTR;
5006 
5007 	pf = (struct pnfsdsfile *)buf;
5008 	/* If curnmp != NULL, check for a match in the mirror list. */
5009 	if (curnmp != NULL && error == 0) {
5010 		fnd = 0;
5011 		for (i = 0; i < mirrorcnt; i++, pf++) {
5012 			sad = (struct sockaddr *)&pf->dsf_sin;
5013 			if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5014 				if (ippos != NULL)
5015 					*ippos = i;
5016 				fnd = 1;
5017 				break;
5018 			}
5019 		}
5020 		if (fnd == 0)
5021 			error = ENXIO;
5022 	}
5023 
5024 	gotone = 0;
5025 	pf = (struct pnfsdsfile *)buf;
5026 	NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5027 	    error);
5028 	for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5029 		fhiszero = 0;
5030 		sad = (struct sockaddr *)&pf->dsf_sin;
5031 		sin = &pf->dsf_sin;
5032 		dsdir = pf->dsf_dir;
5033 		if (dsdir >= nfsrv_dsdirsize) {
5034 			printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5035 			error = ENOATTR;
5036 		} else if (nvpp != NULL && newnmp != NULL &&
5037 		    nfsaddr2_match(sad, newnmp->nm_nam))
5038 			error = EEXIST;
5039 		if (error == 0) {
5040 			if (ippos != NULL && curnmp == NULL &&
5041 			    sad->sa_family == AF_INET &&
5042 			    sin->sin_addr.s_addr == 0)
5043 				*ippos = i;
5044 			if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5045 				fhiszero = 1;
5046 			/* Use the socket address to find the mount point. */
5047 			fndds = NULL;
5048 			NFSDDSLOCK();
5049 			/* Find a match for the IP address. */
5050 			TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5051 				if (ds->nfsdev_nmp != NULL) {
5052 					dvp = ds->nfsdev_dvp;
5053 					nmp = VFSTONFS(dvp->v_mount);
5054 					if (nmp != ds->nfsdev_nmp)
5055 						printf("different2 nmp %p %p\n",
5056 						    nmp, ds->nfsdev_nmp);
5057 					if (nfsaddr2_match(sad, nmp->nm_nam)) {
5058 						fndds = ds;
5059 						break;
5060 					}
5061 				}
5062 			}
5063 			if (fndds != NULL && newnmpp != NULL &&
5064 			    newnmp == NULL) {
5065 				/* Search for a place to make a mirror copy. */
5066 				TAILQ_FOREACH(tds, &nfsrv_devidhead,
5067 				    nfsdev_list) {
5068 					if (tds->nfsdev_nmp != NULL &&
5069 					    fndds != tds &&
5070 					    ((tds->nfsdev_mdsisset == 0 &&
5071 					      fndds->nfsdev_mdsisset == 0) ||
5072 					     (tds->nfsdev_mdsisset != 0 &&
5073 					      fndds->nfsdev_mdsisset != 0 &&
5074 					      fsidcmp(&tds->nfsdev_mdsfsid,
5075 					      &mp->mnt_stat.f_fsid) == 0))) {
5076 						*newnmpp = tds->nfsdev_nmp;
5077 						break;
5078 					}
5079 				}
5080 				if (tds != NULL) {
5081 					/*
5082 					 * Move this entry to the end of the
5083 					 * list, so it won't be selected as
5084 					 * easily the next time.
5085 					 */
5086 					TAILQ_REMOVE(&nfsrv_devidhead, tds,
5087 					    nfsdev_list);
5088 					TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5089 					    nfsdev_list);
5090 				}
5091 			}
5092 			NFSDDSUNLOCK();
5093 			if (fndds != NULL) {
5094 				dvp = fndds->nfsdev_dsdir[dsdir];
5095 				if (lktype != 0 || fhiszero != 0 ||
5096 				    (nvpp != NULL && *nvpp == NULL)) {
5097 					if (fhiszero != 0)
5098 						error = vn_lock(dvp,
5099 						    LK_EXCLUSIVE);
5100 					else if (lktype != 0)
5101 						error = vn_lock(dvp, lktype);
5102 					else
5103 						error = vn_lock(dvp, LK_SHARED);
5104 					/*
5105 					 * If the file handle is all 0's, try to
5106 					 * do a Lookup against the DS to acquire
5107 					 * it.
5108 					 * If dvpp == NULL or the Lookup fails,
5109 					 * unlock dvp after the call.
5110 					 */
5111 					if (error == 0 && (fhiszero != 0 ||
5112 					    (nvpp != NULL && *nvpp == NULL))) {
5113 						error = nfsrv_pnfslookupds(vp,
5114 						    dvp, pf, &nvp, p);
5115 						if (error == 0) {
5116 							if (fhiszero != 0)
5117 								nfsrv_pnfssetfh(
5118 								    vp, pf,
5119 								    devid,
5120 								    fnamep,
5121 								    nvp, p);
5122 							if (nvpp != NULL &&
5123 							    *nvpp == NULL) {
5124 								*nvpp = nvp;
5125 								*dsdirp = dsdir;
5126 							} else
5127 								vput(nvp);
5128 						}
5129 						if (error != 0 || lktype == 0)
5130 							NFSVOPUNLOCK(dvp);
5131 					}
5132 				}
5133 				if (error == 0) {
5134 					gotone++;
5135 					NFSD_DEBUG(4, "gotone=%d\n", gotone);
5136 					if (devid != NULL) {
5137 						NFSBCOPY(fndds->nfsdev_deviceid,
5138 						    devid, NFSX_V4DEVICEID);
5139 						devid += NFSX_V4DEVICEID;
5140 					}
5141 					if (dvpp != NULL)
5142 						*tdvpp++ = dvp;
5143 					if (fhp != NULL)
5144 						NFSBCOPY(&pf->dsf_fh, fhp++,
5145 						    NFSX_MYFH);
5146 					if (fnamep != NULL && gotone == 1)
5147 						strlcpy(fnamep,
5148 						    pf->dsf_filename,
5149 						    sizeof(pf->dsf_filename));
5150 				} else
5151 					NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5152 					    "err=%d\n", error);
5153 			}
5154 		}
5155 	}
5156 	if (error == 0 && gotone == 0)
5157 		error = ENOENT;
5158 
5159 	NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5160 	    error);
5161 	if (error == 0)
5162 		*mirrorcntp = gotone;
5163 	else {
5164 		if (gotone > 0 && dvpp != NULL) {
5165 			/*
5166 			 * If the error didn't occur on the first one and
5167 			 * dvpp != NULL, the one(s) prior to the failure will
5168 			 * have locked dvp's that need to be unlocked.
5169 			 */
5170 			for (i = 0; i < gotone; i++) {
5171 				NFSVOPUNLOCK(*dvpp);
5172 				*dvpp++ = NULL;
5173 			}
5174 		}
5175 		/*
5176 		 * If it found the vnode to be copied from before a failure,
5177 		 * it needs to be vput()'d.
5178 		 */
5179 		if (nvpp != NULL && *nvpp != NULL) {
5180 			vput(*nvpp);
5181 			*nvpp = NULL;
5182 		}
5183 	}
5184 	return (error);
5185 }
5186 
5187 /*
5188  * Set the extended attribute for the Change attribute.
5189  */
5190 static int
5191 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5192 {
5193 	struct pnfsdsattr dsattr;
5194 	int error;
5195 
5196 	ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5197 	dsattr.dsa_filerev = nap->na_filerev;
5198 	dsattr.dsa_size = nap->na_size;
5199 	dsattr.dsa_atime = nap->na_atime;
5200 	dsattr.dsa_mtime = nap->na_mtime;
5201 	dsattr.dsa_bytes = nap->na_bytes;
5202 	error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5203 	    "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5204 	if (error != 0)
5205 		printf("pNFS: setextattr=%d\n", error);
5206 	return (error);
5207 }
5208 
5209 static int
5210 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5211     NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5212 {
5213 	uint32_t *tl;
5214 	struct nfsrv_descript *nd;
5215 	nfsv4stateid_t st;
5216 	struct mbuf *m, *m2;
5217 	int error = 0, retlen, tlen, trimlen;
5218 
5219 	NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5220 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5221 	*mpp = NULL;
5222 	/*
5223 	 * Use a stateid where other is an alternating 01010 pattern and
5224 	 * seqid is 0xffffffff.  This value is not defined as special by
5225 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5226 	 * MDS->DS proxy operation.
5227 	 */
5228 	st.other[0] = 0x55555555;
5229 	st.other[1] = 0x55555555;
5230 	st.other[2] = 0x55555555;
5231 	st.seqid = 0xffffffff;
5232 	nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5233 	    NULL, NULL, 0, 0, cred);
5234 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5235 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5236 	txdr_hyper(off, tl);
5237 	*(tl + 2) = txdr_unsigned(len);
5238 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5239 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5240 	if (error != 0) {
5241 		free(nd, M_TEMP);
5242 		return (error);
5243 	}
5244 	if (nd->nd_repstat == 0) {
5245 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5246 		NFSM_STRSIZ(retlen, len);
5247 		if (retlen > 0) {
5248 			/* Trim off the pre-data XDR from the mbuf chain. */
5249 			m = nd->nd_mrep;
5250 			while (m != NULL && m != nd->nd_md) {
5251 				if (m->m_next == nd->nd_md) {
5252 					m->m_next = NULL;
5253 					m_freem(nd->nd_mrep);
5254 					nd->nd_mrep = m = nd->nd_md;
5255 				} else
5256 					m = m->m_next;
5257 			}
5258 			if (m == NULL) {
5259 				printf("nfsrv_readdsrpc: busted mbuf list\n");
5260 				error = ENOENT;
5261 				goto nfsmout;
5262 			}
5263 
5264 			/*
5265 			 * Now, adjust first mbuf so that any XDR before the
5266 			 * read data is skipped over.
5267 			 */
5268 			trimlen = nd->nd_dpos - mtod(m, char *);
5269 			if (trimlen > 0) {
5270 				m->m_len -= trimlen;
5271 				NFSM_DATAP(m, trimlen);
5272 			}
5273 
5274 			/*
5275 			 * Truncate the mbuf chain at retlen bytes of data,
5276 			 * plus XDR padding that brings the length up to a
5277 			 * multiple of 4.
5278 			 */
5279 			tlen = NFSM_RNDUP(retlen);
5280 			do {
5281 				if (m->m_len >= tlen) {
5282 					m->m_len = tlen;
5283 					tlen = 0;
5284 					m2 = m->m_next;
5285 					m->m_next = NULL;
5286 					m_freem(m2);
5287 					break;
5288 				}
5289 				tlen -= m->m_len;
5290 				m = m->m_next;
5291 			} while (m != NULL);
5292 			if (tlen > 0) {
5293 				printf("nfsrv_readdsrpc: busted mbuf list\n");
5294 				error = ENOENT;
5295 				goto nfsmout;
5296 			}
5297 			*mpp = nd->nd_mrep;
5298 			*mpendp = m;
5299 			nd->nd_mrep = NULL;
5300 		}
5301 	} else
5302 		error = nd->nd_repstat;
5303 nfsmout:
5304 	/* If nd->nd_mrep is already NULL, this is a no-op. */
5305 	m_freem(nd->nd_mrep);
5306 	free(nd, M_TEMP);
5307 	NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5308 	return (error);
5309 }
5310 
5311 /*
5312  * Do a write RPC on a DS data file, using this structure for the arguments,
5313  * so that this function can be executed by a separate kernel process.
5314  */
5315 struct nfsrvwritedsdorpc {
5316 	int			done;
5317 	int			inprog;
5318 	struct task		tsk;
5319 	fhandle_t		fh;
5320 	off_t			off;
5321 	int			len;
5322 	struct nfsmount		*nmp;
5323 	struct ucred		*cred;
5324 	NFSPROC_T		*p;
5325 	struct mbuf		*m;
5326 	int			err;
5327 };
5328 
5329 static int
5330 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5331     struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5332 {
5333 	uint32_t *tl;
5334 	struct nfsrv_descript *nd;
5335 	nfsattrbit_t attrbits;
5336 	nfsv4stateid_t st;
5337 	int commit, error, retlen;
5338 
5339 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5340 	nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5341 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5342 
5343 	/*
5344 	 * Use a stateid where other is an alternating 01010 pattern and
5345 	 * seqid is 0xffffffff.  This value is not defined as special by
5346 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5347 	 * MDS->DS proxy operation.
5348 	 */
5349 	st.other[0] = 0x55555555;
5350 	st.other[1] = 0x55555555;
5351 	st.other[2] = 0x55555555;
5352 	st.seqid = 0xffffffff;
5353 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5354 	NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5355 	txdr_hyper(off, tl);
5356 	tl += 2;
5357 	/*
5358 	 * Do all writes FileSync, since the server doesn't hold onto dirty
5359 	 * buffers.  Since clients should be accessing the DS servers directly
5360 	 * using the pNFS layouts, this just needs to work correctly as a
5361 	 * fallback.
5362 	 */
5363 	*tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5364 	*tl = txdr_unsigned(len);
5365 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5366 
5367 	/* Put data in mbuf chain. */
5368 	nd->nd_mb->m_next = m;
5369 
5370 	/* Set nd_mb and nd_bpos to end of data. */
5371 	while (m->m_next != NULL)
5372 		m = m->m_next;
5373 	nd->nd_mb = m;
5374 	nfsm_set(nd, m->m_len);
5375 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5376 
5377 	/* Do a Getattr for the attributes that change upon writing. */
5378 	NFSZERO_ATTRBIT(&attrbits);
5379 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5380 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5381 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5382 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5383 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5384 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5385 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5386 	(void) nfsrv_putattrbit(nd, &attrbits);
5387 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5388 	    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5389 	if (error != 0) {
5390 		free(nd, M_TEMP);
5391 		return (error);
5392 	}
5393 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5394 	/* Get rid of weak cache consistency data for now. */
5395 	if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5396 	    (ND_NFSV4 | ND_V4WCCATTR)) {
5397 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5398 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5399 		NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5400 		if (error != 0)
5401 			goto nfsmout;
5402 		/*
5403 		 * Get rid of Op# and status for next op.
5404 		 */
5405 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5406 		if (*++tl != 0)
5407 			nd->nd_flag |= ND_NOMOREDATA;
5408 	}
5409 	if (nd->nd_repstat == 0) {
5410 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5411 		retlen = fxdr_unsigned(int, *tl++);
5412 		commit = fxdr_unsigned(int, *tl);
5413 		if (commit != NFSWRITE_FILESYNC)
5414 			error = NFSERR_IO;
5415 		NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5416 		    retlen, commit, error);
5417 	} else
5418 		error = nd->nd_repstat;
5419 	/* We have no use for the Write Verifier since we use FileSync. */
5420 
5421 	/*
5422 	 * Get the Change, Size, Access Time and Modify Time attributes and set
5423 	 * on the Metadata file, so its attributes will be what the file's
5424 	 * would be if it had been written.
5425 	 */
5426 	if (error == 0) {
5427 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5428 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5429 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5430 	}
5431 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5432 nfsmout:
5433 	m_freem(nd->nd_mrep);
5434 	free(nd, M_TEMP);
5435 	NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5436 	return (error);
5437 }
5438 
5439 /*
5440  * Start up the thread that will execute nfsrv_writedsdorpc().
5441  */
5442 static void
5443 start_writedsdorpc(void *arg, int pending)
5444 {
5445 	struct nfsrvwritedsdorpc *drpc;
5446 
5447 	drpc = (struct nfsrvwritedsdorpc *)arg;
5448 	drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5449 	    drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5450 	drpc->done = 1;
5451 	NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5452 }
5453 
5454 static int
5455 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5456     NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5457     struct mbuf **mpp, char *cp, int *failposp)
5458 {
5459 	struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5460 	struct nfsvattr na;
5461 	struct mbuf *m;
5462 	int error, i, offs, ret, timo;
5463 
5464 	NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5465 	KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5466 	drpc = NULL;
5467 	if (mirrorcnt > 1)
5468 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5469 		    M_WAITOK);
5470 
5471 	/* Calculate offset in mbuf chain that data starts. */
5472 	offs = cp - mtod(*mpp, char *);
5473 	NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5474 
5475 	/*
5476 	 * Do the write RPC for every DS, using a separate kernel process
5477 	 * for every DS except the last one.
5478 	 */
5479 	error = 0;
5480 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5481 		tdrpc->done = 0;
5482 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5483 		tdrpc->off = off;
5484 		tdrpc->len = len;
5485 		tdrpc->nmp = *nmpp;
5486 		tdrpc->cred = cred;
5487 		tdrpc->p = p;
5488 		tdrpc->inprog = 0;
5489 		tdrpc->err = 0;
5490 		tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5491 		ret = EIO;
5492 		if (nfs_pnfsiothreads != 0) {
5493 			ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5494 			NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5495 			    ret);
5496 		}
5497 		if (ret != 0) {
5498 			ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5499 			    tdrpc->m, cred, p);
5500 			if (nfsds_failerr(ret) && *failposp == -1)
5501 				*failposp = i;
5502 			else if (error == 0 && ret != 0)
5503 				error = ret;
5504 		}
5505 		nmpp++;
5506 		fhp++;
5507 	}
5508 	m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5509 	ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5510 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5511 		*failposp = mirrorcnt - 1;
5512 	else if (error == 0 && ret != 0)
5513 		error = ret;
5514 	if (error == 0)
5515 		error = nfsrv_setextattr(vp, &na, p);
5516 	NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5517 	tdrpc = drpc;
5518 	timo = hz / 50;		/* Wait for 20msec. */
5519 	if (timo < 1)
5520 		timo = 1;
5521 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5522 		/* Wait for RPCs on separate threads to complete. */
5523 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
5524 			tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5525 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5526 			*failposp = i;
5527 		else if (error == 0 && tdrpc->err != 0)
5528 			error = tdrpc->err;
5529 	}
5530 	free(drpc, M_TEMP);
5531 	return (error);
5532 }
5533 
5534 /*
5535  * Do a allocate RPC on a DS data file, using this structure for the arguments,
5536  * so that this function can be executed by a separate kernel process.
5537  */
5538 struct nfsrvallocatedsdorpc {
5539 	int			done;
5540 	int			inprog;
5541 	struct task		tsk;
5542 	fhandle_t		fh;
5543 	off_t			off;
5544 	off_t			len;
5545 	struct nfsmount		*nmp;
5546 	struct ucred		*cred;
5547 	NFSPROC_T		*p;
5548 	int			err;
5549 };
5550 
5551 static int
5552 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5553     off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5554 {
5555 	uint32_t *tl;
5556 	struct nfsrv_descript *nd;
5557 	nfsattrbit_t attrbits;
5558 	nfsv4stateid_t st;
5559 	int error;
5560 
5561 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5562 	nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5563 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5564 
5565 	/*
5566 	 * Use a stateid where other is an alternating 01010 pattern and
5567 	 * seqid is 0xffffffff.  This value is not defined as special by
5568 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5569 	 * MDS->DS proxy operation.
5570 	 */
5571 	st.other[0] = 0x55555555;
5572 	st.other[1] = 0x55555555;
5573 	st.other[2] = 0x55555555;
5574 	st.seqid = 0xffffffff;
5575 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5576 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5577 	txdr_hyper(off, tl); tl += 2;
5578 	txdr_hyper(len, tl); tl += 2;
5579 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5580 
5581 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5582 	NFSGETATTR_ATTRBIT(&attrbits);
5583 	nfsrv_putattrbit(nd, &attrbits);
5584 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5585 	    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5586 	if (error != 0) {
5587 		free(nd, M_TEMP);
5588 		return (error);
5589 	}
5590 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5591 	    nd->nd_repstat);
5592 	if (nd->nd_repstat == 0) {
5593 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5594 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5595 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5596 	} else
5597 		error = nd->nd_repstat;
5598 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5599 nfsmout:
5600 	m_freem(nd->nd_mrep);
5601 	free(nd, M_TEMP);
5602 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5603 	return (error);
5604 }
5605 
5606 /*
5607  * Start up the thread that will execute nfsrv_allocatedsdorpc().
5608  */
5609 static void
5610 start_allocatedsdorpc(void *arg, int pending)
5611 {
5612 	struct nfsrvallocatedsdorpc *drpc;
5613 
5614 	drpc = (struct nfsrvallocatedsdorpc *)arg;
5615 	drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5616 	    drpc->len, NULL, drpc->cred, drpc->p);
5617 	drpc->done = 1;
5618 	NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5619 }
5620 
5621 static int
5622 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5623     NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5624     int *failposp)
5625 {
5626 	struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5627 	struct nfsvattr na;
5628 	int error, i, ret, timo;
5629 
5630 	NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5631 	drpc = NULL;
5632 	if (mirrorcnt > 1)
5633 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5634 		    M_WAITOK);
5635 
5636 	/*
5637 	 * Do the allocate RPC for every DS, using a separate kernel process
5638 	 * for every DS except the last one.
5639 	 */
5640 	error = 0;
5641 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5642 		tdrpc->done = 0;
5643 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5644 		tdrpc->off = off;
5645 		tdrpc->len = len;
5646 		tdrpc->nmp = *nmpp;
5647 		tdrpc->cred = cred;
5648 		tdrpc->p = p;
5649 		tdrpc->inprog = 0;
5650 		tdrpc->err = 0;
5651 		ret = EIO;
5652 		if (nfs_pnfsiothreads != 0) {
5653 			ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5654 			NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5655 			    ret);
5656 		}
5657 		if (ret != 0) {
5658 			ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5659 			    cred, p);
5660 			if (nfsds_failerr(ret) && *failposp == -1)
5661 				*failposp = i;
5662 			else if (error == 0 && ret != 0)
5663 				error = ret;
5664 		}
5665 		nmpp++;
5666 		fhp++;
5667 	}
5668 	ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5669 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5670 		*failposp = mirrorcnt - 1;
5671 	else if (error == 0 && ret != 0)
5672 		error = ret;
5673 	if (error == 0)
5674 		error = nfsrv_setextattr(vp, &na, p);
5675 	NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5676 	tdrpc = drpc;
5677 	timo = hz / 50;		/* Wait for 20msec. */
5678 	if (timo < 1)
5679 		timo = 1;
5680 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5681 		/* Wait for RPCs on separate threads to complete. */
5682 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
5683 			tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5684 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5685 			*failposp = i;
5686 		else if (error == 0 && tdrpc->err != 0)
5687 			error = tdrpc->err;
5688 	}
5689 	free(drpc, M_TEMP);
5690 	return (error);
5691 }
5692 
5693 /*
5694  * Do a deallocate RPC on a DS data file, using this structure for the
5695  * arguments, so that this function can be executed by a separate kernel
5696  * process.
5697  */
5698 struct nfsrvdeallocatedsdorpc {
5699 	int			done;
5700 	int			inprog;
5701 	struct task		tsk;
5702 	fhandle_t		fh;
5703 	off_t			off;
5704 	off_t			len;
5705 	struct nfsmount		*nmp;
5706 	struct ucred		*cred;
5707 	NFSPROC_T		*p;
5708 	int			err;
5709 };
5710 
5711 static int
5712 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5713     off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5714 {
5715 	uint32_t *tl;
5716 	struct nfsrv_descript *nd;
5717 	nfsattrbit_t attrbits;
5718 	nfsv4stateid_t st;
5719 	int error;
5720 
5721 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5722 	nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp,
5723 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5724 
5725 	/*
5726 	 * Use a stateid where other is an alternating 01010 pattern and
5727 	 * seqid is 0xffffffff.  This value is not defined as special by
5728 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5729 	 * MDS->DS proxy operation.
5730 	 */
5731 	st.other[0] = 0x55555555;
5732 	st.other[1] = 0x55555555;
5733 	st.other[2] = 0x55555555;
5734 	st.seqid = 0xffffffff;
5735 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5736 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5737 	txdr_hyper(off, tl); tl += 2;
5738 	txdr_hyper(len, tl); tl += 2;
5739 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len);
5740 
5741 	/* Do a Getattr for the attributes that change upon writing. */
5742 	NFSZERO_ATTRBIT(&attrbits);
5743 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5744 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5745 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5746 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5747 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5748 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5749 	nfsrv_putattrbit(nd, &attrbits);
5750 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5751 	    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5752 	if (error != 0) {
5753 		free(nd, M_TEMP);
5754 		return (error);
5755 	}
5756 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n",
5757 	    nd->nd_repstat);
5758 	/* Get rid of weak cache consistency data for now. */
5759 	if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5760 	    (ND_NFSV4 | ND_V4WCCATTR)) {
5761 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5762 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5763 		NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error);
5764 		if (error != 0)
5765 			goto nfsmout;
5766 		/*
5767 		 * Get rid of Op# and status for next op.
5768 		 */
5769 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5770 		if (*++tl != 0)
5771 			nd->nd_flag |= ND_NOMOREDATA;
5772 	}
5773 	if (nd->nd_repstat == 0) {
5774 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5775 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5776 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5777 	} else
5778 		error = nd->nd_repstat;
5779 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error);
5780 nfsmout:
5781 	m_freem(nd->nd_mrep);
5782 	free(nd, M_TEMP);
5783 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error);
5784 	return (error);
5785 }
5786 
5787 /*
5788  * Start up the thread that will execute nfsrv_deallocatedsdorpc().
5789  */
5790 static void
5791 start_deallocatedsdorpc(void *arg, int pending)
5792 {
5793 	struct nfsrvdeallocatedsdorpc *drpc;
5794 
5795 	drpc = (struct nfsrvdeallocatedsdorpc *)arg;
5796 	drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5797 	    drpc->len, NULL, drpc->cred, drpc->p);
5798 	drpc->done = 1;
5799 	NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err);
5800 }
5801 
5802 static int
5803 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5804     NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5805     int *failposp)
5806 {
5807 	struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL;
5808 	struct nfsvattr na;
5809 	int error, i, ret, timo;
5810 
5811 	NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n");
5812 	drpc = NULL;
5813 	if (mirrorcnt > 1)
5814 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5815 		    M_WAITOK);
5816 
5817 	/*
5818 	 * Do the deallocate RPC for every DS, using a separate kernel process
5819 	 * for every DS except the last one.
5820 	 */
5821 	error = 0;
5822 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5823 		tdrpc->done = 0;
5824 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5825 		tdrpc->off = off;
5826 		tdrpc->len = len;
5827 		tdrpc->nmp = *nmpp;
5828 		tdrpc->cred = cred;
5829 		tdrpc->p = p;
5830 		tdrpc->inprog = 0;
5831 		tdrpc->err = 0;
5832 		ret = EIO;
5833 		if (nfs_pnfsiothreads != 0) {
5834 			ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc);
5835 			NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n",
5836 			    ret);
5837 		}
5838 		if (ret != 0) {
5839 			ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len,
5840 			    NULL, cred, p);
5841 			if (nfsds_failerr(ret) && *failposp == -1)
5842 				*failposp = i;
5843 			else if (error == 0 && ret != 0)
5844 				error = ret;
5845 		}
5846 		nmpp++;
5847 		fhp++;
5848 	}
5849 	ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5850 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5851 		*failposp = mirrorcnt - 1;
5852 	else if (error == 0 && ret != 0)
5853 		error = ret;
5854 	if (error == 0)
5855 		error = nfsrv_setextattr(vp, &na, p);
5856 	NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error);
5857 	tdrpc = drpc;
5858 	timo = hz / 50;		/* Wait for 20msec. */
5859 	if (timo < 1)
5860 		timo = 1;
5861 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5862 		/* Wait for RPCs on separate threads to complete. */
5863 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
5864 			tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5865 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5866 			*failposp = i;
5867 		else if (error == 0 && tdrpc->err != 0)
5868 			error = tdrpc->err;
5869 	}
5870 	free(drpc, M_TEMP);
5871 	return (error);
5872 }
5873 
5874 static int
5875 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5876     struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5877     struct nfsvattr *dsnap)
5878 {
5879 	uint32_t *tl;
5880 	struct nfsrv_descript *nd;
5881 	nfsv4stateid_t st;
5882 	nfsattrbit_t attrbits;
5883 	int error;
5884 
5885 	NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5886 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5887 	/*
5888 	 * Use a stateid where other is an alternating 01010 pattern and
5889 	 * seqid is 0xffffffff.  This value is not defined as special by
5890 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5891 	 * MDS->DS proxy operation.
5892 	 */
5893 	st.other[0] = 0x55555555;
5894 	st.other[1] = 0x55555555;
5895 	st.other[2] = 0x55555555;
5896 	st.seqid = 0xffffffff;
5897 	nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5898 	    NULL, NULL, 0, 0, cred);
5899 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5900 	nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5901 
5902 	/* Do a Getattr for the attributes that change due to writing. */
5903 	NFSZERO_ATTRBIT(&attrbits);
5904 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5905 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5906 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5907 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5908 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5909 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5910 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5911 	(void) nfsrv_putattrbit(nd, &attrbits);
5912 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5913 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5914 	if (error != 0) {
5915 		free(nd, M_TEMP);
5916 		return (error);
5917 	}
5918 	NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5919 	    nd->nd_repstat);
5920 	/* Get rid of weak cache consistency data for now. */
5921 	if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5922 	    (ND_NFSV4 | ND_V4WCCATTR)) {
5923 		error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5924 		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5925 		NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5926 		if (error != 0)
5927 			goto nfsmout;
5928 		/*
5929 		 * Get rid of Op# and status for next op.
5930 		 */
5931 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5932 		if (*++tl != 0)
5933 			nd->nd_flag |= ND_NOMOREDATA;
5934 	}
5935 	error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5936 	if (error != 0)
5937 		goto nfsmout;
5938 	if (nd->nd_repstat != 0)
5939 		error = nd->nd_repstat;
5940 	/*
5941 	 * Get the Change, Size, Access Time and Modify Time attributes and set
5942 	 * on the Metadata file, so its attributes will be what the file's
5943 	 * would be if it had been written.
5944 	 */
5945 	if (error == 0) {
5946 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5947 		error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5948 		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5949 	}
5950 	NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5951 nfsmout:
5952 	m_freem(nd->nd_mrep);
5953 	free(nd, M_TEMP);
5954 	NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5955 	return (error);
5956 }
5957 
5958 struct nfsrvsetattrdsdorpc {
5959 	int			done;
5960 	int			inprog;
5961 	struct task		tsk;
5962 	fhandle_t		fh;
5963 	struct nfsmount		*nmp;
5964 	struct vnode		*vp;
5965 	struct ucred		*cred;
5966 	NFSPROC_T		*p;
5967 	struct nfsvattr		na;
5968 	struct nfsvattr		dsna;
5969 	int			err;
5970 };
5971 
5972 /*
5973  * Start up the thread that will execute nfsrv_setattrdsdorpc().
5974  */
5975 static void
5976 start_setattrdsdorpc(void *arg, int pending)
5977 {
5978 	struct nfsrvsetattrdsdorpc *drpc;
5979 
5980 	drpc = (struct nfsrvsetattrdsdorpc *)arg;
5981 	drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5982 	    drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5983 	drpc->done = 1;
5984 }
5985 
5986 static int
5987 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5988     struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5989     struct nfsvattr *nap, int *failposp)
5990 {
5991 	struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5992 	struct nfsvattr na;
5993 	int error, i, ret, timo;
5994 
5995 	NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5996 	drpc = NULL;
5997 	if (mirrorcnt > 1)
5998 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5999 		    M_WAITOK);
6000 
6001 	/*
6002 	 * Do the setattr RPC for every DS, using a separate kernel process
6003 	 * for every DS except the last one.
6004 	 */
6005 	error = 0;
6006 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6007 		tdrpc->done = 0;
6008 		tdrpc->inprog = 0;
6009 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6010 		tdrpc->nmp = *nmpp;
6011 		tdrpc->vp = vp;
6012 		tdrpc->cred = cred;
6013 		tdrpc->p = p;
6014 		tdrpc->na = *nap;
6015 		tdrpc->err = 0;
6016 		ret = EIO;
6017 		if (nfs_pnfsiothreads != 0) {
6018 			ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
6019 			NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
6020 			    ret);
6021 		}
6022 		if (ret != 0) {
6023 			ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
6024 			    &na);
6025 			if (nfsds_failerr(ret) && *failposp == -1)
6026 				*failposp = i;
6027 			else if (error == 0 && ret != 0)
6028 				error = ret;
6029 		}
6030 		nmpp++;
6031 		fhp++;
6032 	}
6033 	ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
6034 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6035 		*failposp = mirrorcnt - 1;
6036 	else if (error == 0 && ret != 0)
6037 		error = ret;
6038 	if (error == 0)
6039 		error = nfsrv_setextattr(vp, &na, p);
6040 	NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
6041 	tdrpc = drpc;
6042 	timo = hz / 50;		/* Wait for 20msec. */
6043 	if (timo < 1)
6044 		timo = 1;
6045 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6046 		/* Wait for RPCs on separate threads to complete. */
6047 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
6048 			tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
6049 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6050 			*failposp = i;
6051 		else if (error == 0 && tdrpc->err != 0)
6052 			error = tdrpc->err;
6053 	}
6054 	free(drpc, M_TEMP);
6055 	return (error);
6056 }
6057 
6058 /*
6059  * Do a Setattr of an NFSv4 ACL on the DS file.
6060  */
6061 static int
6062 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6063     struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
6064 {
6065 	struct nfsrv_descript *nd;
6066 	nfsv4stateid_t st;
6067 	nfsattrbit_t attrbits;
6068 	int error;
6069 
6070 	NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
6071 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6072 	/*
6073 	 * Use a stateid where other is an alternating 01010 pattern and
6074 	 * seqid is 0xffffffff.  This value is not defined as special by
6075 	 * the RFC and is used by the FreeBSD NFS server to indicate an
6076 	 * MDS->DS proxy operation.
6077 	 */
6078 	st.other[0] = 0x55555555;
6079 	st.other[1] = 0x55555555;
6080 	st.other[2] = 0x55555555;
6081 	st.seqid = 0xffffffff;
6082 	nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6083 	    NULL, NULL, 0, 0, cred);
6084 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6085 	NFSZERO_ATTRBIT(&attrbits);
6086 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
6087 	/*
6088 	 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
6089 	 * so passing in the metadata "vp" will be ok, since it is of
6090 	 * the same type (VREG).
6091 	 */
6092 	nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
6093 	    NULL, 0, 0, 0, 0, 0, NULL);
6094 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6095 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6096 	if (error != 0) {
6097 		free(nd, M_TEMP);
6098 		return (error);
6099 	}
6100 	NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
6101 	    nd->nd_repstat);
6102 	error = nd->nd_repstat;
6103 	m_freem(nd->nd_mrep);
6104 	free(nd, M_TEMP);
6105 	return (error);
6106 }
6107 
6108 struct nfsrvsetacldsdorpc {
6109 	int			done;
6110 	int			inprog;
6111 	struct task		tsk;
6112 	fhandle_t		fh;
6113 	struct nfsmount		*nmp;
6114 	struct vnode		*vp;
6115 	struct ucred		*cred;
6116 	NFSPROC_T		*p;
6117 	struct acl		*aclp;
6118 	int			err;
6119 };
6120 
6121 /*
6122  * Start up the thread that will execute nfsrv_setacldsdorpc().
6123  */
6124 static void
6125 start_setacldsdorpc(void *arg, int pending)
6126 {
6127 	struct nfsrvsetacldsdorpc *drpc;
6128 
6129 	drpc = (struct nfsrvsetacldsdorpc *)arg;
6130 	drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
6131 	    drpc->vp, drpc->nmp, drpc->aclp);
6132 	drpc->done = 1;
6133 }
6134 
6135 static int
6136 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6137     struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
6138     int *failposp)
6139 {
6140 	struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
6141 	int error, i, ret, timo;
6142 
6143 	NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
6144 	drpc = NULL;
6145 	if (mirrorcnt > 1)
6146 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6147 		    M_WAITOK);
6148 
6149 	/*
6150 	 * Do the setattr RPC for every DS, using a separate kernel process
6151 	 * for every DS except the last one.
6152 	 */
6153 	error = 0;
6154 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6155 		tdrpc->done = 0;
6156 		tdrpc->inprog = 0;
6157 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6158 		tdrpc->nmp = *nmpp;
6159 		tdrpc->vp = vp;
6160 		tdrpc->cred = cred;
6161 		tdrpc->p = p;
6162 		tdrpc->aclp = aclp;
6163 		tdrpc->err = 0;
6164 		ret = EIO;
6165 		if (nfs_pnfsiothreads != 0) {
6166 			ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
6167 			NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
6168 			    ret);
6169 		}
6170 		if (ret != 0) {
6171 			ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
6172 			    aclp);
6173 			if (nfsds_failerr(ret) && *failposp == -1)
6174 				*failposp = i;
6175 			else if (error == 0 && ret != 0)
6176 				error = ret;
6177 		}
6178 		nmpp++;
6179 		fhp++;
6180 	}
6181 	ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
6182 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6183 		*failposp = mirrorcnt - 1;
6184 	else if (error == 0 && ret != 0)
6185 		error = ret;
6186 	NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
6187 	tdrpc = drpc;
6188 	timo = hz / 50;		/* Wait for 20msec. */
6189 	if (timo < 1)
6190 		timo = 1;
6191 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6192 		/* Wait for RPCs on separate threads to complete. */
6193 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
6194 			tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6195 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6196 			*failposp = i;
6197 		else if (error == 0 && tdrpc->err != 0)
6198 			error = tdrpc->err;
6199 	}
6200 	free(drpc, M_TEMP);
6201 	return (error);
6202 }
6203 
6204 /*
6205  * Getattr call to the DS for the attributes that change due to writing.
6206  */
6207 static int
6208 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6209     struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6210 {
6211 	struct nfsrv_descript *nd;
6212 	int error;
6213 	nfsattrbit_t attrbits;
6214 
6215 	NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6216 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6217 	nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6218 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6219 	NFSZERO_ATTRBIT(&attrbits);
6220 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6221 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6222 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6223 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6224 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6225 	(void) nfsrv_putattrbit(nd, &attrbits);
6226 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6227 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6228 	if (error != 0) {
6229 		free(nd, M_TEMP);
6230 		return (error);
6231 	}
6232 	NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6233 	    nd->nd_repstat);
6234 	if (nd->nd_repstat == 0) {
6235 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6236 		    NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6237 		    NULL, NULL);
6238 		/*
6239 		 * We can only save the updated values in the extended
6240 		 * attribute if the vp is exclusively locked.
6241 		 * This should happen when any of the following operations
6242 		 * occur on the vnode:
6243 		 *    Close, Delegreturn, LayoutCommit, LayoutReturn
6244 		 * As such, the updated extended attribute should get saved
6245 		 * before nfsrv_checkdsattr() returns 0 and allows the cached
6246 		 * attributes to be returned without calling this function.
6247 		 */
6248 		if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6249 			error = nfsrv_setextattr(vp, nap, p);
6250 			NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6251 			    error);
6252 		}
6253 	} else
6254 		error = nd->nd_repstat;
6255 	m_freem(nd->nd_mrep);
6256 	free(nd, M_TEMP);
6257 	NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6258 	return (error);
6259 }
6260 
6261 /*
6262  * Seek call to a DS.
6263  */
6264 static int
6265 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6266     struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6267 {
6268 	uint32_t *tl;
6269 	struct nfsrv_descript *nd;
6270 	nfsv4stateid_t st;
6271 	int error;
6272 
6273 	NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6274 	/*
6275 	 * Use a stateid where other is an alternating 01010 pattern and
6276 	 * seqid is 0xffffffff.  This value is not defined as special by
6277 	 * the RFC and is used by the FreeBSD NFS server to indicate an
6278 	 * MDS->DS proxy operation.
6279 	 */
6280 	st.other[0] = 0x55555555;
6281 	st.other[1] = 0x55555555;
6282 	st.other[2] = 0x55555555;
6283 	st.seqid = 0xffffffff;
6284 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6285 	nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6286 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6287 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6288 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6289 	txdr_hyper(*offp, tl); tl += 2;
6290 	*tl = txdr_unsigned(content);
6291 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6292 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6293 	if (error != 0) {
6294 		free(nd, M_TEMP);
6295 		return (error);
6296 	}
6297 	NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6298 	if (nd->nd_repstat == 0) {
6299 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6300 		if (*tl++ == newnfs_true)
6301 			*eofp = true;
6302 		else
6303 			*eofp = false;
6304 		*offp = fxdr_hyper(tl);
6305 	} else
6306 		error = nd->nd_repstat;
6307 nfsmout:
6308 	m_freem(nd->nd_mrep);
6309 	free(nd, M_TEMP);
6310 	NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6311 	return (error);
6312 }
6313 
6314 /*
6315  * Get the device id and file handle for a DS file.
6316  */
6317 int
6318 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6319     fhandle_t *fhp, char *devid)
6320 {
6321 	int buflen, error;
6322 	char *buf;
6323 
6324 	buflen = 1024;
6325 	buf = malloc(buflen, M_TEMP, M_WAITOK);
6326 	error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6327 	    fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6328 	free(buf, M_TEMP);
6329 	return (error);
6330 }
6331 
6332 /*
6333  * Do a Lookup against the DS for the filename.
6334  */
6335 static int
6336 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6337     struct vnode **nvpp, NFSPROC_T *p)
6338 {
6339 	struct nameidata named;
6340 	struct ucred *tcred;
6341 	char *bufp;
6342 	u_long *hashp;
6343 	struct vnode *nvp;
6344 	int error;
6345 
6346 	tcred = newnfs_getcred();
6347 	named.ni_cnd.cn_nameiop = LOOKUP;
6348 	named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6349 	named.ni_cnd.cn_cred = tcred;
6350 	named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
6351 	nfsvno_setpathbuf(&named, &bufp, &hashp);
6352 	named.ni_cnd.cn_nameptr = bufp;
6353 	named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6354 	strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6355 	NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6356 	error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6357 	NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6358 	NFSFREECRED(tcred);
6359 	nfsvno_relpathbuf(&named);
6360 	if (error == 0)
6361 		*nvpp = nvp;
6362 	NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6363 	return (error);
6364 }
6365 
6366 /*
6367  * Set the file handle to the correct one.
6368  */
6369 static void
6370 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6371     char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6372 {
6373 	struct nfsnode *np;
6374 	int ret = 0;
6375 
6376 	np = VTONFS(nvp);
6377 	NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6378 	/*
6379 	 * We can only do a vn_set_extattr() if the vnode is exclusively
6380 	 * locked and vn_start_write() has been done.  If devid != NULL or
6381 	 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6382 	 * may not have been done.
6383 	 * If not done now, it will be done on a future call.
6384 	 */
6385 	if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6386 	    LK_EXCLUSIVE)
6387 		ret = vn_extattr_set(vp, IO_NODELOCKED,
6388 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6389 		    (char *)pf, p);
6390 	NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6391 }
6392 
6393 /*
6394  * Cause RPCs waiting on "nmp" to fail.  This is called for a DS mount point
6395  * when the DS has failed.
6396  */
6397 void
6398 nfsrv_killrpcs(struct nfsmount *nmp)
6399 {
6400 
6401 	/*
6402 	 * Call newnfs_nmcancelreqs() to cause
6403 	 * any RPCs in progress on the mount point to
6404 	 * fail.
6405 	 * This will cause any process waiting for an
6406 	 * RPC to complete while holding a vnode lock
6407 	 * on the mounted-on vnode (such as "df" or
6408 	 * a non-forced "umount") to fail.
6409 	 * This will unlock the mounted-on vnode so
6410 	 * a forced dismount can succeed.
6411 	 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6412 	 * called.
6413 	 */
6414 	newnfs_nmcancelreqs(nmp);
6415 }
6416 
6417 /*
6418  * Sum up the statfs info for each of the DSs, so that the client will
6419  * receive the total for all DSs.
6420  */
6421 static int
6422 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6423 {
6424 	struct statfs *tsf;
6425 	struct nfsdevice *ds;
6426 	struct vnode **dvpp, **tdvpp, *dvp;
6427 	uint64_t tot;
6428 	int cnt, error = 0, i;
6429 
6430 	if (nfsrv_devidcnt <= 0)
6431 		return (ENXIO);
6432 	dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6433 	tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6434 
6435 	/* Get an array of the dvps for the DSs. */
6436 	tdvpp = dvpp;
6437 	i = 0;
6438 	NFSDDSLOCK();
6439 	/* First, search for matches for same file system. */
6440 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6441 		if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6442 		    fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6443 			if (++i > nfsrv_devidcnt)
6444 				break;
6445 			*tdvpp++ = ds->nfsdev_dvp;
6446 		}
6447 	}
6448 	/*
6449 	 * If no matches for same file system, total all servers not assigned
6450 	 * to a file system.
6451 	 */
6452 	if (i == 0) {
6453 		TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6454 			if (ds->nfsdev_nmp != NULL &&
6455 			    ds->nfsdev_mdsisset == 0) {
6456 				if (++i > nfsrv_devidcnt)
6457 					break;
6458 				*tdvpp++ = ds->nfsdev_dvp;
6459 			}
6460 		}
6461 	}
6462 	NFSDDSUNLOCK();
6463 	cnt = i;
6464 
6465 	/* Do a VFS_STATFS() for each of the DSs and sum them up. */
6466 	tdvpp = dvpp;
6467 	for (i = 0; i < cnt && error == 0; i++) {
6468 		dvp = *tdvpp++;
6469 		error = VFS_STATFS(dvp->v_mount, tsf);
6470 		if (error == 0) {
6471 			if (sf->f_bsize == 0) {
6472 				if (tsf->f_bsize > 0)
6473 					sf->f_bsize = tsf->f_bsize;
6474 				else
6475 					sf->f_bsize = 8192;
6476 			}
6477 			if (tsf->f_blocks > 0) {
6478 				if (sf->f_bsize != tsf->f_bsize) {
6479 					tot = tsf->f_blocks * tsf->f_bsize;
6480 					sf->f_blocks += (tot / sf->f_bsize);
6481 				} else
6482 					sf->f_blocks += tsf->f_blocks;
6483 			}
6484 			if (tsf->f_bfree > 0) {
6485 				if (sf->f_bsize != tsf->f_bsize) {
6486 					tot = tsf->f_bfree * tsf->f_bsize;
6487 					sf->f_bfree += (tot / sf->f_bsize);
6488 				} else
6489 					sf->f_bfree += tsf->f_bfree;
6490 			}
6491 			if (tsf->f_bavail > 0) {
6492 				if (sf->f_bsize != tsf->f_bsize) {
6493 					tot = tsf->f_bavail * tsf->f_bsize;
6494 					sf->f_bavail += (tot / sf->f_bsize);
6495 				} else
6496 					sf->f_bavail += tsf->f_bavail;
6497 			}
6498 		}
6499 	}
6500 	free(tsf, M_TEMP);
6501 	free(dvpp, M_TEMP);
6502 	return (error);
6503 }
6504 
6505 /*
6506  * Set an NFSv4 acl.
6507  */
6508 int
6509 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6510 {
6511 	int error;
6512 
6513 	if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6514 		error = NFSERR_ATTRNOTSUPP;
6515 		goto out;
6516 	}
6517 	/*
6518 	 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6519 	 * Make sure it has enough room for that - splitting every entry
6520 	 * into two and appending "canonical six" entries at the end.
6521 	 * Cribbed out of kern/vfs_acl.c - Rick M.
6522 	 */
6523 	if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6524 		error = NFSERR_ATTRNOTSUPP;
6525 		goto out;
6526 	}
6527 	error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6528 	if (error == 0) {
6529 		error = nfsrv_dssetacl(vp, aclp, cred, p);
6530 		if (error == ENOENT)
6531 			error = 0;
6532 	}
6533 
6534 out:
6535 	NFSEXITCODE(error);
6536 	return (error);
6537 }
6538 
6539 /*
6540  * Seek vnode op call (actually it is a VOP_IOCTL()).
6541  * This function is called with the vnode locked, but unlocks and vrele()s
6542  * the vp before returning.
6543  */
6544 int
6545 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6546     off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6547 {
6548 	struct nfsvattr at;
6549 	int error, ret;
6550 
6551 	ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6552 	/*
6553 	 * Attempt to seek on a DS file. A return of ENOENT implies
6554 	 * there is no DS file to seek on.
6555 	 */
6556 	error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6557 	    NULL, NULL, NULL, NULL, offp, content, eofp);
6558 	if (error != ENOENT) {
6559 		vput(vp);
6560 		return (error);
6561 	}
6562 
6563 	/*
6564 	 * Do the VOP_IOCTL() call.  For the case where *offp == file_size,
6565 	 * VOP_IOCTL() will return ENXIO.  However, the correct reply for
6566 	 * NFSv4.2 is *eofp == true and error == 0 for this case.
6567 	 */
6568 	NFSVOPUNLOCK(vp);
6569 	error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6570 	*eofp = false;
6571 	if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6572 		/* Handle the cases where we might be at EOF. */
6573 		ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6574 		if (ret == 0 && *offp == at.na_size) {
6575 			*eofp = true;
6576 			error = 0;
6577 		}
6578 		if (ret != 0 && error == 0)
6579 			error = ret;
6580 	}
6581 	vrele(vp);
6582 	NFSEXITCODE(error);
6583 	return (error);
6584 }
6585 
6586 /*
6587  * Allocate vnode op call.
6588  */
6589 int
6590 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6591     NFSPROC_T *p)
6592 {
6593 	int error;
6594 	off_t olen;
6595 
6596 	ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6597 	/*
6598 	 * Attempt to allocate on a DS file. A return of ENOENT implies
6599 	 * there is no DS file to allocate on.
6600 	 */
6601 	error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6602 	    NULL, NULL, NULL, NULL, &len, 0, NULL);
6603 	if (error != ENOENT)
6604 		return (error);
6605 
6606 	/*
6607 	 * Do the actual VOP_ALLOCATE(), looping so long as
6608 	 * progress is being made, to achieve completion.
6609 	 */
6610 	do {
6611 		olen = len;
6612 		error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred);
6613 		if (error == 0 && len > 0 && olen > len)
6614 			maybe_yield();
6615 	} while (error == 0 && len > 0 && olen > len);
6616 	if (error == 0 && len > 0)
6617 		error = NFSERR_IO;
6618 	NFSEXITCODE(error);
6619 	return (error);
6620 }
6621 
6622 /*
6623  * Deallocate vnode op call.
6624  */
6625 int
6626 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6627     NFSPROC_T *p)
6628 {
6629 	int error;
6630 	off_t olen;
6631 
6632 	ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp");
6633 	/*
6634 	 * Attempt to deallocate on a DS file. A return of ENOENT implies
6635 	 * there is no DS file to deallocate on.
6636 	 */
6637 	error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL,
6638 	    NULL, NULL, NULL, NULL, &len, 0, NULL);
6639 	if (error != ENOENT)
6640 		return (error);
6641 
6642 	/*
6643 	 * Do the actual VOP_DEALLOCATE(), looping so long as
6644 	 * progress is being made, to achieve completion.
6645 	 */
6646 	do {
6647 		olen = len;
6648 		error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred);
6649 		if (error == 0 && len > 0 && olen > len)
6650 			maybe_yield();
6651 	} while (error == 0 && len > 0 && olen > len);
6652 	if (error == 0 && len > 0)
6653 		error = NFSERR_IO;
6654 	NFSEXITCODE(error);
6655 	return (error);
6656 }
6657 
6658 /*
6659  * Get Extended Atribute vnode op into an mbuf list.
6660  */
6661 int
6662 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6663     struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6664     struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6665 {
6666 	struct iovec *iv;
6667 	struct uio io, *uiop = &io;
6668 	struct mbuf *m, *m2;
6669 	int alen, error, len, tlen;
6670 	size_t siz;
6671 
6672 	/* First, find out the size of the extended attribute. */
6673 	error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6674 	    &siz, cred, p);
6675 	if (error != 0)
6676 		return (NFSERR_NOXATTR);
6677 	if (siz > maxresp - NFS_MAXXDR)
6678 		return (NFSERR_XATTR2BIG);
6679 	len = siz;
6680 	tlen = NFSM_RNDUP(len);
6681 	if (tlen > 0) {
6682 		/*
6683 		 * If cnt > MCLBYTES and the reply will not be saved, use
6684 		 * ext_pgs mbufs for TLS.
6685 		 * For NFSv4.0, we do not know for sure if the reply will
6686 		 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6687 		 * Always use ext_pgs mbufs if ND_EXTPG is set.
6688 		 */
6689 		if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6690 		    (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6691 		    (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6692 			uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6693 			    maxextsiz, &m, &m2, &iv);
6694 		else
6695 			uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6696 			    &iv);
6697 		uiop->uio_iov = iv;
6698 	} else {
6699 		uiop->uio_iovcnt = 0;
6700 		uiop->uio_iov = iv = NULL;
6701 		m = m2 = NULL;
6702 	}
6703 	uiop->uio_offset = 0;
6704 	uiop->uio_resid = tlen;
6705 	uiop->uio_rw = UIO_READ;
6706 	uiop->uio_segflg = UIO_SYSSPACE;
6707 	uiop->uio_td = p;
6708 #ifdef MAC
6709 	error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6710 	    name);
6711 	if (error != 0)
6712 		goto out;
6713 #endif
6714 
6715 	if (tlen > 0)
6716 		error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6717 		    NULL, cred, p);
6718 	if (error != 0)
6719 		goto out;
6720 	if (uiop->uio_resid > 0) {
6721 		alen = tlen;
6722 		len = tlen - uiop->uio_resid;
6723 		tlen = NFSM_RNDUP(len);
6724 		if (alen != tlen)
6725 			printf("nfsvno_getxattr: weird size read\n");
6726 		if (tlen == 0) {
6727 			m_freem(m);
6728 			m = m2 = NULL;
6729 		} else if (alen != tlen || tlen != len)
6730 			m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6731 	}
6732 	*lenp = len;
6733 	*mpp = m;
6734 	*mpendp = m2;
6735 
6736 out:
6737 	if (error != 0) {
6738 		if (m != NULL)
6739 			m_freem(m);
6740 		*lenp = 0;
6741 	}
6742 	free(iv, M_TEMP);
6743 	NFSEXITCODE(error);
6744 	return (error);
6745 }
6746 
6747 /*
6748  * Set Extended attribute vnode op from an mbuf list.
6749  */
6750 int
6751 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6752     char *cp, struct ucred *cred, struct thread *p)
6753 {
6754 	struct iovec *iv;
6755 	struct uio uio, *uiop = &uio;
6756 	int cnt, error;
6757 
6758 	error = 0;
6759 #ifdef MAC
6760 	error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6761 	    name);
6762 #endif
6763 	if (error != 0)
6764 		goto out;
6765 
6766 	uiop->uio_rw = UIO_WRITE;
6767 	uiop->uio_segflg = UIO_SYSSPACE;
6768 	uiop->uio_td = p;
6769 	uiop->uio_offset = 0;
6770 	uiop->uio_resid = len;
6771 	if (len > 0) {
6772 		error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6773 		uiop->uio_iov = iv;
6774 		uiop->uio_iovcnt = cnt;
6775 	} else {
6776 		uiop->uio_iov = iv = NULL;
6777 		uiop->uio_iovcnt = 0;
6778 	}
6779 	if (error == 0) {
6780 		error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6781 		    cred, p);
6782 		if (error == 0) {
6783 			if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6784 				nfsvno_updateds(vp, cred, p);
6785 			error = VOP_FSYNC(vp, MNT_WAIT, p);
6786 		}
6787 		free(iv, M_TEMP);
6788 	}
6789 
6790 out:
6791 	NFSEXITCODE(error);
6792 	return (error);
6793 }
6794 
6795 /*
6796  * For a pNFS server, the DS file's ctime and
6797  * va_filerev (TimeMetadata and Change) needs to
6798  * be updated.  This is a hack, but works by
6799  * flipping the S_ISGID bit in va_mode and then
6800  * flipping it back.
6801  * It does result in two MDS->DS RPCs, but creating
6802  * a custom RPC just to do this seems overkill, since
6803  * Setxattr/Rmxattr will not be done that frequently.
6804  * If it fails part way through, that is not too
6805  * serious, since the DS file is never executed.
6806  */
6807 static void
6808 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p)
6809 {
6810 	struct nfsvattr nva;
6811 	int ret;
6812 	u_short tmode;
6813 
6814 	ret = VOP_GETATTR(vp, &nva.na_vattr, cred);
6815 	if (ret == 0) {
6816 		tmode = nva.na_mode;
6817 		NFSVNO_ATTRINIT(&nva);
6818 		tmode ^= S_ISGID;
6819 		NFSVNO_SETATTRVAL(&nva, mode, tmode);
6820 		ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6821 		    NFSPROC_SETATTR, NULL, NULL, NULL, &nva,
6822 		    NULL, NULL, 0, NULL);
6823 		if (ret == 0) {
6824 			tmode ^= S_ISGID;
6825 			NFSVNO_SETATTRVAL(&nva, mode, tmode);
6826 			ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6827 			    NFSPROC_SETATTR, NULL, NULL, NULL,
6828 			    &nva, NULL, NULL, 0, NULL);
6829 		}
6830 	}
6831 }
6832 
6833 /*
6834  * Remove Extended attribute vnode op.
6835  */
6836 int
6837 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6838     struct ucred *cred, struct thread *p)
6839 {
6840 	int error;
6841 
6842 	/*
6843 	 * Get rid of any delegations.  I am not sure why this is required,
6844 	 * but RFC-8276 says so.
6845 	 */
6846 	error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6847 	if (error != 0)
6848 		goto out;
6849 #ifdef MAC
6850 	error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6851 	    name);
6852 	if (error != 0)
6853 		goto out;
6854 #endif
6855 
6856 	error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6857 	if (error == EOPNOTSUPP)
6858 		error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6859 		    cred, p);
6860 	if (error == 0) {
6861 		if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6862 			nfsvno_updateds(vp, cred, p);
6863 		error = VOP_FSYNC(vp, MNT_WAIT, p);
6864 	}
6865 out:
6866 	NFSEXITCODE(error);
6867 	return (error);
6868 }
6869 
6870 /*
6871  * List Extended Atribute vnode op into an mbuf list.
6872  */
6873 int
6874 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6875     struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6876 {
6877 	struct iovec iv;
6878 	struct uio io;
6879 	int error;
6880 	size_t siz;
6881 
6882 	*bufp = NULL;
6883 	/* First, find out the size of the extended attribute. */
6884 	error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6885 	    p);
6886 	if (error != 0)
6887 		return (NFSERR_NOXATTR);
6888 	if (siz <= cookie) {
6889 		*lenp = 0;
6890 		*eofp = true;
6891 		goto out;
6892 	}
6893 	if (siz > cookie + *lenp) {
6894 		siz = cookie + *lenp;
6895 		*eofp = false;
6896 	} else
6897 		*eofp = true;
6898 	/* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6899 	if (siz > 10 * 1024 * 1024) {
6900 		error = NFSERR_XATTR2BIG;
6901 		goto out;
6902 	}
6903 	*bufp = malloc(siz, M_TEMP, M_WAITOK);
6904 	iv.iov_base = *bufp;
6905 	iv.iov_len = siz;
6906 	io.uio_iovcnt = 1;
6907 	io.uio_iov = &iv;
6908 	io.uio_offset = 0;
6909 	io.uio_resid = siz;
6910 	io.uio_rw = UIO_READ;
6911 	io.uio_segflg = UIO_SYSSPACE;
6912 	io.uio_td = p;
6913 #ifdef MAC
6914 	error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6915 	if (error != 0)
6916 		goto out;
6917 #endif
6918 
6919 	error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6920 	    p);
6921 	if (error != 0)
6922 		goto out;
6923 	if (io.uio_resid > 0)
6924 		siz -= io.uio_resid;
6925 	*lenp = siz;
6926 
6927 out:
6928 	if (error != 0) {
6929 		free(*bufp, M_TEMP);
6930 		*bufp = NULL;
6931 	}
6932 	NFSEXITCODE(error);
6933 	return (error);
6934 }
6935 
6936 /*
6937  * Trim trailing data off the mbuf list being built.
6938  */
6939 void
6940 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6941     int bextpg, int bextpgsiz)
6942 {
6943 	vm_page_t pg;
6944 	int fullpgsiz, i;
6945 
6946 	if (mb->m_next != NULL) {
6947 		m_freem(mb->m_next);
6948 		mb->m_next = NULL;
6949 	}
6950 	if ((mb->m_flags & M_EXTPG) != 0) {
6951 		KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
6952 		    ("nfsm_trimtrailing: bextpg out of range"));
6953 		KASSERT(bpos == (char *)(void *)
6954 		    PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
6955 		    ("nfsm_trimtrailing: bextpgsiz bad!"));
6956 
6957 		/* First, get rid of any pages after this position. */
6958 		for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
6959 			pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
6960 			vm_page_unwire_noq(pg);
6961 			vm_page_free(pg);
6962 		}
6963 		mb->m_epg_npgs = bextpg + 1;
6964 		if (bextpg == 0)
6965 			fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
6966 		else
6967 			fullpgsiz = PAGE_SIZE;
6968 		mb->m_epg_last_len = fullpgsiz - bextpgsiz;
6969 		mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
6970 		for (i = 1; i < mb->m_epg_npgs; i++)
6971 			mb->m_len += m_epg_pagelen(mb, i, 0);
6972 		nd->nd_bextpgsiz = bextpgsiz;
6973 		nd->nd_bextpg = bextpg;
6974 	} else
6975 		mb->m_len = bpos - mtod(mb, char *);
6976 	nd->nd_mb = mb;
6977 	nd->nd_bpos = bpos;
6978 }
6979 
6980 
6981 /*
6982  * Check to see if a put file handle operation should test for
6983  * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
6984  * When Open is the next operation, NFSERR_WRONGSEC cannot be
6985  * replied for the Open cases that use a component.  This can
6986  * be identified by the fact that the file handle's type is VDIR.
6987  */
6988 bool
6989 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, enum vtype vtyp)
6990 {
6991 
6992 	if ((nd->nd_flag & ND_NFSV4) == 0)
6993 		return (true);
6994 
6995 	if ((nd->nd_flag & ND_LASTOP) != 0)
6996 		return (false);
6997 
6998 	if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
6999 	    nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
7000 	    nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
7001 	    nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
7002 		return (false);
7003 	if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
7004 		return (false);
7005 	return (true);
7006 }
7007 
7008 /*
7009  * Check DSs marked no space.
7010  */
7011 void
7012 nfsrv_checknospc(void)
7013 {
7014 	struct statfs *tsf;
7015 	struct nfsdevice *ds;
7016 	struct vnode **dvpp, **tdvpp, *dvp;
7017 	char *devid, *tdevid;
7018 	int cnt, error = 0, i;
7019 
7020 	if (nfsrv_devidcnt <= 0)
7021 		return;
7022 	dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
7023 	devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK);
7024 	tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
7025 
7026 	/* Get an array of the dvps for the DSs. */
7027 	tdvpp = dvpp;
7028 	tdevid = devid;
7029 	i = 0;
7030 	NFSDDSLOCK();
7031 	/* First, search for matches for same file system. */
7032 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7033 		if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) {
7034 			if (++i > nfsrv_devidcnt)
7035 				break;
7036 			*tdvpp++ = ds->nfsdev_dvp;
7037 			NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID);
7038 			tdevid += NFSX_V4DEVICEID;
7039 		}
7040 	}
7041 	NFSDDSUNLOCK();
7042 
7043 	/* Do a VFS_STATFS() for each of the DSs and clear no space. */
7044 	cnt = i;
7045 	tdvpp = dvpp;
7046 	tdevid = devid;
7047 	for (i = 0; i < cnt && error == 0; i++) {
7048 		dvp = *tdvpp++;
7049 		error = VFS_STATFS(dvp->v_mount, tsf);
7050 		if (error == 0 && tsf->f_bavail > 0) {
7051 			NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n");
7052 			nfsrv_marknospc(tdevid, false);
7053 		}
7054 		tdevid += NFSX_V4DEVICEID;
7055 	}
7056 	free(tsf, M_TEMP);
7057 	free(dvpp, M_TEMP);
7058 	free(devid, M_TEMP);
7059 }
7060 
7061 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
7062 
7063 /*
7064  * Called once to initialize data structures...
7065  */
7066 static int
7067 nfsd_modevent(module_t mod, int type, void *data)
7068 {
7069 	int error = 0, i;
7070 	static int loaded = 0;
7071 
7072 	switch (type) {
7073 	case MOD_LOAD:
7074 		if (loaded)
7075 			goto out;
7076 		newnfs_portinit();
7077 		for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7078 			mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
7079 			    MTX_DEF);
7080 			mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
7081 			    MTX_DEF);
7082 		}
7083 		mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
7084 		mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
7085 		mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
7086 		mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
7087 		mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
7088 		lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
7089 		callout_init(&nfsd_callout, 1);
7090 		nfsrvd_initcache();
7091 		nfsd_init();
7092 		NFSD_LOCK();
7093 		nfsrvd_init(0);
7094 		NFSD_UNLOCK();
7095 		nfsd_mntinit();
7096 #ifdef VV_DISABLEDELEG
7097 		vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
7098 		vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
7099 #endif
7100 		nfsd_call_nfsd = nfssvc_nfsd;
7101 		loaded = 1;
7102 		break;
7103 
7104 	case MOD_UNLOAD:
7105 		if (newnfs_numnfsd != 0) {
7106 			error = EBUSY;
7107 			break;
7108 		}
7109 
7110 #ifdef VV_DISABLEDELEG
7111 		vn_deleg_ops.vndeleg_recall = NULL;
7112 		vn_deleg_ops.vndeleg_disable = NULL;
7113 #endif
7114 		nfsd_call_nfsd = NULL;
7115 		callout_drain(&nfsd_callout);
7116 
7117 		/* Clean out all NFSv4 state. */
7118 		nfsrv_throwawayallstate(curthread);
7119 
7120 		/* Clean the NFS server reply cache */
7121 		nfsrvd_cleancache();
7122 
7123 		/* Free up the krpc server pool. */
7124 		if (nfsrvd_pool != NULL)
7125 			svcpool_destroy(nfsrvd_pool);
7126 
7127 		/* and get rid of the locks */
7128 		for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7129 			mtx_destroy(&nfsrchash_table[i].mtx);
7130 			mtx_destroy(&nfsrcahash_table[i].mtx);
7131 		}
7132 		mtx_destroy(&nfsrc_udpmtx);
7133 		mtx_destroy(&nfs_v4root_mutex);
7134 		mtx_destroy(&nfsv4root_mnt.mnt_mtx);
7135 		mtx_destroy(&nfsrv_dontlistlock_mtx);
7136 		mtx_destroy(&nfsrv_recalllock_mtx);
7137 		for (i = 0; i < nfsrv_sessionhashsize; i++)
7138 			mtx_destroy(&nfssessionhash[i].mtx);
7139 		if (nfslayouthash != NULL) {
7140 			for (i = 0; i < nfsrv_layouthashsize; i++)
7141 				mtx_destroy(&nfslayouthash[i].mtx);
7142 			free(nfslayouthash, M_NFSDSESSION);
7143 		}
7144 		lockdestroy(&nfsv4root_mnt.mnt_explock);
7145 		free(nfsclienthash, M_NFSDCLIENT);
7146 		free(nfslockhash, M_NFSDLOCKFILE);
7147 		free(nfssessionhash, M_NFSDSESSION);
7148 		loaded = 0;
7149 		break;
7150 	default:
7151 		error = EOPNOTSUPP;
7152 		break;
7153 	}
7154 
7155 out:
7156 	NFSEXITCODE(error);
7157 	return (error);
7158 }
7159 static moduledata_t nfsd_mod = {
7160 	"nfsd",
7161 	nfsd_modevent,
7162 	NULL,
7163 };
7164 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
7165 
7166 /* So that loader and kldload(2) can find us, wherever we are.. */
7167 MODULE_VERSION(nfsd, 1);
7168 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
7169 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
7170 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
7171 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
7172