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.19 2004/10/12 19:21:01 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>
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 1017
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, 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;
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;
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);
2091 cache_purge(nd.ni_vp);
2092 NDFREE(&nd, NDF_ONLY_PNBUF);
2096 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2100 nfsm_reply(NFSX_WCCDATA(v3));
2102 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2106 NDFREE(&nd, NDF_ONLY_PNBUF);
2108 if (nd.ni_dvp == nd.ni_vp)
2119 * nfs rename service
2122 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2123 struct thread *td, struct mbuf **mrq)
2125 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2126 struct sockaddr *nam = nfsd->nd_nam;
2127 caddr_t dpos = nfsd->nd_dpos;
2128 struct ucred *cred = &nfsd->nd_cr;
2132 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2133 int tdirfor_ret = 1, tdiraft_ret = 1;
2134 int v3 = (nfsd->nd_flag & ND_NFSV3);
2136 struct mbuf *mb, *mreq;
2137 struct nameidata fromnd, tond;
2138 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2139 struct vnode *tdirp = (struct vnode *)0;
2140 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2142 fhandle_t *ffhp, *tfhp;
2146 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2148 fvp = (struct vnode *)0;
2150 ffhp = &fnfh.fh_generic;
2151 tfhp = &tnfh.fh_generic;
2154 * Clear fields incase goto nfsmout occurs from macro.
2160 nfsm_srvmtofh(ffhp);
2161 nfsm_srvnamesiz(len);
2163 * Remember our original uid so that we can reset cr_uid before
2164 * the second nfs_namei() call, in case it is remapped.
2166 saved_uid = cred->cr_uid;
2167 fromnd.ni_cnd.cn_cred = cred;
2168 fromnd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2169 fromnd.ni_cnd.cn_flags = CNP_WANTPARENT | CNP_SAVESTART;
2170 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2171 &dpos, &fdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2174 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2181 nfsm_reply(2 * NFSX_WCCDATA(v3));
2182 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2183 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2188 nfsm_srvmtofh(tfhp);
2189 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2190 cred->cr_uid = saved_uid;
2191 tond.ni_cnd.cn_cred = cred;
2192 tond.ni_cnd.cn_nameiop = NAMEI_RENAME;
2193 tond.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF |
2194 CNP_NOCACHE | CNP_SAVESTART;
2195 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2196 &dpos, &tdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2199 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2211 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2217 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2224 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2232 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2239 if (fvp->v_mount != tdvp->v_mount) {
2253 * If source is the same as the destination (that is the
2254 * same vnode with the same name in the same directory),
2255 * then there is nothing to do.
2257 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2258 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2259 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2260 fromnd.ni_cnd.cn_namelen))
2265 * The VOP_RENAME function releases all vnode references &
2266 * locks prior to returning so we need to clear the pointers
2267 * to bypass cleanup code later on.
2269 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2270 nqsrv_getl(tdvp, ND_WRITE);
2272 nqsrv_getl(tvp, ND_WRITE);
2274 error = VOP_RENAME(fromnd.ni_dvp, NCPNULL, fromnd.ni_vp, &fromnd.ni_cnd,
2275 tond.ni_dvp, NCPNULL, tond.ni_vp, &tond.ni_cnd);
2277 cache_purge(fromnd.ni_vp);
2278 fromnd.ni_dvp = NULL;
2279 fromnd.ni_vp = NULL;
2283 fromnd.ni_cnd.cn_flags &= ~CNP_HASBUF;
2284 tond.ni_cnd.cn_flags &= ~CNP_HASBUF;
2294 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2296 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2297 nfsm_reply(2 * NFSX_WCCDATA(v3));
2299 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2300 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2307 * Clear out tond related fields
2311 if (tond.ni_startdir)
2312 vrele(tond.ni_startdir);
2313 NDFREE(&tond, NDF_ONLY_PNBUF);
2315 if (tond.ni_dvp == tond.ni_vp)
2324 * Clear out fromnd related fields
2328 if (fromnd.ni_startdir)
2329 vrele(fromnd.ni_startdir);
2330 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2332 vrele(fromnd.ni_dvp);
2334 vrele(fromnd.ni_vp);
2343 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2344 struct thread *td, struct mbuf **mrq)
2346 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2347 struct sockaddr *nam = nfsd->nd_nam;
2348 caddr_t dpos = nfsd->nd_dpos;
2349 struct ucred *cred = &nfsd->nd_cr;
2350 struct nameidata nd;
2354 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2355 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2357 struct mbuf *mb, *mreq;
2358 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2359 struct vattr dirfor, diraft, at;
2361 fhandle_t *fhp, *dfhp;
2364 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2367 fhp = &nfh.fh_generic;
2368 dfhp = &dnfh.fh_generic;
2370 nfsm_srvmtofh(dfhp);
2371 nfsm_srvnamesiz(len);
2373 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2374 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2376 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2377 nfsm_srvpostop_attr(getret, &at);
2378 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2383 if (vp->v_type == VDIR) {
2384 error = EPERM; /* POSIX */
2387 nd.ni_cnd.cn_cred = cred;
2388 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2389 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2390 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2391 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2394 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2409 if (vp->v_mount != xp->v_mount)
2413 nqsrv_getl(vp, ND_WRITE);
2414 nqsrv_getl(xp, ND_WRITE);
2415 error = VOP_LINK(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
2416 NDFREE(&nd, NDF_ONLY_PNBUF);
2422 getret = VOP_GETATTR(vp, &at, td);
2424 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2425 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2427 nfsm_srvpostop_attr(getret, &at);
2428 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2434 NDFREE(&nd, NDF_ONLY_PNBUF);
2440 if (nd.ni_dvp == nd.ni_vp)
2451 * nfs symbolic link service
2454 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2455 struct thread *td, struct mbuf **mrq)
2457 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2458 struct sockaddr *nam = nfsd->nd_nam;
2459 caddr_t dpos = nfsd->nd_dpos;
2460 struct ucred *cred = &nfsd->nd_cr;
2461 struct vattr va, dirfor, diraft;
2462 struct nameidata nd;
2463 struct vattr *vap = &va;
2466 struct nfsv2_sattr *sp;
2467 char *bpos, *pathcp = (char *)0, *cp2;
2470 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2471 int v3 = (nfsd->nd_flag & ND_NFSV3);
2472 struct mbuf *mb, *mreq, *mb2;
2473 struct vnode *dirp = (struct vnode *)0;
2478 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2481 fhp = &nfh.fh_generic;
2483 nfsm_srvnamesiz(len);
2484 nd.ni_cnd.cn_cred = cred;
2485 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2486 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_SAVESTART;
2487 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2488 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2491 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2503 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2504 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2505 iv.iov_base = pathcp;
2507 io.uio_resid = len2;
2511 io.uio_segflg = UIO_SYSSPACE;
2512 io.uio_rw = UIO_READ;
2514 nfsm_mtouio(&io, len2);
2516 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2517 vap->va_mode = nfstov_mode(sp->sa_mode);
2519 *(pathcp + len2) = '\0';
2526 * issue symlink op. SAVESTART is set so the underlying path component
2527 * is only freed by the VOP if an error occurs.
2529 if (vap->va_mode == (mode_t)VNOVAL)
2531 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2532 error = VOP_SYMLINK(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2534 NDFREE(&nd, NDF_ONLY_PNBUF);
2539 * releases directory prior to potential lookup op.
2547 * Issue lookup. Leave SAVESTART set so we can easily free
2548 * the name buffer later on.
2550 * since LOCKPARENT is not set, ni_dvp will be garbage on
2551 * return whether an error occurs or not.
2553 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2554 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2555 nd.ni_cnd.cn_flags |= CNP_LOCKLEAF;
2556 nd.ni_cnd.cn_td = td;
2557 nd.ni_cnd.cn_cred = cred;
2559 error = lookup(&nd);
2563 bzero((caddr_t)fhp, sizeof(nfh));
2564 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2565 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2567 error = VOP_GETATTR(nd.ni_vp, vap, td);
2575 * These releases aren't strictly required, does even doing them
2576 * make any sense? XXX can nfsm_reply() block?
2579 FREE(pathcp, M_TEMP);
2583 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2587 if (nd.ni_startdir) {
2588 vrele(nd.ni_startdir);
2589 nd.ni_startdir = NULL;
2591 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2594 nfsm_srvpostop_fh(fhp);
2595 nfsm_srvpostop_attr(0, vap);
2597 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2603 NDFREE(&nd, NDF_ONLY_PNBUF);
2605 if (nd.ni_dvp == nd.ni_vp)
2613 vrele(nd.ni_startdir);
2617 FREE(pathcp, M_TEMP);
2626 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2627 struct thread *td, struct mbuf **mrq)
2629 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2630 struct sockaddr *nam = nfsd->nd_nam;
2631 caddr_t dpos = nfsd->nd_dpos;
2632 struct ucred *cred = &nfsd->nd_cr;
2633 struct vattr va, dirfor, diraft;
2634 struct vattr *vap = &va;
2635 struct nfs_fattr *fp;
2636 struct nameidata nd;
2641 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2642 int v3 = (nfsd->nd_flag & ND_NFSV3);
2644 struct mbuf *mb, *mb2, *mreq;
2645 struct vnode *dirp = NULL;
2651 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2654 fhp = &nfh.fh_generic;
2656 nfsm_srvnamesiz(len);
2657 nd.ni_cnd.cn_cred = cred;
2658 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2659 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2661 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2662 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2665 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2672 nfsm_reply(NFSX_WCCDATA(v3));
2673 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2681 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2682 vap->va_mode = nfstov_mode(*tl++);
2686 * At this point nd.ni_dvp is referenced and exclusively locked and
2687 * nd.ni_vp, if it exists, is referenced but not locked.
2690 vap->va_type = VDIR;
2691 if (nd.ni_vp != NULL) {
2692 NDFREE(&nd, NDF_ONLY_PNBUF);
2698 * Issue mkdir op. Since SAVESTART is not set, the pathname
2699 * component is freed by the VOP call. This will fill-in
2700 * nd.ni_vp, reference, and exclusively lock it.
2702 if (vap->va_mode == (mode_t)VNOVAL)
2704 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2705 error = VOP_MKDIR(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, vap);
2706 NDFREE(&nd, NDF_ONLY_PNBUF);
2713 bzero((caddr_t)fhp, sizeof(nfh));
2714 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2715 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2717 error = VOP_GETATTR(nd.ni_vp, vap, td);
2721 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2722 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2725 nfsm_srvpostop_fh(fhp);
2726 nfsm_srvpostop_attr(0, vap);
2728 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2730 nfsm_srvfhtom(fhp, v3);
2731 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2732 nfsm_srvfillattr(vap, fp);
2741 NDFREE(&nd, NDF_ONLY_PNBUF);
2742 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2760 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2761 struct thread *td, struct mbuf **mrq)
2763 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2764 struct sockaddr *nam = nfsd->nd_nam;
2765 caddr_t dpos = nfsd->nd_dpos;
2766 struct ucred *cred = &nfsd->nd_cr;
2770 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2771 int v3 = (nfsd->nd_flag & ND_NFSV3);
2773 struct mbuf *mb, *mreq;
2774 struct vnode *vp, *dirp = (struct vnode *)0;
2775 struct vattr dirfor, diraft;
2778 struct nameidata nd;
2781 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2784 fhp = &nfh.fh_generic;
2786 nfsm_srvnamesiz(len);
2787 nd.ni_cnd.cn_cred = cred;
2788 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2789 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2790 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2791 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2794 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2801 nfsm_reply(NFSX_WCCDATA(v3));
2802 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2807 if (vp->v_type != VDIR) {
2812 * No rmdir "." please.
2814 if (nd.ni_dvp == vp) {
2819 * The root of a mounted filesystem cannot be deleted.
2821 if (vp->v_flag & VROOT)
2825 * Issue or abort op. Since SAVESTART is not set, path name
2826 * component is freed by the VOP after either.
2829 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2830 nqsrv_getl(vp, ND_WRITE);
2831 error = VOP_RMDIR(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2833 cache_purge(nd.ni_vp);
2835 NDFREE(&nd, NDF_ONLY_PNBUF);
2838 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2839 nfsm_reply(NFSX_WCCDATA(v3));
2841 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2847 NDFREE(&nd, NDF_ONLY_PNBUF);
2851 if (nd.ni_dvp == nd.ni_vp)
2863 * nfs readdir service
2864 * - mallocs what it thinks is enough to read
2865 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2866 * - calls VOP_READDIR()
2867 * - loops around building the reply
2868 * if the output generated exceeds count break out of loop
2869 * The nfsm_clget macro is used here so that the reply will be packed
2870 * tightly in mbuf clusters.
2871 * - it only knows that it has encountered eof when the VOP_READDIR()
2873 * - as such one readdir rpc will return eof false although you are there
2874 * and then the next will return eof
2875 * - it trims out records with d_fileno == 0
2876 * this doesn't matter for Unix clients, but they might confuse clients
2878 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2879 * than requested, but this may not apply to all filesystems. For
2880 * example, client NFS does not { although it is never remote mounted
2882 * The alternate call nfsrv_readdirplus() does lookups as well.
2883 * PS: The NFS protocol spec. does not clarify what the "count" byte
2884 * argument is a count of.. just name strings and file id's or the
2885 * entire reply rpc or ...
2886 * I tried just file name and id sizes and it confused the Sun client,
2887 * so I am using the full rpc size now. The "paranoia.." comment refers
2888 * to including the status longwords that are not a part of the dir.
2889 * "entry" structures, but are in the rpc.
2893 u_int32_t fl_postopok;
2894 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2896 u_int32_t fl_fhsize;
2897 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2901 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2902 struct thread *td, struct mbuf **mrq)
2904 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2905 struct sockaddr *nam = nfsd->nd_nam;
2906 caddr_t dpos = nfsd->nd_dpos;
2907 struct ucred *cred = &nfsd->nd_cr;
2915 struct mbuf *mb, *mb2, *mreq, *mp2;
2916 char *cpos, *cend, *cp2, *rbuf;
2917 struct vnode *vp = NULL;
2923 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2924 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2925 int v3 = (nfsd->nd_flag & ND_NFSV3);
2926 u_quad_t frev, off, toff, verf;
2927 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2929 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2930 fhp = &nfh.fh_generic;
2933 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2934 toff = fxdr_hyper(tl);
2936 verf = fxdr_hyper(tl);
2939 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2940 toff = fxdr_unsigned(u_quad_t, *tl++);
2941 verf = 0; /* shut up gcc */
2944 cnt = fxdr_unsigned(int, *tl);
2945 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2946 xfer = NFS_SRVMAXDATA(nfsd);
2952 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2953 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2954 if (!error && vp->v_type != VDIR) {
2960 nfsm_reply(NFSX_UNSIGNED);
2961 nfsm_srvpostop_attr(getret, &at);
2967 * Obtain lock on vnode for this section of the code
2970 nqsrv_getl(vp, ND_READ);
2972 error = getret = VOP_GETATTR(vp, &at, td);
2975 * XXX This check may be too strict for Solaris 2.5 clients.
2977 if (!error && toff && verf && verf != at.va_filerev)
2978 error = NFSERR_BAD_COOKIE;
2982 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
2986 nfsm_reply(NFSX_POSTOPATTR(v3));
2987 nfsm_srvpostop_attr(getret, &at);
2991 VOP_UNLOCK(vp, 0, td);
2994 * end section. Allocate rbuf and continue
2996 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2999 iv.iov_len = fullsiz;
3002 io.uio_offset = (off_t)off;
3003 io.uio_resid = fullsiz;
3004 io.uio_segflg = UIO_SYSSPACE;
3005 io.uio_rw = UIO_READ;
3008 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3010 free((caddr_t)cookies, M_TEMP);
3013 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3014 off = (off_t)io.uio_offset;
3015 if (!cookies && !error)
3016 error = NFSERR_PERM;
3018 getret = VOP_GETATTR(vp, &at, td);
3022 VOP_UNLOCK(vp, 0, td);
3026 free((caddr_t)rbuf, M_TEMP);
3028 free((caddr_t)cookies, M_TEMP);
3029 nfsm_reply(NFSX_POSTOPATTR(v3));
3030 nfsm_srvpostop_attr(getret, &at);
3035 siz -= io.uio_resid;
3038 * If nothing read, return eof
3044 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3047 nfsm_srvpostop_attr(getret, &at);
3048 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3049 txdr_hyper(at.va_filerev, tl);
3052 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3055 FREE((caddr_t)rbuf, M_TEMP);
3056 FREE((caddr_t)cookies, M_TEMP);
3063 * Check for degenerate cases of nothing useful read.
3064 * If so go try again
3068 dp = (struct dirent *)cpos;
3071 * For some reason FreeBSD's ufs_readdir() chooses to back the
3072 * directory offset up to a block boundary, so it is necessary to
3073 * skip over the records that preceed the requested offset. This
3074 * requires the assumption that file offset cookies monotonically
3077 while (cpos < cend && ncookies > 0 &&
3078 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3079 ((u_quad_t)(*cookiep)) <= toff)) {
3080 cpos += dp->d_reclen;
3081 dp = (struct dirent *)cpos;
3085 if (cpos >= cend || ncookies == 0) {
3091 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3092 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3094 nfsm_srvpostop_attr(getret, &at);
3095 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3096 txdr_hyper(at.va_filerev, tl);
3100 be = bp + M_TRAILINGSPACE(mp);
3102 /* Loop through the records and build reply */
3103 while (cpos < cend && ncookies > 0) {
3104 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3105 nlen = dp->d_namlen;
3106 rem = nfsm_rndup(nlen) - nlen;
3107 len += (4 * NFSX_UNSIGNED + nlen + rem);
3109 len += 2 * NFSX_UNSIGNED;
3115 * Build the directory record xdr from
3120 bp += NFSX_UNSIGNED;
3124 bp += NFSX_UNSIGNED;
3127 *tl = txdr_unsigned(dp->d_fileno);
3128 bp += NFSX_UNSIGNED;
3130 *tl = txdr_unsigned(nlen);
3131 bp += NFSX_UNSIGNED;
3133 /* And loop around copying the name */
3142 bcopy(cp, bp, tsiz);
3148 /* And null pad to a int32_t boundary */
3149 for (i = 0; i < rem; i++)
3153 /* Finish off the record */
3156 bp += NFSX_UNSIGNED;
3159 *tl = txdr_unsigned(*cookiep);
3160 bp += NFSX_UNSIGNED;
3162 cpos += dp->d_reclen;
3163 dp = (struct dirent *)cpos;
3171 bp += NFSX_UNSIGNED;
3177 bp += NFSX_UNSIGNED;
3180 mp->m_len = bp - mtod(mp, caddr_t);
3182 mp->m_len += bp - bpos;
3183 FREE((caddr_t)rbuf, M_TEMP);
3184 FREE((caddr_t)cookies, M_TEMP);
3193 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3194 struct thread *td, struct mbuf **mrq)
3196 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3197 struct sockaddr *nam = nfsd->nd_nam;
3198 caddr_t dpos = nfsd->nd_dpos;
3199 struct ucred *cred = &nfsd->nd_cr;
3207 struct mbuf *mb, *mb2, *mreq, *mp2;
3208 char *cpos, *cend, *cp2, *rbuf;
3209 struct vnode *vp = NULL, *nvp;
3212 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3215 struct vattr va, at, *vap = &va;
3216 struct nfs_fattr *fp;
3217 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3218 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3219 u_quad_t frev, off, toff, verf;
3220 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3222 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3223 fhp = &nfh.fh_generic;
3225 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3226 toff = fxdr_hyper(tl);
3228 verf = fxdr_hyper(tl);
3230 siz = fxdr_unsigned(int, *tl++);
3231 cnt = fxdr_unsigned(int, *tl);
3233 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3234 xfer = NFS_SRVMAXDATA(nfsd);
3240 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3241 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3242 if (!error && vp->v_type != VDIR) {
3248 nfsm_reply(NFSX_UNSIGNED);
3249 nfsm_srvpostop_attr(getret, &at);
3253 error = getret = VOP_GETATTR(vp, &at, td);
3256 * XXX This check may be too strict for Solaris 2.5 clients.
3258 if (!error && toff && verf && verf != at.va_filerev)
3259 error = NFSERR_BAD_COOKIE;
3262 nqsrv_getl(vp, ND_READ);
3263 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3268 nfsm_reply(NFSX_V3POSTOPATTR);
3269 nfsm_srvpostop_attr(getret, &at);
3273 VOP_UNLOCK(vp, 0, td);
3274 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3277 iv.iov_len = fullsiz;
3280 io.uio_offset = (off_t)off;
3281 io.uio_resid = fullsiz;
3282 io.uio_segflg = UIO_SYSSPACE;
3283 io.uio_rw = UIO_READ;
3286 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3288 free((caddr_t)cookies, M_TEMP);
3291 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3292 off = (u_quad_t)io.uio_offset;
3293 getret = VOP_GETATTR(vp, &at, td);
3294 VOP_UNLOCK(vp, 0, td);
3295 if (!cookies && !error)
3296 error = NFSERR_PERM;
3303 free((caddr_t)cookies, M_TEMP);
3304 free((caddr_t)rbuf, M_TEMP);
3305 nfsm_reply(NFSX_V3POSTOPATTR);
3306 nfsm_srvpostop_attr(getret, &at);
3311 siz -= io.uio_resid;
3314 * If nothing read, return eof
3320 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3322 nfsm_srvpostop_attr(getret, &at);
3323 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3324 txdr_hyper(at.va_filerev, tl);
3328 FREE((caddr_t)cookies, M_TEMP);
3329 FREE((caddr_t)rbuf, M_TEMP);
3336 * Check for degenerate cases of nothing useful read.
3337 * If so go try again
3341 dp = (struct dirent *)cpos;
3344 * For some reason FreeBSD's ufs_readdir() chooses to back the
3345 * directory offset up to a block boundary, so it is necessary to
3346 * skip over the records that preceed the requested offset. This
3347 * requires the assumption that file offset cookies monotonically
3350 while (cpos < cend && ncookies > 0 &&
3351 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3352 ((u_quad_t)(*cookiep)) <= toff)) {
3353 cpos += dp->d_reclen;
3354 dp = (struct dirent *)cpos;
3358 if (cpos >= cend || ncookies == 0) {
3365 * Probe one of the directory entries to see if the filesystem
3368 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3369 error = NFSERR_NOTSUPP;
3372 free((caddr_t)cookies, M_TEMP);
3373 free((caddr_t)rbuf, M_TEMP);
3374 nfsm_reply(NFSX_V3POSTOPATTR);
3375 nfsm_srvpostop_attr(getret, &at);
3382 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3384 nfsm_srvpostop_attr(getret, &at);
3385 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3386 txdr_hyper(at.va_filerev, tl);
3389 be = bp + M_TRAILINGSPACE(mp);
3391 /* Loop through the records and build reply */
3392 while (cpos < cend && ncookies > 0) {
3393 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3394 nlen = dp->d_namlen;
3395 rem = nfsm_rndup(nlen)-nlen;
3398 * For readdir_and_lookup get the vnode using
3401 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3403 bzero((caddr_t)nfhp, NFSX_V3FH);
3405 nvp->v_mount->mnt_stat.f_fsid;
3406 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3411 if (VOP_GETATTR(nvp, vap, td)) {
3420 * If either the dircount or maxcount will be
3421 * exceeded, get out now. Both of these lengths
3422 * are calculated conservatively, including all
3425 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3427 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3428 if (len > cnt || dirlen > fullsiz) {
3434 * Build the directory record xdr from
3437 fp = (struct nfs_fattr *)&fl.fl_fattr;
3438 nfsm_srvfillattr(vap, fp);
3439 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3440 fl.fl_fhok = nfs_true;
3441 fl.fl_postopok = nfs_true;
3442 fl.fl_off.nfsuquad[0] = 0;
3443 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3447 bp += NFSX_UNSIGNED;
3450 bp += NFSX_UNSIGNED;
3452 *tl = txdr_unsigned(dp->d_fileno);
3453 bp += NFSX_UNSIGNED;
3455 *tl = txdr_unsigned(nlen);
3456 bp += NFSX_UNSIGNED;
3458 /* And loop around copying the name */
3463 if ((bp + xfer) > be)
3467 bcopy(cp, bp, tsiz);
3473 /* And null pad to a int32_t boundary */
3474 for (i = 0; i < rem; i++)
3478 * Now copy the flrep structure out.
3480 xfer = sizeof (struct flrep);
3484 if ((bp + xfer) > be)
3488 bcopy(cp, bp, tsiz);
3496 cpos += dp->d_reclen;
3497 dp = (struct dirent *)cpos;
3505 bp += NFSX_UNSIGNED;
3511 bp += NFSX_UNSIGNED;
3514 mp->m_len = bp - mtod(mp, caddr_t);
3516 mp->m_len += bp - bpos;
3517 FREE((caddr_t)cookies, M_TEMP);
3518 FREE((caddr_t)rbuf, M_TEMP);
3526 * nfs commit service
3529 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3530 struct thread *td, struct mbuf **mrq)
3532 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3533 struct sockaddr *nam = nfsd->nd_nam;
3534 caddr_t dpos = nfsd->nd_dpos;
3535 struct ucred *cred = &nfsd->nd_cr;
3536 struct vattr bfor, aft;
3537 struct vnode *vp = NULL;
3543 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3545 struct mbuf *mb, *mb2, *mreq;
3548 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3552 fhp = &nfh.fh_generic;
3554 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3557 * XXX At this time VOP_FSYNC() does not accept offset and byte
3558 * count parameters, so these arguments are useless (someday maybe).
3560 off = fxdr_hyper(tl);
3562 cnt = fxdr_unsigned(int, *tl);
3563 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3564 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3566 nfsm_reply(2 * NFSX_UNSIGNED);
3567 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3571 for_ret = VOP_GETATTR(vp, &bfor, td);
3573 if (cnt > MAX_COMMIT_COUNT) {
3575 * Give up and do the whole thing
3578 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3579 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3581 error = VOP_FSYNC(vp, MNT_WAIT, td);
3584 * Locate and synchronously write any buffers that fall
3585 * into the requested range. Note: we are assuming that
3586 * f_iosize is a power of 2.
3588 int iosize = vp->v_mount->mnt_stat.f_iosize;
3589 int iomask = iosize - 1;
3594 * Align to iosize boundry, super-align to page boundry.
3597 cnt += off & iomask;
3598 off &= ~(u_quad_t)iomask;
3600 if (off & PAGE_MASK) {
3601 cnt += off & PAGE_MASK;
3602 off &= ~(u_quad_t)PAGE_MASK;
3604 lblkno = off / iosize;
3607 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3608 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3616 * If we have a buffer and it is marked B_DELWRI we
3617 * have to lock and write it. Otherwise the prior
3618 * write is assumed to have already been committed.
3620 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3621 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3622 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3623 continue; /* retry */
3626 bp->b_flags &= ~B_ASYNC;
3627 VOP_BWRITE(bp->b_vp, bp);
3639 aft_ret = VOP_GETATTR(vp, &aft, td);
3642 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3643 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3645 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3646 if (nfsver.tv_sec == 0)
3648 *tl++ = txdr_unsigned(nfsver.tv_sec);
3649 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3660 * nfs statfs service
3663 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3664 struct thread *td, struct mbuf **mrq)
3666 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3667 struct sockaddr *nam = nfsd->nd_nam;
3668 caddr_t dpos = nfsd->nd_dpos;
3669 struct ucred *cred = &nfsd->nd_cr;
3671 struct nfs_statfs *sfp;
3675 int error = 0, rdonly, cache, getret = 1;
3676 int v3 = (nfsd->nd_flag & ND_NFSV3);
3678 struct mbuf *mb, *mb2, *mreq;
3679 struct vnode *vp = NULL;
3683 struct statfs statfs;
3684 u_quad_t frev, tval;
3686 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3690 fhp = &nfh.fh_generic;
3692 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3693 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3695 nfsm_reply(NFSX_UNSIGNED);
3696 nfsm_srvpostop_attr(getret, &at);
3701 error = VFS_STATFS(vp->v_mount, sf, td);
3702 getret = VOP_GETATTR(vp, &at, td);
3705 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3707 nfsm_srvpostop_attr(getret, &at);
3712 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3714 tval = (u_quad_t)sf->f_blocks;
3715 tval *= (u_quad_t)sf->f_bsize;
3716 txdr_hyper(tval, &sfp->sf_tbytes);
3717 tval = (u_quad_t)sf->f_bfree;
3718 tval *= (u_quad_t)sf->f_bsize;
3719 txdr_hyper(tval, &sfp->sf_fbytes);
3720 tval = (u_quad_t)sf->f_bavail;
3721 tval *= (u_quad_t)sf->f_bsize;
3722 txdr_hyper(tval, &sfp->sf_abytes);
3723 sfp->sf_tfiles.nfsuquad[0] = 0;
3724 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3725 sfp->sf_ffiles.nfsuquad[0] = 0;
3726 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3727 sfp->sf_afiles.nfsuquad[0] = 0;
3728 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3729 sfp->sf_invarsec = 0;
3731 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3732 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3733 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3734 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3735 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3744 * nfs fsinfo service
3747 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3748 struct thread *td, struct mbuf **mrq)
3750 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3751 struct sockaddr *nam = nfsd->nd_nam;
3752 caddr_t dpos = nfsd->nd_dpos;
3753 struct ucred *cred = &nfsd->nd_cr;
3755 struct nfsv3_fsinfo *sip;
3758 int error = 0, rdonly, cache, getret = 1, pref;
3760 struct mbuf *mb, *mb2, *mreq;
3761 struct vnode *vp = NULL;
3765 u_quad_t frev, maxfsize;
3768 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3772 fhp = &nfh.fh_generic;
3774 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3775 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3777 nfsm_reply(NFSX_UNSIGNED);
3778 nfsm_srvpostop_attr(getret, &at);
3783 /* XXX Try to make a guess on the max file size. */
3784 VFS_STATFS(vp->v_mount, &sb, td);
3785 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3787 getret = VOP_GETATTR(vp, &at, td);
3790 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3791 nfsm_srvpostop_attr(getret, &at);
3792 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3796 * There should be file system VFS OP(s) to get this information.
3797 * For now, assume ufs.
3799 if (slp->ns_so->so_type == SOCK_DGRAM)
3800 pref = NFS_MAXDGRAMDATA;
3803 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3804 sip->fs_rtpref = txdr_unsigned(pref);
3805 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3806 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3807 sip->fs_wtpref = txdr_unsigned(pref);
3808 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3809 sip->fs_dtpref = txdr_unsigned(pref);
3810 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3811 sip->fs_timedelta.nfsv3_sec = 0;
3812 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3813 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3814 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3815 NFSV3FSINFO_CANSETTIME);
3823 * nfs pathconf service
3826 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3827 struct thread *td, struct mbuf **mrq)
3829 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3830 struct sockaddr *nam = nfsd->nd_nam;
3831 caddr_t dpos = nfsd->nd_dpos;
3832 struct ucred *cred = &nfsd->nd_cr;
3834 struct nfsv3_pathconf *pc;
3837 int error = 0, rdonly, cache, getret = 1;
3838 register_t linkmax, namemax, chownres, notrunc;
3840 struct mbuf *mb, *mb2, *mreq;
3841 struct vnode *vp = NULL;
3847 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3851 fhp = &nfh.fh_generic;
3853 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3854 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3856 nfsm_reply(NFSX_UNSIGNED);
3857 nfsm_srvpostop_attr(getret, &at);
3861 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3863 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3865 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3867 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3868 getret = VOP_GETATTR(vp, &at, td);
3871 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3872 nfsm_srvpostop_attr(getret, &at);
3877 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3879 pc->pc_linkmax = txdr_unsigned(linkmax);
3880 pc->pc_namemax = txdr_unsigned(namemax);
3881 pc->pc_notrunc = txdr_unsigned(notrunc);
3882 pc->pc_chownrestricted = txdr_unsigned(chownres);
3885 * These should probably be supported by VOP_PATHCONF(), but
3886 * until msdosfs is exportable (why would you want to?), the
3887 * Unix defaults should be ok.
3889 pc->pc_caseinsensitive = nfs_false;
3890 pc->pc_casepreserving = nfs_true;
3898 * Null operation, used by clients to ping server
3902 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3903 struct thread *td, struct mbuf **mrq)
3905 struct mbuf *mrep = nfsd->nd_mrep;
3907 int error = NFSERR_RETVOID, cache;
3908 struct mbuf *mb, *mreq;
3911 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3920 * No operation, used for obsolete procedures
3924 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3925 struct thread *td, struct mbuf **mrq)
3927 struct mbuf *mrep = nfsd->nd_mrep;
3930 struct mbuf *mb, *mreq;
3933 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3937 if (nfsd->nd_repstat)
3938 error = nfsd->nd_repstat;
3940 error = EPROCUNAVAIL;
3947 * Perform access checking for vnodes obtained from file handles that would
3948 * refer to files already opened by a Unix client. You cannot just use
3949 * vn_writechk() and VOP_ACCESS() for two reasons.
3950 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3951 * 2 - The owner is to be given access irrespective of mode bits for some
3952 * operations, so that processes that chmod after opening a file don't
3953 * break. I don't like this because it opens a security hole, but since
3954 * the nfs server opens a security hole the size of a barn door anyhow,
3957 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3958 * will return EPERM instead of EACCESS. EPERM is always an error.
3961 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
3962 int rdonly, struct thread *td, int override)
3967 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3968 if (flags & VWRITE) {
3969 /* Just vn_writechk() changed to check rdonly */
3971 * Disallow write attempts on read-only file systems;
3972 * unless the file is a socket or a block or character
3973 * device resident on the file system.
3975 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3976 switch (vp->v_type) {
3986 * If there's shared text associated with
3987 * the inode, we can't allow writing.
3989 if (vp->v_flag & VTEXT)
3992 error = VOP_GETATTR(vp, &vattr, td);
3995 error = VOP_ACCESS(vp, flags, cred, td);
3997 * Allow certain operations for the owner (reads and writes
3998 * on files that are already open).
4000 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4004 #endif /* NFS_NOSERVER */