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.2 2003/06/17 04:28:54 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>
96 #include <nfs/nfsproto.h>
97 #include <nfs/rpcv2.h>
99 #include <nfs/xdr_subs.h>
100 #include <nfs/nfsm_subs.h>
101 #include <nfs/nqnfs.h>
104 #define nfsdbprintf(info) printf info
106 #define nfsdbprintf(info)
109 #define MAX_COMMIT_COUNT (1024 * 1024)
111 #define NUM_HEURISTIC 64
112 #define NHUSE_INIT 64
114 #define NHUSE_MAX 2048
116 static struct nfsheur {
117 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
118 off_t nh_nextr; /* next offset for sequential detection */
119 int nh_use; /* use count for selection */
120 int nh_seqcount; /* heuristic */
121 } nfsheur[NUM_HEURISTIC];
123 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
126 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
129 extern u_int32_t nfs_xdrneg1;
130 extern u_int32_t nfs_false, nfs_true;
131 extern enum vtype nv3tov_type[8];
132 extern struct nfsstats nfsstats;
134 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
135 int nfsrvw_procrastinate_v3 = 0;
137 static struct timeval nfsver = { 0 };
139 SYSCTL_DECL(_vfs_nfs);
141 static int nfs_async;
142 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
143 static int nfs_commit_blks;
144 static int nfs_commit_miss;
145 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
146 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
148 static int nfsrv_access __P((struct vnode *,int,struct ucred *,int,
149 struct proc *, int));
150 static void nfsrvw_coalesce __P((struct nfsrv_descript *,
151 struct nfsrv_descript *));
154 * Clear nameidata fields that are tested in nsfmout cleanup code prior
155 * to using first nfsm macro (that might jump to the cleanup code).
160 ndclear(struct nameidata *nd)
162 nd->ni_cnd.cn_flags = 0;
165 nd->ni_startdir = NULL;
169 * nfs v3 access service
172 nfsrv3_access(nfsd, slp, procp, mrq)
173 struct nfsrv_descript *nfsd;
174 struct nfssvc_sock *slp;
178 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
179 struct sockaddr *nam = nfsd->nd_nam;
180 caddr_t dpos = nfsd->nd_dpos;
181 struct ucred *cred = &nfsd->nd_cr;
182 struct vnode *vp = NULL;
185 register u_int32_t *tl;
188 int error = 0, rdonly, cache, getret;
190 struct mbuf *mb, *mreq, *mb2;
191 struct vattr vattr, *vap = &vattr;
192 u_long testmode, nfsmode;
195 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
199 fhp = &nfh.fh_generic;
201 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
202 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
203 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
205 nfsm_reply(NFSX_UNSIGNED);
206 nfsm_srvpostop_attr(1, (struct vattr *)0);
210 nfsmode = fxdr_unsigned(u_int32_t, *tl);
211 if ((nfsmode & NFSV3ACCESS_READ) &&
212 nfsrv_access(vp, VREAD, cred, rdonly, procp, 0))
213 nfsmode &= ~NFSV3ACCESS_READ;
214 if (vp->v_type == VDIR)
215 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
218 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
219 if ((nfsmode & testmode) &&
220 nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0))
221 nfsmode &= ~testmode;
222 if (vp->v_type == VDIR)
223 testmode = NFSV3ACCESS_LOOKUP;
225 testmode = NFSV3ACCESS_EXECUTE;
226 if ((nfsmode & testmode) &&
227 nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0))
228 nfsmode &= ~testmode;
229 getret = VOP_GETATTR(vp, vap, cred, procp);
232 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
233 nfsm_srvpostop_attr(getret, vap);
234 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
235 *tl = txdr_unsigned(nfsmode);
243 * nfs getattr service
246 nfsrv_getattr(nfsd, slp, procp, mrq)
247 struct nfsrv_descript *nfsd;
248 struct nfssvc_sock *slp;
252 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
253 struct sockaddr *nam = nfsd->nd_nam;
254 caddr_t dpos = nfsd->nd_dpos;
255 struct ucred *cred = &nfsd->nd_cr;
256 register struct nfs_fattr *fp;
258 register struct vattr *vap = &va;
259 struct vnode *vp = NULL;
262 register u_int32_t *tl;
265 int error = 0, rdonly, cache;
267 struct mbuf *mb, *mb2, *mreq;
270 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
271 fhp = &nfh.fh_generic;
273 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
274 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
280 nqsrv_getl(vp, ND_READ);
281 error = VOP_GETATTR(vp, vap, cred, procp);
284 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
289 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
290 nfsm_srvfillattr(vap, fp);
300 * nfs setattr service
303 nfsrv_setattr(nfsd, slp, procp, mrq)
304 struct nfsrv_descript *nfsd;
305 struct nfssvc_sock *slp;
309 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
310 struct sockaddr *nam = nfsd->nd_nam;
311 caddr_t dpos = nfsd->nd_dpos;
312 struct ucred *cred = &nfsd->nd_cr;
313 struct vattr va, preat;
314 register struct vattr *vap = &va;
315 register struct nfsv2_sattr *sp;
316 register struct nfs_fattr *fp;
317 struct vnode *vp = NULL;
320 register u_int32_t *tl;
323 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
324 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
326 struct mbuf *mb, *mb2, *mreq;
328 struct timespec guard;
330 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
331 fhp = &nfh.fh_generic;
336 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
337 gcheck = fxdr_unsigned(int, *tl);
339 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
340 fxdr_nfsv3time(tl, &guard);
343 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
345 * Nah nah nah nah na nah
346 * There is a bug in the Sun client that puts 0xffff in the mode
347 * field of sattr when it should put in 0xffffffff. The u_short
348 * doesn't sign extend.
349 * --> check the low order 2 bytes for 0xffff
351 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
352 vap->va_mode = nfstov_mode(sp->sa_mode);
353 if (sp->sa_uid != nfs_xdrneg1)
354 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
355 if (sp->sa_gid != nfs_xdrneg1)
356 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
357 if (sp->sa_size != nfs_xdrneg1)
358 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
359 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
361 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
363 vap->va_atime.tv_sec =
364 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
365 vap->va_atime.tv_nsec = 0;
368 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
369 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
374 * Now that we have all the fields, lets do it.
376 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
377 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
379 nfsm_reply(2 * NFSX_UNSIGNED);
380 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
386 * vp now an active resource, pay careful attention to cleanup
389 nqsrv_getl(vp, ND_WRITE);
391 error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
392 if (!error && gcheck &&
393 (preat.va_ctime.tv_sec != guard.tv_sec ||
394 preat.va_ctime.tv_nsec != guard.tv_nsec))
395 error = NFSERR_NOT_SYNC;
399 nfsm_reply(NFSX_WCCDATA(v3));
400 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
407 * If the size is being changed write acces is required, otherwise
408 * just check for a read only file system.
410 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
411 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
416 if (vp->v_type == VDIR) {
419 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
423 error = VOP_SETATTR(vp, vap, cred, procp);
424 postat_ret = VOP_GETATTR(vp, vap, cred, procp);
430 nfsm_reply(NFSX_WCCORFATTR(v3));
432 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
436 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
437 nfsm_srvfillattr(vap, fp);
451 nfsrv_lookup(nfsd, slp, procp, mrq)
452 struct nfsrv_descript *nfsd;
453 struct nfssvc_sock *slp;
457 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
458 struct sockaddr *nam = nfsd->nd_nam;
459 caddr_t dpos = nfsd->nd_dpos;
460 struct ucred *cred = &nfsd->nd_cr;
461 register struct nfs_fattr *fp;
462 struct nameidata nd, ind, *ndp = &nd;
463 struct vnode *vp, *dirp = NULL;
467 register u_int32_t *tl;
470 int error = 0, cache, len, dirattr_ret = 1;
471 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
473 struct mbuf *mb, *mb2, *mreq;
474 struct vattr va, dirattr, *vap = &va;
477 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
480 fhp = &nfh.fh_generic;
482 nfsm_srvnamesiz(len);
484 pubflag = nfs_ispublicfh(fhp);
486 nd.ni_cnd.cn_cred = cred;
487 nd.ni_cnd.cn_nameiop = LOOKUP;
488 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
489 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
490 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
493 * namei failure, only dirp to cleanup. Clear out garbarge from
494 * structure in case macros jump to nfsmout.
500 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
505 nfsm_reply(NFSX_POSTOPATTR(v3));
506 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
512 * Locate index file for public filehandle
514 * error is 0 on entry and 0 on exit from this block.
518 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
520 * Setup call to lookup() to see if we can find
521 * the index file. Arguably, this doesn't belong
522 * in a kernel.. Ugh. If an error occurs, do not
523 * try to install an index file and then clear the
526 * When we replace nd with ind and redirect ndp,
527 * maintenance of ni_startdir and ni_vp shift to
528 * ind and we have to clean them up in the old nd.
529 * However, the cnd resource continues to be maintained
530 * via the original nd. Confused? You aren't alone!
533 VOP_UNLOCK(nd.ni_vp, 0, procp);
534 ind.ni_pathlen = strlen(nfs_pub.np_index);
535 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
537 ind.ni_startdir = nd.ni_vp;
538 VREF(ind.ni_startdir);
540 error = lookup(&ind);
545 * Found an index file. Get rid of
546 * the old references. transfer nd.ni_vp'
552 vrele(nd.ni_startdir);
553 nd.ni_startdir = NULL;
559 * If the public filehandle was used, check that this lookup
560 * didn't result in a filehandle outside the publicly exported
561 * filesystem. We clear the poor vp here to avoid lockups due
565 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
574 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
581 * Resources at this point:
582 * ndp->ni_vp may not be NULL
587 nfsm_reply(NFSX_POSTOPATTR(v3));
588 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
593 nqsrv_getl(ndp->ni_startdir, ND_READ);
596 * Clear out some resources prior to potentially blocking. This
597 * is not as critical as ni_dvp resources in other routines, but
600 vrele(ndp->ni_startdir);
601 ndp->ni_startdir = NULL;
602 NDFREE(&nd, NDF_ONLY_PNBUF);
605 * Get underlying attribute, then release remaining resources ( for
606 * the same potential blocking reason ) and reply.
609 bzero((caddr_t)fhp, sizeof(nfh));
610 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
611 error = VFS_VPTOFH(vp, &fhp->fh_fid);
613 error = VOP_GETATTR(vp, vap, cred, procp);
617 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
619 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
623 nfsm_srvfhtom(fhp, v3);
625 nfsm_srvpostop_attr(0, vap);
626 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
628 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
629 nfsm_srvfillattr(vap, fp);
635 NDFREE(&nd, NDF_ONLY_PNBUF);
636 if (ndp->ni_startdir)
637 vrele(ndp->ni_startdir);
644 * nfs readlink service
647 nfsrv_readlink(nfsd, slp, procp, mrq)
648 struct nfsrv_descript *nfsd;
649 struct nfssvc_sock *slp;
653 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
654 struct sockaddr *nam = nfsd->nd_nam;
655 caddr_t dpos = nfsd->nd_dpos;
656 struct ucred *cred = &nfsd->nd_cr;
657 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
658 register struct iovec *ivp = iv;
659 register struct mbuf *mp;
660 register u_int32_t *tl;
663 int error = 0, rdonly, cache, i, tlen, len, getret;
664 int v3 = (nfsd->nd_flag & ND_NFSV3);
666 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
667 struct vnode *vp = NULL;
671 struct uio io, *uiop = &io;
674 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
676 mp2 = (struct mbuf *)0;
679 fhp = &nfh.fh_generic;
683 while (len < NFS_MAXPATHLEN) {
684 MGET(mp, M_WAIT, MT_DATA);
686 mp->m_len = NFSMSIZ(mp);
693 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
694 mp->m_len = NFS_MAXPATHLEN-len;
695 len = NFS_MAXPATHLEN;
698 ivp->iov_base = mtod(mp, caddr_t);
699 ivp->iov_len = mp->m_len;
704 uiop->uio_iovcnt = i;
705 uiop->uio_offset = 0;
706 uiop->uio_resid = len;
707 uiop->uio_rw = UIO_READ;
708 uiop->uio_segflg = UIO_SYSSPACE;
709 uiop->uio_procp = (struct proc *)0;
710 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
711 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
713 nfsm_reply(2 * NFSX_UNSIGNED);
714 nfsm_srvpostop_attr(1, (struct vattr *)0);
718 if (vp->v_type != VLNK) {
725 nqsrv_getl(vp, ND_READ);
726 error = VOP_READLINK(vp, uiop, cred);
728 getret = VOP_GETATTR(vp, &attr, cred, procp);
731 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
733 nfsm_srvpostop_attr(getret, &attr);
739 if (uiop->uio_resid > 0) {
740 len -= uiop->uio_resid;
741 tlen = nfsm_rndup(len);
742 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
744 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
745 *tl = txdr_unsigned(len);
760 nfsrv_read(nfsd, slp, procp, mrq)
761 struct nfsrv_descript *nfsd;
762 struct nfssvc_sock *slp;
766 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
767 struct sockaddr *nam = nfsd->nd_nam;
768 caddr_t dpos = nfsd->nd_dpos;
769 struct ucred *cred = &nfsd->nd_cr;
770 register struct iovec *iv;
772 register struct mbuf *m;
773 register struct nfs_fattr *fp;
774 register u_int32_t *tl;
778 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
779 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
781 struct mbuf *mb, *mb2, *mreq;
783 struct vnode *vp = NULL;
786 struct uio io, *uiop = &io;
787 struct vattr va, *vap = &va;
793 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
794 fhp = &nfh.fh_generic;
797 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
798 off = fxdr_hyper(tl);
800 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
801 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
803 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
806 * Reference vp. If an error occurs, vp will be invalid, but we
807 * have to NULL it just in case. The macros might goto nfsmout
811 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
812 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
815 nfsm_reply(2 * NFSX_UNSIGNED);
816 nfsm_srvpostop_attr(1, (struct vattr *)0);
821 if (vp->v_type != VREG) {
825 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
828 nqsrv_getl(vp, ND_READ);
829 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
830 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
832 getret = VOP_GETATTR(vp, vap, cred, procp);
838 nfsm_reply(NFSX_POSTOPATTR(v3));
839 nfsm_srvpostop_attr(getret, vap);
845 * Calculate byte count to read
848 if (off >= vap->va_size)
850 else if ((off + reqlen) > vap->va_size)
851 cnt = vap->va_size - off;
856 * Calculate seqcount for heuristic
864 * Locate best candidate
867 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
871 if (nfsheur[hi].nh_vp == vp) {
875 if (nfsheur[hi].nh_use > 0)
876 --nfsheur[hi].nh_use;
877 hi = (hi + 1) & (NUM_HEURISTIC - 1);
878 if (nfsheur[hi].nh_use < nh->nh_use)
882 if (nh->nh_vp != vp) {
885 nh->nh_use = NHUSE_INIT;
893 * Calculate heuristic
896 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
897 if (++nh->nh_seqcount > IO_SEQMAX)
898 nh->nh_seqcount = IO_SEQMAX;
899 } else if (nh->nh_seqcount > 1) {
904 nh->nh_use += NHUSE_INC;
905 if (nh->nh_use > NHUSE_MAX)
906 nh->nh_use = NHUSE_MAX;
907 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
910 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
912 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
914 fp = (struct nfs_fattr *)tl;
915 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
917 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
918 fp = (struct nfs_fattr *)tl;
919 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
921 len = left = nfsm_rndup(cnt);
924 * Generate the mbuf list with the uio_iov ref. to it.
929 siz = min(M_TRAILINGSPACE(m), left);
935 MGET(m, M_WAIT, MT_DATA);
942 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
944 uiop->uio_iov = iv2 = iv;
950 panic("nfsrv_read iov");
951 siz = min(M_TRAILINGSPACE(m), left);
953 iv->iov_base = mtod(m, caddr_t) + m->m_len;
962 uiop->uio_iovcnt = i;
963 uiop->uio_offset = off;
964 uiop->uio_resid = len;
965 uiop->uio_rw = UIO_READ;
966 uiop->uio_segflg = UIO_SYSSPACE;
967 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
968 off = uiop->uio_offset;
970 FREE((caddr_t)iv2, M_TEMP);
971 if (error || (getret = VOP_GETATTR(vp, vap, cred, procp))) {
977 nfsm_reply(NFSX_POSTOPATTR(v3));
978 nfsm_srvpostop_attr(getret, vap);
987 nfsm_srvfillattr(vap, fp);
988 tlen = len - uiop->uio_resid;
989 cnt = cnt < tlen ? cnt : tlen;
990 tlen = nfsm_rndup(cnt);
991 if (len != tlen || tlen != cnt)
992 nfsm_adj(mb, len - tlen, tlen - cnt);
994 *tl++ = txdr_unsigned(cnt);
1000 *tl = txdr_unsigned(cnt);
1011 nfsrv_write(nfsd, slp, procp, mrq)
1012 struct nfsrv_descript *nfsd;
1013 struct nfssvc_sock *slp;
1017 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1018 struct sockaddr *nam = nfsd->nd_nam;
1019 caddr_t dpos = nfsd->nd_dpos;
1020 struct ucred *cred = &nfsd->nd_cr;
1021 register struct iovec *ivp;
1022 register int i, cnt;
1023 register struct mbuf *mp;
1024 register struct nfs_fattr *fp;
1026 struct vattr va, forat;
1027 register struct vattr *vap = &va;
1028 register u_int32_t *tl;
1029 register int32_t t1;
1031 int error = 0, rdonly, cache, len, forat_ret = 1;
1032 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1033 int stable = NFSV3WRITE_FILESYNC;
1034 int v3 = (nfsd->nd_flag & ND_NFSV3);
1036 struct mbuf *mb, *mb2, *mreq;
1037 struct vnode *vp = NULL;
1040 struct uio io, *uiop = &io;
1044 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1050 fhp = &nfh.fh_generic;
1053 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1054 off = fxdr_hyper(tl);
1056 stable = fxdr_unsigned(int, *tl++);
1058 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1059 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1062 stable = NFSV3WRITE_UNSTABLE;
1064 retlen = len = fxdr_unsigned(int32_t, *tl);
1068 * For NFS Version 2, it is not obvious what a write of zero length
1069 * should do, but I might as well be consistent with Version 3,
1070 * which is to return ok so long as there are no permission problems.
1078 adjust = dpos - mtod(mp, caddr_t);
1079 mp->m_len -= adjust;
1080 if (mp->m_len > 0 && adjust > 0)
1081 NFSMADV(mp, adjust);
1085 else if (mp->m_len > 0) {
1088 mp->m_len -= (i - len);
1097 if (len > NFS_MAXDATA || len < 0 || i < len) {
1099 nfsm_reply(2 * NFSX_UNSIGNED);
1100 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1104 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1105 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1108 nfsm_reply(2 * NFSX_UNSIGNED);
1109 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1114 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1115 if (vp->v_type != VREG) {
1119 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1122 nqsrv_getl(vp, ND_WRITE);
1123 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1128 nfsm_reply(NFSX_WCCDATA(v3));
1129 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1135 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1137 uiop->uio_iov = iv = ivp;
1138 uiop->uio_iovcnt = cnt;
1141 if (mp->m_len > 0) {
1142 ivp->iov_base = mtod(mp, caddr_t);
1143 ivp->iov_len = mp->m_len;
1151 * The IO_METASYNC flag indicates that all metadata (and not just
1152 * enough to ensure data integrity) mus be written to stable storage
1154 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1156 if (stable == NFSV3WRITE_UNSTABLE)
1157 ioflags = IO_NODELOCKED;
1158 else if (stable == NFSV3WRITE_DATASYNC)
1159 ioflags = (IO_SYNC | IO_NODELOCKED);
1161 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1162 uiop->uio_resid = len;
1163 uiop->uio_rw = UIO_WRITE;
1164 uiop->uio_segflg = UIO_SYSSPACE;
1165 uiop->uio_procp = (struct proc *)0;
1166 uiop->uio_offset = off;
1167 error = VOP_WRITE(vp, uiop, ioflags, cred);
1168 nfsstats.srvvop_writes++;
1169 FREE((caddr_t)iv, M_TEMP);
1171 aftat_ret = VOP_GETATTR(vp, vap, cred, procp);
1176 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1177 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1179 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1184 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1185 *tl++ = txdr_unsigned(retlen);
1187 * If nfs_async is set, then pretend the write was FILESYNC.
1189 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1190 *tl++ = txdr_unsigned(stable);
1192 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1194 * Actually, there is no need to txdr these fields,
1195 * but it may make the values more human readable,
1196 * for debugging purposes.
1198 if (nfsver.tv_sec == 0)
1200 *tl++ = txdr_unsigned(nfsver.tv_sec);
1201 *tl = txdr_unsigned(nfsver.tv_usec);
1203 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1204 nfsm_srvfillattr(vap, fp);
1213 * NFS write service with write gathering support. Called when
1214 * nfsrvw_procrastinate > 0.
1215 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1216 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1220 nfsrv_writegather(ndp, slp, procp, mrq)
1221 struct nfsrv_descript **ndp;
1222 struct nfssvc_sock *slp;
1226 register struct iovec *ivp;
1227 register struct mbuf *mp;
1228 register struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1229 register struct nfs_fattr *fp;
1232 struct nfsrvw_delayhash *wpp;
1234 struct vattr va, forat;
1235 register u_int32_t *tl;
1236 register int32_t t1;
1238 int error = 0, rdonly, cache, len, forat_ret = 1;
1239 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1241 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1242 struct vnode *vp = NULL;
1243 struct uio io, *uiop = &io;
1244 u_quad_t frev, cur_usec;
1246 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1255 mrep = nfsd->nd_mrep;
1257 dpos = nfsd->nd_dpos;
1258 cred = &nfsd->nd_cr;
1259 v3 = (nfsd->nd_flag & ND_NFSV3);
1260 LIST_INIT(&nfsd->nd_coalesce);
1261 nfsd->nd_mreq = NULL;
1262 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1263 cur_usec = nfs_curusec();
1264 nfsd->nd_time = cur_usec +
1265 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1268 * Now, get the write header..
1270 nfsm_srvmtofh(&nfsd->nd_fh);
1272 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1273 nfsd->nd_off = fxdr_hyper(tl);
1275 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1277 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1278 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1281 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1283 len = fxdr_unsigned(int32_t, *tl);
1285 nfsd->nd_eoff = nfsd->nd_off + len;
1288 * Trim the header out of the mbuf list and trim off any trailing
1289 * junk so that the mbuf list has only the write data.
1297 adjust = dpos - mtod(mp, caddr_t);
1298 mp->m_len -= adjust;
1299 if (mp->m_len > 0 && adjust > 0)
1300 NFSMADV(mp, adjust);
1307 mp->m_len -= (i - len);
1313 if (len > NFS_MAXDATA || len < 0 || i < len) {
1317 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1319 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1320 nfsd->nd_mreq = mreq;
1321 nfsd->nd_mrep = NULL;
1326 * Add this entry to the hash and time queues.
1330 wp = slp->ns_tq.lh_first;
1331 while (wp && wp->nd_time < nfsd->nd_time) {
1333 wp = wp->nd_tq.le_next;
1335 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1337 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1339 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1341 if (nfsd->nd_mrep) {
1342 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1346 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1348 wp = wp->nd_hash.le_next;
1350 while (wp && wp->nd_off < nfsd->nd_off &&
1351 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1353 wp = wp->nd_hash.le_next;
1356 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1359 * Search the hash list for overlapping entries and
1362 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1363 wp = nfsd->nd_hash.le_next;
1364 if (NFSW_SAMECRED(owp, nfsd))
1365 nfsrvw_coalesce(owp, nfsd);
1368 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1375 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1376 * and generate the associated reply mbuf list(s).
1379 cur_usec = nfs_curusec();
1381 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1382 owp = nfsd->nd_tq.le_next;
1383 if (nfsd->nd_time > cur_usec)
1387 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1388 LIST_REMOVE(nfsd, nd_tq);
1389 LIST_REMOVE(nfsd, nd_hash);
1391 mrep = nfsd->nd_mrep;
1392 nfsd->nd_mrep = NULL;
1393 cred = &nfsd->nd_cr;
1394 v3 = (nfsd->nd_flag & ND_NFSV3);
1395 forat_ret = aftat_ret = 1;
1396 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1397 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1400 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1401 if (vp->v_type != VREG) {
1405 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1411 nqsrv_getl(vp, ND_WRITE);
1412 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1415 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1416 ioflags = IO_NODELOCKED;
1417 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1418 ioflags = (IO_SYNC | IO_NODELOCKED);
1420 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1421 uiop->uio_rw = UIO_WRITE;
1422 uiop->uio_segflg = UIO_SYSSPACE;
1423 uiop->uio_procp = (struct proc *)0;
1424 uiop->uio_offset = nfsd->nd_off;
1425 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1426 if (uiop->uio_resid > 0) {
1434 uiop->uio_iovcnt = i;
1435 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1437 uiop->uio_iov = ivp = iov;
1440 if (mp->m_len > 0) {
1441 ivp->iov_base = mtod(mp, caddr_t);
1442 ivp->iov_len = mp->m_len;
1448 error = VOP_WRITE(vp, uiop, ioflags, cred);
1449 nfsstats.srvvop_writes++;
1451 FREE((caddr_t)iov, M_TEMP);
1455 aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
1461 * Loop around generating replies for all write rpcs that have
1462 * now been completed.
1466 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1468 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1470 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1473 nfsm_writereply(NFSX_PREOPATTR(v3) +
1474 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1475 NFSX_WRITEVERF(v3), v3);
1477 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1478 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1479 *tl++ = txdr_unsigned(nfsd->nd_len);
1480 *tl++ = txdr_unsigned(swp->nd_stable);
1482 * Actually, there is no need to txdr these fields,
1483 * but it may make the values more human readable,
1484 * for debugging purposes.
1486 if (nfsver.tv_sec == 0)
1488 *tl++ = txdr_unsigned(nfsver.tv_sec);
1489 *tl = txdr_unsigned(nfsver.tv_usec);
1491 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1492 nfsm_srvfillattr(&va, fp);
1495 nfsd->nd_mreq = mreq;
1497 panic("nfsrv_write: nd_mrep not free");
1500 * Done. Put it at the head of the timer queue so that
1501 * the final phase can return the reply.
1506 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1508 nfsd = swp->nd_coalesce.lh_first;
1510 LIST_REMOVE(nfsd, nd_tq);
1516 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1523 * Search for a reply to return.
1526 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1527 if (nfsd->nd_mreq) {
1528 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1529 LIST_REMOVE(nfsd, nd_tq);
1530 *mrq = nfsd->nd_mreq;
1539 * Coalesce the write request nfsd into owp. To do this we must:
1540 * - remove nfsd from the queues
1541 * - merge nfsd->nd_mrep into owp->nd_mrep
1542 * - update the nd_eoff and nd_stable for owp
1543 * - put nfsd on owp's nd_coalesce list
1544 * NB: Must be called at splsoftclock().
1547 nfsrvw_coalesce(owp, nfsd)
1548 register struct nfsrv_descript *owp;
1549 register struct nfsrv_descript *nfsd;
1551 register int overlap;
1552 register struct mbuf *mp;
1553 struct nfsrv_descript *p;
1555 NFS_DPF(WG, ("C%03x-%03x",
1556 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1557 LIST_REMOVE(nfsd, nd_hash);
1558 LIST_REMOVE(nfsd, nd_tq);
1559 if (owp->nd_eoff < nfsd->nd_eoff) {
1560 overlap = owp->nd_eoff - nfsd->nd_off;
1562 panic("nfsrv_coalesce: bad off");
1564 m_adj(nfsd->nd_mrep, overlap);
1568 mp->m_next = nfsd->nd_mrep;
1569 owp->nd_eoff = nfsd->nd_eoff;
1571 m_freem(nfsd->nd_mrep);
1572 nfsd->nd_mrep = NULL;
1573 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1574 owp->nd_stable = NFSV3WRITE_FILESYNC;
1575 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1576 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1577 owp->nd_stable = NFSV3WRITE_DATASYNC;
1578 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1581 * If nfsd had anything else coalesced into it, transfer them
1582 * to owp, otherwise their replies will never get sent.
1584 for (p = nfsd->nd_coalesce.lh_first; p;
1585 p = nfsd->nd_coalesce.lh_first) {
1586 LIST_REMOVE(p, nd_tq);
1587 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1592 * nfs create service
1593 * now does a truncate to 0 length via. setattr if it already exists
1596 nfsrv_create(nfsd, slp, procp, mrq)
1597 struct nfsrv_descript *nfsd;
1598 struct nfssvc_sock *slp;
1602 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1603 struct sockaddr *nam = nfsd->nd_nam;
1604 caddr_t dpos = nfsd->nd_dpos;
1605 struct ucred *cred = &nfsd->nd_cr;
1606 register struct nfs_fattr *fp;
1607 struct vattr va, dirfor, diraft;
1608 register struct vattr *vap = &va;
1609 register struct nfsv2_sattr *sp;
1610 register u_int32_t *tl;
1611 struct nameidata nd;
1612 register int32_t t1;
1614 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1615 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1618 struct mbuf *mb, *mb2, *mreq;
1619 struct vnode *dirp = (struct vnode *)0;
1622 u_quad_t frev, tempsize;
1623 u_char cverf[NFSX_V3CREATEVERF];
1625 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1631 fhp = &nfh.fh_generic;
1633 nfsm_srvnamesiz(len);
1635 nd.ni_cnd.cn_cred = cred;
1636 nd.ni_cnd.cn_nameiop = CREATE;
1637 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1640 * Call namei and do initial cleanup to get a few things
1641 * out of the way. If we get an initial error we cleanup
1642 * and return here to avoid special-casing the invalid nd
1643 * structure through the rest of the case. dirp may be
1644 * set even if an error occurs, but the nd structure will not
1645 * be valid at all if an error occurs so we have to invalidate it
1646 * prior to calling nfsm_reply ( which might goto nfsmout ).
1648 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1649 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1652 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1660 nfsm_reply(NFSX_WCCDATA(v3));
1661 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1667 * No error. Continue. State:
1669 * startdir is valid ( we release this immediately )
1671 * nd.ni_vp may be valid
1672 * nd.ni_dvp is valid
1674 * The error state is set through the code and we may also do some
1675 * opportunistic releasing of vnodes to avoid holding locks through
1676 * NFS I/O. The cleanup at the end is a catch-all
1681 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1682 how = fxdr_unsigned(int, *tl);
1684 case NFSV3CREATE_GUARDED:
1690 case NFSV3CREATE_UNCHECKED:
1693 case NFSV3CREATE_EXCLUSIVE:
1694 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1695 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1699 vap->va_type = VREG;
1701 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1702 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1703 if (vap->va_type == VNON)
1704 vap->va_type = VREG;
1705 vap->va_mode = nfstov_mode(sp->sa_mode);
1706 switch (vap->va_type) {
1708 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1710 vap->va_size = (u_quad_t)tsize;
1715 rdev = fxdr_unsigned(long, sp->sa_size);
1723 * Iff doesn't exist, create it
1724 * otherwise just truncate to 0 length
1725 * should I set the mode too ?
1727 * The only possible error we can have at this point is EEXIST.
1728 * nd.ni_vp will also be non-NULL in that case.
1730 if (nd.ni_vp == NULL) {
1731 if (vap->va_mode == (mode_t)VNOVAL)
1733 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1734 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1735 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1737 NDFREE(&nd, NDF_ONLY_PNBUF);
1739 nfsrv_object_create(nd.ni_vp);
1740 if (exclusive_flag) {
1743 bcopy(cverf, (caddr_t)&vap->va_atime,
1745 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1750 vap->va_type == VCHR ||
1751 vap->va_type == VBLK ||
1752 vap->va_type == VFIFO
1755 * Handle SysV FIFO node special cases. All other
1756 * devices require super user to access.
1758 if (vap->va_type == VCHR && rdev == 0xffffffff)
1759 vap->va_type = VFIFO;
1760 if (vap->va_type != VFIFO &&
1761 (error = suser_xxx(cred, 0, 0))) {
1764 vap->va_rdev = rdev;
1765 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1767 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1769 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 * release dvp prior to lookup
1784 * Even though LOCKPARENT was cleared, ni_dvp may
1787 nd.ni_cnd.cn_nameiop = LOOKUP;
1788 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1789 nd.ni_cnd.cn_proc = procp;
1790 nd.ni_cnd.cn_cred = cred;
1792 error = lookup(&nd);
1797 /* fall through on certain errors */
1799 nfsrv_object_create(nd.ni_vp);
1800 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1808 if (vap->va_size != -1) {
1809 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1810 (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
1812 nqsrv_getl(nd.ni_vp, ND_WRITE);
1813 tempsize = vap->va_size;
1815 vap->va_size = tempsize;
1816 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1823 bzero((caddr_t)fhp, sizeof(nfh));
1824 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1825 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1827 error = VOP_GETATTR(nd.ni_vp, vap, cred, procp);
1830 if (exclusive_flag && !error &&
1831 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1833 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
1837 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1840 nfsm_srvpostop_fh(fhp);
1841 nfsm_srvpostop_attr(0, vap);
1843 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1846 nfsm_srvfhtom(fhp, v3);
1847 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1848 nfsm_srvfillattr(vap, fp);
1858 if (nd.ni_startdir) {
1859 vrele(nd.ni_startdir);
1860 nd.ni_startdir = NULL;
1864 NDFREE(&nd, NDF_ONLY_PNBUF);
1866 if (nd.ni_dvp == nd.ni_vp)
1877 * nfs v3 mknod service
1880 nfsrv_mknod(nfsd, slp, procp, mrq)
1881 struct nfsrv_descript *nfsd;
1882 struct nfssvc_sock *slp;
1886 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1887 struct sockaddr *nam = nfsd->nd_nam;
1888 caddr_t dpos = nfsd->nd_dpos;
1889 struct ucred *cred = &nfsd->nd_cr;
1890 struct vattr va, dirfor, diraft;
1891 register struct vattr *vap = &va;
1892 register u_int32_t *tl;
1893 struct nameidata nd;
1894 register int32_t t1;
1896 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1897 u_int32_t major, minor;
1900 struct mbuf *mb, *mb2, *mreq;
1901 struct vnode *vp, *dirp = (struct vnode *)0;
1906 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1909 fhp = &nfh.fh_generic;
1911 nfsm_srvnamesiz(len);
1913 nd.ni_cnd.cn_cred = cred;
1914 nd.ni_cnd.cn_nameiop = CREATE;
1915 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1918 * Handle nfs_namei() call. If an error occurs, the nd structure
1919 * is not valid. However, nfsm_*() routines may still jump to
1923 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1924 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1926 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
1928 nfsm_reply(NFSX_WCCDATA(1));
1929 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1933 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1934 vtyp = nfsv3tov_type(*tl);
1935 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1936 error = NFSERR_BADTYPE;
1941 if (vtyp == VCHR || vtyp == VBLK) {
1942 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1943 major = fxdr_unsigned(u_int32_t, *tl++);
1944 minor = fxdr_unsigned(u_int32_t, *tl);
1945 vap->va_rdev = makeudev(major, minor);
1949 * Iff doesn't exist, create it.
1955 vap->va_type = vtyp;
1956 if (vap->va_mode == (mode_t)VNOVAL)
1958 if (vtyp == VSOCK) {
1959 vrele(nd.ni_startdir);
1960 nd.ni_startdir = NULL;
1961 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1962 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1964 NDFREE(&nd, NDF_ONLY_PNBUF);
1966 if (vtyp != VFIFO && (error = suser_xxx(cred, 0, 0)))
1968 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1970 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1972 NDFREE(&nd, NDF_ONLY_PNBUF);
1979 * Release dvp prior to lookup
1984 nd.ni_cnd.cn_nameiop = LOOKUP;
1985 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1986 nd.ni_cnd.cn_proc = procp;
1987 nd.ni_cnd.cn_cred = procp->p_ucred;
1989 error = lookup(&nd);
1994 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1999 * send response, cleanup, return.
2002 if (nd.ni_startdir) {
2003 vrele(nd.ni_startdir);
2004 nd.ni_startdir = NULL;
2006 NDFREE(&nd, NDF_ONLY_PNBUF);
2008 if (nd.ni_dvp == nd.ni_vp)
2016 bzero((caddr_t)fhp, sizeof(nfh));
2017 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2018 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2020 error = VOP_GETATTR(vp, vap, cred, procp);
2027 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2032 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2034 nfsm_srvpostop_fh(fhp);
2035 nfsm_srvpostop_attr(0, vap);
2037 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2043 vrele(nd.ni_startdir);
2044 NDFREE(&nd, NDF_ONLY_PNBUF);
2046 if (nd.ni_dvp == nd.ni_vp)
2057 * nfs remove service
2060 nfsrv_remove(nfsd, slp, procp, mrq)
2061 struct nfsrv_descript *nfsd;
2062 struct nfssvc_sock *slp;
2066 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2067 struct sockaddr *nam = nfsd->nd_nam;
2068 caddr_t dpos = nfsd->nd_dpos;
2069 struct ucred *cred = &nfsd->nd_cr;
2070 struct nameidata nd;
2071 register u_int32_t *tl;
2072 register int32_t t1;
2074 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2075 int v3 = (nfsd->nd_flag & ND_NFSV3);
2077 struct mbuf *mb, *mreq;
2079 struct vattr dirfor, diraft;
2084 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2087 fhp = &nfh.fh_generic;
2089 nfsm_srvnamesiz(len);
2091 nd.ni_cnd.cn_cred = cred;
2092 nd.ni_cnd.cn_nameiop = DELETE;
2093 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2094 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2095 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2098 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2106 if (nd.ni_vp->v_type == VDIR) {
2107 error = EPERM; /* POSIX */
2111 * The root of a mounted filesystem cannot be deleted.
2113 if (nd.ni_vp->v_flag & VROOT) {
2119 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2120 nqsrv_getl(nd.ni_vp, ND_WRITE);
2121 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2122 NDFREE(&nd, NDF_ONLY_PNBUF);
2126 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2130 nfsm_reply(NFSX_WCCDATA(v3));
2132 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2136 NDFREE(&nd, NDF_ONLY_PNBUF);
2138 if (nd.ni_dvp == nd.ni_vp)
2149 * nfs rename service
2152 nfsrv_rename(nfsd, slp, procp, mrq)
2153 struct nfsrv_descript *nfsd;
2154 struct nfssvc_sock *slp;
2158 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2159 struct sockaddr *nam = nfsd->nd_nam;
2160 caddr_t dpos = nfsd->nd_dpos;
2161 struct ucred *cred = &nfsd->nd_cr;
2162 register u_int32_t *tl;
2163 register int32_t t1;
2165 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2166 int tdirfor_ret = 1, tdiraft_ret = 1;
2167 int v3 = (nfsd->nd_flag & ND_NFSV3);
2169 struct mbuf *mb, *mreq;
2170 struct nameidata fromnd, tond;
2171 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2172 struct vnode *tdirp = (struct vnode *)0;
2173 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2175 fhandle_t *ffhp, *tfhp;
2179 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2181 fvp = (struct vnode *)0;
2183 ffhp = &fnfh.fh_generic;
2184 tfhp = &tnfh.fh_generic;
2187 * Clear fields incase goto nfsmout occurs from macro.
2193 nfsm_srvmtofh(ffhp);
2194 nfsm_srvnamesiz(len);
2196 * Remember our original uid so that we can reset cr_uid before
2197 * the second nfs_namei() call, in case it is remapped.
2199 saved_uid = cred->cr_uid;
2200 fromnd.ni_cnd.cn_cred = cred;
2201 fromnd.ni_cnd.cn_nameiop = DELETE;
2202 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2203 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2204 &dpos, &fdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2207 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2215 nfsm_reply(2 * NFSX_WCCDATA(v3));
2216 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2217 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2222 nfsm_srvmtofh(tfhp);
2223 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2224 cred->cr_uid = saved_uid;
2225 tond.ni_cnd.cn_cred = cred;
2226 tond.ni_cnd.cn_nameiop = RENAME;
2227 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2228 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2229 &dpos, &tdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2232 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
2245 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2251 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2258 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2266 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2273 if (fvp->v_mount != tdvp->v_mount) {
2287 * If source is the same as the destination (that is the
2288 * same vnode with the same name in the same directory),
2289 * then there is nothing to do.
2291 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2292 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2293 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2294 fromnd.ni_cnd.cn_namelen))
2299 * The VOP_RENAME function releases all vnode references &
2300 * locks prior to returning so we need to clear the pointers
2301 * to bypass cleanup code later on.
2303 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2304 nqsrv_getl(tdvp, ND_WRITE);
2306 nqsrv_getl(tvp, ND_WRITE);
2308 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2309 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2310 fromnd.ni_dvp = NULL;
2311 fromnd.ni_vp = NULL;
2315 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2316 tond.ni_cnd.cn_flags &= ~HASBUF;
2326 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
2328 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
2329 nfsm_reply(2 * NFSX_WCCDATA(v3));
2331 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2332 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2339 * Clear out tond related fields
2343 if (tond.ni_startdir)
2344 vrele(tond.ni_startdir);
2345 NDFREE(&tond, NDF_ONLY_PNBUF);
2347 if (tond.ni_dvp == tond.ni_vp)
2356 * Clear out fromnd related fields
2360 if (fromnd.ni_startdir)
2361 vrele(fromnd.ni_startdir);
2362 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2364 vrele(fromnd.ni_dvp);
2366 vrele(fromnd.ni_vp);
2375 nfsrv_link(nfsd, slp, procp, mrq)
2376 struct nfsrv_descript *nfsd;
2377 struct nfssvc_sock *slp;
2381 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2382 struct sockaddr *nam = nfsd->nd_nam;
2383 caddr_t dpos = nfsd->nd_dpos;
2384 struct ucred *cred = &nfsd->nd_cr;
2385 struct nameidata nd;
2386 register u_int32_t *tl;
2387 register int32_t t1;
2389 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2390 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2392 struct mbuf *mb, *mreq;
2393 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2394 struct vattr dirfor, diraft, at;
2396 fhandle_t *fhp, *dfhp;
2399 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2402 fhp = &nfh.fh_generic;
2403 dfhp = &dnfh.fh_generic;
2405 nfsm_srvmtofh(dfhp);
2406 nfsm_srvnamesiz(len);
2408 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2409 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2411 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2412 nfsm_srvpostop_attr(getret, &at);
2413 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2418 if (vp->v_type == VDIR) {
2419 error = EPERM; /* POSIX */
2422 nd.ni_cnd.cn_cred = cred;
2423 nd.ni_cnd.cn_nameiop = CREATE;
2424 nd.ni_cnd.cn_flags = LOCKPARENT;
2425 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2426 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2429 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2445 if (vp->v_mount != xp->v_mount)
2449 nqsrv_getl(vp, ND_WRITE);
2450 nqsrv_getl(xp, ND_WRITE);
2451 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2452 NDFREE(&nd, NDF_ONLY_PNBUF);
2458 getret = VOP_GETATTR(vp, &at, cred, procp);
2460 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2461 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2463 nfsm_srvpostop_attr(getret, &at);
2464 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2470 NDFREE(&nd, NDF_ONLY_PNBUF);
2476 if (nd.ni_dvp == nd.ni_vp)
2487 * nfs symbolic link service
2490 nfsrv_symlink(nfsd, slp, procp, mrq)
2491 struct nfsrv_descript *nfsd;
2492 struct nfssvc_sock *slp;
2496 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2497 struct sockaddr *nam = nfsd->nd_nam;
2498 caddr_t dpos = nfsd->nd_dpos;
2499 struct ucred *cred = &nfsd->nd_cr;
2500 struct vattr va, dirfor, diraft;
2501 struct nameidata nd;
2502 register struct vattr *vap = &va;
2503 register u_int32_t *tl;
2504 register int32_t t1;
2505 struct nfsv2_sattr *sp;
2506 char *bpos, *pathcp = (char *)0, *cp2;
2509 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2510 int v3 = (nfsd->nd_flag & ND_NFSV3);
2511 struct mbuf *mb, *mreq, *mb2;
2512 struct vnode *dirp = (struct vnode *)0;
2517 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2520 fhp = &nfh.fh_generic;
2522 nfsm_srvnamesiz(len);
2523 nd.ni_cnd.cn_cred = cred;
2524 nd.ni_cnd.cn_nameiop = CREATE;
2525 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2526 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2527 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2530 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2543 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2544 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2545 iv.iov_base = pathcp;
2547 io.uio_resid = len2;
2551 io.uio_segflg = UIO_SYSSPACE;
2552 io.uio_rw = UIO_READ;
2553 io.uio_procp = (struct proc *)0;
2554 nfsm_mtouio(&io, len2);
2556 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2557 vap->va_mode = nfstov_mode(sp->sa_mode);
2559 *(pathcp + len2) = '\0';
2566 * issue symlink op. SAVESTART is set so the underlying path component
2567 * is only freed by the VOP if an error occurs.
2569 if (vap->va_mode == (mode_t)VNOVAL)
2571 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2572 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2574 NDFREE(&nd, NDF_ONLY_PNBUF);
2579 * releases directory prior to potential lookup op.
2587 * Issue lookup. Leave SAVESTART set so we can easily free
2588 * the name buffer later on.
2590 * since LOCKPARENT is not set, ni_dvp will be garbage on
2591 * return whether an error occurs or not.
2593 nd.ni_cnd.cn_nameiop = LOOKUP;
2594 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2595 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2596 nd.ni_cnd.cn_proc = procp;
2597 nd.ni_cnd.cn_cred = cred;
2599 error = lookup(&nd);
2603 bzero((caddr_t)fhp, sizeof(nfh));
2604 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2605 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2607 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2616 * These releases aren't strictly required, does even doing them
2617 * make any sense? XXX can nfsm_reply() block?
2620 FREE(pathcp, M_TEMP);
2624 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2628 if (nd.ni_startdir) {
2629 vrele(nd.ni_startdir);
2630 nd.ni_startdir = NULL;
2632 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2635 nfsm_srvpostop_fh(fhp);
2636 nfsm_srvpostop_attr(0, vap);
2638 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2644 NDFREE(&nd, NDF_ONLY_PNBUF);
2646 if (nd.ni_dvp == nd.ni_vp)
2654 vrele(nd.ni_startdir);
2658 FREE(pathcp, M_TEMP);
2667 nfsrv_mkdir(nfsd, slp, procp, mrq)
2668 struct nfsrv_descript *nfsd;
2669 struct nfssvc_sock *slp;
2673 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2674 struct sockaddr *nam = nfsd->nd_nam;
2675 caddr_t dpos = nfsd->nd_dpos;
2676 struct ucred *cred = &nfsd->nd_cr;
2677 struct vattr va, dirfor, diraft;
2678 register struct vattr *vap = &va;
2679 register struct nfs_fattr *fp;
2680 struct nameidata nd;
2681 register caddr_t cp;
2682 register u_int32_t *tl;
2683 register int32_t t1;
2685 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2686 int v3 = (nfsd->nd_flag & ND_NFSV3);
2688 struct mbuf *mb, *mb2, *mreq;
2689 struct vnode *dirp = NULL;
2695 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2698 fhp = &nfh.fh_generic;
2700 nfsm_srvnamesiz(len);
2701 nd.ni_cnd.cn_cred = cred;
2702 nd.ni_cnd.cn_nameiop = CREATE;
2703 nd.ni_cnd.cn_flags = LOCKPARENT;
2705 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2706 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2709 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2717 nfsm_reply(NFSX_WCCDATA(v3));
2718 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2726 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2727 vap->va_mode = nfstov_mode(*tl++);
2731 * At this point nd.ni_dvp is referenced and exclusively locked and
2732 * nd.ni_vp, if it exists, is referenced but not locked.
2735 vap->va_type = VDIR;
2736 if (nd.ni_vp != NULL) {
2737 NDFREE(&nd, NDF_ONLY_PNBUF);
2743 * Issue mkdir op. Since SAVESTART is not set, the pathname
2744 * component is freed by the VOP call. This will fill-in
2745 * nd.ni_vp, reference, and exclusively lock it.
2747 if (vap->va_mode == (mode_t)VNOVAL)
2749 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2750 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2751 NDFREE(&nd, NDF_ONLY_PNBUF);
2758 bzero((caddr_t)fhp, sizeof(nfh));
2759 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2760 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2762 error = VOP_GETATTR(nd.ni_vp, vap, cred, procp);
2766 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2767 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2770 nfsm_srvpostop_fh(fhp);
2771 nfsm_srvpostop_attr(0, vap);
2773 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2775 nfsm_srvfhtom(fhp, v3);
2776 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2777 nfsm_srvfillattr(vap, fp);
2786 NDFREE(&nd, NDF_ONLY_PNBUF);
2787 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2805 nfsrv_rmdir(nfsd, slp, procp, mrq)
2806 struct nfsrv_descript *nfsd;
2807 struct nfssvc_sock *slp;
2811 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2812 struct sockaddr *nam = nfsd->nd_nam;
2813 caddr_t dpos = nfsd->nd_dpos;
2814 struct ucred *cred = &nfsd->nd_cr;
2815 register u_int32_t *tl;
2816 register int32_t t1;
2818 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2819 int v3 = (nfsd->nd_flag & ND_NFSV3);
2821 struct mbuf *mb, *mreq;
2822 struct vnode *vp, *dirp = (struct vnode *)0;
2823 struct vattr dirfor, diraft;
2826 struct nameidata nd;
2829 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2832 fhp = &nfh.fh_generic;
2834 nfsm_srvnamesiz(len);
2835 nd.ni_cnd.cn_cred = cred;
2836 nd.ni_cnd.cn_nameiop = DELETE;
2837 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2838 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2839 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2842 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2850 nfsm_reply(NFSX_WCCDATA(v3));
2851 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2856 if (vp->v_type != VDIR) {
2861 * No rmdir "." please.
2863 if (nd.ni_dvp == vp) {
2868 * The root of a mounted filesystem cannot be deleted.
2870 if (vp->v_flag & VROOT)
2874 * Issue or abort op. Since SAVESTART is not set, path name
2875 * component is freed by the VOP after either.
2878 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2879 nqsrv_getl(vp, ND_WRITE);
2880 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2882 NDFREE(&nd, NDF_ONLY_PNBUF);
2885 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2886 nfsm_reply(NFSX_WCCDATA(v3));
2888 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2894 NDFREE(&nd, NDF_ONLY_PNBUF);
2898 if (nd.ni_dvp == nd.ni_vp)
2910 * nfs readdir service
2911 * - mallocs what it thinks is enough to read
2912 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2913 * - calls VOP_READDIR()
2914 * - loops around building the reply
2915 * if the output generated exceeds count break out of loop
2916 * The nfsm_clget macro is used here so that the reply will be packed
2917 * tightly in mbuf clusters.
2918 * - it only knows that it has encountered eof when the VOP_READDIR()
2920 * - as such one readdir rpc will return eof false although you are there
2921 * and then the next will return eof
2922 * - it trims out records with d_fileno == 0
2923 * this doesn't matter for Unix clients, but they might confuse clients
2925 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2926 * than requested, but this may not apply to all filesystems. For
2927 * example, client NFS does not { although it is never remote mounted
2929 * The alternate call nfsrv_readdirplus() does lookups as well.
2930 * PS: The NFS protocol spec. does not clarify what the "count" byte
2931 * argument is a count of.. just name strings and file id's or the
2932 * entire reply rpc or ...
2933 * I tried just file name and id sizes and it confused the Sun client,
2934 * so I am using the full rpc size now. The "paranoia.." comment refers
2935 * to including the status longwords that are not a part of the dir.
2936 * "entry" structures, but are in the rpc.
2940 u_int32_t fl_postopok;
2941 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2943 u_int32_t fl_fhsize;
2944 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2948 nfsrv_readdir(nfsd, slp, procp, mrq)
2949 struct nfsrv_descript *nfsd;
2950 struct nfssvc_sock *slp;
2954 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2955 struct sockaddr *nam = nfsd->nd_nam;
2956 caddr_t dpos = nfsd->nd_dpos;
2957 struct ucred *cred = &nfsd->nd_cr;
2958 register char *bp, *be;
2959 register struct mbuf *mp;
2960 register struct dirent *dp;
2961 register caddr_t cp;
2962 register u_int32_t *tl;
2963 register int32_t t1;
2965 struct mbuf *mb, *mb2, *mreq, *mp2;
2966 char *cpos, *cend, *cp2, *rbuf;
2967 struct vnode *vp = NULL;
2973 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2974 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2975 int v3 = (nfsd->nd_flag & ND_NFSV3);
2976 u_quad_t frev, off, toff, verf;
2977 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2979 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2980 fhp = &nfh.fh_generic;
2983 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2984 toff = fxdr_hyper(tl);
2986 verf = fxdr_hyper(tl);
2989 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2990 toff = fxdr_unsigned(u_quad_t, *tl++);
2991 verf = 0; /* shut up gcc */
2994 cnt = fxdr_unsigned(int, *tl);
2995 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2996 xfer = NFS_SRVMAXDATA(nfsd);
3002 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3003 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3004 if (!error && vp->v_type != VDIR) {
3010 nfsm_reply(NFSX_UNSIGNED);
3011 nfsm_srvpostop_attr(getret, &at);
3017 * Obtain lock on vnode for this section of the code
3020 nqsrv_getl(vp, ND_READ);
3022 error = getret = VOP_GETATTR(vp, &at, cred, procp);
3025 * XXX This check may be too strict for Solaris 2.5 clients.
3027 if (!error && toff && verf && verf != at.va_filerev)
3028 error = NFSERR_BAD_COOKIE;
3032 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
3036 nfsm_reply(NFSX_POSTOPATTR(v3));
3037 nfsm_srvpostop_attr(getret, &at);
3041 VOP_UNLOCK(vp, 0, procp);
3044 * end section. Allocate rbuf and continue
3046 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3049 iv.iov_len = fullsiz;
3052 io.uio_offset = (off_t)off;
3053 io.uio_resid = fullsiz;
3054 io.uio_segflg = UIO_SYSSPACE;
3055 io.uio_rw = UIO_READ;
3056 io.uio_procp = (struct proc *)0;
3058 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
3060 free((caddr_t)cookies, M_TEMP);
3063 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3064 off = (off_t)io.uio_offset;
3065 if (!cookies && !error)
3066 error = NFSERR_PERM;
3068 getret = VOP_GETATTR(vp, &at, cred, procp);
3072 VOP_UNLOCK(vp, 0, procp);
3076 free((caddr_t)rbuf, M_TEMP);
3078 free((caddr_t)cookies, M_TEMP);
3079 nfsm_reply(NFSX_POSTOPATTR(v3));
3080 nfsm_srvpostop_attr(getret, &at);
3085 siz -= io.uio_resid;
3088 * If nothing read, return eof
3094 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3097 nfsm_srvpostop_attr(getret, &at);
3098 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3099 txdr_hyper(at.va_filerev, tl);
3102 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3105 FREE((caddr_t)rbuf, M_TEMP);
3106 FREE((caddr_t)cookies, M_TEMP);
3113 * Check for degenerate cases of nothing useful read.
3114 * If so go try again
3118 dp = (struct dirent *)cpos;
3121 * For some reason FreeBSD's ufs_readdir() chooses to back the
3122 * directory offset up to a block boundary, so it is necessary to
3123 * skip over the records that preceed the requested offset. This
3124 * requires the assumption that file offset cookies monotonically
3127 while (cpos < cend && ncookies > 0 &&
3128 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3129 ((u_quad_t)(*cookiep)) <= toff)) {
3130 cpos += dp->d_reclen;
3131 dp = (struct dirent *)cpos;
3135 if (cpos >= cend || ncookies == 0) {
3141 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3142 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3144 nfsm_srvpostop_attr(getret, &at);
3145 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3146 txdr_hyper(at.va_filerev, tl);
3150 be = bp + M_TRAILINGSPACE(mp);
3152 /* Loop through the records and build reply */
3153 while (cpos < cend && ncookies > 0) {
3154 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3155 nlen = dp->d_namlen;
3156 rem = nfsm_rndup(nlen) - nlen;
3157 len += (4 * NFSX_UNSIGNED + nlen + rem);
3159 len += 2 * NFSX_UNSIGNED;
3165 * Build the directory record xdr from
3170 bp += NFSX_UNSIGNED;
3174 bp += NFSX_UNSIGNED;
3177 *tl = txdr_unsigned(dp->d_fileno);
3178 bp += NFSX_UNSIGNED;
3180 *tl = txdr_unsigned(nlen);
3181 bp += NFSX_UNSIGNED;
3183 /* And loop around copying the name */
3192 bcopy(cp, bp, tsiz);
3198 /* And null pad to a int32_t boundary */
3199 for (i = 0; i < rem; i++)
3203 /* Finish off the record */
3206 bp += NFSX_UNSIGNED;
3209 *tl = txdr_unsigned(*cookiep);
3210 bp += NFSX_UNSIGNED;
3212 cpos += dp->d_reclen;
3213 dp = (struct dirent *)cpos;
3221 bp += NFSX_UNSIGNED;
3227 bp += NFSX_UNSIGNED;
3230 mp->m_len = bp - mtod(mp, caddr_t);
3232 mp->m_len += bp - bpos;
3233 FREE((caddr_t)rbuf, M_TEMP);
3234 FREE((caddr_t)cookies, M_TEMP);
3243 nfsrv_readdirplus(nfsd, slp, procp, mrq)
3244 struct nfsrv_descript *nfsd;
3245 struct nfssvc_sock *slp;
3249 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3250 struct sockaddr *nam = nfsd->nd_nam;
3251 caddr_t dpos = nfsd->nd_dpos;
3252 struct ucred *cred = &nfsd->nd_cr;
3253 register char *bp, *be;
3254 register struct mbuf *mp;
3255 register struct dirent *dp;
3256 register caddr_t cp;
3257 register u_int32_t *tl;
3258 register int32_t t1;
3260 struct mbuf *mb, *mb2, *mreq, *mp2;
3261 char *cpos, *cend, *cp2, *rbuf;
3262 struct vnode *vp = NULL, *nvp;
3265 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3268 struct vattr va, at, *vap = &va;
3269 struct nfs_fattr *fp;
3270 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3271 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3272 u_quad_t frev, off, toff, verf;
3273 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3275 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3276 fhp = &nfh.fh_generic;
3278 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3279 toff = fxdr_hyper(tl);
3281 verf = fxdr_hyper(tl);
3283 siz = fxdr_unsigned(int, *tl++);
3284 cnt = fxdr_unsigned(int, *tl);
3286 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3287 xfer = NFS_SRVMAXDATA(nfsd);
3293 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3294 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3295 if (!error && vp->v_type != VDIR) {
3301 nfsm_reply(NFSX_UNSIGNED);
3302 nfsm_srvpostop_attr(getret, &at);
3306 error = getret = VOP_GETATTR(vp, &at, cred, procp);
3309 * XXX This check may be too strict for Solaris 2.5 clients.
3311 if (!error && toff && verf && verf != at.va_filerev)
3312 error = NFSERR_BAD_COOKIE;
3315 nqsrv_getl(vp, ND_READ);
3316 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
3321 nfsm_reply(NFSX_V3POSTOPATTR);
3322 nfsm_srvpostop_attr(getret, &at);
3326 VOP_UNLOCK(vp, 0, procp);
3327 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3330 iv.iov_len = fullsiz;
3333 io.uio_offset = (off_t)off;
3334 io.uio_resid = fullsiz;
3335 io.uio_segflg = UIO_SYSSPACE;
3336 io.uio_rw = UIO_READ;
3337 io.uio_procp = (struct proc *)0;
3339 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
3341 free((caddr_t)cookies, M_TEMP);
3344 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3345 off = (u_quad_t)io.uio_offset;
3346 getret = VOP_GETATTR(vp, &at, cred, procp);
3347 VOP_UNLOCK(vp, 0, procp);
3348 if (!cookies && !error)
3349 error = NFSERR_PERM;
3356 free((caddr_t)cookies, M_TEMP);
3357 free((caddr_t)rbuf, M_TEMP);
3358 nfsm_reply(NFSX_V3POSTOPATTR);
3359 nfsm_srvpostop_attr(getret, &at);
3364 siz -= io.uio_resid;
3367 * If nothing read, return eof
3373 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3375 nfsm_srvpostop_attr(getret, &at);
3376 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3377 txdr_hyper(at.va_filerev, tl);
3381 FREE((caddr_t)cookies, M_TEMP);
3382 FREE((caddr_t)rbuf, M_TEMP);
3389 * Check for degenerate cases of nothing useful read.
3390 * If so go try again
3394 dp = (struct dirent *)cpos;
3397 * For some reason FreeBSD's ufs_readdir() chooses to back the
3398 * directory offset up to a block boundary, so it is necessary to
3399 * skip over the records that preceed the requested offset. This
3400 * requires the assumption that file offset cookies monotonically
3403 while (cpos < cend && ncookies > 0 &&
3404 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3405 ((u_quad_t)(*cookiep)) <= toff)) {
3406 cpos += dp->d_reclen;
3407 dp = (struct dirent *)cpos;
3411 if (cpos >= cend || ncookies == 0) {
3418 * Probe one of the directory entries to see if the filesystem
3421 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3422 error = NFSERR_NOTSUPP;
3425 free((caddr_t)cookies, M_TEMP);
3426 free((caddr_t)rbuf, M_TEMP);
3427 nfsm_reply(NFSX_V3POSTOPATTR);
3428 nfsm_srvpostop_attr(getret, &at);
3435 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3437 nfsm_srvpostop_attr(getret, &at);
3438 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3439 txdr_hyper(at.va_filerev, tl);
3442 be = bp + M_TRAILINGSPACE(mp);
3444 /* Loop through the records and build reply */
3445 while (cpos < cend && ncookies > 0) {
3446 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3447 nlen = dp->d_namlen;
3448 rem = nfsm_rndup(nlen)-nlen;
3451 * For readdir_and_lookup get the vnode using
3454 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3456 bzero((caddr_t)nfhp, NFSX_V3FH);
3458 nvp->v_mount->mnt_stat.f_fsid;
3459 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3464 if (VOP_GETATTR(nvp, vap, cred, procp)) {
3473 * If either the dircount or maxcount will be
3474 * exceeded, get out now. Both of these lengths
3475 * are calculated conservatively, including all
3478 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3480 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3481 if (len > cnt || dirlen > fullsiz) {
3487 * Build the directory record xdr from
3490 fp = (struct nfs_fattr *)&fl.fl_fattr;
3491 nfsm_srvfillattr(vap, fp);
3492 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3493 fl.fl_fhok = nfs_true;
3494 fl.fl_postopok = nfs_true;
3495 fl.fl_off.nfsuquad[0] = 0;
3496 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3500 bp += NFSX_UNSIGNED;
3503 bp += NFSX_UNSIGNED;
3505 *tl = txdr_unsigned(dp->d_fileno);
3506 bp += NFSX_UNSIGNED;
3508 *tl = txdr_unsigned(nlen);
3509 bp += NFSX_UNSIGNED;
3511 /* And loop around copying the name */
3516 if ((bp + xfer) > be)
3520 bcopy(cp, bp, tsiz);
3526 /* And null pad to a int32_t boundary */
3527 for (i = 0; i < rem; i++)
3531 * Now copy the flrep structure out.
3533 xfer = sizeof (struct flrep);
3537 if ((bp + xfer) > be)
3541 bcopy(cp, bp, tsiz);
3549 cpos += dp->d_reclen;
3550 dp = (struct dirent *)cpos;
3558 bp += NFSX_UNSIGNED;
3564 bp += NFSX_UNSIGNED;
3567 mp->m_len = bp - mtod(mp, caddr_t);
3569 mp->m_len += bp - bpos;
3570 FREE((caddr_t)cookies, M_TEMP);
3571 FREE((caddr_t)rbuf, M_TEMP);
3579 * nfs commit service
3582 nfsrv_commit(nfsd, slp, procp, mrq)
3583 struct nfsrv_descript *nfsd;
3584 struct nfssvc_sock *slp;
3588 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3589 struct sockaddr *nam = nfsd->nd_nam;
3590 caddr_t dpos = nfsd->nd_dpos;
3591 struct ucred *cred = &nfsd->nd_cr;
3592 struct vattr bfor, aft;
3593 struct vnode *vp = NULL;
3596 register u_int32_t *tl;
3597 register int32_t t1;
3599 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3601 struct mbuf *mb, *mb2, *mreq;
3604 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3608 fhp = &nfh.fh_generic;
3610 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3613 * XXX At this time VOP_FSYNC() does not accept offset and byte
3614 * count parameters, so these arguments are useless (someday maybe).
3616 off = fxdr_hyper(tl);
3618 cnt = fxdr_unsigned(int, *tl);
3619 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3620 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3622 nfsm_reply(2 * NFSX_UNSIGNED);
3623 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3627 for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
3629 if (cnt > MAX_COMMIT_COUNT) {
3631 * Give up and do the whole thing
3634 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3635 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3637 error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
3640 * Locate and synchronously write any buffers that fall
3641 * into the requested range. Note: we are assuming that
3642 * f_iosize is a power of 2.
3644 int iosize = vp->v_mount->mnt_stat.f_iosize;
3645 int iomask = iosize - 1;
3650 * Align to iosize boundry, super-align to page boundry.
3653 cnt += off & iomask;
3654 off &= ~(u_quad_t)iomask;
3656 if (off & PAGE_MASK) {
3657 cnt += off & PAGE_MASK;
3658 off &= ~(u_quad_t)PAGE_MASK;
3660 lblkno = off / iosize;
3663 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3664 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3672 * If we have a buffer and it is marked B_DELWRI we
3673 * have to lock and write it. Otherwise the prior
3674 * write is assumed to have already been committed.
3676 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3677 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3678 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3679 continue; /* retry */
3682 bp->b_flags &= ~B_ASYNC;
3683 VOP_BWRITE(bp->b_vp, bp);
3695 aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
3698 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3699 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3701 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3702 if (nfsver.tv_sec == 0)
3704 *tl++ = txdr_unsigned(nfsver.tv_sec);
3705 *tl = txdr_unsigned(nfsver.tv_usec);
3716 * nfs statfs service
3719 nfsrv_statfs(nfsd, slp, procp, mrq)
3720 struct nfsrv_descript *nfsd;
3721 struct nfssvc_sock *slp;
3725 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3726 struct sockaddr *nam = nfsd->nd_nam;
3727 caddr_t dpos = nfsd->nd_dpos;
3728 struct ucred *cred = &nfsd->nd_cr;
3729 register struct statfs *sf;
3730 register struct nfs_statfs *sfp;
3731 register u_int32_t *tl;
3732 register int32_t t1;
3734 int error = 0, rdonly, cache, getret = 1;
3735 int v3 = (nfsd->nd_flag & ND_NFSV3);
3737 struct mbuf *mb, *mb2, *mreq;
3738 struct vnode *vp = NULL;
3742 struct statfs statfs;
3743 u_quad_t frev, tval;
3745 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3749 fhp = &nfh.fh_generic;
3751 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3752 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3754 nfsm_reply(NFSX_UNSIGNED);
3755 nfsm_srvpostop_attr(getret, &at);
3760 error = VFS_STATFS(vp->v_mount, sf, procp);
3761 getret = VOP_GETATTR(vp, &at, cred, procp);
3764 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3766 nfsm_srvpostop_attr(getret, &at);
3771 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3773 tval = (u_quad_t)sf->f_blocks;
3774 tval *= (u_quad_t)sf->f_bsize;
3775 txdr_hyper(tval, &sfp->sf_tbytes);
3776 tval = (u_quad_t)sf->f_bfree;
3777 tval *= (u_quad_t)sf->f_bsize;
3778 txdr_hyper(tval, &sfp->sf_fbytes);
3779 tval = (u_quad_t)sf->f_bavail;
3780 tval *= (u_quad_t)sf->f_bsize;
3781 txdr_hyper(tval, &sfp->sf_abytes);
3782 sfp->sf_tfiles.nfsuquad[0] = 0;
3783 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3784 sfp->sf_ffiles.nfsuquad[0] = 0;
3785 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3786 sfp->sf_afiles.nfsuquad[0] = 0;
3787 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3788 sfp->sf_invarsec = 0;
3790 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3791 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3792 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3793 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3794 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3803 * nfs fsinfo service
3806 nfsrv_fsinfo(nfsd, slp, procp, mrq)
3807 struct nfsrv_descript *nfsd;
3808 struct nfssvc_sock *slp;
3812 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3813 struct sockaddr *nam = nfsd->nd_nam;
3814 caddr_t dpos = nfsd->nd_dpos;
3815 struct ucred *cred = &nfsd->nd_cr;
3816 register u_int32_t *tl;
3817 register struct nfsv3_fsinfo *sip;
3818 register int32_t t1;
3820 int error = 0, rdonly, cache, getret = 1, pref;
3822 struct mbuf *mb, *mb2, *mreq;
3823 struct vnode *vp = NULL;
3827 u_quad_t frev, maxfsize;
3830 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3834 fhp = &nfh.fh_generic;
3836 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3837 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3839 nfsm_reply(NFSX_UNSIGNED);
3840 nfsm_srvpostop_attr(getret, &at);
3845 /* XXX Try to make a guess on the max file size. */
3846 VFS_STATFS(vp->v_mount, &sb, procp);
3847 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3849 getret = VOP_GETATTR(vp, &at, cred, procp);
3852 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3853 nfsm_srvpostop_attr(getret, &at);
3854 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3858 * There should be file system VFS OP(s) to get this information.
3859 * For now, assume ufs.
3861 if (slp->ns_so->so_type == SOCK_DGRAM)
3862 pref = NFS_MAXDGRAMDATA;
3865 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3866 sip->fs_rtpref = txdr_unsigned(pref);
3867 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3868 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3869 sip->fs_wtpref = txdr_unsigned(pref);
3870 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3871 sip->fs_dtpref = txdr_unsigned(pref);
3872 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3873 sip->fs_timedelta.nfsv3_sec = 0;
3874 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3875 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3876 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3877 NFSV3FSINFO_CANSETTIME);
3885 * nfs pathconf service
3888 nfsrv_pathconf(nfsd, slp, procp, mrq)
3889 struct nfsrv_descript *nfsd;
3890 struct nfssvc_sock *slp;
3894 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3895 struct sockaddr *nam = nfsd->nd_nam;
3896 caddr_t dpos = nfsd->nd_dpos;
3897 struct ucred *cred = &nfsd->nd_cr;
3898 register u_int32_t *tl;
3899 register struct nfsv3_pathconf *pc;
3900 register int32_t t1;
3902 int error = 0, rdonly, cache, getret = 1;
3903 register_t linkmax, namemax, chownres, notrunc;
3905 struct mbuf *mb, *mb2, *mreq;
3906 struct vnode *vp = NULL;
3912 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3916 fhp = &nfh.fh_generic;
3918 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3919 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3921 nfsm_reply(NFSX_UNSIGNED);
3922 nfsm_srvpostop_attr(getret, &at);
3926 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3928 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3930 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3932 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3933 getret = VOP_GETATTR(vp, &at, cred, procp);
3936 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3937 nfsm_srvpostop_attr(getret, &at);
3942 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3944 pc->pc_linkmax = txdr_unsigned(linkmax);
3945 pc->pc_namemax = txdr_unsigned(namemax);
3946 pc->pc_notrunc = txdr_unsigned(notrunc);
3947 pc->pc_chownrestricted = txdr_unsigned(chownres);
3950 * These should probably be supported by VOP_PATHCONF(), but
3951 * until msdosfs is exportable (why would you want to?), the
3952 * Unix defaults should be ok.
3954 pc->pc_caseinsensitive = nfs_false;
3955 pc->pc_casepreserving = nfs_true;
3963 * Null operation, used by clients to ping server
3967 nfsrv_null(nfsd, slp, procp, mrq)
3968 struct nfsrv_descript *nfsd;
3969 struct nfssvc_sock *slp;
3973 struct mbuf *mrep = nfsd->nd_mrep;
3975 int error = NFSERR_RETVOID, cache;
3976 struct mbuf *mb, *mreq;
3979 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3988 * No operation, used for obsolete procedures
3992 nfsrv_noop(nfsd, slp, procp, mrq)
3993 struct nfsrv_descript *nfsd;
3994 struct nfssvc_sock *slp;
3998 struct mbuf *mrep = nfsd->nd_mrep;
4001 struct mbuf *mb, *mreq;
4004 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4008 if (nfsd->nd_repstat)
4009 error = nfsd->nd_repstat;
4011 error = EPROCUNAVAIL;
4018 * Perform access checking for vnodes obtained from file handles that would
4019 * refer to files already opened by a Unix client. You cannot just use
4020 * vn_writechk() and VOP_ACCESS() for two reasons.
4021 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4022 * 2 - The owner is to be given access irrespective of mode bits for some
4023 * operations, so that processes that chmod after opening a file don't
4024 * break. I don't like this because it opens a security hole, but since
4025 * the nfs server opens a security hole the size of a barn door anyhow,
4028 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4029 * will return EPERM instead of EACCESS. EPERM is always an error.
4032 nfsrv_access(vp, flags, cred, rdonly, p, override)
4033 register struct vnode *vp;
4035 register struct ucred *cred;
4043 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4044 if (flags & VWRITE) {
4045 /* Just vn_writechk() changed to check rdonly */
4047 * Disallow write attempts on read-only file systems;
4048 * unless the file is a socket or a block or character
4049 * device resident on the file system.
4051 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4052 switch (vp->v_type) {
4062 * If there's shared text associated with
4063 * the inode, we can't allow writing.
4065 if (vp->v_flag & VTEXT)
4068 error = VOP_GETATTR(vp, &vattr, cred, p);
4071 error = VOP_ACCESS(vp, flags, cred, p);
4073 * Allow certain operations for the owner (reads and writes
4074 * on files that are already open).
4076 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4080 #endif /* NFS_NOSERVER */