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);
1817 NDFREE(&nd, NDF_ONLY_PNBUF);
1819 if (nd.ni_dvp == nd.ni_vp)
1826 vn_finished_write(mp);
1827 VFS_UNLOCK_GIANT(vfslocked);
1832 * nfs rename service
1835 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1838 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1839 struct sockaddr *nam = nfsd->nd_nam;
1840 caddr_t dpos = nfsd->nd_dpos;
1841 struct ucred *cred = nfsd->nd_cr;
1843 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
1844 int tdirfor_ret = 1, tdiraft_ret = 1;
1845 int v3 = (nfsd->nd_flag & ND_NFSV3);
1846 struct mbuf *mb, *mreq;
1847 struct nameidata fromnd, tond;
1848 struct vnode *fvp, *tvp, *tdvp, *fdirp = NULL;
1849 struct vnode *tdirp = NULL;
1850 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1852 fhandle_t *ffhp, *tfhp;
1854 struct mount *mp = NULL;
1857 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1862 ffhp = &fnfh.fh_generic;
1863 tfhp = &tnfh.fh_generic;
1866 * Clear fields incase goto nfsmout occurs from macro.
1872 nfsm_srvmtofh(ffhp);
1873 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
1877 vfslocked = VFS_LOCK_GIANT(mp);
1878 (void) vn_start_write(NULL, &mp, V_WAIT);
1879 vfs_rel(mp); /* The write holds a ref. */
1880 nfsm_srvnamesiz(len);
1882 * Remember our original uid so that we can reset cr_uid before
1883 * the second nfs_namei() call, in case it is remapped.
1885 saved_uid = cred->cr_uid;
1886 fromnd.ni_cnd.cn_cred = cred;
1887 fromnd.ni_cnd.cn_nameiop = DELETE;
1888 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART | MPSAFE;
1889 error = nfs_namei(&fromnd, nfsd, ffhp, len, slp, nam, &md,
1890 &dpos, &fdirp, v3, &fdirfor, &fdirfor_ret, FALSE);
1891 vfslocked = nfsrv_lockedpair_nd(vfslocked, &fromnd);
1897 nfsm_reply(2 * NFSX_WCCDATA(v3));
1899 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1900 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1906 nfsm_srvmtofh(tfhp);
1907 nfsm_srvnamesiz(len2);
1908 cred->cr_uid = saved_uid;
1909 tond.ni_cnd.cn_cred = cred;
1910 tond.ni_cnd.cn_nameiop = RENAME;
1911 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | MPSAFE;
1912 error = nfs_namei(&tond, nfsd, tfhp, len2, slp, nam, &md,
1913 &dpos, &tdirp, v3, &tdirfor, &tdirfor_ret, FALSE);
1914 vfslocked = nfsrv_lockedpair_nd(vfslocked, &tond);
1925 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1931 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1938 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1946 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1953 if (fvp->v_mount != tdvp->v_mount) {
1967 * If source is the same as the destination (that is the
1968 * same vnode with the same name in the same directory),
1969 * then there is nothing to do.
1971 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1972 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1973 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1974 fromnd.ni_cnd.cn_namelen))
1979 * The VOP_RENAME function releases all vnode references &
1980 * locks prior to returning so we need to clear the pointers
1981 * to bypass cleanup code later on.
1983 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1984 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1985 fromnd.ni_dvp = NULL;
1986 fromnd.ni_vp = NULL;
1990 NDFREE(&fromnd, NDF_ONLY_PNBUF);
1991 NDFREE(&tond, NDF_ONLY_PNBUF);
1999 nfsm_reply(2 * NFSX_WCCDATA(v3));
2001 /* Release existing locks to prevent deadlock. */
2003 if (tond.ni_dvp == tond.ni_vp)
2014 vn_lock(fdirp, LK_EXCLUSIVE | LK_RETRY);
2015 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
2016 VOP_UNLOCK(fdirp, 0);
2019 vn_lock(tdirp, LK_EXCLUSIVE | LK_RETRY);
2020 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
2021 VOP_UNLOCK(tdirp, 0);
2023 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2024 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2031 * Clear out tond related fields
2034 if (tond.ni_dvp == tond.ni_vp)
2043 if (tond.ni_startdir)
2044 vrele(tond.ni_startdir);
2045 NDFREE(&tond, NDF_ONLY_PNBUF);
2047 * Clear out fromnd related fields
2051 if (fromnd.ni_startdir)
2052 vrele(fromnd.ni_startdir);
2053 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2055 vrele(fromnd.ni_dvp);
2057 vrele(fromnd.ni_vp);
2059 vn_finished_write(mp);
2060 VFS_UNLOCK_GIANT(vfslocked);
2068 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2071 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2072 struct sockaddr *nam = nfsd->nd_nam;
2073 caddr_t dpos = nfsd->nd_dpos;
2074 struct ucred *cred = nfsd->nd_cr;
2075 struct nameidata nd;
2077 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2078 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2079 struct mbuf *mb, *mreq;
2080 struct vnode *vp = NULL, *xp, *dirp = NULL;
2081 struct vattr dirfor, diraft, at;
2083 fhandle_t *fhp, *dfhp;
2084 struct mount *mp = NULL;
2088 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2092 fhp = &nfh.fh_generic;
2093 dfhp = &dnfh.fh_generic;
2095 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2099 vfslocked = VFS_LOCK_GIANT(mp);
2100 (void) vn_start_write(NULL, &mp, V_WAIT);
2101 vfs_rel(mp); /* The write holds a ref. */
2102 nfsm_srvmtofh(dfhp);
2103 nfsm_srvnamesiz(len);
2105 error = nfsrv_fhtovp(fhp, TRUE, &vp, &tvfslocked, nfsd, slp,
2106 nam, &rdonly, TRUE);
2107 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
2109 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2111 nfsm_srvpostop_attr(getret, &at);
2112 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2119 getret = VOP_GETATTR(vp, &at, cred);
2120 if (vp->v_type == VDIR) {
2121 error = EPERM; /* POSIX */
2125 nd.ni_cnd.cn_cred = cred;
2126 nd.ni_cnd.cn_nameiop = CREATE;
2127 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE | MPSAFE;
2128 error = nfs_namei(&nd, nfsd, dfhp, len, slp, nam, &md, &dpos,
2129 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2130 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2148 if (vp->v_mount != xp->v_mount) {
2154 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2155 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2156 NDFREE(&nd, NDF_ONLY_PNBUF);
2161 getret = VOP_GETATTR(vp, &at, cred);
2164 if (dirp == nd.ni_dvp)
2165 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2167 /* Release existing locks to prevent deadlock. */
2169 if (nd.ni_dvp == nd.ni_vp)
2179 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2180 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2181 VOP_UNLOCK(dirp, 0);
2185 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2187 nfsm_srvpostop_attr(getret, &at);
2188 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2194 NDFREE(&nd, NDF_ONLY_PNBUF);
2198 if (nd.ni_dvp == nd.ni_vp)
2207 vn_finished_write(mp);
2208 VFS_UNLOCK_GIANT(vfslocked);
2213 * nfs symbolic link service
2216 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2219 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2220 struct sockaddr *nam = nfsd->nd_nam;
2221 caddr_t dpos = nfsd->nd_dpos;
2222 struct ucred *cred = nfsd->nd_cr;
2223 struct vattr va, dirfor, diraft;
2224 struct nameidata nd;
2225 struct vattr *vap = &va;
2226 struct nfsv2_sattr *sp;
2227 char *bpos, *pathcp = NULL;
2230 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2231 int v3 = (nfsd->nd_flag & ND_NFSV3);
2232 struct mbuf *mb, *mreq;
2233 struct vnode *dirp = NULL;
2236 struct mount *mp = NULL;
2240 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2244 fhp = &nfh.fh_generic;
2246 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2250 vfslocked = VFS_LOCK_GIANT(mp);
2251 (void) vn_start_write(NULL, &mp, V_WAIT);
2252 vfs_rel(mp); /* The write holds a ref. */
2253 nfsm_srvnamesiz(len);
2254 nd.ni_cnd.cn_cred = cred;
2255 nd.ni_cnd.cn_nameiop = CREATE;
2256 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART | MPSAFE;
2257 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2258 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2259 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2264 nfsm_srvpathsiz(len2);
2272 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2273 iv.iov_base = pathcp;
2275 io.uio_resid = len2;
2279 io.uio_segflg = UIO_SYSSPACE;
2280 io.uio_rw = UIO_READ;
2282 nfsm_mtouio(&io, len2);
2284 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
2285 vap->va_mode = nfstov_mode(sp->sa_mode);
2287 *(pathcp + len2) = '\0';
2294 * issue symlink op. SAVESTART is set so the underlying path component
2295 * is only freed by the VOP if an error occurs.
2297 if (vap->va_mode == (mode_t)VNOVAL)
2299 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2301 NDFREE(&nd, NDF_ONLY_PNBUF);
2306 * releases directory prior to potential lookup op.
2314 * Issue lookup. Leave SAVESTART set so we can easily free
2315 * the name buffer later on.
2317 * since LOCKPARENT is not set, ni_dvp will be garbage on
2318 * return whether an error occurs or not.
2320 nd.ni_cnd.cn_nameiop = LOOKUP;
2321 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2322 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2323 nd.ni_cnd.cn_thread = curthread;
2324 nd.ni_cnd.cn_cred = cred;
2325 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
2327 nd.ni_cnd.cn_flags |= GIANTHELD;
2328 error = lookup(&nd);
2330 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2331 nd.ni_cnd.cn_flags &= ~GIANTHELD;
2334 bzero((caddr_t)fhp, sizeof(nfh));
2335 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2336 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2338 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2346 * These releases aren't strictly required, does even doing them
2347 * make any sense? XXX can nfsm_reply() block?
2350 free(pathcp, M_TEMP);
2354 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2355 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2356 VOP_UNLOCK(dirp, 0);
2358 if (nd.ni_startdir) {
2359 vrele(nd.ni_startdir);
2360 nd.ni_startdir = NULL;
2362 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2365 nfsm_srvpostop_fh(fhp);
2366 nfsm_srvpostop_attr(0, vap);
2368 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2374 NDFREE(&nd, NDF_ONLY_PNBUF);
2376 if (nd.ni_dvp == nd.ni_vp)
2384 vrele(nd.ni_startdir);
2388 free(pathcp, M_TEMP);
2390 vn_finished_write(mp);
2391 VFS_UNLOCK_GIANT(vfslocked);
2399 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2402 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2403 struct sockaddr *nam = nfsd->nd_nam;
2404 caddr_t dpos = nfsd->nd_dpos;
2405 struct ucred *cred = nfsd->nd_cr;
2406 struct vattr va, dirfor, diraft;
2407 struct vattr *vap = &va;
2408 struct nfs_fattr *fp;
2409 struct nameidata nd;
2412 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2413 int v3 = (nfsd->nd_flag & ND_NFSV3);
2414 struct mbuf *mb, *mreq;
2415 struct vnode *dirp = NULL;
2419 struct mount *mp = NULL;
2422 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2426 fhp = &nfh.fh_generic;
2428 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2432 vfslocked = VFS_LOCK_GIANT(mp);
2433 (void) vn_start_write(NULL, &mp, V_WAIT);
2434 vfs_rel(mp); /* The write holds a ref. */
2435 nfsm_srvnamesiz(len);
2436 nd.ni_cnd.cn_cred = cred;
2437 nd.ni_cnd.cn_nameiop = CREATE;
2438 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE;
2440 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2441 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2442 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2448 nfsm_reply(NFSX_WCCDATA(v3));
2450 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2458 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
2459 vap->va_mode = nfstov_mode(*tl++);
2463 * At this point nd.ni_dvp is referenced and exclusively locked and
2464 * nd.ni_vp, if it exists, is referenced but not locked.
2467 vap->va_type = VDIR;
2468 if (nd.ni_vp != NULL) {
2469 NDFREE(&nd, NDF_ONLY_PNBUF);
2475 * Issue mkdir op. Since SAVESTART is not set, the pathname
2476 * component is freed by the VOP call. This will fill-in
2477 * nd.ni_vp, reference, and exclusively lock it.
2479 if (vap->va_mode == (mode_t)VNOVAL)
2481 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2482 NDFREE(&nd, NDF_ONLY_PNBUF);
2489 bzero((caddr_t)fhp, sizeof(nfh));
2490 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2491 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2493 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2497 if (dirp == nd.ni_dvp) {
2498 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2500 /* Release existing locks to prevent deadlock. */
2502 NDFREE(&nd, NDF_ONLY_PNBUF);
2503 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2516 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2517 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2518 VOP_UNLOCK(dirp, 0);
2521 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2524 nfsm_srvpostop_fh(fhp);
2525 nfsm_srvpostop_attr(0, vap);
2527 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2528 } else if (!error) {
2529 /* v2 non-error case. */
2530 nfsm_srvfhtom(fhp, v3);
2531 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2532 nfsm_srvfillattr(vap, fp);
2539 NDFREE(&nd, NDF_ONLY_PNBUF);
2540 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2553 vn_finished_write(mp);
2554 VFS_UNLOCK_GIANT(vfslocked);
2562 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2565 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2566 struct sockaddr *nam = nfsd->nd_nam;
2567 caddr_t dpos = nfsd->nd_dpos;
2568 struct ucred *cred = nfsd->nd_cr;
2570 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2571 int v3 = (nfsd->nd_flag & ND_NFSV3);
2572 struct mbuf *mb, *mreq;
2573 struct vnode *vp, *dirp = NULL;
2574 struct vattr dirfor, diraft;
2577 struct nameidata nd;
2578 struct mount *mp = NULL;
2581 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2585 fhp = &nfh.fh_generic;
2587 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2591 vfslocked = VFS_LOCK_GIANT(mp);
2592 (void) vn_start_write(NULL, &mp, V_WAIT);
2593 vfs_rel(mp); /* The write holds a ref. */
2594 nfsm_srvnamesiz(len);
2595 nd.ni_cnd.cn_cred = cred;
2596 nd.ni_cnd.cn_nameiop = DELETE;
2597 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | MPSAFE;
2598 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2599 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2600 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2606 nfsm_reply(NFSX_WCCDATA(v3));
2608 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2613 if (vp->v_type != VDIR) {
2618 * No rmdir "." please.
2620 if (nd.ni_dvp == vp) {
2625 * The root of a mounted filesystem cannot be deleted.
2627 if (vp->v_vflag & VV_ROOT)
2631 * Issue or abort op. Since SAVESTART is not set, path name
2632 * component is freed by the VOP after either.
2635 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2636 NDFREE(&nd, NDF_ONLY_PNBUF);
2639 if (dirp == nd.ni_dvp)
2640 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2642 /* Release existing locks to prevent deadlock. */
2644 if (nd.ni_dvp == nd.ni_vp)
2653 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2654 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2655 VOP_UNLOCK(dirp, 0);
2658 nfsm_reply(NFSX_WCCDATA(v3));
2661 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2665 NDFREE(&nd, NDF_ONLY_PNBUF);
2667 if (nd.ni_dvp == nd.ni_vp)
2677 vn_finished_write(mp);
2678 VFS_UNLOCK_GIANT(vfslocked);
2683 * nfs readdir service
2684 * - mallocs what it thinks is enough to read
2685 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2686 * - calls VOP_READDIR()
2687 * - loops around building the reply
2688 * if the output generated exceeds count break out of loop
2689 * The nfsm_clget macro is used here so that the reply will be packed
2690 * tightly in mbuf clusters.
2691 * - it only knows that it has encountered eof when the VOP_READDIR()
2693 * - as such one readdir rpc will return eof false although you are there
2694 * and then the next will return eof
2695 * - it trims out records with d_fileno == 0
2696 * this doesn't matter for Unix clients, but they might confuse clients
2698 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2699 * than requested, but this may not apply to all filesystems. For
2700 * example, client NFS does not { although it is never remote mounted
2702 * The alternate call nfsrv_readdirplus() does lookups as well.
2703 * PS: The NFS protocol spec. does not clarify what the "count" byte
2704 * argument is a count of.. just name strings and file id's or the
2705 * entire reply rpc or ...
2706 * I tried just file name and id sizes and it confused the Sun client,
2707 * so I am using the full rpc size now. The "paranoia.." comment refers
2708 * to including the status longwords that are not a part of the dir.
2709 * "entry" structures, but are in the rpc.
2713 u_int32_t fl_postopok;
2714 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2716 u_int32_t fl_fhsize;
2717 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2721 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2724 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2725 struct sockaddr *nam = nfsd->nd_nam;
2726 caddr_t dpos = nfsd->nd_dpos;
2727 struct ucred *cred = nfsd->nd_cr;
2734 struct mbuf *mb, *mreq;
2735 char *cpos, *cend, *rbuf;
2736 struct vnode *vp = NULL;
2742 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2743 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2744 int v3 = (nfsd->nd_flag & ND_NFSV3);
2745 u_quad_t off, toff, verf;
2746 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2749 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2751 fhp = &nfh.fh_generic;
2754 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
2755 toff = fxdr_hyper(tl);
2757 verf = fxdr_hyper(tl);
2760 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
2761 toff = fxdr_unsigned(u_quad_t, *tl++);
2762 verf = 0; /* shut up gcc */
2765 cnt = fxdr_unsigned(int, *tl);
2766 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2767 xfer = NFS_SRVMAXDATA(nfsd);
2773 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
2774 nam, &rdonly, TRUE);
2775 if (!error && vp->v_type != VDIR) {
2781 nfsm_reply(NFSX_UNSIGNED);
2783 nfsm_srvpostop_attr(getret, &at);
2789 * Obtain lock on vnode for this section of the code
2792 error = getret = VOP_GETATTR(vp, &at, cred);
2795 * XXX This check may be too strict for Solaris 2.5 clients.
2797 if (!error && toff && verf && verf != at.va_filerev)
2798 error = NFSERR_BAD_COOKIE;
2802 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
2806 nfsm_reply(NFSX_POSTOPATTR(v3));
2808 nfsm_srvpostop_attr(getret, &at);
2815 * end section. Allocate rbuf and continue
2817 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2820 iv.iov_len = fullsiz;
2823 io.uio_offset = (off_t)off;
2824 io.uio_resid = fullsiz;
2825 io.uio_segflg = UIO_SYSSPACE;
2826 io.uio_rw = UIO_READ;
2829 vn_lock(vp, LK_SHARED | LK_RETRY);
2831 free((caddr_t)cookies, M_TEMP);
2834 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2835 off = (off_t)io.uio_offset;
2836 if (!cookies && !error)
2837 error = NFSERR_PERM;
2839 getret = VOP_GETATTR(vp, &at, cred);
2847 free((caddr_t)rbuf, M_TEMP);
2849 free((caddr_t)cookies, M_TEMP);
2850 nfsm_reply(NFSX_POSTOPATTR(v3));
2852 nfsm_srvpostop_attr(getret, &at);
2857 siz -= io.uio_resid;
2860 * If nothing read, return eof
2866 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2869 nfsm_srvpostop_attr(getret, &at);
2870 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
2871 txdr_hyper(at.va_filerev, tl);
2874 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2875 *tl++ = nfsrv_nfs_false;
2876 *tl = nfsrv_nfs_true;
2877 free((caddr_t)rbuf, M_TEMP);
2878 free((caddr_t)cookies, M_TEMP);
2885 * Check for degenerate cases of nothing useful read.
2886 * If so go try again
2890 dp = (struct dirent *)cpos;
2893 * For some reason FreeBSD's ufs_readdir() chooses to back the
2894 * directory offset up to a block boundary, so it is necessary to
2895 * skip over the records that precede the requested offset. This
2896 * requires the assumption that file offset cookies monotonically
2899 while (cpos < cend && ncookies > 0 &&
2900 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2901 ((u_quad_t)(*cookiep)) <= toff)) {
2902 cpos += dp->d_reclen;
2903 dp = (struct dirent *)cpos;
2907 if (cpos >= cend || ncookies == 0) {
2913 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2914 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2916 nfsm_srvpostop_attr(getret, &at);
2917 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2918 txdr_hyper(at.va_filerev, tl);
2922 be = bp + M_TRAILINGSPACE(mp);
2924 /* Loop through the records and build reply */
2925 while (cpos < cend && ncookies > 0) {
2926 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2927 nlen = dp->d_namlen;
2928 rem = nfsm_rndup(nlen) - nlen;
2929 len += (4 * NFSX_UNSIGNED + nlen + rem);
2931 len += 2 * NFSX_UNSIGNED;
2937 * Build the directory record xdr from
2941 *tl = nfsrv_nfs_true;
2942 bp += NFSX_UNSIGNED;
2946 bp += NFSX_UNSIGNED;
2949 *tl = txdr_unsigned(dp->d_fileno);
2950 bp += NFSX_UNSIGNED;
2952 *tl = txdr_unsigned(nlen);
2953 bp += NFSX_UNSIGNED;
2955 /* And loop around copying the name */
2964 bcopy(cp, bp, tsiz);
2970 /* And null pad to an int32_t boundary. */
2971 for (i = 0; i < rem; i++)
2975 /* Finish off the record */
2978 bp += NFSX_UNSIGNED;
2981 *tl = txdr_unsigned(*cookiep);
2982 bp += NFSX_UNSIGNED;
2984 cpos += dp->d_reclen;
2985 dp = (struct dirent *)cpos;
2992 *tl = nfsrv_nfs_false;
2993 bp += NFSX_UNSIGNED;
2996 *tl = nfsrv_nfs_true;
2998 *tl = nfsrv_nfs_false;
2999 bp += NFSX_UNSIGNED;
3002 mp->m_len = bp - mtod(mp, caddr_t);
3004 mp->m_len += bp - bpos;
3005 free((caddr_t)rbuf, M_TEMP);
3006 free((caddr_t)cookies, M_TEMP);
3011 VFS_UNLOCK_GIANT(vfslocked);
3016 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3019 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3020 struct sockaddr *nam = nfsd->nd_nam;
3021 caddr_t dpos = nfsd->nd_dpos;
3022 struct ucred *cred = nfsd->nd_cr;
3029 struct mbuf *mb, *mreq;
3030 char *cpos, *cend, *rbuf;
3031 struct vnode *vp = NULL, *nvp;
3034 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3037 struct vattr va, at, *vap = &va;
3038 struct nfs_fattr *fp;
3039 int len, nlen, rem, xfer, tsiz, i, error = 0, error1, getret = 1;
3040 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3041 u_quad_t off, toff, verf;
3042 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3043 int v3 = (nfsd->nd_flag & ND_NFSV3);
3044 int usevget = 1, vfslocked;
3045 struct componentname cn;
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) {
3190 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3193 nfsm_srvpostop_attr(getret, &at);
3194 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3195 txdr_hyper(at.va_filerev, tl);
3198 be = bp + M_TRAILINGSPACE(mp);
3200 /* Loop through the records and build reply */
3201 while (cpos < cend && ncookies > 0) {
3202 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3203 nlen = dp->d_namlen;
3204 rem = nfsm_rndup(nlen)-nlen;
3208 * For readdir_and_lookup get the vnode using
3211 error = VFS_VGET(vp->v_mount, dp->d_fileno,
3213 if (error != 0 && error != EOPNOTSUPP) {
3216 } else if (error == EOPNOTSUPP) {
3218 * VFS_VGET() not supported?
3219 * Let's switch to VOP_LOOKUP().
3223 cn.cn_nameiop = LOOKUP;
3224 cn.cn_flags = ISLASTCN | NOFOLLOW | \
3225 LOCKSHARED | LOCKLEAF | MPSAFE;
3226 cn.cn_lkflags = LK_SHARED | LK_RETRY;
3228 cn.cn_thread = curthread;
3232 cn.cn_nameptr = dp->d_name;
3233 cn.cn_namelen = dp->d_namlen;
3234 if (dp->d_namlen == 2 &&
3235 dp->d_name[0] == '.' &&
3236 dp->d_name[1] == '.') {
3237 cn.cn_flags |= ISDOTDOT;
3239 cn.cn_flags &= ~ISDOTDOT;
3241 if (!VOP_ISLOCKED(vp))
3242 vn_lock(vp, LK_SHARED | LK_RETRY);
3243 if ((vp->v_vflag & VV_ROOT) != 0 &&
3244 (cn.cn_flags & ISDOTDOT) != 0) {
3247 } else if (VOP_LOOKUP(vp, &nvp, &cn) != 0)
3251 bzero((caddr_t)nfhp, NFSX_V3FH);
3252 nfhp->fh_fsid = nvp->v_mount->mnt_stat.f_fsid;
3253 if ((error1 = VOP_VPTOFH(nvp, &nfhp->fh_fid)) == 0)
3254 error1 = VOP_GETATTR(nvp, vap, cred);
3264 * If either the dircount or maxcount will be
3265 * exceeded, get out now. Both of these lengths
3266 * are calculated conservatively, including all
3269 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3271 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3272 if (len > cnt || dirlen > fullsiz) {
3278 * Build the directory record xdr from
3281 fp = (struct nfs_fattr *)&fl.fl_fattr;
3282 nfsm_srvfillattr(vap, fp);
3283 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3284 fl.fl_fhok = nfsrv_nfs_true;
3285 fl.fl_postopok = nfsrv_nfs_true;
3286 fl.fl_off.nfsuquad[0] = 0;
3287 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3290 *tl = nfsrv_nfs_true;
3291 bp += NFSX_UNSIGNED;
3294 bp += NFSX_UNSIGNED;
3296 *tl = txdr_unsigned(dp->d_fileno);
3297 bp += NFSX_UNSIGNED;
3299 *tl = txdr_unsigned(nlen);
3300 bp += NFSX_UNSIGNED;
3302 /* And loop around copying the name */
3307 if ((bp + xfer) > be)
3311 bcopy(cp, bp, tsiz);
3317 /* And null pad to an int32_t boundary. */
3318 for (i = 0; i < rem; i++)
3322 * Now copy the flrep structure out.
3324 xfer = sizeof (struct flrep);
3328 if ((bp + xfer) > be)
3332 bcopy(cp, bp, tsiz);
3340 cpos += dp->d_reclen;
3341 dp = (struct dirent *)cpos;
3345 if (!usevget && VOP_ISLOCKED(vp))
3351 *tl = nfsrv_nfs_false;
3352 bp += NFSX_UNSIGNED;
3355 *tl = nfsrv_nfs_true;
3357 *tl = nfsrv_nfs_false;
3358 bp += NFSX_UNSIGNED;
3361 mp->m_len = bp - mtod(mp, caddr_t);
3363 mp->m_len += bp - bpos;
3364 free((caddr_t)cookies, M_TEMP);
3365 free((caddr_t)rbuf, M_TEMP);
3369 VFS_UNLOCK_GIANT(vfslocked);
3374 * nfs commit service
3377 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3380 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3381 struct sockaddr *nam = nfsd->nd_nam;
3382 caddr_t dpos = nfsd->nd_dpos;
3383 struct ucred *cred = nfsd->nd_cr;
3384 struct vattr bfor, aft;
3385 struct vnode *vp = NULL;
3390 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3391 struct mbuf *mb, *mreq;
3393 struct mount *mp = NULL;
3394 int v3 = (nfsd->nd_flag & ND_NFSV3);
3398 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3401 panic("nfsrv_commit: v3 proc called on a v2 connection");
3402 fhp = &nfh.fh_generic;
3404 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3408 vfslocked = VFS_LOCK_GIANT(mp);
3409 (void) vn_start_write(NULL, &mp, V_WAIT);
3410 vfs_rel(mp); /* The write holds a ref. */
3411 tl = nfsm_dissect_nonblock(u_int32_t *, 3 * NFSX_UNSIGNED);
3414 * XXX At this time VOP_FSYNC() does not accept offset and byte
3415 * count parameters, so these arguments are useless (someday maybe).
3417 off = fxdr_hyper(tl);
3419 cnt = fxdr_unsigned(int, *tl);
3420 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, nfsd, slp,
3421 nam, &rdonly, TRUE);
3422 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
3424 nfsm_reply(2 * NFSX_UNSIGNED);
3425 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3429 for_ret = VOP_GETATTR(vp, &bfor, cred);
3431 if (cnt > MAX_COMMIT_COUNT) {
3433 * Give up and do the whole thing
3436 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3437 VM_OBJECT_LOCK(vp->v_object);
3438 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3439 VM_OBJECT_UNLOCK(vp->v_object);
3441 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3444 * Locate and synchronously write any buffers that fall
3445 * into the requested range. Note: we are assuming that
3446 * f_iosize is a power of 2.
3448 int iosize = vp->v_mount->mnt_stat.f_iosize;
3449 int iomask = iosize - 1;
3454 * Align to iosize boundry, super-align to page boundry.
3457 cnt += off & iomask;
3458 off &= ~(u_quad_t)iomask;
3460 if (off & PAGE_MASK) {
3461 cnt += off & PAGE_MASK;
3462 off &= ~(u_quad_t)PAGE_MASK;
3464 lblkno = off / iosize;
3467 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3468 VM_OBJECT_LOCK(vp->v_object);
3469 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3470 VM_OBJECT_UNLOCK(vp->v_object);
3479 * If we have a buffer and it is marked B_DELWRI we
3480 * have to lock and write it. Otherwise the prior
3481 * write is assumed to have already been committed.
3483 * gbincore() can return invalid buffers now so we
3484 * have to check that bit as well (though B_DELWRI
3485 * should not be set if B_INVAL is set there could be
3486 * a race here since we haven't locked the buffer).
3488 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
3489 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
3490 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
3492 continue; /* retry */
3494 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
3497 bp->b_flags &= ~B_ASYNC;
3513 aft_ret = VOP_GETATTR(vp, &aft, cred);
3517 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3518 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3520 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3521 if (nfsver.tv_sec == 0)
3523 *tl++ = txdr_unsigned(nfsver.tv_sec);
3524 *tl = txdr_unsigned(nfsver.tv_usec);
3531 vn_finished_write(mp);
3532 VFS_UNLOCK_GIANT(vfslocked);
3537 * nfs statfs service
3540 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3543 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3544 struct sockaddr *nam = nfsd->nd_nam;
3545 caddr_t dpos = nfsd->nd_dpos;
3546 struct ucred *cred = nfsd->nd_cr;
3548 struct nfs_statfs *sfp;
3550 int error = 0, rdonly, getret = 1;
3551 int v3 = (nfsd->nd_flag & ND_NFSV3);
3552 struct mbuf *mb, *mreq;
3553 struct vnode *vp = NULL;
3557 struct statfs statfs;
3561 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3563 fhp = &nfh.fh_generic;
3565 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3566 nam, &rdonly, TRUE);
3568 nfsm_reply(NFSX_UNSIGNED);
3570 nfsm_srvpostop_attr(getret, &at);
3575 error = VFS_STATFS(vp->v_mount, sf);
3576 getret = VOP_GETATTR(vp, &at, cred);
3579 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3581 nfsm_srvpostop_attr(getret, &at);
3586 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3588 tval = (u_quad_t)sf->f_blocks;
3589 tval *= (u_quad_t)sf->f_bsize;
3590 txdr_hyper(tval, &sfp->sf_tbytes);
3591 tval = (u_quad_t)sf->f_bfree;
3592 tval *= (u_quad_t)sf->f_bsize;
3593 txdr_hyper(tval, &sfp->sf_fbytes);
3595 * Don't send negative values for available space,
3596 * since this field is unsigned in the NFS protocol.
3597 * Otherwise, the client would see absurdly high
3598 * numbers for free space.
3600 if (sf->f_bavail < 0)
3603 tval = (u_quad_t)sf->f_bavail;
3604 tval *= (u_quad_t)sf->f_bsize;
3605 txdr_hyper(tval, &sfp->sf_abytes);
3606 sfp->sf_tfiles.nfsuquad[0] = 0;
3607 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3608 sfp->sf_ffiles.nfsuquad[0] = 0;
3609 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3610 sfp->sf_afiles.nfsuquad[0] = 0;
3611 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3612 sfp->sf_invarsec = 0;
3614 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3615 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3616 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3617 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3618 if (sf->f_bavail < 0)
3621 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3626 VFS_UNLOCK_GIANT(vfslocked);
3631 * nfs fsinfo service
3634 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3637 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3638 struct sockaddr *nam = nfsd->nd_nam;
3639 caddr_t dpos = nfsd->nd_dpos;
3640 struct ucred *cred = nfsd->nd_cr;
3641 struct nfsv3_fsinfo *sip;
3643 int error = 0, rdonly, getret = 1, pref;
3644 struct mbuf *mb, *mreq;
3645 struct vnode *vp = NULL;
3651 int v3 = (nfsd->nd_flag & ND_NFSV3);
3654 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3656 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3657 fhp = &nfh.fh_generic;
3660 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3661 nam, &rdonly, TRUE);
3663 nfsm_reply(NFSX_UNSIGNED);
3664 nfsm_srvpostop_attr(getret, &at);
3669 /* XXX Try to make a guess on the max file size. */
3670 VFS_STATFS(vp->v_mount, &sb);
3671 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3673 getret = VOP_GETATTR(vp, &at, cred);
3676 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3677 nfsm_srvpostop_attr(getret, &at);
3678 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3682 * There should be filesystem VFS OP(s) to get this information.
3683 * For now, assume ufs.
3685 pref = NFS_SRVMAXDATA(nfsd);
3686 sip->fs_rtmax = txdr_unsigned(pref);
3687 sip->fs_rtpref = txdr_unsigned(pref);
3688 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3689 sip->fs_wtmax = txdr_unsigned(pref);
3690 sip->fs_wtpref = txdr_unsigned(pref);
3691 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3692 sip->fs_dtpref = txdr_unsigned(pref);
3693 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3694 sip->fs_timedelta.nfsv3_sec = 0;
3695 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3696 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3697 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3698 NFSV3FSINFO_CANSETTIME);
3702 VFS_UNLOCK_GIANT(vfslocked);
3707 * nfs pathconf service
3710 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3713 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3714 struct sockaddr *nam = nfsd->nd_nam;
3715 caddr_t dpos = nfsd->nd_dpos;
3716 struct ucred *cred = nfsd->nd_cr;
3717 struct nfsv3_pathconf *pc;
3719 int error = 0, rdonly, getret = 1;
3720 register_t linkmax, namemax, chownres, notrunc;
3721 struct mbuf *mb, *mreq;
3722 struct vnode *vp = NULL;
3726 int v3 = (nfsd->nd_flag & ND_NFSV3);
3729 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3731 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3733 fhp = &nfh.fh_generic;
3735 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, nfsd, slp,
3736 nam, &rdonly, TRUE);
3738 nfsm_reply(NFSX_UNSIGNED);
3739 nfsm_srvpostop_attr(getret, &at);
3743 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3745 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3747 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3749 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3750 getret = VOP_GETATTR(vp, &at, cred);
3753 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3754 nfsm_srvpostop_attr(getret, &at);
3759 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3761 pc->pc_linkmax = txdr_unsigned(linkmax);
3762 pc->pc_namemax = txdr_unsigned(namemax);
3763 pc->pc_notrunc = txdr_unsigned(notrunc);
3764 pc->pc_chownrestricted = txdr_unsigned(chownres);
3767 * These should probably be supported by VOP_PATHCONF(), but
3768 * until msdosfs is exportable (why would you want to?), the
3769 * Unix defaults should be ok.
3771 pc->pc_caseinsensitive = nfsrv_nfs_false;
3772 pc->pc_casepreserving = nfsrv_nfs_true;
3776 VFS_UNLOCK_GIANT(vfslocked);
3781 * Null operation, used by clients to ping server
3785 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3788 struct mbuf *mrep = nfsd->nd_mrep;
3790 int error = NFSERR_RETVOID;
3791 struct mbuf *mb, *mreq;
3793 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3800 * No operation, used for obsolete procedures
3804 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3807 struct mbuf *mrep = nfsd->nd_mrep;
3810 struct mbuf *mb, *mreq;
3812 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3813 if (nfsd->nd_repstat)
3814 error = nfsd->nd_repstat;
3816 error = EPROCUNAVAIL;
3824 * Perform access checking for vnodes obtained from file handles that would
3825 * refer to files already opened by a Unix client. You cannot just use
3826 * vn_writechk() and VOP_ACCESS() for two reasons.
3827 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
3829 * 2 - The owner is to be given access irrespective of mode bits for some
3830 * operations, so that processes that chmod after opening a file don't
3831 * break. I don't like this because it opens a security hole, but since
3832 * the nfs server opens a security hole the size of a barn door anyhow,
3835 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3836 * will return EPERM instead of EACCES. EPERM is always an error.
3839 nfsrv_access(struct vnode *vp, accmode_t accmode, struct ucred *cred,
3840 int rdonly, int override)
3845 VFS_ASSERT_GIANT(vp->v_mount);
3847 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3849 if (accmode & VWRITE) {
3850 /* Just vn_writechk() changed to check rdonly */
3852 * Disallow write attempts on read-only filesystems;
3853 * unless the file is a socket or a block or character
3854 * device resident on the filesystem.
3856 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3857 switch (vp->v_type) {
3867 * If there's shared text associated with
3868 * the inode, we can't allow writing.
3870 if (vp->v_vflag & VV_TEXT)
3874 error = VOP_GETATTR(vp, &vattr, cred);
3877 error = VOP_ACCESS(vp, accmode, cred, curthread);
3879 * Allow certain operations for the owner (reads and writes
3880 * on files that are already open).
3882 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)