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