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