xref: /netbsd-src/sys/nfs/nfs_serv.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: nfs_serv.c,v 1.173 2017/04/26 03:02:49 riastradh Exp $	*/
2 
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  *	@(#)nfs_serv.c	8.8 (Berkeley) 7/31/95
35  */
36 
37 /*
38  * nfs version 2 and 3 server calls to vnode ops
39  * - these routines generally have 3 phases
40  *   1 - break down and validate rpc request in mbuf list
41  *   2 - do the vnode ops for the request
42  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
43  *   3 - build the rpc reply in an mbuf list
44  *   nb:
45  *	- do not mix the phases, since the nfsm_?? macros can return failures
46  *	  on a bad rpc or similar and do not do any vrele() or vput()'s
47  *
48  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
49  *	error number iff error != 0 whereas
50  *	returning an error from the server function implies a fatal error
51  *	such as a badly constructed rpc request that should be dropped without
52  *	a reply.
53  *	For Version 3, nfsm_reply() does not return for the error case, since
54  *	most version 3 rpcs return more than the status for error cases.
55  */
56 
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.173 2017/04/26 03:02:49 riastradh Exp $");
59 
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/proc.h>
63 #include <sys/file.h>
64 #include <sys/namei.h>
65 #include <sys/vnode.h>
66 #include <sys/mount.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/mbuf.h>
70 #include <sys/dirent.h>
71 #include <sys/stat.h>
72 #include <sys/kernel.h>
73 #include <sys/hash.h>
74 #include <sys/kauth.h>
75 #include <sys/module.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallargs.h>
78 #include <sys/syscallvar.h>
79 
80 #include <uvm/uvm.h>
81 
82 #include <nfs/nfsproto.h>
83 #include <nfs/rpcv2.h>
84 #include <nfs/nfs.h>
85 #include <nfs/xdr_subs.h>
86 #include <nfs/nfsm_subs.h>
87 #include <nfs/nfs_var.h>
88 
89 MODULE(MODULE_CLASS_MISC, nfsserver, "nfs");
90 
91 /* Global vars */
92 extern u_int32_t nfs_xdrneg1;
93 extern u_int32_t nfs_false, nfs_true;
94 extern const enum vtype nv3tov_type[8];
95 extern struct nfsstats nfsstats;
96 extern const nfstype nfsv2_type[9];
97 extern const nfstype nfsv3_type[9];
98 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
99 bool nfsd_use_loan = true;	/* use page-loan for READ OP */
100 
101 #define	nqsrv_getl(vp, rw)	/* nothing */
102 
103 static const struct syscall_package nfsserver_syscalls[] = {
104 	{ SYS_nfssvc, 0, (sy_call_t *)sys_nfssvc },
105 	{ 0, 0, NULL },
106 };
107 
108 static int
109 nfsserver_modcmd(modcmd_t cmd, void *arg)
110 {
111 	extern struct vfs_hooks nfs_export_hooks;	/* XXX */
112 	int error;
113 
114 	switch (cmd) {
115 	case MODULE_CMD_INIT:
116 		error = syscall_establish(NULL, nfsserver_syscalls);
117 		if (error != 0) {
118 			return error;
119 		}
120 		nfs_init();	/* XXX for monolithic kernel */
121 		netexport_init();
122 		nfsrv_initcache();	/* Init the server request cache */
123 		nfsrv_init(0);		/* Init server data structures */
124 		vfs_hooks_attach(&nfs_export_hooks);
125 		nfs_timer_srvinit(nfsrv_timer);
126 		return 0;
127 	case MODULE_CMD_FINI:
128 		error = syscall_disestablish(NULL, nfsserver_syscalls);
129 		if (error != 0) {
130 			return error;
131 		}
132 		/*
133 		 * Kill export list before detaching VFS hooks, so we
134 		 * we don't leak state due to a concurrent umount().
135 		 */
136 		netexport_fini();
137 		vfs_hooks_detach(&nfs_export_hooks);
138 
139 		/* Kill timer before server goes away. */
140 		nfs_timer_srvfini();
141 		nfsrv_fini();
142 
143 		/* Server uses server cache, so kill cache last. */
144 		nfsrv_finicache();
145 		nfs_fini();
146 		return 0;
147 	case MODULE_CMD_AUTOUNLOAD:
148 		if (netexport_hasexports())
149 			return EBUSY;
150 		/*FALLTHROUGH*/
151 	default:
152 		return ENOTTY;
153 	}
154 }
155 
156 /*
157  * nfs v3 access service
158  */
159 int
160 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
161 {
162 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
163 	struct mbuf *nam = nfsd->nd_nam;
164 	char *dpos = nfsd->nd_dpos;
165 	kauth_cred_t cred = nfsd->nd_cr;
166 	struct vnode *vp;
167 	nfsrvfh_t nsfh;
168 	u_int32_t *tl;
169 	int32_t t1;
170 	char *bpos;
171 	int error = 0, rdonly, cache = 0, getret;
172 	char *cp2;
173 	struct mbuf *mb, *mreq __unused;
174 	struct vattr va;
175 	u_long inmode, testmode, outmode;
176 	u_quad_t frev;
177 
178 	nfsm_srvmtofh(&nsfh);
179 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
180 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
181 	    (nfsd->nd_flag & ND_KERBAUTH), false);
182 	if (error) {
183 		nfsm_reply(NFSX_UNSIGNED);
184 		nfsm_srvpostop_attr(1, (struct vattr *)0);
185 		return (0);
186 	}
187 	inmode = fxdr_unsigned(u_int32_t, *tl);
188 	outmode = 0;
189 	if ((inmode & NFSV3ACCESS_READ) &&
190 	    nfsrv_access(vp, VREAD, cred, rdonly, lwp, 0) == 0)
191 		outmode |= NFSV3ACCESS_READ;
192 	if (vp->v_type != VDIR) {
193 		testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
194 		if (testmode &&
195 		    nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0)
196 			outmode |= testmode;
197 		if ((inmode & NFSV3ACCESS_EXECUTE) &&
198 		    nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0)
199 			outmode |= NFSV3ACCESS_EXECUTE;
200 	} else {
201 		testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
202 		    NFSV3ACCESS_DELETE);
203 		if (testmode &&
204 		    nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0)
205 			outmode |= testmode;
206 		if ((inmode & NFSV3ACCESS_LOOKUP) &&
207 		    nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0)
208 			outmode |= NFSV3ACCESS_LOOKUP;
209 	}
210 	getret = VOP_GETATTR(vp, &va, cred);
211 	vput(vp);
212 	nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
213 	nfsm_srvpostop_attr(getret, &va);
214 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
215 	*tl = txdr_unsigned(outmode);
216 	nfsm_srvdone;
217 }
218 
219 /*
220  * nfs getattr service
221  */
222 int
223 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
224 {
225 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
226 	struct mbuf *nam = nfsd->nd_nam;
227 	char *dpos = nfsd->nd_dpos;
228 	kauth_cred_t cred = nfsd->nd_cr;
229 	struct nfs_fattr *fp;
230 	struct vattr va;
231 	struct vnode *vp;
232 	nfsrvfh_t nsfh;
233 	u_int32_t *tl;
234 	int32_t t1;
235 	char *bpos;
236 	int error = 0, rdonly, cache = 0;
237 	char *cp2;
238 	struct mbuf *mb, *mreq __unused;
239 	u_quad_t frev;
240 
241 	nfsm_srvmtofh(&nsfh);
242 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
243 	    (nfsd->nd_flag & ND_KERBAUTH), false);
244 	if (error) {
245 		nfsm_reply(0);
246 		return (0);
247 	}
248 	nqsrv_getl(vp, ND_READ);
249 	error = VOP_GETATTR(vp, &va, cred);
250 	vput(vp);
251 	nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
252 	if (error)
253 		return (0);
254 	nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
255 	nfsm_srvfillattr(&va, fp);
256 	nfsm_srvdone;
257 }
258 
259 /*
260  * nfs setattr service
261  */
262 int
263 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
264 {
265 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
266 	struct mbuf *nam = nfsd->nd_nam;
267 	char *dpos = nfsd->nd_dpos;
268 	kauth_cred_t cred = nfsd->nd_cr;
269 	struct vattr va, preat;
270 	struct nfsv2_sattr *sp;
271 	struct nfs_fattr *fp;
272 	struct vnode *vp;
273 	nfsrvfh_t nsfh;
274 	u_int32_t *tl;
275 	int32_t t1;
276 	char *bpos;
277 	int error = 0, rdonly, cache = 0, preat_ret = 1, postat_ret = 1;
278 	int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
279 	char *cp2;
280 	struct mbuf *mb, *mreq __unused;
281 	u_quad_t frev;
282 	struct timespec guard;
283 
284 	memset(&guard, 0, sizeof guard);	/* XXX gcc */
285 
286 	nfsm_srvmtofh(&nsfh);
287 	vattr_null(&va);
288 	if (v3) {
289 		nfsm_srvsattr(&va);
290 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
291 		gcheck = fxdr_unsigned(int, *tl);
292 		if (gcheck) {
293 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
294 			fxdr_nfsv3time(tl, &guard);
295 		}
296 	} else {
297 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
298 		/*
299 		 * Nah nah nah nah na nah
300 		 * There is a bug in the Sun client that puts 0xffff in the mode
301 		 * field of sattr when it should put in 0xffffffff. The u_short
302 		 * doesn't sign extend.
303 		 * --> check the low order 2 bytes for 0xffff
304 		 */
305 		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
306 			va.va_mode = nfstov_mode(sp->sa_mode);
307 		if (sp->sa_uid != nfs_xdrneg1)
308 			va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
309 		if (sp->sa_gid != nfs_xdrneg1)
310 			va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
311 		if (sp->sa_size != nfs_xdrneg1)
312 			va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
313 		if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
314 #ifdef notyet
315 			fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
316 #else
317 			va.va_atime.tv_sec =
318 				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
319 			va.va_atime.tv_nsec = 0;
320 #endif
321 		}
322 		if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
323 			fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
324 
325 	}
326 
327 	/*
328 	 * Now that we have all the fields, lets do it.
329 	 */
330 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
331 	    (nfsd->nd_flag & ND_KERBAUTH), false);
332 	if (error) {
333 		nfsm_reply(2 * NFSX_UNSIGNED);
334 		nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
335 		return (0);
336 	}
337 	nqsrv_getl(vp, ND_WRITE);
338 	if (v3) {
339 		error = preat_ret = VOP_GETATTR(vp, &preat, cred);
340 		if (!error && gcheck &&
341 			(preat.va_ctime.tv_sec != guard.tv_sec ||
342 			 preat.va_ctime.tv_nsec != guard.tv_nsec))
343 			error = NFSERR_NOT_SYNC;
344 		if (error) {
345 			vput(vp);
346 			nfsm_reply(NFSX_WCCDATA(v3));
347 			nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
348 			return (0);
349 		}
350 	}
351 
352 	/*
353 	 * If the size is being changed write acces is required, otherwise
354 	 * just check for a read only file system.
355 	 */
356 	if (va.va_size == ((u_quad_t)((quad_t) -1))) {
357 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
358 			error = EROFS;
359 			goto out;
360 		}
361 	} else {
362 		if (vp->v_type == VDIR) {
363 			error = EISDIR;
364 			goto out;
365 		} else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
366 			lwp, 0)) != 0)
367 			goto out;
368 	}
369 	error = VOP_SETATTR(vp, &va, cred);
370 	postat_ret = VOP_GETATTR(vp, &va, cred);
371 	if (!error)
372 		error = postat_ret;
373 out:
374 	vput(vp);
375 	nfsm_reply(NFSX_WCCORFATTR(v3));
376 	if (v3) {
377 		nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
378 		return (0);
379 	} else {
380 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
381 		nfsm_srvfillattr(&va, fp);
382 	}
383 	nfsm_srvdone;
384 }
385 
386 /*
387  * nfs lookup rpc
388  */
389 int
390 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
391 {
392 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
393 	struct mbuf *nam = nfsd->nd_nam;
394 	char *dpos = nfsd->nd_dpos;
395 	kauth_cred_t cred = nfsd->nd_cr;
396 	struct nfs_fattr *fp;
397 	struct nameidata nd, ind, *ndp = &nd;
398 	struct pathbuf *ipb = NULL;
399 	struct vnode *vp, *dirp;
400 	nfsrvfh_t nsfh;
401 	char *cp;
402 	u_int32_t *tl;
403 	int32_t t1;
404 	char *bpos;
405 	int error = 0, cache = 0, dirattr_ret = 1;
406 	uint32_t len;
407 	int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
408 	char *cp2;
409 	struct mbuf *mb, *mreq __unused;
410 	struct vattr va, dirattr;
411 	u_quad_t frev;
412 
413 	nfsm_srvmtofh(&nsfh);
414 	nfsm_srvnamesiz(len);
415 
416 	pubflag = nfs_ispublicfh(&nsfh);
417 
418 	nd.ni_cnd.cn_cred = cred;
419 	nd.ni_cnd.cn_nameiop = LOOKUP;
420 	nd.ni_cnd.cn_flags = LOCKLEAF;
421 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
422 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
423 
424 	if (!error && pubflag) {
425 		if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL &&
426 		    (ipb = pathbuf_create(nfs_pub.np_index)) != NULL) {
427 			/*
428 			 * Setup call to lookup() to see if we can find
429 			 * the index file. Arguably, this doesn't belong
430 			 * in a kernel.. Ugh.
431 			 */
432 			ind = nd;
433 			VOP_UNLOCK(nd.ni_vp);
434 			ind.ni_pathbuf = ipb;
435 
436 			error = lookup_for_nfsd_index(&ind, nd.ni_vp);
437 			if (!error) {
438 				/*
439 				 * Found an index file. Get rid of
440 				 * the old references.
441 				 */
442 				if (dirp)
443 					vrele(dirp);
444 				dirp = nd.ni_vp;
445 				ndp = &ind;
446 			} else
447 				error = 0;
448 		}
449 		/*
450 		 * If the public filehandle was used, check that this lookup
451 		 * didn't result in a filehandle outside the publicly exported
452 		 * filesystem.
453 		 */
454 
455 		if (!error && ndp->ni_vp->v_mount != nfs_pub.np_mount) {
456 			vput(nd.ni_vp);
457 			error = EPERM;
458 		}
459 	}
460 
461 	if (error) {
462 		if (nd.ni_pathbuf != NULL) {
463 			pathbuf_destroy(nd.ni_pathbuf);
464 		}
465 		if (ipb != NULL) {
466 			pathbuf_destroy(ipb);
467 		}
468 		if (dirp) {
469 			if (v3) {
470 				vn_lock(dirp, LK_SHARED | LK_RETRY);
471 				dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred);
472 				vput(dirp);
473 			} else
474 				vrele(dirp);
475 		}
476 		nfsm_reply(NFSX_POSTOPATTR(v3));
477 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
478 		return (0);
479 	}
480 
481 	nqsrv_getl(ndp->ni_startdir, ND_READ);
482 	pathbuf_destroy(nd.ni_pathbuf);
483 	if (ipb != NULL) {
484 		pathbuf_destroy(ipb);
485 	}
486 	vp = ndp->ni_vp;
487 	error = nfsrv_composefh(vp, &nsfh, v3);
488 	if (!error)
489 		error = VOP_GETATTR(vp, &va, cred);
490 	vput(vp);
491 	if (dirp) {
492 		if (v3) {
493 			vn_lock(dirp, LK_SHARED | LK_RETRY);
494 			dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred);
495 			vput(dirp);
496 		} else
497 			vrele(dirp);
498 	}
499 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPORFATTR(v3) +
500 	    NFSX_POSTOPATTR(v3));
501 	if (error) {
502 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
503 		return (0);
504 	}
505 	nfsm_srvfhtom(&nsfh, v3);
506 	if (v3) {
507 		nfsm_srvpostop_attr(0, &va);
508 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
509 	} else {
510 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
511 		nfsm_srvfillattr(&va, fp);
512 	}
513 	nfsm_srvdone;
514 }
515 
516 /*
517  * nfs readlink service
518  */
519 int
520 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
521 {
522 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
523 	struct mbuf *nam = nfsd->nd_nam;
524 	char *dpos = nfsd->nd_dpos;
525 	kauth_cred_t cred = nfsd->nd_cr;
526 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
527 	struct iovec *ivp = iv;
528 	struct mbuf *mp;
529 	u_int32_t *tl;
530 	int32_t t1;
531 	char *bpos;
532 	int error = 0, rdonly, cache = 0, i, padlen, getret;
533 	uint32_t len;
534 	int v3 = (nfsd->nd_flag & ND_NFSV3);
535 	char *cp2;
536 	struct mbuf *mb, *mp2 = NULL, *mp3 = NULL, *mreq __unused;
537 	struct vnode *vp;
538 	struct vattr attr;
539 	nfsrvfh_t nsfh;
540 	struct uio io, *uiop = &io;
541 	u_quad_t frev;
542 
543 	nfsm_srvmtofh(&nsfh);
544 	len = 0;
545 	i = 0;
546 	while (len < NFS_MAXPATHLEN) {
547 		mp = m_get(M_WAIT, MT_DATA);
548 		MCLAIM(mp, &nfs_mowner);
549 		m_clget(mp, M_WAIT);
550 		mp->m_len = NFSMSIZ(mp);
551 		if (len == 0)
552 			mp3 = mp2 = mp;
553 		else {
554 			mp2->m_next = mp;
555 			mp2 = mp;
556 		}
557 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
558 			mp->m_len = NFS_MAXPATHLEN-len;
559 			len = NFS_MAXPATHLEN;
560 		} else
561 			len += mp->m_len;
562 		ivp->iov_base = mtod(mp, void *);
563 		ivp->iov_len = mp->m_len;
564 		i++;
565 		ivp++;
566 	}
567 	uiop->uio_iov = iv;
568 	uiop->uio_iovcnt = i;
569 	uiop->uio_offset = 0;
570 	uiop->uio_resid = len;
571 	uiop->uio_rw = UIO_READ;
572 	UIO_SETUP_SYSSPACE(uiop);
573 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
574 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
575 	if (error) {
576 		m_freem(mp3);
577 		nfsm_reply(2 * NFSX_UNSIGNED);
578 		nfsm_srvpostop_attr(1, (struct vattr *)0);
579 		return (0);
580 	}
581 	if (vp->v_type != VLNK) {
582 		if (v3)
583 			error = EINVAL;
584 		else
585 			error = ENXIO;
586 		goto out;
587 	}
588 	nqsrv_getl(vp, ND_READ);
589 	error = VOP_READLINK(vp, uiop, cred);
590 out:
591 	getret = VOP_GETATTR(vp, &attr, cred);
592 	vput(vp);
593 	if (error)
594 		m_freem(mp3);
595 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
596 	if (v3) {
597 		nfsm_srvpostop_attr(getret, &attr);
598 		if (error)
599 			return (0);
600 	}
601 	len -= uiop->uio_resid;
602 	padlen = nfsm_padlen(len);
603 	if (uiop->uio_resid || padlen)
604 		nfs_zeropad(mp3, uiop->uio_resid, padlen);
605 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
606 	*tl = txdr_unsigned(len);
607 	mb->m_next = mp3;
608 	nfsm_srvdone;
609 }
610 
611 /*
612  * nfs read service
613  */
614 int
615 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
616 {
617 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
618 	struct mbuf *nam = nfsd->nd_nam;
619 	char *dpos = nfsd->nd_dpos;
620 	kauth_cred_t cred = nfsd->nd_cr;
621 	struct mbuf *m;
622 	struct nfs_fattr *fp;
623 	u_int32_t *tl;
624 	int32_t t1;
625 	int i;
626 	char *bpos;
627 	int error = 0, rdonly, cache = 0, getret;
628 	int v3 = (nfsd->nd_flag & ND_NFSV3);
629 	uint32_t reqlen, len, cnt, left;
630 	int padlen;
631 	char *cp2;
632 	struct mbuf *mb, *mreq;
633 	struct vnode *vp;
634 	nfsrvfh_t nsfh;
635 	struct uio io, *uiop = &io;
636 	struct vattr va;
637 	off_t off;
638 	u_quad_t frev;
639 
640 	nfsm_srvmtofh(&nsfh);
641 	if (v3) {
642 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
643 		off = fxdr_hyper(tl);
644 	} else {
645 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
646 		off = (off_t)fxdr_unsigned(u_int32_t, *tl);
647 	}
648 	nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
649 	reqlen = fxdr_unsigned(uint32_t, *tl);
650 	reqlen = MIN(reqlen, NFS_SRVMAXDATA(nfsd));
651 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
652 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
653 	if (error) {
654 		nfsm_reply(2 * NFSX_UNSIGNED);
655 		nfsm_srvpostop_attr(1, (struct vattr *)0);
656 		return (0);
657 	}
658 	if (vp->v_type != VREG) {
659 		if (v3)
660 			error = EINVAL;
661 		else
662 			error = (vp->v_type == VDIR) ? EISDIR : EACCES;
663 	}
664 	if (!error) {
665 	    nqsrv_getl(vp, ND_READ);
666 	    if ((error = nfsrv_access(vp, VREAD, cred, rdonly, lwp, 1)) != 0)
667 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 1);
668 	}
669 	getret = VOP_GETATTR(vp, &va, cred);
670 	if (!error)
671 		error = getret;
672 	if (error) {
673 		vput(vp);
674 		nfsm_reply(NFSX_POSTOPATTR(v3));
675 		nfsm_srvpostop_attr(getret, &va);
676 		return (0);
677 	}
678 	if (off >= va.va_size)
679 		cnt = 0;
680 	else if ((off + reqlen) > va.va_size)
681 		cnt = va.va_size - off;
682 	else
683 		cnt = reqlen;
684 	nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
685 	if (v3) {
686 		nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
687 		*tl++ = nfs_true;
688 		fp = (struct nfs_fattr *)tl;
689 		tl += (NFSX_V3FATTR / sizeof (u_int32_t));
690 	} else {
691 		nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
692 		fp = (struct nfs_fattr *)tl;
693 		tl += (NFSX_V2FATTR / sizeof (u_int32_t));
694 	}
695 	len = left = cnt;
696 	if (cnt > 0) {
697 		if (nfsd_use_loan) {
698 			struct vm_page **pgpp;
699 			voff_t pgoff = trunc_page(off);
700 			int npages;
701 			vaddr_t lva;
702 
703 			npages = (round_page(off + cnt) - pgoff) >> PAGE_SHIFT;
704 			KASSERT(npages <= M_EXT_MAXPAGES); /* XXX */
705 
706 			/* allocate kva for mbuf data */
707 			lva = sokvaalloc(pgoff, npages << PAGE_SHIFT,
708 			    slp->ns_so);
709 			if (lva == 0) {
710 				/* fall back to VOP_READ */
711 				goto loan_fail;
712 			}
713 
714 			/* allocate mbuf */
715 			m = m_get(M_WAIT, MT_DATA);
716 			MCLAIM(m, &nfs_mowner);
717 			pgpp = m->m_ext.ext_pgs;
718 
719 			/* loan pages */
720 			error = uvm_loanuobjpages(&vp->v_uobj, pgoff, npages,
721 			    pgpp);
722 			if (error) {
723 				sokvafree(lva, npages << PAGE_SHIFT);
724 				m_free(m);
725 				if (error == EBUSY)
726 					goto loan_fail;
727 				goto read_error;
728 			}
729 
730 			/* associate kva to mbuf */
731 			MEXTADD(m, (void *)(lva + ((vaddr_t)off & PAGE_MASK)),
732 			    cnt, M_MBUF, soloanfree, slp->ns_so);
733 			m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
734 			m->m_len = cnt;
735 
736 			/* map pages */
737 			for (i = 0; i < npages; i++) {
738 				pmap_kenter_pa(lva, VM_PAGE_TO_PHYS(pgpp[i]),
739 				    VM_PROT_READ, 0);
740 				lva += PAGE_SIZE;
741 			}
742 
743 			pmap_update(pmap_kernel());
744 
745 			mb->m_next = m;
746 			mb = m;
747 			error = 0;
748 			uiop->uio_resid = 0;
749 		} else {
750 			struct iovec *iv;
751 			struct iovec *iv2;
752 			struct mbuf *m2;
753 			int siz;
754 loan_fail:
755 			/*
756 			 * Generate the mbuf list with the uio_iov ref. to it.
757 			 */
758 			i = 0;
759 			m = m2 = mb;
760 			while (left > 0) {
761 				siz = min(M_TRAILINGSPACE(m), left);
762 				if (siz > 0) {
763 					left -= siz;
764 					i++;
765 				}
766 				if (left > 0) {
767 					m = m_get(M_WAIT, MT_DATA);
768 					MCLAIM(m, &nfs_mowner);
769 					m_clget(m, M_WAIT);
770 					m->m_len = 0;
771 					m2->m_next = m;
772 					m2 = m;
773 				}
774 			}
775 			iv = malloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
776 			uiop->uio_iov = iv2 = iv;
777 			m = mb;
778 			left = cnt;
779 			i = 0;
780 			while (left > 0) {
781 				if (m == NULL)
782 					panic("nfsrv_read iov");
783 				siz = min(M_TRAILINGSPACE(m), left);
784 				if (siz > 0) {
785 					iv->iov_base = mtod(m, char *) +
786 					    m->m_len;
787 					iv->iov_len = siz;
788 					m->m_len += siz;
789 					left -= siz;
790 					iv++;
791 					i++;
792 				}
793 				m = m->m_next;
794 			}
795 			uiop->uio_iovcnt = i;
796 			uiop->uio_offset = off;
797 			uiop->uio_resid = cnt;
798 			uiop->uio_rw = UIO_READ;
799 			UIO_SETUP_SYSSPACE(uiop);
800 			error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
801 			free((void *)iv2, M_TEMP);
802 		}
803 read_error:
804 		if (error || (getret = VOP_GETATTR(vp, &va, cred)) != 0){
805 			if (!error)
806 				error = getret;
807 			m_freem(mreq);
808 			vput(vp);
809 			nfsm_reply(NFSX_POSTOPATTR(v3));
810 			nfsm_srvpostop_attr(getret, &va);
811 			return (0);
812 		}
813 	} else {
814 		uiop->uio_resid = 0;
815 	}
816 	vput(vp);
817 	nfsm_srvfillattr(&va, fp);
818 	len -= uiop->uio_resid;
819 	padlen = nfsm_padlen(len);
820 	if (uiop->uio_resid || padlen)
821 		nfs_zeropad(mb, uiop->uio_resid, padlen);
822 	if (v3) {
823 		/* count */
824 		*tl++ = txdr_unsigned(len);
825 		/* eof */
826 		if (off + len >= va.va_size)
827 			*tl++ = nfs_true;
828 		else
829 			*tl++ = nfs_false;
830 	}
831 	*tl = txdr_unsigned(len);
832 	nfsm_srvdone;
833 }
834 
835 /*
836  * nfs write service
837  */
838 int
839 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
840 {
841 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
842 	struct mbuf *nam = nfsd->nd_nam;
843 	char *dpos = nfsd->nd_dpos;
844 	kauth_cred_t cred = nfsd->nd_cr;
845 	struct iovec *ivp;
846 	int i, cnt;
847 	struct mbuf *mp;
848 	struct nfs_fattr *fp;
849 	struct iovec *iv;
850 	struct vattr va, forat;
851 	u_int32_t *tl;
852 	int32_t t1;
853 	char *bpos;
854 	int error = 0, rdonly, cache = 0, len, forat_ret = 1;
855 	int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
856 	int stable = NFSV3WRITE_FILESYNC;
857 	int v3 = (nfsd->nd_flag & ND_NFSV3);
858 	char *cp2;
859 	struct mbuf *mb, *mreq __unused;
860 	struct vnode *vp;
861 	nfsrvfh_t nsfh;
862 	struct uio io, *uiop = &io;
863 	off_t off;
864 	u_quad_t frev;
865 
866 	if (mrep == NULL) {
867 		*mrq = NULL;
868 		return (0);
869 	}
870 	nfsm_srvmtofh(&nsfh);
871 	if (v3) {
872 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
873 		off = fxdr_hyper(tl);
874 		tl += 3;
875 		stable = fxdr_unsigned(int, *tl++);
876 	} else {
877 		nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
878 		off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
879 		tl += 2;
880 	}
881 	retlen = len = fxdr_unsigned(int32_t, *tl);
882 	cnt = i = 0;
883 
884 	/*
885 	 * For NFS Version 2, it is not obvious what a write of zero length
886 	 * should do, but I might as well be consistent with Version 3,
887 	 * which is to return ok so long as there are no permission problems.
888 	 */
889 	if (len > 0) {
890 		zeroing = 1;
891 		mp = mrep;
892 		while (mp) {
893 			if (mp == md) {
894 				zeroing = 0;
895 				adjust = dpos - mtod(mp, char *);
896 				mp->m_len -= adjust;
897 				if (mp->m_len > 0 && adjust > 0)
898 					NFSMADV(mp, adjust);
899 			}
900 			if (zeroing)
901 				mp->m_len = 0;
902 			else if (mp->m_len > 0) {
903 				i += mp->m_len;
904 				if (i > len) {
905 					mp->m_len -= (i - len);
906 					zeroing	= 1;
907 				}
908 				if (mp->m_len > 0)
909 					cnt++;
910 			}
911 			mp = mp->m_next;
912 		}
913 	}
914 	if (len > NFS_MAXDATA || len < 0 || i < len) {
915 		error = EIO;
916 		nfsm_reply(2 * NFSX_UNSIGNED);
917 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
918 		return (0);
919 	}
920 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
921 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
922 	if (error) {
923 		nfsm_reply(2 * NFSX_UNSIGNED);
924 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
925 		return (0);
926 	}
927 	if (v3)
928 		forat_ret = VOP_GETATTR(vp, &forat, cred);
929 	if (vp->v_type != VREG) {
930 		if (v3)
931 			error = EINVAL;
932 		else
933 			error = (vp->v_type == VDIR) ? EISDIR : EACCES;
934 	}
935 	if (!error) {
936 		nqsrv_getl(vp, ND_WRITE);
937 		error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
938 	}
939 	if (error) {
940 		vput(vp);
941 		nfsm_reply(NFSX_WCCDATA(v3));
942 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
943 		return (0);
944 	}
945 
946 	if (len > 0) {
947 		ivp = malloc(cnt * sizeof (struct iovec), M_TEMP, M_WAITOK);
948 		uiop->uio_iov = iv = ivp;
949 		uiop->uio_iovcnt = cnt;
950 		mp = mrep;
951 		while (mp) {
952 			if (mp->m_len > 0) {
953 				ivp->iov_base = mtod(mp, void *);
954 				ivp->iov_len = mp->m_len;
955 				ivp++;
956 			}
957 			mp = mp->m_next;
958 		}
959 
960 		/*
961 		 * XXX
962 		 * The IO_METASYNC flag indicates that all metadata (and not
963 		 * just enough to ensure data integrity) must be written to
964 		 * stable storage synchronously.
965 		 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
966 		 */
967 		if (stable == NFSV3WRITE_UNSTABLE)
968 			ioflags = IO_NODELOCKED;
969 		else if (stable == NFSV3WRITE_DATASYNC)
970 			ioflags = (IO_SYNC | IO_NODELOCKED);
971 		else
972 			ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
973 		uiop->uio_resid = len;
974 		uiop->uio_rw = UIO_WRITE;
975 		uiop->uio_offset = off;
976 		UIO_SETUP_SYSSPACE(uiop);
977 		error = VOP_WRITE(vp, uiop, ioflags, cred);
978 		nfsstats.srvvop_writes++;
979 		free(iv, M_TEMP);
980 	}
981 	aftat_ret = VOP_GETATTR(vp, &va, cred);
982 	vput(vp);
983 	if (!error)
984 		error = aftat_ret;
985 	nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
986 		2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
987 	if (v3) {
988 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
989 		if (error)
990 			return (0);
991 		nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
992 		*tl++ = txdr_unsigned(retlen);
993 		if (stable == NFSV3WRITE_UNSTABLE)
994 			*tl++ = txdr_unsigned(stable);
995 		else
996 			*tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
997 		/*
998 		 * Actually, there is no need to txdr these fields,
999 		 * but it may make the values more human readable,
1000 		 * for debugging purposes.
1001 		 */
1002 		*tl++ = txdr_unsigned(boottime.tv_sec);
1003 		*tl = txdr_unsigned(boottime.tv_nsec / 1000);
1004 	} else {
1005 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1006 		nfsm_srvfillattr(&va, fp);
1007 	}
1008 	nfsm_srvdone;
1009 }
1010 
1011 /*
1012  * XXX elad: the original NFSW_SAMECRED() macro also made sure the
1013  *	     two nd_flag fields of the descriptors contained
1014  *	     ND_KERBAUTH.
1015  */
1016 static int
1017 nfsrv_samecred(kauth_cred_t cred1, kauth_cred_t cred2)
1018 {
1019 	int i, do_ngroups;
1020 
1021 	if (kauth_cred_geteuid(cred1) != kauth_cred_geteuid(cred2))
1022 		return (0);
1023 	if (kauth_cred_ngroups(cred1) != kauth_cred_ngroups(cred2))
1024 		return (0);
1025 	do_ngroups = kauth_cred_ngroups(cred1);
1026 	for (i = 0; i < do_ngroups; i++)
1027 		if (kauth_cred_group(cred1, i) !=
1028 		    kauth_cred_group(cred2, i))
1029 			return (0);
1030 
1031 	return (1);
1032 }
1033 
1034 static struct nfsrvw_delayhash *
1035 nfsrv_nwdelayhash(struct nfssvc_sock *slp, const nfsrvfh_t *nsfh)
1036 {
1037 	uint32_t hash;
1038 
1039 	hash = hash32_buf(NFSRVFH_DATA(nsfh), NFSRVFH_SIZE(nsfh),
1040 	    HASH32_BUF_INIT);
1041 	return &slp->ns_wdelayhashtbl[hash % NFS_WDELAYHASHSIZ];
1042 }
1043 
1044 /*
1045  * NFS write service with write gathering support. Called when
1046  * nfsrvw_procrastinate > 0.
1047  * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1048  * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1049  * Jan. 1994.
1050  */
1051 int
1052 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1053 {
1054 	struct timeval now;
1055 	struct iovec *ivp;
1056 	struct mbuf *mp;
1057 	struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1058 	struct nfs_fattr *fp;
1059 	int i = 0;
1060 	struct iovec *iov;
1061 	struct nfsrvw_delayhash *wpp;
1062 	kauth_cred_t cred;
1063 	struct vattr va, forat;
1064 	u_int32_t *tl;
1065 	int32_t t1;
1066 	char *bpos, *dpos;
1067 	int error = 0, rdonly, cache = 0, len = 0, forat_ret = 1;
1068 	int ioflags, aftat_ret = 1, adjust, v3, zeroing;
1069 	char *cp2;
1070 	struct mbuf *mb, *mreq, *mrep, *md;
1071 	struct vnode *vp;
1072 	struct uio io, *uiop = &io;
1073 	u_quad_t frev, cur_usec;
1074 
1075 	*mrq = NULL;
1076 	if (*ndp) {
1077 	    nfsd = *ndp;
1078 	    *ndp = NULL;
1079 	    mrep = nfsd->nd_mrep;
1080 	    md = nfsd->nd_md;
1081 	    dpos = nfsd->nd_dpos;
1082 	    cred = nfsd->nd_cr;
1083 	    v3 = (nfsd->nd_flag & ND_NFSV3);
1084 	    LIST_INIT(&nfsd->nd_coalesce);
1085 	    nfsd->nd_mreq = NULL;
1086 	    nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1087 	    getmicrotime(&now);
1088 	    cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
1089 	    nfsd->nd_time = cur_usec + nfsrvw_procrastinate;
1090 
1091 	    /*
1092 	     * Now, get the write header..
1093 	     */
1094 	    nfsm_srvmtofh(&nfsd->nd_fh);
1095 	    if (v3) {
1096 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1097 		nfsd->nd_off = fxdr_hyper(tl);
1098 		tl += 3;
1099 		nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1100 	    } else {
1101 		nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1102 		nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1103 		tl += 2;
1104 	    }
1105 	    len = fxdr_unsigned(int32_t, *tl);
1106 	    nfsd->nd_len = len;
1107 	    nfsd->nd_eoff = nfsd->nd_off + len;
1108 
1109 	    /*
1110 	     * Trim the header out of the mbuf list and trim off any trailing
1111 	     * junk so that the mbuf list has only the write data.
1112 	     */
1113 	    zeroing = 1;
1114 	    i = 0;
1115 	    mp = mrep;
1116 	    while (mp) {
1117 		if (mp == md) {
1118 		    zeroing = 0;
1119 		    adjust = dpos - mtod(mp, char *);
1120 		    mp->m_len -= adjust;
1121 		    if (mp->m_len > 0 && adjust > 0)
1122 			NFSMADV(mp, adjust);
1123 		}
1124 		if (zeroing)
1125 		    mp->m_len = 0;
1126 		else {
1127 		    i += mp->m_len;
1128 		    if (i > len) {
1129 			mp->m_len -= (i - len);
1130 			zeroing = 1;
1131 		    }
1132 		}
1133 		mp = mp->m_next;
1134 	    }
1135 	    if (len > NFS_MAXDATA || len < 0  || i < len) {
1136 nfsmout:
1137 		m_freem(mrep);
1138 		error = EIO;
1139 		nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1140 		if (v3)
1141 		    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1142 		nfsd->nd_mreq = mreq;
1143 		nfsd->nd_mrep = NULL;
1144 		nfsd->nd_time = 0;
1145 	    }
1146 
1147 	    /*
1148 	     * Add this entry to the hash and time queues.
1149 	     */
1150 	    owp = NULL;
1151 	    mutex_enter(&nfsd_lock);
1152 	    wp = LIST_FIRST(&slp->ns_tq);
1153 	    while (wp && wp->nd_time < nfsd->nd_time) {
1154 		owp = wp;
1155 		wp = LIST_NEXT(wp, nd_tq);
1156 	    }
1157 	    if (owp) {
1158 		LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1159 	    } else {
1160 		LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1161 	    }
1162 	    if (nfsd->nd_mrep) {
1163 		wpp = nfsrv_nwdelayhash(slp, &nfsd->nd_fh);
1164 		owp = NULL;
1165 		wp = LIST_FIRST(wpp);
1166 		while (wp && nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) {
1167 		    owp = wp;
1168 		    wp = LIST_NEXT(wp, nd_hash);
1169 		}
1170 		while (wp && wp->nd_off < nfsd->nd_off &&
1171 		    !nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) {
1172 		    owp = wp;
1173 		    wp = LIST_NEXT(wp, nd_hash);
1174 		}
1175 		if (owp) {
1176 		    LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1177 
1178 		    /*
1179 		     * Search the hash list for overlapping entries and
1180 		     * coalesce.
1181 		     */
1182 		    for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1183 			wp = LIST_NEXT(nfsd, nd_hash);
1184 			if (nfsrv_samecred(owp->nd_cr, nfsd->nd_cr))
1185 			    nfsrvw_coalesce(owp, nfsd);
1186 		    }
1187 		} else {
1188 		    LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1189 		}
1190 	    }
1191 	    mutex_exit(&nfsd_lock);
1192 	}
1193 
1194 	/*
1195 	 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1196 	 * and generate the associated reply mbuf list(s).
1197 	 */
1198 loop1:
1199 	getmicrotime(&now);
1200 	cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
1201 	mutex_enter(&nfsd_lock);
1202 	for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1203 		owp = LIST_NEXT(nfsd, nd_tq);
1204 		if (nfsd->nd_time > cur_usec)
1205 		    break;
1206 		if (nfsd->nd_mreq)
1207 		    continue;
1208 		LIST_REMOVE(nfsd, nd_tq);
1209 		LIST_REMOVE(nfsd, nd_hash);
1210 		mutex_exit(&nfsd_lock);
1211 
1212 		mrep = nfsd->nd_mrep;
1213 		nfsd->nd_mrep = NULL;
1214 		cred = nfsd->nd_cr;
1215 		v3 = (nfsd->nd_flag & ND_NFSV3);
1216 		forat_ret = aftat_ret = 1;
1217 		error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1218 		    nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH),
1219 		    false);
1220 		if (!error) {
1221 		    if (v3)
1222 			forat_ret = VOP_GETATTR(vp, &forat, cred);
1223 		    if (vp->v_type != VREG) {
1224 			if (v3)
1225 			    error = EINVAL;
1226 			else
1227 			    error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1228 		    }
1229 		} else
1230 		    vp = NULL;
1231 		if (!error) {
1232 		    nqsrv_getl(vp, ND_WRITE);
1233 		    error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
1234 		}
1235 
1236 		if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1237 		    ioflags = IO_NODELOCKED;
1238 		else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1239 		    ioflags = (IO_SYNC | IO_NODELOCKED);
1240 		else
1241 		    ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1242 		uiop->uio_rw = UIO_WRITE;
1243 		uiop->uio_offset = nfsd->nd_off;
1244 		uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1245 		UIO_SETUP_SYSSPACE(uiop);
1246 		if (uiop->uio_resid > 0) {
1247 		    mp = mrep;
1248 		    i = 0;
1249 		    while (mp) {
1250 			if (mp->m_len > 0)
1251 			    i++;
1252 			mp = mp->m_next;
1253 		    }
1254 		    uiop->uio_iovcnt = i;
1255 		    iov = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
1256 		    uiop->uio_iov = ivp = iov;
1257 		    mp = mrep;
1258 		    while (mp) {
1259 			if (mp->m_len > 0) {
1260 			    ivp->iov_base = mtod(mp, void *);
1261 			    ivp->iov_len = mp->m_len;
1262 			    ivp++;
1263 			}
1264 			mp = mp->m_next;
1265 		    }
1266 		    if (!error) {
1267 			error = VOP_WRITE(vp, uiop, ioflags, cred);
1268 			nfsstats.srvvop_writes++;
1269 		    }
1270 		    free((void *)iov, M_TEMP);
1271 		}
1272 		m_freem(mrep);
1273 		if (vp) {
1274 		    aftat_ret = VOP_GETATTR(vp, &va, cred);
1275 		    vput(vp);
1276 		}
1277 
1278 		/*
1279 		 * Loop around generating replies for all write rpcs that have
1280 		 * now been completed.
1281 		 */
1282 		swp = nfsd;
1283 		do {
1284 		    if (error) {
1285 			nfsm_writereply(NFSX_WCCDATA(v3), v3);
1286 			if (v3) {
1287 			    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1288 			}
1289 		    } else {
1290 			nfsm_writereply(NFSX_PREOPATTR(v3) +
1291 			    NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1292 			    NFSX_WRITEVERF(v3), v3);
1293 			if (v3) {
1294 			    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1295 			    nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1296 			    *tl++ = txdr_unsigned(nfsd->nd_len);
1297 			    *tl++ = txdr_unsigned(swp->nd_stable);
1298 			    /*
1299 			     * Actually, there is no need to txdr these fields,
1300 			     * but it may make the values more human readable,
1301 			     * for debugging purposes.
1302 			     */
1303 			    *tl++ = txdr_unsigned(boottime.tv_sec);
1304 			    *tl = txdr_unsigned(boottime.tv_nsec / 1000);
1305 			} else {
1306 			    nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1307 			    nfsm_srvfillattr(&va, fp);
1308 			}
1309 		    }
1310 		    nfsd->nd_mreq = mreq;
1311 		    if (nfsd->nd_mrep)
1312 			panic("nfsrv_write: nd_mrep not free");
1313 
1314 		    /*
1315 		     * Done. Put it at the head of the timer queue so that
1316 		     * the final phase can return the reply.
1317 		     */
1318 		    mutex_enter(&nfsd_lock);
1319 		    if (nfsd != swp) {
1320 			nfsd->nd_time = 0;
1321 			LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1322 		    }
1323 		    nfsd = LIST_FIRST(&swp->nd_coalesce);
1324 		    if (nfsd) {
1325 			LIST_REMOVE(nfsd, nd_tq);
1326 		    }
1327 		    mutex_exit(&nfsd_lock);
1328 		} while (nfsd);
1329 		swp->nd_time = 0;
1330 
1331 		mutex_enter(&nfsd_lock);
1332 		LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1333 		mutex_exit(&nfsd_lock);
1334 		goto loop1;
1335 	}
1336 	mutex_exit(&nfsd_lock);
1337 	nfs_timer_start();
1338 
1339 	/*
1340 	 * Search for a reply to return.
1341 	 */
1342 	mutex_enter(&nfsd_lock);
1343 	LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq) {
1344 		if (nfsd->nd_mreq) {
1345 		    LIST_REMOVE(nfsd, nd_tq);
1346 		    *mrq = nfsd->nd_mreq;
1347 		    *ndp = nfsd;
1348 		    break;
1349 		}
1350 	}
1351 	mutex_exit(&nfsd_lock);
1352 	return (0);
1353 }
1354 
1355 /*
1356  * Coalesce the write request nfsd into owp. To do this we must:
1357  * - remove nfsd from the queues
1358  * - merge nfsd->nd_mrep into owp->nd_mrep
1359  * - update the nd_eoff and nd_stable for owp
1360  * - put nfsd on owp's nd_coalesce list
1361  * NB: Must be called at splsoftclock().
1362  */
1363 void
1364 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1365 {
1366         int overlap;
1367         struct mbuf *mp;
1368 	struct nfsrv_descript *m;
1369 
1370 	KASSERT(mutex_owned(&nfsd_lock));
1371 
1372         LIST_REMOVE(nfsd, nd_hash);
1373         LIST_REMOVE(nfsd, nd_tq);
1374         if (owp->nd_eoff < nfsd->nd_eoff) {
1375             overlap = owp->nd_eoff - nfsd->nd_off;
1376             if (overlap < 0)
1377                 panic("nfsrv_coalesce: bad off");
1378             if (overlap > 0)
1379                 m_adj(nfsd->nd_mrep, overlap);
1380             mp = owp->nd_mrep;
1381             while (mp->m_next)
1382                 mp = mp->m_next;
1383             mp->m_next = nfsd->nd_mrep;
1384             owp->nd_eoff = nfsd->nd_eoff;
1385         } else
1386             m_freem(nfsd->nd_mrep);
1387         nfsd->nd_mrep = NULL;
1388         if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1389             owp->nd_stable = NFSV3WRITE_FILESYNC;
1390         else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1391             owp->nd_stable == NFSV3WRITE_UNSTABLE)
1392             owp->nd_stable = NFSV3WRITE_DATASYNC;
1393         LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1394  	/*
1395  	 * nfsd might hold coalesce elements! Move them to owp.
1396  	 * Otherwise, requests may be lost and clients will be stuck.
1397  	 */
1398 	while ((m = LIST_FIRST(&nfsd->nd_coalesce)) != NULL) {
1399 		LIST_REMOVE(m, nd_tq);
1400 		LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq);
1401 	}
1402 }
1403 
1404 /*
1405  * nfs create service
1406  * now does a truncate to 0 length via. setattr if it already exists
1407  */
1408 int
1409 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1410 {
1411 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1412 	struct mbuf *nam = nfsd->nd_nam;
1413 	char *dpos = nfsd->nd_dpos;
1414 	kauth_cred_t cred = nfsd->nd_cr;
1415 	struct nfs_fattr *fp;
1416 	struct vattr va, dirfor, diraft;
1417 	struct nfsv2_sattr *sp;
1418 	u_int32_t *tl;
1419 	struct nameidata nd;
1420 	char *cp;
1421 	int32_t t1;
1422 	char *bpos;
1423 	int error = 0, cache = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1424 	int rdev = 0, abort = 0;
1425 	int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1426 	char *cp2;
1427 	struct mbuf *mb, *mreq __unused;
1428 	struct vnode *vp = NULL, *dirp = NULL;
1429 	nfsrvfh_t nsfh;
1430 	u_quad_t frev, tempsize;
1431 	u_char cverf[NFSX_V3CREATEVERF];
1432 
1433 	nd.ni_cnd.cn_nameiop = 0;
1434 	nfsm_srvmtofh(&nsfh);
1435 	nfsm_srvnamesiz(len);
1436 	nd.ni_cnd.cn_cred = cred;
1437 	nd.ni_cnd.cn_nameiop = CREATE;
1438 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1439 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1440 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1441 	if (dirp && v3) {
1442 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
1443 	}
1444 	if (error) {
1445 		nfsm_reply(NFSX_WCCDATA(v3));
1446 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1447 		if (dirp)
1448 			vrele(dirp);
1449 		if (nd.ni_pathbuf != NULL) {
1450 			pathbuf_destroy(nd.ni_pathbuf);
1451 			nd.ni_pathbuf = NULL;
1452 		}
1453 		return (0);
1454 	}
1455 	abort = 1;
1456 	vattr_null(&va);
1457 	if (v3) {
1458 		va.va_mode = 0;
1459 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1460 		how = fxdr_unsigned(int, *tl);
1461 		switch (how) {
1462 		case NFSV3CREATE_GUARDED:
1463 			if (nd.ni_vp) {
1464 				error = EEXIST;
1465 				break;
1466 			}
1467 		case NFSV3CREATE_UNCHECKED:
1468 			nfsm_srvsattr(&va);
1469 			break;
1470 		case NFSV3CREATE_EXCLUSIVE:
1471 			nfsm_dissect(cp, void *, NFSX_V3CREATEVERF);
1472 			memcpy(cverf, cp, NFSX_V3CREATEVERF);
1473 			exclusive_flag = 1;
1474 			break;
1475 		};
1476 		va.va_type = VREG;
1477 	} else {
1478 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1479 		va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1480 		if (va.va_type == VNON)
1481 			va.va_type = VREG;
1482 		va.va_mode = nfstov_mode(sp->sa_mode);
1483 		switch (va.va_type) {
1484 		case VREG:
1485 			tsize = fxdr_unsigned(int32_t, sp->sa_size);
1486 			if (tsize != -1)
1487 				va.va_size = (u_quad_t)tsize;
1488 			break;
1489 		case VCHR:
1490 		case VBLK:
1491 		case VFIFO:
1492 			rdev = fxdr_unsigned(int32_t, sp->sa_size);
1493 			break;
1494 		default:
1495 			break;
1496 		};
1497 	}
1498 
1499 	/*
1500 	 * Iff doesn't exist, create it
1501 	 * otherwise just truncate to 0 length
1502 	 *   should I set the mode too ??
1503 	 */
1504 	if (nd.ni_vp == NULL) {
1505 		if (va.va_type == VREG || va.va_type == VSOCK) {
1506 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
1507 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1508 			if (!error) {
1509 				vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1510 				if (exclusive_flag) {
1511 					exclusive_flag = 0;
1512 					vattr_null(&va);
1513 					/*
1514 					 * XXX
1515 					 * assuming NFSX_V3CREATEVERF
1516 					 * == sizeof(nfstime3)
1517 					 */
1518 					fxdr_nfsv3time(cverf, &va.va_atime);
1519 					error = VOP_SETATTR(nd.ni_vp, &va,
1520 						cred);
1521 				}
1522 			}
1523 		} else if (va.va_type == VCHR || va.va_type == VBLK ||
1524 			va.va_type == VFIFO) {
1525 			if (va.va_type == VCHR && rdev == 0xffffffff)
1526 				va.va_type = VFIFO;
1527 			if (va.va_type != VFIFO &&
1528 			    (error = kauth_authorize_system(cred,
1529 			    KAUTH_SYSTEM_MKNOD, 0, NULL, NULL, NULL))) {
1530 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1531 				vput(nd.ni_dvp);
1532 				abort = 0;
1533 				nfsm_reply(0);
1534 				if (nd.ni_pathbuf != NULL) {
1535 					pathbuf_destroy(nd.ni_pathbuf);
1536 					nd.ni_pathbuf = NULL;
1537 				}
1538 				return (error);
1539 			} else
1540 				va.va_rdev = (dev_t)rdev;
1541 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
1542 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
1543 			    &va);
1544 			if (!error) {
1545 				vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1546 			} else {
1547 				nfsm_reply(0);
1548 			}
1549 		} else {
1550 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1551 			if (nd.ni_pathbuf != NULL) {
1552 				pathbuf_destroy(nd.ni_pathbuf);
1553 				nd.ni_pathbuf = NULL;
1554 			}
1555 			error = ENXIO;
1556 			abort = 0;
1557 		}
1558 		vp = nd.ni_vp;
1559 	} else {
1560 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1561 		if (nd.ni_pathbuf != NULL) {
1562 			pathbuf_destroy(nd.ni_pathbuf);
1563 			nd.ni_pathbuf = NULL;
1564 		}
1565 		vp = nd.ni_vp;
1566 		abort = 0;
1567 		if (!error && va.va_size != -1) {
1568 			error = nfsrv_access(vp, VWRITE, cred,
1569 			    (nd.ni_cnd.cn_flags & RDONLY), lwp, 0);
1570 			if (!error) {
1571 				nqsrv_getl(vp, ND_WRITE);
1572 				tempsize = va.va_size;
1573 				vattr_null(&va);
1574 				va.va_size = tempsize;
1575 				error = VOP_SETATTR(vp, &va, cred);
1576 			}
1577 		}
1578 		if (error)
1579 			vput(vp);
1580 	}
1581 	if (!error) {
1582 		error = nfsrv_composefh(vp, &nsfh, v3);
1583 		if (!error)
1584 			error = VOP_GETATTR(vp, &va, cred);
1585 		vput(vp);
1586 	}
1587 	if (nd.ni_dvp == vp)
1588 		vrele(nd.ni_dvp);
1589 	else
1590 		vput(nd.ni_dvp);
1591 	if (v3) {
1592 		if (exclusive_flag && !error) {
1593 			/*
1594 			 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3)
1595 			 */
1596 			char oldverf[NFSX_V3CREATEVERF];
1597 
1598 			txdr_nfsv3time(&va.va_atime, oldverf);
1599 			if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF))
1600 				error = EEXIST;
1601 		}
1602 		if (dirp) {
1603 			vn_lock(dirp, LK_SHARED | LK_RETRY);
1604 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1605 			VOP_UNLOCK(dirp);
1606 		}
1607 	}
1608 	if (dirp) {
1609 		vrele(dirp);
1610 		dirp = NULL;
1611 	}
1612 	if (nd.ni_pathbuf != NULL) {
1613 		pathbuf_destroy(nd.ni_pathbuf);
1614 		nd.ni_pathbuf = NULL;
1615 	}
1616 	abort = 0;
1617 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1618 	if (v3) {
1619 		if (!error) {
1620 			nfsm_srvpostop_fh(&nsfh);
1621 			nfsm_srvpostop_attr(0, &va);
1622 		}
1623 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1624 	} else {
1625 		nfsm_srvfhtom(&nsfh, v3);
1626 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1627 		nfsm_srvfillattr(&va, fp);
1628 	}
1629 	return (0);
1630 nfsmout:
1631 	if (dirp)
1632 		vrele(dirp);
1633 	if (abort) {
1634 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1635 		if (nd.ni_dvp == nd.ni_vp)
1636 			vrele(nd.ni_dvp);
1637 		else
1638 			vput(nd.ni_dvp);
1639 		if (nd.ni_vp)
1640 			vput(nd.ni_vp);
1641 	}
1642 	if (nd.ni_pathbuf != NULL) {
1643 		pathbuf_destroy(nd.ni_pathbuf);
1644 		nd.ni_pathbuf = NULL;
1645 	}
1646 	return (error);
1647 }
1648 
1649 /*
1650  * nfs v3 mknod service
1651  */
1652 int
1653 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1654 {
1655 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1656 	struct mbuf *nam = nfsd->nd_nam;
1657 	char *dpos = nfsd->nd_dpos;
1658 	kauth_cred_t cred = nfsd->nd_cr;
1659 	struct vattr va, dirfor, diraft;
1660 	u_int32_t *tl;
1661 	struct nameidata nd;
1662 	int32_t t1;
1663 	char *bpos;
1664 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1665 	int abort = 0;
1666 	u_int32_t major, minor;
1667 	enum vtype vtyp;
1668 	char *cp2;
1669 	struct mbuf *mb, *mreq __unused;
1670 	struct vnode *vp, *dirp = (struct vnode *)0;
1671 	nfsrvfh_t nsfh;
1672 	u_quad_t frev;
1673 
1674 	nd.ni_cnd.cn_nameiop = 0;
1675 	nfsm_srvmtofh(&nsfh);
1676 	nfsm_srvnamesiz(len);
1677 	nd.ni_cnd.cn_cred = cred;
1678 	nd.ni_cnd.cn_nameiop = CREATE;
1679 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1680 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1681 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1682 	if (dirp)
1683 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
1684 	if (error) {
1685 		nfsm_reply(NFSX_WCCDATA(1));
1686 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1687 		if (dirp)
1688 			vrele(dirp);
1689 		if (nd.ni_pathbuf != NULL) {
1690 			pathbuf_destroy(nd.ni_pathbuf);
1691 			nd.ni_pathbuf = NULL;
1692 		}
1693 		return (0);
1694 	}
1695 	abort = 1;
1696 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1697 	vtyp = nfsv3tov_type(*tl);
1698 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1699 		error = NFSERR_BADTYPE;
1700 		goto abort;
1701 	}
1702 	vattr_null(&va);
1703 	va.va_mode = 0;
1704 	nfsm_srvsattr(&va);
1705 	if (vtyp == VCHR || vtyp == VBLK) {
1706 		dev_t rdev;
1707 
1708 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1709 		major = fxdr_unsigned(u_int32_t, *tl++);
1710 		minor = fxdr_unsigned(u_int32_t, *tl);
1711 		rdev = makedev(major, minor);
1712 		if (major(rdev) != major || minor(rdev) != minor) {
1713 			error = EINVAL;
1714 			goto abort;
1715 		}
1716 		va.va_rdev = rdev;
1717 	}
1718 
1719 	/*
1720 	 * Iff doesn't exist, create it.
1721 	 */
1722 	if (nd.ni_vp) {
1723 		error = EEXIST;
1724 abort:
1725 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1726 		if (nd.ni_vp)
1727 			vput(nd.ni_vp);
1728 		if (nd.ni_pathbuf != NULL) {
1729 			pathbuf_destroy(nd.ni_pathbuf);
1730 			nd.ni_pathbuf = NULL;
1731 		}
1732 		goto out;
1733 	}
1734 	va.va_type = vtyp;
1735 	if (vtyp == VSOCK) {
1736 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
1737 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1738 	} else {
1739 		if (va.va_type != VFIFO &&
1740 		    (error = kauth_authorize_system(cred,
1741 		    KAUTH_SYSTEM_MKNOD, 0, NULL, NULL, NULL))) {
1742 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1743 			vput(nd.ni_dvp);
1744 			goto out;
1745 		}
1746 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
1747 		error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1748 		if (error)
1749 			goto out;
1750 	}
1751 	if (!error) {
1752 		vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1753 	}
1754 out:
1755 	vp = nd.ni_vp;
1756 	if (!error) {
1757 		error = nfsrv_composefh(vp, &nsfh, true);
1758 		if (!error)
1759 			error = VOP_GETATTR(vp, &va, cred);
1760 		vput(vp);
1761 	}
1762 	if (nd.ni_dvp == nd.ni_vp)
1763 		vrele(nd.ni_dvp);
1764 	else
1765 		vput(nd.ni_dvp);
1766 	if (dirp) {
1767 		vn_lock(dirp, LK_SHARED | LK_RETRY);
1768 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1769 		VOP_UNLOCK(dirp);
1770 		vrele(dirp);
1771 		dirp = NULL;
1772 	}
1773 	if (nd.ni_pathbuf != NULL) {
1774 		pathbuf_destroy(nd.ni_pathbuf);
1775 		nd.ni_pathbuf = NULL;
1776 	}
1777 	abort = 0;
1778 	nfsm_reply(NFSX_SRVFH(&nsfh, true) + NFSX_POSTOPATTR(1) +
1779 	    NFSX_WCCDATA(1));
1780 	if (!error) {
1781 		nfsm_srvpostop_fh(&nsfh);
1782 		nfsm_srvpostop_attr(0, &va);
1783 	}
1784 	nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1785 	return (0);
1786 nfsmout:
1787 	if (abort) {
1788 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1789 		if (nd.ni_dvp == nd.ni_vp)
1790 			vrele(nd.ni_dvp);
1791 		else
1792 			vput(nd.ni_dvp);
1793 		if (nd.ni_vp)
1794 			vput(nd.ni_vp);
1795 	}
1796 	if (nd.ni_pathbuf != NULL) {
1797 		pathbuf_destroy(nd.ni_pathbuf);
1798 		nd.ni_pathbuf = NULL;
1799 	}
1800 	if (dirp)
1801 		vrele(dirp);
1802 	return (error);
1803 }
1804 
1805 /*
1806  * nfs remove service
1807  */
1808 int
1809 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1810 {
1811 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1812 	struct mbuf *nam = nfsd->nd_nam;
1813 	char *dpos = nfsd->nd_dpos;
1814 	kauth_cred_t cred = nfsd->nd_cr;
1815 	struct nameidata nd;
1816 	u_int32_t *tl;
1817 	int32_t t1;
1818 	char *bpos;
1819 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1820 	int v3 = (nfsd->nd_flag & ND_NFSV3);
1821 	char *cp2;
1822 	struct mbuf *mb, *mreq __unused;
1823 	struct vnode *vp, *dirp;
1824 	struct vattr dirfor, diraft;
1825 	nfsrvfh_t nsfh;
1826 	u_quad_t frev;
1827 
1828 #ifndef nolint
1829 	vp = (struct vnode *)0;
1830 #endif
1831 	nfsm_srvmtofh(&nsfh);
1832 	nfsm_srvnamesiz(len);
1833 	nd.ni_cnd.cn_cred = cred;
1834 	nd.ni_cnd.cn_nameiop = DELETE;
1835 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1836 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1837 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1838 	if (error == 0 && dirp && v3) {
1839 		if (nd.ni_dvp == nd.ni_vp)
1840 			vn_lock(dirp, LK_SHARED | LK_RETRY);
1841 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
1842 		if (nd.ni_dvp == nd.ni_vp)
1843 			VOP_UNLOCK(dirp);
1844 	}
1845 	if (!error) {
1846 		vp = nd.ni_vp;
1847 		if (vp->v_type == VDIR) {
1848 			error = EPERM;
1849 			goto out;
1850 		}
1851 		/*
1852 		 * The root of a mounted filesystem cannot be deleted.
1853 		 */
1854 		if (vp->v_vflag & VV_ROOT) {
1855 			error = EBUSY;
1856 		}
1857 out:
1858 		if (!error) {
1859 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
1860 			nqsrv_getl(vp, ND_WRITE);
1861 			error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1862 			vput(nd.ni_dvp);
1863 		} else {
1864 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1865 			if (nd.ni_dvp == vp)
1866 				vrele(nd.ni_dvp);
1867 			else
1868 				vput(nd.ni_dvp);
1869 			vput(vp);
1870 		}
1871 	}
1872 	if (nd.ni_pathbuf != NULL) {
1873 		pathbuf_destroy(nd.ni_pathbuf);
1874 		nd.ni_pathbuf = NULL;
1875 	}
1876 	if (dirp) {
1877 		if (v3) {
1878 			vn_lock(dirp, LK_SHARED | LK_RETRY);
1879 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1880 			VOP_UNLOCK(dirp);
1881 		}
1882 		vrele(dirp);
1883 	}
1884 	nfsm_reply(NFSX_WCCDATA(v3));
1885 	if (v3) {
1886 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1887 		return (0);
1888 	}
1889 	nfsm_srvdone;
1890 }
1891 
1892 /*
1893  * nfs rename service
1894  */
1895 int
1896 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1897 {
1898 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1899 	struct mbuf *nam = nfsd->nd_nam;
1900 	char *dpos = nfsd->nd_dpos;
1901 	kauth_cred_t cred = nfsd->nd_cr;
1902 	u_int32_t *tl;
1903 	int32_t t1;
1904 	char *bpos;
1905 	int error = 0, cache = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1906 	uint32_t len, len2;
1907 	int tdirfor_ret = 1, tdiraft_ret = 1;
1908 	int v3 = (nfsd->nd_flag & ND_NFSV3);
1909 	char *cp2;
1910 	struct mbuf *mb, *mreq __unused;
1911 	struct nameidata fromnd, tond;
1912 	struct vnode *fvp, *tvp, *tdvp;
1913 	struct vnode *fdirp = NULL, *tdirp = NULL;
1914 	struct mount *localfs = NULL;
1915 	struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1916 	nfsrvfh_t fnsfh, tnsfh;
1917 	u_quad_t frev;
1918 	uid_t saved_uid;
1919 
1920 #ifndef nolint
1921 	fvp = (struct vnode *)0;
1922 #endif
1923 	fromnd.ni_cnd.cn_nameiop = 0;
1924 	tond.ni_cnd.cn_nameiop = 0;
1925 	nfsm_srvmtofh(&fnsfh);
1926 	nfsm_srvnamesiz(len);
1927 	/*
1928 	 * Remember our original uid so that we can reset cr_uid before
1929 	 * the second nfs_namei() call, in case it is remapped.
1930 	 */
1931 	saved_uid = kauth_cred_geteuid(cred);
1932 	fromnd.ni_cnd.cn_cred = cred;
1933 	fromnd.ni_cnd.cn_nameiop = DELETE;
1934 	fromnd.ni_cnd.cn_flags = LOCKPARENT;
1935 	error = nfs_namei(&fromnd, &fnsfh, len, slp, nam, &md,
1936 		&dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1937 	if (error == 0 && fdirp && v3) {
1938 		if (fromnd.ni_dvp == fromnd.ni_vp)
1939 			vn_lock(fdirp, LK_SHARED | LK_RETRY);
1940 		fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred);
1941 		if (fromnd.ni_dvp == fromnd.ni_vp)
1942 			VOP_UNLOCK(fdirp);
1943 	}
1944 	if (error) {
1945 		fromnd.ni_cnd.cn_nameiop = 0;
1946 		nfsm_reply(2 * NFSX_WCCDATA(v3));
1947 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1948 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1949 		if (fdirp)
1950 			vrele(fdirp);
1951 		if (fromnd.ni_pathbuf != NULL) {
1952 			pathbuf_destroy(fromnd.ni_pathbuf);
1953 		}
1954 		return (0);
1955 	}
1956 	if (fromnd.ni_dvp != fromnd.ni_vp) {
1957 		VOP_UNLOCK(fromnd.ni_dvp);
1958 	}
1959 	fvp = fromnd.ni_vp;
1960 
1961 	localfs = fvp->v_mount;
1962 	error = VFS_RENAMELOCK_ENTER(localfs);
1963 	if (error) {
1964 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1965 		vrele(fromnd.ni_dvp);
1966 		vrele(fvp);
1967 		goto out1;
1968 	}
1969 
1970 	/* Copied, regrettably, from vfs_syscalls.c (q.v.) */
1971 	vrele(fvp);
1972 	if ((fromnd.ni_cnd.cn_namelen == 1 &&
1973 	     fromnd.ni_cnd.cn_nameptr[0] == '.') ||
1974 	    (fromnd.ni_cnd.cn_namelen == 2 &&
1975 	     fromnd.ni_cnd.cn_nameptr[0] == '.' &&
1976 	     fromnd.ni_cnd.cn_nameptr[1] == '.')) {
1977 		error = EINVAL;
1978 		VFS_RENAMELOCK_EXIT(localfs);
1979 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1980 		vrele(fromnd.ni_dvp);
1981 		goto out1;
1982 	}
1983 	vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
1984 	error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd, 0);
1985 	if (error) {
1986 		VOP_UNLOCK(fromnd.ni_dvp);
1987 		VFS_RENAMELOCK_EXIT(localfs);
1988 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1989 		vrele(fromnd.ni_dvp);
1990 		goto out1;
1991 	}
1992 	VOP_UNLOCK(fromnd.ni_vp);
1993 	if (fromnd.ni_dvp != fromnd.ni_vp)
1994 		VOP_UNLOCK(fromnd.ni_dvp);
1995 	fvp = fromnd.ni_vp;
1996 
1997 	nfsm_srvmtofh(&tnsfh);
1998 	if (v3) {
1999 		nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
2000 		len2 = fxdr_unsigned(uint32_t, *tl);
2001 		/* len2 will be checked by nfs_namei */
2002 	}
2003 	else {
2004 		/* NFSv2 */
2005 		nfsm_strsiz(len2, NFS_MAXNAMLEN);
2006 	}
2007 	kauth_cred_seteuid(cred, saved_uid);
2008 	tond.ni_cnd.cn_cred = cred;
2009 	tond.ni_cnd.cn_nameiop = RENAME;
2010 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE;
2011 	error = nfs_namei(&tond, &tnsfh, len2, slp, nam, &md,
2012 		&dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2013 	if (tdirp && v3) {
2014 		tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred);
2015 	}
2016 	if (error) {
2017 		VFS_RENAMELOCK_EXIT(localfs);
2018 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2019 		vrele(fromnd.ni_dvp);
2020 		vrele(fvp);
2021 		goto out1;
2022 	}
2023 	tdvp = tond.ni_dvp;
2024 	tvp = tond.ni_vp;
2025 	if (tvp != NULL) {
2026 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2027 			if (v3)
2028 				error = EEXIST;
2029 			else
2030 				error = EISDIR;
2031 			goto out;
2032 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2033 			if (v3)
2034 				error = EEXIST;
2035 			else
2036 				error = ENOTDIR;
2037 			goto out;
2038 		}
2039 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2040 			if (v3)
2041 				error = EXDEV;
2042 			else
2043 				error = ENOTEMPTY;
2044 			goto out;
2045 		}
2046 	}
2047 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2048 		if (v3)
2049 			error = EXDEV;
2050 		else
2051 			error = ENOTEMPTY;
2052 		goto out;
2053 	}
2054 	if (fvp->v_mount != tdvp->v_mount) {
2055 		if (v3)
2056 			error = EXDEV;
2057 		else
2058 			error = ENOTEMPTY;
2059 		goto out;
2060 	}
2061 	if (fvp == tdvp) {
2062 		if (v3)
2063 			error = EINVAL;
2064 		else
2065 			error = ENOTEMPTY;
2066 	}
2067 	/*
2068 	 * If source is the same as the destination (that is the
2069 	 * same vnode with the same name in the same directory),
2070 	 * then there is nothing to do.
2071 	 */
2072 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2073 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2074 	    !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2075 	      fromnd.ni_cnd.cn_namelen))
2076 		error = -1;
2077 out:
2078 	if (!error) {
2079 		nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2080 		nqsrv_getl(tdvp, ND_WRITE);
2081 		if (tvp) {
2082 			nqsrv_getl(tvp, ND_WRITE);
2083 		}
2084 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2085 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2086 		VFS_RENAMELOCK_EXIT(localfs);
2087 	} else {
2088 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2089 		if (tdvp == tvp)
2090 			vrele(tdvp);
2091 		else
2092 			vput(tdvp);
2093 		if (tvp)
2094 			vput(tvp);
2095 		VFS_RENAMELOCK_EXIT(localfs);
2096 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2097 		vrele(fromnd.ni_dvp);
2098 		vrele(fvp);
2099 		if (error == -1)
2100 			error = 0;
2101 	}
2102 	if (tond.ni_pathbuf != NULL) {
2103 		pathbuf_destroy(tond.ni_pathbuf);
2104 		tond.ni_pathbuf = NULL;
2105 	}
2106 	tond.ni_cnd.cn_nameiop = 0;
2107 out1:
2108 	if (fdirp) {
2109 		if (v3) {
2110 			vn_lock(fdirp, LK_SHARED | LK_RETRY);
2111 			fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
2112 			VOP_UNLOCK(fdirp);
2113 		}
2114 		vrele(fdirp);
2115 		fdirp = NULL;
2116 	}
2117 	if (tdirp) {
2118 		if (v3) {
2119 			vn_lock(tdirp, LK_SHARED | LK_RETRY);
2120 			tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
2121 			VOP_UNLOCK(tdirp);
2122 		}
2123 		vrele(tdirp);
2124 		tdirp = NULL;
2125 	}
2126 	pathbuf_destroy(fromnd.ni_pathbuf);
2127 	fromnd.ni_pathbuf = NULL;
2128 	fromnd.ni_cnd.cn_nameiop = 0;
2129 	localfs = NULL;
2130 	nfsm_reply(2 * NFSX_WCCDATA(v3));
2131 	if (v3) {
2132 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2133 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2134 	}
2135 	return (0);
2136 
2137 nfsmout:
2138 	if (fdirp)
2139 		vrele(fdirp);
2140 #ifdef notdef
2141 	if (tdirp)
2142 		vrele(tdirp);
2143 #endif
2144 	if (tond.ni_cnd.cn_nameiop) {
2145 		if (tond.ni_pathbuf != NULL) {
2146 			pathbuf_destroy(tond.ni_pathbuf);
2147 			tond.ni_pathbuf = NULL;
2148 		}
2149 	}
2150 	if (localfs) {
2151 		VFS_RENAMELOCK_EXIT(localfs);
2152 	}
2153 	if (fromnd.ni_cnd.cn_nameiop) {
2154 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2155 		if (fromnd.ni_pathbuf != NULL) {
2156 			pathbuf_destroy(fromnd.ni_pathbuf);
2157 			fromnd.ni_pathbuf = NULL;
2158 		}
2159 		vrele(fromnd.ni_dvp);
2160 		vrele(fvp);
2161 	}
2162 	return (error);
2163 }
2164 
2165 /*
2166  * nfs link service
2167  */
2168 int
2169 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2170 {
2171 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2172 	struct mbuf *nam = nfsd->nd_nam;
2173 	char *dpos = nfsd->nd_dpos;
2174 	kauth_cred_t cred = nfsd->nd_cr;
2175 	struct nameidata nd;
2176 	u_int32_t *tl;
2177 	int32_t t1;
2178 	char *bpos;
2179 	int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2180 	int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2181 	char *cp2;
2182 	struct mbuf *mb, *mreq __unused;
2183 	struct vnode *vp, *xp, *dirp = (struct vnode *)0;
2184 	struct vattr dirfor, diraft, at;
2185 	nfsrvfh_t nsfh, dnsfh;
2186 	u_quad_t frev;
2187 
2188 	nfsm_srvmtofh(&nsfh);
2189 	nfsm_srvmtofh(&dnsfh);
2190 	nfsm_srvnamesiz(len);
2191 	error = nfsrv_fhtovp(&nsfh, false, &vp, cred, slp, nam,
2192 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2193 	if (error) {
2194 		nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2195 		nfsm_srvpostop_attr(getret, &at);
2196 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2197 		return (0);
2198 	}
2199 	if (vp->v_type == VDIR) {
2200 		error = EPERM;
2201 		goto out1;
2202 	}
2203 	nd.ni_cnd.cn_cred = cred;
2204 	nd.ni_cnd.cn_nameiop = CREATE;
2205 	nd.ni_cnd.cn_flags = LOCKPARENT;
2206 	error = nfs_namei(&nd, &dnsfh, len, slp, nam, &md, &dpos,
2207 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2208 	if (dirp && v3) {
2209 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2210 	}
2211 	if (error)
2212 		goto out1;
2213 	xp = nd.ni_vp;
2214 	if (xp != NULL) {
2215 		error = EEXIST;
2216 		goto out;
2217 	}
2218 	xp = nd.ni_dvp;
2219 	if (vp->v_mount != xp->v_mount)
2220 		error = EXDEV;
2221 out:
2222 	if (!error) {
2223 		nqsrv_getl(vp, ND_WRITE);
2224 		nqsrv_getl(xp, ND_WRITE);
2225 		error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2226 		if (nd.ni_dvp != nd.ni_vp)
2227 			VOP_UNLOCK(nd.ni_dvp);
2228 		vrele(nd.ni_dvp);
2229 	} else {
2230 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2231 		if (nd.ni_dvp == nd.ni_vp)
2232 			vrele(nd.ni_dvp);
2233 		else
2234 			vput(nd.ni_dvp);
2235 		if (nd.ni_vp)
2236 			vrele(nd.ni_vp);
2237 	}
2238 out1:
2239 	if (v3) {
2240 		vn_lock(vp, LK_SHARED | LK_RETRY);
2241 		getret = VOP_GETATTR(vp, &at, cred);
2242 		VOP_UNLOCK(vp);
2243 	}
2244 	if (dirp) {
2245 		if (v3) {
2246 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2247 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2248 			VOP_UNLOCK(dirp);
2249 		}
2250 		vrele(dirp);
2251 	}
2252 	vrele(vp);
2253 	if (nd.ni_pathbuf != NULL) {
2254 		pathbuf_destroy(nd.ni_pathbuf);
2255 		nd.ni_pathbuf = NULL;
2256 	}
2257 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2258 	if (v3) {
2259 		nfsm_srvpostop_attr(getret, &at);
2260 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2261 		return (0);
2262 	}
2263 	nfsm_srvdone;
2264 }
2265 
2266 /*
2267  * nfs symbolic link service
2268  */
2269 int
2270 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2271 {
2272 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2273 	struct mbuf *nam = nfsd->nd_nam;
2274 	char *dpos = nfsd->nd_dpos;
2275 	kauth_cred_t cred = nfsd->nd_cr;
2276 	struct vattr va, dirfor, diraft;
2277 	struct nameidata nd;
2278 	u_int32_t *tl;
2279 	int32_t t1;
2280 	struct nfsv2_sattr *sp;
2281 	char *bpos, *pathcp = NULL, *cp2;
2282 	struct uio io;
2283 	struct iovec iv;
2284 	int error = 0, cache = 0, dirfor_ret = 1, diraft_ret = 1, abort = 0;
2285 	uint32_t len, len2;
2286 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2287 	struct mbuf *mb, *mreq __unused;
2288 	struct vnode *dirp = (struct vnode *)0;
2289 	nfsrvfh_t nsfh;
2290 	u_quad_t frev;
2291 
2292 	nd.ni_cnd.cn_nameiop = 0;
2293 	nfsm_srvmtofh(&nsfh);
2294 	nfsm_srvnamesiz(len);
2295 	nd.ni_cnd.cn_cred = cred;
2296 	nd.ni_cnd.cn_nameiop = CREATE;
2297 	nd.ni_cnd.cn_flags = LOCKPARENT;
2298 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2299 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2300 	if (dirp && v3) {
2301 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2302 	}
2303 	if (error)
2304 		goto out;
2305 	abort = 1;
2306 	vattr_null(&va);
2307 	va.va_type = VLNK;
2308 	if (v3) {
2309 		va.va_mode = 0;
2310 		nfsm_srvsattr(&va);
2311 		nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
2312 		len2 = fxdr_unsigned(uint32_t, *tl);
2313 		if (len2 > PATH_MAX) {
2314 			/* XXX should check _PC_NO_TRUNC */
2315 			error = ENAMETOOLONG;
2316 			goto abortop;
2317 		}
2318 	}
2319 	else {
2320 		/* NFSv2 */
2321 		nfsm_strsiz(len2, NFS_MAXPATHLEN);
2322 	}
2323 	pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2324 	iv.iov_base = pathcp;
2325 	iv.iov_len = len2;
2326 	io.uio_resid = len2;
2327 	io.uio_offset = 0;
2328 	io.uio_iov = &iv;
2329 	io.uio_iovcnt = 1;
2330 	io.uio_rw = UIO_READ;
2331 	UIO_SETUP_SYSSPACE(&io);
2332 	nfsm_mtouio(&io, len2);
2333 	if (!v3) {
2334 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2335 		va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2336 	}
2337 	*(pathcp + len2) = '\0';
2338 	if (nd.ni_vp) {
2339 		error = EEXIST;
2340 abortop:
2341 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2342 		if (nd.ni_dvp == nd.ni_vp)
2343 			vrele(nd.ni_dvp);
2344 		else
2345 			vput(nd.ni_dvp);
2346 		if (nd.ni_vp)
2347 			vrele(nd.ni_vp);
2348 		goto out;
2349 	}
2350 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
2351 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
2352 	if (!error) {
2353 	    if (v3) {
2354 		vn_lock(nd.ni_vp, LK_SHARED | LK_RETRY);
2355 		error = nfsrv_composefh(nd.ni_vp, &nsfh, v3);
2356 		if (!error)
2357 		    error = VOP_GETATTR(nd.ni_vp, &va, cred);
2358 		vput(nd.ni_vp);
2359 	    } else {
2360 		vrele(nd.ni_vp);
2361 	    }
2362 	}
2363 	vput(nd.ni_dvp);
2364 out:
2365 	if (pathcp)
2366 		free(pathcp, M_TEMP);
2367 	if (dirp) {
2368 		if (v3) {
2369 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2370 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2371 			VOP_UNLOCK(dirp);
2372 		}
2373 		vrele(dirp);
2374 		dirp = NULL;
2375 	}
2376 	if (nd.ni_pathbuf != NULL) {
2377 		pathbuf_destroy(nd.ni_pathbuf);
2378 		nd.ni_pathbuf = NULL;
2379 	}
2380 	abort = 0;
2381 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2382 	    NFSX_WCCDATA(v3));
2383 	if (v3) {
2384 		if (!error) {
2385 			nfsm_srvpostop_fh(&nsfh);
2386 			nfsm_srvpostop_attr(0, &va);
2387 		}
2388 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2389 	}
2390 	return (0);
2391 nfsmout:
2392 	if (abort) {
2393 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2394 		if (nd.ni_dvp == nd.ni_vp)
2395 			vrele(nd.ni_dvp);
2396 		else
2397 			vput(nd.ni_dvp);
2398 		if (nd.ni_vp)
2399 			vrele(nd.ni_vp);
2400 		if (nd.ni_pathbuf != NULL) {
2401 			pathbuf_destroy(nd.ni_pathbuf);
2402 			nd.ni_pathbuf = NULL;
2403 		}
2404 	}
2405 	if (dirp)
2406 		vrele(dirp);
2407 	if (pathcp)
2408 		free(pathcp, M_TEMP);
2409 	return (error);
2410 }
2411 
2412 /*
2413  * nfs mkdir service
2414  */
2415 int
2416 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2417 {
2418 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2419 	struct mbuf *nam = nfsd->nd_nam;
2420 	char *dpos = nfsd->nd_dpos;
2421 	kauth_cred_t cred = nfsd->nd_cr;
2422 	struct vattr va, dirfor, diraft;
2423 	struct nfs_fattr *fp;
2424 	struct nameidata nd;
2425 	char *cp;
2426 	u_int32_t *tl;
2427 	int32_t t1;
2428 	char *bpos;
2429 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2430 	int abort = 0;
2431 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2432 	char *cp2;
2433 	struct mbuf *mb, *mreq __unused;
2434 	struct vnode *vp, *dirp = (struct vnode *)0;
2435 	nfsrvfh_t nsfh;
2436 	u_quad_t frev;
2437 
2438 	nfsm_srvmtofh(&nsfh);
2439 	nfsm_srvnamesiz(len);
2440 	nd.ni_cnd.cn_cred = cred;
2441 	nd.ni_cnd.cn_nameiop = CREATE;
2442 	nd.ni_cnd.cn_flags = LOCKPARENT;
2443 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2444 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2445 	if (dirp && v3) {
2446 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2447 	}
2448 	if (error) {
2449 		if (nd.ni_pathbuf != NULL) {
2450 			pathbuf_destroy(nd.ni_pathbuf);
2451 			nd.ni_pathbuf = NULL;
2452 		}
2453 		nfsm_reply(NFSX_WCCDATA(v3));
2454 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2455 		if (dirp)
2456 			vrele(dirp);
2457 		return (0);
2458 	}
2459 	abort = 1;
2460 	vattr_null(&va);
2461 	if (v3) {
2462 		va.va_mode = 0;
2463 		nfsm_srvsattr(&va);
2464 	} else {
2465 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2466 		va.va_mode = nfstov_mode(*tl++);
2467 	}
2468 	va.va_type = VDIR;
2469 	vp = nd.ni_vp;
2470 	if (vp != NULL) {
2471 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2472 		if (nd.ni_dvp == vp)
2473 			vrele(nd.ni_dvp);
2474 		else
2475 			vput(nd.ni_dvp);
2476 		vrele(vp);
2477 		error = EEXIST;
2478 		goto out;
2479 	}
2480 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
2481 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
2482 	if (!error) {
2483 		vp = nd.ni_vp;
2484 		vn_lock(vp, LK_SHARED | LK_RETRY);
2485 		error = nfsrv_composefh(vp, &nsfh, v3);
2486 		if (!error)
2487 			error = VOP_GETATTR(vp, &va, cred);
2488 		vput(vp);
2489 	}
2490 	vput(nd.ni_dvp);
2491 out:
2492 	if (dirp) {
2493 		if (v3) {
2494 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2495 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2496 			VOP_UNLOCK(dirp);
2497 		}
2498 		vrele(dirp);
2499 		dirp = NULL;
2500 	}
2501 	if (nd.ni_pathbuf != NULL) {
2502 		pathbuf_destroy(nd.ni_pathbuf);
2503 		nd.ni_pathbuf = NULL;
2504 	}
2505 	abort = 0;
2506 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2507 	    NFSX_WCCDATA(v3));
2508 	if (v3) {
2509 		if (!error) {
2510 			nfsm_srvpostop_fh(&nsfh);
2511 			nfsm_srvpostop_attr(0, &va);
2512 		}
2513 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2514 	} else {
2515 		nfsm_srvfhtom(&nsfh, v3);
2516 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2517 		nfsm_srvfillattr(&va, fp);
2518 	}
2519 	return (0);
2520 nfsmout:
2521 	if (abort) {
2522 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2523 		if (nd.ni_dvp == nd.ni_vp)
2524 			vrele(nd.ni_dvp);
2525 		else
2526 			vput(nd.ni_dvp);
2527 		if (nd.ni_vp)
2528 			vrele(nd.ni_vp);
2529 		if (nd.ni_pathbuf != NULL) {
2530 			pathbuf_destroy(nd.ni_pathbuf);
2531 			nd.ni_pathbuf = NULL;
2532 		}
2533 	}
2534 	if (dirp)
2535 		vrele(dirp);
2536 	return (error);
2537 }
2538 
2539 /*
2540  * nfs rmdir service
2541  */
2542 int
2543 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2544 {
2545 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2546 	struct mbuf *nam = nfsd->nd_nam;
2547 	char *dpos = nfsd->nd_dpos;
2548 	kauth_cred_t cred = nfsd->nd_cr;
2549 	u_int32_t *tl;
2550 	int32_t t1;
2551 	char *bpos;
2552 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2553 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2554 	char *cp2;
2555 	struct mbuf *mb, *mreq __unused;
2556 	struct vnode *vp, *dirp = (struct vnode *)0;
2557 	struct vattr dirfor, diraft;
2558 	nfsrvfh_t nsfh;
2559 	struct nameidata nd;
2560 	u_quad_t frev;
2561 
2562 	nfsm_srvmtofh(&nsfh);
2563 	nfsm_srvnamesiz(len);
2564 	nd.ni_cnd.cn_cred = cred;
2565 	nd.ni_cnd.cn_nameiop = DELETE;
2566 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2567 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2568 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2569 	if (dirp && v3) {
2570 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2571 	}
2572 	if (error) {
2573 		if (nd.ni_pathbuf != NULL) {
2574 			pathbuf_destroy(nd.ni_pathbuf);
2575 			nd.ni_pathbuf = NULL;
2576 		}
2577 		nfsm_reply(NFSX_WCCDATA(v3));
2578 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2579 		if (dirp)
2580 			vrele(dirp);
2581 		return (0);
2582 	}
2583 	vp = nd.ni_vp;
2584 	if (vp->v_type != VDIR) {
2585 		error = ENOTDIR;
2586 		goto out;
2587 	}
2588 	/*
2589 	 * No rmdir "." please.
2590 	 */
2591 	if (nd.ni_dvp == vp) {
2592 		error = EINVAL;
2593 		goto out;
2594 	}
2595 	/*
2596 	 * The root of a mounted filesystem cannot be deleted.
2597 	 */
2598 	if (vp->v_vflag & VV_ROOT)
2599 		error = EBUSY;
2600 out:
2601 	if (!error) {
2602 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
2603 		nqsrv_getl(vp, ND_WRITE);
2604 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2605 		vput(nd.ni_dvp);
2606 	} else {
2607 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2608 		if (nd.ni_dvp == nd.ni_vp)
2609 			vrele(nd.ni_dvp);
2610 		else
2611 			vput(nd.ni_dvp);
2612 		vput(vp);
2613 	}
2614 	if (nd.ni_pathbuf != NULL) {
2615 		pathbuf_destroy(nd.ni_pathbuf);
2616 		nd.ni_pathbuf = NULL;
2617 	}
2618 	if (dirp) {
2619 		if (v3) {
2620 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2621 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2622 			VOP_UNLOCK(dirp);
2623 		}
2624 		vrele(dirp);
2625 	}
2626 	nfsm_reply(NFSX_WCCDATA(v3));
2627 	if (v3) {
2628 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2629 		return (0);
2630 	}
2631 	nfsm_srvdone;
2632 }
2633 
2634 /*
2635  * nfs readdir service
2636  * - mallocs what it thinks is enough to read
2637  *	count rounded up to a multiple of NFS_SRVDIRBLKSIZ <= NFS_MAXREADDIR
2638  * - calls VOP_READDIR()
2639  * - loops around building the reply
2640  *	if the output generated exceeds count break out of loop
2641  *	The nfsm_clget macro is used here so that the reply will be packed
2642  *	tightly in mbuf clusters.
2643  * - it only knows that it has encountered eof when the VOP_READDIR()
2644  *	reads nothing
2645  * - as such one readdir rpc will return eof false although you are there
2646  *	and then the next will return eof
2647  * - it trims out records with d_fileno == 0
2648  *	this doesn't matter for Unix clients, but they might confuse clients
2649  *	for other os'.
2650  * - it trims out records with d_type == DT_WHT
2651  *	these cannot be seen through NFS (unless we extend the protocol)
2652  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2653  *	than requested, but this may not apply to all filesystems. For
2654  *	example, client NFS does not { although it is never remote mounted
2655  *	anyhow }
2656  *     The alternate call nfsrv_readdirplus() does lookups as well.
2657  * PS: The NFS protocol spec. does not clarify what the "count" byte
2658  *	argument is a count of.. just name strings and file id's or the
2659  *	entire reply rpc or ...
2660  *	I tried just file name and id sizes and it confused the Sun client,
2661  *	so I am using the full rpc size now. The "paranoia.." comment refers
2662  *	to including the status longwords that are not a part of the dir.
2663  *	"entry" structures, but are in the rpc.
2664  */
2665 
2666 #define	NFS_SRVDIRBLKSIZ	1024
2667 
2668 struct flrep {
2669 	nfsuint64 fl_off;
2670 	u_int32_t fl_postopok;
2671 	struct nfs_fattr fl_fattr; /* XXX: must be of fattr3 size */
2672 	u_int32_t fl_fhok;
2673 	u_int32_t fl_fhsize;
2674 	/* handle comes here, filled in dynamically */
2675 };
2676 
2677 int
2678 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2679 {
2680 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2681 	struct mbuf *nam = nfsd->nd_nam;
2682 	char *dpos = nfsd->nd_dpos;
2683 	kauth_cred_t cred = nfsd->nd_cr;
2684 	char *bp, *be;
2685 	struct mbuf *mp;
2686 	struct dirent *dp;
2687 	char *cp;
2688 	u_int32_t *tl;
2689 	int32_t t1;
2690 	char *bpos;
2691 	struct mbuf *mb, *mreq __unused, *mp2;
2692 	char *cpos, *cend, *cp2, *rbuf;
2693 	struct vnode *vp;
2694 	struct vattr at;
2695 	nfsrvfh_t nsfh;
2696 	struct uio io;
2697 	struct iovec iv;
2698 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2699 	int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, ncookies;
2700 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2701 	u_quad_t frev, off, toff;
2702 #ifdef NFS3_STRICTVERF
2703 	u_quad_t verf;
2704 #endif
2705 	off_t *cookies = NULL, *cookiep;
2706 	nfsuint64 jar;
2707 
2708 	nfsm_srvmtofh(&nsfh);
2709 	if (v3) {
2710 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2711 		toff = fxdr_hyper(tl);
2712 		tl += 2;
2713 #ifdef NFS3_STRICTVERF
2714 		verf = fxdr_hyper(tl);
2715 #endif
2716 		tl += 2;
2717 	} else {
2718 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2719 		toff = fxdr_unsigned(u_quad_t, *tl++);
2720 	}
2721 	off = toff;
2722 	cnt = fxdr_unsigned(int, *tl);
2723 	siz = ((cnt + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2724 	xfer = NFS_SRVMAXDATA(nfsd);
2725 	if (siz > xfer)
2726 		siz = xfer;
2727 	fullsiz = siz;
2728 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2729 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2730 	if (!error && vp->v_type != VDIR) {
2731 		error = ENOTDIR;
2732 		vput(vp);
2733 	}
2734 	if (error) {
2735 		nfsm_reply(NFSX_UNSIGNED);
2736 		nfsm_srvpostop_attr(getret, &at);
2737 		return (0);
2738 	}
2739 	nqsrv_getl(vp, ND_READ);
2740 	if (v3) {
2741 		error = getret = VOP_GETATTR(vp, &at, cred);
2742 #ifdef NFS3_STRICTVERF
2743 		/*
2744 		 * XXX This check is too strict for Solaris 2.5 clients.
2745 		 */
2746 		if (!error && toff && verf != at.va_filerev)
2747 			error = NFSERR_BAD_COOKIE;
2748 #endif
2749 	}
2750 	if (!error)
2751 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2752 	if (error) {
2753 		vput(vp);
2754 		nfsm_reply(NFSX_POSTOPATTR(v3));
2755 		nfsm_srvpostop_attr(getret, &at);
2756 		return (0);
2757 	}
2758 	VOP_UNLOCK(vp);
2759 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
2760 again:
2761 	iv.iov_base = rbuf;
2762 	iv.iov_len = fullsiz;
2763 	io.uio_iov = &iv;
2764 	io.uio_iovcnt = 1;
2765 	io.uio_offset = (off_t)off;
2766 	io.uio_resid = fullsiz;
2767 	io.uio_rw = UIO_READ;
2768 	UIO_SETUP_SYSSPACE(&io);
2769 	eofflag = 0;
2770 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2771 
2772 	error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2773 
2774 	off = (off_t)io.uio_offset;
2775 	if (!cookies && !error)
2776 		error = NFSERR_PERM;
2777 	if (v3) {
2778 		getret = VOP_GETATTR(vp, &at, cred);
2779 		if (!error)
2780 			error = getret;
2781 	}
2782 
2783 	VOP_UNLOCK(vp);
2784 	if (error) {
2785 		vrele(vp);
2786 		free((void *)rbuf, M_TEMP);
2787 		if (cookies)
2788 			free((void *)cookies, M_TEMP);
2789 		nfsm_reply(NFSX_POSTOPATTR(v3));
2790 		nfsm_srvpostop_attr(getret, &at);
2791 		return (0);
2792 	}
2793 	if (io.uio_resid) {
2794 		siz -= io.uio_resid;
2795 
2796 		/*
2797 		 * If nothing read, return eof
2798 		 * rpc reply
2799 		 */
2800 		if (siz == 0) {
2801 			vrele(vp);
2802 			nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2803 				2 * NFSX_UNSIGNED);
2804 			if (v3) {
2805 				nfsm_srvpostop_attr(getret, &at);
2806 				nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2807 				txdr_hyper(at.va_filerev, tl);
2808 				tl += 2;
2809 			} else
2810 				nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2811 			*tl++ = nfs_false;
2812 			*tl = nfs_true;
2813 			free((void *)rbuf, M_TEMP);
2814 			free((void *)cookies, M_TEMP);
2815 			return (0);
2816 		}
2817 	}
2818 
2819 	/*
2820 	 * Check for degenerate cases of nothing useful read.
2821 	 * If so go try again
2822 	 */
2823 	cpos = rbuf;
2824 	cend = rbuf + siz;
2825 	dp = (struct dirent *)cpos;
2826 	cookiep = cookies;
2827 
2828 	while (cpos < cend && ncookies > 0 &&
2829 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2830 		cpos += dp->d_reclen;
2831 		dp = (struct dirent *)cpos;
2832 		cookiep++;
2833 		ncookies--;
2834 	}
2835 	if (cpos >= cend || ncookies == 0) {
2836 		toff = off;
2837 		siz = fullsiz;
2838 		free(cookies, M_TEMP);
2839 		cookies = NULL;
2840 		goto again;
2841 	}
2842 
2843 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
2844 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2845 	if (v3) {
2846 		nfsm_srvpostop_attr(getret, &at);
2847 		nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2848 		txdr_hyper(at.va_filerev, tl);
2849 	}
2850 	mp = mp2 = mb;
2851 	bp = bpos;
2852 	be = bp + M_TRAILINGSPACE(mp);
2853 
2854 	/* Loop through the records and build reply */
2855 	while (cpos < cend && ncookies > 0) {
2856 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2857 			nlen = dp->d_namlen;
2858 			rem = nfsm_rndup(nlen)-nlen;
2859 			len += (4 * NFSX_UNSIGNED + nlen + rem);
2860 			if (v3)
2861 				len += 2 * NFSX_UNSIGNED;
2862 			if (len > cnt) {
2863 				eofflag = 0;
2864 				break;
2865 			}
2866 			/*
2867 			 * Build the directory record xdr from
2868 			 * the dirent entry.
2869 			 */
2870 			nfsm_clget;
2871 			*tl = nfs_true;
2872 			bp += NFSX_UNSIGNED;
2873 			if (v3) {
2874 				nfsm_clget;
2875 				*tl = txdr_unsigned(dp->d_fileno >> 32);
2876 				bp += NFSX_UNSIGNED;
2877 			}
2878 			nfsm_clget;
2879 			*tl = txdr_unsigned(dp->d_fileno);
2880 			bp += NFSX_UNSIGNED;
2881 			nfsm_clget;
2882 			*tl = txdr_unsigned(nlen);
2883 			bp += NFSX_UNSIGNED;
2884 
2885 			/* And loop around copying the name */
2886 			xfer = nlen;
2887 			cp = dp->d_name;
2888 			while (xfer > 0) {
2889 				nfsm_clget;
2890 				if ((bp+xfer) > be)
2891 					tsiz = be-bp;
2892 				else
2893 					tsiz = xfer;
2894 				memcpy(bp, cp, tsiz);
2895 				bp += tsiz;
2896 				xfer -= tsiz;
2897 				if (xfer > 0)
2898 					cp += tsiz;
2899 			}
2900 			/* And null pad to an int32_t boundary */
2901 			for (i = 0; i < rem; i++)
2902 				*bp++ = '\0';
2903 			nfsm_clget;
2904 
2905 			/* Finish off the record */
2906 			txdr_hyper(*cookiep, &jar);
2907 			if (v3) {
2908 				*tl = jar.nfsuquad[0];
2909 				bp += NFSX_UNSIGNED;
2910 				nfsm_clget;
2911 			}
2912 			*tl = jar.nfsuquad[1];
2913 			bp += NFSX_UNSIGNED;
2914 		}
2915 		cpos += dp->d_reclen;
2916 		dp = (struct dirent *)cpos;
2917 		cookiep++;
2918 		ncookies--;
2919 	}
2920 	vrele(vp);
2921 	nfsm_clget;
2922 	*tl = nfs_false;
2923 	bp += NFSX_UNSIGNED;
2924 	nfsm_clget;
2925 	if (eofflag)
2926 		*tl = nfs_true;
2927 	else
2928 		*tl = nfs_false;
2929 	bp += NFSX_UNSIGNED;
2930 	if (mp != mb) {
2931 		if (bp < be)
2932 			mp->m_len = bp - mtod(mp, char *);
2933 	} else
2934 		mp->m_len += bp - bpos;
2935 	free((void *)rbuf, M_TEMP);
2936 	free((void *)cookies, M_TEMP);
2937 	nfsm_srvdone;
2938 }
2939 
2940 int
2941 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2942 {
2943 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2944 	struct mbuf *nam = nfsd->nd_nam;
2945 	char *dpos = nfsd->nd_dpos;
2946 	kauth_cred_t cred = nfsd->nd_cr;
2947 	char *bp, *be;
2948 	struct mbuf *mp;
2949 	struct dirent *dp;
2950 	char *cp;
2951 	u_int32_t *tl;
2952 	int32_t t1;
2953 	char *bpos;
2954 	struct mbuf *mb, *mreq __unused, *mp2;
2955 	char *cpos, *cend, *cp2, *rbuf;
2956 	struct vnode *vp, *nvp;
2957 	struct flrep fl;
2958 	nfsrvfh_t nsfh;
2959 	struct uio io;
2960 	struct iovec iv;
2961 	struct vattr va, at, *vap = &va;
2962 	struct nfs_fattr *fp;
2963 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2964 	int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, dirlen, ncookies;
2965 	u_quad_t frev, off, toff;
2966 #ifdef NFS3_STRICTVERF
2967 	u_quad_t verf;
2968 #endif
2969 	off_t *cookies = NULL, *cookiep;
2970 
2971 	nfsm_srvmtofh(&nsfh);
2972 	nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2973 	toff = fxdr_hyper(tl);
2974 	tl += 2;
2975 #ifdef NFS3_STRICTVERF
2976 	verf = fxdr_hyper(tl);
2977 #endif
2978 	tl += 2;
2979 	siz = fxdr_unsigned(int, *tl++);
2980 	cnt = fxdr_unsigned(int, *tl);
2981 	off = toff;
2982 	siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2983 	xfer = NFS_SRVMAXDATA(nfsd);
2984 	if (siz > xfer)
2985 		siz = xfer;
2986 	fullsiz = siz;
2987 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2988 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2989 	if (!error && vp->v_type != VDIR) {
2990 		error = ENOTDIR;
2991 		vput(vp);
2992 	}
2993 	if (error) {
2994 		nfsm_reply(NFSX_UNSIGNED);
2995 		nfsm_srvpostop_attr(getret, &at);
2996 		return (0);
2997 	}
2998 	error = getret = VOP_GETATTR(vp, &at, cred);
2999 #ifdef NFS3_STRICTVERF
3000 	/*
3001 	 * XXX This check is too strict for Solaris 2.5 clients.
3002 	 */
3003 	if (!error && toff && verf != at.va_filerev)
3004 		error = NFSERR_BAD_COOKIE;
3005 #endif
3006 	if (!error) {
3007 		nqsrv_getl(vp, ND_READ);
3008 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
3009 	}
3010 	if (error) {
3011 		vput(vp);
3012 		nfsm_reply(NFSX_V3POSTOPATTR);
3013 		nfsm_srvpostop_attr(getret, &at);
3014 		return (0);
3015 	}
3016 	VOP_UNLOCK(vp);
3017 
3018 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
3019 again:
3020 	iv.iov_base = rbuf;
3021 	iv.iov_len = fullsiz;
3022 	io.uio_iov = &iv;
3023 	io.uio_iovcnt = 1;
3024 	io.uio_offset = (off_t)off;
3025 	io.uio_resid = fullsiz;
3026 	io.uio_rw = UIO_READ;
3027 	UIO_SETUP_SYSSPACE(&io);
3028 	eofflag = 0;
3029 
3030 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3031 
3032 	error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
3033 
3034 	off = (u_quad_t)io.uio_offset;
3035 	getret = VOP_GETATTR(vp, &at, cred);
3036 
3037 	VOP_UNLOCK(vp);
3038 
3039 	/*
3040 	 * If the VGET operation doesn't work for this filesystem,
3041 	 * we can't support readdirplus. Returning NOTSUPP should
3042 	 * make clients fall back to plain readdir.
3043 	 * There's no need to check for VPTOFH as well, we wouldn't
3044 	 * even be here otherwise.
3045 	 */
3046 	if (!getret) {
3047 		if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
3048 			getret = (getret == EOPNOTSUPP) ?
3049 				NFSERR_NOTSUPP : NFSERR_IO;
3050 		else
3051 			vput(nvp);
3052 	}
3053 
3054 	if (!cookies && !error)
3055 		error = NFSERR_PERM;
3056 	if (!error)
3057 		error = getret;
3058 	if (error) {
3059 		vrele(vp);
3060 		if (cookies)
3061 			free((void *)cookies, M_TEMP);
3062 		free((void *)rbuf, M_TEMP);
3063 		nfsm_reply(NFSX_V3POSTOPATTR);
3064 		nfsm_srvpostop_attr(getret, &at);
3065 		return (0);
3066 	}
3067 	if (io.uio_resid) {
3068 		siz -= io.uio_resid;
3069 
3070 		/*
3071 		 * If nothing read, return eof
3072 		 * rpc reply
3073 		 */
3074 		if (siz == 0) {
3075 			vrele(vp);
3076 			nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3077 				2 * NFSX_UNSIGNED);
3078 			nfsm_srvpostop_attr(getret, &at);
3079 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3080 			txdr_hyper(at.va_filerev, tl);
3081 			tl += 2;
3082 			*tl++ = nfs_false;
3083 			*tl = nfs_true;
3084 			free((void *)cookies, M_TEMP);
3085 			free((void *)rbuf, M_TEMP);
3086 			return (0);
3087 		}
3088 	}
3089 
3090 	/*
3091 	 * Check for degenerate cases of nothing useful read.
3092 	 * If so go try again
3093 	 */
3094 	cpos = rbuf;
3095 	cend = rbuf + siz;
3096 	dp = (struct dirent *)cpos;
3097 	cookiep = cookies;
3098 
3099 	while (cpos < cend && ncookies > 0 &&
3100 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
3101 		cpos += dp->d_reclen;
3102 		dp = (struct dirent *)cpos;
3103 		cookiep++;
3104 		ncookies--;
3105 	}
3106 	if (cpos >= cend || ncookies == 0) {
3107 		toff = off;
3108 		siz = fullsiz;
3109 		free(cookies, M_TEMP);
3110 		cookies = NULL;
3111 		goto again;
3112 	}
3113 
3114 	dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3115 	nfsm_reply(cnt);
3116 	nfsm_srvpostop_attr(getret, &at);
3117 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3118 	txdr_hyper(at.va_filerev, tl);
3119 	mp = mp2 = mb;
3120 	bp = bpos;
3121 	be = bp + M_TRAILINGSPACE(mp);
3122 
3123 	/* Loop through the records and build reply */
3124 	while (cpos < cend && ncookies > 0) {
3125 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3126 			nfsrvfh_t nnsfh;
3127 
3128 			nlen = dp->d_namlen;
3129 			rem = nfsm_rndup(nlen)-nlen;
3130 
3131 			/*
3132 			 * For readdir_and_lookup get the vnode using
3133 			 * the file number.
3134 			 */
3135 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3136 				goto invalid;
3137 			if (nfsrv_composefh(nvp, &nnsfh, true)) {
3138 				vput(nvp);
3139 				goto invalid;
3140 			}
3141 			if (VOP_GETATTR(nvp, vap, cred)) {
3142 				vput(nvp);
3143 				goto invalid;
3144 			}
3145 			vput(nvp);
3146 
3147 			/*
3148 			 * If either the dircount or maxcount will be
3149 			 * exceeded, get out now. Both of these lengths
3150 			 * are calculated conservatively, including all
3151 			 * XDR overheads.
3152 			 */
3153 			len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3154 				NFSX_V3POSTOPATTR);
3155 			dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3156 			if (len > cnt || dirlen > fullsiz) {
3157 				eofflag = 0;
3158 				break;
3159 			}
3160 
3161 			/*
3162 			 * Build the directory record xdr from
3163 			 * the dirent entry.
3164 			 */
3165 			fp = (struct nfs_fattr *)&fl.fl_fattr;
3166 			nfsm_srvfillattr(vap, fp);
3167 			fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3168 			fl.fl_fhok = nfs_true;
3169 			fl.fl_postopok = nfs_true;
3170 			txdr_hyper(*cookiep, fl.fl_off.nfsuquad);
3171 
3172 			nfsm_clget;
3173 			*tl = nfs_true;
3174 			bp += NFSX_UNSIGNED;
3175 			nfsm_clget;
3176 			*tl = txdr_unsigned(dp->d_fileno >> 32);
3177 			bp += NFSX_UNSIGNED;
3178 			nfsm_clget;
3179 			*tl = txdr_unsigned(dp->d_fileno);
3180 			bp += NFSX_UNSIGNED;
3181 			nfsm_clget;
3182 			*tl = txdr_unsigned(nlen);
3183 			bp += NFSX_UNSIGNED;
3184 
3185 			/* And loop around copying the name */
3186 			xfer = nlen;
3187 			cp = dp->d_name;
3188 			while (xfer > 0) {
3189 				nfsm_clget;
3190 				if ((bp + xfer) > be)
3191 					tsiz = be - bp;
3192 				else
3193 					tsiz = xfer;
3194 				memcpy(bp, cp, tsiz);
3195 				bp += tsiz;
3196 				xfer -= tsiz;
3197 				if (xfer > 0)
3198 					cp += tsiz;
3199 			}
3200 			/* And null pad to an int32_t boundary */
3201 			for (i = 0; i < rem; i++)
3202 				*bp++ = '\0';
3203 
3204 			/*
3205 			 * Now copy the flrep structure out.
3206 			 */
3207 			xfer = sizeof(struct flrep);
3208 			cp = (void *)&fl;
3209 			while (xfer > 0) {
3210 				nfsm_clget;
3211 				if ((bp + xfer) > be)
3212 					tsiz = be - bp;
3213 				else
3214 					tsiz = xfer;
3215 				memcpy(bp, cp, tsiz);
3216 				bp += tsiz;
3217 				xfer -= tsiz;
3218 				if (xfer > 0)
3219 					cp += tsiz;
3220 			}
3221 
3222 			/*
3223 			 * ... and filehandle.
3224 			 */
3225 			xfer = NFSRVFH_SIZE(&nnsfh);
3226 			cp = NFSRVFH_DATA(&nnsfh);
3227 			while (xfer > 0) {
3228 				nfsm_clget;
3229 				if ((bp + xfer) > be)
3230 					tsiz = be - bp;
3231 				else
3232 					tsiz = xfer;
3233 				memcpy(bp, cp, tsiz);
3234 				bp += tsiz;
3235 				xfer -= tsiz;
3236 				if (xfer > 0)
3237 					cp += tsiz;
3238 			}
3239 		}
3240 invalid:
3241 		cpos += dp->d_reclen;
3242 		dp = (struct dirent *)cpos;
3243 		cookiep++;
3244 		ncookies--;
3245 	}
3246 	vrele(vp);
3247 	nfsm_clget;
3248 	*tl = nfs_false;
3249 	bp += NFSX_UNSIGNED;
3250 	nfsm_clget;
3251 	if (eofflag)
3252 		*tl = nfs_true;
3253 	else
3254 		*tl = nfs_false;
3255 	bp += NFSX_UNSIGNED;
3256 	if (mp != mb) {
3257 		if (bp < be)
3258 			mp->m_len = bp - mtod(mp, char *);
3259 	} else
3260 		mp->m_len += bp - bpos;
3261 	free((void *)cookies, M_TEMP);
3262 	free((void *)rbuf, M_TEMP);
3263 	nfsm_srvdone;
3264 }
3265 
3266 /*
3267  * nfs commit service
3268  */
3269 int
3270 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3271 {
3272 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3273 	struct mbuf *nam = nfsd->nd_nam;
3274 	char *dpos = nfsd->nd_dpos;
3275 	kauth_cred_t cred = nfsd->nd_cr;
3276 	struct vattr bfor, aft;
3277 	struct vnode *vp;
3278 	nfsrvfh_t nsfh;
3279 	u_int32_t *tl;
3280 	int32_t t1;
3281 	char *bpos;
3282 	int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache = 0;
3283 	uint32_t cnt;
3284 	char *cp2;
3285 	struct mbuf *mb, *mreq __unused;
3286 	u_quad_t frev, off, end;
3287 
3288 	nfsm_srvmtofh(&nsfh);
3289 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3290 
3291 	off = fxdr_hyper(tl);
3292 	tl += 2;
3293 	cnt = fxdr_unsigned(uint32_t, *tl);
3294 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3295 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3296 	if (error) {
3297 		nfsm_reply(2 * NFSX_UNSIGNED);
3298 		nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3299 		return (0);
3300 	}
3301 	for_ret = VOP_GETATTR(vp, &bfor, cred);
3302 	end = (cnt > 0) ? off + cnt : vp->v_size;
3303 	if (end < off || end > vp->v_size)
3304 		end = vp->v_size;
3305 	if (off < vp->v_size)
3306 		error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end);
3307 	/* else error == 0, from nfsrv_fhtovp() */
3308 	aft_ret = VOP_GETATTR(vp, &aft, cred);
3309 	vput(vp);
3310 	nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3311 	nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3312 	if (!error) {
3313 		nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3314 		*tl++ = txdr_unsigned(boottime.tv_sec);
3315 		*tl = txdr_unsigned(boottime.tv_nsec / 1000);
3316 	} else {
3317 		return (0);
3318 	}
3319 	nfsm_srvdone;
3320 }
3321 
3322 /*
3323  * nfs statfs service
3324  */
3325 int
3326 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3327 {
3328 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3329 	struct mbuf *nam = nfsd->nd_nam;
3330 	char *dpos = nfsd->nd_dpos;
3331 	kauth_cred_t cred = nfsd->nd_cr;
3332 	struct statvfs *sf = NULL;
3333 	struct nfs_statfs *sfp;
3334 	u_int32_t *tl;
3335 	int32_t t1;
3336 	char *bpos;
3337 	int error = 0, rdonly, cache = 0, getret = 1;
3338 	int v3 = (nfsd->nd_flag & ND_NFSV3);
3339 	char *cp2;
3340 	struct mbuf *mb, *mreq __unused;
3341 	struct vnode *vp;
3342 	struct vattr at;
3343 	nfsrvfh_t nsfh;
3344 	u_quad_t frev, tval;
3345 
3346 	nfsm_srvmtofh(&nsfh);
3347 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3348 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3349 	if (error) {
3350 		nfsm_reply(NFSX_UNSIGNED);
3351 		nfsm_srvpostop_attr(getret, &at);
3352 		return (0);
3353 	}
3354 	sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK);
3355 	error = VFS_STATVFS(vp->v_mount, sf);
3356 	getret = VOP_GETATTR(vp, &at, cred);
3357 	vput(vp);
3358 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3359 	if (v3)
3360 		nfsm_srvpostop_attr(getret, &at);
3361 	if (error) {
3362 		free(sf, M_TEMP);
3363 		return (0);
3364 	}
3365 	nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3366 	if (v3) {
3367 		tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize);
3368 		txdr_hyper(tval, &sfp->sf_tbytes);
3369 		tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize);
3370 		txdr_hyper(tval, &sfp->sf_fbytes);
3371 		tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize);
3372 		txdr_hyper(tval, &sfp->sf_abytes);
3373 		tval = (u_quad_t)sf->f_files;
3374 		txdr_hyper(tval, &sfp->sf_tfiles);
3375 		tval = (u_quad_t)sf->f_ffree;
3376 		txdr_hyper(tval, &sfp->sf_ffiles);
3377 		txdr_hyper(tval, &sfp->sf_afiles);
3378 		sfp->sf_invarsec = 0;
3379 	} else {
3380 		sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3381 		sfp->sf_bsize = txdr_unsigned(sf->f_frsize);
3382 		sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3383 		sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3384 		sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3385 	}
3386 nfsmout:
3387 	if (sf)
3388 	    free(sf, M_TEMP);
3389 	return error;
3390 }
3391 
3392 /*
3393  * nfs fsinfo service
3394  */
3395 int
3396 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3397 {
3398 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3399 	struct mbuf *nam = nfsd->nd_nam;
3400 	char *dpos = nfsd->nd_dpos;
3401 	kauth_cred_t cred = nfsd->nd_cr;
3402 	u_int32_t *tl;
3403 	struct nfsv3_fsinfo *sip;
3404 	int32_t t1;
3405 	char *bpos;
3406 	int error = 0, rdonly, cache = 0, getret = 1;
3407 	uint32_t maxdata;
3408 	char *cp2;
3409 	struct mbuf *mb, *mreq __unused;
3410 	struct vnode *vp;
3411 	struct vattr at;
3412 	nfsrvfh_t nsfh;
3413 	u_quad_t frev, maxfsize;
3414 	struct statvfs *sb;
3415 
3416 	nfsm_srvmtofh(&nsfh);
3417 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3418 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3419 	if (error) {
3420 		nfsm_reply(NFSX_UNSIGNED);
3421 		nfsm_srvpostop_attr(getret, &at);
3422 		return (0);
3423 	}
3424 
3425 	/* XXX Try to make a guess on the max file size. */
3426 	sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK);
3427 	VFS_STATVFS(vp->v_mount, sb);
3428 	maxfsize = (u_quad_t)0x80000000 * sb->f_frsize - 1;
3429 	free(sb, M_TEMP);
3430 
3431 	getret = VOP_GETATTR(vp, &at, cred);
3432 	vput(vp);
3433 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3434 	nfsm_srvpostop_attr(getret, &at);
3435 	nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3436 
3437 	/*
3438 	 * XXX
3439 	 * There should be file system VFS OP(s) to get this information.
3440 	 * For now, assume ufs.
3441 	 */
3442 	if (slp->ns_so->so_type == SOCK_DGRAM)
3443 		maxdata = NFS_MAXDGRAMDATA;
3444 	else
3445 		maxdata = NFS_MAXDATA;
3446 	sip->fs_rtmax = txdr_unsigned(maxdata);
3447 	sip->fs_rtpref = txdr_unsigned(maxdata);
3448 	sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3449 	sip->fs_wtmax = txdr_unsigned(maxdata);
3450 	sip->fs_wtpref = txdr_unsigned(maxdata);
3451 	sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3452 	sip->fs_dtpref = txdr_unsigned(maxdata);
3453 	txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3454 	sip->fs_timedelta.nfsv3_sec = 0;
3455 	sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3456 	sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3457 		NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3458 		NFSV3FSINFO_CANSETTIME);
3459 	nfsm_srvdone;
3460 }
3461 
3462 /*
3463  * nfs pathconf service
3464  */
3465 int
3466 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3467 {
3468 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3469 	struct mbuf *nam = nfsd->nd_nam;
3470 	char *dpos = nfsd->nd_dpos;
3471 	kauth_cred_t cred = nfsd->nd_cr;
3472 	u_int32_t *tl;
3473 	struct nfsv3_pathconf *pc;
3474 	int32_t t1;
3475 	char *bpos;
3476 	int error = 0, rdonly, cache = 0, getret = 1;
3477 	register_t linkmax, namemax, chownres, notrunc;
3478 	char *cp2;
3479 	struct mbuf *mb, *mreq __unused;
3480 	struct vnode *vp;
3481 	struct vattr at;
3482 	nfsrvfh_t nsfh;
3483 	u_quad_t frev;
3484 
3485 	nfsm_srvmtofh(&nsfh);
3486 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3487 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3488 	if (error) {
3489 		nfsm_reply(NFSX_UNSIGNED);
3490 		nfsm_srvpostop_attr(getret, &at);
3491 		return (0);
3492 	}
3493 	error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3494 	if (!error)
3495 		error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3496 	if (!error)
3497 		error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3498 	if (!error)
3499 		error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
3500 	getret = VOP_GETATTR(vp, &at, cred);
3501 	vput(vp);
3502 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3503 	nfsm_srvpostop_attr(getret, &at);
3504 	if (error)
3505 		return (0);
3506 	nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3507 
3508 	pc->pc_linkmax = txdr_unsigned(linkmax);
3509 	pc->pc_namemax = txdr_unsigned(namemax);
3510 	pc->pc_notrunc = txdr_unsigned(notrunc);
3511 	pc->pc_chownrestricted = txdr_unsigned(chownres);
3512 
3513 	/*
3514 	 * These should probably be supported by VOP_PATHCONF(), but
3515 	 * until msdosfs is exportable (why would you want to?), the
3516 	 * Unix defaults should be ok.
3517 	 */
3518 	pc->pc_caseinsensitive = nfs_false;
3519 	pc->pc_casepreserving = nfs_true;
3520 	nfsm_srvdone;
3521 }
3522 
3523 /*
3524  * Null operation, used by clients to ping server
3525  */
3526 /* ARGSUSED */
3527 int
3528 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3529     struct lwp *lwp, struct mbuf **mrq)
3530 {
3531 	struct mbuf *mrep = nfsd->nd_mrep;
3532 	char *bpos;
3533 	int error = NFSERR_RETVOID, cache = 0;
3534 	struct mbuf *mb, *mreq __unused;
3535 	u_quad_t frev;
3536 
3537 	nfsm_reply(0);
3538 nfsmout:
3539 	return (0);
3540 }
3541 
3542 /*
3543  * No operation, used for obsolete procedures
3544  */
3545 /* ARGSUSED */
3546 int
3547 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3548     struct lwp *lwp, struct mbuf **mrq)
3549 {
3550 	struct mbuf *mrep = nfsd->nd_mrep;
3551 	char *bpos;
3552 	int error, cache = 0;
3553 	struct mbuf *mb, *mreq __unused;
3554 	u_quad_t frev;
3555 
3556 	if (nfsd->nd_repstat)
3557 		error = nfsd->nd_repstat;
3558 	else
3559 		error = EPROCUNAVAIL;
3560 	nfsm_reply(0);
3561 nfsmout:
3562 	return (0);
3563 }
3564 
3565 /*
3566  * Perform access checking for vnodes obtained from file handles that would
3567  * refer to files already opened by a Unix client. You cannot just use
3568  * vn_writechk() and VOP_ACCESS() for two reasons.
3569  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3570  * 2 - The owner is to be given access irrespective of mode bits for some
3571  *     operations, so that processes that chmod after opening a file don't
3572  *     break. I don't like this because it opens a security hole, but since
3573  *     the nfs server opens a security hole the size of a barn door anyhow,
3574  *     what the heck.
3575  *
3576  * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3577  * will return EPERM instead of EACCES. EPERM is always an error.
3578  */
3579 int
3580 nfsrv_access(struct vnode *vp, int flags, kauth_cred_t cred, int rdonly, struct lwp *lwp, int override)
3581 {
3582 	struct vattr vattr;
3583 	int error;
3584 	if (flags & VWRITE) {
3585 		/* Just vn_writechk() changed to check rdonly */
3586 		/*
3587 		 * Disallow write attempts on read-only file systems;
3588 		 * unless the file is a socket or a block or character
3589 		 * device resident on the file system.
3590 		 */
3591 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3592 			switch (vp->v_type) {
3593 			case VREG:
3594 			case VDIR:
3595 			case VLNK:
3596 				return (EROFS);
3597 			default:
3598 				break;
3599 			}
3600 		}
3601 
3602 		/*
3603 		 * If the vnode is in use as a process's text,
3604 		 * we can't allow writing.
3605 		 */
3606 		if (vp->v_iflag & VI_TEXT)
3607 			return (ETXTBSY);
3608 	}
3609 	error = VOP_GETATTR(vp, &vattr, cred);
3610 	if (error)
3611 		return (error);
3612 	error = VOP_ACCESS(vp, flags, cred);
3613 	/*
3614 	 * Allow certain operations for the owner (reads and writes
3615 	 * on files that are already open).
3616 	 */
3617 	if (override && error == EACCES && kauth_cred_geteuid(cred) == vattr.va_uid)
3618 		error = 0;
3619 	return error;
3620 }
3621