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.28 2006/03/05 18:38:37 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
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/nlookup.h>
70 #include <sys/namei.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/malloc.h>
78 #include <sys/dirent.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
85 #include <vm/vm_extern.h>
86 #include <vm/vm_zone.h>
87 #include <vm/vm_object.h>
91 #include <sys/thread2.h>
97 #include "nfsm_subs.h"
101 #define nfsdbprintf(info) printf info
103 #define nfsdbprintf(info)
106 #define MAX_COMMIT_COUNT (1024 * 1024)
108 #define NUM_HEURISTIC 1017
109 #define NHUSE_INIT 64
111 #define NHUSE_MAX 2048
113 static struct nfsheur {
114 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
115 off_t nh_nextr; /* next offset for sequential detection */
116 int nh_use; /* use count for selection */
117 int nh_seqcount; /* heuristic */
118 } nfsheur[NUM_HEURISTIC];
120 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
123 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
126 extern u_int32_t nfs_xdrneg1;
127 extern u_int32_t nfs_false, nfs_true;
128 extern enum vtype nv3tov_type[8];
129 extern struct nfsstats nfsstats;
131 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
132 int nfsrvw_procrastinate_v3 = 0;
134 static struct timespec nfsver;
136 SYSCTL_DECL(_vfs_nfs);
138 static int nfs_async;
139 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
140 static int nfs_commit_blks;
141 static int nfs_commit_miss;
142 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
143 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
145 static int nfsrv_access (struct vnode *,int,struct ucred *,int,
146 struct thread *, int);
147 static void nfsrvw_coalesce (struct nfsrv_descript *,
148 struct nfsrv_descript *);
151 * nfs v3 access service
154 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
155 struct thread *td, struct mbuf **mrq)
157 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
158 struct sockaddr *nam = nfsd->nd_nam;
159 caddr_t dpos = nfsd->nd_dpos;
160 struct ucred *cred = &nfsd->nd_cr;
161 struct vnode *vp = NULL;
167 int error = 0, rdonly, cache, getret;
169 struct mbuf *mb, *mreq, *mb2;
170 struct vattr vattr, *vap = &vattr;
171 u_long testmode, nfsmode;
174 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
178 fhp = &nfh.fh_generic;
180 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
181 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
182 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
184 nfsm_reply(NFSX_UNSIGNED);
185 nfsm_srvpostop_attr(1, (struct vattr *)0);
189 nfsmode = fxdr_unsigned(u_int32_t, *tl);
190 if ((nfsmode & NFSV3ACCESS_READ) &&
191 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
192 nfsmode &= ~NFSV3ACCESS_READ;
193 if (vp->v_type == VDIR)
194 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
197 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
198 if ((nfsmode & testmode) &&
199 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
200 nfsmode &= ~testmode;
201 if (vp->v_type == VDIR)
202 testmode = NFSV3ACCESS_LOOKUP;
204 testmode = NFSV3ACCESS_EXECUTE;
205 if ((nfsmode & testmode) &&
206 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
207 nfsmode &= ~testmode;
208 getret = VOP_GETATTR(vp, vap, td);
211 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
212 nfsm_srvpostop_attr(getret, vap);
213 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
214 *tl = txdr_unsigned(nfsmode);
222 * nfs getattr service
225 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
226 struct thread *td, struct mbuf **mrq)
228 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
229 struct sockaddr *nam = nfsd->nd_nam;
230 caddr_t dpos = nfsd->nd_dpos;
231 struct ucred *cred = &nfsd->nd_cr;
232 struct nfs_fattr *fp;
234 struct vattr *vap = &va;
235 struct vnode *vp = NULL;
241 int error = 0, rdonly, cache;
243 struct mbuf *mb, *mb2, *mreq;
246 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
247 fhp = &nfh.fh_generic;
249 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
250 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
256 nqsrv_getl(vp, ND_READ);
257 error = VOP_GETATTR(vp, vap, td);
260 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
265 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
266 nfsm_srvfillattr(vap, fp);
276 * nfs setattr service
279 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
280 struct thread *td, struct mbuf **mrq)
282 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
283 struct sockaddr *nam = nfsd->nd_nam;
284 caddr_t dpos = nfsd->nd_dpos;
285 struct ucred *cred = &nfsd->nd_cr;
286 struct vattr va, preat;
287 struct vattr *vap = &va;
288 struct nfsv2_sattr *sp;
289 struct nfs_fattr *fp;
290 struct vnode *vp = NULL;
296 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
297 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
299 struct mbuf *mb, *mb2, *mreq;
301 struct timespec guard;
303 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
304 fhp = &nfh.fh_generic;
309 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
310 gcheck = fxdr_unsigned(int, *tl);
312 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
313 fxdr_nfsv3time(tl, &guard);
316 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
318 * Nah nah nah nah na nah
319 * There is a bug in the Sun client that puts 0xffff in the mode
320 * field of sattr when it should put in 0xffffffff. The u_short
321 * doesn't sign extend.
322 * --> check the low order 2 bytes for 0xffff
324 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
325 vap->va_mode = nfstov_mode(sp->sa_mode);
326 if (sp->sa_uid != nfs_xdrneg1)
327 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
328 if (sp->sa_gid != nfs_xdrneg1)
329 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
330 if (sp->sa_size != nfs_xdrneg1)
331 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
332 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
334 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
336 vap->va_atime.tv_sec =
337 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
338 vap->va_atime.tv_nsec = 0;
341 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
342 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
347 * Now that we have all the fields, lets do it.
349 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
350 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
352 nfsm_reply(2 * NFSX_UNSIGNED);
353 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
359 * vp now an active resource, pay careful attention to cleanup
362 nqsrv_getl(vp, ND_WRITE);
364 error = preat_ret = VOP_GETATTR(vp, &preat, td);
365 if (!error && gcheck &&
366 (preat.va_ctime.tv_sec != guard.tv_sec ||
367 preat.va_ctime.tv_nsec != guard.tv_nsec))
368 error = NFSERR_NOT_SYNC;
372 nfsm_reply(NFSX_WCCDATA(v3));
373 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
380 * If the size is being changed write acces is required, otherwise
381 * just check for a read only file system.
383 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
384 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
389 if (vp->v_type == VDIR) {
392 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
397 error = VOP_SETATTR(vp, vap, cred, td);
398 postat_ret = VOP_GETATTR(vp, vap, td);
404 nfsm_reply(NFSX_WCCORFATTR(v3));
406 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
410 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
411 nfsm_srvfillattr(vap, fp);
425 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
426 struct thread *td, struct mbuf **mrq)
428 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
429 struct sockaddr *nam = nfsd->nd_nam;
430 caddr_t dpos = nfsd->nd_dpos;
431 struct ucred *cred = &nfsd->nd_cr;
432 struct nfs_fattr *fp;
433 struct nlookupdata nd;
436 struct namecache *ncp;
443 int error = 0, cache = 0, len, dirattr_ret = 1;
444 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
446 struct mbuf *mb, *mb2, *mreq;
447 struct vattr va, dirattr, *vap = &va;
450 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
455 fhp = &nfh.fh_generic;
457 nfsm_srvnamesiz(len);
459 pubflag = nfs_ispublicfh(fhp);
461 error = nfs_namei(&nd, cred, NAMEI_LOOKUP, NULL, &vp,
462 fhp, len, slp, nam, &md, &dpos,
463 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
466 * namei failure, only dirp to cleanup. Clear out garbarge from
467 * structure in case macros jump to nfsmout.
473 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
477 nfsm_reply(NFSX_POSTOPATTR(v3));
478 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
484 * Locate index file for public filehandle
486 * error is 0 on entry and 0 on exit from this block.
490 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
492 * Setup call to lookup() to see if we can find
493 * the index file. Arguably, this doesn't belong
494 * in a kernel.. Ugh. If an error occurs, do not
495 * try to install an index file and then clear the
498 * When we replace nd with ind and redirect ndp,
499 * maintenance of ni_startdir and ni_vp shift to
500 * ind and we have to clean them up in the old nd.
501 * However, the cnd resource continues to be maintained
502 * via the original nd. Confused? You aren't alone!
504 VOP_UNLOCK(vp, 0, td);
505 ncp = cache_hold(nd.nl_ncp);
507 error = nlookup_init_raw(&nd, nfs_pub.np_index,
508 UIO_SYSSPACE, 0, cred, ncp);
511 error = nlookup(&nd);
515 * Found an index file. Get rid of
516 * the old references. transfer vp and
517 * load up the new vp. Fortunately we do
518 * not have to deal with dvp, that would be
525 error = cache_vget(nd.nl_ncp, nd.nl_cred,
527 KKASSERT(error == 0);
532 * If the public filehandle was used, check that this lookup
533 * didn't result in a filehandle outside the publicly exported
534 * filesystem. We clear the poor vp here to avoid lockups due
538 if (vp->v_mount != nfs_pub.np_mount) {
547 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
553 * Resources at this point:
554 * ndp->ni_vp may not be NULL
559 nfsm_reply(NFSX_POSTOPATTR(v3));
560 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
566 /* XXX not sure how to deal with this */
567 nqsrv_getl(ndp->ni_startdir, ND_READ);
571 * Clear out some resources prior to potentially blocking. This
572 * is not as critical as ni_dvp resources in other routines, but
578 * Get underlying attribute, then release remaining resources ( for
579 * the same potential blocking reason ) and reply.
581 bzero((caddr_t)fhp, sizeof(nfh));
582 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
583 error = VFS_VPTOFH(vp, &fhp->fh_fid);
585 error = VOP_GETATTR(vp, vap, td);
589 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
591 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
595 nfsm_srvfhtom(fhp, v3);
597 nfsm_srvpostop_attr(0, vap);
598 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
600 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
601 nfsm_srvfillattr(vap, fp);
607 nlookup_done(&nd); /* may be called twice */
614 * nfs readlink service
617 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
618 struct thread *td, struct mbuf **mrq)
620 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
621 struct sockaddr *nam = nfsd->nd_nam;
622 caddr_t dpos = nfsd->nd_dpos;
623 struct ucred *cred = &nfsd->nd_cr;
624 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
625 struct iovec *ivp = iv;
630 int error = 0, rdonly, cache, i, tlen, len, getret;
631 int v3 = (nfsd->nd_flag & ND_NFSV3);
633 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
634 struct vnode *vp = NULL;
638 struct uio io, *uiop = &io;
641 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
643 mp2 = (struct mbuf *)0;
646 fhp = &nfh.fh_generic;
650 while (len < NFS_MAXPATHLEN) {
651 mp = m_getcl(MB_WAIT, MT_DATA, 0);
652 mp->m_len = MCLBYTES;
659 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
660 mp->m_len = NFS_MAXPATHLEN-len;
661 len = NFS_MAXPATHLEN;
664 ivp->iov_base = mtod(mp, caddr_t);
665 ivp->iov_len = mp->m_len;
670 uiop->uio_iovcnt = i;
671 uiop->uio_offset = 0;
672 uiop->uio_resid = len;
673 uiop->uio_rw = UIO_READ;
674 uiop->uio_segflg = UIO_SYSSPACE;
676 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
677 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
679 nfsm_reply(2 * NFSX_UNSIGNED);
680 nfsm_srvpostop_attr(1, (struct vattr *)0);
684 if (vp->v_type != VLNK) {
691 nqsrv_getl(vp, ND_READ);
692 error = VOP_READLINK(vp, uiop, cred);
694 getret = VOP_GETATTR(vp, &attr, td);
697 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
699 nfsm_srvpostop_attr(getret, &attr);
705 if (uiop->uio_resid > 0) {
706 len -= uiop->uio_resid;
707 tlen = nfsm_rndup(len);
708 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
710 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
711 *tl = txdr_unsigned(len);
726 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
727 struct thread *td, struct mbuf **mrq)
729 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
730 struct sockaddr *nam = nfsd->nd_nam;
731 caddr_t dpos = nfsd->nd_dpos;
732 struct ucred *cred = &nfsd->nd_cr;
736 struct nfs_fattr *fp;
741 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
742 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
744 struct mbuf *mb, *mb2, *mreq;
746 struct vnode *vp = NULL;
749 struct uio io, *uiop = &io;
750 struct vattr va, *vap = &va;
756 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
757 fhp = &nfh.fh_generic;
760 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
761 off = fxdr_hyper(tl);
763 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
764 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
766 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
769 * Reference vp. If an error occurs, vp will be invalid, but we
770 * have to NULL it just in case. The macros might goto nfsmout
774 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
775 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
778 nfsm_reply(2 * NFSX_UNSIGNED);
779 nfsm_srvpostop_attr(1, (struct vattr *)0);
784 if (vp->v_type != VREG) {
788 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
791 nqsrv_getl(vp, ND_READ);
792 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
793 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
795 getret = VOP_GETATTR(vp, vap, td);
801 nfsm_reply(NFSX_POSTOPATTR(v3));
802 nfsm_srvpostop_attr(getret, vap);
808 * Calculate byte count to read
811 if (off >= vap->va_size)
813 else if ((off + reqlen) > vap->va_size)
814 cnt = vap->va_size - off;
819 * Calculate seqcount for heuristic
827 * Locate best candidate
830 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
834 if (nfsheur[hi].nh_vp == vp) {
838 if (nfsheur[hi].nh_use > 0)
839 --nfsheur[hi].nh_use;
840 hi = (hi + 1) % NUM_HEURISTIC;
841 if (nfsheur[hi].nh_use < nh->nh_use)
845 if (nh->nh_vp != vp) {
848 nh->nh_use = NHUSE_INIT;
856 * Calculate heuristic
859 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
860 if (++nh->nh_seqcount > IO_SEQMAX)
861 nh->nh_seqcount = IO_SEQMAX;
862 } else if (nh->nh_seqcount > 1) {
867 nh->nh_use += NHUSE_INC;
868 if (nh->nh_use > NHUSE_MAX)
869 nh->nh_use = NHUSE_MAX;
870 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
873 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
875 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
877 fp = (struct nfs_fattr *)tl;
878 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
880 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
881 fp = (struct nfs_fattr *)tl;
882 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
884 len = left = nfsm_rndup(cnt);
887 * Generate the mbuf list with the uio_iov ref. to it.
892 siz = min(M_TRAILINGSPACE(m), left);
898 m = m_getcl(MB_WAIT, MT_DATA, 0);
904 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
906 uiop->uio_iov = iv2 = iv;
912 panic("nfsrv_read iov");
913 siz = min(M_TRAILINGSPACE(m), left);
915 iv->iov_base = mtod(m, caddr_t) + m->m_len;
924 uiop->uio_iovcnt = i;
925 uiop->uio_offset = off;
926 uiop->uio_resid = len;
927 uiop->uio_rw = UIO_READ;
928 uiop->uio_segflg = UIO_SYSSPACE;
929 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
930 off = uiop->uio_offset;
932 FREE((caddr_t)iv2, M_TEMP);
933 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
939 nfsm_reply(NFSX_POSTOPATTR(v3));
940 nfsm_srvpostop_attr(getret, vap);
949 nfsm_srvfillattr(vap, fp);
950 tlen = len - uiop->uio_resid;
951 cnt = cnt < tlen ? cnt : tlen;
952 tlen = nfsm_rndup(cnt);
953 if (len != tlen || tlen != cnt)
954 nfsm_adj(mb, len - tlen, tlen - cnt);
956 *tl++ = txdr_unsigned(cnt);
962 *tl = txdr_unsigned(cnt);
973 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
974 struct thread *td, struct mbuf **mrq)
976 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
977 struct sockaddr *nam = nfsd->nd_nam;
978 caddr_t dpos = nfsd->nd_dpos;
979 struct ucred *cred = &nfsd->nd_cr;
983 struct nfs_fattr *fp;
985 struct vattr va, forat;
986 struct vattr *vap = &va;
990 int error = 0, rdonly, cache, len, forat_ret = 1;
991 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
992 int stable = NFSV3WRITE_FILESYNC;
993 int v3 = (nfsd->nd_flag & ND_NFSV3);
995 struct mbuf *mb, *mb2, *mreq;
996 struct vnode *vp = NULL;
999 struct uio io, *uiop = &io;
1003 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1009 fhp = &nfh.fh_generic;
1012 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1013 off = fxdr_hyper(tl);
1015 stable = fxdr_unsigned(int, *tl++);
1017 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1018 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1021 stable = NFSV3WRITE_UNSTABLE;
1023 retlen = len = fxdr_unsigned(int32_t, *tl);
1027 * For NFS Version 2, it is not obvious what a write of zero length
1028 * should do, but I might as well be consistent with Version 3,
1029 * which is to return ok so long as there are no permission problems.
1037 adjust = dpos - mtod(mp, caddr_t);
1038 mp->m_len -= adjust;
1039 if (mp->m_len > 0 && adjust > 0)
1040 NFSMADV(mp, adjust);
1044 else if (mp->m_len > 0) {
1047 mp->m_len -= (i - len);
1056 if (len > NFS_MAXDATA || len < 0 || i < len) {
1058 nfsm_reply(2 * NFSX_UNSIGNED);
1059 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1063 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1064 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1067 nfsm_reply(2 * NFSX_UNSIGNED);
1068 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1073 forat_ret = VOP_GETATTR(vp, &forat, td);
1074 if (vp->v_type != VREG) {
1078 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1081 nqsrv_getl(vp, ND_WRITE);
1082 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1087 nfsm_reply(NFSX_WCCDATA(v3));
1088 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1094 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1096 uiop->uio_iov = iv = ivp;
1097 uiop->uio_iovcnt = cnt;
1100 if (mp->m_len > 0) {
1101 ivp->iov_base = mtod(mp, caddr_t);
1102 ivp->iov_len = mp->m_len;
1110 * The IO_METASYNC flag indicates that all metadata (and not just
1111 * enough to ensure data integrity) mus be written to stable storage
1113 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1115 if (stable == NFSV3WRITE_UNSTABLE)
1116 ioflags = IO_NODELOCKED;
1117 else if (stable == NFSV3WRITE_DATASYNC)
1118 ioflags = (IO_SYNC | IO_NODELOCKED);
1120 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1121 uiop->uio_resid = len;
1122 uiop->uio_rw = UIO_WRITE;
1123 uiop->uio_segflg = UIO_SYSSPACE;
1124 uiop->uio_td = NULL;
1125 uiop->uio_offset = off;
1126 error = VOP_WRITE(vp, uiop, ioflags, cred);
1127 nfsstats.srvvop_writes++;
1128 FREE((caddr_t)iv, M_TEMP);
1130 aftat_ret = VOP_GETATTR(vp, vap, td);
1135 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1136 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1138 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1143 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1144 *tl++ = txdr_unsigned(retlen);
1146 * If nfs_async is set, then pretend the write was FILESYNC.
1148 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1149 *tl++ = txdr_unsigned(stable);
1151 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1153 * Actually, there is no need to txdr these fields,
1154 * but it may make the values more human readable,
1155 * for debugging purposes.
1157 if (nfsver.tv_sec == 0)
1159 *tl++ = txdr_unsigned(nfsver.tv_sec);
1160 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1162 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1163 nfsm_srvfillattr(vap, fp);
1172 * NFS write service with write gathering support. Called when
1173 * nfsrvw_procrastinate > 0.
1174 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1175 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1179 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1180 struct thread *td, struct mbuf **mrq)
1184 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1185 struct nfs_fattr *fp;
1188 struct nfsrvw_delayhash *wpp;
1190 struct vattr va, forat;
1194 int error = 0, rdonly, cache, len, forat_ret = 1;
1195 int ioflags, aftat_ret = 1, adjust, v3, zeroing;
1197 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1198 struct vnode *vp = NULL;
1199 struct uio io, *uiop = &io;
1200 u_quad_t frev, cur_usec;
1202 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1211 mrep = nfsd->nd_mrep;
1213 dpos = nfsd->nd_dpos;
1214 cred = &nfsd->nd_cr;
1215 v3 = (nfsd->nd_flag & ND_NFSV3);
1216 LIST_INIT(&nfsd->nd_coalesce);
1217 nfsd->nd_mreq = NULL;
1218 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1219 cur_usec = nfs_curusec();
1220 nfsd->nd_time = cur_usec +
1221 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1224 * Now, get the write header..
1226 nfsm_srvmtofh(&nfsd->nd_fh);
1228 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1229 nfsd->nd_off = fxdr_hyper(tl);
1231 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1233 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1234 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1237 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1239 len = fxdr_unsigned(int32_t, *tl);
1241 nfsd->nd_eoff = nfsd->nd_off + len;
1244 * Trim the header out of the mbuf list and trim off any trailing
1245 * junk so that the mbuf list has only the write data.
1253 adjust = dpos - mtod(mp, caddr_t);
1254 mp->m_len -= adjust;
1255 if (mp->m_len > 0 && adjust > 0)
1256 NFSMADV(mp, adjust);
1263 mp->m_len -= (i - len);
1269 if (len > NFS_MAXDATA || len < 0 || i < len) {
1273 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1275 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1276 nfsd->nd_mreq = mreq;
1277 nfsd->nd_mrep = NULL;
1282 * Add this entry to the hash and time queues.
1286 wp = slp->ns_tq.lh_first;
1287 while (wp && wp->nd_time < nfsd->nd_time) {
1289 wp = wp->nd_tq.le_next;
1291 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1293 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1295 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1297 if (nfsd->nd_mrep) {
1298 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1302 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1304 wp = wp->nd_hash.le_next;
1306 while (wp && wp->nd_off < nfsd->nd_off &&
1307 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1309 wp = wp->nd_hash.le_next;
1312 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1315 * Search the hash list for overlapping entries and
1318 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1319 wp = nfsd->nd_hash.le_next;
1320 if (NFSW_SAMECRED(owp, nfsd))
1321 nfsrvw_coalesce(owp, nfsd);
1324 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1331 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1332 * and generate the associated reply mbuf list(s).
1335 cur_usec = nfs_curusec();
1337 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1338 owp = nfsd->nd_tq.le_next;
1339 if (nfsd->nd_time > cur_usec)
1343 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1344 LIST_REMOVE(nfsd, nd_tq);
1345 LIST_REMOVE(nfsd, nd_hash);
1347 mrep = nfsd->nd_mrep;
1348 nfsd->nd_mrep = NULL;
1349 cred = &nfsd->nd_cr;
1350 v3 = (nfsd->nd_flag & ND_NFSV3);
1351 forat_ret = aftat_ret = 1;
1352 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1353 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1356 forat_ret = VOP_GETATTR(vp, &forat, td);
1357 if (vp->v_type != VREG) {
1361 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1367 nqsrv_getl(vp, ND_WRITE);
1368 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1371 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1372 ioflags = IO_NODELOCKED;
1373 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1374 ioflags = (IO_SYNC | IO_NODELOCKED);
1376 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1377 uiop->uio_rw = UIO_WRITE;
1378 uiop->uio_segflg = UIO_SYSSPACE;
1379 uiop->uio_td = NULL;
1380 uiop->uio_offset = nfsd->nd_off;
1381 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1382 if (uiop->uio_resid > 0) {
1390 uiop->uio_iovcnt = i;
1391 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1393 uiop->uio_iov = ivp = iov;
1396 if (mp->m_len > 0) {
1397 ivp->iov_base = mtod(mp, caddr_t);
1398 ivp->iov_len = mp->m_len;
1404 error = VOP_WRITE(vp, uiop, ioflags, cred);
1405 nfsstats.srvvop_writes++;
1407 FREE((caddr_t)iov, M_TEMP);
1411 aftat_ret = VOP_GETATTR(vp, &va, td);
1417 * Loop around generating replies for all write rpcs that have
1418 * now been completed.
1422 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1424 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1426 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1429 nfsm_writereply(NFSX_PREOPATTR(v3) +
1430 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1431 NFSX_WRITEVERF(v3), v3);
1433 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1434 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1435 *tl++ = txdr_unsigned(nfsd->nd_len);
1436 *tl++ = txdr_unsigned(swp->nd_stable);
1438 * Actually, there is no need to txdr these fields,
1439 * but it may make the values more human readable,
1440 * for debugging purposes.
1442 if (nfsver.tv_sec == 0)
1444 *tl++ = txdr_unsigned(nfsver.tv_sec);
1445 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1447 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1448 nfsm_srvfillattr(&va, fp);
1451 nfsd->nd_mreq = mreq;
1453 panic("nfsrv_write: nd_mrep not free");
1456 * Done. Put it at the head of the timer queue so that
1457 * the final phase can return the reply.
1462 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1464 nfsd = swp->nd_coalesce.lh_first;
1466 LIST_REMOVE(nfsd, nd_tq);
1472 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1479 * Search for a reply to return.
1482 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1483 if (nfsd->nd_mreq) {
1484 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1485 LIST_REMOVE(nfsd, nd_tq);
1486 *mrq = nfsd->nd_mreq;
1495 * Coalesce the write request nfsd into owp. To do this we must:
1496 * - remove nfsd from the queues
1497 * - merge nfsd->nd_mrep into owp->nd_mrep
1498 * - update the nd_eoff and nd_stable for owp
1499 * - put nfsd on owp's nd_coalesce list
1500 * NB: Must be called at splsoftclock().
1503 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1507 struct nfsrv_descript *p;
1509 NFS_DPF(WG, ("C%03x-%03x",
1510 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1511 LIST_REMOVE(nfsd, nd_hash);
1512 LIST_REMOVE(nfsd, nd_tq);
1513 if (owp->nd_eoff < nfsd->nd_eoff) {
1514 overlap = owp->nd_eoff - nfsd->nd_off;
1516 panic("nfsrv_coalesce: bad off");
1518 m_adj(nfsd->nd_mrep, overlap);
1522 mp->m_next = nfsd->nd_mrep;
1523 owp->nd_eoff = nfsd->nd_eoff;
1525 m_freem(nfsd->nd_mrep);
1526 nfsd->nd_mrep = NULL;
1527 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1528 owp->nd_stable = NFSV3WRITE_FILESYNC;
1529 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1530 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1531 owp->nd_stable = NFSV3WRITE_DATASYNC;
1532 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1535 * If nfsd had anything else coalesced into it, transfer them
1536 * to owp, otherwise their replies will never get sent.
1538 for (p = nfsd->nd_coalesce.lh_first; p;
1539 p = nfsd->nd_coalesce.lh_first) {
1540 LIST_REMOVE(p, nd_tq);
1541 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1546 * nfs create service
1547 * now does a truncate to 0 length via. setattr if it already exists
1550 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1551 struct thread *td, struct mbuf **mrq)
1553 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1554 struct sockaddr *nam = nfsd->nd_nam;
1555 caddr_t dpos = nfsd->nd_dpos;
1556 struct ucred *cred = &nfsd->nd_cr;
1557 struct nfs_fattr *fp;
1558 struct vattr va, dirfor, diraft;
1559 struct vattr *vap = &va;
1560 struct nfsv2_sattr *sp;
1562 struct nlookupdata nd;
1565 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1566 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1569 struct mbuf *mb, *mb2, *mreq;
1575 u_quad_t frev, tempsize;
1576 u_char cverf[NFSX_V3CREATEVERF];
1578 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1587 fhp = &nfh.fh_generic;
1589 nfsm_srvnamesiz(len);
1592 * Call namei and do initial cleanup to get a few things
1593 * out of the way. If we get an initial error we cleanup
1594 * and return here to avoid special-casing the invalid nd
1595 * structure through the rest of the case. dirp may be
1596 * set even if an error occurs, but the nd structure will not
1597 * be valid at all if an error occurs so we have to invalidate it
1598 * prior to calling nfsm_reply ( which might goto nfsmout ).
1600 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1601 fhp, len, slp, nam, &md, &dpos, &dirp,
1602 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1605 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1612 nfsm_reply(NFSX_WCCDATA(v3));
1613 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1619 * No error. Continue. State:
1622 * vp may be valid or NULL if the target does not
1626 * The error state is set through the code and we may also do some
1627 * opportunistic releasing of vnodes to avoid holding locks through
1628 * NFS I/O. The cleanup at the end is a catch-all
1633 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1634 how = fxdr_unsigned(int, *tl);
1636 case NFSV3CREATE_GUARDED:
1642 case NFSV3CREATE_UNCHECKED:
1645 case NFSV3CREATE_EXCLUSIVE:
1646 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1647 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1651 vap->va_type = VREG;
1653 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1654 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1655 if (vap->va_type == VNON)
1656 vap->va_type = VREG;
1657 vap->va_mode = nfstov_mode(sp->sa_mode);
1658 switch (vap->va_type) {
1660 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1662 vap->va_size = (u_quad_t)tsize;
1667 rdev = fxdr_unsigned(long, sp->sa_size);
1675 * Iff doesn't exist, create it
1676 * otherwise just truncate to 0 length
1677 * should I set the mode too ?
1679 * The only possible error we can have at this point is EEXIST.
1680 * nd.ni_vp will also be non-NULL in that case.
1683 if (vap->va_mode == (mode_t)VNOVAL)
1685 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1686 nqsrv_getl(dvp, ND_WRITE);
1689 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1691 nfsrv_object_create(vp);
1692 if (exclusive_flag) {
1695 bcopy(cverf, (caddr_t)&vap->va_atime,
1697 error = VOP_SETATTR(vp, vap, cred, td);
1701 vap->va_type == VCHR ||
1702 vap->va_type == VBLK ||
1703 vap->va_type == VFIFO
1706 * Handle SysV FIFO node special cases. All other
1707 * devices require super user to access.
1709 if (vap->va_type == VCHR && rdev == 0xffffffff)
1710 vap->va_type = VFIFO;
1711 if (vap->va_type != VFIFO &&
1712 (error = suser_cred(cred, 0))) {
1715 vap->va_rdev = rdev;
1716 nqsrv_getl(dvp, ND_WRITE);
1720 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1725 * XXX what is this junk supposed to do ?
1732 * release dvp prior to lookup
1740 * Even though LOCKPARENT was cleared, ni_dvp may
1743 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1744 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1745 nd.ni_cnd.cn_td = td;
1746 nd.ni_cnd.cn_cred = cred;
1748 error = lookup(&nd);
1753 /* fall through on certain errors */
1755 nfsrv_object_create(nd.ni_vp);
1756 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1765 if (vap->va_size != -1) {
1766 error = nfsrv_access(vp, VWRITE, cred,
1767 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1769 nqsrv_getl(vp, ND_WRITE);
1770 tempsize = vap->va_size;
1772 vap->va_size = tempsize;
1773 error = VOP_SETATTR(vp, vap, cred, td);
1779 bzero((caddr_t)fhp, sizeof(nfh));
1780 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1781 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1783 error = VOP_GETATTR(vp, vap, td);
1786 if (exclusive_flag && !error &&
1787 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1789 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1793 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1796 nfsm_srvpostop_fh(fhp);
1797 nfsm_srvpostop_attr(0, vap);
1799 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1802 nfsm_srvfhtom(fhp, v3);
1803 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1804 nfsm_srvfillattr(vap, fp);
1829 * nfs v3 mknod service
1832 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1833 struct thread *td, struct mbuf **mrq)
1835 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1836 struct sockaddr *nam = nfsd->nd_nam;
1837 caddr_t dpos = nfsd->nd_dpos;
1838 struct ucred *cred = &nfsd->nd_cr;
1839 struct vattr va, dirfor, diraft;
1840 struct vattr *vap = &va;
1842 struct nlookupdata nd;
1845 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1846 u_int32_t major, minor;
1849 struct mbuf *mb, *mb2, *mreq;
1857 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1863 fhp = &nfh.fh_generic;
1865 nfsm_srvnamesiz(len);
1868 * Handle nfs_namei() call. If an error occurs, the nd structure
1869 * is not valid. However, nfsm_*() routines may still jump to
1873 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1874 fhp, len, slp, nam, &md, &dpos, &dirp,
1875 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1877 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1879 nfsm_reply(NFSX_WCCDATA(1));
1880 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1884 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1885 vtyp = nfsv3tov_type(*tl);
1886 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1887 error = NFSERR_BADTYPE;
1892 if (vtyp == VCHR || vtyp == VBLK) {
1893 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1894 major = fxdr_unsigned(u_int32_t, *tl++);
1895 minor = fxdr_unsigned(u_int32_t, *tl);
1896 vap->va_rdev = makeudev(major, minor);
1900 * Iff doesn't exist, create it.
1906 vap->va_type = vtyp;
1907 if (vap->va_mode == (mode_t)VNOVAL)
1909 if (vtyp == VSOCK) {
1910 nqsrv_getl(dvp, ND_WRITE);
1911 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1913 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1915 nqsrv_getl(dvp, ND_WRITE);
1917 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1926 * Release dvp prior to lookup
1932 * XXX what is this stuff for?
1934 KKASSERT(td->td_proc);
1935 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1936 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1937 nd.ni_cnd.cn_td = td;
1938 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1940 error = lookup(&nd);
1945 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1951 * send response, cleanup, return.
1963 bzero((caddr_t)fhp, sizeof(nfh));
1964 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1965 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1967 error = VOP_GETATTR(vp, vap, td);
1973 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1978 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1980 nfsm_srvpostop_fh(fhp);
1981 nfsm_srvpostop_attr(0, vap);
1983 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2001 * nfs remove service
2004 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2005 struct thread *td, struct mbuf **mrq)
2007 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2008 struct sockaddr *nam = nfsd->nd_nam;
2009 caddr_t dpos = nfsd->nd_dpos;
2010 struct ucred *cred = &nfsd->nd_cr;
2011 struct nlookupdata nd;
2015 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2016 int v3 = (nfsd->nd_flag & ND_NFSV3);
2018 struct mbuf *mb, *mreq;
2022 struct vattr dirfor, diraft;
2027 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2033 fhp = &nfh.fh_generic;
2035 nfsm_srvnamesiz(len);
2037 error = nfs_namei(&nd, cred, NAMEI_DELETE, &dvp, &vp,
2038 fhp, len, slp, nam, &md, &dpos, &dirp,
2039 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2042 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2045 if (vp->v_type == VDIR) {
2046 error = EPERM; /* POSIX */
2050 * The root of a mounted filesystem cannot be deleted.
2052 if (vp->v_flag & VROOT) {
2058 nqsrv_getl(dvp, ND_WRITE);
2059 nqsrv_getl(vp, ND_WRITE);
2071 error = VOP_NREMOVE(nd.nl_ncp, nd.nl_cred);
2075 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2076 nfsm_reply(NFSX_WCCDATA(v3));
2078 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2097 * nfs rename service
2100 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2101 struct thread *td, struct mbuf **mrq)
2103 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2104 struct sockaddr *nam = nfsd->nd_nam;
2105 caddr_t dpos = nfsd->nd_dpos;
2106 struct ucred *cred = &nfsd->nd_cr;
2110 int error = 0, cache = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2111 int tdirfor_ret = 1, tdiraft_ret = 1;
2112 int v3 = (nfsd->nd_flag & ND_NFSV3);
2114 struct mbuf *mb, *mreq;
2115 struct nlookupdata fromnd, tond;
2116 struct vnode *fvp, *fdirp;
2117 struct vnode *tvp, *tdirp;
2118 struct namecache *ncp;
2119 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2121 fhandle_t *ffhp, *tfhp;
2125 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2127 fvp = (struct vnode *)0;
2129 ffhp = &fnfh.fh_generic;
2130 tfhp = &tnfh.fh_generic;
2133 * Clear fields incase goto nfsmout occurs from macro.
2136 nlookup_zero(&fromnd);
2137 nlookup_zero(&tond);
2141 nfsm_srvmtofh(ffhp);
2142 nfsm_srvnamesiz(len);
2144 * Remember our original uid so that we can reset cr_uid before
2145 * the second nfs_namei() call, in case it is remapped.
2147 saved_uid = cred->cr_uid;
2148 error = nfs_namei(&fromnd, cred, NAMEI_DELETE, NULL, NULL,
2149 ffhp, len, slp, nam, &md, &dpos, &fdirp,
2150 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2153 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2156 nfsm_reply(2 * NFSX_WCCDATA(v3));
2157 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2158 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2164 * We have to unlock the from ncp before we can safely lookup
2167 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2168 cache_unlock(fromnd.nl_ncp);
2169 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2170 nfsm_srvmtofh(tfhp);
2171 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2172 cred->cr_uid = saved_uid;
2174 error = nfs_namei(&tond, cred, NAMEI_RENAME, NULL, NULL,
2175 tfhp, len2, slp, nam, &md, &dpos, &tdirp,
2176 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2179 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2187 if (cache_lock_nonblock(fromnd.nl_ncp) == 0) {
2188 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2189 } else if (fromnd.nl_ncp > tond.nl_ncp) {
2190 cache_lock(fromnd.nl_ncp);
2191 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2193 cache_unlock(tond.nl_ncp);
2194 cache_lock(fromnd.nl_ncp);
2195 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2196 cache_lock(tond.nl_ncp);
2197 cache_resolve(tond.nl_ncp, tond.nl_cred);
2199 fromnd.nl_flags |= NLC_NCPISLOCKED;
2201 tvp = tond.nl_ncp->nc_vp;
2202 fvp = fromnd.nl_ncp->nc_vp;
2205 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2211 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2218 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2226 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2233 if (fromnd.nl_ncp->nc_mount != tond.nl_ncp->nc_mount) {
2240 if (fromnd.nl_ncp == tond.nl_ncp->nc_parent) {
2248 * You cannot rename a source into itself or a subdirectory of itself.
2249 * We check this by travsering the target directory upwards looking
2250 * for a match against the source.
2253 for (ncp = tond.nl_ncp; ncp; ncp = ncp->nc_parent) {
2254 if (fromnd.nl_ncp == ncp) {
2262 * If source is the same as the destination (that is the
2263 * same vnode with the same name in the same directory),
2264 * then there is nothing to do.
2266 if (fromnd.nl_ncp == tond.nl_ncp)
2271 * The VOP_NRENAME function releases all vnode references &
2272 * locks prior to returning so we need to clear the pointers
2273 * to bypass cleanup code later on.
2276 /* XXX should be handled by NRENAME */
2277 nqsrv_getl(fdvp, ND_WRITE);
2278 nqsrv_getl(tdvp, ND_WRITE);
2280 nqsrv_getl(tvp, ND_WRITE);
2283 error = VOP_NRENAME(fromnd.nl_ncp, tond.nl_ncp, tond.nl_cred);
2292 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2294 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2295 nfsm_reply(2 * NFSX_WCCDATA(v3));
2297 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2298 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2306 nlookup_done(&tond);
2309 nlookup_done(&fromnd);
2317 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2318 struct thread *td, struct mbuf **mrq)
2320 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2321 struct sockaddr *nam = nfsd->nd_nam;
2322 caddr_t dpos = nfsd->nd_dpos;
2323 struct ucred *cred = &nfsd->nd_cr;
2324 struct nlookupdata nd;
2328 int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2329 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2331 struct mbuf *mb, *mreq;
2336 struct vattr dirfor, diraft, at;
2338 fhandle_t *fhp, *dfhp;
2341 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2343 dirp = dvp = vp = xp = NULL;
2345 fhp = &nfh.fh_generic;
2346 dfhp = &dnfh.fh_generic;
2348 nfsm_srvmtofh(dfhp);
2349 nfsm_srvnamesiz(len);
2351 error = nfsrv_fhtovp(fhp, FALSE, &xp, cred, slp, nam,
2352 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2354 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2355 nfsm_srvpostop_attr(getret, &at);
2356 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2361 if (xp->v_type == VDIR) {
2362 error = EPERM; /* POSIX */
2366 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
2367 dfhp, len, slp, nam, &md, &dpos, &dirp,
2368 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2371 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2380 if (xp->v_mount != dvp->v_mount)
2385 nqsrv_getl(xp, ND_WRITE);
2386 nqsrv_getl(vp, ND_WRITE);
2388 error = VOP_NLINK(nd.nl_ncp, xp, nd.nl_cred);
2394 getret = VOP_GETATTR(xp, &at, td);
2396 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2397 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2399 nfsm_srvpostop_attr(getret, &at);
2400 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2423 * nfs symbolic link service
2426 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2427 struct thread *td, struct mbuf **mrq)
2429 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2430 struct sockaddr *nam = nfsd->nd_nam;
2431 caddr_t dpos = nfsd->nd_dpos;
2432 struct ucred *cred = &nfsd->nd_cr;
2433 struct vattr va, dirfor, diraft;
2434 struct nlookupdata nd;
2435 struct vattr *vap = &va;
2438 struct nfsv2_sattr *sp;
2439 char *bpos, *pathcp = (char *)0, *cp2;
2442 int error = 0, cache = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2443 int v3 = (nfsd->nd_flag & ND_NFSV3);
2444 struct mbuf *mb, *mreq, *mb2;
2451 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2455 fhp = &nfh.fh_generic;
2457 nfsm_srvnamesiz(len);
2459 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2460 fhp, len, slp, nam, &md, &dpos, &dirp,
2461 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2464 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2472 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2473 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2474 iv.iov_base = pathcp;
2476 io.uio_resid = len2;
2480 io.uio_segflg = UIO_SYSSPACE;
2481 io.uio_rw = UIO_READ;
2483 nfsm_mtouio(&io, len2);
2485 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2486 vap->va_mode = nfstov_mode(sp->sa_mode);
2488 *(pathcp + len2) = '\0';
2494 if (vap->va_mode == (mode_t)VNOVAL)
2497 nqsrv_getl(dvp, ND_WRITE);
2499 error = VOP_NSYMLINK(nd.nl_ncp, &vp, nd.nl_cred, vap, pathcp);
2501 bzero((caddr_t)fhp, sizeof(nfh));
2502 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2503 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2505 error = VOP_GETATTR(vp, vap, td);
2510 * We have a vp in hand from the new API call, we do not have to
2516 * Issue lookup. Leave SAVESTART set so we can easily free
2517 * the name buffer later on.
2519 * since LOCKPARENT is not set, ni_dvp will be garbage on
2520 * return whether an error occurs or not.
2522 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2523 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2524 nd.ni_cnd.cn_td = td;
2525 nd.ni_cnd.cn_cred = cred;
2527 error = lookup(&nd);
2531 bzero((caddr_t)fhp, sizeof(nfh));
2532 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2533 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2535 error = VOP_GETATTR(nd.ni_vp, vap, td);
2548 FREE(pathcp, M_TEMP);
2552 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2556 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2559 nfsm_srvpostop_fh(fhp);
2560 nfsm_srvpostop_attr(0, vap);
2562 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2574 FREE(pathcp, M_TEMP);
2582 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2583 struct thread *td, struct mbuf **mrq)
2585 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2586 struct sockaddr *nam = nfsd->nd_nam;
2587 caddr_t dpos = nfsd->nd_dpos;
2588 struct ucred *cred = &nfsd->nd_cr;
2589 struct vattr va, dirfor, diraft;
2590 struct vattr *vap = &va;
2591 struct nfs_fattr *fp;
2592 struct nlookupdata nd;
2597 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2598 int v3 = (nfsd->nd_flag & ND_NFSV3);
2600 struct mbuf *mb, *mb2, *mreq;
2607 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2612 fhp = &nfh.fh_generic;
2614 nfsm_srvnamesiz(len);
2616 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2617 fhp, len, slp, nam, &md, &dpos, &dirp,
2618 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2621 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2624 nfsm_reply(NFSX_WCCDATA(v3));
2625 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2633 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2634 vap->va_mode = nfstov_mode(*tl++);
2638 * At this point nd.ni_dvp is referenced and exclusively locked and
2639 * nd.ni_vp, if it exists, is referenced but not locked.
2642 vap->va_type = VDIR;
2649 * Issue mkdir op. Since SAVESTART is not set, the pathname
2650 * component is freed by the VOP call. This will fill-in
2651 * nd.ni_vp, reference, and exclusively lock it.
2653 if (vap->va_mode == (mode_t)VNOVAL)
2656 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2658 error = VOP_NMKDIR(nd.nl_ncp, &vp, nd.nl_cred, vap);
2661 bzero((caddr_t)fhp, sizeof(nfh));
2662 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2663 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2665 error = VOP_GETATTR(vp, vap, td);
2669 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2670 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2673 nfsm_srvpostop_fh(fhp);
2674 nfsm_srvpostop_attr(0, vap);
2676 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2678 nfsm_srvfhtom(fhp, v3);
2679 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2680 nfsm_srvfillattr(vap, fp);
2698 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2699 struct thread *td, struct mbuf **mrq)
2701 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2702 struct sockaddr *nam = nfsd->nd_nam;
2703 caddr_t dpos = nfsd->nd_dpos;
2704 struct ucred *cred = &nfsd->nd_cr;
2708 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2709 int v3 = (nfsd->nd_flag & ND_NFSV3);
2711 struct mbuf *mb, *mreq;
2714 struct vattr dirfor, diraft;
2717 struct nlookupdata nd;
2720 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2725 fhp = &nfh.fh_generic;
2727 nfsm_srvnamesiz(len);
2729 error = nfs_namei(&nd, cred, NAMEI_DELETE, NULL, &vp,
2730 fhp, len, slp, nam, &md, &dpos, &dirp,
2731 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2734 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2737 nfsm_reply(NFSX_WCCDATA(v3));
2738 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2742 if (vp->v_type != VDIR) {
2748 * The root of a mounted filesystem cannot be deleted.
2750 if (vp->v_flag & VROOT)
2754 * Issue or abort op. Since SAVESTART is not set, path name
2755 * component is freed by the VOP after either.
2759 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2760 nqsrv_getl(vp, ND_WRITE);
2764 error = VOP_NRMDIR(nd.nl_ncp, nd.nl_cred);
2769 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2770 nfsm_reply(NFSX_WCCDATA(v3));
2772 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2787 * nfs readdir service
2788 * - mallocs what it thinks is enough to read
2789 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2790 * - calls VOP_READDIR()
2791 * - loops around building the reply
2792 * if the output generated exceeds count break out of loop
2793 * The nfsm_clget macro is used here so that the reply will be packed
2794 * tightly in mbuf clusters.
2795 * - it only knows that it has encountered eof when the VOP_READDIR()
2797 * - as such one readdir rpc will return eof false although you are there
2798 * and then the next will return eof
2799 * - it trims out records with d_fileno == 0
2800 * this doesn't matter for Unix clients, but they might confuse clients
2802 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2803 * than requested, but this may not apply to all filesystems. For
2804 * example, client NFS does not { although it is never remote mounted
2806 * The alternate call nfsrv_readdirplus() does lookups as well.
2807 * PS: The NFS protocol spec. does not clarify what the "count" byte
2808 * argument is a count of.. just name strings and file id's or the
2809 * entire reply rpc or ...
2810 * I tried just file name and id sizes and it confused the Sun client,
2811 * so I am using the full rpc size now. The "paranoia.." comment refers
2812 * to including the status longwords that are not a part of the dir.
2813 * "entry" structures, but are in the rpc.
2817 u_int32_t fl_postopok;
2818 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2820 u_int32_t fl_fhsize;
2821 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2825 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2826 struct thread *td, struct mbuf **mrq)
2828 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2829 struct sockaddr *nam = nfsd->nd_nam;
2830 caddr_t dpos = nfsd->nd_dpos;
2831 struct ucred *cred = &nfsd->nd_cr;
2839 struct mbuf *mb, *mb2, *mreq, *mp2;
2840 char *cpos, *cend, *cp2, *rbuf;
2841 struct vnode *vp = NULL;
2847 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2848 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2849 int v3 = (nfsd->nd_flag & ND_NFSV3);
2850 u_quad_t frev, off, toff, verf;
2851 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2853 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2854 fhp = &nfh.fh_generic;
2857 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2858 toff = fxdr_hyper(tl);
2860 verf = fxdr_hyper(tl);
2863 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2864 toff = fxdr_unsigned(u_quad_t, *tl++);
2865 verf = 0; /* shut up gcc */
2868 cnt = fxdr_unsigned(int, *tl);
2869 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2870 xfer = NFS_SRVMAXDATA(nfsd);
2876 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2877 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2878 if (!error && vp->v_type != VDIR) {
2884 nfsm_reply(NFSX_UNSIGNED);
2885 nfsm_srvpostop_attr(getret, &at);
2891 * Obtain lock on vnode for this section of the code
2894 nqsrv_getl(vp, ND_READ);
2896 error = getret = VOP_GETATTR(vp, &at, td);
2899 * XXX This check may be too strict for Solaris 2.5 clients.
2901 if (!error && toff && verf && verf != at.va_filerev)
2902 error = NFSERR_BAD_COOKIE;
2906 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
2910 nfsm_reply(NFSX_POSTOPATTR(v3));
2911 nfsm_srvpostop_attr(getret, &at);
2915 VOP_UNLOCK(vp, 0, td);
2918 * end section. Allocate rbuf and continue
2920 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2923 iv.iov_len = fullsiz;
2926 io.uio_offset = (off_t)off;
2927 io.uio_resid = fullsiz;
2928 io.uio_segflg = UIO_SYSSPACE;
2929 io.uio_rw = UIO_READ;
2932 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2934 free((caddr_t)cookies, M_TEMP);
2937 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2938 off = (off_t)io.uio_offset;
2939 if (!cookies && !error)
2940 error = NFSERR_PERM;
2942 getret = VOP_GETATTR(vp, &at, td);
2946 VOP_UNLOCK(vp, 0, td);
2950 free((caddr_t)rbuf, M_TEMP);
2952 free((caddr_t)cookies, M_TEMP);
2953 nfsm_reply(NFSX_POSTOPATTR(v3));
2954 nfsm_srvpostop_attr(getret, &at);
2959 siz -= io.uio_resid;
2962 * If nothing read, return eof
2968 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2971 nfsm_srvpostop_attr(getret, &at);
2972 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2973 txdr_hyper(at.va_filerev, tl);
2976 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2979 FREE((caddr_t)rbuf, M_TEMP);
2980 FREE((caddr_t)cookies, M_TEMP);
2987 * Check for degenerate cases of nothing useful read.
2988 * If so go try again
2992 dp = (struct dirent *)cpos;
2995 * For some reason FreeBSD's ufs_readdir() chooses to back the
2996 * directory offset up to a block boundary, so it is necessary to
2997 * skip over the records that preceed the requested offset. This
2998 * requires the assumption that file offset cookies monotonically
3001 while (cpos < cend && ncookies > 0 &&
3002 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3003 ((u_quad_t)(*cookiep)) <= toff)) {
3004 dp = _DIRENT_NEXT(dp);
3009 if (cpos >= cend || ncookies == 0) {
3015 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3016 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3018 nfsm_srvpostop_attr(getret, &at);
3019 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3020 txdr_hyper(at.va_filerev, tl);
3024 be = bp + M_TRAILINGSPACE(mp);
3026 /* Loop through the records and build reply */
3027 while (cpos < cend && ncookies > 0) {
3028 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3029 nlen = dp->d_namlen;
3030 rem = nfsm_rndup(nlen) - nlen;
3031 len += (4 * NFSX_UNSIGNED + nlen + rem);
3033 len += 2 * NFSX_UNSIGNED;
3039 * Build the directory record xdr from
3044 bp += NFSX_UNSIGNED;
3048 bp += NFSX_UNSIGNED;
3051 *tl = txdr_unsigned(dp->d_ino);
3052 bp += NFSX_UNSIGNED;
3054 *tl = txdr_unsigned(nlen);
3055 bp += NFSX_UNSIGNED;
3057 /* And loop around copying the name */
3066 bcopy(cp, bp, tsiz);
3072 /* And null pad to a int32_t boundary */
3073 for (i = 0; i < rem; i++)
3077 /* Finish off the record */
3080 bp += NFSX_UNSIGNED;
3083 *tl = txdr_unsigned(*cookiep);
3084 bp += NFSX_UNSIGNED;
3086 dp = _DIRENT_NEXT(dp);
3095 bp += NFSX_UNSIGNED;
3101 bp += NFSX_UNSIGNED;
3104 mp->m_len = bp - mtod(mp, caddr_t);
3106 mp->m_len += bp - bpos;
3107 FREE((caddr_t)rbuf, M_TEMP);
3108 FREE((caddr_t)cookies, M_TEMP);
3117 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3118 struct thread *td, struct mbuf **mrq)
3120 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3121 struct sockaddr *nam = nfsd->nd_nam;
3122 caddr_t dpos = nfsd->nd_dpos;
3123 struct ucred *cred = &nfsd->nd_cr;
3131 struct mbuf *mb, *mb2, *mreq, *mp2;
3132 char *cpos, *cend, *cp2, *rbuf;
3133 struct vnode *vp = NULL, *nvp;
3136 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3139 struct vattr va, at, *vap = &va;
3140 struct nfs_fattr *fp;
3141 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3142 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3143 u_quad_t frev, off, toff, verf;
3144 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3146 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3147 fhp = &nfh.fh_generic;
3149 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3150 toff = fxdr_hyper(tl);
3152 verf = fxdr_hyper(tl);
3154 siz = fxdr_unsigned(int, *tl++);
3155 cnt = fxdr_unsigned(int, *tl);
3157 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3158 xfer = NFS_SRVMAXDATA(nfsd);
3164 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3165 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3166 if (!error && vp->v_type != VDIR) {
3172 nfsm_reply(NFSX_UNSIGNED);
3173 nfsm_srvpostop_attr(getret, &at);
3177 error = getret = VOP_GETATTR(vp, &at, td);
3180 * XXX This check may be too strict for Solaris 2.5 clients.
3182 if (!error && toff && verf && verf != at.va_filerev)
3183 error = NFSERR_BAD_COOKIE;
3186 nqsrv_getl(vp, ND_READ);
3187 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3192 nfsm_reply(NFSX_V3POSTOPATTR);
3193 nfsm_srvpostop_attr(getret, &at);
3197 VOP_UNLOCK(vp, 0, td);
3198 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3201 iv.iov_len = fullsiz;
3204 io.uio_offset = (off_t)off;
3205 io.uio_resid = fullsiz;
3206 io.uio_segflg = UIO_SYSSPACE;
3207 io.uio_rw = UIO_READ;
3210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3212 free((caddr_t)cookies, M_TEMP);
3215 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3216 off = (u_quad_t)io.uio_offset;
3217 getret = VOP_GETATTR(vp, &at, td);
3218 VOP_UNLOCK(vp, 0, td);
3219 if (!cookies && !error)
3220 error = NFSERR_PERM;
3227 free((caddr_t)cookies, M_TEMP);
3228 free((caddr_t)rbuf, M_TEMP);
3229 nfsm_reply(NFSX_V3POSTOPATTR);
3230 nfsm_srvpostop_attr(getret, &at);
3235 siz -= io.uio_resid;
3238 * If nothing read, return eof
3244 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3246 nfsm_srvpostop_attr(getret, &at);
3247 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3248 txdr_hyper(at.va_filerev, tl);
3252 FREE((caddr_t)cookies, M_TEMP);
3253 FREE((caddr_t)rbuf, M_TEMP);
3260 * Check for degenerate cases of nothing useful read.
3261 * If so go try again
3265 dp = (struct dirent *)cpos;
3268 * For some reason FreeBSD's ufs_readdir() chooses to back the
3269 * directory offset up to a block boundary, so it is necessary to
3270 * skip over the records that preceed the requested offset. This
3271 * requires the assumption that file offset cookies monotonically
3274 while (cpos < cend && ncookies > 0 &&
3275 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3276 ((u_quad_t)(*cookiep)) <= toff)) {
3277 dp = _DIRENT_NEXT(dp);
3282 if (cpos >= cend || ncookies == 0) {
3289 * Probe one of the directory entries to see if the filesystem
3292 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp) == EOPNOTSUPP) {
3293 error = NFSERR_NOTSUPP;
3296 free((caddr_t)cookies, M_TEMP);
3297 free((caddr_t)rbuf, M_TEMP);
3298 nfsm_reply(NFSX_V3POSTOPATTR);
3299 nfsm_srvpostop_attr(getret, &at);
3308 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3310 nfsm_srvpostop_attr(getret, &at);
3311 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3312 txdr_hyper(at.va_filerev, tl);
3315 be = bp + M_TRAILINGSPACE(mp);
3317 /* Loop through the records and build reply */
3318 while (cpos < cend && ncookies > 0) {
3319 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3320 nlen = dp->d_namlen;
3321 rem = nfsm_rndup(nlen)-nlen;
3324 * For readdir_and_lookup get the vnode using
3327 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp))
3329 bzero((caddr_t)nfhp, NFSX_V3FH);
3331 nvp->v_mount->mnt_stat.f_fsid;
3332 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3337 if (VOP_GETATTR(nvp, vap, td)) {
3346 * If either the dircount or maxcount will be
3347 * exceeded, get out now. Both of these lengths
3348 * are calculated conservatively, including all
3351 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3353 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3354 if (len > cnt || dirlen > fullsiz) {
3360 * Build the directory record xdr from
3363 fp = (struct nfs_fattr *)&fl.fl_fattr;
3364 nfsm_srvfillattr(vap, fp);
3365 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3366 fl.fl_fhok = nfs_true;
3367 fl.fl_postopok = nfs_true;
3368 fl.fl_off.nfsuquad[0] = 0;
3369 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3373 bp += NFSX_UNSIGNED;
3376 bp += NFSX_UNSIGNED;
3378 *tl = txdr_unsigned(dp->d_ino);
3379 bp += NFSX_UNSIGNED;
3381 *tl = txdr_unsigned(nlen);
3382 bp += NFSX_UNSIGNED;
3384 /* And loop around copying the name */
3389 if ((bp + xfer) > be)
3393 bcopy(cp, bp, tsiz);
3399 /* And null pad to a int32_t boundary */
3400 for (i = 0; i < rem; i++)
3404 * Now copy the flrep structure out.
3406 xfer = sizeof (struct flrep);
3410 if ((bp + xfer) > be)
3414 bcopy(cp, bp, tsiz);
3422 dp = _DIRENT_NEXT(dp);
3431 bp += NFSX_UNSIGNED;
3437 bp += NFSX_UNSIGNED;
3440 mp->m_len = bp - mtod(mp, caddr_t);
3442 mp->m_len += bp - bpos;
3443 FREE((caddr_t)cookies, M_TEMP);
3444 FREE((caddr_t)rbuf, M_TEMP);
3452 * nfs commit service
3455 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3456 struct thread *td, struct mbuf **mrq)
3458 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3459 struct sockaddr *nam = nfsd->nd_nam;
3460 caddr_t dpos = nfsd->nd_dpos;
3461 struct ucred *cred = &nfsd->nd_cr;
3462 struct vattr bfor, aft;
3463 struct vnode *vp = NULL;
3469 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3471 struct mbuf *mb, *mb2, *mreq;
3474 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3478 fhp = &nfh.fh_generic;
3480 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3483 * XXX At this time VOP_FSYNC() does not accept offset and byte
3484 * count parameters, so these arguments are useless (someday maybe).
3486 off = fxdr_hyper(tl);
3488 cnt = fxdr_unsigned(int, *tl);
3489 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3490 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3492 nfsm_reply(2 * NFSX_UNSIGNED);
3493 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3497 for_ret = VOP_GETATTR(vp, &bfor, td);
3499 if (cnt > MAX_COMMIT_COUNT) {
3501 * Give up and do the whole thing
3504 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3505 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3507 error = VOP_FSYNC(vp, MNT_WAIT, td);
3510 * Locate and synchronously write any buffers that fall
3511 * into the requested range. Note: we are assuming that
3512 * f_iosize is a power of 2.
3514 int iosize = vp->v_mount->mnt_stat.f_iosize;
3515 int iomask = iosize - 1;
3519 * Align to iosize boundry, super-align to page boundry.
3522 cnt += off & iomask;
3523 off &= ~(u_quad_t)iomask;
3525 if (off & PAGE_MASK) {
3526 cnt += off & PAGE_MASK;
3527 off &= ~(u_quad_t)PAGE_MASK;
3529 lblkno = off / iosize;
3532 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3533 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3541 * If we have a buffer and it is marked B_DELWRI we
3542 * have to lock and write it. Otherwise the prior
3543 * write is assumed to have already been committed.
3545 if ((bp = findblk(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3546 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3547 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL) == 0)
3549 continue; /* retry */
3552 bp->b_flags &= ~B_ASYNC;
3553 VOP_BWRITE(bp->b_vp, bp);
3565 aft_ret = VOP_GETATTR(vp, &aft, td);
3568 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3569 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3571 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3572 if (nfsver.tv_sec == 0)
3574 *tl++ = txdr_unsigned(nfsver.tv_sec);
3575 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3586 * nfs statfs service
3589 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3590 struct thread *td, struct mbuf **mrq)
3592 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3593 struct sockaddr *nam = nfsd->nd_nam;
3594 caddr_t dpos = nfsd->nd_dpos;
3595 struct ucred *cred = &nfsd->nd_cr;
3597 struct nfs_statfs *sfp;
3601 int error = 0, rdonly, cache, getret = 1;
3602 int v3 = (nfsd->nd_flag & ND_NFSV3);
3604 struct mbuf *mb, *mb2, *mreq;
3605 struct vnode *vp = NULL;
3609 struct statfs statfs;
3610 u_quad_t frev, tval;
3612 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3616 fhp = &nfh.fh_generic;
3618 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3619 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3621 nfsm_reply(NFSX_UNSIGNED);
3622 nfsm_srvpostop_attr(getret, &at);
3627 error = VFS_STATFS(vp->v_mount, sf, td);
3628 getret = VOP_GETATTR(vp, &at, td);
3631 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3633 nfsm_srvpostop_attr(getret, &at);
3638 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3640 tval = (u_quad_t)sf->f_blocks;
3641 tval *= (u_quad_t)sf->f_bsize;
3642 txdr_hyper(tval, &sfp->sf_tbytes);
3643 tval = (u_quad_t)sf->f_bfree;
3644 tval *= (u_quad_t)sf->f_bsize;
3645 txdr_hyper(tval, &sfp->sf_fbytes);
3646 tval = (u_quad_t)sf->f_bavail;
3647 tval *= (u_quad_t)sf->f_bsize;
3648 txdr_hyper(tval, &sfp->sf_abytes);
3649 sfp->sf_tfiles.nfsuquad[0] = 0;
3650 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3651 sfp->sf_ffiles.nfsuquad[0] = 0;
3652 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3653 sfp->sf_afiles.nfsuquad[0] = 0;
3654 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3655 sfp->sf_invarsec = 0;
3657 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3658 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3659 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3660 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3661 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3670 * nfs fsinfo service
3673 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3674 struct thread *td, struct mbuf **mrq)
3676 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3677 struct sockaddr *nam = nfsd->nd_nam;
3678 caddr_t dpos = nfsd->nd_dpos;
3679 struct ucred *cred = &nfsd->nd_cr;
3681 struct nfsv3_fsinfo *sip;
3684 int error = 0, rdonly, cache, getret = 1, pref;
3686 struct mbuf *mb, *mb2, *mreq;
3687 struct vnode *vp = NULL;
3691 u_quad_t frev, maxfsize;
3694 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3698 fhp = &nfh.fh_generic;
3700 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3701 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3703 nfsm_reply(NFSX_UNSIGNED);
3704 nfsm_srvpostop_attr(getret, &at);
3709 /* XXX Try to make a guess on the max file size. */
3710 VFS_STATFS(vp->v_mount, &sb, td);
3711 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3713 getret = VOP_GETATTR(vp, &at, td);
3716 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3717 nfsm_srvpostop_attr(getret, &at);
3718 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3722 * There should be file system VFS OP(s) to get this information.
3723 * For now, assume ufs.
3725 if (slp->ns_so->so_type == SOCK_DGRAM)
3726 pref = NFS_MAXDGRAMDATA;
3729 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3730 sip->fs_rtpref = txdr_unsigned(pref);
3731 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3732 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3733 sip->fs_wtpref = txdr_unsigned(pref);
3734 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3735 sip->fs_dtpref = txdr_unsigned(pref);
3736 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3737 sip->fs_timedelta.nfsv3_sec = 0;
3738 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3739 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3740 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3741 NFSV3FSINFO_CANSETTIME);
3749 * nfs pathconf service
3752 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3753 struct thread *td, struct mbuf **mrq)
3755 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3756 struct sockaddr *nam = nfsd->nd_nam;
3757 caddr_t dpos = nfsd->nd_dpos;
3758 struct ucred *cred = &nfsd->nd_cr;
3760 struct nfsv3_pathconf *pc;
3763 int error = 0, rdonly, cache, getret = 1;
3764 register_t linkmax, namemax, chownres, notrunc;
3766 struct mbuf *mb, *mb2, *mreq;
3767 struct vnode *vp = NULL;
3773 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3777 fhp = &nfh.fh_generic;
3779 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3780 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3782 nfsm_reply(NFSX_UNSIGNED);
3783 nfsm_srvpostop_attr(getret, &at);
3787 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3789 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3791 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3793 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3794 getret = VOP_GETATTR(vp, &at, td);
3797 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3798 nfsm_srvpostop_attr(getret, &at);
3803 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3805 pc->pc_linkmax = txdr_unsigned(linkmax);
3806 pc->pc_namemax = txdr_unsigned(namemax);
3807 pc->pc_notrunc = txdr_unsigned(notrunc);
3808 pc->pc_chownrestricted = txdr_unsigned(chownres);
3811 * These should probably be supported by VOP_PATHCONF(), but
3812 * until msdosfs is exportable (why would you want to?), the
3813 * Unix defaults should be ok.
3815 pc->pc_caseinsensitive = nfs_false;
3816 pc->pc_casepreserving = nfs_true;
3824 * Null operation, used by clients to ping server
3828 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3829 struct thread *td, struct mbuf **mrq)
3831 struct mbuf *mrep = nfsd->nd_mrep;
3833 int error = NFSERR_RETVOID, cache;
3834 struct mbuf *mb, *mreq;
3837 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3846 * No operation, used for obsolete procedures
3850 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3851 struct thread *td, struct mbuf **mrq)
3853 struct mbuf *mrep = nfsd->nd_mrep;
3856 struct mbuf *mb, *mreq;
3859 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3863 if (nfsd->nd_repstat)
3864 error = nfsd->nd_repstat;
3866 error = EPROCUNAVAIL;
3873 * Perform access checking for vnodes obtained from file handles that would
3874 * refer to files already opened by a Unix client. You cannot just use
3875 * vn_writechk() and VOP_ACCESS() for two reasons.
3876 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3877 * 2 - The owner is to be given access irrespective of mode bits for some
3878 * operations, so that processes that chmod after opening a file don't
3879 * break. I don't like this because it opens a security hole, but since
3880 * the nfs server opens a security hole the size of a barn door anyhow,
3883 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3884 * will return EPERM instead of EACCESS. EPERM is always an error.
3887 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
3888 int rdonly, struct thread *td, int override)
3893 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3894 if (flags & VWRITE) {
3895 /* Just vn_writechk() changed to check rdonly */
3897 * Disallow write attempts on read-only file systems;
3898 * unless the file is a socket or a block or character
3899 * device resident on the file system.
3901 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3902 switch (vp->v_type) {
3912 * If there's shared text associated with
3913 * the inode, we can't allow writing.
3915 if (vp->v_flag & VTEXT)
3918 error = VOP_GETATTR(vp, &vattr, td);
3921 error = VOP_ACCESS(vp, flags, cred, td);
3923 * Allow certain operations for the owner (reads and writes
3924 * on files that are already open).
3926 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3930 #endif /* NFS_NOSERVER */