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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * nfs version 2 and 3 server calls to vnode ops
40 * - these routines generally have 3 phases
41 * 1 - break down and validate rpc request in mbuf list
42 * 2 - do the vnode ops for the request
43 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
44 * 3 - build the rpc reply in an mbuf list
46 * - do not mix the phases, since the nfsm_?? macros can return failures
47 * on a bad rpc or similar and do not do any vrele() or vput()'s
49 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
50 * error number iff error != 0 whereas
51 * returning an error from the server function implies a fatal error
52 * such as a badly constructed rpc request that should be dropped without
54 * For nfsm_reply(), the case where error == EBADRPC is treated
55 * specially; after constructing a reply, it does an immediate
56 * `goto nfsmout' to avoid getting any V3 post-op status appended.
59 * Warning: always pay careful attention to resource cleanup on return
60 * and note that nfsm_*() macros can terminate a procedure on certain
63 * lookup() and namei()
64 * may return garbage in various structural fields/return elements
65 * if an error is returned, and may garbage up nd.ni_dvp even if no
66 * error is returned and you did not request LOCKPARENT or WANTPARENT.
68 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
69 * buffer has been freed or not.
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/namei.h>
76 #include <sys/unistd.h>
77 #include <sys/vnode.h>
78 #include <sys/mount.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/malloc.h>
84 #include <sys/dirent.h>
86 #include <sys/kernel.h>
87 #include <sys/sysctl.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_object.h>
95 #include <nfs/nfsproto.h>
96 #include <nfsserver/nfs.h>
97 #include <nfs/xdr_subs.h>
98 #include <nfsserver/nfsm_subs.h>
101 #define nfsdbprintf(info) printf info
103 #define nfsdbprintf(info)
106 #define MAX_COMMIT_COUNT (1024 * 1024)
108 #define NUM_HEURISTIC 1017
109 #define NHUSE_INIT 64
111 #define NHUSE_MAX 2048
113 static struct nfsheur {
114 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
115 off_t nh_nextr; /* next offset for sequential detection */
116 int nh_use; /* use count for selection */
117 int nh_seqcount; /* heuristic */
118 } nfsheur[NUM_HEURISTIC];
122 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
123 int nfsrvw_procrastinate_v3 = 0;
125 static struct timeval nfsver = { 0 };
127 SYSCTL_NODE(_vfs, OID_AUTO, nfsrv, CTLFLAG_RW, 0, "NFS server");
129 static int nfs_async;
130 static int nfs_commit_blks;
131 static int nfs_commit_miss;
132 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
133 "Tell client that writes were synced even though they were not");
134 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
135 "Number of completed commits");
136 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
138 struct nfsrvstats nfsrvstats;
139 SYSCTL_STRUCT(_vfs_nfsrv, NFS_NFSRVSTATS, nfsrvstats, CTLFLAG_RW,
140 &nfsrvstats, nfsrvstats, "S,nfsrvstats");
142 static int nfsrv_access(struct vnode *, accmode_t, struct ucred *,
146 * Clear nameidata fields that are tested in nsfmout cleanup code prior
147 * to using first nfsm macro (that might jump to the cleanup code).
151 ndclear(struct nameidata *nd)
154 nd->ni_cnd.cn_flags = 0;
157 nd->ni_startdir = NULL;
161 * Takes two vfslocked integers and returns with at most one
162 * reference to giant. The return value indicates whether giant
163 * is held by either lock. This simplifies nfsrv ops by allowing
164 * them to track only one vfslocked var.
167 nfsrv_lockedpair(int vfs1, int vfs2)
171 VFS_UNLOCK_GIANT(vfs2);
173 return (vfs1 | vfs2);
177 nfsrv_lockedpair_nd(int vfs1, struct nameidata *nd)
181 vfs2 = NDHASGIANT(nd);
183 return nfsrv_lockedpair(vfs1, vfs2);
187 * nfs v3 access service
190 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
193 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
194 struct sockaddr *nam = nfsd->nd_nam;
195 caddr_t dpos = nfsd->nd_dpos;
196 struct ucred *cred = nfsd->nd_cr;
197 struct vnode *vp = NULL;
202 int error = 0, rdonly, getret;
203 struct mbuf *mb, *mreq;
204 struct vattr vattr, *vap = &vattr;
205 u_long testmode, nfsmode;
206 int v3 = (nfsd->nd_flag & ND_NFSV3);
209 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
211 panic("nfsrv3_access: v3 proc called on a v2 connection");
213 fhp = &nfh.fh_generic;
215 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
216 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
219 nfsm_reply(NFSX_UNSIGNED);
220 nfsm_srvpostop_attr(1, NULL);
224 nfsmode = fxdr_unsigned(u_int32_t, *tl);
225 if ((nfsmode & NFSV3ACCESS_READ) &&
226 nfsrv_access(vp, VREAD, cred, rdonly, 0))
227 nfsmode &= ~NFSV3ACCESS_READ;
228 if (vp->v_type == VDIR)
229 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
232 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
233 if ((nfsmode & testmode) &&
234 nfsrv_access(vp, VWRITE, cred, rdonly, 0))
235 nfsmode &= ~testmode;
236 if (vp->v_type == VDIR)
237 testmode = NFSV3ACCESS_LOOKUP;
239 testmode = NFSV3ACCESS_EXECUTE;
240 if ((nfsmode & testmode) &&
241 nfsrv_access(vp, VEXEC, cred, rdonly, 0))
242 nfsmode &= ~testmode;
243 getret = VOP_GETATTR(vp, vap, cred);
246 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
247 nfsm_srvpostop_attr(getret, vap);
248 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
249 *tl = txdr_unsigned(nfsmode);
253 VFS_UNLOCK_GIANT(vfslocked);
258 * nfs getattr service
261 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
264 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
265 struct sockaddr *nam = nfsd->nd_nam;
266 caddr_t dpos = nfsd->nd_dpos;
267 struct ucred *cred = nfsd->nd_cr;
268 struct nfs_fattr *fp;
270 struct vattr *vap = &va;
271 struct vnode *vp = NULL;
275 int error = 0, rdonly;
276 struct mbuf *mb, *mreq;
279 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
281 fhp = &nfh.fh_generic;
283 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp, nam,
290 error = VOP_GETATTR(vp, vap, cred);
293 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
298 fp = nfsm_build(struct nfs_fattr *,
299 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
300 nfsm_srvfillattr(vap, fp);
306 VFS_UNLOCK_GIANT(vfslocked);
311 * nfs setattr service
314 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
317 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
318 struct sockaddr *nam = nfsd->nd_nam;
319 caddr_t dpos = nfsd->nd_dpos;
320 struct ucred *cred = nfsd->nd_cr;
321 struct vattr va, preat;
322 struct vattr *vap = &va;
323 struct nfsv2_sattr *sp;
324 struct nfs_fattr *fp;
325 struct vnode *vp = NULL;
330 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
331 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
332 struct mbuf *mb, *mreq;
333 struct timespec guard = { 0, 0 };
334 struct mount *mp = NULL;
338 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
340 fhp = &nfh.fh_generic;
342 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
346 vfslocked = VFS_LOCK_GIANT(mp);
347 (void) vn_start_write(NULL, &mp, V_WAIT);
348 vfs_rel(mp); /* The write holds a ref. */
352 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
353 gcheck = fxdr_unsigned(int, *tl);
355 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
356 fxdr_nfsv3time(tl, &guard);
359 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
361 * Nah nah nah nah na nah
362 * There is a bug in the Sun client that puts 0xffff in the mode
363 * field of sattr when it should put in 0xffffffff. The u_short
364 * doesn't sign extend.
365 * --> check the low order 2 bytes for 0xffff
367 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
368 vap->va_mode = nfstov_mode(sp->sa_mode);
369 if (sp->sa_uid != nfsrv_nfs_xdrneg1)
370 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
371 if (sp->sa_gid != nfsrv_nfs_xdrneg1)
372 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
373 if (sp->sa_size != nfsrv_nfs_xdrneg1)
374 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
375 if (sp->sa_atime.nfsv2_sec != nfsrv_nfs_xdrneg1) {
377 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
379 vap->va_atime.tv_sec =
380 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
381 vap->va_atime.tv_nsec = 0;
384 if (sp->sa_mtime.nfsv2_sec != nfsrv_nfs_xdrneg1)
385 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
390 * Now that we have all the fields, lets do it.
392 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, nfsd, slp,
394 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
396 nfsm_reply(2 * NFSX_UNSIGNED);
398 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
404 * vp now an active resource, pay careful attention to cleanup
407 error = preat_ret = VOP_GETATTR(vp, &preat, cred);
408 if (!error && gcheck &&
409 (preat.va_ctime.tv_sec != guard.tv_sec ||
410 preat.va_ctime.tv_nsec != guard.tv_nsec))
411 error = NFSERR_NOT_SYNC;
415 nfsm_reply(NFSX_WCCDATA(v3));
417 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
424 * If the size is being changed write acces is required, otherwise
425 * just check for a read only filesystem.
427 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
428 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
433 if (vp->v_type == VDIR) {
436 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
440 error = VOP_SETATTR(vp, vap, cred);
441 postat_ret = VOP_GETATTR(vp, vap, cred);
449 nfsm_reply(NFSX_WCCORFATTR(v3));
451 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
453 /* v2 non-error case. */
454 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
455 nfsm_srvfillattr(vap, fp);
463 vn_finished_write(mp);
464 VFS_UNLOCK_GIANT(vfslocked);
472 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
475 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
476 struct sockaddr *nam = nfsd->nd_nam;
477 caddr_t dpos = nfsd->nd_dpos;
478 struct ucred *cred = nfsd->nd_cr;
479 struct nfs_fattr *fp;
480 struct nameidata nd, ind, *ndp = &nd;
481 struct vnode *vp, *dirp = NULL;
485 int error = 0, len, dirattr_ret = 1;
486 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
487 struct mbuf *mb, *mreq;
488 struct vattr va, dirattr, *vap = &va;
492 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
496 fhp = &nfh.fh_generic;
498 nfsm_srvnamesiz(len);
500 pubflag = nfs_ispublicfh(fhp);
502 nd.ni_cnd.cn_cred = cred;
503 nd.ni_cnd.cn_nameiop = LOOKUP;
504 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART | MPSAFE;
505 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
506 &dirp, v3, &dirattr, &dirattr_ret, pubflag);
507 vfslocked = NDHASGIANT(&nd);
510 * namei failure, only dirp to cleanup. Clear out garbarge from
511 * structure in case macros jump to nfsmout.
519 nfsm_reply(NFSX_POSTOPATTR(v3));
521 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
527 * Locate index file for public filehandle
529 * error is 0 on entry and 0 on exit from this block.
533 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
535 * Setup call to lookup() to see if we can find
536 * the index file. Arguably, this doesn't belong
537 * in a kernel.. Ugh. If an error occurs, do not
538 * try to install an index file and then clear the
541 * When we replace nd with ind and redirect ndp,
542 * maintenance of ni_startdir and ni_vp shift to
543 * ind and we have to clean them up in the old nd.
544 * However, the cnd resource continues to be maintained
545 * via the original nd. Confused? You aren't alone!
548 VOP_UNLOCK(nd.ni_vp, 0);
549 ind.ni_pathlen = strlen(nfs_pub.np_index);
550 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
552 ind.ni_startdir = nd.ni_vp;
553 VREF(ind.ni_startdir);
554 ind.ni_cnd.cn_flags &= ~GIANTHELD;
555 tvfslocked = VFS_LOCK_GIANT(ind.ni_startdir->v_mount);
557 nd.ni_cnd.cn_flags |= GIANTHELD;
558 error = lookup(&ind);
560 vfslocked = nfsrv_lockedpair_nd(vfslocked, &ind);
561 ind.ni_cnd.cn_flags &= ~GIANTHELD;
565 * Found an index file. Get rid of
566 * the old references. transfer nd.ni_vp'
572 vrele(nd.ni_startdir);
573 nd.ni_startdir = NULL;
579 * If the public filehandle was used, check that this lookup
580 * didn't result in a filehandle outside the publicly exported
581 * filesystem. We clear the poor vp here to avoid lockups due
585 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
593 * Resources at this point:
594 * ndp->ni_vp may not be NULL
598 nfsm_reply(NFSX_POSTOPATTR(v3));
600 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
606 * Get underlying attribute, then release remaining resources ( for
607 * the same potential blocking reason ) and reply.
610 bzero((caddr_t)fhp, sizeof(nfh));
611 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
612 error = VOP_VPTOFH(vp, &fhp->fh_fid);
614 error = VOP_GETATTR(vp, vap, cred);
617 vrele(ndp->ni_startdir);
620 ndp->ni_startdir = NULL;
622 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
625 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
629 nfsm_srvfhtom(fhp, v3);
631 nfsm_srvpostop_attr(0, vap);
632 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
634 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
635 nfsm_srvfillattr(vap, fp);
639 if (ndp->ni_vp || dirp || ndp->ni_startdir) {
644 if (ndp->ni_startdir)
645 vrele(ndp->ni_startdir);
647 NDFREE(&nd, NDF_ONLY_PNBUF);
648 VFS_UNLOCK_GIANT(vfslocked);
653 * nfs readlink service
656 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
659 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
660 struct sockaddr *nam = nfsd->nd_nam;
661 caddr_t dpos = nfsd->nd_dpos;
662 struct ucred *cred = nfsd->nd_cr;
663 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
664 struct iovec *ivp = iv;
668 int error = 0, rdonly, i, tlen, len, getret;
669 int v3 = (nfsd->nd_flag & ND_NFSV3);
670 struct mbuf *mb, *mp3, *nmp, *mreq;
671 struct vnode *vp = NULL;
675 struct uio io, *uiop = &io;
678 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
684 fhp = &nfh.fh_generic;
688 while (len < NFS_MAXPATHLEN) {
689 MGET(nmp, M_WAIT, MT_DATA);
691 nmp->m_len = NFSMSIZ(nmp);
698 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
699 mp->m_len = NFS_MAXPATHLEN - len;
700 len = NFS_MAXPATHLEN;
703 ivp->iov_base = mtod(mp, caddr_t);
704 ivp->iov_len = mp->m_len;
709 uiop->uio_iovcnt = i;
710 uiop->uio_offset = 0;
711 uiop->uio_resid = len;
712 uiop->uio_rw = UIO_READ;
713 uiop->uio_segflg = UIO_SYSSPACE;
715 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
718 nfsm_reply(2 * NFSX_UNSIGNED);
720 nfsm_srvpostop_attr(1, NULL);
724 if (vp->v_type != VLNK) {
730 error = VOP_READLINK(vp, uiop, cred);
731 getret = VOP_GETATTR(vp, &attr, cred);
734 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
736 nfsm_srvpostop_attr(getret, &attr);
741 if (uiop->uio_resid > 0) {
742 len -= uiop->uio_resid;
743 tlen = nfsm_rndup(len);
744 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
746 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
747 *tl = txdr_unsigned(len);
755 VFS_UNLOCK_GIANT(vfslocked);
763 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
766 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
767 struct sockaddr *nam = nfsd->nd_nam;
768 caddr_t dpos = nfsd->nd_dpos;
769 struct ucred *cred = nfsd->nd_cr;
773 struct nfs_fattr *fp;
777 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
778 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
779 struct mbuf *mb, *mreq;
781 struct vnode *vp = NULL;
784 struct uio io, *uiop = &io;
785 struct vattr va, *vap = &va;
792 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
794 fhp = &nfh.fh_generic;
797 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
798 off = fxdr_hyper(tl);
800 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
801 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
803 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
806 * Reference vp. If an error occurs, vp will be invalid, but we
807 * have to NULL it just in case. The macros might goto nfsmout
811 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
815 nfsm_reply(2 * NFSX_UNSIGNED);
817 nfsm_srvpostop_attr(1, NULL);
822 if (vp->v_type != VREG) {
826 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
829 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, 1)) != 0)
830 error = nfsrv_access(vp, VEXEC, cred, rdonly, 1);
832 getret = VOP_GETATTR(vp, vap, cred);
838 nfsm_reply(NFSX_POSTOPATTR(v3));
840 nfsm_srvpostop_attr(getret, vap);
846 * Calculate byte count to read
849 if (off >= vap->va_size)
851 else if ((off + reqlen) > vap->va_size)
852 cnt = vap->va_size - off;
857 * Calculate seqcount for heuristic
865 * Locate best candidate
868 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
872 if (nfsheur[hi].nh_vp == vp) {
876 if (nfsheur[hi].nh_use > 0)
877 --nfsheur[hi].nh_use;
878 hi = (hi + 1) % NUM_HEURISTIC;
879 if (nfsheur[hi].nh_use < nh->nh_use)
883 if (nh->nh_vp != vp) {
886 nh->nh_use = NHUSE_INIT;
894 * Calculate heuristic
897 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
898 if (++nh->nh_seqcount > IO_SEQMAX)
899 nh->nh_seqcount = IO_SEQMAX;
900 } else if (nh->nh_seqcount > 1) {
905 nh->nh_use += NHUSE_INC;
906 if (nh->nh_use > NHUSE_MAX)
907 nh->nh_use = NHUSE_MAX;
908 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
911 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
913 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
914 *tl++ = nfsrv_nfs_true;
915 fp = (struct nfs_fattr *)tl;
916 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
918 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
919 fp = (struct nfs_fattr *)tl;
920 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
922 len = left = nfsm_rndup(cnt);
925 * Generate the mbuf list with the uio_iov ref. to it.
930 siz = min(M_TRAILINGSPACE(m), left);
936 MGET(m, M_WAIT, MT_DATA);
943 iv = malloc(i * sizeof (struct iovec),
945 uiop->uio_iov = iv2 = iv;
951 panic("nfsrv_read iov");
952 siz = min(M_TRAILINGSPACE(m), left);
954 iv->iov_base = mtod(m, caddr_t) + m->m_len;
963 uiop->uio_iovcnt = i;
964 uiop->uio_offset = off;
965 uiop->uio_resid = len;
966 uiop->uio_rw = UIO_READ;
967 uiop->uio_segflg = UIO_SYSSPACE;
968 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
969 off = uiop->uio_offset;
971 free((caddr_t)iv2, M_TEMP);
972 if (error || (getret = VOP_GETATTR(vp, vap, cred))) {
978 nfsm_reply(NFSX_POSTOPATTR(v3));
980 nfsm_srvpostop_attr(getret, vap);
988 nfsm_srvfillattr(vap, fp);
989 tlen = len - uiop->uio_resid;
990 cnt = cnt < tlen ? cnt : tlen;
991 tlen = nfsm_rndup(cnt);
992 if (len != tlen || tlen != cnt)
993 nfsm_adj(mb, len - tlen, tlen - cnt);
995 *tl++ = txdr_unsigned(cnt);
997 *tl++ = nfsrv_nfs_true;
999 *tl++ = nfsrv_nfs_false;
1001 *tl = txdr_unsigned(cnt);
1005 VFS_UNLOCK_GIANT(vfslocked);
1013 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1016 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1017 struct sockaddr *nam = nfsd->nd_nam;
1018 caddr_t dpos = nfsd->nd_dpos;
1019 struct ucred *cred = nfsd->nd_cr;
1023 struct nfs_fattr *fp;
1025 struct vattr va, forat;
1026 struct vattr *vap = &va;
1029 int error = 0, rdonly, len, forat_ret = 1;
1030 int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
1031 int stable = NFSV3WRITE_FILESYNC;
1032 int v3 = (nfsd->nd_flag & ND_NFSV3);
1033 struct mbuf *mb, *mreq;
1034 struct vnode *vp = NULL;
1037 struct uio io, *uiop = &io;
1039 struct mount *mntp = NULL;
1043 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1050 fhp = &nfh.fh_generic;
1052 if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1056 vfslocked = VFS_LOCK_GIANT(mntp);
1057 (void) vn_start_write(NULL, &mntp, V_WAIT);
1058 vfs_rel(mntp); /* The write holds a ref. */
1060 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
1061 off = fxdr_hyper(tl);
1063 stable = fxdr_unsigned(int, *tl++);
1065 tl = nfsm_dissect_nonblock(u_int32_t *, 4 * NFSX_UNSIGNED);
1066 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1069 stable = NFSV3WRITE_UNSTABLE;
1071 retlen = len = fxdr_unsigned(int32_t, *tl);
1075 * For NFS Version 2, it is not obvious what a write of zero length
1076 * should do, but I might as well be consistent with Version 3,
1077 * which is to return ok so long as there are no permission problems.
1085 adjust = dpos - mtod(mp, caddr_t);
1086 mp->m_len -= adjust;
1087 if (mp->m_len > 0 && adjust > 0)
1088 mp->m_data += adjust;
1092 else if (mp->m_len > 0) {
1095 mp->m_len -= (i - len);
1104 if (len > NFS_MAXDATA || len < 0 || i < len) {
1106 nfsm_reply(2 * NFSX_UNSIGNED);
1108 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1112 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, nfsd, slp,
1113 nam, &rdonly, TRUE);
1114 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
1117 nfsm_reply(2 * NFSX_UNSIGNED);
1119 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1124 forat_ret = VOP_GETATTR(vp, &forat, cred);
1125 if (vp->v_type != VREG) {
1129 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1132 error = nfsrv_access(vp, VWRITE, cred, rdonly, 1);
1136 nfsm_reply(NFSX_WCCDATA(v3));
1138 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1144 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1146 uiop->uio_iov = iv = ivp;
1147 uiop->uio_iovcnt = cnt;
1150 if (mp->m_len > 0) {
1151 ivp->iov_base = mtod(mp, caddr_t);
1152 ivp->iov_len = mp->m_len;
1160 * The IO_METASYNC flag indicates that all metadata (and not just
1161 * enough to ensure data integrity) mus be written to stable storage
1163 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1165 if (stable == NFSV3WRITE_UNSTABLE)
1166 ioflags = IO_NODELOCKED;
1167 else if (stable == NFSV3WRITE_DATASYNC)
1168 ioflags = (IO_SYNC | IO_NODELOCKED);
1170 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1171 uiop->uio_resid = len;
1172 uiop->uio_rw = UIO_WRITE;
1173 uiop->uio_segflg = UIO_SYSSPACE;
1174 uiop->uio_td = NULL;
1175 uiop->uio_offset = off;
1176 error = VOP_WRITE(vp, uiop, ioflags, cred);
1177 /* Unlocked write. */
1178 nfsrvstats.srvvop_writes++;
1179 free((caddr_t)iv, M_TEMP);
1181 aftat_ret = VOP_GETATTR(vp, vap, cred);
1187 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1188 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1190 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1195 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1196 *tl++ = txdr_unsigned(retlen);
1198 * If nfs_async is set, then pretend the write was FILESYNC.
1200 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1201 *tl++ = txdr_unsigned(stable);
1203 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1205 * Actually, there is no need to txdr these fields,
1206 * but it may make the values more human readable,
1207 * for debugging purposes.
1209 if (nfsver.tv_sec == 0)
1211 *tl++ = txdr_unsigned(nfsver.tv_sec);
1212 *tl = txdr_unsigned(nfsver.tv_usec);
1213 } else if (!error) {
1214 /* v2 non-error case. */
1215 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1216 nfsm_srvfillattr(vap, fp);
1222 vn_finished_write(mntp);
1223 VFS_UNLOCK_GIANT(vfslocked);
1228 * nfs create service
1229 * now does a truncate to 0 length via. setattr if it already exists
1232 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1235 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1236 struct sockaddr *nam = nfsd->nd_nam;
1237 caddr_t dpos = nfsd->nd_dpos;
1238 struct ucred *cred = nfsd->nd_cr;
1239 struct nfs_fattr *fp;
1240 struct vattr va, dirfor, diraft;
1241 struct vattr *vap = &va;
1242 struct nfsv2_sattr *sp;
1244 struct nameidata nd;
1246 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1247 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1248 struct mbuf *mb, *mreq;
1249 struct vnode *dirp = NULL;
1253 struct timespec cverf;
1254 struct mount *mp = NULL;
1258 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1265 fhp = &nfh.fh_generic;
1267 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1271 vfslocked = VFS_LOCK_GIANT(mp);
1272 (void) vn_start_write(NULL, &mp, V_WAIT);
1273 vfs_rel(mp); /* The write holds a ref. */
1274 nfsm_srvnamesiz(len);
1276 nd.ni_cnd.cn_cred = cred;
1277 nd.ni_cnd.cn_nameiop = CREATE;
1278 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE;
1281 * Call namei and do initial cleanup to get a few things
1282 * out of the way. If we get an initial error we cleanup
1283 * and return here to avoid special-casing the invalid nd
1284 * structure through the rest of the case. dirp may be
1285 * set even if an error occurs, but the nd structure will not
1286 * be valid at all if an error occurs so we have to invalidate it
1287 * prior to calling nfsm_reply ( which might goto nfsmout ).
1289 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
1290 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1291 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1297 nfsm_reply(NFSX_WCCDATA(v3));
1299 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1305 * No error. Continue. State:
1307 * startdir is valid ( we release this immediately )
1309 * nd.ni_vp may be valid
1310 * nd.ni_dvp is valid
1312 * The error state is set through the code and we may also do some
1313 * opportunistic releasing of vnodes to avoid holding locks through
1314 * NFS I/O. The cleanup at the end is a catch-all
1319 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1320 how = fxdr_unsigned(int, *tl);
1322 case NFSV3CREATE_GUARDED:
1328 case NFSV3CREATE_UNCHECKED:
1331 case NFSV3CREATE_EXCLUSIVE:
1332 tl = nfsm_dissect_nonblock(u_int32_t *,
1334 /* Unique bytes, endianness is not important. */
1335 cverf.tv_sec = (int32_t)tl[0];
1336 cverf.tv_nsec = tl[1];
1340 vap->va_type = VREG;
1342 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
1343 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1344 if (vap->va_type == VNON)
1345 vap->va_type = VREG;
1346 vap->va_mode = nfstov_mode(sp->sa_mode);
1347 switch (vap->va_type) {
1349 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1351 vap->va_size = (u_quad_t)tsize;
1356 rdev = fxdr_unsigned(long, sp->sa_size);
1364 * Iff doesn't exist, create it
1365 * otherwise just truncate to 0 length
1366 * should I set the mode too ?
1368 * The only possible error we can have at this point is EEXIST.
1369 * nd.ni_vp will also be non-NULL in that case.
1371 if (nd.ni_vp == NULL) {
1372 if (vap->va_mode == (mode_t)VNOVAL)
1374 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1375 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1377 NDFREE(&nd, NDF_ONLY_PNBUF);
1379 if (exclusive_flag) {
1382 vap->va_atime = cverf;
1383 error = VOP_SETATTR(nd.ni_vp, vap,
1387 } else if (vap->va_type == VCHR || vap->va_type == VBLK ||
1388 vap->va_type == VFIFO) {
1390 * NFSv2-specific code for creating device nodes
1393 * Handle SysV FIFO node special cases. All other
1394 * devices require super user to access.
1396 if (vap->va_type == VCHR && rdev == 0xffffffff)
1397 vap->va_type = VFIFO;
1398 if (vap->va_type != VFIFO &&
1399 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV,
1403 vap->va_rdev = rdev;
1404 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1406 NDFREE(&nd, NDF_ONLY_PNBUF);
1413 * release dvp prior to lookup
1420 * Even though LOCKPARENT was cleared, ni_dvp may
1423 nd.ni_cnd.cn_nameiop = LOOKUP;
1424 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1425 nd.ni_cnd.cn_thread = curthread;
1426 nd.ni_cnd.cn_cred = cred;
1427 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
1429 nd.ni_cnd.cn_flags |= GIANTHELD;
1430 error = lookup(&nd);
1432 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1433 nd.ni_cnd.cn_flags &= ~GIANTHELD;
1437 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1445 if (vap->va_size != -1) {
1446 error = nfsrv_access(nd.ni_vp, VWRITE,
1447 cred, (nd.ni_cnd.cn_flags & RDONLY), 0);
1449 tempsize = vap->va_size;
1451 vap->va_size = tempsize;
1452 error = VOP_SETATTR(nd.ni_vp, vap, cred);
1458 bzero((caddr_t)fhp, sizeof(nfh));
1459 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1460 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1462 error = VOP_GETATTR(nd.ni_vp, vap, cred);
1465 if (exclusive_flag && !error &&
1466 bcmp(&cverf, &vap->va_atime, sizeof (cverf)))
1468 if (dirp == nd.ni_dvp)
1469 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1471 /* Drop the other locks to avoid deadlock. */
1473 if (nd.ni_dvp == nd.ni_vp)
1483 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1484 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1485 VOP_UNLOCK(dirp, 0);
1489 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1492 nfsm_srvpostop_fh(fhp);
1493 nfsm_srvpostop_attr(0, vap);
1495 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1496 } else if (!error) {
1497 /* v2 non-error case. */
1498 nfsm_srvfhtom(fhp, v3);
1499 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1500 nfsm_srvfillattr(vap, fp);
1506 if (nd.ni_dvp == nd.ni_vp)
1513 if (nd.ni_startdir) {
1514 vrele(nd.ni_startdir);
1515 nd.ni_startdir = NULL;
1519 NDFREE(&nd, NDF_ONLY_PNBUF);
1520 vn_finished_write(mp);
1521 VFS_UNLOCK_GIANT(vfslocked);
1526 * nfs v3 mknod service
1529 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1532 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1533 struct sockaddr *nam = nfsd->nd_nam;
1534 caddr_t dpos = nfsd->nd_dpos;
1535 struct ucred *cred = nfsd->nd_cr;
1536 struct vattr va, dirfor, diraft;
1537 struct vattr *vap = &va;
1538 struct thread *td = curthread;
1540 struct nameidata nd;
1542 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1543 u_int32_t major, minor;
1545 struct mbuf *mb, *mreq;
1546 struct vnode *vp, *dirp = NULL;
1549 struct mount *mp = NULL;
1550 int v3 = (nfsd->nd_flag & ND_NFSV3);
1554 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1557 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1560 fhp = &nfh.fh_generic;
1562 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1566 vfslocked = VFS_LOCK_GIANT(mp);
1567 (void) vn_start_write(NULL, &mp, V_WAIT);
1568 vfs_rel(mp); /* The write holds a ref. */
1569 nfsm_srvnamesiz(len);
1571 nd.ni_cnd.cn_cred = cred;
1572 nd.ni_cnd.cn_nameiop = CREATE;
1573 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE;
1576 * Handle nfs_namei() call. If an error occurs, the nd structure
1577 * is not valid. However, nfsm_*() routines may still jump to
1581 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
1582 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1583 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1585 nfsm_reply(NFSX_WCCDATA(1));
1586 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1590 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1591 vtyp = nfsv3tov_type(*tl);
1592 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1593 error = NFSERR_BADTYPE;
1598 if (vtyp == VCHR || vtyp == VBLK) {
1599 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
1600 major = fxdr_unsigned(u_int32_t, *tl++);
1601 minor = fxdr_unsigned(u_int32_t, *tl);
1602 vap->va_rdev = makedev(major, minor);
1606 * Iff doesn't exist, create it.
1612 vap->va_type = vtyp;
1613 if (vap->va_mode == (mode_t)VNOVAL)
1615 if (vtyp == VSOCK) {
1616 vrele(nd.ni_startdir);
1617 nd.ni_startdir = NULL;
1618 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1620 NDFREE(&nd, NDF_ONLY_PNBUF);
1622 if (vtyp != VFIFO && (error = priv_check_cred(cred,
1623 PRIV_VFS_MKNOD_DEV, 0)))
1625 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1627 NDFREE(&nd, NDF_ONLY_PNBUF);
1634 * Release dvp prior to lookup
1639 nd.ni_cnd.cn_nameiop = LOOKUP;
1640 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1641 nd.ni_cnd.cn_thread = td;
1642 nd.ni_cnd.cn_cred = td->td_ucred;
1643 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
1645 nd.ni_cnd.cn_flags |= GIANTHELD;
1646 error = lookup(&nd);
1648 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1649 nd.ni_cnd.cn_flags &= ~GIANTHELD;
1653 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1658 * send response, cleanup, return.
1663 bzero((caddr_t)fhp, sizeof(nfh));
1664 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1665 error = VOP_VPTOFH(vp, &fhp->fh_fid);
1667 error = VOP_GETATTR(vp, vap, cred);
1670 if (nd.ni_dvp == nd.ni_vp)
1681 if (nd.ni_startdir) {
1682 vrele(nd.ni_startdir);
1683 nd.ni_startdir = NULL;
1685 NDFREE(&nd, NDF_ONLY_PNBUF);
1687 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1688 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1692 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1695 nfsm_srvpostop_fh(fhp);
1696 nfsm_srvpostop_attr(0, vap);
1698 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1700 vn_finished_write(mp);
1701 VFS_UNLOCK_GIANT(vfslocked);
1705 if (nd.ni_dvp == nd.ni_vp)
1715 vrele(nd.ni_startdir);
1716 NDFREE(&nd, NDF_ONLY_PNBUF);
1717 vn_finished_write(mp);
1718 VFS_UNLOCK_GIANT(vfslocked);
1723 * nfs remove service
1726 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1729 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1730 struct sockaddr *nam = nfsd->nd_nam;
1731 caddr_t dpos = nfsd->nd_dpos;
1732 struct ucred *cred = nfsd->nd_cr;
1733 struct nameidata nd;
1735 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1736 int v3 = (nfsd->nd_flag & ND_NFSV3);
1737 struct mbuf *mb, *mreq;
1739 struct vattr dirfor, diraft;
1742 struct mount *mp = NULL;
1745 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1749 fhp = &nfh.fh_generic;
1751 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1755 vfslocked = VFS_LOCK_GIANT(mp);
1756 (void) vn_start_write(NULL, &mp, V_WAIT);
1757 vfs_rel(mp); /* The write holds a ref. */
1758 nfsm_srvnamesiz(len);
1760 nd.ni_cnd.cn_cred = cred;
1761 nd.ni_cnd.cn_nameiop = DELETE;
1762 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | MPSAFE;
1763 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
1764 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1765 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1771 if (nd.ni_vp->v_type == VDIR) {
1772 error = EPERM; /* POSIX */
1776 * The root of a mounted filesystem cannot be deleted.
1778 if (nd.ni_vp->v_vflag & VV_ROOT) {
1784 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1785 NDFREE(&nd, NDF_ONLY_PNBUF);
1789 if (dirp == nd.ni_dvp)
1790 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1792 /* Drop the other locks to avoid deadlock. */
1794 if (nd.ni_dvp == nd.ni_vp)
1804 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1805 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1806 VOP_UNLOCK(dirp, 0);
1812 nfsm_reply(NFSX_WCCDATA(v3));
1814 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1818 NDFREE(&nd, NDF_ONLY_PNBUF);
1820 if (nd.ni_dvp == nd.ni_vp)
1827 vn_finished_write(mp);
1828 VFS_UNLOCK_GIANT(vfslocked);
1833 * nfs rename service
1836 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1839 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1840 struct sockaddr *nam = nfsd->nd_nam;
1841 caddr_t dpos = nfsd->nd_dpos;
1842 struct ucred *cred = nfsd->nd_cr;
1844 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
1845 int tdirfor_ret = 1, tdiraft_ret = 1;
1846 int v3 = (nfsd->nd_flag & ND_NFSV3);
1847 struct mbuf *mb, *mreq;
1848 struct nameidata fromnd, tond;
1849 struct vnode *fvp, *tvp, *tdvp, *fdirp = NULL;
1850 struct vnode *tdirp = NULL;
1851 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1853 fhandle_t *ffhp, *tfhp;
1855 struct mount *mp = NULL;
1858 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1863 ffhp = &fnfh.fh_generic;
1864 tfhp = &tnfh.fh_generic;
1867 * Clear fields incase goto nfsmout occurs from macro.
1873 nfsm_srvmtofh(ffhp);
1874 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
1878 vfslocked = VFS_LOCK_GIANT(mp);
1879 (void) vn_start_write(NULL, &mp, V_WAIT);
1880 vfs_rel(mp); /* The write holds a ref. */
1881 nfsm_srvnamesiz(len);
1883 * Remember our original uid so that we can reset cr_uid before
1884 * the second nfs_namei() call, in case it is remapped.
1886 saved_uid = cred->cr_uid;
1887 fromnd.ni_cnd.cn_cred = cred;
1888 fromnd.ni_cnd.cn_nameiop = DELETE;
1889 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART | MPSAFE;
1890 error = nfs_namei(&fromnd, nfsd, ffhp, len, slp, nam, &md,
1891 &dpos, &fdirp, v3, &fdirfor, &fdirfor_ret, FALSE);
1892 vfslocked = nfsrv_lockedpair_nd(vfslocked, &fromnd);
1898 nfsm_reply(2 * NFSX_WCCDATA(v3));
1900 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1901 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1907 nfsm_srvmtofh(tfhp);
1908 nfsm_srvnamesiz(len2);
1909 cred->cr_uid = saved_uid;
1910 tond.ni_cnd.cn_cred = cred;
1911 tond.ni_cnd.cn_nameiop = RENAME;
1912 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | MPSAFE;
1913 error = nfs_namei(&tond, nfsd, tfhp, len2, slp, nam, &md,
1914 &dpos, &tdirp, v3, &tdirfor, &tdirfor_ret, FALSE);
1915 vfslocked = nfsrv_lockedpair_nd(vfslocked, &tond);
1926 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1932 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1939 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1947 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1954 if (fvp->v_mount != tdvp->v_mount) {
1968 * If source is the same as the destination (that is the
1969 * same vnode with the same name in the same directory),
1970 * then there is nothing to do.
1972 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1973 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1974 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1975 fromnd.ni_cnd.cn_namelen))
1980 * The VOP_RENAME function releases all vnode references &
1981 * locks prior to returning so we need to clear the pointers
1982 * to bypass cleanup code later on.
1984 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1985 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1986 fromnd.ni_dvp = NULL;
1987 fromnd.ni_vp = NULL;
1991 NDFREE(&fromnd, NDF_ONLY_PNBUF);
1992 NDFREE(&tond, NDF_ONLY_PNBUF);
2000 nfsm_reply(2 * NFSX_WCCDATA(v3));
2002 /* Release existing locks to prevent deadlock. */
2004 if (tond.ni_dvp == tond.ni_vp)
2015 vn_lock(fdirp, LK_EXCLUSIVE | LK_RETRY);
2016 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
2017 VOP_UNLOCK(fdirp, 0);
2020 vn_lock(tdirp, LK_EXCLUSIVE | LK_RETRY);
2021 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
2022 VOP_UNLOCK(tdirp, 0);
2024 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2025 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2032 * Clear out tond related fields
2035 if (tond.ni_dvp == tond.ni_vp)
2044 if (tond.ni_startdir)
2045 vrele(tond.ni_startdir);
2046 NDFREE(&tond, NDF_ONLY_PNBUF);
2048 * Clear out fromnd related fields
2052 if (fromnd.ni_startdir)
2053 vrele(fromnd.ni_startdir);
2054 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2056 vrele(fromnd.ni_dvp);
2058 vrele(fromnd.ni_vp);
2060 vn_finished_write(mp);
2061 VFS_UNLOCK_GIANT(vfslocked);
2069 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2072 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2073 struct sockaddr *nam = nfsd->nd_nam;
2074 caddr_t dpos = nfsd->nd_dpos;
2075 struct ucred *cred = nfsd->nd_cr;
2076 struct nameidata nd;
2078 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2079 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2080 struct mbuf *mb, *mreq;
2081 struct vnode *vp = NULL, *xp, *dirp = NULL;
2082 struct vattr dirfor, diraft, at;
2084 fhandle_t *fhp, *dfhp;
2085 struct mount *mp = NULL;
2089 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2093 fhp = &nfh.fh_generic;
2094 dfhp = &dnfh.fh_generic;
2096 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2100 vfslocked = VFS_LOCK_GIANT(mp);
2101 (void) vn_start_write(NULL, &mp, V_WAIT);
2102 vfs_rel(mp); /* The write holds a ref. */
2103 nfsm_srvmtofh(dfhp);
2104 nfsm_srvnamesiz(len);
2106 error = nfsrv_fhtovp(fhp, TRUE, &vp, &tvfslocked, nfsd, slp,
2107 nam, &rdonly, TRUE);
2108 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
2110 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2112 nfsm_srvpostop_attr(getret, &at);
2113 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2120 getret = VOP_GETATTR(vp, &at, cred);
2121 if (vp->v_type == VDIR) {
2122 error = EPERM; /* POSIX */
2126 nd.ni_cnd.cn_cred = cred;
2127 nd.ni_cnd.cn_nameiop = CREATE;
2128 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE | MPSAFE;
2129 error = nfs_namei(&nd, nfsd, dfhp, len, slp, nam, &md, &dpos,
2130 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2131 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2149 if (vp->v_mount != xp->v_mount) {
2155 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2156 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2157 NDFREE(&nd, NDF_ONLY_PNBUF);
2162 getret = VOP_GETATTR(vp, &at, cred);
2165 if (dirp == nd.ni_dvp)
2166 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2168 /* Release existing locks to prevent deadlock. */
2170 if (nd.ni_dvp == nd.ni_vp)
2180 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2181 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2182 VOP_UNLOCK(dirp, 0);
2186 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2188 nfsm_srvpostop_attr(getret, &at);
2189 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2195 NDFREE(&nd, NDF_ONLY_PNBUF);
2199 if (nd.ni_dvp == nd.ni_vp)
2208 vn_finished_write(mp);
2209 VFS_UNLOCK_GIANT(vfslocked);
2214 * nfs symbolic link service
2217 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2220 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2221 struct sockaddr *nam = nfsd->nd_nam;
2222 caddr_t dpos = nfsd->nd_dpos;
2223 struct ucred *cred = nfsd->nd_cr;
2224 struct vattr va, dirfor, diraft;
2225 struct nameidata nd;
2226 struct vattr *vap = &va;
2227 struct nfsv2_sattr *sp;
2228 char *bpos, *pathcp = NULL;
2231 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2232 int v3 = (nfsd->nd_flag & ND_NFSV3);
2233 struct mbuf *mb, *mreq;
2234 struct vnode *dirp = NULL;
2237 struct mount *mp = NULL;
2241 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2245 fhp = &nfh.fh_generic;
2247 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2251 vfslocked = VFS_LOCK_GIANT(mp);
2252 (void) vn_start_write(NULL, &mp, V_WAIT);
2253 vfs_rel(mp); /* The write holds a ref. */
2254 nfsm_srvnamesiz(len);
2255 nd.ni_cnd.cn_cred = cred;
2256 nd.ni_cnd.cn_nameiop = CREATE;
2257 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART | MPSAFE;
2258 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2259 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2260 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2265 nfsm_srvpathsiz(len2);
2273 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2274 iv.iov_base = pathcp;
2276 io.uio_resid = len2;
2280 io.uio_segflg = UIO_SYSSPACE;
2281 io.uio_rw = UIO_READ;
2283 nfsm_mtouio(&io, len2);
2285 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
2286 vap->va_mode = nfstov_mode(sp->sa_mode);
2288 *(pathcp + len2) = '\0';
2295 * issue symlink op. SAVESTART is set so the underlying path component
2296 * is only freed by the VOP if an error occurs.
2298 if (vap->va_mode == (mode_t)VNOVAL)
2300 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2302 NDFREE(&nd, NDF_ONLY_PNBUF);
2307 * releases directory prior to potential lookup op.
2315 * Issue lookup. Leave SAVESTART set so we can easily free
2316 * the name buffer later on.
2318 * since LOCKPARENT is not set, ni_dvp will be garbage on
2319 * return whether an error occurs or not.
2321 nd.ni_cnd.cn_nameiop = LOOKUP;
2322 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2323 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2324 nd.ni_cnd.cn_thread = curthread;
2325 nd.ni_cnd.cn_cred = cred;
2326 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
2328 nd.ni_cnd.cn_flags |= GIANTHELD;
2329 error = lookup(&nd);
2331 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2332 nd.ni_cnd.cn_flags &= ~GIANTHELD;
2335 bzero((caddr_t)fhp, sizeof(nfh));
2336 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2337 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2339 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2347 * These releases aren't strictly required, does even doing them
2348 * make any sense? XXX can nfsm_reply() block?
2351 free(pathcp, M_TEMP);
2355 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2356 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2357 VOP_UNLOCK(dirp, 0);
2359 if (nd.ni_startdir) {
2360 vrele(nd.ni_startdir);
2361 nd.ni_startdir = NULL;
2363 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2366 nfsm_srvpostop_fh(fhp);
2367 nfsm_srvpostop_attr(0, vap);
2369 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2375 NDFREE(&nd, NDF_ONLY_PNBUF);
2377 if (nd.ni_dvp == nd.ni_vp)
2385 vrele(nd.ni_startdir);
2389 free(pathcp, M_TEMP);
2391 vn_finished_write(mp);
2392 VFS_UNLOCK_GIANT(vfslocked);
2400 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2403 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2404 struct sockaddr *nam = nfsd->nd_nam;
2405 caddr_t dpos = nfsd->nd_dpos;
2406 struct ucred *cred = nfsd->nd_cr;
2407 struct vattr va, dirfor, diraft;
2408 struct vattr *vap = &va;
2409 struct nfs_fattr *fp;
2410 struct nameidata nd;
2413 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2414 int v3 = (nfsd->nd_flag & ND_NFSV3);
2415 struct mbuf *mb, *mreq;
2416 struct vnode *dirp = NULL;
2420 struct mount *mp = NULL;
2423 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2427 fhp = &nfh.fh_generic;
2429 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2433 vfslocked = VFS_LOCK_GIANT(mp);
2434 (void) vn_start_write(NULL, &mp, V_WAIT);
2435 vfs_rel(mp); /* The write holds a ref. */
2436 nfsm_srvnamesiz(len);
2437 nd.ni_cnd.cn_cred = cred;
2438 nd.ni_cnd.cn_nameiop = CREATE;
2439 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE;
2441 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2442 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2443 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2449 nfsm_reply(NFSX_WCCDATA(v3));
2451 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2459 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
2460 vap->va_mode = nfstov_mode(*tl++);
2464 * At this point nd.ni_dvp is referenced and exclusively locked and
2465 * nd.ni_vp, if it exists, is referenced but not locked.
2468 vap->va_type = VDIR;
2469 if (nd.ni_vp != NULL) {
2470 NDFREE(&nd, NDF_ONLY_PNBUF);
2476 * Issue mkdir op. Since SAVESTART is not set, the pathname
2477 * component is freed by the VOP call. This will fill-in
2478 * nd.ni_vp, reference, and exclusively lock it.
2480 if (vap->va_mode == (mode_t)VNOVAL)
2482 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2483 NDFREE(&nd, NDF_ONLY_PNBUF);
2490 bzero((caddr_t)fhp, sizeof(nfh));
2491 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2492 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2494 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2498 if (dirp == nd.ni_dvp) {
2499 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2501 /* Release existing locks to prevent deadlock. */
2503 NDFREE(&nd, NDF_ONLY_PNBUF);
2504 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2517 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2518 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2519 VOP_UNLOCK(dirp, 0);
2522 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2525 nfsm_srvpostop_fh(fhp);
2526 nfsm_srvpostop_attr(0, vap);
2528 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2529 } else if (!error) {
2530 /* v2 non-error case. */
2531 nfsm_srvfhtom(fhp, v3);
2532 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2533 nfsm_srvfillattr(vap, fp);
2540 NDFREE(&nd, NDF_ONLY_PNBUF);
2541 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2554 vn_finished_write(mp);
2555 VFS_UNLOCK_GIANT(vfslocked);
2563 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2566 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2567 struct sockaddr *nam = nfsd->nd_nam;
2568 caddr_t dpos = nfsd->nd_dpos;
2569 struct ucred *cred = nfsd->nd_cr;
2571 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2572 int v3 = (nfsd->nd_flag & ND_NFSV3);
2573 struct mbuf *mb, *mreq;
2574 struct vnode *vp, *dirp = NULL;
2575 struct vattr dirfor, diraft;
2578 struct nameidata nd;
2579 struct mount *mp = NULL;
2582 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2586 fhp = &nfh.fh_generic;
2588 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2592 vfslocked = VFS_LOCK_GIANT(mp);
2593 (void) vn_start_write(NULL, &mp, V_WAIT);
2594 vfs_rel(mp); /* The write holds a ref. */
2595 nfsm_srvnamesiz(len);
2596 nd.ni_cnd.cn_cred = cred;
2597 nd.ni_cnd.cn_nameiop = DELETE;
2598 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | MPSAFE;
2599 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2600 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2601 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2607 nfsm_reply(NFSX_WCCDATA(v3));
2609 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2614 if (vp->v_type != VDIR) {
2619 * No rmdir "." please.
2621 if (nd.ni_dvp == vp) {
2626 * The root of a mounted filesystem cannot be deleted.
2628 if (vp->v_vflag & VV_ROOT)
2632 * Issue or abort op. Since SAVESTART is not set, path name
2633 * component is freed by the VOP after either.
2636 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2637 NDFREE(&nd, NDF_ONLY_PNBUF);
2640 if (dirp == nd.ni_dvp)
2641 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2643 /* Release existing locks to prevent deadlock. */
2645 if (nd.ni_dvp == nd.ni_vp)
2654 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2655 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2656 VOP_UNLOCK(dirp, 0);
2659 nfsm_reply(NFSX_WCCDATA(v3));
2662 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2666 NDFREE(&nd, NDF_ONLY_PNBUF);
2668 if (nd.ni_dvp == nd.ni_vp)
2678 vn_finished_write(mp);
2679 VFS_UNLOCK_GIANT(vfslocked);
2684 * nfs readdir service
2685 * - mallocs what it thinks is enough to read
2686 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2687 * - calls VOP_READDIR()
2688 * - loops around building the reply
2689 * if the output generated exceeds count break out of loop
2690 * The nfsm_clget macro is used here so that the reply will be packed
2691 * tightly in mbuf clusters.
2692 * - it only knows that it has encountered eof when the VOP_READDIR()
2694 * - as such one readdir rpc will return eof false although you are there
2695 * and then the next will return eof
2696 * - it trims out records with d_fileno == 0
2697 * this doesn't matter for Unix clients, but they might confuse clients
2699 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2700 * than requested, but this may not apply to all filesystems. For
2701 * example, client NFS does not { although it is never remote mounted
2703 * The alternate call nfsrv_readdirplus() does lookups as well.
2704 * PS: The NFS protocol spec. does not clarify what the "count" byte
2705 * argument is a count of.. just name strings and file id's or the
2706 * entire reply rpc or ...
2707 * I tried just file name and id sizes and it confused the Sun client,
2708 * so I am using the full rpc size now. The "paranoia.." comment refers
2709 * to including the status longwords that are not a part of the dir.
2710 * "entry" structures, but are in the rpc.
2714 u_int32_t fl_postopok;
2715 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2717 u_int32_t fl_fhsize;
2718 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2722 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2725 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2726 struct sockaddr *nam = nfsd->nd_nam;
2727 caddr_t dpos = nfsd->nd_dpos;
2728 struct ucred *cred = nfsd->nd_cr;
2735 struct mbuf *mb, *mreq;
2736 char *cpos, *cend, *rbuf;
2737 struct vnode *vp = NULL;
2743 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2744 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2745 int v3 = (nfsd->nd_flag & ND_NFSV3);
2746 u_quad_t off, toff, verf;
2747 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2750 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2752 fhp = &nfh.fh_generic;
2755 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
2756 toff = fxdr_hyper(tl);
2758 verf = fxdr_hyper(tl);
2761 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
2762 toff = fxdr_unsigned(u_quad_t, *tl++);
2763 verf = 0; /* shut up gcc */
2766 cnt = fxdr_unsigned(int, *tl);
2767 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2768 xfer = NFS_SRVMAXDATA(nfsd);
2774 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
2775 nam, &rdonly, TRUE);
2776 if (!error && vp->v_type != VDIR) {
2782 nfsm_reply(NFSX_UNSIGNED);
2784 nfsm_srvpostop_attr(getret, &at);
2790 * Obtain lock on vnode for this section of the code
2793 error = getret = VOP_GETATTR(vp, &at, cred);
2796 * XXX This check may be too strict for Solaris 2.5 clients.
2798 if (!error && toff && verf && verf != at.va_filerev)
2799 error = NFSERR_BAD_COOKIE;
2803 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
2807 nfsm_reply(NFSX_POSTOPATTR(v3));
2809 nfsm_srvpostop_attr(getret, &at);
2816 * end section. Allocate rbuf and continue
2818 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2821 iv.iov_len = fullsiz;
2824 io.uio_offset = (off_t)off;
2825 io.uio_resid = fullsiz;
2826 io.uio_segflg = UIO_SYSSPACE;
2827 io.uio_rw = UIO_READ;
2830 vn_lock(vp, LK_SHARED | LK_RETRY);
2832 free((caddr_t)cookies, M_TEMP);
2835 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2836 off = (off_t)io.uio_offset;
2837 if (!cookies && !error)
2838 error = NFSERR_PERM;
2840 getret = VOP_GETATTR(vp, &at, cred);
2848 free((caddr_t)rbuf, M_TEMP);
2850 free((caddr_t)cookies, M_TEMP);
2851 nfsm_reply(NFSX_POSTOPATTR(v3));
2853 nfsm_srvpostop_attr(getret, &at);
2858 siz -= io.uio_resid;
2861 * If nothing read, return eof
2867 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2870 nfsm_srvpostop_attr(getret, &at);
2871 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
2872 txdr_hyper(at.va_filerev, tl);
2875 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2876 *tl++ = nfsrv_nfs_false;
2877 *tl = nfsrv_nfs_true;
2878 free((caddr_t)rbuf, M_TEMP);
2879 free((caddr_t)cookies, M_TEMP);
2886 * Check for degenerate cases of nothing useful read.
2887 * If so go try again
2891 dp = (struct dirent *)cpos;
2894 * For some reason FreeBSD's ufs_readdir() chooses to back the
2895 * directory offset up to a block boundary, so it is necessary to
2896 * skip over the records that precede the requested offset. This
2897 * requires the assumption that file offset cookies monotonically
2900 while (cpos < cend && ncookies > 0 &&
2901 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2902 ((u_quad_t)(*cookiep)) <= toff)) {
2903 cpos += dp->d_reclen;
2904 dp = (struct dirent *)cpos;
2908 if (cpos >= cend || ncookies == 0) {
2914 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2915 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2917 nfsm_srvpostop_attr(getret, &at);
2918 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2919 txdr_hyper(at.va_filerev, tl);
2923 be = bp + M_TRAILINGSPACE(mp);
2925 /* Loop through the records and build reply */
2926 while (cpos < cend && ncookies > 0) {
2927 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2928 nlen = dp->d_namlen;
2929 rem = nfsm_rndup(nlen) - nlen;
2930 len += (4 * NFSX_UNSIGNED + nlen + rem);
2932 len += 2 * NFSX_UNSIGNED;
2938 * Build the directory record xdr from
2942 *tl = nfsrv_nfs_true;
2943 bp += NFSX_UNSIGNED;
2947 bp += NFSX_UNSIGNED;
2950 *tl = txdr_unsigned(dp->d_fileno);
2951 bp += NFSX_UNSIGNED;
2953 *tl = txdr_unsigned(nlen);
2954 bp += NFSX_UNSIGNED;
2956 /* And loop around copying the name */
2965 bcopy(cp, bp, tsiz);
2971 /* And null pad to an int32_t boundary. */
2972 for (i = 0; i < rem; i++)
2976 /* Finish off the record */
2979 bp += NFSX_UNSIGNED;
2982 *tl = txdr_unsigned(*cookiep);
2983 bp += NFSX_UNSIGNED;
2985 cpos += dp->d_reclen;
2986 dp = (struct dirent *)cpos;
2993 *tl = nfsrv_nfs_false;
2994 bp += NFSX_UNSIGNED;
2997 *tl = nfsrv_nfs_true;
2999 *tl = nfsrv_nfs_false;
3000 bp += NFSX_UNSIGNED;
3003 mp->m_len = bp - mtod(mp, caddr_t);
3005 mp->m_len += bp - bpos;
3006 free((caddr_t)rbuf, M_TEMP);
3007 free((caddr_t)cookies, M_TEMP);
3012 VFS_UNLOCK_GIANT(vfslocked);
3017 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3020 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3021 struct sockaddr *nam = nfsd->nd_nam;
3022 caddr_t dpos = nfsd->nd_dpos;
3023 struct ucred *cred = nfsd->nd_cr;
3030 struct mbuf *mb, *mreq;
3031 char *cpos, *cend, *rbuf;
3032 struct vnode *vp = NULL, *nvp;
3035 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3038 struct vattr va, at, *vap = &va;
3039 struct nfs_fattr *fp;
3040 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3041 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3042 u_quad_t off, toff, verf;
3043 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3044 int v3 = (nfsd->nd_flag & ND_NFSV3);
3047 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3050 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3051 fhp = &nfh.fh_generic;
3053 tl = nfsm_dissect_nonblock(u_int32_t *, 6 * NFSX_UNSIGNED);
3054 toff = fxdr_hyper(tl);
3056 verf = fxdr_hyper(tl);
3058 siz = fxdr_unsigned(int, *tl++);
3059 cnt = fxdr_unsigned(int, *tl);
3061 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3062 xfer = NFS_SRVMAXDATA(nfsd);
3068 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3069 nam, &rdonly, TRUE);
3070 if (!error && vp->v_type != VDIR) {
3076 nfsm_reply(NFSX_UNSIGNED);
3077 nfsm_srvpostop_attr(getret, &at);
3081 error = getret = VOP_GETATTR(vp, &at, cred);
3084 * XXX This check may be too strict for Solaris 2.5 clients.
3086 if (!error && toff && verf && verf != at.va_filerev)
3087 error = NFSERR_BAD_COOKIE;
3090 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
3094 nfsm_reply(NFSX_V3POSTOPATTR);
3095 nfsm_srvpostop_attr(getret, &at);
3100 rbuf = malloc(siz, M_TEMP, M_WAITOK);
3103 iv.iov_len = fullsiz;
3106 io.uio_offset = (off_t)off;
3107 io.uio_resid = fullsiz;
3108 io.uio_segflg = UIO_SYSSPACE;
3109 io.uio_rw = UIO_READ;
3112 vn_lock(vp, LK_SHARED | LK_RETRY);
3114 free((caddr_t)cookies, M_TEMP);
3117 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3118 off = (u_quad_t)io.uio_offset;
3119 getret = VOP_GETATTR(vp, &at, cred);
3121 if (!cookies && !error)
3122 error = NFSERR_PERM;
3129 free((caddr_t)cookies, M_TEMP);
3130 free((caddr_t)rbuf, M_TEMP);
3131 nfsm_reply(NFSX_V3POSTOPATTR);
3132 nfsm_srvpostop_attr(getret, &at);
3137 siz -= io.uio_resid;
3140 * If nothing read, return eof
3146 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3148 nfsm_srvpostop_attr(getret, &at);
3149 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3150 txdr_hyper(at.va_filerev, tl);
3152 *tl++ = nfsrv_nfs_false;
3153 *tl = nfsrv_nfs_true;
3154 free((caddr_t)cookies, M_TEMP);
3155 free((caddr_t)rbuf, M_TEMP);
3162 * Check for degenerate cases of nothing useful read.
3163 * If so go try again
3167 dp = (struct dirent *)cpos;
3170 * For some reason FreeBSD's ufs_readdir() chooses to back the
3171 * directory offset up to a block boundary, so it is necessary to
3172 * skip over the records that precede the requested offset. This
3173 * requires the assumption that file offset cookies monotonically
3176 while (cpos < cend && ncookies > 0 &&
3177 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3178 ((u_quad_t)(*cookiep)) <= toff)) {
3179 cpos += dp->d_reclen;
3180 dp = (struct dirent *)cpos;
3184 if (cpos >= cend || ncookies == 0) {
3191 * Probe one of the directory entries to see if the filesystem
3194 error = VFS_VGET(vp->v_mount, dp->d_fileno, LK_EXCLUSIVE, &nvp);
3196 if (error == EOPNOTSUPP)
3197 error = NFSERR_NOTSUPP;
3199 error = NFSERR_SERVERFAULT;
3202 free((caddr_t)cookies, M_TEMP);
3203 free((caddr_t)rbuf, M_TEMP);
3204 nfsm_reply(NFSX_V3POSTOPATTR);
3205 nfsm_srvpostop_attr(getret, &at);
3212 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3215 nfsm_srvpostop_attr(getret, &at);
3216 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3217 txdr_hyper(at.va_filerev, tl);
3220 be = bp + M_TRAILINGSPACE(mp);
3222 /* Loop through the records and build reply */
3223 while (cpos < cend && ncookies > 0) {
3224 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3225 nlen = dp->d_namlen;
3226 rem = nfsm_rndup(nlen)-nlen;
3229 * For readdir_and_lookup get the vnode using
3232 if (VFS_VGET(vp->v_mount, dp->d_fileno, LK_EXCLUSIVE,
3235 bzero((caddr_t)nfhp, NFSX_V3FH);
3237 nvp->v_mount->mnt_stat.f_fsid;
3239 * XXXRW: Assert the mountpoints are the same so that
3240 * we know that acquiring Giant based on the
3241 * directory is the right thing for the child.
3243 KASSERT(nvp->v_mount == vp->v_mount,
3244 ("nfsrv_readdirplus: nvp mount != vp mount"));
3245 if (VOP_VPTOFH(nvp, &nfhp->fh_fid)) {
3250 if (VOP_GETATTR(nvp, vap, cred)) {
3259 * If either the dircount or maxcount will be
3260 * exceeded, get out now. Both of these lengths
3261 * are calculated conservatively, including all
3264 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3266 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3267 if (len > cnt || dirlen > fullsiz) {
3273 * Build the directory record xdr from
3276 fp = (struct nfs_fattr *)&fl.fl_fattr;
3277 nfsm_srvfillattr(vap, fp);
3278 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3279 fl.fl_fhok = nfsrv_nfs_true;
3280 fl.fl_postopok = nfsrv_nfs_true;
3281 fl.fl_off.nfsuquad[0] = 0;
3282 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3285 *tl = nfsrv_nfs_true;
3286 bp += NFSX_UNSIGNED;
3289 bp += NFSX_UNSIGNED;
3291 *tl = txdr_unsigned(dp->d_fileno);
3292 bp += NFSX_UNSIGNED;
3294 *tl = txdr_unsigned(nlen);
3295 bp += NFSX_UNSIGNED;
3297 /* And loop around copying the name */
3302 if ((bp + xfer) > be)
3306 bcopy(cp, bp, tsiz);
3312 /* And null pad to an int32_t boundary. */
3313 for (i = 0; i < rem; i++)
3317 * Now copy the flrep structure out.
3319 xfer = sizeof (struct flrep);
3323 if ((bp + xfer) > be)
3327 bcopy(cp, bp, tsiz);
3335 cpos += dp->d_reclen;
3336 dp = (struct dirent *)cpos;
3343 *tl = nfsrv_nfs_false;
3344 bp += NFSX_UNSIGNED;
3347 *tl = nfsrv_nfs_true;
3349 *tl = nfsrv_nfs_false;
3350 bp += NFSX_UNSIGNED;
3353 mp->m_len = bp - mtod(mp, caddr_t);
3355 mp->m_len += bp - bpos;
3356 free((caddr_t)cookies, M_TEMP);
3357 free((caddr_t)rbuf, M_TEMP);
3361 VFS_UNLOCK_GIANT(vfslocked);
3366 * nfs commit service
3369 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3372 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3373 struct sockaddr *nam = nfsd->nd_nam;
3374 caddr_t dpos = nfsd->nd_dpos;
3375 struct ucred *cred = nfsd->nd_cr;
3376 struct vattr bfor, aft;
3377 struct vnode *vp = NULL;
3382 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3383 struct mbuf *mb, *mreq;
3385 struct mount *mp = NULL;
3386 int v3 = (nfsd->nd_flag & ND_NFSV3);
3390 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3393 panic("nfsrv_commit: v3 proc called on a v2 connection");
3394 fhp = &nfh.fh_generic;
3396 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3400 vfslocked = VFS_LOCK_GIANT(mp);
3401 (void) vn_start_write(NULL, &mp, V_WAIT);
3402 vfs_rel(mp); /* The write holds a ref. */
3403 tl = nfsm_dissect_nonblock(u_int32_t *, 3 * NFSX_UNSIGNED);
3406 * XXX At this time VOP_FSYNC() does not accept offset and byte
3407 * count parameters, so these arguments are useless (someday maybe).
3409 off = fxdr_hyper(tl);
3411 cnt = fxdr_unsigned(int, *tl);
3412 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, nfsd, slp,
3413 nam, &rdonly, TRUE);
3414 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
3416 nfsm_reply(2 * NFSX_UNSIGNED);
3417 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3421 for_ret = VOP_GETATTR(vp, &bfor, cred);
3423 if (cnt > MAX_COMMIT_COUNT) {
3425 * Give up and do the whole thing
3428 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3429 VM_OBJECT_LOCK(vp->v_object);
3430 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3431 VM_OBJECT_UNLOCK(vp->v_object);
3433 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3436 * Locate and synchronously write any buffers that fall
3437 * into the requested range. Note: we are assuming that
3438 * f_iosize is a power of 2.
3440 int iosize = vp->v_mount->mnt_stat.f_iosize;
3441 int iomask = iosize - 1;
3446 * Align to iosize boundry, super-align to page boundry.
3449 cnt += off & iomask;
3450 off &= ~(u_quad_t)iomask;
3452 if (off & PAGE_MASK) {
3453 cnt += off & PAGE_MASK;
3454 off &= ~(u_quad_t)PAGE_MASK;
3456 lblkno = off / iosize;
3459 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3460 VM_OBJECT_LOCK(vp->v_object);
3461 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3462 VM_OBJECT_UNLOCK(vp->v_object);
3471 * If we have a buffer and it is marked B_DELWRI we
3472 * have to lock and write it. Otherwise the prior
3473 * write is assumed to have already been committed.
3475 * gbincore() can return invalid buffers now so we
3476 * have to check that bit as well (though B_DELWRI
3477 * should not be set if B_INVAL is set there could be
3478 * a race here since we haven't locked the buffer).
3480 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
3481 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
3482 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
3484 continue; /* retry */
3486 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
3489 bp->b_flags &= ~B_ASYNC;
3505 aft_ret = VOP_GETATTR(vp, &aft, cred);
3509 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3510 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3512 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3513 if (nfsver.tv_sec == 0)
3515 *tl++ = txdr_unsigned(nfsver.tv_sec);
3516 *tl = txdr_unsigned(nfsver.tv_usec);
3523 vn_finished_write(mp);
3524 VFS_UNLOCK_GIANT(vfslocked);
3529 * nfs statfs service
3532 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3535 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3536 struct sockaddr *nam = nfsd->nd_nam;
3537 caddr_t dpos = nfsd->nd_dpos;
3538 struct ucred *cred = nfsd->nd_cr;
3540 struct nfs_statfs *sfp;
3542 int error = 0, rdonly, getret = 1;
3543 int v3 = (nfsd->nd_flag & ND_NFSV3);
3544 struct mbuf *mb, *mreq;
3545 struct vnode *vp = NULL;
3549 struct statfs statfs;
3553 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3555 fhp = &nfh.fh_generic;
3557 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3558 nam, &rdonly, TRUE);
3560 nfsm_reply(NFSX_UNSIGNED);
3562 nfsm_srvpostop_attr(getret, &at);
3567 error = VFS_STATFS(vp->v_mount, sf);
3568 getret = VOP_GETATTR(vp, &at, cred);
3571 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3573 nfsm_srvpostop_attr(getret, &at);
3578 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3580 tval = (u_quad_t)sf->f_blocks;
3581 tval *= (u_quad_t)sf->f_bsize;
3582 txdr_hyper(tval, &sfp->sf_tbytes);
3583 tval = (u_quad_t)sf->f_bfree;
3584 tval *= (u_quad_t)sf->f_bsize;
3585 txdr_hyper(tval, &sfp->sf_fbytes);
3587 * Don't send negative values for available space,
3588 * since this field is unsigned in the NFS protocol.
3589 * Otherwise, the client would see absurdly high
3590 * numbers for free space.
3592 if (sf->f_bavail < 0)
3595 tval = (u_quad_t)sf->f_bavail;
3596 tval *= (u_quad_t)sf->f_bsize;
3597 txdr_hyper(tval, &sfp->sf_abytes);
3598 sfp->sf_tfiles.nfsuquad[0] = 0;
3599 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3600 sfp->sf_ffiles.nfsuquad[0] = 0;
3601 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3602 sfp->sf_afiles.nfsuquad[0] = 0;
3603 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3604 sfp->sf_invarsec = 0;
3606 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3607 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3608 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3609 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3610 if (sf->f_bavail < 0)
3613 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3618 VFS_UNLOCK_GIANT(vfslocked);
3623 * nfs fsinfo service
3626 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3629 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3630 struct sockaddr *nam = nfsd->nd_nam;
3631 caddr_t dpos = nfsd->nd_dpos;
3632 struct ucred *cred = nfsd->nd_cr;
3633 struct nfsv3_fsinfo *sip;
3635 int error = 0, rdonly, getret = 1, pref;
3636 struct mbuf *mb, *mreq;
3637 struct vnode *vp = NULL;
3643 int v3 = (nfsd->nd_flag & ND_NFSV3);
3646 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3648 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3649 fhp = &nfh.fh_generic;
3652 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3653 nam, &rdonly, TRUE);
3655 nfsm_reply(NFSX_UNSIGNED);
3656 nfsm_srvpostop_attr(getret, &at);
3661 /* XXX Try to make a guess on the max file size. */
3662 VFS_STATFS(vp->v_mount, &sb);
3663 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3665 getret = VOP_GETATTR(vp, &at, cred);
3668 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3669 nfsm_srvpostop_attr(getret, &at);
3670 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3674 * There should be filesystem VFS OP(s) to get this information.
3675 * For now, assume ufs.
3677 pref = NFS_SRVMAXDATA(nfsd);
3678 sip->fs_rtmax = txdr_unsigned(pref);
3679 sip->fs_rtpref = txdr_unsigned(pref);
3680 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3681 sip->fs_wtmax = txdr_unsigned(pref);
3682 sip->fs_wtpref = txdr_unsigned(pref);
3683 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3684 sip->fs_dtpref = txdr_unsigned(pref);
3685 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3686 sip->fs_timedelta.nfsv3_sec = 0;
3687 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3688 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3689 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3690 NFSV3FSINFO_CANSETTIME);
3694 VFS_UNLOCK_GIANT(vfslocked);
3699 * nfs pathconf service
3702 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3705 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3706 struct sockaddr *nam = nfsd->nd_nam;
3707 caddr_t dpos = nfsd->nd_dpos;
3708 struct ucred *cred = nfsd->nd_cr;
3709 struct nfsv3_pathconf *pc;
3711 int error = 0, rdonly, getret = 1;
3712 register_t linkmax, namemax, chownres, notrunc;
3713 struct mbuf *mb, *mreq;
3714 struct vnode *vp = NULL;
3718 int v3 = (nfsd->nd_flag & ND_NFSV3);
3721 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3723 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3725 fhp = &nfh.fh_generic;
3727 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3728 nam, &rdonly, TRUE);
3730 nfsm_reply(NFSX_UNSIGNED);
3731 nfsm_srvpostop_attr(getret, &at);
3735 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3737 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3739 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3741 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3742 getret = VOP_GETATTR(vp, &at, cred);
3745 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3746 nfsm_srvpostop_attr(getret, &at);
3751 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3753 pc->pc_linkmax = txdr_unsigned(linkmax);
3754 pc->pc_namemax = txdr_unsigned(namemax);
3755 pc->pc_notrunc = txdr_unsigned(notrunc);
3756 pc->pc_chownrestricted = txdr_unsigned(chownres);
3759 * These should probably be supported by VOP_PATHCONF(), but
3760 * until msdosfs is exportable (why would you want to?), the
3761 * Unix defaults should be ok.
3763 pc->pc_caseinsensitive = nfsrv_nfs_false;
3764 pc->pc_casepreserving = nfsrv_nfs_true;
3768 VFS_UNLOCK_GIANT(vfslocked);
3773 * Null operation, used by clients to ping server
3777 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3780 struct mbuf *mrep = nfsd->nd_mrep;
3782 int error = NFSERR_RETVOID;
3783 struct mbuf *mb, *mreq;
3785 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3792 * No operation, used for obsolete procedures
3796 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3799 struct mbuf *mrep = nfsd->nd_mrep;
3802 struct mbuf *mb, *mreq;
3804 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3805 if (nfsd->nd_repstat)
3806 error = nfsd->nd_repstat;
3808 error = EPROCUNAVAIL;
3816 * Perform access checking for vnodes obtained from file handles that would
3817 * refer to files already opened by a Unix client. You cannot just use
3818 * vn_writechk() and VOP_ACCESS() for two reasons.
3819 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
3821 * 2 - The owner is to be given access irrespective of mode bits for some
3822 * operations, so that processes that chmod after opening a file don't
3823 * break. I don't like this because it opens a security hole, but since
3824 * the nfs server opens a security hole the size of a barn door anyhow,
3827 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3828 * will return EPERM instead of EACCES. EPERM is always an error.
3831 nfsrv_access(struct vnode *vp, accmode_t accmode, struct ucred *cred,
3832 int rdonly, int override)
3837 VFS_ASSERT_GIANT(vp->v_mount);
3839 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3841 if (accmode & VWRITE) {
3842 /* Just vn_writechk() changed to check rdonly */
3844 * Disallow write attempts on read-only filesystems;
3845 * unless the file is a socket or a block or character
3846 * device resident on the filesystem.
3848 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3849 switch (vp->v_type) {
3859 * If there's shared text associated with
3860 * the inode, we can't allow writing.
3862 if (vp->v_vflag & VV_TEXT)
3866 error = VOP_GETATTR(vp, &vattr, cred);
3869 error = VOP_ACCESS(vp, accmode, cred, curthread);
3871 * Allow certain operations for the owner (reads and writes
3872 * on files that are already open).
3874 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)