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 <nfs/rpcv2.h>
97 #include <nfsserver/nfs.h>
98 #include <nfs/xdr_subs.h>
99 #include <nfsserver/nfsm_subs.h>
102 #define nfsdbprintf(info) printf info
104 #define nfsdbprintf(info)
107 #define MAX_COMMIT_COUNT (1024 * 1024)
109 #define NUM_HEURISTIC 1017
110 #define NHUSE_INIT 64
112 #define NHUSE_MAX 2048
114 static struct nfsheur {
115 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
116 off_t nh_nextr; /* next offset for sequential detection */
117 int nh_use; /* use count for selection */
118 int nh_seqcount; /* heuristic */
119 } nfsheur[NUM_HEURISTIC];
123 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
124 int nfsrvw_procrastinate_v3 = 0;
126 static struct timeval nfsver = { 0 };
128 SYSCTL_NODE(_vfs, OID_AUTO, nfsrv, CTLFLAG_RW, 0, "NFS server");
130 static int nfs_async;
131 static int nfs_commit_blks;
132 static int nfs_commit_miss;
133 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
134 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
135 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
137 struct nfsrvstats nfsrvstats;
138 SYSCTL_STRUCT(_vfs_nfsrv, NFS_NFSRVSTATS, nfsrvstats, CTLFLAG_RW,
139 &nfsrvstats, nfsrvstats, "S,nfsrvstats");
141 static int nfsrv_access(struct vnode *, int, struct ucred *, int, int);
142 static void nfsrvw_coalesce(struct nfsrv_descript *,
143 struct nfsrv_descript *);
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, cred, 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, cred, 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, cred, 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, 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, cred, 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, cred, 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 MALLOC(iv, struct iovec *, 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, cred, 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 MALLOC(ivp, struct iovec *, 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 * For the purposes of write gathering, we must decide if the credential
1229 * associated with two pending requests have equivilent privileges. Since
1230 * NFS only uses a subset of the BSD ucred -- the effective uid and group
1231 * IDs -- we have a compare routine that checks only the relevant fields.
1234 nfsrv_samecred(struct ucred *cr1, struct ucred *cr2)
1238 if (cr1->cr_uid != cr2->cr_uid)
1240 if (cr1->cr_ngroups != cr2->cr_ngroups)
1242 for (i = 0; i < cr1->cr_ngroups; i++) {
1243 if (cr1->cr_groups[i] != cr2->cr_groups[i])
1250 * NFS write service with write gathering support. Called when
1251 * nfsrvw_procrastinate > 0.
1252 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1253 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1257 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1262 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1263 struct nfs_fattr *fp;
1266 struct nfsrvw_delayhash *wpp;
1268 struct vattr va, forat;
1271 int error = 0, rdonly, len, forat_ret = 1;
1272 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1273 struct mbuf *mb, *mreq, *mrep, *md;
1274 struct vnode *vp = NULL;
1275 struct uio io, *uiop = &io;
1277 struct mount *mntp = NULL;
1281 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1291 mrep = nfsd->nd_mrep;
1293 dpos = nfsd->nd_dpos;
1295 v3 = (nfsd->nd_flag & ND_NFSV3);
1296 LIST_INIT(&nfsd->nd_coalesce);
1297 nfsd->nd_mreq = NULL;
1298 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1299 cur_usec = nfs_curusec();
1300 nfsd->nd_time = cur_usec +
1301 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1304 * Now, get the write header..
1306 nfsm_srvmtofh(&nfsd->nd_fh);
1308 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
1309 nfsd->nd_off = fxdr_hyper(tl);
1311 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1313 tl = nfsm_dissect_nonblock(u_int32_t *, 4 * NFSX_UNSIGNED);
1314 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1317 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1319 len = fxdr_unsigned(int32_t, *tl);
1321 nfsd->nd_eoff = nfsd->nd_off + len;
1324 * Trim the header out of the mbuf list and trim off any trailing
1325 * junk so that the mbuf list has only the write data.
1333 adjust = dpos - mtod(mp, caddr_t);
1334 mp->m_len -= adjust;
1335 if (mp->m_len > 0 && adjust > 0)
1336 mp->m_data += adjust;
1343 mp->m_len -= (i - len);
1349 if (len > NFS_MAXDATA || len < 0 || i < len) {
1353 nfsm_writereply(2 * NFSX_UNSIGNED);
1355 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1356 nfsd->nd_mreq = mreq;
1357 nfsd->nd_mrep = NULL;
1362 * Add this entry to the hash and time queues.
1366 wp = LIST_FIRST(&slp->ns_tq);
1367 while (wp && wp->nd_time < nfsd->nd_time) {
1369 wp = LIST_NEXT(wp, nd_tq);
1371 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1373 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1375 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1377 if (nfsd->nd_mrep) {
1378 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1380 wp = LIST_FIRST(wpp);
1382 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)){
1384 wp = LIST_NEXT(wp, nd_hash);
1386 while (wp && wp->nd_off < nfsd->nd_off &&
1387 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)) {
1389 wp = LIST_NEXT(wp, nd_hash);
1392 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1395 * Search the hash list for overlapping entries and
1398 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1399 wp = LIST_NEXT(nfsd, nd_hash);
1400 if (nfsrv_samecred(owp->nd_cr, nfsd->nd_cr))
1401 nfsrvw_coalesce(owp, nfsd);
1404 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1411 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1412 * and generate the associated reply mbuf list(s).
1415 cur_usec = nfs_curusec();
1417 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1418 owp = LIST_NEXT(nfsd, nd_tq);
1419 if (nfsd->nd_time > cur_usec)
1423 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1424 LIST_REMOVE(nfsd, nd_tq);
1425 LIST_REMOVE(nfsd, nd_hash);
1427 mrep = nfsd->nd_mrep;
1428 nfsd->nd_mrep = NULL;
1430 v3 = (nfsd->nd_flag & ND_NFSV3);
1431 forat_ret = aftat_ret = 1;
1432 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, &vfslocked, cred,
1433 slp, nfsd->nd_nam, &rdonly, TRUE);
1436 forat_ret = VOP_GETATTR(vp, &forat, cred);
1437 if (vp->v_type != VREG) {
1441 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1447 error = nfsrv_access(vp, VWRITE, cred, rdonly, 1);
1448 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1449 ioflags = IO_NODELOCKED;
1450 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1451 ioflags = (IO_SYNC | IO_NODELOCKED);
1453 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1454 uiop->uio_rw = UIO_WRITE;
1455 uiop->uio_segflg = UIO_SYSSPACE;
1456 uiop->uio_td = NULL;
1457 uiop->uio_offset = nfsd->nd_off;
1458 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1459 if (uiop->uio_resid > 0) {
1467 uiop->uio_iovcnt = i;
1468 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1470 uiop->uio_iov = ivp = iov;
1473 if (mp->m_len > 0) {
1474 ivp->iov_base = mtod(mp, caddr_t);
1475 ivp->iov_len = mp->m_len;
1482 if (vn_start_write(vp, &mntp, V_NOWAIT) != 0) {
1484 error = vn_start_write(NULL, &mntp, V_WAIT);
1485 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1487 mvfslocked = VFS_LOCK_GIANT(mntp);
1490 error = VOP_WRITE(vp, uiop, ioflags, cred);
1491 /* Unlocked write. */
1492 nfsrvstats.srvvop_writes++;
1493 vn_finished_write(mntp);
1495 VFS_UNLOCK_GIANT(mvfslocked);
1496 FREE((caddr_t)iov, M_TEMP);
1500 aftat_ret = VOP_GETATTR(vp, &va, cred);
1504 VFS_UNLOCK_GIANT(vfslocked);
1506 * Loop around generating replies for all write rpcs that have
1507 * now been completed.
1511 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1513 nfsm_writereply(NFSX_WCCDATA(v3));
1515 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1518 nfsm_writereply(NFSX_PREOPATTR(v3) +
1519 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1520 NFSX_WRITEVERF(v3));
1522 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1523 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1524 *tl++ = txdr_unsigned(nfsd->nd_len);
1525 *tl++ = txdr_unsigned(swp->nd_stable);
1527 * Actually, there is no need to txdr these fields,
1528 * but it may make the values more human readable,
1529 * for debugging purposes.
1531 if (nfsver.tv_sec == 0)
1533 *tl++ = txdr_unsigned(nfsver.tv_sec);
1534 *tl = txdr_unsigned(nfsver.tv_usec);
1536 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1537 nfsm_srvfillattr(&va, fp);
1540 nfsd->nd_mreq = mreq;
1542 panic("nfsrv_write: nd_mrep not free");
1545 * Done. Put it at the head of the timer queue so that
1546 * the final phase can return the reply.
1551 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1553 nfsd = LIST_FIRST(&swp->nd_coalesce);
1555 LIST_REMOVE(nfsd, nd_tq);
1561 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1568 * Search for a reply to return.
1571 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq)
1572 if (nfsd->nd_mreq) {
1573 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1574 LIST_REMOVE(nfsd, nd_tq);
1575 *mrq = nfsd->nd_mreq;
1584 * Coalesce the write request nfsd into owp. To do this we must:
1585 * - remove nfsd from the queues
1586 * - merge nfsd->nd_mrep into owp->nd_mrep
1587 * - update the nd_eoff and nd_stable for owp
1588 * - put nfsd on owp's nd_coalesce list
1589 * NB: Must be called at splsoftclock().
1592 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1596 struct nfsrv_descript *p;
1598 NFS_DPF(WG, ("C%03x-%03x",
1599 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1600 LIST_REMOVE(nfsd, nd_hash);
1601 LIST_REMOVE(nfsd, nd_tq);
1602 if (owp->nd_eoff < nfsd->nd_eoff) {
1603 overlap = owp->nd_eoff - nfsd->nd_off;
1605 panic("nfsrv_coalesce: bad off");
1607 m_adj(nfsd->nd_mrep, overlap);
1611 mp->m_next = nfsd->nd_mrep;
1612 owp->nd_eoff = nfsd->nd_eoff;
1614 m_freem(nfsd->nd_mrep);
1615 nfsd->nd_mrep = NULL;
1616 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1617 owp->nd_stable = NFSV3WRITE_FILESYNC;
1618 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1619 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1620 owp->nd_stable = NFSV3WRITE_DATASYNC;
1621 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1624 * If nfsd had anything else coalesced into it, transfer them
1625 * to owp, otherwise their replies will never get sent.
1627 for (p = LIST_FIRST(&nfsd->nd_coalesce); p;
1628 p = LIST_FIRST(&nfsd->nd_coalesce)) {
1629 LIST_REMOVE(p, nd_tq);
1630 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1635 * nfs create service
1636 * now does a truncate to 0 length via. setattr if it already exists
1639 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1642 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1643 struct sockaddr *nam = nfsd->nd_nam;
1644 caddr_t dpos = nfsd->nd_dpos;
1645 struct ucred *cred = nfsd->nd_cr;
1646 struct nfs_fattr *fp;
1647 struct vattr va, dirfor, diraft;
1648 struct vattr *vap = &va;
1649 struct nfsv2_sattr *sp;
1651 struct nameidata nd;
1653 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1654 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1656 struct mbuf *mb, *mreq;
1657 struct vnode *dirp = NULL;
1661 u_char cverf[NFSX_V3CREATEVERF];
1662 struct mount *mp = NULL;
1666 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1673 fhp = &nfh.fh_generic;
1675 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1679 vfslocked = VFS_LOCK_GIANT(mp);
1680 (void) vn_start_write(NULL, &mp, V_WAIT);
1681 vfs_rel(mp); /* The write holds a ref. */
1682 nfsm_srvnamesiz(len);
1684 nd.ni_cnd.cn_cred = cred;
1685 nd.ni_cnd.cn_nameiop = CREATE;
1686 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE;
1689 * Call namei and do initial cleanup to get a few things
1690 * out of the way. If we get an initial error we cleanup
1691 * and return here to avoid special-casing the invalid nd
1692 * structure through the rest of the case. dirp may be
1693 * set even if an error occurs, but the nd structure will not
1694 * be valid at all if an error occurs so we have to invalidate it
1695 * prior to calling nfsm_reply ( which might goto nfsmout ).
1697 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1698 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1699 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1705 nfsm_reply(NFSX_WCCDATA(v3));
1707 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1713 * No error. Continue. State:
1715 * startdir is valid ( we release this immediately )
1717 * nd.ni_vp may be valid
1718 * nd.ni_dvp is valid
1720 * The error state is set through the code and we may also do some
1721 * opportunistic releasing of vnodes to avoid holding locks through
1722 * NFS I/O. The cleanup at the end is a catch-all
1727 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1728 how = fxdr_unsigned(int, *tl);
1730 case NFSV3CREATE_GUARDED:
1736 case NFSV3CREATE_UNCHECKED:
1739 case NFSV3CREATE_EXCLUSIVE:
1740 cp = nfsm_dissect_nonblock(caddr_t, NFSX_V3CREATEVERF);
1741 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1745 vap->va_type = VREG;
1747 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
1748 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1749 if (vap->va_type == VNON)
1750 vap->va_type = VREG;
1751 vap->va_mode = nfstov_mode(sp->sa_mode);
1752 switch (vap->va_type) {
1754 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1756 vap->va_size = (u_quad_t)tsize;
1761 rdev = fxdr_unsigned(long, sp->sa_size);
1769 * Iff doesn't exist, create it
1770 * otherwise just truncate to 0 length
1771 * should I set the mode too ?
1773 * The only possible error we can have at this point is EEXIST.
1774 * nd.ni_vp will also be non-NULL in that case.
1776 if (nd.ni_vp == NULL) {
1777 if (vap->va_mode == (mode_t)VNOVAL)
1779 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1780 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1782 NDFREE(&nd, NDF_ONLY_PNBUF);
1784 if (exclusive_flag) {
1787 bcopy(cverf, (caddr_t)&vap->va_atime,
1789 error = VOP_SETATTR(nd.ni_vp, vap,
1793 } else if (vap->va_type == VCHR || vap->va_type == VBLK ||
1794 vap->va_type == VFIFO) {
1796 * NFSv2-specific code for creating device nodes
1799 * Handle SysV FIFO node special cases. All other
1800 * devices require super user to access.
1802 if (vap->va_type == VCHR && rdev == 0xffffffff)
1803 vap->va_type = VFIFO;
1804 if (vap->va_type != VFIFO &&
1805 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV,
1809 vap->va_rdev = rdev;
1810 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1812 NDFREE(&nd, NDF_ONLY_PNBUF);
1819 * release dvp prior to lookup
1826 * Even though LOCKPARENT was cleared, ni_dvp may
1829 nd.ni_cnd.cn_nameiop = LOOKUP;
1830 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1831 nd.ni_cnd.cn_thread = curthread;
1832 nd.ni_cnd.cn_cred = cred;
1833 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
1835 nd.ni_cnd.cn_flags |= GIANTHELD;
1836 error = lookup(&nd);
1838 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1839 nd.ni_cnd.cn_flags &= ~GIANTHELD;
1843 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1851 if (vap->va_size != -1) {
1852 error = nfsrv_access(nd.ni_vp, VWRITE,
1853 cred, (nd.ni_cnd.cn_flags & RDONLY), 0);
1855 tempsize = vap->va_size;
1857 vap->va_size = tempsize;
1858 error = VOP_SETATTR(nd.ni_vp, vap, cred);
1864 bzero((caddr_t)fhp, sizeof(nfh));
1865 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1866 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1868 error = VOP_GETATTR(nd.ni_vp, vap, cred);
1871 if (exclusive_flag && !error &&
1872 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1874 if (dirp == nd.ni_dvp)
1875 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1877 /* Drop the other locks to avoid deadlock. */
1879 if (nd.ni_dvp == nd.ni_vp)
1889 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1890 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1891 VOP_UNLOCK(dirp, 0);
1895 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1898 nfsm_srvpostop_fh(fhp);
1899 nfsm_srvpostop_attr(0, vap);
1901 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1902 } else if (!error) {
1903 /* v2 non-error case. */
1904 nfsm_srvfhtom(fhp, v3);
1905 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1906 nfsm_srvfillattr(vap, fp);
1912 if (nd.ni_dvp == nd.ni_vp)
1919 if (nd.ni_startdir) {
1920 vrele(nd.ni_startdir);
1921 nd.ni_startdir = NULL;
1925 NDFREE(&nd, NDF_ONLY_PNBUF);
1926 vn_finished_write(mp);
1927 VFS_UNLOCK_GIANT(vfslocked);
1932 * nfs v3 mknod service
1935 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1938 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1939 struct sockaddr *nam = nfsd->nd_nam;
1940 caddr_t dpos = nfsd->nd_dpos;
1941 struct ucred *cred = nfsd->nd_cr;
1942 struct vattr va, dirfor, diraft;
1943 struct vattr *vap = &va;
1944 struct thread *td = curthread;
1946 struct nameidata nd;
1948 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1949 u_int32_t major, minor;
1951 struct mbuf *mb, *mreq;
1952 struct vnode *vp, *dirp = NULL;
1955 struct mount *mp = NULL;
1956 int v3 = (nfsd->nd_flag & ND_NFSV3);
1960 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1963 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1966 fhp = &nfh.fh_generic;
1968 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1972 vfslocked = VFS_LOCK_GIANT(mp);
1973 (void) vn_start_write(NULL, &mp, V_WAIT);
1974 vfs_rel(mp); /* The write holds a ref. */
1975 nfsm_srvnamesiz(len);
1977 nd.ni_cnd.cn_cred = cred;
1978 nd.ni_cnd.cn_nameiop = CREATE;
1979 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE;
1982 * Handle nfs_namei() call. If an error occurs, the nd structure
1983 * is not valid. However, nfsm_*() routines may still jump to
1987 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1988 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1989 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1991 nfsm_reply(NFSX_WCCDATA(1));
1992 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1996 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1997 vtyp = nfsv3tov_type(*tl);
1998 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1999 error = NFSERR_BADTYPE;
2004 if (vtyp == VCHR || vtyp == VBLK) {
2005 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
2006 major = fxdr_unsigned(u_int32_t, *tl++);
2007 minor = fxdr_unsigned(u_int32_t, *tl);
2008 vap->va_rdev = makedev(major, minor);
2012 * Iff doesn't exist, create it.
2018 vap->va_type = vtyp;
2019 if (vap->va_mode == (mode_t)VNOVAL)
2021 if (vtyp == VSOCK) {
2022 vrele(nd.ni_startdir);
2023 nd.ni_startdir = NULL;
2024 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2026 NDFREE(&nd, NDF_ONLY_PNBUF);
2028 if (vtyp != VFIFO && (error = priv_check_cred(cred,
2029 PRIV_VFS_MKNOD_DEV, 0)))
2031 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2033 NDFREE(&nd, NDF_ONLY_PNBUF);
2040 * Release dvp prior to lookup
2045 nd.ni_cnd.cn_nameiop = LOOKUP;
2046 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
2047 nd.ni_cnd.cn_thread = td;
2048 nd.ni_cnd.cn_cred = td->td_ucred;
2049 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
2051 nd.ni_cnd.cn_flags |= GIANTHELD;
2052 error = lookup(&nd);
2054 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2055 nd.ni_cnd.cn_flags &= ~GIANTHELD;
2059 if (nd.ni_cnd.cn_flags & ISSYMLINK)
2064 * send response, cleanup, return.
2069 bzero((caddr_t)fhp, sizeof(nfh));
2070 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2071 error = VOP_VPTOFH(vp, &fhp->fh_fid);
2073 error = VOP_GETATTR(vp, vap, cred);
2076 if (nd.ni_dvp == nd.ni_vp)
2087 if (nd.ni_startdir) {
2088 vrele(nd.ni_startdir);
2089 nd.ni_startdir = NULL;
2091 NDFREE(&nd, NDF_ONLY_PNBUF);
2093 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2094 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2095 VOP_UNLOCK(dirp, 0);
2098 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2101 nfsm_srvpostop_fh(fhp);
2102 nfsm_srvpostop_attr(0, vap);
2104 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2106 vn_finished_write(mp);
2107 VFS_UNLOCK_GIANT(vfslocked);
2111 if (nd.ni_dvp == nd.ni_vp)
2121 vrele(nd.ni_startdir);
2122 NDFREE(&nd, NDF_ONLY_PNBUF);
2123 vn_finished_write(mp);
2124 VFS_UNLOCK_GIANT(vfslocked);
2129 * nfs remove service
2132 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2135 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2136 struct sockaddr *nam = nfsd->nd_nam;
2137 caddr_t dpos = nfsd->nd_dpos;
2138 struct ucred *cred = nfsd->nd_cr;
2139 struct nameidata nd;
2141 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2142 int v3 = (nfsd->nd_flag & ND_NFSV3);
2143 struct mbuf *mb, *mreq;
2145 struct vattr dirfor, diraft;
2148 struct mount *mp = NULL;
2151 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2155 fhp = &nfh.fh_generic;
2157 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2161 vfslocked = VFS_LOCK_GIANT(mp);
2162 (void) vn_start_write(NULL, &mp, V_WAIT);
2163 vfs_rel(mp); /* The write holds a ref. */
2164 nfsm_srvnamesiz(len);
2166 nd.ni_cnd.cn_cred = cred;
2167 nd.ni_cnd.cn_nameiop = DELETE;
2168 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | MPSAFE;
2169 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2170 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2171 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2177 if (nd.ni_vp->v_type == VDIR) {
2178 error = EPERM; /* POSIX */
2182 * The root of a mounted filesystem cannot be deleted.
2184 if (nd.ni_vp->v_vflag & VV_ROOT) {
2190 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2191 NDFREE(&nd, NDF_ONLY_PNBUF);
2195 if (dirp == nd.ni_dvp)
2196 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2198 /* Drop the other locks to avoid deadlock. */
2200 if (nd.ni_dvp == nd.ni_vp)
2210 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2211 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2212 VOP_UNLOCK(dirp, 0);
2218 nfsm_reply(NFSX_WCCDATA(v3));
2220 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2224 NDFREE(&nd, NDF_ONLY_PNBUF);
2226 if (nd.ni_dvp == nd.ni_vp)
2233 vn_finished_write(mp);
2234 VFS_UNLOCK_GIANT(vfslocked);
2239 * nfs rename service
2242 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2245 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2246 struct sockaddr *nam = nfsd->nd_nam;
2247 caddr_t dpos = nfsd->nd_dpos;
2248 struct ucred *cred = nfsd->nd_cr;
2250 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2251 int tdirfor_ret = 1, tdiraft_ret = 1;
2252 int v3 = (nfsd->nd_flag & ND_NFSV3);
2253 struct mbuf *mb, *mreq;
2254 struct nameidata fromnd, tond;
2255 struct vnode *fvp, *tvp, *tdvp, *fdirp = NULL;
2256 struct vnode *tdirp = NULL;
2257 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2259 fhandle_t *ffhp, *tfhp;
2261 struct mount *mp = NULL;
2264 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2269 ffhp = &fnfh.fh_generic;
2270 tfhp = &tnfh.fh_generic;
2273 * Clear fields incase goto nfsmout occurs from macro.
2279 nfsm_srvmtofh(ffhp);
2280 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
2284 vfslocked = VFS_LOCK_GIANT(mp);
2285 (void) vn_start_write(NULL, &mp, V_WAIT);
2286 vfs_rel(mp); /* The write holds a ref. */
2287 nfsm_srvnamesiz(len);
2289 * Remember our original uid so that we can reset cr_uid before
2290 * the second nfs_namei() call, in case it is remapped.
2292 saved_uid = cred->cr_uid;
2293 fromnd.ni_cnd.cn_cred = cred;
2294 fromnd.ni_cnd.cn_nameiop = DELETE;
2295 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART | MPSAFE;
2296 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2297 &dpos, &fdirp, v3, &fdirfor, &fdirfor_ret, FALSE);
2298 vfslocked = nfsrv_lockedpair_nd(vfslocked, &fromnd);
2304 nfsm_reply(2 * NFSX_WCCDATA(v3));
2306 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2307 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2313 nfsm_srvmtofh(tfhp);
2314 nfsm_srvnamesiz(len2);
2315 cred->cr_uid = saved_uid;
2316 tond.ni_cnd.cn_cred = cred;
2317 tond.ni_cnd.cn_nameiop = RENAME;
2318 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | MPSAFE;
2319 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2320 &dpos, &tdirp, v3, &tdirfor, &tdirfor_ret, FALSE);
2321 vfslocked = nfsrv_lockedpair_nd(vfslocked, &tond);
2332 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2338 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2345 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2353 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2360 if (fvp->v_mount != tdvp->v_mount) {
2374 * If source is the same as the destination (that is the
2375 * same vnode with the same name in the same directory),
2376 * then there is nothing to do.
2378 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2379 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2380 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2381 fromnd.ni_cnd.cn_namelen))
2386 * The VOP_RENAME function releases all vnode references &
2387 * locks prior to returning so we need to clear the pointers
2388 * to bypass cleanup code later on.
2390 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2391 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2392 fromnd.ni_dvp = NULL;
2393 fromnd.ni_vp = NULL;
2397 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2398 NDFREE(&tond, NDF_ONLY_PNBUF);
2406 nfsm_reply(2 * NFSX_WCCDATA(v3));
2408 /* Release existing locks to prevent deadlock. */
2410 if (tond.ni_dvp == tond.ni_vp)
2421 vn_lock(fdirp, LK_EXCLUSIVE | LK_RETRY);
2422 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
2423 VOP_UNLOCK(fdirp, 0);
2426 vn_lock(tdirp, LK_EXCLUSIVE | LK_RETRY);
2427 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
2428 VOP_UNLOCK(tdirp, 0);
2430 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2431 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2438 * Clear out tond related fields
2441 if (tond.ni_dvp == tond.ni_vp)
2450 if (tond.ni_startdir)
2451 vrele(tond.ni_startdir);
2452 NDFREE(&tond, NDF_ONLY_PNBUF);
2454 * Clear out fromnd related fields
2458 if (fromnd.ni_startdir)
2459 vrele(fromnd.ni_startdir);
2460 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2462 vrele(fromnd.ni_dvp);
2464 vrele(fromnd.ni_vp);
2466 vn_finished_write(mp);
2467 VFS_UNLOCK_GIANT(vfslocked);
2475 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2478 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2479 struct sockaddr *nam = nfsd->nd_nam;
2480 caddr_t dpos = nfsd->nd_dpos;
2481 struct ucred *cred = nfsd->nd_cr;
2482 struct nameidata nd;
2484 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2485 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2486 struct mbuf *mb, *mreq;
2487 struct vnode *vp = NULL, *xp, *dirp = NULL;
2488 struct vattr dirfor, diraft, at;
2490 fhandle_t *fhp, *dfhp;
2491 struct mount *mp = NULL;
2495 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2499 fhp = &nfh.fh_generic;
2500 dfhp = &dnfh.fh_generic;
2502 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2506 vfslocked = VFS_LOCK_GIANT(mp);
2507 (void) vn_start_write(NULL, &mp, V_WAIT);
2508 vfs_rel(mp); /* The write holds a ref. */
2509 nfsm_srvmtofh(dfhp);
2510 nfsm_srvnamesiz(len);
2512 error = nfsrv_fhtovp(fhp, TRUE, &vp, &tvfslocked, cred, slp,
2513 nam, &rdonly, TRUE);
2514 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
2516 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2518 nfsm_srvpostop_attr(getret, &at);
2519 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2526 getret = VOP_GETATTR(vp, &at, cred);
2527 if (vp->v_type == VDIR) {
2528 error = EPERM; /* POSIX */
2532 nd.ni_cnd.cn_cred = cred;
2533 nd.ni_cnd.cn_nameiop = CREATE;
2534 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE | MPSAFE;
2535 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2536 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2537 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2555 if (vp->v_mount != xp->v_mount) {
2561 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2562 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2563 NDFREE(&nd, NDF_ONLY_PNBUF);
2568 getret = VOP_GETATTR(vp, &at, cred);
2571 if (dirp == nd.ni_dvp)
2572 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2574 /* Release existing locks to prevent deadlock. */
2576 if (nd.ni_dvp == nd.ni_vp)
2586 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2587 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2588 VOP_UNLOCK(dirp, 0);
2592 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2594 nfsm_srvpostop_attr(getret, &at);
2595 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2601 NDFREE(&nd, NDF_ONLY_PNBUF);
2605 if (nd.ni_dvp == nd.ni_vp)
2614 vn_finished_write(mp);
2615 VFS_UNLOCK_GIANT(vfslocked);
2620 * nfs symbolic link service
2623 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2626 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2627 struct sockaddr *nam = nfsd->nd_nam;
2628 caddr_t dpos = nfsd->nd_dpos;
2629 struct ucred *cred = nfsd->nd_cr;
2630 struct vattr va, dirfor, diraft;
2631 struct nameidata nd;
2632 struct vattr *vap = &va;
2633 struct nfsv2_sattr *sp;
2634 char *bpos, *pathcp = NULL;
2637 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2638 int v3 = (nfsd->nd_flag & ND_NFSV3);
2639 struct mbuf *mb, *mreq;
2640 struct vnode *dirp = NULL;
2643 struct mount *mp = NULL;
2647 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2651 fhp = &nfh.fh_generic;
2653 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2657 vfslocked = VFS_LOCK_GIANT(mp);
2658 (void) vn_start_write(NULL, &mp, V_WAIT);
2659 vfs_rel(mp); /* The write holds a ref. */
2660 nfsm_srvnamesiz(len);
2661 nd.ni_cnd.cn_cred = cred;
2662 nd.ni_cnd.cn_nameiop = CREATE;
2663 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART | MPSAFE;
2664 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2665 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2666 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2671 nfsm_srvpathsiz(len2);
2679 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2680 iv.iov_base = pathcp;
2682 io.uio_resid = len2;
2686 io.uio_segflg = UIO_SYSSPACE;
2687 io.uio_rw = UIO_READ;
2689 nfsm_mtouio(&io, len2);
2691 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
2692 vap->va_mode = nfstov_mode(sp->sa_mode);
2694 *(pathcp + len2) = '\0';
2701 * issue symlink op. SAVESTART is set so the underlying path component
2702 * is only freed by the VOP if an error occurs.
2704 if (vap->va_mode == (mode_t)VNOVAL)
2706 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2708 NDFREE(&nd, NDF_ONLY_PNBUF);
2713 * releases directory prior to potential lookup op.
2721 * Issue lookup. Leave SAVESTART set so we can easily free
2722 * the name buffer later on.
2724 * since LOCKPARENT is not set, ni_dvp will be garbage on
2725 * return whether an error occurs or not.
2727 nd.ni_cnd.cn_nameiop = LOOKUP;
2728 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2729 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2730 nd.ni_cnd.cn_thread = curthread;
2731 nd.ni_cnd.cn_cred = cred;
2732 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
2734 nd.ni_cnd.cn_flags |= GIANTHELD;
2735 error = lookup(&nd);
2737 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2738 nd.ni_cnd.cn_flags &= ~GIANTHELD;
2741 bzero((caddr_t)fhp, sizeof(nfh));
2742 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2743 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2745 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2753 * These releases aren't strictly required, does even doing them
2754 * make any sense? XXX can nfsm_reply() block?
2757 FREE(pathcp, M_TEMP);
2761 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2762 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2763 VOP_UNLOCK(dirp, 0);
2765 if (nd.ni_startdir) {
2766 vrele(nd.ni_startdir);
2767 nd.ni_startdir = NULL;
2769 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2772 nfsm_srvpostop_fh(fhp);
2773 nfsm_srvpostop_attr(0, vap);
2775 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2781 NDFREE(&nd, NDF_ONLY_PNBUF);
2783 if (nd.ni_dvp == nd.ni_vp)
2791 vrele(nd.ni_startdir);
2795 FREE(pathcp, M_TEMP);
2797 vn_finished_write(mp);
2798 VFS_UNLOCK_GIANT(vfslocked);
2806 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2809 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2810 struct sockaddr *nam = nfsd->nd_nam;
2811 caddr_t dpos = nfsd->nd_dpos;
2812 struct ucred *cred = nfsd->nd_cr;
2813 struct vattr va, dirfor, diraft;
2814 struct vattr *vap = &va;
2815 struct nfs_fattr *fp;
2816 struct nameidata nd;
2819 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2820 int v3 = (nfsd->nd_flag & ND_NFSV3);
2821 struct mbuf *mb, *mreq;
2822 struct vnode *dirp = NULL;
2826 struct mount *mp = NULL;
2829 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2833 fhp = &nfh.fh_generic;
2835 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2839 vfslocked = VFS_LOCK_GIANT(mp);
2840 (void) vn_start_write(NULL, &mp, V_WAIT);
2841 vfs_rel(mp); /* The write holds a ref. */
2842 nfsm_srvnamesiz(len);
2843 nd.ni_cnd.cn_cred = cred;
2844 nd.ni_cnd.cn_nameiop = CREATE;
2845 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE;
2847 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2848 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2849 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2855 nfsm_reply(NFSX_WCCDATA(v3));
2857 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2865 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
2866 vap->va_mode = nfstov_mode(*tl++);
2870 * At this point nd.ni_dvp is referenced and exclusively locked and
2871 * nd.ni_vp, if it exists, is referenced but not locked.
2874 vap->va_type = VDIR;
2875 if (nd.ni_vp != NULL) {
2876 NDFREE(&nd, NDF_ONLY_PNBUF);
2882 * Issue mkdir op. Since SAVESTART is not set, the pathname
2883 * component is freed by the VOP call. This will fill-in
2884 * nd.ni_vp, reference, and exclusively lock it.
2886 if (vap->va_mode == (mode_t)VNOVAL)
2888 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2889 NDFREE(&nd, NDF_ONLY_PNBUF);
2896 bzero((caddr_t)fhp, sizeof(nfh));
2897 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2898 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2900 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2904 if (dirp == nd.ni_dvp) {
2905 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2907 /* Release existing locks to prevent deadlock. */
2909 NDFREE(&nd, NDF_ONLY_PNBUF);
2910 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2923 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2924 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2925 VOP_UNLOCK(dirp, 0);
2928 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2931 nfsm_srvpostop_fh(fhp);
2932 nfsm_srvpostop_attr(0, vap);
2934 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2935 } else if (!error) {
2936 /* v2 non-error case. */
2937 nfsm_srvfhtom(fhp, v3);
2938 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2939 nfsm_srvfillattr(vap, fp);
2946 NDFREE(&nd, NDF_ONLY_PNBUF);
2947 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2960 vn_finished_write(mp);
2961 VFS_UNLOCK_GIANT(vfslocked);
2969 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2972 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2973 struct sockaddr *nam = nfsd->nd_nam;
2974 caddr_t dpos = nfsd->nd_dpos;
2975 struct ucred *cred = nfsd->nd_cr;
2977 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2978 int v3 = (nfsd->nd_flag & ND_NFSV3);
2979 struct mbuf *mb, *mreq;
2980 struct vnode *vp, *dirp = NULL;
2981 struct vattr dirfor, diraft;
2984 struct nameidata nd;
2985 struct mount *mp = NULL;
2988 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2992 fhp = &nfh.fh_generic;
2994 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2998 vfslocked = VFS_LOCK_GIANT(mp);
2999 (void) vn_start_write(NULL, &mp, V_WAIT);
3000 vfs_rel(mp); /* The write holds a ref. */
3001 nfsm_srvnamesiz(len);
3002 nd.ni_cnd.cn_cred = cred;
3003 nd.ni_cnd.cn_nameiop = DELETE;
3004 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | MPSAFE;
3005 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
3006 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
3007 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
3013 nfsm_reply(NFSX_WCCDATA(v3));
3015 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
3020 if (vp->v_type != VDIR) {
3025 * No rmdir "." please.
3027 if (nd.ni_dvp == vp) {
3032 * The root of a mounted filesystem cannot be deleted.
3034 if (vp->v_vflag & VV_ROOT)
3038 * Issue or abort op. Since SAVESTART is not set, path name
3039 * component is freed by the VOP after either.
3042 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3043 NDFREE(&nd, NDF_ONLY_PNBUF);
3046 if (dirp == nd.ni_dvp)
3047 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
3049 /* Release existing locks to prevent deadlock. */
3051 if (nd.ni_dvp == nd.ni_vp)
3060 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
3061 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
3062 VOP_UNLOCK(dirp, 0);
3065 nfsm_reply(NFSX_WCCDATA(v3));
3068 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
3072 NDFREE(&nd, NDF_ONLY_PNBUF);
3074 if (nd.ni_dvp == nd.ni_vp)
3084 vn_finished_write(mp);
3085 VFS_UNLOCK_GIANT(vfslocked);
3090 * nfs readdir service
3091 * - mallocs what it thinks is enough to read
3092 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
3093 * - calls VOP_READDIR()
3094 * - loops around building the reply
3095 * if the output generated exceeds count break out of loop
3096 * The nfsm_clget macro is used here so that the reply will be packed
3097 * tightly in mbuf clusters.
3098 * - it only knows that it has encountered eof when the VOP_READDIR()
3100 * - as such one readdir rpc will return eof false although you are there
3101 * and then the next will return eof
3102 * - it trims out records with d_fileno == 0
3103 * this doesn't matter for Unix clients, but they might confuse clients
3105 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
3106 * than requested, but this may not apply to all filesystems. For
3107 * example, client NFS does not { although it is never remote mounted
3109 * The alternate call nfsrv_readdirplus() does lookups as well.
3110 * PS: The NFS protocol spec. does not clarify what the "count" byte
3111 * argument is a count of.. just name strings and file id's or the
3112 * entire reply rpc or ...
3113 * I tried just file name and id sizes and it confused the Sun client,
3114 * so I am using the full rpc size now. The "paranoia.." comment refers
3115 * to including the status longwords that are not a part of the dir.
3116 * "entry" structures, but are in the rpc.
3120 u_int32_t fl_postopok;
3121 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
3123 u_int32_t fl_fhsize;
3124 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
3128 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3131 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3132 struct sockaddr *nam = nfsd->nd_nam;
3133 caddr_t dpos = nfsd->nd_dpos;
3134 struct ucred *cred = nfsd->nd_cr;
3141 struct mbuf *mb, *mreq;
3142 char *cpos, *cend, *rbuf;
3143 struct vnode *vp = NULL;
3149 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3150 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
3151 int v3 = (nfsd->nd_flag & ND_NFSV3);
3152 u_quad_t off, toff, verf;
3153 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3156 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3158 fhp = &nfh.fh_generic;
3161 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
3162 toff = fxdr_hyper(tl);
3164 verf = fxdr_hyper(tl);
3167 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
3168 toff = fxdr_unsigned(u_quad_t, *tl++);
3169 verf = 0; /* shut up gcc */
3172 cnt = fxdr_unsigned(int, *tl);
3173 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3174 xfer = NFS_SRVMAXDATA(nfsd);
3180 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
3181 nam, &rdonly, TRUE);
3182 if (!error && vp->v_type != VDIR) {
3188 nfsm_reply(NFSX_UNSIGNED);
3190 nfsm_srvpostop_attr(getret, &at);
3196 * Obtain lock on vnode for this section of the code
3199 error = getret = VOP_GETATTR(vp, &at, cred);
3202 * XXX This check may be too strict for Solaris 2.5 clients.
3204 if (!error && toff && verf && verf != at.va_filerev)
3205 error = NFSERR_BAD_COOKIE;
3209 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
3213 nfsm_reply(NFSX_POSTOPATTR(v3));
3215 nfsm_srvpostop_attr(getret, &at);
3222 * end section. Allocate rbuf and continue
3224 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3227 iv.iov_len = fullsiz;
3230 io.uio_offset = (off_t)off;
3231 io.uio_resid = fullsiz;
3232 io.uio_segflg = UIO_SYSSPACE;
3233 io.uio_rw = UIO_READ;
3236 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3238 free((caddr_t)cookies, M_TEMP);
3241 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3242 off = (off_t)io.uio_offset;
3243 if (!cookies && !error)
3244 error = NFSERR_PERM;
3246 getret = VOP_GETATTR(vp, &at, cred);
3254 free((caddr_t)rbuf, M_TEMP);
3256 free((caddr_t)cookies, M_TEMP);
3257 nfsm_reply(NFSX_POSTOPATTR(v3));
3259 nfsm_srvpostop_attr(getret, &at);
3264 siz -= io.uio_resid;
3267 * If nothing read, return eof
3273 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3276 nfsm_srvpostop_attr(getret, &at);
3277 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3278 txdr_hyper(at.va_filerev, tl);
3281 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3282 *tl++ = nfsrv_nfs_false;
3283 *tl = nfsrv_nfs_true;
3284 FREE((caddr_t)rbuf, M_TEMP);
3285 FREE((caddr_t)cookies, M_TEMP);
3292 * Check for degenerate cases of nothing useful read.
3293 * If so go try again
3297 dp = (struct dirent *)cpos;
3300 * For some reason FreeBSD's ufs_readdir() chooses to back the
3301 * directory offset up to a block boundary, so it is necessary to
3302 * skip over the records that precede the requested offset. This
3303 * requires the assumption that file offset cookies monotonically
3306 while (cpos < cend && ncookies > 0 &&
3307 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3308 ((u_quad_t)(*cookiep)) <= toff)) {
3309 cpos += dp->d_reclen;
3310 dp = (struct dirent *)cpos;
3314 if (cpos >= cend || ncookies == 0) {
3320 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3321 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3323 nfsm_srvpostop_attr(getret, &at);
3324 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3325 txdr_hyper(at.va_filerev, tl);
3329 be = bp + M_TRAILINGSPACE(mp);
3331 /* Loop through the records and build reply */
3332 while (cpos < cend && ncookies > 0) {
3333 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3334 nlen = dp->d_namlen;
3335 rem = nfsm_rndup(nlen) - nlen;
3336 len += (4 * NFSX_UNSIGNED + nlen + rem);
3338 len += 2 * NFSX_UNSIGNED;
3344 * Build the directory record xdr from
3348 *tl = nfsrv_nfs_true;
3349 bp += NFSX_UNSIGNED;
3353 bp += NFSX_UNSIGNED;
3356 *tl = txdr_unsigned(dp->d_fileno);
3357 bp += NFSX_UNSIGNED;
3359 *tl = txdr_unsigned(nlen);
3360 bp += NFSX_UNSIGNED;
3362 /* And loop around copying the name */
3371 bcopy(cp, bp, tsiz);
3377 /* And null pad to an int32_t boundary. */
3378 for (i = 0; i < rem; i++)
3382 /* Finish off the record */
3385 bp += NFSX_UNSIGNED;
3388 *tl = txdr_unsigned(*cookiep);
3389 bp += NFSX_UNSIGNED;
3391 cpos += dp->d_reclen;
3392 dp = (struct dirent *)cpos;
3399 *tl = nfsrv_nfs_false;
3400 bp += NFSX_UNSIGNED;
3403 *tl = nfsrv_nfs_true;
3405 *tl = nfsrv_nfs_false;
3406 bp += NFSX_UNSIGNED;
3409 mp->m_len = bp - mtod(mp, caddr_t);
3411 mp->m_len += bp - bpos;
3412 FREE((caddr_t)rbuf, M_TEMP);
3413 FREE((caddr_t)cookies, M_TEMP);
3418 VFS_UNLOCK_GIANT(vfslocked);
3423 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3426 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3427 struct sockaddr *nam = nfsd->nd_nam;
3428 caddr_t dpos = nfsd->nd_dpos;
3429 struct ucred *cred = nfsd->nd_cr;
3436 struct mbuf *mb, *mreq;
3437 char *cpos, *cend, *rbuf;
3438 struct vnode *vp = NULL, *nvp;
3441 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3444 struct vattr va, at, *vap = &va;
3445 struct nfs_fattr *fp;
3446 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3447 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3448 u_quad_t off, toff, verf;
3449 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3450 int v3 = (nfsd->nd_flag & ND_NFSV3);
3453 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3456 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3457 fhp = &nfh.fh_generic;
3459 tl = nfsm_dissect_nonblock(u_int32_t *, 6 * NFSX_UNSIGNED);
3460 toff = fxdr_hyper(tl);
3462 verf = fxdr_hyper(tl);
3464 siz = fxdr_unsigned(int, *tl++);
3465 cnt = fxdr_unsigned(int, *tl);
3467 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3468 xfer = NFS_SRVMAXDATA(nfsd);
3474 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
3475 nam, &rdonly, TRUE);
3476 if (!error && vp->v_type != VDIR) {
3482 nfsm_reply(NFSX_UNSIGNED);
3483 nfsm_srvpostop_attr(getret, &at);
3487 error = getret = VOP_GETATTR(vp, &at, cred);
3490 * XXX This check may be too strict for Solaris 2.5 clients.
3492 if (!error && toff && verf && verf != at.va_filerev)
3493 error = NFSERR_BAD_COOKIE;
3496 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
3500 nfsm_reply(NFSX_V3POSTOPATTR);
3501 nfsm_srvpostop_attr(getret, &at);
3506 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3509 iv.iov_len = fullsiz;
3512 io.uio_offset = (off_t)off;
3513 io.uio_resid = fullsiz;
3514 io.uio_segflg = UIO_SYSSPACE;
3515 io.uio_rw = UIO_READ;
3518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3520 free((caddr_t)cookies, M_TEMP);
3523 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3524 off = (u_quad_t)io.uio_offset;
3525 getret = VOP_GETATTR(vp, &at, cred);
3527 if (!cookies && !error)
3528 error = NFSERR_PERM;
3535 free((caddr_t)cookies, M_TEMP);
3536 free((caddr_t)rbuf, M_TEMP);
3537 nfsm_reply(NFSX_V3POSTOPATTR);
3538 nfsm_srvpostop_attr(getret, &at);
3543 siz -= io.uio_resid;
3546 * If nothing read, return eof
3552 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3554 nfsm_srvpostop_attr(getret, &at);
3555 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3556 txdr_hyper(at.va_filerev, tl);
3558 *tl++ = nfsrv_nfs_false;
3559 *tl = nfsrv_nfs_true;
3560 FREE((caddr_t)cookies, M_TEMP);
3561 FREE((caddr_t)rbuf, M_TEMP);
3568 * Check for degenerate cases of nothing useful read.
3569 * If so go try again
3573 dp = (struct dirent *)cpos;
3576 * For some reason FreeBSD's ufs_readdir() chooses to back the
3577 * directory offset up to a block boundary, so it is necessary to
3578 * skip over the records that precede the requested offset. This
3579 * requires the assumption that file offset cookies monotonically
3582 while (cpos < cend && ncookies > 0 &&
3583 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3584 ((u_quad_t)(*cookiep)) <= toff)) {
3585 cpos += dp->d_reclen;
3586 dp = (struct dirent *)cpos;
3590 if (cpos >= cend || ncookies == 0) {
3597 * Probe one of the directory entries to see if the filesystem
3600 if (VFS_VGET(vp->v_mount, dp->d_fileno, LK_EXCLUSIVE, &nvp) ==
3602 error = NFSERR_NOTSUPP;
3605 free((caddr_t)cookies, M_TEMP);
3606 free((caddr_t)rbuf, M_TEMP);
3607 nfsm_reply(NFSX_V3POSTOPATTR);
3608 nfsm_srvpostop_attr(getret, &at);
3615 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3618 nfsm_srvpostop_attr(getret, &at);
3619 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3620 txdr_hyper(at.va_filerev, tl);
3623 be = bp + M_TRAILINGSPACE(mp);
3625 /* Loop through the records and build reply */
3626 while (cpos < cend && ncookies > 0) {
3627 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3628 nlen = dp->d_namlen;
3629 rem = nfsm_rndup(nlen)-nlen;
3632 * For readdir_and_lookup get the vnode using
3635 if (VFS_VGET(vp->v_mount, dp->d_fileno, LK_EXCLUSIVE,
3638 bzero((caddr_t)nfhp, NFSX_V3FH);
3640 nvp->v_mount->mnt_stat.f_fsid;
3642 * XXXRW: Assert the mountpoints are the same so that
3643 * we know that acquiring Giant based on the
3644 * directory is the right thing for the child.
3646 KASSERT(nvp->v_mount == vp->v_mount,
3647 ("nfsrv_readdirplus: nvp mount != vp mount"));
3648 if (VOP_VPTOFH(nvp, &nfhp->fh_fid)) {
3653 if (VOP_GETATTR(nvp, vap, cred)) {
3662 * If either the dircount or maxcount will be
3663 * exceeded, get out now. Both of these lengths
3664 * are calculated conservatively, including all
3667 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3669 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3670 if (len > cnt || dirlen > fullsiz) {
3676 * Build the directory record xdr from
3679 fp = (struct nfs_fattr *)&fl.fl_fattr;
3680 nfsm_srvfillattr(vap, fp);
3681 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3682 fl.fl_fhok = nfsrv_nfs_true;
3683 fl.fl_postopok = nfsrv_nfs_true;
3684 fl.fl_off.nfsuquad[0] = 0;
3685 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3688 *tl = nfsrv_nfs_true;
3689 bp += NFSX_UNSIGNED;
3692 bp += NFSX_UNSIGNED;
3694 *tl = txdr_unsigned(dp->d_fileno);
3695 bp += NFSX_UNSIGNED;
3697 *tl = txdr_unsigned(nlen);
3698 bp += NFSX_UNSIGNED;
3700 /* And loop around copying the name */
3705 if ((bp + xfer) > be)
3709 bcopy(cp, bp, tsiz);
3715 /* And null pad to an int32_t boundary. */
3716 for (i = 0; i < rem; i++)
3720 * Now copy the flrep structure out.
3722 xfer = sizeof (struct flrep);
3726 if ((bp + xfer) > be)
3730 bcopy(cp, bp, tsiz);
3738 cpos += dp->d_reclen;
3739 dp = (struct dirent *)cpos;
3746 *tl = nfsrv_nfs_false;
3747 bp += NFSX_UNSIGNED;
3750 *tl = nfsrv_nfs_true;
3752 *tl = nfsrv_nfs_false;
3753 bp += NFSX_UNSIGNED;
3756 mp->m_len = bp - mtod(mp, caddr_t);
3758 mp->m_len += bp - bpos;
3759 FREE((caddr_t)cookies, M_TEMP);
3760 FREE((caddr_t)rbuf, M_TEMP);
3764 VFS_UNLOCK_GIANT(vfslocked);
3769 * nfs commit service
3772 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3775 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3776 struct sockaddr *nam = nfsd->nd_nam;
3777 caddr_t dpos = nfsd->nd_dpos;
3778 struct ucred *cred = nfsd->nd_cr;
3779 struct vattr bfor, aft;
3780 struct vnode *vp = NULL;
3785 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3786 struct mbuf *mb, *mreq;
3788 struct mount *mp = NULL;
3789 int v3 = (nfsd->nd_flag & ND_NFSV3);
3793 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3796 panic("nfsrv_commit: v3 proc called on a v2 connection");
3797 fhp = &nfh.fh_generic;
3799 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3803 vfslocked = VFS_LOCK_GIANT(mp);
3804 (void) vn_start_write(NULL, &mp, V_WAIT);
3805 vfs_rel(mp); /* The write holds a ref. */
3806 tl = nfsm_dissect_nonblock(u_int32_t *, 3 * NFSX_UNSIGNED);
3809 * XXX At this time VOP_FSYNC() does not accept offset and byte
3810 * count parameters, so these arguments are useless (someday maybe).
3812 off = fxdr_hyper(tl);
3814 cnt = fxdr_unsigned(int, *tl);
3815 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, cred, slp,
3816 nam, &rdonly, TRUE);
3817 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
3819 nfsm_reply(2 * NFSX_UNSIGNED);
3820 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3824 for_ret = VOP_GETATTR(vp, &bfor, cred);
3826 if (cnt > MAX_COMMIT_COUNT) {
3828 * Give up and do the whole thing
3831 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3832 VM_OBJECT_LOCK(vp->v_object);
3833 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3834 VM_OBJECT_UNLOCK(vp->v_object);
3836 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3839 * Locate and synchronously write any buffers that fall
3840 * into the requested range. Note: we are assuming that
3841 * f_iosize is a power of 2.
3843 int iosize = vp->v_mount->mnt_stat.f_iosize;
3844 int iomask = iosize - 1;
3849 * Align to iosize boundry, super-align to page boundry.
3852 cnt += off & iomask;
3853 off &= ~(u_quad_t)iomask;
3855 if (off & PAGE_MASK) {
3856 cnt += off & PAGE_MASK;
3857 off &= ~(u_quad_t)PAGE_MASK;
3859 lblkno = off / iosize;
3862 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3863 VM_OBJECT_LOCK(vp->v_object);
3864 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3865 VM_OBJECT_UNLOCK(vp->v_object);
3874 * If we have a buffer and it is marked B_DELWRI we
3875 * have to lock and write it. Otherwise the prior
3876 * write is assumed to have already been committed.
3878 * gbincore() can return invalid buffers now so we
3879 * have to check that bit as well (though B_DELWRI
3880 * should not be set if B_INVAL is set there could be
3881 * a race here since we haven't locked the buffer).
3883 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
3884 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
3885 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
3887 continue; /* retry */
3889 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
3892 bp->b_flags &= ~B_ASYNC;
3908 aft_ret = VOP_GETATTR(vp, &aft, cred);
3912 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3913 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3915 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3916 if (nfsver.tv_sec == 0)
3918 *tl++ = txdr_unsigned(nfsver.tv_sec);
3919 *tl = txdr_unsigned(nfsver.tv_usec);
3926 vn_finished_write(mp);
3927 VFS_UNLOCK_GIANT(vfslocked);
3932 * nfs statfs service
3935 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3938 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3939 struct sockaddr *nam = nfsd->nd_nam;
3940 caddr_t dpos = nfsd->nd_dpos;
3941 struct ucred *cred = nfsd->nd_cr;
3943 struct nfs_statfs *sfp;
3945 int error = 0, rdonly, getret = 1;
3946 int v3 = (nfsd->nd_flag & ND_NFSV3);
3947 struct mbuf *mb, *mreq;
3948 struct vnode *vp = NULL;
3952 struct statfs statfs;
3956 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3958 fhp = &nfh.fh_generic;
3960 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
3961 nam, &rdonly, TRUE);
3963 nfsm_reply(NFSX_UNSIGNED);
3965 nfsm_srvpostop_attr(getret, &at);
3970 error = VFS_STATFS(vp->v_mount, sf, curthread);
3971 getret = VOP_GETATTR(vp, &at, cred);
3974 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3976 nfsm_srvpostop_attr(getret, &at);
3981 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3983 tval = (u_quad_t)sf->f_blocks;
3984 tval *= (u_quad_t)sf->f_bsize;
3985 txdr_hyper(tval, &sfp->sf_tbytes);
3986 tval = (u_quad_t)sf->f_bfree;
3987 tval *= (u_quad_t)sf->f_bsize;
3988 txdr_hyper(tval, &sfp->sf_fbytes);
3990 * Don't send negative values for available space,
3991 * since this field is unsigned in the NFS protocol.
3992 * Otherwise, the client would see absurdly high
3993 * numbers for free space.
3995 if (sf->f_bavail < 0)
3998 tval = (u_quad_t)sf->f_bavail;
3999 tval *= (u_quad_t)sf->f_bsize;
4000 txdr_hyper(tval, &sfp->sf_abytes);
4001 sfp->sf_tfiles.nfsuquad[0] = 0;
4002 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
4003 sfp->sf_ffiles.nfsuquad[0] = 0;
4004 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
4005 sfp->sf_afiles.nfsuquad[0] = 0;
4006 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
4007 sfp->sf_invarsec = 0;
4009 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
4010 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
4011 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
4012 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
4013 if (sf->f_bavail < 0)
4016 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
4021 VFS_UNLOCK_GIANT(vfslocked);
4026 * nfs fsinfo service
4029 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4032 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
4033 struct sockaddr *nam = nfsd->nd_nam;
4034 caddr_t dpos = nfsd->nd_dpos;
4035 struct ucred *cred = nfsd->nd_cr;
4036 struct nfsv3_fsinfo *sip;
4038 int error = 0, rdonly, getret = 1, pref;
4039 struct mbuf *mb, *mreq;
4040 struct vnode *vp = NULL;
4046 int v3 = (nfsd->nd_flag & ND_NFSV3);
4049 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4051 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
4052 fhp = &nfh.fh_generic;
4055 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
4056 nam, &rdonly, TRUE);
4058 nfsm_reply(NFSX_UNSIGNED);
4059 nfsm_srvpostop_attr(getret, &at);
4064 /* XXX Try to make a guess on the max file size. */
4065 VFS_STATFS(vp->v_mount, &sb, curthread);
4066 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
4068 getret = VOP_GETATTR(vp, &at, cred);
4071 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
4072 nfsm_srvpostop_attr(getret, &at);
4073 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
4077 * There should be filesystem VFS OP(s) to get this information.
4078 * For now, assume ufs.
4080 if (slp->ns_so->so_type == SOCK_DGRAM)
4081 pref = NFS_MAXDGRAMDATA;
4084 sip->fs_rtmax = txdr_unsigned(pref);
4085 sip->fs_rtpref = txdr_unsigned(pref);
4086 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
4087 sip->fs_wtmax = txdr_unsigned(pref);
4088 sip->fs_wtpref = txdr_unsigned(pref);
4089 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
4090 sip->fs_dtpref = txdr_unsigned(pref);
4091 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
4092 sip->fs_timedelta.nfsv3_sec = 0;
4093 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
4094 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
4095 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
4096 NFSV3FSINFO_CANSETTIME);
4100 VFS_UNLOCK_GIANT(vfslocked);
4105 * nfs pathconf service
4108 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4111 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
4112 struct sockaddr *nam = nfsd->nd_nam;
4113 caddr_t dpos = nfsd->nd_dpos;
4114 struct ucred *cred = nfsd->nd_cr;
4115 struct nfsv3_pathconf *pc;
4117 int error = 0, rdonly, getret = 1;
4118 register_t linkmax, namemax, chownres, notrunc;
4119 struct mbuf *mb, *mreq;
4120 struct vnode *vp = NULL;
4124 int v3 = (nfsd->nd_flag & ND_NFSV3);
4127 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4129 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
4131 fhp = &nfh.fh_generic;
4133 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
4134 nam, &rdonly, TRUE);
4136 nfsm_reply(NFSX_UNSIGNED);
4137 nfsm_srvpostop_attr(getret, &at);
4141 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
4143 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
4145 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
4147 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
4148 getret = VOP_GETATTR(vp, &at, cred);
4151 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
4152 nfsm_srvpostop_attr(getret, &at);
4157 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
4159 pc->pc_linkmax = txdr_unsigned(linkmax);
4160 pc->pc_namemax = txdr_unsigned(namemax);
4161 pc->pc_notrunc = txdr_unsigned(notrunc);
4162 pc->pc_chownrestricted = txdr_unsigned(chownres);
4165 * These should probably be supported by VOP_PATHCONF(), but
4166 * until msdosfs is exportable (why would you want to?), the
4167 * Unix defaults should be ok.
4169 pc->pc_caseinsensitive = nfsrv_nfs_false;
4170 pc->pc_casepreserving = nfsrv_nfs_true;
4174 VFS_UNLOCK_GIANT(vfslocked);
4179 * Null operation, used by clients to ping server
4183 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4186 struct mbuf *mrep = nfsd->nd_mrep;
4188 int error = NFSERR_RETVOID;
4189 struct mbuf *mb, *mreq;
4191 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4198 * No operation, used for obsolete procedures
4202 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4205 struct mbuf *mrep = nfsd->nd_mrep;
4208 struct mbuf *mb, *mreq;
4210 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4211 if (nfsd->nd_repstat)
4212 error = nfsd->nd_repstat;
4214 error = EPROCUNAVAIL;
4222 * Perform access checking for vnodes obtained from file handles that would
4223 * refer to files already opened by a Unix client. You cannot just use
4224 * vn_writechk() and VOP_ACCESS() for two reasons.
4225 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
4227 * 2 - The owner is to be given access irrespective of mode bits for some
4228 * operations, so that processes that chmod after opening a file don't
4229 * break. I don't like this because it opens a security hole, but since
4230 * the nfs server opens a security hole the size of a barn door anyhow,
4233 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4234 * will return EPERM instead of EACCESS. EPERM is always an error.
4237 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
4243 VFS_ASSERT_GIANT(vp->v_mount);
4245 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4247 if (flags & VWRITE) {
4248 /* Just vn_writechk() changed to check rdonly */
4250 * Disallow write attempts on read-only filesystems;
4251 * unless the file is a socket or a block or character
4252 * device resident on the filesystem.
4254 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4255 switch (vp->v_type) {
4265 * If there's shared text associated with
4266 * the inode, we can't allow writing.
4268 if (vp->v_vflag & VV_TEXT)
4272 error = VOP_GETATTR(vp, &vattr, cred);
4275 error = VOP_ACCESS(vp, flags, cred, curthread);
4277 * Allow certain operations for the owner (reads and writes
4278 * on files that are already open).
4280 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)