2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/capability.h>
40 * Functions that perform the vfs operations required by the routines in
41 * nfsd_serv.c. It is hoped that this change will make the server more
45 #include <fs/nfs/nfsport.h>
47 #include <sys/sysctl.h>
48 #include <nlm/nlm_prot.h>
51 FEATURE(nfsd, "NFSv4 server");
53 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54 extern int nfsrv_useacl;
55 extern int newnfs_numnfsd;
56 extern struct mount nfsv4root_mnt;
57 extern struct nfsrv_stablefirst nfsrv_stablefirst;
58 extern void (*nfsd_call_servertimer)(void);
59 extern SVCPOOL *nfsrvd_pool;
60 extern struct nfsv4lock nfsd_suspend_lock;
61 extern struct nfssessionhash nfssessionhash[NFSSESSIONHASHSIZE];
62 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
64 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
65 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
66 struct mtx nfsrc_udpmtx;
67 struct mtx nfs_v4root_mutex;
68 struct nfsrvfh nfs_rootfh, nfs_pubfh;
69 int nfs_pubfhset = 0, nfs_rootfhset = 0;
70 struct proc *nfsd_master_proc = NULL;
71 int nfsd_debuglevel = 0;
72 static pid_t nfsd_master_pid = (pid_t)-1;
73 static char nfsd_master_comm[MAXCOMLEN + 1];
74 static struct timeval nfsd_master_start;
75 static uint32_t nfsv4_sysid = 0;
77 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
80 int nfsrv_enable_crossmntpt = 1;
81 static int nfs_commit_blks;
82 static int nfs_commit_miss;
83 extern int nfsrv_issuedelegs;
84 extern int nfsrv_dolocallocks;
85 extern int nfsd_enable_stringtouid;
87 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
88 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
89 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
92 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
95 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
97 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
98 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
99 0, "Debug level for new nfs server");
100 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
101 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
103 #define MAX_REORDERED_RPC 16
104 #define NUM_HEURISTIC 1031
105 #define NHUSE_INIT 64
107 #define NHUSE_MAX 2048
109 static struct nfsheur {
110 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
111 off_t nh_nextoff; /* next offset for sequential detection */
112 int nh_use; /* use count for selection */
113 int nh_seqcount; /* heuristic */
114 } nfsheur[NUM_HEURISTIC];
118 * Heuristic to detect sequential operation.
120 static struct nfsheur *
121 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
126 /* Locate best candidate. */
128 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
131 if (nfsheur[hi].nh_vp == vp) {
135 if (nfsheur[hi].nh_use > 0)
136 --nfsheur[hi].nh_use;
137 hi = (hi + 1) % NUM_HEURISTIC;
138 if (nfsheur[hi].nh_use < nh->nh_use)
142 /* Initialize hint if this is a new file. */
143 if (nh->nh_vp != vp) {
145 nh->nh_nextoff = uio->uio_offset;
146 nh->nh_use = NHUSE_INIT;
147 if (uio->uio_offset == 0)
153 /* Calculate heuristic. */
154 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
155 uio->uio_offset == nh->nh_nextoff) {
156 /* See comments in vfs_vnops.c:sequential_heuristic(). */
157 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
158 if (nh->nh_seqcount > IO_SEQMAX)
159 nh->nh_seqcount = IO_SEQMAX;
160 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
161 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
162 /* Probably a reordered RPC, leave seqcount alone. */
163 } else if (nh->nh_seqcount > 1) {
164 nh->nh_seqcount /= 2;
168 nh->nh_use += NHUSE_INC;
169 if (nh->nh_use > NHUSE_MAX)
170 nh->nh_use = NHUSE_MAX;
175 * Get attributes into nfsvattr structure.
178 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
179 struct thread *p, int vpislocked)
181 int error, lockedit = 0;
183 if (vpislocked == 0) {
185 * When vpislocked == 0, the vnode is either exclusively
186 * locked by this thread or not locked by this thread.
187 * As such, shared lock it, if not exclusively locked.
189 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
191 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
194 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
203 * Get a file handle for a vnode.
206 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
210 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
211 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
212 error = VOP_VPTOFH(vp, &fhp->fh_fid);
219 * Perform access checking for vnodes obtained from file handles that would
220 * refer to files already opened by a Unix client. You cannot just use
221 * vn_writechk() and VOP_ACCESSX() for two reasons.
222 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
224 * 2 - The owner is to be given access irrespective of mode bits for some
225 * operations, so that processes that chmod after opening a file don't
229 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
230 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
231 u_int32_t *supportedtypep)
234 int error = 0, getret = 0;
236 if (vpislocked == 0) {
237 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
242 if (accmode & VWRITE) {
243 /* Just vn_writechk() changed to check rdonly */
245 * Disallow write attempts on read-only file systems;
246 * unless the file is a socket or a block or character
247 * device resident on the file system.
249 if (NFSVNO_EXRDONLY(exp) ||
250 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
251 switch (vp->v_type) {
261 * If there's shared text associated with
262 * the inode, try to free it up once. If
263 * we fail, we can't allow writing.
265 if (VOP_IS_TEXT(vp) && error == 0)
275 * Should the override still be applied when ACLs are enabled?
277 error = VOP_ACCESSX(vp, accmode, cred, p);
278 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
280 * Try again with VEXPLICIT_DENY, to see if the test for
281 * deletion is supported.
283 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
285 if (vp->v_type == VDIR) {
286 accmode &= ~(VDELETE | VDELETE_CHILD);
288 error = VOP_ACCESSX(vp, accmode, cred, p);
289 } else if (supportedtypep != NULL) {
290 *supportedtypep &= ~NFSACCESS_DELETE;
296 * Allow certain operations for the owner (reads and writes
297 * on files that are already open).
299 if (override != NFSACCCHK_NOOVERRIDE &&
300 (error == EPERM || error == EACCES)) {
301 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
303 else if (override & NFSACCCHK_ALLOWOWNER) {
304 getret = VOP_GETATTR(vp, &vattr, cred);
305 if (getret == 0 && cred->cr_uid == vattr.va_uid)
318 * Set attribute(s) vnop.
321 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
322 struct thread *p, struct nfsexstuff *exp)
326 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
332 * Set up nameidata for a lookup() call and do it.
335 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
336 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
337 struct vnode **retdirp)
339 struct componentname *cnp = &ndp->ni_cnd;
343 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
344 int error = 0, crossmnt;
348 cnp->cn_nameptr = cnp->cn_pnbuf;
349 ndp->ni_strictrelative = 0;
351 * Extract and set starting directory.
353 if (dp->v_type != VDIR) {
358 nfsvno_relpathbuf(ndp);
366 if (NFSVNO_EXRDONLY(exp))
367 cnp->cn_flags |= RDONLY;
368 ndp->ni_segflg = UIO_SYSSPACE;
371 if (nd->nd_flag & ND_PUBLOOKUP) {
373 if (cnp->cn_pnbuf[0] == '/') {
376 * Check for degenerate pathnames here, since lookup()
379 for (i = 1; i < ndp->ni_pathlen; i++)
380 if (cnp->cn_pnbuf[i] != '/')
382 if (i == ndp->ni_pathlen) {
383 error = NFSERR_ACCES;
389 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
390 (nd->nd_flag & ND_NFSV4) == 0) {
392 * Only cross mount points for NFSv4 when doing a
393 * mount while traversing the file system above
394 * the mount point, unless nfsrv_enable_crossmntpt is set.
396 cnp->cn_flags |= NOCROSSMOUNT;
401 * Initialize for scan, set ni_startdir and bump ref on dp again
402 * because lookup() will dereference ni_startdir.
406 ndp->ni_startdir = dp;
407 ndp->ni_rootdir = rootvnode;
408 ndp->ni_topdir = NULL;
411 cnp->cn_flags |= LOCKLEAF;
413 cnp->cn_nameptr = cnp->cn_pnbuf;
415 * Call lookup() to do the real work. If an error occurs,
416 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
417 * we do not have to dereference anything before returning.
418 * In either case ni_startdir will be dereferenced and NULLed
426 * Check for encountering a symbolic link. Trivial
427 * termination occurs if no symlink encountered.
429 if ((cnp->cn_flags & ISSYMLINK) == 0) {
430 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
431 nfsvno_relpathbuf(ndp);
432 if (ndp->ni_vp && !lockleaf)
433 NFSVOPUNLOCK(ndp->ni_vp, 0);
440 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
441 NFSVOPUNLOCK(ndp->ni_dvp, 0);
442 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
447 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
451 if (ndp->ni_pathlen > 1)
452 cp = uma_zalloc(namei_zone, M_WAITOK);
456 aiov.iov_len = MAXPATHLEN;
457 auio.uio_iov = &aiov;
460 auio.uio_rw = UIO_READ;
461 auio.uio_segflg = UIO_SYSSPACE;
463 auio.uio_resid = MAXPATHLEN;
464 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
467 if (ndp->ni_pathlen > 1)
468 uma_zfree(namei_zone, cp);
474 linklen = MAXPATHLEN - auio.uio_resid;
479 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
480 error = ENAMETOOLONG;
485 * Adjust or replace path
487 if (ndp->ni_pathlen > 1) {
488 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
489 uma_zfree(namei_zone, cnp->cn_pnbuf);
492 cnp->cn_pnbuf[linklen] = '\0';
493 ndp->ni_pathlen += linklen;
496 * Cleanup refs for next loop and check if root directory
497 * should replace current directory. Normally ni_dvp
498 * becomes the new base directory and is cleaned up when
499 * we loop. Explicitly null pointers after invalidation
500 * to clarify operation.
505 if (cnp->cn_pnbuf[0] == '/') {
507 ndp->ni_dvp = ndp->ni_rootdir;
510 ndp->ni_startdir = ndp->ni_dvp;
514 cnp->cn_flags &= ~LOCKLEAF;
518 nfsvno_relpathbuf(ndp);
521 ndp->ni_startdir = NULL;
522 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
527 NFSEXITCODE2(error, nd);
532 * Set up a pathname buffer and return a pointer to it and, optionally
533 * set a hash pointer.
536 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
538 struct componentname *cnp = &ndp->ni_cnd;
540 cnp->cn_flags |= (NOMACCHECK | HASBUF);
541 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
544 *bufpp = cnp->cn_pnbuf;
548 * Release the above path buffer, if not released by nfsvno_namei().
551 nfsvno_relpathbuf(struct nameidata *ndp)
554 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
556 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
557 ndp->ni_cnd.cn_flags &= ~HASBUF;
561 * Readlink vnode op into an mbuf list.
564 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
565 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
567 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
568 struct iovec *ivp = iv;
569 struct uio io, *uiop = &io;
570 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
571 int i, len, tlen, error = 0;
575 while (len < NFS_MAXPATHLEN) {
577 MCLGET(mp, M_WAITOK);
578 mp->m_len = NFSMSIZ(mp);
585 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
586 mp->m_len = NFS_MAXPATHLEN - len;
587 len = NFS_MAXPATHLEN;
591 ivp->iov_base = mtod(mp, caddr_t);
592 ivp->iov_len = mp->m_len;
597 uiop->uio_iovcnt = i;
598 uiop->uio_offset = 0;
599 uiop->uio_resid = len;
600 uiop->uio_rw = UIO_READ;
601 uiop->uio_segflg = UIO_SYSSPACE;
603 error = VOP_READLINK(vp, uiop, cred);
609 if (uiop->uio_resid > 0) {
610 len -= uiop->uio_resid;
611 tlen = NFSM_RNDUP(len);
612 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
624 * Read vnode op call into mbuf list.
627 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
628 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
634 int error = 0, len, left, siz, tlen, ioflag = 0;
635 struct mbuf *m2 = NULL, *m3;
636 struct uio io, *uiop = &io;
639 len = left = NFSM_RNDUP(cnt);
642 * Generate the mbuf list with the uio_iov ref. to it.
649 siz = min(M_TRAILINGSPACE(m), left);
658 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
660 uiop->uio_iov = iv2 = iv;
666 panic("nfsvno_read iov");
667 siz = min(M_TRAILINGSPACE(m), left);
669 iv->iov_base = mtod(m, caddr_t) + m->m_len;
678 uiop->uio_iovcnt = i;
679 uiop->uio_offset = off;
680 uiop->uio_resid = len;
681 uiop->uio_rw = UIO_READ;
682 uiop->uio_segflg = UIO_SYSSPACE;
684 nh = nfsrv_sequential_heuristic(uiop, vp);
685 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
686 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
687 FREE((caddr_t)iv2, M_TEMP);
693 nh->nh_nextoff = uiop->uio_offset;
694 tlen = len - uiop->uio_resid;
695 cnt = cnt < tlen ? cnt : tlen;
696 tlen = NFSM_RNDUP(cnt);
700 } else if (len != tlen || tlen != cnt)
701 nfsrv_adj(m3, len - tlen, tlen - cnt);
711 * Write vnode op from an mbuf list.
714 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
715 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
721 struct uio io, *uiop = &io;
724 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
726 uiop->uio_iov = iv = ivp;
727 uiop->uio_iovcnt = cnt;
728 i = mtod(mp, caddr_t) + mp->m_len - cp;
732 panic("nfsvno_write");
743 cp = mtod(mp, caddr_t);
747 if (stable == NFSWRITE_UNSTABLE)
748 ioflags = IO_NODELOCKED;
750 ioflags = (IO_SYNC | IO_NODELOCKED);
751 uiop->uio_resid = retlen;
752 uiop->uio_rw = UIO_WRITE;
753 uiop->uio_segflg = UIO_SYSSPACE;
755 uiop->uio_offset = off;
756 nh = nfsrv_sequential_heuristic(uiop, vp);
757 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
758 error = VOP_WRITE(vp, uiop, ioflags, cred);
760 nh->nh_nextoff = uiop->uio_offset;
761 FREE((caddr_t)iv, M_TEMP);
768 * Common code for creating a regular file (plus special files for V2).
771 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
772 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
773 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
778 error = nd->nd_repstat;
779 if (!error && ndp->ni_vp == NULL) {
780 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
781 vrele(ndp->ni_startdir);
782 error = VOP_CREATE(ndp->ni_dvp,
783 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
785 nfsvno_relpathbuf(ndp);
787 if (*exclusive_flagp) {
788 *exclusive_flagp = 0;
789 NFSVNO_ATTRINIT(nvap);
790 nvap->na_atime.tv_sec = cverf[0];
791 nvap->na_atime.tv_nsec = cverf[1];
792 error = VOP_SETATTR(ndp->ni_vp,
793 &nvap->na_vattr, nd->nd_cred);
797 * NFS V2 Only. nfsrvd_mknod() does this for V3.
798 * (This implies, just get out on an error.)
800 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
801 nvap->na_type == VFIFO) {
802 if (nvap->na_type == VCHR && rdev == 0xffffffff)
803 nvap->na_type = VFIFO;
804 if (nvap->na_type != VFIFO &&
805 (error = priv_check_cred(nd->nd_cred,
806 PRIV_VFS_MKNOD_DEV, 0))) {
807 vrele(ndp->ni_startdir);
808 nfsvno_relpathbuf(ndp);
812 nvap->na_rdev = rdev;
813 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
814 &ndp->ni_cnd, &nvap->na_vattr);
816 nfsvno_relpathbuf(ndp);
817 vrele(ndp->ni_startdir);
821 vrele(ndp->ni_startdir);
822 nfsvno_relpathbuf(ndp);
830 * Handle cases where error is already set and/or
832 * 1 - clean up the lookup
833 * 2 - iff !error and na_size set, truncate it
835 vrele(ndp->ni_startdir);
836 nfsvno_relpathbuf(ndp);
838 if (ndp->ni_dvp == *vpp)
842 if (!error && nvap->na_size != VNOVAL) {
843 error = nfsvno_accchk(*vpp, VWRITE,
844 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
845 NFSACCCHK_VPISLOCKED, NULL);
847 tempsize = nvap->na_size;
848 NFSVNO_ATTRINIT(nvap);
849 nvap->na_size = tempsize;
850 error = VOP_SETATTR(*vpp,
851 &nvap->na_vattr, nd->nd_cred);
864 * Do a mknod vnode op.
867 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
873 vtyp = nvap->na_type;
875 * Iff doesn't exist, create it.
878 vrele(ndp->ni_startdir);
879 nfsvno_relpathbuf(ndp);
885 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
886 vrele(ndp->ni_startdir);
887 nfsvno_relpathbuf(ndp);
889 error = NFSERR_BADTYPE;
893 vrele(ndp->ni_startdir);
894 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
895 &ndp->ni_cnd, &nvap->na_vattr);
897 nfsvno_relpathbuf(ndp);
899 if (nvap->na_type != VFIFO &&
900 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
901 vrele(ndp->ni_startdir);
902 nfsvno_relpathbuf(ndp);
906 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
907 &ndp->ni_cnd, &nvap->na_vattr);
909 nfsvno_relpathbuf(ndp);
910 vrele(ndp->ni_startdir);
912 * Since VOP_MKNOD returns the ni_vp, I can't
913 * see any reason to do the lookup.
926 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
927 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
931 if (ndp->ni_vp != NULL) {
932 if (ndp->ni_dvp == ndp->ni_vp)
937 nfsvno_relpathbuf(ndp);
941 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
944 nfsvno_relpathbuf(ndp);
955 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
956 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
957 struct nfsexstuff *exp)
962 vrele(ndp->ni_startdir);
963 nfsvno_relpathbuf(ndp);
964 if (ndp->ni_dvp == ndp->ni_vp)
973 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
974 &nvap->na_vattr, pathcp);
976 vrele(ndp->ni_startdir);
977 nfsvno_relpathbuf(ndp);
979 * Although FreeBSD still had the lookup code in
980 * it for 7/current, there doesn't seem to be any
981 * point, since VOP_SYMLINK() returns the ni_vp.
982 * Just vput it for v2.
984 if (!not_v2 && !error)
993 * Parse symbolic link arguments.
994 * This function has an ugly side effect. It will MALLOC() an area for
995 * the symlink and set iov_base to point to it, only if it succeeds.
996 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1000 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1001 struct thread *p, char **pathcpp, int *lenp)
1004 char *pathcp = NULL;
1006 struct nfsv2_sattr *sp;
1010 if ((nd->nd_flag & ND_NFSV3) &&
1011 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1013 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1014 len = fxdr_unsigned(int, *tl);
1015 if (len > NFS_MAXPATHLEN || len <= 0) {
1019 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1020 error = nfsrv_mtostr(nd, pathcp, len);
1023 if (nd->nd_flag & ND_NFSV2) {
1024 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1025 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1029 NFSEXITCODE2(0, nd);
1033 free(pathcp, M_TEMP);
1034 NFSEXITCODE2(error, nd);
1039 * Remove a non-directory object.
1042 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1043 struct thread *p, struct nfsexstuff *exp)
1049 if (vp->v_type == VDIR)
1050 error = NFSERR_ISDIR;
1052 error = nfsrv_checkremove(vp, 1, p);
1054 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1055 if (ndp->ni_dvp == vp)
1060 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1061 nfsvno_relpathbuf(ndp);
1067 * Remove a directory.
1070 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1071 struct thread *p, struct nfsexstuff *exp)
1077 if (vp->v_type != VDIR) {
1082 * No rmdir "." please.
1084 if (ndp->ni_dvp == vp) {
1089 * The root of a mounted filesystem cannot be deleted.
1091 if (vp->v_vflag & VV_ROOT)
1095 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1096 if (ndp->ni_dvp == vp)
1101 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1102 nfsvno_relpathbuf(ndp);
1111 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1112 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1114 struct vnode *fvp, *tvp, *tdvp;
1117 fvp = fromndp->ni_vp;
1119 vrele(fromndp->ni_dvp);
1124 tdvp = tondp->ni_dvp;
1127 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1128 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1130 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1131 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1134 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1135 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1140 * A rename to '.' or '..' results in a prematurely
1141 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1144 if ((tondp->ni_cnd.cn_namelen == 1 &&
1145 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1146 (tondp->ni_cnd.cn_namelen == 2 &&
1147 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1148 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1153 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1154 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1157 if (fvp->v_mount != tdvp->v_mount) {
1158 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1162 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1167 * If source and destination are the same, there is nothing to
1168 * do. Set error to -1 to indicate this.
1173 if (ndflag & ND_NFSV4) {
1174 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1175 error = nfsrv_checkremove(fvp, 0, p);
1176 NFSVOPUNLOCK(fvp, 0);
1180 error = nfsrv_checkremove(tvp, 1, p);
1183 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1184 * that the NFSv4 client won't be confused by the rename.
1185 * Since nfsd_recalldelegation() can only be called on an
1186 * unlocked vnode at this point and fvp is the file that will
1187 * still exist after the rename, just do fvp.
1189 nfsd_recalldelegation(fvp, p);
1193 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1194 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1203 vrele(fromndp->ni_dvp);
1208 vrele(tondp->ni_startdir);
1209 nfsvno_relpathbuf(tondp);
1211 vrele(fromndp->ni_startdir);
1212 nfsvno_relpathbuf(fromndp);
1221 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1222 struct thread *p, struct nfsexstuff *exp)
1232 if (vp->v_mount != xp->v_mount)
1236 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1237 if ((vp->v_iflag & VI_DOOMED) == 0)
1238 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1241 if (ndp->ni_dvp == vp)
1245 NFSVOPUNLOCK(vp, 0);
1247 if (ndp->ni_dvp == ndp->ni_vp)
1254 nfsvno_relpathbuf(ndp);
1260 * Do the fsync() appropriate for the commit.
1263 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1269 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1270 * file is done. At this time VOP_FSYNC does not accept offset and
1271 * byte count parameters so call VOP_FSYNC the whole file for now.
1272 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1274 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
1276 * Give up and do the whole thing
1279 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1280 VM_OBJECT_WLOCK(vp->v_object);
1281 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1282 VM_OBJECT_WUNLOCK(vp->v_object);
1284 error = VOP_FSYNC(vp, MNT_WAIT, td);
1287 * Locate and synchronously write any buffers that fall
1288 * into the requested range. Note: we are assuming that
1289 * f_iosize is a power of 2.
1291 int iosize = vp->v_mount->mnt_stat.f_iosize;
1292 int iomask = iosize - 1;
1297 * Align to iosize boundry, super-align to page boundry.
1300 cnt += off & iomask;
1301 off &= ~(u_quad_t)iomask;
1303 if (off & PAGE_MASK) {
1304 cnt += off & PAGE_MASK;
1305 off &= ~(u_quad_t)PAGE_MASK;
1307 lblkno = off / iosize;
1310 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1311 VM_OBJECT_WLOCK(vp->v_object);
1312 vm_object_page_clean(vp->v_object, off, off + cnt,
1314 VM_OBJECT_WUNLOCK(vp->v_object);
1323 * If we have a buffer and it is marked B_DELWRI we
1324 * have to lock and write it. Otherwise the prior
1325 * write is assumed to have already been committed.
1327 * gbincore() can return invalid buffers now so we
1328 * have to check that bit as well (though B_DELWRI
1329 * should not be set if B_INVAL is set there could be
1330 * a race here since we haven't locked the buffer).
1332 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1333 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1334 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1336 continue; /* retry */
1338 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1341 bp->b_flags &= ~B_ASYNC;
1364 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1368 error = VFS_STATFS(vp->v_mount, sf);
1371 * Since NFS handles these values as unsigned on the
1372 * wire, there is no way to represent negative values,
1373 * so set them to 0. Without this, they will appear
1374 * to be very large positive values for clients like
1377 if (sf->f_bavail < 0)
1379 if (sf->f_ffree < 0)
1387 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1388 * must handle nfsrv_opencheck() calls after any other access checks.
1391 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1392 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1393 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1394 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1395 struct nfsexstuff *exp, struct vnode **vpp)
1397 struct vnode *vp = NULL;
1399 struct nfsexstuff nes;
1401 if (ndp->ni_vp == NULL)
1402 nd->nd_repstat = nfsrv_opencheck(clientid,
1403 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1404 if (!nd->nd_repstat) {
1405 if (ndp->ni_vp == NULL) {
1406 vrele(ndp->ni_startdir);
1407 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1408 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1410 nfsvno_relpathbuf(ndp);
1411 if (!nd->nd_repstat) {
1412 if (*exclusive_flagp) {
1413 *exclusive_flagp = 0;
1414 NFSVNO_ATTRINIT(nvap);
1415 nvap->na_atime.tv_sec = cverf[0];
1416 nvap->na_atime.tv_nsec = cverf[1];
1417 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1418 &nvap->na_vattr, cred);
1420 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1421 aclp, p, attrbitp, exp);
1426 if (ndp->ni_startdir)
1427 vrele(ndp->ni_startdir);
1428 nfsvno_relpathbuf(ndp);
1430 if (create == NFSV4OPEN_CREATE) {
1431 if (ndp->ni_dvp == vp)
1436 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1437 if (ndp->ni_cnd.cn_flags & RDONLY)
1438 NFSVNO_SETEXRDONLY(&nes);
1440 NFSVNO_EXINIT(&nes);
1441 nd->nd_repstat = nfsvno_accchk(vp,
1442 VWRITE, cred, &nes, p,
1443 NFSACCCHK_NOOVERRIDE,
1444 NFSACCCHK_VPISLOCKED, NULL);
1445 nd->nd_repstat = nfsrv_opencheck(clientid,
1446 stateidp, stp, vp, nd, p, nd->nd_repstat);
1447 if (!nd->nd_repstat) {
1448 tempsize = nvap->na_size;
1449 NFSVNO_ATTRINIT(nvap);
1450 nvap->na_size = tempsize;
1451 nd->nd_repstat = VOP_SETATTR(vp,
1452 &nvap->na_vattr, cred);
1454 } else if (vp->v_type == VREG) {
1455 nd->nd_repstat = nfsrv_opencheck(clientid,
1456 stateidp, stp, vp, nd, p, nd->nd_repstat);
1460 if (ndp->ni_cnd.cn_flags & HASBUF)
1461 nfsvno_relpathbuf(ndp);
1462 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1463 vrele(ndp->ni_startdir);
1464 if (ndp->ni_dvp == ndp->ni_vp)
1474 NFSEXITCODE2(0, nd);
1478 * Updates the file rev and sets the mtime and ctime
1479 * to the current clock time, returning the va_filerev and va_Xtime
1481 * Return ESTALE to indicate the vnode is VI_DOOMED.
1484 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1485 struct ucred *cred, struct thread *p)
1490 vfs_timestamp(&va.va_mtime);
1491 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1492 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1493 if ((vp->v_iflag & VI_DOOMED) != 0)
1496 (void) VOP_SETATTR(vp, &va, cred);
1497 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1502 * Glue routine to nfsv4_fillattr().
1505 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1506 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1507 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1508 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1512 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1513 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1515 NFSEXITCODE2(0, nd);
1519 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1521 * nfs readdir service
1522 * - mallocs what it thinks is enough to read
1523 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1524 * - calls VOP_READDIR()
1525 * - loops around building the reply
1526 * if the output generated exceeds count break out of loop
1527 * The NFSM_CLGET macro is used here so that the reply will be packed
1528 * tightly in mbuf clusters.
1529 * - it trims out records with d_fileno == 0
1530 * this doesn't matter for Unix clients, but they might confuse clients
1532 * - it trims out records with d_type == DT_WHT
1533 * these cannot be seen through NFS (unless we extend the protocol)
1534 * The alternate call nfsrvd_readdirplus() does lookups as well.
1535 * PS: The NFS protocol spec. does not clarify what the "count" byte
1536 * argument is a count of.. just name strings and file id's or the
1537 * entire reply rpc or ...
1538 * I tried just file name and id sizes and it confused the Sun client,
1539 * so I am using the full rpc size now. The "paranoia.." comment refers
1540 * to including the status longwords that are not a part of the dir.
1541 * "entry" structures, but are in the rpc.
1544 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1545 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1550 char *cpos, *cend, *rbuf;
1552 int nlen, error = 0, getret = 1;
1553 int siz, cnt, fullsiz, eofflag, ncookies;
1554 u_int64_t off, toff, verf;
1555 u_long *cookies = NULL, *cookiep;
1560 if (nd->nd_repstat) {
1561 nfsrv_postopattr(nd, getret, &at);
1564 if (nd->nd_flag & ND_NFSV2) {
1565 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1566 off = fxdr_unsigned(u_quad_t, *tl++);
1568 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1569 off = fxdr_hyper(tl);
1571 verf = fxdr_hyper(tl);
1575 cnt = fxdr_unsigned(int, *tl);
1576 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1577 cnt = NFS_SRVMAXDATA(nd);
1578 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1580 if (nd->nd_flag & ND_NFSV3) {
1581 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1585 * va_filerev is not sufficient as a cookie verifier,
1586 * since it is not supposed to change when entries are
1587 * removed/added unless that offset cookies returned to
1588 * the client are no longer valid.
1590 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1591 nd->nd_repstat = NFSERR_BAD_COOKIE;
1594 if (!nd->nd_repstat && vp->v_type != VDIR)
1595 nd->nd_repstat = NFSERR_NOTDIR;
1596 if (nd->nd_repstat == 0 && cnt == 0) {
1597 if (nd->nd_flag & ND_NFSV2)
1598 /* NFSv2 does not have NFSERR_TOOSMALL */
1599 nd->nd_repstat = EPERM;
1601 nd->nd_repstat = NFSERR_TOOSMALL;
1603 if (!nd->nd_repstat)
1604 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1605 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1606 NFSACCCHK_VPISLOCKED, NULL);
1607 if (nd->nd_repstat) {
1609 if (nd->nd_flag & ND_NFSV3)
1610 nfsrv_postopattr(nd, getret, &at);
1613 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1614 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1618 free((caddr_t)cookies, M_TEMP);
1626 io.uio_offset = (off_t)off;
1628 io.uio_segflg = UIO_SYSSPACE;
1629 io.uio_rw = UIO_READ;
1631 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1633 off = (u_int64_t)io.uio_offset;
1635 siz -= io.uio_resid;
1637 if (!cookies && !nd->nd_repstat)
1638 nd->nd_repstat = NFSERR_PERM;
1639 if (nd->nd_flag & ND_NFSV3) {
1640 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1641 if (!nd->nd_repstat)
1642 nd->nd_repstat = getret;
1646 * Handles the failed cases. nd->nd_repstat == 0 past here.
1648 if (nd->nd_repstat) {
1650 free((caddr_t)rbuf, M_TEMP);
1652 free((caddr_t)cookies, M_TEMP);
1653 if (nd->nd_flag & ND_NFSV3)
1654 nfsrv_postopattr(nd, getret, &at);
1658 * If nothing read, return eof
1663 if (nd->nd_flag & ND_NFSV2) {
1664 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1666 nfsrv_postopattr(nd, getret, &at);
1667 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1668 txdr_hyper(at.na_filerev, tl);
1671 *tl++ = newnfs_false;
1673 FREE((caddr_t)rbuf, M_TEMP);
1674 FREE((caddr_t)cookies, M_TEMP);
1679 * Check for degenerate cases of nothing useful read.
1680 * If so go try again
1684 dp = (struct dirent *)cpos;
1688 * For some reason FreeBSD's ufs_readdir() chooses to back the
1689 * directory offset up to a block boundary, so it is necessary to
1690 * skip over the records that precede the requested offset. This
1691 * requires the assumption that file offset cookies monotonically
1694 while (cpos < cend && ncookies > 0 &&
1695 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1696 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1697 cpos += dp->d_reclen;
1698 dp = (struct dirent *)cpos;
1702 if (cpos >= cend || ncookies == 0) {
1710 * dirlen is the size of the reply, including all XDR and must
1711 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1712 * if the XDR should be included in "count", but to be safe, we do.
1713 * (Include the two booleans at the end of the reply in dirlen now.)
1715 if (nd->nd_flag & ND_NFSV3) {
1716 nfsrv_postopattr(nd, getret, &at);
1717 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1718 txdr_hyper(at.na_filerev, tl);
1719 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1721 dirlen = 2 * NFSX_UNSIGNED;
1724 /* Loop through the records and build reply */
1725 while (cpos < cend && ncookies > 0) {
1726 nlen = dp->d_namlen;
1727 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1728 nlen <= NFS_MAXNAMLEN) {
1729 if (nd->nd_flag & ND_NFSV3)
1730 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1732 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1739 * Build the directory record xdr from
1742 if (nd->nd_flag & ND_NFSV3) {
1743 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1744 *tl++ = newnfs_true;
1747 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1748 *tl++ = newnfs_true;
1750 *tl = txdr_unsigned(dp->d_fileno);
1751 (void) nfsm_strtom(nd, dp->d_name, nlen);
1752 if (nd->nd_flag & ND_NFSV3) {
1753 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1756 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1757 *tl = txdr_unsigned(*cookiep);
1759 cpos += dp->d_reclen;
1760 dp = (struct dirent *)cpos;
1766 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1767 *tl++ = newnfs_false;
1772 FREE((caddr_t)rbuf, M_TEMP);
1773 FREE((caddr_t)cookies, M_TEMP);
1776 NFSEXITCODE2(0, nd);
1780 NFSEXITCODE2(error, nd);
1785 * Readdirplus for V3 and Readdir for V4.
1788 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1789 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1794 char *cpos, *cend, *rbuf;
1797 struct nfsvattr nva, at, *nvap = &nva;
1798 struct mbuf *mb0, *mb1;
1799 struct nfsreferral *refp;
1800 int nlen, r, error = 0, getret = 1, usevget = 1;
1801 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1802 caddr_t bpos0, bpos1;
1803 u_int64_t off, toff, verf;
1804 u_long *cookies = NULL, *cookiep;
1805 nfsattrbit_t attrbits, rderrbits, savbits;
1808 struct componentname cn;
1809 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
1810 struct mount *mp, *new_mp;
1811 uint64_t mounted_on_fileno;
1813 if (nd->nd_repstat) {
1814 nfsrv_postopattr(nd, getret, &at);
1817 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1818 off = fxdr_hyper(tl);
1821 verf = fxdr_hyper(tl);
1823 siz = fxdr_unsigned(int, *tl++);
1824 cnt = fxdr_unsigned(int, *tl);
1827 * Use the server's maximum data transfer size as the upper bound
1830 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1831 cnt = NFS_SRVMAXDATA(nd);
1834 * siz is a "hint" of how much directory information (name, fileid,
1835 * cookie) should be in the reply. At least one client "hints" 0,
1836 * so I set it to cnt for that case. I also round it up to the
1837 * next multiple of DIRBLKSIZ.
1841 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1843 if (nd->nd_flag & ND_NFSV4) {
1844 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1847 NFSSET_ATTRBIT(&savbits, &attrbits);
1848 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1849 NFSZERO_ATTRBIT(&rderrbits);
1850 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1852 NFSZERO_ATTRBIT(&attrbits);
1855 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1856 if (!nd->nd_repstat) {
1857 if (off && verf != at.na_filerev) {
1859 * va_filerev is not sufficient as a cookie verifier,
1860 * since it is not supposed to change when entries are
1861 * removed/added unless that offset cookies returned to
1862 * the client are no longer valid.
1865 if (nd->nd_flag & ND_NFSV4) {
1866 nd->nd_repstat = NFSERR_NOTSAME;
1868 nd->nd_repstat = NFSERR_BAD_COOKIE;
1871 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1872 nd->nd_repstat = NFSERR_BAD_COOKIE;
1875 if (!nd->nd_repstat && vp->v_type != VDIR)
1876 nd->nd_repstat = NFSERR_NOTDIR;
1877 if (!nd->nd_repstat && cnt == 0)
1878 nd->nd_repstat = NFSERR_TOOSMALL;
1879 if (!nd->nd_repstat)
1880 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1881 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1882 NFSACCCHK_VPISLOCKED, NULL);
1883 if (nd->nd_repstat) {
1885 if (nd->nd_flag & ND_NFSV3)
1886 nfsrv_postopattr(nd, getret, &at);
1889 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1890 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
1892 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1896 free((caddr_t)cookies, M_TEMP);
1904 io.uio_offset = (off_t)off;
1906 io.uio_segflg = UIO_SYSSPACE;
1907 io.uio_rw = UIO_READ;
1909 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1911 off = (u_int64_t)io.uio_offset;
1913 siz -= io.uio_resid;
1915 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1917 if (!cookies && !nd->nd_repstat)
1918 nd->nd_repstat = NFSERR_PERM;
1919 if (!nd->nd_repstat)
1920 nd->nd_repstat = getret;
1921 if (nd->nd_repstat) {
1924 free((caddr_t)cookies, M_TEMP);
1925 free((caddr_t)rbuf, M_TEMP);
1926 if (nd->nd_flag & ND_NFSV3)
1927 nfsrv_postopattr(nd, getret, &at);
1931 * If nothing read, return eof
1936 if (nd->nd_flag & ND_NFSV3)
1937 nfsrv_postopattr(nd, getret, &at);
1938 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1939 txdr_hyper(at.na_filerev, tl);
1941 *tl++ = newnfs_false;
1943 free((caddr_t)cookies, M_TEMP);
1944 free((caddr_t)rbuf, M_TEMP);
1949 * Check for degenerate cases of nothing useful read.
1950 * If so go try again
1954 dp = (struct dirent *)cpos;
1958 * For some reason FreeBSD's ufs_readdir() chooses to back the
1959 * directory offset up to a block boundary, so it is necessary to
1960 * skip over the records that precede the requested offset. This
1961 * requires the assumption that file offset cookies monotonically
1964 while (cpos < cend && ncookies > 0 &&
1965 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1966 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
1967 ((nd->nd_flag & ND_NFSV4) &&
1968 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1969 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1970 cpos += dp->d_reclen;
1971 dp = (struct dirent *)cpos;
1975 if (cpos >= cend || ncookies == 0) {
1982 * Busy the file system so that the mount point won't go away
1983 * and, as such, VFS_VGET() can be used safely.
1987 NFSVOPUNLOCK(vp, 0);
1988 nd->nd_repstat = vfs_busy(mp, 0);
1990 if (nd->nd_repstat != 0) {
1992 free(cookies, M_TEMP);
1994 if (nd->nd_flag & ND_NFSV3)
1995 nfsrv_postopattr(nd, getret, &at);
2000 * Check to see if entries in this directory can be safely acquired
2001 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2002 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2003 * automount of the snapshot directory that is required will
2005 * This needs to be done here for NFSv4, since NFSv4 never does
2006 * a VFS_VGET() for "." or "..".
2009 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2010 if (r == EOPNOTSUPP) {
2012 cn.cn_nameiop = LOOKUP;
2013 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2014 cn.cn_cred = nd->nd_cred;
2021 * Save this position, in case there is an error before one entry
2025 bpos0 = nd->nd_bpos;
2028 * Fill in the first part of the reply.
2029 * dirlen is the reply length in bytes and cannot exceed cnt.
2030 * (Include the two booleans at the end of the reply in dirlen now,
2031 * so we recognize when we have exceeded cnt.)
2033 if (nd->nd_flag & ND_NFSV3) {
2034 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2035 nfsrv_postopattr(nd, getret, &at);
2037 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2039 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2040 txdr_hyper(at.na_filerev, tl);
2043 * Save this position, in case there is an empty reply needed.
2046 bpos1 = nd->nd_bpos;
2048 /* Loop through the records and build reply */
2050 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2051 nlen = dp->d_namlen;
2052 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2053 nlen <= NFS_MAXNAMLEN &&
2054 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2055 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2056 || (nlen == 1 && dp->d_name[0] != '.'))) {
2058 * Save the current position in the reply, in case
2059 * this entry exceeds cnt.
2062 bpos1 = nd->nd_bpos;
2065 * For readdir_and_lookup get the vnode using
2074 mounted_on_fileno = (uint64_t)dp->d_fileno;
2075 if ((nd->nd_flag & ND_NFSV3) ||
2076 NFSNONZERO_ATTRBIT(&savbits)) {
2077 if (nd->nd_flag & ND_NFSV4)
2078 refp = nfsv4root_getreferral(NULL,
2082 r = VFS_VGET(mp, dp->d_fileno,
2086 if (r == EOPNOTSUPP) {
2089 cn.cn_nameiop = LOOKUP;
2097 cn.cn_nameptr = dp->d_name;
2098 cn.cn_namelen = nlen;
2099 cn.cn_flags = ISLASTCN |
2100 NOFOLLOW | LOCKLEAF;
2102 dp->d_name[0] == '.' &&
2103 dp->d_name[1] == '.')
2106 if (NFSVOPLOCK(vp, LK_SHARED)
2108 nd->nd_repstat = EPERM;
2111 if ((vp->v_vflag & VV_ROOT) != 0
2112 && (cn.cn_flags & ISDOTDOT)
2118 r = VOP_LOOKUP(vp, &nvp,
2127 * For NFSv4, check to see if nvp is
2128 * a mount point and get the mount
2129 * point vnode, as required.
2132 nfsrv_enable_crossmntpt != 0 &&
2133 (nd->nd_flag & ND_NFSV4) != 0 &&
2134 nvp->v_type == VDIR &&
2135 nvp->v_mountedhere != NULL) {
2136 new_mp = nvp->v_mountedhere;
2137 r = vfs_busy(new_mp, 0);
2141 r = VFS_ROOT(new_mp,
2151 ((nd->nd_flag & ND_NFSV3) ||
2152 NFSNONZERO_ATTRBIT(&attrbits))) {
2153 r = nfsvno_getfh(nvp, &nfh, p);
2155 r = nfsvno_getattr(nvp, nvap,
2157 if (r == 0 && is_zfs == 1 &&
2158 nfsrv_enable_crossmntpt != 0 &&
2159 (nd->nd_flag & ND_NFSV4) != 0 &&
2160 nvp->v_type == VDIR &&
2161 vp->v_mount != nvp->v_mount) {
2163 * For a ZFS snapshot, there is a
2164 * pseudo mount that does not set
2165 * v_mountedhere, so it needs to
2166 * be detected via a different
2171 new_mp = nvp->v_mount;
2178 if (!NFSISSET_ATTRBIT(&attrbits,
2179 NFSATTRBIT_RDATTRERROR)) {
2182 if (needs_unbusy != 0)
2191 * Build the directory record xdr
2193 if (nd->nd_flag & ND_NFSV3) {
2194 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2195 *tl++ = newnfs_true;
2197 *tl = txdr_unsigned(dp->d_fileno);
2198 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2199 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2201 *tl = txdr_unsigned(*cookiep);
2202 nfsrv_postopattr(nd, 0, nvap);
2203 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2204 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2208 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2209 *tl++ = newnfs_true;
2211 *tl = txdr_unsigned(*cookiep);
2212 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2214 supports_nfsv4acls =
2215 nfs_supportsnfsv4acls(nvp);
2216 NFSVOPUNLOCK(nvp, 0);
2218 supports_nfsv4acls = 0;
2220 dirlen += nfsrv_putreferralattr(nd,
2223 if (nd->nd_repstat) {
2226 if (needs_unbusy != 0)
2231 dirlen += nfsvno_fillattr(nd, new_mp,
2232 nvp, nvap, &nfh, r, &rderrbits,
2233 nd->nd_cred, p, isdgram, 0,
2234 supports_nfsv4acls, at_root,
2237 dirlen += nfsvno_fillattr(nd, new_mp,
2238 nvp, nvap, &nfh, r, &attrbits,
2239 nd->nd_cred, p, isdgram, 0,
2240 supports_nfsv4acls, at_root,
2245 dirlen += (3 * NFSX_UNSIGNED);
2247 if (needs_unbusy != 0)
2252 cpos += dp->d_reclen;
2253 dp = (struct dirent *)cpos;
2261 * If dirlen > cnt, we must strip off the last entry. If that
2262 * results in an empty reply, report NFSERR_TOOSMALL.
2264 if (dirlen > cnt || nd->nd_repstat) {
2265 if (!nd->nd_repstat && entrycnt == 0)
2266 nd->nd_repstat = NFSERR_TOOSMALL;
2268 newnfs_trimtrailing(nd, mb0, bpos0);
2270 newnfs_trimtrailing(nd, mb1, bpos1);
2272 } else if (cpos < cend)
2274 if (!nd->nd_repstat) {
2275 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2276 *tl++ = newnfs_false;
2282 FREE((caddr_t)cookies, M_TEMP);
2283 FREE((caddr_t)rbuf, M_TEMP);
2286 NFSEXITCODE2(0, nd);
2290 NFSEXITCODE2(error, nd);
2295 * Get the settable attributes out of the mbuf list.
2296 * (Return 0 or EBADRPC)
2299 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2300 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2303 struct nfsv2_sattr *sp;
2304 int error = 0, toclient = 0;
2306 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2308 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2310 * Some old clients didn't fill in the high order 16bits.
2311 * --> check the low order 2 bytes for 0xffff
2313 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2314 nvap->na_mode = nfstov_mode(sp->sa_mode);
2315 if (sp->sa_uid != newnfs_xdrneg1)
2316 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2317 if (sp->sa_gid != newnfs_xdrneg1)
2318 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2319 if (sp->sa_size != newnfs_xdrneg1)
2320 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2321 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2323 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2325 nvap->na_atime.tv_sec =
2326 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2327 nvap->na_atime.tv_nsec = 0;
2330 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2331 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2334 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2335 if (*tl == newnfs_true) {
2336 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2337 nvap->na_mode = nfstov_mode(*tl);
2339 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2340 if (*tl == newnfs_true) {
2341 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2342 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2344 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2345 if (*tl == newnfs_true) {
2346 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2347 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2349 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2350 if (*tl == newnfs_true) {
2351 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2352 nvap->na_size = fxdr_hyper(tl);
2354 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2355 switch (fxdr_unsigned(int, *tl)) {
2356 case NFSV3SATTRTIME_TOCLIENT:
2357 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2358 fxdr_nfsv3time(tl, &nvap->na_atime);
2361 case NFSV3SATTRTIME_TOSERVER:
2362 vfs_timestamp(&nvap->na_atime);
2363 nvap->na_vaflags |= VA_UTIMES_NULL;
2366 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2367 switch (fxdr_unsigned(int, *tl)) {
2368 case NFSV3SATTRTIME_TOCLIENT:
2369 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2370 fxdr_nfsv3time(tl, &nvap->na_mtime);
2371 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2373 case NFSV3SATTRTIME_TOSERVER:
2374 vfs_timestamp(&nvap->na_mtime);
2376 nvap->na_vaflags |= VA_UTIMES_NULL;
2381 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2384 NFSEXITCODE2(error, nd);
2389 * Handle the setable attributes for V4.
2390 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2393 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2394 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2399 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2401 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2405 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2408 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2409 attrsize = fxdr_unsigned(int, *tl);
2412 * Loop around getting the setable attributes. If an unsupported
2413 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2416 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2417 bitpos = NFSATTRBIT_MAX;
2421 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2422 if (attrsum > attrsize) {
2423 error = NFSERR_BADXDR;
2426 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2428 case NFSATTRBIT_SIZE:
2429 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2430 nvap->na_size = fxdr_hyper(tl);
2431 attrsum += NFSX_HYPER;
2433 case NFSATTRBIT_ACL:
2434 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2438 if (aceerr && !nd->nd_repstat)
2439 nd->nd_repstat = aceerr;
2442 case NFSATTRBIT_ARCHIVE:
2443 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2444 if (!nd->nd_repstat)
2445 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2446 attrsum += NFSX_UNSIGNED;
2448 case NFSATTRBIT_HIDDEN:
2449 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2450 if (!nd->nd_repstat)
2451 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2452 attrsum += NFSX_UNSIGNED;
2454 case NFSATTRBIT_MIMETYPE:
2455 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2456 i = fxdr_unsigned(int, *tl);
2457 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2460 if (!nd->nd_repstat)
2461 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2462 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2464 case NFSATTRBIT_MODE:
2465 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2466 nvap->na_mode = nfstov_mode(*tl);
2467 attrsum += NFSX_UNSIGNED;
2469 case NFSATTRBIT_OWNER:
2470 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2471 j = fxdr_unsigned(int, *tl);
2473 error = NFSERR_BADXDR;
2476 if (j > NFSV4_SMALLSTR)
2477 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2480 error = nfsrv_mtostr(nd, cp, j);
2482 if (j > NFSV4_SMALLSTR)
2483 free(cp, M_NFSSTRING);
2486 if (!nd->nd_repstat) {
2487 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2489 if (!nd->nd_repstat)
2492 if (j > NFSV4_SMALLSTR)
2493 free(cp, M_NFSSTRING);
2494 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2496 case NFSATTRBIT_OWNERGROUP:
2497 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2498 j = fxdr_unsigned(int, *tl);
2500 error = NFSERR_BADXDR;
2503 if (j > NFSV4_SMALLSTR)
2504 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2507 error = nfsrv_mtostr(nd, cp, j);
2509 if (j > NFSV4_SMALLSTR)
2510 free(cp, M_NFSSTRING);
2513 if (!nd->nd_repstat) {
2514 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2516 if (!nd->nd_repstat)
2519 if (j > NFSV4_SMALLSTR)
2520 free(cp, M_NFSSTRING);
2521 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2523 case NFSATTRBIT_SYSTEM:
2524 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2525 if (!nd->nd_repstat)
2526 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2527 attrsum += NFSX_UNSIGNED;
2529 case NFSATTRBIT_TIMEACCESSSET:
2530 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2531 attrsum += NFSX_UNSIGNED;
2532 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2533 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2534 fxdr_nfsv4time(tl, &nvap->na_atime);
2536 attrsum += NFSX_V4TIME;
2538 vfs_timestamp(&nvap->na_atime);
2539 nvap->na_vaflags |= VA_UTIMES_NULL;
2542 case NFSATTRBIT_TIMEBACKUP:
2543 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2544 if (!nd->nd_repstat)
2545 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2546 attrsum += NFSX_V4TIME;
2548 case NFSATTRBIT_TIMECREATE:
2549 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2550 if (!nd->nd_repstat)
2551 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2552 attrsum += NFSX_V4TIME;
2554 case NFSATTRBIT_TIMEMODIFYSET:
2555 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2556 attrsum += NFSX_UNSIGNED;
2557 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2558 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2559 fxdr_nfsv4time(tl, &nvap->na_mtime);
2560 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2561 attrsum += NFSX_V4TIME;
2563 vfs_timestamp(&nvap->na_mtime);
2565 nvap->na_vaflags |= VA_UTIMES_NULL;
2569 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2571 * set bitpos so we drop out of the loop.
2573 bitpos = NFSATTRBIT_MAX;
2579 * some clients pad the attrlist, so we need to skip over the
2582 if (attrsum > attrsize) {
2583 error = NFSERR_BADXDR;
2585 attrsize = NFSM_RNDUP(attrsize);
2586 if (attrsum < attrsize)
2587 error = nfsm_advance(nd, attrsize - attrsum, -1);
2590 NFSEXITCODE2(error, nd);
2595 * Check/setup export credentials.
2598 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2599 struct ucred *credanon)
2604 * Check/setup credentials.
2606 if (nd->nd_flag & ND_GSS)
2607 exp->nes_exflag &= ~MNT_EXPORTANON;
2610 * Check to see if the operation is allowed for this security flavor.
2611 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2612 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2613 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2615 if (nfsvno_testexp(nd, exp) &&
2616 nd->nd_procnum != NFSV4OP_SECINFO &&
2617 nd->nd_procnum != NFSPROC_FSINFO) {
2618 if (nd->nd_flag & ND_NFSV4)
2619 error = NFSERR_WRONGSEC;
2621 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2626 * Check to see if the file system is exported V4 only.
2628 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2629 error = NFSERR_PROGNOTV4;
2634 * Now, map the user credentials.
2635 * (Note that ND_AUTHNONE will only be set for an NFSv3
2636 * Fsinfo RPC. If set for anything else, this code might need
2639 if (NFSVNO_EXPORTED(exp) &&
2640 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2641 NFSVNO_EXPORTANON(exp) ||
2642 (nd->nd_flag & ND_AUTHNONE))) {
2643 nd->nd_cred->cr_uid = credanon->cr_uid;
2644 nd->nd_cred->cr_gid = credanon->cr_gid;
2645 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2646 credanon->cr_groups);
2650 NFSEXITCODE2(error, nd);
2658 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2659 struct ucred **credp)
2661 int i, error, *secflavors;
2663 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2664 &exp->nes_numsecflavor, &secflavors);
2666 if (nfs_rootfhset) {
2667 exp->nes_exflag = 0;
2668 exp->nes_numsecflavor = 0;
2672 /* Copy the security flavors. */
2673 for (i = 0; i < exp->nes_numsecflavor; i++)
2674 exp->nes_secflavors[i] = secflavors[i];
2681 * Get a vnode for a file handle and export stuff.
2684 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2685 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2686 struct ucred **credp)
2688 int i, error, *secflavors;
2691 exp->nes_numsecflavor = 0;
2692 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2694 /* Make sure the server replies ESTALE to the client. */
2696 if (nam && !error) {
2697 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2698 &exp->nes_numsecflavor, &secflavors);
2700 if (nfs_rootfhset) {
2701 exp->nes_exflag = 0;
2702 exp->nes_numsecflavor = 0;
2708 /* Copy the security flavors. */
2709 for (i = 0; i < exp->nes_numsecflavor; i++)
2710 exp->nes_secflavors[i] = secflavors[i];
2718 * nfsd_fhtovp() - convert a fh to a vnode ptr
2719 * - look up fsid in mount list (if not found ret error)
2720 * - get vp and export rights by calling nfsvno_fhtovp()
2721 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2723 * - if mpp != NULL, return the mount point so that it can
2724 * be used for vn_finished_write() by the caller
2727 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2728 struct vnode **vpp, struct nfsexstuff *exp,
2729 struct mount **mpp, int startwrite, struct thread *p)
2732 struct ucred *credanon;
2735 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2737 * Check for the special case of the nfsv4root_fh.
2739 mp = vfs_busyfs(&fhp->fh_fsid);
2744 nd->nd_repstat = ESTALE;
2749 vn_start_write(NULL, mpp, V_WAIT);
2750 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2751 lktype = LK_EXCLUSIVE;
2753 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2758 * For NFSv4 without a pseudo root fs, unexported file handles
2759 * can be returned, so that Lookup works everywhere.
2761 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2762 !(nd->nd_flag & ND_NFSV4)) {
2764 nd->nd_repstat = EACCES;
2768 * Personally, I've never seen any point in requiring a
2769 * reserved port#, since only in the rare case where the
2770 * clients are all boxes with secure system priviledges,
2771 * does it provide any enhanced security, but... some people
2772 * believe it to be useful and keep putting this code back in.
2773 * (There is also some "security checker" out there that
2774 * complains if the nfs server doesn't enforce this.)
2775 * However, note the following:
2776 * RFC3530 (NFSv4) specifies that a reserved port# not be
2778 * RFC2623 recommends that, if a reserved port# is checked for,
2779 * that there be a way to turn that off--> ifdef'd.
2781 #ifdef NFS_REQRSVPORT
2782 if (!nd->nd_repstat) {
2783 struct sockaddr_in *saddr;
2784 struct sockaddr_in6 *saddr6;
2786 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2787 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2788 if (!(nd->nd_flag & ND_NFSV4) &&
2789 ((saddr->sin_family == AF_INET &&
2790 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2791 (saddr6->sin6_family == AF_INET6 &&
2792 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2794 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2797 #endif /* NFS_REQRSVPORT */
2800 * Check/setup credentials.
2802 if (!nd->nd_repstat) {
2803 nd->nd_saveduid = nd->nd_cred->cr_uid;
2804 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2808 if (credanon != NULL)
2810 if (nd->nd_repstat) {
2812 vn_finished_write(mp);
2819 NFSEXITCODE2(0, nd);
2826 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2828 struct filedesc *fdp;
2832 fdp = p->td_proc->p_fd;
2833 if (fd < 0 || fd >= fdp->fd_nfiles ||
2834 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2846 * Called from nfssvc() to update the exports list. Just call
2847 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2848 * in the mount list.
2851 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2853 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2855 struct nameidata nd;
2858 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2859 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2861 else if (error == 0) {
2862 if (nfsexargp->fspec == NULL) {
2867 * If fspec != NULL, this is the v4root path.
2869 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2870 nfsexargp->fspec, p);
2871 if ((error = namei(&nd)) != 0)
2873 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2876 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2877 NFSBCOPY((caddr_t)&fh,
2878 nfs_rootfh.nfsrvfh_data,
2879 sizeof (fhandle_t));
2890 * This function needs to test to see if the system is near its limit
2891 * for memory allocation via malloc() or mget() and return True iff
2892 * either of these resources are near their limit.
2893 * XXX (For now, this is just a stub.)
2895 int nfsrv_testmalloclimit = 0;
2897 nfsrv_mallocmget_limit(void)
2899 static int printmesg = 0;
2900 static int testval = 1;
2902 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2903 if ((printmesg++ % 100) == 0)
2904 printf("nfsd: malloc/mget near limit\n");
2911 * BSD specific initialization of a mount point.
2916 static int inited = 0;
2921 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2922 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2923 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2924 nfsv4root_mnt.mnt_export = NULL;
2925 TAILQ_INIT(&nfsv4root_opt);
2926 TAILQ_INIT(&nfsv4root_newopt);
2927 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2928 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2929 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2930 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2934 * Get a vnode for a file handle, without checking exports, etc.
2937 nfsvno_getvp(fhandle_t *fhp)
2943 mp = vfs_busyfs(&fhp->fh_fsid);
2946 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2954 * Do a local VOP_ADVLOCK().
2957 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2958 u_int64_t end, struct thread *td)
2964 if (nfsrv_dolocallocks == 0)
2967 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2968 if ((vp->v_iflag & VI_DOOMED) != 0) {
2973 fl.l_whence = SEEK_SET;
2975 fl.l_start = (off_t)first;
2976 if (end == NFS64BITSSET) {
2980 fl.l_len = (off_t)tlen;
2983 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2984 * values for all calls, so that all locks will be held by the
2985 * nfsd server. (The nfsd server handles conflicts between the
2987 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2988 * bytes, so it can't be put in l_sysid.
2990 if (nfsv4_sysid == 0)
2991 nfsv4_sysid = nlm_acquire_next_sysid();
2992 fl.l_pid = (pid_t)0;
2993 fl.l_sysid = (int)nfsv4_sysid;
2995 NFSVOPUNLOCK(vp, 0);
2996 if (ftype == F_UNLCK)
2997 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2998 (F_POSIX | F_REMOTE));
3000 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3001 (F_POSIX | F_REMOTE));
3002 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3010 * Check the nfsv4 root exports.
3013 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3015 struct ucred *credanon;
3016 int exflags, error = 0, numsecflavor, *secflavors, i;
3018 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3019 &credanon, &numsecflavor, &secflavors);
3021 error = NFSERR_PROGUNAVAIL;
3024 if (credanon != NULL)
3026 for (i = 0; i < numsecflavor; i++) {
3027 if (secflavors[i] == AUTH_SYS)
3028 nd->nd_flag |= ND_EXAUTHSYS;
3029 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3030 nd->nd_flag |= ND_EXGSS;
3031 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3032 nd->nd_flag |= ND_EXGSSINTEGRITY;
3033 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3034 nd->nd_flag |= ND_EXGSSPRIVACY;
3043 * Nfs server psuedo system call for the nfsd's
3049 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3052 struct nfsd_addsock_args sockarg;
3053 struct nfsd_nfsd_args nfsdarg;
3054 cap_rights_t rights;
3057 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3058 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3062 * Since we don't know what rights might be required,
3063 * pretend that we need them all. It is better to be too
3064 * careful than too reckless.
3066 error = fget(td, sockarg.sock,
3067 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3070 if (fp->f_type != DTYPE_SOCKET) {
3075 error = nfsrvd_addsock(fp);
3077 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3078 if (uap->argp == NULL) {
3082 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3086 error = nfsrvd_nfsd(td, &nfsdarg);
3088 error = nfssvc_srvcall(td, uap, td->td_ucred);
3097 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3099 struct nfsex_args export;
3100 struct file *fp = NULL;
3102 struct nfsd_clid adminrevoke;
3103 struct nfsd_dumplist dumplist;
3104 struct nfsd_dumpclients *dumpclients;
3105 struct nfsd_dumplocklist dumplocklist;
3106 struct nfsd_dumplocks *dumplocks;
3107 struct nameidata nd;
3109 int error = EINVAL, igotlock;
3111 static int suspend_nfsd = 0;
3113 if (uap->flag & NFSSVC_PUBLICFH) {
3114 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3115 sizeof (fhandle_t));
3116 error = copyin(uap->argp,
3117 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3120 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3121 error = copyin(uap->argp,(caddr_t)&export,
3122 sizeof (struct nfsex_args));
3124 error = nfsrv_v4rootexport(&export, cred, p);
3125 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3128 } else if (uap->flag & NFSSVC_STABLERESTART) {
3129 error = copyin(uap->argp, (caddr_t)&stablefd,
3132 error = fp_getfvp(p, stablefd, &fp, &vp);
3133 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3135 if (!error && newnfs_numnfsd != 0)
3138 nfsrv_stablefirst.nsf_fp = fp;
3139 nfsrv_setupstable(p);
3141 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3142 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3143 sizeof (struct nfsd_clid));
3145 error = nfsrv_adminrevoke(&adminrevoke, p);
3146 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3147 error = copyin(uap->argp, (caddr_t)&dumplist,
3148 sizeof (struct nfsd_dumplist));
3149 if (!error && (dumplist.ndl_size < 1 ||
3150 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3153 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3154 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3156 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3157 error = copyout(dumpclients,
3158 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3159 free((caddr_t)dumpclients, M_TEMP);
3161 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3162 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3163 sizeof (struct nfsd_dumplocklist));
3164 if (!error && (dumplocklist.ndllck_size < 1 ||
3165 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3168 error = nfsrv_lookupfilename(&nd,
3169 dumplocklist.ndllck_fname, p);
3171 len = sizeof (struct nfsd_dumplocks) *
3172 dumplocklist.ndllck_size;
3173 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3175 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3176 dumplocklist.ndllck_size, p);
3178 error = copyout(dumplocks,
3179 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3180 free((caddr_t)dumplocks, M_TEMP);
3182 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3185 nfsd_master_pid = procp->p_pid;
3186 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3187 nfsd_master_start = procp->p_stats->p_start;
3188 nfsd_master_proc = procp;
3190 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3191 NFSLOCKV4ROOTMUTEX();
3192 if (suspend_nfsd == 0) {
3193 /* Lock out all nfsd threads */
3195 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3196 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3197 } while (igotlock == 0 && suspend_nfsd == 0);
3200 NFSUNLOCKV4ROOTMUTEX();
3202 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3203 NFSLOCKV4ROOTMUTEX();
3204 if (suspend_nfsd != 0) {
3205 nfsv4_unlock(&nfsd_suspend_lock, 0);
3208 NFSUNLOCKV4ROOTMUTEX();
3218 * Returns 0 if ok, 1 otherwise.
3221 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3226 * This seems odd, but allow the case where the security flavor
3227 * list is empty. This happens when NFSv4 is traversing non-exported
3228 * file systems. Exported file systems should always have a non-empty
3229 * security flavor list.
3231 if (exp->nes_numsecflavor == 0)
3234 for (i = 0; i < exp->nes_numsecflavor; i++) {
3236 * The tests for privacy and integrity must be first,
3237 * since ND_GSS is set for everything but AUTH_SYS.
3239 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3240 (nd->nd_flag & ND_GSSPRIVACY))
3242 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3243 (nd->nd_flag & ND_GSSINTEGRITY))
3245 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3246 (nd->nd_flag & ND_GSS))
3248 if (exp->nes_secflavors[i] == AUTH_SYS &&
3249 (nd->nd_flag & ND_GSS) == 0)
3256 * Calculate a hash value for the fid in a file handle.
3259 nfsrv_hashfh(fhandle_t *fhp)
3263 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3268 * Calculate a hash value for the sessionid.
3271 nfsrv_hashsessionid(uint8_t *sessionid)
3275 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3280 * Signal the userland master nfsd to backup the stable restart file.
3283 nfsrv_backupstable(void)
3287 if (nfsd_master_proc != NULL) {
3288 procp = pfind(nfsd_master_pid);
3289 /* Try to make sure it is the correct process. */
3290 if (procp == nfsd_master_proc &&
3291 procp->p_stats->p_start.tv_sec ==
3292 nfsd_master_start.tv_sec &&
3293 procp->p_stats->p_start.tv_usec ==
3294 nfsd_master_start.tv_usec &&
3295 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3296 kern_psignal(procp, SIGUSR2);
3298 nfsd_master_proc = NULL;
3305 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3308 * Called once to initialize data structures...
3311 nfsd_modevent(module_t mod, int type, void *data)
3314 static int loaded = 0;
3321 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3322 snprintf(nfsrchash_table[i].lock_name,
3323 sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d",
3325 mtx_init(&nfsrchash_table[i].mtx,
3326 nfsrchash_table[i].lock_name, NULL, MTX_DEF);
3327 snprintf(nfsrcahash_table[i].lock_name,
3328 sizeof(nfsrcahash_table[i].lock_name), "nfsrc_tcpa%d",
3330 mtx_init(&nfsrcahash_table[i].mtx,
3331 nfsrcahash_table[i].lock_name, NULL, MTX_DEF);
3333 mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF);
3334 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3335 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3337 for (i = 0; i < NFSSESSIONHASHSIZE; i++)
3338 mtx_init(&nfssessionhash[i].mtx, "nfs_session_mutex",
3340 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3347 #ifdef VV_DISABLEDELEG
3348 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3349 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3351 nfsd_call_servertimer = nfsrv_servertimer;
3352 nfsd_call_nfsd = nfssvc_nfsd;
3357 if (newnfs_numnfsd != 0) {
3362 #ifdef VV_DISABLEDELEG
3363 vn_deleg_ops.vndeleg_recall = NULL;
3364 vn_deleg_ops.vndeleg_disable = NULL;
3366 nfsd_call_servertimer = NULL;
3367 nfsd_call_nfsd = NULL;
3369 /* Clean out all NFSv4 state. */
3370 nfsrv_throwawayallstate(curthread);
3372 /* Clean the NFS server reply cache */
3373 nfsrvd_cleancache();
3375 /* Free up the krpc server pool. */
3376 if (nfsrvd_pool != NULL)
3377 svcpool_destroy(nfsrvd_pool);
3379 /* and get rid of the locks */
3380 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3381 mtx_destroy(&nfsrchash_table[i].mtx);
3382 mtx_destroy(&nfsrcahash_table[i].mtx);
3384 mtx_destroy(&nfsrc_udpmtx);
3385 mtx_destroy(&nfs_v4root_mutex);
3386 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3387 for (i = 0; i < NFSSESSIONHASHSIZE; i++)
3388 mtx_destroy(&nfssessionhash[i].mtx);
3389 lockdestroy(&nfsv4root_mnt.mnt_explock);
3401 static moduledata_t nfsd_mod = {
3406 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3408 /* So that loader and kldload(2) can find us, wherever we are.. */
3409 MODULE_VERSION(nfsd, 1);
3410 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3411 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3412 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3413 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3414 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);