xref: /original-bsd/sys/nfs/nfs_serv.c (revision d4efd688)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)nfs_serv.c	7.42 (Berkeley) 12/14/91
11  */
12 
13 /*
14  * nfs version 2 server calls to vnode ops
15  * - these routines generally have 3 phases
16  *   1 - break down and validate rpc request in mbuf list
17  *   2 - do the vnode ops for the request
18  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
19  *   3 - build the rpc reply in an mbuf list
20  *   nb:
21  *	- do not mix the phases, since the nfsm_?? macros can return failures
22  *	  on a bad rpc or similar and do not do any vrele() or vput()'s
23  *
24  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
25  *	error number iff error != 0 whereas
26  *	returning an error from the server function implies a fatal error
27  *	such as a badly constructed rpc request that should be dropped without
28  *	a reply.
29  */
30 
31 #include "param.h"
32 #include "proc.h"
33 #include "file.h"
34 #include "namei.h"
35 #include "vnode.h"
36 #include "mount.h"
37 #include "mbuf.h"
38 
39 #include "ufs/ufs/quota.h"
40 #include "ufs/ufs/inode.h"
41 #include "ufs/ufs/dir.h"
42 
43 #include "nfsv2.h"
44 #include "nfs.h"
45 #include "xdr_subs.h"
46 #include "nfsm_subs.h"
47 
48 /* Defs */
49 #define	TRUE	1
50 #define	FALSE	0
51 
52 /* Global vars */
53 extern u_long nfs_procids[NFS_NPROCS];
54 extern u_long nfs_xdrneg1;
55 extern u_long nfs_false, nfs_true;
56 nfstype nfs_type[9]={ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
57 		      NFCHR, NFNON };
58 
59 /*
60  * nfs getattr service
61  */
62 nfsrv_getattr(mrep, md, dpos, cred, xid, mrq, repstat, p)
63 	struct mbuf **mrq;
64 	struct mbuf *mrep, *md;
65 	caddr_t dpos;
66 	struct ucred *cred;
67 	u_long xid;
68 	int *repstat;
69 	struct proc *p;
70 {
71 	register struct nfsv2_fattr *fp;
72 	struct vattr va;
73 	register struct vattr *vap = &va;
74 	struct vnode *vp;
75 	nfsv2fh_t nfh;
76 	fhandle_t *fhp;
77 	register u_long *tl;
78 	register long t1;
79 	caddr_t bpos;
80 	int error = 0;
81 	char *cp2;
82 	struct mbuf *mb, *mb2, *mreq;
83 
84 	fhp = &nfh.fh_generic;
85 	nfsm_srvmtofh(fhp);
86 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
87 		nfsm_reply(0);
88 	error = VOP_GETATTR(vp, vap, cred, p);
89 	vput(vp);
90 	nfsm_reply(NFSX_FATTR);
91 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
92 	nfsm_srvfillattr;
93 	nfsm_srvdone;
94 }
95 
96 /*
97  * nfs setattr service
98  */
99 nfsrv_setattr(mrep, md, dpos, cred, xid, mrq, repstat, p)
100 	struct mbuf **mrq;
101 	struct mbuf *mrep, *md;
102 	caddr_t dpos;
103 	struct ucred *cred;
104 	u_long xid;
105 	int *repstat;
106 	struct proc *p;
107 {
108 	struct vattr va;
109 	register struct vattr *vap = &va;
110 	register struct nfsv2_sattr *sp;
111 	register struct nfsv2_fattr *fp;
112 	struct vnode *vp;
113 	nfsv2fh_t nfh;
114 	fhandle_t *fhp;
115 	register u_long *tl;
116 	register long t1;
117 	caddr_t bpos;
118 	int error = 0;
119 	char *cp2;
120 	struct mbuf *mb, *mb2, *mreq;
121 
122 	fhp = &nfh.fh_generic;
123 	nfsm_srvmtofh(fhp);
124 	nfsm_disect(sp, struct nfsv2_sattr *, NFSX_SATTR);
125 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
126 		nfsm_reply(0);
127 	if (error = nfsrv_access(vp, VWRITE, cred, p))
128 		goto out;
129 	VATTR_NULL(vap);
130 	/*
131 	 * Nah nah nah nah na nah
132 	 * There is a bug in the Sun client that puts 0xffff in the mode
133 	 * field of sattr when it should put in 0xffffffff. The u_short
134 	 * doesn't sign extend.
135 	 * --> check the low order 2 bytes for 0xffff
136 	 */
137 	if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
138 		vap->va_mode = nfstov_mode(sp->sa_mode);
139 	if (sp->sa_uid != nfs_xdrneg1)
140 		vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
141 	if (sp->sa_gid != nfs_xdrneg1)
142 		vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
143 	if (sp->sa_size != nfs_xdrneg1)
144 		vap->va_size = fxdr_unsigned(u_long, sp->sa_size);
145 	/*
146 	 * The usec field of sa_atime is overloaded with the va_flags field
147 	 * for 4.4BSD clients. Hopefully other clients always set both the
148 	 * sec and usec fields to -1 when not setting the atime.
149 	 */
150 	if (sp->sa_atime.tv_sec != nfs_xdrneg1) {
151 		vap->va_atime.tv_sec = fxdr_unsigned(long, sp->sa_atime.tv_sec);
152 		vap->va_atime.tv_usec = 0;
153 	}
154 	if (sp->sa_atime.tv_usec != nfs_xdrneg1)
155 		vap->va_flags = fxdr_unsigned(u_long, sp->sa_atime.tv_usec);
156 	if (sp->sa_mtime.tv_sec != nfs_xdrneg1)
157 		fxdr_time(&sp->sa_mtime, &vap->va_mtime);
158 	if (error = VOP_SETATTR(vp, vap, cred, p)) {
159 		vput(vp);
160 		nfsm_reply(0);
161 	}
162 	error = VOP_GETATTR(vp, vap, cred, p);
163 out:
164 	vput(vp);
165 	nfsm_reply(NFSX_FATTR);
166 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
167 	nfsm_srvfillattr;
168 	nfsm_srvdone;
169 }
170 
171 /*
172  * nfs lookup rpc
173  */
174 nfsrv_lookup(mrep, md, dpos, cred, xid, mrq, repstat, p)
175 	struct mbuf **mrq;
176 	struct mbuf *mrep, *md;
177 	caddr_t dpos;
178 	struct ucred *cred;
179 	u_long xid;
180 	int *repstat;
181 	struct proc *p;
182 {
183 	register struct nfsv2_fattr *fp;
184 	struct nameidata nd;
185 	struct vnode *vp;
186 	nfsv2fh_t nfh;
187 	fhandle_t *fhp;
188 	register caddr_t cp;
189 	register u_long *tl;
190 	register long t1;
191 	caddr_t bpos;
192 	int error = 0;
193 	char *cp2;
194 	struct mbuf *mb, *mb2, *mreq;
195 	long len;
196 	struct vattr va, *vap = &va;
197 
198 	fhp = &nfh.fh_generic;
199 	nfsm_srvmtofh(fhp);
200 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
201 	nd.ni_cred = cred;
202 	nd.ni_nameiop = LOOKUP | LOCKLEAF;
203 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
204 		nfsm_reply(0);
205 	vp = nd.ni_vp;
206 	bzero((caddr_t)fhp, sizeof(nfh));
207 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
208 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
209 		vput(vp);
210 		nfsm_reply(0);
211 	}
212 	error = VOP_GETATTR(vp, vap, cred, p);
213 	vput(vp);
214 	nfsm_reply(NFSX_FH+NFSX_FATTR);
215 	nfsm_srvfhtom(fhp);
216 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
217 	nfsm_srvfillattr;
218 	nfsm_srvdone;
219 }
220 
221 /*
222  * nfs readlink service
223  */
224 nfsrv_readlink(mrep, md, dpos, cred, xid, mrq, repstat, p)
225 	struct mbuf **mrq;
226 	struct mbuf *mrep, *md;
227 	caddr_t dpos;
228 	struct ucred *cred;
229 	u_long xid;
230 	int *repstat;
231 	struct proc *p;
232 {
233 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
234 	register struct iovec *ivp = iv;
235 	register struct mbuf *mp;
236 	register u_long *tl;
237 	register long t1;
238 	caddr_t bpos;
239 	int error = 0;
240 	char *cp2;
241 	struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
242 	struct vnode *vp;
243 	nfsv2fh_t nfh;
244 	fhandle_t *fhp;
245 	struct uio io, *uiop = &io;
246 	int i, tlen, len;
247 
248 	fhp = &nfh.fh_generic;
249 	nfsm_srvmtofh(fhp);
250 	len = 0;
251 	i = 0;
252 	while (len < NFS_MAXPATHLEN) {
253 		MGET(mp, M_WAIT, MT_DATA);
254 		MCLGET(mp, M_WAIT);
255 		mp->m_len = NFSMSIZ(mp);
256 		if (len == 0)
257 			mp3 = mp2 = mp;
258 		else {
259 			mp2->m_next = mp;
260 			mp2 = mp;
261 		}
262 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
263 			mp->m_len = NFS_MAXPATHLEN-len;
264 			len = NFS_MAXPATHLEN;
265 		} else
266 			len += mp->m_len;
267 		ivp->iov_base = mtod(mp, caddr_t);
268 		ivp->iov_len = mp->m_len;
269 		i++;
270 		ivp++;
271 	}
272 	uiop->uio_iov = iv;
273 	uiop->uio_iovcnt = i;
274 	uiop->uio_offset = 0;
275 	uiop->uio_resid = len;
276 	uiop->uio_rw = UIO_READ;
277 	uiop->uio_segflg = UIO_SYSSPACE;
278 	uiop->uio_procp = (struct proc *)0;
279 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred)) {
280 		m_freem(mp3);
281 		nfsm_reply(0);
282 	}
283 	if (vp->v_type != VLNK) {
284 		error = EINVAL;
285 		goto out;
286 	}
287 	error = VOP_READLINK(vp, uiop, cred);
288 out:
289 	vput(vp);
290 	if (error)
291 		m_freem(mp3);
292 	nfsm_reply(NFSX_UNSIGNED);
293 	if (uiop->uio_resid > 0) {
294 		len -= uiop->uio_resid;
295 		tlen = nfsm_rndup(len);
296 		nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
297 	}
298 	nfsm_build(tl, u_long *, NFSX_UNSIGNED);
299 	*tl = txdr_unsigned(len);
300 	mb->m_next = mp3;
301 	nfsm_srvdone;
302 }
303 
304 /*
305  * nfs read service
306  */
307 nfsrv_read(mrep, md, dpos, cred, xid, mrq, repstat, p)
308 	struct mbuf **mrq;
309 	struct mbuf *mrep, *md;
310 	caddr_t dpos;
311 	struct ucred *cred;
312 	u_long xid;
313 	int *repstat;
314 	struct proc *p;
315 {
316 	register struct iovec *iv;
317 	struct iovec *iv2;
318 	register struct mbuf *m;
319 	register struct nfsv2_fattr *fp;
320 	register u_long *tl;
321 	register long t1;
322 	caddr_t bpos;
323 	int error = 0;
324 	char *cp2;
325 	struct mbuf *mb, *mb2, *mreq;
326 	struct mbuf *m2, *m3;
327 	struct vnode *vp;
328 	nfsv2fh_t nfh;
329 	fhandle_t *fhp;
330 	struct uio io, *uiop = &io;
331 	struct vattr va, *vap = &va;
332 	int i, cnt, len, left, siz, tlen;
333 	off_t off;
334 
335 	fhp = &nfh.fh_generic;
336 	nfsm_srvmtofh(fhp);
337 	nfsm_disect(tl, u_long *, NFSX_UNSIGNED);
338 	off = fxdr_unsigned(off_t, *tl);
339 	nfsm_srvstrsiz(cnt, NFS_MAXDATA);
340 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
341 		nfsm_reply(0);
342 	if (error = nfsrv_access(vp, VREAD | VEXEC, cred, p)) {
343 		vput(vp);
344 		nfsm_reply(0);
345 	}
346 	len = left = cnt;
347 	/*
348 	 * Generate the mbuf list with the uio_iov ref. to it.
349 	 */
350 	i = 0;
351 	m3 = (struct mbuf *)0;
352 #ifdef lint
353 	m2 = (struct mbuf *)0;
354 #endif /* lint */
355 	MALLOC(iv, struct iovec *,
356 	       ((NFS_MAXDATA+MLEN-1)/MLEN) * sizeof (struct iovec), M_TEMP,
357 	       M_WAITOK);
358 	iv2 = iv;
359 	while (left > 0) {
360 		MGET(m, M_WAIT, MT_DATA);
361 		if (left > MINCLSIZE)
362 			MCLGET(m, M_WAIT);
363 		m->m_len = 0;
364 		siz = min(M_TRAILINGSPACE(m), left);
365 		m->m_len = siz;
366 		iv->iov_base = mtod(m, caddr_t);
367 		iv->iov_len = siz;
368 		iv++;
369 		i++;
370 		left -= siz;
371 		if (m3) {
372 			m2->m_next = m;
373 			m2 = m;
374 		} else
375 			m3 = m2 = m;
376 	}
377 	uiop->uio_iov = iv2;
378 	uiop->uio_iovcnt = i;
379 	uiop->uio_offset = off;
380 	uiop->uio_resid = cnt;
381 	uiop->uio_rw = UIO_READ;
382 	uiop->uio_segflg = UIO_SYSSPACE;
383 	uiop->uio_procp = (struct proc *)0;
384 	error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
385 	off = uiop->uio_offset;
386 	FREE((caddr_t)iv2, M_TEMP);
387 	if (error) {
388 		m_freem(m3);
389 		vput(vp);
390 		nfsm_reply(0);
391 	}
392 	if (error = VOP_GETATTR(vp, vap, cred, p))
393 		m_freem(m3);
394 	vput(vp);
395 	nfsm_reply(NFSX_FATTR+NFSX_UNSIGNED);
396 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
397 	nfsm_srvfillattr;
398 	len -= uiop->uio_resid;
399 	if (len > 0) {
400 		tlen = nfsm_rndup(len);
401 		if (cnt != tlen || tlen != len)
402 			nfsm_adj(m3, cnt-tlen, tlen-len);
403 	} else {
404 		m_freem(m3);
405 		m3 = (struct mbuf *)0;
406 	}
407 	nfsm_build(tl, u_long *, NFSX_UNSIGNED);
408 	*tl = txdr_unsigned(len);
409 	mb->m_next = m3;
410 	nfsm_srvdone;
411 }
412 
413 /*
414  * nfs write service
415  */
416 nfsrv_write(mrep, md, dpos, cred, xid, mrq, repstat, p)
417 	struct mbuf *mrep, *md, **mrq;
418 	caddr_t dpos;
419 	struct ucred *cred;
420 	u_long xid;
421 	int *repstat;
422 	struct proc *p;
423 {
424 	register struct iovec *ivp;
425 	register struct mbuf *mp;
426 	register struct nfsv2_fattr *fp;
427 	struct iovec iv[NFS_MAXIOVEC];
428 	struct vattr va;
429 	register struct vattr *vap = &va;
430 	register u_long *tl;
431 	register long t1;
432 	caddr_t bpos;
433 	int error = 0;
434 	char *cp2;
435 	struct mbuf *mb, *mb2, *mreq;
436 	struct vnode *vp;
437 	nfsv2fh_t nfh;
438 	fhandle_t *fhp;
439 	struct uio io, *uiop = &io;
440 	off_t off;
441 	long siz, len, xfer;
442 
443 	fhp = &nfh.fh_generic;
444 	nfsm_srvmtofh(fhp);
445 	nfsm_disect(tl, u_long *, 4*NFSX_UNSIGNED);
446 	off = fxdr_unsigned(off_t, *++tl);
447 	tl += 2;
448 	len = fxdr_unsigned(long, *tl);
449 	if (len > NFS_MAXDATA || len <= 0) {
450 		error = EBADRPC;
451 		nfsm_reply(0);
452 	}
453 	if (dpos == (mtod(md, caddr_t)+md->m_len)) {
454 		mp = md->m_next;
455 		if (mp == NULL) {
456 			error = EBADRPC;
457 			nfsm_reply(0);
458 		}
459 	} else {
460 		mp = md;
461 		siz = dpos-mtod(mp, caddr_t);
462 		mp->m_len -= siz;
463 		NFSMADV(mp, siz);
464 	}
465 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
466 		nfsm_reply(0);
467 	if (error = nfsrv_access(vp, VWRITE, cred, p)) {
468 		vput(vp);
469 		nfsm_reply(0);
470 	}
471 	uiop->uio_resid = 0;
472 	uiop->uio_rw = UIO_WRITE;
473 	uiop->uio_segflg = UIO_SYSSPACE;
474 	uiop->uio_procp = (struct proc *)0;
475 	/*
476 	 * Do up to NFS_MAXIOVEC mbufs of write each iteration of the
477 	 * loop until done.
478 	 */
479 	while (len > 0 && uiop->uio_resid == 0) {
480 		ivp = iv;
481 		siz = 0;
482 		uiop->uio_iov = ivp;
483 		uiop->uio_iovcnt = 0;
484 		uiop->uio_offset = off;
485 		while (len > 0 && uiop->uio_iovcnt < NFS_MAXIOVEC && mp != NULL) {
486 			ivp->iov_base = mtod(mp, caddr_t);
487 			if (len < mp->m_len)
488 				ivp->iov_len = xfer = len;
489 			else
490 				ivp->iov_len = xfer = mp->m_len;
491 #ifdef notdef
492 			/* Not Yet .. */
493 			if (M_HASCL(mp) && (((u_long)ivp->iov_base) & CLOFSET) == 0)
494 				ivp->iov_op = NULL;	/* what should it be ?? */
495 			else
496 				ivp->iov_op = NULL;
497 #endif
498 			uiop->uio_iovcnt++;
499 			ivp++;
500 			len -= xfer;
501 			siz += xfer;
502 			mp = mp->m_next;
503 		}
504 		if (len > 0 && mp == NULL) {
505 			error = EBADRPC;
506 			vput(vp);
507 			nfsm_reply(0);
508 		}
509 		uiop->uio_resid = siz;
510 		if (error = VOP_WRITE(vp, uiop, IO_SYNC | IO_NODELOCKED,
511 			cred)) {
512 			vput(vp);
513 			nfsm_reply(0);
514 		}
515 		off = uiop->uio_offset;
516 	}
517 	error = VOP_GETATTR(vp, vap, cred, p);
518 	vput(vp);
519 	nfsm_reply(NFSX_FATTR);
520 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
521 	nfsm_srvfillattr;
522 	nfsm_srvdone;
523 }
524 
525 /*
526  * nfs create service
527  * now does a truncate to 0 length via. setattr if it already exists
528  */
529 nfsrv_create(mrep, md, dpos, cred, xid, mrq, repstat, p)
530 	struct mbuf *mrep, *md, **mrq;
531 	caddr_t dpos;
532 	struct ucred *cred;
533 	u_long xid;
534 	int *repstat;
535 	struct proc *p;
536 {
537 	register struct nfsv2_fattr *fp;
538 	struct vattr va;
539 	register struct vattr *vap = &va;
540 	struct nameidata nd;
541 	register caddr_t cp;
542 	register u_long *tl;
543 	register long t1;
544 	caddr_t bpos;
545 	long rdev;
546 	int error = 0;
547 	char *cp2;
548 	struct mbuf *mb, *mb2, *mreq;
549 	struct vnode *vp;
550 	nfsv2fh_t nfh;
551 	fhandle_t *fhp;
552 	long len;
553 
554 	nd.ni_nameiop = 0;
555 	fhp = &nfh.fh_generic;
556 	nfsm_srvmtofh(fhp);
557 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
558 	nd.ni_cred = cred;
559 	nd.ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF | SAVESTART;
560 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
561 		nfsm_reply(0);
562 	VATTR_NULL(vap);
563 	nfsm_disect(tl, u_long *, NFSX_SATTR);
564 	/*
565 	 * Iff doesn't exist, create it
566 	 * otherwise just truncate to 0 length
567 	 *   should I set the mode too ??
568 	 */
569 	if (nd.ni_vp == NULL) {
570 		vap->va_type = IFTOVT(fxdr_unsigned(u_long, *tl));
571 		if (vap->va_type == VNON)
572 			vap->va_type = VREG;
573 		vap->va_mode = nfstov_mode(*tl);
574 		rdev = fxdr_unsigned(long, *(tl+3));
575 		if (vap->va_type == VREG || vap->va_type == VSOCK) {
576 			vrele(nd.ni_startdir);
577 			if (error = VOP_CREATE(&nd, vap, p))
578 				nfsm_reply(0);
579 			FREE(nd.ni_pnbuf, M_NAMEI);
580 		} else if (vap->va_type == VCHR || vap->va_type == VBLK ||
581 			vap->va_type == VFIFO) {
582 			if (vap->va_type == VCHR && rdev == 0xffffffff)
583 				vap->va_type = VFIFO;
584 			if (vap->va_type == VFIFO) {
585 #ifndef FIFO
586 				VOP_ABORTOP(&nd);
587 				vput(nd.ni_dvp);
588 				error = ENXIO;
589 				goto out;
590 #endif /* FIFO */
591 			} else if (error = suser(cred, (short *)0)) {
592 				VOP_ABORTOP(&nd);
593 				vput(nd.ni_dvp);
594 				goto out;
595 			} else
596 				vap->va_rdev = (dev_t)rdev;
597 			if (error = VOP_MKNOD(&nd, vap, cred, p)) {
598 				vrele(nd.ni_startdir);
599 				nfsm_reply(0);
600 			}
601 			nd.ni_nameiop &= ~(OPMASK | LOCKPARENT | SAVESTART);
602 			nd.ni_nameiop |= LOOKUP;
603 			if (error = lookup(&nd, p)) {
604 				free(nd.ni_pnbuf, M_NAMEI);
605 				nfsm_reply(0);
606 			}
607 			FREE(nd.ni_pnbuf, M_NAMEI);
608 			if (nd.ni_more) {
609 				vrele(nd.ni_dvp);
610 				vput(nd.ni_vp);
611 				VOP_ABORTOP(&nd);
612 				error = EINVAL;
613 				nfsm_reply(0);
614 			}
615 		} else {
616 			VOP_ABORTOP(&nd);
617 			vput(nd.ni_dvp);
618 			error = ENXIO;
619 			goto out;
620 		}
621 		vp = nd.ni_vp;
622 	} else {
623 		vrele(nd.ni_startdir);
624 		free(nd.ni_pnbuf, M_NAMEI);
625 		vp = nd.ni_vp;
626 		if (nd.ni_dvp == vp)
627 			vrele(nd.ni_dvp);
628 		else
629 			vput(nd.ni_dvp);
630 		VOP_ABORTOP(&nd);
631 		vap->va_size = 0;
632 		if (error = VOP_SETATTR(vp, vap, cred, p)) {
633 			vput(vp);
634 			nfsm_reply(0);
635 		}
636 	}
637 	bzero((caddr_t)fhp, sizeof(nfh));
638 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
639 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
640 		vput(vp);
641 		nfsm_reply(0);
642 	}
643 	error = VOP_GETATTR(vp, vap, cred, p);
644 	vput(vp);
645 	nfsm_reply(NFSX_FH+NFSX_FATTR);
646 	nfsm_srvfhtom(fhp);
647 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
648 	nfsm_srvfillattr;
649 	return (error);
650 nfsmout:
651 	if (nd.ni_nameiop)
652 		vrele(nd.ni_startdir);
653 	VOP_ABORTOP(&nd);
654 	if (nd.ni_dvp == nd.ni_vp)
655 		vrele(nd.ni_dvp);
656 	else
657 		vput(nd.ni_dvp);
658 	if (nd.ni_vp)
659 		vput(nd.ni_vp);
660 	return (error);
661 
662 out:
663 	vrele(nd.ni_startdir);
664 	free(nd.ni_pnbuf, M_NAMEI);
665 	nfsm_reply(0);
666 }
667 
668 /*
669  * nfs remove service
670  */
671 nfsrv_remove(mrep, md, dpos, cred, xid, mrq, repstat, p)
672 	struct mbuf *mrep, *md, **mrq;
673 	caddr_t dpos;
674 	struct ucred *cred;
675 	u_long xid;
676 	int *repstat;
677 	struct proc *p;
678 {
679 	struct nameidata nd;
680 	register u_long *tl;
681 	register long t1;
682 	caddr_t bpos;
683 	int error = 0;
684 	char *cp2;
685 	struct mbuf *mb, *mreq;
686 	struct vnode *vp;
687 	nfsv2fh_t nfh;
688 	fhandle_t *fhp;
689 	long len;
690 
691 	fhp = &nfh.fh_generic;
692 	nfsm_srvmtofh(fhp);
693 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
694 	nd.ni_cred = cred;
695 	nd.ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
696 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
697 		nfsm_reply(0);
698 	vp = nd.ni_vp;
699 	if (vp->v_type == VDIR &&
700 		(error = suser(cred, (short *)0)))
701 		goto out;
702 	/*
703 	 * The root of a mounted filesystem cannot be deleted.
704 	 */
705 	if (vp->v_flag & VROOT) {
706 		error = EBUSY;
707 		goto out;
708 	}
709 	if (vp->v_flag & VTEXT)
710 		(void) vnode_pager_uncache(vp);
711 out:
712 	if (!error) {
713 		error = VOP_REMOVE(&nd, p);
714 	} else {
715 		VOP_ABORTOP(&nd);
716 		if (nd.ni_dvp == vp)
717 			vrele(nd.ni_dvp);
718 		else
719 			vput(nd.ni_dvp);
720 		vput(vp);
721 	}
722 	nfsm_reply(0);
723 	nfsm_srvdone;
724 }
725 
726 /*
727  * nfs rename service
728  */
729 nfsrv_rename(mrep, md, dpos, cred, xid, mrq, repstat, p)
730 	struct mbuf *mrep, *md, **mrq;
731 	caddr_t dpos;
732 	struct ucred *cred;
733 	u_long xid;
734 	int *repstat;
735 	struct proc *p;
736 {
737 	register u_long *tl;
738 	register long t1;
739 	caddr_t bpos;
740 	int error = 0;
741 	char *cp2;
742 	struct mbuf *mb, *mreq;
743 	struct nameidata fromnd, tond;
744 	struct vnode *fvp, *tvp, *tdvp;
745 	nfsv2fh_t fnfh, tnfh;
746 	fhandle_t *ffhp, *tfhp;
747 	long len, len2;
748 	int rootflg = 0;
749 
750 	ffhp = &fnfh.fh_generic;
751 	tfhp = &tnfh.fh_generic;
752 	fromnd.ni_nameiop = 0;
753 	tond.ni_nameiop = 0;
754 	nfsm_srvmtofh(ffhp);
755 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
756 	/*
757 	 * Remember if we are root so that we can reset cr_uid before
758 	 * the second nfs_namei() call
759 	 */
760 	if (cred->cr_uid == 0)
761 		rootflg++;
762 	fromnd.ni_cred = cred;
763 	fromnd.ni_nameiop = DELETE | WANTPARENT | SAVESTART;
764 	if (error = nfs_namei(&fromnd, ffhp, len, &md, &dpos, p))
765 		nfsm_reply(0);
766 	fvp = fromnd.ni_vp;
767 	nfsm_srvmtofh(tfhp);
768 	nfsm_strsiz(len2, NFS_MAXNAMLEN);
769 	if (rootflg)
770 		cred->cr_uid = 0;
771 	tond.ni_cred = cred;
772 	tond.ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE
773 		| SAVESTART;
774 	if (error = nfs_namei(&tond, tfhp, len2, &md, &dpos, p)) {
775 		VOP_ABORTOP(&fromnd);
776 		vrele(fromnd.ni_dvp);
777 		vrele(fvp);
778 		goto out1;
779 	}
780 	tdvp = tond.ni_dvp;
781 	tvp = tond.ni_vp;
782 	if (tvp != NULL) {
783 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
784 			error = EISDIR;
785 			goto out;
786 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
787 			error = ENOTDIR;
788 			goto out;
789 		}
790 	}
791 	if (fvp->v_mount != tdvp->v_mount) {
792 		error = EXDEV;
793 		goto out;
794 	}
795 	if (fvp == tdvp)
796 		error = EINVAL;
797 	/*
798 	 * If source is the same as the destination (that is the
799 	 * same vnode with the same name in the same directory),
800 	 * then there is nothing to do.
801 	 */
802 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
803 	    fromnd.ni_namelen == tond.ni_namelen &&
804 	    !bcmp(fromnd.ni_ptr, tond.ni_ptr, fromnd.ni_namelen))
805 		error = -1;
806 out:
807 	if (!error) {
808 		error = VOP_RENAME(&fromnd, &tond, p);
809 	} else {
810 		VOP_ABORTOP(&tond);
811 		if (tdvp == tvp)
812 			vrele(tdvp);
813 		else
814 			vput(tdvp);
815 		if (tvp)
816 			vput(tvp);
817 		VOP_ABORTOP(&fromnd);
818 		vrele(fromnd.ni_dvp);
819 		vrele(fvp);
820 	}
821 	vrele(tond.ni_startdir);
822 	FREE(tond.ni_pnbuf, M_NAMEI);
823 out1:
824 	vrele(fromnd.ni_startdir);
825 	FREE(fromnd.ni_pnbuf, M_NAMEI);
826 	nfsm_reply(0);
827 	return (error);
828 
829 nfsmout:
830 	if (tond.ni_nameiop) {
831 		vrele(tond.ni_startdir);
832 		FREE(tond.ni_pnbuf, M_NAMEI);
833 	}
834 	if (fromnd.ni_nameiop) {
835 		vrele(fromnd.ni_startdir);
836 		FREE(fromnd.ni_pnbuf, M_NAMEI);
837 		VOP_ABORTOP(&fromnd);
838 		vrele(fromnd.ni_dvp);
839 		vrele(fvp);
840 	}
841 	return (error);
842 }
843 
844 /*
845  * nfs link service
846  */
847 nfsrv_link(mrep, md, dpos, cred, xid, mrq, repstat, p)
848 	struct mbuf *mrep, *md, **mrq;
849 	caddr_t dpos;
850 	struct ucred *cred;
851 	u_long xid;
852 	int *repstat;
853 	struct proc *p;
854 {
855 	struct nameidata nd;
856 	register u_long *tl;
857 	register long t1;
858 	caddr_t bpos;
859 	int error = 0;
860 	char *cp2;
861 	struct mbuf *mb, *mreq;
862 	struct vnode *vp, *xp;
863 	nfsv2fh_t nfh, dnfh;
864 	fhandle_t *fhp, *dfhp;
865 	long len;
866 
867 	fhp = &nfh.fh_generic;
868 	dfhp = &dnfh.fh_generic;
869 	nfsm_srvmtofh(fhp);
870 	nfsm_srvmtofh(dfhp);
871 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
872 	if (error = nfsrv_fhtovp(fhp, FALSE, &vp, cred))
873 		nfsm_reply(0);
874 	if (vp->v_type == VDIR && (error = suser(cred, NULL)))
875 		goto out1;
876 	nd.ni_cred = cred;
877 	nd.ni_nameiop = CREATE | LOCKPARENT;
878 	if (error = nfs_namei(&nd, dfhp, len, &md, &dpos, p))
879 		goto out1;
880 	xp = nd.ni_vp;
881 	if (xp != NULL) {
882 		error = EEXIST;
883 		goto out;
884 	}
885 	xp = nd.ni_dvp;
886 	if (vp->v_mount != xp->v_mount)
887 		error = EXDEV;
888 out:
889 	if (!error) {
890 		error = VOP_LINK(vp, &nd, p);
891 	} else {
892 		VOP_ABORTOP(&nd);
893 		if (nd.ni_dvp == nd.ni_vp)
894 			vrele(nd.ni_dvp);
895 		else
896 			vput(nd.ni_dvp);
897 		if (nd.ni_vp)
898 			vrele(nd.ni_vp);
899 	}
900 out1:
901 	vrele(vp);
902 	nfsm_reply(0);
903 	nfsm_srvdone;
904 }
905 
906 /*
907  * nfs symbolic link service
908  */
909 nfsrv_symlink(mrep, md, dpos, cred, xid, mrq, repstat, p)
910 	struct mbuf *mrep, *md, **mrq;
911 	caddr_t dpos;
912 	struct ucred *cred;
913 	u_long xid;
914 	int *repstat;
915 	struct proc *p;
916 {
917 	struct vattr va;
918 	struct nameidata nd;
919 	register struct vattr *vap = &va;
920 	register u_long *tl;
921 	register long t1;
922 	struct nfsv2_sattr *sp;
923 	caddr_t bpos;
924 	struct uio io;
925 	struct iovec iv;
926 	int error = 0;
927 	char *pathcp, *cp2;
928 	struct mbuf *mb, *mreq;
929 	nfsv2fh_t nfh;
930 	fhandle_t *fhp;
931 	long len, len2;
932 
933 	pathcp = (char *)0;
934 	fhp = &nfh.fh_generic;
935 	nfsm_srvmtofh(fhp);
936 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
937 	nd.ni_cred = cred;
938 	nd.ni_nameiop = CREATE | LOCKPARENT;
939 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
940 		goto out;
941 	nfsm_strsiz(len2, NFS_MAXPATHLEN);
942 	MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
943 	iv.iov_base = pathcp;
944 	iv.iov_len = len2;
945 	io.uio_resid = len2;
946 	io.uio_offset = 0;
947 	io.uio_iov = &iv;
948 	io.uio_iovcnt = 1;
949 	io.uio_segflg = UIO_SYSSPACE;
950 	io.uio_rw = UIO_READ;
951 	io.uio_procp = (struct proc *)0;
952 	nfsm_mtouio(&io, len2);
953 	nfsm_disect(sp, struct nfsv2_sattr *, NFSX_SATTR);
954 	*(pathcp + len2) = '\0';
955 	if (nd.ni_vp) {
956 		VOP_ABORTOP(&nd);
957 		if (nd.ni_dvp == nd.ni_vp)
958 			vrele(nd.ni_dvp);
959 		else
960 			vput(nd.ni_dvp);
961 		vrele(nd.ni_vp);
962 		error = EEXIST;
963 		goto out;
964 	}
965 	VATTR_NULL(vap);
966 	vap->va_mode = fxdr_unsigned(u_short, sp->sa_mode);
967 	error = VOP_SYMLINK(&nd, vap, pathcp, p);
968 out:
969 	if (pathcp)
970 		FREE(pathcp, M_TEMP);
971 	nfsm_reply(0);
972 	return (error);
973 nfsmout:
974 	VOP_ABORTOP(&nd);
975 	if (nd.ni_dvp == nd.ni_vp)
976 		vrele(nd.ni_dvp);
977 	else
978 		vput(nd.ni_dvp);
979 	if (nd.ni_vp)
980 		vrele(nd.ni_vp);
981 	if (pathcp)
982 		FREE(pathcp, M_TEMP);
983 	return (error);
984 }
985 
986 /*
987  * nfs mkdir service
988  */
989 nfsrv_mkdir(mrep, md, dpos, cred, xid, mrq, repstat, p)
990 	struct mbuf *mrep, *md, **mrq;
991 	caddr_t dpos;
992 	struct ucred *cred;
993 	u_long xid;
994 	int *repstat;
995 	struct proc *p;
996 {
997 	struct vattr va;
998 	register struct vattr *vap = &va;
999 	register struct nfsv2_fattr *fp;
1000 	struct nameidata nd;
1001 	register caddr_t cp;
1002 	register u_long *tl;
1003 	register long t1;
1004 	caddr_t bpos;
1005 	int error = 0;
1006 	char *cp2;
1007 	struct mbuf *mb, *mb2, *mreq;
1008 	struct vnode *vp;
1009 	nfsv2fh_t nfh;
1010 	fhandle_t *fhp;
1011 	long len;
1012 
1013 	fhp = &nfh.fh_generic;
1014 	nfsm_srvmtofh(fhp);
1015 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
1016 	nd.ni_cred = cred;
1017 	nd.ni_nameiop = CREATE | LOCKPARENT;
1018 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
1019 		nfsm_reply(0);
1020 	nfsm_disect(tl, u_long *, NFSX_UNSIGNED);
1021 	VATTR_NULL(vap);
1022 	vap->va_type = VDIR;
1023 	vap->va_mode = nfstov_mode(*tl++);
1024 	vp = nd.ni_vp;
1025 	if (vp != NULL) {
1026 		VOP_ABORTOP(&nd);
1027 		if (nd.ni_dvp == vp)
1028 			vrele(nd.ni_dvp);
1029 		else
1030 			vput(nd.ni_dvp);
1031 		vrele(vp);
1032 		error = EEXIST;
1033 		nfsm_reply(0);
1034 	}
1035 	if (error = VOP_MKDIR(&nd, vap, p))
1036 		nfsm_reply(0);
1037 	vp = nd.ni_vp;
1038 	bzero((caddr_t)fhp, sizeof(nfh));
1039 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1040 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
1041 		vput(vp);
1042 		nfsm_reply(0);
1043 	}
1044 	error = VOP_GETATTR(vp, vap, cred, p);
1045 	vput(vp);
1046 	nfsm_reply(NFSX_FH+NFSX_FATTR);
1047 	nfsm_srvfhtom(fhp);
1048 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
1049 	nfsm_srvfillattr;
1050 	return (error);
1051 nfsmout:
1052 	VOP_ABORTOP(&nd);
1053 	if (nd.ni_dvp == nd.ni_vp)
1054 		vrele(nd.ni_dvp);
1055 	else
1056 		vput(nd.ni_dvp);
1057 	if (nd.ni_vp)
1058 		vrele(nd.ni_vp);
1059 	return (error);
1060 }
1061 
1062 /*
1063  * nfs rmdir service
1064  */
1065 nfsrv_rmdir(mrep, md, dpos, cred, xid, mrq, repstat, p)
1066 	struct mbuf *mrep, *md, **mrq;
1067 	caddr_t dpos;
1068 	struct ucred *cred;
1069 	u_long xid;
1070 	int *repstat;
1071 	struct proc *p;
1072 {
1073 	register u_long *tl;
1074 	register long t1;
1075 	caddr_t bpos;
1076 	int error = 0;
1077 	char *cp2;
1078 	struct mbuf *mb, *mreq;
1079 	struct vnode *vp;
1080 	nfsv2fh_t nfh;
1081 	fhandle_t *fhp;
1082 	long len;
1083 	struct nameidata nd;
1084 
1085 	fhp = &nfh.fh_generic;
1086 	nfsm_srvmtofh(fhp);
1087 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
1088 	nd.ni_cred = cred;
1089 	nd.ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
1090 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
1091 		nfsm_reply(0);
1092 	vp = nd.ni_vp;
1093 	if (vp->v_type != VDIR) {
1094 		error = ENOTDIR;
1095 		goto out;
1096 	}
1097 	/*
1098 	 * No rmdir "." please.
1099 	 */
1100 	if (nd.ni_dvp == vp) {
1101 		error = EINVAL;
1102 		goto out;
1103 	}
1104 	/*
1105 	 * The root of a mounted filesystem cannot be deleted.
1106 	 */
1107 	if (vp->v_flag & VROOT)
1108 		error = EBUSY;
1109 out:
1110 	if (!error) {
1111 		error = VOP_RMDIR(&nd, p);
1112 	} else {
1113 		VOP_ABORTOP(&nd);
1114 		if (nd.ni_dvp == nd.ni_vp)
1115 			vrele(nd.ni_dvp);
1116 		else
1117 			vput(nd.ni_dvp);
1118 		vput(vp);
1119 	}
1120 	nfsm_reply(0);
1121 	nfsm_srvdone;
1122 }
1123 
1124 /*
1125  * nfs readdir service
1126  * - mallocs what it thinks is enough to read
1127  *	count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
1128  * - calls VOP_READDIR()
1129  * - loops around building the reply
1130  *	if the output generated exceeds count break out of loop
1131  *	The nfsm_clget macro is used here so that the reply will be packed
1132  *	tightly in mbuf clusters.
1133  * - it only knows that it has encountered eof when the VOP_READDIR()
1134  *	reads nothing
1135  * - as such one readdir rpc will return eof false although you are there
1136  *	and then the next will return eof
1137  * - it trims out records with d_ino == 0
1138  *	this doesn't matter for Unix clients, but they might confuse clients
1139  *	for other os'.
1140  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
1141  *	than requested, but this may not apply to all filesystems. For
1142  *	example, client NFS does not { although it is never remote mounted
1143  *	anyhow }
1144  * PS: The NFS protocol spec. does not clarify what the "count" byte
1145  *	argument is a count of.. just name strings and file id's or the
1146  *	entire reply rpc or ...
1147  *	I tried just file name and id sizes and it confused the Sun client,
1148  *	so I am using the full rpc size now. The "paranoia.." comment refers
1149  *	to including the status longwords that are not a part of the dir.
1150  *	"entry" structures, but are in the rpc.
1151  */
1152 nfsrv_readdir(mrep, md, dpos, cred, xid, mrq, repstat, p)
1153 	struct mbuf **mrq;
1154 	struct mbuf *mrep, *md;
1155 	caddr_t dpos;
1156 	struct ucred *cred;
1157 	u_long xid;
1158 	int *repstat;
1159 	struct proc *p;
1160 {
1161 	register char *bp, *be;
1162 	register struct mbuf *mp;
1163 	register struct direct *dp;
1164 	register caddr_t cp;
1165 	register u_long *tl;
1166 	register long t1;
1167 	caddr_t bpos;
1168 	int error = 0;
1169 	char *cp2;
1170 	struct mbuf *mb, *mb2, *mreq;
1171 	char *cpos, *cend;
1172 	int len, nlen, rem, xfer, tsiz, i;
1173 	struct vnode *vp;
1174 	struct mbuf *mp2, *mp3;
1175 	nfsv2fh_t nfh;
1176 	fhandle_t *fhp;
1177 	struct uio io;
1178 	struct iovec iv;
1179 	int siz, cnt, fullsiz, eofflag;
1180 	u_long on;
1181 	char *rbuf;
1182 	off_t off, toff;
1183 
1184 	fhp = &nfh.fh_generic;
1185 	nfsm_srvmtofh(fhp);
1186 	nfsm_disect(tl, u_long *, 2*NFSX_UNSIGNED);
1187 	toff = fxdr_unsigned(off_t, *tl++);
1188 	off = (toff & ~(NFS_DIRBLKSIZ-1));
1189 	on = (toff & (NFS_DIRBLKSIZ-1));
1190 	cnt = fxdr_unsigned(int, *tl);
1191 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
1192 	if (cnt > NFS_MAXREADDIR)
1193 		siz = NFS_MAXREADDIR;
1194 	fullsiz = siz;
1195 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
1196 		nfsm_reply(0);
1197 	if (error = nfsrv_access(vp, VEXEC, cred, p)) {
1198 		vput(vp);
1199 		nfsm_reply(0);
1200 	}
1201 	VOP_UNLOCK(vp);
1202 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1203 again:
1204 	iv.iov_base = rbuf;
1205 	iv.iov_len = fullsiz;
1206 	io.uio_iov = &iv;
1207 	io.uio_iovcnt = 1;
1208 	io.uio_offset = off;
1209 	io.uio_resid = fullsiz;
1210 	io.uio_segflg = UIO_SYSSPACE;
1211 	io.uio_rw = UIO_READ;
1212 	io.uio_procp = (struct proc *)0;
1213 	error = VOP_READDIR(vp, &io, cred, &eofflag);
1214 	off = io.uio_offset;
1215 	if (error) {
1216 		vrele(vp);
1217 		free((caddr_t)rbuf, M_TEMP);
1218 		nfsm_reply(0);
1219 	}
1220 	if (io.uio_resid) {
1221 		siz -= io.uio_resid;
1222 
1223 		/*
1224 		 * If nothing read, return eof
1225 		 * rpc reply
1226 		 */
1227 		if (siz == 0) {
1228 			vrele(vp);
1229 			nfsm_reply(2*NFSX_UNSIGNED);
1230 			nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
1231 			*tl++ = nfs_false;
1232 			*tl = nfs_true;
1233 			FREE((caddr_t)rbuf, M_TEMP);
1234 			return (0);
1235 		}
1236 	}
1237 
1238 	/*
1239 	 * Check for degenerate cases of nothing useful read.
1240 	 * If so go try again
1241 	 */
1242 	cpos = rbuf + on;
1243 	cend = rbuf + siz;
1244 	dp = (struct direct *)cpos;
1245 	while (cpos < cend && dp->d_ino == 0) {
1246 		cpos += dp->d_reclen;
1247 		dp = (struct direct *)cpos;
1248 	}
1249 	if (cpos >= cend) {
1250 		toff = off;
1251 		siz = fullsiz;
1252 		on = 0;
1253 		goto again;
1254 	}
1255 
1256 	cpos = rbuf + on;
1257 	cend = rbuf + siz;
1258 	dp = (struct direct *)cpos;
1259 	vrele(vp);
1260 	len = 3*NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
1261 	bp = be = (caddr_t)0;
1262 	mp3 = (struct mbuf *)0;
1263 	nfsm_reply(siz);
1264 
1265 	/* Loop through the records and build reply */
1266 	while (cpos < cend) {
1267 		if (dp->d_ino != 0) {
1268 			nlen = dp->d_namlen;
1269 			rem = nfsm_rndup(nlen)-nlen;
1270 
1271 			/*
1272 			 * As noted above, the NFS spec. is not clear about what
1273 			 * should be included in "count" as totalled up here in
1274 			 * "len".
1275 			 */
1276 			len += (4*NFSX_UNSIGNED+nlen+rem);
1277 			if (len > cnt) {
1278 				eofflag = 0;
1279 				break;
1280 			}
1281 
1282 			/* Build the directory record xdr from the direct entry */
1283 			nfsm_clget;
1284 			*tl = nfs_true;
1285 			bp += NFSX_UNSIGNED;
1286 			nfsm_clget;
1287 			*tl = txdr_unsigned(dp->d_ino);
1288 			bp += NFSX_UNSIGNED;
1289 			nfsm_clget;
1290 			*tl = txdr_unsigned(nlen);
1291 			bp += NFSX_UNSIGNED;
1292 
1293 			/* And loop arround copying the name */
1294 			xfer = nlen;
1295 			cp = dp->d_name;
1296 			while (xfer > 0) {
1297 				nfsm_clget;
1298 				if ((bp+xfer) > be)
1299 					tsiz = be-bp;
1300 				else
1301 					tsiz = xfer;
1302 				bcopy(cp, bp, tsiz);
1303 				bp += tsiz;
1304 				xfer -= tsiz;
1305 				if (xfer > 0)
1306 					cp += tsiz;
1307 			}
1308 			/* And null pad to a long boundary */
1309 			for (i = 0; i < rem; i++)
1310 				*bp++ = '\0';
1311 			nfsm_clget;
1312 
1313 			/* Finish off the record */
1314 			toff += dp->d_reclen;
1315 			*tl = txdr_unsigned(toff);
1316 			bp += NFSX_UNSIGNED;
1317 		} else
1318 			toff += dp->d_reclen;
1319 		cpos += dp->d_reclen;
1320 		dp = (struct direct *)cpos;
1321 	}
1322 	nfsm_clget;
1323 	*tl = nfs_false;
1324 	bp += NFSX_UNSIGNED;
1325 	nfsm_clget;
1326 	if (eofflag)
1327 		*tl = nfs_true;
1328 	else
1329 		*tl = nfs_false;
1330 	bp += NFSX_UNSIGNED;
1331 	if (bp < be)
1332 		mp->m_len = bp-mtod(mp, caddr_t);
1333 	mb->m_next = mp3;
1334 	FREE(rbuf, M_TEMP);
1335 	nfsm_srvdone;
1336 }
1337 
1338 /*
1339  * nfs statfs service
1340  */
1341 nfsrv_statfs(mrep, md, dpos, cred, xid, mrq, repstat, p)
1342 	struct mbuf **mrq;
1343 	struct mbuf *mrep, *md;
1344 	caddr_t dpos;
1345 	struct ucred *cred;
1346 	u_long xid;
1347 	int *repstat;
1348 	struct proc *p;
1349 {
1350 	register struct statfs *sf;
1351 	register struct nfsv2_statfs *sfp;
1352 	register u_long *tl;
1353 	register long t1;
1354 	caddr_t bpos;
1355 	int error = 0;
1356 	char *cp2;
1357 	struct mbuf *mb, *mb2, *mreq;
1358 	struct vnode *vp;
1359 	nfsv2fh_t nfh;
1360 	fhandle_t *fhp;
1361 	struct statfs statfs;
1362 
1363 	fhp = &nfh.fh_generic;
1364 	nfsm_srvmtofh(fhp);
1365 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
1366 		nfsm_reply(0);
1367 	sf = &statfs;
1368 	error = VFS_STATFS(vp->v_mount, sf, p);
1369 	vput(vp);
1370 	nfsm_reply(NFSX_STATFS);
1371 	nfsm_build(sfp, struct nfsv2_statfs *, NFSX_STATFS);
1372 	sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
1373 	sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
1374 	sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
1375 	sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
1376 	sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
1377 	nfsm_srvdone;
1378 }
1379 
1380 /*
1381  * Null operation, used by clients to ping server
1382  */
1383 /* ARGSUSED */
1384 nfsrv_null(mrep, md, dpos, cred, xid, mrq, repstat, p)
1385 	struct mbuf **mrq;
1386 	struct mbuf *mrep, *md;
1387 	caddr_t dpos;
1388 	struct ucred *cred;
1389 	u_long xid;
1390 	int *repstat;
1391 	struct proc *p;
1392 {
1393 	caddr_t bpos;
1394 	int error = 0;
1395 	struct mbuf *mb, *mreq;
1396 
1397 	error = VNOVAL;
1398 	nfsm_reply(0);
1399 	return (error);
1400 }
1401 
1402 /*
1403  * No operation, used for obsolete procedures
1404  */
1405 /* ARGSUSED */
1406 nfsrv_noop(mrep, md, dpos, cred, xid, mrq, repstat, p)
1407 	struct mbuf **mrq;
1408 	struct mbuf *mrep, *md;
1409 	caddr_t dpos;
1410 	struct ucred *cred;
1411 	u_long xid;
1412 	int *repstat;
1413 	struct proc *p;
1414 {
1415 	caddr_t bpos;
1416 	int error = 0;
1417 	struct mbuf *mb, *mreq;
1418 
1419 	error = EPROCUNAVAIL;
1420 	nfsm_reply(0);
1421 	return (error);
1422 }
1423 
1424 /*
1425  * Perform access checking for vnodes obtained from file handles that would
1426  * refer to files already opened by a Unix client. You cannot just use
1427  * vn_writechk() and VOP_ACCESS() for two reasons.
1428  * 1 - You must check for MNT_EXRDONLY as well as MNT_RDONLY for the write case
1429  * 2 - The owner is to be given access irrespective of mode bits so that
1430  *     processes that chmod after opening a file don't break. I don't like
1431  *     this because it opens a security hole, but since the nfs server opens
1432  *     a security hole the size of a barn door anyhow, what the heck.
1433  */
1434 nfsrv_access(vp, flags, cred, p)
1435 	register struct vnode *vp;
1436 	int flags;
1437 	register struct ucred *cred;
1438 	struct proc *p;
1439 {
1440 	struct vattr vattr;
1441 	int error;
1442 	if (flags & VWRITE) {
1443 		/* Just vn_writechk() changed to check MNT_EXRDONLY */
1444 		/*
1445 		 * Disallow write attempts on read-only file systems;
1446 		 * unless the file is a socket or a block or character
1447 		 * device resident on the file system.
1448 		 */
1449 		if (vp->v_mount->mnt_flag & (MNT_RDONLY | MNT_EXRDONLY)) {
1450 			switch (vp->v_type) {
1451 			case VREG: case VDIR: case VLNK:
1452 				return (EROFS);
1453 			}
1454 		}
1455 		/*
1456 		 * If there's shared text associated with
1457 		 * the inode, try to free it up once.  If
1458 		 * we fail, we can't allow writing.
1459 		 */
1460 		if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
1461 			return (ETXTBSY);
1462 	}
1463 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
1464 		return (error);
1465 	if ((error = VOP_ACCESS(vp, flags, cred, p)) &&
1466 	    cred->cr_uid != vattr.va_uid)
1467 		return (error);
1468 	return (0);
1469 }
1470