2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/capability.h>
40 * Functions that perform the vfs operations required by the routines in
41 * nfsd_serv.c. It is hoped that this change will make the server more
45 #include <fs/nfs/nfsport.h>
47 #include <sys/sysctl.h>
48 #include <nlm/nlm_prot.h>
51 FEATURE(nfsd, "NFSv4 server");
53 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54 extern int nfsrv_useacl;
55 extern int newnfs_numnfsd;
56 extern struct mount nfsv4root_mnt;
57 extern struct nfsrv_stablefirst nfsrv_stablefirst;
58 extern void (*nfsd_call_servertimer)(void);
59 extern SVCPOOL *nfsrvd_pool;
60 extern struct nfsv4lock nfsd_suspend_lock;
61 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
63 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
64 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
65 struct mtx nfsrc_udpmtx;
66 struct mtx nfs_v4root_mutex;
67 struct nfsrvfh nfs_rootfh, nfs_pubfh;
68 int nfs_pubfhset = 0, nfs_rootfhset = 0;
69 struct proc *nfsd_master_proc = NULL;
70 static pid_t nfsd_master_pid = (pid_t)-1;
71 static char nfsd_master_comm[MAXCOMLEN + 1];
72 static struct timeval nfsd_master_start;
73 static uint32_t nfsv4_sysid = 0;
75 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
78 int nfsrv_enable_crossmntpt = 1;
79 static int nfs_commit_blks;
80 static int nfs_commit_miss;
81 extern int nfsrv_issuedelegs;
82 extern int nfsrv_dolocallocks;
84 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
85 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
86 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
87 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
89 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
91 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
92 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
93 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
94 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
96 #define MAX_REORDERED_RPC 16
97 #define NUM_HEURISTIC 1031
100 #define NHUSE_MAX 2048
102 static struct nfsheur {
103 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
104 off_t nh_nextoff; /* next offset for sequential detection */
105 int nh_use; /* use count for selection */
106 int nh_seqcount; /* heuristic */
107 } nfsheur[NUM_HEURISTIC];
111 * Heuristic to detect sequential operation.
113 static struct nfsheur *
114 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
119 /* Locate best candidate. */
121 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
124 if (nfsheur[hi].nh_vp == vp) {
128 if (nfsheur[hi].nh_use > 0)
129 --nfsheur[hi].nh_use;
130 hi = (hi + 1) % NUM_HEURISTIC;
131 if (nfsheur[hi].nh_use < nh->nh_use)
135 /* Initialize hint if this is a new file. */
136 if (nh->nh_vp != vp) {
138 nh->nh_nextoff = uio->uio_offset;
139 nh->nh_use = NHUSE_INIT;
140 if (uio->uio_offset == 0)
146 /* Calculate heuristic. */
147 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
148 uio->uio_offset == nh->nh_nextoff) {
149 /* See comments in vfs_vnops.c:sequential_heuristic(). */
150 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
151 if (nh->nh_seqcount > IO_SEQMAX)
152 nh->nh_seqcount = IO_SEQMAX;
153 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
154 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
155 /* Probably a reordered RPC, leave seqcount alone. */
156 } else if (nh->nh_seqcount > 1) {
157 nh->nh_seqcount /= 2;
161 nh->nh_use += NHUSE_INC;
162 if (nh->nh_use > NHUSE_MAX)
163 nh->nh_use = NHUSE_MAX;
168 * Get attributes into nfsvattr structure.
171 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
172 struct thread *p, int vpislocked)
174 int error, lockedit = 0;
176 if (vpislocked == 0) {
178 * When vpislocked == 0, the vnode is either exclusively
179 * locked by this thread or not locked by this thread.
180 * As such, shared lock it, if not exclusively locked.
182 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
184 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
187 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
196 * Get a file handle for a vnode.
199 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
203 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
204 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
205 error = VOP_VPTOFH(vp, &fhp->fh_fid);
212 * Perform access checking for vnodes obtained from file handles that would
213 * refer to files already opened by a Unix client. You cannot just use
214 * vn_writechk() and VOP_ACCESSX() for two reasons.
215 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
217 * 2 - The owner is to be given access irrespective of mode bits for some
218 * operations, so that processes that chmod after opening a file don't
222 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
223 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
224 u_int32_t *supportedtypep)
227 int error = 0, getret = 0;
229 if (vpislocked == 0) {
230 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
235 if (accmode & VWRITE) {
236 /* Just vn_writechk() changed to check rdonly */
238 * Disallow write attempts on read-only file systems;
239 * unless the file is a socket or a block or character
240 * device resident on the file system.
242 if (NFSVNO_EXRDONLY(exp) ||
243 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
244 switch (vp->v_type) {
254 * If there's shared text associated with
255 * the inode, try to free it up once. If
256 * we fail, we can't allow writing.
258 if (VOP_IS_TEXT(vp) && error == 0)
268 * Should the override still be applied when ACLs are enabled?
270 error = VOP_ACCESSX(vp, accmode, cred, p);
271 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
273 * Try again with VEXPLICIT_DENY, to see if the test for
274 * deletion is supported.
276 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
278 if (vp->v_type == VDIR) {
279 accmode &= ~(VDELETE | VDELETE_CHILD);
281 error = VOP_ACCESSX(vp, accmode, cred, p);
282 } else if (supportedtypep != NULL) {
283 *supportedtypep &= ~NFSACCESS_DELETE;
289 * Allow certain operations for the owner (reads and writes
290 * on files that are already open).
292 if (override != NFSACCCHK_NOOVERRIDE &&
293 (error == EPERM || error == EACCES)) {
294 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
296 else if (override & NFSACCCHK_ALLOWOWNER) {
297 getret = VOP_GETATTR(vp, &vattr, cred);
298 if (getret == 0 && cred->cr_uid == vattr.va_uid)
311 * Set attribute(s) vnop.
314 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
315 struct thread *p, struct nfsexstuff *exp)
319 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
325 * Set up nameidata for a lookup() call and do it.
328 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
329 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
330 struct vnode **retdirp)
332 struct componentname *cnp = &ndp->ni_cnd;
336 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
337 int error = 0, crossmnt;
341 cnp->cn_nameptr = cnp->cn_pnbuf;
342 ndp->ni_strictrelative = 0;
344 * Extract and set starting directory.
346 if (dp->v_type != VDIR) {
351 nfsvno_relpathbuf(ndp);
359 if (NFSVNO_EXRDONLY(exp))
360 cnp->cn_flags |= RDONLY;
361 ndp->ni_segflg = UIO_SYSSPACE;
364 if (nd->nd_flag & ND_PUBLOOKUP) {
366 if (cnp->cn_pnbuf[0] == '/') {
369 * Check for degenerate pathnames here, since lookup()
372 for (i = 1; i < ndp->ni_pathlen; i++)
373 if (cnp->cn_pnbuf[i] != '/')
375 if (i == ndp->ni_pathlen) {
376 error = NFSERR_ACCES;
382 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
383 (nd->nd_flag & ND_NFSV4) == 0) {
385 * Only cross mount points for NFSv4 when doing a
386 * mount while traversing the file system above
387 * the mount point, unless nfsrv_enable_crossmntpt is set.
389 cnp->cn_flags |= NOCROSSMOUNT;
394 * Initialize for scan, set ni_startdir and bump ref on dp again
395 * becuase lookup() will dereference ni_startdir.
399 ndp->ni_startdir = dp;
400 ndp->ni_rootdir = rootvnode;
401 ndp->ni_topdir = NULL;
404 cnp->cn_flags |= LOCKLEAF;
406 cnp->cn_nameptr = cnp->cn_pnbuf;
408 * Call lookup() to do the real work. If an error occurs,
409 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
410 * we do not have to dereference anything before returning.
411 * In either case ni_startdir will be dereferenced and NULLed
419 * Check for encountering a symbolic link. Trivial
420 * termination occurs if no symlink encountered.
422 if ((cnp->cn_flags & ISSYMLINK) == 0) {
423 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
424 nfsvno_relpathbuf(ndp);
425 if (ndp->ni_vp && !lockleaf)
426 NFSVOPUNLOCK(ndp->ni_vp, 0);
433 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
434 NFSVOPUNLOCK(ndp->ni_dvp, 0);
435 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
440 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
444 if (ndp->ni_pathlen > 1)
445 cp = uma_zalloc(namei_zone, M_WAITOK);
449 aiov.iov_len = MAXPATHLEN;
450 auio.uio_iov = &aiov;
453 auio.uio_rw = UIO_READ;
454 auio.uio_segflg = UIO_SYSSPACE;
456 auio.uio_resid = MAXPATHLEN;
457 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
460 if (ndp->ni_pathlen > 1)
461 uma_zfree(namei_zone, cp);
467 linklen = MAXPATHLEN - auio.uio_resid;
472 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
473 error = ENAMETOOLONG;
478 * Adjust or replace path
480 if (ndp->ni_pathlen > 1) {
481 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
482 uma_zfree(namei_zone, cnp->cn_pnbuf);
485 cnp->cn_pnbuf[linklen] = '\0';
486 ndp->ni_pathlen += linklen;
489 * Cleanup refs for next loop and check if root directory
490 * should replace current directory. Normally ni_dvp
491 * becomes the new base directory and is cleaned up when
492 * we loop. Explicitly null pointers after invalidation
493 * to clarify operation.
498 if (cnp->cn_pnbuf[0] == '/') {
500 ndp->ni_dvp = ndp->ni_rootdir;
503 ndp->ni_startdir = ndp->ni_dvp;
507 cnp->cn_flags &= ~LOCKLEAF;
511 uma_zfree(namei_zone, cnp->cn_pnbuf);
514 ndp->ni_startdir = NULL;
515 cnp->cn_flags &= ~HASBUF;
516 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
521 NFSEXITCODE2(error, nd);
526 * Set up a pathname buffer and return a pointer to it and, optionally
527 * set a hash pointer.
530 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
532 struct componentname *cnp = &ndp->ni_cnd;
534 cnp->cn_flags |= (NOMACCHECK | HASBUF);
535 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
538 *bufpp = cnp->cn_pnbuf;
542 * Release the above path buffer, if not released by nfsvno_namei().
545 nfsvno_relpathbuf(struct nameidata *ndp)
548 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
550 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
551 ndp->ni_cnd.cn_flags &= ~HASBUF;
555 * Readlink vnode op into an mbuf list.
558 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
559 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
561 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
562 struct iovec *ivp = iv;
563 struct uio io, *uiop = &io;
564 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
565 int i, len, tlen, error = 0;
569 while (len < NFS_MAXPATHLEN) {
572 mp->m_len = NFSMSIZ(mp);
579 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
580 mp->m_len = NFS_MAXPATHLEN - len;
581 len = NFS_MAXPATHLEN;
585 ivp->iov_base = mtod(mp, caddr_t);
586 ivp->iov_len = mp->m_len;
591 uiop->uio_iovcnt = i;
592 uiop->uio_offset = 0;
593 uiop->uio_resid = len;
594 uiop->uio_rw = UIO_READ;
595 uiop->uio_segflg = UIO_SYSSPACE;
597 error = VOP_READLINK(vp, uiop, cred);
603 if (uiop->uio_resid > 0) {
604 len -= uiop->uio_resid;
605 tlen = NFSM_RNDUP(len);
606 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
618 * Read vnode op call into mbuf list.
621 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
622 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
628 int error = 0, len, left, siz, tlen, ioflag = 0;
629 struct mbuf *m2 = NULL, *m3;
630 struct uio io, *uiop = &io;
633 len = left = NFSM_RNDUP(cnt);
636 * Generate the mbuf list with the uio_iov ref. to it.
643 siz = min(M_TRAILINGSPACE(m), left);
652 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
654 uiop->uio_iov = iv2 = iv;
660 panic("nfsvno_read iov");
661 siz = min(M_TRAILINGSPACE(m), left);
663 iv->iov_base = mtod(m, caddr_t) + m->m_len;
672 uiop->uio_iovcnt = i;
673 uiop->uio_offset = off;
674 uiop->uio_resid = len;
675 uiop->uio_rw = UIO_READ;
676 uiop->uio_segflg = UIO_SYSSPACE;
678 nh = nfsrv_sequential_heuristic(uiop, vp);
679 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
680 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
681 FREE((caddr_t)iv2, M_TEMP);
687 nh->nh_nextoff = uiop->uio_offset;
688 tlen = len - uiop->uio_resid;
689 cnt = cnt < tlen ? cnt : tlen;
690 tlen = NFSM_RNDUP(cnt);
694 } else if (len != tlen || tlen != cnt)
695 nfsrv_adj(m3, len - tlen, tlen - cnt);
705 * Write vnode op from an mbuf list.
708 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
709 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
715 struct uio io, *uiop = &io;
718 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
720 uiop->uio_iov = iv = ivp;
721 uiop->uio_iovcnt = cnt;
722 i = mtod(mp, caddr_t) + mp->m_len - cp;
726 panic("nfsvno_write");
737 cp = mtod(mp, caddr_t);
741 if (stable == NFSWRITE_UNSTABLE)
742 ioflags = IO_NODELOCKED;
744 ioflags = (IO_SYNC | IO_NODELOCKED);
745 uiop->uio_resid = retlen;
746 uiop->uio_rw = UIO_WRITE;
747 uiop->uio_segflg = UIO_SYSSPACE;
749 uiop->uio_offset = off;
750 nh = nfsrv_sequential_heuristic(uiop, vp);
751 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
752 error = VOP_WRITE(vp, uiop, ioflags, cred);
754 nh->nh_nextoff = uiop->uio_offset;
755 FREE((caddr_t)iv, M_TEMP);
762 * Common code for creating a regular file (plus special files for V2).
765 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
766 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
767 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
772 error = nd->nd_repstat;
773 if (!error && ndp->ni_vp == NULL) {
774 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
775 vrele(ndp->ni_startdir);
776 error = VOP_CREATE(ndp->ni_dvp,
777 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
779 nfsvno_relpathbuf(ndp);
781 if (*exclusive_flagp) {
782 *exclusive_flagp = 0;
783 NFSVNO_ATTRINIT(nvap);
784 nvap->na_atime.tv_sec = cverf[0];
785 nvap->na_atime.tv_nsec = cverf[1];
786 error = VOP_SETATTR(ndp->ni_vp,
787 &nvap->na_vattr, nd->nd_cred);
791 * NFS V2 Only. nfsrvd_mknod() does this for V3.
792 * (This implies, just get out on an error.)
794 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
795 nvap->na_type == VFIFO) {
796 if (nvap->na_type == VCHR && rdev == 0xffffffff)
797 nvap->na_type = VFIFO;
798 if (nvap->na_type != VFIFO &&
799 (error = priv_check_cred(nd->nd_cred,
800 PRIV_VFS_MKNOD_DEV, 0))) {
801 vrele(ndp->ni_startdir);
802 nfsvno_relpathbuf(ndp);
806 nvap->na_rdev = rdev;
807 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
808 &ndp->ni_cnd, &nvap->na_vattr);
810 nfsvno_relpathbuf(ndp);
811 vrele(ndp->ni_startdir);
815 vrele(ndp->ni_startdir);
816 nfsvno_relpathbuf(ndp);
824 * Handle cases where error is already set and/or
826 * 1 - clean up the lookup
827 * 2 - iff !error and na_size set, truncate it
829 vrele(ndp->ni_startdir);
830 nfsvno_relpathbuf(ndp);
832 if (ndp->ni_dvp == *vpp)
836 if (!error && nvap->na_size != VNOVAL) {
837 error = nfsvno_accchk(*vpp, VWRITE,
838 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
839 NFSACCCHK_VPISLOCKED, NULL);
841 tempsize = nvap->na_size;
842 NFSVNO_ATTRINIT(nvap);
843 nvap->na_size = tempsize;
844 error = VOP_SETATTR(*vpp,
845 &nvap->na_vattr, nd->nd_cred);
858 * Do a mknod vnode op.
861 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
867 vtyp = nvap->na_type;
869 * Iff doesn't exist, create it.
872 vrele(ndp->ni_startdir);
873 nfsvno_relpathbuf(ndp);
879 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
880 vrele(ndp->ni_startdir);
881 nfsvno_relpathbuf(ndp);
883 error = NFSERR_BADTYPE;
887 vrele(ndp->ni_startdir);
888 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
889 &ndp->ni_cnd, &nvap->na_vattr);
891 nfsvno_relpathbuf(ndp);
893 if (nvap->na_type != VFIFO &&
894 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
895 vrele(ndp->ni_startdir);
896 nfsvno_relpathbuf(ndp);
900 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
901 &ndp->ni_cnd, &nvap->na_vattr);
903 nfsvno_relpathbuf(ndp);
904 vrele(ndp->ni_startdir);
906 * Since VOP_MKNOD returns the ni_vp, I can't
907 * see any reason to do the lookup.
920 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
921 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
925 if (ndp->ni_vp != NULL) {
926 if (ndp->ni_dvp == ndp->ni_vp)
931 nfsvno_relpathbuf(ndp);
935 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
938 nfsvno_relpathbuf(ndp);
949 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
950 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
951 struct nfsexstuff *exp)
956 vrele(ndp->ni_startdir);
957 nfsvno_relpathbuf(ndp);
958 if (ndp->ni_dvp == ndp->ni_vp)
967 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
968 &nvap->na_vattr, pathcp);
970 vrele(ndp->ni_startdir);
971 nfsvno_relpathbuf(ndp);
973 * Although FreeBSD still had the lookup code in
974 * it for 7/current, there doesn't seem to be any
975 * point, since VOP_SYMLINK() returns the ni_vp.
976 * Just vput it for v2.
978 if (!not_v2 && !error)
987 * Parse symbolic link arguments.
988 * This function has an ugly side effect. It will MALLOC() an area for
989 * the symlink and set iov_base to point to it, only if it succeeds.
990 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
994 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
995 struct thread *p, char **pathcpp, int *lenp)
1000 struct nfsv2_sattr *sp;
1004 if ((nd->nd_flag & ND_NFSV3) &&
1005 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1007 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1008 len = fxdr_unsigned(int, *tl);
1009 if (len > NFS_MAXPATHLEN || len <= 0) {
1013 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1014 error = nfsrv_mtostr(nd, pathcp, len);
1017 if (nd->nd_flag & ND_NFSV2) {
1018 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1019 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1023 NFSEXITCODE2(0, nd);
1027 free(pathcp, M_TEMP);
1028 NFSEXITCODE2(error, nd);
1033 * Remove a non-directory object.
1036 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1037 struct thread *p, struct nfsexstuff *exp)
1043 if (vp->v_type == VDIR)
1044 error = NFSERR_ISDIR;
1046 error = nfsrv_checkremove(vp, 1, p);
1048 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1049 if (ndp->ni_dvp == vp)
1054 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1055 nfsvno_relpathbuf(ndp);
1061 * Remove a directory.
1064 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1065 struct thread *p, struct nfsexstuff *exp)
1071 if (vp->v_type != VDIR) {
1076 * No rmdir "." please.
1078 if (ndp->ni_dvp == vp) {
1083 * The root of a mounted filesystem cannot be deleted.
1085 if (vp->v_vflag & VV_ROOT)
1089 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1090 if (ndp->ni_dvp == vp)
1095 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1096 nfsvno_relpathbuf(ndp);
1105 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1106 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1108 struct vnode *fvp, *tvp, *tdvp;
1111 fvp = fromndp->ni_vp;
1113 vrele(fromndp->ni_dvp);
1118 tdvp = tondp->ni_dvp;
1121 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1122 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1124 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1125 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1128 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1129 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1134 * A rename to '.' or '..' results in a prematurely
1135 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1138 if ((tondp->ni_cnd.cn_namelen == 1 &&
1139 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1140 (tondp->ni_cnd.cn_namelen == 2 &&
1141 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1142 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1147 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1148 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1151 if (fvp->v_mount != tdvp->v_mount) {
1152 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1156 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1161 * If source and destination are the same, there is nothing to
1162 * do. Set error to -1 to indicate this.
1167 if (ndflag & ND_NFSV4) {
1168 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1169 error = nfsrv_checkremove(fvp, 0, p);
1170 NFSVOPUNLOCK(fvp, 0);
1174 error = nfsrv_checkremove(tvp, 1, p);
1177 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1178 * that the NFSv4 client won't be confused by the rename.
1179 * Since nfsd_recalldelegation() can only be called on an
1180 * unlocked vnode at this point and fvp is the file that will
1181 * still exist after the rename, just do fvp.
1183 nfsd_recalldelegation(fvp, p);
1187 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1188 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1197 vrele(fromndp->ni_dvp);
1202 vrele(tondp->ni_startdir);
1203 nfsvno_relpathbuf(tondp);
1205 vrele(fromndp->ni_startdir);
1206 nfsvno_relpathbuf(fromndp);
1215 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1216 struct thread *p, struct nfsexstuff *exp)
1226 if (vp->v_mount != xp->v_mount)
1230 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1231 if ((vp->v_iflag & VI_DOOMED) == 0)
1232 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1235 if (ndp->ni_dvp == vp)
1239 NFSVOPUNLOCK(vp, 0);
1241 if (ndp->ni_dvp == ndp->ni_vp)
1248 nfsvno_relpathbuf(ndp);
1254 * Do the fsync() appropriate for the commit.
1257 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1263 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1264 * file is done. At this time VOP_FSYNC does not accept offset and
1265 * byte count parameters so call VOP_FSYNC the whole file for now.
1266 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1268 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
1270 * Give up and do the whole thing
1273 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1274 VM_OBJECT_LOCK(vp->v_object);
1275 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1276 VM_OBJECT_UNLOCK(vp->v_object);
1278 error = VOP_FSYNC(vp, MNT_WAIT, td);
1281 * Locate and synchronously write any buffers that fall
1282 * into the requested range. Note: we are assuming that
1283 * f_iosize is a power of 2.
1285 int iosize = vp->v_mount->mnt_stat.f_iosize;
1286 int iomask = iosize - 1;
1291 * Align to iosize boundry, super-align to page boundry.
1294 cnt += off & iomask;
1295 off &= ~(u_quad_t)iomask;
1297 if (off & PAGE_MASK) {
1298 cnt += off & PAGE_MASK;
1299 off &= ~(u_quad_t)PAGE_MASK;
1301 lblkno = off / iosize;
1304 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1305 VM_OBJECT_LOCK(vp->v_object);
1306 vm_object_page_clean(vp->v_object, off, off + cnt,
1308 VM_OBJECT_UNLOCK(vp->v_object);
1317 * If we have a buffer and it is marked B_DELWRI we
1318 * have to lock and write it. Otherwise the prior
1319 * write is assumed to have already been committed.
1321 * gbincore() can return invalid buffers now so we
1322 * have to check that bit as well (though B_DELWRI
1323 * should not be set if B_INVAL is set there could be
1324 * a race here since we haven't locked the buffer).
1326 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1327 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1328 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1330 continue; /* retry */
1332 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1335 bp->b_flags &= ~B_ASYNC;
1358 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1362 error = VFS_STATFS(vp->v_mount, sf);
1365 * Since NFS handles these values as unsigned on the
1366 * wire, there is no way to represent negative values,
1367 * so set them to 0. Without this, they will appear
1368 * to be very large positive values for clients like
1371 if (sf->f_bavail < 0)
1373 if (sf->f_ffree < 0)
1381 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1382 * must handle nfsrv_opencheck() calls after any other access checks.
1385 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1386 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1387 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1388 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1389 struct nfsexstuff *exp, struct vnode **vpp)
1391 struct vnode *vp = NULL;
1393 struct nfsexstuff nes;
1395 if (ndp->ni_vp == NULL)
1396 nd->nd_repstat = nfsrv_opencheck(clientid,
1397 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1398 if (!nd->nd_repstat) {
1399 if (ndp->ni_vp == NULL) {
1400 vrele(ndp->ni_startdir);
1401 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1402 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1404 nfsvno_relpathbuf(ndp);
1405 if (!nd->nd_repstat) {
1406 if (*exclusive_flagp) {
1407 *exclusive_flagp = 0;
1408 NFSVNO_ATTRINIT(nvap);
1409 nvap->na_atime.tv_sec = cverf[0];
1410 nvap->na_atime.tv_nsec = cverf[1];
1411 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1412 &nvap->na_vattr, cred);
1414 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1415 aclp, p, attrbitp, exp);
1420 if (ndp->ni_startdir)
1421 vrele(ndp->ni_startdir);
1422 nfsvno_relpathbuf(ndp);
1424 if (create == NFSV4OPEN_CREATE) {
1425 if (ndp->ni_dvp == vp)
1430 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1431 if (ndp->ni_cnd.cn_flags & RDONLY)
1432 NFSVNO_SETEXRDONLY(&nes);
1434 NFSVNO_EXINIT(&nes);
1435 nd->nd_repstat = nfsvno_accchk(vp,
1436 VWRITE, cred, &nes, p,
1437 NFSACCCHK_NOOVERRIDE,
1438 NFSACCCHK_VPISLOCKED, NULL);
1439 nd->nd_repstat = nfsrv_opencheck(clientid,
1440 stateidp, stp, vp, nd, p, nd->nd_repstat);
1441 if (!nd->nd_repstat) {
1442 tempsize = nvap->na_size;
1443 NFSVNO_ATTRINIT(nvap);
1444 nvap->na_size = tempsize;
1445 nd->nd_repstat = VOP_SETATTR(vp,
1446 &nvap->na_vattr, cred);
1448 } else if (vp->v_type == VREG) {
1449 nd->nd_repstat = nfsrv_opencheck(clientid,
1450 stateidp, stp, vp, nd, p, nd->nd_repstat);
1454 if (ndp->ni_cnd.cn_flags & HASBUF)
1455 nfsvno_relpathbuf(ndp);
1456 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1457 vrele(ndp->ni_startdir);
1458 if (ndp->ni_dvp == ndp->ni_vp)
1468 NFSEXITCODE2(0, nd);
1472 * Updates the file rev and sets the mtime and ctime
1473 * to the current clock time, returning the va_filerev and va_Xtime
1475 * Return ESTALE to indicate the vnode is VI_DOOMED.
1478 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1479 struct ucred *cred, struct thread *p)
1484 vfs_timestamp(&va.va_mtime);
1485 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1486 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1487 if ((vp->v_iflag & VI_DOOMED) != 0)
1490 (void) VOP_SETATTR(vp, &va, cred);
1491 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1496 * Glue routine to nfsv4_fillattr().
1499 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1500 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1501 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1502 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1506 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1507 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1509 NFSEXITCODE2(0, nd);
1513 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1515 * nfs readdir service
1516 * - mallocs what it thinks is enough to read
1517 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1518 * - calls VOP_READDIR()
1519 * - loops around building the reply
1520 * if the output generated exceeds count break out of loop
1521 * The NFSM_CLGET macro is used here so that the reply will be packed
1522 * tightly in mbuf clusters.
1523 * - it trims out records with d_fileno == 0
1524 * this doesn't matter for Unix clients, but they might confuse clients
1526 * - it trims out records with d_type == DT_WHT
1527 * these cannot be seen through NFS (unless we extend the protocol)
1528 * The alternate call nfsrvd_readdirplus() does lookups as well.
1529 * PS: The NFS protocol spec. does not clarify what the "count" byte
1530 * argument is a count of.. just name strings and file id's or the
1531 * entire reply rpc or ...
1532 * I tried just file name and id sizes and it confused the Sun client,
1533 * so I am using the full rpc size now. The "paranoia.." comment refers
1534 * to including the status longwords that are not a part of the dir.
1535 * "entry" structures, but are in the rpc.
1538 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1539 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1544 char *cpos, *cend, *rbuf;
1546 int nlen, error = 0, getret = 1;
1547 int siz, cnt, fullsiz, eofflag, ncookies;
1548 u_int64_t off, toff, verf;
1549 u_long *cookies = NULL, *cookiep;
1554 if (nd->nd_repstat) {
1555 nfsrv_postopattr(nd, getret, &at);
1558 if (nd->nd_flag & ND_NFSV2) {
1559 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1560 off = fxdr_unsigned(u_quad_t, *tl++);
1562 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1563 off = fxdr_hyper(tl);
1565 verf = fxdr_hyper(tl);
1569 cnt = fxdr_unsigned(int, *tl);
1570 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1571 cnt = NFS_SRVMAXDATA(nd);
1572 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1574 if (nd->nd_flag & ND_NFSV3) {
1575 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1579 * va_filerev is not sufficient as a cookie verifier,
1580 * since it is not supposed to change when entries are
1581 * removed/added unless that offset cookies returned to
1582 * the client are no longer valid.
1584 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1585 nd->nd_repstat = NFSERR_BAD_COOKIE;
1588 if (!nd->nd_repstat && vp->v_type != VDIR)
1589 nd->nd_repstat = NFSERR_NOTDIR;
1590 if (nd->nd_repstat == 0 && cnt == 0) {
1591 if (nd->nd_flag & ND_NFSV2)
1592 /* NFSv2 does not have NFSERR_TOOSMALL */
1593 nd->nd_repstat = EPERM;
1595 nd->nd_repstat = NFSERR_TOOSMALL;
1597 if (!nd->nd_repstat)
1598 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1599 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1600 NFSACCCHK_VPISLOCKED, NULL);
1601 if (nd->nd_repstat) {
1603 if (nd->nd_flag & ND_NFSV3)
1604 nfsrv_postopattr(nd, getret, &at);
1607 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1608 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1612 free((caddr_t)cookies, M_TEMP);
1620 io.uio_offset = (off_t)off;
1622 io.uio_segflg = UIO_SYSSPACE;
1623 io.uio_rw = UIO_READ;
1625 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1627 off = (u_int64_t)io.uio_offset;
1629 siz -= io.uio_resid;
1631 if (!cookies && !nd->nd_repstat)
1632 nd->nd_repstat = NFSERR_PERM;
1633 if (nd->nd_flag & ND_NFSV3) {
1634 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1635 if (!nd->nd_repstat)
1636 nd->nd_repstat = getret;
1640 * Handles the failed cases. nd->nd_repstat == 0 past here.
1642 if (nd->nd_repstat) {
1644 free((caddr_t)rbuf, M_TEMP);
1646 free((caddr_t)cookies, M_TEMP);
1647 if (nd->nd_flag & ND_NFSV3)
1648 nfsrv_postopattr(nd, getret, &at);
1652 * If nothing read, return eof
1657 if (nd->nd_flag & ND_NFSV2) {
1658 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1660 nfsrv_postopattr(nd, getret, &at);
1661 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1662 txdr_hyper(at.na_filerev, tl);
1665 *tl++ = newnfs_false;
1667 FREE((caddr_t)rbuf, M_TEMP);
1668 FREE((caddr_t)cookies, M_TEMP);
1673 * Check for degenerate cases of nothing useful read.
1674 * If so go try again
1678 dp = (struct dirent *)cpos;
1682 * For some reason FreeBSD's ufs_readdir() chooses to back the
1683 * directory offset up to a block boundary, so it is necessary to
1684 * skip over the records that precede the requested offset. This
1685 * requires the assumption that file offset cookies monotonically
1687 * Since the offset cookies don't monotonically increase for ZFS,
1688 * this is not done when ZFS is the file system.
1690 while (cpos < cend && ncookies > 0 &&
1691 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1692 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1693 cpos += dp->d_reclen;
1694 dp = (struct dirent *)cpos;
1698 if (cpos >= cend || ncookies == 0) {
1706 * dirlen is the size of the reply, including all XDR and must
1707 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1708 * if the XDR should be included in "count", but to be safe, we do.
1709 * (Include the two booleans at the end of the reply in dirlen now.)
1711 if (nd->nd_flag & ND_NFSV3) {
1712 nfsrv_postopattr(nd, getret, &at);
1713 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1714 txdr_hyper(at.na_filerev, tl);
1715 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1717 dirlen = 2 * NFSX_UNSIGNED;
1720 /* Loop through the records and build reply */
1721 while (cpos < cend && ncookies > 0) {
1722 nlen = dp->d_namlen;
1723 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1724 nlen <= NFS_MAXNAMLEN) {
1725 if (nd->nd_flag & ND_NFSV3)
1726 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1728 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1735 * Build the directory record xdr from
1738 if (nd->nd_flag & ND_NFSV3) {
1739 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1740 *tl++ = newnfs_true;
1743 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1744 *tl++ = newnfs_true;
1746 *tl = txdr_unsigned(dp->d_fileno);
1747 (void) nfsm_strtom(nd, dp->d_name, nlen);
1748 if (nd->nd_flag & ND_NFSV3) {
1749 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1752 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1753 *tl = txdr_unsigned(*cookiep);
1755 cpos += dp->d_reclen;
1756 dp = (struct dirent *)cpos;
1762 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1763 *tl++ = newnfs_false;
1768 FREE((caddr_t)rbuf, M_TEMP);
1769 FREE((caddr_t)cookies, M_TEMP);
1772 NFSEXITCODE2(0, nd);
1776 NFSEXITCODE2(error, nd);
1781 * Readdirplus for V3 and Readdir for V4.
1784 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1785 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1790 char *cpos, *cend, *rbuf;
1793 struct nfsvattr nva, at, *nvap = &nva;
1794 struct mbuf *mb0, *mb1;
1795 struct nfsreferral *refp;
1796 int nlen, r, error = 0, getret = 1, usevget = 1;
1797 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1798 caddr_t bpos0, bpos1;
1799 u_int64_t off, toff, verf;
1800 u_long *cookies = NULL, *cookiep;
1801 nfsattrbit_t attrbits, rderrbits, savbits;
1804 struct componentname cn;
1805 int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1806 struct mount *mp, *new_mp;
1807 uint64_t mounted_on_fileno;
1809 if (nd->nd_repstat) {
1810 nfsrv_postopattr(nd, getret, &at);
1813 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1814 off = fxdr_hyper(tl);
1817 verf = fxdr_hyper(tl);
1819 siz = fxdr_unsigned(int, *tl++);
1820 cnt = fxdr_unsigned(int, *tl);
1823 * Use the server's maximum data transfer size as the upper bound
1826 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1827 cnt = NFS_SRVMAXDATA(nd);
1830 * siz is a "hint" of how much directory information (name, fileid,
1831 * cookie) should be in the reply. At least one client "hints" 0,
1832 * so I set it to cnt for that case. I also round it up to the
1833 * next multiple of DIRBLKSIZ.
1837 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1839 if (nd->nd_flag & ND_NFSV4) {
1840 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1843 NFSSET_ATTRBIT(&savbits, &attrbits);
1844 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1845 NFSZERO_ATTRBIT(&rderrbits);
1846 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1848 NFSZERO_ATTRBIT(&attrbits);
1851 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1852 if (!nd->nd_repstat) {
1853 if (off && verf != at.na_filerev) {
1855 * va_filerev is not sufficient as a cookie verifier,
1856 * since it is not supposed to change when entries are
1857 * removed/added unless that offset cookies returned to
1858 * the client are no longer valid.
1861 if (nd->nd_flag & ND_NFSV4) {
1862 nd->nd_repstat = NFSERR_NOTSAME;
1864 nd->nd_repstat = NFSERR_BAD_COOKIE;
1867 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1868 nd->nd_repstat = NFSERR_BAD_COOKIE;
1871 if (!nd->nd_repstat && vp->v_type != VDIR)
1872 nd->nd_repstat = NFSERR_NOTDIR;
1873 if (!nd->nd_repstat && cnt == 0)
1874 nd->nd_repstat = NFSERR_TOOSMALL;
1875 if (!nd->nd_repstat)
1876 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1877 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1878 NFSACCCHK_VPISLOCKED, NULL);
1879 if (nd->nd_repstat) {
1881 if (nd->nd_flag & ND_NFSV3)
1882 nfsrv_postopattr(nd, getret, &at);
1885 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1887 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1891 free((caddr_t)cookies, M_TEMP);
1899 io.uio_offset = (off_t)off;
1901 io.uio_segflg = UIO_SYSSPACE;
1902 io.uio_rw = UIO_READ;
1904 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1906 off = (u_int64_t)io.uio_offset;
1908 siz -= io.uio_resid;
1910 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1912 if (!cookies && !nd->nd_repstat)
1913 nd->nd_repstat = NFSERR_PERM;
1914 if (!nd->nd_repstat)
1915 nd->nd_repstat = getret;
1916 if (nd->nd_repstat) {
1919 free((caddr_t)cookies, M_TEMP);
1920 free((caddr_t)rbuf, M_TEMP);
1921 if (nd->nd_flag & ND_NFSV3)
1922 nfsrv_postopattr(nd, getret, &at);
1926 * If nothing read, return eof
1931 if (nd->nd_flag & ND_NFSV3)
1932 nfsrv_postopattr(nd, getret, &at);
1933 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1934 txdr_hyper(at.na_filerev, tl);
1936 *tl++ = newnfs_false;
1938 free((caddr_t)cookies, M_TEMP);
1939 free((caddr_t)rbuf, M_TEMP);
1944 * Check for degenerate cases of nothing useful read.
1945 * If so go try again
1949 dp = (struct dirent *)cpos;
1953 * For some reason FreeBSD's ufs_readdir() chooses to back the
1954 * directory offset up to a block boundary, so it is necessary to
1955 * skip over the records that precede the requested offset. This
1956 * requires the assumption that file offset cookies monotonically
1958 * Since the offset cookies don't monotonically increase for ZFS,
1959 * this is not done when ZFS is the file system.
1961 while (cpos < cend && ncookies > 0 &&
1962 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1963 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1964 ((nd->nd_flag & ND_NFSV4) &&
1965 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1966 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1967 cpos += dp->d_reclen;
1968 dp = (struct dirent *)cpos;
1972 if (cpos >= cend || ncookies == 0) {
1979 * Busy the file system so that the mount point won't go away
1980 * and, as such, VFS_VGET() can be used safely.
1984 NFSVOPUNLOCK(vp, 0);
1985 nd->nd_repstat = vfs_busy(mp, 0);
1987 if (nd->nd_repstat != 0) {
1989 free(cookies, M_TEMP);
1991 if (nd->nd_flag & ND_NFSV3)
1992 nfsrv_postopattr(nd, getret, &at);
1997 * Check to see if entries in this directory can be safely acquired
1998 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
1999 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2000 * automount of the snapshot directory that is required will
2002 * This needs to be done here for NFSv4, since NFSv4 never does
2003 * a VFS_VGET() for "." or "..".
2006 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2007 if (r == EOPNOTSUPP) {
2009 cn.cn_nameiop = LOOKUP;
2010 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2011 cn.cn_cred = nd->nd_cred;
2018 * Save this position, in case there is an error before one entry
2022 bpos0 = nd->nd_bpos;
2025 * Fill in the first part of the reply.
2026 * dirlen is the reply length in bytes and cannot exceed cnt.
2027 * (Include the two booleans at the end of the reply in dirlen now,
2028 * so we recognize when we have exceeded cnt.)
2030 if (nd->nd_flag & ND_NFSV3) {
2031 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2032 nfsrv_postopattr(nd, getret, &at);
2034 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2036 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2037 txdr_hyper(at.na_filerev, tl);
2040 * Save this position, in case there is an empty reply needed.
2043 bpos1 = nd->nd_bpos;
2045 /* Loop through the records and build reply */
2047 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2048 nlen = dp->d_namlen;
2049 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2050 nlen <= NFS_MAXNAMLEN &&
2051 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2052 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2053 || (nlen == 1 && dp->d_name[0] != '.'))) {
2055 * Save the current position in the reply, in case
2056 * this entry exceeds cnt.
2059 bpos1 = nd->nd_bpos;
2062 * For readdir_and_lookup get the vnode using
2071 mounted_on_fileno = (uint64_t)dp->d_fileno;
2072 if ((nd->nd_flag & ND_NFSV3) ||
2073 NFSNONZERO_ATTRBIT(&savbits)) {
2074 if (nd->nd_flag & ND_NFSV4)
2075 refp = nfsv4root_getreferral(NULL,
2079 r = VFS_VGET(mp, dp->d_fileno,
2083 if (r == EOPNOTSUPP) {
2086 cn.cn_nameiop = LOOKUP;
2094 cn.cn_nameptr = dp->d_name;
2095 cn.cn_namelen = nlen;
2096 cn.cn_flags = ISLASTCN |
2097 NOFOLLOW | LOCKLEAF |
2100 dp->d_name[0] == '.' &&
2101 dp->d_name[1] == '.')
2104 if (NFSVOPLOCK(vp, LK_SHARED)
2106 nd->nd_repstat = EPERM;
2109 if ((vp->v_vflag & VV_ROOT) != 0
2110 && (cn.cn_flags & ISDOTDOT)
2116 r = VOP_LOOKUP(vp, &nvp,
2125 * For NFSv4, check to see if nvp is
2126 * a mount point and get the mount
2127 * point vnode, as required.
2130 nfsrv_enable_crossmntpt != 0 &&
2131 (nd->nd_flag & ND_NFSV4) != 0 &&
2132 nvp->v_type == VDIR &&
2133 nvp->v_mountedhere != NULL) {
2134 new_mp = nvp->v_mountedhere;
2135 r = vfs_busy(new_mp, 0);
2139 r = VFS_ROOT(new_mp,
2149 ((nd->nd_flag & ND_NFSV3) ||
2150 NFSNONZERO_ATTRBIT(&attrbits))) {
2151 r = nfsvno_getfh(nvp, &nfh, p);
2153 r = nfsvno_getattr(nvp, nvap,
2155 if (r == 0 && not_zfs == 0 &&
2156 nfsrv_enable_crossmntpt != 0 &&
2157 (nd->nd_flag & ND_NFSV4) != 0 &&
2158 nvp->v_type == VDIR &&
2159 vp->v_mount != nvp->v_mount) {
2161 * For a ZFS snapshot, there is a
2162 * pseudo mount that does not set
2163 * v_mountedhere, so it needs to
2164 * be detected via a different
2169 new_mp = nvp->v_mount;
2176 if (!NFSISSET_ATTRBIT(&attrbits,
2177 NFSATTRBIT_RDATTRERROR)) {
2180 if (needs_unbusy != 0)
2189 * Build the directory record xdr
2191 if (nd->nd_flag & ND_NFSV3) {
2192 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2193 *tl++ = newnfs_true;
2195 *tl = txdr_unsigned(dp->d_fileno);
2196 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2197 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2199 *tl = txdr_unsigned(*cookiep);
2200 nfsrv_postopattr(nd, 0, nvap);
2201 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2202 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2206 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2207 *tl++ = newnfs_true;
2209 *tl = txdr_unsigned(*cookiep);
2210 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2212 supports_nfsv4acls =
2213 nfs_supportsnfsv4acls(nvp);
2214 NFSVOPUNLOCK(nvp, 0);
2216 supports_nfsv4acls = 0;
2218 dirlen += nfsrv_putreferralattr(nd,
2221 if (nd->nd_repstat) {
2224 if (needs_unbusy != 0)
2229 dirlen += nfsvno_fillattr(nd, new_mp,
2230 nvp, nvap, &nfh, r, &rderrbits,
2231 nd->nd_cred, p, isdgram, 0,
2232 supports_nfsv4acls, at_root,
2235 dirlen += nfsvno_fillattr(nd, new_mp,
2236 nvp, nvap, &nfh, r, &attrbits,
2237 nd->nd_cred, p, isdgram, 0,
2238 supports_nfsv4acls, at_root,
2243 dirlen += (3 * NFSX_UNSIGNED);
2245 if (needs_unbusy != 0)
2250 cpos += dp->d_reclen;
2251 dp = (struct dirent *)cpos;
2259 * If dirlen > cnt, we must strip off the last entry. If that
2260 * results in an empty reply, report NFSERR_TOOSMALL.
2262 if (dirlen > cnt || nd->nd_repstat) {
2263 if (!nd->nd_repstat && entrycnt == 0)
2264 nd->nd_repstat = NFSERR_TOOSMALL;
2266 newnfs_trimtrailing(nd, mb0, bpos0);
2268 newnfs_trimtrailing(nd, mb1, bpos1);
2270 } else if (cpos < cend)
2272 if (!nd->nd_repstat) {
2273 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2274 *tl++ = newnfs_false;
2280 FREE((caddr_t)cookies, M_TEMP);
2281 FREE((caddr_t)rbuf, M_TEMP);
2284 NFSEXITCODE2(0, nd);
2288 NFSEXITCODE2(error, nd);
2293 * Get the settable attributes out of the mbuf list.
2294 * (Return 0 or EBADRPC)
2297 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2298 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2301 struct nfsv2_sattr *sp;
2302 int error = 0, toclient = 0;
2304 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2306 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2308 * Some old clients didn't fill in the high order 16bits.
2309 * --> check the low order 2 bytes for 0xffff
2311 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2312 nvap->na_mode = nfstov_mode(sp->sa_mode);
2313 if (sp->sa_uid != newnfs_xdrneg1)
2314 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2315 if (sp->sa_gid != newnfs_xdrneg1)
2316 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2317 if (sp->sa_size != newnfs_xdrneg1)
2318 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2319 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2321 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2323 nvap->na_atime.tv_sec =
2324 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2325 nvap->na_atime.tv_nsec = 0;
2328 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2329 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2332 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2333 if (*tl == newnfs_true) {
2334 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2335 nvap->na_mode = nfstov_mode(*tl);
2337 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2338 if (*tl == newnfs_true) {
2339 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2340 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2342 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2343 if (*tl == newnfs_true) {
2344 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2345 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2347 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2348 if (*tl == newnfs_true) {
2349 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2350 nvap->na_size = fxdr_hyper(tl);
2352 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2353 switch (fxdr_unsigned(int, *tl)) {
2354 case NFSV3SATTRTIME_TOCLIENT:
2355 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2356 fxdr_nfsv3time(tl, &nvap->na_atime);
2359 case NFSV3SATTRTIME_TOSERVER:
2360 vfs_timestamp(&nvap->na_atime);
2361 nvap->na_vaflags |= VA_UTIMES_NULL;
2364 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2365 switch (fxdr_unsigned(int, *tl)) {
2366 case NFSV3SATTRTIME_TOCLIENT:
2367 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2368 fxdr_nfsv3time(tl, &nvap->na_mtime);
2369 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2371 case NFSV3SATTRTIME_TOSERVER:
2372 vfs_timestamp(&nvap->na_mtime);
2374 nvap->na_vaflags |= VA_UTIMES_NULL;
2379 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2382 NFSEXITCODE2(error, nd);
2387 * Handle the setable attributes for V4.
2388 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2391 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2392 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2397 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2399 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2403 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2406 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2407 attrsize = fxdr_unsigned(int, *tl);
2410 * Loop around getting the setable attributes. If an unsupported
2411 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2414 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2415 bitpos = NFSATTRBIT_MAX;
2419 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2420 if (attrsum > attrsize) {
2421 error = NFSERR_BADXDR;
2424 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2426 case NFSATTRBIT_SIZE:
2427 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2428 nvap->na_size = fxdr_hyper(tl);
2429 attrsum += NFSX_HYPER;
2431 case NFSATTRBIT_ACL:
2432 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2436 if (aceerr && !nd->nd_repstat)
2437 nd->nd_repstat = aceerr;
2440 case NFSATTRBIT_ARCHIVE:
2441 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2442 if (!nd->nd_repstat)
2443 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2444 attrsum += NFSX_UNSIGNED;
2446 case NFSATTRBIT_HIDDEN:
2447 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2448 if (!nd->nd_repstat)
2449 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2450 attrsum += NFSX_UNSIGNED;
2452 case NFSATTRBIT_MIMETYPE:
2453 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2454 i = fxdr_unsigned(int, *tl);
2455 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2458 if (!nd->nd_repstat)
2459 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2460 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2462 case NFSATTRBIT_MODE:
2463 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2464 nvap->na_mode = nfstov_mode(*tl);
2465 attrsum += NFSX_UNSIGNED;
2467 case NFSATTRBIT_OWNER:
2468 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2469 j = fxdr_unsigned(int, *tl);
2471 error = NFSERR_BADXDR;
2474 if (j > NFSV4_SMALLSTR)
2475 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2478 error = nfsrv_mtostr(nd, cp, j);
2480 if (j > NFSV4_SMALLSTR)
2481 free(cp, M_NFSSTRING);
2484 if (!nd->nd_repstat) {
2485 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2487 if (!nd->nd_repstat)
2490 if (j > NFSV4_SMALLSTR)
2491 free(cp, M_NFSSTRING);
2492 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2494 case NFSATTRBIT_OWNERGROUP:
2495 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2496 j = fxdr_unsigned(int, *tl);
2498 error = NFSERR_BADXDR;
2501 if (j > NFSV4_SMALLSTR)
2502 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2505 error = nfsrv_mtostr(nd, cp, j);
2507 if (j > NFSV4_SMALLSTR)
2508 free(cp, M_NFSSTRING);
2511 if (!nd->nd_repstat) {
2512 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2514 if (!nd->nd_repstat)
2517 if (j > NFSV4_SMALLSTR)
2518 free(cp, M_NFSSTRING);
2519 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2521 case NFSATTRBIT_SYSTEM:
2522 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2523 if (!nd->nd_repstat)
2524 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2525 attrsum += NFSX_UNSIGNED;
2527 case NFSATTRBIT_TIMEACCESSSET:
2528 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2529 attrsum += NFSX_UNSIGNED;
2530 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2531 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2532 fxdr_nfsv4time(tl, &nvap->na_atime);
2534 attrsum += NFSX_V4TIME;
2536 vfs_timestamp(&nvap->na_atime);
2537 nvap->na_vaflags |= VA_UTIMES_NULL;
2540 case NFSATTRBIT_TIMEBACKUP:
2541 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2542 if (!nd->nd_repstat)
2543 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2544 attrsum += NFSX_V4TIME;
2546 case NFSATTRBIT_TIMECREATE:
2547 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2548 if (!nd->nd_repstat)
2549 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2550 attrsum += NFSX_V4TIME;
2552 case NFSATTRBIT_TIMEMODIFYSET:
2553 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2554 attrsum += NFSX_UNSIGNED;
2555 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2556 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2557 fxdr_nfsv4time(tl, &nvap->na_mtime);
2558 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2559 attrsum += NFSX_V4TIME;
2561 vfs_timestamp(&nvap->na_mtime);
2563 nvap->na_vaflags |= VA_UTIMES_NULL;
2567 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2569 * set bitpos so we drop out of the loop.
2571 bitpos = NFSATTRBIT_MAX;
2577 * some clients pad the attrlist, so we need to skip over the
2580 if (attrsum > attrsize) {
2581 error = NFSERR_BADXDR;
2583 attrsize = NFSM_RNDUP(attrsize);
2584 if (attrsum < attrsize)
2585 error = nfsm_advance(nd, attrsize - attrsum, -1);
2588 NFSEXITCODE2(error, nd);
2593 * Check/setup export credentials.
2596 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2597 struct ucred *credanon)
2602 * Check/setup credentials.
2604 if (nd->nd_flag & ND_GSS)
2605 exp->nes_exflag &= ~MNT_EXPORTANON;
2608 * Check to see if the operation is allowed for this security flavor.
2609 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2610 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2611 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2613 if (nfsvno_testexp(nd, exp) &&
2614 nd->nd_procnum != NFSV4OP_SECINFO &&
2615 nd->nd_procnum != NFSPROC_FSINFO) {
2616 if (nd->nd_flag & ND_NFSV4)
2617 error = NFSERR_WRONGSEC;
2619 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2624 * Check to see if the file system is exported V4 only.
2626 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2627 error = NFSERR_PROGNOTV4;
2632 * Now, map the user credentials.
2633 * (Note that ND_AUTHNONE will only be set for an NFSv3
2634 * Fsinfo RPC. If set for anything else, this code might need
2637 if (NFSVNO_EXPORTED(exp) &&
2638 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2639 NFSVNO_EXPORTANON(exp) ||
2640 (nd->nd_flag & ND_AUTHNONE))) {
2641 nd->nd_cred->cr_uid = credanon->cr_uid;
2642 nd->nd_cred->cr_gid = credanon->cr_gid;
2643 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2644 credanon->cr_groups);
2648 NFSEXITCODE2(error, nd);
2656 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2657 struct ucred **credp)
2659 int i, error, *secflavors;
2661 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2662 &exp->nes_numsecflavor, &secflavors);
2664 if (nfs_rootfhset) {
2665 exp->nes_exflag = 0;
2666 exp->nes_numsecflavor = 0;
2670 /* Copy the security flavors. */
2671 for (i = 0; i < exp->nes_numsecflavor; i++)
2672 exp->nes_secflavors[i] = secflavors[i];
2679 * Get a vnode for a file handle and export stuff.
2682 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2683 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2684 struct ucred **credp)
2686 int i, error, *secflavors;
2689 exp->nes_numsecflavor = 0;
2690 if (VFS_NEEDSGIANT(mp))
2693 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp);
2695 /* Make sure the server replies ESTALE to the client. */
2697 if (nam && !error) {
2698 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2699 &exp->nes_numsecflavor, &secflavors);
2701 if (nfs_rootfhset) {
2702 exp->nes_exflag = 0;
2703 exp->nes_numsecflavor = 0;
2709 /* Copy the security flavors. */
2710 for (i = 0; i < exp->nes_numsecflavor; i++)
2711 exp->nes_secflavors[i] = secflavors[i];
2714 if (error == 0 && lktype == LK_SHARED)
2716 * It would be much better to pass lktype to VFS_FHTOVP(),
2717 * but this will have to do until VFS_FHTOVP() has a lock
2718 * type argument like VFS_VGET().
2720 NFSVOPLOCK(*vpp, LK_DOWNGRADE | LK_RETRY);
2727 * nfsd_fhtovp() - convert a fh to a vnode ptr
2728 * - look up fsid in mount list (if not found ret error)
2729 * - get vp and export rights by calling nfsvno_fhtovp()
2730 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2732 * - if mpp != NULL, return the mount point so that it can
2733 * be used for vn_finished_write() by the caller
2736 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2737 struct vnode **vpp, struct nfsexstuff *exp,
2738 struct mount **mpp, int startwrite, struct thread *p)
2741 struct ucred *credanon;
2744 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2746 * Check for the special case of the nfsv4root_fh.
2748 mp = vfs_busyfs(&fhp->fh_fsid);
2753 nd->nd_repstat = ESTALE;
2758 vn_start_write(NULL, mpp, V_WAIT);
2759 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2760 lktype = LK_EXCLUSIVE;
2762 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2767 * For NFSv4 without a pseudo root fs, unexported file handles
2768 * can be returned, so that Lookup works everywhere.
2770 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2771 !(nd->nd_flag & ND_NFSV4)) {
2773 nd->nd_repstat = EACCES;
2777 * Personally, I've never seen any point in requiring a
2778 * reserved port#, since only in the rare case where the
2779 * clients are all boxes with secure system priviledges,
2780 * does it provide any enhanced security, but... some people
2781 * believe it to be useful and keep putting this code back in.
2782 * (There is also some "security checker" out there that
2783 * complains if the nfs server doesn't enforce this.)
2784 * However, note the following:
2785 * RFC3530 (NFSv4) specifies that a reserved port# not be
2787 * RFC2623 recommends that, if a reserved port# is checked for,
2788 * that there be a way to turn that off--> ifdef'd.
2790 #ifdef NFS_REQRSVPORT
2791 if (!nd->nd_repstat) {
2792 struct sockaddr_in *saddr;
2793 struct sockaddr_in6 *saddr6;
2795 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2796 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2797 if (!(nd->nd_flag & ND_NFSV4) &&
2798 ((saddr->sin_family == AF_INET &&
2799 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2800 (saddr6->sin6_family == AF_INET6 &&
2801 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2803 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2806 #endif /* NFS_REQRSVPORT */
2809 * Check/setup credentials.
2811 if (!nd->nd_repstat) {
2812 nd->nd_saveduid = nd->nd_cred->cr_uid;
2813 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2817 if (credanon != NULL)
2819 if (nd->nd_repstat) {
2821 vn_finished_write(mp);
2828 NFSEXITCODE2(0, nd);
2835 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2837 struct filedesc *fdp;
2841 fdp = p->td_proc->p_fd;
2842 if (fd >= fdp->fd_nfiles ||
2843 (fp = fdp->fd_ofiles[fd]) == NULL) {
2855 * Called from nfssvc() to update the exports list. Just call
2856 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2857 * in the mount list.
2860 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2862 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2864 struct nameidata nd;
2867 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2868 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2870 else if (error == 0) {
2871 if (nfsexargp->fspec == NULL) {
2876 * If fspec != NULL, this is the v4root path.
2878 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2879 nfsexargp->fspec, p);
2880 if ((error = namei(&nd)) != 0)
2882 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2885 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2886 NFSBCOPY((caddr_t)&fh,
2887 nfs_rootfh.nfsrvfh_data,
2888 sizeof (fhandle_t));
2899 * This function needs to test to see if the system is near its limit
2900 * for memory allocation via malloc() or mget() and return True iff
2901 * either of these resources are near their limit.
2902 * XXX (For now, this is just a stub.)
2904 int nfsrv_testmalloclimit = 0;
2906 nfsrv_mallocmget_limit(void)
2908 static int printmesg = 0;
2909 static int testval = 1;
2911 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2912 if ((printmesg++ % 100) == 0)
2913 printf("nfsd: malloc/mget near limit\n");
2920 * BSD specific initialization of a mount point.
2925 static int inited = 0;
2930 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2931 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2932 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2933 nfsv4root_mnt.mnt_export = NULL;
2934 TAILQ_INIT(&nfsv4root_opt);
2935 TAILQ_INIT(&nfsv4root_newopt);
2936 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2937 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2938 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2939 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2943 * Get a vnode for a file handle, without checking exports, etc.
2946 nfsvno_getvp(fhandle_t *fhp)
2952 mp = vfs_busyfs(&fhp->fh_fsid);
2955 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2963 * Do a local VOP_ADVLOCK().
2966 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2967 u_int64_t end, struct thread *td)
2973 if (nfsrv_dolocallocks == 0)
2976 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2977 if ((vp->v_iflag & VI_DOOMED) != 0) {
2982 fl.l_whence = SEEK_SET;
2984 fl.l_start = (off_t)first;
2985 if (end == NFS64BITSSET) {
2989 fl.l_len = (off_t)tlen;
2992 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2993 * values for all calls, so that all locks will be held by the
2994 * nfsd server. (The nfsd server handles conflicts between the
2996 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2997 * bytes, so it can't be put in l_sysid.
2999 if (nfsv4_sysid == 0)
3000 nfsv4_sysid = nlm_acquire_next_sysid();
3001 fl.l_pid = (pid_t)0;
3002 fl.l_sysid = (int)nfsv4_sysid;
3004 NFSVOPUNLOCK(vp, 0);
3005 if (ftype == F_UNLCK)
3006 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3007 (F_POSIX | F_REMOTE));
3009 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3010 (F_POSIX | F_REMOTE));
3011 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3019 * Check the nfsv4 root exports.
3022 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3024 struct ucred *credanon;
3025 int exflags, error = 0, numsecflavor, *secflavors, i;
3027 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3028 &credanon, &numsecflavor, &secflavors);
3030 error = NFSERR_PROGUNAVAIL;
3033 if (credanon != NULL)
3035 for (i = 0; i < numsecflavor; i++) {
3036 if (secflavors[i] == AUTH_SYS)
3037 nd->nd_flag |= ND_EXAUTHSYS;
3038 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3039 nd->nd_flag |= ND_EXGSS;
3040 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3041 nd->nd_flag |= ND_EXGSSINTEGRITY;
3042 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3043 nd->nd_flag |= ND_EXGSSPRIVACY;
3052 * Nfs server psuedo system call for the nfsd's
3058 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3061 struct nfsd_addsock_args sockarg;
3062 struct nfsd_nfsd_args nfsdarg;
3065 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3066 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3070 * Since we don't know what rights might be required,
3071 * pretend that we need them all. It is better to be too
3072 * careful than too reckless.
3074 if ((error = fget(td, sockarg.sock, CAP_SOCK_ALL, &fp)) != 0)
3076 if (fp->f_type != DTYPE_SOCKET) {
3081 error = nfsrvd_addsock(fp);
3083 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3084 if (uap->argp == NULL) {
3088 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3092 error = nfsrvd_nfsd(td, &nfsdarg);
3094 error = nfssvc_srvcall(td, uap, td->td_ucred);
3103 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3105 struct nfsex_args export;
3106 struct file *fp = NULL;
3108 struct nfsd_clid adminrevoke;
3109 struct nfsd_dumplist dumplist;
3110 struct nfsd_dumpclients *dumpclients;
3111 struct nfsd_dumplocklist dumplocklist;
3112 struct nfsd_dumplocks *dumplocks;
3113 struct nameidata nd;
3115 int error = EINVAL, igotlock;
3117 static int suspend_nfsd = 0;
3119 if (uap->flag & NFSSVC_PUBLICFH) {
3120 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3121 sizeof (fhandle_t));
3122 error = copyin(uap->argp,
3123 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3126 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3127 error = copyin(uap->argp,(caddr_t)&export,
3128 sizeof (struct nfsex_args));
3130 error = nfsrv_v4rootexport(&export, cred, p);
3131 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3134 } else if (uap->flag & NFSSVC_STABLERESTART) {
3135 error = copyin(uap->argp, (caddr_t)&stablefd,
3138 error = fp_getfvp(p, stablefd, &fp, &vp);
3139 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3141 if (!error && newnfs_numnfsd != 0)
3144 nfsrv_stablefirst.nsf_fp = fp;
3145 nfsrv_setupstable(p);
3147 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3148 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3149 sizeof (struct nfsd_clid));
3151 error = nfsrv_adminrevoke(&adminrevoke, p);
3152 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3153 error = copyin(uap->argp, (caddr_t)&dumplist,
3154 sizeof (struct nfsd_dumplist));
3155 if (!error && (dumplist.ndl_size < 1 ||
3156 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3159 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3160 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3162 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3163 error = copyout(dumpclients,
3164 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3165 free((caddr_t)dumpclients, M_TEMP);
3167 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3168 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3169 sizeof (struct nfsd_dumplocklist));
3170 if (!error && (dumplocklist.ndllck_size < 1 ||
3171 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3174 error = nfsrv_lookupfilename(&nd,
3175 dumplocklist.ndllck_fname, p);
3177 len = sizeof (struct nfsd_dumplocks) *
3178 dumplocklist.ndllck_size;
3179 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3181 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3182 dumplocklist.ndllck_size, p);
3184 error = copyout(dumplocks,
3185 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3186 free((caddr_t)dumplocks, M_TEMP);
3188 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3191 nfsd_master_pid = procp->p_pid;
3192 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3193 nfsd_master_start = procp->p_stats->p_start;
3194 nfsd_master_proc = procp;
3196 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3197 NFSLOCKV4ROOTMUTEX();
3198 if (suspend_nfsd == 0) {
3199 /* Lock out all nfsd threads */
3201 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3202 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3203 } while (igotlock == 0 && suspend_nfsd == 0);
3206 NFSUNLOCKV4ROOTMUTEX();
3208 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3209 NFSLOCKV4ROOTMUTEX();
3210 if (suspend_nfsd != 0) {
3211 nfsv4_unlock(&nfsd_suspend_lock, 0);
3214 NFSUNLOCKV4ROOTMUTEX();
3224 * Returns 0 if ok, 1 otherwise.
3227 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3232 * This seems odd, but allow the case where the security flavor
3233 * list is empty. This happens when NFSv4 is traversing non-exported
3234 * file systems. Exported file systems should always have a non-empty
3235 * security flavor list.
3237 if (exp->nes_numsecflavor == 0)
3240 for (i = 0; i < exp->nes_numsecflavor; i++) {
3242 * The tests for privacy and integrity must be first,
3243 * since ND_GSS is set for everything but AUTH_SYS.
3245 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3246 (nd->nd_flag & ND_GSSPRIVACY))
3248 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3249 (nd->nd_flag & ND_GSSINTEGRITY))
3251 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3252 (nd->nd_flag & ND_GSS))
3254 if (exp->nes_secflavors[i] == AUTH_SYS &&
3255 (nd->nd_flag & ND_GSS) == 0)
3262 * Calculate a hash value for the fid in a file handle.
3265 nfsrv_hashfh(fhandle_t *fhp)
3269 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3274 * Signal the userland master nfsd to backup the stable restart file.
3277 nfsrv_backupstable(void)
3281 if (nfsd_master_proc != NULL) {
3282 procp = pfind(nfsd_master_pid);
3283 /* Try to make sure it is the correct process. */
3284 if (procp == nfsd_master_proc &&
3285 procp->p_stats->p_start.tv_sec ==
3286 nfsd_master_start.tv_sec &&
3287 procp->p_stats->p_start.tv_usec ==
3288 nfsd_master_start.tv_usec &&
3289 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3290 kern_psignal(procp, SIGUSR2);
3292 nfsd_master_proc = NULL;
3299 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3302 * Called once to initialize data structures...
3305 nfsd_modevent(module_t mod, int type, void *data)
3308 static int loaded = 0;
3315 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3316 snprintf(nfsrchash_table[i].lock_name,
3317 sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d",
3319 mtx_init(&nfsrchash_table[i].mtx,
3320 nfsrchash_table[i].lock_name, NULL, MTX_DEF);
3321 snprintf(nfsrcahash_table[i].lock_name,
3322 sizeof(nfsrcahash_table[i].lock_name), "nfsrc_tcpa%d",
3324 mtx_init(&nfsrcahash_table[i].mtx,
3325 nfsrcahash_table[i].lock_name, NULL, MTX_DEF);
3327 mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF);
3328 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3329 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3331 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3338 #ifdef VV_DISABLEDELEG
3339 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3340 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3342 nfsd_call_servertimer = nfsrv_servertimer;
3343 nfsd_call_nfsd = nfssvc_nfsd;
3348 if (newnfs_numnfsd != 0) {
3353 #ifdef VV_DISABLEDELEG
3354 vn_deleg_ops.vndeleg_recall = NULL;
3355 vn_deleg_ops.vndeleg_disable = NULL;
3357 nfsd_call_servertimer = NULL;
3358 nfsd_call_nfsd = NULL;
3360 /* Clean out all NFSv4 state. */
3361 nfsrv_throwawayallstate(curthread);
3363 /* Clean the NFS server reply cache */
3364 nfsrvd_cleancache();
3366 /* Free up the krpc server pool. */
3367 if (nfsrvd_pool != NULL)
3368 svcpool_destroy(nfsrvd_pool);
3370 /* and get rid of the locks */
3371 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3372 mtx_destroy(&nfsrchash_table[i].mtx);
3373 mtx_destroy(&nfsrcahash_table[i].mtx);
3375 mtx_destroy(&nfsrc_udpmtx);
3376 mtx_destroy(&nfs_v4root_mutex);
3377 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3378 lockdestroy(&nfsv4root_mnt.mnt_explock);
3390 static moduledata_t nfsd_mod = {
3395 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3397 /* So that loader and kldload(2) can find us, wherever we are.. */
3398 MODULE_VERSION(nfsd, 1);
3399 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3400 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3401 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3402 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3403 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);