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.48 2008/09/17 21:44:24 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>
70 #include <sys/nlookup.h>
71 #include <sys/namei.h>
72 #include <sys/unistd.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/malloc.h>
79 #include <sys/dirent.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_zone.h>
88 #include <vm/vm_object.h>
92 #include <sys/thread2.h>
98 #include "nfsm_subs.h"
101 #define nfsdbprintf(info) kprintf 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 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
127 int nfsrvw_procrastinate_v3 = 0;
129 static struct timespec nfsver;
131 SYSCTL_DECL(_vfs_nfs);
134 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
135 static int nfs_commit_blks;
136 static int nfs_commit_miss;
137 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
138 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
140 static int nfsrv_access (struct mount *, struct vnode *, int,
141 struct ucred *, int, struct thread *, int);
142 static void nfsrvw_coalesce (struct nfsrv_descript *,
143 struct nfsrv_descript *);
146 * nfs v3 access service
149 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
150 struct thread *td, struct mbuf **mrq)
152 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
153 struct sockaddr *nam = nfsd->nd_nam;
154 caddr_t dpos = nfsd->nd_dpos;
155 struct ucred *cred = &nfsd->nd_cr;
156 struct vnode *vp = NULL;
157 struct mount *mp = NULL;
163 int error = 0, rdonly, getret;
165 struct mbuf *mb, *mreq, *mb2;
166 struct vattr vattr, *vap = &vattr;
167 u_long testmode, nfsmode;
169 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
170 fhp = &nfh.fh_generic;
172 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
173 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
174 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
176 nfsm_reply(NFSX_UNSIGNED);
177 nfsm_srvpostop_attr(1, NULL);
181 nfsmode = fxdr_unsigned(u_int32_t, *tl);
182 if ((nfsmode & NFSV3ACCESS_READ) &&
183 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
184 nfsmode &= ~NFSV3ACCESS_READ;
185 if (vp->v_type == VDIR)
186 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
189 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
190 if ((nfsmode & testmode) &&
191 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
192 nfsmode &= ~testmode;
193 if (vp->v_type == VDIR)
194 testmode = NFSV3ACCESS_LOOKUP;
196 testmode = NFSV3ACCESS_EXECUTE;
197 if ((nfsmode & testmode) &&
198 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
199 nfsmode &= ~testmode;
200 getret = VOP_GETATTR(vp, vap);
203 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
204 nfsm_srvpostop_attr(getret, vap);
205 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
206 *tl = txdr_unsigned(nfsmode);
214 * nfs getattr service
217 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
218 struct thread *td, struct mbuf **mrq)
220 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
221 struct sockaddr *nam = nfsd->nd_nam;
222 caddr_t dpos = nfsd->nd_dpos;
223 struct ucred *cred = &nfsd->nd_cr;
224 struct nfs_fattr *fp;
226 struct vattr *vap = &va;
227 struct vnode *vp = NULL;
228 struct mount *mp = NULL;
234 int error = 0, rdonly;
236 struct mbuf *mb, *mb2, *mreq;
238 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
239 fhp = &nfh.fh_generic;
241 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
242 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
248 error = VOP_GETATTR(vp, vap);
251 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
256 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
257 nfsm_srvfillattr(vap, fp);
267 * nfs setattr service
270 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
271 struct thread *td, struct mbuf **mrq)
273 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
274 struct sockaddr *nam = nfsd->nd_nam;
275 caddr_t dpos = nfsd->nd_dpos;
276 struct ucred *cred = &nfsd->nd_cr;
277 struct vattr va, preat;
278 struct vattr *vap = &va;
279 struct nfsv2_sattr *sp;
280 struct nfs_fattr *fp;
281 struct vnode *vp = NULL;
282 struct mount *mp = NULL;
288 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
289 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
291 struct mbuf *mb, *mb2, *mreq;
292 struct timespec guard;
294 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
295 fhp = &nfh.fh_generic;
300 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
301 gcheck = fxdr_unsigned(int, *tl);
303 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
304 fxdr_nfsv3time(tl, &guard);
307 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
309 * Nah nah nah nah na nah
310 * There is a bug in the Sun client that puts 0xffff in the mode
311 * field of sattr when it should put in 0xffffffff. The u_short
312 * doesn't sign extend.
313 * --> check the low order 2 bytes for 0xffff
315 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
316 vap->va_mode = nfstov_mode(sp->sa_mode);
317 if (sp->sa_uid != nfs_xdrneg1)
318 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
319 if (sp->sa_gid != nfs_xdrneg1)
320 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
321 if (sp->sa_size != nfs_xdrneg1)
322 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
323 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
325 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
327 vap->va_atime.tv_sec =
328 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
329 vap->va_atime.tv_nsec = 0;
332 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
333 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
338 * Now that we have all the fields, lets do it.
340 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
341 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
343 nfsm_reply(2 * NFSX_UNSIGNED);
344 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
350 * vp now an active resource, pay careful attention to cleanup
354 error = preat_ret = VOP_GETATTR(vp, &preat);
355 if (!error && gcheck &&
356 (preat.va_ctime.tv_sec != guard.tv_sec ||
357 preat.va_ctime.tv_nsec != guard.tv_nsec))
358 error = NFSERR_NOT_SYNC;
362 nfsm_reply(NFSX_WCCDATA(v3));
363 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
370 * If the size is being changed write acces is required, otherwise
371 * just check for a read only file system.
373 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
374 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
379 if (vp->v_type == VDIR) {
382 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
387 error = VOP_SETATTR(vp, vap, cred);
388 postat_ret = VOP_GETATTR(vp, vap);
394 nfsm_reply(NFSX_WCCORFATTR(v3));
396 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
400 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
401 nfsm_srvfillattr(vap, fp);
415 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
416 struct thread *td, struct mbuf **mrq)
418 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
419 struct sockaddr *nam = nfsd->nd_nam;
420 caddr_t dpos = nfsd->nd_dpos;
421 struct ucred *cred = &nfsd->nd_cr;
422 struct nfs_fattr *fp;
423 struct nlookupdata nd;
433 int error = 0, len, dirattr_ret = 1;
434 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
436 struct mbuf *mb, *mb2, *mreq;
437 struct vattr va, dirattr, *vap = &va;
439 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
444 fhp = &nfh.fh_generic;
446 nfsm_srvnamesiz(len);
448 pubflag = nfs_ispublicfh(fhp);
450 error = nfs_namei(&nd, cred, 0, NULL, &vp,
451 fhp, len, slp, nam, &md, &dpos,
452 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
455 * namei failure, only dirp to cleanup. Clear out garbarge from
456 * structure in case macros jump to nfsmout.
462 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
466 nfsm_reply(NFSX_POSTOPATTR(v3));
467 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
473 * Locate index file for public filehandle
475 * error is 0 on entry and 0 on exit from this block.
479 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
481 * Setup call to lookup() to see if we can find
482 * the index file. Arguably, this doesn't belong
483 * in a kernel.. Ugh. If an error occurs, do not
484 * try to install an index file and then clear the
487 * When we replace nd with ind and redirect ndp,
488 * maintenance of ni_startdir and ni_vp shift to
489 * ind and we have to clean them up in the old nd.
490 * However, the cnd resource continues to be maintained
491 * via the original nd. Confused? You aren't alone!
494 cache_copy(&nd.nl_nch, &nch);
496 error = nlookup_init_raw(&nd, nfs_pub.np_index,
497 UIO_SYSSPACE, 0, cred, &nch);
500 error = nlookup(&nd);
504 * Found an index file. Get rid of
505 * the old references. transfer vp and
506 * load up the new vp. Fortunately we do
507 * not have to deal with dvp, that would be
514 error = cache_vget(&nd.nl_nch, nd.nl_cred,
516 KKASSERT(error == 0);
521 * If the public filehandle was used, check that this lookup
522 * didn't result in a filehandle outside the publicly exported
523 * filesystem. We clear the poor vp here to avoid lockups due
527 if (vp->v_mount != nfs_pub.np_mount) {
536 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
542 * Resources at this point:
543 * ndp->ni_vp may not be NULL
548 nfsm_reply(NFSX_POSTOPATTR(v3));
549 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
555 * Clear out some resources prior to potentially blocking. This
556 * is not as critical as ni_dvp resources in other routines, but
562 * Get underlying attribute, then release remaining resources ( for
563 * the same potential blocking reason ) and reply.
565 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
566 error = VFS_VPTOFH(vp, &fhp->fh_fid);
568 error = VOP_GETATTR(vp, vap);
572 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
574 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
578 nfsm_srvfhtom(fhp, v3);
580 nfsm_srvpostop_attr(0, vap);
581 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
583 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
584 nfsm_srvfillattr(vap, fp);
590 nlookup_done(&nd); /* may be called twice */
597 * nfs readlink service
600 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
601 struct thread *td, struct mbuf **mrq)
603 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
604 struct sockaddr *nam = nfsd->nd_nam;
605 caddr_t dpos = nfsd->nd_dpos;
606 struct ucred *cred = &nfsd->nd_cr;
607 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
608 struct iovec *ivp = iv;
612 int error = 0, rdonly, i, tlen, len, getret;
613 int v3 = (nfsd->nd_flag & ND_NFSV3);
615 struct mbuf *mb, *mb2, *mp1, *mp2, *mp3, *mreq;
616 struct vnode *vp = NULL;
617 struct mount *mp = NULL;
621 struct uio io, *uiop = &io;
623 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
628 fhp = &nfh.fh_generic;
632 while (len < NFS_MAXPATHLEN) {
633 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
634 mp1->m_len = MCLBYTES;
641 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
642 mp1->m_len = NFS_MAXPATHLEN-len;
643 len = NFS_MAXPATHLEN;
646 ivp->iov_base = mtod(mp1, caddr_t);
647 ivp->iov_len = mp1->m_len;
652 uiop->uio_iovcnt = i;
653 uiop->uio_offset = 0;
654 uiop->uio_resid = len;
655 uiop->uio_rw = UIO_READ;
656 uiop->uio_segflg = UIO_SYSSPACE;
658 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
659 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
661 nfsm_reply(2 * NFSX_UNSIGNED);
662 nfsm_srvpostop_attr(1, NULL);
666 if (vp->v_type != VLNK) {
673 error = VOP_READLINK(vp, uiop, cred);
675 getret = VOP_GETATTR(vp, &attr);
678 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
680 nfsm_srvpostop_attr(getret, &attr);
686 if (uiop->uio_resid > 0) {
687 len -= uiop->uio_resid;
688 tlen = nfsm_rndup(len);
689 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
691 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
692 *tl = txdr_unsigned(len);
707 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
708 struct thread *td, struct mbuf **mrq)
710 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
711 struct sockaddr *nam = nfsd->nd_nam;
712 caddr_t dpos = nfsd->nd_dpos;
713 struct ucred *cred = &nfsd->nd_cr;
717 struct nfs_fattr *fp;
722 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
723 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
725 struct mbuf *mb, *mb2, *mreq;
727 struct vnode *vp = NULL;
728 struct mount *mp = NULL;
731 struct uio io, *uiop = &io;
732 struct vattr va, *vap = &va;
737 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
738 fhp = &nfh.fh_generic;
741 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
742 off = fxdr_hyper(tl);
744 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
745 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
747 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
750 * Reference vp. If an error occurs, vp will be invalid, but we
751 * have to NULL it just in case. The macros might goto nfsmout
755 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
756 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
759 nfsm_reply(2 * NFSX_UNSIGNED);
760 nfsm_srvpostop_attr(1, NULL);
765 if (vp->v_type != VREG) {
769 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
772 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
773 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
775 getret = VOP_GETATTR(vp, vap);
781 nfsm_reply(NFSX_POSTOPATTR(v3));
782 nfsm_srvpostop_attr(getret, vap);
788 * Calculate byte count to read
791 if (off >= vap->va_size)
793 else if ((off + reqlen) > vap->va_size)
794 cnt = vap->va_size - off;
799 * Calculate seqcount for heuristic
807 * Locate best candidate
810 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
814 if (nfsheur[hi].nh_vp == vp) {
818 if (nfsheur[hi].nh_use > 0)
819 --nfsheur[hi].nh_use;
820 hi = (hi + 1) % NUM_HEURISTIC;
821 if (nfsheur[hi].nh_use < nh->nh_use)
825 if (nh->nh_vp != vp) {
828 nh->nh_use = NHUSE_INIT;
836 * Calculate heuristic
839 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
840 if (++nh->nh_seqcount > IO_SEQMAX)
841 nh->nh_seqcount = IO_SEQMAX;
842 } else if (nh->nh_seqcount > 1) {
847 nh->nh_use += NHUSE_INC;
848 if (nh->nh_use > NHUSE_MAX)
849 nh->nh_use = NHUSE_MAX;
850 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
853 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
855 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
857 fp = (struct nfs_fattr *)tl;
858 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
860 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
861 fp = (struct nfs_fattr *)tl;
862 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
864 len = left = nfsm_rndup(cnt);
867 * Generate the mbuf list with the uio_iov ref. to it.
872 siz = min(M_TRAILINGSPACE(m), left);
878 m = m_getcl(MB_WAIT, MT_DATA, 0);
884 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
886 uiop->uio_iov = iv2 = iv;
892 panic("nfsrv_read iov");
893 siz = min(M_TRAILINGSPACE(m), left);
895 iv->iov_base = mtod(m, caddr_t) + m->m_len;
904 uiop->uio_iovcnt = i;
905 uiop->uio_offset = off;
906 uiop->uio_resid = len;
907 uiop->uio_rw = UIO_READ;
908 uiop->uio_segflg = UIO_SYSSPACE;
909 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
910 off = uiop->uio_offset;
912 FREE((caddr_t)iv2, M_TEMP);
913 if (error || (getret = VOP_GETATTR(vp, vap))) {
919 nfsm_reply(NFSX_POSTOPATTR(v3));
920 nfsm_srvpostop_attr(getret, vap);
929 nfsm_srvfillattr(vap, fp);
930 tlen = len - uiop->uio_resid;
931 cnt = cnt < tlen ? cnt : tlen;
932 tlen = nfsm_rndup(cnt);
933 if (len != tlen || tlen != cnt)
934 nfsm_adj(mb, len - tlen, tlen - cnt);
936 *tl++ = txdr_unsigned(cnt);
942 *tl = txdr_unsigned(cnt);
953 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
954 struct thread *td, struct mbuf **mrq)
956 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
957 struct sockaddr *nam = nfsd->nd_nam;
958 caddr_t dpos = nfsd->nd_dpos;
959 struct ucred *cred = &nfsd->nd_cr;
963 struct nfs_fattr *fp;
965 struct vattr va, forat;
966 struct vattr *vap = &va;
970 int error = 0, rdonly, len, forat_ret = 1;
971 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
972 int stable = NFSV3WRITE_FILESYNC;
973 int v3 = (nfsd->nd_flag & ND_NFSV3);
975 struct mbuf *mb, *mb2, *mreq;
976 struct vnode *vp = NULL;
977 struct mount *mp = 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(mp1, caddr_t);
1018 mp1->m_len -= adjust;
1019 if (mp1->m_len > 0 && adjust > 0)
1020 NFSMADV(mp1, adjust);
1024 else if (mp1->m_len > 0) {
1027 mp1->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, &mp, &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);
1054 if (vp->v_type != VREG) {
1058 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1061 error = nfsrv_access(mp, 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 (mp1->m_len > 0) {
1080 ivp->iov_base = mtod(mp1, caddr_t);
1081 ivp->iov_len = mp1->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);
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)
1162 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1163 struct nfs_fattr *fp;
1166 struct nfsrvw_delayhash *wpp;
1168 struct vattr va, forat;
1172 int error = 0, rdonly, len, forat_ret = 1;
1173 int ioflags, aftat_ret = 1, adjust, v3, zeroing;
1175 struct mbuf *mb, *mb2, *mreq, *mrep, *md, *mp1;
1176 struct vnode *vp = NULL;
1177 struct mount *mp = 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(mp1, caddr_t);
1233 mp1->m_len -= adjust;
1234 if (mp1->m_len > 0 && adjust > 0)
1235 NFSMADV(mp1, adjust);
1242 mp1->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, &mp, &vp, cred, slp,
1332 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1335 forat_ret = VOP_GETATTR(vp, &forat);
1336 if (vp->v_type != VREG) {
1340 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1346 error = nfsrv_access(mp, 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 (mp1->m_len > 0) {
1375 ivp->iov_base = mtod(mp1, caddr_t);
1376 ivp->iov_len = mp1->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);
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 mp1->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, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1544 udev_t rdev = NOUDEV;
1545 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1548 struct mbuf *mb, *mb2, *mreq;
1556 u_char cverf[NFSX_V3CREATEVERF];
1558 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1564 fhp = &nfh.fh_generic;
1566 nfsm_srvnamesiz(len);
1569 * Call namei and do initial cleanup to get a few things
1570 * out of the way. If we get an initial error we cleanup
1571 * and return here to avoid special-casing the invalid nd
1572 * structure through the rest of the case. dirp may be
1573 * set even if an error occurs, but the nd structure will not
1574 * be valid at all if an error occurs so we have to invalidate it
1575 * prior to calling nfsm_reply ( which might goto nfsmout ).
1577 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1578 fhp, len, slp, nam, &md, &dpos, &dirp,
1579 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1580 mp = vfs_getvfs(&fhp->fh_fsid);
1584 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1591 nfsm_reply(NFSX_WCCDATA(v3));
1592 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1598 * No error. Continue. State:
1601 * vp may be valid or NULL if the target does not
1605 * The error state is set through the code and we may also do some
1606 * opportunistic releasing of vnodes to avoid holding locks through
1607 * NFS I/O. The cleanup at the end is a catch-all
1612 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1613 how = fxdr_unsigned(int, *tl);
1615 case NFSV3CREATE_GUARDED:
1621 case NFSV3CREATE_UNCHECKED:
1624 case NFSV3CREATE_EXCLUSIVE:
1625 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1626 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1630 vap->va_type = VREG;
1632 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1633 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1634 if (vap->va_type == VNON)
1635 vap->va_type = VREG;
1636 vap->va_mode = nfstov_mode(sp->sa_mode);
1637 switch (vap->va_type) {
1639 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1641 vap->va_size = (u_quad_t)tsize;
1646 rdev = fxdr_unsigned(long, sp->sa_size);
1654 * Iff doesn't exist, create it
1655 * otherwise just truncate to 0 length
1656 * should I set the mode too ?
1658 * The only possible error we can have at this point is EEXIST.
1659 * nd.ni_vp will also be non-NULL in that case.
1662 if (vap->va_mode == (mode_t)VNOVAL)
1664 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1666 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1671 if (exclusive_flag) {
1674 bcopy(cverf, (caddr_t)&vap->va_atime,
1676 error = VOP_SETATTR(vp, vap, cred);
1680 vap->va_type == VCHR ||
1681 vap->va_type == VBLK ||
1682 vap->va_type == VFIFO
1685 * Handle SysV FIFO node special cases. All other
1686 * devices require super user to access.
1688 if (vap->va_type == VCHR && rdev == 0xffffffff)
1689 vap->va_type = VFIFO;
1690 if (vap->va_type != VFIFO &&
1691 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1694 vap->va_rmajor = umajor(rdev);
1695 vap->va_rminor = uminor(rdev);
1698 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1705 * XXX what is this junk supposed to do ?
1712 * release dvp prior to lookup
1720 * Even though LOCKPARENT was cleared, ni_dvp may
1723 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1724 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1725 nd.ni_cnd.cn_td = td;
1726 nd.ni_cnd.cn_cred = cred;
1728 error = lookup(&nd);
1733 /* fall through on certain errors */
1735 nfsrv_object_create(nd.ni_vp);
1736 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1745 if (vap->va_size != -1) {
1746 error = nfsrv_access(mp, vp, VWRITE, cred,
1747 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1749 tempsize = vap->va_size;
1751 vap->va_size = tempsize;
1752 error = VOP_SETATTR(vp, vap, cred);
1758 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1759 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1761 error = VOP_GETATTR(vp, vap);
1764 if (exclusive_flag && !error &&
1765 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1767 diraft_ret = VOP_GETATTR(dirp, &diraft);
1771 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1774 nfsm_srvpostop_fh(fhp);
1775 nfsm_srvpostop_attr(0, vap);
1777 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1780 nfsm_srvfhtom(fhp, v3);
1781 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1782 nfsm_srvfillattr(vap, fp);
1807 * nfs v3 mknod service
1810 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1811 struct thread *td, struct mbuf **mrq)
1813 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1814 struct sockaddr *nam = nfsd->nd_nam;
1815 caddr_t dpos = nfsd->nd_dpos;
1816 struct ucred *cred = &nfsd->nd_cr;
1817 struct vattr va, dirfor, diraft;
1818 struct vattr *vap = &va;
1820 struct nlookupdata nd;
1823 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1826 struct mbuf *mb, *mb2, *mreq;
1833 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1839 fhp = &nfh.fh_generic;
1841 nfsm_srvnamesiz(len);
1844 * Handle nfs_namei() call. If an error occurs, the nd structure
1845 * is not valid. However, nfsm_*() routines may still jump to
1849 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1850 fhp, len, slp, nam, &md, &dpos, &dirp,
1851 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1853 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1855 nfsm_reply(NFSX_WCCDATA(1));
1856 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1860 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1861 vtyp = nfsv3tov_type(*tl);
1862 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1863 error = NFSERR_BADTYPE;
1868 if (vtyp == VCHR || vtyp == VBLK) {
1869 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1870 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1871 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1875 * Iff doesn't exist, create it.
1881 vap->va_type = vtyp;
1882 if (vap->va_mode == (mode_t)VNOVAL)
1884 if (vtyp == VSOCK) {
1886 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1890 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1894 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1902 * send response, cleanup, return.
1914 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1915 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1917 error = VOP_GETATTR(vp, vap);
1923 diraft_ret = VOP_GETATTR(dirp, &diraft);
1928 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1930 nfsm_srvpostop_fh(fhp);
1931 nfsm_srvpostop_attr(0, vap);
1933 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1951 * nfs remove service
1954 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1955 struct thread *td, struct mbuf **mrq)
1957 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1958 struct sockaddr *nam = nfsd->nd_nam;
1959 caddr_t dpos = nfsd->nd_dpos;
1960 struct ucred *cred = &nfsd->nd_cr;
1961 struct nlookupdata nd;
1965 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1966 int v3 = (nfsd->nd_flag & ND_NFSV3);
1968 struct mbuf *mb, *mreq;
1972 struct vattr dirfor, diraft;
1976 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1982 fhp = &nfh.fh_generic;
1984 nfsm_srvnamesiz(len);
1986 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
1987 fhp, len, slp, nam, &md, &dpos, &dirp,
1988 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1991 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1994 if (vp->v_type == VDIR) {
1995 error = EPERM; /* POSIX */
1999 * The root of a mounted filesystem cannot be deleted.
2001 if (vp->v_flag & VROOT) {
2013 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2019 diraft_ret = VOP_GETATTR(dirp, &diraft);
2020 nfsm_reply(NFSX_WCCDATA(v3));
2022 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2041 * nfs rename service
2044 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2045 struct thread *td, struct mbuf **mrq)
2047 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2048 struct sockaddr *nam = nfsd->nd_nam;
2049 caddr_t dpos = nfsd->nd_dpos;
2050 struct ucred *cred = &nfsd->nd_cr;
2054 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2055 int tdirfor_ret = 1, tdiraft_ret = 1;
2056 int v3 = (nfsd->nd_flag & ND_NFSV3);
2058 struct mbuf *mb, *mreq;
2059 struct nlookupdata fromnd, tond;
2060 struct vnode *fvp, *fdirp, *fdvp;
2061 struct vnode *tvp, *tdirp, *tdvp;
2062 struct namecache *ncp;
2063 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2065 fhandle_t *ffhp, *tfhp;
2068 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2072 ffhp = &fnfh.fh_generic;
2073 tfhp = &tnfh.fh_generic;
2076 * Clear fields incase goto nfsmout occurs from macro.
2079 nlookup_zero(&fromnd);
2080 nlookup_zero(&tond);
2084 nfsm_srvmtofh(ffhp);
2085 nfsm_srvnamesiz(len);
2087 * Remember our original uid so that we can reset cr_uid before
2088 * the second nfs_namei() call, in case it is remapped.
2090 saved_uid = cred->cr_uid;
2091 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2093 ffhp, len, slp, nam, &md, &dpos, &fdirp,
2094 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2097 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2100 nfsm_reply(2 * NFSX_WCCDATA(v3));
2101 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2102 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2108 * We have to unlock the from ncp before we can safely lookup
2111 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2112 cache_unlock(&fromnd.nl_nch);
2113 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2114 nfsm_srvmtofh(tfhp);
2115 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2116 cred->cr_uid = saved_uid;
2118 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2119 tfhp, len2, slp, nam, &md, &dpos, &tdirp,
2120 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2123 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2131 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2132 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2133 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2134 cache_lock(&fromnd.nl_nch);
2135 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2137 cache_unlock(&tond.nl_nch);
2138 cache_lock(&fromnd.nl_nch);
2139 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2140 cache_lock(&tond.nl_nch);
2141 cache_resolve(&tond.nl_nch, tond.nl_cred);
2143 fromnd.nl_flags |= NLC_NCPISLOCKED;
2145 fvp = fromnd.nl_nch.ncp->nc_vp;
2146 tvp = tond.nl_nch.ncp->nc_vp;
2149 * Set fdvp and tdvp. We haven't done all the topology checks
2150 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2151 * point). If we get through the checks these will be guarenteed
2154 * Holding the children ncp's should be sufficient to prevent
2155 * fdvp and tdvp ripouts.
2157 if (fromnd.nl_nch.ncp->nc_parent)
2158 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2161 if (tond.nl_nch.ncp->nc_parent)
2162 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2167 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2173 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2180 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2188 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2195 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2202 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2210 * You cannot rename a source into itself or a subdirectory of itself.
2211 * We check this by travsering the target directory upwards looking
2212 * for a match against the source.
2215 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2216 if (fromnd.nl_nch.ncp == ncp) {
2224 * If source is the same as the destination (that is the
2225 * same vnode with the same name in the same directory),
2226 * then there is nothing to do.
2228 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2233 * The VOP_NRENAME function releases all vnode references &
2234 * locks prior to returning so we need to clear the pointers
2235 * to bypass cleanup code later on.
2237 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2238 fdvp, tdvp, tond.nl_cred);
2247 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2249 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2250 nfsm_reply(2 * NFSX_WCCDATA(v3));
2252 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2253 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2261 nlookup_done(&tond);
2264 nlookup_done(&fromnd);
2272 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2273 struct thread *td, struct mbuf **mrq)
2275 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2276 struct sockaddr *nam = nfsd->nd_nam;
2277 caddr_t dpos = nfsd->nd_dpos;
2278 struct ucred *cred = &nfsd->nd_cr;
2279 struct nlookupdata nd;
2283 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2284 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2286 struct mbuf *mb, *mreq;
2293 struct vattr dirfor, diraft, at;
2295 fhandle_t *fhp, *dfhp;
2297 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2299 dirp = dvp = vp = xp = NULL;
2302 fhp = &nfh.fh_generic;
2303 dfhp = &dnfh.fh_generic;
2305 nfsm_srvmtofh(dfhp);
2306 nfsm_srvnamesiz(len);
2308 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2309 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2311 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2312 nfsm_srvpostop_attr(getret, &at);
2313 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2318 if (xp->v_type == VDIR) {
2319 error = EPERM; /* POSIX */
2323 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2324 dfhp, len, slp, nam, &md, &dpos, &dirp,
2325 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2328 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2337 if (xp->v_mount != dvp->v_mount)
2342 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2350 getret = VOP_GETATTR(xp, &at);
2352 diraft_ret = VOP_GETATTR(dirp, &diraft);
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 = NULL, *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;
2407 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2413 fhp = &nfh.fh_generic;
2415 nfsm_srvnamesiz(len);
2417 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2418 fhp, len, slp, nam, &md, &dpos, &dirp,
2419 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2422 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2430 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2431 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2432 iv.iov_base = pathcp;
2434 io.uio_resid = len2;
2438 io.uio_segflg = UIO_SYSSPACE;
2439 io.uio_rw = UIO_READ;
2441 nfsm_mtouio(&io, len2);
2443 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2444 vap->va_mode = nfstov_mode(sp->sa_mode);
2446 *(pathcp + len2) = '\0';
2452 if (vap->va_mode == (mode_t)VNOVAL)
2456 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2460 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2461 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2463 error = VOP_GETATTR(vp, vap);
2478 FREE(pathcp, M_TEMP);
2482 diraft_ret = VOP_GETATTR(dirp, &diraft);
2486 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2489 nfsm_srvpostop_fh(fhp);
2490 nfsm_srvpostop_attr(0, vap);
2492 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2504 FREE(pathcp, M_TEMP);
2512 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2513 struct thread *td, struct mbuf **mrq)
2515 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2516 struct sockaddr *nam = nfsd->nd_nam;
2517 caddr_t dpos = nfsd->nd_dpos;
2518 struct ucred *cred = &nfsd->nd_cr;
2519 struct vattr va, dirfor, diraft;
2520 struct vattr *vap = &va;
2521 struct nfs_fattr *fp;
2522 struct nlookupdata nd;
2527 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2528 int v3 = (nfsd->nd_flag & ND_NFSV3);
2530 struct mbuf *mb, *mb2, *mreq;
2537 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2543 fhp = &nfh.fh_generic;
2545 nfsm_srvnamesiz(len);
2547 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2548 fhp, len, slp, nam, &md, &dpos, &dirp,
2549 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2552 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2555 nfsm_reply(NFSX_WCCDATA(v3));
2556 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2564 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2565 vap->va_mode = nfstov_mode(*tl++);
2569 * At this point nd.ni_dvp is referenced and exclusively locked and
2570 * nd.ni_vp, if it exists, is referenced but not locked.
2573 vap->va_type = VDIR;
2580 * Issue mkdir op. Since SAVESTART is not set, the pathname
2581 * component is freed by the VOP call. This will fill-in
2582 * nd.ni_vp, reference, and exclusively lock it.
2584 if (vap->va_mode == (mode_t)VNOVAL)
2587 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2592 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2593 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2595 error = VOP_GETATTR(vp, vap);
2599 diraft_ret = VOP_GETATTR(dirp, &diraft);
2600 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2603 nfsm_srvpostop_fh(fhp);
2604 nfsm_srvpostop_attr(0, vap);
2606 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2608 nfsm_srvfhtom(fhp, v3);
2609 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2610 nfsm_srvfillattr(vap, fp);
2634 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2635 struct thread *td, struct mbuf **mrq)
2637 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2638 struct sockaddr *nam = nfsd->nd_nam;
2639 caddr_t dpos = nfsd->nd_dpos;
2640 struct ucred *cred = &nfsd->nd_cr;
2644 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2645 int v3 = (nfsd->nd_flag & ND_NFSV3);
2647 struct mbuf *mb, *mreq;
2651 struct vattr dirfor, diraft;
2654 struct nlookupdata nd;
2656 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2662 fhp = &nfh.fh_generic;
2664 nfsm_srvnamesiz(len);
2666 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2667 fhp, len, slp, nam, &md, &dpos, &dirp,
2668 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2671 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2674 nfsm_reply(NFSX_WCCDATA(v3));
2675 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2679 if (vp->v_type != VDIR) {
2685 * The root of a mounted filesystem cannot be deleted.
2687 if (vp->v_flag & VROOT)
2691 * Issue or abort op. Since SAVESTART is not set, path name
2692 * component is freed by the VOP after either.
2699 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2706 diraft_ret = VOP_GETATTR(dirp, &diraft);
2707 nfsm_reply(NFSX_WCCDATA(v3));
2709 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;
2781 struct mbuf *mb, *mb2, *mreq, *mp1, *mp2;
2782 char *cpos, *cend, *cp2, *rbuf;
2783 struct vnode *vp = NULL;
2784 struct mount *mp = 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 off_t *cookies = NULL, *cookiep;
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, &mp, &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);
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(mp, vp, VEXEC, cred, rdonly, td, 0);
2852 nfsm_reply(NFSX_POSTOPATTR(v3));
2853 nfsm_srvpostop_attr(getret, &at);
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;
2875 kfree((caddr_t)cookies, M_TEMP);
2878 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2879 off = (off_t)io.uio_offset;
2880 if (!cookies && !error)
2881 error = NFSERR_PERM;
2883 getret = VOP_GETATTR(vp, &at);
2890 kfree((caddr_t)rbuf, M_TEMP);
2892 kfree((caddr_t)cookies, M_TEMP);
2893 nfsm_reply(NFSX_POSTOPATTR(v3));
2894 nfsm_srvpostop_attr(getret, &at);
2899 siz -= io.uio_resid;
2902 * If nothing read, return eof
2908 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2911 nfsm_srvpostop_attr(getret, &at);
2912 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2913 txdr_hyper(at.va_filerev, tl);
2916 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2919 FREE((caddr_t)rbuf, M_TEMP);
2920 FREE((caddr_t)cookies, M_TEMP);
2927 * Check for degenerate cases of nothing useful read.
2928 * If so go try again
2932 dp = (struct dirent *)cpos;
2935 * For some reason FreeBSD's ufs_readdir() chooses to back the
2936 * directory offset up to a block boundary, so it is necessary to
2937 * skip over the records that preceed the requested offset. This
2938 * requires the assumption that file offset cookies monotonically
2941 while (cpos < cend && ncookies > 0 &&
2942 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
2943 ((u_quad_t)(*cookiep)) <= toff)) {
2944 dp = _DIRENT_NEXT(dp);
2949 if (cpos >= cend || ncookies == 0) {
2955 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2956 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2958 nfsm_srvpostop_attr(getret, &at);
2959 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2960 txdr_hyper(at.va_filerev, tl);
2964 be = bp + M_TRAILINGSPACE(mp1);
2966 /* Loop through the records and build reply */
2967 while (cpos < cend && ncookies > 0) {
2968 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
2969 nlen = dp->d_namlen;
2970 rem = nfsm_rndup(nlen) - nlen;
2971 len += (4 * NFSX_UNSIGNED + nlen + rem);
2973 len += 2 * NFSX_UNSIGNED;
2979 * Build the directory record xdr from
2982 nfsm_clget(mp1, mp2, mb, bp, be, tl);
2984 bp += NFSX_UNSIGNED;
2986 nfsm_clget(mp1, mp2, mb, bp, be, tl);
2988 bp += NFSX_UNSIGNED;
2990 nfsm_clget(mp1, mp2, mb, bp, be, tl);
2991 *tl = txdr_unsigned(dp->d_ino);
2992 bp += NFSX_UNSIGNED;
2993 nfsm_clget(mp1, mp2, mb, bp, be, tl);
2994 *tl = txdr_unsigned(nlen);
2995 bp += NFSX_UNSIGNED;
2997 /* And loop around copying the name */
3001 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3006 bcopy(cp, bp, tsiz);
3012 /* And null pad to a int32_t boundary */
3013 for (i = 0; i < rem; i++)
3015 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3017 /* Finish off the record */
3019 *tl = txdr_unsigned(*cookiep >> 32);
3020 bp += NFSX_UNSIGNED;
3021 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3023 *tl = txdr_unsigned(*cookiep);
3024 bp += NFSX_UNSIGNED;
3026 dp = _DIRENT_NEXT(dp);
3033 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3035 bp += NFSX_UNSIGNED;
3036 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3041 bp += NFSX_UNSIGNED;
3044 mp1->m_len = bp - mtod(mp1, caddr_t);
3046 mp1->m_len += bp - bpos;
3047 FREE((caddr_t)rbuf, M_TEMP);
3048 FREE((caddr_t)cookies, M_TEMP);
3057 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3058 struct thread *td, struct mbuf **mrq)
3060 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3061 struct sockaddr *nam = nfsd->nd_nam;
3062 caddr_t dpos = nfsd->nd_dpos;
3063 struct ucred *cred = &nfsd->nd_cr;
3070 struct mbuf *mb, *mb2, *mreq, *mp1, *mp2;
3071 char *cpos, *cend, *cp2, *rbuf;
3072 struct vnode *vp = NULL, *nvp;
3073 struct mount *mp = NULL;
3076 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3079 struct vattr va, at, *vap = &va;
3080 struct nfs_fattr *fp;
3081 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3082 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3083 u_quad_t off, toff, verf;
3084 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3086 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3087 fhp = &nfh.fh_generic;
3089 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3090 toff = fxdr_hyper(tl);
3092 verf = fxdr_hyper(tl);
3094 siz = fxdr_unsigned(int, *tl++);
3095 cnt = fxdr_unsigned(int, *tl);
3097 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3098 xfer = NFS_SRVMAXDATA(nfsd);
3104 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3105 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3106 if (!error && vp->v_type != VDIR) {
3112 nfsm_reply(NFSX_UNSIGNED);
3113 nfsm_srvpostop_attr(getret, &at);
3117 error = getret = VOP_GETATTR(vp, &at);
3120 * XXX This check may be too strict for Solaris 2.5 clients.
3122 if (!error && toff && verf && verf != at.va_filerev)
3123 error = NFSERR_BAD_COOKIE;
3126 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3131 nfsm_reply(NFSX_V3POSTOPATTR);
3132 nfsm_srvpostop_attr(getret, &at);
3137 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3140 iv.iov_len = fullsiz;
3143 io.uio_offset = (off_t)off;
3144 io.uio_resid = fullsiz;
3145 io.uio_segflg = UIO_SYSSPACE;
3146 io.uio_rw = UIO_READ;
3150 kfree((caddr_t)cookies, M_TEMP);
3153 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3154 off = (u_quad_t)io.uio_offset;
3155 getret = VOP_GETATTR(vp, &at);
3156 if (!cookies && !error)
3157 error = NFSERR_PERM;
3164 kfree((caddr_t)cookies, M_TEMP);
3165 kfree((caddr_t)rbuf, M_TEMP);
3166 nfsm_reply(NFSX_V3POSTOPATTR);
3167 nfsm_srvpostop_attr(getret, &at);
3172 siz -= io.uio_resid;
3175 * If nothing read, return eof
3181 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3183 nfsm_srvpostop_attr(getret, &at);
3184 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3185 txdr_hyper(at.va_filerev, tl);
3189 FREE((caddr_t)cookies, M_TEMP);
3190 FREE((caddr_t)rbuf, M_TEMP);
3197 * Check for degenerate cases of nothing useful read.
3198 * If so go try again
3202 dp = (struct dirent *)cpos;
3205 * For some reason FreeBSD's ufs_readdir() chooses to back the
3206 * directory offset up to a block boundary, so it is necessary to
3207 * skip over the records that preceed the requested offset. This
3208 * requires the assumption that file offset cookies monotonically
3211 while (cpos < cend && ncookies > 0 &&
3212 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3213 ((u_quad_t)(*cookiep)) <= toff)) {
3214 dp = _DIRENT_NEXT(dp);
3219 if (cpos >= cend || ncookies == 0) {
3226 * Probe one of the directory entries to see if the filesystem
3229 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp) == EOPNOTSUPP) {
3230 error = NFSERR_NOTSUPP;
3233 kfree((caddr_t)cookies, M_TEMP);
3234 kfree((caddr_t)rbuf, M_TEMP);
3235 nfsm_reply(NFSX_V3POSTOPATTR);
3236 nfsm_srvpostop_attr(getret, &at);
3245 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3247 nfsm_srvpostop_attr(getret, &at);
3248 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3249 txdr_hyper(at.va_filerev, tl);
3252 be = bp + M_TRAILINGSPACE(mp1);
3254 /* Loop through the records and build reply */
3255 while (cpos < cend && ncookies > 0) {
3256 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3257 nlen = dp->d_namlen;
3258 rem = nfsm_rndup(nlen)-nlen;
3261 * For readdir_and_lookup get the vnode using
3264 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp))
3266 bzero((caddr_t)nfhp, NFSX_V3FH);
3267 nfhp->fh_fsid = fhp->fh_fsid;
3268 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3273 if (VOP_GETATTR(nvp, vap)) {
3282 * If either the dircount or maxcount will be
3283 * exceeded, get out now. Both of these lengths
3284 * are calculated conservatively, including all
3287 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3289 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3290 if (len > cnt || dirlen > fullsiz) {
3296 * Build the directory record xdr from
3299 fp = (struct nfs_fattr *)&fl.fl_fattr;
3300 nfsm_srvfillattr(vap, fp);
3301 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3302 fl.fl_fhok = nfs_true;
3303 fl.fl_postopok = nfs_true;
3304 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3305 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3307 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3309 bp += NFSX_UNSIGNED;
3310 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3312 bp += NFSX_UNSIGNED;
3313 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3314 *tl = txdr_unsigned(dp->d_ino);
3315 bp += NFSX_UNSIGNED;
3316 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3317 *tl = txdr_unsigned(nlen);
3318 bp += NFSX_UNSIGNED;
3320 /* And loop around copying the name */
3324 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3325 if ((bp + xfer) > be)
3329 bcopy(cp, bp, tsiz);
3335 /* And null pad to a int32_t boundary */
3336 for (i = 0; i < rem; i++)
3340 * Now copy the flrep structure out.
3342 xfer = sizeof (struct flrep);
3345 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3346 if ((bp + xfer) > be)
3350 bcopy(cp, bp, tsiz);
3358 dp = _DIRENT_NEXT(dp);
3365 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3367 bp += NFSX_UNSIGNED;
3368 nfsm_clget(mp1, mp2, mb, bp, be, tl);
3373 bp += NFSX_UNSIGNED;
3376 mp1->m_len = bp - mtod(mp1, caddr_t);
3378 mp1->m_len += bp - bpos;
3379 FREE((caddr_t)cookies, M_TEMP);
3380 FREE((caddr_t)rbuf, M_TEMP);
3388 * nfs commit service
3391 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3392 struct thread *td, struct mbuf **mrq)
3394 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3395 struct sockaddr *nam = nfsd->nd_nam;
3396 caddr_t dpos = nfsd->nd_dpos;
3397 struct ucred *cred = &nfsd->nd_cr;
3398 struct vattr bfor, aft;
3399 struct vnode *vp = NULL;
3400 struct mount *mp = NULL;
3406 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3408 struct mbuf *mb, *mb2, *mreq;
3411 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3412 fhp = &nfh.fh_generic;
3414 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3417 * XXX At this time VOP_FSYNC() does not accept offset and byte
3418 * count parameters, so these arguments are useless (someday maybe).
3420 off = fxdr_hyper(tl);
3422 cnt = fxdr_unsigned(int, *tl);
3423 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3424 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3426 nfsm_reply(2 * NFSX_UNSIGNED);
3427 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3431 for_ret = VOP_GETATTR(vp, &bfor);
3433 if (cnt > MAX_COMMIT_COUNT) {
3435 * Give up and do the whole thing
3438 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3439 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3441 error = VOP_FSYNC(vp, MNT_WAIT);
3444 * Locate and synchronously write any buffers that fall
3445 * into the requested range. Note: we are assuming that
3446 * f_iosize is a power of 2.
3448 int iosize = vp->v_mount->mnt_stat.f_iosize;
3449 int iomask = iosize - 1;
3453 * Align to iosize boundry, super-align to page boundry.
3456 cnt += off & iomask;
3457 off &= ~(u_quad_t)iomask;
3459 if (off & PAGE_MASK) {
3460 cnt += off & PAGE_MASK;
3461 off &= ~(u_quad_t)PAGE_MASK;
3466 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3467 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3475 * If we have a buffer and it is marked B_DELWRI we
3476 * have to lock and write it. Otherwise the prior
3477 * write is assumed to have already been committed.
3479 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3480 if (bp->b_flags & B_DELWRI)
3481 bp = findblk(vp, loffset, 0);
3486 if (bp->b_flags & B_DELWRI) {
3503 aft_ret = VOP_GETATTR(vp, &aft);
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;
3544 struct mount *mp = NULL;
3548 struct statfs statfs;
3551 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3552 fhp = &nfh.fh_generic;
3554 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3555 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3557 nfsm_reply(NFSX_UNSIGNED);
3558 nfsm_srvpostop_attr(getret, &at);
3563 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3564 getret = VOP_GETATTR(vp, &at);
3567 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3569 nfsm_srvpostop_attr(getret, &at);
3574 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3576 tval = (u_quad_t)sf->f_blocks;
3577 tval *= (u_quad_t)sf->f_bsize;
3578 txdr_hyper(tval, &sfp->sf_tbytes);
3579 tval = (u_quad_t)sf->f_bfree;
3580 tval *= (u_quad_t)sf->f_bsize;
3581 txdr_hyper(tval, &sfp->sf_fbytes);
3582 tval = (u_quad_t)sf->f_bavail;
3583 tval *= (u_quad_t)sf->f_bsize;
3584 txdr_hyper(tval, &sfp->sf_abytes);
3585 sfp->sf_tfiles.nfsuquad[0] = 0;
3586 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3587 sfp->sf_ffiles.nfsuquad[0] = 0;
3588 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3589 sfp->sf_afiles.nfsuquad[0] = 0;
3590 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3591 sfp->sf_invarsec = 0;
3593 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3594 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3595 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3596 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3597 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3606 * nfs fsinfo service
3609 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3610 struct thread *td, struct mbuf **mrq)
3612 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3613 struct sockaddr *nam = nfsd->nd_nam;
3614 caddr_t dpos = nfsd->nd_dpos;
3615 struct ucred *cred = &nfsd->nd_cr;
3617 struct nfsv3_fsinfo *sip;
3620 int error = 0, rdonly, getret = 1, pref;
3622 struct mbuf *mb, *mb2, *mreq;
3623 struct vnode *vp = NULL;
3624 struct mount *mp = NULL;
3631 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3632 fhp = &nfh.fh_generic;
3634 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3635 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3637 nfsm_reply(NFSX_UNSIGNED);
3638 nfsm_srvpostop_attr(getret, &at);
3643 /* XXX Try to make a guess on the max file size. */
3644 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3645 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3647 getret = VOP_GETATTR(vp, &at);
3650 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3651 nfsm_srvpostop_attr(getret, &at);
3652 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3656 * There should be file system VFS OP(s) to get this information.
3657 * For now, assume ufs.
3659 if (slp->ns_so->so_type == SOCK_DGRAM)
3660 pref = NFS_MAXDGRAMDATA;
3663 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3664 sip->fs_rtpref = txdr_unsigned(pref);
3665 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3666 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3667 sip->fs_wtpref = txdr_unsigned(pref);
3668 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3669 sip->fs_dtpref = txdr_unsigned(pref);
3670 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3671 sip->fs_timedelta.nfsv3_sec = 0;
3672 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3673 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3674 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3675 NFSV3FSINFO_CANSETTIME);
3683 * nfs pathconf service
3686 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3687 struct thread *td, struct mbuf **mrq)
3689 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3690 struct sockaddr *nam = nfsd->nd_nam;
3691 caddr_t dpos = nfsd->nd_dpos;
3692 struct ucred *cred = &nfsd->nd_cr;
3694 struct nfsv3_pathconf *pc;
3697 int error = 0, rdonly, getret = 1;
3698 register_t linkmax, namemax, chownres, notrunc;
3700 struct mbuf *mb, *mb2, *mreq;
3701 struct vnode *vp = NULL;
3702 struct mount *mp = NULL;
3707 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3708 fhp = &nfh.fh_generic;
3710 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3711 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3713 nfsm_reply(NFSX_UNSIGNED);
3714 nfsm_srvpostop_attr(getret, &at);
3718 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3720 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3722 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3724 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3725 getret = VOP_GETATTR(vp, &at);
3728 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3729 nfsm_srvpostop_attr(getret, &at);
3734 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3736 pc->pc_linkmax = txdr_unsigned(linkmax);
3737 pc->pc_namemax = txdr_unsigned(namemax);
3738 pc->pc_notrunc = txdr_unsigned(notrunc);
3739 pc->pc_chownrestricted = txdr_unsigned(chownres);
3742 * These should probably be supported by VOP_PATHCONF(), but
3743 * until msdosfs is exportable (why would you want to?), the
3744 * Unix defaults should be ok.
3746 pc->pc_caseinsensitive = nfs_false;
3747 pc->pc_casepreserving = nfs_true;
3755 * Null operation, used by clients to ping server
3759 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3760 struct thread *td, struct mbuf **mrq)
3762 struct mbuf *mrep = nfsd->nd_mrep;
3764 int error = NFSERR_RETVOID;
3765 struct mbuf *mb, *mreq;
3767 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3773 * No operation, used for obsolete procedures
3777 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3778 struct thread *td, struct mbuf **mrq)
3780 struct mbuf *mrep = nfsd->nd_mrep;
3783 struct mbuf *mb, *mreq;
3785 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3786 if (nfsd->nd_repstat)
3787 error = nfsd->nd_repstat;
3789 error = EPROCUNAVAIL;
3796 * Perform access checking for vnodes obtained from file handles that would
3797 * refer to files already opened by a Unix client. You cannot just use
3798 * vn_writechk() and VOP_ACCESS() for two reasons.
3799 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3800 * 2 - The owner is to be given access irrespective of mode bits for some
3801 * operations, so that processes that chmod after opening a file don't
3802 * break. I don't like this because it opens a security hole, but since
3803 * the nfs server opens a security hole the size of a barn door anyhow,
3806 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3807 * will return EPERM instead of EACCESS. EPERM is always an error.
3810 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3811 int rdonly, struct thread *td, int override)
3816 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3817 if (flags & VWRITE) {
3818 /* Just vn_writechk() changed to check rdonly */
3820 * Disallow write attempts on read-only file systems;
3821 * unless the file is a socket or a block or character
3822 * device resident on the file system.
3825 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3826 switch (vp->v_type) {
3836 * If there's shared text associated with
3837 * the inode, we can't allow writing.
3839 if (vp->v_flag & VTEXT)
3842 error = VOP_GETATTR(vp, &vattr);
3845 error = VOP_ACCESS(vp, flags, cred);
3847 * Allow certain operations for the owner (reads and writes
3848 * on files that are already open).
3850 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3854 #endif /* NFS_NOSERVER */