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 $
41 * nfs version 2 and 3 server calls to vnode ops
42 * - these routines generally have 3 phases
43 * 1 - break down and validate rpc request in mbuf list
44 * 2 - do the vnode ops for the request
45 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
46 * 3 - build the rpc reply in an mbuf list
48 * - do not mix the phases, since the nfsm_?? macros can return failures
49 * on a bad rpc or similar and do not do any vrele() or vput()'s
51 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
52 * error number iff error != 0 whereas
53 * returning an error from the server function implies a fatal error
54 * such as a badly constructed rpc request that should be dropped without
56 * For Version 3, nfsm_reply() does not return for the error case, since
57 * most version 3 rpcs return more than the status for error cases.
60 * Warning: always pay careful attention to resource cleanup on return
61 * and note that nfsm_*() macros can terminate a procedure on certain
64 * lookup() and namei()
65 * may return garbage in various structural fields/return elements
66 * if an error is returned, and may garbage up nd.ni_dvp even if no
67 * error is returned and you did not request LOCKPARENT or WANTPARENT.
69 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
70 * buffer has been freed or not.
73 #include <sys/param.h>
74 #include <sys/systm.h>
76 #include <sys/namei.h>
77 #include <sys/unistd.h>
78 #include <sys/vnode.h>
79 #include <sys/mount.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/malloc.h>
84 #include <sys/dirent.h>
86 #include <sys/kernel.h>
87 #include <sys/sysctl.h>
91 #include <vm/vm_extern.h>
92 #include <vm/vm_zone.h>
93 #include <vm/vm_object.h>
95 #include <nfs/nfsproto.h>
96 #include <nfs/rpcv2.h>
98 #include <nfs/xdr_subs.h>
99 #include <nfs/nfsm_subs.h>
100 #include <nfs/nqnfs.h>
103 #define nfsdbprintf(info) printf info
105 #define nfsdbprintf(info)
108 #define MAX_COMMIT_COUNT (1024 * 1024)
110 #define NUM_HEURISTIC 64
111 #define NHUSE_INIT 64
113 #define NHUSE_MAX 2048
115 static struct nfsheur {
116 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
117 off_t nh_nextr; /* next offset for sequential detection */
118 int nh_use; /* use count for selection */
119 int nh_seqcount; /* heuristic */
120 } nfsheur[NUM_HEURISTIC];
122 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
125 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
128 extern u_int32_t nfs_xdrneg1;
129 extern u_int32_t nfs_false, nfs_true;
130 extern enum vtype nv3tov_type[8];
131 extern struct nfsstats nfsstats;
133 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
134 int nfsrvw_procrastinate_v3 = 0;
136 static struct timeval nfsver = { 0 };
138 SYSCTL_DECL(_vfs_nfs);
140 static int nfs_async;
141 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
142 static int nfs_commit_blks;
143 static int nfs_commit_miss;
144 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
145 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
147 static int nfsrv_access __P((struct vnode *,int,struct ucred *,int,
148 struct proc *, int));
149 static void nfsrvw_coalesce __P((struct nfsrv_descript *,
150 struct nfsrv_descript *));
153 * Clear nameidata fields that are tested in nsfmout cleanup code prior
154 * to using first nfsm macro (that might jump to the cleanup code).
159 ndclear(struct nameidata *nd)
161 nd->ni_cnd.cn_flags = 0;
164 nd->ni_startdir = NULL;
168 * nfs v3 access service
171 nfsrv3_access(nfsd, slp, procp, mrq)
172 struct nfsrv_descript *nfsd;
173 struct nfssvc_sock *slp;
177 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
178 struct sockaddr *nam = nfsd->nd_nam;
179 caddr_t dpos = nfsd->nd_dpos;
180 struct ucred *cred = &nfsd->nd_cr;
181 struct vnode *vp = NULL;
184 register u_int32_t *tl;
187 int error = 0, rdonly, cache, getret;
189 struct mbuf *mb, *mreq, *mb2;
190 struct vattr vattr, *vap = &vattr;
191 u_long testmode, nfsmode;
194 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
198 fhp = &nfh.fh_generic;
200 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
201 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
202 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
204 nfsm_reply(NFSX_UNSIGNED);
205 nfsm_srvpostop_attr(1, (struct vattr *)0);
209 nfsmode = fxdr_unsigned(u_int32_t, *tl);
210 if ((nfsmode & NFSV3ACCESS_READ) &&
211 nfsrv_access(vp, VREAD, cred, rdonly, procp, 0))
212 nfsmode &= ~NFSV3ACCESS_READ;
213 if (vp->v_type == VDIR)
214 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
217 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
218 if ((nfsmode & testmode) &&
219 nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0))
220 nfsmode &= ~testmode;
221 if (vp->v_type == VDIR)
222 testmode = NFSV3ACCESS_LOOKUP;
224 testmode = NFSV3ACCESS_EXECUTE;
225 if ((nfsmode & testmode) &&
226 nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0))
227 nfsmode &= ~testmode;
228 getret = VOP_GETATTR(vp, vap, cred, procp);
231 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
232 nfsm_srvpostop_attr(getret, vap);
233 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
234 *tl = txdr_unsigned(nfsmode);
242 * nfs getattr service
245 nfsrv_getattr(nfsd, slp, procp, mrq)
246 struct nfsrv_descript *nfsd;
247 struct nfssvc_sock *slp;
251 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
252 struct sockaddr *nam = nfsd->nd_nam;
253 caddr_t dpos = nfsd->nd_dpos;
254 struct ucred *cred = &nfsd->nd_cr;
255 register struct nfs_fattr *fp;
257 register struct vattr *vap = &va;
258 struct vnode *vp = NULL;
261 register u_int32_t *tl;
264 int error = 0, rdonly, cache;
266 struct mbuf *mb, *mb2, *mreq;
269 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
270 fhp = &nfh.fh_generic;
272 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
273 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
279 nqsrv_getl(vp, ND_READ);
280 error = VOP_GETATTR(vp, vap, cred, procp);
283 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
288 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
289 nfsm_srvfillattr(vap, fp);
299 * nfs setattr service
302 nfsrv_setattr(nfsd, slp, procp, mrq)
303 struct nfsrv_descript *nfsd;
304 struct nfssvc_sock *slp;
308 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
309 struct sockaddr *nam = nfsd->nd_nam;
310 caddr_t dpos = nfsd->nd_dpos;
311 struct ucred *cred = &nfsd->nd_cr;
312 struct vattr va, preat;
313 register struct vattr *vap = &va;
314 register struct nfsv2_sattr *sp;
315 register struct nfs_fattr *fp;
316 struct vnode *vp = NULL;
319 register u_int32_t *tl;
322 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
323 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
325 struct mbuf *mb, *mb2, *mreq;
327 struct timespec guard;
329 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
330 fhp = &nfh.fh_generic;
335 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
336 gcheck = fxdr_unsigned(int, *tl);
338 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
339 fxdr_nfsv3time(tl, &guard);
342 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
344 * Nah nah nah nah na nah
345 * There is a bug in the Sun client that puts 0xffff in the mode
346 * field of sattr when it should put in 0xffffffff. The u_short
347 * doesn't sign extend.
348 * --> check the low order 2 bytes for 0xffff
350 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
351 vap->va_mode = nfstov_mode(sp->sa_mode);
352 if (sp->sa_uid != nfs_xdrneg1)
353 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
354 if (sp->sa_gid != nfs_xdrneg1)
355 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
356 if (sp->sa_size != nfs_xdrneg1)
357 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
358 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
360 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
362 vap->va_atime.tv_sec =
363 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
364 vap->va_atime.tv_nsec = 0;
367 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
368 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
373 * Now that we have all the fields, lets do it.
375 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
376 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
378 nfsm_reply(2 * NFSX_UNSIGNED);
379 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
385 * vp now an active resource, pay careful attention to cleanup
388 nqsrv_getl(vp, ND_WRITE);
390 error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
391 if (!error && gcheck &&
392 (preat.va_ctime.tv_sec != guard.tv_sec ||
393 preat.va_ctime.tv_nsec != guard.tv_nsec))
394 error = NFSERR_NOT_SYNC;
398 nfsm_reply(NFSX_WCCDATA(v3));
399 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
406 * If the size is being changed write acces is required, otherwise
407 * just check for a read only file system.
409 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
410 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
415 if (vp->v_type == VDIR) {
418 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
422 error = VOP_SETATTR(vp, vap, cred, procp);
423 postat_ret = VOP_GETATTR(vp, vap, cred, procp);
429 nfsm_reply(NFSX_WCCORFATTR(v3));
431 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
435 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
436 nfsm_srvfillattr(vap, fp);
450 nfsrv_lookup(nfsd, slp, procp, mrq)
451 struct nfsrv_descript *nfsd;
452 struct nfssvc_sock *slp;
456 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
457 struct sockaddr *nam = nfsd->nd_nam;
458 caddr_t dpos = nfsd->nd_dpos;
459 struct ucred *cred = &nfsd->nd_cr;
460 register struct nfs_fattr *fp;
461 struct nameidata nd, ind, *ndp = &nd;
462 struct vnode *vp, *dirp = NULL;
466 register u_int32_t *tl;
469 int error = 0, cache, len, dirattr_ret = 1;
470 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
472 struct mbuf *mb, *mb2, *mreq;
473 struct vattr va, dirattr, *vap = &va;
476 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
479 fhp = &nfh.fh_generic;
481 nfsm_srvnamesiz(len);
483 pubflag = nfs_ispublicfh(fhp);
485 nd.ni_cnd.cn_cred = cred;
486 nd.ni_cnd.cn_nameiop = LOOKUP;
487 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
488 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
489 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
492 * namei failure, only dirp to cleanup. Clear out garbarge from
493 * structure in case macros jump to nfsmout.
499 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
504 nfsm_reply(NFSX_POSTOPATTR(v3));
505 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
511 * Locate index file for public filehandle
513 * error is 0 on entry and 0 on exit from this block.
517 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
519 * Setup call to lookup() to see if we can find
520 * the index file. Arguably, this doesn't belong
521 * in a kernel.. Ugh. If an error occurs, do not
522 * try to install an index file and then clear the
525 * When we replace nd with ind and redirect ndp,
526 * maintenance of ni_startdir and ni_vp shift to
527 * ind and we have to clean them up in the old nd.
528 * However, the cnd resource continues to be maintained
529 * via the original nd. Confused? You aren't alone!
532 VOP_UNLOCK(nd.ni_vp, 0, procp);
533 ind.ni_pathlen = strlen(nfs_pub.np_index);
534 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
536 ind.ni_startdir = nd.ni_vp;
537 VREF(ind.ni_startdir);
539 error = lookup(&ind);
544 * Found an index file. Get rid of
545 * the old references. transfer nd.ni_vp'
551 vrele(nd.ni_startdir);
552 nd.ni_startdir = NULL;
558 * If the public filehandle was used, check that this lookup
559 * didn't result in a filehandle outside the publicly exported
560 * filesystem. We clear the poor vp here to avoid lockups due
564 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
573 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
580 * Resources at this point:
581 * ndp->ni_vp may not be NULL
586 nfsm_reply(NFSX_POSTOPATTR(v3));
587 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
592 nqsrv_getl(ndp->ni_startdir, ND_READ);
595 * Clear out some resources prior to potentially blocking. This
596 * is not as critical as ni_dvp resources in other routines, but
599 vrele(ndp->ni_startdir);
600 ndp->ni_startdir = NULL;
601 NDFREE(&nd, NDF_ONLY_PNBUF);
604 * Get underlying attribute, then release remaining resources ( for
605 * the same potential blocking reason ) and reply.
608 bzero((caddr_t)fhp, sizeof(nfh));
609 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
610 error = VFS_VPTOFH(vp, &fhp->fh_fid);
612 error = VOP_GETATTR(vp, vap, cred, procp);
616 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
618 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
622 nfsm_srvfhtom(fhp, v3);
624 nfsm_srvpostop_attr(0, vap);
625 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
627 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
628 nfsm_srvfillattr(vap, fp);
634 NDFREE(&nd, NDF_ONLY_PNBUF);
635 if (ndp->ni_startdir)
636 vrele(ndp->ni_startdir);
643 * nfs readlink service
646 nfsrv_readlink(nfsd, slp, procp, mrq)
647 struct nfsrv_descript *nfsd;
648 struct nfssvc_sock *slp;
652 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
653 struct sockaddr *nam = nfsd->nd_nam;
654 caddr_t dpos = nfsd->nd_dpos;
655 struct ucred *cred = &nfsd->nd_cr;
656 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
657 register struct iovec *ivp = iv;
658 register struct mbuf *mp;
659 register u_int32_t *tl;
662 int error = 0, rdonly, cache, i, tlen, len, getret;
663 int v3 = (nfsd->nd_flag & ND_NFSV3);
665 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
666 struct vnode *vp = NULL;
670 struct uio io, *uiop = &io;
673 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
675 mp2 = (struct mbuf *)0;
678 fhp = &nfh.fh_generic;
682 while (len < NFS_MAXPATHLEN) {
683 MGET(mp, M_WAIT, MT_DATA);
685 mp->m_len = NFSMSIZ(mp);
692 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
693 mp->m_len = NFS_MAXPATHLEN-len;
694 len = NFS_MAXPATHLEN;
697 ivp->iov_base = mtod(mp, caddr_t);
698 ivp->iov_len = mp->m_len;
703 uiop->uio_iovcnt = i;
704 uiop->uio_offset = 0;
705 uiop->uio_resid = len;
706 uiop->uio_rw = UIO_READ;
707 uiop->uio_segflg = UIO_SYSSPACE;
708 uiop->uio_procp = (struct proc *)0;
709 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
710 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
712 nfsm_reply(2 * NFSX_UNSIGNED);
713 nfsm_srvpostop_attr(1, (struct vattr *)0);
717 if (vp->v_type != VLNK) {
724 nqsrv_getl(vp, ND_READ);
725 error = VOP_READLINK(vp, uiop, cred);
727 getret = VOP_GETATTR(vp, &attr, cred, procp);
730 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
732 nfsm_srvpostop_attr(getret, &attr);
738 if (uiop->uio_resid > 0) {
739 len -= uiop->uio_resid;
740 tlen = nfsm_rndup(len);
741 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
743 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
744 *tl = txdr_unsigned(len);
759 nfsrv_read(nfsd, slp, procp, mrq)
760 struct nfsrv_descript *nfsd;
761 struct nfssvc_sock *slp;
765 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
766 struct sockaddr *nam = nfsd->nd_nam;
767 caddr_t dpos = nfsd->nd_dpos;
768 struct ucred *cred = &nfsd->nd_cr;
769 register struct iovec *iv;
771 register struct mbuf *m;
772 register struct nfs_fattr *fp;
773 register u_int32_t *tl;
777 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
778 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
780 struct mbuf *mb, *mb2, *mreq;
782 struct vnode *vp = NULL;
785 struct uio io, *uiop = &io;
786 struct vattr va, *vap = &va;
792 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
793 fhp = &nfh.fh_generic;
796 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
797 off = fxdr_hyper(tl);
799 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
800 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
802 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
805 * Reference vp. If an error occurs, vp will be invalid, but we
806 * have to NULL it just in case. The macros might goto nfsmout
810 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
811 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
814 nfsm_reply(2 * NFSX_UNSIGNED);
815 nfsm_srvpostop_attr(1, (struct vattr *)0);
820 if (vp->v_type != VREG) {
824 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
827 nqsrv_getl(vp, ND_READ);
828 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
829 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
831 getret = VOP_GETATTR(vp, vap, cred, procp);
837 nfsm_reply(NFSX_POSTOPATTR(v3));
838 nfsm_srvpostop_attr(getret, vap);
844 * Calculate byte count to read
847 if (off >= vap->va_size)
849 else if ((off + reqlen) > vap->va_size)
850 cnt = vap->va_size - off;
855 * Calculate seqcount for heuristic
863 * Locate best candidate
866 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
870 if (nfsheur[hi].nh_vp == vp) {
874 if (nfsheur[hi].nh_use > 0)
875 --nfsheur[hi].nh_use;
876 hi = (hi + 1) & (NUM_HEURISTIC - 1);
877 if (nfsheur[hi].nh_use < nh->nh_use)
881 if (nh->nh_vp != vp) {
884 nh->nh_use = NHUSE_INIT;
892 * Calculate heuristic
895 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
896 if (++nh->nh_seqcount > IO_SEQMAX)
897 nh->nh_seqcount = IO_SEQMAX;
898 } else if (nh->nh_seqcount > 1) {
903 nh->nh_use += NHUSE_INC;
904 if (nh->nh_use > NHUSE_MAX)
905 nh->nh_use = NHUSE_MAX;
906 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
909 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
911 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
913 fp = (struct nfs_fattr *)tl;
914 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
916 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
917 fp = (struct nfs_fattr *)tl;
918 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
920 len = left = nfsm_rndup(cnt);
923 * Generate the mbuf list with the uio_iov ref. to it.
928 siz = min(M_TRAILINGSPACE(m), left);
934 MGET(m, M_WAIT, MT_DATA);
941 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
943 uiop->uio_iov = iv2 = iv;
949 panic("nfsrv_read iov");
950 siz = min(M_TRAILINGSPACE(m), left);
952 iv->iov_base = mtod(m, caddr_t) + m->m_len;
961 uiop->uio_iovcnt = i;
962 uiop->uio_offset = off;
963 uiop->uio_resid = len;
964 uiop->uio_rw = UIO_READ;
965 uiop->uio_segflg = UIO_SYSSPACE;
966 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
967 off = uiop->uio_offset;
969 FREE((caddr_t)iv2, M_TEMP);
970 if (error || (getret = VOP_GETATTR(vp, vap, cred, procp))) {
976 nfsm_reply(NFSX_POSTOPATTR(v3));
977 nfsm_srvpostop_attr(getret, vap);
986 nfsm_srvfillattr(vap, fp);
987 tlen = len - uiop->uio_resid;
988 cnt = cnt < tlen ? cnt : tlen;
989 tlen = nfsm_rndup(cnt);
990 if (len != tlen || tlen != cnt)
991 nfsm_adj(mb, len - tlen, tlen - cnt);
993 *tl++ = txdr_unsigned(cnt);
999 *tl = txdr_unsigned(cnt);
1010 nfsrv_write(nfsd, slp, procp, mrq)
1011 struct nfsrv_descript *nfsd;
1012 struct nfssvc_sock *slp;
1016 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1017 struct sockaddr *nam = nfsd->nd_nam;
1018 caddr_t dpos = nfsd->nd_dpos;
1019 struct ucred *cred = &nfsd->nd_cr;
1020 register struct iovec *ivp;
1021 register int i, cnt;
1022 register struct mbuf *mp;
1023 register struct nfs_fattr *fp;
1025 struct vattr va, forat;
1026 register struct vattr *vap = &va;
1027 register u_int32_t *tl;
1028 register int32_t t1;
1030 int error = 0, rdonly, cache, len, forat_ret = 1;
1031 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1032 int stable = NFSV3WRITE_FILESYNC;
1033 int v3 = (nfsd->nd_flag & ND_NFSV3);
1035 struct mbuf *mb, *mb2, *mreq;
1036 struct vnode *vp = NULL;
1039 struct uio io, *uiop = &io;
1043 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1049 fhp = &nfh.fh_generic;
1052 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1053 off = fxdr_hyper(tl);
1055 stable = fxdr_unsigned(int, *tl++);
1057 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1058 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1061 stable = NFSV3WRITE_UNSTABLE;
1063 retlen = len = fxdr_unsigned(int32_t, *tl);
1067 * For NFS Version 2, it is not obvious what a write of zero length
1068 * should do, but I might as well be consistent with Version 3,
1069 * which is to return ok so long as there are no permission problems.
1077 adjust = dpos - mtod(mp, caddr_t);
1078 mp->m_len -= adjust;
1079 if (mp->m_len > 0 && adjust > 0)
1080 NFSMADV(mp, adjust);
1084 else if (mp->m_len > 0) {
1087 mp->m_len -= (i - len);
1096 if (len > NFS_MAXDATA || len < 0 || i < len) {
1098 nfsm_reply(2 * NFSX_UNSIGNED);
1099 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1103 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1104 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1107 nfsm_reply(2 * NFSX_UNSIGNED);
1108 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1113 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1114 if (vp->v_type != VREG) {
1118 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1121 nqsrv_getl(vp, ND_WRITE);
1122 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1127 nfsm_reply(NFSX_WCCDATA(v3));
1128 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1134 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1136 uiop->uio_iov = iv = ivp;
1137 uiop->uio_iovcnt = cnt;
1140 if (mp->m_len > 0) {
1141 ivp->iov_base = mtod(mp, caddr_t);
1142 ivp->iov_len = mp->m_len;
1150 * The IO_METASYNC flag indicates that all metadata (and not just
1151 * enough to ensure data integrity) mus be written to stable storage
1153 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1155 if (stable == NFSV3WRITE_UNSTABLE)
1156 ioflags = IO_NODELOCKED;
1157 else if (stable == NFSV3WRITE_DATASYNC)
1158 ioflags = (IO_SYNC | IO_NODELOCKED);
1160 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1161 uiop->uio_resid = len;
1162 uiop->uio_rw = UIO_WRITE;
1163 uiop->uio_segflg = UIO_SYSSPACE;
1164 uiop->uio_procp = (struct proc *)0;
1165 uiop->uio_offset = off;
1166 error = VOP_WRITE(vp, uiop, ioflags, cred);
1167 nfsstats.srvvop_writes++;
1168 FREE((caddr_t)iv, M_TEMP);
1170 aftat_ret = VOP_GETATTR(vp, vap, cred, procp);
1175 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1176 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1178 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1183 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1184 *tl++ = txdr_unsigned(retlen);
1186 * If nfs_async is set, then pretend the write was FILESYNC.
1188 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1189 *tl++ = txdr_unsigned(stable);
1191 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1193 * Actually, there is no need to txdr these fields,
1194 * but it may make the values more human readable,
1195 * for debugging purposes.
1197 if (nfsver.tv_sec == 0)
1199 *tl++ = txdr_unsigned(nfsver.tv_sec);
1200 *tl = txdr_unsigned(nfsver.tv_usec);
1202 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1203 nfsm_srvfillattr(vap, fp);
1212 * NFS write service with write gathering support. Called when
1213 * nfsrvw_procrastinate > 0.
1214 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1215 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1219 nfsrv_writegather(ndp, slp, procp, mrq)
1220 struct nfsrv_descript **ndp;
1221 struct nfssvc_sock *slp;
1225 register struct iovec *ivp;
1226 register struct mbuf *mp;
1227 register struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1228 register struct nfs_fattr *fp;
1231 struct nfsrvw_delayhash *wpp;
1233 struct vattr va, forat;
1234 register u_int32_t *tl;
1235 register int32_t t1;
1237 int error = 0, rdonly, cache, len, forat_ret = 1;
1238 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1240 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1241 struct vnode *vp = NULL;
1242 struct uio io, *uiop = &io;
1243 u_quad_t frev, cur_usec;
1245 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1254 mrep = nfsd->nd_mrep;
1256 dpos = nfsd->nd_dpos;
1257 cred = &nfsd->nd_cr;
1258 v3 = (nfsd->nd_flag & ND_NFSV3);
1259 LIST_INIT(&nfsd->nd_coalesce);
1260 nfsd->nd_mreq = NULL;
1261 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1262 cur_usec = nfs_curusec();
1263 nfsd->nd_time = cur_usec +
1264 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1267 * Now, get the write header..
1269 nfsm_srvmtofh(&nfsd->nd_fh);
1271 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1272 nfsd->nd_off = fxdr_hyper(tl);
1274 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1276 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1277 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1280 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1282 len = fxdr_unsigned(int32_t, *tl);
1284 nfsd->nd_eoff = nfsd->nd_off + len;
1287 * Trim the header out of the mbuf list and trim off any trailing
1288 * junk so that the mbuf list has only the write data.
1296 adjust = dpos - mtod(mp, caddr_t);
1297 mp->m_len -= adjust;
1298 if (mp->m_len > 0 && adjust > 0)
1299 NFSMADV(mp, adjust);
1306 mp->m_len -= (i - len);
1312 if (len > NFS_MAXDATA || len < 0 || i < len) {
1316 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1318 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1319 nfsd->nd_mreq = mreq;
1320 nfsd->nd_mrep = NULL;
1325 * Add this entry to the hash and time queues.
1329 wp = slp->ns_tq.lh_first;
1330 while (wp && wp->nd_time < nfsd->nd_time) {
1332 wp = wp->nd_tq.le_next;
1334 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1336 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1338 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1340 if (nfsd->nd_mrep) {
1341 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1345 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1347 wp = wp->nd_hash.le_next;
1349 while (wp && wp->nd_off < nfsd->nd_off &&
1350 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1352 wp = wp->nd_hash.le_next;
1355 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1358 * Search the hash list for overlapping entries and
1361 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1362 wp = nfsd->nd_hash.le_next;
1363 if (NFSW_SAMECRED(owp, nfsd))
1364 nfsrvw_coalesce(owp, nfsd);
1367 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1374 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1375 * and generate the associated reply mbuf list(s).
1378 cur_usec = nfs_curusec();
1380 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1381 owp = nfsd->nd_tq.le_next;
1382 if (nfsd->nd_time > cur_usec)
1386 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1387 LIST_REMOVE(nfsd, nd_tq);
1388 LIST_REMOVE(nfsd, nd_hash);
1390 mrep = nfsd->nd_mrep;
1391 nfsd->nd_mrep = NULL;
1392 cred = &nfsd->nd_cr;
1393 v3 = (nfsd->nd_flag & ND_NFSV3);
1394 forat_ret = aftat_ret = 1;
1395 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1396 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1399 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1400 if (vp->v_type != VREG) {
1404 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1410 nqsrv_getl(vp, ND_WRITE);
1411 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1414 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1415 ioflags = IO_NODELOCKED;
1416 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1417 ioflags = (IO_SYNC | IO_NODELOCKED);
1419 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1420 uiop->uio_rw = UIO_WRITE;
1421 uiop->uio_segflg = UIO_SYSSPACE;
1422 uiop->uio_procp = (struct proc *)0;
1423 uiop->uio_offset = nfsd->nd_off;
1424 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1425 if (uiop->uio_resid > 0) {
1433 uiop->uio_iovcnt = i;
1434 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1436 uiop->uio_iov = ivp = iov;
1439 if (mp->m_len > 0) {
1440 ivp->iov_base = mtod(mp, caddr_t);
1441 ivp->iov_len = mp->m_len;
1447 error = VOP_WRITE(vp, uiop, ioflags, cred);
1448 nfsstats.srvvop_writes++;
1450 FREE((caddr_t)iov, M_TEMP);
1454 aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
1460 * Loop around generating replies for all write rpcs that have
1461 * now been completed.
1465 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1467 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1469 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1472 nfsm_writereply(NFSX_PREOPATTR(v3) +
1473 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1474 NFSX_WRITEVERF(v3), v3);
1476 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1477 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1478 *tl++ = txdr_unsigned(nfsd->nd_len);
1479 *tl++ = txdr_unsigned(swp->nd_stable);
1481 * Actually, there is no need to txdr these fields,
1482 * but it may make the values more human readable,
1483 * for debugging purposes.
1485 if (nfsver.tv_sec == 0)
1487 *tl++ = txdr_unsigned(nfsver.tv_sec);
1488 *tl = txdr_unsigned(nfsver.tv_usec);
1490 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1491 nfsm_srvfillattr(&va, fp);
1494 nfsd->nd_mreq = mreq;
1496 panic("nfsrv_write: nd_mrep not free");
1499 * Done. Put it at the head of the timer queue so that
1500 * the final phase can return the reply.
1505 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1507 nfsd = swp->nd_coalesce.lh_first;
1509 LIST_REMOVE(nfsd, nd_tq);
1515 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1522 * Search for a reply to return.
1525 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1526 if (nfsd->nd_mreq) {
1527 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1528 LIST_REMOVE(nfsd, nd_tq);
1529 *mrq = nfsd->nd_mreq;
1538 * Coalesce the write request nfsd into owp. To do this we must:
1539 * - remove nfsd from the queues
1540 * - merge nfsd->nd_mrep into owp->nd_mrep
1541 * - update the nd_eoff and nd_stable for owp
1542 * - put nfsd on owp's nd_coalesce list
1543 * NB: Must be called at splsoftclock().
1546 nfsrvw_coalesce(owp, nfsd)
1547 register struct nfsrv_descript *owp;
1548 register struct nfsrv_descript *nfsd;
1550 register int overlap;
1551 register struct mbuf *mp;
1552 struct nfsrv_descript *p;
1554 NFS_DPF(WG, ("C%03x-%03x",
1555 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1556 LIST_REMOVE(nfsd, nd_hash);
1557 LIST_REMOVE(nfsd, nd_tq);
1558 if (owp->nd_eoff < nfsd->nd_eoff) {
1559 overlap = owp->nd_eoff - nfsd->nd_off;
1561 panic("nfsrv_coalesce: bad off");
1563 m_adj(nfsd->nd_mrep, overlap);
1567 mp->m_next = nfsd->nd_mrep;
1568 owp->nd_eoff = nfsd->nd_eoff;
1570 m_freem(nfsd->nd_mrep);
1571 nfsd->nd_mrep = NULL;
1572 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1573 owp->nd_stable = NFSV3WRITE_FILESYNC;
1574 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1575 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1576 owp->nd_stable = NFSV3WRITE_DATASYNC;
1577 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1580 * If nfsd had anything else coalesced into it, transfer them
1581 * to owp, otherwise their replies will never get sent.
1583 for (p = nfsd->nd_coalesce.lh_first; p;
1584 p = nfsd->nd_coalesce.lh_first) {
1585 LIST_REMOVE(p, nd_tq);
1586 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1591 * nfs create service
1592 * now does a truncate to 0 length via. setattr if it already exists
1595 nfsrv_create(nfsd, slp, procp, mrq)
1596 struct nfsrv_descript *nfsd;
1597 struct nfssvc_sock *slp;
1601 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1602 struct sockaddr *nam = nfsd->nd_nam;
1603 caddr_t dpos = nfsd->nd_dpos;
1604 struct ucred *cred = &nfsd->nd_cr;
1605 register struct nfs_fattr *fp;
1606 struct vattr va, dirfor, diraft;
1607 register struct vattr *vap = &va;
1608 register struct nfsv2_sattr *sp;
1609 register u_int32_t *tl;
1610 struct nameidata nd;
1611 register int32_t t1;
1613 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1614 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1617 struct mbuf *mb, *mb2, *mreq;
1618 struct vnode *dirp = (struct vnode *)0;
1621 u_quad_t frev, tempsize;
1622 u_char cverf[NFSX_V3CREATEVERF];
1624 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1630 fhp = &nfh.fh_generic;
1632 nfsm_srvnamesiz(len);
1634 nd.ni_cnd.cn_cred = cred;
1635 nd.ni_cnd.cn_nameiop = CREATE;
1636 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1639 * Call namei and do initial cleanup to get a few things
1640 * out of the way. If we get an initial error we cleanup
1641 * and return here to avoid special-casing the invalid nd
1642 * structure through the rest of the case. dirp may be
1643 * set even if an error occurs, but the nd structure will not
1644 * be valid at all if an error occurs so we have to invalidate it
1645 * prior to calling nfsm_reply ( which might goto nfsmout ).
1647 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1648 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1651 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1659 nfsm_reply(NFSX_WCCDATA(v3));
1660 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1666 * No error. Continue. State:
1668 * startdir is valid ( we release this immediately )
1670 * nd.ni_vp may be valid
1671 * nd.ni_dvp is valid
1673 * The error state is set through the code and we may also do some
1674 * opportunistic releasing of vnodes to avoid holding locks through
1675 * NFS I/O. The cleanup at the end is a catch-all
1680 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1681 how = fxdr_unsigned(int, *tl);
1683 case NFSV3CREATE_GUARDED:
1689 case NFSV3CREATE_UNCHECKED:
1692 case NFSV3CREATE_EXCLUSIVE:
1693 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1694 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1698 vap->va_type = VREG;
1700 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1701 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1702 if (vap->va_type == VNON)
1703 vap->va_type = VREG;
1704 vap->va_mode = nfstov_mode(sp->sa_mode);
1705 switch (vap->va_type) {
1707 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1709 vap->va_size = (u_quad_t)tsize;
1714 rdev = fxdr_unsigned(long, sp->sa_size);
1722 * Iff doesn't exist, create it
1723 * otherwise just truncate to 0 length
1724 * should I set the mode too ?
1726 * The only possible error we can have at this point is EEXIST.
1727 * nd.ni_vp will also be non-NULL in that case.
1729 if (nd.ni_vp == NULL) {
1730 if (vap->va_mode == (mode_t)VNOVAL)
1732 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1733 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1734 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1736 NDFREE(&nd, NDF_ONLY_PNBUF);
1738 nfsrv_object_create(nd.ni_vp);
1739 if (exclusive_flag) {
1742 bcopy(cverf, (caddr_t)&vap->va_atime,
1744 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1749 vap->va_type == VCHR ||
1750 vap->va_type == VBLK ||
1751 vap->va_type == VFIFO
1754 * Handle SysV FIFO node special cases. All other
1755 * devices require super user to access.
1757 if (vap->va_type == VCHR && rdev == 0xffffffff)
1758 vap->va_type = VFIFO;
1759 if (vap->va_type != VFIFO &&
1760 (error = suser_xxx(cred, 0, 0))) {
1763 vap->va_rdev = rdev;
1764 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1766 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1768 NDFREE(&nd, NDF_ONLY_PNBUF);
1775 * release dvp prior to lookup
1783 * Even though LOCKPARENT was cleared, ni_dvp may
1786 nd.ni_cnd.cn_nameiop = LOOKUP;
1787 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1788 nd.ni_cnd.cn_proc = procp;
1789 nd.ni_cnd.cn_cred = cred;
1791 error = lookup(&nd);
1796 /* fall through on certain errors */
1798 nfsrv_object_create(nd.ni_vp);
1799 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1807 if (vap->va_size != -1) {
1808 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1809 (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
1811 nqsrv_getl(nd.ni_vp, ND_WRITE);
1812 tempsize = vap->va_size;
1814 vap->va_size = tempsize;
1815 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1822 bzero((caddr_t)fhp, sizeof(nfh));
1823 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1824 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1826 error = VOP_GETATTR(nd.ni_vp, vap, cred, procp);
1829 if (exclusive_flag && !error &&
1830 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1832 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
1836 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1839 nfsm_srvpostop_fh(fhp);
1840 nfsm_srvpostop_attr(0, vap);
1842 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1845 nfsm_srvfhtom(fhp, v3);
1846 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1847 nfsm_srvfillattr(vap, fp);
1857 if (nd.ni_startdir) {
1858 vrele(nd.ni_startdir);
1859 nd.ni_startdir = NULL;
1863 NDFREE(&nd, NDF_ONLY_PNBUF);
1865 if (nd.ni_dvp == nd.ni_vp)
1876 * nfs v3 mknod service
1879 nfsrv_mknod(nfsd, slp, procp, mrq)
1880 struct nfsrv_descript *nfsd;
1881 struct nfssvc_sock *slp;
1885 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1886 struct sockaddr *nam = nfsd->nd_nam;
1887 caddr_t dpos = nfsd->nd_dpos;
1888 struct ucred *cred = &nfsd->nd_cr;
1889 struct vattr va, dirfor, diraft;
1890 register struct vattr *vap = &va;
1891 register u_int32_t *tl;
1892 struct nameidata nd;
1893 register int32_t t1;
1895 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1896 u_int32_t major, minor;
1899 struct mbuf *mb, *mb2, *mreq;
1900 struct vnode *vp, *dirp = (struct vnode *)0;
1905 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1908 fhp = &nfh.fh_generic;
1910 nfsm_srvnamesiz(len);
1912 nd.ni_cnd.cn_cred = cred;
1913 nd.ni_cnd.cn_nameiop = CREATE;
1914 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1917 * Handle nfs_namei() call. If an error occurs, the nd structure
1918 * is not valid. However, nfsm_*() routines may still jump to
1922 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1923 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1925 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
1927 nfsm_reply(NFSX_WCCDATA(1));
1928 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1932 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1933 vtyp = nfsv3tov_type(*tl);
1934 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1935 error = NFSERR_BADTYPE;
1940 if (vtyp == VCHR || vtyp == VBLK) {
1941 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1942 major = fxdr_unsigned(u_int32_t, *tl++);
1943 minor = fxdr_unsigned(u_int32_t, *tl);
1944 vap->va_rdev = makeudev(major, minor);
1948 * Iff doesn't exist, create it.
1954 vap->va_type = vtyp;
1955 if (vap->va_mode == (mode_t)VNOVAL)
1957 if (vtyp == VSOCK) {
1958 vrele(nd.ni_startdir);
1959 nd.ni_startdir = NULL;
1960 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1961 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1963 NDFREE(&nd, NDF_ONLY_PNBUF);
1965 if (vtyp != VFIFO && (error = suser_xxx(cred, 0, 0)))
1967 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1969 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1971 NDFREE(&nd, NDF_ONLY_PNBUF);
1978 * Release dvp prior to lookup
1983 nd.ni_cnd.cn_nameiop = LOOKUP;
1984 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1985 nd.ni_cnd.cn_proc = procp;
1986 nd.ni_cnd.cn_cred = procp->p_ucred;
1988 error = lookup(&nd);
1993 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1998 * send response, cleanup, return.
2001 if (nd.ni_startdir) {
2002 vrele(nd.ni_startdir);
2003 nd.ni_startdir = NULL;
2005 NDFREE(&nd, NDF_ONLY_PNBUF);
2007 if (nd.ni_dvp == nd.ni_vp)
2015 bzero((caddr_t)fhp, sizeof(nfh));
2016 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2017 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2019 error = VOP_GETATTR(vp, vap, cred, procp);
2026 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2031 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2033 nfsm_srvpostop_fh(fhp);
2034 nfsm_srvpostop_attr(0, vap);
2036 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2042 vrele(nd.ni_startdir);
2043 NDFREE(&nd, NDF_ONLY_PNBUF);
2045 if (nd.ni_dvp == nd.ni_vp)
2056 * nfs remove service
2059 nfsrv_remove(nfsd, slp, procp, mrq)
2060 struct nfsrv_descript *nfsd;
2061 struct nfssvc_sock *slp;
2065 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2066 struct sockaddr *nam = nfsd->nd_nam;
2067 caddr_t dpos = nfsd->nd_dpos;
2068 struct ucred *cred = &nfsd->nd_cr;
2069 struct nameidata nd;
2070 register u_int32_t *tl;
2071 register int32_t t1;
2073 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2074 int v3 = (nfsd->nd_flag & ND_NFSV3);
2076 struct mbuf *mb, *mreq;
2078 struct vattr dirfor, diraft;
2083 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2086 fhp = &nfh.fh_generic;
2088 nfsm_srvnamesiz(len);
2090 nd.ni_cnd.cn_cred = cred;
2091 nd.ni_cnd.cn_nameiop = DELETE;
2092 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2093 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2094 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2097 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2105 if (nd.ni_vp->v_type == VDIR) {
2106 error = EPERM; /* POSIX */
2110 * The root of a mounted filesystem cannot be deleted.
2112 if (nd.ni_vp->v_flag & VROOT) {
2118 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2119 nqsrv_getl(nd.ni_vp, ND_WRITE);
2120 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2121 NDFREE(&nd, NDF_ONLY_PNBUF);
2125 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2129 nfsm_reply(NFSX_WCCDATA(v3));
2131 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2135 NDFREE(&nd, NDF_ONLY_PNBUF);
2137 if (nd.ni_dvp == nd.ni_vp)
2148 * nfs rename service
2151 nfsrv_rename(nfsd, slp, procp, mrq)
2152 struct nfsrv_descript *nfsd;
2153 struct nfssvc_sock *slp;
2157 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2158 struct sockaddr *nam = nfsd->nd_nam;
2159 caddr_t dpos = nfsd->nd_dpos;
2160 struct ucred *cred = &nfsd->nd_cr;
2161 register u_int32_t *tl;
2162 register int32_t t1;
2164 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2165 int tdirfor_ret = 1, tdiraft_ret = 1;
2166 int v3 = (nfsd->nd_flag & ND_NFSV3);
2168 struct mbuf *mb, *mreq;
2169 struct nameidata fromnd, tond;
2170 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2171 struct vnode *tdirp = (struct vnode *)0;
2172 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2174 fhandle_t *ffhp, *tfhp;
2178 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2180 fvp = (struct vnode *)0;
2182 ffhp = &fnfh.fh_generic;
2183 tfhp = &tnfh.fh_generic;
2186 * Clear fields incase goto nfsmout occurs from macro.
2192 nfsm_srvmtofh(ffhp);
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, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2206 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2214 nfsm_reply(2 * NFSX_WCCDATA(v3));
2215 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2216 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2221 nfsm_srvmtofh(tfhp);
2222 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2223 cred->cr_uid = saved_uid;
2224 tond.ni_cnd.cn_cred = cred;
2225 tond.ni_cnd.cn_nameiop = RENAME;
2226 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2227 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2228 &dpos, &tdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2231 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
2244 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2250 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2257 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2265 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2272 if (fvp->v_mount != tdvp->v_mount) {
2286 * If source is the same as the destination (that is the
2287 * same vnode with the same name in the same directory),
2288 * then there is nothing to do.
2290 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2291 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2292 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2293 fromnd.ni_cnd.cn_namelen))
2298 * The VOP_RENAME function releases all vnode references &
2299 * locks prior to returning so we need to clear the pointers
2300 * to bypass cleanup code later on.
2302 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2303 nqsrv_getl(tdvp, ND_WRITE);
2305 nqsrv_getl(tvp, ND_WRITE);
2307 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2308 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2309 fromnd.ni_dvp = NULL;
2310 fromnd.ni_vp = NULL;
2314 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2315 tond.ni_cnd.cn_flags &= ~HASBUF;
2325 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
2327 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
2328 nfsm_reply(2 * NFSX_WCCDATA(v3));
2330 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2331 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2338 * Clear out tond related fields
2342 if (tond.ni_startdir)
2343 vrele(tond.ni_startdir);
2344 NDFREE(&tond, NDF_ONLY_PNBUF);
2346 if (tond.ni_dvp == tond.ni_vp)
2355 * Clear out fromnd related fields
2359 if (fromnd.ni_startdir)
2360 vrele(fromnd.ni_startdir);
2361 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2363 vrele(fromnd.ni_dvp);
2365 vrele(fromnd.ni_vp);
2374 nfsrv_link(nfsd, slp, procp, mrq)
2375 struct nfsrv_descript *nfsd;
2376 struct nfssvc_sock *slp;
2380 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2381 struct sockaddr *nam = nfsd->nd_nam;
2382 caddr_t dpos = nfsd->nd_dpos;
2383 struct ucred *cred = &nfsd->nd_cr;
2384 struct nameidata nd;
2385 register u_int32_t *tl;
2386 register int32_t t1;
2388 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2389 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2391 struct mbuf *mb, *mreq;
2392 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2393 struct vattr dirfor, diraft, at;
2395 fhandle_t *fhp, *dfhp;
2398 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2401 fhp = &nfh.fh_generic;
2402 dfhp = &dnfh.fh_generic;
2404 nfsm_srvmtofh(dfhp);
2405 nfsm_srvnamesiz(len);
2407 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2408 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2410 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2411 nfsm_srvpostop_attr(getret, &at);
2412 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2417 if (vp->v_type == VDIR) {
2418 error = EPERM; /* POSIX */
2421 nd.ni_cnd.cn_cred = cred;
2422 nd.ni_cnd.cn_nameiop = CREATE;
2423 nd.ni_cnd.cn_flags = LOCKPARENT;
2424 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2425 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2428 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2444 if (vp->v_mount != xp->v_mount)
2448 nqsrv_getl(vp, ND_WRITE);
2449 nqsrv_getl(xp, ND_WRITE);
2450 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2451 NDFREE(&nd, NDF_ONLY_PNBUF);
2457 getret = VOP_GETATTR(vp, &at, cred, procp);
2459 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2460 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2462 nfsm_srvpostop_attr(getret, &at);
2463 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2469 NDFREE(&nd, NDF_ONLY_PNBUF);
2475 if (nd.ni_dvp == nd.ni_vp)
2486 * nfs symbolic link service
2489 nfsrv_symlink(nfsd, slp, procp, mrq)
2490 struct nfsrv_descript *nfsd;
2491 struct nfssvc_sock *slp;
2495 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2496 struct sockaddr *nam = nfsd->nd_nam;
2497 caddr_t dpos = nfsd->nd_dpos;
2498 struct ucred *cred = &nfsd->nd_cr;
2499 struct vattr va, dirfor, diraft;
2500 struct nameidata nd;
2501 register struct vattr *vap = &va;
2502 register u_int32_t *tl;
2503 register int32_t t1;
2504 struct nfsv2_sattr *sp;
2505 char *bpos, *pathcp = (char *)0, *cp2;
2508 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2509 int v3 = (nfsd->nd_flag & ND_NFSV3);
2510 struct mbuf *mb, *mreq, *mb2;
2511 struct vnode *dirp = (struct vnode *)0;
2516 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2519 fhp = &nfh.fh_generic;
2521 nfsm_srvnamesiz(len);
2522 nd.ni_cnd.cn_cred = cred;
2523 nd.ni_cnd.cn_nameiop = CREATE;
2524 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2525 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2526 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2529 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2542 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2543 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2544 iv.iov_base = pathcp;
2546 io.uio_resid = len2;
2550 io.uio_segflg = UIO_SYSSPACE;
2551 io.uio_rw = UIO_READ;
2552 io.uio_procp = (struct proc *)0;
2553 nfsm_mtouio(&io, len2);
2555 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2556 vap->va_mode = nfstov_mode(sp->sa_mode);
2558 *(pathcp + len2) = '\0';
2565 * issue symlink op. SAVESTART is set so the underlying path component
2566 * is only freed by the VOP if an error occurs.
2568 if (vap->va_mode == (mode_t)VNOVAL)
2570 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2571 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2573 NDFREE(&nd, NDF_ONLY_PNBUF);
2578 * releases directory prior to potential lookup op.
2586 * Issue lookup. Leave SAVESTART set so we can easily free
2587 * the name buffer later on.
2589 * since LOCKPARENT is not set, ni_dvp will be garbage on
2590 * return whether an error occurs or not.
2592 nd.ni_cnd.cn_nameiop = LOOKUP;
2593 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2594 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2595 nd.ni_cnd.cn_proc = procp;
2596 nd.ni_cnd.cn_cred = cred;
2598 error = lookup(&nd);
2602 bzero((caddr_t)fhp, sizeof(nfh));
2603 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2604 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2606 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2615 * These releases aren't strictly required, does even doing them
2616 * make any sense? XXX can nfsm_reply() block?
2619 FREE(pathcp, M_TEMP);
2623 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2627 if (nd.ni_startdir) {
2628 vrele(nd.ni_startdir);
2629 nd.ni_startdir = NULL;
2631 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2634 nfsm_srvpostop_fh(fhp);
2635 nfsm_srvpostop_attr(0, vap);
2637 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2643 NDFREE(&nd, NDF_ONLY_PNBUF);
2645 if (nd.ni_dvp == nd.ni_vp)
2653 vrele(nd.ni_startdir);
2657 FREE(pathcp, M_TEMP);
2666 nfsrv_mkdir(nfsd, slp, procp, mrq)
2667 struct nfsrv_descript *nfsd;
2668 struct nfssvc_sock *slp;
2672 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2673 struct sockaddr *nam = nfsd->nd_nam;
2674 caddr_t dpos = nfsd->nd_dpos;
2675 struct ucred *cred = &nfsd->nd_cr;
2676 struct vattr va, dirfor, diraft;
2677 register struct vattr *vap = &va;
2678 register struct nfs_fattr *fp;
2679 struct nameidata nd;
2680 register caddr_t cp;
2681 register u_int32_t *tl;
2682 register int32_t t1;
2684 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2685 int v3 = (nfsd->nd_flag & ND_NFSV3);
2687 struct mbuf *mb, *mb2, *mreq;
2688 struct vnode *dirp = NULL;
2694 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2697 fhp = &nfh.fh_generic;
2699 nfsm_srvnamesiz(len);
2700 nd.ni_cnd.cn_cred = cred;
2701 nd.ni_cnd.cn_nameiop = CREATE;
2702 nd.ni_cnd.cn_flags = LOCKPARENT;
2704 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2705 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2708 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2716 nfsm_reply(NFSX_WCCDATA(v3));
2717 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2725 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2726 vap->va_mode = nfstov_mode(*tl++);
2730 * At this point nd.ni_dvp is referenced and exclusively locked and
2731 * nd.ni_vp, if it exists, is referenced but not locked.
2734 vap->va_type = VDIR;
2735 if (nd.ni_vp != NULL) {
2736 NDFREE(&nd, NDF_ONLY_PNBUF);
2742 * Issue mkdir op. Since SAVESTART is not set, the pathname
2743 * component is freed by the VOP call. This will fill-in
2744 * nd.ni_vp, reference, and exclusively lock it.
2746 if (vap->va_mode == (mode_t)VNOVAL)
2748 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2749 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2750 NDFREE(&nd, NDF_ONLY_PNBUF);
2757 bzero((caddr_t)fhp, sizeof(nfh));
2758 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2759 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2761 error = VOP_GETATTR(nd.ni_vp, vap, cred, procp);
2765 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2766 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2769 nfsm_srvpostop_fh(fhp);
2770 nfsm_srvpostop_attr(0, vap);
2772 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2774 nfsm_srvfhtom(fhp, v3);
2775 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2776 nfsm_srvfillattr(vap, fp);
2785 NDFREE(&nd, NDF_ONLY_PNBUF);
2786 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2804 nfsrv_rmdir(nfsd, slp, procp, mrq)
2805 struct nfsrv_descript *nfsd;
2806 struct nfssvc_sock *slp;
2810 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2811 struct sockaddr *nam = nfsd->nd_nam;
2812 caddr_t dpos = nfsd->nd_dpos;
2813 struct ucred *cred = &nfsd->nd_cr;
2814 register u_int32_t *tl;
2815 register int32_t t1;
2817 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2818 int v3 = (nfsd->nd_flag & ND_NFSV3);
2820 struct mbuf *mb, *mreq;
2821 struct vnode *vp, *dirp = (struct vnode *)0;
2822 struct vattr dirfor, diraft;
2825 struct nameidata nd;
2828 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2831 fhp = &nfh.fh_generic;
2833 nfsm_srvnamesiz(len);
2834 nd.ni_cnd.cn_cred = cred;
2835 nd.ni_cnd.cn_nameiop = DELETE;
2836 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2837 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2838 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2841 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2849 nfsm_reply(NFSX_WCCDATA(v3));
2850 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2855 if (vp->v_type != VDIR) {
2860 * No rmdir "." please.
2862 if (nd.ni_dvp == vp) {
2867 * The root of a mounted filesystem cannot be deleted.
2869 if (vp->v_flag & VROOT)
2873 * Issue or abort op. Since SAVESTART is not set, path name
2874 * component is freed by the VOP after either.
2877 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2878 nqsrv_getl(vp, ND_WRITE);
2879 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2881 NDFREE(&nd, NDF_ONLY_PNBUF);
2884 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2885 nfsm_reply(NFSX_WCCDATA(v3));
2887 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2893 NDFREE(&nd, NDF_ONLY_PNBUF);
2897 if (nd.ni_dvp == nd.ni_vp)
2909 * nfs readdir service
2910 * - mallocs what it thinks is enough to read
2911 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2912 * - calls VOP_READDIR()
2913 * - loops around building the reply
2914 * if the output generated exceeds count break out of loop
2915 * The nfsm_clget macro is used here so that the reply will be packed
2916 * tightly in mbuf clusters.
2917 * - it only knows that it has encountered eof when the VOP_READDIR()
2919 * - as such one readdir rpc will return eof false although you are there
2920 * and then the next will return eof
2921 * - it trims out records with d_fileno == 0
2922 * this doesn't matter for Unix clients, but they might confuse clients
2924 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2925 * than requested, but this may not apply to all filesystems. For
2926 * example, client NFS does not { although it is never remote mounted
2928 * The alternate call nfsrv_readdirplus() does lookups as well.
2929 * PS: The NFS protocol spec. does not clarify what the "count" byte
2930 * argument is a count of.. just name strings and file id's or the
2931 * entire reply rpc or ...
2932 * I tried just file name and id sizes and it confused the Sun client,
2933 * so I am using the full rpc size now. The "paranoia.." comment refers
2934 * to including the status longwords that are not a part of the dir.
2935 * "entry" structures, but are in the rpc.
2939 u_int32_t fl_postopok;
2940 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2942 u_int32_t fl_fhsize;
2943 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2947 nfsrv_readdir(nfsd, slp, procp, mrq)
2948 struct nfsrv_descript *nfsd;
2949 struct nfssvc_sock *slp;
2953 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2954 struct sockaddr *nam = nfsd->nd_nam;
2955 caddr_t dpos = nfsd->nd_dpos;
2956 struct ucred *cred = &nfsd->nd_cr;
2957 register char *bp, *be;
2958 register struct mbuf *mp;
2959 register struct dirent *dp;
2960 register caddr_t cp;
2961 register u_int32_t *tl;
2962 register int32_t t1;
2964 struct mbuf *mb, *mb2, *mreq, *mp2;
2965 char *cpos, *cend, *cp2, *rbuf;
2966 struct vnode *vp = NULL;
2972 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2973 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2974 int v3 = (nfsd->nd_flag & ND_NFSV3);
2975 u_quad_t frev, off, toff, verf;
2976 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2978 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2979 fhp = &nfh.fh_generic;
2982 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2983 toff = fxdr_hyper(tl);
2985 verf = fxdr_hyper(tl);
2988 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2989 toff = fxdr_unsigned(u_quad_t, *tl++);
2990 verf = 0; /* shut up gcc */
2993 cnt = fxdr_unsigned(int, *tl);
2994 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2995 xfer = NFS_SRVMAXDATA(nfsd);
3001 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3002 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3003 if (!error && vp->v_type != VDIR) {
3009 nfsm_reply(NFSX_UNSIGNED);
3010 nfsm_srvpostop_attr(getret, &at);
3016 * Obtain lock on vnode for this section of the code
3019 nqsrv_getl(vp, ND_READ);
3021 error = getret = VOP_GETATTR(vp, &at, cred, procp);
3024 * XXX This check may be too strict for Solaris 2.5 clients.
3026 if (!error && toff && verf && verf != at.va_filerev)
3027 error = NFSERR_BAD_COOKIE;
3031 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
3035 nfsm_reply(NFSX_POSTOPATTR(v3));
3036 nfsm_srvpostop_attr(getret, &at);
3040 VOP_UNLOCK(vp, 0, procp);
3043 * end section. Allocate rbuf and continue
3045 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3048 iv.iov_len = fullsiz;
3051 io.uio_offset = (off_t)off;
3052 io.uio_resid = fullsiz;
3053 io.uio_segflg = UIO_SYSSPACE;
3054 io.uio_rw = UIO_READ;
3055 io.uio_procp = (struct proc *)0;
3057 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
3059 free((caddr_t)cookies, M_TEMP);
3062 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3063 off = (off_t)io.uio_offset;
3064 if (!cookies && !error)
3065 error = NFSERR_PERM;
3067 getret = VOP_GETATTR(vp, &at, cred, procp);
3071 VOP_UNLOCK(vp, 0, procp);
3075 free((caddr_t)rbuf, M_TEMP);
3077 free((caddr_t)cookies, M_TEMP);
3078 nfsm_reply(NFSX_POSTOPATTR(v3));
3079 nfsm_srvpostop_attr(getret, &at);
3084 siz -= io.uio_resid;
3087 * If nothing read, return eof
3093 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3096 nfsm_srvpostop_attr(getret, &at);
3097 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3098 txdr_hyper(at.va_filerev, tl);
3101 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3104 FREE((caddr_t)rbuf, M_TEMP);
3105 FREE((caddr_t)cookies, M_TEMP);
3112 * Check for degenerate cases of nothing useful read.
3113 * If so go try again
3117 dp = (struct dirent *)cpos;
3120 * For some reason FreeBSD's ufs_readdir() chooses to back the
3121 * directory offset up to a block boundary, so it is necessary to
3122 * skip over the records that preceed the requested offset. This
3123 * requires the assumption that file offset cookies monotonically
3126 while (cpos < cend && ncookies > 0 &&
3127 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3128 ((u_quad_t)(*cookiep)) <= toff)) {
3129 cpos += dp->d_reclen;
3130 dp = (struct dirent *)cpos;
3134 if (cpos >= cend || ncookies == 0) {
3140 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3141 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3143 nfsm_srvpostop_attr(getret, &at);
3144 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3145 txdr_hyper(at.va_filerev, tl);
3149 be = bp + M_TRAILINGSPACE(mp);
3151 /* Loop through the records and build reply */
3152 while (cpos < cend && ncookies > 0) {
3153 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3154 nlen = dp->d_namlen;
3155 rem = nfsm_rndup(nlen) - nlen;
3156 len += (4 * NFSX_UNSIGNED + nlen + rem);
3158 len += 2 * NFSX_UNSIGNED;
3164 * Build the directory record xdr from
3169 bp += NFSX_UNSIGNED;
3173 bp += NFSX_UNSIGNED;
3176 *tl = txdr_unsigned(dp->d_fileno);
3177 bp += NFSX_UNSIGNED;
3179 *tl = txdr_unsigned(nlen);
3180 bp += NFSX_UNSIGNED;
3182 /* And loop around copying the name */
3191 bcopy(cp, bp, tsiz);
3197 /* And null pad to a int32_t boundary */
3198 for (i = 0; i < rem; i++)
3202 /* Finish off the record */
3205 bp += NFSX_UNSIGNED;
3208 *tl = txdr_unsigned(*cookiep);
3209 bp += NFSX_UNSIGNED;
3211 cpos += dp->d_reclen;
3212 dp = (struct dirent *)cpos;
3220 bp += NFSX_UNSIGNED;
3226 bp += NFSX_UNSIGNED;
3229 mp->m_len = bp - mtod(mp, caddr_t);
3231 mp->m_len += bp - bpos;
3232 FREE((caddr_t)rbuf, M_TEMP);
3233 FREE((caddr_t)cookies, M_TEMP);
3242 nfsrv_readdirplus(nfsd, slp, procp, mrq)
3243 struct nfsrv_descript *nfsd;
3244 struct nfssvc_sock *slp;
3248 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3249 struct sockaddr *nam = nfsd->nd_nam;
3250 caddr_t dpos = nfsd->nd_dpos;
3251 struct ucred *cred = &nfsd->nd_cr;
3252 register char *bp, *be;
3253 register struct mbuf *mp;
3254 register struct dirent *dp;
3255 register caddr_t cp;
3256 register u_int32_t *tl;
3257 register int32_t t1;
3259 struct mbuf *mb, *mb2, *mreq, *mp2;
3260 char *cpos, *cend, *cp2, *rbuf;
3261 struct vnode *vp = NULL, *nvp;
3264 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3267 struct vattr va, at, *vap = &va;
3268 struct nfs_fattr *fp;
3269 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3270 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3271 u_quad_t frev, off, toff, verf;
3272 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3274 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3275 fhp = &nfh.fh_generic;
3277 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3278 toff = fxdr_hyper(tl);
3280 verf = fxdr_hyper(tl);
3282 siz = fxdr_unsigned(int, *tl++);
3283 cnt = fxdr_unsigned(int, *tl);
3285 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3286 xfer = NFS_SRVMAXDATA(nfsd);
3292 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3293 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3294 if (!error && vp->v_type != VDIR) {
3300 nfsm_reply(NFSX_UNSIGNED);
3301 nfsm_srvpostop_attr(getret, &at);
3305 error = getret = VOP_GETATTR(vp, &at, cred, procp);
3308 * XXX This check may be too strict for Solaris 2.5 clients.
3310 if (!error && toff && verf && verf != at.va_filerev)
3311 error = NFSERR_BAD_COOKIE;
3314 nqsrv_getl(vp, ND_READ);
3315 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
3320 nfsm_reply(NFSX_V3POSTOPATTR);
3321 nfsm_srvpostop_attr(getret, &at);
3325 VOP_UNLOCK(vp, 0, procp);
3326 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3329 iv.iov_len = fullsiz;
3332 io.uio_offset = (off_t)off;
3333 io.uio_resid = fullsiz;
3334 io.uio_segflg = UIO_SYSSPACE;
3335 io.uio_rw = UIO_READ;
3336 io.uio_procp = (struct proc *)0;
3338 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
3340 free((caddr_t)cookies, M_TEMP);
3343 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3344 off = (u_quad_t)io.uio_offset;
3345 getret = VOP_GETATTR(vp, &at, cred, procp);
3346 VOP_UNLOCK(vp, 0, procp);
3347 if (!cookies && !error)
3348 error = NFSERR_PERM;
3355 free((caddr_t)cookies, M_TEMP);
3356 free((caddr_t)rbuf, M_TEMP);
3357 nfsm_reply(NFSX_V3POSTOPATTR);
3358 nfsm_srvpostop_attr(getret, &at);
3363 siz -= io.uio_resid;
3366 * If nothing read, return eof
3372 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3374 nfsm_srvpostop_attr(getret, &at);
3375 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3376 txdr_hyper(at.va_filerev, tl);
3380 FREE((caddr_t)cookies, M_TEMP);
3381 FREE((caddr_t)rbuf, M_TEMP);
3388 * Check for degenerate cases of nothing useful read.
3389 * If so go try again
3393 dp = (struct dirent *)cpos;
3396 * For some reason FreeBSD's ufs_readdir() chooses to back the
3397 * directory offset up to a block boundary, so it is necessary to
3398 * skip over the records that preceed the requested offset. This
3399 * requires the assumption that file offset cookies monotonically
3402 while (cpos < cend && ncookies > 0 &&
3403 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3404 ((u_quad_t)(*cookiep)) <= toff)) {
3405 cpos += dp->d_reclen;
3406 dp = (struct dirent *)cpos;
3410 if (cpos >= cend || ncookies == 0) {
3417 * Probe one of the directory entries to see if the filesystem
3420 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3421 error = NFSERR_NOTSUPP;
3424 free((caddr_t)cookies, M_TEMP);
3425 free((caddr_t)rbuf, M_TEMP);
3426 nfsm_reply(NFSX_V3POSTOPATTR);
3427 nfsm_srvpostop_attr(getret, &at);
3434 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3436 nfsm_srvpostop_attr(getret, &at);
3437 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3438 txdr_hyper(at.va_filerev, tl);
3441 be = bp + M_TRAILINGSPACE(mp);
3443 /* Loop through the records and build reply */
3444 while (cpos < cend && ncookies > 0) {
3445 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3446 nlen = dp->d_namlen;
3447 rem = nfsm_rndup(nlen)-nlen;
3450 * For readdir_and_lookup get the vnode using
3453 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3455 bzero((caddr_t)nfhp, NFSX_V3FH);
3457 nvp->v_mount->mnt_stat.f_fsid;
3458 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3463 if (VOP_GETATTR(nvp, vap, cred, procp)) {
3472 * If either the dircount or maxcount will be
3473 * exceeded, get out now. Both of these lengths
3474 * are calculated conservatively, including all
3477 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3479 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3480 if (len > cnt || dirlen > fullsiz) {
3486 * Build the directory record xdr from
3489 fp = (struct nfs_fattr *)&fl.fl_fattr;
3490 nfsm_srvfillattr(vap, fp);
3491 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3492 fl.fl_fhok = nfs_true;
3493 fl.fl_postopok = nfs_true;
3494 fl.fl_off.nfsuquad[0] = 0;
3495 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3499 bp += NFSX_UNSIGNED;
3502 bp += NFSX_UNSIGNED;
3504 *tl = txdr_unsigned(dp->d_fileno);
3505 bp += NFSX_UNSIGNED;
3507 *tl = txdr_unsigned(nlen);
3508 bp += NFSX_UNSIGNED;
3510 /* And loop around copying the name */
3515 if ((bp + xfer) > be)
3519 bcopy(cp, bp, tsiz);
3525 /* And null pad to a int32_t boundary */
3526 for (i = 0; i < rem; i++)
3530 * Now copy the flrep structure out.
3532 xfer = sizeof (struct flrep);
3536 if ((bp + xfer) > be)
3540 bcopy(cp, bp, tsiz);
3548 cpos += dp->d_reclen;
3549 dp = (struct dirent *)cpos;
3557 bp += NFSX_UNSIGNED;
3563 bp += NFSX_UNSIGNED;
3566 mp->m_len = bp - mtod(mp, caddr_t);
3568 mp->m_len += bp - bpos;
3569 FREE((caddr_t)cookies, M_TEMP);
3570 FREE((caddr_t)rbuf, M_TEMP);
3578 * nfs commit service
3581 nfsrv_commit(nfsd, slp, procp, mrq)
3582 struct nfsrv_descript *nfsd;
3583 struct nfssvc_sock *slp;
3587 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3588 struct sockaddr *nam = nfsd->nd_nam;
3589 caddr_t dpos = nfsd->nd_dpos;
3590 struct ucred *cred = &nfsd->nd_cr;
3591 struct vattr bfor, aft;
3592 struct vnode *vp = NULL;
3595 register u_int32_t *tl;
3596 register int32_t t1;
3598 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3600 struct mbuf *mb, *mb2, *mreq;
3603 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3607 fhp = &nfh.fh_generic;
3609 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3612 * XXX At this time VOP_FSYNC() does not accept offset and byte
3613 * count parameters, so these arguments are useless (someday maybe).
3615 off = fxdr_hyper(tl);
3617 cnt = fxdr_unsigned(int, *tl);
3618 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3619 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3621 nfsm_reply(2 * NFSX_UNSIGNED);
3622 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3626 for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
3628 if (cnt > MAX_COMMIT_COUNT) {
3630 * Give up and do the whole thing
3633 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3634 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3636 error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
3639 * Locate and synchronously write any buffers that fall
3640 * into the requested range. Note: we are assuming that
3641 * f_iosize is a power of 2.
3643 int iosize = vp->v_mount->mnt_stat.f_iosize;
3644 int iomask = iosize - 1;
3649 * Align to iosize boundry, super-align to page boundry.
3652 cnt += off & iomask;
3653 off &= ~(u_quad_t)iomask;
3655 if (off & PAGE_MASK) {
3656 cnt += off & PAGE_MASK;
3657 off &= ~(u_quad_t)PAGE_MASK;
3659 lblkno = off / iosize;
3662 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3663 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3671 * If we have a buffer and it is marked B_DELWRI we
3672 * have to lock and write it. Otherwise the prior
3673 * write is assumed to have already been committed.
3675 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3676 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3677 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3678 continue; /* retry */
3681 bp->b_flags &= ~B_ASYNC;
3682 VOP_BWRITE(bp->b_vp, bp);
3694 aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
3697 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3698 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3700 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3701 if (nfsver.tv_sec == 0)
3703 *tl++ = txdr_unsigned(nfsver.tv_sec);
3704 *tl = txdr_unsigned(nfsver.tv_usec);
3715 * nfs statfs service
3718 nfsrv_statfs(nfsd, slp, procp, mrq)
3719 struct nfsrv_descript *nfsd;
3720 struct nfssvc_sock *slp;
3724 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3725 struct sockaddr *nam = nfsd->nd_nam;
3726 caddr_t dpos = nfsd->nd_dpos;
3727 struct ucred *cred = &nfsd->nd_cr;
3728 register struct statfs *sf;
3729 register struct nfs_statfs *sfp;
3730 register u_int32_t *tl;
3731 register int32_t t1;
3733 int error = 0, rdonly, cache, getret = 1;
3734 int v3 = (nfsd->nd_flag & ND_NFSV3);
3736 struct mbuf *mb, *mb2, *mreq;
3737 struct vnode *vp = NULL;
3741 struct statfs statfs;
3742 u_quad_t frev, tval;
3744 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3748 fhp = &nfh.fh_generic;
3750 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3751 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3753 nfsm_reply(NFSX_UNSIGNED);
3754 nfsm_srvpostop_attr(getret, &at);
3759 error = VFS_STATFS(vp->v_mount, sf, procp);
3760 getret = VOP_GETATTR(vp, &at, cred, procp);
3763 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3765 nfsm_srvpostop_attr(getret, &at);
3770 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3772 tval = (u_quad_t)sf->f_blocks;
3773 tval *= (u_quad_t)sf->f_bsize;
3774 txdr_hyper(tval, &sfp->sf_tbytes);
3775 tval = (u_quad_t)sf->f_bfree;
3776 tval *= (u_quad_t)sf->f_bsize;
3777 txdr_hyper(tval, &sfp->sf_fbytes);
3778 tval = (u_quad_t)sf->f_bavail;
3779 tval *= (u_quad_t)sf->f_bsize;
3780 txdr_hyper(tval, &sfp->sf_abytes);
3781 sfp->sf_tfiles.nfsuquad[0] = 0;
3782 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3783 sfp->sf_ffiles.nfsuquad[0] = 0;
3784 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3785 sfp->sf_afiles.nfsuquad[0] = 0;
3786 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3787 sfp->sf_invarsec = 0;
3789 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3790 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3791 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3792 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3793 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3802 * nfs fsinfo service
3805 nfsrv_fsinfo(nfsd, slp, procp, mrq)
3806 struct nfsrv_descript *nfsd;
3807 struct nfssvc_sock *slp;
3811 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3812 struct sockaddr *nam = nfsd->nd_nam;
3813 caddr_t dpos = nfsd->nd_dpos;
3814 struct ucred *cred = &nfsd->nd_cr;
3815 register u_int32_t *tl;
3816 register struct nfsv3_fsinfo *sip;
3817 register int32_t t1;
3819 int error = 0, rdonly, cache, getret = 1, pref;
3821 struct mbuf *mb, *mb2, *mreq;
3822 struct vnode *vp = NULL;
3826 u_quad_t frev, maxfsize;
3829 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3833 fhp = &nfh.fh_generic;
3835 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3836 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3838 nfsm_reply(NFSX_UNSIGNED);
3839 nfsm_srvpostop_attr(getret, &at);
3844 /* XXX Try to make a guess on the max file size. */
3845 VFS_STATFS(vp->v_mount, &sb, procp);
3846 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3848 getret = VOP_GETATTR(vp, &at, cred, procp);
3851 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3852 nfsm_srvpostop_attr(getret, &at);
3853 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3857 * There should be file system VFS OP(s) to get this information.
3858 * For now, assume ufs.
3860 if (slp->ns_so->so_type == SOCK_DGRAM)
3861 pref = NFS_MAXDGRAMDATA;
3864 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3865 sip->fs_rtpref = txdr_unsigned(pref);
3866 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3867 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3868 sip->fs_wtpref = txdr_unsigned(pref);
3869 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3870 sip->fs_dtpref = txdr_unsigned(pref);
3871 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3872 sip->fs_timedelta.nfsv3_sec = 0;
3873 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3874 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3875 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3876 NFSV3FSINFO_CANSETTIME);
3884 * nfs pathconf service
3887 nfsrv_pathconf(nfsd, slp, procp, mrq)
3888 struct nfsrv_descript *nfsd;
3889 struct nfssvc_sock *slp;
3893 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3894 struct sockaddr *nam = nfsd->nd_nam;
3895 caddr_t dpos = nfsd->nd_dpos;
3896 struct ucred *cred = &nfsd->nd_cr;
3897 register u_int32_t *tl;
3898 register struct nfsv3_pathconf *pc;
3899 register int32_t t1;
3901 int error = 0, rdonly, cache, getret = 1;
3902 register_t linkmax, namemax, chownres, notrunc;
3904 struct mbuf *mb, *mb2, *mreq;
3905 struct vnode *vp = NULL;
3911 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3915 fhp = &nfh.fh_generic;
3917 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3918 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3920 nfsm_reply(NFSX_UNSIGNED);
3921 nfsm_srvpostop_attr(getret, &at);
3925 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3927 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3929 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3931 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3932 getret = VOP_GETATTR(vp, &at, cred, procp);
3935 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3936 nfsm_srvpostop_attr(getret, &at);
3941 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3943 pc->pc_linkmax = txdr_unsigned(linkmax);
3944 pc->pc_namemax = txdr_unsigned(namemax);
3945 pc->pc_notrunc = txdr_unsigned(notrunc);
3946 pc->pc_chownrestricted = txdr_unsigned(chownres);
3949 * These should probably be supported by VOP_PATHCONF(), but
3950 * until msdosfs is exportable (why would you want to?), the
3951 * Unix defaults should be ok.
3953 pc->pc_caseinsensitive = nfs_false;
3954 pc->pc_casepreserving = nfs_true;
3962 * Null operation, used by clients to ping server
3966 nfsrv_null(nfsd, slp, procp, mrq)
3967 struct nfsrv_descript *nfsd;
3968 struct nfssvc_sock *slp;
3972 struct mbuf *mrep = nfsd->nd_mrep;
3974 int error = NFSERR_RETVOID, cache;
3975 struct mbuf *mb, *mreq;
3978 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3987 * No operation, used for obsolete procedures
3991 nfsrv_noop(nfsd, slp, procp, mrq)
3992 struct nfsrv_descript *nfsd;
3993 struct nfssvc_sock *slp;
3997 struct mbuf *mrep = nfsd->nd_mrep;
4000 struct mbuf *mb, *mreq;
4003 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4007 if (nfsd->nd_repstat)
4008 error = nfsd->nd_repstat;
4010 error = EPROCUNAVAIL;
4017 * Perform access checking for vnodes obtained from file handles that would
4018 * refer to files already opened by a Unix client. You cannot just use
4019 * vn_writechk() and VOP_ACCESS() for two reasons.
4020 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4021 * 2 - The owner is to be given access irrespective of mode bits for some
4022 * operations, so that processes that chmod after opening a file don't
4023 * break. I don't like this because it opens a security hole, but since
4024 * the nfs server opens a security hole the size of a barn door anyhow,
4027 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4028 * will return EPERM instead of EACCESS. EPERM is always an error.
4031 nfsrv_access(vp, flags, cred, rdonly, p, override)
4032 register struct vnode *vp;
4034 register struct ucred *cred;
4042 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4043 if (flags & VWRITE) {
4044 /* Just vn_writechk() changed to check rdonly */
4046 * Disallow write attempts on read-only file systems;
4047 * unless the file is a socket or a block or character
4048 * device resident on the file system.
4050 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4051 switch (vp->v_type) {
4061 * If there's shared text associated with
4062 * the inode, we can't allow writing.
4064 if (vp->v_flag & VTEXT)
4067 error = VOP_GETATTR(vp, &vattr, cred, p);
4070 error = VOP_ACCESS(vp, flags, cred, p);
4072 * Allow certain operations for the owner (reads and writes
4073 * on files that are already open).
4075 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4079 #endif /* NFS_NOSERVER */