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 nfsm_reply(), the case where error == EBADRPC is treated
59 * specially; after constructing a reply, it does an immediate
60 * `goto nfsmout' to avoid getting any V3 post-op status appended.
63 * Warning: always pay careful attention to resource cleanup on return
64 * and note that nfsm_*() macros can terminate a procedure on certain
67 * lookup() and namei()
68 * may return garbage in various structural fields/return elements
69 * if an error is returned, and may garbage up nd.ni_dvp even if no
70 * error is returned and you did not request LOCKPARENT or WANTPARENT.
72 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
73 * buffer has been freed or not.
76 #include <sys/param.h>
77 #include <sys/systm.h>
79 #include <sys/namei.h>
80 #include <sys/unistd.h>
81 #include <sys/vnode.h>
82 #include <sys/mount.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/malloc.h>
87 #include <sys/dirent.h>
89 #include <sys/kernel.h>
90 #include <sys/sysctl.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_object.h>
98 #include <nfs/nfsproto.h>
99 #include <nfs/rpcv2.h>
100 #include <nfsserver/nfs.h>
101 #include <nfs/xdr_subs.h>
102 #include <nfsserver/nfsm_subs.h>
105 #define nfsdbprintf(info) printf info
107 #define nfsdbprintf(info)
110 #define MAX_COMMIT_COUNT (1024 * 1024)
112 #define NUM_HEURISTIC 64
113 #define NHUSE_INIT 64
115 #define NHUSE_MAX 2048
117 static struct nfsheur {
118 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
119 off_t nh_nextr; /* next offset for sequential detection */
120 int nh_use; /* use count for selection */
121 int nh_seqcount; /* heuristic */
122 } nfsheur[NUM_HEURISTIC];
126 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
127 int nfsrvw_procrastinate_v3 = 0;
129 static struct timeval nfsver = { 0 };
131 SYSCTL_NODE(_vfs, OID_AUTO, nfsrv, CTLFLAG_RW, 0, "NFS server");
133 static int nfs_async;
134 static int nfs_commit_blks;
135 static int nfs_commit_miss;
136 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
137 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
138 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
140 struct nfsrvstats nfsrvstats;
141 SYSCTL_STRUCT(_vfs_nfsrv, NFS_NFSRVSTATS, nfsrvstats, CTLFLAG_RD,
142 &nfsrvstats, nfsrvstats, "S,nfsrvstats");
144 static int nfsrv_access(struct vnode *, int, struct ucred *, int,
145 struct thread *, int);
146 static void nfsrvw_coalesce(struct nfsrv_descript *,
147 struct nfsrv_descript *);
150 * Clear nameidata fields that are tested in nsfmout cleanup code prior
151 * to using first nfsm macro (that might jump to the cleanup code).
155 ndclear(struct nameidata *nd)
158 nd->ni_cnd.cn_flags = 0;
161 nd->ni_startdir = NULL;
165 * nfs v3 access service
168 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
169 struct thread *td, struct mbuf **mrq)
171 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
172 struct sockaddr *nam = nfsd->nd_nam;
173 caddr_t dpos = nfsd->nd_dpos;
174 struct ucred *cred = &nfsd->nd_cr;
175 struct vnode *vp = NULL;
180 int error = 0, rdonly, getret;
181 struct mbuf *mb, *mreq;
182 struct vattr vattr, *vap = &vattr;
183 u_long testmode, nfsmode;
184 int v3 = (nfsd->nd_flag & ND_NFSV3);
186 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
188 panic("nfsrv3_access: v3 proc called on a v2 connection");
189 fhp = &nfh.fh_generic;
191 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
192 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
194 nfsm_reply(NFSX_UNSIGNED);
195 nfsm_srvpostop_attr(1, (struct vattr *)0);
199 nfsmode = fxdr_unsigned(u_int32_t, *tl);
200 if ((nfsmode & NFSV3ACCESS_READ) &&
201 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
202 nfsmode &= ~NFSV3ACCESS_READ;
203 if (vp->v_type == VDIR)
204 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
207 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
208 if ((nfsmode & testmode) &&
209 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
210 nfsmode &= ~testmode;
211 if (vp->v_type == VDIR)
212 testmode = NFSV3ACCESS_LOOKUP;
214 testmode = NFSV3ACCESS_EXECUTE;
215 if ((nfsmode & testmode) &&
216 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
217 nfsmode &= ~testmode;
218 getret = VOP_GETATTR(vp, vap, cred, td);
221 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
222 nfsm_srvpostop_attr(getret, vap);
223 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
224 *tl = txdr_unsigned(nfsmode);
232 * nfs getattr service
235 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
236 struct thread *td, struct mbuf **mrq)
238 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
239 struct sockaddr *nam = nfsd->nd_nam;
240 caddr_t dpos = nfsd->nd_dpos;
241 struct ucred *cred = &nfsd->nd_cr;
242 struct nfs_fattr *fp;
244 struct vattr *vap = &va;
245 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 != nfsrv_nfs_xdrneg1)
341 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
342 if (sp->sa_gid != nfsrv_nfs_xdrneg1)
343 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
344 if (sp->sa_size != nfsrv_nfs_xdrneg1)
345 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
346 if (sp->sa_atime.nfsv2_sec != nfsrv_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 != nfsrv_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);
421 /* v2 non-error case. */
422 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
423 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);
712 if (uiop->uio_resid > 0) {
713 len -= uiop->uio_resid;
714 tlen = nfsm_rndup(len);
715 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
717 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
718 *tl = txdr_unsigned(len);
733 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
734 struct thread *td, struct mbuf **mrq)
736 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
737 struct sockaddr *nam = nfsd->nd_nam;
738 caddr_t dpos = nfsd->nd_dpos;
739 struct ucred *cred = &nfsd->nd_cr;
743 struct nfs_fattr *fp;
747 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
748 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
749 struct mbuf *mb, *mreq;
751 struct vnode *vp = NULL;
754 struct uio io, *uiop = &io;
755 struct vattr va, *vap = &va;
760 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
761 fhp = &nfh.fh_generic;
764 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
765 off = fxdr_hyper(tl);
767 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
768 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
770 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
773 * Reference vp. If an error occurs, vp will be invalid, but we
774 * have to NULL it just in case. The macros might goto nfsmout
778 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
781 nfsm_reply(2 * NFSX_UNSIGNED);
783 nfsm_srvpostop_attr(1, (struct vattr *)0);
788 if (vp->v_type != VREG) {
792 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
795 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
796 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
798 getret = VOP_GETATTR(vp, vap, cred, td);
804 nfsm_reply(NFSX_POSTOPATTR(v3));
806 nfsm_srvpostop_attr(getret, vap);
812 * Calculate byte count to read
815 if (off >= vap->va_size)
817 else if ((off + reqlen) > vap->va_size)
818 cnt = vap->va_size - off;
823 * Calculate seqcount for heuristic
831 * Locate best candidate
834 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
838 if (nfsheur[hi].nh_vp == vp) {
842 if (nfsheur[hi].nh_use > 0)
843 --nfsheur[hi].nh_use;
844 hi = (hi + 1) & (NUM_HEURISTIC - 1);
845 if (nfsheur[hi].nh_use < nh->nh_use)
849 if (nh->nh_vp != vp) {
852 nh->nh_use = NHUSE_INIT;
860 * Calculate heuristic
863 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
864 if (++nh->nh_seqcount > 127)
865 nh->nh_seqcount = 127;
866 } else if (nh->nh_seqcount > 1) {
871 nh->nh_use += NHUSE_INC;
872 if (nh->nh_use > NHUSE_MAX)
873 nh->nh_use = NHUSE_MAX;
874 ioflag |= nh->nh_seqcount << 16;
877 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
879 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
880 *tl++ = nfsrv_nfs_true;
881 fp = (struct nfs_fattr *)tl;
882 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
884 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
885 fp = (struct nfs_fattr *)tl;
886 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
888 len = left = nfsm_rndup(cnt);
891 * Generate the mbuf list with the uio_iov ref. to it.
896 siz = min(M_TRAILINGSPACE(m), left);
902 MGET(m, M_TRYWAIT, MT_DATA);
903 MCLGET(m, M_TRYWAIT);
909 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
911 uiop->uio_iov = iv2 = iv;
917 panic("nfsrv_read iov");
918 siz = min(M_TRAILINGSPACE(m), left);
920 iv->iov_base = mtod(m, caddr_t) + m->m_len;
929 uiop->uio_iovcnt = i;
930 uiop->uio_offset = off;
931 uiop->uio_resid = len;
932 uiop->uio_rw = UIO_READ;
933 uiop->uio_segflg = UIO_SYSSPACE;
934 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
935 off = uiop->uio_offset;
937 FREE((caddr_t)iv2, M_TEMP);
938 if (error || (getret = VOP_GETATTR(vp, vap, cred, td))) {
944 nfsm_reply(NFSX_POSTOPATTR(v3));
946 nfsm_srvpostop_attr(getret, vap);
955 nfsm_srvfillattr(vap, fp);
956 tlen = len - uiop->uio_resid;
957 cnt = cnt < tlen ? cnt : tlen;
958 tlen = nfsm_rndup(cnt);
959 if (len != tlen || tlen != cnt)
960 nfsm_adj(mb, len - tlen, tlen - cnt);
962 *tl++ = txdr_unsigned(cnt);
964 *tl++ = nfsrv_nfs_true;
966 *tl++ = nfsrv_nfs_false;
968 *tl = txdr_unsigned(cnt);
979 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
980 struct thread *td, struct mbuf **mrq)
982 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
983 struct sockaddr *nam = nfsd->nd_nam;
984 caddr_t dpos = nfsd->nd_dpos;
985 struct ucred *cred = &nfsd->nd_cr;
989 struct nfs_fattr *fp;
991 struct vattr va, forat;
992 struct vattr *vap = &va;
995 int error = 0, rdonly, len, forat_ret = 1;
996 int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
997 int stable = NFSV3WRITE_FILESYNC;
998 int v3 = (nfsd->nd_flag & ND_NFSV3);
999 struct mbuf *mb, *mreq;
1000 struct vnode *vp = NULL;
1003 struct uio io, *uiop = &io;
1005 struct mount *mntp = NULL;
1007 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1013 fhp = &nfh.fh_generic;
1015 if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1019 if ((error = VFS_FHTOVP(mntp, &fhp->fh_fid, &vp)) != 0) {
1023 (void) vn_start_write(vp, &mntp, V_WAIT);
1027 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1028 off = fxdr_hyper(tl);
1030 stable = fxdr_unsigned(int, *tl++);
1032 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1033 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1036 stable = NFSV3WRITE_UNSTABLE;
1038 retlen = len = fxdr_unsigned(int32_t, *tl);
1042 * For NFS Version 2, it is not obvious what a write of zero length
1043 * should do, but I might as well be consistent with Version 3,
1044 * which is to return ok so long as there are no permission problems.
1052 adjust = dpos - mtod(mp, caddr_t);
1053 mp->m_len -= adjust;
1054 if (mp->m_len > 0 && adjust > 0)
1055 mp->m_data += adjust;
1059 else if (mp->m_len > 0) {
1062 mp->m_len -= (i - len);
1071 if (len > NFS_MAXDATA || len < 0 || i < len) {
1073 nfsm_reply(2 * NFSX_UNSIGNED);
1075 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1079 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
1082 nfsm_reply(2 * NFSX_UNSIGNED);
1084 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1089 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1090 if (vp->v_type != VREG) {
1094 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1097 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1101 nfsm_reply(NFSX_WCCDATA(v3));
1103 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1109 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1111 uiop->uio_iov = iv = ivp;
1112 uiop->uio_iovcnt = cnt;
1115 if (mp->m_len > 0) {
1116 ivp->iov_base = mtod(mp, caddr_t);
1117 ivp->iov_len = mp->m_len;
1125 * The IO_METASYNC flag indicates that all metadata (and not just
1126 * enough to ensure data integrity) mus be written to stable storage
1128 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1130 if (stable == NFSV3WRITE_UNSTABLE)
1131 ioflags = IO_NODELOCKED;
1132 else if (stable == NFSV3WRITE_DATASYNC)
1133 ioflags = (IO_SYNC | IO_NODELOCKED);
1135 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1136 uiop->uio_resid = len;
1137 uiop->uio_rw = UIO_WRITE;
1138 uiop->uio_segflg = UIO_SYSSPACE;
1139 uiop->uio_td = (struct thread *)0;
1140 uiop->uio_offset = off;
1141 error = VOP_WRITE(vp, uiop, ioflags, cred);
1142 nfsrvstats.srvvop_writes++;
1143 FREE((caddr_t)iv, M_TEMP);
1145 aftat_ret = VOP_GETATTR(vp, vap, cred, td);
1151 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1152 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1154 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1159 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1160 *tl++ = txdr_unsigned(retlen);
1162 * If nfs_async is set, then pretend the write was FILESYNC.
1164 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1165 *tl++ = txdr_unsigned(stable);
1167 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1169 * Actually, there is no need to txdr these fields,
1170 * but it may make the values more human readable,
1171 * for debugging purposes.
1173 if (nfsver.tv_sec == 0)
1175 *tl++ = txdr_unsigned(nfsver.tv_sec);
1176 *tl = txdr_unsigned(nfsver.tv_usec);
1177 } else if (!error) {
1178 /* v2 non-error case. */
1179 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1180 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);
1684 vap->va_type = VREG;
1686 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
1687 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1688 if (vap->va_type == VNON)
1689 vap->va_type = VREG;
1690 vap->va_mode = nfstov_mode(sp->sa_mode);
1691 switch (vap->va_type) {
1693 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1695 vap->va_size = (u_quad_t)tsize;
1700 rdev = fxdr_unsigned(long, sp->sa_size);
1708 * Iff doesn't exist, create it
1709 * otherwise just truncate to 0 length
1710 * should I set the mode too ?
1712 * The only possible error we can have at this point is EEXIST.
1713 * nd.ni_vp will also be non-NULL in that case.
1715 if (nd.ni_vp == NULL) {
1716 if (vap->va_mode == (mode_t)VNOVAL)
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,
1733 } else if (vap->va_type == VCHR || vap->va_type == VBLK ||
1734 vap->va_type == VFIFO) {
1736 * NFSv2-specific code for creating device nodes
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 nfsrv_object_create(nd.ni_vp);
1780 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1788 if (vap->va_size != -1) {
1789 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1790 (nd.ni_cnd.cn_flags & RDONLY), td, 0);
1792 tempsize = vap->va_size;
1794 vap->va_size = tempsize;
1795 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1802 bzero((caddr_t)fhp, sizeof(nfh));
1803 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1804 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1806 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
1809 if (exclusive_flag && !error &&
1810 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1812 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1817 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1820 nfsm_srvpostop_fh(fhp);
1821 nfsm_srvpostop_attr(0, vap);
1823 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1824 } else if (!error) {
1825 /* v2 non-error case. */
1826 nfsm_srvfhtom(fhp, v3);
1827 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1828 nfsm_srvfillattr(vap, fp);
1833 if (nd.ni_startdir) {
1834 vrele(nd.ni_startdir);
1835 nd.ni_startdir = NULL;
1839 NDFREE(&nd, NDF_ONLY_PNBUF);
1841 if (nd.ni_dvp == nd.ni_vp)
1848 vn_finished_write(mp);
1853 * nfs v3 mknod service
1856 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1857 struct thread *td, struct mbuf **mrq)
1859 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1860 struct sockaddr *nam = nfsd->nd_nam;
1861 caddr_t dpos = nfsd->nd_dpos;
1862 struct ucred *cred = &nfsd->nd_cr;
1863 struct vattr va, dirfor, diraft;
1864 struct vattr *vap = &va;
1866 struct nameidata nd;
1868 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1869 u_int32_t major, minor;
1871 struct mbuf *mb, *mreq;
1872 struct vnode *vp, *dirp = (struct vnode *)0;
1875 struct mount *mp = NULL;
1876 int v3 = (nfsd->nd_flag & ND_NFSV3);
1878 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1880 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1883 fhp = &nfh.fh_generic;
1885 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1889 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1893 (void) vn_start_write(vp, &mp, V_WAIT);
1896 nfsm_srvnamesiz(len);
1898 nd.ni_cnd.cn_cred = cred;
1899 nd.ni_cnd.cn_nameiop = CREATE;
1900 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1903 * Handle nfs_namei() call. If an error occurs, the nd structure
1904 * is not valid. However, nfsm_*() routines may still jump to
1908 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1911 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
1913 nfsm_reply(NFSX_WCCDATA(1));
1914 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1918 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1919 vtyp = nfsv3tov_type(*tl);
1920 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1921 error = NFSERR_BADTYPE;
1926 if (vtyp == VCHR || vtyp == VBLK) {
1927 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1928 major = fxdr_unsigned(u_int32_t, *tl++);
1929 minor = fxdr_unsigned(u_int32_t, *tl);
1930 vap->va_rdev = makeudev(major, minor);
1934 * Iff doesn't exist, create it.
1940 vap->va_type = vtyp;
1941 if (vap->va_mode == (mode_t)VNOVAL)
1943 if (vtyp == VSOCK) {
1944 vrele(nd.ni_startdir);
1945 nd.ni_startdir = NULL;
1946 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1948 NDFREE(&nd, NDF_ONLY_PNBUF);
1950 if (vtyp != VFIFO && (error = suser_xxx(cred, 0, 0)))
1952 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1954 NDFREE(&nd, NDF_ONLY_PNBUF);
1961 * Release dvp prior to lookup
1966 nd.ni_cnd.cn_nameiop = LOOKUP;
1967 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1968 nd.ni_cnd.cn_thread = td;
1969 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1971 error = lookup(&nd);
1976 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1981 * send response, cleanup, return.
1984 if (nd.ni_startdir) {
1985 vrele(nd.ni_startdir);
1986 nd.ni_startdir = NULL;
1988 NDFREE(&nd, NDF_ONLY_PNBUF);
1990 if (nd.ni_dvp == nd.ni_vp)
1998 bzero((caddr_t)fhp, sizeof(nfh));
1999 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2000 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2002 error = VOP_GETATTR(vp, vap, cred, td);
2009 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2015 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2018 nfsm_srvpostop_fh(fhp);
2019 nfsm_srvpostop_attr(0, vap);
2021 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2023 vn_finished_write(mp);
2029 vrele(nd.ni_startdir);
2030 NDFREE(&nd, NDF_ONLY_PNBUF);
2032 if (nd.ni_dvp == nd.ni_vp)
2039 vn_finished_write(mp);
2044 * nfs remove service
2047 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2048 struct thread *td, struct mbuf **mrq)
2050 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2051 struct sockaddr *nam = nfsd->nd_nam;
2052 caddr_t dpos = nfsd->nd_dpos;
2053 struct ucred *cred = &nfsd->nd_cr;
2054 struct nameidata nd;
2056 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2057 int v3 = (nfsd->nd_flag & ND_NFSV3);
2058 struct mbuf *mb, *mreq;
2060 struct vattr dirfor, diraft;
2063 struct mount *mp = NULL;
2066 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2069 fhp = &nfh.fh_generic;
2071 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2075 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2079 (void) vn_start_write(vp, &mp, V_WAIT);
2082 nfsm_srvnamesiz(len);
2084 nd.ni_cnd.cn_cred = cred;
2085 nd.ni_cnd.cn_nameiop = DELETE;
2086 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2087 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2091 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2099 if (nd.ni_vp->v_type == VDIR) {
2100 error = EPERM; /* POSIX */
2104 * The root of a mounted filesystem cannot be deleted.
2106 if (nd.ni_vp->v_flag & VROOT) {
2112 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2113 NDFREE(&nd, NDF_ONLY_PNBUF);
2117 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2122 nfsm_reply(NFSX_WCCDATA(v3));
2124 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2128 NDFREE(&nd, NDF_ONLY_PNBUF);
2130 if (nd.ni_dvp == nd.ni_vp)
2137 vn_finished_write(mp);
2142 * nfs rename service
2145 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2146 struct thread *td, struct mbuf **mrq)
2148 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2149 struct sockaddr *nam = nfsd->nd_nam;
2150 caddr_t dpos = nfsd->nd_dpos;
2151 struct ucred *cred = &nfsd->nd_cr;
2153 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2154 int tdirfor_ret = 1, tdiraft_ret = 1;
2155 int v3 = (nfsd->nd_flag & ND_NFSV3);
2156 struct mbuf *mb, *mreq;
2157 struct nameidata fromnd, tond;
2158 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2159 struct vnode *tdirp = (struct vnode *)0;
2160 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2162 fhandle_t *ffhp, *tfhp;
2164 struct mount *mp = NULL;
2167 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2169 fvp = (struct vnode *)0;
2171 ffhp = &fnfh.fh_generic;
2172 tfhp = &tnfh.fh_generic;
2175 * Clear fields incase goto nfsmout occurs from macro.
2181 nfsm_srvmtofh(ffhp);
2182 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
2186 if ((error = VFS_FHTOVP(mp, &ffhp->fh_fid, &vp)) != 0) {
2190 (void) vn_start_write(vp, &mp, V_WAIT);
2193 nfsm_srvnamesiz(len);
2195 * Remember our original uid so that we can reset cr_uid before
2196 * the second nfs_namei() call, in case it is remapped.
2198 saved_uid = cred->cr_uid;
2199 fromnd.ni_cnd.cn_cred = cred;
2200 fromnd.ni_cnd.cn_nameiop = DELETE;
2201 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2202 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2203 &dpos, &fdirp, td, FALSE);
2206 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2214 nfsm_reply(2 * NFSX_WCCDATA(v3));
2216 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2217 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2223 nfsm_srvmtofh(tfhp);
2224 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2225 cred->cr_uid = saved_uid;
2226 tond.ni_cnd.cn_cred = cred;
2227 tond.ni_cnd.cn_nameiop = RENAME;
2228 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2229 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2230 &dpos, &tdirp, td, FALSE);
2233 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
2246 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2252 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2259 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2267 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2274 if (fvp->v_mount != tdvp->v_mount) {
2288 * If source is the same as the destination (that is the
2289 * same vnode with the same name in the same directory),
2290 * then there is nothing to do.
2292 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2293 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2294 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2295 fromnd.ni_cnd.cn_namelen))
2300 * The VOP_RENAME function releases all vnode references &
2301 * locks prior to returning so we need to clear the pointers
2302 * to bypass cleanup code later on.
2304 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2305 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2306 fromnd.ni_dvp = NULL;
2307 fromnd.ni_vp = NULL;
2311 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2312 tond.ni_cnd.cn_flags &= ~HASBUF;
2322 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, td);
2324 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, td);
2325 nfsm_reply(2 * NFSX_WCCDATA(v3));
2327 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2328 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2335 * Clear out tond related fields
2339 if (tond.ni_startdir)
2340 vrele(tond.ni_startdir);
2341 NDFREE(&tond, NDF_ONLY_PNBUF);
2343 if (tond.ni_dvp == tond.ni_vp)
2352 * Clear out fromnd related fields
2356 if (fromnd.ni_startdir)
2357 vrele(fromnd.ni_startdir);
2358 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2360 vrele(fromnd.ni_dvp);
2362 vrele(fromnd.ni_vp);
2364 vn_finished_write(mp);
2372 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2373 struct thread *td, struct mbuf **mrq)
2375 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2376 struct sockaddr *nam = nfsd->nd_nam;
2377 caddr_t dpos = nfsd->nd_dpos;
2378 struct ucred *cred = &nfsd->nd_cr;
2379 struct nameidata nd;
2381 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2382 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2383 struct mbuf *mb, *mreq;
2384 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2385 struct vattr dirfor, diraft, at;
2387 fhandle_t *fhp, *dfhp;
2388 struct mount *mp = NULL;
2390 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2393 fhp = &nfh.fh_generic;
2394 dfhp = &dnfh.fh_generic;
2396 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2400 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2404 (void) vn_start_write(vp, &mp, V_WAIT);
2407 nfsm_srvmtofh(dfhp);
2408 nfsm_srvnamesiz(len);
2410 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam, &rdonly, TRUE);
2412 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2414 nfsm_srvpostop_attr(getret, &at);
2415 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2421 if (vp->v_type == VDIR) {
2422 error = EPERM; /* POSIX */
2425 nd.ni_cnd.cn_cred = cred;
2426 nd.ni_cnd.cn_nameiop = CREATE;
2427 nd.ni_cnd.cn_flags = LOCKPARENT;
2428 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2432 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2448 if (vp->v_mount != xp->v_mount)
2452 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2453 NDFREE(&nd, NDF_ONLY_PNBUF);
2459 getret = VOP_GETATTR(vp, &at, cred, td);
2461 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2463 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2465 nfsm_srvpostop_attr(getret, &at);
2466 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2472 NDFREE(&nd, NDF_ONLY_PNBUF);
2478 if (nd.ni_dvp == nd.ni_vp)
2485 vn_finished_write(mp);
2490 * nfs symbolic link service
2493 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2494 struct thread *td, struct mbuf **mrq)
2496 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2497 struct sockaddr *nam = nfsd->nd_nam;
2498 caddr_t dpos = nfsd->nd_dpos;
2499 struct ucred *cred = &nfsd->nd_cr;
2500 struct vattr va, dirfor, diraft;
2501 struct nameidata nd;
2502 struct vattr *vap = &va;
2503 struct nfsv2_sattr *sp;
2504 char *bpos, *pathcp = (char *)0;
2507 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2508 int v3 = (nfsd->nd_flag & ND_NFSV3);
2509 struct mbuf *mb, *mreq;
2510 struct vnode *dirp = (struct vnode *)0;
2513 struct mount *mp = NULL;
2516 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2519 fhp = &nfh.fh_generic;
2521 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2525 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2529 (void) vn_start_write(vp, &mp, V_WAIT);
2532 nfsm_srvnamesiz(len);
2533 nd.ni_cnd.cn_cred = cred;
2534 nd.ni_cnd.cn_nameiop = CREATE;
2535 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2536 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2540 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2553 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2554 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2555 iv.iov_base = pathcp;
2557 io.uio_resid = len2;
2561 io.uio_segflg = UIO_SYSSPACE;
2562 io.uio_rw = UIO_READ;
2563 io.uio_td = (struct thread *)0;
2564 nfsm_mtouio(&io, len2);
2566 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
2567 vap->va_mode = nfstov_mode(sp->sa_mode);
2569 *(pathcp + len2) = '\0';
2576 * issue symlink op. SAVESTART is set so the underlying path component
2577 * is only freed by the VOP if an error occurs.
2579 if (vap->va_mode == (mode_t)VNOVAL)
2581 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2583 NDFREE(&nd, NDF_ONLY_PNBUF);
2588 * releases directory prior to potential lookup op.
2596 * Issue lookup. Leave SAVESTART set so we can easily free
2597 * the name buffer later on.
2599 * since LOCKPARENT is not set, ni_dvp will be garbage on
2600 * return whether an error occurs or not.
2602 nd.ni_cnd.cn_nameiop = LOOKUP;
2603 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2604 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2605 nd.ni_cnd.cn_thread = td;
2606 nd.ni_cnd.cn_cred = cred;
2608 error = lookup(&nd);
2612 bzero((caddr_t)fhp, sizeof(nfh));
2613 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2614 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2616 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2625 * These releases aren't strictly required, does even doing them
2626 * make any sense? XXX can nfsm_reply() block?
2629 FREE(pathcp, M_TEMP);
2633 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2637 if (nd.ni_startdir) {
2638 vrele(nd.ni_startdir);
2639 nd.ni_startdir = NULL;
2641 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2644 nfsm_srvpostop_fh(fhp);
2645 nfsm_srvpostop_attr(0, vap);
2647 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2653 NDFREE(&nd, NDF_ONLY_PNBUF);
2655 if (nd.ni_dvp == nd.ni_vp)
2663 vrele(nd.ni_startdir);
2667 FREE(pathcp, M_TEMP);
2669 vn_finished_write(mp);
2677 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2678 struct thread *td, struct mbuf **mrq)
2680 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2681 struct sockaddr *nam = nfsd->nd_nam;
2682 caddr_t dpos = nfsd->nd_dpos;
2683 struct ucred *cred = &nfsd->nd_cr;
2684 struct vattr va, dirfor, diraft;
2685 struct vattr *vap = &va;
2686 struct nfs_fattr *fp;
2687 struct nameidata nd;
2690 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2691 int v3 = (nfsd->nd_flag & ND_NFSV3);
2692 struct mbuf *mb, *mreq;
2693 struct vnode *dirp = NULL;
2697 struct mount *mp = NULL;
2700 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2703 fhp = &nfh.fh_generic;
2705 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2709 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2713 (void) vn_start_write(vp, &mp, V_WAIT);
2716 nfsm_srvnamesiz(len);
2717 nd.ni_cnd.cn_cred = cred;
2718 nd.ni_cnd.cn_nameiop = CREATE;
2719 nd.ni_cnd.cn_flags = LOCKPARENT;
2721 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2725 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2733 nfsm_reply(NFSX_WCCDATA(v3));
2735 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2743 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2744 vap->va_mode = nfstov_mode(*tl++);
2748 * At this point nd.ni_dvp is referenced and exclusively locked and
2749 * nd.ni_vp, if it exists, is referenced but not locked.
2752 vap->va_type = VDIR;
2753 if (nd.ni_vp != NULL) {
2754 NDFREE(&nd, NDF_ONLY_PNBUF);
2760 * Issue mkdir op. Since SAVESTART is not set, the pathname
2761 * component is freed by the VOP call. This will fill-in
2762 * nd.ni_vp, reference, and exclusively lock it.
2764 if (vap->va_mode == (mode_t)VNOVAL)
2766 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2767 NDFREE(&nd, NDF_ONLY_PNBUF);
2774 bzero((caddr_t)fhp, sizeof(nfh));
2775 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2776 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2778 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2782 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2783 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2786 nfsm_srvpostop_fh(fhp);
2787 nfsm_srvpostop_attr(0, vap);
2789 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2790 } else if (!error) {
2791 /* v2 non-error case. */
2792 nfsm_srvfhtom(fhp, v3);
2793 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2794 nfsm_srvfillattr(vap, fp);
2803 NDFREE(&nd, NDF_ONLY_PNBUF);
2804 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2815 vn_finished_write(mp);
2823 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2824 struct thread *td, struct mbuf **mrq)
2826 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2827 struct sockaddr *nam = nfsd->nd_nam;
2828 caddr_t dpos = nfsd->nd_dpos;
2829 struct ucred *cred = &nfsd->nd_cr;
2831 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2832 int v3 = (nfsd->nd_flag & ND_NFSV3);
2833 struct mbuf *mb, *mreq;
2834 struct vnode *vp, *dirp = (struct vnode *)0;
2835 struct vattr dirfor, diraft;
2838 struct nameidata nd;
2839 struct mount *mp = NULL;
2841 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2844 fhp = &nfh.fh_generic;
2846 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2850 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2854 (void) vn_start_write(vp, &mp, V_WAIT);
2857 nfsm_srvnamesiz(len);
2858 nd.ni_cnd.cn_cred = cred;
2859 nd.ni_cnd.cn_nameiop = DELETE;
2860 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2861 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2865 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2873 nfsm_reply(NFSX_WCCDATA(v3));
2875 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2880 if (vp->v_type != VDIR) {
2885 * No rmdir "." please.
2887 if (nd.ni_dvp == vp) {
2892 * The root of a mounted filesystem cannot be deleted.
2894 if (vp->v_flag & VROOT)
2898 * Issue or abort op. Since SAVESTART is not set, path name
2899 * component is freed by the VOP after either.
2902 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2903 NDFREE(&nd, NDF_ONLY_PNBUF);
2906 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2907 nfsm_reply(NFSX_WCCDATA(v3));
2909 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2915 NDFREE(&nd, NDF_ONLY_PNBUF);
2919 if (nd.ni_dvp == nd.ni_vp)
2927 vn_finished_write(mp);
2932 * nfs readdir service
2933 * - mallocs what it thinks is enough to read
2934 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2935 * - calls VOP_READDIR()
2936 * - loops around building the reply
2937 * if the output generated exceeds count break out of loop
2938 * The nfsm_clget macro is used here so that the reply will be packed
2939 * tightly in mbuf clusters.
2940 * - it only knows that it has encountered eof when the VOP_READDIR()
2942 * - as such one readdir rpc will return eof false although you are there
2943 * and then the next will return eof
2944 * - it trims out records with d_fileno == 0
2945 * this doesn't matter for Unix clients, but they might confuse clients
2947 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2948 * than requested, but this may not apply to all filesystems. For
2949 * example, client NFS does not { although it is never remote mounted
2951 * The alternate call nfsrv_readdirplus() does lookups as well.
2952 * PS: The NFS protocol spec. does not clarify what the "count" byte
2953 * argument is a count of.. just name strings and file id's or the
2954 * entire reply rpc or ...
2955 * I tried just file name and id sizes and it confused the Sun client,
2956 * so I am using the full rpc size now. The "paranoia.." comment refers
2957 * to including the status longwords that are not a part of the dir.
2958 * "entry" structures, but are in the rpc.
2962 u_int32_t fl_postopok;
2963 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2965 u_int32_t fl_fhsize;
2966 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2970 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2971 struct thread *td, struct mbuf **mrq)
2973 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2974 struct sockaddr *nam = nfsd->nd_nam;
2975 caddr_t dpos = nfsd->nd_dpos;
2976 struct ucred *cred = &nfsd->nd_cr;
2983 struct mbuf *mb, *mreq;
2984 char *cpos, *cend, *rbuf;
2985 struct vnode *vp = NULL;
2991 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2992 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2993 int v3 = (nfsd->nd_flag & ND_NFSV3);
2994 u_quad_t off, toff, verf;
2995 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2997 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2998 fhp = &nfh.fh_generic;
3001 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
3002 toff = fxdr_hyper(tl);
3004 verf = fxdr_hyper(tl);
3007 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
3008 toff = fxdr_unsigned(u_quad_t, *tl++);
3009 verf = 0; /* shut up gcc */
3012 cnt = fxdr_unsigned(int, *tl);
3013 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3014 xfer = NFS_SRVMAXDATA(nfsd);
3018 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3019 if (!error && vp->v_type != VDIR) {
3025 nfsm_reply(NFSX_UNSIGNED);
3027 nfsm_srvpostop_attr(getret, &at);
3033 * Obtain lock on vnode for this section of the code
3036 error = getret = VOP_GETATTR(vp, &at, cred, td);
3039 * XXX This check may be too strict for Solaris 2.5 clients.
3041 if (!error && toff && verf && verf != at.va_filerev)
3042 error = NFSERR_BAD_COOKIE;
3046 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3050 nfsm_reply(NFSX_POSTOPATTR(v3));
3052 nfsm_srvpostop_attr(getret, &at);
3056 VOP_UNLOCK(vp, 0, td);
3059 * end section. Allocate rbuf and continue
3061 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3064 iv.iov_len = fullsiz;
3067 io.uio_offset = (off_t)off;
3068 io.uio_resid = fullsiz;
3069 io.uio_segflg = UIO_SYSSPACE;
3070 io.uio_rw = UIO_READ;
3071 io.uio_td = (struct thread *)0;
3073 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3075 free((caddr_t)cookies, M_TEMP);
3078 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3079 off = (off_t)io.uio_offset;
3080 if (!cookies && !error)
3081 error = NFSERR_PERM;
3083 getret = VOP_GETATTR(vp, &at, cred, td);
3087 VOP_UNLOCK(vp, 0, td);
3091 free((caddr_t)rbuf, M_TEMP);
3093 free((caddr_t)cookies, M_TEMP);
3094 nfsm_reply(NFSX_POSTOPATTR(v3));
3096 nfsm_srvpostop_attr(getret, &at);
3101 siz -= io.uio_resid;
3104 * If nothing read, return eof
3110 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3113 nfsm_srvpostop_attr(getret, &at);
3114 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3115 txdr_hyper(at.va_filerev, tl);
3118 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3119 *tl++ = nfsrv_nfs_false;
3120 *tl = nfsrv_nfs_true;
3121 FREE((caddr_t)rbuf, M_TEMP);
3122 FREE((caddr_t)cookies, M_TEMP);
3129 * Check for degenerate cases of nothing useful read.
3130 * If so go try again
3134 dp = (struct dirent *)cpos;
3137 * For some reason FreeBSD's ufs_readdir() chooses to back the
3138 * directory offset up to a block boundary, so it is necessary to
3139 * skip over the records that precede the requested offset. This
3140 * requires the assumption that file offset cookies monotonically
3143 while (cpos < cend && ncookies > 0 &&
3144 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3145 ((u_quad_t)(*cookiep)) <= toff)) {
3146 cpos += dp->d_reclen;
3147 dp = (struct dirent *)cpos;
3151 if (cpos >= cend || ncookies == 0) {
3157 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3158 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3160 nfsm_srvpostop_attr(getret, &at);
3161 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3162 txdr_hyper(at.va_filerev, tl);
3166 be = bp + M_TRAILINGSPACE(mp);
3168 /* Loop through the records and build reply */
3169 while (cpos < cend && ncookies > 0) {
3170 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3171 nlen = dp->d_namlen;
3172 rem = nfsm_rndup(nlen) - nlen;
3173 len += (4 * NFSX_UNSIGNED + nlen + rem);
3175 len += 2 * NFSX_UNSIGNED;
3181 * Build the directory record xdr from
3185 *tl = nfsrv_nfs_true;
3186 bp += NFSX_UNSIGNED;
3190 bp += NFSX_UNSIGNED;
3193 *tl = txdr_unsigned(dp->d_fileno);
3194 bp += NFSX_UNSIGNED;
3196 *tl = txdr_unsigned(nlen);
3197 bp += NFSX_UNSIGNED;
3199 /* And loop around copying the name */
3208 bcopy(cp, bp, tsiz);
3214 /* And null pad to a int32_t boundary */
3215 for (i = 0; i < rem; i++)
3219 /* Finish off the record */
3222 bp += NFSX_UNSIGNED;
3225 *tl = txdr_unsigned(*cookiep);
3226 bp += NFSX_UNSIGNED;
3228 cpos += dp->d_reclen;
3229 dp = (struct dirent *)cpos;
3236 *tl = nfsrv_nfs_false;
3237 bp += NFSX_UNSIGNED;
3240 *tl = nfsrv_nfs_true;
3242 *tl = nfsrv_nfs_false;
3243 bp += NFSX_UNSIGNED;
3246 mp->m_len = bp - mtod(mp, caddr_t);
3248 mp->m_len += bp - bpos;
3249 FREE((caddr_t)rbuf, M_TEMP);
3250 FREE((caddr_t)cookies, M_TEMP);
3259 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3260 struct thread *td, struct mbuf **mrq)
3262 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3263 struct sockaddr *nam = nfsd->nd_nam;
3264 caddr_t dpos = nfsd->nd_dpos;
3265 struct ucred *cred = &nfsd->nd_cr;
3272 struct mbuf *mb, *mreq;
3273 char *cpos, *cend, *rbuf;
3274 struct vnode *vp = NULL, *nvp;
3277 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3280 struct vattr va, at, *vap = &va;
3281 struct nfs_fattr *fp;
3282 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3283 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3284 u_quad_t off, toff, verf;
3285 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3286 int v3 = (nfsd->nd_flag & ND_NFSV3);
3288 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3290 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3291 fhp = &nfh.fh_generic;
3293 tl = nfsm_dissect(u_int32_t *, 6 * NFSX_UNSIGNED);
3294 toff = fxdr_hyper(tl);
3296 verf = fxdr_hyper(tl);
3298 siz = fxdr_unsigned(int, *tl++);
3299 cnt = fxdr_unsigned(int, *tl);
3301 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3302 xfer = NFS_SRVMAXDATA(nfsd);
3306 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3307 if (!error && vp->v_type != VDIR) {
3313 nfsm_reply(NFSX_UNSIGNED);
3314 nfsm_srvpostop_attr(getret, &at);
3318 error = getret = VOP_GETATTR(vp, &at, cred, td);
3321 * XXX This check may be too strict for Solaris 2.5 clients.
3323 if (!error && toff && verf && verf != at.va_filerev)
3324 error = NFSERR_BAD_COOKIE;
3327 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3331 nfsm_reply(NFSX_V3POSTOPATTR);
3332 nfsm_srvpostop_attr(getret, &at);
3336 VOP_UNLOCK(vp, 0, td);
3337 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3340 iv.iov_len = fullsiz;
3343 io.uio_offset = (off_t)off;
3344 io.uio_resid = fullsiz;
3345 io.uio_segflg = UIO_SYSSPACE;
3346 io.uio_rw = UIO_READ;
3347 io.uio_td = (struct thread *)0;
3349 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3351 free((caddr_t)cookies, M_TEMP);
3354 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3355 off = (u_quad_t)io.uio_offset;
3356 getret = VOP_GETATTR(vp, &at, cred, td);
3357 VOP_UNLOCK(vp, 0, td);
3358 if (!cookies && !error)
3359 error = NFSERR_PERM;
3366 free((caddr_t)cookies, M_TEMP);
3367 free((caddr_t)rbuf, M_TEMP);
3368 nfsm_reply(NFSX_V3POSTOPATTR);
3369 nfsm_srvpostop_attr(getret, &at);
3374 siz -= io.uio_resid;
3377 * If nothing read, return eof
3383 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3385 nfsm_srvpostop_attr(getret, &at);
3386 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3387 txdr_hyper(at.va_filerev, tl);
3389 *tl++ = nfsrv_nfs_false;
3390 *tl = nfsrv_nfs_true;
3391 FREE((caddr_t)cookies, M_TEMP);
3392 FREE((caddr_t)rbuf, M_TEMP);
3399 * Check for degenerate cases of nothing useful read.
3400 * If so go try again
3404 dp = (struct dirent *)cpos;
3407 * For some reason FreeBSD's ufs_readdir() chooses to back the
3408 * directory offset up to a block boundary, so it is necessary to
3409 * skip over the records that precede the requested offset. This
3410 * requires the assumption that file offset cookies monotonically
3413 while (cpos < cend && ncookies > 0 &&
3414 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3415 ((u_quad_t)(*cookiep)) <= toff)) {
3416 cpos += dp->d_reclen;
3417 dp = (struct dirent *)cpos;
3421 if (cpos >= cend || ncookies == 0) {
3428 * Probe one of the directory entries to see if the filesystem
3431 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3432 error = NFSERR_NOTSUPP;
3435 free((caddr_t)cookies, M_TEMP);
3436 free((caddr_t)rbuf, M_TEMP);
3437 nfsm_reply(NFSX_V3POSTOPATTR);
3438 nfsm_srvpostop_attr(getret, &at);
3445 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3448 nfsm_srvpostop_attr(getret, &at);
3449 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3450 txdr_hyper(at.va_filerev, tl);
3453 be = bp + M_TRAILINGSPACE(mp);
3455 /* Loop through the records and build reply */
3456 while (cpos < cend && ncookies > 0) {
3457 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3458 nlen = dp->d_namlen;
3459 rem = nfsm_rndup(nlen)-nlen;
3462 * For readdir_and_lookup get the vnode using
3465 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3467 bzero((caddr_t)nfhp, NFSX_V3FH);
3469 nvp->v_mount->mnt_stat.f_fsid;
3470 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3475 if (VOP_GETATTR(nvp, vap, cred, td)) {
3484 * If either the dircount or maxcount will be
3485 * exceeded, get out now. Both of these lengths
3486 * are calculated conservatively, including all
3489 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3491 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3492 if (len > cnt || dirlen > fullsiz) {
3498 * Build the directory record xdr from
3501 fp = (struct nfs_fattr *)&fl.fl_fattr;
3502 nfsm_srvfillattr(vap, fp);
3503 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3504 fl.fl_fhok = nfsrv_nfs_true;
3505 fl.fl_postopok = nfsrv_nfs_true;
3506 fl.fl_off.nfsuquad[0] = 0;
3507 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3510 *tl = nfsrv_nfs_true;
3511 bp += NFSX_UNSIGNED;
3514 bp += NFSX_UNSIGNED;
3516 *tl = txdr_unsigned(dp->d_fileno);
3517 bp += NFSX_UNSIGNED;
3519 *tl = txdr_unsigned(nlen);
3520 bp += NFSX_UNSIGNED;
3522 /* And loop around copying the name */
3527 if ((bp + xfer) > be)
3531 bcopy(cp, bp, tsiz);
3537 /* And null pad to a int32_t boundary */
3538 for (i = 0; i < rem; i++)
3542 * Now copy the flrep structure out.
3544 xfer = sizeof (struct flrep);
3548 if ((bp + xfer) > be)
3552 bcopy(cp, bp, tsiz);
3560 cpos += dp->d_reclen;
3561 dp = (struct dirent *)cpos;
3568 *tl = nfsrv_nfs_false;
3569 bp += NFSX_UNSIGNED;
3572 *tl = nfsrv_nfs_true;
3574 *tl = nfsrv_nfs_false;
3575 bp += NFSX_UNSIGNED;
3578 mp->m_len = bp - mtod(mp, caddr_t);
3580 mp->m_len += bp - bpos;
3581 FREE((caddr_t)cookies, M_TEMP);
3582 FREE((caddr_t)rbuf, M_TEMP);
3590 * nfs commit service
3593 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3594 struct thread *td, struct mbuf **mrq)
3596 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3597 struct sockaddr *nam = nfsd->nd_nam;
3598 caddr_t dpos = nfsd->nd_dpos;
3599 struct ucred *cred = &nfsd->nd_cr;
3600 struct vattr bfor, aft;
3601 struct vnode *vp = NULL;
3606 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3607 struct mbuf *mb, *mreq;
3609 struct mount *mp = NULL;
3610 int v3 = (nfsd->nd_flag & ND_NFSV3);
3612 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3614 panic("nfsrv_commit: v3 proc called on a v2 connection");
3615 fhp = &nfh.fh_generic;
3617 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3621 if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
3625 (void) vn_start_write(vp, &mp, V_WAIT);
3628 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
3631 * XXX At this time VOP_FSYNC() does not accept offset and byte
3632 * count parameters, so these arguments are useless (someday maybe).
3634 off = fxdr_hyper(tl);
3636 cnt = fxdr_unsigned(int, *tl);
3637 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3639 nfsm_reply(2 * NFSX_UNSIGNED);
3640 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3644 for_ret = VOP_GETATTR(vp, &bfor, cred, td);
3646 if (cnt > MAX_COMMIT_COUNT) {
3648 * Give up and do the whole thing
3651 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3652 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3654 error = VOP_FSYNC(vp, cred, MNT_WAIT, td);
3657 * Locate and synchronously write any buffers that fall
3658 * into the requested range. Note: we are assuming that
3659 * f_iosize is a power of 2.
3661 int iosize = vp->v_mount->mnt_stat.f_iosize;
3662 int iomask = iosize - 1;
3667 * Align to iosize boundry, super-align to page boundry.
3670 cnt += off & iomask;
3671 off &= ~(u_quad_t)iomask;
3673 if (off & PAGE_MASK) {
3674 cnt += off & PAGE_MASK;
3675 off &= ~(u_quad_t)PAGE_MASK;
3677 lblkno = off / iosize;
3680 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3681 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3689 * If we have a buffer and it is marked B_DELWRI we
3690 * have to lock and write it. Otherwise the prior
3691 * write is assumed to have already been committed.
3693 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3694 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3695 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3696 continue; /* retry */
3699 bp->b_flags &= ~B_ASYNC;
3712 aft_ret = VOP_GETATTR(vp, &aft, cred, td);
3716 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3717 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3719 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3720 if (nfsver.tv_sec == 0)
3722 *tl++ = txdr_unsigned(nfsver.tv_sec);
3723 *tl = txdr_unsigned(nfsver.tv_usec);
3730 vn_finished_write(mp);
3735 * nfs statfs service
3738 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3739 struct thread *td, struct mbuf **mrq)
3741 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3742 struct sockaddr *nam = nfsd->nd_nam;
3743 caddr_t dpos = nfsd->nd_dpos;
3744 struct ucred *cred = &nfsd->nd_cr;
3746 struct nfs_statfs *sfp;
3748 int error = 0, rdonly, getret = 1;
3749 int v3 = (nfsd->nd_flag & ND_NFSV3);
3750 struct mbuf *mb, *mreq;
3751 struct vnode *vp = NULL;
3755 struct statfs statfs;
3758 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3759 fhp = &nfh.fh_generic;
3761 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3763 nfsm_reply(NFSX_UNSIGNED);
3765 nfsm_srvpostop_attr(getret, &at);
3770 error = VFS_STATFS(vp->v_mount, sf, td);
3771 getret = VOP_GETATTR(vp, &at, cred, td);
3774 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3776 nfsm_srvpostop_attr(getret, &at);
3781 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3783 tval = (u_quad_t)sf->f_blocks;
3784 tval *= (u_quad_t)sf->f_bsize;
3785 txdr_hyper(tval, &sfp->sf_tbytes);
3786 tval = (u_quad_t)sf->f_bfree;
3787 tval *= (u_quad_t)sf->f_bsize;
3788 txdr_hyper(tval, &sfp->sf_fbytes);
3789 tval = (u_quad_t)sf->f_bavail;
3790 tval *= (u_quad_t)sf->f_bsize;
3791 txdr_hyper(tval, &sfp->sf_abytes);
3792 sfp->sf_tfiles.nfsuquad[0] = 0;
3793 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3794 sfp->sf_ffiles.nfsuquad[0] = 0;
3795 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3796 sfp->sf_afiles.nfsuquad[0] = 0;
3797 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3798 sfp->sf_invarsec = 0;
3800 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3801 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3802 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3803 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3804 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3813 * nfs fsinfo service
3816 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3817 struct thread *td, struct mbuf **mrq)
3819 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3820 struct sockaddr *nam = nfsd->nd_nam;
3821 caddr_t dpos = nfsd->nd_dpos;
3822 struct ucred *cred = &nfsd->nd_cr;
3823 struct nfsv3_fsinfo *sip;
3825 int error = 0, rdonly, getret = 1, pref;
3826 struct mbuf *mb, *mreq;
3827 struct vnode *vp = NULL;
3833 int v3 = (nfsd->nd_flag & ND_NFSV3);
3835 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3837 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3838 fhp = &nfh.fh_generic;
3840 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3842 nfsm_reply(NFSX_UNSIGNED);
3843 nfsm_srvpostop_attr(getret, &at);
3848 /* XXX Try to make a guess on the max file size. */
3849 VFS_STATFS(vp->v_mount, &sb, td);
3850 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3852 getret = VOP_GETATTR(vp, &at, cred, td);
3855 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3856 nfsm_srvpostop_attr(getret, &at);
3857 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3861 * There should be file system VFS OP(s) to get this information.
3862 * For now, assume ufs.
3864 if (slp->ns_so->so_type == SOCK_DGRAM)
3865 pref = NFS_MAXDGRAMDATA;
3868 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3869 sip->fs_rtpref = txdr_unsigned(pref);
3870 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3871 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3872 sip->fs_wtpref = txdr_unsigned(pref);
3873 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3874 sip->fs_dtpref = txdr_unsigned(pref);
3875 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3876 sip->fs_timedelta.nfsv3_sec = 0;
3877 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3878 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3879 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3880 NFSV3FSINFO_CANSETTIME);
3888 * nfs pathconf service
3891 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3892 struct thread *td, struct mbuf **mrq)
3894 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3895 struct sockaddr *nam = nfsd->nd_nam;
3896 caddr_t dpos = nfsd->nd_dpos;
3897 struct ucred *cred = &nfsd->nd_cr;
3898 struct nfsv3_pathconf *pc;
3900 int error = 0, rdonly, getret = 1;
3901 register_t linkmax, namemax, chownres, notrunc;
3902 struct mbuf *mb, *mreq;
3903 struct vnode *vp = NULL;
3907 int v3 = (nfsd->nd_flag & ND_NFSV3);
3909 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3911 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3912 fhp = &nfh.fh_generic;
3914 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3916 nfsm_reply(NFSX_UNSIGNED);
3917 nfsm_srvpostop_attr(getret, &at);
3921 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3923 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3925 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3927 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3928 getret = VOP_GETATTR(vp, &at, cred, td);
3931 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3932 nfsm_srvpostop_attr(getret, &at);
3937 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3939 pc->pc_linkmax = txdr_unsigned(linkmax);
3940 pc->pc_namemax = txdr_unsigned(namemax);
3941 pc->pc_notrunc = txdr_unsigned(notrunc);
3942 pc->pc_chownrestricted = txdr_unsigned(chownres);
3945 * These should probably be supported by VOP_PATHCONF(), but
3946 * until msdosfs is exportable (why would you want to?), the
3947 * Unix defaults should be ok.
3949 pc->pc_caseinsensitive = nfsrv_nfs_false;
3950 pc->pc_casepreserving = nfsrv_nfs_true;
3958 * Null operation, used by clients to ping server
3962 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3963 struct thread *td, struct mbuf **mrq)
3965 struct mbuf *mrep = nfsd->nd_mrep;
3967 int error = NFSERR_RETVOID;
3968 struct mbuf *mb, *mreq;
3970 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3977 * No operation, used for obsolete procedures
3981 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3982 struct thread *td, struct mbuf **mrq)
3984 struct mbuf *mrep = nfsd->nd_mrep;
3987 struct mbuf *mb, *mreq;
3989 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3990 if (nfsd->nd_repstat)
3991 error = nfsd->nd_repstat;
3993 error = EPROCUNAVAIL;
4001 * Perform access checking for vnodes obtained from file handles that would
4002 * refer to files already opened by a Unix client. You cannot just use
4003 * vn_writechk() and VOP_ACCESS() for two reasons.
4004 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4005 * 2 - The owner is to be given access irrespective of mode bits for some
4006 * operations, so that processes that chmod after opening a file don't
4007 * break. I don't like this because it opens a security hole, but since
4008 * the nfs server opens a security hole the size of a barn door anyhow,
4011 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4012 * will return EPERM instead of EACCESS. EPERM is always an error.
4015 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
4016 struct thread *td, int override)
4021 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4022 if (flags & VWRITE) {
4023 /* Just vn_writechk() changed to check rdonly */
4025 * Disallow write attempts on read-only file systems;
4026 * unless the file is a socket or a block or character
4027 * device resident on the file system.
4029 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4030 switch (vp->v_type) {
4040 * If there's shared text associated with
4041 * the inode, we can't allow writing.
4043 if (vp->v_flag & VTEXT)
4046 error = VOP_GETATTR(vp, &vattr, cred, td);
4049 error = VOP_ACCESS(vp, flags, cred, td);
4051 * Allow certain operations for the owner (reads and writes
4052 * on files that are already open).
4054 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)