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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/capability.h>
40 * Functions that perform the vfs operations required by the routines in
41 * nfsd_serv.c. It is hoped that this change will make the server more
45 #include <fs/nfs/nfsport.h>
47 #include <sys/sysctl.h>
48 #include <nlm/nlm_prot.h>
51 FEATURE(nfsd, "NFSv4 server");
53 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54 extern int nfsrv_useacl;
55 extern int newnfs_numnfsd;
56 extern struct mount nfsv4root_mnt;
57 extern struct nfsrv_stablefirst nfsrv_stablefirst;
58 extern void (*nfsd_call_servertimer)(void);
59 extern SVCPOOL *nfsrvd_pool;
60 extern struct nfsv4lock nfsd_suspend_lock;
61 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
63 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
64 struct mtx nfsrc_udpmtx;
65 struct mtx nfs_v4root_mutex;
66 struct nfsrvfh nfs_rootfh, nfs_pubfh;
67 int nfs_pubfhset = 0, nfs_rootfhset = 0;
68 struct proc *nfsd_master_proc = NULL;
69 static pid_t nfsd_master_pid = (pid_t)-1;
70 static char nfsd_master_comm[MAXCOMLEN + 1];
71 static struct timeval nfsd_master_start;
72 static uint32_t nfsv4_sysid = 0;
74 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
77 int nfsrv_enable_crossmntpt = 1;
78 static int nfs_commit_blks;
79 static int nfs_commit_miss;
80 extern int nfsrv_issuedelegs;
81 extern int nfsrv_dolocallocks;
83 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
85 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
86 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
88 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
91 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
92 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
93 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
95 #define MAX_REORDERED_RPC 16
96 #define NUM_HEURISTIC 1031
99 #define NHUSE_MAX 2048
101 static struct nfsheur {
102 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
103 off_t nh_nextoff; /* next offset for sequential detection */
104 int nh_use; /* use count for selection */
105 int nh_seqcount; /* heuristic */
106 } nfsheur[NUM_HEURISTIC];
110 * Heuristic to detect sequential operation.
112 static struct nfsheur *
113 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
118 /* Locate best candidate. */
120 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
123 if (nfsheur[hi].nh_vp == vp) {
127 if (nfsheur[hi].nh_use > 0)
128 --nfsheur[hi].nh_use;
129 hi = (hi + 1) % NUM_HEURISTIC;
130 if (nfsheur[hi].nh_use < nh->nh_use)
134 /* Initialize hint if this is a new file. */
135 if (nh->nh_vp != vp) {
137 nh->nh_nextoff = uio->uio_offset;
138 nh->nh_use = NHUSE_INIT;
139 if (uio->uio_offset == 0)
145 /* Calculate heuristic. */
146 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
147 uio->uio_offset == nh->nh_nextoff) {
148 /* See comments in vfs_vnops.c:sequential_heuristic(). */
149 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
150 if (nh->nh_seqcount > IO_SEQMAX)
151 nh->nh_seqcount = IO_SEQMAX;
152 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
153 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
154 /* Probably a reordered RPC, leave seqcount alone. */
155 } else if (nh->nh_seqcount > 1) {
156 nh->nh_seqcount /= 2;
160 nh->nh_use += NHUSE_INC;
161 if (nh->nh_use > NHUSE_MAX)
162 nh->nh_use = NHUSE_MAX;
167 * Get attributes into nfsvattr structure.
170 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
171 struct thread *p, int vpislocked)
173 int error, lockedit = 0;
175 if (vpislocked == 0) {
177 * When vpislocked == 0, the vnode is either exclusively
178 * locked by this thread or not locked by this thread.
179 * As such, shared lock it, if not exclusively locked.
181 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
183 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
186 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
195 * Get a file handle for a vnode.
198 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
202 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
203 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
204 error = VOP_VPTOFH(vp, &fhp->fh_fid);
211 * Perform access checking for vnodes obtained from file handles that would
212 * refer to files already opened by a Unix client. You cannot just use
213 * vn_writechk() and VOP_ACCESSX() for two reasons.
214 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
216 * 2 - The owner is to be given access irrespective of mode bits for some
217 * operations, so that processes that chmod after opening a file don't
221 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
222 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
223 u_int32_t *supportedtypep)
226 int error = 0, getret = 0;
228 if (vpislocked == 0) {
229 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
234 if (accmode & VWRITE) {
235 /* Just vn_writechk() changed to check rdonly */
237 * Disallow write attempts on read-only file systems;
238 * unless the file is a socket or a block or character
239 * device resident on the file system.
241 if (NFSVNO_EXRDONLY(exp) ||
242 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
243 switch (vp->v_type) {
253 * If there's shared text associated with
254 * the inode, try to free it up once. If
255 * we fail, we can't allow writing.
257 if (VOP_IS_TEXT(vp) && error == 0)
267 * Should the override still be applied when ACLs are enabled?
269 error = VOP_ACCESSX(vp, accmode, cred, p);
270 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
272 * Try again with VEXPLICIT_DENY, to see if the test for
273 * deletion is supported.
275 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
277 if (vp->v_type == VDIR) {
278 accmode &= ~(VDELETE | VDELETE_CHILD);
280 error = VOP_ACCESSX(vp, accmode, cred, p);
281 } else if (supportedtypep != NULL) {
282 *supportedtypep &= ~NFSACCESS_DELETE;
288 * Allow certain operations for the owner (reads and writes
289 * on files that are already open).
291 if (override != NFSACCCHK_NOOVERRIDE &&
292 (error == EPERM || error == EACCES)) {
293 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
295 else if (override & NFSACCCHK_ALLOWOWNER) {
296 getret = VOP_GETATTR(vp, &vattr, cred);
297 if (getret == 0 && cred->cr_uid == vattr.va_uid)
310 * Set attribute(s) vnop.
313 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
314 struct thread *p, struct nfsexstuff *exp)
318 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
324 * Set up nameidata for a lookup() call and do it.
327 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
328 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
329 struct vnode **retdirp)
331 struct componentname *cnp = &ndp->ni_cnd;
335 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
336 int error = 0, crossmnt;
340 cnp->cn_nameptr = cnp->cn_pnbuf;
341 ndp->ni_strictrelative = 0;
343 * Extract and set starting directory.
345 if (dp->v_type != VDIR) {
350 nfsvno_relpathbuf(ndp);
358 if (NFSVNO_EXRDONLY(exp))
359 cnp->cn_flags |= RDONLY;
360 ndp->ni_segflg = UIO_SYSSPACE;
363 if (nd->nd_flag & ND_PUBLOOKUP) {
365 if (cnp->cn_pnbuf[0] == '/') {
368 * Check for degenerate pathnames here, since lookup()
371 for (i = 1; i < ndp->ni_pathlen; i++)
372 if (cnp->cn_pnbuf[i] != '/')
374 if (i == ndp->ni_pathlen) {
375 error = NFSERR_ACCES;
381 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
382 (nd->nd_flag & ND_NFSV4) == 0) {
384 * Only cross mount points for NFSv4 when doing a
385 * mount while traversing the file system above
386 * the mount point, unless nfsrv_enable_crossmntpt is set.
388 cnp->cn_flags |= NOCROSSMOUNT;
393 * Initialize for scan, set ni_startdir and bump ref on dp again
394 * becuase lookup() will dereference ni_startdir.
398 ndp->ni_startdir = dp;
399 ndp->ni_rootdir = rootvnode;
400 ndp->ni_topdir = NULL;
403 cnp->cn_flags |= LOCKLEAF;
405 cnp->cn_nameptr = cnp->cn_pnbuf;
407 * Call lookup() to do the real work. If an error occurs,
408 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
409 * we do not have to dereference anything before returning.
410 * In either case ni_startdir will be dereferenced and NULLed
418 * Check for encountering a symbolic link. Trivial
419 * termination occurs if no symlink encountered.
421 if ((cnp->cn_flags & ISSYMLINK) == 0) {
422 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
423 nfsvno_relpathbuf(ndp);
424 if (ndp->ni_vp && !lockleaf)
425 NFSVOPUNLOCK(ndp->ni_vp, 0);
432 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
433 NFSVOPUNLOCK(ndp->ni_dvp, 0);
434 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
439 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
443 if (ndp->ni_pathlen > 1)
444 cp = uma_zalloc(namei_zone, M_WAITOK);
448 aiov.iov_len = MAXPATHLEN;
449 auio.uio_iov = &aiov;
452 auio.uio_rw = UIO_READ;
453 auio.uio_segflg = UIO_SYSSPACE;
455 auio.uio_resid = MAXPATHLEN;
456 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
459 if (ndp->ni_pathlen > 1)
460 uma_zfree(namei_zone, cp);
466 linklen = MAXPATHLEN - auio.uio_resid;
471 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
472 error = ENAMETOOLONG;
477 * Adjust or replace path
479 if (ndp->ni_pathlen > 1) {
480 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
481 uma_zfree(namei_zone, cnp->cn_pnbuf);
484 cnp->cn_pnbuf[linklen] = '\0';
485 ndp->ni_pathlen += linklen;
488 * Cleanup refs for next loop and check if root directory
489 * should replace current directory. Normally ni_dvp
490 * becomes the new base directory and is cleaned up when
491 * we loop. Explicitly null pointers after invalidation
492 * to clarify operation.
497 if (cnp->cn_pnbuf[0] == '/') {
499 ndp->ni_dvp = ndp->ni_rootdir;
502 ndp->ni_startdir = ndp->ni_dvp;
506 cnp->cn_flags &= ~LOCKLEAF;
510 uma_zfree(namei_zone, cnp->cn_pnbuf);
513 ndp->ni_startdir = NULL;
514 cnp->cn_flags &= ~HASBUF;
515 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
520 NFSEXITCODE2(error, nd);
525 * Set up a pathname buffer and return a pointer to it and, optionally
526 * set a hash pointer.
529 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
531 struct componentname *cnp = &ndp->ni_cnd;
533 cnp->cn_flags |= (NOMACCHECK | HASBUF);
534 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
537 *bufpp = cnp->cn_pnbuf;
541 * Release the above path buffer, if not released by nfsvno_namei().
544 nfsvno_relpathbuf(struct nameidata *ndp)
547 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
549 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
550 ndp->ni_cnd.cn_flags &= ~HASBUF;
554 * Readlink vnode op into an mbuf list.
557 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
558 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
560 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
561 struct iovec *ivp = iv;
562 struct uio io, *uiop = &io;
563 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
564 int i, len, tlen, error = 0;
568 while (len < NFS_MAXPATHLEN) {
571 mp->m_len = NFSMSIZ(mp);
578 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
579 mp->m_len = NFS_MAXPATHLEN - len;
580 len = NFS_MAXPATHLEN;
584 ivp->iov_base = mtod(mp, caddr_t);
585 ivp->iov_len = mp->m_len;
590 uiop->uio_iovcnt = i;
591 uiop->uio_offset = 0;
592 uiop->uio_resid = len;
593 uiop->uio_rw = UIO_READ;
594 uiop->uio_segflg = UIO_SYSSPACE;
596 error = VOP_READLINK(vp, uiop, cred);
602 if (uiop->uio_resid > 0) {
603 len -= uiop->uio_resid;
604 tlen = NFSM_RNDUP(len);
605 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
617 * Read vnode op call into mbuf list.
620 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
621 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
627 int error = 0, len, left, siz, tlen, ioflag = 0;
628 struct mbuf *m2 = NULL, *m3;
629 struct uio io, *uiop = &io;
632 len = left = NFSM_RNDUP(cnt);
635 * Generate the mbuf list with the uio_iov ref. to it.
642 siz = min(M_TRAILINGSPACE(m), left);
651 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
653 uiop->uio_iov = iv2 = iv;
659 panic("nfsvno_read iov");
660 siz = min(M_TRAILINGSPACE(m), left);
662 iv->iov_base = mtod(m, caddr_t) + m->m_len;
671 uiop->uio_iovcnt = i;
672 uiop->uio_offset = off;
673 uiop->uio_resid = len;
674 uiop->uio_rw = UIO_READ;
675 uiop->uio_segflg = UIO_SYSSPACE;
676 nh = nfsrv_sequential_heuristic(uiop, vp);
677 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
678 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
679 FREE((caddr_t)iv2, M_TEMP);
685 nh->nh_nextoff = uiop->uio_offset;
686 tlen = len - uiop->uio_resid;
687 cnt = cnt < tlen ? cnt : tlen;
688 tlen = NFSM_RNDUP(cnt);
692 } else if (len != tlen || tlen != cnt)
693 nfsrv_adj(m3, len - tlen, tlen - cnt);
703 * Write vnode op from an mbuf list.
706 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
707 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
713 struct uio io, *uiop = &io;
716 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
718 uiop->uio_iov = iv = ivp;
719 uiop->uio_iovcnt = cnt;
720 i = mtod(mp, caddr_t) + mp->m_len - cp;
724 panic("nfsvno_write");
735 cp = mtod(mp, caddr_t);
739 if (stable == NFSWRITE_UNSTABLE)
740 ioflags = IO_NODELOCKED;
742 ioflags = (IO_SYNC | IO_NODELOCKED);
743 uiop->uio_resid = retlen;
744 uiop->uio_rw = UIO_WRITE;
745 uiop->uio_segflg = UIO_SYSSPACE;
747 uiop->uio_offset = off;
748 nh = nfsrv_sequential_heuristic(uiop, vp);
749 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
750 error = VOP_WRITE(vp, uiop, ioflags, cred);
752 nh->nh_nextoff = uiop->uio_offset;
753 FREE((caddr_t)iv, M_TEMP);
760 * Common code for creating a regular file (plus special files for V2).
763 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
764 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
765 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
770 error = nd->nd_repstat;
771 if (!error && ndp->ni_vp == NULL) {
772 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
773 vrele(ndp->ni_startdir);
774 error = VOP_CREATE(ndp->ni_dvp,
775 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
777 nfsvno_relpathbuf(ndp);
779 if (*exclusive_flagp) {
780 *exclusive_flagp = 0;
781 NFSVNO_ATTRINIT(nvap);
782 nvap->na_atime.tv_sec = cverf[0];
783 nvap->na_atime.tv_nsec = cverf[1];
784 error = VOP_SETATTR(ndp->ni_vp,
785 &nvap->na_vattr, nd->nd_cred);
789 * NFS V2 Only. nfsrvd_mknod() does this for V3.
790 * (This implies, just get out on an error.)
792 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
793 nvap->na_type == VFIFO) {
794 if (nvap->na_type == VCHR && rdev == 0xffffffff)
795 nvap->na_type = VFIFO;
796 if (nvap->na_type != VFIFO &&
797 (error = priv_check_cred(nd->nd_cred,
798 PRIV_VFS_MKNOD_DEV, 0))) {
799 vrele(ndp->ni_startdir);
800 nfsvno_relpathbuf(ndp);
804 nvap->na_rdev = rdev;
805 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
806 &ndp->ni_cnd, &nvap->na_vattr);
808 nfsvno_relpathbuf(ndp);
809 vrele(ndp->ni_startdir);
813 vrele(ndp->ni_startdir);
814 nfsvno_relpathbuf(ndp);
822 * Handle cases where error is already set and/or
824 * 1 - clean up the lookup
825 * 2 - iff !error and na_size set, truncate it
827 vrele(ndp->ni_startdir);
828 nfsvno_relpathbuf(ndp);
830 if (ndp->ni_dvp == *vpp)
834 if (!error && nvap->na_size != VNOVAL) {
835 error = nfsvno_accchk(*vpp, VWRITE,
836 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
837 NFSACCCHK_VPISLOCKED, NULL);
839 tempsize = nvap->na_size;
840 NFSVNO_ATTRINIT(nvap);
841 nvap->na_size = tempsize;
842 error = VOP_SETATTR(*vpp,
843 &nvap->na_vattr, nd->nd_cred);
856 * Do a mknod vnode op.
859 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
865 vtyp = nvap->na_type;
867 * Iff doesn't exist, create it.
870 vrele(ndp->ni_startdir);
871 nfsvno_relpathbuf(ndp);
877 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
878 vrele(ndp->ni_startdir);
879 nfsvno_relpathbuf(ndp);
881 error = NFSERR_BADTYPE;
885 vrele(ndp->ni_startdir);
886 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
887 &ndp->ni_cnd, &nvap->na_vattr);
889 nfsvno_relpathbuf(ndp);
891 if (nvap->na_type != VFIFO &&
892 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
893 vrele(ndp->ni_startdir);
894 nfsvno_relpathbuf(ndp);
898 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
899 &ndp->ni_cnd, &nvap->na_vattr);
901 nfsvno_relpathbuf(ndp);
902 vrele(ndp->ni_startdir);
904 * Since VOP_MKNOD returns the ni_vp, I can't
905 * see any reason to do the lookup.
918 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
919 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
923 if (ndp->ni_vp != NULL) {
924 if (ndp->ni_dvp == ndp->ni_vp)
929 nfsvno_relpathbuf(ndp);
933 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
936 nfsvno_relpathbuf(ndp);
947 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
948 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
949 struct nfsexstuff *exp)
954 vrele(ndp->ni_startdir);
955 nfsvno_relpathbuf(ndp);
956 if (ndp->ni_dvp == ndp->ni_vp)
965 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
966 &nvap->na_vattr, pathcp);
968 vrele(ndp->ni_startdir);
969 nfsvno_relpathbuf(ndp);
971 * Although FreeBSD still had the lookup code in
972 * it for 7/current, there doesn't seem to be any
973 * point, since VOP_SYMLINK() returns the ni_vp.
974 * Just vput it for v2.
976 if (!not_v2 && !error)
985 * Parse symbolic link arguments.
986 * This function has an ugly side effect. It will MALLOC() an area for
987 * the symlink and set iov_base to point to it, only if it succeeds.
988 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
992 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
993 struct thread *p, char **pathcpp, int *lenp)
998 struct nfsv2_sattr *sp;
1002 if ((nd->nd_flag & ND_NFSV3) &&
1003 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1005 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1006 len = fxdr_unsigned(int, *tl);
1007 if (len > NFS_MAXPATHLEN || len <= 0) {
1011 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1012 error = nfsrv_mtostr(nd, pathcp, len);
1015 if (nd->nd_flag & ND_NFSV2) {
1016 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1017 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1021 NFSEXITCODE2(0, nd);
1025 free(pathcp, M_TEMP);
1026 NFSEXITCODE2(error, nd);
1031 * Remove a non-directory object.
1034 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1035 struct thread *p, struct nfsexstuff *exp)
1041 if (vp->v_type == VDIR)
1042 error = NFSERR_ISDIR;
1044 error = nfsrv_checkremove(vp, 1, p);
1046 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1047 if (ndp->ni_dvp == vp)
1052 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1053 nfsvno_relpathbuf(ndp);
1059 * Remove a directory.
1062 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1063 struct thread *p, struct nfsexstuff *exp)
1069 if (vp->v_type != VDIR) {
1074 * No rmdir "." please.
1076 if (ndp->ni_dvp == vp) {
1081 * The root of a mounted filesystem cannot be deleted.
1083 if (vp->v_vflag & VV_ROOT)
1087 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1088 if (ndp->ni_dvp == vp)
1093 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1094 nfsvno_relpathbuf(ndp);
1103 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1104 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1106 struct vnode *fvp, *tvp, *tdvp;
1109 fvp = fromndp->ni_vp;
1111 vrele(fromndp->ni_dvp);
1116 tdvp = tondp->ni_dvp;
1119 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1120 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1122 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1123 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1126 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1127 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1132 * A rename to '.' or '..' results in a prematurely
1133 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1136 if ((tondp->ni_cnd.cn_namelen == 1 &&
1137 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1138 (tondp->ni_cnd.cn_namelen == 2 &&
1139 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1140 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1145 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1146 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1149 if (fvp->v_mount != tdvp->v_mount) {
1150 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1154 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1159 * If source and destination are the same, there is nothing to
1160 * do. Set error to -1 to indicate this.
1165 if (ndflag & ND_NFSV4) {
1166 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1167 error = nfsrv_checkremove(fvp, 0, p);
1168 NFSVOPUNLOCK(fvp, 0);
1172 error = nfsrv_checkremove(tvp, 1, p);
1175 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1176 * that the NFSv4 client won't be confused by the rename.
1177 * Since nfsd_recalldelegation() can only be called on an
1178 * unlocked vnode at this point and fvp is the file that will
1179 * still exist after the rename, just do fvp.
1181 nfsd_recalldelegation(fvp, p);
1185 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1186 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1195 vrele(fromndp->ni_dvp);
1200 vrele(tondp->ni_startdir);
1201 nfsvno_relpathbuf(tondp);
1203 vrele(fromndp->ni_startdir);
1204 nfsvno_relpathbuf(fromndp);
1213 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1214 struct thread *p, struct nfsexstuff *exp)
1224 if (vp->v_mount != xp->v_mount)
1228 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1229 if ((vp->v_iflag & VI_DOOMED) == 0)
1230 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1233 if (ndp->ni_dvp == vp)
1237 NFSVOPUNLOCK(vp, 0);
1239 if (ndp->ni_dvp == ndp->ni_vp)
1246 nfsvno_relpathbuf(ndp);
1252 * Do the fsync() appropriate for the commit.
1255 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1261 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1262 * file is done. At this time VOP_FSYNC does not accept offset and
1263 * byte count parameters so call VOP_FSYNC the whole file for now.
1264 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1266 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
1268 * Give up and do the whole thing
1271 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1272 VM_OBJECT_LOCK(vp->v_object);
1273 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1274 VM_OBJECT_UNLOCK(vp->v_object);
1276 error = VOP_FSYNC(vp, MNT_WAIT, td);
1279 * Locate and synchronously write any buffers that fall
1280 * into the requested range. Note: we are assuming that
1281 * f_iosize is a power of 2.
1283 int iosize = vp->v_mount->mnt_stat.f_iosize;
1284 int iomask = iosize - 1;
1289 * Align to iosize boundry, super-align to page boundry.
1292 cnt += off & iomask;
1293 off &= ~(u_quad_t)iomask;
1295 if (off & PAGE_MASK) {
1296 cnt += off & PAGE_MASK;
1297 off &= ~(u_quad_t)PAGE_MASK;
1299 lblkno = off / iosize;
1302 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1303 VM_OBJECT_LOCK(vp->v_object);
1304 vm_object_page_clean(vp->v_object, off, off + cnt,
1306 VM_OBJECT_UNLOCK(vp->v_object);
1315 * If we have a buffer and it is marked B_DELWRI we
1316 * have to lock and write it. Otherwise the prior
1317 * write is assumed to have already been committed.
1319 * gbincore() can return invalid buffers now so we
1320 * have to check that bit as well (though B_DELWRI
1321 * should not be set if B_INVAL is set there could be
1322 * a race here since we haven't locked the buffer).
1324 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1325 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1326 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1328 continue; /* retry */
1330 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1333 bp->b_flags &= ~B_ASYNC;
1356 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1360 error = VFS_STATFS(vp->v_mount, sf);
1363 * Since NFS handles these values as unsigned on the
1364 * wire, there is no way to represent negative values,
1365 * so set them to 0. Without this, they will appear
1366 * to be very large positive values for clients like
1369 if (sf->f_bavail < 0)
1371 if (sf->f_ffree < 0)
1379 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1380 * must handle nfsrv_opencheck() calls after any other access checks.
1383 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1384 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1385 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1386 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1387 struct nfsexstuff *exp, struct vnode **vpp)
1389 struct vnode *vp = NULL;
1391 struct nfsexstuff nes;
1393 if (ndp->ni_vp == NULL)
1394 nd->nd_repstat = nfsrv_opencheck(clientid,
1395 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1396 if (!nd->nd_repstat) {
1397 if (ndp->ni_vp == NULL) {
1398 vrele(ndp->ni_startdir);
1399 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1400 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1402 nfsvno_relpathbuf(ndp);
1403 if (!nd->nd_repstat) {
1404 if (*exclusive_flagp) {
1405 *exclusive_flagp = 0;
1406 NFSVNO_ATTRINIT(nvap);
1407 nvap->na_atime.tv_sec = cverf[0];
1408 nvap->na_atime.tv_nsec = cverf[1];
1409 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1410 &nvap->na_vattr, cred);
1412 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1413 aclp, p, attrbitp, exp);
1418 if (ndp->ni_startdir)
1419 vrele(ndp->ni_startdir);
1420 nfsvno_relpathbuf(ndp);
1422 if (create == NFSV4OPEN_CREATE) {
1423 if (ndp->ni_dvp == vp)
1428 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1429 if (ndp->ni_cnd.cn_flags & RDONLY)
1430 NFSVNO_SETEXRDONLY(&nes);
1432 NFSVNO_EXINIT(&nes);
1433 nd->nd_repstat = nfsvno_accchk(vp,
1434 VWRITE, cred, &nes, p,
1435 NFSACCCHK_NOOVERRIDE,
1436 NFSACCCHK_VPISLOCKED, NULL);
1437 nd->nd_repstat = nfsrv_opencheck(clientid,
1438 stateidp, stp, vp, nd, p, nd->nd_repstat);
1439 if (!nd->nd_repstat) {
1440 tempsize = nvap->na_size;
1441 NFSVNO_ATTRINIT(nvap);
1442 nvap->na_size = tempsize;
1443 nd->nd_repstat = VOP_SETATTR(vp,
1444 &nvap->na_vattr, cred);
1446 } else if (vp->v_type == VREG) {
1447 nd->nd_repstat = nfsrv_opencheck(clientid,
1448 stateidp, stp, vp, nd, p, nd->nd_repstat);
1452 if (ndp->ni_cnd.cn_flags & HASBUF)
1453 nfsvno_relpathbuf(ndp);
1454 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1455 vrele(ndp->ni_startdir);
1456 if (ndp->ni_dvp == ndp->ni_vp)
1466 NFSEXITCODE2(0, nd);
1470 * Updates the file rev and sets the mtime and ctime
1471 * to the current clock time, returning the va_filerev and va_Xtime
1475 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1476 struct ucred *cred, struct thread *p)
1481 vfs_timestamp(&va.va_mtime);
1482 (void) VOP_SETATTR(vp, &va, cred);
1483 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1487 * Glue routine to nfsv4_fillattr().
1490 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1491 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1492 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1493 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1497 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1498 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1500 NFSEXITCODE2(0, nd);
1504 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1506 * nfs readdir service
1507 * - mallocs what it thinks is enough to read
1508 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1509 * - calls VOP_READDIR()
1510 * - loops around building the reply
1511 * if the output generated exceeds count break out of loop
1512 * The NFSM_CLGET macro is used here so that the reply will be packed
1513 * tightly in mbuf clusters.
1514 * - it trims out records with d_fileno == 0
1515 * this doesn't matter for Unix clients, but they might confuse clients
1517 * - it trims out records with d_type == DT_WHT
1518 * these cannot be seen through NFS (unless we extend the protocol)
1519 * The alternate call nfsrvd_readdirplus() does lookups as well.
1520 * PS: The NFS protocol spec. does not clarify what the "count" byte
1521 * argument is a count of.. just name strings and file id's or the
1522 * entire reply rpc or ...
1523 * I tried just file name and id sizes and it confused the Sun client,
1524 * so I am using the full rpc size now. The "paranoia.." comment refers
1525 * to including the status longwords that are not a part of the dir.
1526 * "entry" structures, but are in the rpc.
1529 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1530 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1535 char *cpos, *cend, *rbuf;
1537 int nlen, error = 0, getret = 1;
1538 int siz, cnt, fullsiz, eofflag, ncookies;
1539 u_int64_t off, toff, verf;
1540 u_long *cookies = NULL, *cookiep;
1545 if (nd->nd_repstat) {
1546 nfsrv_postopattr(nd, getret, &at);
1549 if (nd->nd_flag & ND_NFSV2) {
1550 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1551 off = fxdr_unsigned(u_quad_t, *tl++);
1553 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1554 off = fxdr_hyper(tl);
1556 verf = fxdr_hyper(tl);
1560 cnt = fxdr_unsigned(int, *tl);
1561 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1562 cnt = NFS_SRVMAXDATA(nd);
1563 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1565 if (nd->nd_flag & ND_NFSV3) {
1566 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1570 * va_filerev is not sufficient as a cookie verifier,
1571 * since it is not supposed to change when entries are
1572 * removed/added unless that offset cookies returned to
1573 * the client are no longer valid.
1575 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1576 nd->nd_repstat = NFSERR_BAD_COOKIE;
1579 if (!nd->nd_repstat && vp->v_type != VDIR)
1580 nd->nd_repstat = NFSERR_NOTDIR;
1581 if (nd->nd_repstat == 0 && cnt == 0) {
1582 if (nd->nd_flag & ND_NFSV2)
1583 /* NFSv2 does not have NFSERR_TOOSMALL */
1584 nd->nd_repstat = EPERM;
1586 nd->nd_repstat = NFSERR_TOOSMALL;
1588 if (!nd->nd_repstat)
1589 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1590 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1591 NFSACCCHK_VPISLOCKED, NULL);
1592 if (nd->nd_repstat) {
1594 if (nd->nd_flag & ND_NFSV3)
1595 nfsrv_postopattr(nd, getret, &at);
1598 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1599 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1603 free((caddr_t)cookies, M_TEMP);
1611 io.uio_offset = (off_t)off;
1613 io.uio_segflg = UIO_SYSSPACE;
1614 io.uio_rw = UIO_READ;
1616 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1618 off = (u_int64_t)io.uio_offset;
1620 siz -= io.uio_resid;
1622 if (!cookies && !nd->nd_repstat)
1623 nd->nd_repstat = NFSERR_PERM;
1624 if (nd->nd_flag & ND_NFSV3) {
1625 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1626 if (!nd->nd_repstat)
1627 nd->nd_repstat = getret;
1631 * Handles the failed cases. nd->nd_repstat == 0 past here.
1633 if (nd->nd_repstat) {
1635 free((caddr_t)rbuf, M_TEMP);
1637 free((caddr_t)cookies, M_TEMP);
1638 if (nd->nd_flag & ND_NFSV3)
1639 nfsrv_postopattr(nd, getret, &at);
1643 * If nothing read, return eof
1648 if (nd->nd_flag & ND_NFSV2) {
1649 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1651 nfsrv_postopattr(nd, getret, &at);
1652 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1653 txdr_hyper(at.na_filerev, tl);
1656 *tl++ = newnfs_false;
1658 FREE((caddr_t)rbuf, M_TEMP);
1659 FREE((caddr_t)cookies, M_TEMP);
1664 * Check for degenerate cases of nothing useful read.
1665 * If so go try again
1669 dp = (struct dirent *)cpos;
1673 * For some reason FreeBSD's ufs_readdir() chooses to back the
1674 * directory offset up to a block boundary, so it is necessary to
1675 * skip over the records that precede the requested offset. This
1676 * requires the assumption that file offset cookies monotonically
1678 * Since the offset cookies don't monotonically increase for ZFS,
1679 * this is not done when ZFS is the file system.
1681 while (cpos < cend && ncookies > 0 &&
1682 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1683 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1684 cpos += dp->d_reclen;
1685 dp = (struct dirent *)cpos;
1689 if (cpos >= cend || ncookies == 0) {
1697 * dirlen is the size of the reply, including all XDR and must
1698 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1699 * if the XDR should be included in "count", but to be safe, we do.
1700 * (Include the two booleans at the end of the reply in dirlen now.)
1702 if (nd->nd_flag & ND_NFSV3) {
1703 nfsrv_postopattr(nd, getret, &at);
1704 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1705 txdr_hyper(at.na_filerev, tl);
1706 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1708 dirlen = 2 * NFSX_UNSIGNED;
1711 /* Loop through the records and build reply */
1712 while (cpos < cend && ncookies > 0) {
1713 nlen = dp->d_namlen;
1714 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1715 nlen <= NFS_MAXNAMLEN) {
1716 if (nd->nd_flag & ND_NFSV3)
1717 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1719 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1726 * Build the directory record xdr from
1729 if (nd->nd_flag & ND_NFSV3) {
1730 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1731 *tl++ = newnfs_true;
1734 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1735 *tl++ = newnfs_true;
1737 *tl = txdr_unsigned(dp->d_fileno);
1738 (void) nfsm_strtom(nd, dp->d_name, nlen);
1739 if (nd->nd_flag & ND_NFSV3) {
1740 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1743 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1744 *tl = txdr_unsigned(*cookiep);
1746 cpos += dp->d_reclen;
1747 dp = (struct dirent *)cpos;
1753 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1754 *tl++ = newnfs_false;
1759 FREE((caddr_t)rbuf, M_TEMP);
1760 FREE((caddr_t)cookies, M_TEMP);
1763 NFSEXITCODE2(0, nd);
1767 NFSEXITCODE2(error, nd);
1772 * Readdirplus for V3 and Readdir for V4.
1775 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1776 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1781 char *cpos, *cend, *rbuf;
1784 struct nfsvattr nva, at, *nvap = &nva;
1785 struct mbuf *mb0, *mb1;
1786 struct nfsreferral *refp;
1787 int nlen, r, error = 0, getret = 1, usevget = 1;
1788 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1789 caddr_t bpos0, bpos1;
1790 u_int64_t off, toff, verf;
1791 u_long *cookies = NULL, *cookiep;
1792 nfsattrbit_t attrbits, rderrbits, savbits;
1795 struct componentname cn;
1796 int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1797 struct mount *mp, *new_mp;
1798 uint64_t mounted_on_fileno;
1800 if (nd->nd_repstat) {
1801 nfsrv_postopattr(nd, getret, &at);
1804 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1805 off = fxdr_hyper(tl);
1808 verf = fxdr_hyper(tl);
1810 siz = fxdr_unsigned(int, *tl++);
1811 cnt = fxdr_unsigned(int, *tl);
1814 * Use the server's maximum data transfer size as the upper bound
1817 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1818 cnt = NFS_SRVMAXDATA(nd);
1821 * siz is a "hint" of how much directory information (name, fileid,
1822 * cookie) should be in the reply. At least one client "hints" 0,
1823 * so I set it to cnt for that case. I also round it up to the
1824 * next multiple of DIRBLKSIZ.
1828 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1830 if (nd->nd_flag & ND_NFSV4) {
1831 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1834 NFSSET_ATTRBIT(&savbits, &attrbits);
1835 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1836 NFSZERO_ATTRBIT(&rderrbits);
1837 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1839 NFSZERO_ATTRBIT(&attrbits);
1842 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1843 if (!nd->nd_repstat) {
1844 if (off && verf != at.na_filerev) {
1846 * va_filerev is not sufficient as a cookie verifier,
1847 * since it is not supposed to change when entries are
1848 * removed/added unless that offset cookies returned to
1849 * the client are no longer valid.
1852 if (nd->nd_flag & ND_NFSV4) {
1853 nd->nd_repstat = NFSERR_NOTSAME;
1855 nd->nd_repstat = NFSERR_BAD_COOKIE;
1858 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1859 nd->nd_repstat = NFSERR_BAD_COOKIE;
1862 if (!nd->nd_repstat && vp->v_type != VDIR)
1863 nd->nd_repstat = NFSERR_NOTDIR;
1864 if (!nd->nd_repstat && cnt == 0)
1865 nd->nd_repstat = NFSERR_TOOSMALL;
1866 if (!nd->nd_repstat)
1867 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1868 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1869 NFSACCCHK_VPISLOCKED, NULL);
1870 if (nd->nd_repstat) {
1872 if (nd->nd_flag & ND_NFSV3)
1873 nfsrv_postopattr(nd, getret, &at);
1876 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1878 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1882 free((caddr_t)cookies, M_TEMP);
1890 io.uio_offset = (off_t)off;
1892 io.uio_segflg = UIO_SYSSPACE;
1893 io.uio_rw = UIO_READ;
1895 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1897 off = (u_int64_t)io.uio_offset;
1899 siz -= io.uio_resid;
1901 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1903 if (!cookies && !nd->nd_repstat)
1904 nd->nd_repstat = NFSERR_PERM;
1905 if (!nd->nd_repstat)
1906 nd->nd_repstat = getret;
1907 if (nd->nd_repstat) {
1910 free((caddr_t)cookies, M_TEMP);
1911 free((caddr_t)rbuf, M_TEMP);
1912 if (nd->nd_flag & ND_NFSV3)
1913 nfsrv_postopattr(nd, getret, &at);
1917 * If nothing read, return eof
1922 if (nd->nd_flag & ND_NFSV3)
1923 nfsrv_postopattr(nd, getret, &at);
1924 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1925 txdr_hyper(at.na_filerev, tl);
1927 *tl++ = newnfs_false;
1929 free((caddr_t)cookies, M_TEMP);
1930 free((caddr_t)rbuf, M_TEMP);
1935 * Check for degenerate cases of nothing useful read.
1936 * If so go try again
1940 dp = (struct dirent *)cpos;
1944 * For some reason FreeBSD's ufs_readdir() chooses to back the
1945 * directory offset up to a block boundary, so it is necessary to
1946 * skip over the records that precede the requested offset. This
1947 * requires the assumption that file offset cookies monotonically
1949 * Since the offset cookies don't monotonically increase for ZFS,
1950 * this is not done when ZFS is the file system.
1952 while (cpos < cend && ncookies > 0 &&
1953 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1954 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1955 ((nd->nd_flag & ND_NFSV4) &&
1956 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1957 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1958 cpos += dp->d_reclen;
1959 dp = (struct dirent *)cpos;
1963 if (cpos >= cend || ncookies == 0) {
1970 * Busy the file system so that the mount point won't go away
1971 * and, as such, VFS_VGET() can be used safely.
1975 NFSVOPUNLOCK(vp, 0);
1976 nd->nd_repstat = vfs_busy(mp, 0);
1978 if (nd->nd_repstat != 0) {
1980 free(cookies, M_TEMP);
1982 if (nd->nd_flag & ND_NFSV3)
1983 nfsrv_postopattr(nd, getret, &at);
1988 * Save this position, in case there is an error before one entry
1992 bpos0 = nd->nd_bpos;
1995 * Fill in the first part of the reply.
1996 * dirlen is the reply length in bytes and cannot exceed cnt.
1997 * (Include the two booleans at the end of the reply in dirlen now,
1998 * so we recognize when we have exceeded cnt.)
2000 if (nd->nd_flag & ND_NFSV3) {
2001 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2002 nfsrv_postopattr(nd, getret, &at);
2004 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2006 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2007 txdr_hyper(at.na_filerev, tl);
2010 * Save this position, in case there is an empty reply needed.
2013 bpos1 = nd->nd_bpos;
2015 /* Loop through the records and build reply */
2017 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2018 nlen = dp->d_namlen;
2019 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2020 nlen <= NFS_MAXNAMLEN &&
2021 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2022 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2023 || (nlen == 1 && dp->d_name[0] != '.'))) {
2025 * Save the current position in the reply, in case
2026 * this entry exceeds cnt.
2029 bpos1 = nd->nd_bpos;
2032 * For readdir_and_lookup get the vnode using
2041 mounted_on_fileno = (uint64_t)dp->d_fileno;
2042 if ((nd->nd_flag & ND_NFSV3) ||
2043 NFSNONZERO_ATTRBIT(&savbits)) {
2044 if (nd->nd_flag & ND_NFSV4)
2045 refp = nfsv4root_getreferral(NULL,
2049 r = VFS_VGET(mp, dp->d_fileno,
2053 if (r == EOPNOTSUPP) {
2056 cn.cn_nameiop = LOOKUP;
2064 cn.cn_nameptr = dp->d_name;
2065 cn.cn_namelen = nlen;
2066 cn.cn_flags = ISLASTCN |
2067 NOFOLLOW | LOCKLEAF |
2070 dp->d_name[0] == '.' &&
2071 dp->d_name[1] == '.')
2074 if (NFSVOPLOCK(vp, LK_SHARED)
2076 nd->nd_repstat = EPERM;
2079 if ((vp->v_vflag & VV_ROOT) != 0
2080 && (cn.cn_flags & ISDOTDOT)
2086 r = VOP_LOOKUP(vp, &nvp,
2095 * For NFSv4, check to see if nvp is
2096 * a mount point and get the mount
2097 * point vnode, as required.
2100 nfsrv_enable_crossmntpt != 0 &&
2101 (nd->nd_flag & ND_NFSV4) != 0 &&
2102 nvp->v_type == VDIR &&
2103 nvp->v_mountedhere != NULL) {
2104 new_mp = nvp->v_mountedhere;
2105 r = vfs_busy(new_mp, 0);
2109 r = VFS_ROOT(new_mp,
2119 ((nd->nd_flag & ND_NFSV3) ||
2120 NFSNONZERO_ATTRBIT(&attrbits))) {
2121 r = nfsvno_getfh(nvp, &nfh, p);
2123 r = nfsvno_getattr(nvp, nvap,
2130 if (!NFSISSET_ATTRBIT(&attrbits,
2131 NFSATTRBIT_RDATTRERROR)) {
2134 if (needs_unbusy != 0)
2143 * Build the directory record xdr
2145 if (nd->nd_flag & ND_NFSV3) {
2146 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2147 *tl++ = newnfs_true;
2149 *tl = txdr_unsigned(dp->d_fileno);
2150 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2151 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2153 *tl = txdr_unsigned(*cookiep);
2154 nfsrv_postopattr(nd, 0, nvap);
2155 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2156 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2160 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2161 *tl++ = newnfs_true;
2163 *tl = txdr_unsigned(*cookiep);
2164 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2166 supports_nfsv4acls =
2167 nfs_supportsnfsv4acls(nvp);
2168 NFSVOPUNLOCK(nvp, 0);
2170 supports_nfsv4acls = 0;
2172 dirlen += nfsrv_putreferralattr(nd,
2175 if (nd->nd_repstat) {
2178 if (needs_unbusy != 0)
2183 dirlen += nfsvno_fillattr(nd, new_mp,
2184 nvp, nvap, &nfh, r, &rderrbits,
2185 nd->nd_cred, p, isdgram, 0,
2186 supports_nfsv4acls, at_root,
2189 dirlen += nfsvno_fillattr(nd, new_mp,
2190 nvp, nvap, &nfh, r, &attrbits,
2191 nd->nd_cred, p, isdgram, 0,
2192 supports_nfsv4acls, at_root,
2197 dirlen += (3 * NFSX_UNSIGNED);
2199 if (needs_unbusy != 0)
2204 cpos += dp->d_reclen;
2205 dp = (struct dirent *)cpos;
2213 * If dirlen > cnt, we must strip off the last entry. If that
2214 * results in an empty reply, report NFSERR_TOOSMALL.
2216 if (dirlen > cnt || nd->nd_repstat) {
2217 if (!nd->nd_repstat && entrycnt == 0)
2218 nd->nd_repstat = NFSERR_TOOSMALL;
2220 newnfs_trimtrailing(nd, mb0, bpos0);
2222 newnfs_trimtrailing(nd, mb1, bpos1);
2224 } else if (cpos < cend)
2226 if (!nd->nd_repstat) {
2227 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2228 *tl++ = newnfs_false;
2234 FREE((caddr_t)cookies, M_TEMP);
2235 FREE((caddr_t)rbuf, M_TEMP);
2238 NFSEXITCODE2(0, nd);
2242 NFSEXITCODE2(error, nd);
2247 * Get the settable attributes out of the mbuf list.
2248 * (Return 0 or EBADRPC)
2251 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2252 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2255 struct nfsv2_sattr *sp;
2256 int error = 0, toclient = 0;
2258 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2260 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2262 * Some old clients didn't fill in the high order 16bits.
2263 * --> check the low order 2 bytes for 0xffff
2265 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2266 nvap->na_mode = nfstov_mode(sp->sa_mode);
2267 if (sp->sa_uid != newnfs_xdrneg1)
2268 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2269 if (sp->sa_gid != newnfs_xdrneg1)
2270 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2271 if (sp->sa_size != newnfs_xdrneg1)
2272 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2273 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2275 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2277 nvap->na_atime.tv_sec =
2278 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2279 nvap->na_atime.tv_nsec = 0;
2282 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2283 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2286 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2287 if (*tl == newnfs_true) {
2288 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2289 nvap->na_mode = nfstov_mode(*tl);
2291 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2292 if (*tl == newnfs_true) {
2293 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2294 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2296 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2297 if (*tl == newnfs_true) {
2298 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2299 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2301 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2302 if (*tl == newnfs_true) {
2303 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2304 nvap->na_size = fxdr_hyper(tl);
2306 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2307 switch (fxdr_unsigned(int, *tl)) {
2308 case NFSV3SATTRTIME_TOCLIENT:
2309 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2310 fxdr_nfsv3time(tl, &nvap->na_atime);
2313 case NFSV3SATTRTIME_TOSERVER:
2314 vfs_timestamp(&nvap->na_atime);
2315 nvap->na_vaflags |= VA_UTIMES_NULL;
2318 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2319 switch (fxdr_unsigned(int, *tl)) {
2320 case NFSV3SATTRTIME_TOCLIENT:
2321 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2322 fxdr_nfsv3time(tl, &nvap->na_mtime);
2323 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2325 case NFSV3SATTRTIME_TOSERVER:
2326 vfs_timestamp(&nvap->na_mtime);
2328 nvap->na_vaflags |= VA_UTIMES_NULL;
2333 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2336 NFSEXITCODE2(error, nd);
2341 * Handle the setable attributes for V4.
2342 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2345 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2346 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2351 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2353 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2357 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2360 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2361 attrsize = fxdr_unsigned(int, *tl);
2364 * Loop around getting the setable attributes. If an unsupported
2365 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2368 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2369 bitpos = NFSATTRBIT_MAX;
2373 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2374 if (attrsum > attrsize) {
2375 error = NFSERR_BADXDR;
2378 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2380 case NFSATTRBIT_SIZE:
2381 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2382 nvap->na_size = fxdr_hyper(tl);
2383 attrsum += NFSX_HYPER;
2385 case NFSATTRBIT_ACL:
2386 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2390 if (aceerr && !nd->nd_repstat)
2391 nd->nd_repstat = aceerr;
2394 case NFSATTRBIT_ARCHIVE:
2395 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2396 if (!nd->nd_repstat)
2397 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2398 attrsum += NFSX_UNSIGNED;
2400 case NFSATTRBIT_HIDDEN:
2401 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2402 if (!nd->nd_repstat)
2403 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2404 attrsum += NFSX_UNSIGNED;
2406 case NFSATTRBIT_MIMETYPE:
2407 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2408 i = fxdr_unsigned(int, *tl);
2409 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2412 if (!nd->nd_repstat)
2413 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2414 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2416 case NFSATTRBIT_MODE:
2417 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2418 nvap->na_mode = nfstov_mode(*tl);
2419 attrsum += NFSX_UNSIGNED;
2421 case NFSATTRBIT_OWNER:
2422 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2423 j = fxdr_unsigned(int, *tl);
2425 error = NFSERR_BADXDR;
2428 if (j > NFSV4_SMALLSTR)
2429 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2432 error = nfsrv_mtostr(nd, cp, j);
2434 if (j > NFSV4_SMALLSTR)
2435 free(cp, M_NFSSTRING);
2438 if (!nd->nd_repstat) {
2439 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2441 if (!nd->nd_repstat)
2444 if (j > NFSV4_SMALLSTR)
2445 free(cp, M_NFSSTRING);
2446 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2448 case NFSATTRBIT_OWNERGROUP:
2449 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2450 j = fxdr_unsigned(int, *tl);
2452 error = NFSERR_BADXDR;
2455 if (j > NFSV4_SMALLSTR)
2456 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2459 error = nfsrv_mtostr(nd, cp, j);
2461 if (j > NFSV4_SMALLSTR)
2462 free(cp, M_NFSSTRING);
2465 if (!nd->nd_repstat) {
2466 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2468 if (!nd->nd_repstat)
2471 if (j > NFSV4_SMALLSTR)
2472 free(cp, M_NFSSTRING);
2473 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2475 case NFSATTRBIT_SYSTEM:
2476 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2477 if (!nd->nd_repstat)
2478 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2479 attrsum += NFSX_UNSIGNED;
2481 case NFSATTRBIT_TIMEACCESSSET:
2482 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2483 attrsum += NFSX_UNSIGNED;
2484 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2485 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2486 fxdr_nfsv4time(tl, &nvap->na_atime);
2488 attrsum += NFSX_V4TIME;
2490 vfs_timestamp(&nvap->na_atime);
2491 nvap->na_vaflags |= VA_UTIMES_NULL;
2494 case NFSATTRBIT_TIMEBACKUP:
2495 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2496 if (!nd->nd_repstat)
2497 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2498 attrsum += NFSX_V4TIME;
2500 case NFSATTRBIT_TIMECREATE:
2501 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2502 if (!nd->nd_repstat)
2503 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2504 attrsum += NFSX_V4TIME;
2506 case NFSATTRBIT_TIMEMODIFYSET:
2507 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2508 attrsum += NFSX_UNSIGNED;
2509 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2510 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2511 fxdr_nfsv4time(tl, &nvap->na_mtime);
2512 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2513 attrsum += NFSX_V4TIME;
2515 vfs_timestamp(&nvap->na_mtime);
2517 nvap->na_vaflags |= VA_UTIMES_NULL;
2521 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2523 * set bitpos so we drop out of the loop.
2525 bitpos = NFSATTRBIT_MAX;
2531 * some clients pad the attrlist, so we need to skip over the
2534 if (attrsum > attrsize) {
2535 error = NFSERR_BADXDR;
2537 attrsize = NFSM_RNDUP(attrsize);
2538 if (attrsum < attrsize)
2539 error = nfsm_advance(nd, attrsize - attrsum, -1);
2542 NFSEXITCODE2(error, nd);
2547 * Check/setup export credentials.
2550 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2551 struct ucred *credanon)
2556 * Check/setup credentials.
2558 if (nd->nd_flag & ND_GSS)
2559 exp->nes_exflag &= ~MNT_EXPORTANON;
2562 * Check to see if the operation is allowed for this security flavor.
2563 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2564 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2565 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2567 if (nfsvno_testexp(nd, exp) &&
2568 nd->nd_procnum != NFSV4OP_SECINFO &&
2569 nd->nd_procnum != NFSPROC_FSINFO) {
2570 if (nd->nd_flag & ND_NFSV4)
2571 error = NFSERR_WRONGSEC;
2573 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2578 * Check to see if the file system is exported V4 only.
2580 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2581 error = NFSERR_PROGNOTV4;
2586 * Now, map the user credentials.
2587 * (Note that ND_AUTHNONE will only be set for an NFSv3
2588 * Fsinfo RPC. If set for anything else, this code might need
2591 if (NFSVNO_EXPORTED(exp) &&
2592 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2593 NFSVNO_EXPORTANON(exp) ||
2594 (nd->nd_flag & ND_AUTHNONE))) {
2595 nd->nd_cred->cr_uid = credanon->cr_uid;
2596 nd->nd_cred->cr_gid = credanon->cr_gid;
2597 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2598 credanon->cr_groups);
2602 NFSEXITCODE2(error, nd);
2610 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2611 struct ucred **credp)
2613 int i, error, *secflavors;
2615 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2616 &exp->nes_numsecflavor, &secflavors);
2618 if (nfs_rootfhset) {
2619 exp->nes_exflag = 0;
2620 exp->nes_numsecflavor = 0;
2624 /* Copy the security flavors. */
2625 for (i = 0; i < exp->nes_numsecflavor; i++)
2626 exp->nes_secflavors[i] = secflavors[i];
2633 * Get a vnode for a file handle and export stuff.
2636 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2637 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2638 struct ucred **credp)
2640 int i, error, *secflavors;
2643 exp->nes_numsecflavor = 0;
2644 if (VFS_NEEDSGIANT(mp))
2647 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp);
2649 /* Make sure the server replies ESTALE to the client. */
2651 if (nam && !error) {
2652 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2653 &exp->nes_numsecflavor, &secflavors);
2655 if (nfs_rootfhset) {
2656 exp->nes_exflag = 0;
2657 exp->nes_numsecflavor = 0;
2663 /* Copy the security flavors. */
2664 for (i = 0; i < exp->nes_numsecflavor; i++)
2665 exp->nes_secflavors[i] = secflavors[i];
2668 if (error == 0 && lktype == LK_SHARED)
2670 * It would be much better to pass lktype to VFS_FHTOVP(),
2671 * but this will have to do until VFS_FHTOVP() has a lock
2672 * type argument like VFS_VGET().
2674 NFSVOPLOCK(*vpp, LK_DOWNGRADE | LK_RETRY);
2681 * nfsd_fhtovp() - convert a fh to a vnode ptr
2682 * - look up fsid in mount list (if not found ret error)
2683 * - get vp and export rights by calling nfsvno_fhtovp()
2684 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2686 * - if mpp != NULL, return the mount point so that it can
2687 * be used for vn_finished_write() by the caller
2690 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2691 struct vnode **vpp, struct nfsexstuff *exp,
2692 struct mount **mpp, int startwrite, struct thread *p)
2695 struct ucred *credanon;
2698 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2700 * Check for the special case of the nfsv4root_fh.
2702 mp = vfs_busyfs(&fhp->fh_fsid);
2707 nd->nd_repstat = ESTALE;
2712 vn_start_write(NULL, mpp, V_WAIT);
2713 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2714 lktype = LK_EXCLUSIVE;
2716 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2721 * For NFSv4 without a pseudo root fs, unexported file handles
2722 * can be returned, so that Lookup works everywhere.
2724 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2725 !(nd->nd_flag & ND_NFSV4)) {
2727 nd->nd_repstat = EACCES;
2731 * Personally, I've never seen any point in requiring a
2732 * reserved port#, since only in the rare case where the
2733 * clients are all boxes with secure system priviledges,
2734 * does it provide any enhanced security, but... some people
2735 * believe it to be useful and keep putting this code back in.
2736 * (There is also some "security checker" out there that
2737 * complains if the nfs server doesn't enforce this.)
2738 * However, note the following:
2739 * RFC3530 (NFSv4) specifies that a reserved port# not be
2741 * RFC2623 recommends that, if a reserved port# is checked for,
2742 * that there be a way to turn that off--> ifdef'd.
2744 #ifdef NFS_REQRSVPORT
2745 if (!nd->nd_repstat) {
2746 struct sockaddr_in *saddr;
2747 struct sockaddr_in6 *saddr6;
2749 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2750 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2751 if (!(nd->nd_flag & ND_NFSV4) &&
2752 ((saddr->sin_family == AF_INET &&
2753 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2754 (saddr6->sin6_family == AF_INET6 &&
2755 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2757 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2760 #endif /* NFS_REQRSVPORT */
2763 * Check/setup credentials.
2765 if (!nd->nd_repstat) {
2766 nd->nd_saveduid = nd->nd_cred->cr_uid;
2767 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2771 if (credanon != NULL)
2773 if (nd->nd_repstat) {
2775 vn_finished_write(mp);
2782 NFSEXITCODE2(0, nd);
2789 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2791 struct filedesc *fdp;
2795 fdp = p->td_proc->p_fd;
2796 if (fd >= fdp->fd_nfiles ||
2797 (fp = fdp->fd_ofiles[fd]) == NULL) {
2809 * Called from nfssvc() to update the exports list. Just call
2810 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2811 * in the mount list.
2814 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2816 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2818 struct nameidata nd;
2821 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2822 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2824 else if (error == 0) {
2825 if (nfsexargp->fspec == NULL) {
2830 * If fspec != NULL, this is the v4root path.
2832 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2833 nfsexargp->fspec, p);
2834 if ((error = namei(&nd)) != 0)
2836 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2839 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2840 NFSBCOPY((caddr_t)&fh,
2841 nfs_rootfh.nfsrvfh_data,
2842 sizeof (fhandle_t));
2853 * Get the tcp socket sequence numbers we need.
2854 * (Maybe this should be moved to the tcp sources?)
2857 nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2863 inp = sotoinpcb(so);
2864 KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2866 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2871 tp = intotcpcb(inp);
2872 if (tp->t_state != TCPS_ESTABLISHED) {
2877 *maxp = tp->snd_max;
2878 *unap = tp->snd_una;
2887 * This function needs to test to see if the system is near its limit
2888 * for memory allocation via malloc() or mget() and return True iff
2889 * either of these resources are near their limit.
2890 * XXX (For now, this is just a stub.)
2892 int nfsrv_testmalloclimit = 0;
2894 nfsrv_mallocmget_limit(void)
2896 static int printmesg = 0;
2897 static int testval = 1;
2899 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2900 if ((printmesg++ % 100) == 0)
2901 printf("nfsd: malloc/mget near limit\n");
2908 * BSD specific initialization of a mount point.
2913 static int inited = 0;
2918 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2919 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2920 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2921 nfsv4root_mnt.mnt_export = NULL;
2922 TAILQ_INIT(&nfsv4root_opt);
2923 TAILQ_INIT(&nfsv4root_newopt);
2924 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2925 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2926 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2927 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2931 * Get a vnode for a file handle, without checking exports, etc.
2934 nfsvno_getvp(fhandle_t *fhp)
2940 mp = vfs_busyfs(&fhp->fh_fsid);
2943 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2951 * Do a local VOP_ADVLOCK().
2954 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2955 u_int64_t end, struct thread *td)
2961 if (nfsrv_dolocallocks == 0)
2964 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2965 if ((vp->v_iflag & VI_DOOMED) != 0) {
2970 fl.l_whence = SEEK_SET;
2972 fl.l_start = (off_t)first;
2973 if (end == NFS64BITSSET) {
2977 fl.l_len = (off_t)tlen;
2980 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2981 * values for all calls, so that all locks will be held by the
2982 * nfsd server. (The nfsd server handles conflicts between the
2984 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2985 * bytes, so it can't be put in l_sysid.
2987 if (nfsv4_sysid == 0)
2988 nfsv4_sysid = nlm_acquire_next_sysid();
2989 fl.l_pid = (pid_t)0;
2990 fl.l_sysid = (int)nfsv4_sysid;
2992 NFSVOPUNLOCK(vp, 0);
2993 if (ftype == F_UNLCK)
2994 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2995 (F_POSIX | F_REMOTE));
2997 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2998 (F_POSIX | F_REMOTE));
2999 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3007 * Check the nfsv4 root exports.
3010 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3012 struct ucred *credanon;
3013 int exflags, error = 0, numsecflavor, *secflavors, i;
3015 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3016 &credanon, &numsecflavor, &secflavors);
3018 error = NFSERR_PROGUNAVAIL;
3021 if (credanon != NULL)
3023 for (i = 0; i < numsecflavor; i++) {
3024 if (secflavors[i] == AUTH_SYS)
3025 nd->nd_flag |= ND_EXAUTHSYS;
3026 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3027 nd->nd_flag |= ND_EXGSS;
3028 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3029 nd->nd_flag |= ND_EXGSSINTEGRITY;
3030 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3031 nd->nd_flag |= ND_EXGSSPRIVACY;
3040 * Nfs server psuedo system call for the nfsd's
3046 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3049 struct nfsd_addsock_args sockarg;
3050 struct nfsd_nfsd_args nfsdarg;
3053 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3054 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3058 * Since we don't know what rights might be required,
3059 * pretend that we need them all. It is better to be too
3060 * careful than too reckless.
3062 if ((error = fget(td, sockarg.sock, CAP_SOCK_ALL, &fp)) != 0)
3064 if (fp->f_type != DTYPE_SOCKET) {
3069 error = nfsrvd_addsock(fp);
3071 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3072 if (uap->argp == NULL) {
3076 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3080 error = nfsrvd_nfsd(td, &nfsdarg);
3082 error = nfssvc_srvcall(td, uap, td->td_ucred);
3091 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3093 struct nfsex_args export;
3094 struct file *fp = NULL;
3096 struct nfsd_clid adminrevoke;
3097 struct nfsd_dumplist dumplist;
3098 struct nfsd_dumpclients *dumpclients;
3099 struct nfsd_dumplocklist dumplocklist;
3100 struct nfsd_dumplocks *dumplocks;
3101 struct nameidata nd;
3103 int error = EINVAL, igotlock;
3105 static int suspend_nfsd = 0;
3107 if (uap->flag & NFSSVC_PUBLICFH) {
3108 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3109 sizeof (fhandle_t));
3110 error = copyin(uap->argp,
3111 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3114 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3115 error = copyin(uap->argp,(caddr_t)&export,
3116 sizeof (struct nfsex_args));
3118 error = nfsrv_v4rootexport(&export, cred, p);
3119 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3122 } else if (uap->flag & NFSSVC_STABLERESTART) {
3123 error = copyin(uap->argp, (caddr_t)&stablefd,
3126 error = fp_getfvp(p, stablefd, &fp, &vp);
3127 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3129 if (!error && newnfs_numnfsd != 0)
3132 nfsrv_stablefirst.nsf_fp = fp;
3133 nfsrv_setupstable(p);
3135 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3136 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3137 sizeof (struct nfsd_clid));
3139 error = nfsrv_adminrevoke(&adminrevoke, p);
3140 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3141 error = copyin(uap->argp, (caddr_t)&dumplist,
3142 sizeof (struct nfsd_dumplist));
3143 if (!error && (dumplist.ndl_size < 1 ||
3144 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3147 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3148 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3150 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3151 error = copyout(dumpclients,
3152 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3153 free((caddr_t)dumpclients, M_TEMP);
3155 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3156 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3157 sizeof (struct nfsd_dumplocklist));
3158 if (!error && (dumplocklist.ndllck_size < 1 ||
3159 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3162 error = nfsrv_lookupfilename(&nd,
3163 dumplocklist.ndllck_fname, p);
3165 len = sizeof (struct nfsd_dumplocks) *
3166 dumplocklist.ndllck_size;
3167 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3169 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3170 dumplocklist.ndllck_size, p);
3172 error = copyout(dumplocks,
3173 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3174 free((caddr_t)dumplocks, M_TEMP);
3176 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3179 nfsd_master_pid = procp->p_pid;
3180 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3181 nfsd_master_start = procp->p_stats->p_start;
3182 nfsd_master_proc = procp;
3184 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3185 NFSLOCKV4ROOTMUTEX();
3186 if (suspend_nfsd == 0) {
3187 /* Lock out all nfsd threads */
3189 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3190 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3191 } while (igotlock == 0 && suspend_nfsd == 0);
3194 NFSUNLOCKV4ROOTMUTEX();
3196 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3197 NFSLOCKV4ROOTMUTEX();
3198 if (suspend_nfsd != 0) {
3199 nfsv4_unlock(&nfsd_suspend_lock, 0);
3202 NFSUNLOCKV4ROOTMUTEX();
3212 * Returns 0 if ok, 1 otherwise.
3215 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3220 * This seems odd, but allow the case where the security flavor
3221 * list is empty. This happens when NFSv4 is traversing non-exported
3222 * file systems. Exported file systems should always have a non-empty
3223 * security flavor list.
3225 if (exp->nes_numsecflavor == 0)
3228 for (i = 0; i < exp->nes_numsecflavor; i++) {
3230 * The tests for privacy and integrity must be first,
3231 * since ND_GSS is set for everything but AUTH_SYS.
3233 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3234 (nd->nd_flag & ND_GSSPRIVACY))
3236 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3237 (nd->nd_flag & ND_GSSINTEGRITY))
3239 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3240 (nd->nd_flag & ND_GSS))
3242 if (exp->nes_secflavors[i] == AUTH_SYS &&
3243 (nd->nd_flag & ND_GSS) == 0)
3250 * Calculate a hash value for the fid in a file handle.
3253 nfsrv_hashfh(fhandle_t *fhp)
3257 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3262 * Signal the userland master nfsd to backup the stable restart file.
3265 nfsrv_backupstable(void)
3269 if (nfsd_master_proc != NULL) {
3270 procp = pfind(nfsd_master_pid);
3271 /* Try to make sure it is the correct process. */
3272 if (procp == nfsd_master_proc &&
3273 procp->p_stats->p_start.tv_sec ==
3274 nfsd_master_start.tv_sec &&
3275 procp->p_stats->p_start.tv_usec ==
3276 nfsd_master_start.tv_usec &&
3277 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3278 kern_psignal(procp, SIGUSR2);
3280 nfsd_master_proc = NULL;
3287 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3290 * Called once to initialize data structures...
3293 nfsd_modevent(module_t mod, int type, void *data)
3296 static int loaded = 0;
3303 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3304 snprintf(nfsrchash_table[i].lock_name,
3305 sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d",
3307 mtx_init(&nfsrchash_table[i].mtx,
3308 nfsrchash_table[i].lock_name, NULL, MTX_DEF);
3310 mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF);
3311 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3312 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3314 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3321 #ifdef VV_DISABLEDELEG
3322 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3323 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3325 nfsd_call_servertimer = nfsrv_servertimer;
3326 nfsd_call_nfsd = nfssvc_nfsd;
3331 if (newnfs_numnfsd != 0) {
3336 #ifdef VV_DISABLEDELEG
3337 vn_deleg_ops.vndeleg_recall = NULL;
3338 vn_deleg_ops.vndeleg_disable = NULL;
3340 nfsd_call_servertimer = NULL;
3341 nfsd_call_nfsd = NULL;
3343 /* Clean out all NFSv4 state. */
3344 nfsrv_throwawayallstate(curthread);
3346 /* Clean the NFS server reply cache */
3347 nfsrvd_cleancache();
3349 /* Free up the krpc server pool. */
3350 if (nfsrvd_pool != NULL)
3351 svcpool_destroy(nfsrvd_pool);
3353 /* and get rid of the locks */
3354 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++)
3355 mtx_destroy(&nfsrchash_table[i].mtx);
3356 mtx_destroy(&nfsrc_udpmtx);
3357 mtx_destroy(&nfs_v4root_mutex);
3358 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3359 lockdestroy(&nfsv4root_mnt.mnt_explock);
3371 static moduledata_t nfsd_mod = {
3376 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3378 /* So that loader and kldload(2) can find us, wherever we are.. */
3379 MODULE_VERSION(nfsd, 1);
3380 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3381 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3382 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3383 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3384 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);