2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
37 * $FreeBSD: src/sys/nfs/nfs_serv.c,v 1.93.2.6 2002/12/29 18:19:53 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_serv.c,v 1.13 2004/03/01 06:33:21 dillon Exp $
42 * nfs version 2 and 3 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request
46 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
47 * 3 - build the rpc reply in an mbuf list
49 * - do not mix the phases, since the nfsm_?? macros can return failures
50 * on a bad rpc or similar and do not do any vrele() or vput()'s
52 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
53 * error number iff error != 0 whereas
54 * returning an error from the server function implies a fatal error
55 * such as a badly constructed rpc request that should be dropped without
57 * For Version 3, nfsm_reply() does not return for the error case, since
58 * most version 3 rpcs return more than the status for error cases.
61 * Warning: always pay careful attention to resource cleanup on return
62 * and note that nfsm_*() macros can terminate a procedure on certain
65 * lookup() and namei()
66 * may return garbage in various structural fields/return elements
67 * if an error is returned, and may garbage up nd.ni_dvp even if no
68 * error is returned and you did not request LOCKPARENT or WANTPARENT.
70 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
71 * buffer has been freed or not.
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/namei.h>
78 #include <sys/unistd.h>
79 #include <sys/vnode.h>
80 #include <sys/mount.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/malloc.h>
85 #include <sys/dirent.h>
87 #include <sys/kernel.h>
88 #include <sys/sysctl.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_zone.h>
94 #include <vm/vm_object.h>
101 #include "xdr_subs.h"
102 #include "nfsm_subs.h"
106 #define nfsdbprintf(info) printf info
108 #define nfsdbprintf(info)
111 #define MAX_COMMIT_COUNT (1024 * 1024)
113 #define NUM_HEURISTIC 64
114 #define NHUSE_INIT 64
116 #define NHUSE_MAX 2048
118 static struct nfsheur {
119 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
120 off_t nh_nextr; /* next offset for sequential detection */
121 int nh_use; /* use count for selection */
122 int nh_seqcount; /* heuristic */
123 } nfsheur[NUM_HEURISTIC];
125 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
128 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
131 extern u_int32_t nfs_xdrneg1;
132 extern u_int32_t nfs_false, nfs_true;
133 extern enum vtype nv3tov_type[8];
134 extern struct nfsstats nfsstats;
136 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
137 int nfsrvw_procrastinate_v3 = 0;
139 static struct timespec nfsver;
141 SYSCTL_DECL(_vfs_nfs);
143 static int nfs_async;
144 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
145 static int nfs_commit_blks;
146 static int nfs_commit_miss;
147 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
148 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
150 static int nfsrv_access (struct vnode *,int,struct ucred *,int,
151 struct thread *, int);
152 static void nfsrvw_coalesce (struct nfsrv_descript *,
153 struct nfsrv_descript *);
156 * Clear nameidata fields that are tested in nsfmout cleanup code prior
157 * to using first nfsm macro (that might jump to the cleanup code).
162 ndclear(struct nameidata *nd)
164 nd->ni_cnd.cn_flags = 0;
167 nd->ni_startdir = NULL;
171 * nfs v3 access service
174 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
175 struct thread *td, struct mbuf **mrq)
177 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
178 struct sockaddr *nam = nfsd->nd_nam;
179 caddr_t dpos = nfsd->nd_dpos;
180 struct ucred *cred = &nfsd->nd_cr;
181 struct vnode *vp = NULL;
187 int error = 0, rdonly, cache, getret;
189 struct mbuf *mb, *mreq, *mb2;
190 struct vattr vattr, *vap = &vattr;
191 u_long testmode, nfsmode;
194 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
198 fhp = &nfh.fh_generic;
200 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
201 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
202 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
204 nfsm_reply(NFSX_UNSIGNED);
205 nfsm_srvpostop_attr(1, (struct vattr *)0);
209 nfsmode = fxdr_unsigned(u_int32_t, *tl);
210 if ((nfsmode & NFSV3ACCESS_READ) &&
211 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
212 nfsmode &= ~NFSV3ACCESS_READ;
213 if (vp->v_type == VDIR)
214 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
217 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
218 if ((nfsmode & testmode) &&
219 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
220 nfsmode &= ~testmode;
221 if (vp->v_type == VDIR)
222 testmode = NFSV3ACCESS_LOOKUP;
224 testmode = NFSV3ACCESS_EXECUTE;
225 if ((nfsmode & testmode) &&
226 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
227 nfsmode &= ~testmode;
228 getret = VOP_GETATTR(vp, vap, td);
231 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
232 nfsm_srvpostop_attr(getret, vap);
233 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
234 *tl = txdr_unsigned(nfsmode);
242 * nfs getattr service
245 nfsrv_getattr(nfsd, slp, td, mrq)
246 struct nfsrv_descript *nfsd;
247 struct nfssvc_sock *slp;
251 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
252 struct sockaddr *nam = nfsd->nd_nam;
253 caddr_t dpos = nfsd->nd_dpos;
254 struct ucred *cred = &nfsd->nd_cr;
255 struct nfs_fattr *fp;
257 struct vattr *vap = &va;
258 struct vnode *vp = NULL;
264 int error = 0, rdonly, cache;
266 struct mbuf *mb, *mb2, *mreq;
269 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
270 fhp = &nfh.fh_generic;
272 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
273 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
279 nqsrv_getl(vp, ND_READ);
280 error = VOP_GETATTR(vp, vap, td);
283 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
288 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
289 nfsm_srvfillattr(vap, fp);
299 * nfs setattr service
302 nfsrv_setattr(nfsd, slp, td, mrq)
303 struct nfsrv_descript *nfsd;
304 struct nfssvc_sock *slp;
308 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
309 struct sockaddr *nam = nfsd->nd_nam;
310 caddr_t dpos = nfsd->nd_dpos;
311 struct ucred *cred = &nfsd->nd_cr;
312 struct vattr va, preat;
313 struct vattr *vap = &va;
314 struct nfsv2_sattr *sp;
315 struct nfs_fattr *fp;
316 struct vnode *vp = NULL;
322 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
323 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
325 struct mbuf *mb, *mb2, *mreq;
327 struct timespec guard;
329 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
330 fhp = &nfh.fh_generic;
335 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
336 gcheck = fxdr_unsigned(int, *tl);
338 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
339 fxdr_nfsv3time(tl, &guard);
342 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
344 * Nah nah nah nah na nah
345 * There is a bug in the Sun client that puts 0xffff in the mode
346 * field of sattr when it should put in 0xffffffff. The u_short
347 * doesn't sign extend.
348 * --> check the low order 2 bytes for 0xffff
350 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
351 vap->va_mode = nfstov_mode(sp->sa_mode);
352 if (sp->sa_uid != nfs_xdrneg1)
353 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
354 if (sp->sa_gid != nfs_xdrneg1)
355 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
356 if (sp->sa_size != nfs_xdrneg1)
357 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
358 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
360 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
362 vap->va_atime.tv_sec =
363 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
364 vap->va_atime.tv_nsec = 0;
367 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
368 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
373 * Now that we have all the fields, lets do it.
375 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
376 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
378 nfsm_reply(2 * NFSX_UNSIGNED);
379 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
385 * vp now an active resource, pay careful attention to cleanup
388 nqsrv_getl(vp, ND_WRITE);
390 error = preat_ret = VOP_GETATTR(vp, &preat, td);
391 if (!error && gcheck &&
392 (preat.va_ctime.tv_sec != guard.tv_sec ||
393 preat.va_ctime.tv_nsec != guard.tv_nsec))
394 error = NFSERR_NOT_SYNC;
398 nfsm_reply(NFSX_WCCDATA(v3));
399 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
406 * If the size is being changed write acces is required, otherwise
407 * just check for a read only file system.
409 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
410 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
415 if (vp->v_type == VDIR) {
418 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
423 error = VOP_SETATTR(vp, vap, cred, td);
424 postat_ret = VOP_GETATTR(vp, vap, td);
430 nfsm_reply(NFSX_WCCORFATTR(v3));
432 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
436 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
437 nfsm_srvfillattr(vap, fp);
451 nfsrv_lookup(nfsd, slp, td, mrq)
452 struct nfsrv_descript *nfsd;
453 struct nfssvc_sock *slp;
457 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
458 struct sockaddr *nam = nfsd->nd_nam;
459 caddr_t dpos = nfsd->nd_dpos;
460 struct ucred *cred = &nfsd->nd_cr;
461 struct nfs_fattr *fp;
462 struct nameidata nd, ind, *ndp = &nd;
463 struct vnode *vp, *dirp = NULL;
470 int error = 0, cache, len, dirattr_ret = 1;
471 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
473 struct mbuf *mb, *mb2, *mreq;
474 struct vattr va, dirattr, *vap = &va;
477 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
480 fhp = &nfh.fh_generic;
482 nfsm_srvnamesiz(len);
484 pubflag = nfs_ispublicfh(fhp);
486 nd.ni_cnd.cn_cred = cred;
487 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
488 nd.ni_cnd.cn_flags = CNP_LOCKLEAF | CNP_SAVESTART;
489 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
490 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
493 * namei failure, only dirp to cleanup. Clear out garbarge from
494 * structure in case macros jump to nfsmout.
500 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
504 nfsm_reply(NFSX_POSTOPATTR(v3));
505 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
511 * Locate index file for public filehandle
513 * error is 0 on entry and 0 on exit from this block.
517 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
519 * Setup call to lookup() to see if we can find
520 * the index file. Arguably, this doesn't belong
521 * in a kernel.. Ugh. If an error occurs, do not
522 * try to install an index file and then clear the
525 * When we replace nd with ind and redirect ndp,
526 * maintenance of ni_startdir and ni_vp shift to
527 * ind and we have to clean them up in the old nd.
528 * However, the cnd resource continues to be maintained
529 * via the original nd. Confused? You aren't alone!
532 VOP_UNLOCK(nd.ni_vp, NULL, 0, td);
533 ind.ni_pathlen = strlen(nfs_pub.np_index);
534 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
536 ind.ni_startdir = nd.ni_vp;
537 VREF(ind.ni_startdir);
539 error = lookup(&ind);
544 * Found an index file. Get rid of
545 * the old references. transfer nd.ni_vp'
551 vrele(nd.ni_startdir);
552 nd.ni_startdir = NULL;
558 * If the public filehandle was used, check that this lookup
559 * didn't result in a filehandle outside the publicly exported
560 * filesystem. We clear the poor vp here to avoid lockups due
564 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
573 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
579 * Resources at this point:
580 * ndp->ni_vp may not be NULL
585 nfsm_reply(NFSX_POSTOPATTR(v3));
586 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
591 nqsrv_getl(ndp->ni_startdir, ND_READ);
594 * Clear out some resources prior to potentially blocking. This
595 * is not as critical as ni_dvp resources in other routines, but
598 vrele(ndp->ni_startdir);
599 ndp->ni_startdir = NULL;
600 NDFREE(&nd, NDF_ONLY_PNBUF);
603 * Get underlying attribute, then release remaining resources ( for
604 * the same potential blocking reason ) and reply.
607 bzero((caddr_t)fhp, sizeof(nfh));
608 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
609 error = VFS_VPTOFH(vp, &fhp->fh_fid);
611 error = VOP_GETATTR(vp, vap, td);
615 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
617 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
621 nfsm_srvfhtom(fhp, v3);
623 nfsm_srvpostop_attr(0, vap);
624 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
626 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
627 nfsm_srvfillattr(vap, fp);
633 NDFREE(&nd, NDF_ONLY_PNBUF);
634 if (ndp->ni_startdir)
635 vrele(ndp->ni_startdir);
642 * nfs readlink service
645 nfsrv_readlink(nfsd, slp, td, mrq)
646 struct nfsrv_descript *nfsd;
647 struct nfssvc_sock *slp;
651 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
652 struct sockaddr *nam = nfsd->nd_nam;
653 caddr_t dpos = nfsd->nd_dpos;
654 struct ucred *cred = &nfsd->nd_cr;
655 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
656 struct iovec *ivp = iv;
661 int error = 0, rdonly, cache, i, tlen, len, getret;
662 int v3 = (nfsd->nd_flag & ND_NFSV3);
664 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
665 struct vnode *vp = NULL;
669 struct uio io, *uiop = &io;
672 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
674 mp2 = (struct mbuf *)0;
677 fhp = &nfh.fh_generic;
681 while (len < NFS_MAXPATHLEN) {
682 MGET(mp, M_WAIT, MT_DATA);
684 mp->m_len = NFSMSIZ(mp);
691 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
692 mp->m_len = NFS_MAXPATHLEN-len;
693 len = NFS_MAXPATHLEN;
696 ivp->iov_base = mtod(mp, caddr_t);
697 ivp->iov_len = mp->m_len;
702 uiop->uio_iovcnt = i;
703 uiop->uio_offset = 0;
704 uiop->uio_resid = len;
705 uiop->uio_rw = UIO_READ;
706 uiop->uio_segflg = UIO_SYSSPACE;
708 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
709 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
711 nfsm_reply(2 * NFSX_UNSIGNED);
712 nfsm_srvpostop_attr(1, (struct vattr *)0);
716 if (vp->v_type != VLNK) {
723 nqsrv_getl(vp, ND_READ);
724 error = VOP_READLINK(vp, uiop, cred);
726 getret = VOP_GETATTR(vp, &attr, td);
729 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
731 nfsm_srvpostop_attr(getret, &attr);
737 if (uiop->uio_resid > 0) {
738 len -= uiop->uio_resid;
739 tlen = nfsm_rndup(len);
740 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
742 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
743 *tl = txdr_unsigned(len);
758 nfsrv_read(nfsd, slp, td, mrq)
759 struct nfsrv_descript *nfsd;
760 struct nfssvc_sock *slp;
764 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
765 struct sockaddr *nam = nfsd->nd_nam;
766 caddr_t dpos = nfsd->nd_dpos;
767 struct ucred *cred = &nfsd->nd_cr;
771 struct nfs_fattr *fp;
776 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
777 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
779 struct mbuf *mb, *mb2, *mreq;
781 struct vnode *vp = NULL;
784 struct uio io, *uiop = &io;
785 struct vattr va, *vap = &va;
791 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
792 fhp = &nfh.fh_generic;
795 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
796 off = fxdr_hyper(tl);
798 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
799 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
801 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
804 * Reference vp. If an error occurs, vp will be invalid, but we
805 * have to NULL it just in case. The macros might goto nfsmout
809 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
810 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
813 nfsm_reply(2 * NFSX_UNSIGNED);
814 nfsm_srvpostop_attr(1, (struct vattr *)0);
819 if (vp->v_type != VREG) {
823 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
826 nqsrv_getl(vp, ND_READ);
827 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
828 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
830 getret = VOP_GETATTR(vp, vap, td);
836 nfsm_reply(NFSX_POSTOPATTR(v3));
837 nfsm_srvpostop_attr(getret, vap);
843 * Calculate byte count to read
846 if (off >= vap->va_size)
848 else if ((off + reqlen) > vap->va_size)
849 cnt = vap->va_size - off;
854 * Calculate seqcount for heuristic
862 * Locate best candidate
865 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
869 if (nfsheur[hi].nh_vp == vp) {
873 if (nfsheur[hi].nh_use > 0)
874 --nfsheur[hi].nh_use;
875 hi = (hi + 1) & (NUM_HEURISTIC - 1);
876 if (nfsheur[hi].nh_use < nh->nh_use)
880 if (nh->nh_vp != vp) {
883 nh->nh_use = NHUSE_INIT;
891 * Calculate heuristic
894 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
895 if (++nh->nh_seqcount > IO_SEQMAX)
896 nh->nh_seqcount = IO_SEQMAX;
897 } else if (nh->nh_seqcount > 1) {
902 nh->nh_use += NHUSE_INC;
903 if (nh->nh_use > NHUSE_MAX)
904 nh->nh_use = NHUSE_MAX;
905 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
908 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
910 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
912 fp = (struct nfs_fattr *)tl;
913 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
915 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
916 fp = (struct nfs_fattr *)tl;
917 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
919 len = left = nfsm_rndup(cnt);
922 * Generate the mbuf list with the uio_iov ref. to it.
927 siz = min(M_TRAILINGSPACE(m), left);
933 MGET(m, M_WAIT, MT_DATA);
940 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
942 uiop->uio_iov = iv2 = iv;
948 panic("nfsrv_read iov");
949 siz = min(M_TRAILINGSPACE(m), left);
951 iv->iov_base = mtod(m, caddr_t) + m->m_len;
960 uiop->uio_iovcnt = i;
961 uiop->uio_offset = off;
962 uiop->uio_resid = len;
963 uiop->uio_rw = UIO_READ;
964 uiop->uio_segflg = UIO_SYSSPACE;
965 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
966 off = uiop->uio_offset;
968 FREE((caddr_t)iv2, M_TEMP);
969 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
975 nfsm_reply(NFSX_POSTOPATTR(v3));
976 nfsm_srvpostop_attr(getret, vap);
985 nfsm_srvfillattr(vap, fp);
986 tlen = len - uiop->uio_resid;
987 cnt = cnt < tlen ? cnt : tlen;
988 tlen = nfsm_rndup(cnt);
989 if (len != tlen || tlen != cnt)
990 nfsm_adj(mb, len - tlen, tlen - cnt);
992 *tl++ = txdr_unsigned(cnt);
998 *tl = txdr_unsigned(cnt);
1009 nfsrv_write(nfsd, slp, td, mrq)
1010 struct nfsrv_descript *nfsd;
1011 struct nfssvc_sock *slp;
1015 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1016 struct sockaddr *nam = nfsd->nd_nam;
1017 caddr_t dpos = nfsd->nd_dpos;
1018 struct ucred *cred = &nfsd->nd_cr;
1022 struct nfs_fattr *fp;
1024 struct vattr va, forat;
1025 struct vattr *vap = &va;
1029 int error = 0, rdonly, cache, len, forat_ret = 1;
1030 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1031 int stable = NFSV3WRITE_FILESYNC;
1032 int v3 = (nfsd->nd_flag & ND_NFSV3);
1034 struct mbuf *mb, *mb2, *mreq;
1035 struct vnode *vp = NULL;
1038 struct uio io, *uiop = &io;
1042 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1048 fhp = &nfh.fh_generic;
1051 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1052 off = fxdr_hyper(tl);
1054 stable = fxdr_unsigned(int, *tl++);
1056 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1057 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1060 stable = NFSV3WRITE_UNSTABLE;
1062 retlen = len = fxdr_unsigned(int32_t, *tl);
1066 * For NFS Version 2, it is not obvious what a write of zero length
1067 * should do, but I might as well be consistent with Version 3,
1068 * which is to return ok so long as there are no permission problems.
1076 adjust = dpos - mtod(mp, caddr_t);
1077 mp->m_len -= adjust;
1078 if (mp->m_len > 0 && adjust > 0)
1079 NFSMADV(mp, adjust);
1083 else if (mp->m_len > 0) {
1086 mp->m_len -= (i - len);
1095 if (len > NFS_MAXDATA || len < 0 || i < len) {
1097 nfsm_reply(2 * NFSX_UNSIGNED);
1098 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1102 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1103 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1106 nfsm_reply(2 * NFSX_UNSIGNED);
1107 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1112 forat_ret = VOP_GETATTR(vp, &forat, td);
1113 if (vp->v_type != VREG) {
1117 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1120 nqsrv_getl(vp, ND_WRITE);
1121 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1126 nfsm_reply(NFSX_WCCDATA(v3));
1127 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1133 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1135 uiop->uio_iov = iv = ivp;
1136 uiop->uio_iovcnt = cnt;
1139 if (mp->m_len > 0) {
1140 ivp->iov_base = mtod(mp, caddr_t);
1141 ivp->iov_len = mp->m_len;
1149 * The IO_METASYNC flag indicates that all metadata (and not just
1150 * enough to ensure data integrity) mus be written to stable storage
1152 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1154 if (stable == NFSV3WRITE_UNSTABLE)
1155 ioflags = IO_NODELOCKED;
1156 else if (stable == NFSV3WRITE_DATASYNC)
1157 ioflags = (IO_SYNC | IO_NODELOCKED);
1159 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1160 uiop->uio_resid = len;
1161 uiop->uio_rw = UIO_WRITE;
1162 uiop->uio_segflg = UIO_SYSSPACE;
1163 uiop->uio_td = NULL;
1164 uiop->uio_offset = off;
1165 error = VOP_WRITE(vp, uiop, ioflags, cred);
1166 nfsstats.srvvop_writes++;
1167 FREE((caddr_t)iv, M_TEMP);
1169 aftat_ret = VOP_GETATTR(vp, vap, td);
1174 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1175 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1177 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1182 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1183 *tl++ = txdr_unsigned(retlen);
1185 * If nfs_async is set, then pretend the write was FILESYNC.
1187 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1188 *tl++ = txdr_unsigned(stable);
1190 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1192 * Actually, there is no need to txdr these fields,
1193 * but it may make the values more human readable,
1194 * for debugging purposes.
1196 if (nfsver.tv_sec == 0)
1198 *tl++ = txdr_unsigned(nfsver.tv_sec);
1199 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1201 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1202 nfsm_srvfillattr(vap, fp);
1211 * NFS write service with write gathering support. Called when
1212 * nfsrvw_procrastinate > 0.
1213 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1214 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1218 nfsrv_writegather(ndp, slp, td, mrq)
1219 struct nfsrv_descript **ndp;
1220 struct nfssvc_sock *slp;
1226 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1227 struct nfs_fattr *fp;
1230 struct nfsrvw_delayhash *wpp;
1232 struct vattr va, forat;
1236 int error = 0, rdonly, cache, len, forat_ret = 1;
1237 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1239 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1240 struct vnode *vp = NULL;
1241 struct uio io, *uiop = &io;
1242 u_quad_t frev, cur_usec;
1244 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1253 mrep = nfsd->nd_mrep;
1255 dpos = nfsd->nd_dpos;
1256 cred = &nfsd->nd_cr;
1257 v3 = (nfsd->nd_flag & ND_NFSV3);
1258 LIST_INIT(&nfsd->nd_coalesce);
1259 nfsd->nd_mreq = NULL;
1260 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1261 cur_usec = nfs_curusec();
1262 nfsd->nd_time = cur_usec +
1263 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1266 * Now, get the write header..
1268 nfsm_srvmtofh(&nfsd->nd_fh);
1270 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1271 nfsd->nd_off = fxdr_hyper(tl);
1273 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1275 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1276 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1279 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1281 len = fxdr_unsigned(int32_t, *tl);
1283 nfsd->nd_eoff = nfsd->nd_off + len;
1286 * Trim the header out of the mbuf list and trim off any trailing
1287 * junk so that the mbuf list has only the write data.
1295 adjust = dpos - mtod(mp, caddr_t);
1296 mp->m_len -= adjust;
1297 if (mp->m_len > 0 && adjust > 0)
1298 NFSMADV(mp, adjust);
1305 mp->m_len -= (i - len);
1311 if (len > NFS_MAXDATA || len < 0 || i < len) {
1315 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1317 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1318 nfsd->nd_mreq = mreq;
1319 nfsd->nd_mrep = NULL;
1324 * Add this entry to the hash and time queues.
1328 wp = slp->ns_tq.lh_first;
1329 while (wp && wp->nd_time < nfsd->nd_time) {
1331 wp = wp->nd_tq.le_next;
1333 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1335 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1337 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1339 if (nfsd->nd_mrep) {
1340 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1344 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1346 wp = wp->nd_hash.le_next;
1348 while (wp && wp->nd_off < nfsd->nd_off &&
1349 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1351 wp = wp->nd_hash.le_next;
1354 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1357 * Search the hash list for overlapping entries and
1360 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1361 wp = nfsd->nd_hash.le_next;
1362 if (NFSW_SAMECRED(owp, nfsd))
1363 nfsrvw_coalesce(owp, nfsd);
1366 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1373 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1374 * and generate the associated reply mbuf list(s).
1377 cur_usec = nfs_curusec();
1379 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1380 owp = nfsd->nd_tq.le_next;
1381 if (nfsd->nd_time > cur_usec)
1385 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1386 LIST_REMOVE(nfsd, nd_tq);
1387 LIST_REMOVE(nfsd, nd_hash);
1389 mrep = nfsd->nd_mrep;
1390 nfsd->nd_mrep = NULL;
1391 cred = &nfsd->nd_cr;
1392 v3 = (nfsd->nd_flag & ND_NFSV3);
1393 forat_ret = aftat_ret = 1;
1394 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1395 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1398 forat_ret = VOP_GETATTR(vp, &forat, td);
1399 if (vp->v_type != VREG) {
1403 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1409 nqsrv_getl(vp, ND_WRITE);
1410 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1413 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1414 ioflags = IO_NODELOCKED;
1415 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1416 ioflags = (IO_SYNC | IO_NODELOCKED);
1418 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1419 uiop->uio_rw = UIO_WRITE;
1420 uiop->uio_segflg = UIO_SYSSPACE;
1421 uiop->uio_td = NULL;
1422 uiop->uio_offset = nfsd->nd_off;
1423 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1424 if (uiop->uio_resid > 0) {
1432 uiop->uio_iovcnt = i;
1433 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1435 uiop->uio_iov = ivp = iov;
1438 if (mp->m_len > 0) {
1439 ivp->iov_base = mtod(mp, caddr_t);
1440 ivp->iov_len = mp->m_len;
1446 error = VOP_WRITE(vp, uiop, ioflags, cred);
1447 nfsstats.srvvop_writes++;
1449 FREE((caddr_t)iov, M_TEMP);
1453 aftat_ret = VOP_GETATTR(vp, &va, td);
1459 * Loop around generating replies for all write rpcs that have
1460 * now been completed.
1464 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1466 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1468 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1471 nfsm_writereply(NFSX_PREOPATTR(v3) +
1472 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1473 NFSX_WRITEVERF(v3), v3);
1475 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1476 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1477 *tl++ = txdr_unsigned(nfsd->nd_len);
1478 *tl++ = txdr_unsigned(swp->nd_stable);
1480 * Actually, there is no need to txdr these fields,
1481 * but it may make the values more human readable,
1482 * for debugging purposes.
1484 if (nfsver.tv_sec == 0)
1486 *tl++ = txdr_unsigned(nfsver.tv_sec);
1487 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1489 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1490 nfsm_srvfillattr(&va, fp);
1493 nfsd->nd_mreq = mreq;
1495 panic("nfsrv_write: nd_mrep not free");
1498 * Done. Put it at the head of the timer queue so that
1499 * the final phase can return the reply.
1504 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1506 nfsd = swp->nd_coalesce.lh_first;
1508 LIST_REMOVE(nfsd, nd_tq);
1514 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1521 * Search for a reply to return.
1524 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1525 if (nfsd->nd_mreq) {
1526 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1527 LIST_REMOVE(nfsd, nd_tq);
1528 *mrq = nfsd->nd_mreq;
1537 * Coalesce the write request nfsd into owp. To do this we must:
1538 * - remove nfsd from the queues
1539 * - merge nfsd->nd_mrep into owp->nd_mrep
1540 * - update the nd_eoff and nd_stable for owp
1541 * - put nfsd on owp's nd_coalesce list
1542 * NB: Must be called at splsoftclock().
1545 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1549 struct nfsrv_descript *p;
1551 NFS_DPF(WG, ("C%03x-%03x",
1552 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1553 LIST_REMOVE(nfsd, nd_hash);
1554 LIST_REMOVE(nfsd, nd_tq);
1555 if (owp->nd_eoff < nfsd->nd_eoff) {
1556 overlap = owp->nd_eoff - nfsd->nd_off;
1558 panic("nfsrv_coalesce: bad off");
1560 m_adj(nfsd->nd_mrep, overlap);
1564 mp->m_next = nfsd->nd_mrep;
1565 owp->nd_eoff = nfsd->nd_eoff;
1567 m_freem(nfsd->nd_mrep);
1568 nfsd->nd_mrep = NULL;
1569 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1570 owp->nd_stable = NFSV3WRITE_FILESYNC;
1571 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1572 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1573 owp->nd_stable = NFSV3WRITE_DATASYNC;
1574 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1577 * If nfsd had anything else coalesced into it, transfer them
1578 * to owp, otherwise their replies will never get sent.
1580 for (p = nfsd->nd_coalesce.lh_first; p;
1581 p = nfsd->nd_coalesce.lh_first) {
1582 LIST_REMOVE(p, nd_tq);
1583 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1588 * nfs create service
1589 * now does a truncate to 0 length via. setattr if it already exists
1592 nfsrv_create(nfsd, slp, td, mrq)
1593 struct nfsrv_descript *nfsd;
1594 struct nfssvc_sock *slp;
1598 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1599 struct sockaddr *nam = nfsd->nd_nam;
1600 caddr_t dpos = nfsd->nd_dpos;
1601 struct ucred *cred = &nfsd->nd_cr;
1602 struct nfs_fattr *fp;
1603 struct vattr va, dirfor, diraft;
1604 struct vattr *vap = &va;
1605 struct nfsv2_sattr *sp;
1607 struct nameidata nd;
1610 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1611 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1614 struct mbuf *mb, *mb2, *mreq;
1615 struct vnode *dirp = (struct vnode *)0;
1618 u_quad_t frev, tempsize;
1619 u_char cverf[NFSX_V3CREATEVERF];
1621 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1627 fhp = &nfh.fh_generic;
1629 nfsm_srvnamesiz(len);
1631 nd.ni_cnd.cn_cred = cred;
1632 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
1633 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_SAVESTART;
1636 * Call namei and do initial cleanup to get a few things
1637 * out of the way. If we get an initial error we cleanup
1638 * and return here to avoid special-casing the invalid nd
1639 * structure through the rest of the case. dirp may be
1640 * set even if an error occurs, but the nd structure will not
1641 * be valid at all if an error occurs so we have to invalidate it
1642 * prior to calling nfsm_reply ( which might goto nfsmout ).
1644 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1645 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1648 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1655 nfsm_reply(NFSX_WCCDATA(v3));
1656 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1662 * No error. Continue. State:
1664 * startdir is valid ( we release this immediately )
1666 * nd.ni_vp may be valid
1667 * nd.ni_dvp is valid
1669 * The error state is set through the code and we may also do some
1670 * opportunistic releasing of vnodes to avoid holding locks through
1671 * NFS I/O. The cleanup at the end is a catch-all
1676 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1677 how = fxdr_unsigned(int, *tl);
1679 case NFSV3CREATE_GUARDED:
1685 case NFSV3CREATE_UNCHECKED:
1688 case NFSV3CREATE_EXCLUSIVE:
1689 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1690 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1694 vap->va_type = VREG;
1696 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1697 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1698 if (vap->va_type == VNON)
1699 vap->va_type = VREG;
1700 vap->va_mode = nfstov_mode(sp->sa_mode);
1701 switch (vap->va_type) {
1703 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1705 vap->va_size = (u_quad_t)tsize;
1710 rdev = fxdr_unsigned(long, sp->sa_size);
1718 * Iff doesn't exist, create it
1719 * otherwise just truncate to 0 length
1720 * should I set the mode too ?
1722 * The only possible error we can have at this point is EEXIST.
1723 * nd.ni_vp will also be non-NULL in that case.
1725 if (nd.ni_vp == NULL) {
1726 if (vap->va_mode == (mode_t)VNOVAL)
1728 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1729 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1730 error = VOP_CREATE(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1733 NDFREE(&nd, NDF_ONLY_PNBUF);
1735 nfsrv_object_create(nd.ni_vp);
1736 if (exclusive_flag) {
1739 bcopy(cverf, (caddr_t)&vap->va_atime,
1741 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1746 vap->va_type == VCHR ||
1747 vap->va_type == VBLK ||
1748 vap->va_type == VFIFO
1751 * Handle SysV FIFO node special cases. All other
1752 * devices require super user to access.
1754 if (vap->va_type == VCHR && rdev == 0xffffffff)
1755 vap->va_type = VFIFO;
1756 if (vap->va_type != VFIFO &&
1757 (error = suser_cred(cred, 0))) {
1760 vap->va_rdev = rdev;
1761 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1763 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1766 NDFREE(&nd, NDF_ONLY_PNBUF);
1773 * release dvp prior to lookup
1781 * Even though LOCKPARENT was cleared, ni_dvp may
1784 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1785 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1786 nd.ni_cnd.cn_td = td;
1787 nd.ni_cnd.cn_cred = cred;
1789 error = lookup(&nd);
1794 /* fall through on certain errors */
1796 nfsrv_object_create(nd.ni_vp);
1797 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1805 if (vap->va_size != -1) {
1806 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1807 (nd.ni_cnd.cn_flags & CNP_RDONLY), td, 0);
1809 nqsrv_getl(nd.ni_vp, ND_WRITE);
1810 tempsize = vap->va_size;
1812 vap->va_size = tempsize;
1813 error = VOP_SETATTR(nd.ni_vp, vap, cred, td);
1819 bzero((caddr_t)fhp, sizeof(nfh));
1820 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1821 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1823 error = VOP_GETATTR(nd.ni_vp, vap, td);
1826 if (exclusive_flag && !error &&
1827 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1829 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1833 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1836 nfsm_srvpostop_fh(fhp);
1837 nfsm_srvpostop_attr(0, vap);
1839 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1842 nfsm_srvfhtom(fhp, v3);
1843 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1844 nfsm_srvfillattr(vap, fp);
1854 if (nd.ni_startdir) {
1855 vrele(nd.ni_startdir);
1856 nd.ni_startdir = NULL;
1860 NDFREE(&nd, NDF_ONLY_PNBUF);
1862 if (nd.ni_dvp == nd.ni_vp)
1873 * nfs v3 mknod service
1876 nfsrv_mknod(nfsd, slp, td, mrq)
1877 struct nfsrv_descript *nfsd;
1878 struct nfssvc_sock *slp;
1882 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1883 struct sockaddr *nam = nfsd->nd_nam;
1884 caddr_t dpos = nfsd->nd_dpos;
1885 struct ucred *cred = &nfsd->nd_cr;
1886 struct vattr va, dirfor, diraft;
1887 struct vattr *vap = &va;
1889 struct nameidata nd;
1892 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1893 u_int32_t major, minor;
1896 struct mbuf *mb, *mb2, *mreq;
1897 struct vnode *vp, *dirp = (struct vnode *)0;
1902 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1905 fhp = &nfh.fh_generic;
1907 nfsm_srvnamesiz(len);
1909 nd.ni_cnd.cn_cred = cred;
1910 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
1911 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_SAVESTART;
1914 * Handle nfs_namei() call. If an error occurs, the nd structure
1915 * is not valid. However, nfsm_*() routines may still jump to
1919 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1920 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1922 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1924 nfsm_reply(NFSX_WCCDATA(1));
1925 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1929 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1930 vtyp = nfsv3tov_type(*tl);
1931 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1932 error = NFSERR_BADTYPE;
1937 if (vtyp == VCHR || vtyp == VBLK) {
1938 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1939 major = fxdr_unsigned(u_int32_t, *tl++);
1940 minor = fxdr_unsigned(u_int32_t, *tl);
1941 vap->va_rdev = makeudev(major, minor);
1945 * Iff doesn't exist, create it.
1951 vap->va_type = vtyp;
1952 if (vap->va_mode == (mode_t)VNOVAL)
1954 if (vtyp == VSOCK) {
1955 vrele(nd.ni_startdir);
1956 nd.ni_startdir = NULL;
1957 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1958 error = VOP_CREATE(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1961 NDFREE(&nd, NDF_ONLY_PNBUF);
1963 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1965 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1967 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1970 NDFREE(&nd, NDF_ONLY_PNBUF);
1977 * Release dvp prior to lookup
1982 KKASSERT(td->td_proc);
1983 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1984 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1985 nd.ni_cnd.cn_td = td;
1986 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1988 error = lookup(&nd);
1993 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1998 * send response, cleanup, return.
2001 if (nd.ni_startdir) {
2002 vrele(nd.ni_startdir);
2003 nd.ni_startdir = NULL;
2005 NDFREE(&nd, NDF_ONLY_PNBUF);
2007 if (nd.ni_dvp == nd.ni_vp)
2015 bzero((caddr_t)fhp, sizeof(nfh));
2016 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2017 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2019 error = VOP_GETATTR(vp, vap, td);
2026 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2031 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2033 nfsm_srvpostop_fh(fhp);
2034 nfsm_srvpostop_attr(0, vap);
2036 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2042 vrele(nd.ni_startdir);
2043 NDFREE(&nd, NDF_ONLY_PNBUF);
2045 if (nd.ni_dvp == nd.ni_vp)
2056 * nfs remove service
2059 nfsrv_remove(nfsd, slp, td, mrq)
2060 struct nfsrv_descript *nfsd;
2061 struct nfssvc_sock *slp;
2065 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2066 struct sockaddr *nam = nfsd->nd_nam;
2067 caddr_t dpos = nfsd->nd_dpos;
2068 struct ucred *cred = &nfsd->nd_cr;
2069 struct nameidata nd;
2073 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2074 int v3 = (nfsd->nd_flag & ND_NFSV3);
2076 struct mbuf *mb, *mreq;
2078 struct vattr dirfor, diraft;
2083 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2086 fhp = &nfh.fh_generic;
2088 nfsm_srvnamesiz(len);
2090 nd.ni_cnd.cn_cred = cred;
2091 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2092 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2093 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2094 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2097 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2104 if (nd.ni_vp->v_type == VDIR) {
2105 error = EPERM; /* POSIX */
2109 * The root of a mounted filesystem cannot be deleted.
2111 if (nd.ni_vp->v_flag & VROOT) {
2117 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2118 nqsrv_getl(nd.ni_vp, ND_WRITE);
2119 error = VOP_REMOVE(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2120 NDFREE(&nd, NDF_ONLY_PNBUF);
2124 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2128 nfsm_reply(NFSX_WCCDATA(v3));
2130 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2134 NDFREE(&nd, NDF_ONLY_PNBUF);
2136 if (nd.ni_dvp == nd.ni_vp)
2147 * nfs rename service
2150 nfsrv_rename(nfsd, slp, td, mrq)
2151 struct nfsrv_descript *nfsd;
2152 struct nfssvc_sock *slp;
2156 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2157 struct sockaddr *nam = nfsd->nd_nam;
2158 caddr_t dpos = nfsd->nd_dpos;
2159 struct ucred *cred = &nfsd->nd_cr;
2163 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2164 int tdirfor_ret = 1, tdiraft_ret = 1;
2165 int v3 = (nfsd->nd_flag & ND_NFSV3);
2167 struct mbuf *mb, *mreq;
2168 struct nameidata fromnd, tond;
2169 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2170 struct vnode *tdirp = (struct vnode *)0;
2171 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2173 fhandle_t *ffhp, *tfhp;
2177 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2179 fvp = (struct vnode *)0;
2181 ffhp = &fnfh.fh_generic;
2182 tfhp = &tnfh.fh_generic;
2185 * Clear fields incase goto nfsmout occurs from macro.
2191 nfsm_srvmtofh(ffhp);
2192 nfsm_srvnamesiz(len);
2194 * Remember our original uid so that we can reset cr_uid before
2195 * the second nfs_namei() call, in case it is remapped.
2197 saved_uid = cred->cr_uid;
2198 fromnd.ni_cnd.cn_cred = cred;
2199 fromnd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2200 fromnd.ni_cnd.cn_flags = CNP_WANTPARENT | CNP_SAVESTART;
2201 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2202 &dpos, &fdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2205 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2212 nfsm_reply(2 * NFSX_WCCDATA(v3));
2213 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2214 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2219 nfsm_srvmtofh(tfhp);
2220 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2221 cred->cr_uid = saved_uid;
2222 tond.ni_cnd.cn_cred = cred;
2223 tond.ni_cnd.cn_nameiop = NAMEI_RENAME;
2224 tond.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF |
2225 CNP_NOCACHE | CNP_SAVESTART;
2226 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2227 &dpos, &tdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2230 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2242 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2248 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2255 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2263 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2270 if (fvp->v_mount != tdvp->v_mount) {
2284 * If source is the same as the destination (that is the
2285 * same vnode with the same name in the same directory),
2286 * then there is nothing to do.
2288 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2289 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2290 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2291 fromnd.ni_cnd.cn_namelen))
2296 * The VOP_RENAME function releases all vnode references &
2297 * locks prior to returning so we need to clear the pointers
2298 * to bypass cleanup code later on.
2300 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2301 nqsrv_getl(tdvp, ND_WRITE);
2303 nqsrv_getl(tvp, ND_WRITE);
2305 error = VOP_RENAME(fromnd.ni_dvp, NCPNULL, fromnd.ni_vp, &fromnd.ni_cnd,
2306 tond.ni_dvp, NCPNULL, tond.ni_vp, &tond.ni_cnd);
2307 fromnd.ni_dvp = NULL;
2308 fromnd.ni_vp = NULL;
2312 fromnd.ni_cnd.cn_flags &= ~CNP_HASBUF;
2313 tond.ni_cnd.cn_flags &= ~CNP_HASBUF;
2323 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2325 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2326 nfsm_reply(2 * NFSX_WCCDATA(v3));
2328 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2329 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2336 * Clear out tond related fields
2340 if (tond.ni_startdir)
2341 vrele(tond.ni_startdir);
2342 NDFREE(&tond, NDF_ONLY_PNBUF);
2344 if (tond.ni_dvp == tond.ni_vp)
2353 * Clear out fromnd related fields
2357 if (fromnd.ni_startdir)
2358 vrele(fromnd.ni_startdir);
2359 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2361 vrele(fromnd.ni_dvp);
2363 vrele(fromnd.ni_vp);
2372 nfsrv_link(nfsd, slp, td, mrq)
2373 struct nfsrv_descript *nfsd;
2374 struct nfssvc_sock *slp;
2378 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2379 struct sockaddr *nam = nfsd->nd_nam;
2380 caddr_t dpos = nfsd->nd_dpos;
2381 struct ucred *cred = &nfsd->nd_cr;
2382 struct nameidata nd;
2386 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2387 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2389 struct mbuf *mb, *mreq;
2390 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2391 struct vattr dirfor, diraft, at;
2393 fhandle_t *fhp, *dfhp;
2396 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2399 fhp = &nfh.fh_generic;
2400 dfhp = &dnfh.fh_generic;
2402 nfsm_srvmtofh(dfhp);
2403 nfsm_srvnamesiz(len);
2405 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2406 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2408 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2409 nfsm_srvpostop_attr(getret, &at);
2410 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2415 if (vp->v_type == VDIR) {
2416 error = EPERM; /* POSIX */
2419 nd.ni_cnd.cn_cred = cred;
2420 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2421 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2422 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2423 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2426 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2441 if (vp->v_mount != xp->v_mount)
2445 nqsrv_getl(vp, ND_WRITE);
2446 nqsrv_getl(xp, ND_WRITE);
2447 error = VOP_LINK(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
2448 NDFREE(&nd, NDF_ONLY_PNBUF);
2454 getret = VOP_GETATTR(vp, &at, td);
2456 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2457 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2459 nfsm_srvpostop_attr(getret, &at);
2460 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2466 NDFREE(&nd, NDF_ONLY_PNBUF);
2472 if (nd.ni_dvp == nd.ni_vp)
2483 * nfs symbolic link service
2486 nfsrv_symlink(nfsd, slp, td, mrq)
2487 struct nfsrv_descript *nfsd;
2488 struct nfssvc_sock *slp;
2492 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2493 struct sockaddr *nam = nfsd->nd_nam;
2494 caddr_t dpos = nfsd->nd_dpos;
2495 struct ucred *cred = &nfsd->nd_cr;
2496 struct vattr va, dirfor, diraft;
2497 struct nameidata nd;
2498 struct vattr *vap = &va;
2501 struct nfsv2_sattr *sp;
2502 char *bpos, *pathcp = (char *)0, *cp2;
2505 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2506 int v3 = (nfsd->nd_flag & ND_NFSV3);
2507 struct mbuf *mb, *mreq, *mb2;
2508 struct vnode *dirp = (struct vnode *)0;
2513 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2516 fhp = &nfh.fh_generic;
2518 nfsm_srvnamesiz(len);
2519 nd.ni_cnd.cn_cred = cred;
2520 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2521 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_SAVESTART;
2522 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2523 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2526 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2538 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2539 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2540 iv.iov_base = pathcp;
2542 io.uio_resid = len2;
2546 io.uio_segflg = UIO_SYSSPACE;
2547 io.uio_rw = UIO_READ;
2549 nfsm_mtouio(&io, len2);
2551 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2552 vap->va_mode = nfstov_mode(sp->sa_mode);
2554 *(pathcp + len2) = '\0';
2561 * issue symlink op. SAVESTART is set so the underlying path component
2562 * is only freed by the VOP if an error occurs.
2564 if (vap->va_mode == (mode_t)VNOVAL)
2566 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2567 error = VOP_SYMLINK(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2569 NDFREE(&nd, NDF_ONLY_PNBUF);
2574 * releases directory prior to potential lookup op.
2582 * Issue lookup. Leave SAVESTART set so we can easily free
2583 * the name buffer later on.
2585 * since LOCKPARENT is not set, ni_dvp will be garbage on
2586 * return whether an error occurs or not.
2588 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2589 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2590 nd.ni_cnd.cn_flags |= CNP_LOCKLEAF;
2591 nd.ni_cnd.cn_td = td;
2592 nd.ni_cnd.cn_cred = cred;
2594 error = lookup(&nd);
2598 bzero((caddr_t)fhp, sizeof(nfh));
2599 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2600 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2602 error = VOP_GETATTR(nd.ni_vp, vap, td);
2610 * These releases aren't strictly required, does even doing them
2611 * make any sense? XXX can nfsm_reply() block?
2614 FREE(pathcp, M_TEMP);
2618 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2622 if (nd.ni_startdir) {
2623 vrele(nd.ni_startdir);
2624 nd.ni_startdir = NULL;
2626 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2629 nfsm_srvpostop_fh(fhp);
2630 nfsm_srvpostop_attr(0, vap);
2632 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2638 NDFREE(&nd, NDF_ONLY_PNBUF);
2640 if (nd.ni_dvp == nd.ni_vp)
2648 vrele(nd.ni_startdir);
2652 FREE(pathcp, M_TEMP);
2661 nfsrv_mkdir(nfsd, slp, td, mrq)
2662 struct nfsrv_descript *nfsd;
2663 struct nfssvc_sock *slp;
2667 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2668 struct sockaddr *nam = nfsd->nd_nam;
2669 caddr_t dpos = nfsd->nd_dpos;
2670 struct ucred *cred = &nfsd->nd_cr;
2671 struct vattr va, dirfor, diraft;
2672 struct vattr *vap = &va;
2673 struct nfs_fattr *fp;
2674 struct nameidata nd;
2679 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2680 int v3 = (nfsd->nd_flag & ND_NFSV3);
2682 struct mbuf *mb, *mb2, *mreq;
2683 struct vnode *dirp = NULL;
2689 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2692 fhp = &nfh.fh_generic;
2694 nfsm_srvnamesiz(len);
2695 nd.ni_cnd.cn_cred = cred;
2696 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2697 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2699 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2700 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2703 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2710 nfsm_reply(NFSX_WCCDATA(v3));
2711 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2719 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2720 vap->va_mode = nfstov_mode(*tl++);
2724 * At this point nd.ni_dvp is referenced and exclusively locked and
2725 * nd.ni_vp, if it exists, is referenced but not locked.
2728 vap->va_type = VDIR;
2729 if (nd.ni_vp != NULL) {
2730 NDFREE(&nd, NDF_ONLY_PNBUF);
2736 * Issue mkdir op. Since SAVESTART is not set, the pathname
2737 * component is freed by the VOP call. This will fill-in
2738 * nd.ni_vp, reference, and exclusively lock it.
2740 if (vap->va_mode == (mode_t)VNOVAL)
2742 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2743 error = VOP_MKDIR(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, vap);
2744 NDFREE(&nd, NDF_ONLY_PNBUF);
2751 bzero((caddr_t)fhp, sizeof(nfh));
2752 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2753 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2755 error = VOP_GETATTR(nd.ni_vp, vap, td);
2759 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2760 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2763 nfsm_srvpostop_fh(fhp);
2764 nfsm_srvpostop_attr(0, vap);
2766 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2768 nfsm_srvfhtom(fhp, v3);
2769 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2770 nfsm_srvfillattr(vap, fp);
2779 NDFREE(&nd, NDF_ONLY_PNBUF);
2780 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2798 nfsrv_rmdir(nfsd, slp, td, mrq)
2799 struct nfsrv_descript *nfsd;
2800 struct nfssvc_sock *slp;
2804 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2805 struct sockaddr *nam = nfsd->nd_nam;
2806 caddr_t dpos = nfsd->nd_dpos;
2807 struct ucred *cred = &nfsd->nd_cr;
2811 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2812 int v3 = (nfsd->nd_flag & ND_NFSV3);
2814 struct mbuf *mb, *mreq;
2815 struct vnode *vp, *dirp = (struct vnode *)0;
2816 struct vattr dirfor, diraft;
2819 struct nameidata nd;
2822 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2825 fhp = &nfh.fh_generic;
2827 nfsm_srvnamesiz(len);
2828 nd.ni_cnd.cn_cred = cred;
2829 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2830 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2831 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2832 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2835 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2842 nfsm_reply(NFSX_WCCDATA(v3));
2843 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2848 if (vp->v_type != VDIR) {
2853 * No rmdir "." please.
2855 if (nd.ni_dvp == vp) {
2860 * The root of a mounted filesystem cannot be deleted.
2862 if (vp->v_flag & VROOT)
2866 * Issue or abort op. Since SAVESTART is not set, path name
2867 * component is freed by the VOP after either.
2870 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2871 nqsrv_getl(vp, ND_WRITE);
2872 error = VOP_RMDIR(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2874 NDFREE(&nd, NDF_ONLY_PNBUF);
2877 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2878 nfsm_reply(NFSX_WCCDATA(v3));
2880 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2886 NDFREE(&nd, NDF_ONLY_PNBUF);
2890 if (nd.ni_dvp == nd.ni_vp)
2902 * nfs readdir service
2903 * - mallocs what it thinks is enough to read
2904 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2905 * - calls VOP_READDIR()
2906 * - loops around building the reply
2907 * if the output generated exceeds count break out of loop
2908 * The nfsm_clget macro is used here so that the reply will be packed
2909 * tightly in mbuf clusters.
2910 * - it only knows that it has encountered eof when the VOP_READDIR()
2912 * - as such one readdir rpc will return eof false although you are there
2913 * and then the next will return eof
2914 * - it trims out records with d_fileno == 0
2915 * this doesn't matter for Unix clients, but they might confuse clients
2917 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2918 * than requested, but this may not apply to all filesystems. For
2919 * example, client NFS does not { although it is never remote mounted
2921 * The alternate call nfsrv_readdirplus() does lookups as well.
2922 * PS: The NFS protocol spec. does not clarify what the "count" byte
2923 * argument is a count of.. just name strings and file id's or the
2924 * entire reply rpc or ...
2925 * I tried just file name and id sizes and it confused the Sun client,
2926 * so I am using the full rpc size now. The "paranoia.." comment refers
2927 * to including the status longwords that are not a part of the dir.
2928 * "entry" structures, but are in the rpc.
2932 u_int32_t fl_postopok;
2933 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2935 u_int32_t fl_fhsize;
2936 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2940 nfsrv_readdir(nfsd, slp, td, mrq)
2941 struct nfsrv_descript *nfsd;
2942 struct nfssvc_sock *slp;
2946 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2947 struct sockaddr *nam = nfsd->nd_nam;
2948 caddr_t dpos = nfsd->nd_dpos;
2949 struct ucred *cred = &nfsd->nd_cr;
2957 struct mbuf *mb, *mb2, *mreq, *mp2;
2958 char *cpos, *cend, *cp2, *rbuf;
2959 struct vnode *vp = NULL;
2965 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2966 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2967 int v3 = (nfsd->nd_flag & ND_NFSV3);
2968 u_quad_t frev, off, toff, verf;
2969 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2971 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2972 fhp = &nfh.fh_generic;
2975 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2976 toff = fxdr_hyper(tl);
2978 verf = fxdr_hyper(tl);
2981 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2982 toff = fxdr_unsigned(u_quad_t, *tl++);
2983 verf = 0; /* shut up gcc */
2986 cnt = fxdr_unsigned(int, *tl);
2987 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2988 xfer = NFS_SRVMAXDATA(nfsd);
2994 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2995 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2996 if (!error && vp->v_type != VDIR) {
3002 nfsm_reply(NFSX_UNSIGNED);
3003 nfsm_srvpostop_attr(getret, &at);
3009 * Obtain lock on vnode for this section of the code
3012 nqsrv_getl(vp, ND_READ);
3014 error = getret = VOP_GETATTR(vp, &at, td);
3017 * XXX This check may be too strict for Solaris 2.5 clients.
3019 if (!error && toff && verf && verf != at.va_filerev)
3020 error = NFSERR_BAD_COOKIE;
3024 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3028 nfsm_reply(NFSX_POSTOPATTR(v3));
3029 nfsm_srvpostop_attr(getret, &at);
3033 VOP_UNLOCK(vp, NULL, 0, td);
3036 * end section. Allocate rbuf and continue
3038 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3041 iv.iov_len = fullsiz;
3044 io.uio_offset = (off_t)off;
3045 io.uio_resid = fullsiz;
3046 io.uio_segflg = UIO_SYSSPACE;
3047 io.uio_rw = UIO_READ;
3050 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
3052 free((caddr_t)cookies, M_TEMP);
3055 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3056 off = (off_t)io.uio_offset;
3057 if (!cookies && !error)
3058 error = NFSERR_PERM;
3060 getret = VOP_GETATTR(vp, &at, td);
3064 VOP_UNLOCK(vp, NULL, 0, td);
3068 free((caddr_t)rbuf, M_TEMP);
3070 free((caddr_t)cookies, M_TEMP);
3071 nfsm_reply(NFSX_POSTOPATTR(v3));
3072 nfsm_srvpostop_attr(getret, &at);
3077 siz -= io.uio_resid;
3080 * If nothing read, return eof
3086 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3089 nfsm_srvpostop_attr(getret, &at);
3090 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3091 txdr_hyper(at.va_filerev, tl);
3094 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3097 FREE((caddr_t)rbuf, M_TEMP);
3098 FREE((caddr_t)cookies, M_TEMP);
3105 * Check for degenerate cases of nothing useful read.
3106 * If so go try again
3110 dp = (struct dirent *)cpos;
3113 * For some reason FreeBSD's ufs_readdir() chooses to back the
3114 * directory offset up to a block boundary, so it is necessary to
3115 * skip over the records that preceed the requested offset. This
3116 * requires the assumption that file offset cookies monotonically
3119 while (cpos < cend && ncookies > 0 &&
3120 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3121 ((u_quad_t)(*cookiep)) <= toff)) {
3122 cpos += dp->d_reclen;
3123 dp = (struct dirent *)cpos;
3127 if (cpos >= cend || ncookies == 0) {
3133 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3134 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3136 nfsm_srvpostop_attr(getret, &at);
3137 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3138 txdr_hyper(at.va_filerev, tl);
3142 be = bp + M_TRAILINGSPACE(mp);
3144 /* Loop through the records and build reply */
3145 while (cpos < cend && ncookies > 0) {
3146 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3147 nlen = dp->d_namlen;
3148 rem = nfsm_rndup(nlen) - nlen;
3149 len += (4 * NFSX_UNSIGNED + nlen + rem);
3151 len += 2 * NFSX_UNSIGNED;
3157 * Build the directory record xdr from
3162 bp += NFSX_UNSIGNED;
3166 bp += NFSX_UNSIGNED;
3169 *tl = txdr_unsigned(dp->d_fileno);
3170 bp += NFSX_UNSIGNED;
3172 *tl = txdr_unsigned(nlen);
3173 bp += NFSX_UNSIGNED;
3175 /* And loop around copying the name */
3184 bcopy(cp, bp, tsiz);
3190 /* And null pad to a int32_t boundary */
3191 for (i = 0; i < rem; i++)
3195 /* Finish off the record */
3198 bp += NFSX_UNSIGNED;
3201 *tl = txdr_unsigned(*cookiep);
3202 bp += NFSX_UNSIGNED;
3204 cpos += dp->d_reclen;
3205 dp = (struct dirent *)cpos;
3213 bp += NFSX_UNSIGNED;
3219 bp += NFSX_UNSIGNED;
3222 mp->m_len = bp - mtod(mp, caddr_t);
3224 mp->m_len += bp - bpos;
3225 FREE((caddr_t)rbuf, M_TEMP);
3226 FREE((caddr_t)cookies, M_TEMP);
3235 nfsrv_readdirplus(nfsd, slp, td, mrq)
3236 struct nfsrv_descript *nfsd;
3237 struct nfssvc_sock *slp;
3241 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3242 struct sockaddr *nam = nfsd->nd_nam;
3243 caddr_t dpos = nfsd->nd_dpos;
3244 struct ucred *cred = &nfsd->nd_cr;
3252 struct mbuf *mb, *mb2, *mreq, *mp2;
3253 char *cpos, *cend, *cp2, *rbuf;
3254 struct vnode *vp = NULL, *nvp;
3257 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3260 struct vattr va, at, *vap = &va;
3261 struct nfs_fattr *fp;
3262 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3263 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3264 u_quad_t frev, off, toff, verf;
3265 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3267 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3268 fhp = &nfh.fh_generic;
3270 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3271 toff = fxdr_hyper(tl);
3273 verf = fxdr_hyper(tl);
3275 siz = fxdr_unsigned(int, *tl++);
3276 cnt = fxdr_unsigned(int, *tl);
3278 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3279 xfer = NFS_SRVMAXDATA(nfsd);
3285 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3286 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3287 if (!error && vp->v_type != VDIR) {
3293 nfsm_reply(NFSX_UNSIGNED);
3294 nfsm_srvpostop_attr(getret, &at);
3298 error = getret = VOP_GETATTR(vp, &at, td);
3301 * XXX This check may be too strict for Solaris 2.5 clients.
3303 if (!error && toff && verf && verf != at.va_filerev)
3304 error = NFSERR_BAD_COOKIE;
3307 nqsrv_getl(vp, ND_READ);
3308 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3313 nfsm_reply(NFSX_V3POSTOPATTR);
3314 nfsm_srvpostop_attr(getret, &at);
3318 VOP_UNLOCK(vp, NULL, 0, td);
3319 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3322 iv.iov_len = fullsiz;
3325 io.uio_offset = (off_t)off;
3326 io.uio_resid = fullsiz;
3327 io.uio_segflg = UIO_SYSSPACE;
3328 io.uio_rw = UIO_READ;
3331 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
3333 free((caddr_t)cookies, M_TEMP);
3336 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3337 off = (u_quad_t)io.uio_offset;
3338 getret = VOP_GETATTR(vp, &at, td);
3339 VOP_UNLOCK(vp, NULL, 0, td);
3340 if (!cookies && !error)
3341 error = NFSERR_PERM;
3348 free((caddr_t)cookies, M_TEMP);
3349 free((caddr_t)rbuf, M_TEMP);
3350 nfsm_reply(NFSX_V3POSTOPATTR);
3351 nfsm_srvpostop_attr(getret, &at);
3356 siz -= io.uio_resid;
3359 * If nothing read, return eof
3365 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3367 nfsm_srvpostop_attr(getret, &at);
3368 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3369 txdr_hyper(at.va_filerev, tl);
3373 FREE((caddr_t)cookies, M_TEMP);
3374 FREE((caddr_t)rbuf, M_TEMP);
3381 * Check for degenerate cases of nothing useful read.
3382 * If so go try again
3386 dp = (struct dirent *)cpos;
3389 * For some reason FreeBSD's ufs_readdir() chooses to back the
3390 * directory offset up to a block boundary, so it is necessary to
3391 * skip over the records that preceed the requested offset. This
3392 * requires the assumption that file offset cookies monotonically
3395 while (cpos < cend && ncookies > 0 &&
3396 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3397 ((u_quad_t)(*cookiep)) <= toff)) {
3398 cpos += dp->d_reclen;
3399 dp = (struct dirent *)cpos;
3403 if (cpos >= cend || ncookies == 0) {
3410 * Probe one of the directory entries to see if the filesystem
3413 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3414 error = NFSERR_NOTSUPP;
3417 free((caddr_t)cookies, M_TEMP);
3418 free((caddr_t)rbuf, M_TEMP);
3419 nfsm_reply(NFSX_V3POSTOPATTR);
3420 nfsm_srvpostop_attr(getret, &at);
3427 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3429 nfsm_srvpostop_attr(getret, &at);
3430 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3431 txdr_hyper(at.va_filerev, tl);
3434 be = bp + M_TRAILINGSPACE(mp);
3436 /* Loop through the records and build reply */
3437 while (cpos < cend && ncookies > 0) {
3438 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3439 nlen = dp->d_namlen;
3440 rem = nfsm_rndup(nlen)-nlen;
3443 * For readdir_and_lookup get the vnode using
3446 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3448 bzero((caddr_t)nfhp, NFSX_V3FH);
3450 nvp->v_mount->mnt_stat.f_fsid;
3451 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3456 if (VOP_GETATTR(nvp, vap, td)) {
3465 * If either the dircount or maxcount will be
3466 * exceeded, get out now. Both of these lengths
3467 * are calculated conservatively, including all
3470 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3472 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3473 if (len > cnt || dirlen > fullsiz) {
3479 * Build the directory record xdr from
3482 fp = (struct nfs_fattr *)&fl.fl_fattr;
3483 nfsm_srvfillattr(vap, fp);
3484 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3485 fl.fl_fhok = nfs_true;
3486 fl.fl_postopok = nfs_true;
3487 fl.fl_off.nfsuquad[0] = 0;
3488 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3492 bp += NFSX_UNSIGNED;
3495 bp += NFSX_UNSIGNED;
3497 *tl = txdr_unsigned(dp->d_fileno);
3498 bp += NFSX_UNSIGNED;
3500 *tl = txdr_unsigned(nlen);
3501 bp += NFSX_UNSIGNED;
3503 /* And loop around copying the name */
3508 if ((bp + xfer) > be)
3512 bcopy(cp, bp, tsiz);
3518 /* And null pad to a int32_t boundary */
3519 for (i = 0; i < rem; i++)
3523 * Now copy the flrep structure out.
3525 xfer = sizeof (struct flrep);
3529 if ((bp + xfer) > be)
3533 bcopy(cp, bp, tsiz);
3541 cpos += dp->d_reclen;
3542 dp = (struct dirent *)cpos;
3550 bp += NFSX_UNSIGNED;
3556 bp += NFSX_UNSIGNED;
3559 mp->m_len = bp - mtod(mp, caddr_t);
3561 mp->m_len += bp - bpos;
3562 FREE((caddr_t)cookies, M_TEMP);
3563 FREE((caddr_t)rbuf, M_TEMP);
3571 * nfs commit service
3574 nfsrv_commit(nfsd, slp, td, mrq)
3575 struct nfsrv_descript *nfsd;
3576 struct nfssvc_sock *slp;
3580 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3581 struct sockaddr *nam = nfsd->nd_nam;
3582 caddr_t dpos = nfsd->nd_dpos;
3583 struct ucred *cred = &nfsd->nd_cr;
3584 struct vattr bfor, aft;
3585 struct vnode *vp = NULL;
3591 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3593 struct mbuf *mb, *mb2, *mreq;
3596 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3600 fhp = &nfh.fh_generic;
3602 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3605 * XXX At this time VOP_FSYNC() does not accept offset and byte
3606 * count parameters, so these arguments are useless (someday maybe).
3608 off = fxdr_hyper(tl);
3610 cnt = fxdr_unsigned(int, *tl);
3611 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3612 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3614 nfsm_reply(2 * NFSX_UNSIGNED);
3615 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3619 for_ret = VOP_GETATTR(vp, &bfor, td);
3621 if (cnt > MAX_COMMIT_COUNT) {
3623 * Give up and do the whole thing
3626 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3627 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3629 error = VOP_FSYNC(vp, MNT_WAIT, td);
3632 * Locate and synchronously write any buffers that fall
3633 * into the requested range. Note: we are assuming that
3634 * f_iosize is a power of 2.
3636 int iosize = vp->v_mount->mnt_stat.f_iosize;
3637 int iomask = iosize - 1;
3642 * Align to iosize boundry, super-align to page boundry.
3645 cnt += off & iomask;
3646 off &= ~(u_quad_t)iomask;
3648 if (off & PAGE_MASK) {
3649 cnt += off & PAGE_MASK;
3650 off &= ~(u_quad_t)PAGE_MASK;
3652 lblkno = off / iosize;
3655 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3656 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3664 * If we have a buffer and it is marked B_DELWRI we
3665 * have to lock and write it. Otherwise the prior
3666 * write is assumed to have already been committed.
3668 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3669 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3670 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3671 continue; /* retry */
3674 bp->b_flags &= ~B_ASYNC;
3675 VOP_BWRITE(bp->b_vp, bp);
3687 aft_ret = VOP_GETATTR(vp, &aft, td);
3690 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3691 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3693 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3694 if (nfsver.tv_sec == 0)
3696 *tl++ = txdr_unsigned(nfsver.tv_sec);
3697 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3708 * nfs statfs service
3711 nfsrv_statfs(nfsd, slp, td, mrq)
3712 struct nfsrv_descript *nfsd;
3713 struct nfssvc_sock *slp;
3717 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3718 struct sockaddr *nam = nfsd->nd_nam;
3719 caddr_t dpos = nfsd->nd_dpos;
3720 struct ucred *cred = &nfsd->nd_cr;
3722 struct nfs_statfs *sfp;
3726 int error = 0, rdonly, cache, getret = 1;
3727 int v3 = (nfsd->nd_flag & ND_NFSV3);
3729 struct mbuf *mb, *mb2, *mreq;
3730 struct vnode *vp = NULL;
3734 struct statfs statfs;
3735 u_quad_t frev, tval;
3737 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3741 fhp = &nfh.fh_generic;
3743 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3744 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3746 nfsm_reply(NFSX_UNSIGNED);
3747 nfsm_srvpostop_attr(getret, &at);
3752 error = VFS_STATFS(vp->v_mount, sf, td);
3753 getret = VOP_GETATTR(vp, &at, td);
3756 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3758 nfsm_srvpostop_attr(getret, &at);
3763 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3765 tval = (u_quad_t)sf->f_blocks;
3766 tval *= (u_quad_t)sf->f_bsize;
3767 txdr_hyper(tval, &sfp->sf_tbytes);
3768 tval = (u_quad_t)sf->f_bfree;
3769 tval *= (u_quad_t)sf->f_bsize;
3770 txdr_hyper(tval, &sfp->sf_fbytes);
3771 tval = (u_quad_t)sf->f_bavail;
3772 tval *= (u_quad_t)sf->f_bsize;
3773 txdr_hyper(tval, &sfp->sf_abytes);
3774 sfp->sf_tfiles.nfsuquad[0] = 0;
3775 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3776 sfp->sf_ffiles.nfsuquad[0] = 0;
3777 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3778 sfp->sf_afiles.nfsuquad[0] = 0;
3779 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3780 sfp->sf_invarsec = 0;
3782 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3783 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3784 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3785 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3786 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3795 * nfs fsinfo service
3798 nfsrv_fsinfo(nfsd, slp, td, mrq)
3799 struct nfsrv_descript *nfsd;
3800 struct nfssvc_sock *slp;
3804 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3805 struct sockaddr *nam = nfsd->nd_nam;
3806 caddr_t dpos = nfsd->nd_dpos;
3807 struct ucred *cred = &nfsd->nd_cr;
3809 struct nfsv3_fsinfo *sip;
3812 int error = 0, rdonly, cache, getret = 1, pref;
3814 struct mbuf *mb, *mb2, *mreq;
3815 struct vnode *vp = NULL;
3819 u_quad_t frev, maxfsize;
3822 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3826 fhp = &nfh.fh_generic;
3828 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3829 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3831 nfsm_reply(NFSX_UNSIGNED);
3832 nfsm_srvpostop_attr(getret, &at);
3837 /* XXX Try to make a guess on the max file size. */
3838 VFS_STATFS(vp->v_mount, &sb, td);
3839 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3841 getret = VOP_GETATTR(vp, &at, td);
3844 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3845 nfsm_srvpostop_attr(getret, &at);
3846 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3850 * There should be file system VFS OP(s) to get this information.
3851 * For now, assume ufs.
3853 if (slp->ns_so->so_type == SOCK_DGRAM)
3854 pref = NFS_MAXDGRAMDATA;
3857 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3858 sip->fs_rtpref = txdr_unsigned(pref);
3859 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3860 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3861 sip->fs_wtpref = txdr_unsigned(pref);
3862 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3863 sip->fs_dtpref = txdr_unsigned(pref);
3864 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3865 sip->fs_timedelta.nfsv3_sec = 0;
3866 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3867 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3868 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3869 NFSV3FSINFO_CANSETTIME);
3877 * nfs pathconf service
3880 nfsrv_pathconf(nfsd, slp, td, mrq)
3881 struct nfsrv_descript *nfsd;
3882 struct nfssvc_sock *slp;
3886 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3887 struct sockaddr *nam = nfsd->nd_nam;
3888 caddr_t dpos = nfsd->nd_dpos;
3889 struct ucred *cred = &nfsd->nd_cr;
3891 struct nfsv3_pathconf *pc;
3894 int error = 0, rdonly, cache, getret = 1;
3895 register_t linkmax, namemax, chownres, notrunc;
3897 struct mbuf *mb, *mb2, *mreq;
3898 struct vnode *vp = NULL;
3904 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3908 fhp = &nfh.fh_generic;
3910 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3911 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3913 nfsm_reply(NFSX_UNSIGNED);
3914 nfsm_srvpostop_attr(getret, &at);
3918 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3920 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3922 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3924 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3925 getret = VOP_GETATTR(vp, &at, td);
3928 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3929 nfsm_srvpostop_attr(getret, &at);
3934 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3936 pc->pc_linkmax = txdr_unsigned(linkmax);
3937 pc->pc_namemax = txdr_unsigned(namemax);
3938 pc->pc_notrunc = txdr_unsigned(notrunc);
3939 pc->pc_chownrestricted = txdr_unsigned(chownres);
3942 * These should probably be supported by VOP_PATHCONF(), but
3943 * until msdosfs is exportable (why would you want to?), the
3944 * Unix defaults should be ok.
3946 pc->pc_caseinsensitive = nfs_false;
3947 pc->pc_casepreserving = nfs_true;
3955 * Null operation, used by clients to ping server
3959 nfsrv_null(nfsd, slp, td, mrq)
3960 struct nfsrv_descript *nfsd;
3961 struct nfssvc_sock *slp;
3965 struct mbuf *mrep = nfsd->nd_mrep;
3967 int error = NFSERR_RETVOID, cache;
3968 struct mbuf *mb, *mreq;
3971 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3980 * No operation, used for obsolete procedures
3984 nfsrv_noop(nfsd, slp, td, mrq)
3985 struct nfsrv_descript *nfsd;
3986 struct nfssvc_sock *slp;
3990 struct mbuf *mrep = nfsd->nd_mrep;
3993 struct mbuf *mb, *mreq;
3996 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4000 if (nfsd->nd_repstat)
4001 error = nfsd->nd_repstat;
4003 error = EPROCUNAVAIL;
4010 * Perform access checking for vnodes obtained from file handles that would
4011 * refer to files already opened by a Unix client. You cannot just use
4012 * vn_writechk() and VOP_ACCESS() for two reasons.
4013 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4014 * 2 - The owner is to be given access irrespective of mode bits for some
4015 * operations, so that processes that chmod after opening a file don't
4016 * break. I don't like this because it opens a security hole, but since
4017 * the nfs server opens a security hole the size of a barn door anyhow,
4020 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4021 * will return EPERM instead of EACCESS. EPERM is always an error.
4024 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
4025 int rdonly, struct thread *td, int override)
4030 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4031 if (flags & VWRITE) {
4032 /* Just vn_writechk() changed to check rdonly */
4034 * Disallow write attempts on read-only file systems;
4035 * unless the file is a socket or a block or character
4036 * device resident on the file system.
4038 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4039 switch (vp->v_type) {
4049 * If there's shared text associated with
4050 * the inode, we can't allow writing.
4052 if (vp->v_flag & VTEXT)
4055 error = VOP_GETATTR(vp, &vattr, td);
4058 error = VOP_ACCESS(vp, flags, cred, td);
4060 * Allow certain operations for the owner (reads and writes
4061 * on files that are already open).
4063 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4067 #endif /* NFS_NOSERVER */