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