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 nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
65 struct mtx nfsrc_udpmtx;
66 struct mtx nfs_v4root_mutex;
67 struct nfsrvfh nfs_rootfh, nfs_pubfh;
68 int nfs_pubfhset = 0, nfs_rootfhset = 0;
69 struct proc *nfsd_master_proc = NULL;
70 static pid_t nfsd_master_pid = (pid_t)-1;
71 static char nfsd_master_comm[MAXCOMLEN + 1];
72 static struct timeval nfsd_master_start;
73 static uint32_t nfsv4_sysid = 0;
75 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
78 int nfsrv_enable_crossmntpt = 1;
79 static int nfs_commit_blks;
80 static int nfs_commit_miss;
81 extern int nfsrv_issuedelegs;
82 extern int nfsrv_dolocallocks;
83 extern int nfsd_enable_stringtouid;
85 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
86 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
87 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
88 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
92 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
93 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
95 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
97 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
99 #define MAX_REORDERED_RPC 16
100 #define NUM_HEURISTIC 1031
101 #define NHUSE_INIT 64
103 #define NHUSE_MAX 2048
105 static struct nfsheur {
106 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
107 off_t nh_nextoff; /* next offset for sequential detection */
108 int nh_use; /* use count for selection */
109 int nh_seqcount; /* heuristic */
110 } nfsheur[NUM_HEURISTIC];
114 * Heuristic to detect sequential operation.
116 static struct nfsheur *
117 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
122 /* Locate best candidate. */
124 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
127 if (nfsheur[hi].nh_vp == vp) {
131 if (nfsheur[hi].nh_use > 0)
132 --nfsheur[hi].nh_use;
133 hi = (hi + 1) % NUM_HEURISTIC;
134 if (nfsheur[hi].nh_use < nh->nh_use)
138 /* Initialize hint if this is a new file. */
139 if (nh->nh_vp != vp) {
141 nh->nh_nextoff = uio->uio_offset;
142 nh->nh_use = NHUSE_INIT;
143 if (uio->uio_offset == 0)
149 /* Calculate heuristic. */
150 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
151 uio->uio_offset == nh->nh_nextoff) {
152 /* See comments in vfs_vnops.c:sequential_heuristic(). */
153 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
154 if (nh->nh_seqcount > IO_SEQMAX)
155 nh->nh_seqcount = IO_SEQMAX;
156 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
157 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
158 /* Probably a reordered RPC, leave seqcount alone. */
159 } else if (nh->nh_seqcount > 1) {
160 nh->nh_seqcount /= 2;
164 nh->nh_use += NHUSE_INC;
165 if (nh->nh_use > NHUSE_MAX)
166 nh->nh_use = NHUSE_MAX;
171 * Get attributes into nfsvattr structure.
174 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
175 struct thread *p, int vpislocked)
177 int error, lockedit = 0;
179 if (vpislocked == 0) {
181 * When vpislocked == 0, the vnode is either exclusively
182 * locked by this thread or not locked by this thread.
183 * As such, shared lock it, if not exclusively locked.
185 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
187 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
190 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
199 * Get a file handle for a vnode.
202 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
206 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
207 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
208 error = VOP_VPTOFH(vp, &fhp->fh_fid);
215 * Perform access checking for vnodes obtained from file handles that would
216 * refer to files already opened by a Unix client. You cannot just use
217 * vn_writechk() and VOP_ACCESSX() for two reasons.
218 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
220 * 2 - The owner is to be given access irrespective of mode bits for some
221 * operations, so that processes that chmod after opening a file don't
225 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
226 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
227 u_int32_t *supportedtypep)
230 int error = 0, getret = 0;
232 if (vpislocked == 0) {
233 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
238 if (accmode & VWRITE) {
239 /* Just vn_writechk() changed to check rdonly */
241 * Disallow write attempts on read-only file systems;
242 * unless the file is a socket or a block or character
243 * device resident on the file system.
245 if (NFSVNO_EXRDONLY(exp) ||
246 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
247 switch (vp->v_type) {
257 * If there's shared text associated with
258 * the inode, try to free it up once. If
259 * we fail, we can't allow writing.
261 if (VOP_IS_TEXT(vp) && error == 0)
271 * Should the override still be applied when ACLs are enabled?
273 error = VOP_ACCESSX(vp, accmode, cred, p);
274 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
276 * Try again with VEXPLICIT_DENY, to see if the test for
277 * deletion is supported.
279 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
281 if (vp->v_type == VDIR) {
282 accmode &= ~(VDELETE | VDELETE_CHILD);
284 error = VOP_ACCESSX(vp, accmode, cred, p);
285 } else if (supportedtypep != NULL) {
286 *supportedtypep &= ~NFSACCESS_DELETE;
292 * Allow certain operations for the owner (reads and writes
293 * on files that are already open).
295 if (override != NFSACCCHK_NOOVERRIDE &&
296 (error == EPERM || error == EACCES)) {
297 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
299 else if (override & NFSACCCHK_ALLOWOWNER) {
300 getret = VOP_GETATTR(vp, &vattr, cred);
301 if (getret == 0 && cred->cr_uid == vattr.va_uid)
314 * Set attribute(s) vnop.
317 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
318 struct thread *p, struct nfsexstuff *exp)
322 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
328 * Set up nameidata for a lookup() call and do it.
331 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
332 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
333 struct vnode **retdirp)
335 struct componentname *cnp = &ndp->ni_cnd;
339 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
340 int error = 0, crossmnt;
344 cnp->cn_nameptr = cnp->cn_pnbuf;
345 ndp->ni_strictrelative = 0;
347 * Extract and set starting directory.
349 if (dp->v_type != VDIR) {
354 nfsvno_relpathbuf(ndp);
362 if (NFSVNO_EXRDONLY(exp))
363 cnp->cn_flags |= RDONLY;
364 ndp->ni_segflg = UIO_SYSSPACE;
367 if (nd->nd_flag & ND_PUBLOOKUP) {
369 if (cnp->cn_pnbuf[0] == '/') {
372 * Check for degenerate pathnames here, since lookup()
375 for (i = 1; i < ndp->ni_pathlen; i++)
376 if (cnp->cn_pnbuf[i] != '/')
378 if (i == ndp->ni_pathlen) {
379 error = NFSERR_ACCES;
385 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
386 (nd->nd_flag & ND_NFSV4) == 0) {
388 * Only cross mount points for NFSv4 when doing a
389 * mount while traversing the file system above
390 * the mount point, unless nfsrv_enable_crossmntpt is set.
392 cnp->cn_flags |= NOCROSSMOUNT;
397 * Initialize for scan, set ni_startdir and bump ref on dp again
398 * because lookup() will dereference ni_startdir.
402 ndp->ni_startdir = dp;
403 ndp->ni_rootdir = rootvnode;
404 ndp->ni_topdir = NULL;
407 cnp->cn_flags |= LOCKLEAF;
409 cnp->cn_nameptr = cnp->cn_pnbuf;
411 * Call lookup() to do the real work. If an error occurs,
412 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
413 * we do not have to dereference anything before returning.
414 * In either case ni_startdir will be dereferenced and NULLed
422 * Check for encountering a symbolic link. Trivial
423 * termination occurs if no symlink encountered.
425 if ((cnp->cn_flags & ISSYMLINK) == 0) {
426 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
427 nfsvno_relpathbuf(ndp);
428 if (ndp->ni_vp && !lockleaf)
429 NFSVOPUNLOCK(ndp->ni_vp, 0);
436 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
437 NFSVOPUNLOCK(ndp->ni_dvp, 0);
438 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
443 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
447 if (ndp->ni_pathlen > 1)
448 cp = uma_zalloc(namei_zone, M_WAITOK);
452 aiov.iov_len = MAXPATHLEN;
453 auio.uio_iov = &aiov;
456 auio.uio_rw = UIO_READ;
457 auio.uio_segflg = UIO_SYSSPACE;
459 auio.uio_resid = MAXPATHLEN;
460 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
463 if (ndp->ni_pathlen > 1)
464 uma_zfree(namei_zone, cp);
470 linklen = MAXPATHLEN - auio.uio_resid;
475 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
476 error = ENAMETOOLONG;
481 * Adjust or replace path
483 if (ndp->ni_pathlen > 1) {
484 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
485 uma_zfree(namei_zone, cnp->cn_pnbuf);
488 cnp->cn_pnbuf[linklen] = '\0';
489 ndp->ni_pathlen += linklen;
492 * Cleanup refs for next loop and check if root directory
493 * should replace current directory. Normally ni_dvp
494 * becomes the new base directory and is cleaned up when
495 * we loop. Explicitly null pointers after invalidation
496 * to clarify operation.
501 if (cnp->cn_pnbuf[0] == '/') {
503 ndp->ni_dvp = ndp->ni_rootdir;
506 ndp->ni_startdir = ndp->ni_dvp;
510 cnp->cn_flags &= ~LOCKLEAF;
514 nfsvno_relpathbuf(ndp);
517 ndp->ni_startdir = NULL;
518 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
523 NFSEXITCODE2(error, nd);
528 * Set up a pathname buffer and return a pointer to it and, optionally
529 * set a hash pointer.
532 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
534 struct componentname *cnp = &ndp->ni_cnd;
536 cnp->cn_flags |= (NOMACCHECK | HASBUF);
537 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
540 *bufpp = cnp->cn_pnbuf;
544 * Release the above path buffer, if not released by nfsvno_namei().
547 nfsvno_relpathbuf(struct nameidata *ndp)
550 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
552 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
553 ndp->ni_cnd.cn_flags &= ~HASBUF;
557 * Readlink vnode op into an mbuf list.
560 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
561 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
563 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
564 struct iovec *ivp = iv;
565 struct uio io, *uiop = &io;
566 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
567 int i, len, tlen, error = 0;
571 while (len < NFS_MAXPATHLEN) {
573 MCLGET(mp, M_WAITOK);
574 mp->m_len = NFSMSIZ(mp);
581 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
582 mp->m_len = NFS_MAXPATHLEN - len;
583 len = NFS_MAXPATHLEN;
587 ivp->iov_base = mtod(mp, caddr_t);
588 ivp->iov_len = mp->m_len;
593 uiop->uio_iovcnt = i;
594 uiop->uio_offset = 0;
595 uiop->uio_resid = len;
596 uiop->uio_rw = UIO_READ;
597 uiop->uio_segflg = UIO_SYSSPACE;
599 error = VOP_READLINK(vp, uiop, cred);
605 if (uiop->uio_resid > 0) {
606 len -= uiop->uio_resid;
607 tlen = NFSM_RNDUP(len);
608 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
620 * Read vnode op call into mbuf list.
623 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
624 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
630 int error = 0, len, left, siz, tlen, ioflag = 0;
631 struct mbuf *m2 = NULL, *m3;
632 struct uio io, *uiop = &io;
635 len = left = NFSM_RNDUP(cnt);
638 * Generate the mbuf list with the uio_iov ref. to it.
645 siz = min(M_TRAILINGSPACE(m), left);
654 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
656 uiop->uio_iov = iv2 = iv;
662 panic("nfsvno_read iov");
663 siz = min(M_TRAILINGSPACE(m), left);
665 iv->iov_base = mtod(m, caddr_t) + m->m_len;
674 uiop->uio_iovcnt = i;
675 uiop->uio_offset = off;
676 uiop->uio_resid = len;
677 uiop->uio_rw = UIO_READ;
678 uiop->uio_segflg = UIO_SYSSPACE;
680 nh = nfsrv_sequential_heuristic(uiop, vp);
681 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
682 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
683 FREE((caddr_t)iv2, M_TEMP);
689 nh->nh_nextoff = uiop->uio_offset;
690 tlen = len - uiop->uio_resid;
691 cnt = cnt < tlen ? cnt : tlen;
692 tlen = NFSM_RNDUP(cnt);
696 } else if (len != tlen || tlen != cnt)
697 nfsrv_adj(m3, len - tlen, tlen - cnt);
707 * Write vnode op from an mbuf list.
710 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
711 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
717 struct uio io, *uiop = &io;
720 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
722 uiop->uio_iov = iv = ivp;
723 uiop->uio_iovcnt = cnt;
724 i = mtod(mp, caddr_t) + mp->m_len - cp;
728 panic("nfsvno_write");
739 cp = mtod(mp, caddr_t);
743 if (stable == NFSWRITE_UNSTABLE)
744 ioflags = IO_NODELOCKED;
746 ioflags = (IO_SYNC | IO_NODELOCKED);
747 uiop->uio_resid = retlen;
748 uiop->uio_rw = UIO_WRITE;
749 uiop->uio_segflg = UIO_SYSSPACE;
751 uiop->uio_offset = off;
752 nh = nfsrv_sequential_heuristic(uiop, vp);
753 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
754 error = VOP_WRITE(vp, uiop, ioflags, cred);
756 nh->nh_nextoff = uiop->uio_offset;
757 FREE((caddr_t)iv, M_TEMP);
764 * Common code for creating a regular file (plus special files for V2).
767 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
768 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
769 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
774 error = nd->nd_repstat;
775 if (!error && ndp->ni_vp == NULL) {
776 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
777 vrele(ndp->ni_startdir);
778 error = VOP_CREATE(ndp->ni_dvp,
779 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
781 nfsvno_relpathbuf(ndp);
783 if (*exclusive_flagp) {
784 *exclusive_flagp = 0;
785 NFSVNO_ATTRINIT(nvap);
786 nvap->na_atime.tv_sec = cverf[0];
787 nvap->na_atime.tv_nsec = cverf[1];
788 error = VOP_SETATTR(ndp->ni_vp,
789 &nvap->na_vattr, nd->nd_cred);
793 * NFS V2 Only. nfsrvd_mknod() does this for V3.
794 * (This implies, just get out on an error.)
796 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
797 nvap->na_type == VFIFO) {
798 if (nvap->na_type == VCHR && rdev == 0xffffffff)
799 nvap->na_type = VFIFO;
800 if (nvap->na_type != VFIFO &&
801 (error = priv_check_cred(nd->nd_cred,
802 PRIV_VFS_MKNOD_DEV, 0))) {
803 vrele(ndp->ni_startdir);
804 nfsvno_relpathbuf(ndp);
808 nvap->na_rdev = rdev;
809 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
810 &ndp->ni_cnd, &nvap->na_vattr);
812 nfsvno_relpathbuf(ndp);
813 vrele(ndp->ni_startdir);
817 vrele(ndp->ni_startdir);
818 nfsvno_relpathbuf(ndp);
826 * Handle cases where error is already set and/or
828 * 1 - clean up the lookup
829 * 2 - iff !error and na_size set, truncate it
831 vrele(ndp->ni_startdir);
832 nfsvno_relpathbuf(ndp);
834 if (ndp->ni_dvp == *vpp)
838 if (!error && nvap->na_size != VNOVAL) {
839 error = nfsvno_accchk(*vpp, VWRITE,
840 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
841 NFSACCCHK_VPISLOCKED, NULL);
843 tempsize = nvap->na_size;
844 NFSVNO_ATTRINIT(nvap);
845 nvap->na_size = tempsize;
846 error = VOP_SETATTR(*vpp,
847 &nvap->na_vattr, nd->nd_cred);
860 * Do a mknod vnode op.
863 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
869 vtyp = nvap->na_type;
871 * Iff doesn't exist, create it.
874 vrele(ndp->ni_startdir);
875 nfsvno_relpathbuf(ndp);
881 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
882 vrele(ndp->ni_startdir);
883 nfsvno_relpathbuf(ndp);
885 error = NFSERR_BADTYPE;
889 vrele(ndp->ni_startdir);
890 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
891 &ndp->ni_cnd, &nvap->na_vattr);
893 nfsvno_relpathbuf(ndp);
895 if (nvap->na_type != VFIFO &&
896 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
897 vrele(ndp->ni_startdir);
898 nfsvno_relpathbuf(ndp);
902 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
903 &ndp->ni_cnd, &nvap->na_vattr);
905 nfsvno_relpathbuf(ndp);
906 vrele(ndp->ni_startdir);
908 * Since VOP_MKNOD returns the ni_vp, I can't
909 * see any reason to do the lookup.
922 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
923 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
927 if (ndp->ni_vp != NULL) {
928 if (ndp->ni_dvp == ndp->ni_vp)
933 nfsvno_relpathbuf(ndp);
937 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
940 nfsvno_relpathbuf(ndp);
951 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
952 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
953 struct nfsexstuff *exp)
958 vrele(ndp->ni_startdir);
959 nfsvno_relpathbuf(ndp);
960 if (ndp->ni_dvp == ndp->ni_vp)
969 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
970 &nvap->na_vattr, pathcp);
972 vrele(ndp->ni_startdir);
973 nfsvno_relpathbuf(ndp);
975 * Although FreeBSD still had the lookup code in
976 * it for 7/current, there doesn't seem to be any
977 * point, since VOP_SYMLINK() returns the ni_vp.
978 * Just vput it for v2.
980 if (!not_v2 && !error)
989 * Parse symbolic link arguments.
990 * This function has an ugly side effect. It will MALLOC() an area for
991 * the symlink and set iov_base to point to it, only if it succeeds.
992 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
996 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
997 struct thread *p, char **pathcpp, int *lenp)
1000 char *pathcp = NULL;
1002 struct nfsv2_sattr *sp;
1006 if ((nd->nd_flag & ND_NFSV3) &&
1007 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1009 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1010 len = fxdr_unsigned(int, *tl);
1011 if (len > NFS_MAXPATHLEN || len <= 0) {
1015 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1016 error = nfsrv_mtostr(nd, pathcp, len);
1019 if (nd->nd_flag & ND_NFSV2) {
1020 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1021 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1025 NFSEXITCODE2(0, nd);
1029 free(pathcp, M_TEMP);
1030 NFSEXITCODE2(error, nd);
1035 * Remove a non-directory object.
1038 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1039 struct thread *p, struct nfsexstuff *exp)
1045 if (vp->v_type == VDIR)
1046 error = NFSERR_ISDIR;
1048 error = nfsrv_checkremove(vp, 1, p);
1050 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1051 if (ndp->ni_dvp == vp)
1056 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1057 nfsvno_relpathbuf(ndp);
1063 * Remove a directory.
1066 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1067 struct thread *p, struct nfsexstuff *exp)
1073 if (vp->v_type != VDIR) {
1078 * No rmdir "." please.
1080 if (ndp->ni_dvp == vp) {
1085 * The root of a mounted filesystem cannot be deleted.
1087 if (vp->v_vflag & VV_ROOT)
1091 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1092 if (ndp->ni_dvp == vp)
1097 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1098 nfsvno_relpathbuf(ndp);
1107 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1108 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1110 struct vnode *fvp, *tvp, *tdvp;
1113 fvp = fromndp->ni_vp;
1115 vrele(fromndp->ni_dvp);
1120 tdvp = tondp->ni_dvp;
1123 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1124 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1126 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1127 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1130 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1131 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1136 * A rename to '.' or '..' results in a prematurely
1137 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1140 if ((tondp->ni_cnd.cn_namelen == 1 &&
1141 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1142 (tondp->ni_cnd.cn_namelen == 2 &&
1143 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1144 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1149 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1150 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1153 if (fvp->v_mount != tdvp->v_mount) {
1154 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1158 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1163 * If source and destination are the same, there is nothing to
1164 * do. Set error to -1 to indicate this.
1169 if (ndflag & ND_NFSV4) {
1170 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1171 error = nfsrv_checkremove(fvp, 0, p);
1172 NFSVOPUNLOCK(fvp, 0);
1176 error = nfsrv_checkremove(tvp, 1, p);
1179 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1180 * that the NFSv4 client won't be confused by the rename.
1181 * Since nfsd_recalldelegation() can only be called on an
1182 * unlocked vnode at this point and fvp is the file that will
1183 * still exist after the rename, just do fvp.
1185 nfsd_recalldelegation(fvp, p);
1189 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1190 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1199 vrele(fromndp->ni_dvp);
1204 vrele(tondp->ni_startdir);
1205 nfsvno_relpathbuf(tondp);
1207 vrele(fromndp->ni_startdir);
1208 nfsvno_relpathbuf(fromndp);
1217 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1218 struct thread *p, struct nfsexstuff *exp)
1228 if (vp->v_mount != xp->v_mount)
1232 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1233 if ((vp->v_iflag & VI_DOOMED) == 0)
1234 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1237 if (ndp->ni_dvp == vp)
1241 NFSVOPUNLOCK(vp, 0);
1243 if (ndp->ni_dvp == ndp->ni_vp)
1250 nfsvno_relpathbuf(ndp);
1256 * Do the fsync() appropriate for the commit.
1259 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1265 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1266 * file is done. At this time VOP_FSYNC does not accept offset and
1267 * byte count parameters so call VOP_FSYNC the whole file for now.
1268 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1270 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
1272 * Give up and do the whole thing
1275 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1276 VM_OBJECT_WLOCK(vp->v_object);
1277 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1278 VM_OBJECT_WUNLOCK(vp->v_object);
1280 error = VOP_FSYNC(vp, MNT_WAIT, td);
1283 * Locate and synchronously write any buffers that fall
1284 * into the requested range. Note: we are assuming that
1285 * f_iosize is a power of 2.
1287 int iosize = vp->v_mount->mnt_stat.f_iosize;
1288 int iomask = iosize - 1;
1293 * Align to iosize boundry, super-align to page boundry.
1296 cnt += off & iomask;
1297 off &= ~(u_quad_t)iomask;
1299 if (off & PAGE_MASK) {
1300 cnt += off & PAGE_MASK;
1301 off &= ~(u_quad_t)PAGE_MASK;
1303 lblkno = off / iosize;
1306 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1307 VM_OBJECT_WLOCK(vp->v_object);
1308 vm_object_page_clean(vp->v_object, off, off + cnt,
1310 VM_OBJECT_WUNLOCK(vp->v_object);
1319 * If we have a buffer and it is marked B_DELWRI we
1320 * have to lock and write it. Otherwise the prior
1321 * write is assumed to have already been committed.
1323 * gbincore() can return invalid buffers now so we
1324 * have to check that bit as well (though B_DELWRI
1325 * should not be set if B_INVAL is set there could be
1326 * a race here since we haven't locked the buffer).
1328 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1329 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1330 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1332 continue; /* retry */
1334 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1337 bp->b_flags &= ~B_ASYNC;
1360 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1364 error = VFS_STATFS(vp->v_mount, sf);
1367 * Since NFS handles these values as unsigned on the
1368 * wire, there is no way to represent negative values,
1369 * so set them to 0. Without this, they will appear
1370 * to be very large positive values for clients like
1373 if (sf->f_bavail < 0)
1375 if (sf->f_ffree < 0)
1383 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1384 * must handle nfsrv_opencheck() calls after any other access checks.
1387 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1388 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1389 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1390 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1391 struct nfsexstuff *exp, struct vnode **vpp)
1393 struct vnode *vp = NULL;
1395 struct nfsexstuff nes;
1397 if (ndp->ni_vp == NULL)
1398 nd->nd_repstat = nfsrv_opencheck(clientid,
1399 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1400 if (!nd->nd_repstat) {
1401 if (ndp->ni_vp == NULL) {
1402 vrele(ndp->ni_startdir);
1403 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1404 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1406 nfsvno_relpathbuf(ndp);
1407 if (!nd->nd_repstat) {
1408 if (*exclusive_flagp) {
1409 *exclusive_flagp = 0;
1410 NFSVNO_ATTRINIT(nvap);
1411 nvap->na_atime.tv_sec = cverf[0];
1412 nvap->na_atime.tv_nsec = cverf[1];
1413 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1414 &nvap->na_vattr, cred);
1416 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1417 aclp, p, attrbitp, exp);
1422 if (ndp->ni_startdir)
1423 vrele(ndp->ni_startdir);
1424 nfsvno_relpathbuf(ndp);
1426 if (create == NFSV4OPEN_CREATE) {
1427 if (ndp->ni_dvp == vp)
1432 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1433 if (ndp->ni_cnd.cn_flags & RDONLY)
1434 NFSVNO_SETEXRDONLY(&nes);
1436 NFSVNO_EXINIT(&nes);
1437 nd->nd_repstat = nfsvno_accchk(vp,
1438 VWRITE, cred, &nes, p,
1439 NFSACCCHK_NOOVERRIDE,
1440 NFSACCCHK_VPISLOCKED, NULL);
1441 nd->nd_repstat = nfsrv_opencheck(clientid,
1442 stateidp, stp, vp, nd, p, nd->nd_repstat);
1443 if (!nd->nd_repstat) {
1444 tempsize = nvap->na_size;
1445 NFSVNO_ATTRINIT(nvap);
1446 nvap->na_size = tempsize;
1447 nd->nd_repstat = VOP_SETATTR(vp,
1448 &nvap->na_vattr, cred);
1450 } else if (vp->v_type == VREG) {
1451 nd->nd_repstat = nfsrv_opencheck(clientid,
1452 stateidp, stp, vp, nd, p, nd->nd_repstat);
1456 if (ndp->ni_cnd.cn_flags & HASBUF)
1457 nfsvno_relpathbuf(ndp);
1458 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1459 vrele(ndp->ni_startdir);
1460 if (ndp->ni_dvp == ndp->ni_vp)
1470 NFSEXITCODE2(0, nd);
1474 * Updates the file rev and sets the mtime and ctime
1475 * to the current clock time, returning the va_filerev and va_Xtime
1477 * Return ESTALE to indicate the vnode is VI_DOOMED.
1480 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1481 struct ucred *cred, struct thread *p)
1486 vfs_timestamp(&va.va_mtime);
1487 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1488 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1489 if ((vp->v_iflag & VI_DOOMED) != 0)
1492 (void) VOP_SETATTR(vp, &va, cred);
1493 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1498 * Glue routine to nfsv4_fillattr().
1501 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1502 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1503 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1504 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1508 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1509 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1511 NFSEXITCODE2(0, nd);
1515 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1517 * nfs readdir service
1518 * - mallocs what it thinks is enough to read
1519 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1520 * - calls VOP_READDIR()
1521 * - loops around building the reply
1522 * if the output generated exceeds count break out of loop
1523 * The NFSM_CLGET macro is used here so that the reply will be packed
1524 * tightly in mbuf clusters.
1525 * - it trims out records with d_fileno == 0
1526 * this doesn't matter for Unix clients, but they might confuse clients
1528 * - it trims out records with d_type == DT_WHT
1529 * these cannot be seen through NFS (unless we extend the protocol)
1530 * The alternate call nfsrvd_readdirplus() does lookups as well.
1531 * PS: The NFS protocol spec. does not clarify what the "count" byte
1532 * argument is a count of.. just name strings and file id's or the
1533 * entire reply rpc or ...
1534 * I tried just file name and id sizes and it confused the Sun client,
1535 * so I am using the full rpc size now. The "paranoia.." comment refers
1536 * to including the status longwords that are not a part of the dir.
1537 * "entry" structures, but are in the rpc.
1540 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1541 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1546 char *cpos, *cend, *rbuf;
1548 int nlen, error = 0, getret = 1;
1549 int siz, cnt, fullsiz, eofflag, ncookies;
1550 u_int64_t off, toff, verf;
1551 u_long *cookies = NULL, *cookiep;
1556 if (nd->nd_repstat) {
1557 nfsrv_postopattr(nd, getret, &at);
1560 if (nd->nd_flag & ND_NFSV2) {
1561 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1562 off = fxdr_unsigned(u_quad_t, *tl++);
1564 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1565 off = fxdr_hyper(tl);
1567 verf = fxdr_hyper(tl);
1571 cnt = fxdr_unsigned(int, *tl);
1572 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1573 cnt = NFS_SRVMAXDATA(nd);
1574 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1576 if (nd->nd_flag & ND_NFSV3) {
1577 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1581 * va_filerev is not sufficient as a cookie verifier,
1582 * since it is not supposed to change when entries are
1583 * removed/added unless that offset cookies returned to
1584 * the client are no longer valid.
1586 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1587 nd->nd_repstat = NFSERR_BAD_COOKIE;
1590 if (!nd->nd_repstat && vp->v_type != VDIR)
1591 nd->nd_repstat = NFSERR_NOTDIR;
1592 if (nd->nd_repstat == 0 && cnt == 0) {
1593 if (nd->nd_flag & ND_NFSV2)
1594 /* NFSv2 does not have NFSERR_TOOSMALL */
1595 nd->nd_repstat = EPERM;
1597 nd->nd_repstat = NFSERR_TOOSMALL;
1599 if (!nd->nd_repstat)
1600 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1601 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1602 NFSACCCHK_VPISLOCKED, NULL);
1603 if (nd->nd_repstat) {
1605 if (nd->nd_flag & ND_NFSV3)
1606 nfsrv_postopattr(nd, getret, &at);
1609 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1610 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1614 free((caddr_t)cookies, M_TEMP);
1622 io.uio_offset = (off_t)off;
1624 io.uio_segflg = UIO_SYSSPACE;
1625 io.uio_rw = UIO_READ;
1627 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1629 off = (u_int64_t)io.uio_offset;
1631 siz -= io.uio_resid;
1633 if (!cookies && !nd->nd_repstat)
1634 nd->nd_repstat = NFSERR_PERM;
1635 if (nd->nd_flag & ND_NFSV3) {
1636 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1637 if (!nd->nd_repstat)
1638 nd->nd_repstat = getret;
1642 * Handles the failed cases. nd->nd_repstat == 0 past here.
1644 if (nd->nd_repstat) {
1646 free((caddr_t)rbuf, M_TEMP);
1648 free((caddr_t)cookies, M_TEMP);
1649 if (nd->nd_flag & ND_NFSV3)
1650 nfsrv_postopattr(nd, getret, &at);
1654 * If nothing read, return eof
1659 if (nd->nd_flag & ND_NFSV2) {
1660 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1662 nfsrv_postopattr(nd, getret, &at);
1663 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1664 txdr_hyper(at.na_filerev, tl);
1667 *tl++ = newnfs_false;
1669 FREE((caddr_t)rbuf, M_TEMP);
1670 FREE((caddr_t)cookies, M_TEMP);
1675 * Check for degenerate cases of nothing useful read.
1676 * If so go try again
1680 dp = (struct dirent *)cpos;
1684 * For some reason FreeBSD's ufs_readdir() chooses to back the
1685 * directory offset up to a block boundary, so it is necessary to
1686 * skip over the records that precede the requested offset. This
1687 * requires the assumption that file offset cookies monotonically
1689 * Since the offset cookies don't monotonically increase for ZFS,
1690 * this is not done when ZFS is the file system.
1692 while (cpos < cend && ncookies > 0 &&
1693 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1694 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1695 cpos += dp->d_reclen;
1696 dp = (struct dirent *)cpos;
1700 if (cpos >= cend || ncookies == 0) {
1708 * dirlen is the size of the reply, including all XDR and must
1709 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1710 * if the XDR should be included in "count", but to be safe, we do.
1711 * (Include the two booleans at the end of the reply in dirlen now.)
1713 if (nd->nd_flag & ND_NFSV3) {
1714 nfsrv_postopattr(nd, getret, &at);
1715 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1716 txdr_hyper(at.na_filerev, tl);
1717 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1719 dirlen = 2 * NFSX_UNSIGNED;
1722 /* Loop through the records and build reply */
1723 while (cpos < cend && ncookies > 0) {
1724 nlen = dp->d_namlen;
1725 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1726 nlen <= NFS_MAXNAMLEN) {
1727 if (nd->nd_flag & ND_NFSV3)
1728 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1730 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1737 * Build the directory record xdr from
1740 if (nd->nd_flag & ND_NFSV3) {
1741 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1742 *tl++ = newnfs_true;
1745 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1746 *tl++ = newnfs_true;
1748 *tl = txdr_unsigned(dp->d_fileno);
1749 (void) nfsm_strtom(nd, dp->d_name, nlen);
1750 if (nd->nd_flag & ND_NFSV3) {
1751 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1754 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1755 *tl = txdr_unsigned(*cookiep);
1757 cpos += dp->d_reclen;
1758 dp = (struct dirent *)cpos;
1764 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1765 *tl++ = newnfs_false;
1770 FREE((caddr_t)rbuf, M_TEMP);
1771 FREE((caddr_t)cookies, M_TEMP);
1774 NFSEXITCODE2(0, nd);
1778 NFSEXITCODE2(error, nd);
1783 * Readdirplus for V3 and Readdir for V4.
1786 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1787 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1792 char *cpos, *cend, *rbuf;
1795 struct nfsvattr nva, at, *nvap = &nva;
1796 struct mbuf *mb0, *mb1;
1797 struct nfsreferral *refp;
1798 int nlen, r, error = 0, getret = 1, usevget = 1;
1799 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1800 caddr_t bpos0, bpos1;
1801 u_int64_t off, toff, verf;
1802 u_long *cookies = NULL, *cookiep;
1803 nfsattrbit_t attrbits, rderrbits, savbits;
1806 struct componentname cn;
1807 int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1808 struct mount *mp, *new_mp;
1809 uint64_t mounted_on_fileno;
1811 if (nd->nd_repstat) {
1812 nfsrv_postopattr(nd, getret, &at);
1815 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1816 off = fxdr_hyper(tl);
1819 verf = fxdr_hyper(tl);
1821 siz = fxdr_unsigned(int, *tl++);
1822 cnt = fxdr_unsigned(int, *tl);
1825 * Use the server's maximum data transfer size as the upper bound
1828 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1829 cnt = NFS_SRVMAXDATA(nd);
1832 * siz is a "hint" of how much directory information (name, fileid,
1833 * cookie) should be in the reply. At least one client "hints" 0,
1834 * so I set it to cnt for that case. I also round it up to the
1835 * next multiple of DIRBLKSIZ.
1839 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1841 if (nd->nd_flag & ND_NFSV4) {
1842 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1845 NFSSET_ATTRBIT(&savbits, &attrbits);
1846 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1847 NFSZERO_ATTRBIT(&rderrbits);
1848 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1850 NFSZERO_ATTRBIT(&attrbits);
1853 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1854 if (!nd->nd_repstat) {
1855 if (off && verf != at.na_filerev) {
1857 * va_filerev is not sufficient as a cookie verifier,
1858 * since it is not supposed to change when entries are
1859 * removed/added unless that offset cookies returned to
1860 * the client are no longer valid.
1863 if (nd->nd_flag & ND_NFSV4) {
1864 nd->nd_repstat = NFSERR_NOTSAME;
1866 nd->nd_repstat = NFSERR_BAD_COOKIE;
1869 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1870 nd->nd_repstat = NFSERR_BAD_COOKIE;
1873 if (!nd->nd_repstat && vp->v_type != VDIR)
1874 nd->nd_repstat = NFSERR_NOTDIR;
1875 if (!nd->nd_repstat && cnt == 0)
1876 nd->nd_repstat = NFSERR_TOOSMALL;
1877 if (!nd->nd_repstat)
1878 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1879 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1880 NFSACCCHK_VPISLOCKED, NULL);
1881 if (nd->nd_repstat) {
1883 if (nd->nd_flag & ND_NFSV3)
1884 nfsrv_postopattr(nd, getret, &at);
1887 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1889 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1893 free((caddr_t)cookies, M_TEMP);
1901 io.uio_offset = (off_t)off;
1903 io.uio_segflg = UIO_SYSSPACE;
1904 io.uio_rw = UIO_READ;
1906 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1908 off = (u_int64_t)io.uio_offset;
1910 siz -= io.uio_resid;
1912 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1914 if (!cookies && !nd->nd_repstat)
1915 nd->nd_repstat = NFSERR_PERM;
1916 if (!nd->nd_repstat)
1917 nd->nd_repstat = getret;
1918 if (nd->nd_repstat) {
1921 free((caddr_t)cookies, M_TEMP);
1922 free((caddr_t)rbuf, M_TEMP);
1923 if (nd->nd_flag & ND_NFSV3)
1924 nfsrv_postopattr(nd, getret, &at);
1928 * If nothing read, return eof
1933 if (nd->nd_flag & ND_NFSV3)
1934 nfsrv_postopattr(nd, getret, &at);
1935 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1936 txdr_hyper(at.na_filerev, tl);
1938 *tl++ = newnfs_false;
1940 free((caddr_t)cookies, M_TEMP);
1941 free((caddr_t)rbuf, M_TEMP);
1946 * Check for degenerate cases of nothing useful read.
1947 * If so go try again
1951 dp = (struct dirent *)cpos;
1955 * For some reason FreeBSD's ufs_readdir() chooses to back the
1956 * directory offset up to a block boundary, so it is necessary to
1957 * skip over the records that precede the requested offset. This
1958 * requires the assumption that file offset cookies monotonically
1960 * Since the offset cookies don't monotonically increase for ZFS,
1961 * this is not done when ZFS is the file system.
1963 while (cpos < cend && ncookies > 0 &&
1964 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1965 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1966 ((nd->nd_flag & ND_NFSV4) &&
1967 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1968 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1969 cpos += dp->d_reclen;
1970 dp = (struct dirent *)cpos;
1974 if (cpos >= cend || ncookies == 0) {
1981 * Busy the file system so that the mount point won't go away
1982 * and, as such, VFS_VGET() can be used safely.
1986 NFSVOPUNLOCK(vp, 0);
1987 nd->nd_repstat = vfs_busy(mp, 0);
1989 if (nd->nd_repstat != 0) {
1991 free(cookies, M_TEMP);
1993 if (nd->nd_flag & ND_NFSV3)
1994 nfsrv_postopattr(nd, getret, &at);
1999 * Check to see if entries in this directory can be safely acquired
2000 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2001 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2002 * automount of the snapshot directory that is required will
2004 * This needs to be done here for NFSv4, since NFSv4 never does
2005 * a VFS_VGET() for "." or "..".
2008 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2009 if (r == EOPNOTSUPP) {
2011 cn.cn_nameiop = LOOKUP;
2012 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2013 cn.cn_cred = nd->nd_cred;
2020 * Save this position, in case there is an error before one entry
2024 bpos0 = nd->nd_bpos;
2027 * Fill in the first part of the reply.
2028 * dirlen is the reply length in bytes and cannot exceed cnt.
2029 * (Include the two booleans at the end of the reply in dirlen now,
2030 * so we recognize when we have exceeded cnt.)
2032 if (nd->nd_flag & ND_NFSV3) {
2033 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2034 nfsrv_postopattr(nd, getret, &at);
2036 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2038 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2039 txdr_hyper(at.na_filerev, tl);
2042 * Save this position, in case there is an empty reply needed.
2045 bpos1 = nd->nd_bpos;
2047 /* Loop through the records and build reply */
2049 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2050 nlen = dp->d_namlen;
2051 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2052 nlen <= NFS_MAXNAMLEN &&
2053 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2054 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2055 || (nlen == 1 && dp->d_name[0] != '.'))) {
2057 * Save the current position in the reply, in case
2058 * this entry exceeds cnt.
2061 bpos1 = nd->nd_bpos;
2064 * For readdir_and_lookup get the vnode using
2073 mounted_on_fileno = (uint64_t)dp->d_fileno;
2074 if ((nd->nd_flag & ND_NFSV3) ||
2075 NFSNONZERO_ATTRBIT(&savbits)) {
2076 if (nd->nd_flag & ND_NFSV4)
2077 refp = nfsv4root_getreferral(NULL,
2081 r = VFS_VGET(mp, dp->d_fileno,
2085 if (r == EOPNOTSUPP) {
2088 cn.cn_nameiop = LOOKUP;
2096 cn.cn_nameptr = dp->d_name;
2097 cn.cn_namelen = nlen;
2098 cn.cn_flags = ISLASTCN |
2099 NOFOLLOW | LOCKLEAF;
2101 dp->d_name[0] == '.' &&
2102 dp->d_name[1] == '.')
2105 if (NFSVOPLOCK(vp, LK_SHARED)
2107 nd->nd_repstat = EPERM;
2110 if ((vp->v_vflag & VV_ROOT) != 0
2111 && (cn.cn_flags & ISDOTDOT)
2117 r = VOP_LOOKUP(vp, &nvp,
2126 * For NFSv4, check to see if nvp is
2127 * a mount point and get the mount
2128 * point vnode, as required.
2131 nfsrv_enable_crossmntpt != 0 &&
2132 (nd->nd_flag & ND_NFSV4) != 0 &&
2133 nvp->v_type == VDIR &&
2134 nvp->v_mountedhere != NULL) {
2135 new_mp = nvp->v_mountedhere;
2136 r = vfs_busy(new_mp, 0);
2140 r = VFS_ROOT(new_mp,
2150 ((nd->nd_flag & ND_NFSV3) ||
2151 NFSNONZERO_ATTRBIT(&attrbits))) {
2152 r = nfsvno_getfh(nvp, &nfh, p);
2154 r = nfsvno_getattr(nvp, nvap,
2156 if (r == 0 && not_zfs == 0 &&
2157 nfsrv_enable_crossmntpt != 0 &&
2158 (nd->nd_flag & ND_NFSV4) != 0 &&
2159 nvp->v_type == VDIR &&
2160 vp->v_mount != nvp->v_mount) {
2162 * For a ZFS snapshot, there is a
2163 * pseudo mount that does not set
2164 * v_mountedhere, so it needs to
2165 * be detected via a different
2170 new_mp = nvp->v_mount;
2177 if (!NFSISSET_ATTRBIT(&attrbits,
2178 NFSATTRBIT_RDATTRERROR)) {
2181 if (needs_unbusy != 0)
2190 * Build the directory record xdr
2192 if (nd->nd_flag & ND_NFSV3) {
2193 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2194 *tl++ = newnfs_true;
2196 *tl = txdr_unsigned(dp->d_fileno);
2197 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2198 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2200 *tl = txdr_unsigned(*cookiep);
2201 nfsrv_postopattr(nd, 0, nvap);
2202 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2203 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2207 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2208 *tl++ = newnfs_true;
2210 *tl = txdr_unsigned(*cookiep);
2211 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2213 supports_nfsv4acls =
2214 nfs_supportsnfsv4acls(nvp);
2215 NFSVOPUNLOCK(nvp, 0);
2217 supports_nfsv4acls = 0;
2219 dirlen += nfsrv_putreferralattr(nd,
2222 if (nd->nd_repstat) {
2225 if (needs_unbusy != 0)
2230 dirlen += nfsvno_fillattr(nd, new_mp,
2231 nvp, nvap, &nfh, r, &rderrbits,
2232 nd->nd_cred, p, isdgram, 0,
2233 supports_nfsv4acls, at_root,
2236 dirlen += nfsvno_fillattr(nd, new_mp,
2237 nvp, nvap, &nfh, r, &attrbits,
2238 nd->nd_cred, p, isdgram, 0,
2239 supports_nfsv4acls, at_root,
2244 dirlen += (3 * NFSX_UNSIGNED);
2246 if (needs_unbusy != 0)
2251 cpos += dp->d_reclen;
2252 dp = (struct dirent *)cpos;
2260 * If dirlen > cnt, we must strip off the last entry. If that
2261 * results in an empty reply, report NFSERR_TOOSMALL.
2263 if (dirlen > cnt || nd->nd_repstat) {
2264 if (!nd->nd_repstat && entrycnt == 0)
2265 nd->nd_repstat = NFSERR_TOOSMALL;
2267 newnfs_trimtrailing(nd, mb0, bpos0);
2269 newnfs_trimtrailing(nd, mb1, bpos1);
2271 } else if (cpos < cend)
2273 if (!nd->nd_repstat) {
2274 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2275 *tl++ = newnfs_false;
2281 FREE((caddr_t)cookies, M_TEMP);
2282 FREE((caddr_t)rbuf, M_TEMP);
2285 NFSEXITCODE2(0, nd);
2289 NFSEXITCODE2(error, nd);
2294 * Get the settable attributes out of the mbuf list.
2295 * (Return 0 or EBADRPC)
2298 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2299 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2302 struct nfsv2_sattr *sp;
2303 int error = 0, toclient = 0;
2305 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2307 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2309 * Some old clients didn't fill in the high order 16bits.
2310 * --> check the low order 2 bytes for 0xffff
2312 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2313 nvap->na_mode = nfstov_mode(sp->sa_mode);
2314 if (sp->sa_uid != newnfs_xdrneg1)
2315 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2316 if (sp->sa_gid != newnfs_xdrneg1)
2317 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2318 if (sp->sa_size != newnfs_xdrneg1)
2319 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2320 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2322 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2324 nvap->na_atime.tv_sec =
2325 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2326 nvap->na_atime.tv_nsec = 0;
2329 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2330 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2333 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2334 if (*tl == newnfs_true) {
2335 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2336 nvap->na_mode = nfstov_mode(*tl);
2338 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2339 if (*tl == newnfs_true) {
2340 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2341 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2343 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2344 if (*tl == newnfs_true) {
2345 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2346 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2348 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2349 if (*tl == newnfs_true) {
2350 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2351 nvap->na_size = fxdr_hyper(tl);
2353 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2354 switch (fxdr_unsigned(int, *tl)) {
2355 case NFSV3SATTRTIME_TOCLIENT:
2356 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2357 fxdr_nfsv3time(tl, &nvap->na_atime);
2360 case NFSV3SATTRTIME_TOSERVER:
2361 vfs_timestamp(&nvap->na_atime);
2362 nvap->na_vaflags |= VA_UTIMES_NULL;
2365 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2366 switch (fxdr_unsigned(int, *tl)) {
2367 case NFSV3SATTRTIME_TOCLIENT:
2368 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2369 fxdr_nfsv3time(tl, &nvap->na_mtime);
2370 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2372 case NFSV3SATTRTIME_TOSERVER:
2373 vfs_timestamp(&nvap->na_mtime);
2375 nvap->na_vaflags |= VA_UTIMES_NULL;
2380 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2383 NFSEXITCODE2(error, nd);
2388 * Handle the setable attributes for V4.
2389 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2392 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2393 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2398 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2400 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2404 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2407 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2408 attrsize = fxdr_unsigned(int, *tl);
2411 * Loop around getting the setable attributes. If an unsupported
2412 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2415 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2416 bitpos = NFSATTRBIT_MAX;
2420 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2421 if (attrsum > attrsize) {
2422 error = NFSERR_BADXDR;
2425 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2427 case NFSATTRBIT_SIZE:
2428 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2429 nvap->na_size = fxdr_hyper(tl);
2430 attrsum += NFSX_HYPER;
2432 case NFSATTRBIT_ACL:
2433 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2437 if (aceerr && !nd->nd_repstat)
2438 nd->nd_repstat = aceerr;
2441 case NFSATTRBIT_ARCHIVE:
2442 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2443 if (!nd->nd_repstat)
2444 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2445 attrsum += NFSX_UNSIGNED;
2447 case NFSATTRBIT_HIDDEN:
2448 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2449 if (!nd->nd_repstat)
2450 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2451 attrsum += NFSX_UNSIGNED;
2453 case NFSATTRBIT_MIMETYPE:
2454 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2455 i = fxdr_unsigned(int, *tl);
2456 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2459 if (!nd->nd_repstat)
2460 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2461 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2463 case NFSATTRBIT_MODE:
2464 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2465 nvap->na_mode = nfstov_mode(*tl);
2466 attrsum += NFSX_UNSIGNED;
2468 case NFSATTRBIT_OWNER:
2469 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2470 j = fxdr_unsigned(int, *tl);
2472 error = NFSERR_BADXDR;
2475 if (j > NFSV4_SMALLSTR)
2476 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2479 error = nfsrv_mtostr(nd, cp, j);
2481 if (j > NFSV4_SMALLSTR)
2482 free(cp, M_NFSSTRING);
2485 if (!nd->nd_repstat) {
2486 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2488 if (!nd->nd_repstat)
2491 if (j > NFSV4_SMALLSTR)
2492 free(cp, M_NFSSTRING);
2493 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2495 case NFSATTRBIT_OWNERGROUP:
2496 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2497 j = fxdr_unsigned(int, *tl);
2499 error = NFSERR_BADXDR;
2502 if (j > NFSV4_SMALLSTR)
2503 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2506 error = nfsrv_mtostr(nd, cp, j);
2508 if (j > NFSV4_SMALLSTR)
2509 free(cp, M_NFSSTRING);
2512 if (!nd->nd_repstat) {
2513 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2515 if (!nd->nd_repstat)
2518 if (j > NFSV4_SMALLSTR)
2519 free(cp, M_NFSSTRING);
2520 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2522 case NFSATTRBIT_SYSTEM:
2523 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2524 if (!nd->nd_repstat)
2525 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2526 attrsum += NFSX_UNSIGNED;
2528 case NFSATTRBIT_TIMEACCESSSET:
2529 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2530 attrsum += NFSX_UNSIGNED;
2531 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2532 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2533 fxdr_nfsv4time(tl, &nvap->na_atime);
2535 attrsum += NFSX_V4TIME;
2537 vfs_timestamp(&nvap->na_atime);
2538 nvap->na_vaflags |= VA_UTIMES_NULL;
2541 case NFSATTRBIT_TIMEBACKUP:
2542 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2543 if (!nd->nd_repstat)
2544 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2545 attrsum += NFSX_V4TIME;
2547 case NFSATTRBIT_TIMECREATE:
2548 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2549 if (!nd->nd_repstat)
2550 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2551 attrsum += NFSX_V4TIME;
2553 case NFSATTRBIT_TIMEMODIFYSET:
2554 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2555 attrsum += NFSX_UNSIGNED;
2556 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2557 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2558 fxdr_nfsv4time(tl, &nvap->na_mtime);
2559 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2560 attrsum += NFSX_V4TIME;
2562 vfs_timestamp(&nvap->na_mtime);
2564 nvap->na_vaflags |= VA_UTIMES_NULL;
2568 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2570 * set bitpos so we drop out of the loop.
2572 bitpos = NFSATTRBIT_MAX;
2578 * some clients pad the attrlist, so we need to skip over the
2581 if (attrsum > attrsize) {
2582 error = NFSERR_BADXDR;
2584 attrsize = NFSM_RNDUP(attrsize);
2585 if (attrsum < attrsize)
2586 error = nfsm_advance(nd, attrsize - attrsum, -1);
2589 NFSEXITCODE2(error, nd);
2594 * Check/setup export credentials.
2597 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2598 struct ucred *credanon)
2603 * Check/setup credentials.
2605 if (nd->nd_flag & ND_GSS)
2606 exp->nes_exflag &= ~MNT_EXPORTANON;
2609 * Check to see if the operation is allowed for this security flavor.
2610 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2611 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2612 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2614 if (nfsvno_testexp(nd, exp) &&
2615 nd->nd_procnum != NFSV4OP_SECINFO &&
2616 nd->nd_procnum != NFSPROC_FSINFO) {
2617 if (nd->nd_flag & ND_NFSV4)
2618 error = NFSERR_WRONGSEC;
2620 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2625 * Check to see if the file system is exported V4 only.
2627 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2628 error = NFSERR_PROGNOTV4;
2633 * Now, map the user credentials.
2634 * (Note that ND_AUTHNONE will only be set for an NFSv3
2635 * Fsinfo RPC. If set for anything else, this code might need
2638 if (NFSVNO_EXPORTED(exp) &&
2639 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2640 NFSVNO_EXPORTANON(exp) ||
2641 (nd->nd_flag & ND_AUTHNONE))) {
2642 nd->nd_cred->cr_uid = credanon->cr_uid;
2643 nd->nd_cred->cr_gid = credanon->cr_gid;
2644 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2645 credanon->cr_groups);
2649 NFSEXITCODE2(error, nd);
2657 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2658 struct ucred **credp)
2660 int i, error, *secflavors;
2662 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2663 &exp->nes_numsecflavor, &secflavors);
2665 if (nfs_rootfhset) {
2666 exp->nes_exflag = 0;
2667 exp->nes_numsecflavor = 0;
2671 /* Copy the security flavors. */
2672 for (i = 0; i < exp->nes_numsecflavor; i++)
2673 exp->nes_secflavors[i] = secflavors[i];
2680 * Get a vnode for a file handle and export stuff.
2683 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2684 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2685 struct ucred **credp)
2687 int i, error, *secflavors;
2690 exp->nes_numsecflavor = 0;
2691 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2693 /* Make sure the server replies ESTALE to the client. */
2695 if (nam && !error) {
2696 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2697 &exp->nes_numsecflavor, &secflavors);
2699 if (nfs_rootfhset) {
2700 exp->nes_exflag = 0;
2701 exp->nes_numsecflavor = 0;
2707 /* Copy the security flavors. */
2708 for (i = 0; i < exp->nes_numsecflavor; i++)
2709 exp->nes_secflavors[i] = secflavors[i];
2717 * nfsd_fhtovp() - convert a fh to a vnode ptr
2718 * - look up fsid in mount list (if not found ret error)
2719 * - get vp and export rights by calling nfsvno_fhtovp()
2720 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2722 * - if mpp != NULL, return the mount point so that it can
2723 * be used for vn_finished_write() by the caller
2726 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2727 struct vnode **vpp, struct nfsexstuff *exp,
2728 struct mount **mpp, int startwrite, struct thread *p)
2731 struct ucred *credanon;
2734 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2736 * Check for the special case of the nfsv4root_fh.
2738 mp = vfs_busyfs(&fhp->fh_fsid);
2743 nd->nd_repstat = ESTALE;
2748 vn_start_write(NULL, mpp, V_WAIT);
2749 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2750 lktype = LK_EXCLUSIVE;
2752 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2757 * For NFSv4 without a pseudo root fs, unexported file handles
2758 * can be returned, so that Lookup works everywhere.
2760 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2761 !(nd->nd_flag & ND_NFSV4)) {
2763 nd->nd_repstat = EACCES;
2767 * Personally, I've never seen any point in requiring a
2768 * reserved port#, since only in the rare case where the
2769 * clients are all boxes with secure system priviledges,
2770 * does it provide any enhanced security, but... some people
2771 * believe it to be useful and keep putting this code back in.
2772 * (There is also some "security checker" out there that
2773 * complains if the nfs server doesn't enforce this.)
2774 * However, note the following:
2775 * RFC3530 (NFSv4) specifies that a reserved port# not be
2777 * RFC2623 recommends that, if a reserved port# is checked for,
2778 * that there be a way to turn that off--> ifdef'd.
2780 #ifdef NFS_REQRSVPORT
2781 if (!nd->nd_repstat) {
2782 struct sockaddr_in *saddr;
2783 struct sockaddr_in6 *saddr6;
2785 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2786 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2787 if (!(nd->nd_flag & ND_NFSV4) &&
2788 ((saddr->sin_family == AF_INET &&
2789 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2790 (saddr6->sin6_family == AF_INET6 &&
2791 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2793 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2796 #endif /* NFS_REQRSVPORT */
2799 * Check/setup credentials.
2801 if (!nd->nd_repstat) {
2802 nd->nd_saveduid = nd->nd_cred->cr_uid;
2803 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2807 if (credanon != NULL)
2809 if (nd->nd_repstat) {
2811 vn_finished_write(mp);
2818 NFSEXITCODE2(0, nd);
2825 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2827 struct filedesc *fdp;
2831 fdp = p->td_proc->p_fd;
2832 if (fd < 0 || fd >= fdp->fd_nfiles ||
2833 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2845 * Called from nfssvc() to update the exports list. Just call
2846 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2847 * in the mount list.
2850 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2852 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2854 struct nameidata nd;
2857 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2858 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2860 else if (error == 0) {
2861 if (nfsexargp->fspec == NULL) {
2866 * If fspec != NULL, this is the v4root path.
2868 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2869 nfsexargp->fspec, p);
2870 if ((error = namei(&nd)) != 0)
2872 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2875 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2876 NFSBCOPY((caddr_t)&fh,
2877 nfs_rootfh.nfsrvfh_data,
2878 sizeof (fhandle_t));
2889 * This function needs to test to see if the system is near its limit
2890 * for memory allocation via malloc() or mget() and return True iff
2891 * either of these resources are near their limit.
2892 * XXX (For now, this is just a stub.)
2894 int nfsrv_testmalloclimit = 0;
2896 nfsrv_mallocmget_limit(void)
2898 static int printmesg = 0;
2899 static int testval = 1;
2901 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2902 if ((printmesg++ % 100) == 0)
2903 printf("nfsd: malloc/mget near limit\n");
2910 * BSD specific initialization of a mount point.
2915 static int inited = 0;
2920 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2921 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2922 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2923 nfsv4root_mnt.mnt_export = NULL;
2924 TAILQ_INIT(&nfsv4root_opt);
2925 TAILQ_INIT(&nfsv4root_newopt);
2926 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2927 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2928 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2929 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2933 * Get a vnode for a file handle, without checking exports, etc.
2936 nfsvno_getvp(fhandle_t *fhp)
2942 mp = vfs_busyfs(&fhp->fh_fsid);
2945 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2953 * Do a local VOP_ADVLOCK().
2956 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2957 u_int64_t end, struct thread *td)
2963 if (nfsrv_dolocallocks == 0)
2966 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2967 if ((vp->v_iflag & VI_DOOMED) != 0) {
2972 fl.l_whence = SEEK_SET;
2974 fl.l_start = (off_t)first;
2975 if (end == NFS64BITSSET) {
2979 fl.l_len = (off_t)tlen;
2982 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2983 * values for all calls, so that all locks will be held by the
2984 * nfsd server. (The nfsd server handles conflicts between the
2986 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2987 * bytes, so it can't be put in l_sysid.
2989 if (nfsv4_sysid == 0)
2990 nfsv4_sysid = nlm_acquire_next_sysid();
2991 fl.l_pid = (pid_t)0;
2992 fl.l_sysid = (int)nfsv4_sysid;
2994 NFSVOPUNLOCK(vp, 0);
2995 if (ftype == F_UNLCK)
2996 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2997 (F_POSIX | F_REMOTE));
2999 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3000 (F_POSIX | F_REMOTE));
3001 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3009 * Check the nfsv4 root exports.
3012 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3014 struct ucred *credanon;
3015 int exflags, error = 0, numsecflavor, *secflavors, i;
3017 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3018 &credanon, &numsecflavor, &secflavors);
3020 error = NFSERR_PROGUNAVAIL;
3023 if (credanon != NULL)
3025 for (i = 0; i < numsecflavor; i++) {
3026 if (secflavors[i] == AUTH_SYS)
3027 nd->nd_flag |= ND_EXAUTHSYS;
3028 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3029 nd->nd_flag |= ND_EXGSS;
3030 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3031 nd->nd_flag |= ND_EXGSSINTEGRITY;
3032 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3033 nd->nd_flag |= ND_EXGSSPRIVACY;
3042 * Nfs server psuedo system call for the nfsd's
3048 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3051 struct nfsd_addsock_args sockarg;
3052 struct nfsd_nfsd_args nfsdarg;
3053 cap_rights_t rights;
3056 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3057 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3061 * Since we don't know what rights might be required,
3062 * pretend that we need them all. It is better to be too
3063 * careful than too reckless.
3065 error = fget(td, sockarg.sock,
3066 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3069 if (fp->f_type != DTYPE_SOCKET) {
3074 error = nfsrvd_addsock(fp);
3076 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3077 if (uap->argp == NULL) {
3081 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3085 error = nfsrvd_nfsd(td, &nfsdarg);
3087 error = nfssvc_srvcall(td, uap, td->td_ucred);
3096 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3098 struct nfsex_args export;
3099 struct file *fp = NULL;
3101 struct nfsd_clid adminrevoke;
3102 struct nfsd_dumplist dumplist;
3103 struct nfsd_dumpclients *dumpclients;
3104 struct nfsd_dumplocklist dumplocklist;
3105 struct nfsd_dumplocks *dumplocks;
3106 struct nameidata nd;
3108 int error = EINVAL, igotlock;
3110 static int suspend_nfsd = 0;
3112 if (uap->flag & NFSSVC_PUBLICFH) {
3113 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3114 sizeof (fhandle_t));
3115 error = copyin(uap->argp,
3116 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3119 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3120 error = copyin(uap->argp,(caddr_t)&export,
3121 sizeof (struct nfsex_args));
3123 error = nfsrv_v4rootexport(&export, cred, p);
3124 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3127 } else if (uap->flag & NFSSVC_STABLERESTART) {
3128 error = copyin(uap->argp, (caddr_t)&stablefd,
3131 error = fp_getfvp(p, stablefd, &fp, &vp);
3132 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3134 if (!error && newnfs_numnfsd != 0)
3137 nfsrv_stablefirst.nsf_fp = fp;
3138 nfsrv_setupstable(p);
3140 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3141 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3142 sizeof (struct nfsd_clid));
3144 error = nfsrv_adminrevoke(&adminrevoke, p);
3145 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3146 error = copyin(uap->argp, (caddr_t)&dumplist,
3147 sizeof (struct nfsd_dumplist));
3148 if (!error && (dumplist.ndl_size < 1 ||
3149 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3152 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3153 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3155 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3156 error = copyout(dumpclients,
3157 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3158 free((caddr_t)dumpclients, M_TEMP);
3160 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3161 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3162 sizeof (struct nfsd_dumplocklist));
3163 if (!error && (dumplocklist.ndllck_size < 1 ||
3164 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3167 error = nfsrv_lookupfilename(&nd,
3168 dumplocklist.ndllck_fname, p);
3170 len = sizeof (struct nfsd_dumplocks) *
3171 dumplocklist.ndllck_size;
3172 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3174 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3175 dumplocklist.ndllck_size, p);
3177 error = copyout(dumplocks,
3178 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3179 free((caddr_t)dumplocks, M_TEMP);
3181 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3184 nfsd_master_pid = procp->p_pid;
3185 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3186 nfsd_master_start = procp->p_stats->p_start;
3187 nfsd_master_proc = procp;
3189 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3190 NFSLOCKV4ROOTMUTEX();
3191 if (suspend_nfsd == 0) {
3192 /* Lock out all nfsd threads */
3194 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3195 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3196 } while (igotlock == 0 && suspend_nfsd == 0);
3199 NFSUNLOCKV4ROOTMUTEX();
3201 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3202 NFSLOCKV4ROOTMUTEX();
3203 if (suspend_nfsd != 0) {
3204 nfsv4_unlock(&nfsd_suspend_lock, 0);
3207 NFSUNLOCKV4ROOTMUTEX();
3217 * Returns 0 if ok, 1 otherwise.
3220 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3225 * This seems odd, but allow the case where the security flavor
3226 * list is empty. This happens when NFSv4 is traversing non-exported
3227 * file systems. Exported file systems should always have a non-empty
3228 * security flavor list.
3230 if (exp->nes_numsecflavor == 0)
3233 for (i = 0; i < exp->nes_numsecflavor; i++) {
3235 * The tests for privacy and integrity must be first,
3236 * since ND_GSS is set for everything but AUTH_SYS.
3238 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3239 (nd->nd_flag & ND_GSSPRIVACY))
3241 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3242 (nd->nd_flag & ND_GSSINTEGRITY))
3244 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3245 (nd->nd_flag & ND_GSS))
3247 if (exp->nes_secflavors[i] == AUTH_SYS &&
3248 (nd->nd_flag & ND_GSS) == 0)
3255 * Calculate a hash value for the fid in a file handle.
3258 nfsrv_hashfh(fhandle_t *fhp)
3262 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3267 * Signal the userland master nfsd to backup the stable restart file.
3270 nfsrv_backupstable(void)
3274 if (nfsd_master_proc != NULL) {
3275 procp = pfind(nfsd_master_pid);
3276 /* Try to make sure it is the correct process. */
3277 if (procp == nfsd_master_proc &&
3278 procp->p_stats->p_start.tv_sec ==
3279 nfsd_master_start.tv_sec &&
3280 procp->p_stats->p_start.tv_usec ==
3281 nfsd_master_start.tv_usec &&
3282 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3283 kern_psignal(procp, SIGUSR2);
3285 nfsd_master_proc = NULL;
3292 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3295 * Called once to initialize data structures...
3298 nfsd_modevent(module_t mod, int type, void *data)
3301 static int loaded = 0;
3308 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3309 snprintf(nfsrchash_table[i].lock_name,
3310 sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d",
3312 mtx_init(&nfsrchash_table[i].mtx,
3313 nfsrchash_table[i].lock_name, NULL, MTX_DEF);
3314 snprintf(nfsrcahash_table[i].lock_name,
3315 sizeof(nfsrcahash_table[i].lock_name), "nfsrc_tcpa%d",
3317 mtx_init(&nfsrcahash_table[i].mtx,
3318 nfsrcahash_table[i].lock_name, NULL, MTX_DEF);
3320 mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF);
3321 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3322 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3324 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3331 #ifdef VV_DISABLEDELEG
3332 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3333 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3335 nfsd_call_servertimer = nfsrv_servertimer;
3336 nfsd_call_nfsd = nfssvc_nfsd;
3341 if (newnfs_numnfsd != 0) {
3346 #ifdef VV_DISABLEDELEG
3347 vn_deleg_ops.vndeleg_recall = NULL;
3348 vn_deleg_ops.vndeleg_disable = NULL;
3350 nfsd_call_servertimer = NULL;
3351 nfsd_call_nfsd = NULL;
3353 /* Clean out all NFSv4 state. */
3354 nfsrv_throwawayallstate(curthread);
3356 /* Clean the NFS server reply cache */
3357 nfsrvd_cleancache();
3359 /* Free up the krpc server pool. */
3360 if (nfsrvd_pool != NULL)
3361 svcpool_destroy(nfsrvd_pool);
3363 /* and get rid of the locks */
3364 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3365 mtx_destroy(&nfsrchash_table[i].mtx);
3366 mtx_destroy(&nfsrcahash_table[i].mtx);
3368 mtx_destroy(&nfsrc_udpmtx);
3369 mtx_destroy(&nfs_v4root_mutex);
3370 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3371 lockdestroy(&nfsv4root_mnt.mnt_explock);
3383 static moduledata_t nfsd_mod = {
3388 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3390 /* So that loader and kldload(2) can find us, wherever we are.. */
3391 MODULE_VERSION(nfsd, 1);
3392 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3393 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3394 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3395 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3396 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);