2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * nfs version 2 and 3 server calls to vnode ops
44 * - these routines generally have 3 phases
45 * 1 - break down and validate rpc request in mbuf list
46 * 2 - do the vnode ops for the request
47 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
48 * 3 - build the rpc reply in an mbuf list
50 * - do not mix the phases, since the nfsm_?? macros can return failures
51 * on a bad rpc or similar and do not do any vrele() or vput()'s
53 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
54 * error number iff error != 0 whereas
55 * returning an error from the server function implies a fatal error
56 * such as a badly constructed rpc request that should be dropped without
58 * For Version 3, nfsm_reply() does not return for the error case, since
59 * most version 3 rpcs return more than the status for error cases.
62 * Warning: always pay careful attention to resource cleanup on return
63 * and note that nfsm_*() macros can terminate a procedure on certain
66 * lookup() and namei()
67 * may return garbage in various structural fields/return elements
68 * if an error is returned, and may garbage up nd.ni_dvp even if no
69 * error is returned and you did not request LOCKPARENT or WANTPARENT.
71 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
72 * buffer has been freed or not.
75 #include <sys/param.h>
76 #include <sys/systm.h>
78 #include <sys/namei.h>
79 #include <sys/unistd.h>
80 #include <sys/vnode.h>
81 #include <sys/mount.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/malloc.h>
86 #include <sys/dirent.h>
88 #include <sys/kernel.h>
89 #include <sys/sysctl.h>
94 #include <vm/vm_extern.h>
95 #include <vm/vm_object.h>
97 #include <nfs/nfsproto.h>
98 #include <nfs/rpcv2.h>
99 #include <nfsserver/nfs.h>
100 #include <nfs/xdr_subs.h>
101 #include <nfsserver/nfsm_subs.h>
104 #define nfsdbprintf(info) printf info
106 #define nfsdbprintf(info)
109 #define MAX_COMMIT_COUNT (1024 * 1024)
111 #define NUM_HEURISTIC 64
112 #define NHUSE_INIT 64
114 #define NHUSE_MAX 2048
116 static struct nfsheur {
117 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
118 off_t nh_nextr; /* next offset for sequential detection */
119 int nh_use; /* use count for selection */
120 int nh_seqcount; /* heuristic */
121 } nfsheur[NUM_HEURISTIC];
125 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
126 int nfsrvw_procrastinate_v3 = 0;
128 static struct timeval nfsver = { 0 };
130 SYSCTL_NODE(_vfs, OID_AUTO, nfsrv, CTLFLAG_RW, 0, "NFS server");
132 static int nfs_async;
133 static int nfs_commit_blks;
134 static int nfs_commit_miss;
135 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
136 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
137 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
139 struct nfsrvstats nfsrvstats;
140 SYSCTL_STRUCT(_vfs_nfsrv, NFS_NFSRVSTATS, nfsrvstats, CTLFLAG_RD,
141 &nfsrvstats, nfsrvstats, "S,nfsrvstats");
143 static int nfsrv_access(struct vnode *, int, struct ucred *, int,
144 struct thread *, int);
145 static void nfsrvw_coalesce(struct nfsrv_descript *,
146 struct nfsrv_descript *);
149 * Clear nameidata fields that are tested in nsfmout cleanup code prior
150 * to using first nfsm macro (that might jump to the cleanup code).
154 ndclear(struct nameidata *nd)
157 nd->ni_cnd.cn_flags = 0;
160 nd->ni_startdir = NULL;
164 * nfs v3 access service
167 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
168 struct thread *td, struct mbuf **mrq)
170 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
171 struct sockaddr *nam = nfsd->nd_nam;
172 caddr_t dpos = nfsd->nd_dpos;
173 struct ucred *cred = &nfsd->nd_cr;
174 struct vnode *vp = NULL;
179 int error = 0, rdonly, getret;
180 struct mbuf *mb, *mreq;
181 struct vattr vattr, *vap = &vattr;
182 u_long testmode, nfsmode;
183 int v3 = (nfsd->nd_flag & ND_NFSV3);
185 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
187 panic("nfsrv3_access: v3 proc called on a v2 connection");
188 fhp = &nfh.fh_generic;
190 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
191 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
193 nfsm_reply(NFSX_UNSIGNED);
194 nfsm_srvpostop_attr(1, (struct vattr *)0);
198 nfsmode = fxdr_unsigned(u_int32_t, *tl);
199 if ((nfsmode & NFSV3ACCESS_READ) &&
200 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
201 nfsmode &= ~NFSV3ACCESS_READ;
202 if (vp->v_type == VDIR)
203 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
206 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
207 if ((nfsmode & testmode) &&
208 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
209 nfsmode &= ~testmode;
210 if (vp->v_type == VDIR)
211 testmode = NFSV3ACCESS_LOOKUP;
213 testmode = NFSV3ACCESS_EXECUTE;
214 if ((nfsmode & testmode) &&
215 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
216 nfsmode &= ~testmode;
217 getret = VOP_GETATTR(vp, vap, cred, td);
220 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
221 nfsm_srvpostop_attr(getret, vap);
222 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
223 *tl = txdr_unsigned(nfsmode);
231 * nfs getattr service
234 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
235 struct thread *td, struct mbuf **mrq)
237 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
238 struct sockaddr *nam = nfsd->nd_nam;
239 caddr_t dpos = nfsd->nd_dpos;
240 struct ucred *cred = &nfsd->nd_cr;
241 struct nfs_fattr *fp;
243 struct vattr *vap = &va;
244 struct vnode *vp = NULL;
249 int error = 0, rdonly;
250 struct mbuf *mb, *mreq;
252 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
253 fhp = &nfh.fh_generic;
255 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
261 error = VOP_GETATTR(vp, vap, cred, td);
264 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
269 fp = nfsm_build(struct nfs_fattr *,
270 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
271 nfsm_srvfillattr(vap, fp);
281 * nfs setattr service
284 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
285 struct thread *td, struct mbuf **mrq)
287 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
288 struct sockaddr *nam = nfsd->nd_nam;
289 caddr_t dpos = nfsd->nd_dpos;
290 struct ucred *cred = &nfsd->nd_cr;
291 struct vattr va, preat;
292 struct vattr *vap = &va;
293 struct nfsv2_sattr *sp;
294 struct nfs_fattr *fp;
295 struct vnode *vp = NULL;
300 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
301 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
302 struct mbuf *mb, *mreq;
303 struct timespec guard;
304 struct mount *mp = NULL;
306 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
307 fhp = &nfh.fh_generic;
309 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
313 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
317 (void) vn_start_write(vp, &mp, V_WAIT);
323 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
324 gcheck = fxdr_unsigned(int, *tl);
326 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
327 fxdr_nfsv3time(tl, &guard);
330 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
332 * Nah nah nah nah na nah
333 * There is a bug in the Sun client that puts 0xffff in the mode
334 * field of sattr when it should put in 0xffffffff. The u_short
335 * doesn't sign extend.
336 * --> check the low order 2 bytes for 0xffff
338 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
339 vap->va_mode = nfstov_mode(sp->sa_mode);
340 if (sp->sa_uid != nfs_xdrneg1)
341 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
342 if (sp->sa_gid != nfs_xdrneg1)
343 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
344 if (sp->sa_size != nfs_xdrneg1)
345 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
346 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
348 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
350 vap->va_atime.tv_sec =
351 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
352 vap->va_atime.tv_nsec = 0;
355 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
356 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
361 * Now that we have all the fields, lets do it.
363 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
365 nfsm_reply(2 * NFSX_UNSIGNED);
367 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
373 * vp now an active resource, pay careful attention to cleanup
376 error = preat_ret = VOP_GETATTR(vp, &preat, cred, td);
377 if (!error && gcheck &&
378 (preat.va_ctime.tv_sec != guard.tv_sec ||
379 preat.va_ctime.tv_nsec != guard.tv_nsec))
380 error = NFSERR_NOT_SYNC;
384 nfsm_reply(NFSX_WCCDATA(v3));
386 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
393 * If the size is being changed write acces is required, otherwise
394 * just check for a read only file system.
396 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
397 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
402 if (vp->v_type == VDIR) {
405 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
409 error = VOP_SETATTR(vp, vap, cred, td);
410 postat_ret = VOP_GETATTR(vp, vap, cred, td);
417 nfsm_reply(NFSX_WCCORFATTR(v3));
419 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
423 /* v2 non-error case (see nfsm_reply). */
424 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
425 nfsm_srvfillattr(vap, fp);
432 vn_finished_write(mp);
440 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
441 struct thread *td, struct mbuf **mrq)
443 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
444 struct sockaddr *nam = nfsd->nd_nam;
445 caddr_t dpos = nfsd->nd_dpos;
446 struct ucred *cred = &nfsd->nd_cr;
447 struct nfs_fattr *fp;
448 struct nameidata nd, ind, *ndp = &nd;
449 struct vnode *vp, *dirp = NULL;
454 int error = 0, len, dirattr_ret = 1;
455 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
456 struct mbuf *mb, *mreq;
457 struct vattr va, dirattr, *vap = &va;
459 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
462 fhp = &nfh.fh_generic;
464 nfsm_srvnamesiz(len);
466 pubflag = nfs_ispublicfh(fhp);
468 nd.ni_cnd.cn_cred = cred;
469 nd.ni_cnd.cn_nameiop = LOOKUP;
470 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
471 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
475 * namei failure, only dirp to cleanup. Clear out garbarge from
476 * structure in case macros jump to nfsmout.
482 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
487 nfsm_reply(NFSX_POSTOPATTR(v3));
489 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
495 * Locate index file for public filehandle
497 * error is 0 on entry and 0 on exit from this block.
501 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
503 * Setup call to lookup() to see if we can find
504 * the index file. Arguably, this doesn't belong
505 * in a kernel.. Ugh. If an error occurs, do not
506 * try to install an index file and then clear the
509 * When we replace nd with ind and redirect ndp,
510 * maintenance of ni_startdir and ni_vp shift to
511 * ind and we have to clean them up in the old nd.
512 * However, the cnd resource continues to be maintained
513 * via the original nd. Confused? You aren't alone!
516 VOP_UNLOCK(nd.ni_vp, 0, td);
517 ind.ni_pathlen = strlen(nfs_pub.np_index);
518 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
520 ind.ni_startdir = nd.ni_vp;
521 VREF(ind.ni_startdir);
523 error = lookup(&ind);
528 * Found an index file. Get rid of
529 * the old references. transfer nd.ni_vp'
535 vrele(nd.ni_startdir);
536 nd.ni_startdir = NULL;
542 * If the public filehandle was used, check that this lookup
543 * didn't result in a filehandle outside the publicly exported
544 * filesystem. We clear the poor vp here to avoid lockups due
548 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
557 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
564 * Resources at this point:
565 * ndp->ni_vp may not be NULL
570 nfsm_reply(NFSX_POSTOPATTR(v3));
572 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
578 * Clear out some resources prior to potentially blocking. This
579 * is not as critical as ni_dvp resources in other routines, but
582 vrele(ndp->ni_startdir);
583 ndp->ni_startdir = NULL;
584 NDFREE(&nd, NDF_ONLY_PNBUF);
587 * Get underlying attribute, then release remaining resources ( for
588 * the same potential blocking reason ) and reply.
591 bzero((caddr_t)fhp, sizeof(nfh));
592 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
593 error = VFS_VPTOFH(vp, &fhp->fh_fid);
595 error = VOP_GETATTR(vp, vap, cred, td);
599 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
602 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
606 nfsm_srvfhtom(fhp, v3);
608 nfsm_srvpostop_attr(0, vap);
609 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
611 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
612 nfsm_srvfillattr(vap, fp);
618 NDFREE(&nd, NDF_ONLY_PNBUF);
619 if (ndp->ni_startdir)
620 vrele(ndp->ni_startdir);
627 * nfs readlink service
630 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
631 struct thread *td, struct mbuf **mrq)
633 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
634 struct sockaddr *nam = nfsd->nd_nam;
635 caddr_t dpos = nfsd->nd_dpos;
636 struct ucred *cred = &nfsd->nd_cr;
637 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
638 struct iovec *ivp = iv;
642 int error = 0, rdonly, i, tlen, len, getret;
643 int v3 = (nfsd->nd_flag & ND_NFSV3);
644 struct mbuf *mb, *mp3, *nmp, *mreq;
645 struct vnode *vp = NULL;
649 struct uio io, *uiop = &io;
651 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
653 mp = (struct mbuf *)0;
656 fhp = &nfh.fh_generic;
660 while (len < NFS_MAXPATHLEN) {
661 MGET(nmp, M_TRYWAIT, MT_DATA);
662 MCLGET(nmp, M_TRYWAIT);
663 nmp->m_len = NFSMSIZ(nmp);
670 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
671 mp->m_len = NFS_MAXPATHLEN - len;
672 len = NFS_MAXPATHLEN;
675 ivp->iov_base = mtod(mp, caddr_t);
676 ivp->iov_len = mp->m_len;
681 uiop->uio_iovcnt = i;
682 uiop->uio_offset = 0;
683 uiop->uio_resid = len;
684 uiop->uio_rw = UIO_READ;
685 uiop->uio_segflg = UIO_SYSSPACE;
686 uiop->uio_td = (struct thread *)0;
687 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
689 nfsm_reply(2 * NFSX_UNSIGNED);
691 nfsm_srvpostop_attr(1, (struct vattr *)0);
695 if (vp->v_type != VLNK) {
702 error = VOP_READLINK(vp, uiop, cred);
704 getret = VOP_GETATTR(vp, &attr, cred, td);
707 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
709 nfsm_srvpostop_attr(getret, &attr);
715 if (uiop->uio_resid > 0) {
716 len -= uiop->uio_resid;
717 tlen = nfsm_rndup(len);
718 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
720 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
721 *tl = txdr_unsigned(len);
736 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
737 struct thread *td, struct mbuf **mrq)
739 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
740 struct sockaddr *nam = nfsd->nd_nam;
741 caddr_t dpos = nfsd->nd_dpos;
742 struct ucred *cred = &nfsd->nd_cr;
746 struct nfs_fattr *fp;
750 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
751 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
752 struct mbuf *mb, *mreq;
754 struct vnode *vp = NULL;
757 struct uio io, *uiop = &io;
758 struct vattr va, *vap = &va;
763 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
764 fhp = &nfh.fh_generic;
767 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
768 off = fxdr_hyper(tl);
770 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
771 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
773 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
776 * Reference vp. If an error occurs, vp will be invalid, but we
777 * have to NULL it just in case. The macros might goto nfsmout
781 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
784 nfsm_reply(2 * NFSX_UNSIGNED);
786 nfsm_srvpostop_attr(1, (struct vattr *)0);
791 if (vp->v_type != VREG) {
795 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
798 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
799 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
801 getret = VOP_GETATTR(vp, vap, cred, td);
807 nfsm_reply(NFSX_POSTOPATTR(v3));
809 nfsm_srvpostop_attr(getret, vap);
815 * Calculate byte count to read
818 if (off >= vap->va_size)
820 else if ((off + reqlen) > vap->va_size)
821 cnt = vap->va_size - off;
826 * Calculate seqcount for heuristic
834 * Locate best candidate
837 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
841 if (nfsheur[hi].nh_vp == vp) {
845 if (nfsheur[hi].nh_use > 0)
846 --nfsheur[hi].nh_use;
847 hi = (hi + 1) & (NUM_HEURISTIC - 1);
848 if (nfsheur[hi].nh_use < nh->nh_use)
852 if (nh->nh_vp != vp) {
855 nh->nh_use = NHUSE_INIT;
863 * Calculate heuristic
866 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
867 if (++nh->nh_seqcount > 127)
868 nh->nh_seqcount = 127;
869 } else if (nh->nh_seqcount > 1) {
874 nh->nh_use += NHUSE_INC;
875 if (nh->nh_use > NHUSE_MAX)
876 nh->nh_use = NHUSE_MAX;
877 ioflag |= nh->nh_seqcount << 16;
880 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
882 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
884 fp = (struct nfs_fattr *)tl;
885 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
887 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
888 fp = (struct nfs_fattr *)tl;
889 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
891 len = left = nfsm_rndup(cnt);
894 * Generate the mbuf list with the uio_iov ref. to it.
899 siz = min(M_TRAILINGSPACE(m), left);
905 MGET(m, M_TRYWAIT, MT_DATA);
906 MCLGET(m, M_TRYWAIT);
912 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
914 uiop->uio_iov = iv2 = iv;
920 panic("nfsrv_read iov");
921 siz = min(M_TRAILINGSPACE(m), left);
923 iv->iov_base = mtod(m, caddr_t) + m->m_len;
932 uiop->uio_iovcnt = i;
933 uiop->uio_offset = off;
934 uiop->uio_resid = len;
935 uiop->uio_rw = UIO_READ;
936 uiop->uio_segflg = UIO_SYSSPACE;
937 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
938 off = uiop->uio_offset;
940 FREE((caddr_t)iv2, M_TEMP);
941 if (error || (getret = VOP_GETATTR(vp, vap, cred, td))) {
947 nfsm_reply(NFSX_POSTOPATTR(v3));
949 nfsm_srvpostop_attr(getret, vap);
958 nfsm_srvfillattr(vap, fp);
959 tlen = len - uiop->uio_resid;
960 cnt = cnt < tlen ? cnt : tlen;
961 tlen = nfsm_rndup(cnt);
962 if (len != tlen || tlen != cnt)
963 nfsm_adj(mb, len - tlen, tlen - cnt);
965 *tl++ = txdr_unsigned(cnt);
971 *tl = txdr_unsigned(cnt);
982 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
983 struct thread *td, struct mbuf **mrq)
985 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
986 struct sockaddr *nam = nfsd->nd_nam;
987 caddr_t dpos = nfsd->nd_dpos;
988 struct ucred *cred = &nfsd->nd_cr;
992 struct nfs_fattr *fp;
994 struct vattr va, forat;
995 struct vattr *vap = &va;
998 int error = 0, rdonly, len, forat_ret = 1;
999 int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
1000 int stable = NFSV3WRITE_FILESYNC;
1001 int v3 = (nfsd->nd_flag & ND_NFSV3);
1002 struct mbuf *mb, *mreq;
1003 struct vnode *vp = NULL;
1006 struct uio io, *uiop = &io;
1008 struct mount *mntp = NULL;
1010 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1016 fhp = &nfh.fh_generic;
1018 if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1022 if ((error = VFS_FHTOVP(mntp, &fhp->fh_fid, &vp)) != 0) {
1026 (void) vn_start_write(vp, &mntp, V_WAIT);
1030 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1031 off = fxdr_hyper(tl);
1033 stable = fxdr_unsigned(int, *tl++);
1035 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1036 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1039 stable = NFSV3WRITE_UNSTABLE;
1041 retlen = len = fxdr_unsigned(int32_t, *tl);
1045 * For NFS Version 2, it is not obvious what a write of zero length
1046 * should do, but I might as well be consistent with Version 3,
1047 * which is to return ok so long as there are no permission problems.
1055 adjust = dpos - mtod(mp, caddr_t);
1056 mp->m_len -= adjust;
1057 if (mp->m_len > 0 && adjust > 0)
1058 mp->m_data += adjust;
1062 else if (mp->m_len > 0) {
1065 mp->m_len -= (i - len);
1074 if (len > NFS_MAXDATA || len < 0 || i < len) {
1076 nfsm_reply(2 * NFSX_UNSIGNED);
1078 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1082 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
1085 nfsm_reply(2 * NFSX_UNSIGNED);
1087 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1092 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1093 if (vp->v_type != VREG) {
1097 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1100 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1104 nfsm_reply(NFSX_WCCDATA(v3));
1106 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1112 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1114 uiop->uio_iov = iv = ivp;
1115 uiop->uio_iovcnt = cnt;
1118 if (mp->m_len > 0) {
1119 ivp->iov_base = mtod(mp, caddr_t);
1120 ivp->iov_len = mp->m_len;
1128 * The IO_METASYNC flag indicates that all metadata (and not just
1129 * enough to ensure data integrity) mus be written to stable storage
1131 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1133 if (stable == NFSV3WRITE_UNSTABLE)
1134 ioflags = IO_NODELOCKED;
1135 else if (stable == NFSV3WRITE_DATASYNC)
1136 ioflags = (IO_SYNC | IO_NODELOCKED);
1138 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1139 uiop->uio_resid = len;
1140 uiop->uio_rw = UIO_WRITE;
1141 uiop->uio_segflg = UIO_SYSSPACE;
1142 uiop->uio_td = (struct thread *)0;
1143 uiop->uio_offset = off;
1144 error = VOP_WRITE(vp, uiop, ioflags, cred);
1145 nfsrvstats.srvvop_writes++;
1146 FREE((caddr_t)iv, M_TEMP);
1148 aftat_ret = VOP_GETATTR(vp, vap, cred, td);
1154 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1155 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1157 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1162 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1163 *tl++ = txdr_unsigned(retlen);
1165 * If nfs_async is set, then pretend the write was FILESYNC.
1167 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1168 *tl++ = txdr_unsigned(stable);
1170 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1172 * Actually, there is no need to txdr these fields,
1173 * but it may make the values more human readable,
1174 * for debugging purposes.
1176 if (nfsver.tv_sec == 0)
1178 *tl++ = txdr_unsigned(nfsver.tv_sec);
1179 *tl = txdr_unsigned(nfsver.tv_usec);
1181 /* v2, non-error case (see nfsm_reply). */
1182 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1183 nfsm_srvfillattr(vap, fp);
1188 vn_finished_write(mntp);
1193 * NFS write service with write gathering support. Called when
1194 * nfsrvw_procrastinate > 0.
1195 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1196 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1200 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1201 struct thread *td, struct mbuf **mrq)
1205 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1206 struct nfs_fattr *fp;
1209 struct nfsrvw_delayhash *wpp;
1211 struct vattr va, forat;
1214 int error = 0, rdonly, len, forat_ret = 1;
1215 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1216 struct mbuf *mb, *mreq, *mrep, *md;
1217 struct vnode *vp = NULL;
1218 struct uio io, *uiop = &io;
1220 struct mount *mntp = NULL;
1222 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1231 mrep = nfsd->nd_mrep;
1233 dpos = nfsd->nd_dpos;
1234 cred = &nfsd->nd_cr;
1235 v3 = (nfsd->nd_flag & ND_NFSV3);
1236 LIST_INIT(&nfsd->nd_coalesce);
1237 nfsd->nd_mreq = NULL;
1238 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1239 cur_usec = nfs_curusec();
1240 nfsd->nd_time = cur_usec +
1241 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1244 * Now, get the write header..
1246 nfsm_srvmtofh(&nfsd->nd_fh);
1248 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1249 nfsd->nd_off = fxdr_hyper(tl);
1251 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1253 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1254 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1257 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1259 len = fxdr_unsigned(int32_t, *tl);
1261 nfsd->nd_eoff = nfsd->nd_off + len;
1264 * Trim the header out of the mbuf list and trim off any trailing
1265 * junk so that the mbuf list has only the write data.
1273 adjust = dpos - mtod(mp, caddr_t);
1274 mp->m_len -= adjust;
1275 if (mp->m_len > 0 && adjust > 0)
1276 mp->m_data += adjust;
1283 mp->m_len -= (i - len);
1289 if (len > NFS_MAXDATA || len < 0 || i < len) {
1293 nfsm_writereply(2 * NFSX_UNSIGNED);
1295 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1296 nfsd->nd_mreq = mreq;
1297 nfsd->nd_mrep = NULL;
1302 * Add this entry to the hash and time queues.
1306 wp = LIST_FIRST(&slp->ns_tq);
1307 while (wp && wp->nd_time < nfsd->nd_time) {
1309 wp = LIST_NEXT(wp, nd_tq);
1311 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1313 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1315 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1317 if (nfsd->nd_mrep) {
1318 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1320 wp = LIST_FIRST(wpp);
1322 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)){
1324 wp = LIST_NEXT(wp, nd_hash);
1326 while (wp && wp->nd_off < nfsd->nd_off &&
1327 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)) {
1329 wp = LIST_NEXT(wp, nd_hash);
1332 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1335 * Search the hash list for overlapping entries and
1338 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1339 wp = LIST_NEXT(nfsd, nd_hash);
1340 if (NFSW_SAMECRED(owp, nfsd))
1341 nfsrvw_coalesce(owp, nfsd);
1344 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1351 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1352 * and generate the associated reply mbuf list(s).
1355 cur_usec = nfs_curusec();
1357 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1358 owp = LIST_NEXT(nfsd, nd_tq);
1359 if (nfsd->nd_time > cur_usec)
1363 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1364 LIST_REMOVE(nfsd, nd_tq);
1365 LIST_REMOVE(nfsd, nd_hash);
1367 mrep = nfsd->nd_mrep;
1368 nfsd->nd_mrep = NULL;
1369 cred = &nfsd->nd_cr;
1370 v3 = (nfsd->nd_flag & ND_NFSV3);
1371 forat_ret = aftat_ret = 1;
1372 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1373 nfsd->nd_nam, &rdonly, TRUE);
1376 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1377 if (vp->v_type != VREG) {
1381 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1387 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1388 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1389 ioflags = IO_NODELOCKED;
1390 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1391 ioflags = (IO_SYNC | IO_NODELOCKED);
1393 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1394 uiop->uio_rw = UIO_WRITE;
1395 uiop->uio_segflg = UIO_SYSSPACE;
1396 uiop->uio_td = (struct thread *)0;
1397 uiop->uio_offset = nfsd->nd_off;
1398 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1399 if (uiop->uio_resid > 0) {
1407 uiop->uio_iovcnt = i;
1408 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1410 uiop->uio_iov = ivp = iov;
1413 if (mp->m_len > 0) {
1414 ivp->iov_base = mtod(mp, caddr_t);
1415 ivp->iov_len = mp->m_len;
1421 if (vn_start_write(vp, &mntp, V_NOWAIT) != 0) {
1422 VOP_UNLOCK(vp, 0, td);
1423 error = vn_start_write(NULL, &mntp, V_WAIT);
1424 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1428 error = VOP_WRITE(vp, uiop, ioflags, cred);
1429 nfsrvstats.srvvop_writes++;
1430 vn_finished_write(mntp);
1432 FREE((caddr_t)iov, M_TEMP);
1436 aftat_ret = VOP_GETATTR(vp, &va, cred, td);
1442 * Loop around generating replies for all write rpcs that have
1443 * now been completed.
1447 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1449 nfsm_writereply(NFSX_WCCDATA(v3));
1451 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1454 nfsm_writereply(NFSX_PREOPATTR(v3) +
1455 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1456 NFSX_WRITEVERF(v3));
1458 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1459 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1460 *tl++ = txdr_unsigned(nfsd->nd_len);
1461 *tl++ = txdr_unsigned(swp->nd_stable);
1463 * Actually, there is no need to txdr these fields,
1464 * but it may make the values more human readable,
1465 * for debugging purposes.
1467 if (nfsver.tv_sec == 0)
1469 *tl++ = txdr_unsigned(nfsver.tv_sec);
1470 *tl = txdr_unsigned(nfsver.tv_usec);
1472 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1473 nfsm_srvfillattr(&va, fp);
1476 nfsd->nd_mreq = mreq;
1478 panic("nfsrv_write: nd_mrep not free");
1481 * Done. Put it at the head of the timer queue so that
1482 * the final phase can return the reply.
1487 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1489 nfsd = LIST_FIRST(&swp->nd_coalesce);
1491 LIST_REMOVE(nfsd, nd_tq);
1497 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1504 * Search for a reply to return.
1507 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq)
1508 if (nfsd->nd_mreq) {
1509 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1510 LIST_REMOVE(nfsd, nd_tq);
1511 *mrq = nfsd->nd_mreq;
1520 * Coalesce the write request nfsd into owp. To do this we must:
1521 * - remove nfsd from the queues
1522 * - merge nfsd->nd_mrep into owp->nd_mrep
1523 * - update the nd_eoff and nd_stable for owp
1524 * - put nfsd on owp's nd_coalesce list
1525 * NB: Must be called at splsoftclock().
1528 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1532 struct nfsrv_descript *p;
1534 NFS_DPF(WG, ("C%03x-%03x",
1535 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1536 LIST_REMOVE(nfsd, nd_hash);
1537 LIST_REMOVE(nfsd, nd_tq);
1538 if (owp->nd_eoff < nfsd->nd_eoff) {
1539 overlap = owp->nd_eoff - nfsd->nd_off;
1541 panic("nfsrv_coalesce: bad off");
1543 m_adj(nfsd->nd_mrep, overlap);
1547 mp->m_next = nfsd->nd_mrep;
1548 owp->nd_eoff = nfsd->nd_eoff;
1550 m_freem(nfsd->nd_mrep);
1551 nfsd->nd_mrep = NULL;
1552 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1553 owp->nd_stable = NFSV3WRITE_FILESYNC;
1554 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1555 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1556 owp->nd_stable = NFSV3WRITE_DATASYNC;
1557 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1560 * If nfsd had anything else coalesced into it, transfer them
1561 * to owp, otherwise their replies will never get sent.
1563 for (p = LIST_FIRST(&nfsd->nd_coalesce); p;
1564 p = LIST_FIRST(&nfsd->nd_coalesce)) {
1565 LIST_REMOVE(p, nd_tq);
1566 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1571 * nfs create service
1572 * now does a truncate to 0 length via. setattr if it already exists
1575 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1576 struct thread *td, struct mbuf **mrq)
1578 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1579 struct sockaddr *nam = nfsd->nd_nam;
1580 caddr_t dpos = nfsd->nd_dpos;
1581 struct ucred *cred = &nfsd->nd_cr;
1582 struct nfs_fattr *fp;
1583 struct vattr va, dirfor, diraft;
1584 struct vattr *vap = &va;
1585 struct nfsv2_sattr *sp;
1587 struct nameidata nd;
1589 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1590 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1592 struct mbuf *mb, *mreq;
1593 struct vnode *dirp = (struct vnode *)0;
1597 u_char cverf[NFSX_V3CREATEVERF];
1598 struct mount *mp = NULL;
1601 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1607 fhp = &nfh.fh_generic;
1609 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1613 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1617 (void) vn_start_write(vp, &mp, V_WAIT);
1619 nfsm_srvnamesiz(len);
1621 nd.ni_cnd.cn_cred = cred;
1622 nd.ni_cnd.cn_nameiop = CREATE;
1623 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1626 * Call namei and do initial cleanup to get a few things
1627 * out of the way. If we get an initial error we cleanup
1628 * and return here to avoid special-casing the invalid nd
1629 * structure through the rest of the case. dirp may be
1630 * set even if an error occurs, but the nd structure will not
1631 * be valid at all if an error occurs so we have to invalidate it
1632 * prior to calling nfsm_reply ( which might goto nfsmout ).
1634 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1638 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1646 nfsm_reply(NFSX_WCCDATA(v3));
1648 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1654 * No error. Continue. State:
1656 * startdir is valid ( we release this immediately )
1658 * nd.ni_vp may be valid
1659 * nd.ni_dvp is valid
1661 * The error state is set through the code and we may also do some
1662 * opportunistic releasing of vnodes to avoid holding locks through
1663 * NFS I/O. The cleanup at the end is a catch-all
1668 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1669 how = fxdr_unsigned(int, *tl);
1671 case NFSV3CREATE_GUARDED:
1677 case NFSV3CREATE_UNCHECKED:
1680 case NFSV3CREATE_EXCLUSIVE:
1681 cp = nfsm_dissect(caddr_t, NFSX_V3CREATEVERF);
1682 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1684 if (nd.ni_vp == NULL)
1688 vap->va_type = VREG;
1690 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
1691 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1692 if (vap->va_type == VNON)
1693 vap->va_type = VREG;
1694 vap->va_mode = nfstov_mode(sp->sa_mode);
1695 switch (vap->va_type) {
1697 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1699 vap->va_size = (u_quad_t)tsize;
1704 rdev = fxdr_unsigned(long, sp->sa_size);
1712 * Iff doesn't exist, create it
1713 * otherwise just truncate to 0 length
1714 * should I set the mode too ?
1716 * The only possible error we can have at this point is EEXIST.
1717 * nd.ni_vp will also be non-NULL in that case.
1719 if (nd.ni_vp == NULL) {
1720 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1721 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1723 NDFREE(&nd, NDF_ONLY_PNBUF);
1725 nfsrv_object_create(nd.ni_vp);
1726 if (exclusive_flag) {
1729 bcopy(cverf, (caddr_t)&vap->va_atime,
1731 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1736 vap->va_type == VCHR ||
1737 vap->va_type == VBLK ||
1738 vap->va_type == VFIFO
1741 * Handle SysV FIFO node special cases. All other
1742 * devices require super user to access.
1744 if (vap->va_type == VCHR && rdev == 0xffffffff)
1745 vap->va_type = VFIFO;
1746 if (vap->va_type != VFIFO &&
1747 (error = suser_xxx(cred, 0, 0))) {
1750 vap->va_rdev = rdev;
1751 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1753 NDFREE(&nd, NDF_ONLY_PNBUF);
1760 * release dvp prior to lookup
1768 * Even though LOCKPARENT was cleared, ni_dvp may
1771 nd.ni_cnd.cn_nameiop = LOOKUP;
1772 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1773 nd.ni_cnd.cn_thread = td;
1774 nd.ni_cnd.cn_cred = cred;
1776 error = lookup(&nd);
1781 /* fall through on certain errors */
1783 nfsrv_object_create(nd.ni_vp);
1784 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1792 if (vap->va_size != -1) {
1793 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1794 (nd.ni_cnd.cn_flags & RDONLY), td, 0);
1796 tempsize = vap->va_size;
1798 vap->va_size = tempsize;
1799 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1806 bzero((caddr_t)fhp, sizeof(nfh));
1807 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1808 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1810 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
1813 if (exclusive_flag && !error &&
1814 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1816 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1821 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1824 nfsm_srvpostop_fh(fhp);
1825 nfsm_srvpostop_attr(0, vap);
1827 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1830 /* v2 non-error case (see nfsm_reply). */
1831 nfsm_srvfhtom(fhp, v3);
1832 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1833 nfsm_srvfillattr(vap, fp);
1840 if (nd.ni_startdir) {
1841 vrele(nd.ni_startdir);
1842 nd.ni_startdir = NULL;
1846 NDFREE(&nd, NDF_ONLY_PNBUF);
1848 if (nd.ni_dvp == nd.ni_vp)
1855 vn_finished_write(mp);
1860 * nfs v3 mknod service
1863 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1864 struct thread *td, struct mbuf **mrq)
1866 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1867 struct sockaddr *nam = nfsd->nd_nam;
1868 caddr_t dpos = nfsd->nd_dpos;
1869 struct ucred *cred = &nfsd->nd_cr;
1870 struct vattr va, dirfor, diraft;
1871 struct vattr *vap = &va;
1873 struct nameidata nd;
1875 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1876 u_int32_t major, minor;
1878 struct mbuf *mb, *mreq;
1879 struct vnode *vp, *dirp = (struct vnode *)0;
1882 struct mount *mp = NULL;
1883 int v3 = (nfsd->nd_flag & ND_NFSV3);
1885 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1887 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1890 fhp = &nfh.fh_generic;
1892 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1896 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1900 (void) vn_start_write(vp, &mp, V_WAIT);
1903 nfsm_srvnamesiz(len);
1905 nd.ni_cnd.cn_cred = cred;
1906 nd.ni_cnd.cn_nameiop = CREATE;
1907 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1910 * Handle nfs_namei() call. If an error occurs, the nd structure
1911 * is not valid. However, nfsm_*() routines may still jump to
1915 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1918 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
1920 nfsm_reply(NFSX_WCCDATA(1));
1921 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1925 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1926 vtyp = nfsv3tov_type(*tl);
1927 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1928 error = NFSERR_BADTYPE;
1933 if (vtyp == VCHR || vtyp == VBLK) {
1934 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1935 major = fxdr_unsigned(u_int32_t, *tl++);
1936 minor = fxdr_unsigned(u_int32_t, *tl);
1937 vap->va_rdev = makeudev(major, minor);
1941 * Iff doesn't exist, create it.
1947 vap->va_type = vtyp;
1948 if (vtyp == VSOCK) {
1949 vrele(nd.ni_startdir);
1950 nd.ni_startdir = NULL;
1951 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1953 NDFREE(&nd, NDF_ONLY_PNBUF);
1955 if (vtyp != VFIFO && (error = suser_xxx(cred, 0, 0)))
1957 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1959 NDFREE(&nd, NDF_ONLY_PNBUF);
1966 * Release dvp prior to lookup
1971 nd.ni_cnd.cn_nameiop = LOOKUP;
1972 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1973 nd.ni_cnd.cn_thread = td;
1974 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1976 error = lookup(&nd);
1981 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1986 * send response, cleanup, return.
1989 if (nd.ni_startdir) {
1990 vrele(nd.ni_startdir);
1991 nd.ni_startdir = NULL;
1993 NDFREE(&nd, NDF_ONLY_PNBUF);
1995 if (nd.ni_dvp == nd.ni_vp)
2003 bzero((caddr_t)fhp, sizeof(nfh));
2004 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2005 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2007 error = VOP_GETATTR(vp, vap, cred, td);
2012 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2018 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2021 nfsm_srvpostop_fh(fhp);
2022 nfsm_srvpostop_attr(0, vap);
2024 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2026 vn_finished_write(mp);
2032 vrele(nd.ni_startdir);
2033 NDFREE(&nd, NDF_ONLY_PNBUF);
2035 if (nd.ni_dvp == nd.ni_vp)
2042 vn_finished_write(mp);
2047 * nfs remove service
2050 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2051 struct thread *td, struct mbuf **mrq)
2053 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2054 struct sockaddr *nam = nfsd->nd_nam;
2055 caddr_t dpos = nfsd->nd_dpos;
2056 struct ucred *cred = &nfsd->nd_cr;
2057 struct nameidata nd;
2060 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2061 int v3 = (nfsd->nd_flag & ND_NFSV3);
2062 struct mbuf *mb, *mreq;
2064 struct vattr dirfor, diraft;
2067 struct mount *mp = NULL;
2070 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2073 fhp = &nfh.fh_generic;
2075 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2079 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2083 (void) vn_start_write(vp, &mp, V_WAIT);
2086 nfsm_srvnamesiz(len);
2088 nd.ni_cnd.cn_cred = cred;
2089 nd.ni_cnd.cn_nameiop = DELETE;
2090 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2091 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2095 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2103 if (nd.ni_vp->v_type == VDIR) {
2104 error = EPERM; /* POSIX */
2108 * The root of a mounted filesystem cannot be deleted.
2110 if (nd.ni_vp->v_flag & VROOT) {
2116 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2117 NDFREE(&nd, NDF_ONLY_PNBUF);
2121 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2126 nfsm_reply(NFSX_WCCDATA(v3));
2128 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2132 NDFREE(&nd, NDF_ONLY_PNBUF);
2134 if (nd.ni_dvp == nd.ni_vp)
2141 vn_finished_write(mp);
2146 * nfs rename service
2149 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2150 struct thread *td, struct mbuf **mrq)
2152 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2153 struct sockaddr *nam = nfsd->nd_nam;
2154 caddr_t dpos = nfsd->nd_dpos;
2155 struct ucred *cred = &nfsd->nd_cr;
2158 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2159 int tdirfor_ret = 1, tdiraft_ret = 1;
2160 int v3 = (nfsd->nd_flag & ND_NFSV3);
2161 struct mbuf *mb, *mreq;
2162 struct nameidata fromnd, tond;
2163 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2164 struct vnode *tdirp = (struct vnode *)0;
2165 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2167 fhandle_t *ffhp, *tfhp;
2169 struct mount *mp = NULL;
2172 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2174 fvp = (struct vnode *)0;
2176 ffhp = &fnfh.fh_generic;
2177 tfhp = &tnfh.fh_generic;
2180 * Clear fields incase goto nfsmout occurs from macro.
2186 nfsm_srvmtofh(ffhp);
2187 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
2191 if ((error = VFS_FHTOVP(mp, &ffhp->fh_fid, &vp)) != 0) {
2195 (void) vn_start_write(vp, &mp, V_WAIT);
2198 nfsm_srvnamesiz(len);
2200 * Remember our original uid so that we can reset cr_uid before
2201 * the second nfs_namei() call, in case it is remapped.
2203 saved_uid = cred->cr_uid;
2204 fromnd.ni_cnd.cn_cred = cred;
2205 fromnd.ni_cnd.cn_nameiop = DELETE;
2206 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2207 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2208 &dpos, &fdirp, td, FALSE);
2211 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2219 nfsm_reply(2 * NFSX_WCCDATA(v3));
2221 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2222 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2228 nfsm_srvmtofh(tfhp);
2229 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2230 cred->cr_uid = saved_uid;
2231 tond.ni_cnd.cn_cred = cred;
2232 tond.ni_cnd.cn_nameiop = RENAME;
2233 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2234 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2235 &dpos, &tdirp, td, FALSE);
2238 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
2251 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2257 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2264 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2272 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2279 if (fvp->v_mount != tdvp->v_mount) {
2293 * If source is the same as the destination (that is the
2294 * same vnode with the same name in the same directory),
2295 * then there is nothing to do.
2297 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2298 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2299 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2300 fromnd.ni_cnd.cn_namelen))
2305 * The VOP_RENAME function releases all vnode references &
2306 * locks prior to returning so we need to clear the pointers
2307 * to bypass cleanup code later on.
2309 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2310 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2311 fromnd.ni_dvp = NULL;
2312 fromnd.ni_vp = NULL;
2316 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2317 tond.ni_cnd.cn_flags &= ~HASBUF;
2327 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, td);
2329 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, td);
2330 nfsm_reply(2 * NFSX_WCCDATA(v3));
2332 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2333 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2340 * Clear out tond related fields
2344 if (tond.ni_startdir)
2345 vrele(tond.ni_startdir);
2346 NDFREE(&tond, NDF_ONLY_PNBUF);
2348 if (tond.ni_dvp == tond.ni_vp)
2357 * Clear out fromnd related fields
2361 if (fromnd.ni_startdir)
2362 vrele(fromnd.ni_startdir);
2363 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2365 vrele(fromnd.ni_dvp);
2367 vrele(fromnd.ni_vp);
2369 vn_finished_write(mp);
2377 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2378 struct thread *td, struct mbuf **mrq)
2380 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2381 struct sockaddr *nam = nfsd->nd_nam;
2382 caddr_t dpos = nfsd->nd_dpos;
2383 struct ucred *cred = &nfsd->nd_cr;
2384 struct nameidata nd;
2387 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2388 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2389 struct mbuf *mb, *mreq;
2390 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2391 struct vattr dirfor, diraft, at;
2393 fhandle_t *fhp, *dfhp;
2394 struct mount *mp = NULL;
2396 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2399 fhp = &nfh.fh_generic;
2400 dfhp = &dnfh.fh_generic;
2402 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2406 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2410 (void) vn_start_write(vp, &mp, V_WAIT);
2413 nfsm_srvmtofh(dfhp);
2414 nfsm_srvnamesiz(len);
2416 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam, &rdonly, TRUE);
2418 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2420 nfsm_srvpostop_attr(getret, &at);
2421 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2427 if (vp->v_type == VDIR) {
2428 error = EPERM; /* POSIX */
2431 nd.ni_cnd.cn_cred = cred;
2432 nd.ni_cnd.cn_nameiop = CREATE;
2433 nd.ni_cnd.cn_flags = LOCKPARENT;
2434 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2438 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2454 if (vp->v_mount != xp->v_mount)
2458 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2459 NDFREE(&nd, NDF_ONLY_PNBUF);
2465 getret = VOP_GETATTR(vp, &at, cred, td);
2467 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2469 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2471 nfsm_srvpostop_attr(getret, &at);
2472 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2478 NDFREE(&nd, NDF_ONLY_PNBUF);
2484 if (nd.ni_dvp == nd.ni_vp)
2491 vn_finished_write(mp);
2496 * nfs symbolic link service
2499 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2500 struct thread *td, struct mbuf **mrq)
2502 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2503 struct sockaddr *nam = nfsd->nd_nam;
2504 caddr_t dpos = nfsd->nd_dpos;
2505 struct ucred *cred = &nfsd->nd_cr;
2506 struct vattr va, dirfor, diraft;
2507 struct nameidata nd;
2508 struct vattr *vap = &va;
2510 struct nfsv2_sattr *sp;
2511 char *bpos, *pathcp = (char *)0;
2514 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2515 int v3 = (nfsd->nd_flag & ND_NFSV3);
2516 struct mbuf *mb, *mreq;
2517 struct vnode *dirp = (struct vnode *)0;
2520 struct mount *mp = NULL;
2523 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2526 fhp = &nfh.fh_generic;
2528 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2532 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2536 (void) vn_start_write(vp, &mp, V_WAIT);
2539 nfsm_srvnamesiz(len);
2540 nd.ni_cnd.cn_cred = cred;
2541 nd.ni_cnd.cn_nameiop = CREATE;
2542 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2543 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2547 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2560 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2561 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2562 iv.iov_base = pathcp;
2564 io.uio_resid = len2;
2568 io.uio_segflg = UIO_SYSSPACE;
2569 io.uio_rw = UIO_READ;
2570 io.uio_td = (struct thread *)0;
2571 nfsm_mtouio(&io, len2);
2573 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
2574 vap->va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2576 *(pathcp + len2) = '\0';
2583 * issue symlink op. SAVESTART is set so the underlying path component
2584 * is only freed by the VOP if an error occurs.
2586 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2588 NDFREE(&nd, NDF_ONLY_PNBUF);
2594 * releases directory prior to potential lookup op.
2602 * Issue lookup. Leave SAVESTART set so we can easily free
2603 * the name buffer later on.
2605 * since LOCKPARENT is not set, ni_dvp will be garbage on
2606 * return whether an error occurs or not.
2608 nd.ni_cnd.cn_nameiop = LOOKUP;
2609 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2610 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2611 nd.ni_cnd.cn_thread = td;
2612 nd.ni_cnd.cn_cred = cred;
2614 error = lookup(&nd);
2618 bzero((caddr_t)fhp, sizeof(nfh));
2619 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2620 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2622 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2631 * These releases aren't strictly required, does even doing them
2632 * make any sense? XXX can nfsm_reply() block?
2635 FREE(pathcp, M_TEMP);
2639 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2643 if (nd.ni_startdir) {
2644 vrele(nd.ni_startdir);
2645 nd.ni_startdir = NULL;
2647 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2650 nfsm_srvpostop_fh(fhp);
2651 nfsm_srvpostop_attr(0, vap);
2653 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2659 NDFREE(&nd, NDF_ONLY_PNBUF);
2661 if (nd.ni_dvp == nd.ni_vp)
2669 vrele(nd.ni_startdir);
2673 FREE(pathcp, M_TEMP);
2675 vn_finished_write(mp);
2683 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2684 struct thread *td, struct mbuf **mrq)
2686 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2687 struct sockaddr *nam = nfsd->nd_nam;
2688 caddr_t dpos = nfsd->nd_dpos;
2689 struct ucred *cred = &nfsd->nd_cr;
2690 struct vattr va, dirfor, diraft;
2691 struct vattr *vap = &va;
2692 struct nfs_fattr *fp;
2693 struct nameidata nd;
2696 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2697 int v3 = (nfsd->nd_flag & ND_NFSV3);
2698 struct mbuf *mb, *mreq;
2699 struct vnode *dirp = NULL;
2703 struct mount *mp = NULL;
2706 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2709 fhp = &nfh.fh_generic;
2711 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2715 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2719 (void) vn_start_write(vp, &mp, V_WAIT);
2722 nfsm_srvnamesiz(len);
2723 nd.ni_cnd.cn_cred = cred;
2724 nd.ni_cnd.cn_nameiop = CREATE;
2725 nd.ni_cnd.cn_flags = LOCKPARENT;
2727 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2731 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2739 nfsm_reply(NFSX_WCCDATA(v3));
2741 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2749 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2750 vap->va_mode = nfstov_mode(*tl++);
2754 * At this point nd.ni_dvp is referenced and exclusively locked and
2755 * nd.ni_vp, if it exists, is referenced but not locked.
2758 vap->va_type = VDIR;
2759 if (nd.ni_vp != NULL) {
2760 NDFREE(&nd, NDF_ONLY_PNBUF);
2766 * Issue mkdir op. Since SAVESTART is not set, the pathname
2767 * component is freed by the VOP call. This will fill-in
2768 * nd.ni_vp, reference, and exclusively lock it.
2770 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2771 NDFREE(&nd, NDF_ONLY_PNBUF);
2778 bzero((caddr_t)fhp, sizeof(nfh));
2779 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2780 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2782 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2786 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2787 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2790 nfsm_srvpostop_fh(fhp);
2791 nfsm_srvpostop_attr(0, vap);
2793 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2795 /* v2, non-error case (see nfsm_reply) */
2796 nfsm_srvfhtom(fhp, v3);
2797 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2798 nfsm_srvfillattr(vap, fp);
2807 NDFREE(&nd, NDF_ONLY_PNBUF);
2808 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2819 vn_finished_write(mp);
2827 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2828 struct thread *td, struct mbuf **mrq)
2830 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2831 struct sockaddr *nam = nfsd->nd_nam;
2832 caddr_t dpos = nfsd->nd_dpos;
2833 struct ucred *cred = &nfsd->nd_cr;
2836 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2837 int v3 = (nfsd->nd_flag & ND_NFSV3);
2838 struct mbuf *mb, *mreq;
2839 struct vnode *vp, *dirp = (struct vnode *)0;
2840 struct vattr dirfor, diraft;
2843 struct nameidata nd;
2844 struct mount *mp = NULL;
2846 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2849 fhp = &nfh.fh_generic;
2851 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2855 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2859 (void) vn_start_write(vp, &mp, V_WAIT);
2862 nfsm_srvnamesiz(len);
2863 nd.ni_cnd.cn_cred = cred;
2864 nd.ni_cnd.cn_nameiop = DELETE;
2865 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2866 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2870 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2878 nfsm_reply(NFSX_WCCDATA(v3));
2880 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2885 if (vp->v_type != VDIR) {
2890 * No rmdir "." please.
2892 if (nd.ni_dvp == vp) {
2897 * The root of a mounted filesystem cannot be deleted.
2899 if (vp->v_flag & VROOT)
2903 * Issue or abort op. Since SAVESTART is not set, path name
2904 * component is freed by the VOP after either.
2907 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2908 NDFREE(&nd, NDF_ONLY_PNBUF);
2911 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2912 nfsm_reply(NFSX_WCCDATA(v3));
2914 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2920 NDFREE(&nd, NDF_ONLY_PNBUF);
2924 if (nd.ni_dvp == nd.ni_vp)
2932 vn_finished_write(mp);
2937 * nfs readdir service
2938 * - mallocs what it thinks is enough to read
2939 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2940 * - calls VOP_READDIR()
2941 * - loops around building the reply
2942 * if the output generated exceeds count break out of loop
2943 * The nfsm_clget macro is used here so that the reply will be packed
2944 * tightly in mbuf clusters.
2945 * - it only knows that it has encountered eof when the VOP_READDIR()
2947 * - as such one readdir rpc will return eof false although you are there
2948 * and then the next will return eof
2949 * - it trims out records with d_fileno == 0
2950 * this doesn't matter for Unix clients, but they might confuse clients
2952 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2953 * than requested, but this may not apply to all filesystems. For
2954 * example, client NFS does not { although it is never remote mounted
2956 * The alternate call nfsrv_readdirplus() does lookups as well.
2957 * PS: The NFS protocol spec. does not clarify what the "count" byte
2958 * argument is a count of.. just name strings and file id's or the
2959 * entire reply rpc or ...
2960 * I tried just file name and id sizes and it confused the Sun client,
2961 * so I am using the full rpc size now. The "paranoia.." comment refers
2962 * to including the status longwords that are not a part of the dir.
2963 * "entry" structures, but are in the rpc.
2967 u_int32_t fl_postopok;
2968 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2970 u_int32_t fl_fhsize;
2971 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2975 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2976 struct thread *td, struct mbuf **mrq)
2978 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2979 struct sockaddr *nam = nfsd->nd_nam;
2980 caddr_t dpos = nfsd->nd_dpos;
2981 struct ucred *cred = &nfsd->nd_cr;
2988 struct mbuf *mb, *mreq;
2989 char *cpos, *cend, *rbuf;
2990 struct vnode *vp = NULL;
2996 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2997 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2998 int v3 = (nfsd->nd_flag & ND_NFSV3);
2999 u_quad_t off, toff, verf;
3000 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3002 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3003 fhp = &nfh.fh_generic;
3006 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
3007 toff = fxdr_hyper(tl);
3009 verf = fxdr_hyper(tl);
3012 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
3013 toff = fxdr_unsigned(u_quad_t, *tl++);
3014 verf = 0; /* shut up gcc */
3017 cnt = fxdr_unsigned(int, *tl);
3018 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3019 xfer = NFS_SRVMAXDATA(nfsd);
3023 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3024 if (!error && vp->v_type != VDIR) {
3030 nfsm_reply(NFSX_UNSIGNED);
3032 nfsm_srvpostop_attr(getret, &at);
3038 * Obtain lock on vnode for this section of the code
3041 error = getret = VOP_GETATTR(vp, &at, cred, td);
3044 * XXX This check may be too strict for Solaris 2.5 clients.
3046 if (!error && toff && verf && verf != at.va_filerev)
3047 error = NFSERR_BAD_COOKIE;
3051 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3055 nfsm_reply(NFSX_POSTOPATTR(v3));
3057 nfsm_srvpostop_attr(getret, &at);
3061 VOP_UNLOCK(vp, 0, td);
3064 * end section. Allocate rbuf and continue
3066 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3069 iv.iov_len = fullsiz;
3072 io.uio_offset = (off_t)off;
3073 io.uio_resid = fullsiz;
3074 io.uio_segflg = UIO_SYSSPACE;
3075 io.uio_rw = UIO_READ;
3076 io.uio_td = (struct thread *)0;
3078 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3080 free((caddr_t)cookies, M_TEMP);
3083 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3084 off = (off_t)io.uio_offset;
3085 if (!cookies && !error)
3086 error = NFSERR_PERM;
3088 getret = VOP_GETATTR(vp, &at, cred, td);
3092 VOP_UNLOCK(vp, 0, td);
3096 free((caddr_t)rbuf, M_TEMP);
3098 free((caddr_t)cookies, M_TEMP);
3099 nfsm_reply(NFSX_POSTOPATTR(v3));
3101 nfsm_srvpostop_attr(getret, &at);
3106 siz -= io.uio_resid;
3109 * If nothing read, return eof
3115 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3118 nfsm_srvpostop_attr(getret, &at);
3119 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3120 txdr_hyper(at.va_filerev, tl);
3123 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3126 FREE((caddr_t)rbuf, M_TEMP);
3127 FREE((caddr_t)cookies, M_TEMP);
3134 * Check for degenerate cases of nothing useful read.
3135 * If so go try again
3139 dp = (struct dirent *)cpos;
3142 * For some reason FreeBSD's ufs_readdir() chooses to back the
3143 * directory offset up to a block boundary, so it is necessary to
3144 * skip over the records that precede the requested offset. This
3145 * requires the assumption that file offset cookies monotonically
3148 while (cpos < cend && ncookies > 0 &&
3149 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3150 ((u_quad_t)(*cookiep)) <= toff)) {
3151 cpos += dp->d_reclen;
3152 dp = (struct dirent *)cpos;
3156 if (cpos >= cend || ncookies == 0) {
3162 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3163 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3165 nfsm_srvpostop_attr(getret, &at);
3166 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3167 txdr_hyper(at.va_filerev, tl);
3171 be = bp + M_TRAILINGSPACE(mp);
3173 /* Loop through the records and build reply */
3174 while (cpos < cend && ncookies > 0) {
3175 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3176 nlen = dp->d_namlen;
3177 rem = nfsm_rndup(nlen) - nlen;
3178 len += (4 * NFSX_UNSIGNED + nlen + rem);
3180 len += 2 * NFSX_UNSIGNED;
3186 * Build the directory record xdr from
3191 bp += NFSX_UNSIGNED;
3195 bp += NFSX_UNSIGNED;
3198 *tl = txdr_unsigned(dp->d_fileno);
3199 bp += NFSX_UNSIGNED;
3201 *tl = txdr_unsigned(nlen);
3202 bp += NFSX_UNSIGNED;
3204 /* And loop around copying the name */
3213 bcopy(cp, bp, tsiz);
3219 /* And null pad to a int32_t boundary */
3220 for (i = 0; i < rem; i++)
3224 /* Finish off the record */
3227 bp += NFSX_UNSIGNED;
3230 *tl = txdr_unsigned(*cookiep);
3231 bp += NFSX_UNSIGNED;
3233 cpos += dp->d_reclen;
3234 dp = (struct dirent *)cpos;
3242 bp += NFSX_UNSIGNED;
3248 bp += NFSX_UNSIGNED;
3251 mp->m_len = bp - mtod(mp, caddr_t);
3253 mp->m_len += bp - bpos;
3254 FREE((caddr_t)rbuf, M_TEMP);
3255 FREE((caddr_t)cookies, M_TEMP);
3264 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3265 struct thread *td, struct mbuf **mrq)
3267 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3268 struct sockaddr *nam = nfsd->nd_nam;
3269 caddr_t dpos = nfsd->nd_dpos;
3270 struct ucred *cred = &nfsd->nd_cr;
3277 struct mbuf *mb, *mreq;
3278 char *cpos, *cend, *rbuf;
3279 struct vnode *vp = NULL, *nvp;
3282 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3285 struct vattr va, at, *vap = &va;
3286 struct nfs_fattr *fp;
3287 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3288 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3289 u_quad_t off, toff, verf;
3290 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3291 int v3 = (nfsd->nd_flag & ND_NFSV3);
3293 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3295 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3296 fhp = &nfh.fh_generic;
3298 tl = nfsm_dissect(u_int32_t *, 6 * NFSX_UNSIGNED);
3299 toff = fxdr_hyper(tl);
3301 verf = fxdr_hyper(tl);
3303 siz = fxdr_unsigned(int, *tl++);
3304 cnt = fxdr_unsigned(int, *tl);
3306 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3307 xfer = NFS_SRVMAXDATA(nfsd);
3311 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3312 if (!error && vp->v_type != VDIR) {
3318 nfsm_reply(NFSX_UNSIGNED);
3319 nfsm_srvpostop_attr(getret, &at);
3323 error = getret = VOP_GETATTR(vp, &at, cred, td);
3326 * XXX This check may be too strict for Solaris 2.5 clients.
3328 if (!error && toff && verf && verf != at.va_filerev)
3329 error = NFSERR_BAD_COOKIE;
3332 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3336 nfsm_reply(NFSX_V3POSTOPATTR);
3337 nfsm_srvpostop_attr(getret, &at);
3341 VOP_UNLOCK(vp, 0, td);
3342 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3345 iv.iov_len = fullsiz;
3348 io.uio_offset = (off_t)off;
3349 io.uio_resid = fullsiz;
3350 io.uio_segflg = UIO_SYSSPACE;
3351 io.uio_rw = UIO_READ;
3352 io.uio_td = (struct thread *)0;
3354 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3356 free((caddr_t)cookies, M_TEMP);
3359 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3360 off = (u_quad_t)io.uio_offset;
3361 getret = VOP_GETATTR(vp, &at, cred, td);
3362 VOP_UNLOCK(vp, 0, td);
3363 if (!cookies && !error)
3364 error = NFSERR_PERM;
3371 free((caddr_t)cookies, M_TEMP);
3372 free((caddr_t)rbuf, M_TEMP);
3373 nfsm_reply(NFSX_V3POSTOPATTR);
3374 nfsm_srvpostop_attr(getret, &at);
3379 siz -= io.uio_resid;
3382 * If nothing read, return eof
3388 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3390 nfsm_srvpostop_attr(getret, &at);
3391 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3392 txdr_hyper(at.va_filerev, tl);
3396 FREE((caddr_t)cookies, M_TEMP);
3397 FREE((caddr_t)rbuf, M_TEMP);
3404 * Check for degenerate cases of nothing useful read.
3405 * If so go try again
3409 dp = (struct dirent *)cpos;
3412 * For some reason FreeBSD's ufs_readdir() chooses to back the
3413 * directory offset up to a block boundary, so it is necessary to
3414 * skip over the records that precede the requested offset. This
3415 * requires the assumption that file offset cookies monotonically
3418 while (cpos < cend && ncookies > 0 &&
3419 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3420 ((u_quad_t)(*cookiep)) <= toff)) {
3421 cpos += dp->d_reclen;
3422 dp = (struct dirent *)cpos;
3426 if (cpos >= cend || ncookies == 0) {
3433 * Probe one of the directory entries to see if the filesystem
3436 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3437 error = NFSERR_NOTSUPP;
3440 free((caddr_t)cookies, M_TEMP);
3441 free((caddr_t)rbuf, M_TEMP);
3442 nfsm_reply(NFSX_V3POSTOPATTR);
3443 nfsm_srvpostop_attr(getret, &at);
3450 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3453 nfsm_srvpostop_attr(getret, &at);
3454 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3455 txdr_hyper(at.va_filerev, tl);
3458 be = bp + M_TRAILINGSPACE(mp);
3460 /* Loop through the records and build reply */
3461 while (cpos < cend && ncookies > 0) {
3462 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3463 nlen = dp->d_namlen;
3464 rem = nfsm_rndup(nlen)-nlen;
3467 * For readdir_and_lookup get the vnode using
3470 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3472 bzero((caddr_t)nfhp, NFSX_V3FH);
3474 nvp->v_mount->mnt_stat.f_fsid;
3475 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3480 if (VOP_GETATTR(nvp, vap, cred, td)) {
3489 * If either the dircount or maxcount will be
3490 * exceeded, get out now. Both of these lengths
3491 * are calculated conservatively, including all
3494 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3496 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3497 if (len > cnt || dirlen > fullsiz) {
3503 * Build the directory record xdr from
3506 fp = (struct nfs_fattr *)&fl.fl_fattr;
3507 nfsm_srvfillattr(vap, fp);
3508 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3509 fl.fl_fhok = nfs_true;
3510 fl.fl_postopok = nfs_true;
3511 fl.fl_off.nfsuquad[0] = 0;
3512 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3516 bp += NFSX_UNSIGNED;
3519 bp += NFSX_UNSIGNED;
3521 *tl = txdr_unsigned(dp->d_fileno);
3522 bp += NFSX_UNSIGNED;
3524 *tl = txdr_unsigned(nlen);
3525 bp += NFSX_UNSIGNED;
3527 /* And loop around copying the name */
3532 if ((bp + xfer) > be)
3536 bcopy(cp, bp, tsiz);
3542 /* And null pad to a int32_t boundary */
3543 for (i = 0; i < rem; i++)
3547 * Now copy the flrep structure out.
3549 xfer = sizeof (struct flrep);
3553 if ((bp + xfer) > be)
3557 bcopy(cp, bp, tsiz);
3565 cpos += dp->d_reclen;
3566 dp = (struct dirent *)cpos;
3574 bp += NFSX_UNSIGNED;
3580 bp += NFSX_UNSIGNED;
3583 mp->m_len = bp - mtod(mp, caddr_t);
3585 mp->m_len += bp - bpos;
3586 FREE((caddr_t)cookies, M_TEMP);
3587 FREE((caddr_t)rbuf, M_TEMP);
3595 * nfs commit service
3598 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3599 struct thread *td, struct mbuf **mrq)
3601 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3602 struct sockaddr *nam = nfsd->nd_nam;
3603 caddr_t dpos = nfsd->nd_dpos;
3604 struct ucred *cred = &nfsd->nd_cr;
3605 struct vattr bfor, aft;
3606 struct vnode *vp = NULL;
3611 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3612 struct mbuf *mb, *mreq;
3614 struct mount *mp = NULL;
3615 int v3 = (nfsd->nd_flag & ND_NFSV3);
3617 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3619 panic("nfsrv_commit: v3 proc called on a v2 connection");
3620 fhp = &nfh.fh_generic;
3622 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3626 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
3630 (void) vn_start_write(vp, &mp, V_WAIT);
3633 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
3636 * XXX At this time VOP_FSYNC() does not accept offset and byte
3637 * count parameters, so these arguments are useless (someday maybe).
3639 off = fxdr_hyper(tl);
3641 cnt = fxdr_unsigned(int, *tl);
3642 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3644 nfsm_reply(2 * NFSX_UNSIGNED);
3645 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3649 for_ret = VOP_GETATTR(vp, &bfor, cred, td);
3651 if (cnt > MAX_COMMIT_COUNT) {
3653 * Give up and do the whole thing
3656 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3657 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3659 error = VOP_FSYNC(vp, cred, MNT_WAIT, td);
3662 * Locate and synchronously write any buffers that fall
3663 * into the requested range. Note: we are assuming that
3664 * f_iosize is a power of 2.
3666 int iosize = vp->v_mount->mnt_stat.f_iosize;
3667 int iomask = iosize - 1;
3672 * Align to iosize boundry, super-align to page boundry.
3675 cnt += off & iomask;
3676 off &= ~(u_quad_t)iomask;
3678 if (off & PAGE_MASK) {
3679 cnt += off & PAGE_MASK;
3680 off &= ~(u_quad_t)PAGE_MASK;
3682 lblkno = off / iosize;
3685 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3686 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3694 * If we have a buffer and it is marked B_DELWRI we
3695 * have to lock and write it. Otherwise the prior
3696 * write is assumed to have already been committed.
3698 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3699 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3700 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3701 continue; /* retry */
3704 bp->b_flags &= ~B_ASYNC;
3717 aft_ret = VOP_GETATTR(vp, &aft, cred, td);
3721 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3722 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3724 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3725 if (nfsver.tv_sec == 0)
3727 *tl++ = txdr_unsigned(nfsver.tv_sec);
3728 *tl = txdr_unsigned(nfsver.tv_usec);
3735 vn_finished_write(mp);
3740 * nfs statfs service
3743 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3744 struct thread *td, struct mbuf **mrq)
3746 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3747 struct sockaddr *nam = nfsd->nd_nam;
3748 caddr_t dpos = nfsd->nd_dpos;
3749 struct ucred *cred = &nfsd->nd_cr;
3751 struct nfs_statfs *sfp;
3754 int error = 0, rdonly, getret = 1;
3755 int v3 = (nfsd->nd_flag & ND_NFSV3);
3756 struct mbuf *mb, *mreq;
3757 struct vnode *vp = NULL;
3761 struct statfs statfs;
3764 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3765 fhp = &nfh.fh_generic;
3767 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3769 nfsm_reply(NFSX_UNSIGNED);
3771 nfsm_srvpostop_attr(getret, &at);
3776 error = VFS_STATFS(vp->v_mount, sf, td);
3777 getret = VOP_GETATTR(vp, &at, cred, td);
3780 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3782 nfsm_srvpostop_attr(getret, &at);
3787 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3789 tval = (u_quad_t)sf->f_blocks;
3790 tval *= (u_quad_t)sf->f_bsize;
3791 txdr_hyper(tval, &sfp->sf_tbytes);
3792 tval = (u_quad_t)sf->f_bfree;
3793 tval *= (u_quad_t)sf->f_bsize;
3794 txdr_hyper(tval, &sfp->sf_fbytes);
3795 tval = (u_quad_t)sf->f_bavail;
3796 tval *= (u_quad_t)sf->f_bsize;
3797 txdr_hyper(tval, &sfp->sf_abytes);
3798 sfp->sf_tfiles.nfsuquad[0] = 0;
3799 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3800 sfp->sf_ffiles.nfsuquad[0] = 0;
3801 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3802 sfp->sf_afiles.nfsuquad[0] = 0;
3803 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3804 sfp->sf_invarsec = 0;
3806 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3807 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3808 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3809 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3810 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3819 * nfs fsinfo service
3822 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3823 struct thread *td, struct mbuf **mrq)
3825 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3826 struct sockaddr *nam = nfsd->nd_nam;
3827 caddr_t dpos = nfsd->nd_dpos;
3828 struct ucred *cred = &nfsd->nd_cr;
3830 struct nfsv3_fsinfo *sip;
3832 int error = 0, rdonly, getret = 1, pref;
3833 struct mbuf *mb, *mreq;
3834 struct vnode *vp = NULL;
3840 int v3 = (nfsd->nd_flag & ND_NFSV3);
3842 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3844 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3845 fhp = &nfh.fh_generic;
3847 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3849 nfsm_reply(NFSX_UNSIGNED);
3850 nfsm_srvpostop_attr(getret, &at);
3855 /* XXX Try to make a guess on the max file size. */
3856 VFS_STATFS(vp->v_mount, &sb, td);
3857 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3859 getret = VOP_GETATTR(vp, &at, cred, td);
3862 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3863 nfsm_srvpostop_attr(getret, &at);
3864 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3868 * There should be file system VFS OP(s) to get this information.
3869 * For now, assume ufs.
3871 if (slp->ns_so->so_type == SOCK_DGRAM)
3872 pref = NFS_MAXDGRAMDATA;
3875 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3876 sip->fs_rtpref = txdr_unsigned(pref);
3877 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3878 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3879 sip->fs_wtpref = txdr_unsigned(pref);
3880 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3881 sip->fs_dtpref = txdr_unsigned(pref);
3882 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3883 sip->fs_timedelta.nfsv3_sec = 0;
3884 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3885 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3886 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3887 NFSV3FSINFO_CANSETTIME);
3895 * nfs pathconf service
3898 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3899 struct thread *td, struct mbuf **mrq)
3901 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3902 struct sockaddr *nam = nfsd->nd_nam;
3903 caddr_t dpos = nfsd->nd_dpos;
3904 struct ucred *cred = &nfsd->nd_cr;
3906 struct nfsv3_pathconf *pc;
3908 int error = 0, rdonly, getret = 1;
3909 register_t linkmax, namemax, chownres, notrunc;
3910 struct mbuf *mb, *mreq;
3911 struct vnode *vp = NULL;
3915 int v3 = (nfsd->nd_flag & ND_NFSV3);
3917 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3919 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3920 fhp = &nfh.fh_generic;
3922 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3924 nfsm_reply(NFSX_UNSIGNED);
3925 nfsm_srvpostop_attr(getret, &at);
3929 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3931 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3933 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3935 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3936 getret = VOP_GETATTR(vp, &at, cred, td);
3939 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3940 nfsm_srvpostop_attr(getret, &at);
3945 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3947 pc->pc_linkmax = txdr_unsigned(linkmax);
3948 pc->pc_namemax = txdr_unsigned(namemax);
3949 pc->pc_notrunc = txdr_unsigned(notrunc);
3950 pc->pc_chownrestricted = txdr_unsigned(chownres);
3953 * These should probably be supported by VOP_PATHCONF(), but
3954 * until msdosfs is exportable (why would you want to?), the
3955 * Unix defaults should be ok.
3957 pc->pc_caseinsensitive = nfs_false;
3958 pc->pc_casepreserving = nfs_true;
3966 * Null operation, used by clients to ping server
3970 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3971 struct thread *td, struct mbuf **mrq)
3973 struct mbuf *mrep = nfsd->nd_mrep;
3975 int error = NFSERR_RETVOID;
3976 struct mbuf *mb, *mreq;
3978 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3985 * No operation, used for obsolete procedures
3989 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3990 struct thread *td, struct mbuf **mrq)
3992 struct mbuf *mrep = nfsd->nd_mrep;
3995 struct mbuf *mb, *mreq;
3997 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3998 if (nfsd->nd_repstat)
3999 error = nfsd->nd_repstat;
4001 error = EPROCUNAVAIL;
4009 * Perform access checking for vnodes obtained from file handles that would
4010 * refer to files already opened by a Unix client. You cannot just use
4011 * vn_writechk() and VOP_ACCESS() for two reasons.
4012 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4013 * 2 - The owner is to be given access irrespective of mode bits for some
4014 * operations, so that processes that chmod after opening a file don't
4015 * break. I don't like this because it opens a security hole, but since
4016 * the nfs server opens a security hole the size of a barn door anyhow,
4019 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4020 * will return EPERM instead of EACCESS. EPERM is always an error.
4023 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
4024 struct thread *td, int override)
4029 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4030 if (flags & VWRITE) {
4031 /* Just vn_writechk() changed to check rdonly */
4033 * Disallow write attempts on read-only file systems;
4034 * unless the file is a socket or a block or character
4035 * device resident on the file system.
4037 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4038 switch (vp->v_type) {
4048 * If there's shared text associated with
4049 * the inode, we can't allow writing.
4051 if (vp->v_flag & VTEXT)
4054 error = VOP_GETATTR(vp, &vattr, cred, td);
4057 error = VOP_ACCESS(vp, flags, cred, td);
4059 * Allow certain operations for the owner (reads and writes
4060 * on files that are already open).
4062 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)