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,
142 struct thread *, int);
143 static void nfsrvw_coalesce(struct nfsrv_descript *,
144 struct nfsrv_descript *);
147 * Clear nameidata fields that are tested in nsfmout cleanup code prior
148 * to using first nfsm macro (that might jump to the cleanup code).
152 ndclear(struct nameidata *nd)
155 nd->ni_cnd.cn_flags = 0;
158 nd->ni_startdir = NULL;
162 * Takes two vfslocked integers and returns with at most one
163 * reference to giant. The return value indicates whether giant
164 * is held by either lock. This simplifies nfsrv ops by allowing
165 * them to track only one vfslocked var.
168 nfsrv_lockedpair(int vfs1, int vfs2)
172 VFS_UNLOCK_GIANT(vfs2);
174 return (vfs1 | vfs2);
178 nfsrv_lockedpair_nd(int vfs1, struct nameidata *nd)
182 vfs2 = NDHASGIANT(nd);
184 return nfsrv_lockedpair(vfs1, vfs2);
188 * nfs v3 access service
191 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
192 struct thread *td, struct mbuf **mrq)
194 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
195 struct sockaddr *nam = nfsd->nd_nam;
196 caddr_t dpos = nfsd->nd_dpos;
197 struct ucred *cred = nfsd->nd_cr;
198 struct vnode *vp = NULL;
203 int error = 0, rdonly, getret;
204 struct mbuf *mb, *mreq;
205 struct vattr vattr, *vap = &vattr;
206 u_long testmode, nfsmode;
207 int v3 = (nfsd->nd_flag & ND_NFSV3);
210 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
212 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, td, 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, td, 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, td, 0))
242 nfsmode &= ~testmode;
243 getret = VOP_GETATTR(vp, vap, cred, td);
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,
262 struct thread *td, struct mbuf **mrq)
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, td);
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,
315 struct thread *td, struct mbuf **mrq)
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, td);
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, td);
441 postat_ret = VOP_GETATTR(vp, vap, cred, td);
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,
473 struct thread *td, struct mbuf **mrq)
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, td, 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, td);
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,
657 struct thread *td, struct mbuf **mrq)
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_TRYWAIT, MT_DATA);
690 MCLGET(nmp, M_TRYWAIT);
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, td);
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,
764 struct thread *td, struct mbuf **mrq)
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,
831 error = nfsrv_access(vp, VEXEC, cred,
834 getret = VOP_GETATTR(vp, vap, cred, td);
840 nfsm_reply(NFSX_POSTOPATTR(v3));
842 nfsm_srvpostop_attr(getret, vap);
848 * Calculate byte count to read
851 if (off >= vap->va_size)
853 else if ((off + reqlen) > vap->va_size)
854 cnt = vap->va_size - off;
859 * Calculate seqcount for heuristic
867 * Locate best candidate
870 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
874 if (nfsheur[hi].nh_vp == vp) {
878 if (nfsheur[hi].nh_use > 0)
879 --nfsheur[hi].nh_use;
880 hi = (hi + 1) % NUM_HEURISTIC;
881 if (nfsheur[hi].nh_use < nh->nh_use)
885 if (nh->nh_vp != vp) {
888 nh->nh_use = NHUSE_INIT;
896 * Calculate heuristic
899 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
900 if (++nh->nh_seqcount > IO_SEQMAX)
901 nh->nh_seqcount = IO_SEQMAX;
902 } else if (nh->nh_seqcount > 1) {
907 nh->nh_use += NHUSE_INC;
908 if (nh->nh_use > NHUSE_MAX)
909 nh->nh_use = NHUSE_MAX;
910 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
913 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
915 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
916 *tl++ = nfsrv_nfs_true;
917 fp = (struct nfs_fattr *)tl;
918 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
920 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
921 fp = (struct nfs_fattr *)tl;
922 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
924 len = left = nfsm_rndup(cnt);
927 * Generate the mbuf list with the uio_iov ref. to it.
932 siz = min(M_TRAILINGSPACE(m), left);
938 MGET(m, M_TRYWAIT, MT_DATA);
939 MCLGET(m, M_TRYWAIT);
945 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
947 uiop->uio_iov = iv2 = iv;
953 panic("nfsrv_read iov");
954 siz = min(M_TRAILINGSPACE(m), left);
956 iv->iov_base = mtod(m, caddr_t) + m->m_len;
965 uiop->uio_iovcnt = i;
966 uiop->uio_offset = off;
967 uiop->uio_resid = len;
968 uiop->uio_rw = UIO_READ;
969 uiop->uio_segflg = UIO_SYSSPACE;
970 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
971 off = uiop->uio_offset;
973 FREE((caddr_t)iv2, M_TEMP);
974 if (error || (getret = VOP_GETATTR(vp, vap, cred, td))) {
980 nfsm_reply(NFSX_POSTOPATTR(v3));
982 nfsm_srvpostop_attr(getret, vap);
990 nfsm_srvfillattr(vap, fp);
991 tlen = len - uiop->uio_resid;
992 cnt = cnt < tlen ? cnt : tlen;
993 tlen = nfsm_rndup(cnt);
994 if (len != tlen || tlen != cnt)
995 nfsm_adj(mb, len - tlen, tlen - cnt);
997 *tl++ = txdr_unsigned(cnt);
999 *tl++ = nfsrv_nfs_true;
1001 *tl++ = nfsrv_nfs_false;
1003 *tl = txdr_unsigned(cnt);
1007 VFS_UNLOCK_GIANT(vfslocked);
1015 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1016 struct thread *td, struct mbuf **mrq)
1018 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1019 struct sockaddr *nam = nfsd->nd_nam;
1020 caddr_t dpos = nfsd->nd_dpos;
1021 struct ucred *cred = nfsd->nd_cr;
1025 struct nfs_fattr *fp;
1027 struct vattr va, forat;
1028 struct vattr *vap = &va;
1031 int error = 0, rdonly, len, forat_ret = 1;
1032 int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
1033 int stable = NFSV3WRITE_FILESYNC;
1034 int v3 = (nfsd->nd_flag & ND_NFSV3);
1035 struct mbuf *mb, *mreq;
1036 struct vnode *vp = NULL;
1039 struct uio io, *uiop = &io;
1041 struct mount *mntp = NULL;
1045 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1052 fhp = &nfh.fh_generic;
1054 if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1058 vfslocked = VFS_LOCK_GIANT(mntp);
1059 (void) vn_start_write(NULL, &mntp, V_WAIT);
1060 vfs_rel(mntp); /* The write holds a ref. */
1062 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
1063 off = fxdr_hyper(tl);
1065 stable = fxdr_unsigned(int, *tl++);
1067 tl = nfsm_dissect_nonblock(u_int32_t *, 4 * NFSX_UNSIGNED);
1068 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1071 stable = NFSV3WRITE_UNSTABLE;
1073 retlen = len = fxdr_unsigned(int32_t, *tl);
1077 * For NFS Version 2, it is not obvious what a write of zero length
1078 * should do, but I might as well be consistent with Version 3,
1079 * which is to return ok so long as there are no permission problems.
1087 adjust = dpos - mtod(mp, caddr_t);
1088 mp->m_len -= adjust;
1089 if (mp->m_len > 0 && adjust > 0)
1090 mp->m_data += adjust;
1094 else if (mp->m_len > 0) {
1097 mp->m_len -= (i - len);
1106 if (len > NFS_MAXDATA || len < 0 || i < len) {
1108 nfsm_reply(2 * NFSX_UNSIGNED);
1110 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1114 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, cred, slp,
1115 nam, &rdonly, TRUE);
1116 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
1119 nfsm_reply(2 * NFSX_UNSIGNED);
1121 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1126 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1127 if (vp->v_type != VREG) {
1131 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1134 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1138 nfsm_reply(NFSX_WCCDATA(v3));
1140 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1146 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1148 uiop->uio_iov = iv = ivp;
1149 uiop->uio_iovcnt = cnt;
1152 if (mp->m_len > 0) {
1153 ivp->iov_base = mtod(mp, caddr_t);
1154 ivp->iov_len = mp->m_len;
1162 * The IO_METASYNC flag indicates that all metadata (and not just
1163 * enough to ensure data integrity) mus be written to stable storage
1165 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1167 if (stable == NFSV3WRITE_UNSTABLE)
1168 ioflags = IO_NODELOCKED;
1169 else if (stable == NFSV3WRITE_DATASYNC)
1170 ioflags = (IO_SYNC | IO_NODELOCKED);
1172 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1173 uiop->uio_resid = len;
1174 uiop->uio_rw = UIO_WRITE;
1175 uiop->uio_segflg = UIO_SYSSPACE;
1176 uiop->uio_td = NULL;
1177 uiop->uio_offset = off;
1178 error = VOP_WRITE(vp, uiop, ioflags, cred);
1179 /* XXXRW: unlocked write. */
1180 nfsrvstats.srvvop_writes++;
1181 FREE((caddr_t)iv, M_TEMP);
1183 aftat_ret = VOP_GETATTR(vp, vap, cred, td);
1189 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1190 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1192 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1197 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1198 *tl++ = txdr_unsigned(retlen);
1200 * If nfs_async is set, then pretend the write was FILESYNC.
1202 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1203 *tl++ = txdr_unsigned(stable);
1205 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1207 * Actually, there is no need to txdr these fields,
1208 * but it may make the values more human readable,
1209 * for debugging purposes.
1211 if (nfsver.tv_sec == 0)
1213 *tl++ = txdr_unsigned(nfsver.tv_sec);
1214 *tl = txdr_unsigned(nfsver.tv_usec);
1215 } else if (!error) {
1216 /* v2 non-error case. */
1217 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1218 nfsm_srvfillattr(vap, fp);
1224 vn_finished_write(mntp);
1225 VFS_UNLOCK_GIANT(vfslocked);
1230 * For the purposes of write gathering, we must decide if the credential
1231 * associated with two pending requests have equivilent privileges. Since
1232 * NFS only uses a subset of the BSD ucred -- the effective uid and group
1233 * IDs -- we have a compare routine that checks only the relevant fields.
1236 nfsrv_samecred(struct ucred *cr1, struct ucred *cr2)
1240 if (cr1->cr_uid != cr2->cr_uid)
1242 if (cr1->cr_ngroups != cr2->cr_ngroups)
1244 for (i = 0; i < cr1->cr_ngroups; i++) {
1245 if (cr1->cr_groups[i] != cr2->cr_groups[i])
1252 * NFS write service with write gathering support. Called when
1253 * nfsrvw_procrastinate > 0.
1254 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1255 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1259 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1260 struct thread *td, struct mbuf **mrq)
1264 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1265 struct nfs_fattr *fp;
1268 struct nfsrvw_delayhash *wpp;
1270 struct vattr va, forat;
1273 int error = 0, rdonly, len, forat_ret = 1;
1274 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1275 struct mbuf *mb, *mreq, *mrep, *md;
1276 struct vnode *vp = NULL;
1277 struct uio io, *uiop = &io;
1279 struct mount *mntp = NULL;
1283 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1292 mrep = nfsd->nd_mrep;
1294 dpos = nfsd->nd_dpos;
1296 v3 = (nfsd->nd_flag & ND_NFSV3);
1297 LIST_INIT(&nfsd->nd_coalesce);
1298 nfsd->nd_mreq = NULL;
1299 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1300 cur_usec = nfs_curusec();
1301 nfsd->nd_time = cur_usec +
1302 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1305 * Now, get the write header..
1307 nfsm_srvmtofh(&nfsd->nd_fh);
1309 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
1310 nfsd->nd_off = fxdr_hyper(tl);
1312 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1314 tl = nfsm_dissect_nonblock(u_int32_t *, 4 * NFSX_UNSIGNED);
1315 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1318 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1320 len = fxdr_unsigned(int32_t, *tl);
1322 nfsd->nd_eoff = nfsd->nd_off + len;
1325 * Trim the header out of the mbuf list and trim off any trailing
1326 * junk so that the mbuf list has only the write data.
1334 adjust = dpos - mtod(mp, caddr_t);
1335 mp->m_len -= adjust;
1336 if (mp->m_len > 0 && adjust > 0)
1337 mp->m_data += adjust;
1344 mp->m_len -= (i - len);
1350 if (len > NFS_MAXDATA || len < 0 || i < len) {
1354 nfsm_writereply(2 * NFSX_UNSIGNED);
1356 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1357 nfsd->nd_mreq = mreq;
1358 nfsd->nd_mrep = NULL;
1363 * Add this entry to the hash and time queues.
1367 wp = LIST_FIRST(&slp->ns_tq);
1368 while (wp && wp->nd_time < nfsd->nd_time) {
1370 wp = LIST_NEXT(wp, nd_tq);
1372 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1374 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1376 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1378 if (nfsd->nd_mrep) {
1379 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1381 wp = LIST_FIRST(wpp);
1383 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)){
1385 wp = LIST_NEXT(wp, nd_hash);
1387 while (wp && wp->nd_off < nfsd->nd_off &&
1388 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)) {
1390 wp = LIST_NEXT(wp, nd_hash);
1393 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1396 * Search the hash list for overlapping entries and
1399 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1400 wp = LIST_NEXT(nfsd, nd_hash);
1401 if (nfsrv_samecred(owp->nd_cr, nfsd->nd_cr))
1402 nfsrvw_coalesce(owp, nfsd);
1405 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1412 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1413 * and generate the associated reply mbuf list(s).
1416 cur_usec = nfs_curusec();
1418 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1419 owp = LIST_NEXT(nfsd, nd_tq);
1420 if (nfsd->nd_time > cur_usec)
1424 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1425 LIST_REMOVE(nfsd, nd_tq);
1426 LIST_REMOVE(nfsd, nd_hash);
1428 mrep = nfsd->nd_mrep;
1429 nfsd->nd_mrep = NULL;
1431 v3 = (nfsd->nd_flag & ND_NFSV3);
1432 forat_ret = aftat_ret = 1;
1433 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, &vfslocked, cred,
1434 slp, nfsd->nd_nam, &rdonly, TRUE);
1437 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1438 if (vp->v_type != VREG) {
1442 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1448 error = nfsrv_access(vp, VWRITE, cred, rdonly,
1450 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1451 ioflags = IO_NODELOCKED;
1452 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1453 ioflags = (IO_SYNC | IO_NODELOCKED);
1455 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1456 uiop->uio_rw = UIO_WRITE;
1457 uiop->uio_segflg = UIO_SYSSPACE;
1458 uiop->uio_td = NULL;
1459 uiop->uio_offset = nfsd->nd_off;
1460 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1461 if (uiop->uio_resid > 0) {
1469 uiop->uio_iovcnt = i;
1470 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1472 uiop->uio_iov = ivp = iov;
1475 if (mp->m_len > 0) {
1476 ivp->iov_base = mtod(mp, caddr_t);
1477 ivp->iov_len = mp->m_len;
1484 if (vn_start_write(vp, &mntp, V_NOWAIT) != 0) {
1486 error = vn_start_write(NULL, &mntp, V_WAIT);
1487 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1489 mvfslocked = VFS_LOCK_GIANT(mntp);
1492 error = VOP_WRITE(vp, uiop, ioflags, cred);
1493 /* XXXRW: unlocked write. */
1494 nfsrvstats.srvvop_writes++;
1495 vn_finished_write(mntp);
1497 VFS_UNLOCK_GIANT(mvfslocked);
1498 FREE((caddr_t)iov, M_TEMP);
1502 aftat_ret = VOP_GETATTR(vp, &va, cred, td);
1506 VFS_UNLOCK_GIANT(vfslocked);
1508 * Loop around generating replies for all write rpcs that have
1509 * now been completed.
1513 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1515 nfsm_writereply(NFSX_WCCDATA(v3));
1517 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1520 nfsm_writereply(NFSX_PREOPATTR(v3) +
1521 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1522 NFSX_WRITEVERF(v3));
1524 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1525 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1526 *tl++ = txdr_unsigned(nfsd->nd_len);
1527 *tl++ = txdr_unsigned(swp->nd_stable);
1529 * Actually, there is no need to txdr these fields,
1530 * but it may make the values more human readable,
1531 * for debugging purposes.
1533 if (nfsver.tv_sec == 0)
1535 *tl++ = txdr_unsigned(nfsver.tv_sec);
1536 *tl = txdr_unsigned(nfsver.tv_usec);
1538 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1539 nfsm_srvfillattr(&va, fp);
1542 nfsd->nd_mreq = mreq;
1544 panic("nfsrv_write: nd_mrep not free");
1547 * Done. Put it at the head of the timer queue so that
1548 * the final phase can return the reply.
1553 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1555 nfsd = LIST_FIRST(&swp->nd_coalesce);
1557 LIST_REMOVE(nfsd, nd_tq);
1563 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1570 * Search for a reply to return.
1573 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq)
1574 if (nfsd->nd_mreq) {
1575 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1576 LIST_REMOVE(nfsd, nd_tq);
1577 *mrq = nfsd->nd_mreq;
1586 * Coalesce the write request nfsd into owp. To do this we must:
1587 * - remove nfsd from the queues
1588 * - merge nfsd->nd_mrep into owp->nd_mrep
1589 * - update the nd_eoff and nd_stable for owp
1590 * - put nfsd on owp's nd_coalesce list
1591 * NB: Must be called at splsoftclock().
1594 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1598 struct nfsrv_descript *p;
1600 NFS_DPF(WG, ("C%03x-%03x",
1601 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1602 LIST_REMOVE(nfsd, nd_hash);
1603 LIST_REMOVE(nfsd, nd_tq);
1604 if (owp->nd_eoff < nfsd->nd_eoff) {
1605 overlap = owp->nd_eoff - nfsd->nd_off;
1607 panic("nfsrv_coalesce: bad off");
1609 m_adj(nfsd->nd_mrep, overlap);
1613 mp->m_next = nfsd->nd_mrep;
1614 owp->nd_eoff = nfsd->nd_eoff;
1616 m_freem(nfsd->nd_mrep);
1617 nfsd->nd_mrep = NULL;
1618 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1619 owp->nd_stable = NFSV3WRITE_FILESYNC;
1620 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1621 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1622 owp->nd_stable = NFSV3WRITE_DATASYNC;
1623 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1626 * If nfsd had anything else coalesced into it, transfer them
1627 * to owp, otherwise their replies will never get sent.
1629 for (p = LIST_FIRST(&nfsd->nd_coalesce); p;
1630 p = LIST_FIRST(&nfsd->nd_coalesce)) {
1631 LIST_REMOVE(p, nd_tq);
1632 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1637 * nfs create service
1638 * now does a truncate to 0 length via. setattr if it already exists
1641 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1642 struct thread *td, struct mbuf **mrq)
1644 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1645 struct sockaddr *nam = nfsd->nd_nam;
1646 caddr_t dpos = nfsd->nd_dpos;
1647 struct ucred *cred = nfsd->nd_cr;
1648 struct nfs_fattr *fp;
1649 struct vattr va, dirfor, diraft;
1650 struct vattr *vap = &va;
1651 struct nfsv2_sattr *sp;
1653 struct nameidata nd;
1655 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1656 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1658 struct mbuf *mb, *mreq;
1659 struct vnode *dirp = NULL;
1663 u_char cverf[NFSX_V3CREATEVERF];
1664 struct mount *mp = NULL;
1668 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1675 fhp = &nfh.fh_generic;
1677 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1681 vfslocked = VFS_LOCK_GIANT(mp);
1682 (void) vn_start_write(NULL, &mp, V_WAIT);
1683 vfs_rel(mp); /* The write holds a ref. */
1684 nfsm_srvnamesiz(len);
1686 nd.ni_cnd.cn_cred = cred;
1687 nd.ni_cnd.cn_nameiop = CREATE;
1688 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE;
1691 * Call namei and do initial cleanup to get a few things
1692 * out of the way. If we get an initial error we cleanup
1693 * and return here to avoid special-casing the invalid nd
1694 * structure through the rest of the case. dirp may be
1695 * set even if an error occurs, but the nd structure will not
1696 * be valid at all if an error occurs so we have to invalidate it
1697 * prior to calling nfsm_reply ( which might goto nfsmout ).
1699 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1700 &dirp, v3, &dirfor, &dirfor_ret, td, FALSE);
1701 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1707 nfsm_reply(NFSX_WCCDATA(v3));
1709 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1715 * No error. Continue. State:
1717 * startdir is valid ( we release this immediately )
1719 * nd.ni_vp may be valid
1720 * nd.ni_dvp is valid
1722 * The error state is set through the code and we may also do some
1723 * opportunistic releasing of vnodes to avoid holding locks through
1724 * NFS I/O. The cleanup at the end is a catch-all
1729 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1730 how = fxdr_unsigned(int, *tl);
1732 case NFSV3CREATE_GUARDED:
1738 case NFSV3CREATE_UNCHECKED:
1741 case NFSV3CREATE_EXCLUSIVE:
1742 cp = nfsm_dissect_nonblock(caddr_t, NFSX_V3CREATEVERF);
1743 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1747 vap->va_type = VREG;
1749 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
1750 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1751 if (vap->va_type == VNON)
1752 vap->va_type = VREG;
1753 vap->va_mode = nfstov_mode(sp->sa_mode);
1754 switch (vap->va_type) {
1756 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1758 vap->va_size = (u_quad_t)tsize;
1763 rdev = fxdr_unsigned(long, sp->sa_size);
1771 * Iff doesn't exist, create it
1772 * otherwise just truncate to 0 length
1773 * should I set the mode too ?
1775 * The only possible error we can have at this point is EEXIST.
1776 * nd.ni_vp will also be non-NULL in that case.
1778 if (nd.ni_vp == NULL) {
1779 if (vap->va_mode == (mode_t)VNOVAL)
1781 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1782 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1784 NDFREE(&nd, NDF_ONLY_PNBUF);
1786 if (exclusive_flag) {
1789 bcopy(cverf, (caddr_t)&vap->va_atime,
1791 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1795 } else if (vap->va_type == VCHR || vap->va_type == VBLK ||
1796 vap->va_type == VFIFO) {
1798 * NFSv2-specific code for creating device nodes
1801 * Handle SysV FIFO node special cases. All other
1802 * devices require super user to access.
1804 if (vap->va_type == VCHR && rdev == 0xffffffff)
1805 vap->va_type = VFIFO;
1806 if (vap->va_type != VFIFO &&
1807 (error = suser_cred(cred, 0))) {
1810 vap->va_rdev = rdev;
1811 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1813 NDFREE(&nd, NDF_ONLY_PNBUF);
1820 * release dvp prior to lookup
1827 * Even though LOCKPARENT was cleared, ni_dvp may
1830 nd.ni_cnd.cn_nameiop = LOOKUP;
1831 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1832 nd.ni_cnd.cn_thread = td;
1833 nd.ni_cnd.cn_cred = cred;
1834 tvfslocked = VFS_LOCK_GIANT(nd.ni_startdir->v_mount);
1836 nd.ni_cnd.cn_flags |= GIANTHELD;
1837 error = lookup(&nd);
1839 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1840 nd.ni_cnd.cn_flags &= ~GIANTHELD;
1844 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1852 if (vap->va_size != -1) {
1853 error = nfsrv_access(nd.ni_vp, VWRITE,
1854 cred, (nd.ni_cnd.cn_flags & RDONLY), td, 0);
1856 tempsize = vap->va_size;
1858 vap->va_size = tempsize;
1859 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1866 bzero((caddr_t)fhp, sizeof(nfh));
1867 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1868 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1870 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
1873 if (exclusive_flag && !error &&
1874 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1876 if (dirp == nd.ni_dvp)
1877 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1879 /* Drop the other locks to avoid deadlock. */
1881 if (nd.ni_dvp == nd.ni_vp)
1891 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1892 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1893 VOP_UNLOCK(dirp, 0);
1897 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1900 nfsm_srvpostop_fh(fhp);
1901 nfsm_srvpostop_attr(0, vap);
1903 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1904 } else if (!error) {
1905 /* v2 non-error case. */
1906 nfsm_srvfhtom(fhp, v3);
1907 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1908 nfsm_srvfillattr(vap, fp);
1914 if (nd.ni_dvp == nd.ni_vp)
1921 if (nd.ni_startdir) {
1922 vrele(nd.ni_startdir);
1923 nd.ni_startdir = NULL;
1927 NDFREE(&nd, NDF_ONLY_PNBUF);
1928 vn_finished_write(mp);
1929 VFS_UNLOCK_GIANT(vfslocked);
1934 * nfs v3 mknod service
1937 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1938 struct thread *td, struct mbuf **mrq)
1940 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1941 struct sockaddr *nam = nfsd->nd_nam;
1942 caddr_t dpos = nfsd->nd_dpos;
1943 struct ucred *cred = nfsd->nd_cr;
1944 struct vattr va, dirfor, diraft;
1945 struct vattr *vap = &va;
1947 struct nameidata nd;
1949 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1950 u_int32_t major, minor;
1952 struct mbuf *mb, *mreq;
1953 struct vnode *vp, *dirp = NULL;
1956 struct mount *mp = NULL;
1957 int v3 = (nfsd->nd_flag & ND_NFSV3);
1961 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1964 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1967 fhp = &nfh.fh_generic;
1969 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1973 vfslocked = VFS_LOCK_GIANT(mp);
1974 (void) vn_start_write(NULL, &mp, V_WAIT);
1975 vfs_rel(mp); /* The write holds a ref. */
1976 nfsm_srvnamesiz(len);
1978 nd.ni_cnd.cn_cred = cred;
1979 nd.ni_cnd.cn_nameiop = CREATE;
1980 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE;
1983 * Handle nfs_namei() call. If an error occurs, the nd structure
1984 * is not valid. However, nfsm_*() routines may still jump to
1988 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1989 &dirp, v3, &dirfor, &dirfor_ret, td, FALSE);
1990 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
1992 nfsm_reply(NFSX_WCCDATA(1));
1993 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1997 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1998 vtyp = nfsv3tov_type(*tl);
1999 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
2000 error = NFSERR_BADTYPE;
2005 if (vtyp == VCHR || vtyp == VBLK) {
2006 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
2007 major = fxdr_unsigned(u_int32_t, *tl++);
2008 minor = fxdr_unsigned(u_int32_t, *tl);
2009 vap->va_rdev = makedev(major, minor);
2013 * Iff doesn't exist, create it.
2019 vap->va_type = vtyp;
2020 if (vap->va_mode == (mode_t)VNOVAL)
2022 if (vtyp == VSOCK) {
2023 vrele(nd.ni_startdir);
2024 nd.ni_startdir = NULL;
2025 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2027 NDFREE(&nd, NDF_ONLY_PNBUF);
2029 if (vtyp != VFIFO && (error = suser_cred(cred, 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, td);
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, td);
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,
2133 struct thread *td, struct mbuf **mrq)
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, td, FALSE);
2171 vfslocked = NDHASGIANT(&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, td);
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, td);
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,
2243 struct thread *td, struct mbuf **mrq)
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, td, 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, td, 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, td);
2423 VOP_UNLOCK(fdirp, 0);
2426 vn_lock(tdirp, LK_EXCLUSIVE | LK_RETRY);
2427 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, td);
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,
2476 struct thread *td, struct mbuf **mrq)
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, td);
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, td, 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, td);
2571 if (dirp == nd.ni_dvp)
2572 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
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, td);
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,
2624 struct thread *td, struct mbuf **mrq)
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, td, 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 = td;
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,
2754 * These releases aren't strictly required, does even doing them
2755 * make any sense? XXX can nfsm_reply() block?
2758 FREE(pathcp, M_TEMP);
2762 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2763 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2764 VOP_UNLOCK(dirp, 0);
2766 if (nd.ni_startdir) {
2767 vrele(nd.ni_startdir);
2768 nd.ni_startdir = NULL;
2770 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2773 nfsm_srvpostop_fh(fhp);
2774 nfsm_srvpostop_attr(0, vap);
2776 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2782 NDFREE(&nd, NDF_ONLY_PNBUF);
2784 if (nd.ni_dvp == nd.ni_vp)
2792 vrele(nd.ni_startdir);
2796 FREE(pathcp, M_TEMP);
2798 vn_finished_write(mp);
2799 VFS_UNLOCK_GIANT(vfslocked);
2807 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2808 struct thread *td, struct mbuf **mrq)
2810 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2811 struct sockaddr *nam = nfsd->nd_nam;
2812 caddr_t dpos = nfsd->nd_dpos;
2813 struct ucred *cred = nfsd->nd_cr;
2814 struct vattr va, dirfor, diraft;
2815 struct vattr *vap = &va;
2816 struct nfs_fattr *fp;
2817 struct nameidata nd;
2820 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2821 int v3 = (nfsd->nd_flag & ND_NFSV3);
2822 struct mbuf *mb, *mreq;
2823 struct vnode *dirp = NULL;
2827 struct mount *mp = NULL;
2830 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2834 fhp = &nfh.fh_generic;
2836 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2840 vfslocked = VFS_LOCK_GIANT(mp);
2841 (void) vn_start_write(NULL, &mp, V_WAIT);
2842 vfs_rel(mp); /* The write holds a ref. */
2843 nfsm_srvnamesiz(len);
2844 nd.ni_cnd.cn_cred = cred;
2845 nd.ni_cnd.cn_nameiop = CREATE;
2846 nd.ni_cnd.cn_flags = LOCKPARENT | MPSAFE;
2848 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2849 &dirp, v3, &dirfor, &dirfor_ret, td, FALSE);
2850 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
2856 nfsm_reply(NFSX_WCCDATA(v3));
2858 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2866 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
2867 vap->va_mode = nfstov_mode(*tl++);
2871 * At this point nd.ni_dvp is referenced and exclusively locked and
2872 * nd.ni_vp, if it exists, is referenced but not locked.
2875 vap->va_type = VDIR;
2876 if (nd.ni_vp != NULL) {
2877 NDFREE(&nd, NDF_ONLY_PNBUF);
2883 * Issue mkdir op. Since SAVESTART is not set, the pathname
2884 * component is freed by the VOP call. This will fill-in
2885 * nd.ni_vp, reference, and exclusively lock it.
2887 if (vap->va_mode == (mode_t)VNOVAL)
2889 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2890 NDFREE(&nd, NDF_ONLY_PNBUF);
2897 bzero((caddr_t)fhp, sizeof(nfh));
2898 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2899 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2901 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2905 if (dirp == nd.ni_dvp) {
2906 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2908 /* Release existing locks to prevent deadlock. */
2910 NDFREE(&nd, NDF_ONLY_PNBUF);
2911 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2924 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2925 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2926 VOP_UNLOCK(dirp, 0);
2929 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2932 nfsm_srvpostop_fh(fhp);
2933 nfsm_srvpostop_attr(0, vap);
2935 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2936 } else if (!error) {
2937 /* v2 non-error case. */
2938 nfsm_srvfhtom(fhp, v3);
2939 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2940 nfsm_srvfillattr(vap, fp);
2947 NDFREE(&nd, NDF_ONLY_PNBUF);
2948 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2961 vn_finished_write(mp);
2962 VFS_UNLOCK_GIANT(vfslocked);
2970 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2971 struct thread *td, struct mbuf **mrq)
2973 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2974 struct sockaddr *nam = nfsd->nd_nam;
2975 caddr_t dpos = nfsd->nd_dpos;
2976 struct ucred *cred = nfsd->nd_cr;
2978 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2979 int v3 = (nfsd->nd_flag & ND_NFSV3);
2980 struct mbuf *mb, *mreq;
2981 struct vnode *vp, *dirp = NULL;
2982 struct vattr dirfor, diraft;
2985 struct nameidata nd;
2986 struct mount *mp = NULL;
2989 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2993 fhp = &nfh.fh_generic;
2995 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2999 vfslocked = VFS_LOCK_GIANT(mp);
3000 (void) vn_start_write(NULL, &mp, V_WAIT);
3001 vfs_rel(mp); /* The write holds a ref. */
3002 nfsm_srvnamesiz(len);
3003 nd.ni_cnd.cn_cred = cred;
3004 nd.ni_cnd.cn_nameiop = DELETE;
3005 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | MPSAFE;
3006 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
3007 &dirp, v3, &dirfor, &dirfor_ret, td, FALSE);
3008 vfslocked = nfsrv_lockedpair_nd(vfslocked, &nd);
3014 nfsm_reply(NFSX_WCCDATA(v3));
3016 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
3021 if (vp->v_type != VDIR) {
3026 * No rmdir "." please.
3028 if (nd.ni_dvp == vp) {
3033 * The root of a mounted filesystem cannot be deleted.
3035 if (vp->v_vflag & VV_ROOT)
3039 * Issue or abort op. Since SAVESTART is not set, path name
3040 * component is freed by the VOP after either.
3043 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3044 NDFREE(&nd, NDF_ONLY_PNBUF);
3047 if (dirp == nd.ni_dvp)
3048 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
3050 /* Release existing locks to prevent deadlock. */
3052 if (nd.ni_dvp == nd.ni_vp)
3061 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
3062 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
3063 VOP_UNLOCK(dirp, 0);
3066 nfsm_reply(NFSX_WCCDATA(v3));
3069 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
3073 NDFREE(&nd, NDF_ONLY_PNBUF);
3075 if (nd.ni_dvp == nd.ni_vp)
3085 vn_finished_write(mp);
3086 VFS_UNLOCK_GIANT(vfslocked);
3091 * nfs readdir service
3092 * - mallocs what it thinks is enough to read
3093 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
3094 * - calls VOP_READDIR()
3095 * - loops around building the reply
3096 * if the output generated exceeds count break out of loop
3097 * The nfsm_clget macro is used here so that the reply will be packed
3098 * tightly in mbuf clusters.
3099 * - it only knows that it has encountered eof when the VOP_READDIR()
3101 * - as such one readdir rpc will return eof false although you are there
3102 * and then the next will return eof
3103 * - it trims out records with d_fileno == 0
3104 * this doesn't matter for Unix clients, but they might confuse clients
3106 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
3107 * than requested, but this may not apply to all filesystems. For
3108 * example, client NFS does not { although it is never remote mounted
3110 * The alternate call nfsrv_readdirplus() does lookups as well.
3111 * PS: The NFS protocol spec. does not clarify what the "count" byte
3112 * argument is a count of.. just name strings and file id's or the
3113 * entire reply rpc or ...
3114 * I tried just file name and id sizes and it confused the Sun client,
3115 * so I am using the full rpc size now. The "paranoia.." comment refers
3116 * to including the status longwords that are not a part of the dir.
3117 * "entry" structures, but are in the rpc.
3121 u_int32_t fl_postopok;
3122 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
3124 u_int32_t fl_fhsize;
3125 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
3129 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3130 struct thread *td, struct mbuf **mrq)
3132 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3133 struct sockaddr *nam = nfsd->nd_nam;
3134 caddr_t dpos = nfsd->nd_dpos;
3135 struct ucred *cred = nfsd->nd_cr;
3142 struct mbuf *mb, *mreq;
3143 char *cpos, *cend, *rbuf;
3144 struct vnode *vp = NULL;
3150 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3151 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
3152 int v3 = (nfsd->nd_flag & ND_NFSV3);
3153 u_quad_t off, toff, verf;
3154 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3157 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3159 fhp = &nfh.fh_generic;
3162 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
3163 toff = fxdr_hyper(tl);
3165 verf = fxdr_hyper(tl);
3168 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
3169 toff = fxdr_unsigned(u_quad_t, *tl++);
3170 verf = 0; /* shut up gcc */
3173 cnt = fxdr_unsigned(int, *tl);
3174 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3175 xfer = NFS_SRVMAXDATA(nfsd);
3181 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
3182 nam, &rdonly, TRUE);
3183 if (!error && vp->v_type != VDIR) {
3189 nfsm_reply(NFSX_UNSIGNED);
3191 nfsm_srvpostop_attr(getret, &at);
3197 * Obtain lock on vnode for this section of the code
3200 error = getret = VOP_GETATTR(vp, &at, cred, td);
3203 * XXX This check may be too strict for Solaris 2.5 clients.
3205 if (!error && toff && verf && verf != at.va_filerev)
3206 error = NFSERR_BAD_COOKIE;
3210 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3214 nfsm_reply(NFSX_POSTOPATTR(v3));
3216 nfsm_srvpostop_attr(getret, &at);
3223 * end section. Allocate rbuf and continue
3225 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3228 iv.iov_len = fullsiz;
3231 io.uio_offset = (off_t)off;
3232 io.uio_resid = fullsiz;
3233 io.uio_segflg = UIO_SYSSPACE;
3234 io.uio_rw = UIO_READ;
3237 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3239 free((caddr_t)cookies, M_TEMP);
3242 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3243 off = (off_t)io.uio_offset;
3244 if (!cookies && !error)
3245 error = NFSERR_PERM;
3247 getret = VOP_GETATTR(vp, &at, cred, td);
3255 free((caddr_t)rbuf, M_TEMP);
3257 free((caddr_t)cookies, M_TEMP);
3258 nfsm_reply(NFSX_POSTOPATTR(v3));
3260 nfsm_srvpostop_attr(getret, &at);
3265 siz -= io.uio_resid;
3268 * If nothing read, return eof
3274 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3277 nfsm_srvpostop_attr(getret, &at);
3278 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3279 txdr_hyper(at.va_filerev, tl);
3282 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3283 *tl++ = nfsrv_nfs_false;
3284 *tl = nfsrv_nfs_true;
3285 FREE((caddr_t)rbuf, M_TEMP);
3286 FREE((caddr_t)cookies, M_TEMP);
3293 * Check for degenerate cases of nothing useful read.
3294 * If so go try again
3298 dp = (struct dirent *)cpos;
3301 * For some reason FreeBSD's ufs_readdir() chooses to back the
3302 * directory offset up to a block boundary, so it is necessary to
3303 * skip over the records that precede the requested offset. This
3304 * requires the assumption that file offset cookies monotonically
3307 while (cpos < cend && ncookies > 0 &&
3308 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3309 ((u_quad_t)(*cookiep)) <= toff)) {
3310 cpos += dp->d_reclen;
3311 dp = (struct dirent *)cpos;
3315 if (cpos >= cend || ncookies == 0) {
3321 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3322 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3324 nfsm_srvpostop_attr(getret, &at);
3325 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3326 txdr_hyper(at.va_filerev, tl);
3330 be = bp + M_TRAILINGSPACE(mp);
3332 /* Loop through the records and build reply */
3333 while (cpos < cend && ncookies > 0) {
3334 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3335 nlen = dp->d_namlen;
3336 rem = nfsm_rndup(nlen) - nlen;
3337 len += (4 * NFSX_UNSIGNED + nlen + rem);
3339 len += 2 * NFSX_UNSIGNED;
3345 * Build the directory record xdr from
3349 *tl = nfsrv_nfs_true;
3350 bp += NFSX_UNSIGNED;
3354 bp += NFSX_UNSIGNED;
3357 *tl = txdr_unsigned(dp->d_fileno);
3358 bp += NFSX_UNSIGNED;
3360 *tl = txdr_unsigned(nlen);
3361 bp += NFSX_UNSIGNED;
3363 /* And loop around copying the name */
3372 bcopy(cp, bp, tsiz);
3378 /* And null pad to an int32_t boundary. */
3379 for (i = 0; i < rem; i++)
3383 /* Finish off the record */
3386 bp += NFSX_UNSIGNED;
3389 *tl = txdr_unsigned(*cookiep);
3390 bp += NFSX_UNSIGNED;
3392 cpos += dp->d_reclen;
3393 dp = (struct dirent *)cpos;
3400 *tl = nfsrv_nfs_false;
3401 bp += NFSX_UNSIGNED;
3404 *tl = nfsrv_nfs_true;
3406 *tl = nfsrv_nfs_false;
3407 bp += NFSX_UNSIGNED;
3410 mp->m_len = bp - mtod(mp, caddr_t);
3412 mp->m_len += bp - bpos;
3413 FREE((caddr_t)rbuf, M_TEMP);
3414 FREE((caddr_t)cookies, M_TEMP);
3419 VFS_UNLOCK_GIANT(vfslocked);
3424 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3425 struct thread *td, struct mbuf **mrq)
3427 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3428 struct sockaddr *nam = nfsd->nd_nam;
3429 caddr_t dpos = nfsd->nd_dpos;
3430 struct ucred *cred = nfsd->nd_cr;
3437 struct mbuf *mb, *mreq;
3438 char *cpos, *cend, *rbuf;
3439 struct vnode *vp = NULL, *nvp;
3442 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3445 struct vattr va, at, *vap = &va;
3446 struct nfs_fattr *fp;
3447 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3448 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3449 u_quad_t off, toff, verf;
3450 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3451 int v3 = (nfsd->nd_flag & ND_NFSV3);
3454 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3457 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3458 fhp = &nfh.fh_generic;
3460 tl = nfsm_dissect_nonblock(u_int32_t *, 6 * NFSX_UNSIGNED);
3461 toff = fxdr_hyper(tl);
3463 verf = fxdr_hyper(tl);
3465 siz = fxdr_unsigned(int, *tl++);
3466 cnt = fxdr_unsigned(int, *tl);
3468 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3469 xfer = NFS_SRVMAXDATA(nfsd);
3475 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
3476 nam, &rdonly, TRUE);
3477 if (!error && vp->v_type != VDIR) {
3483 nfsm_reply(NFSX_UNSIGNED);
3484 nfsm_srvpostop_attr(getret, &at);
3488 error = getret = VOP_GETATTR(vp, &at, cred, td);
3491 * XXX This check may be too strict for Solaris 2.5 clients.
3493 if (!error && toff && verf && verf != at.va_filerev)
3494 error = NFSERR_BAD_COOKIE;
3497 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3501 nfsm_reply(NFSX_V3POSTOPATTR);
3502 nfsm_srvpostop_attr(getret, &at);
3507 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3510 iv.iov_len = fullsiz;
3513 io.uio_offset = (off_t)off;
3514 io.uio_resid = fullsiz;
3515 io.uio_segflg = UIO_SYSSPACE;
3516 io.uio_rw = UIO_READ;
3519 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3521 free((caddr_t)cookies, M_TEMP);
3524 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3525 off = (u_quad_t)io.uio_offset;
3526 getret = VOP_GETATTR(vp, &at, cred, td);
3528 if (!cookies && !error)
3529 error = NFSERR_PERM;
3536 free((caddr_t)cookies, M_TEMP);
3537 free((caddr_t)rbuf, M_TEMP);
3538 nfsm_reply(NFSX_V3POSTOPATTR);
3539 nfsm_srvpostop_attr(getret, &at);
3544 siz -= io.uio_resid;
3547 * If nothing read, return eof
3553 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3555 nfsm_srvpostop_attr(getret, &at);
3556 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3557 txdr_hyper(at.va_filerev, tl);
3559 *tl++ = nfsrv_nfs_false;
3560 *tl = nfsrv_nfs_true;
3561 FREE((caddr_t)cookies, M_TEMP);
3562 FREE((caddr_t)rbuf, M_TEMP);
3569 * Check for degenerate cases of nothing useful read.
3570 * If so go try again
3574 dp = (struct dirent *)cpos;
3577 * For some reason FreeBSD's ufs_readdir() chooses to back the
3578 * directory offset up to a block boundary, so it is necessary to
3579 * skip over the records that precede the requested offset. This
3580 * requires the assumption that file offset cookies monotonically
3583 while (cpos < cend && ncookies > 0 &&
3584 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3585 ((u_quad_t)(*cookiep)) <= toff)) {
3586 cpos += dp->d_reclen;
3587 dp = (struct dirent *)cpos;
3591 if (cpos >= cend || ncookies == 0) {
3598 * Probe one of the directory entries to see if the filesystem
3601 if (VFS_VGET(vp->v_mount, dp->d_fileno, LK_EXCLUSIVE, &nvp) ==
3603 error = NFSERR_NOTSUPP;
3606 free((caddr_t)cookies, M_TEMP);
3607 free((caddr_t)rbuf, M_TEMP);
3608 nfsm_reply(NFSX_V3POSTOPATTR);
3609 nfsm_srvpostop_attr(getret, &at);
3616 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3619 nfsm_srvpostop_attr(getret, &at);
3620 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3621 txdr_hyper(at.va_filerev, tl);
3624 be = bp + M_TRAILINGSPACE(mp);
3626 /* Loop through the records and build reply */
3627 while (cpos < cend && ncookies > 0) {
3628 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3629 nlen = dp->d_namlen;
3630 rem = nfsm_rndup(nlen)-nlen;
3633 * For readdir_and_lookup get the vnode using
3636 if (VFS_VGET(vp->v_mount, dp->d_fileno, LK_EXCLUSIVE,
3639 bzero((caddr_t)nfhp, NFSX_V3FH);
3641 nvp->v_mount->mnt_stat.f_fsid;
3643 * XXXRW: Assert the mountpoints are the same so that
3644 * we know that acquiring Giant based on the
3645 * directory is the right thing for the child.
3647 KASSERT(nvp->v_mount == vp->v_mount,
3648 ("nfsrv_readdirplus: nvp mount != vp mount"));
3649 if (VOP_VPTOFH(nvp, &nfhp->fh_fid)) {
3654 if (VOP_GETATTR(nvp, vap, cred, td)) {
3663 * If either the dircount or maxcount will be
3664 * exceeded, get out now. Both of these lengths
3665 * are calculated conservatively, including all
3668 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3670 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3671 if (len > cnt || dirlen > fullsiz) {
3677 * Build the directory record xdr from
3680 fp = (struct nfs_fattr *)&fl.fl_fattr;
3681 nfsm_srvfillattr(vap, fp);
3682 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3683 fl.fl_fhok = nfsrv_nfs_true;
3684 fl.fl_postopok = nfsrv_nfs_true;
3685 fl.fl_off.nfsuquad[0] = 0;
3686 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3689 *tl = nfsrv_nfs_true;
3690 bp += NFSX_UNSIGNED;
3693 bp += NFSX_UNSIGNED;
3695 *tl = txdr_unsigned(dp->d_fileno);
3696 bp += NFSX_UNSIGNED;
3698 *tl = txdr_unsigned(nlen);
3699 bp += NFSX_UNSIGNED;
3701 /* And loop around copying the name */
3706 if ((bp + xfer) > be)
3710 bcopy(cp, bp, tsiz);
3716 /* And null pad to an int32_t boundary. */
3717 for (i = 0; i < rem; i++)
3721 * Now copy the flrep structure out.
3723 xfer = sizeof (struct flrep);
3727 if ((bp + xfer) > be)
3731 bcopy(cp, bp, tsiz);
3739 cpos += dp->d_reclen;
3740 dp = (struct dirent *)cpos;
3747 *tl = nfsrv_nfs_false;
3748 bp += NFSX_UNSIGNED;
3751 *tl = nfsrv_nfs_true;
3753 *tl = nfsrv_nfs_false;
3754 bp += NFSX_UNSIGNED;
3757 mp->m_len = bp - mtod(mp, caddr_t);
3759 mp->m_len += bp - bpos;
3760 FREE((caddr_t)cookies, M_TEMP);
3761 FREE((caddr_t)rbuf, M_TEMP);
3765 VFS_UNLOCK_GIANT(vfslocked);
3770 * nfs commit service
3773 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3774 struct thread *td, struct mbuf **mrq)
3776 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3777 struct sockaddr *nam = nfsd->nd_nam;
3778 caddr_t dpos = nfsd->nd_dpos;
3779 struct ucred *cred = nfsd->nd_cr;
3780 struct vattr bfor, aft;
3781 struct vnode *vp = NULL;
3786 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3787 struct mbuf *mb, *mreq;
3789 struct mount *mp = NULL;
3790 int v3 = (nfsd->nd_flag & ND_NFSV3);
3794 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3797 panic("nfsrv_commit: v3 proc called on a v2 connection");
3798 fhp = &nfh.fh_generic;
3800 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3804 vfslocked = VFS_LOCK_GIANT(mp);
3805 (void) vn_start_write(NULL, &mp, V_WAIT);
3806 vfs_rel(mp); /* The write holds a ref. */
3807 tl = nfsm_dissect_nonblock(u_int32_t *, 3 * NFSX_UNSIGNED);
3810 * XXX At this time VOP_FSYNC() does not accept offset and byte
3811 * count parameters, so these arguments are useless (someday maybe).
3813 off = fxdr_hyper(tl);
3815 cnt = fxdr_unsigned(int, *tl);
3816 error = nfsrv_fhtovp(fhp, 1, &vp, &tvfslocked, cred, slp,
3817 nam, &rdonly, TRUE);
3818 vfslocked = nfsrv_lockedpair(vfslocked, tvfslocked);
3820 nfsm_reply(2 * NFSX_UNSIGNED);
3821 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3825 for_ret = VOP_GETATTR(vp, &bfor, cred, td);
3827 if (cnt > MAX_COMMIT_COUNT) {
3829 * Give up and do the whole thing
3832 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3833 VM_OBJECT_LOCK(vp->v_object);
3834 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3835 VM_OBJECT_UNLOCK(vp->v_object);
3837 error = VOP_FSYNC(vp, MNT_WAIT, td);
3840 * Locate and synchronously write any buffers that fall
3841 * into the requested range. Note: we are assuming that
3842 * f_iosize is a power of 2.
3844 int iosize = vp->v_mount->mnt_stat.f_iosize;
3845 int iomask = iosize - 1;
3850 * Align to iosize boundry, super-align to page boundry.
3853 cnt += off & iomask;
3854 off &= ~(u_quad_t)iomask;
3856 if (off & PAGE_MASK) {
3857 cnt += off & PAGE_MASK;
3858 off &= ~(u_quad_t)PAGE_MASK;
3860 lblkno = off / iosize;
3863 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3864 VM_OBJECT_LOCK(vp->v_object);
3865 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3866 VM_OBJECT_UNLOCK(vp->v_object);
3875 * If we have a buffer and it is marked B_DELWRI we
3876 * have to lock and write it. Otherwise the prior
3877 * write is assumed to have already been committed.
3879 * gbincore() can return invalid buffers now so we
3880 * have to check that bit as well (though B_DELWRI
3881 * should not be set if B_INVAL is set there could be
3882 * a race here since we haven't locked the buffer).
3884 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
3885 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
3886 LK_INTERLOCK, VI_MTX(vp)) == ENOLCK) {
3888 continue; /* retry */
3890 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
3893 bp->b_flags &= ~B_ASYNC;
3910 aft_ret = VOP_GETATTR(vp, &aft, cred, td);
3914 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3915 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3917 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3918 if (nfsver.tv_sec == 0)
3920 *tl++ = txdr_unsigned(nfsver.tv_sec);
3921 *tl = txdr_unsigned(nfsver.tv_usec);
3928 vn_finished_write(mp);
3929 VFS_UNLOCK_GIANT(vfslocked);
3934 * nfs statfs service
3937 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3938 struct thread *td, struct mbuf **mrq)
3940 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3941 struct sockaddr *nam = nfsd->nd_nam;
3942 caddr_t dpos = nfsd->nd_dpos;
3943 struct ucred *cred = nfsd->nd_cr;
3945 struct nfs_statfs *sfp;
3947 int error = 0, rdonly, getret = 1;
3948 int v3 = (nfsd->nd_flag & ND_NFSV3);
3949 struct mbuf *mb, *mreq;
3950 struct vnode *vp = NULL;
3954 struct statfs statfs;
3958 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3960 fhp = &nfh.fh_generic;
3962 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
3963 nam, &rdonly, TRUE);
3965 nfsm_reply(NFSX_UNSIGNED);
3967 nfsm_srvpostop_attr(getret, &at);
3972 error = VFS_STATFS(vp->v_mount, sf, td);
3973 getret = VOP_GETATTR(vp, &at, cred, td);
3976 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3978 nfsm_srvpostop_attr(getret, &at);
3983 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3985 tval = (u_quad_t)sf->f_blocks;
3986 tval *= (u_quad_t)sf->f_bsize;
3987 txdr_hyper(tval, &sfp->sf_tbytes);
3988 tval = (u_quad_t)sf->f_bfree;
3989 tval *= (u_quad_t)sf->f_bsize;
3990 txdr_hyper(tval, &sfp->sf_fbytes);
3992 * Don't send negative values for available space,
3993 * since this field is unsigned in the NFS protocol.
3994 * Otherwise, the client would see absurdly high
3995 * numbers for free space.
3997 if (sf->f_bavail < 0)
4000 tval = (u_quad_t)sf->f_bavail;
4001 tval *= (u_quad_t)sf->f_bsize;
4002 txdr_hyper(tval, &sfp->sf_abytes);
4003 sfp->sf_tfiles.nfsuquad[0] = 0;
4004 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
4005 sfp->sf_ffiles.nfsuquad[0] = 0;
4006 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
4007 sfp->sf_afiles.nfsuquad[0] = 0;
4008 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
4009 sfp->sf_invarsec = 0;
4011 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
4012 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
4013 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
4014 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
4015 if (sf->f_bavail < 0)
4018 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
4023 VFS_UNLOCK_GIANT(vfslocked);
4028 * nfs fsinfo service
4031 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4032 struct thread *td, struct mbuf **mrq)
4034 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
4035 struct sockaddr *nam = nfsd->nd_nam;
4036 caddr_t dpos = nfsd->nd_dpos;
4037 struct ucred *cred = nfsd->nd_cr;
4038 struct nfsv3_fsinfo *sip;
4040 int error = 0, rdonly, getret = 1, pref;
4041 struct mbuf *mb, *mreq;
4042 struct vnode *vp = NULL;
4048 int v3 = (nfsd->nd_flag & ND_NFSV3);
4051 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4053 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
4054 fhp = &nfh.fh_generic;
4057 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
4058 nam, &rdonly, TRUE);
4060 nfsm_reply(NFSX_UNSIGNED);
4061 nfsm_srvpostop_attr(getret, &at);
4066 /* XXX Try to make a guess on the max file size. */
4067 VFS_STATFS(vp->v_mount, &sb, td);
4068 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
4070 getret = VOP_GETATTR(vp, &at, cred, td);
4073 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
4074 nfsm_srvpostop_attr(getret, &at);
4075 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
4079 * There should be filesystem VFS OP(s) to get this information.
4080 * For now, assume ufs.
4082 if (slp->ns_so->so_type == SOCK_DGRAM)
4083 pref = NFS_MAXDGRAMDATA;
4086 sip->fs_rtmax = txdr_unsigned(pref);
4087 sip->fs_rtpref = txdr_unsigned(pref);
4088 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
4089 sip->fs_wtmax = txdr_unsigned(pref);
4090 sip->fs_wtpref = txdr_unsigned(pref);
4091 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
4092 sip->fs_dtpref = txdr_unsigned(pref);
4093 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
4094 sip->fs_timedelta.nfsv3_sec = 0;
4095 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
4096 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
4097 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
4098 NFSV3FSINFO_CANSETTIME);
4102 VFS_UNLOCK_GIANT(vfslocked);
4107 * nfs pathconf service
4110 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4111 struct thread *td, struct mbuf **mrq)
4113 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
4114 struct sockaddr *nam = nfsd->nd_nam;
4115 caddr_t dpos = nfsd->nd_dpos;
4116 struct ucred *cred = nfsd->nd_cr;
4117 struct nfsv3_pathconf *pc;
4119 int error = 0, rdonly, getret = 1;
4120 register_t linkmax, namemax, chownres, notrunc;
4121 struct mbuf *mb, *mreq;
4122 struct vnode *vp = NULL;
4126 int v3 = (nfsd->nd_flag & ND_NFSV3);
4129 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4131 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
4132 fhp = &nfh.fh_generic;
4134 error = nfsrv_fhtovp(fhp, 1, &vp, &vfslocked, cred, slp,
4135 nam, &rdonly, TRUE);
4137 nfsm_reply(NFSX_UNSIGNED);
4138 nfsm_srvpostop_attr(getret, &at);
4142 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
4144 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
4146 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
4148 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
4149 getret = VOP_GETATTR(vp, &at, cred, td);
4152 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
4153 nfsm_srvpostop_attr(getret, &at);
4158 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
4160 pc->pc_linkmax = txdr_unsigned(linkmax);
4161 pc->pc_namemax = txdr_unsigned(namemax);
4162 pc->pc_notrunc = txdr_unsigned(notrunc);
4163 pc->pc_chownrestricted = txdr_unsigned(chownres);
4166 * These should probably be supported by VOP_PATHCONF(), but
4167 * until msdosfs is exportable (why would you want to?), the
4168 * Unix defaults should be ok.
4170 pc->pc_caseinsensitive = nfsrv_nfs_false;
4171 pc->pc_casepreserving = nfsrv_nfs_true;
4175 VFS_UNLOCK_GIANT(vfslocked);
4180 * Null operation, used by clients to ping server
4184 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4185 struct thread *td, struct mbuf **mrq)
4187 struct mbuf *mrep = nfsd->nd_mrep;
4189 int error = NFSERR_RETVOID;
4190 struct mbuf *mb, *mreq;
4192 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4199 * No operation, used for obsolete procedures
4203 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
4204 struct thread *td, struct mbuf **mrq)
4206 struct mbuf *mrep = nfsd->nd_mrep;
4209 struct mbuf *mb, *mreq;
4211 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4212 if (nfsd->nd_repstat)
4213 error = nfsd->nd_repstat;
4215 error = EPROCUNAVAIL;
4223 * Perform access checking for vnodes obtained from file handles that would
4224 * refer to files already opened by a Unix client. You cannot just use
4225 * vn_writechk() and VOP_ACCESS() for two reasons.
4226 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
4228 * 2 - The owner is to be given access irrespective of mode bits for some
4229 * operations, so that processes that chmod after opening a file don't
4230 * break. I don't like this because it opens a security hole, but since
4231 * the nfs server opens a security hole the size of a barn door anyhow,
4234 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4235 * will return EPERM instead of EACCESS. EPERM is always an error.
4238 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
4239 int rdonly, struct thread *td, int override)
4244 VFS_ASSERT_GIANT(vp->v_mount);
4246 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4248 if (flags & VWRITE) {
4249 /* Just vn_writechk() changed to check rdonly */
4251 * Disallow write attempts on read-only filesystems;
4252 * unless the file is a socket or a block or character
4253 * device resident on the filesystem.
4255 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4256 switch (vp->v_type) {
4266 * If there's shared text associated with
4267 * the inode, we can't allow writing.
4269 if (vp->v_vflag & VV_TEXT)
4273 error = VOP_GETATTR(vp, &vattr, cred, td);
4276 error = VOP_ACCESS(vp, flags, cred, td);
4278 * Allow certain operations for the owner (reads and writes
4279 * on files that are already open).
4281 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)