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.31 2006/03/29 18:45:00 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"
100 #define nfsdbprintf(info) printf info
102 #define nfsdbprintf(info)
105 #define MAX_COMMIT_COUNT (1024 * 1024)
107 #define NUM_HEURISTIC 1017
108 #define NHUSE_INIT 64
110 #define NHUSE_MAX 2048
112 static struct nfsheur {
113 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
114 off_t nh_nextr; /* next offset for sequential detection */
115 int nh_use; /* use count for selection */
116 int nh_seqcount; /* heuristic */
117 } nfsheur[NUM_HEURISTIC];
119 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
122 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
125 extern u_int32_t nfs_xdrneg1;
126 extern u_int32_t nfs_false, nfs_true;
127 extern enum vtype nv3tov_type[8];
128 extern struct nfsstats nfsstats;
130 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
131 int nfsrvw_procrastinate_v3 = 0;
133 static struct timespec nfsver;
135 SYSCTL_DECL(_vfs_nfs);
137 static int nfs_async;
138 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
139 static int nfs_commit_blks;
140 static int nfs_commit_miss;
141 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
142 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
144 static int nfsrv_access (struct vnode *,int,struct ucred *,int,
145 struct thread *, int);
146 static void nfsrvw_coalesce (struct nfsrv_descript *,
147 struct nfsrv_descript *);
150 * nfs v3 access service
153 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
154 struct thread *td, struct mbuf **mrq)
156 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
157 struct sockaddr *nam = nfsd->nd_nam;
158 caddr_t dpos = nfsd->nd_dpos;
159 struct ucred *cred = &nfsd->nd_cr;
160 struct vnode *vp = NULL;
166 int error = 0, rdonly, getret;
168 struct mbuf *mb, *mreq, *mb2;
169 struct vattr vattr, *vap = &vattr;
170 u_long testmode, nfsmode;
172 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
173 fhp = &nfh.fh_generic;
175 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
176 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
177 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
179 nfsm_reply(NFSX_UNSIGNED);
180 nfsm_srvpostop_attr(1, (struct vattr *)0);
184 nfsmode = fxdr_unsigned(u_int32_t, *tl);
185 if ((nfsmode & NFSV3ACCESS_READ) &&
186 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
187 nfsmode &= ~NFSV3ACCESS_READ;
188 if (vp->v_type == VDIR)
189 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
192 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
193 if ((nfsmode & testmode) &&
194 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
195 nfsmode &= ~testmode;
196 if (vp->v_type == VDIR)
197 testmode = NFSV3ACCESS_LOOKUP;
199 testmode = NFSV3ACCESS_EXECUTE;
200 if ((nfsmode & testmode) &&
201 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
202 nfsmode &= ~testmode;
203 getret = VOP_GETATTR(vp, vap, td);
206 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
207 nfsm_srvpostop_attr(getret, vap);
208 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
209 *tl = txdr_unsigned(nfsmode);
217 * nfs getattr service
220 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
221 struct thread *td, struct mbuf **mrq)
223 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
224 struct sockaddr *nam = nfsd->nd_nam;
225 caddr_t dpos = nfsd->nd_dpos;
226 struct ucred *cred = &nfsd->nd_cr;
227 struct nfs_fattr *fp;
229 struct vattr *vap = &va;
230 struct vnode *vp = NULL;
236 int error = 0, rdonly;
238 struct mbuf *mb, *mb2, *mreq;
240 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
241 fhp = &nfh.fh_generic;
243 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
244 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
250 error = VOP_GETATTR(vp, vap, td);
253 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
258 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
259 nfsm_srvfillattr(vap, fp);
269 * nfs setattr service
272 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
273 struct thread *td, struct mbuf **mrq)
275 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
276 struct sockaddr *nam = nfsd->nd_nam;
277 caddr_t dpos = nfsd->nd_dpos;
278 struct ucred *cred = &nfsd->nd_cr;
279 struct vattr va, preat;
280 struct vattr *vap = &va;
281 struct nfsv2_sattr *sp;
282 struct nfs_fattr *fp;
283 struct vnode *vp = NULL;
289 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
290 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
292 struct mbuf *mb, *mb2, *mreq;
293 struct timespec guard;
295 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
296 fhp = &nfh.fh_generic;
301 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
302 gcheck = fxdr_unsigned(int, *tl);
304 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
305 fxdr_nfsv3time(tl, &guard);
308 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
310 * Nah nah nah nah na nah
311 * There is a bug in the Sun client that puts 0xffff in the mode
312 * field of sattr when it should put in 0xffffffff. The u_short
313 * doesn't sign extend.
314 * --> check the low order 2 bytes for 0xffff
316 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
317 vap->va_mode = nfstov_mode(sp->sa_mode);
318 if (sp->sa_uid != nfs_xdrneg1)
319 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
320 if (sp->sa_gid != nfs_xdrneg1)
321 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
322 if (sp->sa_size != nfs_xdrneg1)
323 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
324 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
326 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
328 vap->va_atime.tv_sec =
329 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
330 vap->va_atime.tv_nsec = 0;
333 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
334 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
339 * Now that we have all the fields, lets do it.
341 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
342 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
344 nfsm_reply(2 * NFSX_UNSIGNED);
345 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
351 * vp now an active resource, pay careful attention to cleanup
355 error = preat_ret = VOP_GETATTR(vp, &preat, td);
356 if (!error && gcheck &&
357 (preat.va_ctime.tv_sec != guard.tv_sec ||
358 preat.va_ctime.tv_nsec != guard.tv_nsec))
359 error = NFSERR_NOT_SYNC;
363 nfsm_reply(NFSX_WCCDATA(v3));
364 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
371 * If the size is being changed write acces is required, otherwise
372 * just check for a read only file system.
374 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
375 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
380 if (vp->v_type == VDIR) {
383 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
388 error = VOP_SETATTR(vp, vap, cred, td);
389 postat_ret = VOP_GETATTR(vp, vap, td);
395 nfsm_reply(NFSX_WCCORFATTR(v3));
397 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
401 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
402 nfsm_srvfillattr(vap, fp);
416 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
417 struct thread *td, struct mbuf **mrq)
419 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
420 struct sockaddr *nam = nfsd->nd_nam;
421 caddr_t dpos = nfsd->nd_dpos;
422 struct ucred *cred = &nfsd->nd_cr;
423 struct nfs_fattr *fp;
424 struct nlookupdata nd;
427 struct namecache *ncp;
434 int error = 0, len, dirattr_ret = 1;
435 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
437 struct mbuf *mb, *mb2, *mreq;
438 struct vattr va, dirattr, *vap = &va;
440 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
445 fhp = &nfh.fh_generic;
447 nfsm_srvnamesiz(len);
449 pubflag = nfs_ispublicfh(fhp);
451 error = nfs_namei(&nd, cred, NAMEI_LOOKUP, NULL, &vp,
452 fhp, len, slp, nam, &md, &dpos,
453 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
456 * namei failure, only dirp to cleanup. Clear out garbarge from
457 * structure in case macros jump to nfsmout.
463 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
467 nfsm_reply(NFSX_POSTOPATTR(v3));
468 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
474 * Locate index file for public filehandle
476 * error is 0 on entry and 0 on exit from this block.
480 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
482 * Setup call to lookup() to see if we can find
483 * the index file. Arguably, this doesn't belong
484 * in a kernel.. Ugh. If an error occurs, do not
485 * try to install an index file and then clear the
488 * When we replace nd with ind and redirect ndp,
489 * maintenance of ni_startdir and ni_vp shift to
490 * ind and we have to clean them up in the old nd.
491 * However, the cnd resource continues to be maintained
492 * via the original nd. Confused? You aren't alone!
494 VOP_UNLOCK(vp, 0, td);
495 ncp = cache_hold(nd.nl_ncp);
497 error = nlookup_init_raw(&nd, nfs_pub.np_index,
498 UIO_SYSSPACE, 0, cred, ncp);
501 error = nlookup(&nd);
505 * Found an index file. Get rid of
506 * the old references. transfer vp and
507 * load up the new vp. Fortunately we do
508 * not have to deal with dvp, that would be
515 error = cache_vget(nd.nl_ncp, nd.nl_cred,
517 KKASSERT(error == 0);
522 * If the public filehandle was used, check that this lookup
523 * didn't result in a filehandle outside the publicly exported
524 * filesystem. We clear the poor vp here to avoid lockups due
528 if (vp->v_mount != nfs_pub.np_mount) {
537 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
543 * Resources at this point:
544 * ndp->ni_vp may not be NULL
549 nfsm_reply(NFSX_POSTOPATTR(v3));
550 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
556 * Clear out some resources prior to potentially blocking. This
557 * is not as critical as ni_dvp resources in other routines, but
563 * Get underlying attribute, then release remaining resources ( for
564 * the same potential blocking reason ) and reply.
566 bzero((caddr_t)fhp, sizeof(nfh));
567 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
568 error = VFS_VPTOFH(vp, &fhp->fh_fid);
570 error = VOP_GETATTR(vp, vap, td);
574 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
576 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
580 nfsm_srvfhtom(fhp, v3);
582 nfsm_srvpostop_attr(0, vap);
583 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
585 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
586 nfsm_srvfillattr(vap, fp);
592 nlookup_done(&nd); /* may be called twice */
599 * nfs readlink service
602 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
603 struct thread *td, struct mbuf **mrq)
605 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
606 struct sockaddr *nam = nfsd->nd_nam;
607 caddr_t dpos = nfsd->nd_dpos;
608 struct ucred *cred = &nfsd->nd_cr;
609 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
610 struct iovec *ivp = iv;
615 int error = 0, rdonly, i, tlen, len, getret;
616 int v3 = (nfsd->nd_flag & ND_NFSV3);
618 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
619 struct vnode *vp = NULL;
623 struct uio io, *uiop = &io;
625 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
627 mp2 = (struct mbuf *)0;
630 fhp = &nfh.fh_generic;
634 while (len < NFS_MAXPATHLEN) {
635 mp = m_getcl(MB_WAIT, MT_DATA, 0);
636 mp->m_len = MCLBYTES;
643 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
644 mp->m_len = NFS_MAXPATHLEN-len;
645 len = NFS_MAXPATHLEN;
648 ivp->iov_base = mtod(mp, caddr_t);
649 ivp->iov_len = mp->m_len;
654 uiop->uio_iovcnt = i;
655 uiop->uio_offset = 0;
656 uiop->uio_resid = len;
657 uiop->uio_rw = UIO_READ;
658 uiop->uio_segflg = UIO_SYSSPACE;
660 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
661 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
663 nfsm_reply(2 * NFSX_UNSIGNED);
664 nfsm_srvpostop_attr(1, (struct vattr *)0);
668 if (vp->v_type != VLNK) {
675 error = VOP_READLINK(vp, uiop, cred);
677 getret = VOP_GETATTR(vp, &attr, td);
680 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
682 nfsm_srvpostop_attr(getret, &attr);
688 if (uiop->uio_resid > 0) {
689 len -= uiop->uio_resid;
690 tlen = nfsm_rndup(len);
691 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
693 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
694 *tl = txdr_unsigned(len);
709 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
710 struct thread *td, struct mbuf **mrq)
712 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
713 struct sockaddr *nam = nfsd->nd_nam;
714 caddr_t dpos = nfsd->nd_dpos;
715 struct ucred *cred = &nfsd->nd_cr;
719 struct nfs_fattr *fp;
724 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
725 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
727 struct mbuf *mb, *mb2, *mreq;
729 struct vnode *vp = NULL;
732 struct uio io, *uiop = &io;
733 struct vattr va, *vap = &va;
738 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
739 fhp = &nfh.fh_generic;
742 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
743 off = fxdr_hyper(tl);
745 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
746 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
748 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
751 * Reference vp. If an error occurs, vp will be invalid, but we
752 * have to NULL it just in case. The macros might goto nfsmout
756 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
757 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
760 nfsm_reply(2 * NFSX_UNSIGNED);
761 nfsm_srvpostop_attr(1, (struct vattr *)0);
766 if (vp->v_type != VREG) {
770 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
773 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
774 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
776 getret = VOP_GETATTR(vp, vap, td);
782 nfsm_reply(NFSX_POSTOPATTR(v3));
783 nfsm_srvpostop_attr(getret, vap);
789 * Calculate byte count to read
792 if (off >= vap->va_size)
794 else if ((off + reqlen) > vap->va_size)
795 cnt = vap->va_size - off;
800 * Calculate seqcount for heuristic
808 * Locate best candidate
811 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
815 if (nfsheur[hi].nh_vp == vp) {
819 if (nfsheur[hi].nh_use > 0)
820 --nfsheur[hi].nh_use;
821 hi = (hi + 1) % NUM_HEURISTIC;
822 if (nfsheur[hi].nh_use < nh->nh_use)
826 if (nh->nh_vp != vp) {
829 nh->nh_use = NHUSE_INIT;
837 * Calculate heuristic
840 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
841 if (++nh->nh_seqcount > IO_SEQMAX)
842 nh->nh_seqcount = IO_SEQMAX;
843 } else if (nh->nh_seqcount > 1) {
848 nh->nh_use += NHUSE_INC;
849 if (nh->nh_use > NHUSE_MAX)
850 nh->nh_use = NHUSE_MAX;
851 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
854 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
856 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
858 fp = (struct nfs_fattr *)tl;
859 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
861 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
862 fp = (struct nfs_fattr *)tl;
863 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
865 len = left = nfsm_rndup(cnt);
868 * Generate the mbuf list with the uio_iov ref. to it.
873 siz = min(M_TRAILINGSPACE(m), left);
879 m = m_getcl(MB_WAIT, MT_DATA, 0);
885 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
887 uiop->uio_iov = iv2 = iv;
893 panic("nfsrv_read iov");
894 siz = min(M_TRAILINGSPACE(m), left);
896 iv->iov_base = mtod(m, caddr_t) + m->m_len;
905 uiop->uio_iovcnt = i;
906 uiop->uio_offset = off;
907 uiop->uio_resid = len;
908 uiop->uio_rw = UIO_READ;
909 uiop->uio_segflg = UIO_SYSSPACE;
910 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
911 off = uiop->uio_offset;
913 FREE((caddr_t)iv2, M_TEMP);
914 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
920 nfsm_reply(NFSX_POSTOPATTR(v3));
921 nfsm_srvpostop_attr(getret, vap);
930 nfsm_srvfillattr(vap, fp);
931 tlen = len - uiop->uio_resid;
932 cnt = cnt < tlen ? cnt : tlen;
933 tlen = nfsm_rndup(cnt);
934 if (len != tlen || tlen != cnt)
935 nfsm_adj(mb, len - tlen, tlen - cnt);
937 *tl++ = txdr_unsigned(cnt);
943 *tl = txdr_unsigned(cnt);
954 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
955 struct thread *td, struct mbuf **mrq)
957 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
958 struct sockaddr *nam = nfsd->nd_nam;
959 caddr_t dpos = nfsd->nd_dpos;
960 struct ucred *cred = &nfsd->nd_cr;
964 struct nfs_fattr *fp;
966 struct vattr va, forat;
967 struct vattr *vap = &va;
971 int error = 0, rdonly, len, forat_ret = 1;
972 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
973 int stable = NFSV3WRITE_FILESYNC;
974 int v3 = (nfsd->nd_flag & ND_NFSV3);
976 struct mbuf *mb, *mb2, *mreq;
977 struct vnode *vp = NULL;
980 struct uio io, *uiop = &io;
983 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
989 fhp = &nfh.fh_generic;
992 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
993 off = fxdr_hyper(tl);
995 stable = fxdr_unsigned(int, *tl++);
997 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
998 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1001 stable = NFSV3WRITE_UNSTABLE;
1003 retlen = len = fxdr_unsigned(int32_t, *tl);
1007 * For NFS Version 2, it is not obvious what a write of zero length
1008 * should do, but I might as well be consistent with Version 3,
1009 * which is to return ok so long as there are no permission problems.
1017 adjust = dpos - mtod(mp, caddr_t);
1018 mp->m_len -= adjust;
1019 if (mp->m_len > 0 && adjust > 0)
1020 NFSMADV(mp, adjust);
1024 else if (mp->m_len > 0) {
1027 mp->m_len -= (i - len);
1036 if (len > NFS_MAXDATA || len < 0 || i < len) {
1038 nfsm_reply(2 * NFSX_UNSIGNED);
1039 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1043 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1044 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1047 nfsm_reply(2 * NFSX_UNSIGNED);
1048 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1053 forat_ret = VOP_GETATTR(vp, &forat, td);
1054 if (vp->v_type != VREG) {
1058 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1061 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1066 nfsm_reply(NFSX_WCCDATA(v3));
1067 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1073 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1075 uiop->uio_iov = iv = ivp;
1076 uiop->uio_iovcnt = cnt;
1079 if (mp->m_len > 0) {
1080 ivp->iov_base = mtod(mp, caddr_t);
1081 ivp->iov_len = mp->m_len;
1089 * The IO_METASYNC flag indicates that all metadata (and not just
1090 * enough to ensure data integrity) mus be written to stable storage
1092 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1094 if (stable == NFSV3WRITE_UNSTABLE)
1095 ioflags = IO_NODELOCKED;
1096 else if (stable == NFSV3WRITE_DATASYNC)
1097 ioflags = (IO_SYNC | IO_NODELOCKED);
1099 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1100 uiop->uio_resid = len;
1101 uiop->uio_rw = UIO_WRITE;
1102 uiop->uio_segflg = UIO_SYSSPACE;
1103 uiop->uio_td = NULL;
1104 uiop->uio_offset = off;
1105 error = VOP_WRITE(vp, uiop, ioflags, cred);
1106 nfsstats.srvvop_writes++;
1107 FREE((caddr_t)iv, M_TEMP);
1109 aftat_ret = VOP_GETATTR(vp, vap, td);
1114 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1115 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1117 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1122 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1123 *tl++ = txdr_unsigned(retlen);
1125 * If nfs_async is set, then pretend the write was FILESYNC.
1127 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1128 *tl++ = txdr_unsigned(stable);
1130 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1132 * Actually, there is no need to txdr these fields,
1133 * but it may make the values more human readable,
1134 * for debugging purposes.
1136 if (nfsver.tv_sec == 0)
1138 *tl++ = txdr_unsigned(nfsver.tv_sec);
1139 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1141 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1142 nfsm_srvfillattr(vap, fp);
1151 * NFS write service with write gathering support. Called when
1152 * nfsrvw_procrastinate > 0.
1153 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1154 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1158 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1159 struct thread *td, struct mbuf **mrq)
1163 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1164 struct nfs_fattr *fp;
1167 struct nfsrvw_delayhash *wpp;
1169 struct vattr va, forat;
1173 int error = 0, rdonly, len, forat_ret = 1;
1174 int ioflags, aftat_ret = 1, adjust, v3, zeroing;
1176 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1177 struct vnode *vp = NULL;
1178 struct uio io, *uiop = &io;
1181 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1190 mrep = nfsd->nd_mrep;
1192 dpos = nfsd->nd_dpos;
1193 cred = &nfsd->nd_cr;
1194 v3 = (nfsd->nd_flag & ND_NFSV3);
1195 LIST_INIT(&nfsd->nd_coalesce);
1196 nfsd->nd_mreq = NULL;
1197 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1198 cur_usec = nfs_curusec();
1199 nfsd->nd_time = cur_usec +
1200 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1203 * Now, get the write header..
1205 nfsm_srvmtofh(&nfsd->nd_fh);
1207 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1208 nfsd->nd_off = fxdr_hyper(tl);
1210 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1212 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1213 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1216 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1218 len = fxdr_unsigned(int32_t, *tl);
1220 nfsd->nd_eoff = nfsd->nd_off + len;
1223 * Trim the header out of the mbuf list and trim off any trailing
1224 * junk so that the mbuf list has only the write data.
1232 adjust = dpos - mtod(mp, caddr_t);
1233 mp->m_len -= adjust;
1234 if (mp->m_len > 0 && adjust > 0)
1235 NFSMADV(mp, adjust);
1242 mp->m_len -= (i - len);
1248 if (len > NFS_MAXDATA || len < 0 || i < len) {
1252 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1254 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1255 nfsd->nd_mreq = mreq;
1256 nfsd->nd_mrep = NULL;
1261 * Add this entry to the hash and time queues.
1265 wp = slp->ns_tq.lh_first;
1266 while (wp && wp->nd_time < nfsd->nd_time) {
1268 wp = wp->nd_tq.le_next;
1270 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1272 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1274 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1276 if (nfsd->nd_mrep) {
1277 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1281 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1283 wp = wp->nd_hash.le_next;
1285 while (wp && wp->nd_off < nfsd->nd_off &&
1286 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1288 wp = wp->nd_hash.le_next;
1291 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1294 * Search the hash list for overlapping entries and
1297 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1298 wp = nfsd->nd_hash.le_next;
1299 if (NFSW_SAMECRED(owp, nfsd))
1300 nfsrvw_coalesce(owp, nfsd);
1303 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1310 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1311 * and generate the associated reply mbuf list(s).
1314 cur_usec = nfs_curusec();
1316 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1317 owp = nfsd->nd_tq.le_next;
1318 if (nfsd->nd_time > cur_usec)
1322 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1323 LIST_REMOVE(nfsd, nd_tq);
1324 LIST_REMOVE(nfsd, nd_hash);
1326 mrep = nfsd->nd_mrep;
1327 nfsd->nd_mrep = NULL;
1328 cred = &nfsd->nd_cr;
1329 v3 = (nfsd->nd_flag & ND_NFSV3);
1330 forat_ret = aftat_ret = 1;
1331 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1332 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1335 forat_ret = VOP_GETATTR(vp, &forat, td);
1336 if (vp->v_type != VREG) {
1340 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1346 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1349 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1350 ioflags = IO_NODELOCKED;
1351 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1352 ioflags = (IO_SYNC | IO_NODELOCKED);
1354 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1355 uiop->uio_rw = UIO_WRITE;
1356 uiop->uio_segflg = UIO_SYSSPACE;
1357 uiop->uio_td = NULL;
1358 uiop->uio_offset = nfsd->nd_off;
1359 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1360 if (uiop->uio_resid > 0) {
1368 uiop->uio_iovcnt = i;
1369 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1371 uiop->uio_iov = ivp = iov;
1374 if (mp->m_len > 0) {
1375 ivp->iov_base = mtod(mp, caddr_t);
1376 ivp->iov_len = mp->m_len;
1382 error = VOP_WRITE(vp, uiop, ioflags, cred);
1383 nfsstats.srvvop_writes++;
1385 FREE((caddr_t)iov, M_TEMP);
1389 aftat_ret = VOP_GETATTR(vp, &va, td);
1395 * Loop around generating replies for all write rpcs that have
1396 * now been completed.
1400 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1402 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1404 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1407 nfsm_writereply(NFSX_PREOPATTR(v3) +
1408 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1409 NFSX_WRITEVERF(v3), v3);
1411 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1412 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1413 *tl++ = txdr_unsigned(nfsd->nd_len);
1414 *tl++ = txdr_unsigned(swp->nd_stable);
1416 * Actually, there is no need to txdr these fields,
1417 * but it may make the values more human readable,
1418 * for debugging purposes.
1420 if (nfsver.tv_sec == 0)
1422 *tl++ = txdr_unsigned(nfsver.tv_sec);
1423 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1425 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1426 nfsm_srvfillattr(&va, fp);
1429 nfsd->nd_mreq = mreq;
1431 panic("nfsrv_write: nd_mrep not free");
1434 * Done. Put it at the head of the timer queue so that
1435 * the final phase can return the reply.
1440 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1442 nfsd = swp->nd_coalesce.lh_first;
1444 LIST_REMOVE(nfsd, nd_tq);
1450 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1457 * Search for a reply to return.
1460 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1461 if (nfsd->nd_mreq) {
1462 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1463 LIST_REMOVE(nfsd, nd_tq);
1464 *mrq = nfsd->nd_mreq;
1473 * Coalesce the write request nfsd into owp. To do this we must:
1474 * - remove nfsd from the queues
1475 * - merge nfsd->nd_mrep into owp->nd_mrep
1476 * - update the nd_eoff and nd_stable for owp
1477 * - put nfsd on owp's nd_coalesce list
1478 * NB: Must be called at splsoftclock().
1481 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1485 struct nfsrv_descript *p;
1487 NFS_DPF(WG, ("C%03x-%03x",
1488 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1489 LIST_REMOVE(nfsd, nd_hash);
1490 LIST_REMOVE(nfsd, nd_tq);
1491 if (owp->nd_eoff < nfsd->nd_eoff) {
1492 overlap = owp->nd_eoff - nfsd->nd_off;
1494 panic("nfsrv_coalesce: bad off");
1496 m_adj(nfsd->nd_mrep, overlap);
1500 mp->m_next = nfsd->nd_mrep;
1501 owp->nd_eoff = nfsd->nd_eoff;
1503 m_freem(nfsd->nd_mrep);
1504 nfsd->nd_mrep = NULL;
1505 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1506 owp->nd_stable = NFSV3WRITE_FILESYNC;
1507 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1508 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1509 owp->nd_stable = NFSV3WRITE_DATASYNC;
1510 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1513 * If nfsd had anything else coalesced into it, transfer them
1514 * to owp, otherwise their replies will never get sent.
1516 for (p = nfsd->nd_coalesce.lh_first; p;
1517 p = nfsd->nd_coalesce.lh_first) {
1518 LIST_REMOVE(p, nd_tq);
1519 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1524 * nfs create service
1525 * now does a truncate to 0 length via. setattr if it already exists
1528 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1529 struct thread *td, struct mbuf **mrq)
1531 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1532 struct sockaddr *nam = nfsd->nd_nam;
1533 caddr_t dpos = nfsd->nd_dpos;
1534 struct ucred *cred = &nfsd->nd_cr;
1535 struct nfs_fattr *fp;
1536 struct vattr va, dirfor, diraft;
1537 struct vattr *vap = &va;
1538 struct nfsv2_sattr *sp;
1540 struct nlookupdata nd;
1543 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1544 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1547 struct mbuf *mb, *mb2, *mreq;
1554 u_char cverf[NFSX_V3CREATEVERF];
1556 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1565 fhp = &nfh.fh_generic;
1567 nfsm_srvnamesiz(len);
1570 * Call namei and do initial cleanup to get a few things
1571 * out of the way. If we get an initial error we cleanup
1572 * and return here to avoid special-casing the invalid nd
1573 * structure through the rest of the case. dirp may be
1574 * set even if an error occurs, but the nd structure will not
1575 * be valid at all if an error occurs so we have to invalidate it
1576 * prior to calling nfsm_reply ( which might goto nfsmout ).
1578 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1579 fhp, len, slp, nam, &md, &dpos, &dirp,
1580 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1583 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1590 nfsm_reply(NFSX_WCCDATA(v3));
1591 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1597 * No error. Continue. State:
1600 * vp may be valid or NULL if the target does not
1604 * The error state is set through the code and we may also do some
1605 * opportunistic releasing of vnodes to avoid holding locks through
1606 * NFS I/O. The cleanup at the end is a catch-all
1611 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1612 how = fxdr_unsigned(int, *tl);
1614 case NFSV3CREATE_GUARDED:
1620 case NFSV3CREATE_UNCHECKED:
1623 case NFSV3CREATE_EXCLUSIVE:
1624 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1625 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1629 vap->va_type = VREG;
1631 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1632 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1633 if (vap->va_type == VNON)
1634 vap->va_type = VREG;
1635 vap->va_mode = nfstov_mode(sp->sa_mode);
1636 switch (vap->va_type) {
1638 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1640 vap->va_size = (u_quad_t)tsize;
1645 rdev = fxdr_unsigned(long, sp->sa_size);
1653 * Iff doesn't exist, create it
1654 * otherwise just truncate to 0 length
1655 * should I set the mode too ?
1657 * The only possible error we can have at this point is EEXIST.
1658 * nd.ni_vp will also be non-NULL in that case.
1661 if (vap->va_mode == (mode_t)VNOVAL)
1663 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1666 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1668 if (vap->va_type == VREG)
1670 if (exclusive_flag) {
1673 bcopy(cverf, (caddr_t)&vap->va_atime,
1675 error = VOP_SETATTR(vp, vap, cred, td);
1679 vap->va_type == VCHR ||
1680 vap->va_type == VBLK ||
1681 vap->va_type == VFIFO
1684 * Handle SysV FIFO node special cases. All other
1685 * devices require super user to access.
1687 if (vap->va_type == VCHR && rdev == 0xffffffff)
1688 vap->va_type = VFIFO;
1689 if (vap->va_type != VFIFO &&
1690 (error = suser_cred(cred, 0))) {
1693 vap->va_rdev = rdev;
1697 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1702 * XXX what is this junk supposed to do ?
1709 * release dvp prior to lookup
1717 * Even though LOCKPARENT was cleared, ni_dvp may
1720 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1721 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1722 nd.ni_cnd.cn_td = td;
1723 nd.ni_cnd.cn_cred = cred;
1725 error = lookup(&nd);
1730 /* fall through on certain errors */
1732 nfsrv_object_create(nd.ni_vp);
1733 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1742 if (vap->va_size != -1) {
1743 error = nfsrv_access(vp, VWRITE, cred,
1744 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1746 tempsize = vap->va_size;
1748 vap->va_size = tempsize;
1749 error = VOP_SETATTR(vp, vap, cred, td);
1755 bzero((caddr_t)fhp, sizeof(nfh));
1756 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1757 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1759 error = VOP_GETATTR(vp, vap, td);
1762 if (exclusive_flag && !error &&
1763 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1765 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1769 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1772 nfsm_srvpostop_fh(fhp);
1773 nfsm_srvpostop_attr(0, vap);
1775 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1778 nfsm_srvfhtom(fhp, v3);
1779 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1780 nfsm_srvfillattr(vap, fp);
1805 * nfs v3 mknod service
1808 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1809 struct thread *td, struct mbuf **mrq)
1811 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1812 struct sockaddr *nam = nfsd->nd_nam;
1813 caddr_t dpos = nfsd->nd_dpos;
1814 struct ucred *cred = &nfsd->nd_cr;
1815 struct vattr va, dirfor, diraft;
1816 struct vattr *vap = &va;
1818 struct nlookupdata nd;
1821 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1822 u_int32_t major, minor;
1825 struct mbuf *mb, *mb2, *mreq;
1832 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1838 fhp = &nfh.fh_generic;
1840 nfsm_srvnamesiz(len);
1843 * Handle nfs_namei() call. If an error occurs, the nd structure
1844 * is not valid. However, nfsm_*() routines may still jump to
1848 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1849 fhp, len, slp, nam, &md, &dpos, &dirp,
1850 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1852 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1854 nfsm_reply(NFSX_WCCDATA(1));
1855 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1859 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1860 vtyp = nfsv3tov_type(*tl);
1861 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1862 error = NFSERR_BADTYPE;
1867 if (vtyp == VCHR || vtyp == VBLK) {
1868 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1869 major = fxdr_unsigned(u_int32_t, *tl++);
1870 minor = fxdr_unsigned(u_int32_t, *tl);
1871 vap->va_rdev = makeudev(major, minor);
1875 * Iff doesn't exist, create it.
1881 vap->va_type = vtyp;
1882 if (vap->va_mode == (mode_t)VNOVAL)
1884 if (vtyp == VSOCK) {
1885 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1887 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1890 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1899 * Release dvp prior to lookup
1905 * XXX what is this stuff for?
1907 KKASSERT(td->td_proc);
1908 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1909 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1910 nd.ni_cnd.cn_td = td;
1911 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1913 error = lookup(&nd);
1918 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1924 * send response, cleanup, return.
1936 bzero((caddr_t)fhp, sizeof(nfh));
1937 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1938 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1940 error = VOP_GETATTR(vp, vap, td);
1946 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1951 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1953 nfsm_srvpostop_fh(fhp);
1954 nfsm_srvpostop_attr(0, vap);
1956 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1974 * nfs remove service
1977 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1978 struct thread *td, struct mbuf **mrq)
1980 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1981 struct sockaddr *nam = nfsd->nd_nam;
1982 caddr_t dpos = nfsd->nd_dpos;
1983 struct ucred *cred = &nfsd->nd_cr;
1984 struct nlookupdata nd;
1988 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1989 int v3 = (nfsd->nd_flag & ND_NFSV3);
1991 struct mbuf *mb, *mreq;
1995 struct vattr dirfor, diraft;
1999 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2005 fhp = &nfh.fh_generic;
2007 nfsm_srvnamesiz(len);
2009 error = nfs_namei(&nd, cred, NAMEI_DELETE, &dvp, &vp,
2010 fhp, len, slp, nam, &md, &dpos, &dirp,
2011 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2014 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2017 if (vp->v_type == VDIR) {
2018 error = EPERM; /* POSIX */
2022 * The root of a mounted filesystem cannot be deleted.
2024 if (vp->v_flag & VROOT) {
2041 error = VOP_NREMOVE(nd.nl_ncp, nd.nl_cred);
2045 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2046 nfsm_reply(NFSX_WCCDATA(v3));
2048 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2067 * nfs rename service
2070 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2071 struct thread *td, struct mbuf **mrq)
2073 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2074 struct sockaddr *nam = nfsd->nd_nam;
2075 caddr_t dpos = nfsd->nd_dpos;
2076 struct ucred *cred = &nfsd->nd_cr;
2080 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2081 int tdirfor_ret = 1, tdiraft_ret = 1;
2082 int v3 = (nfsd->nd_flag & ND_NFSV3);
2084 struct mbuf *mb, *mreq;
2085 struct nlookupdata fromnd, tond;
2086 struct vnode *fvp, *fdirp;
2087 struct vnode *tvp, *tdirp;
2088 struct namecache *ncp;
2089 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2091 fhandle_t *ffhp, *tfhp;
2094 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2096 fvp = (struct vnode *)0;
2098 ffhp = &fnfh.fh_generic;
2099 tfhp = &tnfh.fh_generic;
2102 * Clear fields incase goto nfsmout occurs from macro.
2105 nlookup_zero(&fromnd);
2106 nlookup_zero(&tond);
2110 nfsm_srvmtofh(ffhp);
2111 nfsm_srvnamesiz(len);
2113 * Remember our original uid so that we can reset cr_uid before
2114 * the second nfs_namei() call, in case it is remapped.
2116 saved_uid = cred->cr_uid;
2117 error = nfs_namei(&fromnd, cred, NAMEI_DELETE, NULL, NULL,
2118 ffhp, len, slp, nam, &md, &dpos, &fdirp,
2119 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2122 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2125 nfsm_reply(2 * NFSX_WCCDATA(v3));
2126 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2127 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2133 * We have to unlock the from ncp before we can safely lookup
2136 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2137 cache_unlock(fromnd.nl_ncp);
2138 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2139 nfsm_srvmtofh(tfhp);
2140 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2141 cred->cr_uid = saved_uid;
2143 error = nfs_namei(&tond, cred, NAMEI_RENAME, NULL, NULL,
2144 tfhp, len2, slp, nam, &md, &dpos, &tdirp,
2145 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2148 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2156 if (cache_lock_nonblock(fromnd.nl_ncp) == 0) {
2157 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2158 } else if (fromnd.nl_ncp > tond.nl_ncp) {
2159 cache_lock(fromnd.nl_ncp);
2160 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2162 cache_unlock(tond.nl_ncp);
2163 cache_lock(fromnd.nl_ncp);
2164 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2165 cache_lock(tond.nl_ncp);
2166 cache_resolve(tond.nl_ncp, tond.nl_cred);
2168 fromnd.nl_flags |= NLC_NCPISLOCKED;
2170 tvp = tond.nl_ncp->nc_vp;
2171 fvp = fromnd.nl_ncp->nc_vp;
2174 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2180 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2187 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2195 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2202 if (fromnd.nl_ncp->nc_mount != tond.nl_ncp->nc_mount) {
2209 if (fromnd.nl_ncp == tond.nl_ncp->nc_parent) {
2217 * You cannot rename a source into itself or a subdirectory of itself.
2218 * We check this by travsering the target directory upwards looking
2219 * for a match against the source.
2222 for (ncp = tond.nl_ncp; ncp; ncp = ncp->nc_parent) {
2223 if (fromnd.nl_ncp == ncp) {
2231 * If source is the same as the destination (that is the
2232 * same vnode with the same name in the same directory),
2233 * then there is nothing to do.
2235 if (fromnd.nl_ncp == tond.nl_ncp)
2240 * The VOP_NRENAME function releases all vnode references &
2241 * locks prior to returning so we need to clear the pointers
2242 * to bypass cleanup code later on.
2244 error = VOP_NRENAME(fromnd.nl_ncp, tond.nl_ncp, tond.nl_cred);
2253 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2255 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2256 nfsm_reply(2 * NFSX_WCCDATA(v3));
2258 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2259 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2267 nlookup_done(&tond);
2270 nlookup_done(&fromnd);
2278 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2279 struct thread *td, struct mbuf **mrq)
2281 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2282 struct sockaddr *nam = nfsd->nd_nam;
2283 caddr_t dpos = nfsd->nd_dpos;
2284 struct ucred *cred = &nfsd->nd_cr;
2285 struct nlookupdata nd;
2289 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2290 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2292 struct mbuf *mb, *mreq;
2297 struct vattr dirfor, diraft, at;
2299 fhandle_t *fhp, *dfhp;
2301 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2303 dirp = dvp = vp = xp = NULL;
2305 fhp = &nfh.fh_generic;
2306 dfhp = &dnfh.fh_generic;
2308 nfsm_srvmtofh(dfhp);
2309 nfsm_srvnamesiz(len);
2311 error = nfsrv_fhtovp(fhp, FALSE, &xp, cred, slp, nam,
2312 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2314 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2315 nfsm_srvpostop_attr(getret, &at);
2316 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2321 if (xp->v_type == VDIR) {
2322 error = EPERM; /* POSIX */
2326 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
2327 dfhp, len, slp, nam, &md, &dpos, &dirp,
2328 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2331 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2340 if (xp->v_mount != dvp->v_mount)
2344 error = VOP_NLINK(nd.nl_ncp, xp, nd.nl_cred);
2350 getret = VOP_GETATTR(xp, &at, td);
2352 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2353 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2355 nfsm_srvpostop_attr(getret, &at);
2356 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2379 * nfs symbolic link service
2382 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2383 struct thread *td, struct mbuf **mrq)
2385 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2386 struct sockaddr *nam = nfsd->nd_nam;
2387 caddr_t dpos = nfsd->nd_dpos;
2388 struct ucred *cred = &nfsd->nd_cr;
2389 struct vattr va, dirfor, diraft;
2390 struct nlookupdata nd;
2391 struct vattr *vap = &va;
2394 struct nfsv2_sattr *sp;
2395 char *bpos, *pathcp = (char *)0, *cp2;
2398 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2399 int v3 = (nfsd->nd_flag & ND_NFSV3);
2400 struct mbuf *mb, *mreq, *mb2;
2406 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2410 fhp = &nfh.fh_generic;
2412 nfsm_srvnamesiz(len);
2414 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2415 fhp, len, slp, nam, &md, &dpos, &dirp,
2416 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2419 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2427 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2428 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2429 iv.iov_base = pathcp;
2431 io.uio_resid = len2;
2435 io.uio_segflg = UIO_SYSSPACE;
2436 io.uio_rw = UIO_READ;
2438 nfsm_mtouio(&io, len2);
2440 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2441 vap->va_mode = nfstov_mode(sp->sa_mode);
2443 *(pathcp + len2) = '\0';
2449 if (vap->va_mode == (mode_t)VNOVAL)
2451 error = VOP_NSYMLINK(nd.nl_ncp, &vp, nd.nl_cred, vap, pathcp);
2453 bzero((caddr_t)fhp, sizeof(nfh));
2454 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2455 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2457 error = VOP_GETATTR(vp, vap, td);
2462 * We have a vp in hand from the new API call, we do not have to
2468 * Issue lookup. Leave SAVESTART set so we can easily free
2469 * the name buffer later on.
2471 * since LOCKPARENT is not set, ni_dvp will be garbage on
2472 * return whether an error occurs or not.
2474 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2475 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2476 nd.ni_cnd.cn_td = td;
2477 nd.ni_cnd.cn_cred = cred;
2479 error = lookup(&nd);
2483 bzero((caddr_t)fhp, sizeof(nfh));
2484 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2485 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2487 error = VOP_GETATTR(nd.ni_vp, vap, td);
2500 FREE(pathcp, M_TEMP);
2504 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2508 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2511 nfsm_srvpostop_fh(fhp);
2512 nfsm_srvpostop_attr(0, vap);
2514 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2526 FREE(pathcp, M_TEMP);
2534 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2535 struct thread *td, struct mbuf **mrq)
2537 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2538 struct sockaddr *nam = nfsd->nd_nam;
2539 caddr_t dpos = nfsd->nd_dpos;
2540 struct ucred *cred = &nfsd->nd_cr;
2541 struct vattr va, dirfor, diraft;
2542 struct vattr *vap = &va;
2543 struct nfs_fattr *fp;
2544 struct nlookupdata nd;
2549 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2550 int v3 = (nfsd->nd_flag & ND_NFSV3);
2552 struct mbuf *mb, *mb2, *mreq;
2558 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2563 fhp = &nfh.fh_generic;
2565 nfsm_srvnamesiz(len);
2567 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2568 fhp, len, slp, nam, &md, &dpos, &dirp,
2569 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2572 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2575 nfsm_reply(NFSX_WCCDATA(v3));
2576 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2584 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2585 vap->va_mode = nfstov_mode(*tl++);
2589 * At this point nd.ni_dvp is referenced and exclusively locked and
2590 * nd.ni_vp, if it exists, is referenced but not locked.
2593 vap->va_type = VDIR;
2600 * Issue mkdir op. Since SAVESTART is not set, the pathname
2601 * component is freed by the VOP call. This will fill-in
2602 * nd.ni_vp, reference, and exclusively lock it.
2604 if (vap->va_mode == (mode_t)VNOVAL)
2606 error = VOP_NMKDIR(nd.nl_ncp, &vp, nd.nl_cred, vap);
2609 bzero((caddr_t)fhp, sizeof(nfh));
2610 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2611 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2613 error = VOP_GETATTR(vp, vap, td);
2617 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2618 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2621 nfsm_srvpostop_fh(fhp);
2622 nfsm_srvpostop_attr(0, vap);
2624 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2626 nfsm_srvfhtom(fhp, v3);
2627 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2628 nfsm_srvfillattr(vap, fp);
2646 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2647 struct thread *td, struct mbuf **mrq)
2649 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2650 struct sockaddr *nam = nfsd->nd_nam;
2651 caddr_t dpos = nfsd->nd_dpos;
2652 struct ucred *cred = &nfsd->nd_cr;
2656 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2657 int v3 = (nfsd->nd_flag & ND_NFSV3);
2659 struct mbuf *mb, *mreq;
2662 struct vattr dirfor, diraft;
2665 struct nlookupdata nd;
2667 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2672 fhp = &nfh.fh_generic;
2674 nfsm_srvnamesiz(len);
2676 error = nfs_namei(&nd, cred, NAMEI_DELETE, NULL, &vp,
2677 fhp, len, slp, nam, &md, &dpos, &dirp,
2678 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2681 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2684 nfsm_reply(NFSX_WCCDATA(v3));
2685 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2689 if (vp->v_type != VDIR) {
2695 * The root of a mounted filesystem cannot be deleted.
2697 if (vp->v_flag & VROOT)
2701 * Issue or abort op. Since SAVESTART is not set, path name
2702 * component is freed by the VOP after either.
2707 error = VOP_NRMDIR(nd.nl_ncp, nd.nl_cred);
2712 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2713 nfsm_reply(NFSX_WCCDATA(v3));
2715 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2730 * nfs readdir service
2731 * - mallocs what it thinks is enough to read
2732 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2733 * - calls VOP_READDIR()
2734 * - loops around building the reply
2735 * if the output generated exceeds count break out of loop
2736 * The nfsm_clget macro is used here so that the reply will be packed
2737 * tightly in mbuf clusters.
2738 * - it only knows that it has encountered eof when the VOP_READDIR()
2740 * - as such one readdir rpc will return eof false although you are there
2741 * and then the next will return eof
2742 * - it trims out records with d_fileno == 0
2743 * this doesn't matter for Unix clients, but they might confuse clients
2745 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2746 * than requested, but this may not apply to all filesystems. For
2747 * example, client NFS does not { although it is never remote mounted
2749 * The alternate call nfsrv_readdirplus() does lookups as well.
2750 * PS: The NFS protocol spec. does not clarify what the "count" byte
2751 * argument is a count of.. just name strings and file id's or the
2752 * entire reply rpc or ...
2753 * I tried just file name and id sizes and it confused the Sun client,
2754 * so I am using the full rpc size now. The "paranoia.." comment refers
2755 * to including the status longwords that are not a part of the dir.
2756 * "entry" structures, but are in the rpc.
2760 u_int32_t fl_postopok;
2761 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2763 u_int32_t fl_fhsize;
2764 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2768 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2769 struct thread *td, struct mbuf **mrq)
2771 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2772 struct sockaddr *nam = nfsd->nd_nam;
2773 caddr_t dpos = nfsd->nd_dpos;
2774 struct ucred *cred = &nfsd->nd_cr;
2782 struct mbuf *mb, *mb2, *mreq, *mp2;
2783 char *cpos, *cend, *cp2, *rbuf;
2784 struct vnode *vp = NULL;
2790 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2791 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2792 int v3 = (nfsd->nd_flag & ND_NFSV3);
2793 u_quad_t off, toff, verf;
2794 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2796 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2797 fhp = &nfh.fh_generic;
2800 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2801 toff = fxdr_hyper(tl);
2803 verf = fxdr_hyper(tl);
2806 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2807 toff = fxdr_unsigned(u_quad_t, *tl++);
2808 verf = 0; /* shut up gcc */
2811 cnt = fxdr_unsigned(int, *tl);
2812 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2813 xfer = NFS_SRVMAXDATA(nfsd);
2819 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2820 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2821 if (!error && vp->v_type != VDIR) {
2827 nfsm_reply(NFSX_UNSIGNED);
2828 nfsm_srvpostop_attr(getret, &at);
2834 * Obtain lock on vnode for this section of the code
2838 error = getret = VOP_GETATTR(vp, &at, td);
2841 * XXX This check may be too strict for Solaris 2.5 clients.
2843 if (!error && toff && verf && verf != at.va_filerev)
2844 error = NFSERR_BAD_COOKIE;
2848 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
2852 nfsm_reply(NFSX_POSTOPATTR(v3));
2853 nfsm_srvpostop_attr(getret, &at);
2857 VOP_UNLOCK(vp, 0, td);
2860 * end section. Allocate rbuf and continue
2862 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2865 iv.iov_len = fullsiz;
2868 io.uio_offset = (off_t)off;
2869 io.uio_resid = fullsiz;
2870 io.uio_segflg = UIO_SYSSPACE;
2871 io.uio_rw = UIO_READ;
2874 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2876 free((caddr_t)cookies, M_TEMP);
2879 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2880 off = (off_t)io.uio_offset;
2881 if (!cookies && !error)
2882 error = NFSERR_PERM;
2884 getret = VOP_GETATTR(vp, &at, td);
2888 VOP_UNLOCK(vp, 0, td);
2892 free((caddr_t)rbuf, M_TEMP);
2894 free((caddr_t)cookies, M_TEMP);
2895 nfsm_reply(NFSX_POSTOPATTR(v3));
2896 nfsm_srvpostop_attr(getret, &at);
2901 siz -= io.uio_resid;
2904 * If nothing read, return eof
2910 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2913 nfsm_srvpostop_attr(getret, &at);
2914 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2915 txdr_hyper(at.va_filerev, tl);
2918 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2921 FREE((caddr_t)rbuf, M_TEMP);
2922 FREE((caddr_t)cookies, M_TEMP);
2929 * Check for degenerate cases of nothing useful read.
2930 * If so go try again
2934 dp = (struct dirent *)cpos;
2937 * For some reason FreeBSD's ufs_readdir() chooses to back the
2938 * directory offset up to a block boundary, so it is necessary to
2939 * skip over the records that preceed the requested offset. This
2940 * requires the assumption that file offset cookies monotonically
2943 while (cpos < cend && ncookies > 0 &&
2944 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
2945 ((u_quad_t)(*cookiep)) <= toff)) {
2946 dp = _DIRENT_NEXT(dp);
2951 if (cpos >= cend || ncookies == 0) {
2957 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2958 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2960 nfsm_srvpostop_attr(getret, &at);
2961 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2962 txdr_hyper(at.va_filerev, tl);
2966 be = bp + M_TRAILINGSPACE(mp);
2968 /* Loop through the records and build reply */
2969 while (cpos < cend && ncookies > 0) {
2970 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
2971 nlen = dp->d_namlen;
2972 rem = nfsm_rndup(nlen) - nlen;
2973 len += (4 * NFSX_UNSIGNED + nlen + rem);
2975 len += 2 * NFSX_UNSIGNED;
2981 * Build the directory record xdr from
2986 bp += NFSX_UNSIGNED;
2990 bp += NFSX_UNSIGNED;
2993 *tl = txdr_unsigned(dp->d_ino);
2994 bp += NFSX_UNSIGNED;
2996 *tl = txdr_unsigned(nlen);
2997 bp += NFSX_UNSIGNED;
2999 /* And loop around copying the name */
3008 bcopy(cp, bp, tsiz);
3014 /* And null pad to a int32_t boundary */
3015 for (i = 0; i < rem; i++)
3019 /* Finish off the record */
3022 bp += NFSX_UNSIGNED;
3025 *tl = txdr_unsigned(*cookiep);
3026 bp += NFSX_UNSIGNED;
3028 dp = _DIRENT_NEXT(dp);
3037 bp += NFSX_UNSIGNED;
3043 bp += NFSX_UNSIGNED;
3046 mp->m_len = bp - mtod(mp, caddr_t);
3048 mp->m_len += bp - bpos;
3049 FREE((caddr_t)rbuf, M_TEMP);
3050 FREE((caddr_t)cookies, M_TEMP);
3059 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3060 struct thread *td, struct mbuf **mrq)
3062 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3063 struct sockaddr *nam = nfsd->nd_nam;
3064 caddr_t dpos = nfsd->nd_dpos;
3065 struct ucred *cred = &nfsd->nd_cr;
3073 struct mbuf *mb, *mb2, *mreq, *mp2;
3074 char *cpos, *cend, *cp2, *rbuf;
3075 struct vnode *vp = NULL, *nvp;
3078 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3081 struct vattr va, at, *vap = &va;
3082 struct nfs_fattr *fp;
3083 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3084 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3085 u_quad_t off, toff, verf;
3086 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3088 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3089 fhp = &nfh.fh_generic;
3091 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3092 toff = fxdr_hyper(tl);
3094 verf = fxdr_hyper(tl);
3096 siz = fxdr_unsigned(int, *tl++);
3097 cnt = fxdr_unsigned(int, *tl);
3099 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3100 xfer = NFS_SRVMAXDATA(nfsd);
3106 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3107 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3108 if (!error && vp->v_type != VDIR) {
3114 nfsm_reply(NFSX_UNSIGNED);
3115 nfsm_srvpostop_attr(getret, &at);
3119 error = getret = VOP_GETATTR(vp, &at, td);
3122 * XXX This check may be too strict for Solaris 2.5 clients.
3124 if (!error && toff && verf && verf != at.va_filerev)
3125 error = NFSERR_BAD_COOKIE;
3128 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3133 nfsm_reply(NFSX_V3POSTOPATTR);
3134 nfsm_srvpostop_attr(getret, &at);
3138 VOP_UNLOCK(vp, 0, td);
3139 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3142 iv.iov_len = fullsiz;
3145 io.uio_offset = (off_t)off;
3146 io.uio_resid = fullsiz;
3147 io.uio_segflg = UIO_SYSSPACE;
3148 io.uio_rw = UIO_READ;
3151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3153 free((caddr_t)cookies, M_TEMP);
3156 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3157 off = (u_quad_t)io.uio_offset;
3158 getret = VOP_GETATTR(vp, &at, td);
3159 VOP_UNLOCK(vp, 0, td);
3160 if (!cookies && !error)
3161 error = NFSERR_PERM;
3168 free((caddr_t)cookies, M_TEMP);
3169 free((caddr_t)rbuf, M_TEMP);
3170 nfsm_reply(NFSX_V3POSTOPATTR);
3171 nfsm_srvpostop_attr(getret, &at);
3176 siz -= io.uio_resid;
3179 * If nothing read, return eof
3185 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3187 nfsm_srvpostop_attr(getret, &at);
3188 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3189 txdr_hyper(at.va_filerev, tl);
3193 FREE((caddr_t)cookies, M_TEMP);
3194 FREE((caddr_t)rbuf, M_TEMP);
3201 * Check for degenerate cases of nothing useful read.
3202 * If so go try again
3206 dp = (struct dirent *)cpos;
3209 * For some reason FreeBSD's ufs_readdir() chooses to back the
3210 * directory offset up to a block boundary, so it is necessary to
3211 * skip over the records that preceed the requested offset. This
3212 * requires the assumption that file offset cookies monotonically
3215 while (cpos < cend && ncookies > 0 &&
3216 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3217 ((u_quad_t)(*cookiep)) <= toff)) {
3218 dp = _DIRENT_NEXT(dp);
3223 if (cpos >= cend || ncookies == 0) {
3230 * Probe one of the directory entries to see if the filesystem
3233 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp) == EOPNOTSUPP) {
3234 error = NFSERR_NOTSUPP;
3237 free((caddr_t)cookies, M_TEMP);
3238 free((caddr_t)rbuf, M_TEMP);
3239 nfsm_reply(NFSX_V3POSTOPATTR);
3240 nfsm_srvpostop_attr(getret, &at);
3249 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3251 nfsm_srvpostop_attr(getret, &at);
3252 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3253 txdr_hyper(at.va_filerev, tl);
3256 be = bp + M_TRAILINGSPACE(mp);
3258 /* Loop through the records and build reply */
3259 while (cpos < cend && ncookies > 0) {
3260 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3261 nlen = dp->d_namlen;
3262 rem = nfsm_rndup(nlen)-nlen;
3265 * For readdir_and_lookup get the vnode using
3268 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp))
3270 bzero((caddr_t)nfhp, NFSX_V3FH);
3272 nvp->v_mount->mnt_stat.f_fsid;
3273 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3278 if (VOP_GETATTR(nvp, vap, td)) {
3287 * If either the dircount or maxcount will be
3288 * exceeded, get out now. Both of these lengths
3289 * are calculated conservatively, including all
3292 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3294 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3295 if (len > cnt || dirlen > fullsiz) {
3301 * Build the directory record xdr from
3304 fp = (struct nfs_fattr *)&fl.fl_fattr;
3305 nfsm_srvfillattr(vap, fp);
3306 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3307 fl.fl_fhok = nfs_true;
3308 fl.fl_postopok = nfs_true;
3309 fl.fl_off.nfsuquad[0] = 0;
3310 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3314 bp += NFSX_UNSIGNED;
3317 bp += NFSX_UNSIGNED;
3319 *tl = txdr_unsigned(dp->d_ino);
3320 bp += NFSX_UNSIGNED;
3322 *tl = txdr_unsigned(nlen);
3323 bp += NFSX_UNSIGNED;
3325 /* And loop around copying the name */
3330 if ((bp + xfer) > be)
3334 bcopy(cp, bp, tsiz);
3340 /* And null pad to a int32_t boundary */
3341 for (i = 0; i < rem; i++)
3345 * Now copy the flrep structure out.
3347 xfer = sizeof (struct flrep);
3351 if ((bp + xfer) > be)
3355 bcopy(cp, bp, tsiz);
3363 dp = _DIRENT_NEXT(dp);
3372 bp += NFSX_UNSIGNED;
3378 bp += NFSX_UNSIGNED;
3381 mp->m_len = bp - mtod(mp, caddr_t);
3383 mp->m_len += bp - bpos;
3384 FREE((caddr_t)cookies, M_TEMP);
3385 FREE((caddr_t)rbuf, M_TEMP);
3393 * nfs commit service
3396 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3397 struct thread *td, struct mbuf **mrq)
3399 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3400 struct sockaddr *nam = nfsd->nd_nam;
3401 caddr_t dpos = nfsd->nd_dpos;
3402 struct ucred *cred = &nfsd->nd_cr;
3403 struct vattr bfor, aft;
3404 struct vnode *vp = NULL;
3410 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3412 struct mbuf *mb, *mb2, *mreq;
3415 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3416 fhp = &nfh.fh_generic;
3418 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3421 * XXX At this time VOP_FSYNC() does not accept offset and byte
3422 * count parameters, so these arguments are useless (someday maybe).
3424 off = fxdr_hyper(tl);
3426 cnt = fxdr_unsigned(int, *tl);
3427 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3428 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3430 nfsm_reply(2 * NFSX_UNSIGNED);
3431 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3435 for_ret = VOP_GETATTR(vp, &bfor, td);
3437 if (cnt > MAX_COMMIT_COUNT) {
3439 * Give up and do the whole thing
3442 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3443 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3445 error = VOP_FSYNC(vp, MNT_WAIT, td);
3448 * Locate and synchronously write any buffers that fall
3449 * into the requested range. Note: we are assuming that
3450 * f_iosize is a power of 2.
3452 int iosize = vp->v_mount->mnt_stat.f_iosize;
3453 int iomask = iosize - 1;
3457 * Align to iosize boundry, super-align to page boundry.
3460 cnt += off & iomask;
3461 off &= ~(u_quad_t)iomask;
3463 if (off & PAGE_MASK) {
3464 cnt += off & PAGE_MASK;
3465 off &= ~(u_quad_t)PAGE_MASK;
3470 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3471 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3479 * If we have a buffer and it is marked B_DELWRI we
3480 * have to lock and write it. Otherwise the prior
3481 * write is assumed to have already been committed.
3483 if ((bp = findblk(vp, loffset)) != NULL && (bp->b_flags & B_DELWRI)) {
3484 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3485 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL) == 0)
3487 continue; /* retry */
3490 bp->b_flags &= ~B_ASYNC;
3491 VOP_BWRITE(bp->b_vp, bp);
3503 aft_ret = VOP_GETATTR(vp, &aft, td);
3506 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3507 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3509 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3510 if (nfsver.tv_sec == 0)
3512 *tl++ = txdr_unsigned(nfsver.tv_sec);
3513 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3524 * nfs statfs service
3527 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3528 struct thread *td, struct mbuf **mrq)
3530 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3531 struct sockaddr *nam = nfsd->nd_nam;
3532 caddr_t dpos = nfsd->nd_dpos;
3533 struct ucred *cred = &nfsd->nd_cr;
3535 struct nfs_statfs *sfp;
3539 int error = 0, rdonly, getret = 1;
3540 int v3 = (nfsd->nd_flag & ND_NFSV3);
3542 struct mbuf *mb, *mb2, *mreq;
3543 struct vnode *vp = NULL;
3547 struct statfs statfs;
3550 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3551 fhp = &nfh.fh_generic;
3553 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3554 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3556 nfsm_reply(NFSX_UNSIGNED);
3557 nfsm_srvpostop_attr(getret, &at);
3562 error = VFS_STATFS(vp->v_mount, sf, td);
3563 getret = VOP_GETATTR(vp, &at, td);
3566 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3568 nfsm_srvpostop_attr(getret, &at);
3573 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3575 tval = (u_quad_t)sf->f_blocks;
3576 tval *= (u_quad_t)sf->f_bsize;
3577 txdr_hyper(tval, &sfp->sf_tbytes);
3578 tval = (u_quad_t)sf->f_bfree;
3579 tval *= (u_quad_t)sf->f_bsize;
3580 txdr_hyper(tval, &sfp->sf_fbytes);
3581 tval = (u_quad_t)sf->f_bavail;
3582 tval *= (u_quad_t)sf->f_bsize;
3583 txdr_hyper(tval, &sfp->sf_abytes);
3584 sfp->sf_tfiles.nfsuquad[0] = 0;
3585 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3586 sfp->sf_ffiles.nfsuquad[0] = 0;
3587 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3588 sfp->sf_afiles.nfsuquad[0] = 0;
3589 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3590 sfp->sf_invarsec = 0;
3592 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3593 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3594 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3595 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3596 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3605 * nfs fsinfo service
3608 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3609 struct thread *td, struct mbuf **mrq)
3611 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3612 struct sockaddr *nam = nfsd->nd_nam;
3613 caddr_t dpos = nfsd->nd_dpos;
3614 struct ucred *cred = &nfsd->nd_cr;
3616 struct nfsv3_fsinfo *sip;
3619 int error = 0, rdonly, getret = 1, pref;
3621 struct mbuf *mb, *mb2, *mreq;
3622 struct vnode *vp = NULL;
3629 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3630 fhp = &nfh.fh_generic;
3632 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3633 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3635 nfsm_reply(NFSX_UNSIGNED);
3636 nfsm_srvpostop_attr(getret, &at);
3641 /* XXX Try to make a guess on the max file size. */
3642 VFS_STATFS(vp->v_mount, &sb, td);
3643 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3645 getret = VOP_GETATTR(vp, &at, td);
3648 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3649 nfsm_srvpostop_attr(getret, &at);
3650 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3654 * There should be file system VFS OP(s) to get this information.
3655 * For now, assume ufs.
3657 if (slp->ns_so->so_type == SOCK_DGRAM)
3658 pref = NFS_MAXDGRAMDATA;
3661 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3662 sip->fs_rtpref = txdr_unsigned(pref);
3663 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3664 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3665 sip->fs_wtpref = txdr_unsigned(pref);
3666 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3667 sip->fs_dtpref = txdr_unsigned(pref);
3668 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3669 sip->fs_timedelta.nfsv3_sec = 0;
3670 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3671 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3672 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3673 NFSV3FSINFO_CANSETTIME);
3681 * nfs pathconf service
3684 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3685 struct thread *td, struct mbuf **mrq)
3687 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3688 struct sockaddr *nam = nfsd->nd_nam;
3689 caddr_t dpos = nfsd->nd_dpos;
3690 struct ucred *cred = &nfsd->nd_cr;
3692 struct nfsv3_pathconf *pc;
3695 int error = 0, rdonly, getret = 1;
3696 register_t linkmax, namemax, chownres, notrunc;
3698 struct mbuf *mb, *mb2, *mreq;
3699 struct vnode *vp = NULL;
3704 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3705 fhp = &nfh.fh_generic;
3707 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3708 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3710 nfsm_reply(NFSX_UNSIGNED);
3711 nfsm_srvpostop_attr(getret, &at);
3715 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3717 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3719 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3721 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3722 getret = VOP_GETATTR(vp, &at, td);
3725 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3726 nfsm_srvpostop_attr(getret, &at);
3731 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3733 pc->pc_linkmax = txdr_unsigned(linkmax);
3734 pc->pc_namemax = txdr_unsigned(namemax);
3735 pc->pc_notrunc = txdr_unsigned(notrunc);
3736 pc->pc_chownrestricted = txdr_unsigned(chownres);
3739 * These should probably be supported by VOP_PATHCONF(), but
3740 * until msdosfs is exportable (why would you want to?), the
3741 * Unix defaults should be ok.
3743 pc->pc_caseinsensitive = nfs_false;
3744 pc->pc_casepreserving = nfs_true;
3752 * Null operation, used by clients to ping server
3756 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3757 struct thread *td, struct mbuf **mrq)
3759 struct mbuf *mrep = nfsd->nd_mrep;
3761 int error = NFSERR_RETVOID;
3762 struct mbuf *mb, *mreq;
3764 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3770 * No operation, used for obsolete procedures
3774 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3775 struct thread *td, struct mbuf **mrq)
3777 struct mbuf *mrep = nfsd->nd_mrep;
3780 struct mbuf *mb, *mreq;
3782 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3783 if (nfsd->nd_repstat)
3784 error = nfsd->nd_repstat;
3786 error = EPROCUNAVAIL;
3793 * Perform access checking for vnodes obtained from file handles that would
3794 * refer to files already opened by a Unix client. You cannot just use
3795 * vn_writechk() and VOP_ACCESS() for two reasons.
3796 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3797 * 2 - The owner is to be given access irrespective of mode bits for some
3798 * operations, so that processes that chmod after opening a file don't
3799 * break. I don't like this because it opens a security hole, but since
3800 * the nfs server opens a security hole the size of a barn door anyhow,
3803 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3804 * will return EPERM instead of EACCESS. EPERM is always an error.
3807 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
3808 int rdonly, struct thread *td, int override)
3813 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3814 if (flags & VWRITE) {
3815 /* Just vn_writechk() changed to check rdonly */
3817 * Disallow write attempts on read-only file systems;
3818 * unless the file is a socket or a block or character
3819 * device resident on the file system.
3821 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3822 switch (vp->v_type) {
3832 * If there's shared text associated with
3833 * the inode, we can't allow writing.
3835 if (vp->v_flag & VTEXT)
3838 error = VOP_GETATTR(vp, &vattr, td);
3841 error = VOP_ACCESS(vp, flags, cred, td);
3843 * Allow certain operations for the owner (reads and writes
3844 * on files that are already open).
3846 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3850 #endif /* NFS_NOSERVER */