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