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.16 2004/06/02 14:43:04 eirikn 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>
101 #include "xdr_subs.h"
102 #include "nfsm_subs.h"
106 #define nfsdbprintf(info) printf info
108 #define nfsdbprintf(info)
111 #define MAX_COMMIT_COUNT (1024 * 1024)
113 #define NUM_HEURISTIC 64
114 #define NHUSE_INIT 64
116 #define NHUSE_MAX 2048
118 static struct nfsheur {
119 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
120 off_t nh_nextr; /* next offset for sequential detection */
121 int nh_use; /* use count for selection */
122 int nh_seqcount; /* heuristic */
123 } nfsheur[NUM_HEURISTIC];
125 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
128 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
131 extern u_int32_t nfs_xdrneg1;
132 extern u_int32_t nfs_false, nfs_true;
133 extern enum vtype nv3tov_type[8];
134 extern struct nfsstats nfsstats;
136 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
137 int nfsrvw_procrastinate_v3 = 0;
139 static struct timespec nfsver;
141 SYSCTL_DECL(_vfs_nfs);
143 static int nfs_async;
144 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
145 static int nfs_commit_blks;
146 static int nfs_commit_miss;
147 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
148 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
150 static int nfsrv_access (struct vnode *,int,struct ucred *,int,
151 struct thread *, int);
152 static void nfsrvw_coalesce (struct nfsrv_descript *,
153 struct nfsrv_descript *);
156 * Clear nameidata fields that are tested in nsfmout cleanup code prior
157 * to using first nfsm macro (that might jump to the cleanup code).
162 ndclear(struct nameidata *nd)
164 nd->ni_cnd.cn_flags = 0;
167 nd->ni_startdir = NULL;
171 * nfs v3 access service
174 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
175 struct thread *td, struct mbuf **mrq)
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;
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, td, 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, td, 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, td, 0))
227 nfsmode &= ~testmode;
228 getret = VOP_GETATTR(vp, vap, td);
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(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
246 struct thread *td, struct mbuf **mrq)
248 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
249 struct sockaddr *nam = nfsd->nd_nam;
250 caddr_t dpos = nfsd->nd_dpos;
251 struct ucred *cred = &nfsd->nd_cr;
252 struct nfs_fattr *fp;
254 struct vattr *vap = &va;
255 struct vnode *vp = NULL;
261 int error = 0, rdonly, cache;
263 struct mbuf *mb, *mb2, *mreq;
266 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
267 fhp = &nfh.fh_generic;
269 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
270 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
276 nqsrv_getl(vp, ND_READ);
277 error = VOP_GETATTR(vp, vap, td);
280 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
285 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
286 nfsm_srvfillattr(vap, fp);
296 * nfs setattr service
299 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
300 struct thread *td, struct mbuf **mrq)
302 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
303 struct sockaddr *nam = nfsd->nd_nam;
304 caddr_t dpos = nfsd->nd_dpos;
305 struct ucred *cred = &nfsd->nd_cr;
306 struct vattr va, preat;
307 struct vattr *vap = &va;
308 struct nfsv2_sattr *sp;
309 struct nfs_fattr *fp;
310 struct vnode *vp = NULL;
316 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
317 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
319 struct mbuf *mb, *mb2, *mreq;
321 struct timespec guard;
323 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
324 fhp = &nfh.fh_generic;
329 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
330 gcheck = fxdr_unsigned(int, *tl);
332 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
333 fxdr_nfsv3time(tl, &guard);
336 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
338 * Nah nah nah nah na nah
339 * There is a bug in the Sun client that puts 0xffff in the mode
340 * field of sattr when it should put in 0xffffffff. The u_short
341 * doesn't sign extend.
342 * --> check the low order 2 bytes for 0xffff
344 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
345 vap->va_mode = nfstov_mode(sp->sa_mode);
346 if (sp->sa_uid != nfs_xdrneg1)
347 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
348 if (sp->sa_gid != nfs_xdrneg1)
349 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
350 if (sp->sa_size != nfs_xdrneg1)
351 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
352 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
354 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
356 vap->va_atime.tv_sec =
357 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
358 vap->va_atime.tv_nsec = 0;
361 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
362 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
367 * Now that we have all the fields, lets do it.
369 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
370 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
372 nfsm_reply(2 * NFSX_UNSIGNED);
373 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
379 * vp now an active resource, pay careful attention to cleanup
382 nqsrv_getl(vp, ND_WRITE);
384 error = preat_ret = VOP_GETATTR(vp, &preat, td);
385 if (!error && gcheck &&
386 (preat.va_ctime.tv_sec != guard.tv_sec ||
387 preat.va_ctime.tv_nsec != guard.tv_nsec))
388 error = NFSERR_NOT_SYNC;
392 nfsm_reply(NFSX_WCCDATA(v3));
393 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
400 * If the size is being changed write acces is required, otherwise
401 * just check for a read only file system.
403 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
404 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
409 if (vp->v_type == VDIR) {
412 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
417 error = VOP_SETATTR(vp, vap, cred, td);
418 postat_ret = VOP_GETATTR(vp, vap, td);
424 nfsm_reply(NFSX_WCCORFATTR(v3));
426 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
430 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
431 nfsm_srvfillattr(vap, fp);
445 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
446 struct thread *td, struct mbuf **mrq)
448 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
449 struct sockaddr *nam = nfsd->nd_nam;
450 caddr_t dpos = nfsd->nd_dpos;
451 struct ucred *cred = &nfsd->nd_cr;
452 struct nfs_fattr *fp;
453 struct nameidata nd, ind, *ndp = &nd;
454 struct vnode *vp, *dirp = NULL;
461 int error = 0, cache, len, dirattr_ret = 1;
462 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
464 struct mbuf *mb, *mb2, *mreq;
465 struct vattr va, dirattr, *vap = &va;
468 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
471 fhp = &nfh.fh_generic;
473 nfsm_srvnamesiz(len);
475 pubflag = nfs_ispublicfh(fhp);
477 nd.ni_cnd.cn_cred = cred;
478 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
479 nd.ni_cnd.cn_flags = CNP_LOCKLEAF | CNP_SAVESTART;
480 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
481 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
484 * namei failure, only dirp to cleanup. Clear out garbarge from
485 * structure in case macros jump to nfsmout.
491 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
495 nfsm_reply(NFSX_POSTOPATTR(v3));
496 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
502 * Locate index file for public filehandle
504 * error is 0 on entry and 0 on exit from this block.
508 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
510 * Setup call to lookup() to see if we can find
511 * the index file. Arguably, this doesn't belong
512 * in a kernel.. Ugh. If an error occurs, do not
513 * try to install an index file and then clear the
516 * When we replace nd with ind and redirect ndp,
517 * maintenance of ni_startdir and ni_vp shift to
518 * ind and we have to clean them up in the old nd.
519 * However, the cnd resource continues to be maintained
520 * via the original nd. Confused? You aren't alone!
523 VOP_UNLOCK(nd.ni_vp, NULL, 0, td);
524 ind.ni_pathlen = strlen(nfs_pub.np_index);
525 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
527 ind.ni_startdir = nd.ni_vp;
528 vref(ind.ni_startdir);
530 error = lookup(&ind);
535 * Found an index file. Get rid of
536 * the old references. transfer nd.ni_vp'
542 vrele(nd.ni_startdir);
543 nd.ni_startdir = NULL;
549 * If the public filehandle was used, check that this lookup
550 * didn't result in a filehandle outside the publicly exported
551 * filesystem. We clear the poor vp here to avoid lockups due
555 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
564 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
570 * Resources at this point:
571 * ndp->ni_vp may not be NULL
576 nfsm_reply(NFSX_POSTOPATTR(v3));
577 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
582 nqsrv_getl(ndp->ni_startdir, ND_READ);
585 * Clear out some resources prior to potentially blocking. This
586 * is not as critical as ni_dvp resources in other routines, but
589 vrele(ndp->ni_startdir);
590 ndp->ni_startdir = NULL;
591 NDFREE(&nd, NDF_ONLY_PNBUF);
594 * Get underlying attribute, then release remaining resources ( for
595 * the same potential blocking reason ) and reply.
598 bzero((caddr_t)fhp, sizeof(nfh));
599 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
600 error = VFS_VPTOFH(vp, &fhp->fh_fid);
602 error = VOP_GETATTR(vp, vap, td);
606 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
608 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
612 nfsm_srvfhtom(fhp, v3);
614 nfsm_srvpostop_attr(0, vap);
615 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
617 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
618 nfsm_srvfillattr(vap, fp);
624 NDFREE(&nd, NDF_ONLY_PNBUF);
625 if (ndp->ni_startdir)
626 vrele(ndp->ni_startdir);
633 * nfs readlink service
636 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
637 struct thread *td, struct mbuf **mrq)
639 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
640 struct sockaddr *nam = nfsd->nd_nam;
641 caddr_t dpos = nfsd->nd_dpos;
642 struct ucred *cred = &nfsd->nd_cr;
643 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
644 struct iovec *ivp = iv;
649 int error = 0, rdonly, cache, i, tlen, len, getret;
650 int v3 = (nfsd->nd_flag & ND_NFSV3);
652 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
653 struct vnode *vp = NULL;
657 struct uio io, *uiop = &io;
660 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
662 mp2 = (struct mbuf *)0;
665 fhp = &nfh.fh_generic;
669 while (len < NFS_MAXPATHLEN) {
670 MGET(mp, MB_WAIT, MT_DATA);
672 mp->m_len = NFSMSIZ(mp);
679 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
680 mp->m_len = NFS_MAXPATHLEN-len;
681 len = NFS_MAXPATHLEN;
684 ivp->iov_base = mtod(mp, caddr_t);
685 ivp->iov_len = mp->m_len;
690 uiop->uio_iovcnt = i;
691 uiop->uio_offset = 0;
692 uiop->uio_resid = len;
693 uiop->uio_rw = UIO_READ;
694 uiop->uio_segflg = UIO_SYSSPACE;
696 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
697 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
699 nfsm_reply(2 * NFSX_UNSIGNED);
700 nfsm_srvpostop_attr(1, (struct vattr *)0);
704 if (vp->v_type != VLNK) {
711 nqsrv_getl(vp, ND_READ);
712 error = VOP_READLINK(vp, uiop, cred);
714 getret = VOP_GETATTR(vp, &attr, td);
717 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
719 nfsm_srvpostop_attr(getret, &attr);
725 if (uiop->uio_resid > 0) {
726 len -= uiop->uio_resid;
727 tlen = nfsm_rndup(len);
728 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
730 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
731 *tl = txdr_unsigned(len);
746 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
747 struct thread *td, struct mbuf **mrq)
749 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
750 struct sockaddr *nam = nfsd->nd_nam;
751 caddr_t dpos = nfsd->nd_dpos;
752 struct ucred *cred = &nfsd->nd_cr;
756 struct nfs_fattr *fp;
761 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
762 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
764 struct mbuf *mb, *mb2, *mreq;
766 struct vnode *vp = NULL;
769 struct uio io, *uiop = &io;
770 struct vattr va, *vap = &va;
776 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
777 fhp = &nfh.fh_generic;
780 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
781 off = fxdr_hyper(tl);
783 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
784 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
786 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
789 * Reference vp. If an error occurs, vp will be invalid, but we
790 * have to NULL it just in case. The macros might goto nfsmout
794 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
795 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
798 nfsm_reply(2 * NFSX_UNSIGNED);
799 nfsm_srvpostop_attr(1, (struct vattr *)0);
804 if (vp->v_type != VREG) {
808 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
811 nqsrv_getl(vp, ND_READ);
812 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
813 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
815 getret = VOP_GETATTR(vp, vap, td);
821 nfsm_reply(NFSX_POSTOPATTR(v3));
822 nfsm_srvpostop_attr(getret, vap);
828 * Calculate byte count to read
831 if (off >= vap->va_size)
833 else if ((off + reqlen) > vap->va_size)
834 cnt = vap->va_size - off;
839 * Calculate seqcount for heuristic
847 * Locate best candidate
850 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
854 if (nfsheur[hi].nh_vp == vp) {
858 if (nfsheur[hi].nh_use > 0)
859 --nfsheur[hi].nh_use;
860 hi = (hi + 1) & (NUM_HEURISTIC - 1);
861 if (nfsheur[hi].nh_use < nh->nh_use)
865 if (nh->nh_vp != vp) {
868 nh->nh_use = NHUSE_INIT;
876 * Calculate heuristic
879 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
880 if (++nh->nh_seqcount > IO_SEQMAX)
881 nh->nh_seqcount = IO_SEQMAX;
882 } else if (nh->nh_seqcount > 1) {
887 nh->nh_use += NHUSE_INC;
888 if (nh->nh_use > NHUSE_MAX)
889 nh->nh_use = NHUSE_MAX;
890 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
893 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
895 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
897 fp = (struct nfs_fattr *)tl;
898 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
900 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
901 fp = (struct nfs_fattr *)tl;
902 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
904 len = left = nfsm_rndup(cnt);
907 * Generate the mbuf list with the uio_iov ref. to it.
912 siz = min(M_TRAILINGSPACE(m), left);
918 MGET(m, MB_WAIT, MT_DATA);
925 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
927 uiop->uio_iov = iv2 = iv;
933 panic("nfsrv_read iov");
934 siz = min(M_TRAILINGSPACE(m), left);
936 iv->iov_base = mtod(m, caddr_t) + m->m_len;
945 uiop->uio_iovcnt = i;
946 uiop->uio_offset = off;
947 uiop->uio_resid = len;
948 uiop->uio_rw = UIO_READ;
949 uiop->uio_segflg = UIO_SYSSPACE;
950 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
951 off = uiop->uio_offset;
953 FREE((caddr_t)iv2, M_TEMP);
954 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
960 nfsm_reply(NFSX_POSTOPATTR(v3));
961 nfsm_srvpostop_attr(getret, vap);
970 nfsm_srvfillattr(vap, fp);
971 tlen = len - uiop->uio_resid;
972 cnt = cnt < tlen ? cnt : tlen;
973 tlen = nfsm_rndup(cnt);
974 if (len != tlen || tlen != cnt)
975 nfsm_adj(mb, len - tlen, tlen - cnt);
977 *tl++ = txdr_unsigned(cnt);
983 *tl = txdr_unsigned(cnt);
994 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
995 struct thread *td, struct mbuf **mrq)
997 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
998 struct sockaddr *nam = nfsd->nd_nam;
999 caddr_t dpos = nfsd->nd_dpos;
1000 struct ucred *cred = &nfsd->nd_cr;
1004 struct nfs_fattr *fp;
1006 struct vattr va, forat;
1007 struct vattr *vap = &va;
1011 int error = 0, rdonly, cache, len, forat_ret = 1;
1012 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1013 int stable = NFSV3WRITE_FILESYNC;
1014 int v3 = (nfsd->nd_flag & ND_NFSV3);
1016 struct mbuf *mb, *mb2, *mreq;
1017 struct vnode *vp = NULL;
1020 struct uio io, *uiop = &io;
1024 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1030 fhp = &nfh.fh_generic;
1033 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1034 off = fxdr_hyper(tl);
1036 stable = fxdr_unsigned(int, *tl++);
1038 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1039 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1042 stable = NFSV3WRITE_UNSTABLE;
1044 retlen = len = fxdr_unsigned(int32_t, *tl);
1048 * For NFS Version 2, it is not obvious what a write of zero length
1049 * should do, but I might as well be consistent with Version 3,
1050 * which is to return ok so long as there are no permission problems.
1058 adjust = dpos - mtod(mp, caddr_t);
1059 mp->m_len -= adjust;
1060 if (mp->m_len > 0 && adjust > 0)
1061 NFSMADV(mp, adjust);
1065 else if (mp->m_len > 0) {
1068 mp->m_len -= (i - len);
1077 if (len > NFS_MAXDATA || len < 0 || i < len) {
1079 nfsm_reply(2 * NFSX_UNSIGNED);
1080 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1084 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1085 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1088 nfsm_reply(2 * NFSX_UNSIGNED);
1089 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1094 forat_ret = VOP_GETATTR(vp, &forat, td);
1095 if (vp->v_type != VREG) {
1099 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1102 nqsrv_getl(vp, ND_WRITE);
1103 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1108 nfsm_reply(NFSX_WCCDATA(v3));
1109 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1115 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1117 uiop->uio_iov = iv = ivp;
1118 uiop->uio_iovcnt = cnt;
1121 if (mp->m_len > 0) {
1122 ivp->iov_base = mtod(mp, caddr_t);
1123 ivp->iov_len = mp->m_len;
1131 * The IO_METASYNC flag indicates that all metadata (and not just
1132 * enough to ensure data integrity) mus be written to stable storage
1134 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1136 if (stable == NFSV3WRITE_UNSTABLE)
1137 ioflags = IO_NODELOCKED;
1138 else if (stable == NFSV3WRITE_DATASYNC)
1139 ioflags = (IO_SYNC | IO_NODELOCKED);
1141 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1142 uiop->uio_resid = len;
1143 uiop->uio_rw = UIO_WRITE;
1144 uiop->uio_segflg = UIO_SYSSPACE;
1145 uiop->uio_td = NULL;
1146 uiop->uio_offset = off;
1147 error = VOP_WRITE(vp, uiop, ioflags, cred);
1148 nfsstats.srvvop_writes++;
1149 FREE((caddr_t)iv, M_TEMP);
1151 aftat_ret = VOP_GETATTR(vp, vap, td);
1156 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1157 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1159 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1164 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1165 *tl++ = txdr_unsigned(retlen);
1167 * If nfs_async is set, then pretend the write was FILESYNC.
1169 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1170 *tl++ = txdr_unsigned(stable);
1172 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1174 * Actually, there is no need to txdr these fields,
1175 * but it may make the values more human readable,
1176 * for debugging purposes.
1178 if (nfsver.tv_sec == 0)
1180 *tl++ = txdr_unsigned(nfsver.tv_sec);
1181 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1183 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1184 nfsm_srvfillattr(vap, fp);
1193 * NFS write service with write gathering support. Called when
1194 * nfsrvw_procrastinate > 0.
1195 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1196 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1200 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1201 struct thread *td, struct mbuf **mrq)
1205 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1206 struct nfs_fattr *fp;
1209 struct nfsrvw_delayhash *wpp;
1211 struct vattr va, forat;
1215 int error = 0, rdonly, cache, len, forat_ret = 1;
1216 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1218 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1219 struct vnode *vp = NULL;
1220 struct uio io, *uiop = &io;
1221 u_quad_t frev, cur_usec;
1223 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1232 mrep = nfsd->nd_mrep;
1234 dpos = nfsd->nd_dpos;
1235 cred = &nfsd->nd_cr;
1236 v3 = (nfsd->nd_flag & ND_NFSV3);
1237 LIST_INIT(&nfsd->nd_coalesce);
1238 nfsd->nd_mreq = NULL;
1239 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1240 cur_usec = nfs_curusec();
1241 nfsd->nd_time = cur_usec +
1242 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1245 * Now, get the write header..
1247 nfsm_srvmtofh(&nfsd->nd_fh);
1249 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1250 nfsd->nd_off = fxdr_hyper(tl);
1252 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1254 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1255 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1258 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1260 len = fxdr_unsigned(int32_t, *tl);
1262 nfsd->nd_eoff = nfsd->nd_off + len;
1265 * Trim the header out of the mbuf list and trim off any trailing
1266 * junk so that the mbuf list has only the write data.
1274 adjust = dpos - mtod(mp, caddr_t);
1275 mp->m_len -= adjust;
1276 if (mp->m_len > 0 && adjust > 0)
1277 NFSMADV(mp, adjust);
1284 mp->m_len -= (i - len);
1290 if (len > NFS_MAXDATA || len < 0 || i < len) {
1294 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1296 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1297 nfsd->nd_mreq = mreq;
1298 nfsd->nd_mrep = NULL;
1303 * Add this entry to the hash and time queues.
1307 wp = slp->ns_tq.lh_first;
1308 while (wp && wp->nd_time < nfsd->nd_time) {
1310 wp = wp->nd_tq.le_next;
1312 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1314 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1316 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1318 if (nfsd->nd_mrep) {
1319 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1323 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1325 wp = wp->nd_hash.le_next;
1327 while (wp && wp->nd_off < nfsd->nd_off &&
1328 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1330 wp = wp->nd_hash.le_next;
1333 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1336 * Search the hash list for overlapping entries and
1339 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1340 wp = nfsd->nd_hash.le_next;
1341 if (NFSW_SAMECRED(owp, nfsd))
1342 nfsrvw_coalesce(owp, nfsd);
1345 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1352 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1353 * and generate the associated reply mbuf list(s).
1356 cur_usec = nfs_curusec();
1358 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1359 owp = nfsd->nd_tq.le_next;
1360 if (nfsd->nd_time > cur_usec)
1364 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1365 LIST_REMOVE(nfsd, nd_tq);
1366 LIST_REMOVE(nfsd, nd_hash);
1368 mrep = nfsd->nd_mrep;
1369 nfsd->nd_mrep = NULL;
1370 cred = &nfsd->nd_cr;
1371 v3 = (nfsd->nd_flag & ND_NFSV3);
1372 forat_ret = aftat_ret = 1;
1373 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1374 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1377 forat_ret = VOP_GETATTR(vp, &forat, td);
1378 if (vp->v_type != VREG) {
1382 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1388 nqsrv_getl(vp, ND_WRITE);
1389 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1392 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1393 ioflags = IO_NODELOCKED;
1394 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1395 ioflags = (IO_SYNC | IO_NODELOCKED);
1397 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1398 uiop->uio_rw = UIO_WRITE;
1399 uiop->uio_segflg = UIO_SYSSPACE;
1400 uiop->uio_td = NULL;
1401 uiop->uio_offset = nfsd->nd_off;
1402 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1403 if (uiop->uio_resid > 0) {
1411 uiop->uio_iovcnt = i;
1412 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1414 uiop->uio_iov = ivp = iov;
1417 if (mp->m_len > 0) {
1418 ivp->iov_base = mtod(mp, caddr_t);
1419 ivp->iov_len = mp->m_len;
1425 error = VOP_WRITE(vp, uiop, ioflags, cred);
1426 nfsstats.srvvop_writes++;
1428 FREE((caddr_t)iov, M_TEMP);
1432 aftat_ret = VOP_GETATTR(vp, &va, td);
1438 * Loop around generating replies for all write rpcs that have
1439 * now been completed.
1443 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1445 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1447 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1450 nfsm_writereply(NFSX_PREOPATTR(v3) +
1451 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1452 NFSX_WRITEVERF(v3), v3);
1454 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1455 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1456 *tl++ = txdr_unsigned(nfsd->nd_len);
1457 *tl++ = txdr_unsigned(swp->nd_stable);
1459 * Actually, there is no need to txdr these fields,
1460 * but it may make the values more human readable,
1461 * for debugging purposes.
1463 if (nfsver.tv_sec == 0)
1465 *tl++ = txdr_unsigned(nfsver.tv_sec);
1466 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1468 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1469 nfsm_srvfillattr(&va, fp);
1472 nfsd->nd_mreq = mreq;
1474 panic("nfsrv_write: nd_mrep not free");
1477 * Done. Put it at the head of the timer queue so that
1478 * the final phase can return the reply.
1483 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1485 nfsd = swp->nd_coalesce.lh_first;
1487 LIST_REMOVE(nfsd, nd_tq);
1493 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1500 * Search for a reply to return.
1503 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1504 if (nfsd->nd_mreq) {
1505 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1506 LIST_REMOVE(nfsd, nd_tq);
1507 *mrq = nfsd->nd_mreq;
1516 * Coalesce the write request nfsd into owp. To do this we must:
1517 * - remove nfsd from the queues
1518 * - merge nfsd->nd_mrep into owp->nd_mrep
1519 * - update the nd_eoff and nd_stable for owp
1520 * - put nfsd on owp's nd_coalesce list
1521 * NB: Must be called at splsoftclock().
1524 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1528 struct nfsrv_descript *p;
1530 NFS_DPF(WG, ("C%03x-%03x",
1531 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1532 LIST_REMOVE(nfsd, nd_hash);
1533 LIST_REMOVE(nfsd, nd_tq);
1534 if (owp->nd_eoff < nfsd->nd_eoff) {
1535 overlap = owp->nd_eoff - nfsd->nd_off;
1537 panic("nfsrv_coalesce: bad off");
1539 m_adj(nfsd->nd_mrep, overlap);
1543 mp->m_next = nfsd->nd_mrep;
1544 owp->nd_eoff = nfsd->nd_eoff;
1546 m_freem(nfsd->nd_mrep);
1547 nfsd->nd_mrep = NULL;
1548 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1549 owp->nd_stable = NFSV3WRITE_FILESYNC;
1550 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1551 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1552 owp->nd_stable = NFSV3WRITE_DATASYNC;
1553 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1556 * If nfsd had anything else coalesced into it, transfer them
1557 * to owp, otherwise their replies will never get sent.
1559 for (p = nfsd->nd_coalesce.lh_first; p;
1560 p = nfsd->nd_coalesce.lh_first) {
1561 LIST_REMOVE(p, nd_tq);
1562 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1567 * nfs create service
1568 * now does a truncate to 0 length via. setattr if it already exists
1571 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1572 struct thread *td, struct mbuf **mrq)
1574 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1575 struct sockaddr *nam = nfsd->nd_nam;
1576 caddr_t dpos = nfsd->nd_dpos;
1577 struct ucred *cred = &nfsd->nd_cr;
1578 struct nfs_fattr *fp;
1579 struct vattr va, dirfor, diraft;
1580 struct vattr *vap = &va;
1581 struct nfsv2_sattr *sp;
1583 struct nameidata nd;
1586 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1587 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1590 struct mbuf *mb, *mb2, *mreq;
1591 struct vnode *dirp = (struct vnode *)0;
1594 u_quad_t frev, tempsize;
1595 u_char cverf[NFSX_V3CREATEVERF];
1597 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1603 fhp = &nfh.fh_generic;
1605 nfsm_srvnamesiz(len);
1607 nd.ni_cnd.cn_cred = cred;
1608 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
1609 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_SAVESTART;
1612 * Call namei and do initial cleanup to get a few things
1613 * out of the way. If we get an initial error we cleanup
1614 * and return here to avoid special-casing the invalid nd
1615 * structure through the rest of the case. dirp may be
1616 * set even if an error occurs, but the nd structure will not
1617 * be valid at all if an error occurs so we have to invalidate it
1618 * prior to calling nfsm_reply ( which might goto nfsmout ).
1620 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1621 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1624 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1631 nfsm_reply(NFSX_WCCDATA(v3));
1632 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1638 * No error. Continue. State:
1640 * startdir is valid ( we release this immediately )
1642 * nd.ni_vp may be valid
1643 * nd.ni_dvp is valid
1645 * The error state is set through the code and we may also do some
1646 * opportunistic releasing of vnodes to avoid holding locks through
1647 * NFS I/O. The cleanup at the end is a catch-all
1652 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1653 how = fxdr_unsigned(int, *tl);
1655 case NFSV3CREATE_GUARDED:
1661 case NFSV3CREATE_UNCHECKED:
1664 case NFSV3CREATE_EXCLUSIVE:
1665 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1666 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1670 vap->va_type = VREG;
1672 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1673 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1674 if (vap->va_type == VNON)
1675 vap->va_type = VREG;
1676 vap->va_mode = nfstov_mode(sp->sa_mode);
1677 switch (vap->va_type) {
1679 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1681 vap->va_size = (u_quad_t)tsize;
1686 rdev = fxdr_unsigned(long, sp->sa_size);
1694 * Iff doesn't exist, create it
1695 * otherwise just truncate to 0 length
1696 * should I set the mode too ?
1698 * The only possible error we can have at this point is EEXIST.
1699 * nd.ni_vp will also be non-NULL in that case.
1701 if (nd.ni_vp == NULL) {
1702 if (vap->va_mode == (mode_t)VNOVAL)
1704 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1705 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1706 error = VOP_CREATE(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1709 NDFREE(&nd, NDF_ONLY_PNBUF);
1711 nfsrv_object_create(nd.ni_vp);
1712 if (exclusive_flag) {
1715 bcopy(cverf, (caddr_t)&vap->va_atime,
1717 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1722 vap->va_type == VCHR ||
1723 vap->va_type == VBLK ||
1724 vap->va_type == VFIFO
1727 * Handle SysV FIFO node special cases. All other
1728 * devices require super user to access.
1730 if (vap->va_type == VCHR && rdev == 0xffffffff)
1731 vap->va_type = VFIFO;
1732 if (vap->va_type != VFIFO &&
1733 (error = suser_cred(cred, 0))) {
1736 vap->va_rdev = rdev;
1737 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1739 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1742 NDFREE(&nd, NDF_ONLY_PNBUF);
1749 * release dvp prior to lookup
1757 * Even though LOCKPARENT was cleared, ni_dvp may
1760 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1761 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1762 nd.ni_cnd.cn_td = td;
1763 nd.ni_cnd.cn_cred = cred;
1765 error = lookup(&nd);
1770 /* fall through on certain errors */
1772 nfsrv_object_create(nd.ni_vp);
1773 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1781 if (vap->va_size != -1) {
1782 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1783 (nd.ni_cnd.cn_flags & CNP_RDONLY), td, 0);
1785 nqsrv_getl(nd.ni_vp, ND_WRITE);
1786 tempsize = vap->va_size;
1788 vap->va_size = tempsize;
1789 error = VOP_SETATTR(nd.ni_vp, vap, cred, td);
1795 bzero((caddr_t)fhp, sizeof(nfh));
1796 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1797 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1799 error = VOP_GETATTR(nd.ni_vp, vap, td);
1802 if (exclusive_flag && !error &&
1803 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1805 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1809 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1812 nfsm_srvpostop_fh(fhp);
1813 nfsm_srvpostop_attr(0, vap);
1815 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1818 nfsm_srvfhtom(fhp, v3);
1819 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1820 nfsm_srvfillattr(vap, fp);
1830 if (nd.ni_startdir) {
1831 vrele(nd.ni_startdir);
1832 nd.ni_startdir = NULL;
1836 NDFREE(&nd, NDF_ONLY_PNBUF);
1838 if (nd.ni_dvp == nd.ni_vp)
1849 * nfs v3 mknod service
1852 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1853 struct thread *td, struct mbuf **mrq)
1855 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1856 struct sockaddr *nam = nfsd->nd_nam;
1857 caddr_t dpos = nfsd->nd_dpos;
1858 struct ucred *cred = &nfsd->nd_cr;
1859 struct vattr va, dirfor, diraft;
1860 struct vattr *vap = &va;
1862 struct nameidata nd;
1865 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1866 u_int32_t major, minor;
1869 struct mbuf *mb, *mb2, *mreq;
1870 struct vnode *vp, *dirp = (struct vnode *)0;
1875 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1878 fhp = &nfh.fh_generic;
1880 nfsm_srvnamesiz(len);
1882 nd.ni_cnd.cn_cred = cred;
1883 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
1884 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_SAVESTART;
1887 * Handle nfs_namei() call. If an error occurs, the nd structure
1888 * is not valid. However, nfsm_*() routines may still jump to
1892 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1893 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1895 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1897 nfsm_reply(NFSX_WCCDATA(1));
1898 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1902 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1903 vtyp = nfsv3tov_type(*tl);
1904 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1905 error = NFSERR_BADTYPE;
1910 if (vtyp == VCHR || vtyp == VBLK) {
1911 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1912 major = fxdr_unsigned(u_int32_t, *tl++);
1913 minor = fxdr_unsigned(u_int32_t, *tl);
1914 vap->va_rdev = makeudev(major, minor);
1918 * Iff doesn't exist, create it.
1924 vap->va_type = vtyp;
1925 if (vap->va_mode == (mode_t)VNOVAL)
1927 if (vtyp == VSOCK) {
1928 vrele(nd.ni_startdir);
1929 nd.ni_startdir = NULL;
1930 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1931 error = VOP_CREATE(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1934 NDFREE(&nd, NDF_ONLY_PNBUF);
1936 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1938 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1940 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1943 NDFREE(&nd, NDF_ONLY_PNBUF);
1950 * Release dvp prior to lookup
1955 KKASSERT(td->td_proc);
1956 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1957 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1958 nd.ni_cnd.cn_td = td;
1959 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1961 error = lookup(&nd);
1966 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1971 * send response, cleanup, return.
1974 if (nd.ni_startdir) {
1975 vrele(nd.ni_startdir);
1976 nd.ni_startdir = NULL;
1978 NDFREE(&nd, NDF_ONLY_PNBUF);
1980 if (nd.ni_dvp == nd.ni_vp)
1988 bzero((caddr_t)fhp, sizeof(nfh));
1989 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1990 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1992 error = VOP_GETATTR(vp, vap, td);
1999 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2004 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2006 nfsm_srvpostop_fh(fhp);
2007 nfsm_srvpostop_attr(0, vap);
2009 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2015 vrele(nd.ni_startdir);
2016 NDFREE(&nd, NDF_ONLY_PNBUF);
2018 if (nd.ni_dvp == nd.ni_vp)
2029 * nfs remove service
2032 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2033 struct thread *td, struct mbuf **mrq)
2035 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2036 struct sockaddr *nam = nfsd->nd_nam;
2037 caddr_t dpos = nfsd->nd_dpos;
2038 struct ucred *cred = &nfsd->nd_cr;
2039 struct nameidata nd;
2043 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2044 int v3 = (nfsd->nd_flag & ND_NFSV3);
2046 struct mbuf *mb, *mreq;
2048 struct vattr dirfor, diraft;
2053 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2056 fhp = &nfh.fh_generic;
2058 nfsm_srvnamesiz(len);
2060 nd.ni_cnd.cn_cred = cred;
2061 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2062 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2063 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2064 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2067 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2074 if (nd.ni_vp->v_type == VDIR) {
2075 error = EPERM; /* POSIX */
2079 * The root of a mounted filesystem cannot be deleted.
2081 if (nd.ni_vp->v_flag & VROOT) {
2087 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2088 nqsrv_getl(nd.ni_vp, ND_WRITE);
2089 error = VOP_REMOVE(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2090 NDFREE(&nd, NDF_ONLY_PNBUF);
2094 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2098 nfsm_reply(NFSX_WCCDATA(v3));
2100 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2104 NDFREE(&nd, NDF_ONLY_PNBUF);
2106 if (nd.ni_dvp == nd.ni_vp)
2117 * nfs rename service
2120 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2121 struct thread *td, struct mbuf **mrq)
2123 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2124 struct sockaddr *nam = nfsd->nd_nam;
2125 caddr_t dpos = nfsd->nd_dpos;
2126 struct ucred *cred = &nfsd->nd_cr;
2130 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2131 int tdirfor_ret = 1, tdiraft_ret = 1;
2132 int v3 = (nfsd->nd_flag & ND_NFSV3);
2134 struct mbuf *mb, *mreq;
2135 struct nameidata fromnd, tond;
2136 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2137 struct vnode *tdirp = (struct vnode *)0;
2138 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2140 fhandle_t *ffhp, *tfhp;
2144 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2146 fvp = (struct vnode *)0;
2148 ffhp = &fnfh.fh_generic;
2149 tfhp = &tnfh.fh_generic;
2152 * Clear fields incase goto nfsmout occurs from macro.
2158 nfsm_srvmtofh(ffhp);
2159 nfsm_srvnamesiz(len);
2161 * Remember our original uid so that we can reset cr_uid before
2162 * the second nfs_namei() call, in case it is remapped.
2164 saved_uid = cred->cr_uid;
2165 fromnd.ni_cnd.cn_cred = cred;
2166 fromnd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2167 fromnd.ni_cnd.cn_flags = CNP_WANTPARENT | CNP_SAVESTART;
2168 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2169 &dpos, &fdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2172 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2179 nfsm_reply(2 * NFSX_WCCDATA(v3));
2180 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2181 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2186 nfsm_srvmtofh(tfhp);
2187 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2188 cred->cr_uid = saved_uid;
2189 tond.ni_cnd.cn_cred = cred;
2190 tond.ni_cnd.cn_nameiop = NAMEI_RENAME;
2191 tond.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF |
2192 CNP_NOCACHE | CNP_SAVESTART;
2193 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2194 &dpos, &tdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2197 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2209 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2215 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2222 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2230 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2237 if (fvp->v_mount != tdvp->v_mount) {
2251 * If source is the same as the destination (that is the
2252 * same vnode with the same name in the same directory),
2253 * then there is nothing to do.
2255 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2256 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2257 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2258 fromnd.ni_cnd.cn_namelen))
2263 * The VOP_RENAME function releases all vnode references &
2264 * locks prior to returning so we need to clear the pointers
2265 * to bypass cleanup code later on.
2267 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2268 nqsrv_getl(tdvp, ND_WRITE);
2270 nqsrv_getl(tvp, ND_WRITE);
2272 error = VOP_RENAME(fromnd.ni_dvp, NCPNULL, fromnd.ni_vp, &fromnd.ni_cnd,
2273 tond.ni_dvp, NCPNULL, tond.ni_vp, &tond.ni_cnd);
2274 fromnd.ni_dvp = NULL;
2275 fromnd.ni_vp = NULL;
2279 fromnd.ni_cnd.cn_flags &= ~CNP_HASBUF;
2280 tond.ni_cnd.cn_flags &= ~CNP_HASBUF;
2290 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2292 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2293 nfsm_reply(2 * NFSX_WCCDATA(v3));
2295 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2296 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2303 * Clear out tond related fields
2307 if (tond.ni_startdir)
2308 vrele(tond.ni_startdir);
2309 NDFREE(&tond, NDF_ONLY_PNBUF);
2311 if (tond.ni_dvp == tond.ni_vp)
2320 * Clear out fromnd related fields
2324 if (fromnd.ni_startdir)
2325 vrele(fromnd.ni_startdir);
2326 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2328 vrele(fromnd.ni_dvp);
2330 vrele(fromnd.ni_vp);
2339 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2340 struct thread *td, struct mbuf **mrq)
2342 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2343 struct sockaddr *nam = nfsd->nd_nam;
2344 caddr_t dpos = nfsd->nd_dpos;
2345 struct ucred *cred = &nfsd->nd_cr;
2346 struct nameidata nd;
2350 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2351 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2353 struct mbuf *mb, *mreq;
2354 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2355 struct vattr dirfor, diraft, at;
2357 fhandle_t *fhp, *dfhp;
2360 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2363 fhp = &nfh.fh_generic;
2364 dfhp = &dnfh.fh_generic;
2366 nfsm_srvmtofh(dfhp);
2367 nfsm_srvnamesiz(len);
2369 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2370 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2372 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2373 nfsm_srvpostop_attr(getret, &at);
2374 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2379 if (vp->v_type == VDIR) {
2380 error = EPERM; /* POSIX */
2383 nd.ni_cnd.cn_cred = cred;
2384 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2385 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2386 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2387 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2390 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2405 if (vp->v_mount != xp->v_mount)
2409 nqsrv_getl(vp, ND_WRITE);
2410 nqsrv_getl(xp, ND_WRITE);
2411 error = VOP_LINK(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
2412 NDFREE(&nd, NDF_ONLY_PNBUF);
2418 getret = VOP_GETATTR(vp, &at, td);
2420 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2421 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2423 nfsm_srvpostop_attr(getret, &at);
2424 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2430 NDFREE(&nd, NDF_ONLY_PNBUF);
2436 if (nd.ni_dvp == nd.ni_vp)
2447 * nfs symbolic link service
2450 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2451 struct thread *td, struct mbuf **mrq)
2453 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2454 struct sockaddr *nam = nfsd->nd_nam;
2455 caddr_t dpos = nfsd->nd_dpos;
2456 struct ucred *cred = &nfsd->nd_cr;
2457 struct vattr va, dirfor, diraft;
2458 struct nameidata nd;
2459 struct vattr *vap = &va;
2462 struct nfsv2_sattr *sp;
2463 char *bpos, *pathcp = (char *)0, *cp2;
2466 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2467 int v3 = (nfsd->nd_flag & ND_NFSV3);
2468 struct mbuf *mb, *mreq, *mb2;
2469 struct vnode *dirp = (struct vnode *)0;
2474 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2477 fhp = &nfh.fh_generic;
2479 nfsm_srvnamesiz(len);
2480 nd.ni_cnd.cn_cred = cred;
2481 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2482 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_SAVESTART;
2483 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2484 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2487 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2499 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2500 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2501 iv.iov_base = pathcp;
2503 io.uio_resid = len2;
2507 io.uio_segflg = UIO_SYSSPACE;
2508 io.uio_rw = UIO_READ;
2510 nfsm_mtouio(&io, len2);
2512 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2513 vap->va_mode = nfstov_mode(sp->sa_mode);
2515 *(pathcp + len2) = '\0';
2522 * issue symlink op. SAVESTART is set so the underlying path component
2523 * is only freed by the VOP if an error occurs.
2525 if (vap->va_mode == (mode_t)VNOVAL)
2527 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2528 error = VOP_SYMLINK(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2530 NDFREE(&nd, NDF_ONLY_PNBUF);
2535 * releases directory prior to potential lookup op.
2543 * Issue lookup. Leave SAVESTART set so we can easily free
2544 * the name buffer later on.
2546 * since LOCKPARENT is not set, ni_dvp will be garbage on
2547 * return whether an error occurs or not.
2549 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2550 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2551 nd.ni_cnd.cn_flags |= CNP_LOCKLEAF;
2552 nd.ni_cnd.cn_td = td;
2553 nd.ni_cnd.cn_cred = cred;
2555 error = lookup(&nd);
2559 bzero((caddr_t)fhp, sizeof(nfh));
2560 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2561 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2563 error = VOP_GETATTR(nd.ni_vp, vap, td);
2571 * These releases aren't strictly required, does even doing them
2572 * make any sense? XXX can nfsm_reply() block?
2575 FREE(pathcp, M_TEMP);
2579 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2583 if (nd.ni_startdir) {
2584 vrele(nd.ni_startdir);
2585 nd.ni_startdir = NULL;
2587 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2590 nfsm_srvpostop_fh(fhp);
2591 nfsm_srvpostop_attr(0, vap);
2593 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2599 NDFREE(&nd, NDF_ONLY_PNBUF);
2601 if (nd.ni_dvp == nd.ni_vp)
2609 vrele(nd.ni_startdir);
2613 FREE(pathcp, M_TEMP);
2622 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2623 struct thread *td, struct mbuf **mrq)
2625 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2626 struct sockaddr *nam = nfsd->nd_nam;
2627 caddr_t dpos = nfsd->nd_dpos;
2628 struct ucred *cred = &nfsd->nd_cr;
2629 struct vattr va, dirfor, diraft;
2630 struct vattr *vap = &va;
2631 struct nfs_fattr *fp;
2632 struct nameidata nd;
2637 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2638 int v3 = (nfsd->nd_flag & ND_NFSV3);
2640 struct mbuf *mb, *mb2, *mreq;
2641 struct vnode *dirp = NULL;
2647 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2650 fhp = &nfh.fh_generic;
2652 nfsm_srvnamesiz(len);
2653 nd.ni_cnd.cn_cred = cred;
2654 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2655 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2657 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2658 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2661 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2668 nfsm_reply(NFSX_WCCDATA(v3));
2669 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2677 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2678 vap->va_mode = nfstov_mode(*tl++);
2682 * At this point nd.ni_dvp is referenced and exclusively locked and
2683 * nd.ni_vp, if it exists, is referenced but not locked.
2686 vap->va_type = VDIR;
2687 if (nd.ni_vp != NULL) {
2688 NDFREE(&nd, NDF_ONLY_PNBUF);
2694 * Issue mkdir op. Since SAVESTART is not set, the pathname
2695 * component is freed by the VOP call. This will fill-in
2696 * nd.ni_vp, reference, and exclusively lock it.
2698 if (vap->va_mode == (mode_t)VNOVAL)
2700 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2701 error = VOP_MKDIR(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, vap);
2702 NDFREE(&nd, NDF_ONLY_PNBUF);
2709 bzero((caddr_t)fhp, sizeof(nfh));
2710 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2711 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2713 error = VOP_GETATTR(nd.ni_vp, vap, td);
2717 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2718 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2721 nfsm_srvpostop_fh(fhp);
2722 nfsm_srvpostop_attr(0, vap);
2724 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2726 nfsm_srvfhtom(fhp, v3);
2727 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2728 nfsm_srvfillattr(vap, fp);
2737 NDFREE(&nd, NDF_ONLY_PNBUF);
2738 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2756 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2757 struct thread *td, struct mbuf **mrq)
2759 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2760 struct sockaddr *nam = nfsd->nd_nam;
2761 caddr_t dpos = nfsd->nd_dpos;
2762 struct ucred *cred = &nfsd->nd_cr;
2766 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2767 int v3 = (nfsd->nd_flag & ND_NFSV3);
2769 struct mbuf *mb, *mreq;
2770 struct vnode *vp, *dirp = (struct vnode *)0;
2771 struct vattr dirfor, diraft;
2774 struct nameidata nd;
2777 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2780 fhp = &nfh.fh_generic;
2782 nfsm_srvnamesiz(len);
2783 nd.ni_cnd.cn_cred = cred;
2784 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2785 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2786 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2787 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2790 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2797 nfsm_reply(NFSX_WCCDATA(v3));
2798 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2803 if (vp->v_type != VDIR) {
2808 * No rmdir "." please.
2810 if (nd.ni_dvp == vp) {
2815 * The root of a mounted filesystem cannot be deleted.
2817 if (vp->v_flag & VROOT)
2821 * Issue or abort op. Since SAVESTART is not set, path name
2822 * component is freed by the VOP after either.
2825 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2826 nqsrv_getl(vp, ND_WRITE);
2827 error = VOP_RMDIR(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2829 NDFREE(&nd, NDF_ONLY_PNBUF);
2832 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2833 nfsm_reply(NFSX_WCCDATA(v3));
2835 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2841 NDFREE(&nd, NDF_ONLY_PNBUF);
2845 if (nd.ni_dvp == nd.ni_vp)
2857 * nfs readdir service
2858 * - mallocs what it thinks is enough to read
2859 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2860 * - calls VOP_READDIR()
2861 * - loops around building the reply
2862 * if the output generated exceeds count break out of loop
2863 * The nfsm_clget macro is used here so that the reply will be packed
2864 * tightly in mbuf clusters.
2865 * - it only knows that it has encountered eof when the VOP_READDIR()
2867 * - as such one readdir rpc will return eof false although you are there
2868 * and then the next will return eof
2869 * - it trims out records with d_fileno == 0
2870 * this doesn't matter for Unix clients, but they might confuse clients
2872 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2873 * than requested, but this may not apply to all filesystems. For
2874 * example, client NFS does not { although it is never remote mounted
2876 * The alternate call nfsrv_readdirplus() does lookups as well.
2877 * PS: The NFS protocol spec. does not clarify what the "count" byte
2878 * argument is a count of.. just name strings and file id's or the
2879 * entire reply rpc or ...
2880 * I tried just file name and id sizes and it confused the Sun client,
2881 * so I am using the full rpc size now. The "paranoia.." comment refers
2882 * to including the status longwords that are not a part of the dir.
2883 * "entry" structures, but are in the rpc.
2887 u_int32_t fl_postopok;
2888 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2890 u_int32_t fl_fhsize;
2891 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2895 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2896 struct thread *td, struct mbuf **mrq)
2898 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2899 struct sockaddr *nam = nfsd->nd_nam;
2900 caddr_t dpos = nfsd->nd_dpos;
2901 struct ucred *cred = &nfsd->nd_cr;
2909 struct mbuf *mb, *mb2, *mreq, *mp2;
2910 char *cpos, *cend, *cp2, *rbuf;
2911 struct vnode *vp = NULL;
2917 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2918 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2919 int v3 = (nfsd->nd_flag & ND_NFSV3);
2920 u_quad_t frev, off, toff, verf;
2921 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2923 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2924 fhp = &nfh.fh_generic;
2927 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2928 toff = fxdr_hyper(tl);
2930 verf = fxdr_hyper(tl);
2933 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2934 toff = fxdr_unsigned(u_quad_t, *tl++);
2935 verf = 0; /* shut up gcc */
2938 cnt = fxdr_unsigned(int, *tl);
2939 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2940 xfer = NFS_SRVMAXDATA(nfsd);
2946 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2947 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2948 if (!error && vp->v_type != VDIR) {
2954 nfsm_reply(NFSX_UNSIGNED);
2955 nfsm_srvpostop_attr(getret, &at);
2961 * Obtain lock on vnode for this section of the code
2964 nqsrv_getl(vp, ND_READ);
2966 error = getret = VOP_GETATTR(vp, &at, td);
2969 * XXX This check may be too strict for Solaris 2.5 clients.
2971 if (!error && toff && verf && verf != at.va_filerev)
2972 error = NFSERR_BAD_COOKIE;
2976 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
2980 nfsm_reply(NFSX_POSTOPATTR(v3));
2981 nfsm_srvpostop_attr(getret, &at);
2985 VOP_UNLOCK(vp, NULL, 0, td);
2988 * end section. Allocate rbuf and continue
2990 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2993 iv.iov_len = fullsiz;
2996 io.uio_offset = (off_t)off;
2997 io.uio_resid = fullsiz;
2998 io.uio_segflg = UIO_SYSSPACE;
2999 io.uio_rw = UIO_READ;
3002 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
3004 free((caddr_t)cookies, M_TEMP);
3007 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3008 off = (off_t)io.uio_offset;
3009 if (!cookies && !error)
3010 error = NFSERR_PERM;
3012 getret = VOP_GETATTR(vp, &at, td);
3016 VOP_UNLOCK(vp, NULL, 0, td);
3020 free((caddr_t)rbuf, M_TEMP);
3022 free((caddr_t)cookies, M_TEMP);
3023 nfsm_reply(NFSX_POSTOPATTR(v3));
3024 nfsm_srvpostop_attr(getret, &at);
3029 siz -= io.uio_resid;
3032 * If nothing read, return eof
3038 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3041 nfsm_srvpostop_attr(getret, &at);
3042 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3043 txdr_hyper(at.va_filerev, tl);
3046 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3049 FREE((caddr_t)rbuf, M_TEMP);
3050 FREE((caddr_t)cookies, M_TEMP);
3057 * Check for degenerate cases of nothing useful read.
3058 * If so go try again
3062 dp = (struct dirent *)cpos;
3065 * For some reason FreeBSD's ufs_readdir() chooses to back the
3066 * directory offset up to a block boundary, so it is necessary to
3067 * skip over the records that preceed the requested offset. This
3068 * requires the assumption that file offset cookies monotonically
3071 while (cpos < cend && ncookies > 0 &&
3072 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3073 ((u_quad_t)(*cookiep)) <= toff)) {
3074 cpos += dp->d_reclen;
3075 dp = (struct dirent *)cpos;
3079 if (cpos >= cend || ncookies == 0) {
3085 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3086 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3088 nfsm_srvpostop_attr(getret, &at);
3089 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3090 txdr_hyper(at.va_filerev, tl);
3094 be = bp + M_TRAILINGSPACE(mp);
3096 /* Loop through the records and build reply */
3097 while (cpos < cend && ncookies > 0) {
3098 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3099 nlen = dp->d_namlen;
3100 rem = nfsm_rndup(nlen) - nlen;
3101 len += (4 * NFSX_UNSIGNED + nlen + rem);
3103 len += 2 * NFSX_UNSIGNED;
3109 * Build the directory record xdr from
3114 bp += NFSX_UNSIGNED;
3118 bp += NFSX_UNSIGNED;
3121 *tl = txdr_unsigned(dp->d_fileno);
3122 bp += NFSX_UNSIGNED;
3124 *tl = txdr_unsigned(nlen);
3125 bp += NFSX_UNSIGNED;
3127 /* And loop around copying the name */
3136 bcopy(cp, bp, tsiz);
3142 /* And null pad to a int32_t boundary */
3143 for (i = 0; i < rem; i++)
3147 /* Finish off the record */
3150 bp += NFSX_UNSIGNED;
3153 *tl = txdr_unsigned(*cookiep);
3154 bp += NFSX_UNSIGNED;
3156 cpos += dp->d_reclen;
3157 dp = (struct dirent *)cpos;
3165 bp += NFSX_UNSIGNED;
3171 bp += NFSX_UNSIGNED;
3174 mp->m_len = bp - mtod(mp, caddr_t);
3176 mp->m_len += bp - bpos;
3177 FREE((caddr_t)rbuf, M_TEMP);
3178 FREE((caddr_t)cookies, M_TEMP);
3187 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3188 struct thread *td, struct mbuf **mrq)
3190 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3191 struct sockaddr *nam = nfsd->nd_nam;
3192 caddr_t dpos = nfsd->nd_dpos;
3193 struct ucred *cred = &nfsd->nd_cr;
3201 struct mbuf *mb, *mb2, *mreq, *mp2;
3202 char *cpos, *cend, *cp2, *rbuf;
3203 struct vnode *vp = NULL, *nvp;
3206 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3209 struct vattr va, at, *vap = &va;
3210 struct nfs_fattr *fp;
3211 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3212 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3213 u_quad_t frev, off, toff, verf;
3214 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3216 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3217 fhp = &nfh.fh_generic;
3219 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3220 toff = fxdr_hyper(tl);
3222 verf = fxdr_hyper(tl);
3224 siz = fxdr_unsigned(int, *tl++);
3225 cnt = fxdr_unsigned(int, *tl);
3227 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3228 xfer = NFS_SRVMAXDATA(nfsd);
3234 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3235 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3236 if (!error && vp->v_type != VDIR) {
3242 nfsm_reply(NFSX_UNSIGNED);
3243 nfsm_srvpostop_attr(getret, &at);
3247 error = getret = VOP_GETATTR(vp, &at, td);
3250 * XXX This check may be too strict for Solaris 2.5 clients.
3252 if (!error && toff && verf && verf != at.va_filerev)
3253 error = NFSERR_BAD_COOKIE;
3256 nqsrv_getl(vp, ND_READ);
3257 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3262 nfsm_reply(NFSX_V3POSTOPATTR);
3263 nfsm_srvpostop_attr(getret, &at);
3267 VOP_UNLOCK(vp, NULL, 0, td);
3268 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3271 iv.iov_len = fullsiz;
3274 io.uio_offset = (off_t)off;
3275 io.uio_resid = fullsiz;
3276 io.uio_segflg = UIO_SYSSPACE;
3277 io.uio_rw = UIO_READ;
3280 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
3282 free((caddr_t)cookies, M_TEMP);
3285 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3286 off = (u_quad_t)io.uio_offset;
3287 getret = VOP_GETATTR(vp, &at, td);
3288 VOP_UNLOCK(vp, NULL, 0, td);
3289 if (!cookies && !error)
3290 error = NFSERR_PERM;
3297 free((caddr_t)cookies, M_TEMP);
3298 free((caddr_t)rbuf, M_TEMP);
3299 nfsm_reply(NFSX_V3POSTOPATTR);
3300 nfsm_srvpostop_attr(getret, &at);
3305 siz -= io.uio_resid;
3308 * If nothing read, return eof
3314 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3316 nfsm_srvpostop_attr(getret, &at);
3317 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3318 txdr_hyper(at.va_filerev, tl);
3322 FREE((caddr_t)cookies, M_TEMP);
3323 FREE((caddr_t)rbuf, M_TEMP);
3330 * Check for degenerate cases of nothing useful read.
3331 * If so go try again
3335 dp = (struct dirent *)cpos;
3338 * For some reason FreeBSD's ufs_readdir() chooses to back the
3339 * directory offset up to a block boundary, so it is necessary to
3340 * skip over the records that preceed the requested offset. This
3341 * requires the assumption that file offset cookies monotonically
3344 while (cpos < cend && ncookies > 0 &&
3345 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3346 ((u_quad_t)(*cookiep)) <= toff)) {
3347 cpos += dp->d_reclen;
3348 dp = (struct dirent *)cpos;
3352 if (cpos >= cend || ncookies == 0) {
3359 * Probe one of the directory entries to see if the filesystem
3362 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3363 error = NFSERR_NOTSUPP;
3366 free((caddr_t)cookies, M_TEMP);
3367 free((caddr_t)rbuf, M_TEMP);
3368 nfsm_reply(NFSX_V3POSTOPATTR);
3369 nfsm_srvpostop_attr(getret, &at);
3376 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3378 nfsm_srvpostop_attr(getret, &at);
3379 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3380 txdr_hyper(at.va_filerev, tl);
3383 be = bp + M_TRAILINGSPACE(mp);
3385 /* Loop through the records and build reply */
3386 while (cpos < cend && ncookies > 0) {
3387 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3388 nlen = dp->d_namlen;
3389 rem = nfsm_rndup(nlen)-nlen;
3392 * For readdir_and_lookup get the vnode using
3395 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3397 bzero((caddr_t)nfhp, NFSX_V3FH);
3399 nvp->v_mount->mnt_stat.f_fsid;
3400 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3405 if (VOP_GETATTR(nvp, vap, td)) {
3414 * If either the dircount or maxcount will be
3415 * exceeded, get out now. Both of these lengths
3416 * are calculated conservatively, including all
3419 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3421 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3422 if (len > cnt || dirlen > fullsiz) {
3428 * Build the directory record xdr from
3431 fp = (struct nfs_fattr *)&fl.fl_fattr;
3432 nfsm_srvfillattr(vap, fp);
3433 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3434 fl.fl_fhok = nfs_true;
3435 fl.fl_postopok = nfs_true;
3436 fl.fl_off.nfsuquad[0] = 0;
3437 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3441 bp += NFSX_UNSIGNED;
3444 bp += NFSX_UNSIGNED;
3446 *tl = txdr_unsigned(dp->d_fileno);
3447 bp += NFSX_UNSIGNED;
3449 *tl = txdr_unsigned(nlen);
3450 bp += NFSX_UNSIGNED;
3452 /* And loop around copying the name */
3457 if ((bp + xfer) > be)
3461 bcopy(cp, bp, tsiz);
3467 /* And null pad to a int32_t boundary */
3468 for (i = 0; i < rem; i++)
3472 * Now copy the flrep structure out.
3474 xfer = sizeof (struct flrep);
3478 if ((bp + xfer) > be)
3482 bcopy(cp, bp, tsiz);
3490 cpos += dp->d_reclen;
3491 dp = (struct dirent *)cpos;
3499 bp += NFSX_UNSIGNED;
3505 bp += NFSX_UNSIGNED;
3508 mp->m_len = bp - mtod(mp, caddr_t);
3510 mp->m_len += bp - bpos;
3511 FREE((caddr_t)cookies, M_TEMP);
3512 FREE((caddr_t)rbuf, M_TEMP);
3520 * nfs commit service
3523 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3524 struct thread *td, struct mbuf **mrq)
3526 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3527 struct sockaddr *nam = nfsd->nd_nam;
3528 caddr_t dpos = nfsd->nd_dpos;
3529 struct ucred *cred = &nfsd->nd_cr;
3530 struct vattr bfor, aft;
3531 struct vnode *vp = NULL;
3537 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3539 struct mbuf *mb, *mb2, *mreq;
3542 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3546 fhp = &nfh.fh_generic;
3548 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3551 * XXX At this time VOP_FSYNC() does not accept offset and byte
3552 * count parameters, so these arguments are useless (someday maybe).
3554 off = fxdr_hyper(tl);
3556 cnt = fxdr_unsigned(int, *tl);
3557 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3558 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3560 nfsm_reply(2 * NFSX_UNSIGNED);
3561 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3565 for_ret = VOP_GETATTR(vp, &bfor, td);
3567 if (cnt > MAX_COMMIT_COUNT) {
3569 * Give up and do the whole thing
3572 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3573 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3575 error = VOP_FSYNC(vp, MNT_WAIT, td);
3578 * Locate and synchronously write any buffers that fall
3579 * into the requested range. Note: we are assuming that
3580 * f_iosize is a power of 2.
3582 int iosize = vp->v_mount->mnt_stat.f_iosize;
3583 int iomask = iosize - 1;
3588 * Align to iosize boundry, super-align to page boundry.
3591 cnt += off & iomask;
3592 off &= ~(u_quad_t)iomask;
3594 if (off & PAGE_MASK) {
3595 cnt += off & PAGE_MASK;
3596 off &= ~(u_quad_t)PAGE_MASK;
3598 lblkno = off / iosize;
3601 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3602 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3610 * If we have a buffer and it is marked B_DELWRI we
3611 * have to lock and write it. Otherwise the prior
3612 * write is assumed to have already been committed.
3614 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3615 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3616 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3617 continue; /* retry */
3620 bp->b_flags &= ~B_ASYNC;
3621 VOP_BWRITE(bp->b_vp, bp);
3633 aft_ret = VOP_GETATTR(vp, &aft, td);
3636 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3637 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3639 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3640 if (nfsver.tv_sec == 0)
3642 *tl++ = txdr_unsigned(nfsver.tv_sec);
3643 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3654 * nfs statfs service
3657 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3658 struct thread *td, struct mbuf **mrq)
3660 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3661 struct sockaddr *nam = nfsd->nd_nam;
3662 caddr_t dpos = nfsd->nd_dpos;
3663 struct ucred *cred = &nfsd->nd_cr;
3665 struct nfs_statfs *sfp;
3669 int error = 0, rdonly, cache, getret = 1;
3670 int v3 = (nfsd->nd_flag & ND_NFSV3);
3672 struct mbuf *mb, *mb2, *mreq;
3673 struct vnode *vp = NULL;
3677 struct statfs statfs;
3678 u_quad_t frev, tval;
3680 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3684 fhp = &nfh.fh_generic;
3686 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3687 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3689 nfsm_reply(NFSX_UNSIGNED);
3690 nfsm_srvpostop_attr(getret, &at);
3695 error = VFS_STATFS(vp->v_mount, sf, td);
3696 getret = VOP_GETATTR(vp, &at, td);
3699 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3701 nfsm_srvpostop_attr(getret, &at);
3706 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3708 tval = (u_quad_t)sf->f_blocks;
3709 tval *= (u_quad_t)sf->f_bsize;
3710 txdr_hyper(tval, &sfp->sf_tbytes);
3711 tval = (u_quad_t)sf->f_bfree;
3712 tval *= (u_quad_t)sf->f_bsize;
3713 txdr_hyper(tval, &sfp->sf_fbytes);
3714 tval = (u_quad_t)sf->f_bavail;
3715 tval *= (u_quad_t)sf->f_bsize;
3716 txdr_hyper(tval, &sfp->sf_abytes);
3717 sfp->sf_tfiles.nfsuquad[0] = 0;
3718 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3719 sfp->sf_ffiles.nfsuquad[0] = 0;
3720 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3721 sfp->sf_afiles.nfsuquad[0] = 0;
3722 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3723 sfp->sf_invarsec = 0;
3725 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3726 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3727 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3728 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3729 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3738 * nfs fsinfo service
3741 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3742 struct thread *td, struct mbuf **mrq)
3744 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3745 struct sockaddr *nam = nfsd->nd_nam;
3746 caddr_t dpos = nfsd->nd_dpos;
3747 struct ucred *cred = &nfsd->nd_cr;
3749 struct nfsv3_fsinfo *sip;
3752 int error = 0, rdonly, cache, getret = 1, pref;
3754 struct mbuf *mb, *mb2, *mreq;
3755 struct vnode *vp = NULL;
3759 u_quad_t frev, maxfsize;
3762 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3766 fhp = &nfh.fh_generic;
3768 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3769 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3771 nfsm_reply(NFSX_UNSIGNED);
3772 nfsm_srvpostop_attr(getret, &at);
3777 /* XXX Try to make a guess on the max file size. */
3778 VFS_STATFS(vp->v_mount, &sb, td);
3779 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3781 getret = VOP_GETATTR(vp, &at, td);
3784 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3785 nfsm_srvpostop_attr(getret, &at);
3786 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3790 * There should be file system VFS OP(s) to get this information.
3791 * For now, assume ufs.
3793 if (slp->ns_so->so_type == SOCK_DGRAM)
3794 pref = NFS_MAXDGRAMDATA;
3797 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3798 sip->fs_rtpref = txdr_unsigned(pref);
3799 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3800 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3801 sip->fs_wtpref = txdr_unsigned(pref);
3802 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3803 sip->fs_dtpref = txdr_unsigned(pref);
3804 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3805 sip->fs_timedelta.nfsv3_sec = 0;
3806 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3807 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3808 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3809 NFSV3FSINFO_CANSETTIME);
3817 * nfs pathconf service
3820 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3821 struct thread *td, struct mbuf **mrq)
3823 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3824 struct sockaddr *nam = nfsd->nd_nam;
3825 caddr_t dpos = nfsd->nd_dpos;
3826 struct ucred *cred = &nfsd->nd_cr;
3828 struct nfsv3_pathconf *pc;
3831 int error = 0, rdonly, cache, getret = 1;
3832 register_t linkmax, namemax, chownres, notrunc;
3834 struct mbuf *mb, *mb2, *mreq;
3835 struct vnode *vp = NULL;
3841 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3845 fhp = &nfh.fh_generic;
3847 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3848 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3850 nfsm_reply(NFSX_UNSIGNED);
3851 nfsm_srvpostop_attr(getret, &at);
3855 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3857 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3859 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3861 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3862 getret = VOP_GETATTR(vp, &at, td);
3865 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3866 nfsm_srvpostop_attr(getret, &at);
3871 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3873 pc->pc_linkmax = txdr_unsigned(linkmax);
3874 pc->pc_namemax = txdr_unsigned(namemax);
3875 pc->pc_notrunc = txdr_unsigned(notrunc);
3876 pc->pc_chownrestricted = txdr_unsigned(chownres);
3879 * These should probably be supported by VOP_PATHCONF(), but
3880 * until msdosfs is exportable (why would you want to?), the
3881 * Unix defaults should be ok.
3883 pc->pc_caseinsensitive = nfs_false;
3884 pc->pc_casepreserving = nfs_true;
3892 * Null operation, used by clients to ping server
3896 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3897 struct thread *td, struct mbuf **mrq)
3899 struct mbuf *mrep = nfsd->nd_mrep;
3901 int error = NFSERR_RETVOID, cache;
3902 struct mbuf *mb, *mreq;
3905 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3914 * No operation, used for obsolete procedures
3918 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3919 struct thread *td, struct mbuf **mrq)
3921 struct mbuf *mrep = nfsd->nd_mrep;
3924 struct mbuf *mb, *mreq;
3927 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3931 if (nfsd->nd_repstat)
3932 error = nfsd->nd_repstat;
3934 error = EPROCUNAVAIL;
3941 * Perform access checking for vnodes obtained from file handles that would
3942 * refer to files already opened by a Unix client. You cannot just use
3943 * vn_writechk() and VOP_ACCESS() for two reasons.
3944 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3945 * 2 - The owner is to be given access irrespective of mode bits for some
3946 * operations, so that processes that chmod after opening a file don't
3947 * break. I don't like this because it opens a security hole, but since
3948 * the nfs server opens a security hole the size of a barn door anyhow,
3951 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3952 * will return EPERM instead of EACCESS. EPERM is always an error.
3955 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
3956 int rdonly, struct thread *td, int override)
3961 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3962 if (flags & VWRITE) {
3963 /* Just vn_writechk() changed to check rdonly */
3965 * Disallow write attempts on read-only file systems;
3966 * unless the file is a socket or a block or character
3967 * device resident on the file system.
3969 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3970 switch (vp->v_type) {
3980 * If there's shared text associated with
3981 * the inode, we can't allow writing.
3983 if (vp->v_flag & VTEXT)
3986 error = VOP_GETATTR(vp, &vattr, td);
3989 error = VOP_ACCESS(vp, flags, cred, td);
3991 * Allow certain operations for the owner (reads and writes
3992 * on files that are already open).
3994 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3998 #endif /* NFS_NOSERVER */