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