2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/capsicum.h>
42 * Functions that perform the vfs operations required by the routines in
43 * nfsd_serv.c. It is hoped that this change will make the server more
47 #include <fs/nfs/nfsport.h>
49 #include <sys/sysctl.h>
50 #include <nlm/nlm_prot.h>
53 FEATURE(nfsd, "NFSv4 server");
55 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
56 extern int nfsrv_useacl;
57 extern int newnfs_numnfsd;
58 extern struct mount nfsv4root_mnt;
59 extern struct nfsrv_stablefirst nfsrv_stablefirst;
60 extern void (*nfsd_call_servertimer)(void);
61 extern SVCPOOL *nfsrvd_pool;
62 extern struct nfsv4lock nfsd_suspend_lock;
63 extern struct nfsclienthashhead *nfsclienthash;
64 extern struct nfslockhashhead *nfslockhash;
65 extern struct nfssessionhash *nfssessionhash;
66 extern int nfsrv_sessionhashsize;
67 extern struct nfsstatsv1 nfsstatsv1;
68 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
70 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
71 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
72 struct mtx nfsrc_udpmtx;
73 struct mtx nfs_v4root_mutex;
74 struct nfsrvfh nfs_rootfh, nfs_pubfh;
75 int nfs_pubfhset = 0, nfs_rootfhset = 0;
76 struct proc *nfsd_master_proc = NULL;
77 int nfsd_debuglevel = 0;
78 static pid_t nfsd_master_pid = (pid_t)-1;
79 static char nfsd_master_comm[MAXCOMLEN + 1];
80 static struct timeval nfsd_master_start;
81 static uint32_t nfsv4_sysid = 0;
83 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
86 int nfsrv_enable_crossmntpt = 1;
87 static int nfs_commit_blks;
88 static int nfs_commit_miss;
89 extern int nfsrv_issuedelegs;
90 extern int nfsrv_dolocallocks;
91 extern int nfsd_enable_stringtouid;
93 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server");
94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
95 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
98 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
100 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
101 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
102 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
103 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
104 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
105 0, "Debug level for NFS server");
106 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
107 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
109 #define MAX_REORDERED_RPC 16
110 #define NUM_HEURISTIC 1031
111 #define NHUSE_INIT 64
113 #define NHUSE_MAX 2048
115 static struct nfsheur {
116 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
117 off_t nh_nextoff; /* next offset for sequential detection */
118 int nh_use; /* use count for selection */
119 int nh_seqcount; /* heuristic */
120 } nfsheur[NUM_HEURISTIC];
124 * Heuristic to detect sequential operation.
126 static struct nfsheur *
127 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
132 /* Locate best candidate. */
134 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
137 if (nfsheur[hi].nh_vp == vp) {
141 if (nfsheur[hi].nh_use > 0)
142 --nfsheur[hi].nh_use;
143 hi = (hi + 1) % NUM_HEURISTIC;
144 if (nfsheur[hi].nh_use < nh->nh_use)
148 /* Initialize hint if this is a new file. */
149 if (nh->nh_vp != vp) {
151 nh->nh_nextoff = uio->uio_offset;
152 nh->nh_use = NHUSE_INIT;
153 if (uio->uio_offset == 0)
159 /* Calculate heuristic. */
160 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
161 uio->uio_offset == nh->nh_nextoff) {
162 /* See comments in vfs_vnops.c:sequential_heuristic(). */
163 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
164 if (nh->nh_seqcount > IO_SEQMAX)
165 nh->nh_seqcount = IO_SEQMAX;
166 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
167 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
168 /* Probably a reordered RPC, leave seqcount alone. */
169 } else if (nh->nh_seqcount > 1) {
170 nh->nh_seqcount /= 2;
174 nh->nh_use += NHUSE_INC;
175 if (nh->nh_use > NHUSE_MAX)
176 nh->nh_use = NHUSE_MAX;
181 * Get attributes into nfsvattr structure.
184 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
185 struct thread *p, int vpislocked)
187 int error, lockedit = 0;
189 if (vpislocked == 0) {
191 * When vpislocked == 0, the vnode is either exclusively
192 * locked by this thread or not locked by this thread.
193 * As such, shared lock it, if not exclusively locked.
195 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
197 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
200 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
209 * Get a file handle for a vnode.
212 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
216 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
217 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
218 error = VOP_VPTOFH(vp, &fhp->fh_fid);
225 * Perform access checking for vnodes obtained from file handles that would
226 * refer to files already opened by a Unix client. You cannot just use
227 * vn_writechk() and VOP_ACCESSX() for two reasons.
228 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
230 * 2 - The owner is to be given access irrespective of mode bits for some
231 * operations, so that processes that chmod after opening a file don't
235 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
236 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
237 u_int32_t *supportedtypep)
240 int error = 0, getret = 0;
242 if (vpislocked == 0) {
243 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
248 if (accmode & VWRITE) {
249 /* Just vn_writechk() changed to check rdonly */
251 * Disallow write attempts on read-only file systems;
252 * unless the file is a socket or a block or character
253 * device resident on the file system.
255 if (NFSVNO_EXRDONLY(exp) ||
256 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
257 switch (vp->v_type) {
267 * If there's shared text associated with
268 * the inode, try to free it up once. If
269 * we fail, we can't allow writing.
271 if (VOP_IS_TEXT(vp) && error == 0)
281 * Should the override still be applied when ACLs are enabled?
283 error = VOP_ACCESSX(vp, accmode, cred, p);
284 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
286 * Try again with VEXPLICIT_DENY, to see if the test for
287 * deletion is supported.
289 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
291 if (vp->v_type == VDIR) {
292 accmode &= ~(VDELETE | VDELETE_CHILD);
294 error = VOP_ACCESSX(vp, accmode, cred, p);
295 } else if (supportedtypep != NULL) {
296 *supportedtypep &= ~NFSACCESS_DELETE;
302 * Allow certain operations for the owner (reads and writes
303 * on files that are already open).
305 if (override != NFSACCCHK_NOOVERRIDE &&
306 (error == EPERM || error == EACCES)) {
307 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
309 else if (override & NFSACCCHK_ALLOWOWNER) {
310 getret = VOP_GETATTR(vp, &vattr, cred);
311 if (getret == 0 && cred->cr_uid == vattr.va_uid)
324 * Set attribute(s) vnop.
327 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
328 struct thread *p, struct nfsexstuff *exp)
332 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
338 * Set up nameidata for a lookup() call and do it.
341 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
342 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
343 struct vnode **retdirp)
345 struct componentname *cnp = &ndp->ni_cnd;
349 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
350 int error = 0, crossmnt;
354 cnp->cn_nameptr = cnp->cn_pnbuf;
357 * Extract and set starting directory.
359 if (dp->v_type != VDIR) {
364 nfsvno_relpathbuf(ndp);
372 if (NFSVNO_EXRDONLY(exp))
373 cnp->cn_flags |= RDONLY;
374 ndp->ni_segflg = UIO_SYSSPACE;
377 if (nd->nd_flag & ND_PUBLOOKUP) {
379 if (cnp->cn_pnbuf[0] == '/') {
382 * Check for degenerate pathnames here, since lookup()
385 for (i = 1; i < ndp->ni_pathlen; i++)
386 if (cnp->cn_pnbuf[i] != '/')
388 if (i == ndp->ni_pathlen) {
389 error = NFSERR_ACCES;
395 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
396 (nd->nd_flag & ND_NFSV4) == 0) {
398 * Only cross mount points for NFSv4 when doing a
399 * mount while traversing the file system above
400 * the mount point, unless nfsrv_enable_crossmntpt is set.
402 cnp->cn_flags |= NOCROSSMOUNT;
407 * Initialize for scan, set ni_startdir and bump ref on dp again
408 * because lookup() will dereference ni_startdir.
412 ndp->ni_startdir = dp;
413 ndp->ni_rootdir = rootvnode;
414 ndp->ni_topdir = NULL;
417 cnp->cn_flags |= LOCKLEAF;
419 cnp->cn_nameptr = cnp->cn_pnbuf;
421 * Call lookup() to do the real work. If an error occurs,
422 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
423 * we do not have to dereference anything before returning.
424 * In either case ni_startdir will be dereferenced and NULLed
432 * Check for encountering a symbolic link. Trivial
433 * termination occurs if no symlink encountered.
435 if ((cnp->cn_flags & ISSYMLINK) == 0) {
436 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
437 nfsvno_relpathbuf(ndp);
438 if (ndp->ni_vp && !lockleaf)
439 NFSVOPUNLOCK(ndp->ni_vp, 0);
446 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
447 NFSVOPUNLOCK(ndp->ni_dvp, 0);
448 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
453 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
457 if (ndp->ni_pathlen > 1)
458 cp = uma_zalloc(namei_zone, M_WAITOK);
462 aiov.iov_len = MAXPATHLEN;
463 auio.uio_iov = &aiov;
466 auio.uio_rw = UIO_READ;
467 auio.uio_segflg = UIO_SYSSPACE;
469 auio.uio_resid = MAXPATHLEN;
470 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
473 if (ndp->ni_pathlen > 1)
474 uma_zfree(namei_zone, cp);
480 linklen = MAXPATHLEN - auio.uio_resid;
485 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
486 error = ENAMETOOLONG;
491 * Adjust or replace path
493 if (ndp->ni_pathlen > 1) {
494 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
495 uma_zfree(namei_zone, cnp->cn_pnbuf);
498 cnp->cn_pnbuf[linklen] = '\0';
499 ndp->ni_pathlen += linklen;
502 * Cleanup refs for next loop and check if root directory
503 * should replace current directory. Normally ni_dvp
504 * becomes the new base directory and is cleaned up when
505 * we loop. Explicitly null pointers after invalidation
506 * to clarify operation.
511 if (cnp->cn_pnbuf[0] == '/') {
513 ndp->ni_dvp = ndp->ni_rootdir;
516 ndp->ni_startdir = ndp->ni_dvp;
520 cnp->cn_flags &= ~LOCKLEAF;
524 nfsvno_relpathbuf(ndp);
527 ndp->ni_startdir = NULL;
528 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
533 NFSEXITCODE2(error, nd);
538 * Set up a pathname buffer and return a pointer to it and, optionally
539 * set a hash pointer.
542 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
544 struct componentname *cnp = &ndp->ni_cnd;
546 cnp->cn_flags |= (NOMACCHECK | HASBUF);
547 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
550 *bufpp = cnp->cn_pnbuf;
554 * Release the above path buffer, if not released by nfsvno_namei().
557 nfsvno_relpathbuf(struct nameidata *ndp)
560 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
562 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
563 ndp->ni_cnd.cn_flags &= ~HASBUF;
567 * Readlink vnode op into an mbuf list.
570 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
571 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
573 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
574 struct iovec *ivp = iv;
575 struct uio io, *uiop = &io;
576 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
577 int i, len, tlen, error = 0;
581 while (len < NFS_MAXPATHLEN) {
583 MCLGET(mp, M_WAITOK);
584 mp->m_len = M_SIZE(mp);
591 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
592 mp->m_len = NFS_MAXPATHLEN - len;
593 len = NFS_MAXPATHLEN;
597 ivp->iov_base = mtod(mp, caddr_t);
598 ivp->iov_len = mp->m_len;
603 uiop->uio_iovcnt = i;
604 uiop->uio_offset = 0;
605 uiop->uio_resid = len;
606 uiop->uio_rw = UIO_READ;
607 uiop->uio_segflg = UIO_SYSSPACE;
609 error = VOP_READLINK(vp, uiop, cred);
615 if (uiop->uio_resid > 0) {
616 len -= uiop->uio_resid;
617 tlen = NFSM_RNDUP(len);
618 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
630 * Read vnode op call into mbuf list.
633 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
634 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
640 int error = 0, len, left, siz, tlen, ioflag = 0;
641 struct mbuf *m2 = NULL, *m3;
642 struct uio io, *uiop = &io;
645 len = left = NFSM_RNDUP(cnt);
648 * Generate the mbuf list with the uio_iov ref. to it.
655 siz = min(M_TRAILINGSPACE(m), left);
664 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
666 uiop->uio_iov = iv2 = iv;
672 panic("nfsvno_read iov");
673 siz = min(M_TRAILINGSPACE(m), left);
675 iv->iov_base = mtod(m, caddr_t) + m->m_len;
684 uiop->uio_iovcnt = i;
685 uiop->uio_offset = off;
686 uiop->uio_resid = len;
687 uiop->uio_rw = UIO_READ;
688 uiop->uio_segflg = UIO_SYSSPACE;
690 nh = nfsrv_sequential_heuristic(uiop, vp);
691 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
692 /* XXX KDM make this more systematic? */
693 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
694 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
695 FREE((caddr_t)iv2, M_TEMP);
701 nh->nh_nextoff = uiop->uio_offset;
702 tlen = len - uiop->uio_resid;
703 cnt = cnt < tlen ? cnt : tlen;
704 tlen = NFSM_RNDUP(cnt);
708 } else if (len != tlen || tlen != cnt)
709 nfsrv_adj(m3, len - tlen, tlen - cnt);
719 * Write vnode op from an mbuf list.
722 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
723 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
729 struct uio io, *uiop = &io;
732 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
734 uiop->uio_iov = iv = ivp;
735 uiop->uio_iovcnt = cnt;
736 i = mtod(mp, caddr_t) + mp->m_len - cp;
740 panic("nfsvno_write");
751 cp = mtod(mp, caddr_t);
755 if (stable == NFSWRITE_UNSTABLE)
756 ioflags = IO_NODELOCKED;
758 ioflags = (IO_SYNC | IO_NODELOCKED);
759 uiop->uio_resid = retlen;
760 uiop->uio_rw = UIO_WRITE;
761 uiop->uio_segflg = UIO_SYSSPACE;
763 uiop->uio_offset = off;
764 nh = nfsrv_sequential_heuristic(uiop, vp);
765 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
766 /* XXX KDM make this more systematic? */
767 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
768 error = VOP_WRITE(vp, uiop, ioflags, cred);
770 nh->nh_nextoff = uiop->uio_offset;
771 FREE((caddr_t)iv, M_TEMP);
778 * Common code for creating a regular file (plus special files for V2).
781 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
782 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
783 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
788 error = nd->nd_repstat;
789 if (!error && ndp->ni_vp == NULL) {
790 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
791 vrele(ndp->ni_startdir);
792 error = VOP_CREATE(ndp->ni_dvp,
793 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
795 nfsvno_relpathbuf(ndp);
797 if (*exclusive_flagp) {
798 *exclusive_flagp = 0;
799 NFSVNO_ATTRINIT(nvap);
800 nvap->na_atime.tv_sec = cverf[0];
801 nvap->na_atime.tv_nsec = cverf[1];
802 error = VOP_SETATTR(ndp->ni_vp,
803 &nvap->na_vattr, nd->nd_cred);
807 error = NFSERR_NOTSUPP;
812 * NFS V2 Only. nfsrvd_mknod() does this for V3.
813 * (This implies, just get out on an error.)
815 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
816 nvap->na_type == VFIFO) {
817 if (nvap->na_type == VCHR && rdev == 0xffffffff)
818 nvap->na_type = VFIFO;
819 if (nvap->na_type != VFIFO &&
820 (error = priv_check_cred(nd->nd_cred,
821 PRIV_VFS_MKNOD_DEV, 0))) {
822 vrele(ndp->ni_startdir);
823 nfsvno_relpathbuf(ndp);
827 nvap->na_rdev = rdev;
828 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
829 &ndp->ni_cnd, &nvap->na_vattr);
831 nfsvno_relpathbuf(ndp);
832 vrele(ndp->ni_startdir);
836 vrele(ndp->ni_startdir);
837 nfsvno_relpathbuf(ndp);
845 * Handle cases where error is already set and/or
847 * 1 - clean up the lookup
848 * 2 - iff !error and na_size set, truncate it
850 vrele(ndp->ni_startdir);
851 nfsvno_relpathbuf(ndp);
853 if (ndp->ni_dvp == *vpp)
857 if (!error && nvap->na_size != VNOVAL) {
858 error = nfsvno_accchk(*vpp, VWRITE,
859 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
860 NFSACCCHK_VPISLOCKED, NULL);
862 tempsize = nvap->na_size;
863 NFSVNO_ATTRINIT(nvap);
864 nvap->na_size = tempsize;
865 error = VOP_SETATTR(*vpp,
866 &nvap->na_vattr, nd->nd_cred);
879 * Do a mknod vnode op.
882 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
888 vtyp = nvap->na_type;
890 * Iff doesn't exist, create it.
893 vrele(ndp->ni_startdir);
894 nfsvno_relpathbuf(ndp);
900 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
901 vrele(ndp->ni_startdir);
902 nfsvno_relpathbuf(ndp);
904 error = NFSERR_BADTYPE;
908 vrele(ndp->ni_startdir);
909 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
910 &ndp->ni_cnd, &nvap->na_vattr);
912 nfsvno_relpathbuf(ndp);
914 if (nvap->na_type != VFIFO &&
915 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
916 vrele(ndp->ni_startdir);
917 nfsvno_relpathbuf(ndp);
921 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
922 &ndp->ni_cnd, &nvap->na_vattr);
924 nfsvno_relpathbuf(ndp);
925 vrele(ndp->ni_startdir);
927 * Since VOP_MKNOD returns the ni_vp, I can't
928 * see any reason to do the lookup.
941 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
942 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
946 if (ndp->ni_vp != NULL) {
947 if (ndp->ni_dvp == ndp->ni_vp)
952 nfsvno_relpathbuf(ndp);
956 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
959 nfsvno_relpathbuf(ndp);
970 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
971 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
972 struct nfsexstuff *exp)
977 vrele(ndp->ni_startdir);
978 nfsvno_relpathbuf(ndp);
979 if (ndp->ni_dvp == ndp->ni_vp)
988 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
989 &nvap->na_vattr, pathcp);
991 vrele(ndp->ni_startdir);
992 nfsvno_relpathbuf(ndp);
994 * Although FreeBSD still had the lookup code in
995 * it for 7/current, there doesn't seem to be any
996 * point, since VOP_SYMLINK() returns the ni_vp.
997 * Just vput it for v2.
999 if (!not_v2 && !error)
1008 * Parse symbolic link arguments.
1009 * This function has an ugly side effect. It will MALLOC() an area for
1010 * the symlink and set iov_base to point to it, only if it succeeds.
1011 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1015 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1016 struct thread *p, char **pathcpp, int *lenp)
1019 char *pathcp = NULL;
1021 struct nfsv2_sattr *sp;
1025 if ((nd->nd_flag & ND_NFSV3) &&
1026 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1028 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1029 len = fxdr_unsigned(int, *tl);
1030 if (len > NFS_MAXPATHLEN || len <= 0) {
1034 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1035 error = nfsrv_mtostr(nd, pathcp, len);
1038 if (nd->nd_flag & ND_NFSV2) {
1039 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1040 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1044 NFSEXITCODE2(0, nd);
1048 free(pathcp, M_TEMP);
1049 NFSEXITCODE2(error, nd);
1054 * Remove a non-directory object.
1057 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1058 struct thread *p, struct nfsexstuff *exp)
1064 if (vp->v_type == VDIR)
1065 error = NFSERR_ISDIR;
1067 error = nfsrv_checkremove(vp, 1, p);
1069 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1070 if (ndp->ni_dvp == vp)
1075 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1076 nfsvno_relpathbuf(ndp);
1082 * Remove a directory.
1085 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1086 struct thread *p, struct nfsexstuff *exp)
1092 if (vp->v_type != VDIR) {
1097 * No rmdir "." please.
1099 if (ndp->ni_dvp == vp) {
1104 * The root of a mounted filesystem cannot be deleted.
1106 if (vp->v_vflag & VV_ROOT)
1110 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1111 if (ndp->ni_dvp == vp)
1116 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1117 nfsvno_relpathbuf(ndp);
1126 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1127 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1129 struct vnode *fvp, *tvp, *tdvp;
1132 fvp = fromndp->ni_vp;
1134 vrele(fromndp->ni_dvp);
1139 tdvp = tondp->ni_dvp;
1142 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1143 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1145 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1146 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1149 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1150 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1155 * A rename to '.' or '..' results in a prematurely
1156 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1159 if ((tondp->ni_cnd.cn_namelen == 1 &&
1160 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1161 (tondp->ni_cnd.cn_namelen == 2 &&
1162 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1163 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1168 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1169 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1172 if (fvp->v_mount != tdvp->v_mount) {
1173 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1177 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1182 * If source and destination are the same, there is nothing to
1183 * do. Set error to -1 to indicate this.
1188 if (ndflag & ND_NFSV4) {
1189 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1190 error = nfsrv_checkremove(fvp, 0, p);
1191 NFSVOPUNLOCK(fvp, 0);
1195 error = nfsrv_checkremove(tvp, 1, p);
1198 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1199 * that the NFSv4 client won't be confused by the rename.
1200 * Since nfsd_recalldelegation() can only be called on an
1201 * unlocked vnode at this point and fvp is the file that will
1202 * still exist after the rename, just do fvp.
1204 nfsd_recalldelegation(fvp, p);
1208 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1209 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1218 vrele(fromndp->ni_dvp);
1223 vrele(tondp->ni_startdir);
1224 nfsvno_relpathbuf(tondp);
1226 vrele(fromndp->ni_startdir);
1227 nfsvno_relpathbuf(fromndp);
1236 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1237 struct thread *p, struct nfsexstuff *exp)
1247 if (vp->v_mount != xp->v_mount)
1251 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1252 if ((vp->v_iflag & VI_DOOMED) == 0)
1253 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1256 if (ndp->ni_dvp == vp)
1260 NFSVOPUNLOCK(vp, 0);
1262 if (ndp->ni_dvp == ndp->ni_vp)
1269 nfsvno_relpathbuf(ndp);
1275 * Do the fsync() appropriate for the commit.
1278 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1284 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1285 * file is done. At this time VOP_FSYNC does not accept offset and
1286 * byte count parameters so call VOP_FSYNC the whole file for now.
1287 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1288 * File systems that do not use the buffer cache (as indicated
1289 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1291 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1292 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1294 * Give up and do the whole thing
1297 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1298 VM_OBJECT_WLOCK(vp->v_object);
1299 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1300 VM_OBJECT_WUNLOCK(vp->v_object);
1302 error = VOP_FSYNC(vp, MNT_WAIT, td);
1305 * Locate and synchronously write any buffers that fall
1306 * into the requested range. Note: we are assuming that
1307 * f_iosize is a power of 2.
1309 int iosize = vp->v_mount->mnt_stat.f_iosize;
1310 int iomask = iosize - 1;
1315 * Align to iosize boundary, super-align to page boundary.
1318 cnt += off & iomask;
1319 off &= ~(u_quad_t)iomask;
1321 if (off & PAGE_MASK) {
1322 cnt += off & PAGE_MASK;
1323 off &= ~(u_quad_t)PAGE_MASK;
1325 lblkno = off / iosize;
1328 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1329 VM_OBJECT_WLOCK(vp->v_object);
1330 vm_object_page_clean(vp->v_object, off, off + cnt,
1332 VM_OBJECT_WUNLOCK(vp->v_object);
1341 * If we have a buffer and it is marked B_DELWRI we
1342 * have to lock and write it. Otherwise the prior
1343 * write is assumed to have already been committed.
1345 * gbincore() can return invalid buffers now so we
1346 * have to check that bit as well (though B_DELWRI
1347 * should not be set if B_INVAL is set there could be
1348 * a race here since we haven't locked the buffer).
1350 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1351 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1352 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1354 continue; /* retry */
1356 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1359 bp->b_flags &= ~B_ASYNC;
1382 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1386 error = VFS_STATFS(vp->v_mount, sf);
1389 * Since NFS handles these values as unsigned on the
1390 * wire, there is no way to represent negative values,
1391 * so set them to 0. Without this, they will appear
1392 * to be very large positive values for clients like
1395 if (sf->f_bavail < 0)
1397 if (sf->f_ffree < 0)
1405 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1406 * must handle nfsrv_opencheck() calls after any other access checks.
1409 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1410 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1411 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1412 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1413 struct nfsexstuff *exp, struct vnode **vpp)
1415 struct vnode *vp = NULL;
1417 struct nfsexstuff nes;
1419 if (ndp->ni_vp == NULL)
1420 nd->nd_repstat = nfsrv_opencheck(clientid,
1421 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1422 if (!nd->nd_repstat) {
1423 if (ndp->ni_vp == NULL) {
1424 vrele(ndp->ni_startdir);
1425 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1426 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1428 nfsvno_relpathbuf(ndp);
1429 if (!nd->nd_repstat) {
1430 if (*exclusive_flagp) {
1431 *exclusive_flagp = 0;
1432 NFSVNO_ATTRINIT(nvap);
1433 nvap->na_atime.tv_sec = cverf[0];
1434 nvap->na_atime.tv_nsec = cverf[1];
1435 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1436 &nvap->na_vattr, cred);
1437 if (nd->nd_repstat != 0) {
1440 nd->nd_repstat = NFSERR_NOTSUPP;
1442 NFSSETBIT_ATTRBIT(attrbitp,
1443 NFSATTRBIT_TIMEACCESS);
1445 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1446 aclp, p, attrbitp, exp);
1451 if (ndp->ni_startdir)
1452 vrele(ndp->ni_startdir);
1453 nfsvno_relpathbuf(ndp);
1455 if (create == NFSV4OPEN_CREATE) {
1456 if (ndp->ni_dvp == vp)
1461 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1462 if (ndp->ni_cnd.cn_flags & RDONLY)
1463 NFSVNO_SETEXRDONLY(&nes);
1465 NFSVNO_EXINIT(&nes);
1466 nd->nd_repstat = nfsvno_accchk(vp,
1467 VWRITE, cred, &nes, p,
1468 NFSACCCHK_NOOVERRIDE,
1469 NFSACCCHK_VPISLOCKED, NULL);
1470 nd->nd_repstat = nfsrv_opencheck(clientid,
1471 stateidp, stp, vp, nd, p, nd->nd_repstat);
1472 if (!nd->nd_repstat) {
1473 tempsize = nvap->na_size;
1474 NFSVNO_ATTRINIT(nvap);
1475 nvap->na_size = tempsize;
1476 nd->nd_repstat = VOP_SETATTR(vp,
1477 &nvap->na_vattr, cred);
1479 } else if (vp->v_type == VREG) {
1480 nd->nd_repstat = nfsrv_opencheck(clientid,
1481 stateidp, stp, vp, nd, p, nd->nd_repstat);
1485 if (ndp->ni_cnd.cn_flags & HASBUF)
1486 nfsvno_relpathbuf(ndp);
1487 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1488 vrele(ndp->ni_startdir);
1489 if (ndp->ni_dvp == ndp->ni_vp)
1499 NFSEXITCODE2(0, nd);
1503 * Updates the file rev and sets the mtime and ctime
1504 * to the current clock time, returning the va_filerev and va_Xtime
1506 * Return ESTALE to indicate the vnode is VI_DOOMED.
1509 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1510 struct ucred *cred, struct thread *p)
1515 vfs_timestamp(&va.va_mtime);
1516 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1517 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1518 if ((vp->v_iflag & VI_DOOMED) != 0)
1521 (void) VOP_SETATTR(vp, &va, cred);
1522 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1527 * Glue routine to nfsv4_fillattr().
1530 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1531 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1532 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1533 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1537 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1538 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1540 NFSEXITCODE2(0, nd);
1544 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1546 * nfs readdir service
1547 * - mallocs what it thinks is enough to read
1548 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1549 * - calls VOP_READDIR()
1550 * - loops around building the reply
1551 * if the output generated exceeds count break out of loop
1552 * The NFSM_CLGET macro is used here so that the reply will be packed
1553 * tightly in mbuf clusters.
1554 * - it trims out records with d_fileno == 0
1555 * this doesn't matter for Unix clients, but they might confuse clients
1557 * - it trims out records with d_type == DT_WHT
1558 * these cannot be seen through NFS (unless we extend the protocol)
1559 * The alternate call nfsrvd_readdirplus() does lookups as well.
1560 * PS: The NFS protocol spec. does not clarify what the "count" byte
1561 * argument is a count of.. just name strings and file id's or the
1562 * entire reply rpc or ...
1563 * I tried just file name and id sizes and it confused the Sun client,
1564 * so I am using the full rpc size now. The "paranoia.." comment refers
1565 * to including the status longwords that are not a part of the dir.
1566 * "entry" structures, but are in the rpc.
1569 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1570 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1575 char *cpos, *cend, *rbuf;
1577 int nlen, error = 0, getret = 1;
1578 int siz, cnt, fullsiz, eofflag, ncookies;
1579 u_int64_t off, toff, verf;
1580 u_long *cookies = NULL, *cookiep;
1585 if (nd->nd_repstat) {
1586 nfsrv_postopattr(nd, getret, &at);
1589 if (nd->nd_flag & ND_NFSV2) {
1590 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1591 off = fxdr_unsigned(u_quad_t, *tl++);
1593 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1594 off = fxdr_hyper(tl);
1596 verf = fxdr_hyper(tl);
1600 cnt = fxdr_unsigned(int, *tl);
1601 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1602 cnt = NFS_SRVMAXDATA(nd);
1603 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1605 if (nd->nd_flag & ND_NFSV3) {
1606 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1610 * va_filerev is not sufficient as a cookie verifier,
1611 * since it is not supposed to change when entries are
1612 * removed/added unless that offset cookies returned to
1613 * the client are no longer valid.
1615 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1616 nd->nd_repstat = NFSERR_BAD_COOKIE;
1619 if (!nd->nd_repstat && vp->v_type != VDIR)
1620 nd->nd_repstat = NFSERR_NOTDIR;
1621 if (nd->nd_repstat == 0 && cnt == 0) {
1622 if (nd->nd_flag & ND_NFSV2)
1623 /* NFSv2 does not have NFSERR_TOOSMALL */
1624 nd->nd_repstat = EPERM;
1626 nd->nd_repstat = NFSERR_TOOSMALL;
1628 if (!nd->nd_repstat)
1629 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1630 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1631 NFSACCCHK_VPISLOCKED, NULL);
1632 if (nd->nd_repstat) {
1634 if (nd->nd_flag & ND_NFSV3)
1635 nfsrv_postopattr(nd, getret, &at);
1638 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1639 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1643 free((caddr_t)cookies, M_TEMP);
1651 io.uio_offset = (off_t)off;
1653 io.uio_segflg = UIO_SYSSPACE;
1654 io.uio_rw = UIO_READ;
1656 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1658 off = (u_int64_t)io.uio_offset;
1660 siz -= io.uio_resid;
1662 if (!cookies && !nd->nd_repstat)
1663 nd->nd_repstat = NFSERR_PERM;
1664 if (nd->nd_flag & ND_NFSV3) {
1665 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1666 if (!nd->nd_repstat)
1667 nd->nd_repstat = getret;
1671 * Handles the failed cases. nd->nd_repstat == 0 past here.
1673 if (nd->nd_repstat) {
1675 free((caddr_t)rbuf, M_TEMP);
1677 free((caddr_t)cookies, M_TEMP);
1678 if (nd->nd_flag & ND_NFSV3)
1679 nfsrv_postopattr(nd, getret, &at);
1683 * If nothing read, return eof
1688 if (nd->nd_flag & ND_NFSV2) {
1689 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1691 nfsrv_postopattr(nd, getret, &at);
1692 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1693 txdr_hyper(at.na_filerev, tl);
1696 *tl++ = newnfs_false;
1698 FREE((caddr_t)rbuf, M_TEMP);
1699 FREE((caddr_t)cookies, M_TEMP);
1704 * Check for degenerate cases of nothing useful read.
1705 * If so go try again
1709 dp = (struct dirent *)cpos;
1713 * For some reason FreeBSD's ufs_readdir() chooses to back the
1714 * directory offset up to a block boundary, so it is necessary to
1715 * skip over the records that precede the requested offset. This
1716 * requires the assumption that file offset cookies monotonically
1719 while (cpos < cend && ncookies > 0 &&
1720 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1721 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1722 cpos += dp->d_reclen;
1723 dp = (struct dirent *)cpos;
1727 if (cpos >= cend || ncookies == 0) {
1735 * dirlen is the size of the reply, including all XDR and must
1736 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1737 * if the XDR should be included in "count", but to be safe, we do.
1738 * (Include the two booleans at the end of the reply in dirlen now.)
1740 if (nd->nd_flag & ND_NFSV3) {
1741 nfsrv_postopattr(nd, getret, &at);
1742 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1743 txdr_hyper(at.na_filerev, tl);
1744 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1746 dirlen = 2 * NFSX_UNSIGNED;
1749 /* Loop through the records and build reply */
1750 while (cpos < cend && ncookies > 0) {
1751 nlen = dp->d_namlen;
1752 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1753 nlen <= NFS_MAXNAMLEN) {
1754 if (nd->nd_flag & ND_NFSV3)
1755 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1757 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1764 * Build the directory record xdr from
1767 if (nd->nd_flag & ND_NFSV3) {
1768 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1769 *tl++ = newnfs_true;
1772 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1773 *tl++ = newnfs_true;
1775 *tl = txdr_unsigned(dp->d_fileno);
1776 (void) nfsm_strtom(nd, dp->d_name, nlen);
1777 if (nd->nd_flag & ND_NFSV3) {
1778 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1781 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1782 *tl = txdr_unsigned(*cookiep);
1784 cpos += dp->d_reclen;
1785 dp = (struct dirent *)cpos;
1791 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1792 *tl++ = newnfs_false;
1797 FREE((caddr_t)rbuf, M_TEMP);
1798 FREE((caddr_t)cookies, M_TEMP);
1801 NFSEXITCODE2(0, nd);
1805 NFSEXITCODE2(error, nd);
1810 * Readdirplus for V3 and Readdir for V4.
1813 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1814 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1819 char *cpos, *cend, *rbuf;
1822 struct nfsvattr nva, at, *nvap = &nva;
1823 struct mbuf *mb0, *mb1;
1824 struct nfsreferral *refp;
1825 int nlen, r, error = 0, getret = 1, usevget = 1;
1826 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1827 caddr_t bpos0, bpos1;
1828 u_int64_t off, toff, verf;
1829 u_long *cookies = NULL, *cookiep;
1830 nfsattrbit_t attrbits, rderrbits, savbits;
1833 struct componentname cn;
1834 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
1835 struct mount *mp, *new_mp;
1836 uint64_t mounted_on_fileno;
1838 if (nd->nd_repstat) {
1839 nfsrv_postopattr(nd, getret, &at);
1842 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1843 off = fxdr_hyper(tl);
1846 verf = fxdr_hyper(tl);
1848 siz = fxdr_unsigned(int, *tl++);
1849 cnt = fxdr_unsigned(int, *tl);
1852 * Use the server's maximum data transfer size as the upper bound
1855 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1856 cnt = NFS_SRVMAXDATA(nd);
1859 * siz is a "hint" of how much directory information (name, fileid,
1860 * cookie) should be in the reply. At least one client "hints" 0,
1861 * so I set it to cnt for that case. I also round it up to the
1862 * next multiple of DIRBLKSIZ.
1866 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1868 if (nd->nd_flag & ND_NFSV4) {
1869 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1872 NFSSET_ATTRBIT(&savbits, &attrbits);
1873 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1874 NFSZERO_ATTRBIT(&rderrbits);
1875 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1877 NFSZERO_ATTRBIT(&attrbits);
1880 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1881 if (!nd->nd_repstat) {
1882 if (off && verf != at.na_filerev) {
1884 * va_filerev is not sufficient as a cookie verifier,
1885 * since it is not supposed to change when entries are
1886 * removed/added unless that offset cookies returned to
1887 * the client are no longer valid.
1890 if (nd->nd_flag & ND_NFSV4) {
1891 nd->nd_repstat = NFSERR_NOTSAME;
1893 nd->nd_repstat = NFSERR_BAD_COOKIE;
1896 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1897 nd->nd_repstat = NFSERR_BAD_COOKIE;
1900 if (!nd->nd_repstat && vp->v_type != VDIR)
1901 nd->nd_repstat = NFSERR_NOTDIR;
1902 if (!nd->nd_repstat && cnt == 0)
1903 nd->nd_repstat = NFSERR_TOOSMALL;
1904 if (!nd->nd_repstat)
1905 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1906 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1907 NFSACCCHK_VPISLOCKED, NULL);
1908 if (nd->nd_repstat) {
1910 if (nd->nd_flag & ND_NFSV3)
1911 nfsrv_postopattr(nd, getret, &at);
1914 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1915 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
1917 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1921 free((caddr_t)cookies, M_TEMP);
1929 io.uio_offset = (off_t)off;
1931 io.uio_segflg = UIO_SYSSPACE;
1932 io.uio_rw = UIO_READ;
1934 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1936 off = (u_int64_t)io.uio_offset;
1938 siz -= io.uio_resid;
1940 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1942 if (!cookies && !nd->nd_repstat)
1943 nd->nd_repstat = NFSERR_PERM;
1944 if (!nd->nd_repstat)
1945 nd->nd_repstat = getret;
1946 if (nd->nd_repstat) {
1949 free((caddr_t)cookies, M_TEMP);
1950 free((caddr_t)rbuf, M_TEMP);
1951 if (nd->nd_flag & ND_NFSV3)
1952 nfsrv_postopattr(nd, getret, &at);
1956 * If nothing read, return eof
1961 if (nd->nd_flag & ND_NFSV3)
1962 nfsrv_postopattr(nd, getret, &at);
1963 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1964 txdr_hyper(at.na_filerev, tl);
1966 *tl++ = newnfs_false;
1968 free((caddr_t)cookies, M_TEMP);
1969 free((caddr_t)rbuf, M_TEMP);
1974 * Check for degenerate cases of nothing useful read.
1975 * If so go try again
1979 dp = (struct dirent *)cpos;
1983 * For some reason FreeBSD's ufs_readdir() chooses to back the
1984 * directory offset up to a block boundary, so it is necessary to
1985 * skip over the records that precede the requested offset. This
1986 * requires the assumption that file offset cookies monotonically
1989 while (cpos < cend && ncookies > 0 &&
1990 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1991 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
1992 ((nd->nd_flag & ND_NFSV4) &&
1993 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1994 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1995 cpos += dp->d_reclen;
1996 dp = (struct dirent *)cpos;
2000 if (cpos >= cend || ncookies == 0) {
2007 * Busy the file system so that the mount point won't go away
2008 * and, as such, VFS_VGET() can be used safely.
2012 NFSVOPUNLOCK(vp, 0);
2013 nd->nd_repstat = vfs_busy(mp, 0);
2015 if (nd->nd_repstat != 0) {
2017 free(cookies, M_TEMP);
2019 if (nd->nd_flag & ND_NFSV3)
2020 nfsrv_postopattr(nd, getret, &at);
2025 * Check to see if entries in this directory can be safely acquired
2026 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2027 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2028 * automount of the snapshot directory that is required will
2030 * This needs to be done here for NFSv4, since NFSv4 never does
2031 * a VFS_VGET() for "." or "..".
2034 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2035 if (r == EOPNOTSUPP) {
2037 cn.cn_nameiop = LOOKUP;
2038 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2039 cn.cn_cred = nd->nd_cred;
2046 * Save this position, in case there is an error before one entry
2050 bpos0 = nd->nd_bpos;
2053 * Fill in the first part of the reply.
2054 * dirlen is the reply length in bytes and cannot exceed cnt.
2055 * (Include the two booleans at the end of the reply in dirlen now,
2056 * so we recognize when we have exceeded cnt.)
2058 if (nd->nd_flag & ND_NFSV3) {
2059 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2060 nfsrv_postopattr(nd, getret, &at);
2062 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2064 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2065 txdr_hyper(at.na_filerev, tl);
2068 * Save this position, in case there is an empty reply needed.
2071 bpos1 = nd->nd_bpos;
2073 /* Loop through the records and build reply */
2075 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2076 nlen = dp->d_namlen;
2077 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2078 nlen <= NFS_MAXNAMLEN &&
2079 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2080 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2081 || (nlen == 1 && dp->d_name[0] != '.'))) {
2083 * Save the current position in the reply, in case
2084 * this entry exceeds cnt.
2087 bpos1 = nd->nd_bpos;
2090 * For readdir_and_lookup get the vnode using
2099 mounted_on_fileno = (uint64_t)dp->d_fileno;
2100 if ((nd->nd_flag & ND_NFSV3) ||
2101 NFSNONZERO_ATTRBIT(&savbits)) {
2102 if (nd->nd_flag & ND_NFSV4)
2103 refp = nfsv4root_getreferral(NULL,
2107 r = VFS_VGET(mp, dp->d_fileno,
2111 if (r == EOPNOTSUPP) {
2114 cn.cn_nameiop = LOOKUP;
2122 cn.cn_nameptr = dp->d_name;
2123 cn.cn_namelen = nlen;
2124 cn.cn_flags = ISLASTCN |
2125 NOFOLLOW | LOCKLEAF;
2127 dp->d_name[0] == '.' &&
2128 dp->d_name[1] == '.')
2131 if (NFSVOPLOCK(vp, LK_SHARED)
2133 nd->nd_repstat = EPERM;
2136 if ((vp->v_vflag & VV_ROOT) != 0
2137 && (cn.cn_flags & ISDOTDOT)
2143 r = VOP_LOOKUP(vp, &nvp,
2152 * For NFSv4, check to see if nvp is
2153 * a mount point and get the mount
2154 * point vnode, as required.
2157 nfsrv_enable_crossmntpt != 0 &&
2158 (nd->nd_flag & ND_NFSV4) != 0 &&
2159 nvp->v_type == VDIR &&
2160 nvp->v_mountedhere != NULL) {
2161 new_mp = nvp->v_mountedhere;
2162 r = vfs_busy(new_mp, 0);
2166 r = VFS_ROOT(new_mp,
2176 ((nd->nd_flag & ND_NFSV3) ||
2177 NFSNONZERO_ATTRBIT(&attrbits))) {
2178 r = nfsvno_getfh(nvp, &nfh, p);
2180 r = nfsvno_getattr(nvp, nvap,
2182 if (r == 0 && is_zfs == 1 &&
2183 nfsrv_enable_crossmntpt != 0 &&
2184 (nd->nd_flag & ND_NFSV4) != 0 &&
2185 nvp->v_type == VDIR &&
2186 vp->v_mount != nvp->v_mount) {
2188 * For a ZFS snapshot, there is a
2189 * pseudo mount that does not set
2190 * v_mountedhere, so it needs to
2191 * be detected via a different
2196 new_mp = nvp->v_mount;
2203 if (!NFSISSET_ATTRBIT(&attrbits,
2204 NFSATTRBIT_RDATTRERROR)) {
2207 if (needs_unbusy != 0)
2216 * Build the directory record xdr
2218 if (nd->nd_flag & ND_NFSV3) {
2219 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2220 *tl++ = newnfs_true;
2222 *tl = txdr_unsigned(dp->d_fileno);
2223 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2224 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2226 *tl = txdr_unsigned(*cookiep);
2227 nfsrv_postopattr(nd, 0, nvap);
2228 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2229 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2233 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2234 *tl++ = newnfs_true;
2236 *tl = txdr_unsigned(*cookiep);
2237 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2239 supports_nfsv4acls =
2240 nfs_supportsnfsv4acls(nvp);
2241 NFSVOPUNLOCK(nvp, 0);
2243 supports_nfsv4acls = 0;
2245 dirlen += nfsrv_putreferralattr(nd,
2248 if (nd->nd_repstat) {
2251 if (needs_unbusy != 0)
2256 dirlen += nfsvno_fillattr(nd, new_mp,
2257 nvp, nvap, &nfh, r, &rderrbits,
2258 nd->nd_cred, p, isdgram, 0,
2259 supports_nfsv4acls, at_root,
2262 dirlen += nfsvno_fillattr(nd, new_mp,
2263 nvp, nvap, &nfh, r, &attrbits,
2264 nd->nd_cred, p, isdgram, 0,
2265 supports_nfsv4acls, at_root,
2270 dirlen += (3 * NFSX_UNSIGNED);
2272 if (needs_unbusy != 0)
2277 cpos += dp->d_reclen;
2278 dp = (struct dirent *)cpos;
2286 * If dirlen > cnt, we must strip off the last entry. If that
2287 * results in an empty reply, report NFSERR_TOOSMALL.
2289 if (dirlen > cnt || nd->nd_repstat) {
2290 if (!nd->nd_repstat && entrycnt == 0)
2291 nd->nd_repstat = NFSERR_TOOSMALL;
2292 if (nd->nd_repstat) {
2293 newnfs_trimtrailing(nd, mb0, bpos0);
2294 if (nd->nd_flag & ND_NFSV3)
2295 nfsrv_postopattr(nd, getret, &at);
2297 newnfs_trimtrailing(nd, mb1, bpos1);
2299 } else if (cpos < cend)
2301 if (!nd->nd_repstat) {
2302 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2303 *tl++ = newnfs_false;
2309 FREE((caddr_t)cookies, M_TEMP);
2310 FREE((caddr_t)rbuf, M_TEMP);
2313 NFSEXITCODE2(0, nd);
2317 NFSEXITCODE2(error, nd);
2322 * Get the settable attributes out of the mbuf list.
2323 * (Return 0 or EBADRPC)
2326 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2327 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2330 struct nfsv2_sattr *sp;
2331 int error = 0, toclient = 0;
2333 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2335 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2337 * Some old clients didn't fill in the high order 16bits.
2338 * --> check the low order 2 bytes for 0xffff
2340 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2341 nvap->na_mode = nfstov_mode(sp->sa_mode);
2342 if (sp->sa_uid != newnfs_xdrneg1)
2343 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2344 if (sp->sa_gid != newnfs_xdrneg1)
2345 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2346 if (sp->sa_size != newnfs_xdrneg1)
2347 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2348 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2350 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2352 nvap->na_atime.tv_sec =
2353 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2354 nvap->na_atime.tv_nsec = 0;
2357 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2358 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2361 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2362 if (*tl == newnfs_true) {
2363 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2364 nvap->na_mode = nfstov_mode(*tl);
2366 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2367 if (*tl == newnfs_true) {
2368 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2369 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2371 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2372 if (*tl == newnfs_true) {
2373 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2374 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2376 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2377 if (*tl == newnfs_true) {
2378 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2379 nvap->na_size = fxdr_hyper(tl);
2381 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2382 switch (fxdr_unsigned(int, *tl)) {
2383 case NFSV3SATTRTIME_TOCLIENT:
2384 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2385 fxdr_nfsv3time(tl, &nvap->na_atime);
2388 case NFSV3SATTRTIME_TOSERVER:
2389 vfs_timestamp(&nvap->na_atime);
2390 nvap->na_vaflags |= VA_UTIMES_NULL;
2393 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2394 switch (fxdr_unsigned(int, *tl)) {
2395 case NFSV3SATTRTIME_TOCLIENT:
2396 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2397 fxdr_nfsv3time(tl, &nvap->na_mtime);
2398 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2400 case NFSV3SATTRTIME_TOSERVER:
2401 vfs_timestamp(&nvap->na_mtime);
2403 nvap->na_vaflags |= VA_UTIMES_NULL;
2408 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2411 NFSEXITCODE2(error, nd);
2416 * Handle the setable attributes for V4.
2417 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2420 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2421 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2426 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2428 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2432 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2435 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2436 attrsize = fxdr_unsigned(int, *tl);
2439 * Loop around getting the setable attributes. If an unsupported
2440 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2443 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2444 bitpos = NFSATTRBIT_MAX;
2448 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2449 if (attrsum > attrsize) {
2450 error = NFSERR_BADXDR;
2453 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2455 case NFSATTRBIT_SIZE:
2456 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2457 if (vp != NULL && vp->v_type != VREG) {
2458 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2462 nvap->na_size = fxdr_hyper(tl);
2463 attrsum += NFSX_HYPER;
2465 case NFSATTRBIT_ACL:
2466 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2470 if (aceerr && !nd->nd_repstat)
2471 nd->nd_repstat = aceerr;
2474 case NFSATTRBIT_ARCHIVE:
2475 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2476 if (!nd->nd_repstat)
2477 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2478 attrsum += NFSX_UNSIGNED;
2480 case NFSATTRBIT_HIDDEN:
2481 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2482 if (!nd->nd_repstat)
2483 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2484 attrsum += NFSX_UNSIGNED;
2486 case NFSATTRBIT_MIMETYPE:
2487 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2488 i = fxdr_unsigned(int, *tl);
2489 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2492 if (!nd->nd_repstat)
2493 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2494 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2496 case NFSATTRBIT_MODE:
2497 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2498 nvap->na_mode = nfstov_mode(*tl);
2499 attrsum += NFSX_UNSIGNED;
2501 case NFSATTRBIT_OWNER:
2502 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2503 j = fxdr_unsigned(int, *tl);
2505 error = NFSERR_BADXDR;
2508 if (j > NFSV4_SMALLSTR)
2509 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2512 error = nfsrv_mtostr(nd, cp, j);
2514 if (j > NFSV4_SMALLSTR)
2515 free(cp, M_NFSSTRING);
2518 if (!nd->nd_repstat) {
2519 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2521 if (!nd->nd_repstat)
2524 if (j > NFSV4_SMALLSTR)
2525 free(cp, M_NFSSTRING);
2526 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2528 case NFSATTRBIT_OWNERGROUP:
2529 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2530 j = fxdr_unsigned(int, *tl);
2532 error = NFSERR_BADXDR;
2535 if (j > NFSV4_SMALLSTR)
2536 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2539 error = nfsrv_mtostr(nd, cp, j);
2541 if (j > NFSV4_SMALLSTR)
2542 free(cp, M_NFSSTRING);
2545 if (!nd->nd_repstat) {
2546 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2548 if (!nd->nd_repstat)
2551 if (j > NFSV4_SMALLSTR)
2552 free(cp, M_NFSSTRING);
2553 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2555 case NFSATTRBIT_SYSTEM:
2556 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2557 if (!nd->nd_repstat)
2558 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2559 attrsum += NFSX_UNSIGNED;
2561 case NFSATTRBIT_TIMEACCESSSET:
2562 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2563 attrsum += NFSX_UNSIGNED;
2564 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2565 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2566 fxdr_nfsv4time(tl, &nvap->na_atime);
2568 attrsum += NFSX_V4TIME;
2570 vfs_timestamp(&nvap->na_atime);
2571 nvap->na_vaflags |= VA_UTIMES_NULL;
2574 case NFSATTRBIT_TIMEBACKUP:
2575 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2576 if (!nd->nd_repstat)
2577 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2578 attrsum += NFSX_V4TIME;
2580 case NFSATTRBIT_TIMECREATE:
2581 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2582 if (!nd->nd_repstat)
2583 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2584 attrsum += NFSX_V4TIME;
2586 case NFSATTRBIT_TIMEMODIFYSET:
2587 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2588 attrsum += NFSX_UNSIGNED;
2589 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2590 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2591 fxdr_nfsv4time(tl, &nvap->na_mtime);
2592 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2593 attrsum += NFSX_V4TIME;
2595 vfs_timestamp(&nvap->na_mtime);
2597 nvap->na_vaflags |= VA_UTIMES_NULL;
2601 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2603 * set bitpos so we drop out of the loop.
2605 bitpos = NFSATTRBIT_MAX;
2611 * some clients pad the attrlist, so we need to skip over the
2614 if (attrsum > attrsize) {
2615 error = NFSERR_BADXDR;
2617 attrsize = NFSM_RNDUP(attrsize);
2618 if (attrsum < attrsize)
2619 error = nfsm_advance(nd, attrsize - attrsum, -1);
2622 NFSEXITCODE2(error, nd);
2627 * Check/setup export credentials.
2630 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2631 struct ucred *credanon)
2636 * Check/setup credentials.
2638 if (nd->nd_flag & ND_GSS)
2639 exp->nes_exflag &= ~MNT_EXPORTANON;
2642 * Check to see if the operation is allowed for this security flavor.
2643 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2644 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2645 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2647 if (nfsvno_testexp(nd, exp) &&
2648 nd->nd_procnum != NFSV4OP_SECINFO &&
2649 nd->nd_procnum != NFSPROC_FSINFO) {
2650 if (nd->nd_flag & ND_NFSV4)
2651 error = NFSERR_WRONGSEC;
2653 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2658 * Check to see if the file system is exported V4 only.
2660 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2661 error = NFSERR_PROGNOTV4;
2666 * Now, map the user credentials.
2667 * (Note that ND_AUTHNONE will only be set for an NFSv3
2668 * Fsinfo RPC. If set for anything else, this code might need
2671 if (NFSVNO_EXPORTED(exp)) {
2672 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2673 NFSVNO_EXPORTANON(exp) ||
2674 (nd->nd_flag & ND_AUTHNONE) != 0) {
2675 nd->nd_cred->cr_uid = credanon->cr_uid;
2676 nd->nd_cred->cr_gid = credanon->cr_gid;
2677 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2678 credanon->cr_groups);
2679 } else if ((nd->nd_flag & ND_GSS) == 0) {
2681 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2682 * if there is a replacement credential with a group
2683 * list set up by "nfsuserd -manage-gids".
2684 * If there is no replacement, nfsrv_getgrpscred()
2685 * simply returns its argument.
2687 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2692 NFSEXITCODE2(error, nd);
2700 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2701 struct ucred **credp)
2703 int i, error, *secflavors;
2705 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2706 &exp->nes_numsecflavor, &secflavors);
2708 if (nfs_rootfhset) {
2709 exp->nes_exflag = 0;
2710 exp->nes_numsecflavor = 0;
2714 /* Copy the security flavors. */
2715 for (i = 0; i < exp->nes_numsecflavor; i++)
2716 exp->nes_secflavors[i] = secflavors[i];
2723 * Get a vnode for a file handle and export stuff.
2726 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2727 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2728 struct ucred **credp)
2730 int i, error, *secflavors;
2733 exp->nes_numsecflavor = 0;
2734 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2736 /* Make sure the server replies ESTALE to the client. */
2738 if (nam && !error) {
2739 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2740 &exp->nes_numsecflavor, &secflavors);
2742 if (nfs_rootfhset) {
2743 exp->nes_exflag = 0;
2744 exp->nes_numsecflavor = 0;
2750 /* Copy the security flavors. */
2751 for (i = 0; i < exp->nes_numsecflavor; i++)
2752 exp->nes_secflavors[i] = secflavors[i];
2760 * nfsd_fhtovp() - convert a fh to a vnode ptr
2761 * - look up fsid in mount list (if not found ret error)
2762 * - get vp and export rights by calling nfsvno_fhtovp()
2763 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2765 * - if mpp != NULL, return the mount point so that it can
2766 * be used for vn_finished_write() by the caller
2769 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2770 struct vnode **vpp, struct nfsexstuff *exp,
2771 struct mount **mpp, int startwrite, struct thread *p)
2774 struct ucred *credanon;
2777 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2779 * Check for the special case of the nfsv4root_fh.
2781 mp = vfs_busyfs(&fhp->fh_fsid);
2786 nd->nd_repstat = ESTALE;
2791 vn_start_write(NULL, mpp, V_WAIT);
2792 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2793 lktype = LK_EXCLUSIVE;
2795 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2800 * For NFSv4 without a pseudo root fs, unexported file handles
2801 * can be returned, so that Lookup works everywhere.
2803 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2804 !(nd->nd_flag & ND_NFSV4)) {
2806 nd->nd_repstat = EACCES;
2810 * Personally, I've never seen any point in requiring a
2811 * reserved port#, since only in the rare case where the
2812 * clients are all boxes with secure system privileges,
2813 * does it provide any enhanced security, but... some people
2814 * believe it to be useful and keep putting this code back in.
2815 * (There is also some "security checker" out there that
2816 * complains if the nfs server doesn't enforce this.)
2817 * However, note the following:
2818 * RFC3530 (NFSv4) specifies that a reserved port# not be
2820 * RFC2623 recommends that, if a reserved port# is checked for,
2821 * that there be a way to turn that off--> ifdef'd.
2823 #ifdef NFS_REQRSVPORT
2824 if (!nd->nd_repstat) {
2825 struct sockaddr_in *saddr;
2826 struct sockaddr_in6 *saddr6;
2828 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2829 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2830 if (!(nd->nd_flag & ND_NFSV4) &&
2831 ((saddr->sin_family == AF_INET &&
2832 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2833 (saddr6->sin6_family == AF_INET6 &&
2834 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2836 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2839 #endif /* NFS_REQRSVPORT */
2842 * Check/setup credentials.
2844 if (!nd->nd_repstat) {
2845 nd->nd_saveduid = nd->nd_cred->cr_uid;
2846 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2850 if (credanon != NULL)
2852 if (nd->nd_repstat) {
2854 vn_finished_write(mp);
2861 NFSEXITCODE2(0, nd);
2868 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2870 struct filedesc *fdp;
2874 fdp = p->td_proc->p_fd;
2875 if (fd < 0 || fd >= fdp->fd_nfiles ||
2876 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2888 * Called from nfssvc() to update the exports list. Just call
2889 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2890 * in the mount list.
2893 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2895 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2897 struct nameidata nd;
2900 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2901 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2903 else if (error == 0) {
2904 if (nfsexargp->fspec == NULL) {
2909 * If fspec != NULL, this is the v4root path.
2911 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2912 nfsexargp->fspec, p);
2913 if ((error = namei(&nd)) != 0)
2915 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2918 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2919 NFSBCOPY((caddr_t)&fh,
2920 nfs_rootfh.nfsrvfh_data,
2921 sizeof (fhandle_t));
2932 * This function needs to test to see if the system is near its limit
2933 * for memory allocation via malloc() or mget() and return True iff
2934 * either of these resources are near their limit.
2935 * XXX (For now, this is just a stub.)
2937 int nfsrv_testmalloclimit = 0;
2939 nfsrv_mallocmget_limit(void)
2941 static int printmesg = 0;
2942 static int testval = 1;
2944 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2945 if ((printmesg++ % 100) == 0)
2946 printf("nfsd: malloc/mget near limit\n");
2953 * BSD specific initialization of a mount point.
2958 static int inited = 0;
2963 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2964 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2965 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2966 nfsv4root_mnt.mnt_export = NULL;
2967 TAILQ_INIT(&nfsv4root_opt);
2968 TAILQ_INIT(&nfsv4root_newopt);
2969 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2970 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2971 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2972 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2976 * Get a vnode for a file handle, without checking exports, etc.
2979 nfsvno_getvp(fhandle_t *fhp)
2985 mp = vfs_busyfs(&fhp->fh_fsid);
2988 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2996 * Do a local VOP_ADVLOCK().
2999 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3000 u_int64_t end, struct thread *td)
3006 if (nfsrv_dolocallocks == 0)
3008 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3010 fl.l_whence = SEEK_SET;
3012 fl.l_start = (off_t)first;
3013 if (end == NFS64BITSSET) {
3017 fl.l_len = (off_t)tlen;
3020 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3021 * values for all calls, so that all locks will be held by the
3022 * nfsd server. (The nfsd server handles conflicts between the
3024 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3025 * bytes, so it can't be put in l_sysid.
3027 if (nfsv4_sysid == 0)
3028 nfsv4_sysid = nlm_acquire_next_sysid();
3029 fl.l_pid = (pid_t)0;
3030 fl.l_sysid = (int)nfsv4_sysid;
3032 if (ftype == F_UNLCK)
3033 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3034 (F_POSIX | F_REMOTE));
3036 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3037 (F_POSIX | F_REMOTE));
3045 * Check the nfsv4 root exports.
3048 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3050 struct ucred *credanon;
3051 int exflags, error = 0, numsecflavor, *secflavors, i;
3053 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3054 &credanon, &numsecflavor, &secflavors);
3056 error = NFSERR_PROGUNAVAIL;
3059 if (credanon != NULL)
3061 for (i = 0; i < numsecflavor; i++) {
3062 if (secflavors[i] == AUTH_SYS)
3063 nd->nd_flag |= ND_EXAUTHSYS;
3064 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3065 nd->nd_flag |= ND_EXGSS;
3066 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3067 nd->nd_flag |= ND_EXGSSINTEGRITY;
3068 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3069 nd->nd_flag |= ND_EXGSSPRIVACY;
3078 * Nfs server pseudo system call for the nfsd's
3084 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3087 struct nfsd_addsock_args sockarg;
3088 struct nfsd_nfsd_args nfsdarg;
3089 cap_rights_t rights;
3092 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3093 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3097 * Since we don't know what rights might be required,
3098 * pretend that we need them all. It is better to be too
3099 * careful than too reckless.
3101 error = fget(td, sockarg.sock,
3102 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3105 if (fp->f_type != DTYPE_SOCKET) {
3110 error = nfsrvd_addsock(fp);
3112 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3113 if (uap->argp == NULL) {
3117 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3121 error = nfsrvd_nfsd(td, &nfsdarg);
3123 error = nfssvc_srvcall(td, uap, td->td_ucred);
3132 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3134 struct nfsex_args export;
3135 struct file *fp = NULL;
3137 struct nfsd_clid adminrevoke;
3138 struct nfsd_dumplist dumplist;
3139 struct nfsd_dumpclients *dumpclients;
3140 struct nfsd_dumplocklist dumplocklist;
3141 struct nfsd_dumplocks *dumplocks;
3142 struct nameidata nd;
3144 int error = EINVAL, igotlock;
3146 static int suspend_nfsd = 0;
3148 if (uap->flag & NFSSVC_PUBLICFH) {
3149 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3150 sizeof (fhandle_t));
3151 error = copyin(uap->argp,
3152 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3155 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3156 error = copyin(uap->argp,(caddr_t)&export,
3157 sizeof (struct nfsex_args));
3159 error = nfsrv_v4rootexport(&export, cred, p);
3160 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3163 } else if (uap->flag & NFSSVC_STABLERESTART) {
3164 error = copyin(uap->argp, (caddr_t)&stablefd,
3167 error = fp_getfvp(p, stablefd, &fp, &vp);
3168 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3170 if (!error && newnfs_numnfsd != 0)
3173 nfsrv_stablefirst.nsf_fp = fp;
3174 nfsrv_setupstable(p);
3176 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3177 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3178 sizeof (struct nfsd_clid));
3180 error = nfsrv_adminrevoke(&adminrevoke, p);
3181 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3182 error = copyin(uap->argp, (caddr_t)&dumplist,
3183 sizeof (struct nfsd_dumplist));
3184 if (!error && (dumplist.ndl_size < 1 ||
3185 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3188 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3189 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3191 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3192 error = copyout(dumpclients,
3193 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3194 free((caddr_t)dumpclients, M_TEMP);
3196 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3197 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3198 sizeof (struct nfsd_dumplocklist));
3199 if (!error && (dumplocklist.ndllck_size < 1 ||
3200 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3203 error = nfsrv_lookupfilename(&nd,
3204 dumplocklist.ndllck_fname, p);
3206 len = sizeof (struct nfsd_dumplocks) *
3207 dumplocklist.ndllck_size;
3208 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3210 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3211 dumplocklist.ndllck_size, p);
3213 error = copyout(dumplocks,
3214 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3215 free((caddr_t)dumplocks, M_TEMP);
3217 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3220 nfsd_master_pid = procp->p_pid;
3221 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3222 nfsd_master_start = procp->p_stats->p_start;
3223 nfsd_master_proc = procp;
3225 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3226 NFSLOCKV4ROOTMUTEX();
3227 if (suspend_nfsd == 0) {
3228 /* Lock out all nfsd threads */
3230 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3231 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3232 } while (igotlock == 0 && suspend_nfsd == 0);
3235 NFSUNLOCKV4ROOTMUTEX();
3237 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3238 NFSLOCKV4ROOTMUTEX();
3239 if (suspend_nfsd != 0) {
3240 nfsv4_unlock(&nfsd_suspend_lock, 0);
3243 NFSUNLOCKV4ROOTMUTEX();
3253 * Returns 0 if ok, 1 otherwise.
3256 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3261 * This seems odd, but allow the case where the security flavor
3262 * list is empty. This happens when NFSv4 is traversing non-exported
3263 * file systems. Exported file systems should always have a non-empty
3264 * security flavor list.
3266 if (exp->nes_numsecflavor == 0)
3269 for (i = 0; i < exp->nes_numsecflavor; i++) {
3271 * The tests for privacy and integrity must be first,
3272 * since ND_GSS is set for everything but AUTH_SYS.
3274 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3275 (nd->nd_flag & ND_GSSPRIVACY))
3277 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3278 (nd->nd_flag & ND_GSSINTEGRITY))
3280 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3281 (nd->nd_flag & ND_GSS))
3283 if (exp->nes_secflavors[i] == AUTH_SYS &&
3284 (nd->nd_flag & ND_GSS) == 0)
3291 * Calculate a hash value for the fid in a file handle.
3294 nfsrv_hashfh(fhandle_t *fhp)
3298 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3303 * Calculate a hash value for the sessionid.
3306 nfsrv_hashsessionid(uint8_t *sessionid)
3310 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3315 * Signal the userland master nfsd to backup the stable restart file.
3318 nfsrv_backupstable(void)
3322 if (nfsd_master_proc != NULL) {
3323 procp = pfind(nfsd_master_pid);
3324 /* Try to make sure it is the correct process. */
3325 if (procp == nfsd_master_proc &&
3326 procp->p_stats->p_start.tv_sec ==
3327 nfsd_master_start.tv_sec &&
3328 procp->p_stats->p_start.tv_usec ==
3329 nfsd_master_start.tv_usec &&
3330 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3331 kern_psignal(procp, SIGUSR2);
3333 nfsd_master_proc = NULL;
3340 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3343 * Called once to initialize data structures...
3346 nfsd_modevent(module_t mod, int type, void *data)
3349 static int loaded = 0;
3356 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3357 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
3359 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
3362 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
3363 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
3364 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3365 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3372 #ifdef VV_DISABLEDELEG
3373 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3374 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3376 nfsd_call_servertimer = nfsrv_servertimer;
3377 nfsd_call_nfsd = nfssvc_nfsd;
3382 if (newnfs_numnfsd != 0) {
3387 #ifdef VV_DISABLEDELEG
3388 vn_deleg_ops.vndeleg_recall = NULL;
3389 vn_deleg_ops.vndeleg_disable = NULL;
3391 nfsd_call_servertimer = NULL;
3392 nfsd_call_nfsd = NULL;
3394 /* Clean out all NFSv4 state. */
3395 nfsrv_throwawayallstate(curthread);
3397 /* Clean the NFS server reply cache */
3398 nfsrvd_cleancache();
3400 /* Free up the krpc server pool. */
3401 if (nfsrvd_pool != NULL)
3402 svcpool_destroy(nfsrvd_pool);
3404 /* and get rid of the locks */
3405 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3406 mtx_destroy(&nfsrchash_table[i].mtx);
3407 mtx_destroy(&nfsrcahash_table[i].mtx);
3409 mtx_destroy(&nfsrc_udpmtx);
3410 mtx_destroy(&nfs_v4root_mutex);
3411 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3412 for (i = 0; i < nfsrv_sessionhashsize; i++)
3413 mtx_destroy(&nfssessionhash[i].mtx);
3414 lockdestroy(&nfsv4root_mnt.mnt_explock);
3415 free(nfsclienthash, M_NFSDCLIENT);
3416 free(nfslockhash, M_NFSDLOCKFILE);
3417 free(nfssessionhash, M_NFSDSESSION);
3429 static moduledata_t nfsd_mod = {
3434 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3436 /* So that loader and kldload(2) can find us, wherever we are.. */
3437 MODULE_VERSION(nfsd, 1);
3438 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3439 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3440 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3441 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3442 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);