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;
248 int error = 0, rdonly;
249 struct mbuf *mb, *mreq;
251 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
252 fhp = &nfh.fh_generic;
254 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
260 error = VOP_GETATTR(vp, vap, cred, td);
263 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
268 fp = nfsm_build(struct nfs_fattr *,
269 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
270 nfsm_srvfillattr(vap, fp);
280 * nfs setattr service
283 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
284 struct thread *td, struct mbuf **mrq)
286 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
287 struct sockaddr *nam = nfsd->nd_nam;
288 caddr_t dpos = nfsd->nd_dpos;
289 struct ucred *cred = &nfsd->nd_cr;
290 struct vattr va, preat;
291 struct vattr *vap = &va;
292 struct nfsv2_sattr *sp;
293 struct nfs_fattr *fp;
294 struct vnode *vp = NULL;
299 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
300 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
301 struct mbuf *mb, *mreq;
302 struct timespec guard;
303 struct mount *mp = NULL;
305 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
306 fhp = &nfh.fh_generic;
308 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
312 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
316 (void) vn_start_write(vp, &mp, V_WAIT);
322 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
323 gcheck = fxdr_unsigned(int, *tl);
325 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
326 fxdr_nfsv3time(tl, &guard);
329 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
331 * Nah nah nah nah na nah
332 * There is a bug in the Sun client that puts 0xffff in the mode
333 * field of sattr when it should put in 0xffffffff. The u_short
334 * doesn't sign extend.
335 * --> check the low order 2 bytes for 0xffff
337 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
338 vap->va_mode = nfstov_mode(sp->sa_mode);
339 if (sp->sa_uid != nfsrv_nfs_xdrneg1)
340 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
341 if (sp->sa_gid != nfsrv_nfs_xdrneg1)
342 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
343 if (sp->sa_size != nfsrv_nfs_xdrneg1)
344 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
345 if (sp->sa_atime.nfsv2_sec != nfsrv_nfs_xdrneg1) {
347 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
349 vap->va_atime.tv_sec =
350 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
351 vap->va_atime.tv_nsec = 0;
354 if (sp->sa_mtime.nfsv2_sec != nfsrv_nfs_xdrneg1)
355 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
360 * Now that we have all the fields, lets do it.
362 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
364 nfsm_reply(2 * NFSX_UNSIGNED);
366 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
372 * vp now an active resource, pay careful attention to cleanup
375 error = preat_ret = VOP_GETATTR(vp, &preat, cred, td);
376 if (!error && gcheck &&
377 (preat.va_ctime.tv_sec != guard.tv_sec ||
378 preat.va_ctime.tv_nsec != guard.tv_nsec))
379 error = NFSERR_NOT_SYNC;
383 nfsm_reply(NFSX_WCCDATA(v3));
385 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
392 * If the size is being changed write acces is required, otherwise
393 * just check for a read only file system.
395 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
396 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
401 if (vp->v_type == VDIR) {
404 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
408 error = VOP_SETATTR(vp, vap, cred, td);
409 postat_ret = VOP_GETATTR(vp, vap, cred, td);
416 nfsm_reply(NFSX_WCCORFATTR(v3));
418 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
422 /* v2 non-error case (see nfsm_reply). */
423 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
424 nfsm_srvfillattr(vap, fp);
431 vn_finished_write(mp);
439 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
440 struct thread *td, struct mbuf **mrq)
442 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
443 struct sockaddr *nam = nfsd->nd_nam;
444 caddr_t dpos = nfsd->nd_dpos;
445 struct ucred *cred = &nfsd->nd_cr;
446 struct nfs_fattr *fp;
447 struct nameidata nd, ind, *ndp = &nd;
448 struct vnode *vp, *dirp = NULL;
452 int error = 0, len, dirattr_ret = 1;
453 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
454 struct mbuf *mb, *mreq;
455 struct vattr va, dirattr, *vap = &va;
457 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
460 fhp = &nfh.fh_generic;
462 nfsm_srvnamesiz(len);
464 pubflag = nfs_ispublicfh(fhp);
466 nd.ni_cnd.cn_cred = cred;
467 nd.ni_cnd.cn_nameiop = LOOKUP;
468 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
469 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
473 * namei failure, only dirp to cleanup. Clear out garbarge from
474 * structure in case macros jump to nfsmout.
480 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
485 nfsm_reply(NFSX_POSTOPATTR(v3));
487 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
493 * Locate index file for public filehandle
495 * error is 0 on entry and 0 on exit from this block.
499 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
501 * Setup call to lookup() to see if we can find
502 * the index file. Arguably, this doesn't belong
503 * in a kernel.. Ugh. If an error occurs, do not
504 * try to install an index file and then clear the
507 * When we replace nd with ind and redirect ndp,
508 * maintenance of ni_startdir and ni_vp shift to
509 * ind and we have to clean them up in the old nd.
510 * However, the cnd resource continues to be maintained
511 * via the original nd. Confused? You aren't alone!
514 VOP_UNLOCK(nd.ni_vp, 0, td);
515 ind.ni_pathlen = strlen(nfs_pub.np_index);
516 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
518 ind.ni_startdir = nd.ni_vp;
519 VREF(ind.ni_startdir);
521 error = lookup(&ind);
526 * Found an index file. Get rid of
527 * the old references. transfer nd.ni_vp'
533 vrele(nd.ni_startdir);
534 nd.ni_startdir = NULL;
540 * If the public filehandle was used, check that this lookup
541 * didn't result in a filehandle outside the publicly exported
542 * filesystem. We clear the poor vp here to avoid lockups due
546 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
555 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
562 * Resources at this point:
563 * ndp->ni_vp may not be NULL
568 nfsm_reply(NFSX_POSTOPATTR(v3));
570 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
576 * Clear out some resources prior to potentially blocking. This
577 * is not as critical as ni_dvp resources in other routines, but
580 vrele(ndp->ni_startdir);
581 ndp->ni_startdir = NULL;
582 NDFREE(&nd, NDF_ONLY_PNBUF);
585 * Get underlying attribute, then release remaining resources ( for
586 * the same potential blocking reason ) and reply.
589 bzero((caddr_t)fhp, sizeof(nfh));
590 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
591 error = VFS_VPTOFH(vp, &fhp->fh_fid);
593 error = VOP_GETATTR(vp, vap, cred, td);
597 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
600 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
604 nfsm_srvfhtom(fhp, v3);
606 nfsm_srvpostop_attr(0, vap);
607 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
609 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
610 nfsm_srvfillattr(vap, fp);
616 NDFREE(&nd, NDF_ONLY_PNBUF);
617 if (ndp->ni_startdir)
618 vrele(ndp->ni_startdir);
625 * nfs readlink service
628 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
629 struct thread *td, struct mbuf **mrq)
631 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
632 struct sockaddr *nam = nfsd->nd_nam;
633 caddr_t dpos = nfsd->nd_dpos;
634 struct ucred *cred = &nfsd->nd_cr;
635 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
636 struct iovec *ivp = iv;
640 int error = 0, rdonly, i, tlen, len, getret;
641 int v3 = (nfsd->nd_flag & ND_NFSV3);
642 struct mbuf *mb, *mp3, *nmp, *mreq;
643 struct vnode *vp = NULL;
647 struct uio io, *uiop = &io;
649 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
651 mp = (struct mbuf *)0;
654 fhp = &nfh.fh_generic;
658 while (len < NFS_MAXPATHLEN) {
659 MGET(nmp, M_TRYWAIT, MT_DATA);
660 MCLGET(nmp, M_TRYWAIT);
661 nmp->m_len = NFSMSIZ(nmp);
668 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
669 mp->m_len = NFS_MAXPATHLEN - len;
670 len = NFS_MAXPATHLEN;
673 ivp->iov_base = mtod(mp, caddr_t);
674 ivp->iov_len = mp->m_len;
679 uiop->uio_iovcnt = i;
680 uiop->uio_offset = 0;
681 uiop->uio_resid = len;
682 uiop->uio_rw = UIO_READ;
683 uiop->uio_segflg = UIO_SYSSPACE;
684 uiop->uio_td = (struct thread *)0;
685 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
687 nfsm_reply(2 * NFSX_UNSIGNED);
689 nfsm_srvpostop_attr(1, (struct vattr *)0);
693 if (vp->v_type != VLNK) {
700 error = VOP_READLINK(vp, uiop, cred);
702 getret = VOP_GETATTR(vp, &attr, cred, td);
705 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
707 nfsm_srvpostop_attr(getret, &attr);
713 if (uiop->uio_resid > 0) {
714 len -= uiop->uio_resid;
715 tlen = nfsm_rndup(len);
716 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
718 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
719 *tl = txdr_unsigned(len);
734 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
735 struct thread *td, struct mbuf **mrq)
737 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
738 struct sockaddr *nam = nfsd->nd_nam;
739 caddr_t dpos = nfsd->nd_dpos;
740 struct ucred *cred = &nfsd->nd_cr;
744 struct nfs_fattr *fp;
748 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
749 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
750 struct mbuf *mb, *mreq;
752 struct vnode *vp = NULL;
755 struct uio io, *uiop = &io;
756 struct vattr va, *vap = &va;
761 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
762 fhp = &nfh.fh_generic;
765 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
766 off = fxdr_hyper(tl);
768 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
769 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
771 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
774 * Reference vp. If an error occurs, vp will be invalid, but we
775 * have to NULL it just in case. The macros might goto nfsmout
779 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
782 nfsm_reply(2 * NFSX_UNSIGNED);
784 nfsm_srvpostop_attr(1, (struct vattr *)0);
789 if (vp->v_type != VREG) {
793 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
796 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
797 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
799 getret = VOP_GETATTR(vp, vap, cred, td);
805 nfsm_reply(NFSX_POSTOPATTR(v3));
807 nfsm_srvpostop_attr(getret, vap);
813 * Calculate byte count to read
816 if (off >= vap->va_size)
818 else if ((off + reqlen) > vap->va_size)
819 cnt = vap->va_size - off;
824 * Calculate seqcount for heuristic
832 * Locate best candidate
835 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
839 if (nfsheur[hi].nh_vp == vp) {
843 if (nfsheur[hi].nh_use > 0)
844 --nfsheur[hi].nh_use;
845 hi = (hi + 1) & (NUM_HEURISTIC - 1);
846 if (nfsheur[hi].nh_use < nh->nh_use)
850 if (nh->nh_vp != vp) {
853 nh->nh_use = NHUSE_INIT;
861 * Calculate heuristic
864 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
865 if (++nh->nh_seqcount > 127)
866 nh->nh_seqcount = 127;
867 } else if (nh->nh_seqcount > 1) {
872 nh->nh_use += NHUSE_INC;
873 if (nh->nh_use > NHUSE_MAX)
874 nh->nh_use = NHUSE_MAX;
875 ioflag |= nh->nh_seqcount << 16;
878 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
880 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
881 *tl++ = nfsrv_nfs_true;
882 fp = (struct nfs_fattr *)tl;
883 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
885 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
886 fp = (struct nfs_fattr *)tl;
887 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
889 len = left = nfsm_rndup(cnt);
892 * Generate the mbuf list with the uio_iov ref. to it.
897 siz = min(M_TRAILINGSPACE(m), left);
903 MGET(m, M_TRYWAIT, MT_DATA);
904 MCLGET(m, M_TRYWAIT);
910 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
912 uiop->uio_iov = iv2 = iv;
918 panic("nfsrv_read iov");
919 siz = min(M_TRAILINGSPACE(m), left);
921 iv->iov_base = mtod(m, caddr_t) + m->m_len;
930 uiop->uio_iovcnt = i;
931 uiop->uio_offset = off;
932 uiop->uio_resid = len;
933 uiop->uio_rw = UIO_READ;
934 uiop->uio_segflg = UIO_SYSSPACE;
935 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
936 off = uiop->uio_offset;
938 FREE((caddr_t)iv2, M_TEMP);
939 if (error || (getret = VOP_GETATTR(vp, vap, cred, td))) {
945 nfsm_reply(NFSX_POSTOPATTR(v3));
947 nfsm_srvpostop_attr(getret, vap);
956 nfsm_srvfillattr(vap, fp);
957 tlen = len - uiop->uio_resid;
958 cnt = cnt < tlen ? cnt : tlen;
959 tlen = nfsm_rndup(cnt);
960 if (len != tlen || tlen != cnt)
961 nfsm_adj(mb, len - tlen, tlen - cnt);
963 *tl++ = txdr_unsigned(cnt);
965 *tl++ = nfsrv_nfs_true;
967 *tl++ = nfsrv_nfs_false;
969 *tl = txdr_unsigned(cnt);
980 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
981 struct thread *td, struct mbuf **mrq)
983 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
984 struct sockaddr *nam = nfsd->nd_nam;
985 caddr_t dpos = nfsd->nd_dpos;
986 struct ucred *cred = &nfsd->nd_cr;
990 struct nfs_fattr *fp;
992 struct vattr va, forat;
993 struct vattr *vap = &va;
996 int error = 0, rdonly, len, forat_ret = 1;
997 int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
998 int stable = NFSV3WRITE_FILESYNC;
999 int v3 = (nfsd->nd_flag & ND_NFSV3);
1000 struct mbuf *mb, *mreq;
1001 struct vnode *vp = NULL;
1004 struct uio io, *uiop = &io;
1006 struct mount *mntp = NULL;
1008 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1014 fhp = &nfh.fh_generic;
1016 if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1020 if ((error = VFS_FHTOVP(mntp, &fhp->fh_fid, &vp)) != 0) {
1024 (void) vn_start_write(vp, &mntp, V_WAIT);
1028 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1029 off = fxdr_hyper(tl);
1031 stable = fxdr_unsigned(int, *tl++);
1033 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1034 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1037 stable = NFSV3WRITE_UNSTABLE;
1039 retlen = len = fxdr_unsigned(int32_t, *tl);
1043 * For NFS Version 2, it is not obvious what a write of zero length
1044 * should do, but I might as well be consistent with Version 3,
1045 * which is to return ok so long as there are no permission problems.
1053 adjust = dpos - mtod(mp, caddr_t);
1054 mp->m_len -= adjust;
1055 if (mp->m_len > 0 && adjust > 0)
1056 mp->m_data += adjust;
1060 else if (mp->m_len > 0) {
1063 mp->m_len -= (i - len);
1072 if (len > NFS_MAXDATA || len < 0 || i < len) {
1074 nfsm_reply(2 * NFSX_UNSIGNED);
1076 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1080 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
1083 nfsm_reply(2 * NFSX_UNSIGNED);
1085 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1090 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1091 if (vp->v_type != VREG) {
1095 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1098 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1102 nfsm_reply(NFSX_WCCDATA(v3));
1104 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1110 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1112 uiop->uio_iov = iv = ivp;
1113 uiop->uio_iovcnt = cnt;
1116 if (mp->m_len > 0) {
1117 ivp->iov_base = mtod(mp, caddr_t);
1118 ivp->iov_len = mp->m_len;
1126 * The IO_METASYNC flag indicates that all metadata (and not just
1127 * enough to ensure data integrity) mus be written to stable storage
1129 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1131 if (stable == NFSV3WRITE_UNSTABLE)
1132 ioflags = IO_NODELOCKED;
1133 else if (stable == NFSV3WRITE_DATASYNC)
1134 ioflags = (IO_SYNC | IO_NODELOCKED);
1136 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1137 uiop->uio_resid = len;
1138 uiop->uio_rw = UIO_WRITE;
1139 uiop->uio_segflg = UIO_SYSSPACE;
1140 uiop->uio_td = (struct thread *)0;
1141 uiop->uio_offset = off;
1142 error = VOP_WRITE(vp, uiop, ioflags, cred);
1143 nfsrvstats.srvvop_writes++;
1144 FREE((caddr_t)iv, M_TEMP);
1146 aftat_ret = VOP_GETATTR(vp, vap, cred, td);
1152 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1153 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1155 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1160 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1161 *tl++ = txdr_unsigned(retlen);
1163 * If nfs_async is set, then pretend the write was FILESYNC.
1165 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1166 *tl++ = txdr_unsigned(stable);
1168 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1170 * Actually, there is no need to txdr these fields,
1171 * but it may make the values more human readable,
1172 * for debugging purposes.
1174 if (nfsver.tv_sec == 0)
1176 *tl++ = txdr_unsigned(nfsver.tv_sec);
1177 *tl = txdr_unsigned(nfsver.tv_usec);
1179 /* v2, non-error case (see nfsm_reply). */
1180 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1181 nfsm_srvfillattr(vap, fp);
1186 vn_finished_write(mntp);
1191 * NFS write service with write gathering support. Called when
1192 * nfsrvw_procrastinate > 0.
1193 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1194 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1198 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1199 struct thread *td, struct mbuf **mrq)
1203 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1204 struct nfs_fattr *fp;
1207 struct nfsrvw_delayhash *wpp;
1209 struct vattr va, forat;
1212 int error = 0, rdonly, len, forat_ret = 1;
1213 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1214 struct mbuf *mb, *mreq, *mrep, *md;
1215 struct vnode *vp = NULL;
1216 struct uio io, *uiop = &io;
1218 struct mount *mntp = NULL;
1220 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1229 mrep = nfsd->nd_mrep;
1231 dpos = nfsd->nd_dpos;
1232 cred = &nfsd->nd_cr;
1233 v3 = (nfsd->nd_flag & ND_NFSV3);
1234 LIST_INIT(&nfsd->nd_coalesce);
1235 nfsd->nd_mreq = NULL;
1236 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1237 cur_usec = nfs_curusec();
1238 nfsd->nd_time = cur_usec +
1239 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1242 * Now, get the write header..
1244 nfsm_srvmtofh(&nfsd->nd_fh);
1246 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1247 nfsd->nd_off = fxdr_hyper(tl);
1249 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1251 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1252 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1255 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1257 len = fxdr_unsigned(int32_t, *tl);
1259 nfsd->nd_eoff = nfsd->nd_off + len;
1262 * Trim the header out of the mbuf list and trim off any trailing
1263 * junk so that the mbuf list has only the write data.
1271 adjust = dpos - mtod(mp, caddr_t);
1272 mp->m_len -= adjust;
1273 if (mp->m_len > 0 && adjust > 0)
1274 mp->m_data += adjust;
1281 mp->m_len -= (i - len);
1287 if (len > NFS_MAXDATA || len < 0 || i < len) {
1291 nfsm_writereply(2 * NFSX_UNSIGNED);
1293 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1294 nfsd->nd_mreq = mreq;
1295 nfsd->nd_mrep = NULL;
1300 * Add this entry to the hash and time queues.
1304 wp = LIST_FIRST(&slp->ns_tq);
1305 while (wp && wp->nd_time < nfsd->nd_time) {
1307 wp = LIST_NEXT(wp, nd_tq);
1309 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1311 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1313 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1315 if (nfsd->nd_mrep) {
1316 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1318 wp = LIST_FIRST(wpp);
1320 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)){
1322 wp = LIST_NEXT(wp, nd_hash);
1324 while (wp && wp->nd_off < nfsd->nd_off &&
1325 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)) {
1327 wp = LIST_NEXT(wp, nd_hash);
1330 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1333 * Search the hash list for overlapping entries and
1336 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1337 wp = LIST_NEXT(nfsd, nd_hash);
1338 if (NFSW_SAMECRED(owp, nfsd))
1339 nfsrvw_coalesce(owp, nfsd);
1342 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1349 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1350 * and generate the associated reply mbuf list(s).
1353 cur_usec = nfs_curusec();
1355 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1356 owp = LIST_NEXT(nfsd, nd_tq);
1357 if (nfsd->nd_time > cur_usec)
1361 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1362 LIST_REMOVE(nfsd, nd_tq);
1363 LIST_REMOVE(nfsd, nd_hash);
1365 mrep = nfsd->nd_mrep;
1366 nfsd->nd_mrep = NULL;
1367 cred = &nfsd->nd_cr;
1368 v3 = (nfsd->nd_flag & ND_NFSV3);
1369 forat_ret = aftat_ret = 1;
1370 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1371 nfsd->nd_nam, &rdonly, TRUE);
1374 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1375 if (vp->v_type != VREG) {
1379 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1385 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1386 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1387 ioflags = IO_NODELOCKED;
1388 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1389 ioflags = (IO_SYNC | IO_NODELOCKED);
1391 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1392 uiop->uio_rw = UIO_WRITE;
1393 uiop->uio_segflg = UIO_SYSSPACE;
1394 uiop->uio_td = (struct thread *)0;
1395 uiop->uio_offset = nfsd->nd_off;
1396 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1397 if (uiop->uio_resid > 0) {
1405 uiop->uio_iovcnt = i;
1406 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1408 uiop->uio_iov = ivp = iov;
1411 if (mp->m_len > 0) {
1412 ivp->iov_base = mtod(mp, caddr_t);
1413 ivp->iov_len = mp->m_len;
1419 if (vn_start_write(vp, &mntp, V_NOWAIT) != 0) {
1420 VOP_UNLOCK(vp, 0, td);
1421 error = vn_start_write(NULL, &mntp, V_WAIT);
1422 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1426 error = VOP_WRITE(vp, uiop, ioflags, cred);
1427 nfsrvstats.srvvop_writes++;
1428 vn_finished_write(mntp);
1430 FREE((caddr_t)iov, M_TEMP);
1434 aftat_ret = VOP_GETATTR(vp, &va, cred, td);
1440 * Loop around generating replies for all write rpcs that have
1441 * now been completed.
1445 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1447 nfsm_writereply(NFSX_WCCDATA(v3));
1449 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1452 nfsm_writereply(NFSX_PREOPATTR(v3) +
1453 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1454 NFSX_WRITEVERF(v3));
1456 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1457 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1458 *tl++ = txdr_unsigned(nfsd->nd_len);
1459 *tl++ = txdr_unsigned(swp->nd_stable);
1461 * Actually, there is no need to txdr these fields,
1462 * but it may make the values more human readable,
1463 * for debugging purposes.
1465 if (nfsver.tv_sec == 0)
1467 *tl++ = txdr_unsigned(nfsver.tv_sec);
1468 *tl = txdr_unsigned(nfsver.tv_usec);
1470 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1471 nfsm_srvfillattr(&va, fp);
1474 nfsd->nd_mreq = mreq;
1476 panic("nfsrv_write: nd_mrep not free");
1479 * Done. Put it at the head of the timer queue so that
1480 * the final phase can return the reply.
1485 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1487 nfsd = LIST_FIRST(&swp->nd_coalesce);
1489 LIST_REMOVE(nfsd, nd_tq);
1495 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1502 * Search for a reply to return.
1505 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq)
1506 if (nfsd->nd_mreq) {
1507 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1508 LIST_REMOVE(nfsd, nd_tq);
1509 *mrq = nfsd->nd_mreq;
1518 * Coalesce the write request nfsd into owp. To do this we must:
1519 * - remove nfsd from the queues
1520 * - merge nfsd->nd_mrep into owp->nd_mrep
1521 * - update the nd_eoff and nd_stable for owp
1522 * - put nfsd on owp's nd_coalesce list
1523 * NB: Must be called at splsoftclock().
1526 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1530 struct nfsrv_descript *p;
1532 NFS_DPF(WG, ("C%03x-%03x",
1533 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1534 LIST_REMOVE(nfsd, nd_hash);
1535 LIST_REMOVE(nfsd, nd_tq);
1536 if (owp->nd_eoff < nfsd->nd_eoff) {
1537 overlap = owp->nd_eoff - nfsd->nd_off;
1539 panic("nfsrv_coalesce: bad off");
1541 m_adj(nfsd->nd_mrep, overlap);
1545 mp->m_next = nfsd->nd_mrep;
1546 owp->nd_eoff = nfsd->nd_eoff;
1548 m_freem(nfsd->nd_mrep);
1549 nfsd->nd_mrep = NULL;
1550 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1551 owp->nd_stable = NFSV3WRITE_FILESYNC;
1552 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1553 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1554 owp->nd_stable = NFSV3WRITE_DATASYNC;
1555 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1558 * If nfsd had anything else coalesced into it, transfer them
1559 * to owp, otherwise their replies will never get sent.
1561 for (p = LIST_FIRST(&nfsd->nd_coalesce); p;
1562 p = LIST_FIRST(&nfsd->nd_coalesce)) {
1563 LIST_REMOVE(p, nd_tq);
1564 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1569 * nfs create service
1570 * now does a truncate to 0 length via. setattr if it already exists
1573 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1574 struct thread *td, struct mbuf **mrq)
1576 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1577 struct sockaddr *nam = nfsd->nd_nam;
1578 caddr_t dpos = nfsd->nd_dpos;
1579 struct ucred *cred = &nfsd->nd_cr;
1580 struct nfs_fattr *fp;
1581 struct vattr va, dirfor, diraft;
1582 struct vattr *vap = &va;
1583 struct nfsv2_sattr *sp;
1585 struct nameidata nd;
1587 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1588 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1590 struct mbuf *mb, *mreq;
1591 struct vnode *dirp = (struct vnode *)0;
1595 u_char cverf[NFSX_V3CREATEVERF];
1596 struct mount *mp = NULL;
1599 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1605 fhp = &nfh.fh_generic;
1607 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1611 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1615 (void) vn_start_write(vp, &mp, V_WAIT);
1617 nfsm_srvnamesiz(len);
1619 nd.ni_cnd.cn_cred = cred;
1620 nd.ni_cnd.cn_nameiop = CREATE;
1621 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1624 * Call namei and do initial cleanup to get a few things
1625 * out of the way. If we get an initial error we cleanup
1626 * and return here to avoid special-casing the invalid nd
1627 * structure through the rest of the case. dirp may be
1628 * set even if an error occurs, but the nd structure will not
1629 * be valid at all if an error occurs so we have to invalidate it
1630 * prior to calling nfsm_reply ( which might goto nfsmout ).
1632 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1636 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1644 nfsm_reply(NFSX_WCCDATA(v3));
1646 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1652 * No error. Continue. State:
1654 * startdir is valid ( we release this immediately )
1656 * nd.ni_vp may be valid
1657 * nd.ni_dvp is valid
1659 * The error state is set through the code and we may also do some
1660 * opportunistic releasing of vnodes to avoid holding locks through
1661 * NFS I/O. The cleanup at the end is a catch-all
1666 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1667 how = fxdr_unsigned(int, *tl);
1669 case NFSV3CREATE_GUARDED:
1675 case NFSV3CREATE_UNCHECKED:
1678 case NFSV3CREATE_EXCLUSIVE:
1679 cp = nfsm_dissect(caddr_t, NFSX_V3CREATEVERF);
1680 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1682 if (nd.ni_vp == NULL)
1686 vap->va_type = VREG;
1688 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
1689 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1690 if (vap->va_type == VNON)
1691 vap->va_type = VREG;
1692 vap->va_mode = nfstov_mode(sp->sa_mode);
1693 switch (vap->va_type) {
1695 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1697 vap->va_size = (u_quad_t)tsize;
1702 rdev = fxdr_unsigned(long, sp->sa_size);
1710 * Iff doesn't exist, create it
1711 * otherwise just truncate to 0 length
1712 * should I set the mode too ?
1714 * The only possible error we can have at this point is EEXIST.
1715 * nd.ni_vp will also be non-NULL in that case.
1717 if (nd.ni_vp == NULL) {
1718 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1719 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1721 NDFREE(&nd, NDF_ONLY_PNBUF);
1723 nfsrv_object_create(nd.ni_vp);
1724 if (exclusive_flag) {
1727 bcopy(cverf, (caddr_t)&vap->va_atime,
1729 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1734 vap->va_type == VCHR ||
1735 vap->va_type == VBLK ||
1736 vap->va_type == VFIFO
1739 * Handle SysV FIFO node special cases. All other
1740 * devices require super user to access.
1742 if (vap->va_type == VCHR && rdev == 0xffffffff)
1743 vap->va_type = VFIFO;
1744 if (vap->va_type != VFIFO &&
1745 (error = suser_xxx(cred, 0, 0))) {
1748 vap->va_rdev = rdev;
1749 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1751 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 * release dvp prior to lookup
1766 * Even though LOCKPARENT was cleared, ni_dvp may
1769 nd.ni_cnd.cn_nameiop = LOOKUP;
1770 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1771 nd.ni_cnd.cn_thread = td;
1772 nd.ni_cnd.cn_cred = cred;
1774 error = lookup(&nd);
1779 /* fall through on certain errors */
1781 nfsrv_object_create(nd.ni_vp);
1782 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1790 if (vap->va_size != -1) {
1791 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1792 (nd.ni_cnd.cn_flags & RDONLY), td, 0);
1794 tempsize = vap->va_size;
1796 vap->va_size = tempsize;
1797 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1804 bzero((caddr_t)fhp, sizeof(nfh));
1805 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1806 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1808 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
1811 if (exclusive_flag && !error &&
1812 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1814 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1819 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1822 nfsm_srvpostop_fh(fhp);
1823 nfsm_srvpostop_attr(0, vap);
1825 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1828 /* v2 non-error case (see nfsm_reply). */
1829 nfsm_srvfhtom(fhp, v3);
1830 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1831 nfsm_srvfillattr(vap, fp);
1838 if (nd.ni_startdir) {
1839 vrele(nd.ni_startdir);
1840 nd.ni_startdir = NULL;
1844 NDFREE(&nd, NDF_ONLY_PNBUF);
1846 if (nd.ni_dvp == nd.ni_vp)
1853 vn_finished_write(mp);
1858 * nfs v3 mknod service
1861 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1862 struct thread *td, struct mbuf **mrq)
1864 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1865 struct sockaddr *nam = nfsd->nd_nam;
1866 caddr_t dpos = nfsd->nd_dpos;
1867 struct ucred *cred = &nfsd->nd_cr;
1868 struct vattr va, dirfor, diraft;
1869 struct vattr *vap = &va;
1871 struct nameidata nd;
1873 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1874 u_int32_t major, minor;
1876 struct mbuf *mb, *mreq;
1877 struct vnode *vp, *dirp = (struct vnode *)0;
1880 struct mount *mp = NULL;
1881 int v3 = (nfsd->nd_flag & ND_NFSV3);
1883 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1885 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1888 fhp = &nfh.fh_generic;
1890 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1894 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1898 (void) vn_start_write(vp, &mp, V_WAIT);
1901 nfsm_srvnamesiz(len);
1903 nd.ni_cnd.cn_cred = cred;
1904 nd.ni_cnd.cn_nameiop = CREATE;
1905 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1908 * Handle nfs_namei() call. If an error occurs, the nd structure
1909 * is not valid. However, nfsm_*() routines may still jump to
1913 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1916 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
1918 nfsm_reply(NFSX_WCCDATA(1));
1919 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1923 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1924 vtyp = nfsv3tov_type(*tl);
1925 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1926 error = NFSERR_BADTYPE;
1931 if (vtyp == VCHR || vtyp == VBLK) {
1932 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1933 major = fxdr_unsigned(u_int32_t, *tl++);
1934 minor = fxdr_unsigned(u_int32_t, *tl);
1935 vap->va_rdev = makeudev(major, minor);
1939 * Iff doesn't exist, create it.
1945 vap->va_type = vtyp;
1946 if (vtyp == VSOCK) {
1947 vrele(nd.ni_startdir);
1948 nd.ni_startdir = NULL;
1949 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1951 NDFREE(&nd, NDF_ONLY_PNBUF);
1953 if (vtyp != VFIFO && (error = suser_xxx(cred, 0, 0)))
1955 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1957 NDFREE(&nd, NDF_ONLY_PNBUF);
1964 * Release dvp prior to lookup
1969 nd.ni_cnd.cn_nameiop = LOOKUP;
1970 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1971 nd.ni_cnd.cn_thread = td;
1972 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1974 error = lookup(&nd);
1979 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1984 * send response, cleanup, return.
1987 if (nd.ni_startdir) {
1988 vrele(nd.ni_startdir);
1989 nd.ni_startdir = NULL;
1991 NDFREE(&nd, NDF_ONLY_PNBUF);
1993 if (nd.ni_dvp == nd.ni_vp)
2001 bzero((caddr_t)fhp, sizeof(nfh));
2002 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2003 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2005 error = VOP_GETATTR(vp, vap, cred, td);
2010 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2016 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2019 nfsm_srvpostop_fh(fhp);
2020 nfsm_srvpostop_attr(0, vap);
2022 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2024 vn_finished_write(mp);
2030 vrele(nd.ni_startdir);
2031 NDFREE(&nd, NDF_ONLY_PNBUF);
2033 if (nd.ni_dvp == nd.ni_vp)
2040 vn_finished_write(mp);
2045 * nfs remove service
2048 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2049 struct thread *td, struct mbuf **mrq)
2051 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2052 struct sockaddr *nam = nfsd->nd_nam;
2053 caddr_t dpos = nfsd->nd_dpos;
2054 struct ucred *cred = &nfsd->nd_cr;
2055 struct nameidata nd;
2057 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2058 int v3 = (nfsd->nd_flag & ND_NFSV3);
2059 struct mbuf *mb, *mreq;
2061 struct vattr dirfor, diraft;
2064 struct mount *mp = NULL;
2067 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2070 fhp = &nfh.fh_generic;
2072 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2076 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2080 (void) vn_start_write(vp, &mp, V_WAIT);
2083 nfsm_srvnamesiz(len);
2085 nd.ni_cnd.cn_cred = cred;
2086 nd.ni_cnd.cn_nameiop = DELETE;
2087 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2088 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2092 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2100 if (nd.ni_vp->v_type == VDIR) {
2101 error = EPERM; /* POSIX */
2105 * The root of a mounted filesystem cannot be deleted.
2107 if (nd.ni_vp->v_flag & VROOT) {
2113 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2114 NDFREE(&nd, NDF_ONLY_PNBUF);
2118 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2123 nfsm_reply(NFSX_WCCDATA(v3));
2125 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2129 NDFREE(&nd, NDF_ONLY_PNBUF);
2131 if (nd.ni_dvp == nd.ni_vp)
2138 vn_finished_write(mp);
2143 * nfs rename service
2146 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2147 struct thread *td, struct mbuf **mrq)
2149 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2150 struct sockaddr *nam = nfsd->nd_nam;
2151 caddr_t dpos = nfsd->nd_dpos;
2152 struct ucred *cred = &nfsd->nd_cr;
2154 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2155 int tdirfor_ret = 1, tdiraft_ret = 1;
2156 int v3 = (nfsd->nd_flag & ND_NFSV3);
2157 struct mbuf *mb, *mreq;
2158 struct nameidata fromnd, tond;
2159 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2160 struct vnode *tdirp = (struct vnode *)0;
2161 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2163 fhandle_t *ffhp, *tfhp;
2165 struct mount *mp = NULL;
2168 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2170 fvp = (struct vnode *)0;
2172 ffhp = &fnfh.fh_generic;
2173 tfhp = &tnfh.fh_generic;
2176 * Clear fields incase goto nfsmout occurs from macro.
2182 nfsm_srvmtofh(ffhp);
2183 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
2187 if ((error = VFS_FHTOVP(mp, &ffhp->fh_fid, &vp)) != 0) {
2191 (void) vn_start_write(vp, &mp, V_WAIT);
2194 nfsm_srvnamesiz(len);
2196 * Remember our original uid so that we can reset cr_uid before
2197 * the second nfs_namei() call, in case it is remapped.
2199 saved_uid = cred->cr_uid;
2200 fromnd.ni_cnd.cn_cred = cred;
2201 fromnd.ni_cnd.cn_nameiop = DELETE;
2202 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2203 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2204 &dpos, &fdirp, td, FALSE);
2207 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2215 nfsm_reply(2 * NFSX_WCCDATA(v3));
2217 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2218 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2224 nfsm_srvmtofh(tfhp);
2225 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2226 cred->cr_uid = saved_uid;
2227 tond.ni_cnd.cn_cred = cred;
2228 tond.ni_cnd.cn_nameiop = RENAME;
2229 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2230 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2231 &dpos, &tdirp, td, FALSE);
2234 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
2247 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2253 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2260 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2268 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2275 if (fvp->v_mount != tdvp->v_mount) {
2289 * If source is the same as the destination (that is the
2290 * same vnode with the same name in the same directory),
2291 * then there is nothing to do.
2293 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2294 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2295 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2296 fromnd.ni_cnd.cn_namelen))
2301 * The VOP_RENAME function releases all vnode references &
2302 * locks prior to returning so we need to clear the pointers
2303 * to bypass cleanup code later on.
2305 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2306 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2307 fromnd.ni_dvp = NULL;
2308 fromnd.ni_vp = NULL;
2312 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2313 tond.ni_cnd.cn_flags &= ~HASBUF;
2323 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, td);
2325 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, td);
2326 nfsm_reply(2 * NFSX_WCCDATA(v3));
2328 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2329 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2336 * Clear out tond related fields
2340 if (tond.ni_startdir)
2341 vrele(tond.ni_startdir);
2342 NDFREE(&tond, NDF_ONLY_PNBUF);
2344 if (tond.ni_dvp == tond.ni_vp)
2353 * Clear out fromnd related fields
2357 if (fromnd.ni_startdir)
2358 vrele(fromnd.ni_startdir);
2359 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2361 vrele(fromnd.ni_dvp);
2363 vrele(fromnd.ni_vp);
2365 vn_finished_write(mp);
2373 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2374 struct thread *td, struct mbuf **mrq)
2376 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2377 struct sockaddr *nam = nfsd->nd_nam;
2378 caddr_t dpos = nfsd->nd_dpos;
2379 struct ucred *cred = &nfsd->nd_cr;
2380 struct nameidata nd;
2382 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2383 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2384 struct mbuf *mb, *mreq;
2385 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2386 struct vattr dirfor, diraft, at;
2388 fhandle_t *fhp, *dfhp;
2389 struct mount *mp = NULL;
2391 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2394 fhp = &nfh.fh_generic;
2395 dfhp = &dnfh.fh_generic;
2397 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2401 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2405 (void) vn_start_write(vp, &mp, V_WAIT);
2408 nfsm_srvmtofh(dfhp);
2409 nfsm_srvnamesiz(len);
2411 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam, &rdonly, TRUE);
2413 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2415 nfsm_srvpostop_attr(getret, &at);
2416 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2422 if (vp->v_type == VDIR) {
2423 error = EPERM; /* POSIX */
2426 nd.ni_cnd.cn_cred = cred;
2427 nd.ni_cnd.cn_nameiop = CREATE;
2428 nd.ni_cnd.cn_flags = LOCKPARENT;
2429 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2433 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2449 if (vp->v_mount != xp->v_mount)
2453 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2454 NDFREE(&nd, NDF_ONLY_PNBUF);
2460 getret = VOP_GETATTR(vp, &at, cred, td);
2462 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2464 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2466 nfsm_srvpostop_attr(getret, &at);
2467 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2473 NDFREE(&nd, NDF_ONLY_PNBUF);
2479 if (nd.ni_dvp == nd.ni_vp)
2486 vn_finished_write(mp);
2491 * nfs symbolic link service
2494 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2495 struct thread *td, struct mbuf **mrq)
2497 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2498 struct sockaddr *nam = nfsd->nd_nam;
2499 caddr_t dpos = nfsd->nd_dpos;
2500 struct ucred *cred = &nfsd->nd_cr;
2501 struct vattr va, dirfor, diraft;
2502 struct nameidata nd;
2503 struct vattr *vap = &va;
2504 struct nfsv2_sattr *sp;
2505 char *bpos, *pathcp = (char *)0;
2508 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2509 int v3 = (nfsd->nd_flag & ND_NFSV3);
2510 struct mbuf *mb, *mreq;
2511 struct vnode *dirp = (struct vnode *)0;
2514 struct mount *mp = NULL;
2517 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2520 fhp = &nfh.fh_generic;
2522 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2526 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2530 (void) vn_start_write(vp, &mp, V_WAIT);
2533 nfsm_srvnamesiz(len);
2534 nd.ni_cnd.cn_cred = cred;
2535 nd.ni_cnd.cn_nameiop = CREATE;
2536 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2537 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2541 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2554 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2555 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2556 iv.iov_base = pathcp;
2558 io.uio_resid = len2;
2562 io.uio_segflg = UIO_SYSSPACE;
2563 io.uio_rw = UIO_READ;
2564 io.uio_td = (struct thread *)0;
2565 nfsm_mtouio(&io, len2);
2567 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
2568 vap->va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2570 *(pathcp + len2) = '\0';
2577 * issue symlink op. SAVESTART is set so the underlying path component
2578 * is only freed by the VOP if an error occurs.
2580 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2582 NDFREE(&nd, NDF_ONLY_PNBUF);
2587 * releases directory prior to potential lookup op.
2595 * Issue lookup. Leave SAVESTART set so we can easily free
2596 * the name buffer later on.
2598 * since LOCKPARENT is not set, ni_dvp will be garbage on
2599 * return whether an error occurs or not.
2601 nd.ni_cnd.cn_nameiop = LOOKUP;
2602 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2603 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2604 nd.ni_cnd.cn_thread = td;
2605 nd.ni_cnd.cn_cred = cred;
2607 error = lookup(&nd);
2611 bzero((caddr_t)fhp, sizeof(nfh));
2612 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2613 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2615 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2624 * These releases aren't strictly required, does even doing them
2625 * make any sense? XXX can nfsm_reply() block?
2628 FREE(pathcp, M_TEMP);
2632 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2636 if (nd.ni_startdir) {
2637 vrele(nd.ni_startdir);
2638 nd.ni_startdir = NULL;
2640 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2643 nfsm_srvpostop_fh(fhp);
2644 nfsm_srvpostop_attr(0, vap);
2646 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2652 NDFREE(&nd, NDF_ONLY_PNBUF);
2654 if (nd.ni_dvp == nd.ni_vp)
2662 vrele(nd.ni_startdir);
2666 FREE(pathcp, M_TEMP);
2668 vn_finished_write(mp);
2676 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2677 struct thread *td, struct mbuf **mrq)
2679 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2680 struct sockaddr *nam = nfsd->nd_nam;
2681 caddr_t dpos = nfsd->nd_dpos;
2682 struct ucred *cred = &nfsd->nd_cr;
2683 struct vattr va, dirfor, diraft;
2684 struct vattr *vap = &va;
2685 struct nfs_fattr *fp;
2686 struct nameidata nd;
2689 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2690 int v3 = (nfsd->nd_flag & ND_NFSV3);
2691 struct mbuf *mb, *mreq;
2692 struct vnode *dirp = NULL;
2696 struct mount *mp = NULL;
2699 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2702 fhp = &nfh.fh_generic;
2704 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2708 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2712 (void) vn_start_write(vp, &mp, V_WAIT);
2715 nfsm_srvnamesiz(len);
2716 nd.ni_cnd.cn_cred = cred;
2717 nd.ni_cnd.cn_nameiop = CREATE;
2718 nd.ni_cnd.cn_flags = LOCKPARENT;
2720 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2724 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2732 nfsm_reply(NFSX_WCCDATA(v3));
2734 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2742 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2743 vap->va_mode = nfstov_mode(*tl++);
2747 * At this point nd.ni_dvp is referenced and exclusively locked and
2748 * nd.ni_vp, if it exists, is referenced but not locked.
2751 vap->va_type = VDIR;
2752 if (nd.ni_vp != NULL) {
2753 NDFREE(&nd, NDF_ONLY_PNBUF);
2759 * Issue mkdir op. Since SAVESTART is not set, the pathname
2760 * component is freed by the VOP call. This will fill-in
2761 * nd.ni_vp, reference, and exclusively lock it.
2763 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2764 NDFREE(&nd, NDF_ONLY_PNBUF);
2771 bzero((caddr_t)fhp, sizeof(nfh));
2772 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2773 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2775 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2779 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2780 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2783 nfsm_srvpostop_fh(fhp);
2784 nfsm_srvpostop_attr(0, vap);
2786 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2788 /* v2, non-error case (see nfsm_reply) */
2789 nfsm_srvfhtom(fhp, v3);
2790 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2791 nfsm_srvfillattr(vap, fp);
2800 NDFREE(&nd, NDF_ONLY_PNBUF);
2801 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2812 vn_finished_write(mp);
2820 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2821 struct thread *td, struct mbuf **mrq)
2823 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2824 struct sockaddr *nam = nfsd->nd_nam;
2825 caddr_t dpos = nfsd->nd_dpos;
2826 struct ucred *cred = &nfsd->nd_cr;
2828 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2829 int v3 = (nfsd->nd_flag & ND_NFSV3);
2830 struct mbuf *mb, *mreq;
2831 struct vnode *vp, *dirp = (struct vnode *)0;
2832 struct vattr dirfor, diraft;
2835 struct nameidata nd;
2836 struct mount *mp = NULL;
2838 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2841 fhp = &nfh.fh_generic;
2843 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2847 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2851 (void) vn_start_write(vp, &mp, V_WAIT);
2854 nfsm_srvnamesiz(len);
2855 nd.ni_cnd.cn_cred = cred;
2856 nd.ni_cnd.cn_nameiop = DELETE;
2857 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2858 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2862 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2870 nfsm_reply(NFSX_WCCDATA(v3));
2872 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2877 if (vp->v_type != VDIR) {
2882 * No rmdir "." please.
2884 if (nd.ni_dvp == vp) {
2889 * The root of a mounted filesystem cannot be deleted.
2891 if (vp->v_flag & VROOT)
2895 * Issue or abort op. Since SAVESTART is not set, path name
2896 * component is freed by the VOP after either.
2899 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2900 NDFREE(&nd, NDF_ONLY_PNBUF);
2903 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2904 nfsm_reply(NFSX_WCCDATA(v3));
2906 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2912 NDFREE(&nd, NDF_ONLY_PNBUF);
2916 if (nd.ni_dvp == nd.ni_vp)
2924 vn_finished_write(mp);
2929 * nfs readdir service
2930 * - mallocs what it thinks is enough to read
2931 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2932 * - calls VOP_READDIR()
2933 * - loops around building the reply
2934 * if the output generated exceeds count break out of loop
2935 * The nfsm_clget macro is used here so that the reply will be packed
2936 * tightly in mbuf clusters.
2937 * - it only knows that it has encountered eof when the VOP_READDIR()
2939 * - as such one readdir rpc will return eof false although you are there
2940 * and then the next will return eof
2941 * - it trims out records with d_fileno == 0
2942 * this doesn't matter for Unix clients, but they might confuse clients
2944 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2945 * than requested, but this may not apply to all filesystems. For
2946 * example, client NFS does not { although it is never remote mounted
2948 * The alternate call nfsrv_readdirplus() does lookups as well.
2949 * PS: The NFS protocol spec. does not clarify what the "count" byte
2950 * argument is a count of.. just name strings and file id's or the
2951 * entire reply rpc or ...
2952 * I tried just file name and id sizes and it confused the Sun client,
2953 * so I am using the full rpc size now. The "paranoia.." comment refers
2954 * to including the status longwords that are not a part of the dir.
2955 * "entry" structures, but are in the rpc.
2959 u_int32_t fl_postopok;
2960 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2962 u_int32_t fl_fhsize;
2963 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2967 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2968 struct thread *td, struct mbuf **mrq)
2970 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2971 struct sockaddr *nam = nfsd->nd_nam;
2972 caddr_t dpos = nfsd->nd_dpos;
2973 struct ucred *cred = &nfsd->nd_cr;
2980 struct mbuf *mb, *mreq;
2981 char *cpos, *cend, *rbuf;
2982 struct vnode *vp = NULL;
2988 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2989 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2990 int v3 = (nfsd->nd_flag & ND_NFSV3);
2991 u_quad_t off, toff, verf;
2992 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2994 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2995 fhp = &nfh.fh_generic;
2998 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
2999 toff = fxdr_hyper(tl);
3001 verf = fxdr_hyper(tl);
3004 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
3005 toff = fxdr_unsigned(u_quad_t, *tl++);
3006 verf = 0; /* shut up gcc */
3009 cnt = fxdr_unsigned(int, *tl);
3010 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3011 xfer = NFS_SRVMAXDATA(nfsd);
3015 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3016 if (!error && vp->v_type != VDIR) {
3022 nfsm_reply(NFSX_UNSIGNED);
3024 nfsm_srvpostop_attr(getret, &at);
3030 * Obtain lock on vnode for this section of the code
3033 error = getret = VOP_GETATTR(vp, &at, cred, td);
3036 * XXX This check may be too strict for Solaris 2.5 clients.
3038 if (!error && toff && verf && verf != at.va_filerev)
3039 error = NFSERR_BAD_COOKIE;
3043 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3047 nfsm_reply(NFSX_POSTOPATTR(v3));
3049 nfsm_srvpostop_attr(getret, &at);
3053 VOP_UNLOCK(vp, 0, td);
3056 * end section. Allocate rbuf and continue
3058 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3061 iv.iov_len = fullsiz;
3064 io.uio_offset = (off_t)off;
3065 io.uio_resid = fullsiz;
3066 io.uio_segflg = UIO_SYSSPACE;
3067 io.uio_rw = UIO_READ;
3068 io.uio_td = (struct thread *)0;
3070 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3072 free((caddr_t)cookies, M_TEMP);
3075 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3076 off = (off_t)io.uio_offset;
3077 if (!cookies && !error)
3078 error = NFSERR_PERM;
3080 getret = VOP_GETATTR(vp, &at, cred, td);
3084 VOP_UNLOCK(vp, 0, td);
3088 free((caddr_t)rbuf, M_TEMP);
3090 free((caddr_t)cookies, M_TEMP);
3091 nfsm_reply(NFSX_POSTOPATTR(v3));
3093 nfsm_srvpostop_attr(getret, &at);
3098 siz -= io.uio_resid;
3101 * If nothing read, return eof
3107 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3110 nfsm_srvpostop_attr(getret, &at);
3111 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3112 txdr_hyper(at.va_filerev, tl);
3115 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3116 *tl++ = nfsrv_nfs_false;
3117 *tl = nfsrv_nfs_true;
3118 FREE((caddr_t)rbuf, M_TEMP);
3119 FREE((caddr_t)cookies, M_TEMP);
3126 * Check for degenerate cases of nothing useful read.
3127 * If so go try again
3131 dp = (struct dirent *)cpos;
3134 * For some reason FreeBSD's ufs_readdir() chooses to back the
3135 * directory offset up to a block boundary, so it is necessary to
3136 * skip over the records that precede the requested offset. This
3137 * requires the assumption that file offset cookies monotonically
3140 while (cpos < cend && ncookies > 0 &&
3141 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3142 ((u_quad_t)(*cookiep)) <= toff)) {
3143 cpos += dp->d_reclen;
3144 dp = (struct dirent *)cpos;
3148 if (cpos >= cend || ncookies == 0) {
3154 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3155 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3157 nfsm_srvpostop_attr(getret, &at);
3158 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3159 txdr_hyper(at.va_filerev, tl);
3163 be = bp + M_TRAILINGSPACE(mp);
3165 /* Loop through the records and build reply */
3166 while (cpos < cend && ncookies > 0) {
3167 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3168 nlen = dp->d_namlen;
3169 rem = nfsm_rndup(nlen) - nlen;
3170 len += (4 * NFSX_UNSIGNED + nlen + rem);
3172 len += 2 * NFSX_UNSIGNED;
3178 * Build the directory record xdr from
3182 *tl = nfsrv_nfs_true;
3183 bp += NFSX_UNSIGNED;
3187 bp += NFSX_UNSIGNED;
3190 *tl = txdr_unsigned(dp->d_fileno);
3191 bp += NFSX_UNSIGNED;
3193 *tl = txdr_unsigned(nlen);
3194 bp += NFSX_UNSIGNED;
3196 /* And loop around copying the name */
3205 bcopy(cp, bp, tsiz);
3211 /* And null pad to a int32_t boundary */
3212 for (i = 0; i < rem; i++)
3216 /* Finish off the record */
3219 bp += NFSX_UNSIGNED;
3222 *tl = txdr_unsigned(*cookiep);
3223 bp += NFSX_UNSIGNED;
3225 cpos += dp->d_reclen;
3226 dp = (struct dirent *)cpos;
3233 *tl = nfsrv_nfs_false;
3234 bp += NFSX_UNSIGNED;
3237 *tl = nfsrv_nfs_true;
3239 *tl = nfsrv_nfs_false;
3240 bp += NFSX_UNSIGNED;
3243 mp->m_len = bp - mtod(mp, caddr_t);
3245 mp->m_len += bp - bpos;
3246 FREE((caddr_t)rbuf, M_TEMP);
3247 FREE((caddr_t)cookies, M_TEMP);
3256 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3257 struct thread *td, struct mbuf **mrq)
3259 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3260 struct sockaddr *nam = nfsd->nd_nam;
3261 caddr_t dpos = nfsd->nd_dpos;
3262 struct ucred *cred = &nfsd->nd_cr;
3269 struct mbuf *mb, *mreq;
3270 char *cpos, *cend, *rbuf;
3271 struct vnode *vp = NULL, *nvp;
3274 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3277 struct vattr va, at, *vap = &va;
3278 struct nfs_fattr *fp;
3279 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3280 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3281 u_quad_t off, toff, verf;
3282 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3283 int v3 = (nfsd->nd_flag & ND_NFSV3);
3285 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3287 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3288 fhp = &nfh.fh_generic;
3290 tl = nfsm_dissect(u_int32_t *, 6 * NFSX_UNSIGNED);
3291 toff = fxdr_hyper(tl);
3293 verf = fxdr_hyper(tl);
3295 siz = fxdr_unsigned(int, *tl++);
3296 cnt = fxdr_unsigned(int, *tl);
3298 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3299 xfer = NFS_SRVMAXDATA(nfsd);
3303 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3304 if (!error && vp->v_type != VDIR) {
3310 nfsm_reply(NFSX_UNSIGNED);
3311 nfsm_srvpostop_attr(getret, &at);
3315 error = getret = VOP_GETATTR(vp, &at, cred, td);
3318 * XXX This check may be too strict for Solaris 2.5 clients.
3320 if (!error && toff && verf && verf != at.va_filerev)
3321 error = NFSERR_BAD_COOKIE;
3324 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3328 nfsm_reply(NFSX_V3POSTOPATTR);
3329 nfsm_srvpostop_attr(getret, &at);
3333 VOP_UNLOCK(vp, 0, td);
3334 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3337 iv.iov_len = fullsiz;
3340 io.uio_offset = (off_t)off;
3341 io.uio_resid = fullsiz;
3342 io.uio_segflg = UIO_SYSSPACE;
3343 io.uio_rw = UIO_READ;
3344 io.uio_td = (struct thread *)0;
3346 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3348 free((caddr_t)cookies, M_TEMP);
3351 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3352 off = (u_quad_t)io.uio_offset;
3353 getret = VOP_GETATTR(vp, &at, cred, td);
3354 VOP_UNLOCK(vp, 0, td);
3355 if (!cookies && !error)
3356 error = NFSERR_PERM;
3363 free((caddr_t)cookies, M_TEMP);
3364 free((caddr_t)rbuf, M_TEMP);
3365 nfsm_reply(NFSX_V3POSTOPATTR);
3366 nfsm_srvpostop_attr(getret, &at);
3371 siz -= io.uio_resid;
3374 * If nothing read, return eof
3380 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3382 nfsm_srvpostop_attr(getret, &at);
3383 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3384 txdr_hyper(at.va_filerev, tl);
3386 *tl++ = nfsrv_nfs_false;
3387 *tl = nfsrv_nfs_true;
3388 FREE((caddr_t)cookies, M_TEMP);
3389 FREE((caddr_t)rbuf, M_TEMP);
3396 * Check for degenerate cases of nothing useful read.
3397 * If so go try again
3401 dp = (struct dirent *)cpos;
3404 * For some reason FreeBSD's ufs_readdir() chooses to back the
3405 * directory offset up to a block boundary, so it is necessary to
3406 * skip over the records that precede the requested offset. This
3407 * requires the assumption that file offset cookies monotonically
3410 while (cpos < cend && ncookies > 0 &&
3411 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3412 ((u_quad_t)(*cookiep)) <= toff)) {
3413 cpos += dp->d_reclen;
3414 dp = (struct dirent *)cpos;
3418 if (cpos >= cend || ncookies == 0) {
3425 * Probe one of the directory entries to see if the filesystem
3428 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3429 error = NFSERR_NOTSUPP;
3432 free((caddr_t)cookies, M_TEMP);
3433 free((caddr_t)rbuf, M_TEMP);
3434 nfsm_reply(NFSX_V3POSTOPATTR);
3435 nfsm_srvpostop_attr(getret, &at);
3442 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3445 nfsm_srvpostop_attr(getret, &at);
3446 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3447 txdr_hyper(at.va_filerev, tl);
3450 be = bp + M_TRAILINGSPACE(mp);
3452 /* Loop through the records and build reply */
3453 while (cpos < cend && ncookies > 0) {
3454 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3455 nlen = dp->d_namlen;
3456 rem = nfsm_rndup(nlen)-nlen;
3459 * For readdir_and_lookup get the vnode using
3462 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3464 bzero((caddr_t)nfhp, NFSX_V3FH);
3466 nvp->v_mount->mnt_stat.f_fsid;
3467 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3472 if (VOP_GETATTR(nvp, vap, cred, td)) {
3481 * If either the dircount or maxcount will be
3482 * exceeded, get out now. Both of these lengths
3483 * are calculated conservatively, including all
3486 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3488 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3489 if (len > cnt || dirlen > fullsiz) {
3495 * Build the directory record xdr from
3498 fp = (struct nfs_fattr *)&fl.fl_fattr;
3499 nfsm_srvfillattr(vap, fp);
3500 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3501 fl.fl_fhok = nfsrv_nfs_true;
3502 fl.fl_postopok = nfsrv_nfs_true;
3503 fl.fl_off.nfsuquad[0] = 0;
3504 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3507 *tl = nfsrv_nfs_true;
3508 bp += NFSX_UNSIGNED;
3511 bp += NFSX_UNSIGNED;
3513 *tl = txdr_unsigned(dp->d_fileno);
3514 bp += NFSX_UNSIGNED;
3516 *tl = txdr_unsigned(nlen);
3517 bp += NFSX_UNSIGNED;
3519 /* And loop around copying the name */
3524 if ((bp + xfer) > be)
3528 bcopy(cp, bp, tsiz);
3534 /* And null pad to a int32_t boundary */
3535 for (i = 0; i < rem; i++)
3539 * Now copy the flrep structure out.
3541 xfer = sizeof (struct flrep);
3545 if ((bp + xfer) > be)
3549 bcopy(cp, bp, tsiz);
3557 cpos += dp->d_reclen;
3558 dp = (struct dirent *)cpos;
3565 *tl = nfsrv_nfs_false;
3566 bp += NFSX_UNSIGNED;
3569 *tl = nfsrv_nfs_true;
3571 *tl = nfsrv_nfs_false;
3572 bp += NFSX_UNSIGNED;
3575 mp->m_len = bp - mtod(mp, caddr_t);
3577 mp->m_len += bp - bpos;
3578 FREE((caddr_t)cookies, M_TEMP);
3579 FREE((caddr_t)rbuf, M_TEMP);
3587 * nfs commit service
3590 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3591 struct thread *td, struct mbuf **mrq)
3593 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3594 struct sockaddr *nam = nfsd->nd_nam;
3595 caddr_t dpos = nfsd->nd_dpos;
3596 struct ucred *cred = &nfsd->nd_cr;
3597 struct vattr bfor, aft;
3598 struct vnode *vp = NULL;
3603 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3604 struct mbuf *mb, *mreq;
3606 struct mount *mp = NULL;
3607 int v3 = (nfsd->nd_flag & ND_NFSV3);
3609 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3611 panic("nfsrv_commit: v3 proc called on a v2 connection");
3612 fhp = &nfh.fh_generic;
3614 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3618 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
3622 (void) vn_start_write(vp, &mp, V_WAIT);
3625 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
3628 * XXX At this time VOP_FSYNC() does not accept offset and byte
3629 * count parameters, so these arguments are useless (someday maybe).
3631 off = fxdr_hyper(tl);
3633 cnt = fxdr_unsigned(int, *tl);
3634 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3636 nfsm_reply(2 * NFSX_UNSIGNED);
3637 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3641 for_ret = VOP_GETATTR(vp, &bfor, cred, td);
3643 if (cnt > MAX_COMMIT_COUNT) {
3645 * Give up and do the whole thing
3648 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3649 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3651 error = VOP_FSYNC(vp, cred, MNT_WAIT, td);
3654 * Locate and synchronously write any buffers that fall
3655 * into the requested range. Note: we are assuming that
3656 * f_iosize is a power of 2.
3658 int iosize = vp->v_mount->mnt_stat.f_iosize;
3659 int iomask = iosize - 1;
3664 * Align to iosize boundry, super-align to page boundry.
3667 cnt += off & iomask;
3668 off &= ~(u_quad_t)iomask;
3670 if (off & PAGE_MASK) {
3671 cnt += off & PAGE_MASK;
3672 off &= ~(u_quad_t)PAGE_MASK;
3674 lblkno = off / iosize;
3677 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3678 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3686 * If we have a buffer and it is marked B_DELWRI we
3687 * have to lock and write it. Otherwise the prior
3688 * write is assumed to have already been committed.
3690 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3691 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3692 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3693 continue; /* retry */
3696 bp->b_flags &= ~B_ASYNC;
3709 aft_ret = VOP_GETATTR(vp, &aft, cred, td);
3713 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3714 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3716 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3717 if (nfsver.tv_sec == 0)
3719 *tl++ = txdr_unsigned(nfsver.tv_sec);
3720 *tl = txdr_unsigned(nfsver.tv_usec);
3727 vn_finished_write(mp);
3732 * nfs statfs service
3735 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3736 struct thread *td, struct mbuf **mrq)
3738 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3739 struct sockaddr *nam = nfsd->nd_nam;
3740 caddr_t dpos = nfsd->nd_dpos;
3741 struct ucred *cred = &nfsd->nd_cr;
3743 struct nfs_statfs *sfp;
3745 int error = 0, rdonly, getret = 1;
3746 int v3 = (nfsd->nd_flag & ND_NFSV3);
3747 struct mbuf *mb, *mreq;
3748 struct vnode *vp = NULL;
3752 struct statfs statfs;
3755 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3756 fhp = &nfh.fh_generic;
3758 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3760 nfsm_reply(NFSX_UNSIGNED);
3762 nfsm_srvpostop_attr(getret, &at);
3767 error = VFS_STATFS(vp->v_mount, sf, td);
3768 getret = VOP_GETATTR(vp, &at, cred, td);
3771 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3773 nfsm_srvpostop_attr(getret, &at);
3778 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3780 tval = (u_quad_t)sf->f_blocks;
3781 tval *= (u_quad_t)sf->f_bsize;
3782 txdr_hyper(tval, &sfp->sf_tbytes);
3783 tval = (u_quad_t)sf->f_bfree;
3784 tval *= (u_quad_t)sf->f_bsize;
3785 txdr_hyper(tval, &sfp->sf_fbytes);
3786 tval = (u_quad_t)sf->f_bavail;
3787 tval *= (u_quad_t)sf->f_bsize;
3788 txdr_hyper(tval, &sfp->sf_abytes);
3789 sfp->sf_tfiles.nfsuquad[0] = 0;
3790 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3791 sfp->sf_ffiles.nfsuquad[0] = 0;
3792 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3793 sfp->sf_afiles.nfsuquad[0] = 0;
3794 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3795 sfp->sf_invarsec = 0;
3797 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3798 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3799 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3800 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3801 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3810 * nfs fsinfo service
3813 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3814 struct thread *td, struct mbuf **mrq)
3816 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3817 struct sockaddr *nam = nfsd->nd_nam;
3818 caddr_t dpos = nfsd->nd_dpos;
3819 struct ucred *cred = &nfsd->nd_cr;
3820 struct nfsv3_fsinfo *sip;
3822 int error = 0, rdonly, getret = 1, pref;
3823 struct mbuf *mb, *mreq;
3824 struct vnode *vp = NULL;
3830 int v3 = (nfsd->nd_flag & ND_NFSV3);
3832 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3834 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3835 fhp = &nfh.fh_generic;
3837 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3839 nfsm_reply(NFSX_UNSIGNED);
3840 nfsm_srvpostop_attr(getret, &at);
3845 /* XXX Try to make a guess on the max file size. */
3846 VFS_STATFS(vp->v_mount, &sb, td);
3847 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3849 getret = VOP_GETATTR(vp, &at, cred, td);
3852 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3853 nfsm_srvpostop_attr(getret, &at);
3854 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3858 * There should be file system VFS OP(s) to get this information.
3859 * For now, assume ufs.
3861 if (slp->ns_so->so_type == SOCK_DGRAM)
3862 pref = NFS_MAXDGRAMDATA;
3865 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3866 sip->fs_rtpref = txdr_unsigned(pref);
3867 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3868 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3869 sip->fs_wtpref = txdr_unsigned(pref);
3870 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3871 sip->fs_dtpref = txdr_unsigned(pref);
3872 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3873 sip->fs_timedelta.nfsv3_sec = 0;
3874 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3875 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3876 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3877 NFSV3FSINFO_CANSETTIME);
3885 * nfs pathconf service
3888 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3889 struct thread *td, struct mbuf **mrq)
3891 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3892 struct sockaddr *nam = nfsd->nd_nam;
3893 caddr_t dpos = nfsd->nd_dpos;
3894 struct ucred *cred = &nfsd->nd_cr;
3895 struct nfsv3_pathconf *pc;
3897 int error = 0, rdonly, getret = 1;
3898 register_t linkmax, namemax, chownres, notrunc;
3899 struct mbuf *mb, *mreq;
3900 struct vnode *vp = NULL;
3904 int v3 = (nfsd->nd_flag & ND_NFSV3);
3906 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3908 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3909 fhp = &nfh.fh_generic;
3911 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3913 nfsm_reply(NFSX_UNSIGNED);
3914 nfsm_srvpostop_attr(getret, &at);
3918 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3920 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3922 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3924 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3925 getret = VOP_GETATTR(vp, &at, cred, td);
3928 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3929 nfsm_srvpostop_attr(getret, &at);
3934 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3936 pc->pc_linkmax = txdr_unsigned(linkmax);
3937 pc->pc_namemax = txdr_unsigned(namemax);
3938 pc->pc_notrunc = txdr_unsigned(notrunc);
3939 pc->pc_chownrestricted = txdr_unsigned(chownres);
3942 * These should probably be supported by VOP_PATHCONF(), but
3943 * until msdosfs is exportable (why would you want to?), the
3944 * Unix defaults should be ok.
3946 pc->pc_caseinsensitive = nfsrv_nfs_false;
3947 pc->pc_casepreserving = nfsrv_nfs_true;
3955 * Null operation, used by clients to ping server
3959 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3960 struct thread *td, struct mbuf **mrq)
3962 struct mbuf *mrep = nfsd->nd_mrep;
3964 int error = NFSERR_RETVOID;
3965 struct mbuf *mb, *mreq;
3967 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3974 * No operation, used for obsolete procedures
3978 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3979 struct thread *td, struct mbuf **mrq)
3981 struct mbuf *mrep = nfsd->nd_mrep;
3984 struct mbuf *mb, *mreq;
3986 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3987 if (nfsd->nd_repstat)
3988 error = nfsd->nd_repstat;
3990 error = EPROCUNAVAIL;
3998 * Perform access checking for vnodes obtained from file handles that would
3999 * refer to files already opened by a Unix client. You cannot just use
4000 * vn_writechk() and VOP_ACCESS() for two reasons.
4001 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4002 * 2 - The owner is to be given access irrespective of mode bits for some
4003 * operations, so that processes that chmod after opening a file don't
4004 * break. I don't like this because it opens a security hole, but since
4005 * the nfs server opens a security hole the size of a barn door anyhow,
4008 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4009 * will return EPERM instead of EACCESS. EPERM is always an error.
4012 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
4013 struct thread *td, int override)
4018 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4019 if (flags & VWRITE) {
4020 /* Just vn_writechk() changed to check rdonly */
4022 * Disallow write attempts on read-only file systems;
4023 * unless the file is a socket or a block or character
4024 * device resident on the file system.
4026 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4027 switch (vp->v_type) {
4037 * If there's shared text associated with
4038 * the inode, we can't allow writing.
4040 if (vp->v_flag & VTEXT)
4043 error = VOP_GETATTR(vp, &vattr, cred, td);
4046 error = VOP_ACCESS(vp, flags, cred, td);
4048 * Allow certain operations for the owner (reads and writes
4049 * on files that are already open).
4051 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)