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.10 2003/09/29 18:52:16 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 64
114 #define NHUSE_INIT 64
116 #define NHUSE_MAX 2048
118 static struct nfsheur {
119 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
120 off_t nh_nextr; /* next offset for sequential detection */
121 int nh_use; /* use count for selection */
122 int nh_seqcount; /* heuristic */
123 } nfsheur[NUM_HEURISTIC];
125 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
128 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
131 extern u_int32_t nfs_xdrneg1;
132 extern u_int32_t nfs_false, nfs_true;
133 extern enum vtype nv3tov_type[8];
134 extern struct nfsstats nfsstats;
136 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
137 int nfsrvw_procrastinate_v3 = 0;
139 static struct timeval nfsver = { 0 };
141 SYSCTL_DECL(_vfs_nfs);
143 static int nfs_async;
144 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
145 static int nfs_commit_blks;
146 static int nfs_commit_miss;
147 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
148 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
150 static int nfsrv_access (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(nfsd, slp, td, mrq)
246 struct nfsrv_descript *nfsd;
247 struct nfssvc_sock *slp;
251 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
252 struct sockaddr *nam = nfsd->nd_nam;
253 caddr_t dpos = nfsd->nd_dpos;
254 struct ucred *cred = &nfsd->nd_cr;
255 struct nfs_fattr *fp;
257 struct vattr *vap = &va;
258 struct vnode *vp = NULL;
264 int error = 0, rdonly, cache;
266 struct mbuf *mb, *mb2, *mreq;
269 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
270 fhp = &nfh.fh_generic;
272 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
273 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
279 nqsrv_getl(vp, ND_READ);
280 error = VOP_GETATTR(vp, vap, td);
283 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
288 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
289 nfsm_srvfillattr(vap, fp);
299 * nfs setattr service
302 nfsrv_setattr(nfsd, slp, td, mrq)
303 struct nfsrv_descript *nfsd;
304 struct nfssvc_sock *slp;
308 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
309 struct sockaddr *nam = nfsd->nd_nam;
310 caddr_t dpos = nfsd->nd_dpos;
311 struct ucred *cred = &nfsd->nd_cr;
312 struct vattr va, preat;
313 struct vattr *vap = &va;
314 struct nfsv2_sattr *sp;
315 struct nfs_fattr *fp;
316 struct vnode *vp = NULL;
322 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
323 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
325 struct mbuf *mb, *mb2, *mreq;
327 struct timespec guard;
329 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
330 fhp = &nfh.fh_generic;
335 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
336 gcheck = fxdr_unsigned(int, *tl);
338 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
339 fxdr_nfsv3time(tl, &guard);
342 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
344 * Nah nah nah nah na nah
345 * There is a bug in the Sun client that puts 0xffff in the mode
346 * field of sattr when it should put in 0xffffffff. The u_short
347 * doesn't sign extend.
348 * --> check the low order 2 bytes for 0xffff
350 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
351 vap->va_mode = nfstov_mode(sp->sa_mode);
352 if (sp->sa_uid != nfs_xdrneg1)
353 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
354 if (sp->sa_gid != nfs_xdrneg1)
355 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
356 if (sp->sa_size != nfs_xdrneg1)
357 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
358 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
360 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
362 vap->va_atime.tv_sec =
363 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
364 vap->va_atime.tv_nsec = 0;
367 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
368 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
373 * Now that we have all the fields, lets do it.
375 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
376 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
378 nfsm_reply(2 * NFSX_UNSIGNED);
379 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
385 * vp now an active resource, pay careful attention to cleanup
388 nqsrv_getl(vp, ND_WRITE);
390 error = preat_ret = VOP_GETATTR(vp, &preat, td);
391 if (!error && gcheck &&
392 (preat.va_ctime.tv_sec != guard.tv_sec ||
393 preat.va_ctime.tv_nsec != guard.tv_nsec))
394 error = NFSERR_NOT_SYNC;
398 nfsm_reply(NFSX_WCCDATA(v3));
399 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
406 * If the size is being changed write acces is required, otherwise
407 * just check for a read only file system.
409 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
410 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
415 if (vp->v_type == VDIR) {
418 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
423 error = VOP_SETATTR(vp, vap, cred, td);
424 postat_ret = VOP_GETATTR(vp, vap, td);
430 nfsm_reply(NFSX_WCCORFATTR(v3));
432 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
436 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
437 nfsm_srvfillattr(vap, fp);
451 nfsrv_lookup(nfsd, slp, td, mrq)
452 struct nfsrv_descript *nfsd;
453 struct nfssvc_sock *slp;
457 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
458 struct sockaddr *nam = nfsd->nd_nam;
459 caddr_t dpos = nfsd->nd_dpos;
460 struct ucred *cred = &nfsd->nd_cr;
461 struct nfs_fattr *fp;
462 struct nameidata nd, ind, *ndp = &nd;
463 struct vnode *vp, *dirp = NULL;
470 int error = 0, cache, len, dirattr_ret = 1;
471 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
473 struct mbuf *mb, *mb2, *mreq;
474 struct vattr va, dirattr, *vap = &va;
477 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
480 fhp = &nfh.fh_generic;
482 nfsm_srvnamesiz(len);
484 pubflag = nfs_ispublicfh(fhp);
486 nd.ni_cnd.cn_cred = cred;
487 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
488 nd.ni_cnd.cn_flags = CNP_LOCKLEAF | CNP_SAVESTART;
489 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
490 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
493 * namei failure, only dirp to cleanup. Clear out garbarge from
494 * structure in case macros jump to nfsmout.
500 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
504 nfsm_reply(NFSX_POSTOPATTR(v3));
505 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
511 * Locate index file for public filehandle
513 * error is 0 on entry and 0 on exit from this block.
517 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
519 * Setup call to lookup() to see if we can find
520 * the index file. Arguably, this doesn't belong
521 * in a kernel.. Ugh. If an error occurs, do not
522 * try to install an index file and then clear the
525 * When we replace nd with ind and redirect ndp,
526 * maintenance of ni_startdir and ni_vp shift to
527 * ind and we have to clean them up in the old nd.
528 * However, the cnd resource continues to be maintained
529 * via the original nd. Confused? You aren't alone!
532 VOP_UNLOCK(nd.ni_vp, 0, td);
533 ind.ni_pathlen = strlen(nfs_pub.np_index);
534 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
536 ind.ni_startdir = nd.ni_vp;
537 VREF(ind.ni_startdir);
539 error = lookup(&ind);
544 * Found an index file. Get rid of
545 * the old references. transfer nd.ni_vp'
551 vrele(nd.ni_startdir);
552 nd.ni_startdir = NULL;
558 * If the public filehandle was used, check that this lookup
559 * didn't result in a filehandle outside the publicly exported
560 * filesystem. We clear the poor vp here to avoid lockups due
564 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
573 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
579 * Resources at this point:
580 * ndp->ni_vp may not be NULL
585 nfsm_reply(NFSX_POSTOPATTR(v3));
586 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
591 nqsrv_getl(ndp->ni_startdir, ND_READ);
594 * Clear out some resources prior to potentially blocking. This
595 * is not as critical as ni_dvp resources in other routines, but
598 vrele(ndp->ni_startdir);
599 ndp->ni_startdir = NULL;
600 NDFREE(&nd, NDF_ONLY_PNBUF);
603 * Get underlying attribute, then release remaining resources ( for
604 * the same potential blocking reason ) and reply.
607 bzero((caddr_t)fhp, sizeof(nfh));
608 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
609 error = VFS_VPTOFH(vp, &fhp->fh_fid);
611 error = VOP_GETATTR(vp, vap, td);
615 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
617 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
621 nfsm_srvfhtom(fhp, v3);
623 nfsm_srvpostop_attr(0, vap);
624 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
626 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
627 nfsm_srvfillattr(vap, fp);
633 NDFREE(&nd, NDF_ONLY_PNBUF);
634 if (ndp->ni_startdir)
635 vrele(ndp->ni_startdir);
642 * nfs readlink service
645 nfsrv_readlink(nfsd, slp, td, mrq)
646 struct nfsrv_descript *nfsd;
647 struct nfssvc_sock *slp;
651 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
652 struct sockaddr *nam = nfsd->nd_nam;
653 caddr_t dpos = nfsd->nd_dpos;
654 struct ucred *cred = &nfsd->nd_cr;
655 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
656 struct iovec *ivp = iv;
661 int error = 0, rdonly, cache, i, tlen, len, getret;
662 int v3 = (nfsd->nd_flag & ND_NFSV3);
664 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
665 struct vnode *vp = NULL;
669 struct uio io, *uiop = &io;
672 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
674 mp2 = (struct mbuf *)0;
677 fhp = &nfh.fh_generic;
681 while (len < NFS_MAXPATHLEN) {
682 MGET(mp, M_WAIT, MT_DATA);
684 mp->m_len = NFSMSIZ(mp);
691 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
692 mp->m_len = NFS_MAXPATHLEN-len;
693 len = NFS_MAXPATHLEN;
696 ivp->iov_base = mtod(mp, caddr_t);
697 ivp->iov_len = mp->m_len;
702 uiop->uio_iovcnt = i;
703 uiop->uio_offset = 0;
704 uiop->uio_resid = len;
705 uiop->uio_rw = UIO_READ;
706 uiop->uio_segflg = UIO_SYSSPACE;
708 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
709 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
711 nfsm_reply(2 * NFSX_UNSIGNED);
712 nfsm_srvpostop_attr(1, (struct vattr *)0);
716 if (vp->v_type != VLNK) {
723 nqsrv_getl(vp, ND_READ);
724 error = VOP_READLINK(vp, uiop, cred);
726 getret = VOP_GETATTR(vp, &attr, td);
729 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
731 nfsm_srvpostop_attr(getret, &attr);
737 if (uiop->uio_resid > 0) {
738 len -= uiop->uio_resid;
739 tlen = nfsm_rndup(len);
740 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
742 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
743 *tl = txdr_unsigned(len);
758 nfsrv_read(nfsd, slp, td, mrq)
759 struct nfsrv_descript *nfsd;
760 struct nfssvc_sock *slp;
764 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
765 struct sockaddr *nam = nfsd->nd_nam;
766 caddr_t dpos = nfsd->nd_dpos;
767 struct ucred *cred = &nfsd->nd_cr;
771 struct nfs_fattr *fp;
776 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
777 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
779 struct mbuf *mb, *mb2, *mreq;
781 struct vnode *vp = NULL;
784 struct uio io, *uiop = &io;
785 struct vattr va, *vap = &va;
791 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
792 fhp = &nfh.fh_generic;
795 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
796 off = fxdr_hyper(tl);
798 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
799 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
801 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
804 * Reference vp. If an error occurs, vp will be invalid, but we
805 * have to NULL it just in case. The macros might goto nfsmout
809 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
810 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
813 nfsm_reply(2 * NFSX_UNSIGNED);
814 nfsm_srvpostop_attr(1, (struct vattr *)0);
819 if (vp->v_type != VREG) {
823 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
826 nqsrv_getl(vp, ND_READ);
827 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
828 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
830 getret = VOP_GETATTR(vp, vap, td);
836 nfsm_reply(NFSX_POSTOPATTR(v3));
837 nfsm_srvpostop_attr(getret, vap);
843 * Calculate byte count to read
846 if (off >= vap->va_size)
848 else if ((off + reqlen) > vap->va_size)
849 cnt = vap->va_size - off;
854 * Calculate seqcount for heuristic
862 * Locate best candidate
865 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
869 if (nfsheur[hi].nh_vp == vp) {
873 if (nfsheur[hi].nh_use > 0)
874 --nfsheur[hi].nh_use;
875 hi = (hi + 1) & (NUM_HEURISTIC - 1);
876 if (nfsheur[hi].nh_use < nh->nh_use)
880 if (nh->nh_vp != vp) {
883 nh->nh_use = NHUSE_INIT;
891 * Calculate heuristic
894 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
895 if (++nh->nh_seqcount > IO_SEQMAX)
896 nh->nh_seqcount = IO_SEQMAX;
897 } else if (nh->nh_seqcount > 1) {
902 nh->nh_use += NHUSE_INC;
903 if (nh->nh_use > NHUSE_MAX)
904 nh->nh_use = NHUSE_MAX;
905 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
908 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
910 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
912 fp = (struct nfs_fattr *)tl;
913 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
915 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
916 fp = (struct nfs_fattr *)tl;
917 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
919 len = left = nfsm_rndup(cnt);
922 * Generate the mbuf list with the uio_iov ref. to it.
927 siz = min(M_TRAILINGSPACE(m), left);
933 MGET(m, M_WAIT, MT_DATA);
940 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
942 uiop->uio_iov = iv2 = iv;
948 panic("nfsrv_read iov");
949 siz = min(M_TRAILINGSPACE(m), left);
951 iv->iov_base = mtod(m, caddr_t) + m->m_len;
960 uiop->uio_iovcnt = i;
961 uiop->uio_offset = off;
962 uiop->uio_resid = len;
963 uiop->uio_rw = UIO_READ;
964 uiop->uio_segflg = UIO_SYSSPACE;
965 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
966 off = uiop->uio_offset;
968 FREE((caddr_t)iv2, M_TEMP);
969 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
975 nfsm_reply(NFSX_POSTOPATTR(v3));
976 nfsm_srvpostop_attr(getret, vap);
985 nfsm_srvfillattr(vap, fp);
986 tlen = len - uiop->uio_resid;
987 cnt = cnt < tlen ? cnt : tlen;
988 tlen = nfsm_rndup(cnt);
989 if (len != tlen || tlen != cnt)
990 nfsm_adj(mb, len - tlen, tlen - cnt);
992 *tl++ = txdr_unsigned(cnt);
998 *tl = txdr_unsigned(cnt);
1009 nfsrv_write(nfsd, slp, td, mrq)
1010 struct nfsrv_descript *nfsd;
1011 struct nfssvc_sock *slp;
1015 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1016 struct sockaddr *nam = nfsd->nd_nam;
1017 caddr_t dpos = nfsd->nd_dpos;
1018 struct ucred *cred = &nfsd->nd_cr;
1022 struct nfs_fattr *fp;
1024 struct vattr va, forat;
1025 struct vattr *vap = &va;
1029 int error = 0, rdonly, cache, len, forat_ret = 1;
1030 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1031 int stable = NFSV3WRITE_FILESYNC;
1032 int v3 = (nfsd->nd_flag & ND_NFSV3);
1034 struct mbuf *mb, *mb2, *mreq;
1035 struct vnode *vp = NULL;
1038 struct uio io, *uiop = &io;
1042 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1048 fhp = &nfh.fh_generic;
1051 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1052 off = fxdr_hyper(tl);
1054 stable = fxdr_unsigned(int, *tl++);
1056 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1057 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1060 stable = NFSV3WRITE_UNSTABLE;
1062 retlen = len = fxdr_unsigned(int32_t, *tl);
1066 * For NFS Version 2, it is not obvious what a write of zero length
1067 * should do, but I might as well be consistent with Version 3,
1068 * which is to return ok so long as there are no permission problems.
1076 adjust = dpos - mtod(mp, caddr_t);
1077 mp->m_len -= adjust;
1078 if (mp->m_len > 0 && adjust > 0)
1079 NFSMADV(mp, adjust);
1083 else if (mp->m_len > 0) {
1086 mp->m_len -= (i - len);
1095 if (len > NFS_MAXDATA || len < 0 || i < len) {
1097 nfsm_reply(2 * NFSX_UNSIGNED);
1098 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1102 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1103 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1106 nfsm_reply(2 * NFSX_UNSIGNED);
1107 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1112 forat_ret = VOP_GETATTR(vp, &forat, td);
1113 if (vp->v_type != VREG) {
1117 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1120 nqsrv_getl(vp, ND_WRITE);
1121 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1126 nfsm_reply(NFSX_WCCDATA(v3));
1127 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1133 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1135 uiop->uio_iov = iv = ivp;
1136 uiop->uio_iovcnt = cnt;
1139 if (mp->m_len > 0) {
1140 ivp->iov_base = mtod(mp, caddr_t);
1141 ivp->iov_len = mp->m_len;
1149 * The IO_METASYNC flag indicates that all metadata (and not just
1150 * enough to ensure data integrity) mus be written to stable storage
1152 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1154 if (stable == NFSV3WRITE_UNSTABLE)
1155 ioflags = IO_NODELOCKED;
1156 else if (stable == NFSV3WRITE_DATASYNC)
1157 ioflags = (IO_SYNC | IO_NODELOCKED);
1159 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1160 uiop->uio_resid = len;
1161 uiop->uio_rw = UIO_WRITE;
1162 uiop->uio_segflg = UIO_SYSSPACE;
1163 uiop->uio_td = NULL;
1164 uiop->uio_offset = off;
1165 error = VOP_WRITE(vp, uiop, ioflags, cred);
1166 nfsstats.srvvop_writes++;
1167 FREE((caddr_t)iv, M_TEMP);
1169 aftat_ret = VOP_GETATTR(vp, vap, td);
1174 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1175 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1177 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1182 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1183 *tl++ = txdr_unsigned(retlen);
1185 * If nfs_async is set, then pretend the write was FILESYNC.
1187 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1188 *tl++ = txdr_unsigned(stable);
1190 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1192 * Actually, there is no need to txdr these fields,
1193 * but it may make the values more human readable,
1194 * for debugging purposes.
1196 if (nfsver.tv_sec == 0)
1198 *tl++ = txdr_unsigned(nfsver.tv_sec);
1199 *tl = txdr_unsigned(nfsver.tv_usec);
1201 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1202 nfsm_srvfillattr(vap, fp);
1211 * NFS write service with write gathering support. Called when
1212 * nfsrvw_procrastinate > 0.
1213 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1214 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1218 nfsrv_writegather(ndp, slp, td, mrq)
1219 struct nfsrv_descript **ndp;
1220 struct nfssvc_sock *slp;
1226 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1227 struct nfs_fattr *fp;
1230 struct nfsrvw_delayhash *wpp;
1232 struct vattr va, forat;
1236 int error = 0, rdonly, cache, len, forat_ret = 1;
1237 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1239 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1240 struct vnode *vp = NULL;
1241 struct uio io, *uiop = &io;
1242 u_quad_t frev, cur_usec;
1244 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1253 mrep = nfsd->nd_mrep;
1255 dpos = nfsd->nd_dpos;
1256 cred = &nfsd->nd_cr;
1257 v3 = (nfsd->nd_flag & ND_NFSV3);
1258 LIST_INIT(&nfsd->nd_coalesce);
1259 nfsd->nd_mreq = NULL;
1260 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1261 cur_usec = nfs_curusec();
1262 nfsd->nd_time = cur_usec +
1263 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1266 * Now, get the write header..
1268 nfsm_srvmtofh(&nfsd->nd_fh);
1270 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1271 nfsd->nd_off = fxdr_hyper(tl);
1273 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1275 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1276 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1279 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1281 len = fxdr_unsigned(int32_t, *tl);
1283 nfsd->nd_eoff = nfsd->nd_off + len;
1286 * Trim the header out of the mbuf list and trim off any trailing
1287 * junk so that the mbuf list has only the write data.
1295 adjust = dpos - mtod(mp, caddr_t);
1296 mp->m_len -= adjust;
1297 if (mp->m_len > 0 && adjust > 0)
1298 NFSMADV(mp, adjust);
1305 mp->m_len -= (i - len);
1311 if (len > NFS_MAXDATA || len < 0 || i < len) {
1315 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1317 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1318 nfsd->nd_mreq = mreq;
1319 nfsd->nd_mrep = NULL;
1324 * Add this entry to the hash and time queues.
1328 wp = slp->ns_tq.lh_first;
1329 while (wp && wp->nd_time < nfsd->nd_time) {
1331 wp = wp->nd_tq.le_next;
1333 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1335 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1337 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1339 if (nfsd->nd_mrep) {
1340 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1344 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1346 wp = wp->nd_hash.le_next;
1348 while (wp && wp->nd_off < nfsd->nd_off &&
1349 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1351 wp = wp->nd_hash.le_next;
1354 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1357 * Search the hash list for overlapping entries and
1360 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1361 wp = nfsd->nd_hash.le_next;
1362 if (NFSW_SAMECRED(owp, nfsd))
1363 nfsrvw_coalesce(owp, nfsd);
1366 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1373 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1374 * and generate the associated reply mbuf list(s).
1377 cur_usec = nfs_curusec();
1379 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1380 owp = nfsd->nd_tq.le_next;
1381 if (nfsd->nd_time > cur_usec)
1385 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1386 LIST_REMOVE(nfsd, nd_tq);
1387 LIST_REMOVE(nfsd, nd_hash);
1389 mrep = nfsd->nd_mrep;
1390 nfsd->nd_mrep = NULL;
1391 cred = &nfsd->nd_cr;
1392 v3 = (nfsd->nd_flag & ND_NFSV3);
1393 forat_ret = aftat_ret = 1;
1394 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1395 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1398 forat_ret = VOP_GETATTR(vp, &forat, td);
1399 if (vp->v_type != VREG) {
1403 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1409 nqsrv_getl(vp, ND_WRITE);
1410 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1413 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1414 ioflags = IO_NODELOCKED;
1415 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1416 ioflags = (IO_SYNC | IO_NODELOCKED);
1418 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1419 uiop->uio_rw = UIO_WRITE;
1420 uiop->uio_segflg = UIO_SYSSPACE;
1421 uiop->uio_td = NULL;
1422 uiop->uio_offset = nfsd->nd_off;
1423 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1424 if (uiop->uio_resid > 0) {
1432 uiop->uio_iovcnt = i;
1433 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1435 uiop->uio_iov = ivp = iov;
1438 if (mp->m_len > 0) {
1439 ivp->iov_base = mtod(mp, caddr_t);
1440 ivp->iov_len = mp->m_len;
1446 error = VOP_WRITE(vp, uiop, ioflags, cred);
1447 nfsstats.srvvop_writes++;
1449 FREE((caddr_t)iov, M_TEMP);
1453 aftat_ret = VOP_GETATTR(vp, &va, td);
1459 * Loop around generating replies for all write rpcs that have
1460 * now been completed.
1464 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1466 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1468 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1471 nfsm_writereply(NFSX_PREOPATTR(v3) +
1472 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1473 NFSX_WRITEVERF(v3), v3);
1475 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1476 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1477 *tl++ = txdr_unsigned(nfsd->nd_len);
1478 *tl++ = txdr_unsigned(swp->nd_stable);
1480 * Actually, there is no need to txdr these fields,
1481 * but it may make the values more human readable,
1482 * for debugging purposes.
1484 if (nfsver.tv_sec == 0)
1486 *tl++ = txdr_unsigned(nfsver.tv_sec);
1487 *tl = txdr_unsigned(nfsver.tv_usec);
1489 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1490 nfsm_srvfillattr(&va, fp);
1493 nfsd->nd_mreq = mreq;
1495 panic("nfsrv_write: nd_mrep not free");
1498 * Done. Put it at the head of the timer queue so that
1499 * the final phase can return the reply.
1504 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1506 nfsd = swp->nd_coalesce.lh_first;
1508 LIST_REMOVE(nfsd, nd_tq);
1514 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1521 * Search for a reply to return.
1524 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1525 if (nfsd->nd_mreq) {
1526 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1527 LIST_REMOVE(nfsd, nd_tq);
1528 *mrq = nfsd->nd_mreq;
1537 * Coalesce the write request nfsd into owp. To do this we must:
1538 * - remove nfsd from the queues
1539 * - merge nfsd->nd_mrep into owp->nd_mrep
1540 * - update the nd_eoff and nd_stable for owp
1541 * - put nfsd on owp's nd_coalesce list
1542 * NB: Must be called at splsoftclock().
1545 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1549 struct nfsrv_descript *p;
1551 NFS_DPF(WG, ("C%03x-%03x",
1552 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1553 LIST_REMOVE(nfsd, nd_hash);
1554 LIST_REMOVE(nfsd, nd_tq);
1555 if (owp->nd_eoff < nfsd->nd_eoff) {
1556 overlap = owp->nd_eoff - nfsd->nd_off;
1558 panic("nfsrv_coalesce: bad off");
1560 m_adj(nfsd->nd_mrep, overlap);
1564 mp->m_next = nfsd->nd_mrep;
1565 owp->nd_eoff = nfsd->nd_eoff;
1567 m_freem(nfsd->nd_mrep);
1568 nfsd->nd_mrep = NULL;
1569 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1570 owp->nd_stable = NFSV3WRITE_FILESYNC;
1571 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1572 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1573 owp->nd_stable = NFSV3WRITE_DATASYNC;
1574 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1577 * If nfsd had anything else coalesced into it, transfer them
1578 * to owp, otherwise their replies will never get sent.
1580 for (p = nfsd->nd_coalesce.lh_first; p;
1581 p = nfsd->nd_coalesce.lh_first) {
1582 LIST_REMOVE(p, nd_tq);
1583 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1588 * nfs create service
1589 * now does a truncate to 0 length via. setattr if it already exists
1592 nfsrv_create(nfsd, slp, td, mrq)
1593 struct nfsrv_descript *nfsd;
1594 struct nfssvc_sock *slp;
1598 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1599 struct sockaddr *nam = nfsd->nd_nam;
1600 caddr_t dpos = nfsd->nd_dpos;
1601 struct ucred *cred = &nfsd->nd_cr;
1602 struct nfs_fattr *fp;
1603 struct vattr va, dirfor, diraft;
1604 struct vattr *vap = &va;
1605 struct nfsv2_sattr *sp;
1607 struct nameidata nd;
1610 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1611 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1614 struct mbuf *mb, *mb2, *mreq;
1615 struct vnode *dirp = (struct vnode *)0;
1618 u_quad_t frev, tempsize;
1619 u_char cverf[NFSX_V3CREATEVERF];
1621 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1627 fhp = &nfh.fh_generic;
1629 nfsm_srvnamesiz(len);
1631 nd.ni_cnd.cn_cred = cred;
1632 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
1633 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_SAVESTART;
1636 * Call namei and do initial cleanup to get a few things
1637 * out of the way. If we get an initial error we cleanup
1638 * and return here to avoid special-casing the invalid nd
1639 * structure through the rest of the case. dirp may be
1640 * set even if an error occurs, but the nd structure will not
1641 * be valid at all if an error occurs so we have to invalidate it
1642 * prior to calling nfsm_reply ( which might goto nfsmout ).
1644 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1645 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1648 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1655 nfsm_reply(NFSX_WCCDATA(v3));
1656 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1662 * No error. Continue. State:
1664 * startdir is valid ( we release this immediately )
1666 * nd.ni_vp may be valid
1667 * nd.ni_dvp is valid
1669 * The error state is set through the code and we may also do some
1670 * opportunistic releasing of vnodes to avoid holding locks through
1671 * NFS I/O. The cleanup at the end is a catch-all
1676 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1677 how = fxdr_unsigned(int, *tl);
1679 case NFSV3CREATE_GUARDED:
1685 case NFSV3CREATE_UNCHECKED:
1688 case NFSV3CREATE_EXCLUSIVE:
1689 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1690 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1694 vap->va_type = VREG;
1696 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1697 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1698 if (vap->va_type == VNON)
1699 vap->va_type = VREG;
1700 vap->va_mode = nfstov_mode(sp->sa_mode);
1701 switch (vap->va_type) {
1703 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1705 vap->va_size = (u_quad_t)tsize;
1710 rdev = fxdr_unsigned(long, sp->sa_size);
1718 * Iff doesn't exist, create it
1719 * otherwise just truncate to 0 length
1720 * should I set the mode too ?
1722 * The only possible error we can have at this point is EEXIST.
1723 * nd.ni_vp will also be non-NULL in that case.
1725 if (nd.ni_vp == NULL) {
1726 if (vap->va_mode == (mode_t)VNOVAL)
1728 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1729 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1730 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1734 nfsrv_object_create(nd.ni_vp);
1735 if (exclusive_flag) {
1738 bcopy(cverf, (caddr_t)&vap->va_atime,
1740 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1745 vap->va_type == VCHR ||
1746 vap->va_type == VBLK ||
1747 vap->va_type == VFIFO
1750 * Handle SysV FIFO node special cases. All other
1751 * devices require super user to access.
1753 if (vap->va_type == VCHR && rdev == 0xffffffff)
1754 vap->va_type = VFIFO;
1755 if (vap->va_type != VFIFO &&
1756 (error = suser_cred(cred, 0))) {
1759 vap->va_rdev = rdev;
1760 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1762 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1764 NDFREE(&nd, NDF_ONLY_PNBUF);
1771 * release dvp prior to lookup
1779 * Even though LOCKPARENT was cleared, ni_dvp may
1782 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1783 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1784 nd.ni_cnd.cn_td = td;
1785 nd.ni_cnd.cn_cred = cred;
1787 error = lookup(&nd);
1792 /* fall through on certain errors */
1794 nfsrv_object_create(nd.ni_vp);
1795 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1803 if (vap->va_size != -1) {
1804 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1805 (nd.ni_cnd.cn_flags & CNP_RDONLY), td, 0);
1807 nqsrv_getl(nd.ni_vp, ND_WRITE);
1808 tempsize = vap->va_size;
1810 vap->va_size = tempsize;
1811 error = VOP_SETATTR(nd.ni_vp, vap, cred, td);
1817 bzero((caddr_t)fhp, sizeof(nfh));
1818 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1819 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1821 error = VOP_GETATTR(nd.ni_vp, vap, td);
1824 if (exclusive_flag && !error &&
1825 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1827 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1831 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1834 nfsm_srvpostop_fh(fhp);
1835 nfsm_srvpostop_attr(0, vap);
1837 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1840 nfsm_srvfhtom(fhp, v3);
1841 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1842 nfsm_srvfillattr(vap, fp);
1852 if (nd.ni_startdir) {
1853 vrele(nd.ni_startdir);
1854 nd.ni_startdir = NULL;
1858 NDFREE(&nd, NDF_ONLY_PNBUF);
1860 if (nd.ni_dvp == nd.ni_vp)
1871 * nfs v3 mknod service
1874 nfsrv_mknod(nfsd, slp, td, mrq)
1875 struct nfsrv_descript *nfsd;
1876 struct nfssvc_sock *slp;
1880 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1881 struct sockaddr *nam = nfsd->nd_nam;
1882 caddr_t dpos = nfsd->nd_dpos;
1883 struct ucred *cred = &nfsd->nd_cr;
1884 struct vattr va, dirfor, diraft;
1885 struct vattr *vap = &va;
1887 struct nameidata nd;
1890 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1891 u_int32_t major, minor;
1894 struct mbuf *mb, *mb2, *mreq;
1895 struct vnode *vp, *dirp = (struct vnode *)0;
1900 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1903 fhp = &nfh.fh_generic;
1905 nfsm_srvnamesiz(len);
1907 nd.ni_cnd.cn_cred = cred;
1908 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
1909 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_SAVESTART;
1912 * Handle nfs_namei() call. If an error occurs, the nd structure
1913 * is not valid. However, nfsm_*() routines may still jump to
1917 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1918 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1920 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1922 nfsm_reply(NFSX_WCCDATA(1));
1923 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1927 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1928 vtyp = nfsv3tov_type(*tl);
1929 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1930 error = NFSERR_BADTYPE;
1935 if (vtyp == VCHR || vtyp == VBLK) {
1936 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1937 major = fxdr_unsigned(u_int32_t, *tl++);
1938 minor = fxdr_unsigned(u_int32_t, *tl);
1939 vap->va_rdev = makeudev(major, minor);
1943 * Iff doesn't exist, create it.
1949 vap->va_type = vtyp;
1950 if (vap->va_mode == (mode_t)VNOVAL)
1952 if (vtyp == VSOCK) {
1953 vrele(nd.ni_startdir);
1954 nd.ni_startdir = NULL;
1955 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1956 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1958 NDFREE(&nd, NDF_ONLY_PNBUF);
1960 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1962 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1964 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1966 NDFREE(&nd, NDF_ONLY_PNBUF);
1973 * Release dvp prior to lookup
1978 KKASSERT(td->td_proc);
1979 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1980 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1981 nd.ni_cnd.cn_td = td;
1982 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1984 error = lookup(&nd);
1989 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1994 * send response, cleanup, return.
1997 if (nd.ni_startdir) {
1998 vrele(nd.ni_startdir);
1999 nd.ni_startdir = NULL;
2001 NDFREE(&nd, NDF_ONLY_PNBUF);
2003 if (nd.ni_dvp == nd.ni_vp)
2011 bzero((caddr_t)fhp, sizeof(nfh));
2012 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2013 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2015 error = VOP_GETATTR(vp, vap, td);
2022 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2027 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2029 nfsm_srvpostop_fh(fhp);
2030 nfsm_srvpostop_attr(0, vap);
2032 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2038 vrele(nd.ni_startdir);
2039 NDFREE(&nd, NDF_ONLY_PNBUF);
2041 if (nd.ni_dvp == nd.ni_vp)
2052 * nfs remove service
2055 nfsrv_remove(nfsd, slp, td, mrq)
2056 struct nfsrv_descript *nfsd;
2057 struct nfssvc_sock *slp;
2061 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2062 struct sockaddr *nam = nfsd->nd_nam;
2063 caddr_t dpos = nfsd->nd_dpos;
2064 struct ucred *cred = &nfsd->nd_cr;
2065 struct nameidata nd;
2069 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2070 int v3 = (nfsd->nd_flag & ND_NFSV3);
2072 struct mbuf *mb, *mreq;
2074 struct vattr dirfor, diraft;
2079 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2082 fhp = &nfh.fh_generic;
2084 nfsm_srvnamesiz(len);
2086 nd.ni_cnd.cn_cred = cred;
2087 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2088 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2089 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2090 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2093 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2100 if (nd.ni_vp->v_type == VDIR) {
2101 error = EPERM; /* POSIX */
2105 * The root of a mounted filesystem cannot be deleted.
2107 if (nd.ni_vp->v_flag & VROOT) {
2113 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2114 nqsrv_getl(nd.ni_vp, ND_WRITE);
2115 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2116 NDFREE(&nd, NDF_ONLY_PNBUF);
2120 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2124 nfsm_reply(NFSX_WCCDATA(v3));
2126 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2130 NDFREE(&nd, NDF_ONLY_PNBUF);
2132 if (nd.ni_dvp == nd.ni_vp)
2143 * nfs rename service
2146 nfsrv_rename(nfsd, slp, td, mrq)
2147 struct nfsrv_descript *nfsd;
2148 struct nfssvc_sock *slp;
2152 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2153 struct sockaddr *nam = nfsd->nd_nam;
2154 caddr_t dpos = nfsd->nd_dpos;
2155 struct ucred *cred = &nfsd->nd_cr;
2159 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2160 int tdirfor_ret = 1, tdiraft_ret = 1;
2161 int v3 = (nfsd->nd_flag & ND_NFSV3);
2163 struct mbuf *mb, *mreq;
2164 struct nameidata fromnd, tond;
2165 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2166 struct vnode *tdirp = (struct vnode *)0;
2167 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2169 fhandle_t *ffhp, *tfhp;
2173 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2175 fvp = (struct vnode *)0;
2177 ffhp = &fnfh.fh_generic;
2178 tfhp = &tnfh.fh_generic;
2181 * Clear fields incase goto nfsmout occurs from macro.
2187 nfsm_srvmtofh(ffhp);
2188 nfsm_srvnamesiz(len);
2190 * Remember our original uid so that we can reset cr_uid before
2191 * the second nfs_namei() call, in case it is remapped.
2193 saved_uid = cred->cr_uid;
2194 fromnd.ni_cnd.cn_cred = cred;
2195 fromnd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2196 fromnd.ni_cnd.cn_flags = CNP_WANTPARENT | CNP_SAVESTART;
2197 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2198 &dpos, &fdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2201 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2208 nfsm_reply(2 * NFSX_WCCDATA(v3));
2209 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2210 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2215 nfsm_srvmtofh(tfhp);
2216 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2217 cred->cr_uid = saved_uid;
2218 tond.ni_cnd.cn_cred = cred;
2219 tond.ni_cnd.cn_nameiop = NAMEI_RENAME;
2220 tond.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF |
2221 CNP_NOCACHE | CNP_SAVESTART;
2222 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2223 &dpos, &tdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2226 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2238 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2244 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2251 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2259 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2266 if (fvp->v_mount != tdvp->v_mount) {
2280 * If source is the same as the destination (that is the
2281 * same vnode with the same name in the same directory),
2282 * then there is nothing to do.
2284 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2285 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2286 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2287 fromnd.ni_cnd.cn_namelen))
2292 * The VOP_RENAME function releases all vnode references &
2293 * locks prior to returning so we need to clear the pointers
2294 * to bypass cleanup code later on.
2296 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2297 nqsrv_getl(tdvp, ND_WRITE);
2299 nqsrv_getl(tvp, ND_WRITE);
2301 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2302 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2303 fromnd.ni_dvp = NULL;
2304 fromnd.ni_vp = NULL;
2308 fromnd.ni_cnd.cn_flags &= ~CNP_HASBUF;
2309 tond.ni_cnd.cn_flags &= ~CNP_HASBUF;
2319 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2321 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2322 nfsm_reply(2 * NFSX_WCCDATA(v3));
2324 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2325 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2332 * Clear out tond related fields
2336 if (tond.ni_startdir)
2337 vrele(tond.ni_startdir);
2338 NDFREE(&tond, NDF_ONLY_PNBUF);
2340 if (tond.ni_dvp == tond.ni_vp)
2349 * Clear out fromnd related fields
2353 if (fromnd.ni_startdir)
2354 vrele(fromnd.ni_startdir);
2355 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2357 vrele(fromnd.ni_dvp);
2359 vrele(fromnd.ni_vp);
2368 nfsrv_link(nfsd, slp, td, mrq)
2369 struct nfsrv_descript *nfsd;
2370 struct nfssvc_sock *slp;
2374 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2375 struct sockaddr *nam = nfsd->nd_nam;
2376 caddr_t dpos = nfsd->nd_dpos;
2377 struct ucred *cred = &nfsd->nd_cr;
2378 struct nameidata nd;
2382 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2383 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2385 struct mbuf *mb, *mreq;
2386 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2387 struct vattr dirfor, diraft, at;
2389 fhandle_t *fhp, *dfhp;
2392 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2395 fhp = &nfh.fh_generic;
2396 dfhp = &dnfh.fh_generic;
2398 nfsm_srvmtofh(dfhp);
2399 nfsm_srvnamesiz(len);
2401 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2402 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2404 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2405 nfsm_srvpostop_attr(getret, &at);
2406 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2411 if (vp->v_type == VDIR) {
2412 error = EPERM; /* POSIX */
2415 nd.ni_cnd.cn_cred = cred;
2416 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2417 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2418 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2419 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2422 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2437 if (vp->v_mount != xp->v_mount)
2441 nqsrv_getl(vp, ND_WRITE);
2442 nqsrv_getl(xp, ND_WRITE);
2443 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2444 NDFREE(&nd, NDF_ONLY_PNBUF);
2450 getret = VOP_GETATTR(vp, &at, td);
2452 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2453 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2455 nfsm_srvpostop_attr(getret, &at);
2456 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2462 NDFREE(&nd, NDF_ONLY_PNBUF);
2468 if (nd.ni_dvp == nd.ni_vp)
2479 * nfs symbolic link service
2482 nfsrv_symlink(nfsd, slp, td, mrq)
2483 struct nfsrv_descript *nfsd;
2484 struct nfssvc_sock *slp;
2488 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2489 struct sockaddr *nam = nfsd->nd_nam;
2490 caddr_t dpos = nfsd->nd_dpos;
2491 struct ucred *cred = &nfsd->nd_cr;
2492 struct vattr va, dirfor, diraft;
2493 struct nameidata nd;
2494 struct vattr *vap = &va;
2497 struct nfsv2_sattr *sp;
2498 char *bpos, *pathcp = (char *)0, *cp2;
2501 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2502 int v3 = (nfsd->nd_flag & ND_NFSV3);
2503 struct mbuf *mb, *mreq, *mb2;
2504 struct vnode *dirp = (struct vnode *)0;
2509 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2512 fhp = &nfh.fh_generic;
2514 nfsm_srvnamesiz(len);
2515 nd.ni_cnd.cn_cred = cred;
2516 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2517 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_SAVESTART;
2518 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2519 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2522 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2534 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2535 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2536 iv.iov_base = pathcp;
2538 io.uio_resid = len2;
2542 io.uio_segflg = UIO_SYSSPACE;
2543 io.uio_rw = UIO_READ;
2545 nfsm_mtouio(&io, len2);
2547 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2548 vap->va_mode = nfstov_mode(sp->sa_mode);
2550 *(pathcp + len2) = '\0';
2557 * issue symlink op. SAVESTART is set so the underlying path component
2558 * is only freed by the VOP if an error occurs.
2560 if (vap->va_mode == (mode_t)VNOVAL)
2562 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2563 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2565 NDFREE(&nd, NDF_ONLY_PNBUF);
2570 * releases directory prior to potential lookup op.
2578 * Issue lookup. Leave SAVESTART set so we can easily free
2579 * the name buffer later on.
2581 * since LOCKPARENT is not set, ni_dvp will be garbage on
2582 * return whether an error occurs or not.
2584 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2585 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2586 nd.ni_cnd.cn_flags |= CNP_LOCKLEAF;
2587 nd.ni_cnd.cn_td = td;
2588 nd.ni_cnd.cn_cred = cred;
2590 error = lookup(&nd);
2594 bzero((caddr_t)fhp, sizeof(nfh));
2595 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2596 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2598 error = VOP_GETATTR(nd.ni_vp, vap, td);
2606 * These releases aren't strictly required, does even doing them
2607 * make any sense? XXX can nfsm_reply() block?
2610 FREE(pathcp, M_TEMP);
2614 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2618 if (nd.ni_startdir) {
2619 vrele(nd.ni_startdir);
2620 nd.ni_startdir = NULL;
2622 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2625 nfsm_srvpostop_fh(fhp);
2626 nfsm_srvpostop_attr(0, vap);
2628 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2634 NDFREE(&nd, NDF_ONLY_PNBUF);
2636 if (nd.ni_dvp == nd.ni_vp)
2644 vrele(nd.ni_startdir);
2648 FREE(pathcp, M_TEMP);
2657 nfsrv_mkdir(nfsd, slp, td, mrq)
2658 struct nfsrv_descript *nfsd;
2659 struct nfssvc_sock *slp;
2663 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2664 struct sockaddr *nam = nfsd->nd_nam;
2665 caddr_t dpos = nfsd->nd_dpos;
2666 struct ucred *cred = &nfsd->nd_cr;
2667 struct vattr va, dirfor, diraft;
2668 struct vattr *vap = &va;
2669 struct nfs_fattr *fp;
2670 struct nameidata nd;
2675 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2676 int v3 = (nfsd->nd_flag & ND_NFSV3);
2678 struct mbuf *mb, *mb2, *mreq;
2679 struct vnode *dirp = NULL;
2685 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2688 fhp = &nfh.fh_generic;
2690 nfsm_srvnamesiz(len);
2691 nd.ni_cnd.cn_cred = cred;
2692 nd.ni_cnd.cn_nameiop = NAMEI_CREATE;
2693 nd.ni_cnd.cn_flags = CNP_LOCKPARENT;
2695 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2696 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2699 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2706 nfsm_reply(NFSX_WCCDATA(v3));
2707 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2715 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2716 vap->va_mode = nfstov_mode(*tl++);
2720 * At this point nd.ni_dvp is referenced and exclusively locked and
2721 * nd.ni_vp, if it exists, is referenced but not locked.
2724 vap->va_type = VDIR;
2725 if (nd.ni_vp != NULL) {
2726 NDFREE(&nd, NDF_ONLY_PNBUF);
2732 * Issue mkdir op. Since SAVESTART is not set, the pathname
2733 * component is freed by the VOP call. This will fill-in
2734 * nd.ni_vp, reference, and exclusively lock it.
2736 if (vap->va_mode == (mode_t)VNOVAL)
2738 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2739 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2740 NDFREE(&nd, NDF_ONLY_PNBUF);
2747 bzero((caddr_t)fhp, sizeof(nfh));
2748 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2749 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2751 error = VOP_GETATTR(nd.ni_vp, vap, td);
2755 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2756 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2759 nfsm_srvpostop_fh(fhp);
2760 nfsm_srvpostop_attr(0, vap);
2762 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2764 nfsm_srvfhtom(fhp, v3);
2765 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2766 nfsm_srvfillattr(vap, fp);
2775 NDFREE(&nd, NDF_ONLY_PNBUF);
2776 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2794 nfsrv_rmdir(nfsd, slp, td, mrq)
2795 struct nfsrv_descript *nfsd;
2796 struct nfssvc_sock *slp;
2800 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2801 struct sockaddr *nam = nfsd->nd_nam;
2802 caddr_t dpos = nfsd->nd_dpos;
2803 struct ucred *cred = &nfsd->nd_cr;
2807 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2808 int v3 = (nfsd->nd_flag & ND_NFSV3);
2810 struct mbuf *mb, *mreq;
2811 struct vnode *vp, *dirp = (struct vnode *)0;
2812 struct vattr dirfor, diraft;
2815 struct nameidata nd;
2818 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2821 fhp = &nfh.fh_generic;
2823 nfsm_srvnamesiz(len);
2824 nd.ni_cnd.cn_cred = cred;
2825 nd.ni_cnd.cn_nameiop = NAMEI_DELETE;
2826 nd.ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
2827 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2828 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2831 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2838 nfsm_reply(NFSX_WCCDATA(v3));
2839 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2844 if (vp->v_type != VDIR) {
2849 * No rmdir "." please.
2851 if (nd.ni_dvp == vp) {
2856 * The root of a mounted filesystem cannot be deleted.
2858 if (vp->v_flag & VROOT)
2862 * Issue or abort op. Since SAVESTART is not set, path name
2863 * component is freed by the VOP after either.
2866 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2867 nqsrv_getl(vp, ND_WRITE);
2868 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2870 NDFREE(&nd, NDF_ONLY_PNBUF);
2873 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2874 nfsm_reply(NFSX_WCCDATA(v3));
2876 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2882 NDFREE(&nd, NDF_ONLY_PNBUF);
2886 if (nd.ni_dvp == nd.ni_vp)
2898 * nfs readdir service
2899 * - mallocs what it thinks is enough to read
2900 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2901 * - calls VOP_READDIR()
2902 * - loops around building the reply
2903 * if the output generated exceeds count break out of loop
2904 * The nfsm_clget macro is used here so that the reply will be packed
2905 * tightly in mbuf clusters.
2906 * - it only knows that it has encountered eof when the VOP_READDIR()
2908 * - as such one readdir rpc will return eof false although you are there
2909 * and then the next will return eof
2910 * - it trims out records with d_fileno == 0
2911 * this doesn't matter for Unix clients, but they might confuse clients
2913 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2914 * than requested, but this may not apply to all filesystems. For
2915 * example, client NFS does not { although it is never remote mounted
2917 * The alternate call nfsrv_readdirplus() does lookups as well.
2918 * PS: The NFS protocol spec. does not clarify what the "count" byte
2919 * argument is a count of.. just name strings and file id's or the
2920 * entire reply rpc or ...
2921 * I tried just file name and id sizes and it confused the Sun client,
2922 * so I am using the full rpc size now. The "paranoia.." comment refers
2923 * to including the status longwords that are not a part of the dir.
2924 * "entry" structures, but are in the rpc.
2928 u_int32_t fl_postopok;
2929 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2931 u_int32_t fl_fhsize;
2932 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2936 nfsrv_readdir(nfsd, slp, td, mrq)
2937 struct nfsrv_descript *nfsd;
2938 struct nfssvc_sock *slp;
2942 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2943 struct sockaddr *nam = nfsd->nd_nam;
2944 caddr_t dpos = nfsd->nd_dpos;
2945 struct ucred *cred = &nfsd->nd_cr;
2953 struct mbuf *mb, *mb2, *mreq, *mp2;
2954 char *cpos, *cend, *cp2, *rbuf;
2955 struct vnode *vp = NULL;
2961 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2962 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2963 int v3 = (nfsd->nd_flag & ND_NFSV3);
2964 u_quad_t frev, off, toff, verf;
2965 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2967 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2968 fhp = &nfh.fh_generic;
2971 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2972 toff = fxdr_hyper(tl);
2974 verf = fxdr_hyper(tl);
2977 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2978 toff = fxdr_unsigned(u_quad_t, *tl++);
2979 verf = 0; /* shut up gcc */
2982 cnt = fxdr_unsigned(int, *tl);
2983 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2984 xfer = NFS_SRVMAXDATA(nfsd);
2990 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2991 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2992 if (!error && vp->v_type != VDIR) {
2998 nfsm_reply(NFSX_UNSIGNED);
2999 nfsm_srvpostop_attr(getret, &at);
3005 * Obtain lock on vnode for this section of the code
3008 nqsrv_getl(vp, ND_READ);
3010 error = getret = VOP_GETATTR(vp, &at, td);
3013 * XXX This check may be too strict for Solaris 2.5 clients.
3015 if (!error && toff && verf && verf != at.va_filerev)
3016 error = NFSERR_BAD_COOKIE;
3020 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3024 nfsm_reply(NFSX_POSTOPATTR(v3));
3025 nfsm_srvpostop_attr(getret, &at);
3029 VOP_UNLOCK(vp, 0, td);
3032 * end section. Allocate rbuf and continue
3034 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3037 iv.iov_len = fullsiz;
3040 io.uio_offset = (off_t)off;
3041 io.uio_resid = fullsiz;
3042 io.uio_segflg = UIO_SYSSPACE;
3043 io.uio_rw = UIO_READ;
3046 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3048 free((caddr_t)cookies, M_TEMP);
3051 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3052 off = (off_t)io.uio_offset;
3053 if (!cookies && !error)
3054 error = NFSERR_PERM;
3056 getret = VOP_GETATTR(vp, &at, td);
3060 VOP_UNLOCK(vp, 0, td);
3064 free((caddr_t)rbuf, M_TEMP);
3066 free((caddr_t)cookies, M_TEMP);
3067 nfsm_reply(NFSX_POSTOPATTR(v3));
3068 nfsm_srvpostop_attr(getret, &at);
3073 siz -= io.uio_resid;
3076 * If nothing read, return eof
3082 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3085 nfsm_srvpostop_attr(getret, &at);
3086 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3087 txdr_hyper(at.va_filerev, tl);
3090 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3093 FREE((caddr_t)rbuf, M_TEMP);
3094 FREE((caddr_t)cookies, M_TEMP);
3101 * Check for degenerate cases of nothing useful read.
3102 * If so go try again
3106 dp = (struct dirent *)cpos;
3109 * For some reason FreeBSD's ufs_readdir() chooses to back the
3110 * directory offset up to a block boundary, so it is necessary to
3111 * skip over the records that preceed the requested offset. This
3112 * requires the assumption that file offset cookies monotonically
3115 while (cpos < cend && ncookies > 0 &&
3116 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3117 ((u_quad_t)(*cookiep)) <= toff)) {
3118 cpos += dp->d_reclen;
3119 dp = (struct dirent *)cpos;
3123 if (cpos >= cend || ncookies == 0) {
3129 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3130 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3132 nfsm_srvpostop_attr(getret, &at);
3133 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3134 txdr_hyper(at.va_filerev, tl);
3138 be = bp + M_TRAILINGSPACE(mp);
3140 /* Loop through the records and build reply */
3141 while (cpos < cend && ncookies > 0) {
3142 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3143 nlen = dp->d_namlen;
3144 rem = nfsm_rndup(nlen) - nlen;
3145 len += (4 * NFSX_UNSIGNED + nlen + rem);
3147 len += 2 * NFSX_UNSIGNED;
3153 * Build the directory record xdr from
3158 bp += NFSX_UNSIGNED;
3162 bp += NFSX_UNSIGNED;
3165 *tl = txdr_unsigned(dp->d_fileno);
3166 bp += NFSX_UNSIGNED;
3168 *tl = txdr_unsigned(nlen);
3169 bp += NFSX_UNSIGNED;
3171 /* And loop around copying the name */
3180 bcopy(cp, bp, tsiz);
3186 /* And null pad to a int32_t boundary */
3187 for (i = 0; i < rem; i++)
3191 /* Finish off the record */
3194 bp += NFSX_UNSIGNED;
3197 *tl = txdr_unsigned(*cookiep);
3198 bp += NFSX_UNSIGNED;
3200 cpos += dp->d_reclen;
3201 dp = (struct dirent *)cpos;
3209 bp += NFSX_UNSIGNED;
3215 bp += NFSX_UNSIGNED;
3218 mp->m_len = bp - mtod(mp, caddr_t);
3220 mp->m_len += bp - bpos;
3221 FREE((caddr_t)rbuf, M_TEMP);
3222 FREE((caddr_t)cookies, M_TEMP);
3231 nfsrv_readdirplus(nfsd, slp, td, mrq)
3232 struct nfsrv_descript *nfsd;
3233 struct nfssvc_sock *slp;
3237 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3238 struct sockaddr *nam = nfsd->nd_nam;
3239 caddr_t dpos = nfsd->nd_dpos;
3240 struct ucred *cred = &nfsd->nd_cr;
3248 struct mbuf *mb, *mb2, *mreq, *mp2;
3249 char *cpos, *cend, *cp2, *rbuf;
3250 struct vnode *vp = NULL, *nvp;
3253 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3256 struct vattr va, at, *vap = &va;
3257 struct nfs_fattr *fp;
3258 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3259 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3260 u_quad_t frev, off, toff, verf;
3261 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3263 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3264 fhp = &nfh.fh_generic;
3266 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3267 toff = fxdr_hyper(tl);
3269 verf = fxdr_hyper(tl);
3271 siz = fxdr_unsigned(int, *tl++);
3272 cnt = fxdr_unsigned(int, *tl);
3274 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3275 xfer = NFS_SRVMAXDATA(nfsd);
3281 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3282 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3283 if (!error && vp->v_type != VDIR) {
3289 nfsm_reply(NFSX_UNSIGNED);
3290 nfsm_srvpostop_attr(getret, &at);
3294 error = getret = VOP_GETATTR(vp, &at, td);
3297 * XXX This check may be too strict for Solaris 2.5 clients.
3299 if (!error && toff && verf && verf != at.va_filerev)
3300 error = NFSERR_BAD_COOKIE;
3303 nqsrv_getl(vp, ND_READ);
3304 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3309 nfsm_reply(NFSX_V3POSTOPATTR);
3310 nfsm_srvpostop_attr(getret, &at);
3314 VOP_UNLOCK(vp, 0, td);
3315 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3318 iv.iov_len = fullsiz;
3321 io.uio_offset = (off_t)off;
3322 io.uio_resid = fullsiz;
3323 io.uio_segflg = UIO_SYSSPACE;
3324 io.uio_rw = UIO_READ;
3327 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3329 free((caddr_t)cookies, M_TEMP);
3332 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3333 off = (u_quad_t)io.uio_offset;
3334 getret = VOP_GETATTR(vp, &at, td);
3335 VOP_UNLOCK(vp, 0, td);
3336 if (!cookies && !error)
3337 error = NFSERR_PERM;
3344 free((caddr_t)cookies, M_TEMP);
3345 free((caddr_t)rbuf, M_TEMP);
3346 nfsm_reply(NFSX_V3POSTOPATTR);
3347 nfsm_srvpostop_attr(getret, &at);
3352 siz -= io.uio_resid;
3355 * If nothing read, return eof
3361 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3363 nfsm_srvpostop_attr(getret, &at);
3364 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3365 txdr_hyper(at.va_filerev, tl);
3369 FREE((caddr_t)cookies, M_TEMP);
3370 FREE((caddr_t)rbuf, M_TEMP);
3377 * Check for degenerate cases of nothing useful read.
3378 * If so go try again
3382 dp = (struct dirent *)cpos;
3385 * For some reason FreeBSD's ufs_readdir() chooses to back the
3386 * directory offset up to a block boundary, so it is necessary to
3387 * skip over the records that preceed the requested offset. This
3388 * requires the assumption that file offset cookies monotonically
3391 while (cpos < cend && ncookies > 0 &&
3392 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3393 ((u_quad_t)(*cookiep)) <= toff)) {
3394 cpos += dp->d_reclen;
3395 dp = (struct dirent *)cpos;
3399 if (cpos >= cend || ncookies == 0) {
3406 * Probe one of the directory entries to see if the filesystem
3409 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3410 error = NFSERR_NOTSUPP;
3413 free((caddr_t)cookies, M_TEMP);
3414 free((caddr_t)rbuf, M_TEMP);
3415 nfsm_reply(NFSX_V3POSTOPATTR);
3416 nfsm_srvpostop_attr(getret, &at);
3423 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3425 nfsm_srvpostop_attr(getret, &at);
3426 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3427 txdr_hyper(at.va_filerev, tl);
3430 be = bp + M_TRAILINGSPACE(mp);
3432 /* Loop through the records and build reply */
3433 while (cpos < cend && ncookies > 0) {
3434 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3435 nlen = dp->d_namlen;
3436 rem = nfsm_rndup(nlen)-nlen;
3439 * For readdir_and_lookup get the vnode using
3442 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3444 bzero((caddr_t)nfhp, NFSX_V3FH);
3446 nvp->v_mount->mnt_stat.f_fsid;
3447 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3452 if (VOP_GETATTR(nvp, vap, td)) {
3461 * If either the dircount or maxcount will be
3462 * exceeded, get out now. Both of these lengths
3463 * are calculated conservatively, including all
3466 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3468 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3469 if (len > cnt || dirlen > fullsiz) {
3475 * Build the directory record xdr from
3478 fp = (struct nfs_fattr *)&fl.fl_fattr;
3479 nfsm_srvfillattr(vap, fp);
3480 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3481 fl.fl_fhok = nfs_true;
3482 fl.fl_postopok = nfs_true;
3483 fl.fl_off.nfsuquad[0] = 0;
3484 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3488 bp += NFSX_UNSIGNED;
3491 bp += NFSX_UNSIGNED;
3493 *tl = txdr_unsigned(dp->d_fileno);
3494 bp += NFSX_UNSIGNED;
3496 *tl = txdr_unsigned(nlen);
3497 bp += NFSX_UNSIGNED;
3499 /* And loop around copying the name */
3504 if ((bp + xfer) > be)
3508 bcopy(cp, bp, tsiz);
3514 /* And null pad to a int32_t boundary */
3515 for (i = 0; i < rem; i++)
3519 * Now copy the flrep structure out.
3521 xfer = sizeof (struct flrep);
3525 if ((bp + xfer) > be)
3529 bcopy(cp, bp, tsiz);
3537 cpos += dp->d_reclen;
3538 dp = (struct dirent *)cpos;
3546 bp += NFSX_UNSIGNED;
3552 bp += NFSX_UNSIGNED;
3555 mp->m_len = bp - mtod(mp, caddr_t);
3557 mp->m_len += bp - bpos;
3558 FREE((caddr_t)cookies, M_TEMP);
3559 FREE((caddr_t)rbuf, M_TEMP);
3567 * nfs commit service
3570 nfsrv_commit(nfsd, slp, td, mrq)
3571 struct nfsrv_descript *nfsd;
3572 struct nfssvc_sock *slp;
3576 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3577 struct sockaddr *nam = nfsd->nd_nam;
3578 caddr_t dpos = nfsd->nd_dpos;
3579 struct ucred *cred = &nfsd->nd_cr;
3580 struct vattr bfor, aft;
3581 struct vnode *vp = NULL;
3587 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3589 struct mbuf *mb, *mb2, *mreq;
3592 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3596 fhp = &nfh.fh_generic;
3598 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3601 * XXX At this time VOP_FSYNC() does not accept offset and byte
3602 * count parameters, so these arguments are useless (someday maybe).
3604 off = fxdr_hyper(tl);
3606 cnt = fxdr_unsigned(int, *tl);
3607 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3608 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3610 nfsm_reply(2 * NFSX_UNSIGNED);
3611 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3615 for_ret = VOP_GETATTR(vp, &bfor, td);
3617 if (cnt > MAX_COMMIT_COUNT) {
3619 * Give up and do the whole thing
3622 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3623 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3625 error = VOP_FSYNC(vp, MNT_WAIT, td);
3628 * Locate and synchronously write any buffers that fall
3629 * into the requested range. Note: we are assuming that
3630 * f_iosize is a power of 2.
3632 int iosize = vp->v_mount->mnt_stat.f_iosize;
3633 int iomask = iosize - 1;
3638 * Align to iosize boundry, super-align to page boundry.
3641 cnt += off & iomask;
3642 off &= ~(u_quad_t)iomask;
3644 if (off & PAGE_MASK) {
3645 cnt += off & PAGE_MASK;
3646 off &= ~(u_quad_t)PAGE_MASK;
3648 lblkno = off / iosize;
3651 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3652 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3660 * If we have a buffer and it is marked B_DELWRI we
3661 * have to lock and write it. Otherwise the prior
3662 * write is assumed to have already been committed.
3664 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3665 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3666 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3667 continue; /* retry */
3670 bp->b_flags &= ~B_ASYNC;
3671 VOP_BWRITE(bp->b_vp, bp);
3683 aft_ret = VOP_GETATTR(vp, &aft, td);
3686 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3687 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3689 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3690 if (nfsver.tv_sec == 0)
3692 *tl++ = txdr_unsigned(nfsver.tv_sec);
3693 *tl = txdr_unsigned(nfsver.tv_usec);
3704 * nfs statfs service
3707 nfsrv_statfs(nfsd, slp, td, mrq)
3708 struct nfsrv_descript *nfsd;
3709 struct nfssvc_sock *slp;
3713 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3714 struct sockaddr *nam = nfsd->nd_nam;
3715 caddr_t dpos = nfsd->nd_dpos;
3716 struct ucred *cred = &nfsd->nd_cr;
3718 struct nfs_statfs *sfp;
3722 int error = 0, rdonly, cache, getret = 1;
3723 int v3 = (nfsd->nd_flag & ND_NFSV3);
3725 struct mbuf *mb, *mb2, *mreq;
3726 struct vnode *vp = NULL;
3730 struct statfs statfs;
3731 u_quad_t frev, tval;
3733 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3737 fhp = &nfh.fh_generic;
3739 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3740 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3742 nfsm_reply(NFSX_UNSIGNED);
3743 nfsm_srvpostop_attr(getret, &at);
3748 error = VFS_STATFS(vp->v_mount, sf, td);
3749 getret = VOP_GETATTR(vp, &at, td);
3752 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3754 nfsm_srvpostop_attr(getret, &at);
3759 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3761 tval = (u_quad_t)sf->f_blocks;
3762 tval *= (u_quad_t)sf->f_bsize;
3763 txdr_hyper(tval, &sfp->sf_tbytes);
3764 tval = (u_quad_t)sf->f_bfree;
3765 tval *= (u_quad_t)sf->f_bsize;
3766 txdr_hyper(tval, &sfp->sf_fbytes);
3767 tval = (u_quad_t)sf->f_bavail;
3768 tval *= (u_quad_t)sf->f_bsize;
3769 txdr_hyper(tval, &sfp->sf_abytes);
3770 sfp->sf_tfiles.nfsuquad[0] = 0;
3771 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3772 sfp->sf_ffiles.nfsuquad[0] = 0;
3773 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3774 sfp->sf_afiles.nfsuquad[0] = 0;
3775 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3776 sfp->sf_invarsec = 0;
3778 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3779 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3780 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3781 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3782 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3791 * nfs fsinfo service
3794 nfsrv_fsinfo(nfsd, slp, td, mrq)
3795 struct nfsrv_descript *nfsd;
3796 struct nfssvc_sock *slp;
3800 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3801 struct sockaddr *nam = nfsd->nd_nam;
3802 caddr_t dpos = nfsd->nd_dpos;
3803 struct ucred *cred = &nfsd->nd_cr;
3805 struct nfsv3_fsinfo *sip;
3808 int error = 0, rdonly, cache, getret = 1, pref;
3810 struct mbuf *mb, *mb2, *mreq;
3811 struct vnode *vp = NULL;
3815 u_quad_t frev, maxfsize;
3818 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3822 fhp = &nfh.fh_generic;
3824 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3825 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3827 nfsm_reply(NFSX_UNSIGNED);
3828 nfsm_srvpostop_attr(getret, &at);
3833 /* XXX Try to make a guess on the max file size. */
3834 VFS_STATFS(vp->v_mount, &sb, td);
3835 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3837 getret = VOP_GETATTR(vp, &at, td);
3840 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3841 nfsm_srvpostop_attr(getret, &at);
3842 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3846 * There should be file system VFS OP(s) to get this information.
3847 * For now, assume ufs.
3849 if (slp->ns_so->so_type == SOCK_DGRAM)
3850 pref = NFS_MAXDGRAMDATA;
3853 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3854 sip->fs_rtpref = txdr_unsigned(pref);
3855 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3856 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3857 sip->fs_wtpref = txdr_unsigned(pref);
3858 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3859 sip->fs_dtpref = txdr_unsigned(pref);
3860 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3861 sip->fs_timedelta.nfsv3_sec = 0;
3862 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3863 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3864 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3865 NFSV3FSINFO_CANSETTIME);
3873 * nfs pathconf service
3876 nfsrv_pathconf(nfsd, slp, td, mrq)
3877 struct nfsrv_descript *nfsd;
3878 struct nfssvc_sock *slp;
3882 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3883 struct sockaddr *nam = nfsd->nd_nam;
3884 caddr_t dpos = nfsd->nd_dpos;
3885 struct ucred *cred = &nfsd->nd_cr;
3887 struct nfsv3_pathconf *pc;
3890 int error = 0, rdonly, cache, getret = 1;
3891 register_t linkmax, namemax, chownres, notrunc;
3893 struct mbuf *mb, *mb2, *mreq;
3894 struct vnode *vp = NULL;
3900 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3904 fhp = &nfh.fh_generic;
3906 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3907 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3909 nfsm_reply(NFSX_UNSIGNED);
3910 nfsm_srvpostop_attr(getret, &at);
3914 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3916 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3918 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3920 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3921 getret = VOP_GETATTR(vp, &at, td);
3924 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3925 nfsm_srvpostop_attr(getret, &at);
3930 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3932 pc->pc_linkmax = txdr_unsigned(linkmax);
3933 pc->pc_namemax = txdr_unsigned(namemax);
3934 pc->pc_notrunc = txdr_unsigned(notrunc);
3935 pc->pc_chownrestricted = txdr_unsigned(chownres);
3938 * These should probably be supported by VOP_PATHCONF(), but
3939 * until msdosfs is exportable (why would you want to?), the
3940 * Unix defaults should be ok.
3942 pc->pc_caseinsensitive = nfs_false;
3943 pc->pc_casepreserving = nfs_true;
3951 * Null operation, used by clients to ping server
3955 nfsrv_null(nfsd, slp, td, mrq)
3956 struct nfsrv_descript *nfsd;
3957 struct nfssvc_sock *slp;
3961 struct mbuf *mrep = nfsd->nd_mrep;
3963 int error = NFSERR_RETVOID, cache;
3964 struct mbuf *mb, *mreq;
3967 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3976 * No operation, used for obsolete procedures
3980 nfsrv_noop(nfsd, slp, td, mrq)
3981 struct nfsrv_descript *nfsd;
3982 struct nfssvc_sock *slp;
3986 struct mbuf *mrep = nfsd->nd_mrep;
3989 struct mbuf *mb, *mreq;
3992 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3996 if (nfsd->nd_repstat)
3997 error = nfsd->nd_repstat;
3999 error = EPROCUNAVAIL;
4006 * Perform access checking for vnodes obtained from file handles that would
4007 * refer to files already opened by a Unix client. You cannot just use
4008 * vn_writechk() and VOP_ACCESS() for two reasons.
4009 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4010 * 2 - The owner is to be given access irrespective of mode bits for some
4011 * operations, so that processes that chmod after opening a file don't
4012 * break. I don't like this because it opens a security hole, but since
4013 * the nfs server opens a security hole the size of a barn door anyhow,
4016 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4017 * will return EPERM instead of EACCESS. EPERM is always an error.
4020 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
4021 int rdonly, struct thread *td, int override)
4026 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4027 if (flags & VWRITE) {
4028 /* Just vn_writechk() changed to check rdonly */
4030 * Disallow write attempts on read-only file systems;
4031 * unless the file is a socket or a block or character
4032 * device resident on the file system.
4034 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4035 switch (vp->v_type) {
4045 * If there's shared text associated with
4046 * the inode, we can't allow writing.
4048 if (vp->v_flag & VTEXT)
4051 error = VOP_GETATTR(vp, &vattr, td);
4054 error = VOP_ACCESS(vp, flags, cred, td);
4056 * Allow certain operations for the owner (reads and writes
4057 * on files that are already open).
4059 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4063 #endif /* NFS_NOSERVER */