xref: /netbsd-src/sys/nfs/nfs_serv.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: nfs_serv.c,v 1.168 2013/12/14 22:04:03 christos 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.168 2013/12/14 22:04:03 christos 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 				if (exclusive_flag) {
1510 					exclusive_flag = 0;
1511 					vattr_null(&va);
1512 					/*
1513 					 * XXX
1514 					 * assuming NFSX_V3CREATEVERF
1515 					 * == sizeof(nfstime3)
1516 					 */
1517 					fxdr_nfsv3time(cverf, &va.va_atime);
1518 					error = VOP_SETATTR(nd.ni_vp, &va,
1519 						cred);
1520 				}
1521 			}
1522 		} else if (va.va_type == VCHR || va.va_type == VBLK ||
1523 			va.va_type == VFIFO) {
1524 			if (va.va_type == VCHR && rdev == 0xffffffff)
1525 				va.va_type = VFIFO;
1526 			if (va.va_type != VFIFO &&
1527 			    (error = kauth_authorize_system(cred,
1528 			    KAUTH_SYSTEM_MKNOD, 0, NULL, NULL, NULL))) {
1529 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1530 				vput(nd.ni_dvp);
1531 				abort = 0;
1532 				nfsm_reply(0);
1533 				if (nd.ni_pathbuf != NULL) {
1534 					pathbuf_destroy(nd.ni_pathbuf);
1535 					nd.ni_pathbuf = NULL;
1536 				}
1537 				return (error);
1538 			} else
1539 				va.va_rdev = (dev_t)rdev;
1540 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
1541 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
1542 			    &va);
1543 			if (error) {
1544 				nfsm_reply(0);
1545 			}
1546 		} else {
1547 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1548 			if (nd.ni_pathbuf != NULL) {
1549 				pathbuf_destroy(nd.ni_pathbuf);
1550 				nd.ni_pathbuf = NULL;
1551 			}
1552 			vput(nd.ni_dvp);
1553 			error = ENXIO;
1554 			abort = 0;
1555 		}
1556 		vp = nd.ni_vp;
1557 	} else {
1558 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1559 		if (nd.ni_pathbuf != NULL) {
1560 			pathbuf_destroy(nd.ni_pathbuf);
1561 			nd.ni_pathbuf = NULL;
1562 		}
1563 		vp = nd.ni_vp;
1564 		if (nd.ni_dvp == vp)
1565 			vrele(nd.ni_dvp);
1566 		else
1567 			vput(nd.ni_dvp);
1568 		abort = 0;
1569 		if (!error && va.va_size != -1) {
1570 			error = nfsrv_access(vp, VWRITE, cred,
1571 			    (nd.ni_cnd.cn_flags & RDONLY), lwp, 0);
1572 			if (!error) {
1573 				nqsrv_getl(vp, ND_WRITE);
1574 				tempsize = va.va_size;
1575 				vattr_null(&va);
1576 				va.va_size = tempsize;
1577 				error = VOP_SETATTR(vp, &va, cred);
1578 			}
1579 		}
1580 		if (error)
1581 			vput(vp);
1582 	}
1583 	if (!error) {
1584 		error = nfsrv_composefh(vp, &nsfh, v3);
1585 		if (!error)
1586 			error = VOP_GETATTR(vp, &va, cred);
1587 		vput(vp);
1588 	}
1589 	if (v3) {
1590 		if (exclusive_flag && !error) {
1591 			/*
1592 			 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3)
1593 			 */
1594 			char oldverf[NFSX_V3CREATEVERF];
1595 
1596 			txdr_nfsv3time(&va.va_atime, oldverf);
1597 			if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF))
1598 				error = EEXIST;
1599 		}
1600 		if (dirp) {
1601 			vn_lock(dirp, LK_SHARED | LK_RETRY);
1602 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1603 			VOP_UNLOCK(dirp);
1604 		}
1605 	}
1606 	if (dirp) {
1607 		vrele(dirp);
1608 		dirp = NULL;
1609 	}
1610 	if (nd.ni_pathbuf != NULL) {
1611 		pathbuf_destroy(nd.ni_pathbuf);
1612 		nd.ni_pathbuf = NULL;
1613 	}
1614 	abort = 0;
1615 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1616 	if (v3) {
1617 		if (!error) {
1618 			nfsm_srvpostop_fh(&nsfh);
1619 			nfsm_srvpostop_attr(0, &va);
1620 		}
1621 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1622 	} else {
1623 		nfsm_srvfhtom(&nsfh, v3);
1624 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1625 		nfsm_srvfillattr(&va, fp);
1626 	}
1627 	return (0);
1628 nfsmout:
1629 	if (dirp)
1630 		vrele(dirp);
1631 	if (abort) {
1632 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1633 		if (nd.ni_dvp == nd.ni_vp)
1634 			vrele(nd.ni_dvp);
1635 		else
1636 			vput(nd.ni_dvp);
1637 		if (nd.ni_vp)
1638 			vput(nd.ni_vp);
1639 	}
1640 	if (nd.ni_pathbuf != NULL) {
1641 		pathbuf_destroy(nd.ni_pathbuf);
1642 		nd.ni_pathbuf = NULL;
1643 	}
1644 	return (error);
1645 }
1646 
1647 /*
1648  * nfs v3 mknod service
1649  */
1650 int
1651 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1652 {
1653 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1654 	struct mbuf *nam = nfsd->nd_nam;
1655 	char *dpos = nfsd->nd_dpos;
1656 	kauth_cred_t cred = nfsd->nd_cr;
1657 	struct vattr va, dirfor, diraft;
1658 	u_int32_t *tl;
1659 	struct nameidata nd;
1660 	int32_t t1;
1661 	char *bpos;
1662 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1663 	int abort = 0;
1664 	u_int32_t major, minor;
1665 	enum vtype vtyp;
1666 	char *cp2;
1667 	struct mbuf *mb, *mreq __unused;
1668 	struct vnode *vp, *dirp = (struct vnode *)0;
1669 	nfsrvfh_t nsfh;
1670 	u_quad_t frev;
1671 
1672 	nd.ni_cnd.cn_nameiop = 0;
1673 	nfsm_srvmtofh(&nsfh);
1674 	nfsm_srvnamesiz(len);
1675 	nd.ni_cnd.cn_cred = cred;
1676 	nd.ni_cnd.cn_nameiop = CREATE;
1677 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1678 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1679 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1680 	if (dirp)
1681 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
1682 	if (error) {
1683 		nfsm_reply(NFSX_WCCDATA(1));
1684 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1685 		if (dirp)
1686 			vrele(dirp);
1687 		if (nd.ni_pathbuf != NULL) {
1688 			pathbuf_destroy(nd.ni_pathbuf);
1689 			nd.ni_pathbuf = NULL;
1690 		}
1691 		return (0);
1692 	}
1693 	abort = 1;
1694 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1695 	vtyp = nfsv3tov_type(*tl);
1696 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1697 		error = NFSERR_BADTYPE;
1698 		goto abort;
1699 	}
1700 	vattr_null(&va);
1701 	va.va_mode = 0;
1702 	nfsm_srvsattr(&va);
1703 	if (vtyp == VCHR || vtyp == VBLK) {
1704 		dev_t rdev;
1705 
1706 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1707 		major = fxdr_unsigned(u_int32_t, *tl++);
1708 		minor = fxdr_unsigned(u_int32_t, *tl);
1709 		rdev = makedev(major, minor);
1710 		if (major(rdev) != major || minor(rdev) != minor) {
1711 			error = EINVAL;
1712 			goto abort;
1713 		}
1714 		va.va_rdev = rdev;
1715 	}
1716 
1717 	/*
1718 	 * Iff doesn't exist, create it.
1719 	 */
1720 	if (nd.ni_vp) {
1721 		error = EEXIST;
1722 abort:
1723 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1724 		if (nd.ni_dvp == nd.ni_vp)
1725 			vrele(nd.ni_dvp);
1726 		else
1727 			vput(nd.ni_dvp);
1728 		if (nd.ni_vp)
1729 			vput(nd.ni_vp);
1730 		if (nd.ni_pathbuf != NULL) {
1731 			pathbuf_destroy(nd.ni_pathbuf);
1732 			nd.ni_pathbuf = NULL;
1733 		}
1734 		goto out;
1735 	}
1736 	va.va_type = vtyp;
1737 	if (vtyp == VSOCK) {
1738 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
1739 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1740 	} else {
1741 		if (va.va_type != VFIFO &&
1742 		    (error = kauth_authorize_system(cred,
1743 		    KAUTH_SYSTEM_MKNOD, 0, NULL, NULL, NULL))) {
1744 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1745 			vput(nd.ni_dvp);
1746 			goto out;
1747 		}
1748 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
1749 		error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1750 		if (error)
1751 			goto out;
1752 	}
1753 out:
1754 	vp = nd.ni_vp;
1755 	if (!error) {
1756 		error = nfsrv_composefh(vp, &nsfh, true);
1757 		if (!error)
1758 			error = VOP_GETATTR(vp, &va, cred);
1759 		vput(vp);
1760 	}
1761 	if (dirp) {
1762 		vn_lock(dirp, LK_SHARED | LK_RETRY);
1763 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1764 		VOP_UNLOCK(dirp);
1765 		vrele(dirp);
1766 		dirp = NULL;
1767 	}
1768 	if (nd.ni_pathbuf != NULL) {
1769 		pathbuf_destroy(nd.ni_pathbuf);
1770 		nd.ni_pathbuf = NULL;
1771 	}
1772 	abort = 0;
1773 	nfsm_reply(NFSX_SRVFH(&nsfh, true) + NFSX_POSTOPATTR(1) +
1774 	    NFSX_WCCDATA(1));
1775 	if (!error) {
1776 		nfsm_srvpostop_fh(&nsfh);
1777 		nfsm_srvpostop_attr(0, &va);
1778 	}
1779 	nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1780 	return (0);
1781 nfsmout:
1782 	if (abort) {
1783 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1784 		if (nd.ni_dvp == nd.ni_vp)
1785 			vrele(nd.ni_dvp);
1786 		else
1787 			vput(nd.ni_dvp);
1788 		if (nd.ni_vp)
1789 			vput(nd.ni_vp);
1790 	}
1791 	if (nd.ni_pathbuf != NULL) {
1792 		pathbuf_destroy(nd.ni_pathbuf);
1793 		nd.ni_pathbuf = NULL;
1794 	}
1795 	if (dirp)
1796 		vrele(dirp);
1797 	return (error);
1798 }
1799 
1800 /*
1801  * nfs remove service
1802  */
1803 int
1804 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1805 {
1806 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1807 	struct mbuf *nam = nfsd->nd_nam;
1808 	char *dpos = nfsd->nd_dpos;
1809 	kauth_cred_t cred = nfsd->nd_cr;
1810 	struct nameidata nd;
1811 	u_int32_t *tl;
1812 	int32_t t1;
1813 	char *bpos;
1814 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1815 	int v3 = (nfsd->nd_flag & ND_NFSV3);
1816 	char *cp2;
1817 	struct mbuf *mb, *mreq __unused;
1818 	struct vnode *vp, *dirp;
1819 	struct vattr dirfor, diraft;
1820 	nfsrvfh_t nsfh;
1821 	u_quad_t frev;
1822 
1823 #ifndef nolint
1824 	vp = (struct vnode *)0;
1825 #endif
1826 	nfsm_srvmtofh(&nsfh);
1827 	nfsm_srvnamesiz(len);
1828 	nd.ni_cnd.cn_cred = cred;
1829 	nd.ni_cnd.cn_nameiop = DELETE;
1830 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1831 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1832 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1833 	if (error == 0 && dirp && v3) {
1834 		if (nd.ni_dvp == nd.ni_vp)
1835 			vn_lock(dirp, LK_SHARED | LK_RETRY);
1836 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
1837 		if (nd.ni_dvp == nd.ni_vp)
1838 			VOP_UNLOCK(dirp);
1839 	}
1840 	if (!error) {
1841 		vp = nd.ni_vp;
1842 		if (vp->v_type == VDIR) {
1843 			error = EPERM;
1844 			goto out;
1845 		}
1846 		/*
1847 		 * The root of a mounted filesystem cannot be deleted.
1848 		 */
1849 		if (vp->v_vflag & VV_ROOT) {
1850 			error = EBUSY;
1851 		}
1852 out:
1853 		if (!error) {
1854 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
1855 			nqsrv_getl(vp, ND_WRITE);
1856 			error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1857 		} else {
1858 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1859 			if (nd.ni_dvp == vp)
1860 				vrele(nd.ni_dvp);
1861 			else
1862 				vput(nd.ni_dvp);
1863 			vput(vp);
1864 		}
1865 	}
1866 	if (nd.ni_pathbuf != NULL) {
1867 		pathbuf_destroy(nd.ni_pathbuf);
1868 		nd.ni_pathbuf = NULL;
1869 	}
1870 	if (dirp) {
1871 		if (v3) {
1872 			vn_lock(dirp, LK_SHARED | LK_RETRY);
1873 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1874 			VOP_UNLOCK(dirp);
1875 		}
1876 		vrele(dirp);
1877 	}
1878 	nfsm_reply(NFSX_WCCDATA(v3));
1879 	if (v3) {
1880 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1881 		return (0);
1882 	}
1883 	nfsm_srvdone;
1884 }
1885 
1886 /*
1887  * nfs rename service
1888  */
1889 int
1890 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
1891 {
1892 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1893 	struct mbuf *nam = nfsd->nd_nam;
1894 	char *dpos = nfsd->nd_dpos;
1895 	kauth_cred_t cred = nfsd->nd_cr;
1896 	u_int32_t *tl;
1897 	int32_t t1;
1898 	char *bpos;
1899 	int error = 0, cache = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1900 	uint32_t len, len2;
1901 	int tdirfor_ret = 1, tdiraft_ret = 1;
1902 	int v3 = (nfsd->nd_flag & ND_NFSV3);
1903 	char *cp2;
1904 	struct mbuf *mb, *mreq __unused;
1905 	struct nameidata fromnd, tond;
1906 	struct vnode *fvp, *tvp, *tdvp;
1907 	struct vnode *fdirp = NULL, *tdirp = NULL;
1908 	struct mount *localfs = NULL;
1909 	struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1910 	nfsrvfh_t fnsfh, tnsfh;
1911 	u_quad_t frev;
1912 	uid_t saved_uid;
1913 
1914 #ifndef nolint
1915 	fvp = (struct vnode *)0;
1916 #endif
1917 	fromnd.ni_cnd.cn_nameiop = 0;
1918 	tond.ni_cnd.cn_nameiop = 0;
1919 	nfsm_srvmtofh(&fnsfh);
1920 	nfsm_srvnamesiz(len);
1921 	/*
1922 	 * Remember our original uid so that we can reset cr_uid before
1923 	 * the second nfs_namei() call, in case it is remapped.
1924 	 */
1925 	saved_uid = kauth_cred_geteuid(cred);
1926 	fromnd.ni_cnd.cn_cred = cred;
1927 	fromnd.ni_cnd.cn_nameiop = DELETE;
1928 	fromnd.ni_cnd.cn_flags = LOCKPARENT | INRENAME;
1929 	error = nfs_namei(&fromnd, &fnsfh, len, slp, nam, &md,
1930 		&dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1931 	if (error == 0 && fdirp && v3) {
1932 		if (fromnd.ni_dvp == fromnd.ni_vp)
1933 			vn_lock(fdirp, LK_SHARED | LK_RETRY);
1934 		fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred);
1935 		if (fromnd.ni_dvp == fromnd.ni_vp)
1936 			VOP_UNLOCK(fdirp);
1937 	}
1938 	if (error) {
1939 		fromnd.ni_cnd.cn_nameiop = 0;
1940 		nfsm_reply(2 * NFSX_WCCDATA(v3));
1941 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1942 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1943 		if (fdirp)
1944 			vrele(fdirp);
1945 		if (fromnd.ni_pathbuf != NULL) {
1946 			pathbuf_destroy(fromnd.ni_pathbuf);
1947 		}
1948 		return (0);
1949 	}
1950 	if (fromnd.ni_dvp != fromnd.ni_vp) {
1951 		VOP_UNLOCK(fromnd.ni_dvp);
1952 	}
1953 	fvp = fromnd.ni_vp;
1954 
1955 	localfs = fvp->v_mount;
1956 	error = VFS_RENAMELOCK_ENTER(localfs);
1957 	if (error) {
1958 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1959 		vrele(fromnd.ni_dvp);
1960 		vrele(fvp);
1961 		goto out1;
1962 	}
1963 
1964 	/* Copied, regrettably, from vfs_syscalls.c (q.v.) */
1965 	vrele(fvp);
1966 	if ((fromnd.ni_cnd.cn_namelen == 1 &&
1967 	     fromnd.ni_cnd.cn_nameptr[0] == '.') ||
1968 	    (fromnd.ni_cnd.cn_namelen == 2 &&
1969 	     fromnd.ni_cnd.cn_nameptr[0] == '.' &&
1970 	     fromnd.ni_cnd.cn_nameptr[1] == '.')) {
1971 		error = EINVAL;
1972 		VFS_RENAMELOCK_EXIT(localfs);
1973 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1974 		vrele(fromnd.ni_dvp);
1975 		goto out1;
1976 	}
1977 	vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
1978 	error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd, 0);
1979 	if (error) {
1980 		VOP_UNLOCK(fromnd.ni_dvp);
1981 		VFS_RENAMELOCK_EXIT(localfs);
1982 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1983 		vrele(fromnd.ni_dvp);
1984 		goto out1;
1985 	}
1986 	VOP_UNLOCK(fromnd.ni_vp);
1987 	if (fromnd.ni_dvp != fromnd.ni_vp)
1988 		VOP_UNLOCK(fromnd.ni_dvp);
1989 	fvp = fromnd.ni_vp;
1990 
1991 	nfsm_srvmtofh(&tnsfh);
1992 	if (v3) {
1993 		nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1994 		len2 = fxdr_unsigned(uint32_t, *tl);
1995 		/* len2 will be checked by nfs_namei */
1996 	}
1997 	else {
1998 		/* NFSv2 */
1999 		nfsm_strsiz(len2, NFS_MAXNAMLEN);
2000 	}
2001 	kauth_cred_seteuid(cred, saved_uid);
2002 	tond.ni_cnd.cn_cred = cred;
2003 	tond.ni_cnd.cn_nameiop = RENAME;
2004 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | INRENAME;
2005 	error = nfs_namei(&tond, &tnsfh, len2, slp, nam, &md,
2006 		&dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2007 	if (tdirp && v3) {
2008 		tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred);
2009 	}
2010 	if (error) {
2011 		VFS_RENAMELOCK_EXIT(localfs);
2012 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2013 		vrele(fromnd.ni_dvp);
2014 		vrele(fvp);
2015 		goto out1;
2016 	}
2017 	tdvp = tond.ni_dvp;
2018 	tvp = tond.ni_vp;
2019 	if (tvp != NULL) {
2020 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2021 			if (v3)
2022 				error = EEXIST;
2023 			else
2024 				error = EISDIR;
2025 			goto out;
2026 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2027 			if (v3)
2028 				error = EEXIST;
2029 			else
2030 				error = ENOTDIR;
2031 			goto out;
2032 		}
2033 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2034 			if (v3)
2035 				error = EXDEV;
2036 			else
2037 				error = ENOTEMPTY;
2038 			goto out;
2039 		}
2040 	}
2041 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2042 		if (v3)
2043 			error = EXDEV;
2044 		else
2045 			error = ENOTEMPTY;
2046 		goto out;
2047 	}
2048 	if (fvp->v_mount != tdvp->v_mount) {
2049 		if (v3)
2050 			error = EXDEV;
2051 		else
2052 			error = ENOTEMPTY;
2053 		goto out;
2054 	}
2055 	if (fvp == tdvp) {
2056 		if (v3)
2057 			error = EINVAL;
2058 		else
2059 			error = ENOTEMPTY;
2060 	}
2061 	/*
2062 	 * If source is the same as the destination (that is the
2063 	 * same vnode with the same name in the same directory),
2064 	 * then there is nothing to do.
2065 	 */
2066 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2067 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2068 	    !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2069 	      fromnd.ni_cnd.cn_namelen))
2070 		error = -1;
2071 out:
2072 	if (!error) {
2073 		nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2074 		nqsrv_getl(tdvp, ND_WRITE);
2075 		if (tvp) {
2076 			nqsrv_getl(tvp, ND_WRITE);
2077 		}
2078 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2079 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2080 		VFS_RENAMELOCK_EXIT(localfs);
2081 	} else {
2082 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2083 		if (tdvp == tvp)
2084 			vrele(tdvp);
2085 		else
2086 			vput(tdvp);
2087 		if (tvp)
2088 			vput(tvp);
2089 		VFS_RENAMELOCK_EXIT(localfs);
2090 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2091 		vrele(fromnd.ni_dvp);
2092 		vrele(fvp);
2093 		if (error == -1)
2094 			error = 0;
2095 	}
2096 	if (tond.ni_pathbuf != NULL) {
2097 		pathbuf_destroy(tond.ni_pathbuf);
2098 		tond.ni_pathbuf = NULL;
2099 	}
2100 	tond.ni_cnd.cn_nameiop = 0;
2101 out1:
2102 	if (fdirp) {
2103 		if (v3) {
2104 			vn_lock(fdirp, LK_SHARED | LK_RETRY);
2105 			fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
2106 			VOP_UNLOCK(fdirp);
2107 		}
2108 		vrele(fdirp);
2109 		fdirp = NULL;
2110 	}
2111 	if (tdirp) {
2112 		if (v3) {
2113 			vn_lock(tdirp, LK_SHARED | LK_RETRY);
2114 			tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
2115 			VOP_UNLOCK(tdirp);
2116 		}
2117 		vrele(tdirp);
2118 		tdirp = NULL;
2119 	}
2120 	pathbuf_destroy(fromnd.ni_pathbuf);
2121 	fromnd.ni_pathbuf = NULL;
2122 	fromnd.ni_cnd.cn_nameiop = 0;
2123 	localfs = NULL;
2124 	nfsm_reply(2 * NFSX_WCCDATA(v3));
2125 	if (v3) {
2126 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2127 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2128 	}
2129 	return (0);
2130 
2131 nfsmout:
2132 	if (fdirp)
2133 		vrele(fdirp);
2134 #ifdef notdef
2135 	if (tdirp)
2136 		vrele(tdirp);
2137 #endif
2138 	if (tond.ni_cnd.cn_nameiop) {
2139 		if (tond.ni_pathbuf != NULL) {
2140 			pathbuf_destroy(tond.ni_pathbuf);
2141 			tond.ni_pathbuf = NULL;
2142 		}
2143 	}
2144 	if (localfs) {
2145 		VFS_RENAMELOCK_EXIT(localfs);
2146 	}
2147 	if (fromnd.ni_cnd.cn_nameiop) {
2148 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2149 		if (fromnd.ni_pathbuf != NULL) {
2150 			pathbuf_destroy(fromnd.ni_pathbuf);
2151 			fromnd.ni_pathbuf = NULL;
2152 		}
2153 		vrele(fromnd.ni_dvp);
2154 		vrele(fvp);
2155 	}
2156 	return (error);
2157 }
2158 
2159 /*
2160  * nfs link service
2161  */
2162 int
2163 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2164 {
2165 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2166 	struct mbuf *nam = nfsd->nd_nam;
2167 	char *dpos = nfsd->nd_dpos;
2168 	kauth_cred_t cred = nfsd->nd_cr;
2169 	struct nameidata nd;
2170 	u_int32_t *tl;
2171 	int32_t t1;
2172 	char *bpos;
2173 	int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2174 	int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2175 	char *cp2;
2176 	struct mbuf *mb, *mreq __unused;
2177 	struct vnode *vp, *xp, *dirp = (struct vnode *)0;
2178 	struct vattr dirfor, diraft, at;
2179 	nfsrvfh_t nsfh, dnsfh;
2180 	u_quad_t frev;
2181 
2182 	nfsm_srvmtofh(&nsfh);
2183 	nfsm_srvmtofh(&dnsfh);
2184 	nfsm_srvnamesiz(len);
2185 	error = nfsrv_fhtovp(&nsfh, false, &vp, cred, slp, nam,
2186 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2187 	if (error) {
2188 		nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2189 		nfsm_srvpostop_attr(getret, &at);
2190 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2191 		return (0);
2192 	}
2193 	if (vp->v_type == VDIR) {
2194 		error = EPERM;
2195 		goto out1;
2196 	}
2197 	nd.ni_cnd.cn_cred = cred;
2198 	nd.ni_cnd.cn_nameiop = CREATE;
2199 	nd.ni_cnd.cn_flags = LOCKPARENT;
2200 	error = nfs_namei(&nd, &dnsfh, len, slp, nam, &md, &dpos,
2201 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2202 	if (dirp && v3) {
2203 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2204 	}
2205 	if (error)
2206 		goto out1;
2207 	xp = nd.ni_vp;
2208 	if (xp != NULL) {
2209 		error = EEXIST;
2210 		goto out;
2211 	}
2212 	xp = nd.ni_dvp;
2213 	if (vp->v_mount != xp->v_mount)
2214 		error = EXDEV;
2215 out:
2216 	if (!error) {
2217 		nqsrv_getl(vp, ND_WRITE);
2218 		nqsrv_getl(xp, ND_WRITE);
2219 		error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2220 	} else {
2221 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2222 		if (nd.ni_dvp == nd.ni_vp)
2223 			vrele(nd.ni_dvp);
2224 		else
2225 			vput(nd.ni_dvp);
2226 		if (nd.ni_vp)
2227 			vrele(nd.ni_vp);
2228 	}
2229 out1:
2230 	if (v3) {
2231 		vn_lock(vp, LK_SHARED | LK_RETRY);
2232 		getret = VOP_GETATTR(vp, &at, cred);
2233 		VOP_UNLOCK(vp);
2234 	}
2235 	if (dirp) {
2236 		if (v3) {
2237 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2238 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2239 			VOP_UNLOCK(dirp);
2240 		}
2241 		vrele(dirp);
2242 	}
2243 	vrele(vp);
2244 	if (nd.ni_pathbuf != NULL) {
2245 		pathbuf_destroy(nd.ni_pathbuf);
2246 		nd.ni_pathbuf = NULL;
2247 	}
2248 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2249 	if (v3) {
2250 		nfsm_srvpostop_attr(getret, &at);
2251 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2252 		return (0);
2253 	}
2254 	nfsm_srvdone;
2255 }
2256 
2257 /*
2258  * nfs symbolic link service
2259  */
2260 int
2261 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2262 {
2263 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2264 	struct mbuf *nam = nfsd->nd_nam;
2265 	char *dpos = nfsd->nd_dpos;
2266 	kauth_cred_t cred = nfsd->nd_cr;
2267 	struct vattr va, dirfor, diraft;
2268 	struct nameidata nd;
2269 	u_int32_t *tl;
2270 	int32_t t1;
2271 	struct nfsv2_sattr *sp;
2272 	char *bpos, *pathcp = NULL, *cp2;
2273 	struct uio io;
2274 	struct iovec iv;
2275 	int error = 0, cache = 0, dirfor_ret = 1, diraft_ret = 1, abort = 0;
2276 	uint32_t len, len2;
2277 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2278 	struct mbuf *mb, *mreq __unused;
2279 	struct vnode *dirp = (struct vnode *)0;
2280 	nfsrvfh_t nsfh;
2281 	u_quad_t frev;
2282 
2283 	nd.ni_cnd.cn_nameiop = 0;
2284 	nfsm_srvmtofh(&nsfh);
2285 	nfsm_srvnamesiz(len);
2286 	nd.ni_cnd.cn_cred = cred;
2287 	nd.ni_cnd.cn_nameiop = CREATE;
2288 	nd.ni_cnd.cn_flags = LOCKPARENT;
2289 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2290 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2291 	if (dirp && v3) {
2292 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2293 	}
2294 	if (error)
2295 		goto out;
2296 	abort = 1;
2297 	vattr_null(&va);
2298 	va.va_type = VLNK;
2299 	if (v3) {
2300 		va.va_mode = 0;
2301 		nfsm_srvsattr(&va);
2302 		nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
2303 		len2 = fxdr_unsigned(uint32_t, *tl);
2304 		if (len2 > PATH_MAX) {
2305 			/* XXX should check _PC_NO_TRUNC */
2306 			error = ENAMETOOLONG;
2307 			goto abortop;
2308 		}
2309 	}
2310 	else {
2311 		/* NFSv2 */
2312 		nfsm_strsiz(len2, NFS_MAXPATHLEN);
2313 	}
2314 	pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2315 	iv.iov_base = pathcp;
2316 	iv.iov_len = len2;
2317 	io.uio_resid = len2;
2318 	io.uio_offset = 0;
2319 	io.uio_iov = &iv;
2320 	io.uio_iovcnt = 1;
2321 	io.uio_rw = UIO_READ;
2322 	UIO_SETUP_SYSSPACE(&io);
2323 	nfsm_mtouio(&io, len2);
2324 	if (!v3) {
2325 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2326 		va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2327 	}
2328 	*(pathcp + len2) = '\0';
2329 	if (nd.ni_vp) {
2330 		error = EEXIST;
2331 abortop:
2332 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2333 		if (nd.ni_dvp == nd.ni_vp)
2334 			vrele(nd.ni_dvp);
2335 		else
2336 			vput(nd.ni_dvp);
2337 		if (nd.ni_vp)
2338 			vrele(nd.ni_vp);
2339 		goto out;
2340 	}
2341 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
2342 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
2343 	if (!error) {
2344 	    if (v3) {
2345 		error = nfsrv_composefh(nd.ni_vp, &nsfh, v3);
2346 		if (!error)
2347 		    error = VOP_GETATTR(nd.ni_vp, &va, cred);
2348 		vput(nd.ni_vp);
2349 	    } else {
2350 		vput(nd.ni_vp);
2351 	    }
2352 	}
2353 out:
2354 	if (pathcp)
2355 		free(pathcp, M_TEMP);
2356 	if (dirp) {
2357 		if (v3) {
2358 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2359 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2360 			VOP_UNLOCK(dirp);
2361 		}
2362 		vrele(dirp);
2363 		dirp = NULL;
2364 	}
2365 	if (nd.ni_pathbuf != NULL) {
2366 		pathbuf_destroy(nd.ni_pathbuf);
2367 		nd.ni_pathbuf = NULL;
2368 	}
2369 	abort = 0;
2370 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2371 	    NFSX_WCCDATA(v3));
2372 	if (v3) {
2373 		if (!error) {
2374 			nfsm_srvpostop_fh(&nsfh);
2375 			nfsm_srvpostop_attr(0, &va);
2376 		}
2377 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2378 	}
2379 	return (0);
2380 nfsmout:
2381 	if (abort) {
2382 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2383 		if (nd.ni_dvp == nd.ni_vp)
2384 			vrele(nd.ni_dvp);
2385 		else
2386 			vput(nd.ni_dvp);
2387 		if (nd.ni_vp)
2388 			vrele(nd.ni_vp);
2389 		if (nd.ni_pathbuf != NULL) {
2390 			pathbuf_destroy(nd.ni_pathbuf);
2391 			nd.ni_pathbuf = NULL;
2392 		}
2393 	}
2394 	if (dirp)
2395 		vrele(dirp);
2396 	if (pathcp)
2397 		free(pathcp, M_TEMP);
2398 	return (error);
2399 }
2400 
2401 /*
2402  * nfs mkdir service
2403  */
2404 int
2405 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2406 {
2407 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2408 	struct mbuf *nam = nfsd->nd_nam;
2409 	char *dpos = nfsd->nd_dpos;
2410 	kauth_cred_t cred = nfsd->nd_cr;
2411 	struct vattr va, dirfor, diraft;
2412 	struct nfs_fattr *fp;
2413 	struct nameidata nd;
2414 	char *cp;
2415 	u_int32_t *tl;
2416 	int32_t t1;
2417 	char *bpos;
2418 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2419 	int abort = 0;
2420 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2421 	char *cp2;
2422 	struct mbuf *mb, *mreq __unused;
2423 	struct vnode *vp, *dirp = (struct vnode *)0;
2424 	nfsrvfh_t nsfh;
2425 	u_quad_t frev;
2426 
2427 	nfsm_srvmtofh(&nsfh);
2428 	nfsm_srvnamesiz(len);
2429 	nd.ni_cnd.cn_cred = cred;
2430 	nd.ni_cnd.cn_nameiop = CREATE;
2431 	nd.ni_cnd.cn_flags = LOCKPARENT;
2432 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2433 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2434 	if (dirp && v3) {
2435 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2436 	}
2437 	if (error) {
2438 		if (nd.ni_pathbuf != NULL) {
2439 			pathbuf_destroy(nd.ni_pathbuf);
2440 			nd.ni_pathbuf = NULL;
2441 		}
2442 		nfsm_reply(NFSX_WCCDATA(v3));
2443 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2444 		if (dirp)
2445 			vrele(dirp);
2446 		return (0);
2447 	}
2448 	abort = 1;
2449 	vattr_null(&va);
2450 	if (v3) {
2451 		va.va_mode = 0;
2452 		nfsm_srvsattr(&va);
2453 	} else {
2454 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2455 		va.va_mode = nfstov_mode(*tl++);
2456 	}
2457 	va.va_type = VDIR;
2458 	vp = nd.ni_vp;
2459 	if (vp != NULL) {
2460 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2461 		if (nd.ni_dvp == vp)
2462 			vrele(nd.ni_dvp);
2463 		else
2464 			vput(nd.ni_dvp);
2465 		vrele(vp);
2466 		error = EEXIST;
2467 		goto out;
2468 	}
2469 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
2470 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
2471 	if (!error) {
2472 		vp = nd.ni_vp;
2473 		error = nfsrv_composefh(vp, &nsfh, v3);
2474 		if (!error)
2475 			error = VOP_GETATTR(vp, &va, cred);
2476 		vput(vp);
2477 	}
2478 out:
2479 	if (dirp) {
2480 		if (v3) {
2481 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2482 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2483 			VOP_UNLOCK(dirp);
2484 		}
2485 		vrele(dirp);
2486 		dirp = NULL;
2487 	}
2488 	if (nd.ni_pathbuf != NULL) {
2489 		pathbuf_destroy(nd.ni_pathbuf);
2490 		nd.ni_pathbuf = NULL;
2491 	}
2492 	abort = 0;
2493 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2494 	    NFSX_WCCDATA(v3));
2495 	if (v3) {
2496 		if (!error) {
2497 			nfsm_srvpostop_fh(&nsfh);
2498 			nfsm_srvpostop_attr(0, &va);
2499 		}
2500 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2501 	} else {
2502 		nfsm_srvfhtom(&nsfh, v3);
2503 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2504 		nfsm_srvfillattr(&va, fp);
2505 	}
2506 	return (0);
2507 nfsmout:
2508 	if (abort) {
2509 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2510 		if (nd.ni_dvp == nd.ni_vp)
2511 			vrele(nd.ni_dvp);
2512 		else
2513 			vput(nd.ni_dvp);
2514 		if (nd.ni_vp)
2515 			vrele(nd.ni_vp);
2516 		if (nd.ni_pathbuf != NULL) {
2517 			pathbuf_destroy(nd.ni_pathbuf);
2518 			nd.ni_pathbuf = NULL;
2519 		}
2520 	}
2521 	if (dirp)
2522 		vrele(dirp);
2523 	return (error);
2524 }
2525 
2526 /*
2527  * nfs rmdir service
2528  */
2529 int
2530 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2531 {
2532 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2533 	struct mbuf *nam = nfsd->nd_nam;
2534 	char *dpos = nfsd->nd_dpos;
2535 	kauth_cred_t cred = nfsd->nd_cr;
2536 	u_int32_t *tl;
2537 	int32_t t1;
2538 	char *bpos;
2539 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2540 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2541 	char *cp2;
2542 	struct mbuf *mb, *mreq __unused;
2543 	struct vnode *vp, *dirp = (struct vnode *)0;
2544 	struct vattr dirfor, diraft;
2545 	nfsrvfh_t nsfh;
2546 	struct nameidata nd;
2547 	u_quad_t frev;
2548 
2549 	nfsm_srvmtofh(&nsfh);
2550 	nfsm_srvnamesiz(len);
2551 	nd.ni_cnd.cn_cred = cred;
2552 	nd.ni_cnd.cn_nameiop = DELETE;
2553 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2554 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2555 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2556 	if (dirp && v3) {
2557 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
2558 	}
2559 	if (error) {
2560 		if (nd.ni_pathbuf != NULL) {
2561 			pathbuf_destroy(nd.ni_pathbuf);
2562 			nd.ni_pathbuf = NULL;
2563 		}
2564 		nfsm_reply(NFSX_WCCDATA(v3));
2565 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2566 		if (dirp)
2567 			vrele(dirp);
2568 		return (0);
2569 	}
2570 	vp = nd.ni_vp;
2571 	if (vp->v_type != VDIR) {
2572 		error = ENOTDIR;
2573 		goto out;
2574 	}
2575 	/*
2576 	 * No rmdir "." please.
2577 	 */
2578 	if (nd.ni_dvp == vp) {
2579 		error = EINVAL;
2580 		goto out;
2581 	}
2582 	/*
2583 	 * The root of a mounted filesystem cannot be deleted.
2584 	 */
2585 	if (vp->v_vflag & VV_ROOT)
2586 		error = EBUSY;
2587 out:
2588 	if (!error) {
2589 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
2590 		nqsrv_getl(vp, ND_WRITE);
2591 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2592 	} else {
2593 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2594 		if (nd.ni_dvp == nd.ni_vp)
2595 			vrele(nd.ni_dvp);
2596 		else
2597 			vput(nd.ni_dvp);
2598 		vput(vp);
2599 	}
2600 	if (nd.ni_pathbuf != NULL) {
2601 		pathbuf_destroy(nd.ni_pathbuf);
2602 		nd.ni_pathbuf = NULL;
2603 	}
2604 	if (dirp) {
2605 		if (v3) {
2606 			vn_lock(dirp, LK_SHARED | LK_RETRY);
2607 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2608 			VOP_UNLOCK(dirp);
2609 		}
2610 		vrele(dirp);
2611 	}
2612 	nfsm_reply(NFSX_WCCDATA(v3));
2613 	if (v3) {
2614 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2615 		return (0);
2616 	}
2617 	nfsm_srvdone;
2618 }
2619 
2620 /*
2621  * nfs readdir service
2622  * - mallocs what it thinks is enough to read
2623  *	count rounded up to a multiple of NFS_SRVDIRBLKSIZ <= NFS_MAXREADDIR
2624  * - calls VOP_READDIR()
2625  * - loops around building the reply
2626  *	if the output generated exceeds count break out of loop
2627  *	The nfsm_clget macro is used here so that the reply will be packed
2628  *	tightly in mbuf clusters.
2629  * - it only knows that it has encountered eof when the VOP_READDIR()
2630  *	reads nothing
2631  * - as such one readdir rpc will return eof false although you are there
2632  *	and then the next will return eof
2633  * - it trims out records with d_fileno == 0
2634  *	this doesn't matter for Unix clients, but they might confuse clients
2635  *	for other os'.
2636  * - it trims out records with d_type == DT_WHT
2637  *	these cannot be seen through NFS (unless we extend the protocol)
2638  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2639  *	than requested, but this may not apply to all filesystems. For
2640  *	example, client NFS does not { although it is never remote mounted
2641  *	anyhow }
2642  *     The alternate call nfsrv_readdirplus() does lookups as well.
2643  * PS: The NFS protocol spec. does not clarify what the "count" byte
2644  *	argument is a count of.. just name strings and file id's or the
2645  *	entire reply rpc or ...
2646  *	I tried just file name and id sizes and it confused the Sun client,
2647  *	so I am using the full rpc size now. The "paranoia.." comment refers
2648  *	to including the status longwords that are not a part of the dir.
2649  *	"entry" structures, but are in the rpc.
2650  */
2651 
2652 #define	NFS_SRVDIRBLKSIZ	1024
2653 
2654 struct flrep {
2655 	nfsuint64 fl_off;
2656 	u_int32_t fl_postopok;
2657 	struct nfs_fattr fl_fattr; /* XXX: must be of fattr3 size */
2658 	u_int32_t fl_fhok;
2659 	u_int32_t fl_fhsize;
2660 	/* handle comes here, filled in dynamically */
2661 };
2662 
2663 int
2664 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2665 {
2666 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2667 	struct mbuf *nam = nfsd->nd_nam;
2668 	char *dpos = nfsd->nd_dpos;
2669 	kauth_cred_t cred = nfsd->nd_cr;
2670 	char *bp, *be;
2671 	struct mbuf *mp;
2672 	struct dirent *dp;
2673 	char *cp;
2674 	u_int32_t *tl;
2675 	int32_t t1;
2676 	char *bpos;
2677 	struct mbuf *mb, *mreq __unused, *mp2;
2678 	char *cpos, *cend, *cp2, *rbuf;
2679 	struct vnode *vp;
2680 	struct vattr at;
2681 	nfsrvfh_t nsfh;
2682 	struct uio io;
2683 	struct iovec iv;
2684 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2685 	int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, ncookies;
2686 	int v3 = (nfsd->nd_flag & ND_NFSV3);
2687 	u_quad_t frev, off, toff;
2688 #ifdef NFS3_STRICTVERF
2689 	u_quad_t verf;
2690 #endif
2691 	off_t *cookies = NULL, *cookiep;
2692 	nfsuint64 jar;
2693 
2694 	nfsm_srvmtofh(&nsfh);
2695 	if (v3) {
2696 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2697 		toff = fxdr_hyper(tl);
2698 		tl += 2;
2699 #ifdef NFS3_STRICTVERF
2700 		verf = fxdr_hyper(tl);
2701 #endif
2702 		tl += 2;
2703 	} else {
2704 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2705 		toff = fxdr_unsigned(u_quad_t, *tl++);
2706 	}
2707 	off = toff;
2708 	cnt = fxdr_unsigned(int, *tl);
2709 	siz = ((cnt + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2710 	xfer = NFS_SRVMAXDATA(nfsd);
2711 	if (siz > xfer)
2712 		siz = xfer;
2713 	fullsiz = siz;
2714 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2715 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2716 	if (!error && vp->v_type != VDIR) {
2717 		error = ENOTDIR;
2718 		vput(vp);
2719 	}
2720 	if (error) {
2721 		nfsm_reply(NFSX_UNSIGNED);
2722 		nfsm_srvpostop_attr(getret, &at);
2723 		return (0);
2724 	}
2725 	nqsrv_getl(vp, ND_READ);
2726 	if (v3) {
2727 		error = getret = VOP_GETATTR(vp, &at, cred);
2728 #ifdef NFS3_STRICTVERF
2729 		/*
2730 		 * XXX This check is too strict for Solaris 2.5 clients.
2731 		 */
2732 		if (!error && toff && verf != at.va_filerev)
2733 			error = NFSERR_BAD_COOKIE;
2734 #endif
2735 	}
2736 	if (!error)
2737 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2738 	if (error) {
2739 		vput(vp);
2740 		nfsm_reply(NFSX_POSTOPATTR(v3));
2741 		nfsm_srvpostop_attr(getret, &at);
2742 		return (0);
2743 	}
2744 	VOP_UNLOCK(vp);
2745 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
2746 again:
2747 	iv.iov_base = rbuf;
2748 	iv.iov_len = fullsiz;
2749 	io.uio_iov = &iv;
2750 	io.uio_iovcnt = 1;
2751 	io.uio_offset = (off_t)off;
2752 	io.uio_resid = fullsiz;
2753 	io.uio_rw = UIO_READ;
2754 	UIO_SETUP_SYSSPACE(&io);
2755 	eofflag = 0;
2756 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2757 
2758 	error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2759 
2760 	off = (off_t)io.uio_offset;
2761 	if (!cookies && !error)
2762 		error = NFSERR_PERM;
2763 	if (v3) {
2764 		getret = VOP_GETATTR(vp, &at, cred);
2765 		if (!error)
2766 			error = getret;
2767 	}
2768 
2769 	VOP_UNLOCK(vp);
2770 	if (error) {
2771 		vrele(vp);
2772 		free((void *)rbuf, M_TEMP);
2773 		if (cookies)
2774 			free((void *)cookies, M_TEMP);
2775 		nfsm_reply(NFSX_POSTOPATTR(v3));
2776 		nfsm_srvpostop_attr(getret, &at);
2777 		return (0);
2778 	}
2779 	if (io.uio_resid) {
2780 		siz -= io.uio_resid;
2781 
2782 		/*
2783 		 * If nothing read, return eof
2784 		 * rpc reply
2785 		 */
2786 		if (siz == 0) {
2787 			vrele(vp);
2788 			nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2789 				2 * NFSX_UNSIGNED);
2790 			if (v3) {
2791 				nfsm_srvpostop_attr(getret, &at);
2792 				nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2793 				txdr_hyper(at.va_filerev, tl);
2794 				tl += 2;
2795 			} else
2796 				nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2797 			*tl++ = nfs_false;
2798 			*tl = nfs_true;
2799 			free((void *)rbuf, M_TEMP);
2800 			free((void *)cookies, M_TEMP);
2801 			return (0);
2802 		}
2803 	}
2804 
2805 	/*
2806 	 * Check for degenerate cases of nothing useful read.
2807 	 * If so go try again
2808 	 */
2809 	cpos = rbuf;
2810 	cend = rbuf + siz;
2811 	dp = (struct dirent *)cpos;
2812 	cookiep = cookies;
2813 
2814 	while (cpos < cend && ncookies > 0 &&
2815 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2816 		cpos += dp->d_reclen;
2817 		dp = (struct dirent *)cpos;
2818 		cookiep++;
2819 		ncookies--;
2820 	}
2821 	if (cpos >= cend || ncookies == 0) {
2822 		toff = off;
2823 		siz = fullsiz;
2824 		free(cookies, M_TEMP);
2825 		cookies = NULL;
2826 		goto again;
2827 	}
2828 
2829 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
2830 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2831 	if (v3) {
2832 		nfsm_srvpostop_attr(getret, &at);
2833 		nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2834 		txdr_hyper(at.va_filerev, tl);
2835 	}
2836 	mp = mp2 = mb;
2837 	bp = bpos;
2838 	be = bp + M_TRAILINGSPACE(mp);
2839 
2840 	/* Loop through the records and build reply */
2841 	while (cpos < cend && ncookies > 0) {
2842 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2843 			nlen = dp->d_namlen;
2844 			rem = nfsm_rndup(nlen)-nlen;
2845 			len += (4 * NFSX_UNSIGNED + nlen + rem);
2846 			if (v3)
2847 				len += 2 * NFSX_UNSIGNED;
2848 			if (len > cnt) {
2849 				eofflag = 0;
2850 				break;
2851 			}
2852 			/*
2853 			 * Build the directory record xdr from
2854 			 * the dirent entry.
2855 			 */
2856 			nfsm_clget;
2857 			*tl = nfs_true;
2858 			bp += NFSX_UNSIGNED;
2859 			if (v3) {
2860 				nfsm_clget;
2861 				*tl = txdr_unsigned(dp->d_fileno >> 32);
2862 				bp += NFSX_UNSIGNED;
2863 			}
2864 			nfsm_clget;
2865 			*tl = txdr_unsigned(dp->d_fileno);
2866 			bp += NFSX_UNSIGNED;
2867 			nfsm_clget;
2868 			*tl = txdr_unsigned(nlen);
2869 			bp += NFSX_UNSIGNED;
2870 
2871 			/* And loop around copying the name */
2872 			xfer = nlen;
2873 			cp = dp->d_name;
2874 			while (xfer > 0) {
2875 				nfsm_clget;
2876 				if ((bp+xfer) > be)
2877 					tsiz = be-bp;
2878 				else
2879 					tsiz = xfer;
2880 				memcpy(bp, cp, tsiz);
2881 				bp += tsiz;
2882 				xfer -= tsiz;
2883 				if (xfer > 0)
2884 					cp += tsiz;
2885 			}
2886 			/* And null pad to an int32_t boundary */
2887 			for (i = 0; i < rem; i++)
2888 				*bp++ = '\0';
2889 			nfsm_clget;
2890 
2891 			/* Finish off the record */
2892 			txdr_hyper(*cookiep, &jar);
2893 			if (v3) {
2894 				*tl = jar.nfsuquad[0];
2895 				bp += NFSX_UNSIGNED;
2896 				nfsm_clget;
2897 			}
2898 			*tl = jar.nfsuquad[1];
2899 			bp += NFSX_UNSIGNED;
2900 		}
2901 		cpos += dp->d_reclen;
2902 		dp = (struct dirent *)cpos;
2903 		cookiep++;
2904 		ncookies--;
2905 	}
2906 	vrele(vp);
2907 	nfsm_clget;
2908 	*tl = nfs_false;
2909 	bp += NFSX_UNSIGNED;
2910 	nfsm_clget;
2911 	if (eofflag)
2912 		*tl = nfs_true;
2913 	else
2914 		*tl = nfs_false;
2915 	bp += NFSX_UNSIGNED;
2916 	if (mp != mb) {
2917 		if (bp < be)
2918 			mp->m_len = bp - mtod(mp, char *);
2919 	} else
2920 		mp->m_len += bp - bpos;
2921 	free((void *)rbuf, M_TEMP);
2922 	free((void *)cookies, M_TEMP);
2923 	nfsm_srvdone;
2924 }
2925 
2926 int
2927 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
2928 {
2929 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2930 	struct mbuf *nam = nfsd->nd_nam;
2931 	char *dpos = nfsd->nd_dpos;
2932 	kauth_cred_t cred = nfsd->nd_cr;
2933 	char *bp, *be;
2934 	struct mbuf *mp;
2935 	struct dirent *dp;
2936 	char *cp;
2937 	u_int32_t *tl;
2938 	int32_t t1;
2939 	char *bpos;
2940 	struct mbuf *mb, *mreq __unused, *mp2;
2941 	char *cpos, *cend, *cp2, *rbuf;
2942 	struct vnode *vp, *nvp;
2943 	struct flrep fl;
2944 	nfsrvfh_t nsfh;
2945 	struct uio io;
2946 	struct iovec iv;
2947 	struct vattr va, at, *vap = &va;
2948 	struct nfs_fattr *fp;
2949 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2950 	int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, dirlen, ncookies;
2951 	u_quad_t frev, off, toff;
2952 #ifdef NFS3_STRICTVERF
2953 	u_quad_t verf;
2954 #endif
2955 	off_t *cookies = NULL, *cookiep;
2956 
2957 	nfsm_srvmtofh(&nsfh);
2958 	nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2959 	toff = fxdr_hyper(tl);
2960 	tl += 2;
2961 #ifdef NFS3_STRICTVERF
2962 	verf = fxdr_hyper(tl);
2963 #endif
2964 	tl += 2;
2965 	siz = fxdr_unsigned(int, *tl++);
2966 	cnt = fxdr_unsigned(int, *tl);
2967 	off = toff;
2968 	siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2969 	xfer = NFS_SRVMAXDATA(nfsd);
2970 	if (siz > xfer)
2971 		siz = xfer;
2972 	fullsiz = siz;
2973 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2974 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2975 	if (!error && vp->v_type != VDIR) {
2976 		error = ENOTDIR;
2977 		vput(vp);
2978 	}
2979 	if (error) {
2980 		nfsm_reply(NFSX_UNSIGNED);
2981 		nfsm_srvpostop_attr(getret, &at);
2982 		return (0);
2983 	}
2984 	error = getret = VOP_GETATTR(vp, &at, cred);
2985 #ifdef NFS3_STRICTVERF
2986 	/*
2987 	 * XXX This check is too strict for Solaris 2.5 clients.
2988 	 */
2989 	if (!error && toff && verf != at.va_filerev)
2990 		error = NFSERR_BAD_COOKIE;
2991 #endif
2992 	if (!error) {
2993 		nqsrv_getl(vp, ND_READ);
2994 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2995 	}
2996 	if (error) {
2997 		vput(vp);
2998 		nfsm_reply(NFSX_V3POSTOPATTR);
2999 		nfsm_srvpostop_attr(getret, &at);
3000 		return (0);
3001 	}
3002 	VOP_UNLOCK(vp);
3003 
3004 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
3005 again:
3006 	iv.iov_base = rbuf;
3007 	iv.iov_len = fullsiz;
3008 	io.uio_iov = &iv;
3009 	io.uio_iovcnt = 1;
3010 	io.uio_offset = (off_t)off;
3011 	io.uio_resid = fullsiz;
3012 	io.uio_rw = UIO_READ;
3013 	UIO_SETUP_SYSSPACE(&io);
3014 	eofflag = 0;
3015 
3016 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3017 
3018 	error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
3019 
3020 	off = (u_quad_t)io.uio_offset;
3021 	getret = VOP_GETATTR(vp, &at, cred);
3022 
3023 	VOP_UNLOCK(vp);
3024 
3025 	/*
3026 	 * If the VGET operation doesn't work for this filesystem,
3027 	 * we can't support readdirplus. Returning NOTSUPP should
3028 	 * make clients fall back to plain readdir.
3029 	 * There's no need to check for VPTOFH as well, we wouldn't
3030 	 * even be here otherwise.
3031 	 */
3032 	if (!getret) {
3033 		if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
3034 			getret = (getret == EOPNOTSUPP) ?
3035 				NFSERR_NOTSUPP : NFSERR_IO;
3036 		else
3037 			vput(nvp);
3038 	}
3039 
3040 	if (!cookies && !error)
3041 		error = NFSERR_PERM;
3042 	if (!error)
3043 		error = getret;
3044 	if (error) {
3045 		vrele(vp);
3046 		if (cookies)
3047 			free((void *)cookies, M_TEMP);
3048 		free((void *)rbuf, M_TEMP);
3049 		nfsm_reply(NFSX_V3POSTOPATTR);
3050 		nfsm_srvpostop_attr(getret, &at);
3051 		return (0);
3052 	}
3053 	if (io.uio_resid) {
3054 		siz -= io.uio_resid;
3055 
3056 		/*
3057 		 * If nothing read, return eof
3058 		 * rpc reply
3059 		 */
3060 		if (siz == 0) {
3061 			vrele(vp);
3062 			nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3063 				2 * NFSX_UNSIGNED);
3064 			nfsm_srvpostop_attr(getret, &at);
3065 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3066 			txdr_hyper(at.va_filerev, tl);
3067 			tl += 2;
3068 			*tl++ = nfs_false;
3069 			*tl = nfs_true;
3070 			free((void *)cookies, M_TEMP);
3071 			free((void *)rbuf, M_TEMP);
3072 			return (0);
3073 		}
3074 	}
3075 
3076 	/*
3077 	 * Check for degenerate cases of nothing useful read.
3078 	 * If so go try again
3079 	 */
3080 	cpos = rbuf;
3081 	cend = rbuf + siz;
3082 	dp = (struct dirent *)cpos;
3083 	cookiep = cookies;
3084 
3085 	while (cpos < cend && ncookies > 0 &&
3086 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
3087 		cpos += dp->d_reclen;
3088 		dp = (struct dirent *)cpos;
3089 		cookiep++;
3090 		ncookies--;
3091 	}
3092 	if (cpos >= cend || ncookies == 0) {
3093 		toff = off;
3094 		siz = fullsiz;
3095 		free(cookies, M_TEMP);
3096 		cookies = NULL;
3097 		goto again;
3098 	}
3099 
3100 	dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3101 	nfsm_reply(cnt);
3102 	nfsm_srvpostop_attr(getret, &at);
3103 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3104 	txdr_hyper(at.va_filerev, tl);
3105 	mp = mp2 = mb;
3106 	bp = bpos;
3107 	be = bp + M_TRAILINGSPACE(mp);
3108 
3109 	/* Loop through the records and build reply */
3110 	while (cpos < cend && ncookies > 0) {
3111 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3112 			nfsrvfh_t nnsfh;
3113 
3114 			nlen = dp->d_namlen;
3115 			rem = nfsm_rndup(nlen)-nlen;
3116 
3117 			/*
3118 			 * For readdir_and_lookup get the vnode using
3119 			 * the file number.
3120 			 */
3121 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3122 				goto invalid;
3123 			if (nfsrv_composefh(nvp, &nnsfh, true)) {
3124 				vput(nvp);
3125 				goto invalid;
3126 			}
3127 			if (VOP_GETATTR(nvp, vap, cred)) {
3128 				vput(nvp);
3129 				goto invalid;
3130 			}
3131 			vput(nvp);
3132 
3133 			/*
3134 			 * If either the dircount or maxcount will be
3135 			 * exceeded, get out now. Both of these lengths
3136 			 * are calculated conservatively, including all
3137 			 * XDR overheads.
3138 			 */
3139 			len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3140 				NFSX_V3POSTOPATTR);
3141 			dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3142 			if (len > cnt || dirlen > fullsiz) {
3143 				eofflag = 0;
3144 				break;
3145 			}
3146 
3147 			/*
3148 			 * Build the directory record xdr from
3149 			 * the dirent entry.
3150 			 */
3151 			fp = (struct nfs_fattr *)&fl.fl_fattr;
3152 			nfsm_srvfillattr(vap, fp);
3153 			fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3154 			fl.fl_fhok = nfs_true;
3155 			fl.fl_postopok = nfs_true;
3156 			txdr_hyper(*cookiep, fl.fl_off.nfsuquad);
3157 
3158 			nfsm_clget;
3159 			*tl = nfs_true;
3160 			bp += NFSX_UNSIGNED;
3161 			nfsm_clget;
3162 			*tl = txdr_unsigned(dp->d_fileno >> 32);
3163 			bp += NFSX_UNSIGNED;
3164 			nfsm_clget;
3165 			*tl = txdr_unsigned(dp->d_fileno);
3166 			bp += NFSX_UNSIGNED;
3167 			nfsm_clget;
3168 			*tl = txdr_unsigned(nlen);
3169 			bp += NFSX_UNSIGNED;
3170 
3171 			/* And loop around copying the name */
3172 			xfer = nlen;
3173 			cp = dp->d_name;
3174 			while (xfer > 0) {
3175 				nfsm_clget;
3176 				if ((bp + xfer) > be)
3177 					tsiz = be - bp;
3178 				else
3179 					tsiz = xfer;
3180 				memcpy(bp, cp, tsiz);
3181 				bp += tsiz;
3182 				xfer -= tsiz;
3183 				if (xfer > 0)
3184 					cp += tsiz;
3185 			}
3186 			/* And null pad to an int32_t boundary */
3187 			for (i = 0; i < rem; i++)
3188 				*bp++ = '\0';
3189 
3190 			/*
3191 			 * Now copy the flrep structure out.
3192 			 */
3193 			xfer = sizeof(struct flrep);
3194 			cp = (void *)&fl;
3195 			while (xfer > 0) {
3196 				nfsm_clget;
3197 				if ((bp + xfer) > be)
3198 					tsiz = be - bp;
3199 				else
3200 					tsiz = xfer;
3201 				memcpy(bp, cp, tsiz);
3202 				bp += tsiz;
3203 				xfer -= tsiz;
3204 				if (xfer > 0)
3205 					cp += tsiz;
3206 			}
3207 
3208 			/*
3209 			 * ... and filehandle.
3210 			 */
3211 			xfer = NFSRVFH_SIZE(&nnsfh);
3212 			cp = NFSRVFH_DATA(&nnsfh);
3213 			while (xfer > 0) {
3214 				nfsm_clget;
3215 				if ((bp + xfer) > be)
3216 					tsiz = be - bp;
3217 				else
3218 					tsiz = xfer;
3219 				memcpy(bp, cp, tsiz);
3220 				bp += tsiz;
3221 				xfer -= tsiz;
3222 				if (xfer > 0)
3223 					cp += tsiz;
3224 			}
3225 		}
3226 invalid:
3227 		cpos += dp->d_reclen;
3228 		dp = (struct dirent *)cpos;
3229 		cookiep++;
3230 		ncookies--;
3231 	}
3232 	vrele(vp);
3233 	nfsm_clget;
3234 	*tl = nfs_false;
3235 	bp += NFSX_UNSIGNED;
3236 	nfsm_clget;
3237 	if (eofflag)
3238 		*tl = nfs_true;
3239 	else
3240 		*tl = nfs_false;
3241 	bp += NFSX_UNSIGNED;
3242 	if (mp != mb) {
3243 		if (bp < be)
3244 			mp->m_len = bp - mtod(mp, char *);
3245 	} else
3246 		mp->m_len += bp - bpos;
3247 	free((void *)cookies, M_TEMP);
3248 	free((void *)rbuf, M_TEMP);
3249 	nfsm_srvdone;
3250 }
3251 
3252 /*
3253  * nfs commit service
3254  */
3255 int
3256 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3257 {
3258 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3259 	struct mbuf *nam = nfsd->nd_nam;
3260 	char *dpos = nfsd->nd_dpos;
3261 	kauth_cred_t cred = nfsd->nd_cr;
3262 	struct vattr bfor, aft;
3263 	struct vnode *vp;
3264 	nfsrvfh_t nsfh;
3265 	u_int32_t *tl;
3266 	int32_t t1;
3267 	char *bpos;
3268 	int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache = 0;
3269 	uint32_t cnt;
3270 	char *cp2;
3271 	struct mbuf *mb, *mreq __unused;
3272 	u_quad_t frev, off, end;
3273 
3274 	nfsm_srvmtofh(&nsfh);
3275 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3276 
3277 	off = fxdr_hyper(tl);
3278 	tl += 2;
3279 	cnt = fxdr_unsigned(uint32_t, *tl);
3280 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3281 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3282 	if (error) {
3283 		nfsm_reply(2 * NFSX_UNSIGNED);
3284 		nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3285 		return (0);
3286 	}
3287 	for_ret = VOP_GETATTR(vp, &bfor, cred);
3288 	end = (cnt > 0) ? off + cnt : vp->v_size;
3289 	if (end < off || end > vp->v_size)
3290 		end = vp->v_size;
3291 	if (off < vp->v_size)
3292 		error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end);
3293 	/* else error == 0, from nfsrv_fhtovp() */
3294 	aft_ret = VOP_GETATTR(vp, &aft, cred);
3295 	vput(vp);
3296 	nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3297 	nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3298 	if (!error) {
3299 		nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3300 		*tl++ = txdr_unsigned(boottime.tv_sec);
3301 		*tl = txdr_unsigned(boottime.tv_nsec / 1000);
3302 	} else {
3303 		return (0);
3304 	}
3305 	nfsm_srvdone;
3306 }
3307 
3308 /*
3309  * nfs statfs service
3310  */
3311 int
3312 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3313 {
3314 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3315 	struct mbuf *nam = nfsd->nd_nam;
3316 	char *dpos = nfsd->nd_dpos;
3317 	kauth_cred_t cred = nfsd->nd_cr;
3318 	struct statvfs *sf = NULL;
3319 	struct nfs_statfs *sfp;
3320 	u_int32_t *tl;
3321 	int32_t t1;
3322 	char *bpos;
3323 	int error = 0, rdonly, cache = 0, getret = 1;
3324 	int v3 = (nfsd->nd_flag & ND_NFSV3);
3325 	char *cp2;
3326 	struct mbuf *mb, *mreq __unused;
3327 	struct vnode *vp;
3328 	struct vattr at;
3329 	nfsrvfh_t nsfh;
3330 	u_quad_t frev, tval;
3331 
3332 	nfsm_srvmtofh(&nsfh);
3333 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3334 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3335 	if (error) {
3336 		nfsm_reply(NFSX_UNSIGNED);
3337 		nfsm_srvpostop_attr(getret, &at);
3338 		return (0);
3339 	}
3340 	sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK);
3341 	error = VFS_STATVFS(vp->v_mount, sf);
3342 	getret = VOP_GETATTR(vp, &at, cred);
3343 	vput(vp);
3344 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3345 	if (v3)
3346 		nfsm_srvpostop_attr(getret, &at);
3347 	if (error) {
3348 		free(sf, M_TEMP);
3349 		return (0);
3350 	}
3351 	nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3352 	if (v3) {
3353 		tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize);
3354 		txdr_hyper(tval, &sfp->sf_tbytes);
3355 		tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize);
3356 		txdr_hyper(tval, &sfp->sf_fbytes);
3357 		tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize);
3358 		txdr_hyper(tval, &sfp->sf_abytes);
3359 		tval = (u_quad_t)sf->f_files;
3360 		txdr_hyper(tval, &sfp->sf_tfiles);
3361 		tval = (u_quad_t)sf->f_ffree;
3362 		txdr_hyper(tval, &sfp->sf_ffiles);
3363 		txdr_hyper(tval, &sfp->sf_afiles);
3364 		sfp->sf_invarsec = 0;
3365 	} else {
3366 		sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3367 		sfp->sf_bsize = txdr_unsigned(sf->f_frsize);
3368 		sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3369 		sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3370 		sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3371 	}
3372 nfsmout:
3373 	if (sf)
3374 	    free(sf, M_TEMP);
3375 	return error;
3376 }
3377 
3378 /*
3379  * nfs fsinfo service
3380  */
3381 int
3382 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3383 {
3384 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3385 	struct mbuf *nam = nfsd->nd_nam;
3386 	char *dpos = nfsd->nd_dpos;
3387 	kauth_cred_t cred = nfsd->nd_cr;
3388 	u_int32_t *tl;
3389 	struct nfsv3_fsinfo *sip;
3390 	int32_t t1;
3391 	char *bpos;
3392 	int error = 0, rdonly, cache = 0, getret = 1;
3393 	uint32_t maxdata;
3394 	char *cp2;
3395 	struct mbuf *mb, *mreq __unused;
3396 	struct vnode *vp;
3397 	struct vattr at;
3398 	nfsrvfh_t nsfh;
3399 	u_quad_t frev, maxfsize;
3400 	struct statvfs *sb;
3401 
3402 	nfsm_srvmtofh(&nsfh);
3403 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3404 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3405 	if (error) {
3406 		nfsm_reply(NFSX_UNSIGNED);
3407 		nfsm_srvpostop_attr(getret, &at);
3408 		return (0);
3409 	}
3410 
3411 	/* XXX Try to make a guess on the max file size. */
3412 	sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK);
3413 	VFS_STATVFS(vp->v_mount, sb);
3414 	maxfsize = (u_quad_t)0x80000000 * sb->f_frsize - 1;
3415 	free(sb, M_TEMP);
3416 
3417 	getret = VOP_GETATTR(vp, &at, cred);
3418 	vput(vp);
3419 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3420 	nfsm_srvpostop_attr(getret, &at);
3421 	nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3422 
3423 	/*
3424 	 * XXX
3425 	 * There should be file system VFS OP(s) to get this information.
3426 	 * For now, assume ufs.
3427 	 */
3428 	if (slp->ns_so->so_type == SOCK_DGRAM)
3429 		maxdata = NFS_MAXDGRAMDATA;
3430 	else
3431 		maxdata = NFS_MAXDATA;
3432 	sip->fs_rtmax = txdr_unsigned(maxdata);
3433 	sip->fs_rtpref = txdr_unsigned(maxdata);
3434 	sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3435 	sip->fs_wtmax = txdr_unsigned(maxdata);
3436 	sip->fs_wtpref = txdr_unsigned(maxdata);
3437 	sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3438 	sip->fs_dtpref = txdr_unsigned(maxdata);
3439 	txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3440 	sip->fs_timedelta.nfsv3_sec = 0;
3441 	sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3442 	sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3443 		NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3444 		NFSV3FSINFO_CANSETTIME);
3445 	nfsm_srvdone;
3446 }
3447 
3448 /*
3449  * nfs pathconf service
3450  */
3451 int
3452 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq)
3453 {
3454 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3455 	struct mbuf *nam = nfsd->nd_nam;
3456 	char *dpos = nfsd->nd_dpos;
3457 	kauth_cred_t cred = nfsd->nd_cr;
3458 	u_int32_t *tl;
3459 	struct nfsv3_pathconf *pc;
3460 	int32_t t1;
3461 	char *bpos;
3462 	int error = 0, rdonly, cache = 0, getret = 1;
3463 	register_t linkmax, namemax, chownres, notrunc;
3464 	char *cp2;
3465 	struct mbuf *mb, *mreq __unused;
3466 	struct vnode *vp;
3467 	struct vattr at;
3468 	nfsrvfh_t nsfh;
3469 	u_quad_t frev;
3470 
3471 	nfsm_srvmtofh(&nsfh);
3472 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3473 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3474 	if (error) {
3475 		nfsm_reply(NFSX_UNSIGNED);
3476 		nfsm_srvpostop_attr(getret, &at);
3477 		return (0);
3478 	}
3479 	error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3480 	if (!error)
3481 		error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3482 	if (!error)
3483 		error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3484 	if (!error)
3485 		error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
3486 	getret = VOP_GETATTR(vp, &at, cred);
3487 	vput(vp);
3488 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3489 	nfsm_srvpostop_attr(getret, &at);
3490 	if (error)
3491 		return (0);
3492 	nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3493 
3494 	pc->pc_linkmax = txdr_unsigned(linkmax);
3495 	pc->pc_namemax = txdr_unsigned(namemax);
3496 	pc->pc_notrunc = txdr_unsigned(notrunc);
3497 	pc->pc_chownrestricted = txdr_unsigned(chownres);
3498 
3499 	/*
3500 	 * These should probably be supported by VOP_PATHCONF(), but
3501 	 * until msdosfs is exportable (why would you want to?), the
3502 	 * Unix defaults should be ok.
3503 	 */
3504 	pc->pc_caseinsensitive = nfs_false;
3505 	pc->pc_casepreserving = nfs_true;
3506 	nfsm_srvdone;
3507 }
3508 
3509 /*
3510  * Null operation, used by clients to ping server
3511  */
3512 /* ARGSUSED */
3513 int
3514 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3515     struct lwp *lwp, struct mbuf **mrq)
3516 {
3517 	struct mbuf *mrep = nfsd->nd_mrep;
3518 	char *bpos;
3519 	int error = NFSERR_RETVOID, cache = 0;
3520 	struct mbuf *mb, *mreq __unused;
3521 	u_quad_t frev;
3522 
3523 	nfsm_reply(0);
3524 nfsmout:
3525 	return (0);
3526 }
3527 
3528 /*
3529  * No operation, used for obsolete procedures
3530  */
3531 /* ARGSUSED */
3532 int
3533 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3534     struct lwp *lwp, struct mbuf **mrq)
3535 {
3536 	struct mbuf *mrep = nfsd->nd_mrep;
3537 	char *bpos;
3538 	int error, cache = 0;
3539 	struct mbuf *mb, *mreq __unused;
3540 	u_quad_t frev;
3541 
3542 	if (nfsd->nd_repstat)
3543 		error = nfsd->nd_repstat;
3544 	else
3545 		error = EPROCUNAVAIL;
3546 	nfsm_reply(0);
3547 nfsmout:
3548 	return (0);
3549 }
3550 
3551 /*
3552  * Perform access checking for vnodes obtained from file handles that would
3553  * refer to files already opened by a Unix client. You cannot just use
3554  * vn_writechk() and VOP_ACCESS() for two reasons.
3555  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3556  * 2 - The owner is to be given access irrespective of mode bits for some
3557  *     operations, so that processes that chmod after opening a file don't
3558  *     break. I don't like this because it opens a security hole, but since
3559  *     the nfs server opens a security hole the size of a barn door anyhow,
3560  *     what the heck.
3561  *
3562  * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3563  * will return EPERM instead of EACCES. EPERM is always an error.
3564  */
3565 int
3566 nfsrv_access(struct vnode *vp, int flags, kauth_cred_t cred, int rdonly, struct lwp *lwp, int override)
3567 {
3568 	struct vattr vattr;
3569 	int error;
3570 	if (flags & VWRITE) {
3571 		/* Just vn_writechk() changed to check rdonly */
3572 		/*
3573 		 * Disallow write attempts on read-only file systems;
3574 		 * unless the file is a socket or a block or character
3575 		 * device resident on the file system.
3576 		 */
3577 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3578 			switch (vp->v_type) {
3579 			case VREG:
3580 			case VDIR:
3581 			case VLNK:
3582 				return (EROFS);
3583 			default:
3584 				break;
3585 			}
3586 		}
3587 
3588 		/*
3589 		 * If the vnode is in use as a process's text,
3590 		 * we can't allow writing.
3591 		 */
3592 		if (vp->v_iflag & VI_TEXT)
3593 			return (ETXTBSY);
3594 	}
3595 	error = VOP_GETATTR(vp, &vattr, cred);
3596 	if (error)
3597 		return (error);
3598 	error = VOP_ACCESS(vp, flags, cred);
3599 	/*
3600 	 * Allow certain operations for the owner (reads and writes
3601 	 * on files that are already open).
3602 	 */
3603 	if (override && error == EACCES && kauth_cred_geteuid(cred) == vattr.va_uid)
3604 		error = 0;
3605 	return error;
3606 }
3607