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