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
37 * $FreeBSD: src/sys/nfs/nfs_serv.c,v 1.93.2.6 2002/12/29 18:19:53 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_serv.c,v 1.4 2003/06/23 17:55:48 dillon Exp $
42 * nfs version 2 and 3 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request
46 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
47 * 3 - build the rpc reply in an mbuf list
49 * - do not mix the phases, since the nfsm_?? macros can return failures
50 * on a bad rpc or similar and do not do any vrele() or vput()'s
52 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
53 * error number iff error != 0 whereas
54 * returning an error from the server function implies a fatal error
55 * such as a badly constructed rpc request that should be dropped without
57 * For Version 3, nfsm_reply() does not return for the error case, since
58 * most version 3 rpcs return more than the status for error cases.
61 * Warning: always pay careful attention to resource cleanup on return
62 * and note that nfsm_*() macros can terminate a procedure on certain
65 * lookup() and namei()
66 * may return garbage in various structural fields/return elements
67 * if an error is returned, and may garbage up nd.ni_dvp even if no
68 * error is returned and you did not request LOCKPARENT or WANTPARENT.
70 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
71 * buffer has been freed or not.
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/namei.h>
78 #include <sys/unistd.h>
79 #include <sys/vnode.h>
80 #include <sys/mount.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/malloc.h>
85 #include <sys/dirent.h>
87 #include <sys/kernel.h>
88 #include <sys/sysctl.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_zone.h>
94 #include <vm/vm_object.h>
98 #include <nfs/nfsproto.h>
99 #include <nfs/rpcv2.h>
101 #include <nfs/xdr_subs.h>
102 #include <nfs/nfsm_subs.h>
103 #include <nfs/nqnfs.h>
106 #define nfsdbprintf(info) printf info
108 #define nfsdbprintf(info)
111 #define MAX_COMMIT_COUNT (1024 * 1024)
113 #define NUM_HEURISTIC 64
114 #define NHUSE_INIT 64
116 #define NHUSE_MAX 2048
118 static struct nfsheur {
119 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
120 off_t nh_nextr; /* next offset for sequential detection */
121 int nh_use; /* use count for selection */
122 int nh_seqcount; /* heuristic */
123 } nfsheur[NUM_HEURISTIC];
125 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
128 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
131 extern u_int32_t nfs_xdrneg1;
132 extern u_int32_t nfs_false, nfs_true;
133 extern enum vtype nv3tov_type[8];
134 extern struct nfsstats nfsstats;
136 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
137 int nfsrvw_procrastinate_v3 = 0;
139 static struct timeval nfsver = { 0 };
141 SYSCTL_DECL(_vfs_nfs);
143 static int nfs_async;
144 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
145 static int nfs_commit_blks;
146 static int nfs_commit_miss;
147 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
148 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
150 static int nfsrv_access __P((struct vnode *,int,struct ucred *,int,
151 struct proc *, int));
152 static void nfsrvw_coalesce __P((struct nfsrv_descript *,
153 struct nfsrv_descript *));
156 * Clear nameidata fields that are tested in nsfmout cleanup code prior
157 * to using first nfsm macro (that might jump to the cleanup code).
162 ndclear(struct nameidata *nd)
164 nd->ni_cnd.cn_flags = 0;
167 nd->ni_startdir = NULL;
171 * nfs v3 access service
174 nfsrv3_access(nfsd, slp, procp, mrq)
175 struct nfsrv_descript *nfsd;
176 struct nfssvc_sock *slp;
180 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
181 struct sockaddr *nam = nfsd->nd_nam;
182 caddr_t dpos = nfsd->nd_dpos;
183 struct ucred *cred = &nfsd->nd_cr;
184 struct vnode *vp = NULL;
187 register u_int32_t *tl;
190 int error = 0, rdonly, cache, getret;
192 struct mbuf *mb, *mreq, *mb2;
193 struct vattr vattr, *vap = &vattr;
194 u_long testmode, nfsmode;
197 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
201 fhp = &nfh.fh_generic;
203 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
204 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
205 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
207 nfsm_reply(NFSX_UNSIGNED);
208 nfsm_srvpostop_attr(1, (struct vattr *)0);
212 nfsmode = fxdr_unsigned(u_int32_t, *tl);
213 if ((nfsmode & NFSV3ACCESS_READ) &&
214 nfsrv_access(vp, VREAD, cred, rdonly, procp, 0))
215 nfsmode &= ~NFSV3ACCESS_READ;
216 if (vp->v_type == VDIR)
217 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
220 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
221 if ((nfsmode & testmode) &&
222 nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0))
223 nfsmode &= ~testmode;
224 if (vp->v_type == VDIR)
225 testmode = NFSV3ACCESS_LOOKUP;
227 testmode = NFSV3ACCESS_EXECUTE;
228 if ((nfsmode & testmode) &&
229 nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0))
230 nfsmode &= ~testmode;
231 getret = VOP_GETATTR(vp, vap, cred, procp);
234 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
235 nfsm_srvpostop_attr(getret, vap);
236 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
237 *tl = txdr_unsigned(nfsmode);
245 * nfs getattr service
248 nfsrv_getattr(nfsd, slp, procp, mrq)
249 struct nfsrv_descript *nfsd;
250 struct nfssvc_sock *slp;
254 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
255 struct sockaddr *nam = nfsd->nd_nam;
256 caddr_t dpos = nfsd->nd_dpos;
257 struct ucred *cred = &nfsd->nd_cr;
258 register struct nfs_fattr *fp;
260 register struct vattr *vap = &va;
261 struct vnode *vp = NULL;
264 register u_int32_t *tl;
267 int error = 0, rdonly, cache;
269 struct mbuf *mb, *mb2, *mreq;
272 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
273 fhp = &nfh.fh_generic;
275 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
276 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
282 nqsrv_getl(vp, ND_READ);
283 error = VOP_GETATTR(vp, vap, cred, procp);
286 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
291 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
292 nfsm_srvfillattr(vap, fp);
302 * nfs setattr service
305 nfsrv_setattr(nfsd, slp, procp, mrq)
306 struct nfsrv_descript *nfsd;
307 struct nfssvc_sock *slp;
311 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
312 struct sockaddr *nam = nfsd->nd_nam;
313 caddr_t dpos = nfsd->nd_dpos;
314 struct ucred *cred = &nfsd->nd_cr;
315 struct vattr va, preat;
316 register struct vattr *vap = &va;
317 register struct nfsv2_sattr *sp;
318 register struct nfs_fattr *fp;
319 struct vnode *vp = NULL;
322 register u_int32_t *tl;
325 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
326 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
328 struct mbuf *mb, *mb2, *mreq;
330 struct timespec guard;
332 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
333 fhp = &nfh.fh_generic;
338 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
339 gcheck = fxdr_unsigned(int, *tl);
341 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
342 fxdr_nfsv3time(tl, &guard);
345 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
347 * Nah nah nah nah na nah
348 * There is a bug in the Sun client that puts 0xffff in the mode
349 * field of sattr when it should put in 0xffffffff. The u_short
350 * doesn't sign extend.
351 * --> check the low order 2 bytes for 0xffff
353 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
354 vap->va_mode = nfstov_mode(sp->sa_mode);
355 if (sp->sa_uid != nfs_xdrneg1)
356 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
357 if (sp->sa_gid != nfs_xdrneg1)
358 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
359 if (sp->sa_size != nfs_xdrneg1)
360 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
361 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
363 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
365 vap->va_atime.tv_sec =
366 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
367 vap->va_atime.tv_nsec = 0;
370 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
371 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
376 * Now that we have all the fields, lets do it.
378 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
379 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
381 nfsm_reply(2 * NFSX_UNSIGNED);
382 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
388 * vp now an active resource, pay careful attention to cleanup
391 nqsrv_getl(vp, ND_WRITE);
393 error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
394 if (!error && gcheck &&
395 (preat.va_ctime.tv_sec != guard.tv_sec ||
396 preat.va_ctime.tv_nsec != guard.tv_nsec))
397 error = NFSERR_NOT_SYNC;
401 nfsm_reply(NFSX_WCCDATA(v3));
402 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
409 * If the size is being changed write acces is required, otherwise
410 * just check for a read only file system.
412 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
413 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
418 if (vp->v_type == VDIR) {
421 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
425 error = VOP_SETATTR(vp, vap, cred, procp);
426 postat_ret = VOP_GETATTR(vp, vap, cred, procp);
432 nfsm_reply(NFSX_WCCORFATTR(v3));
434 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
438 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
439 nfsm_srvfillattr(vap, fp);
453 nfsrv_lookup(nfsd, slp, procp, mrq)
454 struct nfsrv_descript *nfsd;
455 struct nfssvc_sock *slp;
459 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
460 struct sockaddr *nam = nfsd->nd_nam;
461 caddr_t dpos = nfsd->nd_dpos;
462 struct ucred *cred = &nfsd->nd_cr;
463 register struct nfs_fattr *fp;
464 struct nameidata nd, ind, *ndp = &nd;
465 struct vnode *vp, *dirp = NULL;
469 register u_int32_t *tl;
472 int error = 0, cache, len, dirattr_ret = 1;
473 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
475 struct mbuf *mb, *mb2, *mreq;
476 struct vattr va, dirattr, *vap = &va;
479 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
482 fhp = &nfh.fh_generic;
484 nfsm_srvnamesiz(len);
486 pubflag = nfs_ispublicfh(fhp);
488 nd.ni_cnd.cn_cred = cred;
489 nd.ni_cnd.cn_nameiop = LOOKUP;
490 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
491 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
492 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
495 * namei failure, only dirp to cleanup. Clear out garbarge from
496 * structure in case macros jump to nfsmout.
502 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
507 nfsm_reply(NFSX_POSTOPATTR(v3));
508 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
514 * Locate index file for public filehandle
516 * error is 0 on entry and 0 on exit from this block.
520 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
522 * Setup call to lookup() to see if we can find
523 * the index file. Arguably, this doesn't belong
524 * in a kernel.. Ugh. If an error occurs, do not
525 * try to install an index file and then clear the
528 * When we replace nd with ind and redirect ndp,
529 * maintenance of ni_startdir and ni_vp shift to
530 * ind and we have to clean them up in the old nd.
531 * However, the cnd resource continues to be maintained
532 * via the original nd. Confused? You aren't alone!
535 VOP_UNLOCK(nd.ni_vp, 0, procp);
536 ind.ni_pathlen = strlen(nfs_pub.np_index);
537 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
539 ind.ni_startdir = nd.ni_vp;
540 VREF(ind.ni_startdir);
542 error = lookup(&ind);
547 * Found an index file. Get rid of
548 * the old references. transfer nd.ni_vp'
554 vrele(nd.ni_startdir);
555 nd.ni_startdir = NULL;
561 * If the public filehandle was used, check that this lookup
562 * didn't result in a filehandle outside the publicly exported
563 * filesystem. We clear the poor vp here to avoid lockups due
567 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
576 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
583 * Resources at this point:
584 * ndp->ni_vp may not be NULL
589 nfsm_reply(NFSX_POSTOPATTR(v3));
590 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
595 nqsrv_getl(ndp->ni_startdir, ND_READ);
598 * Clear out some resources prior to potentially blocking. This
599 * is not as critical as ni_dvp resources in other routines, but
602 vrele(ndp->ni_startdir);
603 ndp->ni_startdir = NULL;
604 NDFREE(&nd, NDF_ONLY_PNBUF);
607 * Get underlying attribute, then release remaining resources ( for
608 * the same potential blocking reason ) and reply.
611 bzero((caddr_t)fhp, sizeof(nfh));
612 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
613 error = VFS_VPTOFH(vp, &fhp->fh_fid);
615 error = VOP_GETATTR(vp, vap, cred, procp);
619 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
621 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
625 nfsm_srvfhtom(fhp, v3);
627 nfsm_srvpostop_attr(0, vap);
628 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
630 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
631 nfsm_srvfillattr(vap, fp);
637 NDFREE(&nd, NDF_ONLY_PNBUF);
638 if (ndp->ni_startdir)
639 vrele(ndp->ni_startdir);
646 * nfs readlink service
649 nfsrv_readlink(nfsd, slp, procp, mrq)
650 struct nfsrv_descript *nfsd;
651 struct nfssvc_sock *slp;
655 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
656 struct sockaddr *nam = nfsd->nd_nam;
657 caddr_t dpos = nfsd->nd_dpos;
658 struct ucred *cred = &nfsd->nd_cr;
659 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
660 register struct iovec *ivp = iv;
661 register struct mbuf *mp;
662 register u_int32_t *tl;
665 int error = 0, rdonly, cache, i, tlen, len, getret;
666 int v3 = (nfsd->nd_flag & ND_NFSV3);
668 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
669 struct vnode *vp = NULL;
673 struct uio io, *uiop = &io;
676 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
678 mp2 = (struct mbuf *)0;
681 fhp = &nfh.fh_generic;
685 while (len < NFS_MAXPATHLEN) {
686 MGET(mp, M_WAIT, MT_DATA);
688 mp->m_len = NFSMSIZ(mp);
695 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
696 mp->m_len = NFS_MAXPATHLEN-len;
697 len = NFS_MAXPATHLEN;
700 ivp->iov_base = mtod(mp, caddr_t);
701 ivp->iov_len = mp->m_len;
706 uiop->uio_iovcnt = i;
707 uiop->uio_offset = 0;
708 uiop->uio_resid = len;
709 uiop->uio_rw = UIO_READ;
710 uiop->uio_segflg = UIO_SYSSPACE;
711 uiop->uio_procp = (struct proc *)0;
712 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
713 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
715 nfsm_reply(2 * NFSX_UNSIGNED);
716 nfsm_srvpostop_attr(1, (struct vattr *)0);
720 if (vp->v_type != VLNK) {
727 nqsrv_getl(vp, ND_READ);
728 error = VOP_READLINK(vp, uiop, cred);
730 getret = VOP_GETATTR(vp, &attr, cred, procp);
733 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
735 nfsm_srvpostop_attr(getret, &attr);
741 if (uiop->uio_resid > 0) {
742 len -= uiop->uio_resid;
743 tlen = nfsm_rndup(len);
744 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
746 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
747 *tl = txdr_unsigned(len);
762 nfsrv_read(nfsd, slp, procp, mrq)
763 struct nfsrv_descript *nfsd;
764 struct nfssvc_sock *slp;
768 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
769 struct sockaddr *nam = nfsd->nd_nam;
770 caddr_t dpos = nfsd->nd_dpos;
771 struct ucred *cred = &nfsd->nd_cr;
772 register struct iovec *iv;
774 register struct mbuf *m;
775 register struct nfs_fattr *fp;
776 register u_int32_t *tl;
780 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
781 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
783 struct mbuf *mb, *mb2, *mreq;
785 struct vnode *vp = NULL;
788 struct uio io, *uiop = &io;
789 struct vattr va, *vap = &va;
795 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
796 fhp = &nfh.fh_generic;
799 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
800 off = fxdr_hyper(tl);
802 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
803 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
805 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
808 * Reference vp. If an error occurs, vp will be invalid, but we
809 * have to NULL it just in case. The macros might goto nfsmout
813 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
814 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
817 nfsm_reply(2 * NFSX_UNSIGNED);
818 nfsm_srvpostop_attr(1, (struct vattr *)0);
823 if (vp->v_type != VREG) {
827 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
830 nqsrv_getl(vp, ND_READ);
831 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
832 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
834 getret = VOP_GETATTR(vp, vap, cred, procp);
840 nfsm_reply(NFSX_POSTOPATTR(v3));
841 nfsm_srvpostop_attr(getret, vap);
847 * Calculate byte count to read
850 if (off >= vap->va_size)
852 else if ((off + reqlen) > vap->va_size)
853 cnt = vap->va_size - off;
858 * Calculate seqcount for heuristic
866 * Locate best candidate
869 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
873 if (nfsheur[hi].nh_vp == vp) {
877 if (nfsheur[hi].nh_use > 0)
878 --nfsheur[hi].nh_use;
879 hi = (hi + 1) & (NUM_HEURISTIC - 1);
880 if (nfsheur[hi].nh_use < nh->nh_use)
884 if (nh->nh_vp != vp) {
887 nh->nh_use = NHUSE_INIT;
895 * Calculate heuristic
898 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
899 if (++nh->nh_seqcount > IO_SEQMAX)
900 nh->nh_seqcount = IO_SEQMAX;
901 } else if (nh->nh_seqcount > 1) {
906 nh->nh_use += NHUSE_INC;
907 if (nh->nh_use > NHUSE_MAX)
908 nh->nh_use = NHUSE_MAX;
909 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
912 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
914 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
916 fp = (struct nfs_fattr *)tl;
917 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
919 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
920 fp = (struct nfs_fattr *)tl;
921 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
923 len = left = nfsm_rndup(cnt);
926 * Generate the mbuf list with the uio_iov ref. to it.
931 siz = min(M_TRAILINGSPACE(m), left);
937 MGET(m, M_WAIT, MT_DATA);
944 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
946 uiop->uio_iov = iv2 = iv;
952 panic("nfsrv_read iov");
953 siz = min(M_TRAILINGSPACE(m), left);
955 iv->iov_base = mtod(m, caddr_t) + m->m_len;
964 uiop->uio_iovcnt = i;
965 uiop->uio_offset = off;
966 uiop->uio_resid = len;
967 uiop->uio_rw = UIO_READ;
968 uiop->uio_segflg = UIO_SYSSPACE;
969 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
970 off = uiop->uio_offset;
972 FREE((caddr_t)iv2, M_TEMP);
973 if (error || (getret = VOP_GETATTR(vp, vap, cred, procp))) {
979 nfsm_reply(NFSX_POSTOPATTR(v3));
980 nfsm_srvpostop_attr(getret, vap);
989 nfsm_srvfillattr(vap, fp);
990 tlen = len - uiop->uio_resid;
991 cnt = cnt < tlen ? cnt : tlen;
992 tlen = nfsm_rndup(cnt);
993 if (len != tlen || tlen != cnt)
994 nfsm_adj(mb, len - tlen, tlen - cnt);
996 *tl++ = txdr_unsigned(cnt);
1002 *tl = txdr_unsigned(cnt);
1013 nfsrv_write(nfsd, slp, procp, mrq)
1014 struct nfsrv_descript *nfsd;
1015 struct nfssvc_sock *slp;
1019 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1020 struct sockaddr *nam = nfsd->nd_nam;
1021 caddr_t dpos = nfsd->nd_dpos;
1022 struct ucred *cred = &nfsd->nd_cr;
1023 register struct iovec *ivp;
1024 register int i, cnt;
1025 register struct mbuf *mp;
1026 register struct nfs_fattr *fp;
1028 struct vattr va, forat;
1029 register struct vattr *vap = &va;
1030 register u_int32_t *tl;
1031 register int32_t t1;
1033 int error = 0, rdonly, cache, len, forat_ret = 1;
1034 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1035 int stable = NFSV3WRITE_FILESYNC;
1036 int v3 = (nfsd->nd_flag & ND_NFSV3);
1038 struct mbuf *mb, *mb2, *mreq;
1039 struct vnode *vp = NULL;
1042 struct uio io, *uiop = &io;
1046 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1052 fhp = &nfh.fh_generic;
1055 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1056 off = fxdr_hyper(tl);
1058 stable = fxdr_unsigned(int, *tl++);
1060 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1061 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1064 stable = NFSV3WRITE_UNSTABLE;
1066 retlen = len = fxdr_unsigned(int32_t, *tl);
1070 * For NFS Version 2, it is not obvious what a write of zero length
1071 * should do, but I might as well be consistent with Version 3,
1072 * which is to return ok so long as there are no permission problems.
1080 adjust = dpos - mtod(mp, caddr_t);
1081 mp->m_len -= adjust;
1082 if (mp->m_len > 0 && adjust > 0)
1083 NFSMADV(mp, adjust);
1087 else if (mp->m_len > 0) {
1090 mp->m_len -= (i - len);
1099 if (len > NFS_MAXDATA || len < 0 || i < len) {
1101 nfsm_reply(2 * NFSX_UNSIGNED);
1102 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1106 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1107 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1110 nfsm_reply(2 * NFSX_UNSIGNED);
1111 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1116 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1117 if (vp->v_type != VREG) {
1121 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1124 nqsrv_getl(vp, ND_WRITE);
1125 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1130 nfsm_reply(NFSX_WCCDATA(v3));
1131 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1137 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1139 uiop->uio_iov = iv = ivp;
1140 uiop->uio_iovcnt = cnt;
1143 if (mp->m_len > 0) {
1144 ivp->iov_base = mtod(mp, caddr_t);
1145 ivp->iov_len = mp->m_len;
1153 * The IO_METASYNC flag indicates that all metadata (and not just
1154 * enough to ensure data integrity) mus be written to stable storage
1156 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1158 if (stable == NFSV3WRITE_UNSTABLE)
1159 ioflags = IO_NODELOCKED;
1160 else if (stable == NFSV3WRITE_DATASYNC)
1161 ioflags = (IO_SYNC | IO_NODELOCKED);
1163 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1164 uiop->uio_resid = len;
1165 uiop->uio_rw = UIO_WRITE;
1166 uiop->uio_segflg = UIO_SYSSPACE;
1167 uiop->uio_procp = (struct proc *)0;
1168 uiop->uio_offset = off;
1169 error = VOP_WRITE(vp, uiop, ioflags, cred);
1170 nfsstats.srvvop_writes++;
1171 FREE((caddr_t)iv, M_TEMP);
1173 aftat_ret = VOP_GETATTR(vp, vap, cred, procp);
1178 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1179 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1181 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1186 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1187 *tl++ = txdr_unsigned(retlen);
1189 * If nfs_async is set, then pretend the write was FILESYNC.
1191 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1192 *tl++ = txdr_unsigned(stable);
1194 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1196 * Actually, there is no need to txdr these fields,
1197 * but it may make the values more human readable,
1198 * for debugging purposes.
1200 if (nfsver.tv_sec == 0)
1202 *tl++ = txdr_unsigned(nfsver.tv_sec);
1203 *tl = txdr_unsigned(nfsver.tv_usec);
1205 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1206 nfsm_srvfillattr(vap, fp);
1215 * NFS write service with write gathering support. Called when
1216 * nfsrvw_procrastinate > 0.
1217 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1218 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1222 nfsrv_writegather(ndp, slp, procp, mrq)
1223 struct nfsrv_descript **ndp;
1224 struct nfssvc_sock *slp;
1228 register struct iovec *ivp;
1229 register struct mbuf *mp;
1230 register struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1231 register struct nfs_fattr *fp;
1234 struct nfsrvw_delayhash *wpp;
1236 struct vattr va, forat;
1237 register u_int32_t *tl;
1238 register int32_t t1;
1240 int error = 0, rdonly, cache, len, forat_ret = 1;
1241 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1243 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1244 struct vnode *vp = NULL;
1245 struct uio io, *uiop = &io;
1246 u_quad_t frev, cur_usec;
1248 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1257 mrep = nfsd->nd_mrep;
1259 dpos = nfsd->nd_dpos;
1260 cred = &nfsd->nd_cr;
1261 v3 = (nfsd->nd_flag & ND_NFSV3);
1262 LIST_INIT(&nfsd->nd_coalesce);
1263 nfsd->nd_mreq = NULL;
1264 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1265 cur_usec = nfs_curusec();
1266 nfsd->nd_time = cur_usec +
1267 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1270 * Now, get the write header..
1272 nfsm_srvmtofh(&nfsd->nd_fh);
1274 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1275 nfsd->nd_off = fxdr_hyper(tl);
1277 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1279 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1280 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1283 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1285 len = fxdr_unsigned(int32_t, *tl);
1287 nfsd->nd_eoff = nfsd->nd_off + len;
1290 * Trim the header out of the mbuf list and trim off any trailing
1291 * junk so that the mbuf list has only the write data.
1299 adjust = dpos - mtod(mp, caddr_t);
1300 mp->m_len -= adjust;
1301 if (mp->m_len > 0 && adjust > 0)
1302 NFSMADV(mp, adjust);
1309 mp->m_len -= (i - len);
1315 if (len > NFS_MAXDATA || len < 0 || i < len) {
1319 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1321 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1322 nfsd->nd_mreq = mreq;
1323 nfsd->nd_mrep = NULL;
1328 * Add this entry to the hash and time queues.
1332 wp = slp->ns_tq.lh_first;
1333 while (wp && wp->nd_time < nfsd->nd_time) {
1335 wp = wp->nd_tq.le_next;
1337 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1339 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1341 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1343 if (nfsd->nd_mrep) {
1344 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1348 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1350 wp = wp->nd_hash.le_next;
1352 while (wp && wp->nd_off < nfsd->nd_off &&
1353 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1355 wp = wp->nd_hash.le_next;
1358 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1361 * Search the hash list for overlapping entries and
1364 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1365 wp = nfsd->nd_hash.le_next;
1366 if (NFSW_SAMECRED(owp, nfsd))
1367 nfsrvw_coalesce(owp, nfsd);
1370 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1377 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1378 * and generate the associated reply mbuf list(s).
1381 cur_usec = nfs_curusec();
1383 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1384 owp = nfsd->nd_tq.le_next;
1385 if (nfsd->nd_time > cur_usec)
1389 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1390 LIST_REMOVE(nfsd, nd_tq);
1391 LIST_REMOVE(nfsd, nd_hash);
1393 mrep = nfsd->nd_mrep;
1394 nfsd->nd_mrep = NULL;
1395 cred = &nfsd->nd_cr;
1396 v3 = (nfsd->nd_flag & ND_NFSV3);
1397 forat_ret = aftat_ret = 1;
1398 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1399 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1402 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1403 if (vp->v_type != VREG) {
1407 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1413 nqsrv_getl(vp, ND_WRITE);
1414 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1417 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1418 ioflags = IO_NODELOCKED;
1419 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1420 ioflags = (IO_SYNC | IO_NODELOCKED);
1422 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1423 uiop->uio_rw = UIO_WRITE;
1424 uiop->uio_segflg = UIO_SYSSPACE;
1425 uiop->uio_procp = (struct proc *)0;
1426 uiop->uio_offset = nfsd->nd_off;
1427 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1428 if (uiop->uio_resid > 0) {
1436 uiop->uio_iovcnt = i;
1437 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1439 uiop->uio_iov = ivp = iov;
1442 if (mp->m_len > 0) {
1443 ivp->iov_base = mtod(mp, caddr_t);
1444 ivp->iov_len = mp->m_len;
1450 error = VOP_WRITE(vp, uiop, ioflags, cred);
1451 nfsstats.srvvop_writes++;
1453 FREE((caddr_t)iov, M_TEMP);
1457 aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
1463 * Loop around generating replies for all write rpcs that have
1464 * now been completed.
1468 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1470 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1472 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1475 nfsm_writereply(NFSX_PREOPATTR(v3) +
1476 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1477 NFSX_WRITEVERF(v3), v3);
1479 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1480 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1481 *tl++ = txdr_unsigned(nfsd->nd_len);
1482 *tl++ = txdr_unsigned(swp->nd_stable);
1484 * Actually, there is no need to txdr these fields,
1485 * but it may make the values more human readable,
1486 * for debugging purposes.
1488 if (nfsver.tv_sec == 0)
1490 *tl++ = txdr_unsigned(nfsver.tv_sec);
1491 *tl = txdr_unsigned(nfsver.tv_usec);
1493 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1494 nfsm_srvfillattr(&va, fp);
1497 nfsd->nd_mreq = mreq;
1499 panic("nfsrv_write: nd_mrep not free");
1502 * Done. Put it at the head of the timer queue so that
1503 * the final phase can return the reply.
1508 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1510 nfsd = swp->nd_coalesce.lh_first;
1512 LIST_REMOVE(nfsd, nd_tq);
1518 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1525 * Search for a reply to return.
1528 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1529 if (nfsd->nd_mreq) {
1530 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1531 LIST_REMOVE(nfsd, nd_tq);
1532 *mrq = nfsd->nd_mreq;
1541 * Coalesce the write request nfsd into owp. To do this we must:
1542 * - remove nfsd from the queues
1543 * - merge nfsd->nd_mrep into owp->nd_mrep
1544 * - update the nd_eoff and nd_stable for owp
1545 * - put nfsd on owp's nd_coalesce list
1546 * NB: Must be called at splsoftclock().
1549 nfsrvw_coalesce(owp, nfsd)
1550 register struct nfsrv_descript *owp;
1551 register struct nfsrv_descript *nfsd;
1553 register int overlap;
1554 register struct mbuf *mp;
1555 struct nfsrv_descript *p;
1557 NFS_DPF(WG, ("C%03x-%03x",
1558 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1559 LIST_REMOVE(nfsd, nd_hash);
1560 LIST_REMOVE(nfsd, nd_tq);
1561 if (owp->nd_eoff < nfsd->nd_eoff) {
1562 overlap = owp->nd_eoff - nfsd->nd_off;
1564 panic("nfsrv_coalesce: bad off");
1566 m_adj(nfsd->nd_mrep, overlap);
1570 mp->m_next = nfsd->nd_mrep;
1571 owp->nd_eoff = nfsd->nd_eoff;
1573 m_freem(nfsd->nd_mrep);
1574 nfsd->nd_mrep = NULL;
1575 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1576 owp->nd_stable = NFSV3WRITE_FILESYNC;
1577 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1578 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1579 owp->nd_stable = NFSV3WRITE_DATASYNC;
1580 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1583 * If nfsd had anything else coalesced into it, transfer them
1584 * to owp, otherwise their replies will never get sent.
1586 for (p = nfsd->nd_coalesce.lh_first; p;
1587 p = nfsd->nd_coalesce.lh_first) {
1588 LIST_REMOVE(p, nd_tq);
1589 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1594 * nfs create service
1595 * now does a truncate to 0 length via. setattr if it already exists
1598 nfsrv_create(nfsd, slp, procp, mrq)
1599 struct nfsrv_descript *nfsd;
1600 struct nfssvc_sock *slp;
1604 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1605 struct sockaddr *nam = nfsd->nd_nam;
1606 caddr_t dpos = nfsd->nd_dpos;
1607 struct ucred *cred = &nfsd->nd_cr;
1608 register struct nfs_fattr *fp;
1609 struct vattr va, dirfor, diraft;
1610 register struct vattr *vap = &va;
1611 register struct nfsv2_sattr *sp;
1612 register u_int32_t *tl;
1613 struct nameidata nd;
1614 register int32_t t1;
1616 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1617 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1620 struct mbuf *mb, *mb2, *mreq;
1621 struct vnode *dirp = (struct vnode *)0;
1624 u_quad_t frev, tempsize;
1625 u_char cverf[NFSX_V3CREATEVERF];
1627 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1633 fhp = &nfh.fh_generic;
1635 nfsm_srvnamesiz(len);
1637 nd.ni_cnd.cn_cred = cred;
1638 nd.ni_cnd.cn_nameiop = CREATE;
1639 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1642 * Call namei and do initial cleanup to get a few things
1643 * out of the way. If we get an initial error we cleanup
1644 * and return here to avoid special-casing the invalid nd
1645 * structure through the rest of the case. dirp may be
1646 * set even if an error occurs, but the nd structure will not
1647 * be valid at all if an error occurs so we have to invalidate it
1648 * prior to calling nfsm_reply ( which might goto nfsmout ).
1650 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1651 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1654 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1662 nfsm_reply(NFSX_WCCDATA(v3));
1663 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1669 * No error. Continue. State:
1671 * startdir is valid ( we release this immediately )
1673 * nd.ni_vp may be valid
1674 * nd.ni_dvp is valid
1676 * The error state is set through the code and we may also do some
1677 * opportunistic releasing of vnodes to avoid holding locks through
1678 * NFS I/O. The cleanup at the end is a catch-all
1683 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1684 how = fxdr_unsigned(int, *tl);
1686 case NFSV3CREATE_GUARDED:
1692 case NFSV3CREATE_UNCHECKED:
1695 case NFSV3CREATE_EXCLUSIVE:
1696 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1697 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1701 vap->va_type = VREG;
1703 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1704 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1705 if (vap->va_type == VNON)
1706 vap->va_type = VREG;
1707 vap->va_mode = nfstov_mode(sp->sa_mode);
1708 switch (vap->va_type) {
1710 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1712 vap->va_size = (u_quad_t)tsize;
1717 rdev = fxdr_unsigned(long, sp->sa_size);
1725 * Iff doesn't exist, create it
1726 * otherwise just truncate to 0 length
1727 * should I set the mode too ?
1729 * The only possible error we can have at this point is EEXIST.
1730 * nd.ni_vp will also be non-NULL in that case.
1732 if (nd.ni_vp == NULL) {
1733 if (vap->va_mode == (mode_t)VNOVAL)
1735 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1736 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1737 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1739 NDFREE(&nd, NDF_ONLY_PNBUF);
1741 nfsrv_object_create(nd.ni_vp);
1742 if (exclusive_flag) {
1745 bcopy(cverf, (caddr_t)&vap->va_atime,
1747 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1752 vap->va_type == VCHR ||
1753 vap->va_type == VBLK ||
1754 vap->va_type == VFIFO
1757 * Handle SysV FIFO node special cases. All other
1758 * devices require super user to access.
1760 if (vap->va_type == VCHR && rdev == 0xffffffff)
1761 vap->va_type = VFIFO;
1762 if (vap->va_type != VFIFO &&
1763 (error = suser_xxx(cred, 0))) {
1766 vap->va_rdev = rdev;
1767 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1769 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1771 NDFREE(&nd, NDF_ONLY_PNBUF);
1778 * release dvp prior to lookup
1786 * Even though LOCKPARENT was cleared, ni_dvp may
1789 nd.ni_cnd.cn_nameiop = LOOKUP;
1790 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1791 nd.ni_cnd.cn_proc = procp;
1792 nd.ni_cnd.cn_cred = cred;
1794 error = lookup(&nd);
1799 /* fall through on certain errors */
1801 nfsrv_object_create(nd.ni_vp);
1802 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1810 if (vap->va_size != -1) {
1811 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1812 (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
1814 nqsrv_getl(nd.ni_vp, ND_WRITE);
1815 tempsize = vap->va_size;
1817 vap->va_size = tempsize;
1818 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1825 bzero((caddr_t)fhp, sizeof(nfh));
1826 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1827 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1829 error = VOP_GETATTR(nd.ni_vp, vap, cred, procp);
1832 if (exclusive_flag && !error &&
1833 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1835 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
1839 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1842 nfsm_srvpostop_fh(fhp);
1843 nfsm_srvpostop_attr(0, vap);
1845 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1848 nfsm_srvfhtom(fhp, v3);
1849 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1850 nfsm_srvfillattr(vap, fp);
1860 if (nd.ni_startdir) {
1861 vrele(nd.ni_startdir);
1862 nd.ni_startdir = NULL;
1866 NDFREE(&nd, NDF_ONLY_PNBUF);
1868 if (nd.ni_dvp == nd.ni_vp)
1879 * nfs v3 mknod service
1882 nfsrv_mknod(nfsd, slp, procp, mrq)
1883 struct nfsrv_descript *nfsd;
1884 struct nfssvc_sock *slp;
1888 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1889 struct sockaddr *nam = nfsd->nd_nam;
1890 caddr_t dpos = nfsd->nd_dpos;
1891 struct ucred *cred = &nfsd->nd_cr;
1892 struct vattr va, dirfor, diraft;
1893 register struct vattr *vap = &va;
1894 register u_int32_t *tl;
1895 struct nameidata nd;
1896 register int32_t t1;
1898 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1899 u_int32_t major, minor;
1902 struct mbuf *mb, *mb2, *mreq;
1903 struct vnode *vp, *dirp = (struct vnode *)0;
1908 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1911 fhp = &nfh.fh_generic;
1913 nfsm_srvnamesiz(len);
1915 nd.ni_cnd.cn_cred = cred;
1916 nd.ni_cnd.cn_nameiop = CREATE;
1917 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1920 * Handle nfs_namei() call. If an error occurs, the nd structure
1921 * is not valid. However, nfsm_*() routines may still jump to
1925 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1926 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1928 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
1930 nfsm_reply(NFSX_WCCDATA(1));
1931 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1935 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1936 vtyp = nfsv3tov_type(*tl);
1937 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1938 error = NFSERR_BADTYPE;
1943 if (vtyp == VCHR || vtyp == VBLK) {
1944 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1945 major = fxdr_unsigned(u_int32_t, *tl++);
1946 minor = fxdr_unsigned(u_int32_t, *tl);
1947 vap->va_rdev = makeudev(major, minor);
1951 * Iff doesn't exist, create it.
1957 vap->va_type = vtyp;
1958 if (vap->va_mode == (mode_t)VNOVAL)
1960 if (vtyp == VSOCK) {
1961 vrele(nd.ni_startdir);
1962 nd.ni_startdir = NULL;
1963 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1964 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1966 NDFREE(&nd, NDF_ONLY_PNBUF);
1968 if (vtyp != VFIFO && (error = suser_xxx(cred, 0)))
1970 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1972 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1974 NDFREE(&nd, NDF_ONLY_PNBUF);
1981 * Release dvp prior to lookup
1986 nd.ni_cnd.cn_nameiop = LOOKUP;
1987 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1988 nd.ni_cnd.cn_proc = procp;
1989 nd.ni_cnd.cn_cred = procp->p_ucred;
1991 error = lookup(&nd);
1996 if (nd.ni_cnd.cn_flags & ISSYMLINK)
2001 * send response, cleanup, return.
2004 if (nd.ni_startdir) {
2005 vrele(nd.ni_startdir);
2006 nd.ni_startdir = NULL;
2008 NDFREE(&nd, NDF_ONLY_PNBUF);
2010 if (nd.ni_dvp == nd.ni_vp)
2018 bzero((caddr_t)fhp, sizeof(nfh));
2019 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2020 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2022 error = VOP_GETATTR(vp, vap, cred, procp);
2029 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2034 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2036 nfsm_srvpostop_fh(fhp);
2037 nfsm_srvpostop_attr(0, vap);
2039 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2045 vrele(nd.ni_startdir);
2046 NDFREE(&nd, NDF_ONLY_PNBUF);
2048 if (nd.ni_dvp == nd.ni_vp)
2059 * nfs remove service
2062 nfsrv_remove(nfsd, slp, procp, mrq)
2063 struct nfsrv_descript *nfsd;
2064 struct nfssvc_sock *slp;
2068 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2069 struct sockaddr *nam = nfsd->nd_nam;
2070 caddr_t dpos = nfsd->nd_dpos;
2071 struct ucred *cred = &nfsd->nd_cr;
2072 struct nameidata nd;
2073 register u_int32_t *tl;
2074 register int32_t t1;
2076 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2077 int v3 = (nfsd->nd_flag & ND_NFSV3);
2079 struct mbuf *mb, *mreq;
2081 struct vattr dirfor, diraft;
2086 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2089 fhp = &nfh.fh_generic;
2091 nfsm_srvnamesiz(len);
2093 nd.ni_cnd.cn_cred = cred;
2094 nd.ni_cnd.cn_nameiop = DELETE;
2095 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2096 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2097 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2100 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2108 if (nd.ni_vp->v_type == VDIR) {
2109 error = EPERM; /* POSIX */
2113 * The root of a mounted filesystem cannot be deleted.
2115 if (nd.ni_vp->v_flag & VROOT) {
2121 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2122 nqsrv_getl(nd.ni_vp, ND_WRITE);
2123 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2124 NDFREE(&nd, NDF_ONLY_PNBUF);
2128 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2132 nfsm_reply(NFSX_WCCDATA(v3));
2134 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2138 NDFREE(&nd, NDF_ONLY_PNBUF);
2140 if (nd.ni_dvp == nd.ni_vp)
2151 * nfs rename service
2154 nfsrv_rename(nfsd, slp, procp, mrq)
2155 struct nfsrv_descript *nfsd;
2156 struct nfssvc_sock *slp;
2160 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2161 struct sockaddr *nam = nfsd->nd_nam;
2162 caddr_t dpos = nfsd->nd_dpos;
2163 struct ucred *cred = &nfsd->nd_cr;
2164 register u_int32_t *tl;
2165 register int32_t t1;
2167 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2168 int tdirfor_ret = 1, tdiraft_ret = 1;
2169 int v3 = (nfsd->nd_flag & ND_NFSV3);
2171 struct mbuf *mb, *mreq;
2172 struct nameidata fromnd, tond;
2173 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2174 struct vnode *tdirp = (struct vnode *)0;
2175 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2177 fhandle_t *ffhp, *tfhp;
2181 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2183 fvp = (struct vnode *)0;
2185 ffhp = &fnfh.fh_generic;
2186 tfhp = &tnfh.fh_generic;
2189 * Clear fields incase goto nfsmout occurs from macro.
2195 nfsm_srvmtofh(ffhp);
2196 nfsm_srvnamesiz(len);
2198 * Remember our original uid so that we can reset cr_uid before
2199 * the second nfs_namei() call, in case it is remapped.
2201 saved_uid = cred->cr_uid;
2202 fromnd.ni_cnd.cn_cred = cred;
2203 fromnd.ni_cnd.cn_nameiop = DELETE;
2204 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2205 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2206 &dpos, &fdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2209 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2217 nfsm_reply(2 * NFSX_WCCDATA(v3));
2218 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2219 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, procp, (nfsd->nd_flag & ND_KERBAUTH), 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 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2306 nqsrv_getl(tdvp, ND_WRITE);
2308 nqsrv_getl(tvp, ND_WRITE);
2310 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2311 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2312 fromnd.ni_dvp = NULL;
2313 fromnd.ni_vp = NULL;
2317 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2318 tond.ni_cnd.cn_flags &= ~HASBUF;
2328 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
2330 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
2331 nfsm_reply(2 * NFSX_WCCDATA(v3));
2333 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2334 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2341 * Clear out tond related fields
2345 if (tond.ni_startdir)
2346 vrele(tond.ni_startdir);
2347 NDFREE(&tond, NDF_ONLY_PNBUF);
2349 if (tond.ni_dvp == tond.ni_vp)
2358 * Clear out fromnd related fields
2362 if (fromnd.ni_startdir)
2363 vrele(fromnd.ni_startdir);
2364 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2366 vrele(fromnd.ni_dvp);
2368 vrele(fromnd.ni_vp);
2377 nfsrv_link(nfsd, slp, procp, mrq)
2378 struct nfsrv_descript *nfsd;
2379 struct nfssvc_sock *slp;
2383 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2384 struct sockaddr *nam = nfsd->nd_nam;
2385 caddr_t dpos = nfsd->nd_dpos;
2386 struct ucred *cred = &nfsd->nd_cr;
2387 struct nameidata nd;
2388 register u_int32_t *tl;
2389 register int32_t t1;
2391 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2392 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2394 struct mbuf *mb, *mreq;
2395 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2396 struct vattr dirfor, diraft, at;
2398 fhandle_t *fhp, *dfhp;
2401 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2404 fhp = &nfh.fh_generic;
2405 dfhp = &dnfh.fh_generic;
2407 nfsm_srvmtofh(dfhp);
2408 nfsm_srvnamesiz(len);
2410 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2411 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2413 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2414 nfsm_srvpostop_attr(getret, &at);
2415 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2420 if (vp->v_type == VDIR) {
2421 error = EPERM; /* POSIX */
2424 nd.ni_cnd.cn_cred = cred;
2425 nd.ni_cnd.cn_nameiop = CREATE;
2426 nd.ni_cnd.cn_flags = LOCKPARENT;
2427 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2428 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2431 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2447 if (vp->v_mount != xp->v_mount)
2451 nqsrv_getl(vp, ND_WRITE);
2452 nqsrv_getl(xp, ND_WRITE);
2453 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2454 NDFREE(&nd, NDF_ONLY_PNBUF);
2460 getret = VOP_GETATTR(vp, &at, cred, procp);
2462 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
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)
2489 * nfs symbolic link service
2492 nfsrv_symlink(nfsd, slp, procp, mrq)
2493 struct nfsrv_descript *nfsd;
2494 struct nfssvc_sock *slp;
2498 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2499 struct sockaddr *nam = nfsd->nd_nam;
2500 caddr_t dpos = nfsd->nd_dpos;
2501 struct ucred *cred = &nfsd->nd_cr;
2502 struct vattr va, dirfor, diraft;
2503 struct nameidata nd;
2504 register struct vattr *vap = &va;
2505 register u_int32_t *tl;
2506 register int32_t t1;
2507 struct nfsv2_sattr *sp;
2508 char *bpos, *pathcp = (char *)0, *cp2;
2511 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2512 int v3 = (nfsd->nd_flag & ND_NFSV3);
2513 struct mbuf *mb, *mreq, *mb2;
2514 struct vnode *dirp = (struct vnode *)0;
2519 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2522 fhp = &nfh.fh_generic;
2524 nfsm_srvnamesiz(len);
2525 nd.ni_cnd.cn_cred = cred;
2526 nd.ni_cnd.cn_nameiop = CREATE;
2527 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2528 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2529 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2532 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2545 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2546 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2547 iv.iov_base = pathcp;
2549 io.uio_resid = len2;
2553 io.uio_segflg = UIO_SYSSPACE;
2554 io.uio_rw = UIO_READ;
2555 io.uio_procp = (struct proc *)0;
2556 nfsm_mtouio(&io, len2);
2558 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2559 vap->va_mode = nfstov_mode(sp->sa_mode);
2561 *(pathcp + len2) = '\0';
2568 * issue symlink op. SAVESTART is set so the underlying path component
2569 * is only freed by the VOP if an error occurs.
2571 if (vap->va_mode == (mode_t)VNOVAL)
2573 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2574 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2576 NDFREE(&nd, NDF_ONLY_PNBUF);
2581 * releases directory prior to potential lookup op.
2589 * Issue lookup. Leave SAVESTART set so we can easily free
2590 * the name buffer later on.
2592 * since LOCKPARENT is not set, ni_dvp will be garbage on
2593 * return whether an error occurs or not.
2595 nd.ni_cnd.cn_nameiop = LOOKUP;
2596 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2597 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2598 nd.ni_cnd.cn_proc = procp;
2599 nd.ni_cnd.cn_cred = cred;
2601 error = lookup(&nd);
2605 bzero((caddr_t)fhp, sizeof(nfh));
2606 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2607 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2609 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2618 * These releases aren't strictly required, does even doing them
2619 * make any sense? XXX can nfsm_reply() block?
2622 FREE(pathcp, M_TEMP);
2626 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2630 if (nd.ni_startdir) {
2631 vrele(nd.ni_startdir);
2632 nd.ni_startdir = NULL;
2634 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2637 nfsm_srvpostop_fh(fhp);
2638 nfsm_srvpostop_attr(0, vap);
2640 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2646 NDFREE(&nd, NDF_ONLY_PNBUF);
2648 if (nd.ni_dvp == nd.ni_vp)
2656 vrele(nd.ni_startdir);
2660 FREE(pathcp, M_TEMP);
2669 nfsrv_mkdir(nfsd, slp, procp, mrq)
2670 struct nfsrv_descript *nfsd;
2671 struct nfssvc_sock *slp;
2675 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2676 struct sockaddr *nam = nfsd->nd_nam;
2677 caddr_t dpos = nfsd->nd_dpos;
2678 struct ucred *cred = &nfsd->nd_cr;
2679 struct vattr va, dirfor, diraft;
2680 register struct vattr *vap = &va;
2681 register struct nfs_fattr *fp;
2682 struct nameidata nd;
2683 register caddr_t cp;
2684 register u_int32_t *tl;
2685 register int32_t t1;
2687 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2688 int v3 = (nfsd->nd_flag & ND_NFSV3);
2690 struct mbuf *mb, *mb2, *mreq;
2691 struct vnode *dirp = NULL;
2697 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2700 fhp = &nfh.fh_generic;
2702 nfsm_srvnamesiz(len);
2703 nd.ni_cnd.cn_cred = cred;
2704 nd.ni_cnd.cn_nameiop = CREATE;
2705 nd.ni_cnd.cn_flags = LOCKPARENT;
2707 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2708 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2711 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2719 nfsm_reply(NFSX_WCCDATA(v3));
2720 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2728 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2729 vap->va_mode = nfstov_mode(*tl++);
2733 * At this point nd.ni_dvp is referenced and exclusively locked and
2734 * nd.ni_vp, if it exists, is referenced but not locked.
2737 vap->va_type = VDIR;
2738 if (nd.ni_vp != NULL) {
2739 NDFREE(&nd, NDF_ONLY_PNBUF);
2745 * Issue mkdir op. Since SAVESTART is not set, the pathname
2746 * component is freed by the VOP call. This will fill-in
2747 * nd.ni_vp, reference, and exclusively lock it.
2749 if (vap->va_mode == (mode_t)VNOVAL)
2751 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2752 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2753 NDFREE(&nd, NDF_ONLY_PNBUF);
2760 bzero((caddr_t)fhp, sizeof(nfh));
2761 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2762 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2764 error = VOP_GETATTR(nd.ni_vp, vap, cred, procp);
2768 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2769 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2772 nfsm_srvpostop_fh(fhp);
2773 nfsm_srvpostop_attr(0, vap);
2775 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2777 nfsm_srvfhtom(fhp, v3);
2778 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2779 nfsm_srvfillattr(vap, fp);
2788 NDFREE(&nd, NDF_ONLY_PNBUF);
2789 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2807 nfsrv_rmdir(nfsd, slp, procp, mrq)
2808 struct nfsrv_descript *nfsd;
2809 struct nfssvc_sock *slp;
2813 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2814 struct sockaddr *nam = nfsd->nd_nam;
2815 caddr_t dpos = nfsd->nd_dpos;
2816 struct ucred *cred = &nfsd->nd_cr;
2817 register u_int32_t *tl;
2818 register int32_t t1;
2820 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2821 int v3 = (nfsd->nd_flag & ND_NFSV3);
2823 struct mbuf *mb, *mreq;
2824 struct vnode *vp, *dirp = (struct vnode *)0;
2825 struct vattr dirfor, diraft;
2828 struct nameidata nd;
2831 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2834 fhp = &nfh.fh_generic;
2836 nfsm_srvnamesiz(len);
2837 nd.ni_cnd.cn_cred = cred;
2838 nd.ni_cnd.cn_nameiop = DELETE;
2839 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2840 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2841 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2844 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2852 nfsm_reply(NFSX_WCCDATA(v3));
2853 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2858 if (vp->v_type != VDIR) {
2863 * No rmdir "." please.
2865 if (nd.ni_dvp == vp) {
2870 * The root of a mounted filesystem cannot be deleted.
2872 if (vp->v_flag & VROOT)
2876 * Issue or abort op. Since SAVESTART is not set, path name
2877 * component is freed by the VOP after either.
2880 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2881 nqsrv_getl(vp, ND_WRITE);
2882 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2884 NDFREE(&nd, NDF_ONLY_PNBUF);
2887 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2888 nfsm_reply(NFSX_WCCDATA(v3));
2890 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2896 NDFREE(&nd, NDF_ONLY_PNBUF);
2900 if (nd.ni_dvp == nd.ni_vp)
2912 * nfs readdir service
2913 * - mallocs what it thinks is enough to read
2914 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2915 * - calls VOP_READDIR()
2916 * - loops around building the reply
2917 * if the output generated exceeds count break out of loop
2918 * The nfsm_clget macro is used here so that the reply will be packed
2919 * tightly in mbuf clusters.
2920 * - it only knows that it has encountered eof when the VOP_READDIR()
2922 * - as such one readdir rpc will return eof false although you are there
2923 * and then the next will return eof
2924 * - it trims out records with d_fileno == 0
2925 * this doesn't matter for Unix clients, but they might confuse clients
2927 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2928 * than requested, but this may not apply to all filesystems. For
2929 * example, client NFS does not { although it is never remote mounted
2931 * The alternate call nfsrv_readdirplus() does lookups as well.
2932 * PS: The NFS protocol spec. does not clarify what the "count" byte
2933 * argument is a count of.. just name strings and file id's or the
2934 * entire reply rpc or ...
2935 * I tried just file name and id sizes and it confused the Sun client,
2936 * so I am using the full rpc size now. The "paranoia.." comment refers
2937 * to including the status longwords that are not a part of the dir.
2938 * "entry" structures, but are in the rpc.
2942 u_int32_t fl_postopok;
2943 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2945 u_int32_t fl_fhsize;
2946 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2950 nfsrv_readdir(nfsd, slp, procp, mrq)
2951 struct nfsrv_descript *nfsd;
2952 struct nfssvc_sock *slp;
2956 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2957 struct sockaddr *nam = nfsd->nd_nam;
2958 caddr_t dpos = nfsd->nd_dpos;
2959 struct ucred *cred = &nfsd->nd_cr;
2960 register char *bp, *be;
2961 register struct mbuf *mp;
2962 register struct dirent *dp;
2963 register caddr_t cp;
2964 register u_int32_t *tl;
2965 register int32_t t1;
2967 struct mbuf *mb, *mb2, *mreq, *mp2;
2968 char *cpos, *cend, *cp2, *rbuf;
2969 struct vnode *vp = NULL;
2975 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2976 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2977 int v3 = (nfsd->nd_flag & ND_NFSV3);
2978 u_quad_t frev, off, toff, verf;
2979 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2981 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2982 fhp = &nfh.fh_generic;
2985 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2986 toff = fxdr_hyper(tl);
2988 verf = fxdr_hyper(tl);
2991 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2992 toff = fxdr_unsigned(u_quad_t, *tl++);
2993 verf = 0; /* shut up gcc */
2996 cnt = fxdr_unsigned(int, *tl);
2997 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2998 xfer = NFS_SRVMAXDATA(nfsd);
3004 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3005 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3006 if (!error && vp->v_type != VDIR) {
3012 nfsm_reply(NFSX_UNSIGNED);
3013 nfsm_srvpostop_attr(getret, &at);
3019 * Obtain lock on vnode for this section of the code
3022 nqsrv_getl(vp, ND_READ);
3024 error = getret = VOP_GETATTR(vp, &at, cred, procp);
3027 * XXX This check may be too strict for Solaris 2.5 clients.
3029 if (!error && toff && verf && verf != at.va_filerev)
3030 error = NFSERR_BAD_COOKIE;
3034 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
3038 nfsm_reply(NFSX_POSTOPATTR(v3));
3039 nfsm_srvpostop_attr(getret, &at);
3043 VOP_UNLOCK(vp, 0, procp);
3046 * end section. Allocate rbuf and continue
3048 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3051 iv.iov_len = fullsiz;
3054 io.uio_offset = (off_t)off;
3055 io.uio_resid = fullsiz;
3056 io.uio_segflg = UIO_SYSSPACE;
3057 io.uio_rw = UIO_READ;
3058 io.uio_procp = (struct proc *)0;
3060 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
3062 free((caddr_t)cookies, M_TEMP);
3065 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3066 off = (off_t)io.uio_offset;
3067 if (!cookies && !error)
3068 error = NFSERR_PERM;
3070 getret = VOP_GETATTR(vp, &at, cred, procp);
3074 VOP_UNLOCK(vp, 0, procp);
3078 free((caddr_t)rbuf, M_TEMP);
3080 free((caddr_t)cookies, M_TEMP);
3081 nfsm_reply(NFSX_POSTOPATTR(v3));
3082 nfsm_srvpostop_attr(getret, &at);
3087 siz -= io.uio_resid;
3090 * If nothing read, return eof
3096 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3099 nfsm_srvpostop_attr(getret, &at);
3100 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3101 txdr_hyper(at.va_filerev, tl);
3104 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3107 FREE((caddr_t)rbuf, M_TEMP);
3108 FREE((caddr_t)cookies, M_TEMP);
3115 * Check for degenerate cases of nothing useful read.
3116 * If so go try again
3120 dp = (struct dirent *)cpos;
3123 * For some reason FreeBSD's ufs_readdir() chooses to back the
3124 * directory offset up to a block boundary, so it is necessary to
3125 * skip over the records that preceed the requested offset. This
3126 * requires the assumption that file offset cookies monotonically
3129 while (cpos < cend && ncookies > 0 &&
3130 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3131 ((u_quad_t)(*cookiep)) <= toff)) {
3132 cpos += dp->d_reclen;
3133 dp = (struct dirent *)cpos;
3137 if (cpos >= cend || ncookies == 0) {
3143 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3144 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3146 nfsm_srvpostop_attr(getret, &at);
3147 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3148 txdr_hyper(at.va_filerev, tl);
3152 be = bp + M_TRAILINGSPACE(mp);
3154 /* Loop through the records and build reply */
3155 while (cpos < cend && ncookies > 0) {
3156 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3157 nlen = dp->d_namlen;
3158 rem = nfsm_rndup(nlen) - nlen;
3159 len += (4 * NFSX_UNSIGNED + nlen + rem);
3161 len += 2 * NFSX_UNSIGNED;
3167 * Build the directory record xdr from
3172 bp += NFSX_UNSIGNED;
3176 bp += NFSX_UNSIGNED;
3179 *tl = txdr_unsigned(dp->d_fileno);
3180 bp += NFSX_UNSIGNED;
3182 *tl = txdr_unsigned(nlen);
3183 bp += NFSX_UNSIGNED;
3185 /* And loop around copying the name */
3194 bcopy(cp, bp, tsiz);
3200 /* And null pad to a int32_t boundary */
3201 for (i = 0; i < rem; i++)
3205 /* Finish off the record */
3208 bp += NFSX_UNSIGNED;
3211 *tl = txdr_unsigned(*cookiep);
3212 bp += NFSX_UNSIGNED;
3214 cpos += dp->d_reclen;
3215 dp = (struct dirent *)cpos;
3223 bp += NFSX_UNSIGNED;
3229 bp += NFSX_UNSIGNED;
3232 mp->m_len = bp - mtod(mp, caddr_t);
3234 mp->m_len += bp - bpos;
3235 FREE((caddr_t)rbuf, M_TEMP);
3236 FREE((caddr_t)cookies, M_TEMP);
3245 nfsrv_readdirplus(nfsd, slp, procp, mrq)
3246 struct nfsrv_descript *nfsd;
3247 struct nfssvc_sock *slp;
3251 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3252 struct sockaddr *nam = nfsd->nd_nam;
3253 caddr_t dpos = nfsd->nd_dpos;
3254 struct ucred *cred = &nfsd->nd_cr;
3255 register char *bp, *be;
3256 register struct mbuf *mp;
3257 register struct dirent *dp;
3258 register caddr_t cp;
3259 register u_int32_t *tl;
3260 register int32_t t1;
3262 struct mbuf *mb, *mb2, *mreq, *mp2;
3263 char *cpos, *cend, *cp2, *rbuf;
3264 struct vnode *vp = NULL, *nvp;
3267 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3270 struct vattr va, at, *vap = &va;
3271 struct nfs_fattr *fp;
3272 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3273 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3274 u_quad_t frev, off, toff, verf;
3275 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3277 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3278 fhp = &nfh.fh_generic;
3280 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3281 toff = fxdr_hyper(tl);
3283 verf = fxdr_hyper(tl);
3285 siz = fxdr_unsigned(int, *tl++);
3286 cnt = fxdr_unsigned(int, *tl);
3288 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3289 xfer = NFS_SRVMAXDATA(nfsd);
3295 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3296 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3297 if (!error && vp->v_type != VDIR) {
3303 nfsm_reply(NFSX_UNSIGNED);
3304 nfsm_srvpostop_attr(getret, &at);
3308 error = getret = VOP_GETATTR(vp, &at, cred, procp);
3311 * XXX This check may be too strict for Solaris 2.5 clients.
3313 if (!error && toff && verf && verf != at.va_filerev)
3314 error = NFSERR_BAD_COOKIE;
3317 nqsrv_getl(vp, ND_READ);
3318 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
3323 nfsm_reply(NFSX_V3POSTOPATTR);
3324 nfsm_srvpostop_attr(getret, &at);
3328 VOP_UNLOCK(vp, 0, procp);
3329 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3332 iv.iov_len = fullsiz;
3335 io.uio_offset = (off_t)off;
3336 io.uio_resid = fullsiz;
3337 io.uio_segflg = UIO_SYSSPACE;
3338 io.uio_rw = UIO_READ;
3339 io.uio_procp = (struct proc *)0;
3341 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
3343 free((caddr_t)cookies, M_TEMP);
3346 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3347 off = (u_quad_t)io.uio_offset;
3348 getret = VOP_GETATTR(vp, &at, cred, procp);
3349 VOP_UNLOCK(vp, 0, procp);
3350 if (!cookies && !error)
3351 error = NFSERR_PERM;
3358 free((caddr_t)cookies, M_TEMP);
3359 free((caddr_t)rbuf, M_TEMP);
3360 nfsm_reply(NFSX_V3POSTOPATTR);
3361 nfsm_srvpostop_attr(getret, &at);
3366 siz -= io.uio_resid;
3369 * If nothing read, return eof
3375 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3377 nfsm_srvpostop_attr(getret, &at);
3378 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3379 txdr_hyper(at.va_filerev, tl);
3383 FREE((caddr_t)cookies, M_TEMP);
3384 FREE((caddr_t)rbuf, M_TEMP);
3391 * Check for degenerate cases of nothing useful read.
3392 * If so go try again
3396 dp = (struct dirent *)cpos;
3399 * For some reason FreeBSD's ufs_readdir() chooses to back the
3400 * directory offset up to a block boundary, so it is necessary to
3401 * skip over the records that preceed the requested offset. This
3402 * requires the assumption that file offset cookies monotonically
3405 while (cpos < cend && ncookies > 0 &&
3406 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3407 ((u_quad_t)(*cookiep)) <= toff)) {
3408 cpos += dp->d_reclen;
3409 dp = (struct dirent *)cpos;
3413 if (cpos >= cend || ncookies == 0) {
3420 * Probe one of the directory entries to see if the filesystem
3423 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3424 error = NFSERR_NOTSUPP;
3427 free((caddr_t)cookies, M_TEMP);
3428 free((caddr_t)rbuf, M_TEMP);
3429 nfsm_reply(NFSX_V3POSTOPATTR);
3430 nfsm_srvpostop_attr(getret, &at);
3437 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3439 nfsm_srvpostop_attr(getret, &at);
3440 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3441 txdr_hyper(at.va_filerev, tl);
3444 be = bp + M_TRAILINGSPACE(mp);
3446 /* Loop through the records and build reply */
3447 while (cpos < cend && ncookies > 0) {
3448 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3449 nlen = dp->d_namlen;
3450 rem = nfsm_rndup(nlen)-nlen;
3453 * For readdir_and_lookup get the vnode using
3456 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3458 bzero((caddr_t)nfhp, NFSX_V3FH);
3460 nvp->v_mount->mnt_stat.f_fsid;
3461 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3466 if (VOP_GETATTR(nvp, vap, cred, procp)) {
3475 * If either the dircount or maxcount will be
3476 * exceeded, get out now. Both of these lengths
3477 * are calculated conservatively, including all
3480 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3482 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3483 if (len > cnt || dirlen > fullsiz) {
3489 * Build the directory record xdr from
3492 fp = (struct nfs_fattr *)&fl.fl_fattr;
3493 nfsm_srvfillattr(vap, fp);
3494 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3495 fl.fl_fhok = nfs_true;
3496 fl.fl_postopok = nfs_true;
3497 fl.fl_off.nfsuquad[0] = 0;
3498 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3502 bp += NFSX_UNSIGNED;
3505 bp += NFSX_UNSIGNED;
3507 *tl = txdr_unsigned(dp->d_fileno);
3508 bp += NFSX_UNSIGNED;
3510 *tl = txdr_unsigned(nlen);
3511 bp += NFSX_UNSIGNED;
3513 /* And loop around copying the name */
3518 if ((bp + xfer) > be)
3522 bcopy(cp, bp, tsiz);
3528 /* And null pad to a int32_t boundary */
3529 for (i = 0; i < rem; i++)
3533 * Now copy the flrep structure out.
3535 xfer = sizeof (struct flrep);
3539 if ((bp + xfer) > be)
3543 bcopy(cp, bp, tsiz);
3551 cpos += dp->d_reclen;
3552 dp = (struct dirent *)cpos;
3560 bp += NFSX_UNSIGNED;
3566 bp += NFSX_UNSIGNED;
3569 mp->m_len = bp - mtod(mp, caddr_t);
3571 mp->m_len += bp - bpos;
3572 FREE((caddr_t)cookies, M_TEMP);
3573 FREE((caddr_t)rbuf, M_TEMP);
3581 * nfs commit service
3584 nfsrv_commit(nfsd, slp, procp, mrq)
3585 struct nfsrv_descript *nfsd;
3586 struct nfssvc_sock *slp;
3590 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3591 struct sockaddr *nam = nfsd->nd_nam;
3592 caddr_t dpos = nfsd->nd_dpos;
3593 struct ucred *cred = &nfsd->nd_cr;
3594 struct vattr bfor, aft;
3595 struct vnode *vp = NULL;
3598 register u_int32_t *tl;
3599 register int32_t t1;
3601 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3603 struct mbuf *mb, *mb2, *mreq;
3606 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3610 fhp = &nfh.fh_generic;
3612 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3615 * XXX At this time VOP_FSYNC() does not accept offset and byte
3616 * count parameters, so these arguments are useless (someday maybe).
3618 off = fxdr_hyper(tl);
3620 cnt = fxdr_unsigned(int, *tl);
3621 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3622 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3624 nfsm_reply(2 * NFSX_UNSIGNED);
3625 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3629 for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
3631 if (cnt > MAX_COMMIT_COUNT) {
3633 * Give up and do the whole thing
3636 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3637 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3639 error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
3642 * Locate and synchronously write any buffers that fall
3643 * into the requested range. Note: we are assuming that
3644 * f_iosize is a power of 2.
3646 int iosize = vp->v_mount->mnt_stat.f_iosize;
3647 int iomask = iosize - 1;
3652 * Align to iosize boundry, super-align to page boundry.
3655 cnt += off & iomask;
3656 off &= ~(u_quad_t)iomask;
3658 if (off & PAGE_MASK) {
3659 cnt += off & PAGE_MASK;
3660 off &= ~(u_quad_t)PAGE_MASK;
3662 lblkno = off / iosize;
3665 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3666 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3674 * If we have a buffer and it is marked B_DELWRI we
3675 * have to lock and write it. Otherwise the prior
3676 * write is assumed to have already been committed.
3678 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3679 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3680 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3681 continue; /* retry */
3684 bp->b_flags &= ~B_ASYNC;
3685 VOP_BWRITE(bp->b_vp, bp);
3697 aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
3700 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3701 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3703 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3704 if (nfsver.tv_sec == 0)
3706 *tl++ = txdr_unsigned(nfsver.tv_sec);
3707 *tl = txdr_unsigned(nfsver.tv_usec);
3718 * nfs statfs service
3721 nfsrv_statfs(nfsd, slp, procp, mrq)
3722 struct nfsrv_descript *nfsd;
3723 struct nfssvc_sock *slp;
3727 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3728 struct sockaddr *nam = nfsd->nd_nam;
3729 caddr_t dpos = nfsd->nd_dpos;
3730 struct ucred *cred = &nfsd->nd_cr;
3731 register struct statfs *sf;
3732 register struct nfs_statfs *sfp;
3733 register u_int32_t *tl;
3734 register int32_t t1;
3736 int error = 0, rdonly, cache, getret = 1;
3737 int v3 = (nfsd->nd_flag & ND_NFSV3);
3739 struct mbuf *mb, *mb2, *mreq;
3740 struct vnode *vp = NULL;
3744 struct statfs statfs;
3745 u_quad_t frev, tval;
3747 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3751 fhp = &nfh.fh_generic;
3753 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3754 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3756 nfsm_reply(NFSX_UNSIGNED);
3757 nfsm_srvpostop_attr(getret, &at);
3762 error = VFS_STATFS(vp->v_mount, sf, procp);
3763 getret = VOP_GETATTR(vp, &at, cred, procp);
3766 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3768 nfsm_srvpostop_attr(getret, &at);
3773 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3775 tval = (u_quad_t)sf->f_blocks;
3776 tval *= (u_quad_t)sf->f_bsize;
3777 txdr_hyper(tval, &sfp->sf_tbytes);
3778 tval = (u_quad_t)sf->f_bfree;
3779 tval *= (u_quad_t)sf->f_bsize;
3780 txdr_hyper(tval, &sfp->sf_fbytes);
3781 tval = (u_quad_t)sf->f_bavail;
3782 tval *= (u_quad_t)sf->f_bsize;
3783 txdr_hyper(tval, &sfp->sf_abytes);
3784 sfp->sf_tfiles.nfsuquad[0] = 0;
3785 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3786 sfp->sf_ffiles.nfsuquad[0] = 0;
3787 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3788 sfp->sf_afiles.nfsuquad[0] = 0;
3789 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3790 sfp->sf_invarsec = 0;
3792 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3793 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3794 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3795 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3796 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3805 * nfs fsinfo service
3808 nfsrv_fsinfo(nfsd, slp, procp, mrq)
3809 struct nfsrv_descript *nfsd;
3810 struct nfssvc_sock *slp;
3814 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3815 struct sockaddr *nam = nfsd->nd_nam;
3816 caddr_t dpos = nfsd->nd_dpos;
3817 struct ucred *cred = &nfsd->nd_cr;
3818 register u_int32_t *tl;
3819 register struct nfsv3_fsinfo *sip;
3820 register int32_t t1;
3822 int error = 0, rdonly, cache, getret = 1, pref;
3824 struct mbuf *mb, *mb2, *mreq;
3825 struct vnode *vp = NULL;
3829 u_quad_t frev, maxfsize;
3832 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3836 fhp = &nfh.fh_generic;
3838 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3839 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3841 nfsm_reply(NFSX_UNSIGNED);
3842 nfsm_srvpostop_attr(getret, &at);
3847 /* XXX Try to make a guess on the max file size. */
3848 VFS_STATFS(vp->v_mount, &sb, procp);
3849 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3851 getret = VOP_GETATTR(vp, &at, cred, procp);
3854 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3855 nfsm_srvpostop_attr(getret, &at);
3856 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3860 * There should be file system VFS OP(s) to get this information.
3861 * For now, assume ufs.
3863 if (slp->ns_so->so_type == SOCK_DGRAM)
3864 pref = NFS_MAXDGRAMDATA;
3867 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3868 sip->fs_rtpref = txdr_unsigned(pref);
3869 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3870 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3871 sip->fs_wtpref = txdr_unsigned(pref);
3872 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3873 sip->fs_dtpref = txdr_unsigned(pref);
3874 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3875 sip->fs_timedelta.nfsv3_sec = 0;
3876 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3877 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3878 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3879 NFSV3FSINFO_CANSETTIME);
3887 * nfs pathconf service
3890 nfsrv_pathconf(nfsd, slp, procp, mrq)
3891 struct nfsrv_descript *nfsd;
3892 struct nfssvc_sock *slp;
3896 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3897 struct sockaddr *nam = nfsd->nd_nam;
3898 caddr_t dpos = nfsd->nd_dpos;
3899 struct ucred *cred = &nfsd->nd_cr;
3900 register u_int32_t *tl;
3901 register struct nfsv3_pathconf *pc;
3902 register int32_t t1;
3904 int error = 0, rdonly, cache, getret = 1;
3905 register_t linkmax, namemax, chownres, notrunc;
3907 struct mbuf *mb, *mb2, *mreq;
3908 struct vnode *vp = NULL;
3914 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3918 fhp = &nfh.fh_generic;
3920 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3921 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3923 nfsm_reply(NFSX_UNSIGNED);
3924 nfsm_srvpostop_attr(getret, &at);
3928 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3930 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3932 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3934 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3935 getret = VOP_GETATTR(vp, &at, cred, procp);
3938 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3939 nfsm_srvpostop_attr(getret, &at);
3944 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3946 pc->pc_linkmax = txdr_unsigned(linkmax);
3947 pc->pc_namemax = txdr_unsigned(namemax);
3948 pc->pc_notrunc = txdr_unsigned(notrunc);
3949 pc->pc_chownrestricted = txdr_unsigned(chownres);
3952 * These should probably be supported by VOP_PATHCONF(), but
3953 * until msdosfs is exportable (why would you want to?), the
3954 * Unix defaults should be ok.
3956 pc->pc_caseinsensitive = nfs_false;
3957 pc->pc_casepreserving = nfs_true;
3965 * Null operation, used by clients to ping server
3969 nfsrv_null(nfsd, slp, procp, mrq)
3970 struct nfsrv_descript *nfsd;
3971 struct nfssvc_sock *slp;
3975 struct mbuf *mrep = nfsd->nd_mrep;
3977 int error = NFSERR_RETVOID, cache;
3978 struct mbuf *mb, *mreq;
3981 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3990 * No operation, used for obsolete procedures
3994 nfsrv_noop(nfsd, slp, procp, mrq)
3995 struct nfsrv_descript *nfsd;
3996 struct nfssvc_sock *slp;
4000 struct mbuf *mrep = nfsd->nd_mrep;
4003 struct mbuf *mb, *mreq;
4006 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4010 if (nfsd->nd_repstat)
4011 error = nfsd->nd_repstat;
4013 error = EPROCUNAVAIL;
4020 * Perform access checking for vnodes obtained from file handles that would
4021 * refer to files already opened by a Unix client. You cannot just use
4022 * vn_writechk() and VOP_ACCESS() for two reasons.
4023 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4024 * 2 - The owner is to be given access irrespective of mode bits for some
4025 * operations, so that processes that chmod after opening a file don't
4026 * break. I don't like this because it opens a security hole, but since
4027 * the nfs server opens a security hole the size of a barn door anyhow,
4030 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4031 * will return EPERM instead of EACCESS. EPERM is always an error.
4034 nfsrv_access(vp, flags, cred, rdonly, p, override)
4035 register struct vnode *vp;
4037 register struct ucred *cred;
4045 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4046 if (flags & VWRITE) {
4047 /* Just vn_writechk() changed to check rdonly */
4049 * Disallow write attempts on read-only file systems;
4050 * unless the file is a socket or a block or character
4051 * device resident on the file system.
4053 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4054 switch (vp->v_type) {
4064 * If there's shared text associated with
4065 * the inode, we can't allow writing.
4067 if (vp->v_flag & VTEXT)
4070 error = VOP_GETATTR(vp, &vattr, cred, p);
4073 error = VOP_ACCESS(vp, flags, cred, p);
4075 * Allow certain operations for the owner (reads and writes
4076 * on files that are already open).
4078 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4082 #endif /* NFS_NOSERVER */