2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. 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/capsicum.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 extern struct nfsclienthashhead *nfsclienthash;
62 extern struct nfslockhashhead *nfslockhash;
63 extern struct nfssessionhash *nfssessionhash;
64 extern int nfsrv_sessionhashsize;
65 extern struct nfsstatsv1 nfsstatsv1;
66 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
68 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
69 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
70 struct mtx nfsrc_udpmtx;
71 struct mtx nfs_v4root_mutex;
72 struct nfsrvfh nfs_rootfh, nfs_pubfh;
73 int nfs_pubfhset = 0, nfs_rootfhset = 0;
74 struct proc *nfsd_master_proc = NULL;
75 int nfsd_debuglevel = 0;
76 static pid_t nfsd_master_pid = (pid_t)-1;
77 static char nfsd_master_comm[MAXCOMLEN + 1];
78 static struct timeval nfsd_master_start;
79 static uint32_t nfsv4_sysid = 0;
81 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
84 int nfsrv_enable_crossmntpt = 1;
85 static int nfs_commit_blks;
86 static int nfs_commit_miss;
87 extern int nfsrv_issuedelegs;
88 extern int nfsrv_dolocallocks;
89 extern int nfsd_enable_stringtouid;
90 extern int nfsd_enable_uidtostring;
92 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server");
93 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
94 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
95 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
97 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
100 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
101 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
102 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
103 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
104 0, "Debug level for NFS server");
105 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
106 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
107 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_uidtostring, CTLFLAG_RW,
108 &nfsd_enable_uidtostring, 0, "Make nfsd always send numeric owner_names");
110 #define MAX_REORDERED_RPC 16
111 #define NUM_HEURISTIC 1031
112 #define NHUSE_INIT 64
114 #define NHUSE_MAX 2048
116 static struct nfsheur {
117 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
118 off_t nh_nextoff; /* next offset for sequential detection */
119 int nh_use; /* use count for selection */
120 int nh_seqcount; /* heuristic */
121 } nfsheur[NUM_HEURISTIC];
125 * Heuristic to detect sequential operation.
127 static struct nfsheur *
128 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
133 /* Locate best candidate. */
135 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
138 if (nfsheur[hi].nh_vp == vp) {
142 if (nfsheur[hi].nh_use > 0)
143 --nfsheur[hi].nh_use;
144 hi = (hi + 1) % NUM_HEURISTIC;
145 if (nfsheur[hi].nh_use < nh->nh_use)
149 /* Initialize hint if this is a new file. */
150 if (nh->nh_vp != vp) {
152 nh->nh_nextoff = uio->uio_offset;
153 nh->nh_use = NHUSE_INIT;
154 if (uio->uio_offset == 0)
160 /* Calculate heuristic. */
161 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
162 uio->uio_offset == nh->nh_nextoff) {
163 /* See comments in vfs_vnops.c:sequential_heuristic(). */
164 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
165 if (nh->nh_seqcount > IO_SEQMAX)
166 nh->nh_seqcount = IO_SEQMAX;
167 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
168 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
169 /* Probably a reordered RPC, leave seqcount alone. */
170 } else if (nh->nh_seqcount > 1) {
171 nh->nh_seqcount /= 2;
175 nh->nh_use += NHUSE_INC;
176 if (nh->nh_use > NHUSE_MAX)
177 nh->nh_use = NHUSE_MAX;
182 * Get attributes into nfsvattr structure.
185 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
186 struct thread *p, int vpislocked)
188 int error, lockedit = 0;
190 if (vpislocked == 0) {
192 * When vpislocked == 0, the vnode is either exclusively
193 * locked by this thread or not locked by this thread.
194 * As such, shared lock it, if not exclusively locked.
196 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
198 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
201 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
210 * Get a file handle for a vnode.
213 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
217 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
218 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
219 error = VOP_VPTOFH(vp, &fhp->fh_fid);
226 * Perform access checking for vnodes obtained from file handles that would
227 * refer to files already opened by a Unix client. You cannot just use
228 * vn_writechk() and VOP_ACCESSX() for two reasons.
229 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
231 * 2 - The owner is to be given access irrespective of mode bits for some
232 * operations, so that processes that chmod after opening a file don't
236 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
237 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
238 u_int32_t *supportedtypep)
241 int error = 0, getret = 0;
243 if (vpislocked == 0) {
244 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
249 if (accmode & VWRITE) {
250 /* Just vn_writechk() changed to check rdonly */
252 * Disallow write attempts on read-only file systems;
253 * unless the file is a socket or a block or character
254 * device resident on the file system.
256 if (NFSVNO_EXRDONLY(exp) ||
257 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
258 switch (vp->v_type) {
268 * If there's shared text associated with
269 * the inode, try to free it up once. If
270 * we fail, we can't allow writing.
272 if (VOP_IS_TEXT(vp) && error == 0)
282 * Should the override still be applied when ACLs are enabled?
284 error = VOP_ACCESSX(vp, accmode, cred, p);
285 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
287 * Try again with VEXPLICIT_DENY, to see if the test for
288 * deletion is supported.
290 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
292 if (vp->v_type == VDIR) {
293 accmode &= ~(VDELETE | VDELETE_CHILD);
295 error = VOP_ACCESSX(vp, accmode, cred, p);
296 } else if (supportedtypep != NULL) {
297 *supportedtypep &= ~NFSACCESS_DELETE;
303 * Allow certain operations for the owner (reads and writes
304 * on files that are already open).
306 if (override != NFSACCCHK_NOOVERRIDE &&
307 (error == EPERM || error == EACCES)) {
308 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
310 else if (override & NFSACCCHK_ALLOWOWNER) {
311 getret = VOP_GETATTR(vp, &vattr, cred);
312 if (getret == 0 && cred->cr_uid == vattr.va_uid)
325 * Set attribute(s) vnop.
328 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
329 struct thread *p, struct nfsexstuff *exp)
333 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
339 * Set up nameidata for a lookup() call and do it.
342 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
343 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
344 struct vnode **retdirp)
346 struct componentname *cnp = &ndp->ni_cnd;
350 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
351 int error = 0, crossmnt;
355 cnp->cn_nameptr = cnp->cn_pnbuf;
358 * Extract and set starting directory.
360 if (dp->v_type != VDIR) {
365 nfsvno_relpathbuf(ndp);
373 if (NFSVNO_EXRDONLY(exp))
374 cnp->cn_flags |= RDONLY;
375 ndp->ni_segflg = UIO_SYSSPACE;
378 if (nd->nd_flag & ND_PUBLOOKUP) {
380 if (cnp->cn_pnbuf[0] == '/') {
383 * Check for degenerate pathnames here, since lookup()
386 for (i = 1; i < ndp->ni_pathlen; i++)
387 if (cnp->cn_pnbuf[i] != '/')
389 if (i == ndp->ni_pathlen) {
390 error = NFSERR_ACCES;
396 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
397 (nd->nd_flag & ND_NFSV4) == 0) {
399 * Only cross mount points for NFSv4 when doing a
400 * mount while traversing the file system above
401 * the mount point, unless nfsrv_enable_crossmntpt is set.
403 cnp->cn_flags |= NOCROSSMOUNT;
408 * Initialize for scan, set ni_startdir and bump ref on dp again
409 * because lookup() will dereference ni_startdir.
413 ndp->ni_startdir = dp;
414 ndp->ni_rootdir = rootvnode;
415 ndp->ni_topdir = NULL;
418 cnp->cn_flags |= LOCKLEAF;
420 cnp->cn_nameptr = cnp->cn_pnbuf;
422 * Call lookup() to do the real work. If an error occurs,
423 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
424 * we do not have to dereference anything before returning.
425 * In either case ni_startdir will be dereferenced and NULLed
433 * Check for encountering a symbolic link. Trivial
434 * termination occurs if no symlink encountered.
436 if ((cnp->cn_flags & ISSYMLINK) == 0) {
437 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
438 nfsvno_relpathbuf(ndp);
439 if (ndp->ni_vp && !lockleaf)
440 NFSVOPUNLOCK(ndp->ni_vp, 0);
447 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
448 NFSVOPUNLOCK(ndp->ni_dvp, 0);
449 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
454 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
458 if (ndp->ni_pathlen > 1)
459 cp = uma_zalloc(namei_zone, M_WAITOK);
463 aiov.iov_len = MAXPATHLEN;
464 auio.uio_iov = &aiov;
467 auio.uio_rw = UIO_READ;
468 auio.uio_segflg = UIO_SYSSPACE;
470 auio.uio_resid = MAXPATHLEN;
471 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
474 if (ndp->ni_pathlen > 1)
475 uma_zfree(namei_zone, cp);
481 linklen = MAXPATHLEN - auio.uio_resid;
486 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
487 error = ENAMETOOLONG;
492 * Adjust or replace path
494 if (ndp->ni_pathlen > 1) {
495 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
496 uma_zfree(namei_zone, cnp->cn_pnbuf);
499 cnp->cn_pnbuf[linklen] = '\0';
500 ndp->ni_pathlen += linklen;
503 * Cleanup refs for next loop and check if root directory
504 * should replace current directory. Normally ni_dvp
505 * becomes the new base directory and is cleaned up when
506 * we loop. Explicitly null pointers after invalidation
507 * to clarify operation.
512 if (cnp->cn_pnbuf[0] == '/') {
514 ndp->ni_dvp = ndp->ni_rootdir;
517 ndp->ni_startdir = ndp->ni_dvp;
521 cnp->cn_flags &= ~LOCKLEAF;
525 nfsvno_relpathbuf(ndp);
528 ndp->ni_startdir = NULL;
529 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
534 NFSEXITCODE2(error, nd);
539 * Set up a pathname buffer and return a pointer to it and, optionally
540 * set a hash pointer.
543 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
545 struct componentname *cnp = &ndp->ni_cnd;
547 cnp->cn_flags |= (NOMACCHECK | HASBUF);
548 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
551 *bufpp = cnp->cn_pnbuf;
555 * Release the above path buffer, if not released by nfsvno_namei().
558 nfsvno_relpathbuf(struct nameidata *ndp)
561 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
563 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
564 ndp->ni_cnd.cn_flags &= ~HASBUF;
568 * Readlink vnode op into an mbuf list.
571 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
572 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
574 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
575 struct iovec *ivp = iv;
576 struct uio io, *uiop = &io;
577 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
578 int i, len, tlen, error = 0;
582 while (len < NFS_MAXPATHLEN) {
584 MCLGET(mp, M_WAITOK);
585 mp->m_len = M_SIZE(mp);
592 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
593 mp->m_len = NFS_MAXPATHLEN - len;
594 len = NFS_MAXPATHLEN;
598 ivp->iov_base = mtod(mp, caddr_t);
599 ivp->iov_len = mp->m_len;
604 uiop->uio_iovcnt = i;
605 uiop->uio_offset = 0;
606 uiop->uio_resid = len;
607 uiop->uio_rw = UIO_READ;
608 uiop->uio_segflg = UIO_SYSSPACE;
610 error = VOP_READLINK(vp, uiop, cred);
616 if (uiop->uio_resid > 0) {
617 len -= uiop->uio_resid;
618 tlen = NFSM_RNDUP(len);
619 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
631 * Read vnode op call into mbuf list.
634 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
635 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
641 int error = 0, len, left, siz, tlen, ioflag = 0;
642 struct mbuf *m2 = NULL, *m3;
643 struct uio io, *uiop = &io;
646 len = left = NFSM_RNDUP(cnt);
649 * Generate the mbuf list with the uio_iov ref. to it.
656 siz = min(M_TRAILINGSPACE(m), left);
665 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
667 uiop->uio_iov = iv2 = iv;
673 panic("nfsvno_read iov");
674 siz = min(M_TRAILINGSPACE(m), left);
676 iv->iov_base = mtod(m, caddr_t) + m->m_len;
685 uiop->uio_iovcnt = i;
686 uiop->uio_offset = off;
687 uiop->uio_resid = len;
688 uiop->uio_rw = UIO_READ;
689 uiop->uio_segflg = UIO_SYSSPACE;
691 nh = nfsrv_sequential_heuristic(uiop, vp);
692 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
693 /* XXX KDM make this more systematic? */
694 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
695 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
696 FREE((caddr_t)iv2, M_TEMP);
702 nh->nh_nextoff = uiop->uio_offset;
703 tlen = len - uiop->uio_resid;
704 cnt = cnt < tlen ? cnt : tlen;
705 tlen = NFSM_RNDUP(cnt);
709 } else if (len != tlen || tlen != cnt)
710 nfsrv_adj(m3, len - tlen, tlen - cnt);
720 * Write vnode op from an mbuf list.
723 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
724 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
730 struct uio io, *uiop = &io;
733 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
735 uiop->uio_iov = iv = ivp;
736 uiop->uio_iovcnt = cnt;
737 i = mtod(mp, caddr_t) + mp->m_len - cp;
741 panic("nfsvno_write");
752 cp = mtod(mp, caddr_t);
756 if (stable == NFSWRITE_UNSTABLE)
757 ioflags = IO_NODELOCKED;
759 ioflags = (IO_SYNC | IO_NODELOCKED);
760 uiop->uio_resid = retlen;
761 uiop->uio_rw = UIO_WRITE;
762 uiop->uio_segflg = UIO_SYSSPACE;
764 uiop->uio_offset = off;
765 nh = nfsrv_sequential_heuristic(uiop, vp);
766 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
767 /* XXX KDM make this more systematic? */
768 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
769 error = VOP_WRITE(vp, uiop, ioflags, cred);
771 nh->nh_nextoff = uiop->uio_offset;
772 FREE((caddr_t)iv, M_TEMP);
779 * Common code for creating a regular file (plus special files for V2).
782 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
783 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
784 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
789 error = nd->nd_repstat;
790 if (!error && ndp->ni_vp == NULL) {
791 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
792 vrele(ndp->ni_startdir);
793 error = VOP_CREATE(ndp->ni_dvp,
794 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
796 nfsvno_relpathbuf(ndp);
798 if (*exclusive_flagp) {
799 *exclusive_flagp = 0;
800 NFSVNO_ATTRINIT(nvap);
801 nvap->na_atime.tv_sec = cverf[0];
802 nvap->na_atime.tv_nsec = cverf[1];
803 error = VOP_SETATTR(ndp->ni_vp,
804 &nvap->na_vattr, nd->nd_cred);
808 error = NFSERR_NOTSUPP;
813 * NFS V2 Only. nfsrvd_mknod() does this for V3.
814 * (This implies, just get out on an error.)
816 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
817 nvap->na_type == VFIFO) {
818 if (nvap->na_type == VCHR && rdev == 0xffffffff)
819 nvap->na_type = VFIFO;
820 if (nvap->na_type != VFIFO &&
821 (error = priv_check_cred(nd->nd_cred,
822 PRIV_VFS_MKNOD_DEV, 0))) {
823 vrele(ndp->ni_startdir);
824 nfsvno_relpathbuf(ndp);
828 nvap->na_rdev = rdev;
829 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
830 &ndp->ni_cnd, &nvap->na_vattr);
832 nfsvno_relpathbuf(ndp);
833 vrele(ndp->ni_startdir);
837 vrele(ndp->ni_startdir);
838 nfsvno_relpathbuf(ndp);
846 * Handle cases where error is already set and/or
848 * 1 - clean up the lookup
849 * 2 - iff !error and na_size set, truncate it
851 vrele(ndp->ni_startdir);
852 nfsvno_relpathbuf(ndp);
854 if (ndp->ni_dvp == *vpp)
858 if (!error && nvap->na_size != VNOVAL) {
859 error = nfsvno_accchk(*vpp, VWRITE,
860 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
861 NFSACCCHK_VPISLOCKED, NULL);
863 tempsize = nvap->na_size;
864 NFSVNO_ATTRINIT(nvap);
865 nvap->na_size = tempsize;
866 error = VOP_SETATTR(*vpp,
867 &nvap->na_vattr, nd->nd_cred);
880 * Do a mknod vnode op.
883 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
889 vtyp = nvap->na_type;
891 * Iff doesn't exist, create it.
894 vrele(ndp->ni_startdir);
895 nfsvno_relpathbuf(ndp);
901 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
902 vrele(ndp->ni_startdir);
903 nfsvno_relpathbuf(ndp);
905 error = NFSERR_BADTYPE;
909 vrele(ndp->ni_startdir);
910 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
911 &ndp->ni_cnd, &nvap->na_vattr);
913 nfsvno_relpathbuf(ndp);
915 if (nvap->na_type != VFIFO &&
916 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
917 vrele(ndp->ni_startdir);
918 nfsvno_relpathbuf(ndp);
922 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
923 &ndp->ni_cnd, &nvap->na_vattr);
925 nfsvno_relpathbuf(ndp);
926 vrele(ndp->ni_startdir);
928 * Since VOP_MKNOD returns the ni_vp, I can't
929 * see any reason to do the lookup.
942 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
943 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
947 if (ndp->ni_vp != NULL) {
948 if (ndp->ni_dvp == ndp->ni_vp)
953 nfsvno_relpathbuf(ndp);
957 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
960 nfsvno_relpathbuf(ndp);
971 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
972 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
973 struct nfsexstuff *exp)
978 vrele(ndp->ni_startdir);
979 nfsvno_relpathbuf(ndp);
980 if (ndp->ni_dvp == ndp->ni_vp)
989 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
990 &nvap->na_vattr, pathcp);
992 vrele(ndp->ni_startdir);
993 nfsvno_relpathbuf(ndp);
995 * Although FreeBSD still had the lookup code in
996 * it for 7/current, there doesn't seem to be any
997 * point, since VOP_SYMLINK() returns the ni_vp.
998 * Just vput it for v2.
1000 if (!not_v2 && !error)
1009 * Parse symbolic link arguments.
1010 * This function has an ugly side effect. It will MALLOC() an area for
1011 * the symlink and set iov_base to point to it, only if it succeeds.
1012 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1016 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1017 struct thread *p, char **pathcpp, int *lenp)
1020 char *pathcp = NULL;
1022 struct nfsv2_sattr *sp;
1026 if ((nd->nd_flag & ND_NFSV3) &&
1027 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1029 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1030 len = fxdr_unsigned(int, *tl);
1031 if (len > NFS_MAXPATHLEN || len <= 0) {
1035 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1036 error = nfsrv_mtostr(nd, pathcp, len);
1039 if (nd->nd_flag & ND_NFSV2) {
1040 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1041 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1045 NFSEXITCODE2(0, nd);
1049 free(pathcp, M_TEMP);
1050 NFSEXITCODE2(error, nd);
1055 * Remove a non-directory object.
1058 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1059 struct thread *p, struct nfsexstuff *exp)
1065 if (vp->v_type == VDIR)
1066 error = NFSERR_ISDIR;
1068 error = nfsrv_checkremove(vp, 1, p);
1070 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1071 if (ndp->ni_dvp == vp)
1076 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1077 nfsvno_relpathbuf(ndp);
1083 * Remove a directory.
1086 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1087 struct thread *p, struct nfsexstuff *exp)
1093 if (vp->v_type != VDIR) {
1098 * No rmdir "." please.
1100 if (ndp->ni_dvp == vp) {
1105 * The root of a mounted filesystem cannot be deleted.
1107 if (vp->v_vflag & VV_ROOT)
1111 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1112 if (ndp->ni_dvp == vp)
1117 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1118 nfsvno_relpathbuf(ndp);
1127 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1128 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1130 struct vnode *fvp, *tvp, *tdvp;
1133 fvp = fromndp->ni_vp;
1135 vrele(fromndp->ni_dvp);
1140 tdvp = tondp->ni_dvp;
1143 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1144 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1146 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1147 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1150 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1151 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1156 * A rename to '.' or '..' results in a prematurely
1157 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1160 if ((tondp->ni_cnd.cn_namelen == 1 &&
1161 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1162 (tondp->ni_cnd.cn_namelen == 2 &&
1163 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1164 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1169 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1170 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1173 if (fvp->v_mount != tdvp->v_mount) {
1174 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1178 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1183 * If source and destination are the same, there is nothing to
1184 * do. Set error to -1 to indicate this.
1189 if (ndflag & ND_NFSV4) {
1190 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1191 error = nfsrv_checkremove(fvp, 0, p);
1192 NFSVOPUNLOCK(fvp, 0);
1196 error = nfsrv_checkremove(tvp, 1, p);
1199 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1200 * that the NFSv4 client won't be confused by the rename.
1201 * Since nfsd_recalldelegation() can only be called on an
1202 * unlocked vnode at this point and fvp is the file that will
1203 * still exist after the rename, just do fvp.
1205 nfsd_recalldelegation(fvp, p);
1209 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1210 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1219 vrele(fromndp->ni_dvp);
1224 vrele(tondp->ni_startdir);
1225 nfsvno_relpathbuf(tondp);
1227 vrele(fromndp->ni_startdir);
1228 nfsvno_relpathbuf(fromndp);
1237 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1238 struct thread *p, struct nfsexstuff *exp)
1248 if (vp->v_mount != xp->v_mount)
1252 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1253 if ((vp->v_iflag & VI_DOOMED) == 0)
1254 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1257 if (ndp->ni_dvp == vp)
1261 NFSVOPUNLOCK(vp, 0);
1263 if (ndp->ni_dvp == ndp->ni_vp)
1270 nfsvno_relpathbuf(ndp);
1276 * Do the fsync() appropriate for the commit.
1279 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1285 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1286 * file is done. At this time VOP_FSYNC does not accept offset and
1287 * byte count parameters so call VOP_FSYNC the whole file for now.
1288 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1289 * File systems that do not use the buffer cache (as indicated
1290 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1292 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1293 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1295 * Give up and do the whole thing
1298 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1299 VM_OBJECT_WLOCK(vp->v_object);
1300 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1301 VM_OBJECT_WUNLOCK(vp->v_object);
1303 error = VOP_FSYNC(vp, MNT_WAIT, td);
1306 * Locate and synchronously write any buffers that fall
1307 * into the requested range. Note: we are assuming that
1308 * f_iosize is a power of 2.
1310 int iosize = vp->v_mount->mnt_stat.f_iosize;
1311 int iomask = iosize - 1;
1316 * Align to iosize boundary, super-align to page boundary.
1319 cnt += off & iomask;
1320 off &= ~(u_quad_t)iomask;
1322 if (off & PAGE_MASK) {
1323 cnt += off & PAGE_MASK;
1324 off &= ~(u_quad_t)PAGE_MASK;
1326 lblkno = off / iosize;
1329 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1330 VM_OBJECT_WLOCK(vp->v_object);
1331 vm_object_page_clean(vp->v_object, off, off + cnt,
1333 VM_OBJECT_WUNLOCK(vp->v_object);
1342 * If we have a buffer and it is marked B_DELWRI we
1343 * have to lock and write it. Otherwise the prior
1344 * write is assumed to have already been committed.
1346 * gbincore() can return invalid buffers now so we
1347 * have to check that bit as well (though B_DELWRI
1348 * should not be set if B_INVAL is set there could be
1349 * a race here since we haven't locked the buffer).
1351 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1352 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1353 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1355 continue; /* retry */
1357 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1360 bp->b_flags &= ~B_ASYNC;
1383 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1387 error = VFS_STATFS(vp->v_mount, sf);
1390 * Since NFS handles these values as unsigned on the
1391 * wire, there is no way to represent negative values,
1392 * so set them to 0. Without this, they will appear
1393 * to be very large positive values for clients like
1396 if (sf->f_bavail < 0)
1398 if (sf->f_ffree < 0)
1406 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1407 * must handle nfsrv_opencheck() calls after any other access checks.
1410 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1411 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1412 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1413 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1414 struct nfsexstuff *exp, struct vnode **vpp)
1416 struct vnode *vp = NULL;
1418 struct nfsexstuff nes;
1420 if (ndp->ni_vp == NULL)
1421 nd->nd_repstat = nfsrv_opencheck(clientid,
1422 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1423 if (!nd->nd_repstat) {
1424 if (ndp->ni_vp == NULL) {
1425 vrele(ndp->ni_startdir);
1426 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1427 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1429 nfsvno_relpathbuf(ndp);
1430 if (!nd->nd_repstat) {
1431 if (*exclusive_flagp) {
1432 *exclusive_flagp = 0;
1433 NFSVNO_ATTRINIT(nvap);
1434 nvap->na_atime.tv_sec = cverf[0];
1435 nvap->na_atime.tv_nsec = cverf[1];
1436 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1437 &nvap->na_vattr, cred);
1438 if (nd->nd_repstat != 0) {
1441 nd->nd_repstat = NFSERR_NOTSUPP;
1443 NFSSETBIT_ATTRBIT(attrbitp,
1444 NFSATTRBIT_TIMEACCESS);
1446 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1447 aclp, p, attrbitp, exp);
1452 if (ndp->ni_startdir)
1453 vrele(ndp->ni_startdir);
1454 nfsvno_relpathbuf(ndp);
1456 if (create == NFSV4OPEN_CREATE) {
1457 if (ndp->ni_dvp == vp)
1462 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1463 if (ndp->ni_cnd.cn_flags & RDONLY)
1464 NFSVNO_SETEXRDONLY(&nes);
1466 NFSVNO_EXINIT(&nes);
1467 nd->nd_repstat = nfsvno_accchk(vp,
1468 VWRITE, cred, &nes, p,
1469 NFSACCCHK_NOOVERRIDE,
1470 NFSACCCHK_VPISLOCKED, NULL);
1471 nd->nd_repstat = nfsrv_opencheck(clientid,
1472 stateidp, stp, vp, nd, p, nd->nd_repstat);
1473 if (!nd->nd_repstat) {
1474 tempsize = nvap->na_size;
1475 NFSVNO_ATTRINIT(nvap);
1476 nvap->na_size = tempsize;
1477 nd->nd_repstat = VOP_SETATTR(vp,
1478 &nvap->na_vattr, cred);
1480 } else if (vp->v_type == VREG) {
1481 nd->nd_repstat = nfsrv_opencheck(clientid,
1482 stateidp, stp, vp, nd, p, nd->nd_repstat);
1486 if (ndp->ni_cnd.cn_flags & HASBUF)
1487 nfsvno_relpathbuf(ndp);
1488 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1489 vrele(ndp->ni_startdir);
1490 if (ndp->ni_dvp == ndp->ni_vp)
1500 NFSEXITCODE2(0, nd);
1504 * Updates the file rev and sets the mtime and ctime
1505 * to the current clock time, returning the va_filerev and va_Xtime
1507 * Return ESTALE to indicate the vnode is VI_DOOMED.
1510 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1511 struct ucred *cred, struct thread *p)
1516 vfs_timestamp(&va.va_mtime);
1517 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1518 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1519 if ((vp->v_iflag & VI_DOOMED) != 0)
1522 (void) VOP_SETATTR(vp, &va, cred);
1523 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1528 * Glue routine to nfsv4_fillattr().
1531 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1532 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1533 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1534 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1538 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1539 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1541 NFSEXITCODE2(0, nd);
1545 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1547 * nfs readdir service
1548 * - mallocs what it thinks is enough to read
1549 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1550 * - calls VOP_READDIR()
1551 * - loops around building the reply
1552 * if the output generated exceeds count break out of loop
1553 * The NFSM_CLGET macro is used here so that the reply will be packed
1554 * tightly in mbuf clusters.
1555 * - it trims out records with d_fileno == 0
1556 * this doesn't matter for Unix clients, but they might confuse clients
1558 * - it trims out records with d_type == DT_WHT
1559 * these cannot be seen through NFS (unless we extend the protocol)
1560 * The alternate call nfsrvd_readdirplus() does lookups as well.
1561 * PS: The NFS protocol spec. does not clarify what the "count" byte
1562 * argument is a count of.. just name strings and file id's or the
1563 * entire reply rpc or ...
1564 * I tried just file name and id sizes and it confused the Sun client,
1565 * so I am using the full rpc size now. The "paranoia.." comment refers
1566 * to including the status longwords that are not a part of the dir.
1567 * "entry" structures, but are in the rpc.
1570 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1571 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1576 char *cpos, *cend, *rbuf;
1578 int nlen, error = 0, getret = 1;
1579 int siz, cnt, fullsiz, eofflag, ncookies;
1580 u_int64_t off, toff, verf;
1581 u_long *cookies = NULL, *cookiep;
1586 if (nd->nd_repstat) {
1587 nfsrv_postopattr(nd, getret, &at);
1590 if (nd->nd_flag & ND_NFSV2) {
1591 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1592 off = fxdr_unsigned(u_quad_t, *tl++);
1594 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1595 off = fxdr_hyper(tl);
1597 verf = fxdr_hyper(tl);
1601 cnt = fxdr_unsigned(int, *tl);
1602 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1603 cnt = NFS_SRVMAXDATA(nd);
1604 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1606 if (nd->nd_flag & ND_NFSV3) {
1607 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1611 * va_filerev is not sufficient as a cookie verifier,
1612 * since it is not supposed to change when entries are
1613 * removed/added unless that offset cookies returned to
1614 * the client are no longer valid.
1616 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1617 nd->nd_repstat = NFSERR_BAD_COOKIE;
1620 if (!nd->nd_repstat && vp->v_type != VDIR)
1621 nd->nd_repstat = NFSERR_NOTDIR;
1622 if (nd->nd_repstat == 0 && cnt == 0) {
1623 if (nd->nd_flag & ND_NFSV2)
1624 /* NFSv2 does not have NFSERR_TOOSMALL */
1625 nd->nd_repstat = EPERM;
1627 nd->nd_repstat = NFSERR_TOOSMALL;
1629 if (!nd->nd_repstat)
1630 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1631 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1632 NFSACCCHK_VPISLOCKED, NULL);
1633 if (nd->nd_repstat) {
1635 if (nd->nd_flag & ND_NFSV3)
1636 nfsrv_postopattr(nd, getret, &at);
1639 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1640 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1644 free((caddr_t)cookies, M_TEMP);
1652 io.uio_offset = (off_t)off;
1654 io.uio_segflg = UIO_SYSSPACE;
1655 io.uio_rw = UIO_READ;
1657 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1659 off = (u_int64_t)io.uio_offset;
1661 siz -= io.uio_resid;
1663 if (!cookies && !nd->nd_repstat)
1664 nd->nd_repstat = NFSERR_PERM;
1665 if (nd->nd_flag & ND_NFSV3) {
1666 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1667 if (!nd->nd_repstat)
1668 nd->nd_repstat = getret;
1672 * Handles the failed cases. nd->nd_repstat == 0 past here.
1674 if (nd->nd_repstat) {
1676 free((caddr_t)rbuf, M_TEMP);
1678 free((caddr_t)cookies, M_TEMP);
1679 if (nd->nd_flag & ND_NFSV3)
1680 nfsrv_postopattr(nd, getret, &at);
1684 * If nothing read, return eof
1689 if (nd->nd_flag & ND_NFSV2) {
1690 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1692 nfsrv_postopattr(nd, getret, &at);
1693 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1694 txdr_hyper(at.na_filerev, tl);
1697 *tl++ = newnfs_false;
1699 FREE((caddr_t)rbuf, M_TEMP);
1700 FREE((caddr_t)cookies, M_TEMP);
1705 * Check for degenerate cases of nothing useful read.
1706 * If so go try again
1710 dp = (struct dirent *)cpos;
1714 * For some reason FreeBSD's ufs_readdir() chooses to back the
1715 * directory offset up to a block boundary, so it is necessary to
1716 * skip over the records that precede the requested offset. This
1717 * requires the assumption that file offset cookies monotonically
1720 while (cpos < cend && ncookies > 0 &&
1721 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1722 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1723 cpos += dp->d_reclen;
1724 dp = (struct dirent *)cpos;
1728 if (cpos >= cend || ncookies == 0) {
1736 * dirlen is the size of the reply, including all XDR and must
1737 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1738 * if the XDR should be included in "count", but to be safe, we do.
1739 * (Include the two booleans at the end of the reply in dirlen now.)
1741 if (nd->nd_flag & ND_NFSV3) {
1742 nfsrv_postopattr(nd, getret, &at);
1743 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1744 txdr_hyper(at.na_filerev, tl);
1745 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1747 dirlen = 2 * NFSX_UNSIGNED;
1750 /* Loop through the records and build reply */
1751 while (cpos < cend && ncookies > 0) {
1752 nlen = dp->d_namlen;
1753 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1754 nlen <= NFS_MAXNAMLEN) {
1755 if (nd->nd_flag & ND_NFSV3)
1756 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1758 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1765 * Build the directory record xdr from
1768 if (nd->nd_flag & ND_NFSV3) {
1769 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1770 *tl++ = newnfs_true;
1773 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1774 *tl++ = newnfs_true;
1776 *tl = txdr_unsigned(dp->d_fileno);
1777 (void) nfsm_strtom(nd, dp->d_name, nlen);
1778 if (nd->nd_flag & ND_NFSV3) {
1779 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1782 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1783 *tl = txdr_unsigned(*cookiep);
1785 cpos += dp->d_reclen;
1786 dp = (struct dirent *)cpos;
1792 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1793 *tl++ = newnfs_false;
1798 FREE((caddr_t)rbuf, M_TEMP);
1799 FREE((caddr_t)cookies, M_TEMP);
1802 NFSEXITCODE2(0, nd);
1806 NFSEXITCODE2(error, nd);
1811 * Readdirplus for V3 and Readdir for V4.
1814 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1815 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1820 char *cpos, *cend, *rbuf;
1823 struct nfsvattr nva, at, *nvap = &nva;
1824 struct mbuf *mb0, *mb1;
1825 struct nfsreferral *refp;
1826 int nlen, r, error = 0, getret = 1, usevget = 1;
1827 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1828 caddr_t bpos0, bpos1;
1829 u_int64_t off, toff, verf;
1830 u_long *cookies = NULL, *cookiep;
1831 nfsattrbit_t attrbits, rderrbits, savbits;
1834 struct componentname cn;
1835 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
1836 struct mount *mp, *new_mp;
1837 uint64_t mounted_on_fileno;
1839 if (nd->nd_repstat) {
1840 nfsrv_postopattr(nd, getret, &at);
1843 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1844 off = fxdr_hyper(tl);
1847 verf = fxdr_hyper(tl);
1849 siz = fxdr_unsigned(int, *tl++);
1850 cnt = fxdr_unsigned(int, *tl);
1853 * Use the server's maximum data transfer size as the upper bound
1856 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1857 cnt = NFS_SRVMAXDATA(nd);
1860 * siz is a "hint" of how much directory information (name, fileid,
1861 * cookie) should be in the reply. At least one client "hints" 0,
1862 * so I set it to cnt for that case. I also round it up to the
1863 * next multiple of DIRBLKSIZ.
1867 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1869 if (nd->nd_flag & ND_NFSV4) {
1870 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1873 NFSSET_ATTRBIT(&savbits, &attrbits);
1874 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1875 NFSZERO_ATTRBIT(&rderrbits);
1876 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1878 NFSZERO_ATTRBIT(&attrbits);
1881 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1882 if (!nd->nd_repstat) {
1883 if (off && verf != at.na_filerev) {
1885 * va_filerev is not sufficient as a cookie verifier,
1886 * since it is not supposed to change when entries are
1887 * removed/added unless that offset cookies returned to
1888 * the client are no longer valid.
1891 if (nd->nd_flag & ND_NFSV4) {
1892 nd->nd_repstat = NFSERR_NOTSAME;
1894 nd->nd_repstat = NFSERR_BAD_COOKIE;
1897 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1898 nd->nd_repstat = NFSERR_BAD_COOKIE;
1901 if (!nd->nd_repstat && vp->v_type != VDIR)
1902 nd->nd_repstat = NFSERR_NOTDIR;
1903 if (!nd->nd_repstat && cnt == 0)
1904 nd->nd_repstat = NFSERR_TOOSMALL;
1905 if (!nd->nd_repstat)
1906 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1907 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1908 NFSACCCHK_VPISLOCKED, NULL);
1909 if (nd->nd_repstat) {
1911 if (nd->nd_flag & ND_NFSV3)
1912 nfsrv_postopattr(nd, getret, &at);
1915 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1916 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
1918 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1922 free((caddr_t)cookies, M_TEMP);
1930 io.uio_offset = (off_t)off;
1932 io.uio_segflg = UIO_SYSSPACE;
1933 io.uio_rw = UIO_READ;
1935 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1937 off = (u_int64_t)io.uio_offset;
1939 siz -= io.uio_resid;
1941 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1943 if (!cookies && !nd->nd_repstat)
1944 nd->nd_repstat = NFSERR_PERM;
1945 if (!nd->nd_repstat)
1946 nd->nd_repstat = getret;
1947 if (nd->nd_repstat) {
1950 free((caddr_t)cookies, M_TEMP);
1951 free((caddr_t)rbuf, M_TEMP);
1952 if (nd->nd_flag & ND_NFSV3)
1953 nfsrv_postopattr(nd, getret, &at);
1957 * If nothing read, return eof
1962 if (nd->nd_flag & ND_NFSV3)
1963 nfsrv_postopattr(nd, getret, &at);
1964 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1965 txdr_hyper(at.na_filerev, tl);
1967 *tl++ = newnfs_false;
1969 free((caddr_t)cookies, M_TEMP);
1970 free((caddr_t)rbuf, M_TEMP);
1975 * Check for degenerate cases of nothing useful read.
1976 * If so go try again
1980 dp = (struct dirent *)cpos;
1984 * For some reason FreeBSD's ufs_readdir() chooses to back the
1985 * directory offset up to a block boundary, so it is necessary to
1986 * skip over the records that precede the requested offset. This
1987 * requires the assumption that file offset cookies monotonically
1990 while (cpos < cend && ncookies > 0 &&
1991 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1992 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
1993 ((nd->nd_flag & ND_NFSV4) &&
1994 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1995 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1996 cpos += dp->d_reclen;
1997 dp = (struct dirent *)cpos;
2001 if (cpos >= cend || ncookies == 0) {
2008 * Busy the file system so that the mount point won't go away
2009 * and, as such, VFS_VGET() can be used safely.
2013 NFSVOPUNLOCK(vp, 0);
2014 nd->nd_repstat = vfs_busy(mp, 0);
2016 if (nd->nd_repstat != 0) {
2018 free(cookies, M_TEMP);
2020 if (nd->nd_flag & ND_NFSV3)
2021 nfsrv_postopattr(nd, getret, &at);
2026 * Check to see if entries in this directory can be safely acquired
2027 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2028 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2029 * automount of the snapshot directory that is required will
2031 * This needs to be done here for NFSv4, since NFSv4 never does
2032 * a VFS_VGET() for "." or "..".
2035 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2036 if (r == EOPNOTSUPP) {
2038 cn.cn_nameiop = LOOKUP;
2039 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2040 cn.cn_cred = nd->nd_cred;
2047 * Save this position, in case there is an error before one entry
2051 bpos0 = nd->nd_bpos;
2054 * Fill in the first part of the reply.
2055 * dirlen is the reply length in bytes and cannot exceed cnt.
2056 * (Include the two booleans at the end of the reply in dirlen now,
2057 * so we recognize when we have exceeded cnt.)
2059 if (nd->nd_flag & ND_NFSV3) {
2060 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2061 nfsrv_postopattr(nd, getret, &at);
2063 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2065 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2066 txdr_hyper(at.na_filerev, tl);
2069 * Save this position, in case there is an empty reply needed.
2072 bpos1 = nd->nd_bpos;
2074 /* Loop through the records and build reply */
2076 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2077 nlen = dp->d_namlen;
2078 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2079 nlen <= NFS_MAXNAMLEN &&
2080 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2081 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2082 || (nlen == 1 && dp->d_name[0] != '.'))) {
2084 * Save the current position in the reply, in case
2085 * this entry exceeds cnt.
2088 bpos1 = nd->nd_bpos;
2091 * For readdir_and_lookup get the vnode using
2100 mounted_on_fileno = (uint64_t)dp->d_fileno;
2101 if ((nd->nd_flag & ND_NFSV3) ||
2102 NFSNONZERO_ATTRBIT(&savbits)) {
2103 if (nd->nd_flag & ND_NFSV4)
2104 refp = nfsv4root_getreferral(NULL,
2108 r = VFS_VGET(mp, dp->d_fileno,
2112 if (r == EOPNOTSUPP) {
2115 cn.cn_nameiop = LOOKUP;
2123 cn.cn_nameptr = dp->d_name;
2124 cn.cn_namelen = nlen;
2125 cn.cn_flags = ISLASTCN |
2126 NOFOLLOW | LOCKLEAF;
2128 dp->d_name[0] == '.' &&
2129 dp->d_name[1] == '.')
2132 if (NFSVOPLOCK(vp, LK_SHARED)
2134 nd->nd_repstat = EPERM;
2137 if ((vp->v_vflag & VV_ROOT) != 0
2138 && (cn.cn_flags & ISDOTDOT)
2144 r = VOP_LOOKUP(vp, &nvp,
2153 * For NFSv4, check to see if nvp is
2154 * a mount point and get the mount
2155 * point vnode, as required.
2158 nfsrv_enable_crossmntpt != 0 &&
2159 (nd->nd_flag & ND_NFSV4) != 0 &&
2160 nvp->v_type == VDIR &&
2161 nvp->v_mountedhere != NULL) {
2162 new_mp = nvp->v_mountedhere;
2163 r = vfs_busy(new_mp, 0);
2167 r = VFS_ROOT(new_mp,
2177 ((nd->nd_flag & ND_NFSV3) ||
2178 NFSNONZERO_ATTRBIT(&attrbits))) {
2179 r = nfsvno_getfh(nvp, &nfh, p);
2181 r = nfsvno_getattr(nvp, nvap,
2183 if (r == 0 && is_zfs == 1 &&
2184 nfsrv_enable_crossmntpt != 0 &&
2185 (nd->nd_flag & ND_NFSV4) != 0 &&
2186 nvp->v_type == VDIR &&
2187 vp->v_mount != nvp->v_mount) {
2189 * For a ZFS snapshot, there is a
2190 * pseudo mount that does not set
2191 * v_mountedhere, so it needs to
2192 * be detected via a different
2197 new_mp = nvp->v_mount;
2204 if (!NFSISSET_ATTRBIT(&attrbits,
2205 NFSATTRBIT_RDATTRERROR)) {
2208 if (needs_unbusy != 0)
2217 * Build the directory record xdr
2219 if (nd->nd_flag & ND_NFSV3) {
2220 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2221 *tl++ = newnfs_true;
2223 *tl = txdr_unsigned(dp->d_fileno);
2224 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2225 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2227 *tl = txdr_unsigned(*cookiep);
2228 nfsrv_postopattr(nd, 0, nvap);
2229 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2230 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2234 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2235 *tl++ = newnfs_true;
2237 *tl = txdr_unsigned(*cookiep);
2238 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2240 supports_nfsv4acls =
2241 nfs_supportsnfsv4acls(nvp);
2242 NFSVOPUNLOCK(nvp, 0);
2244 supports_nfsv4acls = 0;
2246 dirlen += nfsrv_putreferralattr(nd,
2249 if (nd->nd_repstat) {
2252 if (needs_unbusy != 0)
2257 dirlen += nfsvno_fillattr(nd, new_mp,
2258 nvp, nvap, &nfh, r, &rderrbits,
2259 nd->nd_cred, p, isdgram, 0,
2260 supports_nfsv4acls, at_root,
2263 dirlen += nfsvno_fillattr(nd, new_mp,
2264 nvp, nvap, &nfh, r, &attrbits,
2265 nd->nd_cred, p, isdgram, 0,
2266 supports_nfsv4acls, at_root,
2271 dirlen += (3 * NFSX_UNSIGNED);
2273 if (needs_unbusy != 0)
2278 cpos += dp->d_reclen;
2279 dp = (struct dirent *)cpos;
2287 * If dirlen > cnt, we must strip off the last entry. If that
2288 * results in an empty reply, report NFSERR_TOOSMALL.
2290 if (dirlen > cnt || nd->nd_repstat) {
2291 if (!nd->nd_repstat && entrycnt == 0)
2292 nd->nd_repstat = NFSERR_TOOSMALL;
2293 if (nd->nd_repstat) {
2294 newnfs_trimtrailing(nd, mb0, bpos0);
2295 if (nd->nd_flag & ND_NFSV3)
2296 nfsrv_postopattr(nd, getret, &at);
2298 newnfs_trimtrailing(nd, mb1, bpos1);
2300 } else if (cpos < cend)
2302 if (!nd->nd_repstat) {
2303 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2304 *tl++ = newnfs_false;
2310 FREE((caddr_t)cookies, M_TEMP);
2311 FREE((caddr_t)rbuf, M_TEMP);
2314 NFSEXITCODE2(0, nd);
2318 NFSEXITCODE2(error, nd);
2323 * Get the settable attributes out of the mbuf list.
2324 * (Return 0 or EBADRPC)
2327 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2328 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2331 struct nfsv2_sattr *sp;
2332 int error = 0, toclient = 0;
2334 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2336 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2338 * Some old clients didn't fill in the high order 16bits.
2339 * --> check the low order 2 bytes for 0xffff
2341 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2342 nvap->na_mode = nfstov_mode(sp->sa_mode);
2343 if (sp->sa_uid != newnfs_xdrneg1)
2344 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2345 if (sp->sa_gid != newnfs_xdrneg1)
2346 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2347 if (sp->sa_size != newnfs_xdrneg1)
2348 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2349 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2351 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2353 nvap->na_atime.tv_sec =
2354 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2355 nvap->na_atime.tv_nsec = 0;
2358 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2359 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2362 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2363 if (*tl == newnfs_true) {
2364 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2365 nvap->na_mode = nfstov_mode(*tl);
2367 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2368 if (*tl == newnfs_true) {
2369 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2370 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2372 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2373 if (*tl == newnfs_true) {
2374 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2375 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2377 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2378 if (*tl == newnfs_true) {
2379 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2380 nvap->na_size = fxdr_hyper(tl);
2382 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2383 switch (fxdr_unsigned(int, *tl)) {
2384 case NFSV3SATTRTIME_TOCLIENT:
2385 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2386 fxdr_nfsv3time(tl, &nvap->na_atime);
2389 case NFSV3SATTRTIME_TOSERVER:
2390 vfs_timestamp(&nvap->na_atime);
2391 nvap->na_vaflags |= VA_UTIMES_NULL;
2394 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2395 switch (fxdr_unsigned(int, *tl)) {
2396 case NFSV3SATTRTIME_TOCLIENT:
2397 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2398 fxdr_nfsv3time(tl, &nvap->na_mtime);
2399 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2401 case NFSV3SATTRTIME_TOSERVER:
2402 vfs_timestamp(&nvap->na_mtime);
2404 nvap->na_vaflags |= VA_UTIMES_NULL;
2409 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2412 NFSEXITCODE2(error, nd);
2417 * Handle the setable attributes for V4.
2418 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2421 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2422 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2427 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2429 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2433 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2436 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2437 attrsize = fxdr_unsigned(int, *tl);
2440 * Loop around getting the setable attributes. If an unsupported
2441 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2444 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2445 bitpos = NFSATTRBIT_MAX;
2449 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2450 if (attrsum > attrsize) {
2451 error = NFSERR_BADXDR;
2454 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2456 case NFSATTRBIT_SIZE:
2457 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2458 if (vp != NULL && vp->v_type != VREG) {
2459 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2463 nvap->na_size = fxdr_hyper(tl);
2464 attrsum += NFSX_HYPER;
2466 case NFSATTRBIT_ACL:
2467 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2471 if (aceerr && !nd->nd_repstat)
2472 nd->nd_repstat = aceerr;
2475 case NFSATTRBIT_ARCHIVE:
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_HIDDEN:
2482 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2483 if (!nd->nd_repstat)
2484 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2485 attrsum += NFSX_UNSIGNED;
2487 case NFSATTRBIT_MIMETYPE:
2488 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2489 i = fxdr_unsigned(int, *tl);
2490 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2493 if (!nd->nd_repstat)
2494 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2495 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2497 case NFSATTRBIT_MODE:
2498 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2499 nvap->na_mode = nfstov_mode(*tl);
2500 attrsum += NFSX_UNSIGNED;
2502 case NFSATTRBIT_OWNER:
2503 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2504 j = fxdr_unsigned(int, *tl);
2506 error = NFSERR_BADXDR;
2509 if (j > NFSV4_SMALLSTR)
2510 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2513 error = nfsrv_mtostr(nd, cp, j);
2515 if (j > NFSV4_SMALLSTR)
2516 free(cp, M_NFSSTRING);
2519 if (!nd->nd_repstat) {
2520 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2522 if (!nd->nd_repstat)
2525 if (j > NFSV4_SMALLSTR)
2526 free(cp, M_NFSSTRING);
2527 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2529 case NFSATTRBIT_OWNERGROUP:
2530 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2531 j = fxdr_unsigned(int, *tl);
2533 error = NFSERR_BADXDR;
2536 if (j > NFSV4_SMALLSTR)
2537 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2540 error = nfsrv_mtostr(nd, cp, j);
2542 if (j > NFSV4_SMALLSTR)
2543 free(cp, M_NFSSTRING);
2546 if (!nd->nd_repstat) {
2547 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2549 if (!nd->nd_repstat)
2552 if (j > NFSV4_SMALLSTR)
2553 free(cp, M_NFSSTRING);
2554 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2556 case NFSATTRBIT_SYSTEM:
2557 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2558 if (!nd->nd_repstat)
2559 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2560 attrsum += NFSX_UNSIGNED;
2562 case NFSATTRBIT_TIMEACCESSSET:
2563 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2564 attrsum += NFSX_UNSIGNED;
2565 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2566 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2567 fxdr_nfsv4time(tl, &nvap->na_atime);
2569 attrsum += NFSX_V4TIME;
2571 vfs_timestamp(&nvap->na_atime);
2572 nvap->na_vaflags |= VA_UTIMES_NULL;
2575 case NFSATTRBIT_TIMEBACKUP:
2576 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2577 if (!nd->nd_repstat)
2578 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2579 attrsum += NFSX_V4TIME;
2581 case NFSATTRBIT_TIMECREATE:
2582 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2583 if (!nd->nd_repstat)
2584 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2585 attrsum += NFSX_V4TIME;
2587 case NFSATTRBIT_TIMEMODIFYSET:
2588 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2589 attrsum += NFSX_UNSIGNED;
2590 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2591 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2592 fxdr_nfsv4time(tl, &nvap->na_mtime);
2593 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2594 attrsum += NFSX_V4TIME;
2596 vfs_timestamp(&nvap->na_mtime);
2598 nvap->na_vaflags |= VA_UTIMES_NULL;
2602 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2604 * set bitpos so we drop out of the loop.
2606 bitpos = NFSATTRBIT_MAX;
2612 * some clients pad the attrlist, so we need to skip over the
2615 if (attrsum > attrsize) {
2616 error = NFSERR_BADXDR;
2618 attrsize = NFSM_RNDUP(attrsize);
2619 if (attrsum < attrsize)
2620 error = nfsm_advance(nd, attrsize - attrsum, -1);
2623 NFSEXITCODE2(error, nd);
2628 * Check/setup export credentials.
2631 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2632 struct ucred *credanon)
2637 * Check/setup credentials.
2639 if (nd->nd_flag & ND_GSS)
2640 exp->nes_exflag &= ~MNT_EXPORTANON;
2643 * Check to see if the operation is allowed for this security flavor.
2644 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2645 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2646 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2648 if (nfsvno_testexp(nd, exp) &&
2649 nd->nd_procnum != NFSV4OP_SECINFO &&
2650 nd->nd_procnum != NFSPROC_FSINFO) {
2651 if (nd->nd_flag & ND_NFSV4)
2652 error = NFSERR_WRONGSEC;
2654 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2659 * Check to see if the file system is exported V4 only.
2661 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2662 error = NFSERR_PROGNOTV4;
2667 * Now, map the user credentials.
2668 * (Note that ND_AUTHNONE will only be set for an NFSv3
2669 * Fsinfo RPC. If set for anything else, this code might need
2672 if (NFSVNO_EXPORTED(exp)) {
2673 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2674 NFSVNO_EXPORTANON(exp) ||
2675 (nd->nd_flag & ND_AUTHNONE) != 0) {
2676 nd->nd_cred->cr_uid = credanon->cr_uid;
2677 nd->nd_cred->cr_gid = credanon->cr_gid;
2678 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2679 credanon->cr_groups);
2680 } else if ((nd->nd_flag & ND_GSS) == 0) {
2682 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2683 * if there is a replacement credential with a group
2684 * list set up by "nfsuserd -manage-gids".
2685 * If there is no replacement, nfsrv_getgrpscred()
2686 * simply returns its argument.
2688 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2693 NFSEXITCODE2(error, nd);
2701 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2702 struct ucred **credp)
2704 int i, error, *secflavors;
2706 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2707 &exp->nes_numsecflavor, &secflavors);
2709 if (nfs_rootfhset) {
2710 exp->nes_exflag = 0;
2711 exp->nes_numsecflavor = 0;
2715 /* Copy the security flavors. */
2716 for (i = 0; i < exp->nes_numsecflavor; i++)
2717 exp->nes_secflavors[i] = secflavors[i];
2724 * Get a vnode for a file handle and export stuff.
2727 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2728 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2729 struct ucred **credp)
2731 int i, error, *secflavors;
2734 exp->nes_numsecflavor = 0;
2735 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2737 /* Make sure the server replies ESTALE to the client. */
2739 if (nam && !error) {
2740 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2741 &exp->nes_numsecflavor, &secflavors);
2743 if (nfs_rootfhset) {
2744 exp->nes_exflag = 0;
2745 exp->nes_numsecflavor = 0;
2751 /* Copy the security flavors. */
2752 for (i = 0; i < exp->nes_numsecflavor; i++)
2753 exp->nes_secflavors[i] = secflavors[i];
2761 * nfsd_fhtovp() - convert a fh to a vnode ptr
2762 * - look up fsid in mount list (if not found ret error)
2763 * - get vp and export rights by calling nfsvno_fhtovp()
2764 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2766 * - if mpp != NULL, return the mount point so that it can
2767 * be used for vn_finished_write() by the caller
2770 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2771 struct vnode **vpp, struct nfsexstuff *exp,
2772 struct mount **mpp, int startwrite, struct thread *p)
2775 struct ucred *credanon;
2778 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2780 * Check for the special case of the nfsv4root_fh.
2782 mp = vfs_busyfs(&fhp->fh_fsid);
2787 nd->nd_repstat = ESTALE;
2792 vn_start_write(NULL, mpp, V_WAIT);
2793 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2794 lktype = LK_EXCLUSIVE;
2796 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2801 * For NFSv4 without a pseudo root fs, unexported file handles
2802 * can be returned, so that Lookup works everywhere.
2804 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2805 !(nd->nd_flag & ND_NFSV4)) {
2807 nd->nd_repstat = EACCES;
2811 * Personally, I've never seen any point in requiring a
2812 * reserved port#, since only in the rare case where the
2813 * clients are all boxes with secure system privileges,
2814 * does it provide any enhanced security, but... some people
2815 * believe it to be useful and keep putting this code back in.
2816 * (There is also some "security checker" out there that
2817 * complains if the nfs server doesn't enforce this.)
2818 * However, note the following:
2819 * RFC3530 (NFSv4) specifies that a reserved port# not be
2821 * RFC2623 recommends that, if a reserved port# is checked for,
2822 * that there be a way to turn that off--> ifdef'd.
2824 #ifdef NFS_REQRSVPORT
2825 if (!nd->nd_repstat) {
2826 struct sockaddr_in *saddr;
2827 struct sockaddr_in6 *saddr6;
2829 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2830 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2831 if (!(nd->nd_flag & ND_NFSV4) &&
2832 ((saddr->sin_family == AF_INET &&
2833 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2834 (saddr6->sin6_family == AF_INET6 &&
2835 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2837 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2840 #endif /* NFS_REQRSVPORT */
2843 * Check/setup credentials.
2845 if (!nd->nd_repstat) {
2846 nd->nd_saveduid = nd->nd_cred->cr_uid;
2847 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2851 if (credanon != NULL)
2853 if (nd->nd_repstat) {
2855 vn_finished_write(mp);
2862 NFSEXITCODE2(0, nd);
2869 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2871 struct filedesc *fdp;
2875 fdp = p->td_proc->p_fd;
2876 if (fd < 0 || fd >= fdp->fd_nfiles ||
2877 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2889 * Called from nfssvc() to update the exports list. Just call
2890 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2891 * in the mount list.
2894 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2896 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2898 struct nameidata nd;
2901 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2902 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2904 else if (error == 0) {
2905 if (nfsexargp->fspec == NULL) {
2910 * If fspec != NULL, this is the v4root path.
2912 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2913 nfsexargp->fspec, p);
2914 if ((error = namei(&nd)) != 0)
2916 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2919 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2920 NFSBCOPY((caddr_t)&fh,
2921 nfs_rootfh.nfsrvfh_data,
2922 sizeof (fhandle_t));
2933 * This function needs to test to see if the system is near its limit
2934 * for memory allocation via malloc() or mget() and return True iff
2935 * either of these resources are near their limit.
2936 * XXX (For now, this is just a stub.)
2938 int nfsrv_testmalloclimit = 0;
2940 nfsrv_mallocmget_limit(void)
2942 static int printmesg = 0;
2943 static int testval = 1;
2945 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2946 if ((printmesg++ % 100) == 0)
2947 printf("nfsd: malloc/mget near limit\n");
2954 * BSD specific initialization of a mount point.
2959 static int inited = 0;
2964 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2965 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2966 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2967 nfsv4root_mnt.mnt_export = NULL;
2968 TAILQ_INIT(&nfsv4root_opt);
2969 TAILQ_INIT(&nfsv4root_newopt);
2970 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2971 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2972 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2973 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2977 * Get a vnode for a file handle, without checking exports, etc.
2980 nfsvno_getvp(fhandle_t *fhp)
2986 mp = vfs_busyfs(&fhp->fh_fsid);
2989 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2997 * Do a local VOP_ADVLOCK().
3000 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3001 u_int64_t end, struct thread *td)
3007 if (nfsrv_dolocallocks == 0)
3009 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3011 fl.l_whence = SEEK_SET;
3013 fl.l_start = (off_t)first;
3014 if (end == NFS64BITSSET) {
3018 fl.l_len = (off_t)tlen;
3021 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3022 * values for all calls, so that all locks will be held by the
3023 * nfsd server. (The nfsd server handles conflicts between the
3025 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3026 * bytes, so it can't be put in l_sysid.
3028 if (nfsv4_sysid == 0)
3029 nfsv4_sysid = nlm_acquire_next_sysid();
3030 fl.l_pid = (pid_t)0;
3031 fl.l_sysid = (int)nfsv4_sysid;
3033 if (ftype == F_UNLCK)
3034 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3035 (F_POSIX | F_REMOTE));
3037 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3038 (F_POSIX | F_REMOTE));
3046 * Check the nfsv4 root exports.
3049 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3051 struct ucred *credanon;
3052 int exflags, error = 0, numsecflavor, *secflavors, i;
3054 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3055 &credanon, &numsecflavor, &secflavors);
3057 error = NFSERR_PROGUNAVAIL;
3060 if (credanon != NULL)
3062 for (i = 0; i < numsecflavor; i++) {
3063 if (secflavors[i] == AUTH_SYS)
3064 nd->nd_flag |= ND_EXAUTHSYS;
3065 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3066 nd->nd_flag |= ND_EXGSS;
3067 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3068 nd->nd_flag |= ND_EXGSSINTEGRITY;
3069 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3070 nd->nd_flag |= ND_EXGSSPRIVACY;
3079 * Nfs server pseudo system call for the nfsd's
3085 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3088 struct nfsd_addsock_args sockarg;
3089 struct nfsd_nfsd_args nfsdarg;
3090 cap_rights_t rights;
3093 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3094 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3098 * Since we don't know what rights might be required,
3099 * pretend that we need them all. It is better to be too
3100 * careful than too reckless.
3102 error = fget(td, sockarg.sock,
3103 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3106 if (fp->f_type != DTYPE_SOCKET) {
3111 error = nfsrvd_addsock(fp);
3113 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3114 if (uap->argp == NULL) {
3118 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3122 error = nfsrvd_nfsd(td, &nfsdarg);
3124 error = nfssvc_srvcall(td, uap, td->td_ucred);
3133 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3135 struct nfsex_args export;
3136 struct file *fp = NULL;
3138 struct nfsd_clid adminrevoke;
3139 struct nfsd_dumplist dumplist;
3140 struct nfsd_dumpclients *dumpclients;
3141 struct nfsd_dumplocklist dumplocklist;
3142 struct nfsd_dumplocks *dumplocks;
3143 struct nameidata nd;
3145 int error = EINVAL, igotlock;
3147 static int suspend_nfsd = 0;
3149 if (uap->flag & NFSSVC_PUBLICFH) {
3150 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3151 sizeof (fhandle_t));
3152 error = copyin(uap->argp,
3153 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3156 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3157 error = copyin(uap->argp,(caddr_t)&export,
3158 sizeof (struct nfsex_args));
3160 error = nfsrv_v4rootexport(&export, cred, p);
3161 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3164 } else if (uap->flag & NFSSVC_STABLERESTART) {
3165 error = copyin(uap->argp, (caddr_t)&stablefd,
3168 error = fp_getfvp(p, stablefd, &fp, &vp);
3169 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3171 if (!error && newnfs_numnfsd != 0)
3174 nfsrv_stablefirst.nsf_fp = fp;
3175 nfsrv_setupstable(p);
3177 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3178 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3179 sizeof (struct nfsd_clid));
3181 error = nfsrv_adminrevoke(&adminrevoke, p);
3182 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3183 error = copyin(uap->argp, (caddr_t)&dumplist,
3184 sizeof (struct nfsd_dumplist));
3185 if (!error && (dumplist.ndl_size < 1 ||
3186 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3189 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3190 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3192 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3193 error = copyout(dumpclients,
3194 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3195 free((caddr_t)dumpclients, M_TEMP);
3197 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3198 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3199 sizeof (struct nfsd_dumplocklist));
3200 if (!error && (dumplocklist.ndllck_size < 1 ||
3201 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3204 error = nfsrv_lookupfilename(&nd,
3205 dumplocklist.ndllck_fname, p);
3207 len = sizeof (struct nfsd_dumplocks) *
3208 dumplocklist.ndllck_size;
3209 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3211 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3212 dumplocklist.ndllck_size, p);
3214 error = copyout(dumplocks,
3215 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3216 free((caddr_t)dumplocks, M_TEMP);
3218 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3221 nfsd_master_pid = procp->p_pid;
3222 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3223 nfsd_master_start = procp->p_stats->p_start;
3224 nfsd_master_proc = procp;
3226 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3227 NFSLOCKV4ROOTMUTEX();
3228 if (suspend_nfsd == 0) {
3229 /* Lock out all nfsd threads */
3231 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3232 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3233 } while (igotlock == 0 && suspend_nfsd == 0);
3236 NFSUNLOCKV4ROOTMUTEX();
3238 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3239 NFSLOCKV4ROOTMUTEX();
3240 if (suspend_nfsd != 0) {
3241 nfsv4_unlock(&nfsd_suspend_lock, 0);
3244 NFSUNLOCKV4ROOTMUTEX();
3254 * Returns 0 if ok, 1 otherwise.
3257 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3262 * This seems odd, but allow the case where the security flavor
3263 * list is empty. This happens when NFSv4 is traversing non-exported
3264 * file systems. Exported file systems should always have a non-empty
3265 * security flavor list.
3267 if (exp->nes_numsecflavor == 0)
3270 for (i = 0; i < exp->nes_numsecflavor; i++) {
3272 * The tests for privacy and integrity must be first,
3273 * since ND_GSS is set for everything but AUTH_SYS.
3275 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3276 (nd->nd_flag & ND_GSSPRIVACY))
3278 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3279 (nd->nd_flag & ND_GSSINTEGRITY))
3281 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3282 (nd->nd_flag & ND_GSS))
3284 if (exp->nes_secflavors[i] == AUTH_SYS &&
3285 (nd->nd_flag & ND_GSS) == 0)
3292 * Calculate a hash value for the fid in a file handle.
3295 nfsrv_hashfh(fhandle_t *fhp)
3299 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3304 * Calculate a hash value for the sessionid.
3307 nfsrv_hashsessionid(uint8_t *sessionid)
3311 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3316 * Signal the userland master nfsd to backup the stable restart file.
3319 nfsrv_backupstable(void)
3323 if (nfsd_master_proc != NULL) {
3324 procp = pfind(nfsd_master_pid);
3325 /* Try to make sure it is the correct process. */
3326 if (procp == nfsd_master_proc &&
3327 procp->p_stats->p_start.tv_sec ==
3328 nfsd_master_start.tv_sec &&
3329 procp->p_stats->p_start.tv_usec ==
3330 nfsd_master_start.tv_usec &&
3331 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3332 kern_psignal(procp, SIGUSR2);
3334 nfsd_master_proc = NULL;
3341 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3344 * Called once to initialize data structures...
3347 nfsd_modevent(module_t mod, int type, void *data)
3350 static int loaded = 0;
3357 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3358 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
3360 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
3363 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
3364 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
3365 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3366 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3373 #ifdef VV_DISABLEDELEG
3374 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3375 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3377 nfsd_call_servertimer = nfsrv_servertimer;
3378 nfsd_call_nfsd = nfssvc_nfsd;
3383 if (newnfs_numnfsd != 0) {
3388 #ifdef VV_DISABLEDELEG
3389 vn_deleg_ops.vndeleg_recall = NULL;
3390 vn_deleg_ops.vndeleg_disable = NULL;
3392 nfsd_call_servertimer = NULL;
3393 nfsd_call_nfsd = NULL;
3395 /* Clean out all NFSv4 state. */
3396 nfsrv_throwawayallstate(curthread);
3398 /* Clean the NFS server reply cache */
3399 nfsrvd_cleancache();
3401 /* Free up the krpc server pool. */
3402 if (nfsrvd_pool != NULL)
3403 svcpool_destroy(nfsrvd_pool);
3405 /* and get rid of the locks */
3406 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3407 mtx_destroy(&nfsrchash_table[i].mtx);
3408 mtx_destroy(&nfsrcahash_table[i].mtx);
3410 mtx_destroy(&nfsrc_udpmtx);
3411 mtx_destroy(&nfs_v4root_mutex);
3412 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3413 for (i = 0; i < nfsrv_sessionhashsize; i++)
3414 mtx_destroy(&nfssessionhash[i].mtx);
3415 lockdestroy(&nfsv4root_mnt.mnt_explock);
3416 free(nfsclienthash, M_NFSDCLIENT);
3417 free(nfslockhash, M_NFSDLOCKFILE);
3418 free(nfssessionhash, M_NFSDSESSION);
3430 static moduledata_t nfsd_mod = {
3435 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3437 /* So that loader and kldload(2) can find us, wherever we are.. */
3438 MODULE_VERSION(nfsd, 1);
3439 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3440 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3441 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3442 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3443 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);