2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/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;
91 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server");
92 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
93 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
98 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
99 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
100 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
101 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
102 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
103 0, "Debug level for NFS server");
104 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
105 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
107 #define MAX_REORDERED_RPC 16
108 #define NUM_HEURISTIC 1031
109 #define NHUSE_INIT 64
111 #define NHUSE_MAX 2048
113 static struct nfsheur {
114 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
115 off_t nh_nextoff; /* next offset for sequential detection */
116 int nh_use; /* use count for selection */
117 int nh_seqcount; /* heuristic */
118 } nfsheur[NUM_HEURISTIC];
122 * Heuristic to detect sequential operation.
124 static struct nfsheur *
125 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
130 /* Locate best candidate. */
132 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
135 if (nfsheur[hi].nh_vp == vp) {
139 if (nfsheur[hi].nh_use > 0)
140 --nfsheur[hi].nh_use;
141 hi = (hi + 1) % NUM_HEURISTIC;
142 if (nfsheur[hi].nh_use < nh->nh_use)
146 /* Initialize hint if this is a new file. */
147 if (nh->nh_vp != vp) {
149 nh->nh_nextoff = uio->uio_offset;
150 nh->nh_use = NHUSE_INIT;
151 if (uio->uio_offset == 0)
157 /* Calculate heuristic. */
158 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
159 uio->uio_offset == nh->nh_nextoff) {
160 /* See comments in vfs_vnops.c:sequential_heuristic(). */
161 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
162 if (nh->nh_seqcount > IO_SEQMAX)
163 nh->nh_seqcount = IO_SEQMAX;
164 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
165 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
166 /* Probably a reordered RPC, leave seqcount alone. */
167 } else if (nh->nh_seqcount > 1) {
168 nh->nh_seqcount /= 2;
172 nh->nh_use += NHUSE_INC;
173 if (nh->nh_use > NHUSE_MAX)
174 nh->nh_use = NHUSE_MAX;
179 * Get attributes into nfsvattr structure.
182 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
183 struct thread *p, int vpislocked)
185 int error, lockedit = 0;
187 if (vpislocked == 0) {
189 * When vpislocked == 0, the vnode is either exclusively
190 * locked by this thread or not locked by this thread.
191 * As such, shared lock it, if not exclusively locked.
193 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
195 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
198 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
207 * Get a file handle for a vnode.
210 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
214 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
215 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
216 error = VOP_VPTOFH(vp, &fhp->fh_fid);
223 * Perform access checking for vnodes obtained from file handles that would
224 * refer to files already opened by a Unix client. You cannot just use
225 * vn_writechk() and VOP_ACCESSX() for two reasons.
226 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
228 * 2 - The owner is to be given access irrespective of mode bits for some
229 * operations, so that processes that chmod after opening a file don't
233 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
234 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
235 u_int32_t *supportedtypep)
238 int error = 0, getret = 0;
240 if (vpislocked == 0) {
241 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
246 if (accmode & VWRITE) {
247 /* Just vn_writechk() changed to check rdonly */
249 * Disallow write attempts on read-only file systems;
250 * unless the file is a socket or a block or character
251 * device resident on the file system.
253 if (NFSVNO_EXRDONLY(exp) ||
254 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
255 switch (vp->v_type) {
265 * If there's shared text associated with
266 * the inode, try to free it up once. If
267 * we fail, we can't allow writing.
269 if (VOP_IS_TEXT(vp) && error == 0)
279 * Should the override still be applied when ACLs are enabled?
281 error = VOP_ACCESSX(vp, accmode, cred, p);
282 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
284 * Try again with VEXPLICIT_DENY, to see if the test for
285 * deletion is supported.
287 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
289 if (vp->v_type == VDIR) {
290 accmode &= ~(VDELETE | VDELETE_CHILD);
292 error = VOP_ACCESSX(vp, accmode, cred, p);
293 } else if (supportedtypep != NULL) {
294 *supportedtypep &= ~NFSACCESS_DELETE;
300 * Allow certain operations for the owner (reads and writes
301 * on files that are already open).
303 if (override != NFSACCCHK_NOOVERRIDE &&
304 (error == EPERM || error == EACCES)) {
305 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
307 else if (override & NFSACCCHK_ALLOWOWNER) {
308 getret = VOP_GETATTR(vp, &vattr, cred);
309 if (getret == 0 && cred->cr_uid == vattr.va_uid)
322 * Set attribute(s) vnop.
325 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
326 struct thread *p, struct nfsexstuff *exp)
330 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
336 * Set up nameidata for a lookup() call and do it.
339 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
340 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
341 struct vnode **retdirp)
343 struct componentname *cnp = &ndp->ni_cnd;
347 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
348 int error = 0, crossmnt;
352 cnp->cn_nameptr = cnp->cn_pnbuf;
353 ndp->ni_strictrelative = 0;
355 * Extract and set starting directory.
357 if (dp->v_type != VDIR) {
362 nfsvno_relpathbuf(ndp);
370 if (NFSVNO_EXRDONLY(exp))
371 cnp->cn_flags |= RDONLY;
372 ndp->ni_segflg = UIO_SYSSPACE;
375 if (nd->nd_flag & ND_PUBLOOKUP) {
377 if (cnp->cn_pnbuf[0] == '/') {
380 * Check for degenerate pathnames here, since lookup()
383 for (i = 1; i < ndp->ni_pathlen; i++)
384 if (cnp->cn_pnbuf[i] != '/')
386 if (i == ndp->ni_pathlen) {
387 error = NFSERR_ACCES;
393 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
394 (nd->nd_flag & ND_NFSV4) == 0) {
396 * Only cross mount points for NFSv4 when doing a
397 * mount while traversing the file system above
398 * the mount point, unless nfsrv_enable_crossmntpt is set.
400 cnp->cn_flags |= NOCROSSMOUNT;
405 * Initialize for scan, set ni_startdir and bump ref on dp again
406 * because lookup() will dereference ni_startdir.
410 ndp->ni_startdir = dp;
411 ndp->ni_rootdir = rootvnode;
412 ndp->ni_topdir = NULL;
415 cnp->cn_flags |= LOCKLEAF;
417 cnp->cn_nameptr = cnp->cn_pnbuf;
419 * Call lookup() to do the real work. If an error occurs,
420 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
421 * we do not have to dereference anything before returning.
422 * In either case ni_startdir will be dereferenced and NULLed
430 * Check for encountering a symbolic link. Trivial
431 * termination occurs if no symlink encountered.
433 if ((cnp->cn_flags & ISSYMLINK) == 0) {
434 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
435 nfsvno_relpathbuf(ndp);
436 if (ndp->ni_vp && !lockleaf)
437 NFSVOPUNLOCK(ndp->ni_vp, 0);
444 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
445 NFSVOPUNLOCK(ndp->ni_dvp, 0);
446 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
451 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
455 if (ndp->ni_pathlen > 1)
456 cp = uma_zalloc(namei_zone, M_WAITOK);
460 aiov.iov_len = MAXPATHLEN;
461 auio.uio_iov = &aiov;
464 auio.uio_rw = UIO_READ;
465 auio.uio_segflg = UIO_SYSSPACE;
467 auio.uio_resid = MAXPATHLEN;
468 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
471 if (ndp->ni_pathlen > 1)
472 uma_zfree(namei_zone, cp);
478 linklen = MAXPATHLEN - auio.uio_resid;
483 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
484 error = ENAMETOOLONG;
489 * Adjust or replace path
491 if (ndp->ni_pathlen > 1) {
492 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
493 uma_zfree(namei_zone, cnp->cn_pnbuf);
496 cnp->cn_pnbuf[linklen] = '\0';
497 ndp->ni_pathlen += linklen;
500 * Cleanup refs for next loop and check if root directory
501 * should replace current directory. Normally ni_dvp
502 * becomes the new base directory and is cleaned up when
503 * we loop. Explicitly null pointers after invalidation
504 * to clarify operation.
509 if (cnp->cn_pnbuf[0] == '/') {
511 ndp->ni_dvp = ndp->ni_rootdir;
514 ndp->ni_startdir = ndp->ni_dvp;
518 cnp->cn_flags &= ~LOCKLEAF;
522 nfsvno_relpathbuf(ndp);
525 ndp->ni_startdir = NULL;
526 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
531 NFSEXITCODE2(error, nd);
536 * Set up a pathname buffer and return a pointer to it and, optionally
537 * set a hash pointer.
540 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
542 struct componentname *cnp = &ndp->ni_cnd;
544 cnp->cn_flags |= (NOMACCHECK | HASBUF);
545 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
548 *bufpp = cnp->cn_pnbuf;
552 * Release the above path buffer, if not released by nfsvno_namei().
555 nfsvno_relpathbuf(struct nameidata *ndp)
558 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
560 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
561 ndp->ni_cnd.cn_flags &= ~HASBUF;
565 * Readlink vnode op into an mbuf list.
568 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
569 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
571 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
572 struct iovec *ivp = iv;
573 struct uio io, *uiop = &io;
574 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
575 int i, len, tlen, error = 0;
579 while (len < NFS_MAXPATHLEN) {
581 MCLGET(mp, M_WAITOK);
582 mp->m_len = M_SIZE(mp);
589 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
590 mp->m_len = NFS_MAXPATHLEN - len;
591 len = NFS_MAXPATHLEN;
595 ivp->iov_base = mtod(mp, caddr_t);
596 ivp->iov_len = mp->m_len;
601 uiop->uio_iovcnt = i;
602 uiop->uio_offset = 0;
603 uiop->uio_resid = len;
604 uiop->uio_rw = UIO_READ;
605 uiop->uio_segflg = UIO_SYSSPACE;
607 error = VOP_READLINK(vp, uiop, cred);
613 if (uiop->uio_resid > 0) {
614 len -= uiop->uio_resid;
615 tlen = NFSM_RNDUP(len);
616 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
628 * Read vnode op call into mbuf list.
631 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
632 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
638 int error = 0, len, left, siz, tlen, ioflag = 0;
639 struct mbuf *m2 = NULL, *m3;
640 struct uio io, *uiop = &io;
643 len = left = NFSM_RNDUP(cnt);
646 * Generate the mbuf list with the uio_iov ref. to it.
653 siz = min(M_TRAILINGSPACE(m), left);
662 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
664 uiop->uio_iov = iv2 = iv;
670 panic("nfsvno_read iov");
671 siz = min(M_TRAILINGSPACE(m), left);
673 iv->iov_base = mtod(m, caddr_t) + m->m_len;
682 uiop->uio_iovcnt = i;
683 uiop->uio_offset = off;
684 uiop->uio_resid = len;
685 uiop->uio_rw = UIO_READ;
686 uiop->uio_segflg = UIO_SYSSPACE;
688 nh = nfsrv_sequential_heuristic(uiop, vp);
689 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
690 /* XXX KDM make this more systematic? */
691 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
692 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
693 FREE((caddr_t)iv2, M_TEMP);
699 nh->nh_nextoff = uiop->uio_offset;
700 tlen = len - uiop->uio_resid;
701 cnt = cnt < tlen ? cnt : tlen;
702 tlen = NFSM_RNDUP(cnt);
706 } else if (len != tlen || tlen != cnt)
707 nfsrv_adj(m3, len - tlen, tlen - cnt);
717 * Write vnode op from an mbuf list.
720 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
721 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
727 struct uio io, *uiop = &io;
730 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
732 uiop->uio_iov = iv = ivp;
733 uiop->uio_iovcnt = cnt;
734 i = mtod(mp, caddr_t) + mp->m_len - cp;
738 panic("nfsvno_write");
749 cp = mtod(mp, caddr_t);
753 if (stable == NFSWRITE_UNSTABLE)
754 ioflags = IO_NODELOCKED;
756 ioflags = (IO_SYNC | IO_NODELOCKED);
757 uiop->uio_resid = retlen;
758 uiop->uio_rw = UIO_WRITE;
759 uiop->uio_segflg = UIO_SYSSPACE;
761 uiop->uio_offset = off;
762 nh = nfsrv_sequential_heuristic(uiop, vp);
763 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
764 /* XXX KDM make this more systematic? */
765 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
766 error = VOP_WRITE(vp, uiop, ioflags, cred);
768 nh->nh_nextoff = uiop->uio_offset;
769 FREE((caddr_t)iv, M_TEMP);
776 * Common code for creating a regular file (plus special files for V2).
779 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
780 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
781 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
786 error = nd->nd_repstat;
787 if (!error && ndp->ni_vp == NULL) {
788 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
789 vrele(ndp->ni_startdir);
790 error = VOP_CREATE(ndp->ni_dvp,
791 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
793 nfsvno_relpathbuf(ndp);
795 if (*exclusive_flagp) {
796 *exclusive_flagp = 0;
797 NFSVNO_ATTRINIT(nvap);
798 nvap->na_atime.tv_sec = cverf[0];
799 nvap->na_atime.tv_nsec = cverf[1];
800 error = VOP_SETATTR(ndp->ni_vp,
801 &nvap->na_vattr, nd->nd_cred);
805 error = NFSERR_NOTSUPP;
810 * NFS V2 Only. nfsrvd_mknod() does this for V3.
811 * (This implies, just get out on an error.)
813 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
814 nvap->na_type == VFIFO) {
815 if (nvap->na_type == VCHR && rdev == 0xffffffff)
816 nvap->na_type = VFIFO;
817 if (nvap->na_type != VFIFO &&
818 (error = priv_check_cred(nd->nd_cred,
819 PRIV_VFS_MKNOD_DEV, 0))) {
820 vrele(ndp->ni_startdir);
821 nfsvno_relpathbuf(ndp);
825 nvap->na_rdev = rdev;
826 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
827 &ndp->ni_cnd, &nvap->na_vattr);
829 nfsvno_relpathbuf(ndp);
830 vrele(ndp->ni_startdir);
834 vrele(ndp->ni_startdir);
835 nfsvno_relpathbuf(ndp);
843 * Handle cases where error is already set and/or
845 * 1 - clean up the lookup
846 * 2 - iff !error and na_size set, truncate it
848 vrele(ndp->ni_startdir);
849 nfsvno_relpathbuf(ndp);
851 if (ndp->ni_dvp == *vpp)
855 if (!error && nvap->na_size != VNOVAL) {
856 error = nfsvno_accchk(*vpp, VWRITE,
857 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
858 NFSACCCHK_VPISLOCKED, NULL);
860 tempsize = nvap->na_size;
861 NFSVNO_ATTRINIT(nvap);
862 nvap->na_size = tempsize;
863 error = VOP_SETATTR(*vpp,
864 &nvap->na_vattr, nd->nd_cred);
877 * Do a mknod vnode op.
880 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
886 vtyp = nvap->na_type;
888 * Iff doesn't exist, create it.
891 vrele(ndp->ni_startdir);
892 nfsvno_relpathbuf(ndp);
898 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
899 vrele(ndp->ni_startdir);
900 nfsvno_relpathbuf(ndp);
902 error = NFSERR_BADTYPE;
906 vrele(ndp->ni_startdir);
907 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
908 &ndp->ni_cnd, &nvap->na_vattr);
910 nfsvno_relpathbuf(ndp);
912 if (nvap->na_type != VFIFO &&
913 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
914 vrele(ndp->ni_startdir);
915 nfsvno_relpathbuf(ndp);
919 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
920 &ndp->ni_cnd, &nvap->na_vattr);
922 nfsvno_relpathbuf(ndp);
923 vrele(ndp->ni_startdir);
925 * Since VOP_MKNOD returns the ni_vp, I can't
926 * see any reason to do the lookup.
939 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
940 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
944 if (ndp->ni_vp != NULL) {
945 if (ndp->ni_dvp == ndp->ni_vp)
950 nfsvno_relpathbuf(ndp);
954 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
957 nfsvno_relpathbuf(ndp);
968 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
969 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
970 struct nfsexstuff *exp)
975 vrele(ndp->ni_startdir);
976 nfsvno_relpathbuf(ndp);
977 if (ndp->ni_dvp == ndp->ni_vp)
986 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
987 &nvap->na_vattr, pathcp);
989 vrele(ndp->ni_startdir);
990 nfsvno_relpathbuf(ndp);
992 * Although FreeBSD still had the lookup code in
993 * it for 7/current, there doesn't seem to be any
994 * point, since VOP_SYMLINK() returns the ni_vp.
995 * Just vput it for v2.
997 if (!not_v2 && !error)
1006 * Parse symbolic link arguments.
1007 * This function has an ugly side effect. It will MALLOC() an area for
1008 * the symlink and set iov_base to point to it, only if it succeeds.
1009 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1013 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1014 struct thread *p, char **pathcpp, int *lenp)
1017 char *pathcp = NULL;
1019 struct nfsv2_sattr *sp;
1023 if ((nd->nd_flag & ND_NFSV3) &&
1024 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1026 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1027 len = fxdr_unsigned(int, *tl);
1028 if (len > NFS_MAXPATHLEN || len <= 0) {
1032 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1033 error = nfsrv_mtostr(nd, pathcp, len);
1036 if (nd->nd_flag & ND_NFSV2) {
1037 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1038 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1042 NFSEXITCODE2(0, nd);
1046 free(pathcp, M_TEMP);
1047 NFSEXITCODE2(error, nd);
1052 * Remove a non-directory object.
1055 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1056 struct thread *p, struct nfsexstuff *exp)
1062 if (vp->v_type == VDIR)
1063 error = NFSERR_ISDIR;
1065 error = nfsrv_checkremove(vp, 1, p);
1067 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1068 if (ndp->ni_dvp == vp)
1073 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1074 nfsvno_relpathbuf(ndp);
1080 * Remove a directory.
1083 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1084 struct thread *p, struct nfsexstuff *exp)
1090 if (vp->v_type != VDIR) {
1095 * No rmdir "." please.
1097 if (ndp->ni_dvp == vp) {
1102 * The root of a mounted filesystem cannot be deleted.
1104 if (vp->v_vflag & VV_ROOT)
1108 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1109 if (ndp->ni_dvp == vp)
1114 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1115 nfsvno_relpathbuf(ndp);
1124 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1125 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1127 struct vnode *fvp, *tvp, *tdvp;
1130 fvp = fromndp->ni_vp;
1132 vrele(fromndp->ni_dvp);
1137 tdvp = tondp->ni_dvp;
1140 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1141 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1143 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1144 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1147 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1148 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1153 * A rename to '.' or '..' results in a prematurely
1154 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1157 if ((tondp->ni_cnd.cn_namelen == 1 &&
1158 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1159 (tondp->ni_cnd.cn_namelen == 2 &&
1160 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1161 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1166 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1167 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1170 if (fvp->v_mount != tdvp->v_mount) {
1171 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1175 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1180 * If source and destination are the same, there is nothing to
1181 * do. Set error to -1 to indicate this.
1186 if (ndflag & ND_NFSV4) {
1187 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1188 error = nfsrv_checkremove(fvp, 0, p);
1189 NFSVOPUNLOCK(fvp, 0);
1193 error = nfsrv_checkremove(tvp, 1, p);
1196 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1197 * that the NFSv4 client won't be confused by the rename.
1198 * Since nfsd_recalldelegation() can only be called on an
1199 * unlocked vnode at this point and fvp is the file that will
1200 * still exist after the rename, just do fvp.
1202 nfsd_recalldelegation(fvp, p);
1206 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1207 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1216 vrele(fromndp->ni_dvp);
1221 vrele(tondp->ni_startdir);
1222 nfsvno_relpathbuf(tondp);
1224 vrele(fromndp->ni_startdir);
1225 nfsvno_relpathbuf(fromndp);
1234 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1235 struct thread *p, struct nfsexstuff *exp)
1245 if (vp->v_mount != xp->v_mount)
1249 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1250 if ((vp->v_iflag & VI_DOOMED) == 0)
1251 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1254 if (ndp->ni_dvp == vp)
1258 NFSVOPUNLOCK(vp, 0);
1260 if (ndp->ni_dvp == ndp->ni_vp)
1267 nfsvno_relpathbuf(ndp);
1273 * Do the fsync() appropriate for the commit.
1276 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1282 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1283 * file is done. At this time VOP_FSYNC does not accept offset and
1284 * byte count parameters so call VOP_FSYNC the whole file for now.
1285 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1286 * File systems that do not use the buffer cache (as indicated
1287 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1289 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1290 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1292 * Give up and do the whole thing
1295 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1296 VM_OBJECT_WLOCK(vp->v_object);
1297 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1298 VM_OBJECT_WUNLOCK(vp->v_object);
1300 error = VOP_FSYNC(vp, MNT_WAIT, td);
1303 * Locate and synchronously write any buffers that fall
1304 * into the requested range. Note: we are assuming that
1305 * f_iosize is a power of 2.
1307 int iosize = vp->v_mount->mnt_stat.f_iosize;
1308 int iomask = iosize - 1;
1313 * Align to iosize boundary, super-align to page boundary.
1316 cnt += off & iomask;
1317 off &= ~(u_quad_t)iomask;
1319 if (off & PAGE_MASK) {
1320 cnt += off & PAGE_MASK;
1321 off &= ~(u_quad_t)PAGE_MASK;
1323 lblkno = off / iosize;
1326 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1327 VM_OBJECT_WLOCK(vp->v_object);
1328 vm_object_page_clean(vp->v_object, off, off + cnt,
1330 VM_OBJECT_WUNLOCK(vp->v_object);
1339 * If we have a buffer and it is marked B_DELWRI we
1340 * have to lock and write it. Otherwise the prior
1341 * write is assumed to have already been committed.
1343 * gbincore() can return invalid buffers now so we
1344 * have to check that bit as well (though B_DELWRI
1345 * should not be set if B_INVAL is set there could be
1346 * a race here since we haven't locked the buffer).
1348 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1349 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1350 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1352 continue; /* retry */
1354 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1357 bp->b_flags &= ~B_ASYNC;
1380 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1384 error = VFS_STATFS(vp->v_mount, sf);
1387 * Since NFS handles these values as unsigned on the
1388 * wire, there is no way to represent negative values,
1389 * so set them to 0. Without this, they will appear
1390 * to be very large positive values for clients like
1393 if (sf->f_bavail < 0)
1395 if (sf->f_ffree < 0)
1403 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1404 * must handle nfsrv_opencheck() calls after any other access checks.
1407 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1408 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1409 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1410 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1411 struct nfsexstuff *exp, struct vnode **vpp)
1413 struct vnode *vp = NULL;
1415 struct nfsexstuff nes;
1417 if (ndp->ni_vp == NULL)
1418 nd->nd_repstat = nfsrv_opencheck(clientid,
1419 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1420 if (!nd->nd_repstat) {
1421 if (ndp->ni_vp == NULL) {
1422 vrele(ndp->ni_startdir);
1423 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1424 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1426 nfsvno_relpathbuf(ndp);
1427 if (!nd->nd_repstat) {
1428 if (*exclusive_flagp) {
1429 *exclusive_flagp = 0;
1430 NFSVNO_ATTRINIT(nvap);
1431 nvap->na_atime.tv_sec = cverf[0];
1432 nvap->na_atime.tv_nsec = cverf[1];
1433 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1434 &nvap->na_vattr, cred);
1435 if (nd->nd_repstat != 0) {
1438 nd->nd_repstat = NFSERR_NOTSUPP;
1441 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1442 aclp, p, attrbitp, exp);
1447 if (ndp->ni_startdir)
1448 vrele(ndp->ni_startdir);
1449 nfsvno_relpathbuf(ndp);
1451 if (create == NFSV4OPEN_CREATE) {
1452 if (ndp->ni_dvp == vp)
1457 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1458 if (ndp->ni_cnd.cn_flags & RDONLY)
1459 NFSVNO_SETEXRDONLY(&nes);
1461 NFSVNO_EXINIT(&nes);
1462 nd->nd_repstat = nfsvno_accchk(vp,
1463 VWRITE, cred, &nes, p,
1464 NFSACCCHK_NOOVERRIDE,
1465 NFSACCCHK_VPISLOCKED, NULL);
1466 nd->nd_repstat = nfsrv_opencheck(clientid,
1467 stateidp, stp, vp, nd, p, nd->nd_repstat);
1468 if (!nd->nd_repstat) {
1469 tempsize = nvap->na_size;
1470 NFSVNO_ATTRINIT(nvap);
1471 nvap->na_size = tempsize;
1472 nd->nd_repstat = VOP_SETATTR(vp,
1473 &nvap->na_vattr, cred);
1475 } else if (vp->v_type == VREG) {
1476 nd->nd_repstat = nfsrv_opencheck(clientid,
1477 stateidp, stp, vp, nd, p, nd->nd_repstat);
1481 if (ndp->ni_cnd.cn_flags & HASBUF)
1482 nfsvno_relpathbuf(ndp);
1483 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1484 vrele(ndp->ni_startdir);
1485 if (ndp->ni_dvp == ndp->ni_vp)
1495 NFSEXITCODE2(0, nd);
1499 * Updates the file rev and sets the mtime and ctime
1500 * to the current clock time, returning the va_filerev and va_Xtime
1502 * Return ESTALE to indicate the vnode is VI_DOOMED.
1505 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1506 struct ucred *cred, struct thread *p)
1511 vfs_timestamp(&va.va_mtime);
1512 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1513 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1514 if ((vp->v_iflag & VI_DOOMED) != 0)
1517 (void) VOP_SETATTR(vp, &va, cred);
1518 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1523 * Glue routine to nfsv4_fillattr().
1526 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1527 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1528 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1529 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1533 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1534 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1536 NFSEXITCODE2(0, nd);
1540 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1542 * nfs readdir service
1543 * - mallocs what it thinks is enough to read
1544 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1545 * - calls VOP_READDIR()
1546 * - loops around building the reply
1547 * if the output generated exceeds count break out of loop
1548 * The NFSM_CLGET macro is used here so that the reply will be packed
1549 * tightly in mbuf clusters.
1550 * - it trims out records with d_fileno == 0
1551 * this doesn't matter for Unix clients, but they might confuse clients
1553 * - it trims out records with d_type == DT_WHT
1554 * these cannot be seen through NFS (unless we extend the protocol)
1555 * The alternate call nfsrvd_readdirplus() does lookups as well.
1556 * PS: The NFS protocol spec. does not clarify what the "count" byte
1557 * argument is a count of.. just name strings and file id's or the
1558 * entire reply rpc or ...
1559 * I tried just file name and id sizes and it confused the Sun client,
1560 * so I am using the full rpc size now. The "paranoia.." comment refers
1561 * to including the status longwords that are not a part of the dir.
1562 * "entry" structures, but are in the rpc.
1565 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1566 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1571 char *cpos, *cend, *rbuf;
1573 int nlen, error = 0, getret = 1;
1574 int siz, cnt, fullsiz, eofflag, ncookies;
1575 u_int64_t off, toff, verf;
1576 u_long *cookies = NULL, *cookiep;
1581 if (nd->nd_repstat) {
1582 nfsrv_postopattr(nd, getret, &at);
1585 if (nd->nd_flag & ND_NFSV2) {
1586 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1587 off = fxdr_unsigned(u_quad_t, *tl++);
1589 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1590 off = fxdr_hyper(tl);
1592 verf = fxdr_hyper(tl);
1596 cnt = fxdr_unsigned(int, *tl);
1597 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1598 cnt = NFS_SRVMAXDATA(nd);
1599 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1601 if (nd->nd_flag & ND_NFSV3) {
1602 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1606 * va_filerev is not sufficient as a cookie verifier,
1607 * since it is not supposed to change when entries are
1608 * removed/added unless that offset cookies returned to
1609 * the client are no longer valid.
1611 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1612 nd->nd_repstat = NFSERR_BAD_COOKIE;
1615 if (!nd->nd_repstat && vp->v_type != VDIR)
1616 nd->nd_repstat = NFSERR_NOTDIR;
1617 if (nd->nd_repstat == 0 && cnt == 0) {
1618 if (nd->nd_flag & ND_NFSV2)
1619 /* NFSv2 does not have NFSERR_TOOSMALL */
1620 nd->nd_repstat = EPERM;
1622 nd->nd_repstat = NFSERR_TOOSMALL;
1624 if (!nd->nd_repstat)
1625 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1626 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1627 NFSACCCHK_VPISLOCKED, NULL);
1628 if (nd->nd_repstat) {
1630 if (nd->nd_flag & ND_NFSV3)
1631 nfsrv_postopattr(nd, getret, &at);
1634 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1635 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1639 free((caddr_t)cookies, M_TEMP);
1647 io.uio_offset = (off_t)off;
1649 io.uio_segflg = UIO_SYSSPACE;
1650 io.uio_rw = UIO_READ;
1652 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1654 off = (u_int64_t)io.uio_offset;
1656 siz -= io.uio_resid;
1658 if (!cookies && !nd->nd_repstat)
1659 nd->nd_repstat = NFSERR_PERM;
1660 if (nd->nd_flag & ND_NFSV3) {
1661 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1662 if (!nd->nd_repstat)
1663 nd->nd_repstat = getret;
1667 * Handles the failed cases. nd->nd_repstat == 0 past here.
1669 if (nd->nd_repstat) {
1671 free((caddr_t)rbuf, M_TEMP);
1673 free((caddr_t)cookies, M_TEMP);
1674 if (nd->nd_flag & ND_NFSV3)
1675 nfsrv_postopattr(nd, getret, &at);
1679 * If nothing read, return eof
1684 if (nd->nd_flag & ND_NFSV2) {
1685 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1687 nfsrv_postopattr(nd, getret, &at);
1688 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1689 txdr_hyper(at.na_filerev, tl);
1692 *tl++ = newnfs_false;
1694 FREE((caddr_t)rbuf, M_TEMP);
1695 FREE((caddr_t)cookies, M_TEMP);
1700 * Check for degenerate cases of nothing useful read.
1701 * If so go try again
1705 dp = (struct dirent *)cpos;
1709 * For some reason FreeBSD's ufs_readdir() chooses to back the
1710 * directory offset up to a block boundary, so it is necessary to
1711 * skip over the records that precede the requested offset. This
1712 * requires the assumption that file offset cookies monotonically
1715 while (cpos < cend && ncookies > 0 &&
1716 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1717 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1718 cpos += dp->d_reclen;
1719 dp = (struct dirent *)cpos;
1723 if (cpos >= cend || ncookies == 0) {
1731 * dirlen is the size of the reply, including all XDR and must
1732 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1733 * if the XDR should be included in "count", but to be safe, we do.
1734 * (Include the two booleans at the end of the reply in dirlen now.)
1736 if (nd->nd_flag & ND_NFSV3) {
1737 nfsrv_postopattr(nd, getret, &at);
1738 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1739 txdr_hyper(at.na_filerev, tl);
1740 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1742 dirlen = 2 * NFSX_UNSIGNED;
1745 /* Loop through the records and build reply */
1746 while (cpos < cend && ncookies > 0) {
1747 nlen = dp->d_namlen;
1748 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1749 nlen <= NFS_MAXNAMLEN) {
1750 if (nd->nd_flag & ND_NFSV3)
1751 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1753 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1760 * Build the directory record xdr from
1763 if (nd->nd_flag & ND_NFSV3) {
1764 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1765 *tl++ = newnfs_true;
1768 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1769 *tl++ = newnfs_true;
1771 *tl = txdr_unsigned(dp->d_fileno);
1772 (void) nfsm_strtom(nd, dp->d_name, nlen);
1773 if (nd->nd_flag & ND_NFSV3) {
1774 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1777 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1778 *tl = txdr_unsigned(*cookiep);
1780 cpos += dp->d_reclen;
1781 dp = (struct dirent *)cpos;
1787 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1788 *tl++ = newnfs_false;
1793 FREE((caddr_t)rbuf, M_TEMP);
1794 FREE((caddr_t)cookies, M_TEMP);
1797 NFSEXITCODE2(0, nd);
1801 NFSEXITCODE2(error, nd);
1806 * Readdirplus for V3 and Readdir for V4.
1809 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1810 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1815 char *cpos, *cend, *rbuf;
1818 struct nfsvattr nva, at, *nvap = &nva;
1819 struct mbuf *mb0, *mb1;
1820 struct nfsreferral *refp;
1821 int nlen, r, error = 0, getret = 1, usevget = 1;
1822 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1823 caddr_t bpos0, bpos1;
1824 u_int64_t off, toff, verf;
1825 u_long *cookies = NULL, *cookiep;
1826 nfsattrbit_t attrbits, rderrbits, savbits;
1829 struct componentname cn;
1830 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
1831 struct mount *mp, *new_mp;
1832 uint64_t mounted_on_fileno;
1834 if (nd->nd_repstat) {
1835 nfsrv_postopattr(nd, getret, &at);
1838 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1839 off = fxdr_hyper(tl);
1842 verf = fxdr_hyper(tl);
1844 siz = fxdr_unsigned(int, *tl++);
1845 cnt = fxdr_unsigned(int, *tl);
1848 * Use the server's maximum data transfer size as the upper bound
1851 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1852 cnt = NFS_SRVMAXDATA(nd);
1855 * siz is a "hint" of how much directory information (name, fileid,
1856 * cookie) should be in the reply. At least one client "hints" 0,
1857 * so I set it to cnt for that case. I also round it up to the
1858 * next multiple of DIRBLKSIZ.
1862 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1864 if (nd->nd_flag & ND_NFSV4) {
1865 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1868 NFSSET_ATTRBIT(&savbits, &attrbits);
1869 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1870 NFSZERO_ATTRBIT(&rderrbits);
1871 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1873 NFSZERO_ATTRBIT(&attrbits);
1876 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1877 if (!nd->nd_repstat) {
1878 if (off && verf != at.na_filerev) {
1880 * va_filerev is not sufficient as a cookie verifier,
1881 * since it is not supposed to change when entries are
1882 * removed/added unless that offset cookies returned to
1883 * the client are no longer valid.
1886 if (nd->nd_flag & ND_NFSV4) {
1887 nd->nd_repstat = NFSERR_NOTSAME;
1889 nd->nd_repstat = NFSERR_BAD_COOKIE;
1892 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1893 nd->nd_repstat = NFSERR_BAD_COOKIE;
1896 if (!nd->nd_repstat && vp->v_type != VDIR)
1897 nd->nd_repstat = NFSERR_NOTDIR;
1898 if (!nd->nd_repstat && cnt == 0)
1899 nd->nd_repstat = NFSERR_TOOSMALL;
1900 if (!nd->nd_repstat)
1901 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1902 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1903 NFSACCCHK_VPISLOCKED, NULL);
1904 if (nd->nd_repstat) {
1906 if (nd->nd_flag & ND_NFSV3)
1907 nfsrv_postopattr(nd, getret, &at);
1910 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1911 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
1913 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1917 free((caddr_t)cookies, M_TEMP);
1925 io.uio_offset = (off_t)off;
1927 io.uio_segflg = UIO_SYSSPACE;
1928 io.uio_rw = UIO_READ;
1930 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1932 off = (u_int64_t)io.uio_offset;
1934 siz -= io.uio_resid;
1936 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1938 if (!cookies && !nd->nd_repstat)
1939 nd->nd_repstat = NFSERR_PERM;
1940 if (!nd->nd_repstat)
1941 nd->nd_repstat = getret;
1942 if (nd->nd_repstat) {
1945 free((caddr_t)cookies, M_TEMP);
1946 free((caddr_t)rbuf, M_TEMP);
1947 if (nd->nd_flag & ND_NFSV3)
1948 nfsrv_postopattr(nd, getret, &at);
1952 * If nothing read, return eof
1957 if (nd->nd_flag & ND_NFSV3)
1958 nfsrv_postopattr(nd, getret, &at);
1959 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1960 txdr_hyper(at.na_filerev, tl);
1962 *tl++ = newnfs_false;
1964 free((caddr_t)cookies, M_TEMP);
1965 free((caddr_t)rbuf, M_TEMP);
1970 * Check for degenerate cases of nothing useful read.
1971 * If so go try again
1975 dp = (struct dirent *)cpos;
1979 * For some reason FreeBSD's ufs_readdir() chooses to back the
1980 * directory offset up to a block boundary, so it is necessary to
1981 * skip over the records that precede the requested offset. This
1982 * requires the assumption that file offset cookies monotonically
1985 while (cpos < cend && ncookies > 0 &&
1986 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1987 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
1988 ((nd->nd_flag & ND_NFSV4) &&
1989 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1990 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1991 cpos += dp->d_reclen;
1992 dp = (struct dirent *)cpos;
1996 if (cpos >= cend || ncookies == 0) {
2003 * Busy the file system so that the mount point won't go away
2004 * and, as such, VFS_VGET() can be used safely.
2008 NFSVOPUNLOCK(vp, 0);
2009 nd->nd_repstat = vfs_busy(mp, 0);
2011 if (nd->nd_repstat != 0) {
2013 free(cookies, M_TEMP);
2015 if (nd->nd_flag & ND_NFSV3)
2016 nfsrv_postopattr(nd, getret, &at);
2021 * Check to see if entries in this directory can be safely acquired
2022 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2023 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2024 * automount of the snapshot directory that is required will
2026 * This needs to be done here for NFSv4, since NFSv4 never does
2027 * a VFS_VGET() for "." or "..".
2030 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2031 if (r == EOPNOTSUPP) {
2033 cn.cn_nameiop = LOOKUP;
2034 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2035 cn.cn_cred = nd->nd_cred;
2042 * Save this position, in case there is an error before one entry
2046 bpos0 = nd->nd_bpos;
2049 * Fill in the first part of the reply.
2050 * dirlen is the reply length in bytes and cannot exceed cnt.
2051 * (Include the two booleans at the end of the reply in dirlen now,
2052 * so we recognize when we have exceeded cnt.)
2054 if (nd->nd_flag & ND_NFSV3) {
2055 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2056 nfsrv_postopattr(nd, getret, &at);
2058 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2060 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2061 txdr_hyper(at.na_filerev, tl);
2064 * Save this position, in case there is an empty reply needed.
2067 bpos1 = nd->nd_bpos;
2069 /* Loop through the records and build reply */
2071 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2072 nlen = dp->d_namlen;
2073 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2074 nlen <= NFS_MAXNAMLEN &&
2075 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2076 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2077 || (nlen == 1 && dp->d_name[0] != '.'))) {
2079 * Save the current position in the reply, in case
2080 * this entry exceeds cnt.
2083 bpos1 = nd->nd_bpos;
2086 * For readdir_and_lookup get the vnode using
2095 mounted_on_fileno = (uint64_t)dp->d_fileno;
2096 if ((nd->nd_flag & ND_NFSV3) ||
2097 NFSNONZERO_ATTRBIT(&savbits)) {
2098 if (nd->nd_flag & ND_NFSV4)
2099 refp = nfsv4root_getreferral(NULL,
2103 r = VFS_VGET(mp, dp->d_fileno,
2107 if (r == EOPNOTSUPP) {
2110 cn.cn_nameiop = LOOKUP;
2118 cn.cn_nameptr = dp->d_name;
2119 cn.cn_namelen = nlen;
2120 cn.cn_flags = ISLASTCN |
2121 NOFOLLOW | LOCKLEAF;
2123 dp->d_name[0] == '.' &&
2124 dp->d_name[1] == '.')
2127 if (NFSVOPLOCK(vp, LK_SHARED)
2129 nd->nd_repstat = EPERM;
2132 if ((vp->v_vflag & VV_ROOT) != 0
2133 && (cn.cn_flags & ISDOTDOT)
2139 r = VOP_LOOKUP(vp, &nvp,
2148 * For NFSv4, check to see if nvp is
2149 * a mount point and get the mount
2150 * point vnode, as required.
2153 nfsrv_enable_crossmntpt != 0 &&
2154 (nd->nd_flag & ND_NFSV4) != 0 &&
2155 nvp->v_type == VDIR &&
2156 nvp->v_mountedhere != NULL) {
2157 new_mp = nvp->v_mountedhere;
2158 r = vfs_busy(new_mp, 0);
2162 r = VFS_ROOT(new_mp,
2172 ((nd->nd_flag & ND_NFSV3) ||
2173 NFSNONZERO_ATTRBIT(&attrbits))) {
2174 r = nfsvno_getfh(nvp, &nfh, p);
2176 r = nfsvno_getattr(nvp, nvap,
2178 if (r == 0 && is_zfs == 1 &&
2179 nfsrv_enable_crossmntpt != 0 &&
2180 (nd->nd_flag & ND_NFSV4) != 0 &&
2181 nvp->v_type == VDIR &&
2182 vp->v_mount != nvp->v_mount) {
2184 * For a ZFS snapshot, there is a
2185 * pseudo mount that does not set
2186 * v_mountedhere, so it needs to
2187 * be detected via a different
2192 new_mp = nvp->v_mount;
2199 if (!NFSISSET_ATTRBIT(&attrbits,
2200 NFSATTRBIT_RDATTRERROR)) {
2203 if (needs_unbusy != 0)
2212 * Build the directory record xdr
2214 if (nd->nd_flag & ND_NFSV3) {
2215 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2216 *tl++ = newnfs_true;
2218 *tl = txdr_unsigned(dp->d_fileno);
2219 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2220 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2222 *tl = txdr_unsigned(*cookiep);
2223 nfsrv_postopattr(nd, 0, nvap);
2224 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2225 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2229 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2230 *tl++ = newnfs_true;
2232 *tl = txdr_unsigned(*cookiep);
2233 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2235 supports_nfsv4acls =
2236 nfs_supportsnfsv4acls(nvp);
2237 NFSVOPUNLOCK(nvp, 0);
2239 supports_nfsv4acls = 0;
2241 dirlen += nfsrv_putreferralattr(nd,
2244 if (nd->nd_repstat) {
2247 if (needs_unbusy != 0)
2252 dirlen += nfsvno_fillattr(nd, new_mp,
2253 nvp, nvap, &nfh, r, &rderrbits,
2254 nd->nd_cred, p, isdgram, 0,
2255 supports_nfsv4acls, at_root,
2258 dirlen += nfsvno_fillattr(nd, new_mp,
2259 nvp, nvap, &nfh, r, &attrbits,
2260 nd->nd_cred, p, isdgram, 0,
2261 supports_nfsv4acls, at_root,
2266 dirlen += (3 * NFSX_UNSIGNED);
2268 if (needs_unbusy != 0)
2273 cpos += dp->d_reclen;
2274 dp = (struct dirent *)cpos;
2282 * If dirlen > cnt, we must strip off the last entry. If that
2283 * results in an empty reply, report NFSERR_TOOSMALL.
2285 if (dirlen > cnt || nd->nd_repstat) {
2286 if (!nd->nd_repstat && entrycnt == 0)
2287 nd->nd_repstat = NFSERR_TOOSMALL;
2288 if (nd->nd_repstat) {
2289 newnfs_trimtrailing(nd, mb0, bpos0);
2290 if (nd->nd_flag & ND_NFSV3)
2291 nfsrv_postopattr(nd, getret, &at);
2293 newnfs_trimtrailing(nd, mb1, bpos1);
2295 } else if (cpos < cend)
2297 if (!nd->nd_repstat) {
2298 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2299 *tl++ = newnfs_false;
2305 FREE((caddr_t)cookies, M_TEMP);
2306 FREE((caddr_t)rbuf, M_TEMP);
2309 NFSEXITCODE2(0, nd);
2313 NFSEXITCODE2(error, nd);
2318 * Get the settable attributes out of the mbuf list.
2319 * (Return 0 or EBADRPC)
2322 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2323 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2326 struct nfsv2_sattr *sp;
2327 int error = 0, toclient = 0;
2329 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2331 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2333 * Some old clients didn't fill in the high order 16bits.
2334 * --> check the low order 2 bytes for 0xffff
2336 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2337 nvap->na_mode = nfstov_mode(sp->sa_mode);
2338 if (sp->sa_uid != newnfs_xdrneg1)
2339 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2340 if (sp->sa_gid != newnfs_xdrneg1)
2341 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2342 if (sp->sa_size != newnfs_xdrneg1)
2343 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2344 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2346 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2348 nvap->na_atime.tv_sec =
2349 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2350 nvap->na_atime.tv_nsec = 0;
2353 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2354 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2357 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2358 if (*tl == newnfs_true) {
2359 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2360 nvap->na_mode = nfstov_mode(*tl);
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_uid = fxdr_unsigned(uid_t, *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_gid = fxdr_unsigned(gid_t, *tl);
2372 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2373 if (*tl == newnfs_true) {
2374 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2375 nvap->na_size = fxdr_hyper(tl);
2377 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2378 switch (fxdr_unsigned(int, *tl)) {
2379 case NFSV3SATTRTIME_TOCLIENT:
2380 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2381 fxdr_nfsv3time(tl, &nvap->na_atime);
2384 case NFSV3SATTRTIME_TOSERVER:
2385 vfs_timestamp(&nvap->na_atime);
2386 nvap->na_vaflags |= VA_UTIMES_NULL;
2389 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2390 switch (fxdr_unsigned(int, *tl)) {
2391 case NFSV3SATTRTIME_TOCLIENT:
2392 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2393 fxdr_nfsv3time(tl, &nvap->na_mtime);
2394 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2396 case NFSV3SATTRTIME_TOSERVER:
2397 vfs_timestamp(&nvap->na_mtime);
2399 nvap->na_vaflags |= VA_UTIMES_NULL;
2404 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2407 NFSEXITCODE2(error, nd);
2412 * Handle the setable attributes for V4.
2413 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2416 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2417 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2422 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2424 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2428 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2431 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2432 attrsize = fxdr_unsigned(int, *tl);
2435 * Loop around getting the setable attributes. If an unsupported
2436 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2439 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2440 bitpos = NFSATTRBIT_MAX;
2444 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2445 if (attrsum > attrsize) {
2446 error = NFSERR_BADXDR;
2449 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2451 case NFSATTRBIT_SIZE:
2452 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2453 if (vp != NULL && vp->v_type != VREG) {
2454 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2458 nvap->na_size = fxdr_hyper(tl);
2459 attrsum += NFSX_HYPER;
2461 case NFSATTRBIT_ACL:
2462 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2466 if (aceerr && !nd->nd_repstat)
2467 nd->nd_repstat = aceerr;
2470 case NFSATTRBIT_ARCHIVE:
2471 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2472 if (!nd->nd_repstat)
2473 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2474 attrsum += NFSX_UNSIGNED;
2476 case NFSATTRBIT_HIDDEN:
2477 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2478 if (!nd->nd_repstat)
2479 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2480 attrsum += NFSX_UNSIGNED;
2482 case NFSATTRBIT_MIMETYPE:
2483 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2484 i = fxdr_unsigned(int, *tl);
2485 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2488 if (!nd->nd_repstat)
2489 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2490 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2492 case NFSATTRBIT_MODE:
2493 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2494 nvap->na_mode = nfstov_mode(*tl);
2495 attrsum += NFSX_UNSIGNED;
2497 case NFSATTRBIT_OWNER:
2498 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2499 j = fxdr_unsigned(int, *tl);
2501 error = NFSERR_BADXDR;
2504 if (j > NFSV4_SMALLSTR)
2505 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2508 error = nfsrv_mtostr(nd, cp, j);
2510 if (j > NFSV4_SMALLSTR)
2511 free(cp, M_NFSSTRING);
2514 if (!nd->nd_repstat) {
2515 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2517 if (!nd->nd_repstat)
2520 if (j > NFSV4_SMALLSTR)
2521 free(cp, M_NFSSTRING);
2522 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2524 case NFSATTRBIT_OWNERGROUP:
2525 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2526 j = fxdr_unsigned(int, *tl);
2528 error = NFSERR_BADXDR;
2531 if (j > NFSV4_SMALLSTR)
2532 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2535 error = nfsrv_mtostr(nd, cp, j);
2537 if (j > NFSV4_SMALLSTR)
2538 free(cp, M_NFSSTRING);
2541 if (!nd->nd_repstat) {
2542 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2544 if (!nd->nd_repstat)
2547 if (j > NFSV4_SMALLSTR)
2548 free(cp, M_NFSSTRING);
2549 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2551 case NFSATTRBIT_SYSTEM:
2552 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2553 if (!nd->nd_repstat)
2554 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2555 attrsum += NFSX_UNSIGNED;
2557 case NFSATTRBIT_TIMEACCESSSET:
2558 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2559 attrsum += NFSX_UNSIGNED;
2560 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2561 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2562 fxdr_nfsv4time(tl, &nvap->na_atime);
2564 attrsum += NFSX_V4TIME;
2566 vfs_timestamp(&nvap->na_atime);
2567 nvap->na_vaflags |= VA_UTIMES_NULL;
2570 case NFSATTRBIT_TIMEBACKUP:
2571 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2572 if (!nd->nd_repstat)
2573 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2574 attrsum += NFSX_V4TIME;
2576 case NFSATTRBIT_TIMECREATE:
2577 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2578 if (!nd->nd_repstat)
2579 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2580 attrsum += NFSX_V4TIME;
2582 case NFSATTRBIT_TIMEMODIFYSET:
2583 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2584 attrsum += NFSX_UNSIGNED;
2585 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2586 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2587 fxdr_nfsv4time(tl, &nvap->na_mtime);
2588 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2589 attrsum += NFSX_V4TIME;
2591 vfs_timestamp(&nvap->na_mtime);
2593 nvap->na_vaflags |= VA_UTIMES_NULL;
2597 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2599 * set bitpos so we drop out of the loop.
2601 bitpos = NFSATTRBIT_MAX;
2607 * some clients pad the attrlist, so we need to skip over the
2610 if (attrsum > attrsize) {
2611 error = NFSERR_BADXDR;
2613 attrsize = NFSM_RNDUP(attrsize);
2614 if (attrsum < attrsize)
2615 error = nfsm_advance(nd, attrsize - attrsum, -1);
2618 NFSEXITCODE2(error, nd);
2623 * Check/setup export credentials.
2626 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2627 struct ucred *credanon)
2632 * Check/setup credentials.
2634 if (nd->nd_flag & ND_GSS)
2635 exp->nes_exflag &= ~MNT_EXPORTANON;
2638 * Check to see if the operation is allowed for this security flavor.
2639 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2640 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2641 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2643 if (nfsvno_testexp(nd, exp) &&
2644 nd->nd_procnum != NFSV4OP_SECINFO &&
2645 nd->nd_procnum != NFSPROC_FSINFO) {
2646 if (nd->nd_flag & ND_NFSV4)
2647 error = NFSERR_WRONGSEC;
2649 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2654 * Check to see if the file system is exported V4 only.
2656 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2657 error = NFSERR_PROGNOTV4;
2662 * Now, map the user credentials.
2663 * (Note that ND_AUTHNONE will only be set for an NFSv3
2664 * Fsinfo RPC. If set for anything else, this code might need
2667 if (NFSVNO_EXPORTED(exp)) {
2668 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2669 NFSVNO_EXPORTANON(exp) ||
2670 (nd->nd_flag & ND_AUTHNONE) != 0) {
2671 nd->nd_cred->cr_uid = credanon->cr_uid;
2672 nd->nd_cred->cr_gid = credanon->cr_gid;
2673 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2674 credanon->cr_groups);
2675 } else if ((nd->nd_flag & ND_GSS) == 0) {
2677 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2678 * if there is a replacement credential with a group
2679 * list set up by "nfsuserd -manage-gids".
2680 * If there is no replacement, nfsrv_getgrpscred()
2681 * simply returns its argument.
2683 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2688 NFSEXITCODE2(error, nd);
2696 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2697 struct ucred **credp)
2699 int i, error, *secflavors;
2701 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2702 &exp->nes_numsecflavor, &secflavors);
2704 if (nfs_rootfhset) {
2705 exp->nes_exflag = 0;
2706 exp->nes_numsecflavor = 0;
2710 /* Copy the security flavors. */
2711 for (i = 0; i < exp->nes_numsecflavor; i++)
2712 exp->nes_secflavors[i] = secflavors[i];
2719 * Get a vnode for a file handle and export stuff.
2722 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2723 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2724 struct ucred **credp)
2726 int i, error, *secflavors;
2729 exp->nes_numsecflavor = 0;
2730 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2732 /* Make sure the server replies ESTALE to the client. */
2734 if (nam && !error) {
2735 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2736 &exp->nes_numsecflavor, &secflavors);
2738 if (nfs_rootfhset) {
2739 exp->nes_exflag = 0;
2740 exp->nes_numsecflavor = 0;
2746 /* Copy the security flavors. */
2747 for (i = 0; i < exp->nes_numsecflavor; i++)
2748 exp->nes_secflavors[i] = secflavors[i];
2756 * nfsd_fhtovp() - convert a fh to a vnode ptr
2757 * - look up fsid in mount list (if not found ret error)
2758 * - get vp and export rights by calling nfsvno_fhtovp()
2759 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2761 * - if mpp != NULL, return the mount point so that it can
2762 * be used for vn_finished_write() by the caller
2765 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2766 struct vnode **vpp, struct nfsexstuff *exp,
2767 struct mount **mpp, int startwrite, struct thread *p)
2770 struct ucred *credanon;
2773 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2775 * Check for the special case of the nfsv4root_fh.
2777 mp = vfs_busyfs(&fhp->fh_fsid);
2782 nd->nd_repstat = ESTALE;
2787 vn_start_write(NULL, mpp, V_WAIT);
2788 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2789 lktype = LK_EXCLUSIVE;
2791 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2796 * For NFSv4 without a pseudo root fs, unexported file handles
2797 * can be returned, so that Lookup works everywhere.
2799 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2800 !(nd->nd_flag & ND_NFSV4)) {
2802 nd->nd_repstat = EACCES;
2806 * Personally, I've never seen any point in requiring a
2807 * reserved port#, since only in the rare case where the
2808 * clients are all boxes with secure system privileges,
2809 * does it provide any enhanced security, but... some people
2810 * believe it to be useful and keep putting this code back in.
2811 * (There is also some "security checker" out there that
2812 * complains if the nfs server doesn't enforce this.)
2813 * However, note the following:
2814 * RFC3530 (NFSv4) specifies that a reserved port# not be
2816 * RFC2623 recommends that, if a reserved port# is checked for,
2817 * that there be a way to turn that off--> ifdef'd.
2819 #ifdef NFS_REQRSVPORT
2820 if (!nd->nd_repstat) {
2821 struct sockaddr_in *saddr;
2822 struct sockaddr_in6 *saddr6;
2824 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2825 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2826 if (!(nd->nd_flag & ND_NFSV4) &&
2827 ((saddr->sin_family == AF_INET &&
2828 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2829 (saddr6->sin6_family == AF_INET6 &&
2830 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2832 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2835 #endif /* NFS_REQRSVPORT */
2838 * Check/setup credentials.
2840 if (!nd->nd_repstat) {
2841 nd->nd_saveduid = nd->nd_cred->cr_uid;
2842 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2846 if (credanon != NULL)
2848 if (nd->nd_repstat) {
2850 vn_finished_write(mp);
2857 NFSEXITCODE2(0, nd);
2864 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2866 struct filedesc *fdp;
2870 fdp = p->td_proc->p_fd;
2871 if (fd < 0 || fd >= fdp->fd_nfiles ||
2872 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2884 * Called from nfssvc() to update the exports list. Just call
2885 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2886 * in the mount list.
2889 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2891 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2893 struct nameidata nd;
2896 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2897 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2899 else if (error == 0) {
2900 if (nfsexargp->fspec == NULL) {
2905 * If fspec != NULL, this is the v4root path.
2907 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2908 nfsexargp->fspec, p);
2909 if ((error = namei(&nd)) != 0)
2911 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2914 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2915 NFSBCOPY((caddr_t)&fh,
2916 nfs_rootfh.nfsrvfh_data,
2917 sizeof (fhandle_t));
2928 * This function needs to test to see if the system is near its limit
2929 * for memory allocation via malloc() or mget() and return True iff
2930 * either of these resources are near their limit.
2931 * XXX (For now, this is just a stub.)
2933 int nfsrv_testmalloclimit = 0;
2935 nfsrv_mallocmget_limit(void)
2937 static int printmesg = 0;
2938 static int testval = 1;
2940 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2941 if ((printmesg++ % 100) == 0)
2942 printf("nfsd: malloc/mget near limit\n");
2949 * BSD specific initialization of a mount point.
2954 static int inited = 0;
2959 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2960 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2961 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2962 nfsv4root_mnt.mnt_export = NULL;
2963 TAILQ_INIT(&nfsv4root_opt);
2964 TAILQ_INIT(&nfsv4root_newopt);
2965 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2966 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2967 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2968 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2972 * Get a vnode for a file handle, without checking exports, etc.
2975 nfsvno_getvp(fhandle_t *fhp)
2981 mp = vfs_busyfs(&fhp->fh_fsid);
2984 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2992 * Do a local VOP_ADVLOCK().
2995 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2996 u_int64_t end, struct thread *td)
3002 if (nfsrv_dolocallocks == 0)
3004 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3006 fl.l_whence = SEEK_SET;
3008 fl.l_start = (off_t)first;
3009 if (end == NFS64BITSSET) {
3013 fl.l_len = (off_t)tlen;
3016 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3017 * values for all calls, so that all locks will be held by the
3018 * nfsd server. (The nfsd server handles conflicts between the
3020 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3021 * bytes, so it can't be put in l_sysid.
3023 if (nfsv4_sysid == 0)
3024 nfsv4_sysid = nlm_acquire_next_sysid();
3025 fl.l_pid = (pid_t)0;
3026 fl.l_sysid = (int)nfsv4_sysid;
3028 if (ftype == F_UNLCK)
3029 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3030 (F_POSIX | F_REMOTE));
3032 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3033 (F_POSIX | F_REMOTE));
3041 * Check the nfsv4 root exports.
3044 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3046 struct ucred *credanon;
3047 int exflags, error = 0, numsecflavor, *secflavors, i;
3049 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3050 &credanon, &numsecflavor, &secflavors);
3052 error = NFSERR_PROGUNAVAIL;
3055 if (credanon != NULL)
3057 for (i = 0; i < numsecflavor; i++) {
3058 if (secflavors[i] == AUTH_SYS)
3059 nd->nd_flag |= ND_EXAUTHSYS;
3060 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3061 nd->nd_flag |= ND_EXGSS;
3062 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3063 nd->nd_flag |= ND_EXGSSINTEGRITY;
3064 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3065 nd->nd_flag |= ND_EXGSSPRIVACY;
3074 * Nfs server pseudo system call for the nfsd's
3080 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3083 struct nfsd_addsock_args sockarg;
3084 struct nfsd_nfsd_args nfsdarg;
3085 cap_rights_t rights;
3088 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3089 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3093 * Since we don't know what rights might be required,
3094 * pretend that we need them all. It is better to be too
3095 * careful than too reckless.
3097 error = fget(td, sockarg.sock,
3098 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3101 if (fp->f_type != DTYPE_SOCKET) {
3106 error = nfsrvd_addsock(fp);
3108 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3109 if (uap->argp == NULL) {
3113 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3117 error = nfsrvd_nfsd(td, &nfsdarg);
3119 error = nfssvc_srvcall(td, uap, td->td_ucred);
3128 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3130 struct nfsex_args export;
3131 struct file *fp = NULL;
3133 struct nfsd_clid adminrevoke;
3134 struct nfsd_dumplist dumplist;
3135 struct nfsd_dumpclients *dumpclients;
3136 struct nfsd_dumplocklist dumplocklist;
3137 struct nfsd_dumplocks *dumplocks;
3138 struct nameidata nd;
3140 int error = EINVAL, igotlock;
3142 static int suspend_nfsd = 0;
3144 if (uap->flag & NFSSVC_PUBLICFH) {
3145 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3146 sizeof (fhandle_t));
3147 error = copyin(uap->argp,
3148 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3151 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3152 error = copyin(uap->argp,(caddr_t)&export,
3153 sizeof (struct nfsex_args));
3155 error = nfsrv_v4rootexport(&export, cred, p);
3156 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3159 } else if (uap->flag & NFSSVC_STABLERESTART) {
3160 error = copyin(uap->argp, (caddr_t)&stablefd,
3163 error = fp_getfvp(p, stablefd, &fp, &vp);
3164 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3166 if (!error && newnfs_numnfsd != 0)
3169 nfsrv_stablefirst.nsf_fp = fp;
3170 nfsrv_setupstable(p);
3172 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3173 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3174 sizeof (struct nfsd_clid));
3176 error = nfsrv_adminrevoke(&adminrevoke, p);
3177 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3178 error = copyin(uap->argp, (caddr_t)&dumplist,
3179 sizeof (struct nfsd_dumplist));
3180 if (!error && (dumplist.ndl_size < 1 ||
3181 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3184 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3185 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3187 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3188 error = copyout(dumpclients,
3189 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3190 free((caddr_t)dumpclients, M_TEMP);
3192 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3193 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3194 sizeof (struct nfsd_dumplocklist));
3195 if (!error && (dumplocklist.ndllck_size < 1 ||
3196 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3199 error = nfsrv_lookupfilename(&nd,
3200 dumplocklist.ndllck_fname, p);
3202 len = sizeof (struct nfsd_dumplocks) *
3203 dumplocklist.ndllck_size;
3204 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3206 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3207 dumplocklist.ndllck_size, p);
3209 error = copyout(dumplocks,
3210 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3211 free((caddr_t)dumplocks, M_TEMP);
3213 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3216 nfsd_master_pid = procp->p_pid;
3217 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3218 nfsd_master_start = procp->p_stats->p_start;
3219 nfsd_master_proc = procp;
3221 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3222 NFSLOCKV4ROOTMUTEX();
3223 if (suspend_nfsd == 0) {
3224 /* Lock out all nfsd threads */
3226 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3227 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3228 } while (igotlock == 0 && suspend_nfsd == 0);
3231 NFSUNLOCKV4ROOTMUTEX();
3233 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3234 NFSLOCKV4ROOTMUTEX();
3235 if (suspend_nfsd != 0) {
3236 nfsv4_unlock(&nfsd_suspend_lock, 0);
3239 NFSUNLOCKV4ROOTMUTEX();
3249 * Returns 0 if ok, 1 otherwise.
3252 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3257 * This seems odd, but allow the case where the security flavor
3258 * list is empty. This happens when NFSv4 is traversing non-exported
3259 * file systems. Exported file systems should always have a non-empty
3260 * security flavor list.
3262 if (exp->nes_numsecflavor == 0)
3265 for (i = 0; i < exp->nes_numsecflavor; i++) {
3267 * The tests for privacy and integrity must be first,
3268 * since ND_GSS is set for everything but AUTH_SYS.
3270 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3271 (nd->nd_flag & ND_GSSPRIVACY))
3273 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3274 (nd->nd_flag & ND_GSSINTEGRITY))
3276 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3277 (nd->nd_flag & ND_GSS))
3279 if (exp->nes_secflavors[i] == AUTH_SYS &&
3280 (nd->nd_flag & ND_GSS) == 0)
3287 * Calculate a hash value for the fid in a file handle.
3290 nfsrv_hashfh(fhandle_t *fhp)
3294 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3299 * Calculate a hash value for the sessionid.
3302 nfsrv_hashsessionid(uint8_t *sessionid)
3306 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3311 * Signal the userland master nfsd to backup the stable restart file.
3314 nfsrv_backupstable(void)
3318 if (nfsd_master_proc != NULL) {
3319 procp = pfind(nfsd_master_pid);
3320 /* Try to make sure it is the correct process. */
3321 if (procp == nfsd_master_proc &&
3322 procp->p_stats->p_start.tv_sec ==
3323 nfsd_master_start.tv_sec &&
3324 procp->p_stats->p_start.tv_usec ==
3325 nfsd_master_start.tv_usec &&
3326 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3327 kern_psignal(procp, SIGUSR2);
3329 nfsd_master_proc = NULL;
3336 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3339 * Called once to initialize data structures...
3342 nfsd_modevent(module_t mod, int type, void *data)
3345 static int loaded = 0;
3352 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3353 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
3355 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
3358 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
3359 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
3360 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3361 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3368 #ifdef VV_DISABLEDELEG
3369 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3370 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3372 nfsd_call_servertimer = nfsrv_servertimer;
3373 nfsd_call_nfsd = nfssvc_nfsd;
3378 if (newnfs_numnfsd != 0) {
3383 #ifdef VV_DISABLEDELEG
3384 vn_deleg_ops.vndeleg_recall = NULL;
3385 vn_deleg_ops.vndeleg_disable = NULL;
3387 nfsd_call_servertimer = NULL;
3388 nfsd_call_nfsd = NULL;
3390 /* Clean out all NFSv4 state. */
3391 nfsrv_throwawayallstate(curthread);
3393 /* Clean the NFS server reply cache */
3394 nfsrvd_cleancache();
3396 /* Free up the krpc server pool. */
3397 if (nfsrvd_pool != NULL)
3398 svcpool_destroy(nfsrvd_pool);
3400 /* and get rid of the locks */
3401 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3402 mtx_destroy(&nfsrchash_table[i].mtx);
3403 mtx_destroy(&nfsrcahash_table[i].mtx);
3405 mtx_destroy(&nfsrc_udpmtx);
3406 mtx_destroy(&nfs_v4root_mutex);
3407 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3408 for (i = 0; i < nfsrv_sessionhashsize; i++)
3409 mtx_destroy(&nfssessionhash[i].mtx);
3410 lockdestroy(&nfsv4root_mnt.mnt_explock);
3411 free(nfsclienthash, M_NFSDCLIENT);
3412 free(nfslockhash, M_NFSDLOCKFILE);
3413 free(nfssessionhash, M_NFSDSESSION);
3425 static moduledata_t nfsd_mod = {
3430 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3432 /* So that loader and kldload(2) can find us, wherever we are.. */
3433 MODULE_VERSION(nfsd, 1);
3434 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3435 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3436 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3437 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3438 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);