2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/capsicum.h>
40 * Functions that perform the vfs operations required by the routines in
41 * nfsd_serv.c. It is hoped that this change will make the server more
45 #include <fs/nfs/nfsport.h>
47 #include <sys/sysctl.h>
48 #include <nlm/nlm_prot.h>
51 FEATURE(nfsd, "NFSv4 server");
53 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54 extern int nfsrv_useacl;
55 extern int newnfs_numnfsd;
56 extern struct mount nfsv4root_mnt;
57 extern struct nfsrv_stablefirst nfsrv_stablefirst;
58 extern void (*nfsd_call_servertimer)(void);
59 extern SVCPOOL *nfsrvd_pool;
60 extern struct nfsv4lock nfsd_suspend_lock;
61 extern struct nfsclienthashhead *nfsclienthash;
62 extern struct nfslockhashhead *nfslockhash;
63 extern struct nfssessionhash *nfssessionhash;
64 extern int nfsrv_sessionhashsize;
65 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
67 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
68 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
69 struct mtx nfsrc_udpmtx;
70 struct mtx nfs_v4root_mutex;
71 struct nfsrvfh nfs_rootfh, nfs_pubfh;
72 int nfs_pubfhset = 0, nfs_rootfhset = 0;
73 struct proc *nfsd_master_proc = NULL;
74 int nfsd_debuglevel = 0;
75 static pid_t nfsd_master_pid = (pid_t)-1;
76 static char nfsd_master_comm[MAXCOMLEN + 1];
77 static struct timeval nfsd_master_start;
78 static uint32_t nfsv4_sysid = 0;
80 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
83 int nfsrv_enable_crossmntpt = 1;
84 static int nfs_commit_blks;
85 static int nfs_commit_miss;
86 extern int nfsrv_issuedelegs;
87 extern int nfsrv_dolocallocks;
88 extern int nfsd_enable_stringtouid;
90 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
91 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
92 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
93 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
95 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
97 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
98 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
100 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
101 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
102 0, "Debug level for new nfs server");
103 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
104 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
106 #define MAX_REORDERED_RPC 16
107 #define NUM_HEURISTIC 1031
108 #define NHUSE_INIT 64
110 #define NHUSE_MAX 2048
112 static struct nfsheur {
113 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
114 off_t nh_nextoff; /* next offset for sequential detection */
115 int nh_use; /* use count for selection */
116 int nh_seqcount; /* heuristic */
117 } nfsheur[NUM_HEURISTIC];
121 * Heuristic to detect sequential operation.
123 static struct nfsheur *
124 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
129 /* Locate best candidate. */
131 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
134 if (nfsheur[hi].nh_vp == vp) {
138 if (nfsheur[hi].nh_use > 0)
139 --nfsheur[hi].nh_use;
140 hi = (hi + 1) % NUM_HEURISTIC;
141 if (nfsheur[hi].nh_use < nh->nh_use)
145 /* Initialize hint if this is a new file. */
146 if (nh->nh_vp != vp) {
148 nh->nh_nextoff = uio->uio_offset;
149 nh->nh_use = NHUSE_INIT;
150 if (uio->uio_offset == 0)
156 /* Calculate heuristic. */
157 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
158 uio->uio_offset == nh->nh_nextoff) {
159 /* See comments in vfs_vnops.c:sequential_heuristic(). */
160 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
161 if (nh->nh_seqcount > IO_SEQMAX)
162 nh->nh_seqcount = IO_SEQMAX;
163 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
164 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
165 /* Probably a reordered RPC, leave seqcount alone. */
166 } else if (nh->nh_seqcount > 1) {
167 nh->nh_seqcount /= 2;
171 nh->nh_use += NHUSE_INC;
172 if (nh->nh_use > NHUSE_MAX)
173 nh->nh_use = NHUSE_MAX;
178 * Get attributes into nfsvattr structure.
181 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
182 struct thread *p, int vpislocked)
184 int error, lockedit = 0;
186 if (vpislocked == 0) {
188 * When vpislocked == 0, the vnode is either exclusively
189 * locked by this thread or not locked by this thread.
190 * As such, shared lock it, if not exclusively locked.
192 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
194 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
197 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
206 * Get a file handle for a vnode.
209 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
213 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
214 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
215 error = VOP_VPTOFH(vp, &fhp->fh_fid);
222 * Perform access checking for vnodes obtained from file handles that would
223 * refer to files already opened by a Unix client. You cannot just use
224 * vn_writechk() and VOP_ACCESSX() for two reasons.
225 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
227 * 2 - The owner is to be given access irrespective of mode bits for some
228 * operations, so that processes that chmod after opening a file don't
232 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
233 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
234 u_int32_t *supportedtypep)
237 int error = 0, getret = 0;
239 if (vpislocked == 0) {
240 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
245 if (accmode & VWRITE) {
246 /* Just vn_writechk() changed to check rdonly */
248 * Disallow write attempts on read-only file systems;
249 * unless the file is a socket or a block or character
250 * device resident on the file system.
252 if (NFSVNO_EXRDONLY(exp) ||
253 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
254 switch (vp->v_type) {
264 * If there's shared text associated with
265 * the inode, try to free it up once. If
266 * we fail, we can't allow writing.
268 if (VOP_IS_TEXT(vp) && error == 0)
278 * Should the override still be applied when ACLs are enabled?
280 error = VOP_ACCESSX(vp, accmode, cred, p);
281 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
283 * Try again with VEXPLICIT_DENY, to see if the test for
284 * deletion is supported.
286 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
288 if (vp->v_type == VDIR) {
289 accmode &= ~(VDELETE | VDELETE_CHILD);
291 error = VOP_ACCESSX(vp, accmode, cred, p);
292 } else if (supportedtypep != NULL) {
293 *supportedtypep &= ~NFSACCESS_DELETE;
299 * Allow certain operations for the owner (reads and writes
300 * on files that are already open).
302 if (override != NFSACCCHK_NOOVERRIDE &&
303 (error == EPERM || error == EACCES)) {
304 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
306 else if (override & NFSACCCHK_ALLOWOWNER) {
307 getret = VOP_GETATTR(vp, &vattr, cred);
308 if (getret == 0 && cred->cr_uid == vattr.va_uid)
321 * Set attribute(s) vnop.
324 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
325 struct thread *p, struct nfsexstuff *exp)
329 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
335 * Set up nameidata for a lookup() call and do it.
338 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
339 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
340 struct vnode **retdirp)
342 struct componentname *cnp = &ndp->ni_cnd;
346 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
347 int error = 0, crossmnt;
351 cnp->cn_nameptr = cnp->cn_pnbuf;
352 ndp->ni_strictrelative = 0;
354 * Extract and set starting directory.
356 if (dp->v_type != VDIR) {
361 nfsvno_relpathbuf(ndp);
369 if (NFSVNO_EXRDONLY(exp))
370 cnp->cn_flags |= RDONLY;
371 ndp->ni_segflg = UIO_SYSSPACE;
374 if (nd->nd_flag & ND_PUBLOOKUP) {
376 if (cnp->cn_pnbuf[0] == '/') {
379 * Check for degenerate pathnames here, since lookup()
382 for (i = 1; i < ndp->ni_pathlen; i++)
383 if (cnp->cn_pnbuf[i] != '/')
385 if (i == ndp->ni_pathlen) {
386 error = NFSERR_ACCES;
392 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
393 (nd->nd_flag & ND_NFSV4) == 0) {
395 * Only cross mount points for NFSv4 when doing a
396 * mount while traversing the file system above
397 * the mount point, unless nfsrv_enable_crossmntpt is set.
399 cnp->cn_flags |= NOCROSSMOUNT;
404 * Initialize for scan, set ni_startdir and bump ref on dp again
405 * because lookup() will dereference ni_startdir.
409 ndp->ni_startdir = dp;
410 ndp->ni_rootdir = rootvnode;
411 ndp->ni_topdir = NULL;
414 cnp->cn_flags |= LOCKLEAF;
416 cnp->cn_nameptr = cnp->cn_pnbuf;
418 * Call lookup() to do the real work. If an error occurs,
419 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
420 * we do not have to dereference anything before returning.
421 * In either case ni_startdir will be dereferenced and NULLed
429 * Check for encountering a symbolic link. Trivial
430 * termination occurs if no symlink encountered.
432 if ((cnp->cn_flags & ISSYMLINK) == 0) {
433 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
434 nfsvno_relpathbuf(ndp);
435 if (ndp->ni_vp && !lockleaf)
436 NFSVOPUNLOCK(ndp->ni_vp, 0);
443 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
444 NFSVOPUNLOCK(ndp->ni_dvp, 0);
445 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
450 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
454 if (ndp->ni_pathlen > 1)
455 cp = uma_zalloc(namei_zone, M_WAITOK);
459 aiov.iov_len = MAXPATHLEN;
460 auio.uio_iov = &aiov;
463 auio.uio_rw = UIO_READ;
464 auio.uio_segflg = UIO_SYSSPACE;
466 auio.uio_resid = MAXPATHLEN;
467 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
470 if (ndp->ni_pathlen > 1)
471 uma_zfree(namei_zone, cp);
477 linklen = MAXPATHLEN - auio.uio_resid;
482 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
483 error = ENAMETOOLONG;
488 * Adjust or replace path
490 if (ndp->ni_pathlen > 1) {
491 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
492 uma_zfree(namei_zone, cnp->cn_pnbuf);
495 cnp->cn_pnbuf[linklen] = '\0';
496 ndp->ni_pathlen += linklen;
499 * Cleanup refs for next loop and check if root directory
500 * should replace current directory. Normally ni_dvp
501 * becomes the new base directory and is cleaned up when
502 * we loop. Explicitly null pointers after invalidation
503 * to clarify operation.
508 if (cnp->cn_pnbuf[0] == '/') {
510 ndp->ni_dvp = ndp->ni_rootdir;
513 ndp->ni_startdir = ndp->ni_dvp;
517 cnp->cn_flags &= ~LOCKLEAF;
521 nfsvno_relpathbuf(ndp);
524 ndp->ni_startdir = NULL;
525 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
530 NFSEXITCODE2(error, nd);
535 * Set up a pathname buffer and return a pointer to it and, optionally
536 * set a hash pointer.
539 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
541 struct componentname *cnp = &ndp->ni_cnd;
543 cnp->cn_flags |= (NOMACCHECK | HASBUF);
544 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
547 *bufpp = cnp->cn_pnbuf;
551 * Release the above path buffer, if not released by nfsvno_namei().
554 nfsvno_relpathbuf(struct nameidata *ndp)
557 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
559 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
560 ndp->ni_cnd.cn_flags &= ~HASBUF;
564 * Readlink vnode op into an mbuf list.
567 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
568 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
570 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
571 struct iovec *ivp = iv;
572 struct uio io, *uiop = &io;
573 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
574 int i, len, tlen, error = 0;
578 while (len < NFS_MAXPATHLEN) {
580 MCLGET(mp, M_WAITOK);
581 mp->m_len = NFSMSIZ(mp);
588 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
589 mp->m_len = NFS_MAXPATHLEN - len;
590 len = NFS_MAXPATHLEN;
594 ivp->iov_base = mtod(mp, caddr_t);
595 ivp->iov_len = mp->m_len;
600 uiop->uio_iovcnt = i;
601 uiop->uio_offset = 0;
602 uiop->uio_resid = len;
603 uiop->uio_rw = UIO_READ;
604 uiop->uio_segflg = UIO_SYSSPACE;
606 error = VOP_READLINK(vp, uiop, cred);
612 if (uiop->uio_resid > 0) {
613 len -= uiop->uio_resid;
614 tlen = NFSM_RNDUP(len);
615 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
627 * Read vnode op call into mbuf list.
630 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
631 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
637 int error = 0, len, left, siz, tlen, ioflag = 0;
638 struct mbuf *m2 = NULL, *m3;
639 struct uio io, *uiop = &io;
642 len = left = NFSM_RNDUP(cnt);
645 * Generate the mbuf list with the uio_iov ref. to it.
652 siz = min(M_TRAILINGSPACE(m), left);
661 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
663 uiop->uio_iov = iv2 = iv;
669 panic("nfsvno_read iov");
670 siz = min(M_TRAILINGSPACE(m), left);
672 iv->iov_base = mtod(m, caddr_t) + m->m_len;
681 uiop->uio_iovcnt = i;
682 uiop->uio_offset = off;
683 uiop->uio_resid = len;
684 uiop->uio_rw = UIO_READ;
685 uiop->uio_segflg = UIO_SYSSPACE;
687 nh = nfsrv_sequential_heuristic(uiop, vp);
688 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
689 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
690 FREE((caddr_t)iv2, M_TEMP);
696 nh->nh_nextoff = uiop->uio_offset;
697 tlen = len - uiop->uio_resid;
698 cnt = cnt < tlen ? cnt : tlen;
699 tlen = NFSM_RNDUP(cnt);
703 } else if (len != tlen || tlen != cnt)
704 nfsrv_adj(m3, len - tlen, tlen - cnt);
714 * Write vnode op from an mbuf list.
717 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
718 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
724 struct uio io, *uiop = &io;
727 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
729 uiop->uio_iov = iv = ivp;
730 uiop->uio_iovcnt = cnt;
731 i = mtod(mp, caddr_t) + mp->m_len - cp;
735 panic("nfsvno_write");
746 cp = mtod(mp, caddr_t);
750 if (stable == NFSWRITE_UNSTABLE)
751 ioflags = IO_NODELOCKED;
753 ioflags = (IO_SYNC | IO_NODELOCKED);
754 uiop->uio_resid = retlen;
755 uiop->uio_rw = UIO_WRITE;
756 uiop->uio_segflg = UIO_SYSSPACE;
758 uiop->uio_offset = off;
759 nh = nfsrv_sequential_heuristic(uiop, vp);
760 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
761 error = VOP_WRITE(vp, uiop, ioflags, cred);
763 nh->nh_nextoff = uiop->uio_offset;
764 FREE((caddr_t)iv, M_TEMP);
771 * Common code for creating a regular file (plus special files for V2).
774 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
775 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
776 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
781 error = nd->nd_repstat;
782 if (!error && ndp->ni_vp == NULL) {
783 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
784 vrele(ndp->ni_startdir);
785 error = VOP_CREATE(ndp->ni_dvp,
786 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
788 nfsvno_relpathbuf(ndp);
790 if (*exclusive_flagp) {
791 *exclusive_flagp = 0;
792 NFSVNO_ATTRINIT(nvap);
793 nvap->na_atime.tv_sec = cverf[0];
794 nvap->na_atime.tv_nsec = cverf[1];
795 error = VOP_SETATTR(ndp->ni_vp,
796 &nvap->na_vattr, nd->nd_cred);
800 error = NFSERR_NOTSUPP;
805 * NFS V2 Only. nfsrvd_mknod() does this for V3.
806 * (This implies, just get out on an error.)
808 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
809 nvap->na_type == VFIFO) {
810 if (nvap->na_type == VCHR && rdev == 0xffffffff)
811 nvap->na_type = VFIFO;
812 if (nvap->na_type != VFIFO &&
813 (error = priv_check_cred(nd->nd_cred,
814 PRIV_VFS_MKNOD_DEV, 0))) {
815 vrele(ndp->ni_startdir);
816 nfsvno_relpathbuf(ndp);
820 nvap->na_rdev = rdev;
821 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
822 &ndp->ni_cnd, &nvap->na_vattr);
824 nfsvno_relpathbuf(ndp);
825 vrele(ndp->ni_startdir);
829 vrele(ndp->ni_startdir);
830 nfsvno_relpathbuf(ndp);
838 * Handle cases where error is already set and/or
840 * 1 - clean up the lookup
841 * 2 - iff !error and na_size set, truncate it
843 vrele(ndp->ni_startdir);
844 nfsvno_relpathbuf(ndp);
846 if (ndp->ni_dvp == *vpp)
850 if (!error && nvap->na_size != VNOVAL) {
851 error = nfsvno_accchk(*vpp, VWRITE,
852 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
853 NFSACCCHK_VPISLOCKED, NULL);
855 tempsize = nvap->na_size;
856 NFSVNO_ATTRINIT(nvap);
857 nvap->na_size = tempsize;
858 error = VOP_SETATTR(*vpp,
859 &nvap->na_vattr, nd->nd_cred);
872 * Do a mknod vnode op.
875 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
881 vtyp = nvap->na_type;
883 * Iff doesn't exist, create it.
886 vrele(ndp->ni_startdir);
887 nfsvno_relpathbuf(ndp);
893 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
894 vrele(ndp->ni_startdir);
895 nfsvno_relpathbuf(ndp);
897 error = NFSERR_BADTYPE;
901 vrele(ndp->ni_startdir);
902 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
903 &ndp->ni_cnd, &nvap->na_vattr);
905 nfsvno_relpathbuf(ndp);
907 if (nvap->na_type != VFIFO &&
908 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
909 vrele(ndp->ni_startdir);
910 nfsvno_relpathbuf(ndp);
914 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
915 &ndp->ni_cnd, &nvap->na_vattr);
917 nfsvno_relpathbuf(ndp);
918 vrele(ndp->ni_startdir);
920 * Since VOP_MKNOD returns the ni_vp, I can't
921 * see any reason to do the lookup.
934 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
935 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
939 if (ndp->ni_vp != NULL) {
940 if (ndp->ni_dvp == ndp->ni_vp)
945 nfsvno_relpathbuf(ndp);
949 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
952 nfsvno_relpathbuf(ndp);
963 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
964 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
965 struct nfsexstuff *exp)
970 vrele(ndp->ni_startdir);
971 nfsvno_relpathbuf(ndp);
972 if (ndp->ni_dvp == ndp->ni_vp)
981 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
982 &nvap->na_vattr, pathcp);
984 vrele(ndp->ni_startdir);
985 nfsvno_relpathbuf(ndp);
987 * Although FreeBSD still had the lookup code in
988 * it for 7/current, there doesn't seem to be any
989 * point, since VOP_SYMLINK() returns the ni_vp.
990 * Just vput it for v2.
992 if (!not_v2 && !error)
1001 * Parse symbolic link arguments.
1002 * This function has an ugly side effect. It will MALLOC() an area for
1003 * the symlink and set iov_base to point to it, only if it succeeds.
1004 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1008 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1009 struct thread *p, char **pathcpp, int *lenp)
1012 char *pathcp = NULL;
1014 struct nfsv2_sattr *sp;
1018 if ((nd->nd_flag & ND_NFSV3) &&
1019 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1021 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1022 len = fxdr_unsigned(int, *tl);
1023 if (len > NFS_MAXPATHLEN || len <= 0) {
1027 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1028 error = nfsrv_mtostr(nd, pathcp, len);
1031 if (nd->nd_flag & ND_NFSV2) {
1032 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1033 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1037 NFSEXITCODE2(0, nd);
1041 free(pathcp, M_TEMP);
1042 NFSEXITCODE2(error, nd);
1047 * Remove a non-directory object.
1050 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1051 struct thread *p, struct nfsexstuff *exp)
1057 if (vp->v_type == VDIR)
1058 error = NFSERR_ISDIR;
1060 error = nfsrv_checkremove(vp, 1, p);
1062 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1063 if (ndp->ni_dvp == vp)
1068 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1069 nfsvno_relpathbuf(ndp);
1075 * Remove a directory.
1078 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1079 struct thread *p, struct nfsexstuff *exp)
1085 if (vp->v_type != VDIR) {
1090 * No rmdir "." please.
1092 if (ndp->ni_dvp == vp) {
1097 * The root of a mounted filesystem cannot be deleted.
1099 if (vp->v_vflag & VV_ROOT)
1103 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1104 if (ndp->ni_dvp == vp)
1109 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1110 nfsvno_relpathbuf(ndp);
1119 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1120 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1122 struct vnode *fvp, *tvp, *tdvp;
1125 fvp = fromndp->ni_vp;
1127 vrele(fromndp->ni_dvp);
1132 tdvp = tondp->ni_dvp;
1135 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1136 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1138 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1139 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1142 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1143 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1148 * A rename to '.' or '..' results in a prematurely
1149 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1152 if ((tondp->ni_cnd.cn_namelen == 1 &&
1153 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1154 (tondp->ni_cnd.cn_namelen == 2 &&
1155 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1156 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1161 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1162 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1165 if (fvp->v_mount != tdvp->v_mount) {
1166 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1170 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1175 * If source and destination are the same, there is nothing to
1176 * do. Set error to -1 to indicate this.
1181 if (ndflag & ND_NFSV4) {
1182 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1183 error = nfsrv_checkremove(fvp, 0, p);
1184 NFSVOPUNLOCK(fvp, 0);
1188 error = nfsrv_checkremove(tvp, 1, p);
1191 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1192 * that the NFSv4 client won't be confused by the rename.
1193 * Since nfsd_recalldelegation() can only be called on an
1194 * unlocked vnode at this point and fvp is the file that will
1195 * still exist after the rename, just do fvp.
1197 nfsd_recalldelegation(fvp, p);
1201 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1202 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1211 vrele(fromndp->ni_dvp);
1216 vrele(tondp->ni_startdir);
1217 nfsvno_relpathbuf(tondp);
1219 vrele(fromndp->ni_startdir);
1220 nfsvno_relpathbuf(fromndp);
1229 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1230 struct thread *p, struct nfsexstuff *exp)
1240 if (vp->v_mount != xp->v_mount)
1244 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1245 if ((vp->v_iflag & VI_DOOMED) == 0)
1246 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1249 if (ndp->ni_dvp == vp)
1253 NFSVOPUNLOCK(vp, 0);
1255 if (ndp->ni_dvp == ndp->ni_vp)
1262 nfsvno_relpathbuf(ndp);
1268 * Do the fsync() appropriate for the commit.
1271 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1277 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1278 * file is done. At this time VOP_FSYNC does not accept offset and
1279 * byte count parameters so call VOP_FSYNC the whole file for now.
1280 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1281 * File systems that do not use the buffer cache (as indicated
1282 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1284 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1285 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1287 * Give up and do the whole thing
1290 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1291 VM_OBJECT_WLOCK(vp->v_object);
1292 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1293 VM_OBJECT_WUNLOCK(vp->v_object);
1295 error = VOP_FSYNC(vp, MNT_WAIT, td);
1298 * Locate and synchronously write any buffers that fall
1299 * into the requested range. Note: we are assuming that
1300 * f_iosize is a power of 2.
1302 int iosize = vp->v_mount->mnt_stat.f_iosize;
1303 int iomask = iosize - 1;
1308 * Align to iosize boundry, super-align to page boundry.
1311 cnt += off & iomask;
1312 off &= ~(u_quad_t)iomask;
1314 if (off & PAGE_MASK) {
1315 cnt += off & PAGE_MASK;
1316 off &= ~(u_quad_t)PAGE_MASK;
1318 lblkno = off / iosize;
1321 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1322 VM_OBJECT_WLOCK(vp->v_object);
1323 vm_object_page_clean(vp->v_object, off, off + cnt,
1325 VM_OBJECT_WUNLOCK(vp->v_object);
1334 * If we have a buffer and it is marked B_DELWRI we
1335 * have to lock and write it. Otherwise the prior
1336 * write is assumed to have already been committed.
1338 * gbincore() can return invalid buffers now so we
1339 * have to check that bit as well (though B_DELWRI
1340 * should not be set if B_INVAL is set there could be
1341 * a race here since we haven't locked the buffer).
1343 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1344 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1345 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1347 continue; /* retry */
1349 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1352 bp->b_flags &= ~B_ASYNC;
1375 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1379 error = VFS_STATFS(vp->v_mount, sf);
1382 * Since NFS handles these values as unsigned on the
1383 * wire, there is no way to represent negative values,
1384 * so set them to 0. Without this, they will appear
1385 * to be very large positive values for clients like
1388 if (sf->f_bavail < 0)
1390 if (sf->f_ffree < 0)
1398 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1399 * must handle nfsrv_opencheck() calls after any other access checks.
1402 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1403 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1404 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1405 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1406 struct nfsexstuff *exp, struct vnode **vpp)
1408 struct vnode *vp = NULL;
1410 struct nfsexstuff nes;
1412 if (ndp->ni_vp == NULL)
1413 nd->nd_repstat = nfsrv_opencheck(clientid,
1414 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1415 if (!nd->nd_repstat) {
1416 if (ndp->ni_vp == NULL) {
1417 vrele(ndp->ni_startdir);
1418 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1419 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1421 nfsvno_relpathbuf(ndp);
1422 if (!nd->nd_repstat) {
1423 if (*exclusive_flagp) {
1424 *exclusive_flagp = 0;
1425 NFSVNO_ATTRINIT(nvap);
1426 nvap->na_atime.tv_sec = cverf[0];
1427 nvap->na_atime.tv_nsec = cverf[1];
1428 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1429 &nvap->na_vattr, cred);
1430 if (nd->nd_repstat != 0) {
1433 nd->nd_repstat = NFSERR_NOTSUPP;
1436 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1437 aclp, p, attrbitp, exp);
1442 if (ndp->ni_startdir)
1443 vrele(ndp->ni_startdir);
1444 nfsvno_relpathbuf(ndp);
1446 if (create == NFSV4OPEN_CREATE) {
1447 if (ndp->ni_dvp == vp)
1452 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1453 if (ndp->ni_cnd.cn_flags & RDONLY)
1454 NFSVNO_SETEXRDONLY(&nes);
1456 NFSVNO_EXINIT(&nes);
1457 nd->nd_repstat = nfsvno_accchk(vp,
1458 VWRITE, cred, &nes, p,
1459 NFSACCCHK_NOOVERRIDE,
1460 NFSACCCHK_VPISLOCKED, NULL);
1461 nd->nd_repstat = nfsrv_opencheck(clientid,
1462 stateidp, stp, vp, nd, p, nd->nd_repstat);
1463 if (!nd->nd_repstat) {
1464 tempsize = nvap->na_size;
1465 NFSVNO_ATTRINIT(nvap);
1466 nvap->na_size = tempsize;
1467 nd->nd_repstat = VOP_SETATTR(vp,
1468 &nvap->na_vattr, cred);
1470 } else if (vp->v_type == VREG) {
1471 nd->nd_repstat = nfsrv_opencheck(clientid,
1472 stateidp, stp, vp, nd, p, nd->nd_repstat);
1476 if (ndp->ni_cnd.cn_flags & HASBUF)
1477 nfsvno_relpathbuf(ndp);
1478 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1479 vrele(ndp->ni_startdir);
1480 if (ndp->ni_dvp == ndp->ni_vp)
1490 NFSEXITCODE2(0, nd);
1494 * Updates the file rev and sets the mtime and ctime
1495 * to the current clock time, returning the va_filerev and va_Xtime
1497 * Return ESTALE to indicate the vnode is VI_DOOMED.
1500 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1501 struct ucred *cred, struct thread *p)
1506 vfs_timestamp(&va.va_mtime);
1507 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1508 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1509 if ((vp->v_iflag & VI_DOOMED) != 0)
1512 (void) VOP_SETATTR(vp, &va, cred);
1513 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1518 * Glue routine to nfsv4_fillattr().
1521 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1522 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1523 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1524 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1528 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1529 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1531 NFSEXITCODE2(0, nd);
1535 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1537 * nfs readdir service
1538 * - mallocs what it thinks is enough to read
1539 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1540 * - calls VOP_READDIR()
1541 * - loops around building the reply
1542 * if the output generated exceeds count break out of loop
1543 * The NFSM_CLGET macro is used here so that the reply will be packed
1544 * tightly in mbuf clusters.
1545 * - it trims out records with d_fileno == 0
1546 * this doesn't matter for Unix clients, but they might confuse clients
1548 * - it trims out records with d_type == DT_WHT
1549 * these cannot be seen through NFS (unless we extend the protocol)
1550 * The alternate call nfsrvd_readdirplus() does lookups as well.
1551 * PS: The NFS protocol spec. does not clarify what the "count" byte
1552 * argument is a count of.. just name strings and file id's or the
1553 * entire reply rpc or ...
1554 * I tried just file name and id sizes and it confused the Sun client,
1555 * so I am using the full rpc size now. The "paranoia.." comment refers
1556 * to including the status longwords that are not a part of the dir.
1557 * "entry" structures, but are in the rpc.
1560 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1561 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1566 char *cpos, *cend, *rbuf;
1568 int nlen, error = 0, getret = 1;
1569 int siz, cnt, fullsiz, eofflag, ncookies;
1570 u_int64_t off, toff, verf;
1571 u_long *cookies = NULL, *cookiep;
1576 if (nd->nd_repstat) {
1577 nfsrv_postopattr(nd, getret, &at);
1580 if (nd->nd_flag & ND_NFSV2) {
1581 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1582 off = fxdr_unsigned(u_quad_t, *tl++);
1584 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1585 off = fxdr_hyper(tl);
1587 verf = fxdr_hyper(tl);
1591 cnt = fxdr_unsigned(int, *tl);
1592 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1593 cnt = NFS_SRVMAXDATA(nd);
1594 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1596 if (nd->nd_flag & ND_NFSV3) {
1597 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1601 * va_filerev is not sufficient as a cookie verifier,
1602 * since it is not supposed to change when entries are
1603 * removed/added unless that offset cookies returned to
1604 * the client are no longer valid.
1606 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1607 nd->nd_repstat = NFSERR_BAD_COOKIE;
1610 if (!nd->nd_repstat && vp->v_type != VDIR)
1611 nd->nd_repstat = NFSERR_NOTDIR;
1612 if (nd->nd_repstat == 0 && cnt == 0) {
1613 if (nd->nd_flag & ND_NFSV2)
1614 /* NFSv2 does not have NFSERR_TOOSMALL */
1615 nd->nd_repstat = EPERM;
1617 nd->nd_repstat = NFSERR_TOOSMALL;
1619 if (!nd->nd_repstat)
1620 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1621 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1622 NFSACCCHK_VPISLOCKED, NULL);
1623 if (nd->nd_repstat) {
1625 if (nd->nd_flag & ND_NFSV3)
1626 nfsrv_postopattr(nd, getret, &at);
1629 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1630 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1634 free((caddr_t)cookies, M_TEMP);
1642 io.uio_offset = (off_t)off;
1644 io.uio_segflg = UIO_SYSSPACE;
1645 io.uio_rw = UIO_READ;
1647 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1649 off = (u_int64_t)io.uio_offset;
1651 siz -= io.uio_resid;
1653 if (!cookies && !nd->nd_repstat)
1654 nd->nd_repstat = NFSERR_PERM;
1655 if (nd->nd_flag & ND_NFSV3) {
1656 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1657 if (!nd->nd_repstat)
1658 nd->nd_repstat = getret;
1662 * Handles the failed cases. nd->nd_repstat == 0 past here.
1664 if (nd->nd_repstat) {
1666 free((caddr_t)rbuf, M_TEMP);
1668 free((caddr_t)cookies, M_TEMP);
1669 if (nd->nd_flag & ND_NFSV3)
1670 nfsrv_postopattr(nd, getret, &at);
1674 * If nothing read, return eof
1679 if (nd->nd_flag & ND_NFSV2) {
1680 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1682 nfsrv_postopattr(nd, getret, &at);
1683 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1684 txdr_hyper(at.na_filerev, tl);
1687 *tl++ = newnfs_false;
1689 FREE((caddr_t)rbuf, M_TEMP);
1690 FREE((caddr_t)cookies, M_TEMP);
1695 * Check for degenerate cases of nothing useful read.
1696 * If so go try again
1700 dp = (struct dirent *)cpos;
1704 * For some reason FreeBSD's ufs_readdir() chooses to back the
1705 * directory offset up to a block boundary, so it is necessary to
1706 * skip over the records that precede the requested offset. This
1707 * requires the assumption that file offset cookies monotonically
1710 while (cpos < cend && ncookies > 0 &&
1711 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1712 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1713 cpos += dp->d_reclen;
1714 dp = (struct dirent *)cpos;
1718 if (cpos >= cend || ncookies == 0) {
1726 * dirlen is the size of the reply, including all XDR and must
1727 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1728 * if the XDR should be included in "count", but to be safe, we do.
1729 * (Include the two booleans at the end of the reply in dirlen now.)
1731 if (nd->nd_flag & ND_NFSV3) {
1732 nfsrv_postopattr(nd, getret, &at);
1733 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1734 txdr_hyper(at.na_filerev, tl);
1735 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1737 dirlen = 2 * NFSX_UNSIGNED;
1740 /* Loop through the records and build reply */
1741 while (cpos < cend && ncookies > 0) {
1742 nlen = dp->d_namlen;
1743 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1744 nlen <= NFS_MAXNAMLEN) {
1745 if (nd->nd_flag & ND_NFSV3)
1746 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1748 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1755 * Build the directory record xdr from
1758 if (nd->nd_flag & ND_NFSV3) {
1759 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1760 *tl++ = newnfs_true;
1763 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1764 *tl++ = newnfs_true;
1766 *tl = txdr_unsigned(dp->d_fileno);
1767 (void) nfsm_strtom(nd, dp->d_name, nlen);
1768 if (nd->nd_flag & ND_NFSV3) {
1769 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1772 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1773 *tl = txdr_unsigned(*cookiep);
1775 cpos += dp->d_reclen;
1776 dp = (struct dirent *)cpos;
1782 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1783 *tl++ = newnfs_false;
1788 FREE((caddr_t)rbuf, M_TEMP);
1789 FREE((caddr_t)cookies, M_TEMP);
1792 NFSEXITCODE2(0, nd);
1796 NFSEXITCODE2(error, nd);
1801 * Readdirplus for V3 and Readdir for V4.
1804 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1805 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1810 char *cpos, *cend, *rbuf;
1813 struct nfsvattr nva, at, *nvap = &nva;
1814 struct mbuf *mb0, *mb1;
1815 struct nfsreferral *refp;
1816 int nlen, r, error = 0, getret = 1, usevget = 1;
1817 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1818 caddr_t bpos0, bpos1;
1819 u_int64_t off, toff, verf;
1820 u_long *cookies = NULL, *cookiep;
1821 nfsattrbit_t attrbits, rderrbits, savbits;
1824 struct componentname cn;
1825 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
1826 struct mount *mp, *new_mp;
1827 uint64_t mounted_on_fileno;
1829 if (nd->nd_repstat) {
1830 nfsrv_postopattr(nd, getret, &at);
1833 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1834 off = fxdr_hyper(tl);
1837 verf = fxdr_hyper(tl);
1839 siz = fxdr_unsigned(int, *tl++);
1840 cnt = fxdr_unsigned(int, *tl);
1843 * Use the server's maximum data transfer size as the upper bound
1846 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1847 cnt = NFS_SRVMAXDATA(nd);
1850 * siz is a "hint" of how much directory information (name, fileid,
1851 * cookie) should be in the reply. At least one client "hints" 0,
1852 * so I set it to cnt for that case. I also round it up to the
1853 * next multiple of DIRBLKSIZ.
1857 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1859 if (nd->nd_flag & ND_NFSV4) {
1860 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1863 NFSSET_ATTRBIT(&savbits, &attrbits);
1864 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1865 NFSZERO_ATTRBIT(&rderrbits);
1866 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1868 NFSZERO_ATTRBIT(&attrbits);
1871 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1872 if (!nd->nd_repstat) {
1873 if (off && verf != at.na_filerev) {
1875 * va_filerev is not sufficient as a cookie verifier,
1876 * since it is not supposed to change when entries are
1877 * removed/added unless that offset cookies returned to
1878 * the client are no longer valid.
1881 if (nd->nd_flag & ND_NFSV4) {
1882 nd->nd_repstat = NFSERR_NOTSAME;
1884 nd->nd_repstat = NFSERR_BAD_COOKIE;
1887 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1888 nd->nd_repstat = NFSERR_BAD_COOKIE;
1891 if (!nd->nd_repstat && vp->v_type != VDIR)
1892 nd->nd_repstat = NFSERR_NOTDIR;
1893 if (!nd->nd_repstat && cnt == 0)
1894 nd->nd_repstat = NFSERR_TOOSMALL;
1895 if (!nd->nd_repstat)
1896 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1897 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1898 NFSACCCHK_VPISLOCKED, NULL);
1899 if (nd->nd_repstat) {
1901 if (nd->nd_flag & ND_NFSV3)
1902 nfsrv_postopattr(nd, getret, &at);
1905 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1906 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
1908 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1912 free((caddr_t)cookies, M_TEMP);
1920 io.uio_offset = (off_t)off;
1922 io.uio_segflg = UIO_SYSSPACE;
1923 io.uio_rw = UIO_READ;
1925 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1927 off = (u_int64_t)io.uio_offset;
1929 siz -= io.uio_resid;
1931 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1933 if (!cookies && !nd->nd_repstat)
1934 nd->nd_repstat = NFSERR_PERM;
1935 if (!nd->nd_repstat)
1936 nd->nd_repstat = getret;
1937 if (nd->nd_repstat) {
1940 free((caddr_t)cookies, M_TEMP);
1941 free((caddr_t)rbuf, M_TEMP);
1942 if (nd->nd_flag & ND_NFSV3)
1943 nfsrv_postopattr(nd, getret, &at);
1947 * If nothing read, return eof
1952 if (nd->nd_flag & ND_NFSV3)
1953 nfsrv_postopattr(nd, getret, &at);
1954 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1955 txdr_hyper(at.na_filerev, tl);
1957 *tl++ = newnfs_false;
1959 free((caddr_t)cookies, M_TEMP);
1960 free((caddr_t)rbuf, M_TEMP);
1965 * Check for degenerate cases of nothing useful read.
1966 * If so go try again
1970 dp = (struct dirent *)cpos;
1974 * For some reason FreeBSD's ufs_readdir() chooses to back the
1975 * directory offset up to a block boundary, so it is necessary to
1976 * skip over the records that precede the requested offset. This
1977 * requires the assumption that file offset cookies monotonically
1980 while (cpos < cend && ncookies > 0 &&
1981 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1982 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
1983 ((nd->nd_flag & ND_NFSV4) &&
1984 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1985 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1986 cpos += dp->d_reclen;
1987 dp = (struct dirent *)cpos;
1991 if (cpos >= cend || ncookies == 0) {
1998 * Busy the file system so that the mount point won't go away
1999 * and, as such, VFS_VGET() can be used safely.
2003 NFSVOPUNLOCK(vp, 0);
2004 nd->nd_repstat = vfs_busy(mp, 0);
2006 if (nd->nd_repstat != 0) {
2008 free(cookies, M_TEMP);
2010 if (nd->nd_flag & ND_NFSV3)
2011 nfsrv_postopattr(nd, getret, &at);
2016 * For now ZFS requires VOP_LOOKUP as a workaround. Until ino_t is changed
2017 * to 64 bit type a ZFS filesystem with over 1 billion files in it
2018 * will suffer from 64bit -> 32bit truncation.
2023 cn.cn_nameiop = LOOKUP;
2024 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2025 cn.cn_cred = nd->nd_cred;
2029 * Save this position, in case there is an error before one entry
2033 bpos0 = nd->nd_bpos;
2036 * Fill in the first part of the reply.
2037 * dirlen is the reply length in bytes and cannot exceed cnt.
2038 * (Include the two booleans at the end of the reply in dirlen now,
2039 * so we recognize when we have exceeded cnt.)
2041 if (nd->nd_flag & ND_NFSV3) {
2042 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2043 nfsrv_postopattr(nd, getret, &at);
2045 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2047 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2048 txdr_hyper(at.na_filerev, tl);
2051 * Save this position, in case there is an empty reply needed.
2054 bpos1 = nd->nd_bpos;
2056 /* Loop through the records and build reply */
2058 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2059 nlen = dp->d_namlen;
2060 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2061 nlen <= NFS_MAXNAMLEN &&
2062 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2063 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2064 || (nlen == 1 && dp->d_name[0] != '.'))) {
2066 * Save the current position in the reply, in case
2067 * this entry exceeds cnt.
2070 bpos1 = nd->nd_bpos;
2073 * For readdir_and_lookup get the vnode using
2082 mounted_on_fileno = (uint64_t)dp->d_fileno;
2083 if ((nd->nd_flag & ND_NFSV3) ||
2084 NFSNONZERO_ATTRBIT(&savbits)) {
2085 if (nd->nd_flag & ND_NFSV4)
2086 refp = nfsv4root_getreferral(NULL,
2090 r = VFS_VGET(mp, dp->d_fileno,
2094 if (r == EOPNOTSUPP) {
2096 cn.cn_nameptr = dp->d_name;
2097 cn.cn_namelen = nlen;
2098 cn.cn_flags = ISLASTCN |
2099 NOFOLLOW | LOCKLEAF;
2101 dp->d_name[0] == '.' &&
2102 dp->d_name[1] == '.')
2105 if (NFSVOPLOCK(vp, LK_SHARED)
2107 nd->nd_repstat = EPERM;
2110 if ((vp->v_vflag & VV_ROOT) != 0
2111 && (cn.cn_flags & ISDOTDOT)
2117 r = VOP_LOOKUP(vp, &nvp,
2126 * For NFSv4, check to see if nvp is
2127 * a mount point and get the mount
2128 * point vnode, as required.
2131 nfsrv_enable_crossmntpt != 0 &&
2132 (nd->nd_flag & ND_NFSV4) != 0 &&
2133 nvp->v_type == VDIR &&
2134 nvp->v_mountedhere != NULL) {
2135 new_mp = nvp->v_mountedhere;
2136 r = vfs_busy(new_mp, 0);
2140 r = VFS_ROOT(new_mp,
2150 ((nd->nd_flag & ND_NFSV3) ||
2151 NFSNONZERO_ATTRBIT(&attrbits))) {
2152 r = nfsvno_getfh(nvp, &nfh, p);
2154 r = nfsvno_getattr(nvp, nvap,
2156 if (r == 0 && is_zfs == 1 &&
2157 nfsrv_enable_crossmntpt != 0 &&
2158 (nd->nd_flag & ND_NFSV4) != 0 &&
2159 nvp->v_type == VDIR &&
2160 vp->v_mount != nvp->v_mount) {
2162 * For a ZFS snapshot, there is a
2163 * pseudo mount that does not set
2164 * v_mountedhere, so it needs to
2165 * be detected via a different
2170 new_mp = nvp->v_mount;
2177 if (!NFSISSET_ATTRBIT(&attrbits,
2178 NFSATTRBIT_RDATTRERROR)) {
2181 if (needs_unbusy != 0)
2190 * Build the directory record xdr
2192 if (nd->nd_flag & ND_NFSV3) {
2193 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2194 *tl++ = newnfs_true;
2196 *tl = txdr_unsigned(dp->d_fileno);
2197 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2198 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2200 *tl = txdr_unsigned(*cookiep);
2201 nfsrv_postopattr(nd, 0, nvap);
2202 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2203 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2207 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2208 *tl++ = newnfs_true;
2210 *tl = txdr_unsigned(*cookiep);
2211 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2213 supports_nfsv4acls =
2214 nfs_supportsnfsv4acls(nvp);
2215 NFSVOPUNLOCK(nvp, 0);
2217 supports_nfsv4acls = 0;
2219 dirlen += nfsrv_putreferralattr(nd,
2222 if (nd->nd_repstat) {
2225 if (needs_unbusy != 0)
2230 dirlen += nfsvno_fillattr(nd, new_mp,
2231 nvp, nvap, &nfh, r, &rderrbits,
2232 nd->nd_cred, p, isdgram, 0,
2233 supports_nfsv4acls, at_root,
2236 dirlen += nfsvno_fillattr(nd, new_mp,
2237 nvp, nvap, &nfh, r, &attrbits,
2238 nd->nd_cred, p, isdgram, 0,
2239 supports_nfsv4acls, at_root,
2244 dirlen += (3 * NFSX_UNSIGNED);
2246 if (needs_unbusy != 0)
2251 cpos += dp->d_reclen;
2252 dp = (struct dirent *)cpos;
2260 * If dirlen > cnt, we must strip off the last entry. If that
2261 * results in an empty reply, report NFSERR_TOOSMALL.
2263 if (dirlen > cnt || nd->nd_repstat) {
2264 if (!nd->nd_repstat && entrycnt == 0)
2265 nd->nd_repstat = NFSERR_TOOSMALL;
2266 if (nd->nd_repstat) {
2267 newnfs_trimtrailing(nd, mb0, bpos0);
2268 if (nd->nd_flag & ND_NFSV3)
2269 nfsrv_postopattr(nd, getret, &at);
2271 newnfs_trimtrailing(nd, mb1, bpos1);
2273 } else if (cpos < cend)
2275 if (!nd->nd_repstat) {
2276 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2277 *tl++ = newnfs_false;
2283 FREE((caddr_t)cookies, M_TEMP);
2284 FREE((caddr_t)rbuf, M_TEMP);
2287 NFSEXITCODE2(0, nd);
2291 NFSEXITCODE2(error, nd);
2296 * Get the settable attributes out of the mbuf list.
2297 * (Return 0 or EBADRPC)
2300 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2301 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2304 struct nfsv2_sattr *sp;
2305 int error = 0, toclient = 0;
2307 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2309 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2311 * Some old clients didn't fill in the high order 16bits.
2312 * --> check the low order 2 bytes for 0xffff
2314 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2315 nvap->na_mode = nfstov_mode(sp->sa_mode);
2316 if (sp->sa_uid != newnfs_xdrneg1)
2317 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2318 if (sp->sa_gid != newnfs_xdrneg1)
2319 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2320 if (sp->sa_size != newnfs_xdrneg1)
2321 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2322 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2324 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2326 nvap->na_atime.tv_sec =
2327 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2328 nvap->na_atime.tv_nsec = 0;
2331 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2332 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2335 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2336 if (*tl == newnfs_true) {
2337 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2338 nvap->na_mode = nfstov_mode(*tl);
2340 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2341 if (*tl == newnfs_true) {
2342 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2343 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2345 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2346 if (*tl == newnfs_true) {
2347 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2348 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2350 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2351 if (*tl == newnfs_true) {
2352 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2353 nvap->na_size = fxdr_hyper(tl);
2355 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2356 switch (fxdr_unsigned(int, *tl)) {
2357 case NFSV3SATTRTIME_TOCLIENT:
2358 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2359 fxdr_nfsv3time(tl, &nvap->na_atime);
2362 case NFSV3SATTRTIME_TOSERVER:
2363 vfs_timestamp(&nvap->na_atime);
2364 nvap->na_vaflags |= VA_UTIMES_NULL;
2367 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2368 switch (fxdr_unsigned(int, *tl)) {
2369 case NFSV3SATTRTIME_TOCLIENT:
2370 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2371 fxdr_nfsv3time(tl, &nvap->na_mtime);
2372 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2374 case NFSV3SATTRTIME_TOSERVER:
2375 vfs_timestamp(&nvap->na_mtime);
2377 nvap->na_vaflags |= VA_UTIMES_NULL;
2382 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2385 NFSEXITCODE2(error, nd);
2390 * Handle the setable attributes for V4.
2391 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2394 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2395 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2400 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2402 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2406 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2409 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2410 attrsize = fxdr_unsigned(int, *tl);
2413 * Loop around getting the setable attributes. If an unsupported
2414 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2417 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2418 bitpos = NFSATTRBIT_MAX;
2422 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2423 if (attrsum > attrsize) {
2424 error = NFSERR_BADXDR;
2427 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2429 case NFSATTRBIT_SIZE:
2430 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2431 nvap->na_size = fxdr_hyper(tl);
2432 attrsum += NFSX_HYPER;
2434 case NFSATTRBIT_ACL:
2435 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2439 if (aceerr && !nd->nd_repstat)
2440 nd->nd_repstat = aceerr;
2443 case NFSATTRBIT_ARCHIVE:
2444 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2445 if (!nd->nd_repstat)
2446 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2447 attrsum += NFSX_UNSIGNED;
2449 case NFSATTRBIT_HIDDEN:
2450 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2451 if (!nd->nd_repstat)
2452 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2453 attrsum += NFSX_UNSIGNED;
2455 case NFSATTRBIT_MIMETYPE:
2456 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2457 i = fxdr_unsigned(int, *tl);
2458 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2461 if (!nd->nd_repstat)
2462 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2463 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2465 case NFSATTRBIT_MODE:
2466 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2467 nvap->na_mode = nfstov_mode(*tl);
2468 attrsum += NFSX_UNSIGNED;
2470 case NFSATTRBIT_OWNER:
2471 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2472 j = fxdr_unsigned(int, *tl);
2474 error = NFSERR_BADXDR;
2477 if (j > NFSV4_SMALLSTR)
2478 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2481 error = nfsrv_mtostr(nd, cp, j);
2483 if (j > NFSV4_SMALLSTR)
2484 free(cp, M_NFSSTRING);
2487 if (!nd->nd_repstat) {
2488 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2490 if (!nd->nd_repstat)
2493 if (j > NFSV4_SMALLSTR)
2494 free(cp, M_NFSSTRING);
2495 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2497 case NFSATTRBIT_OWNERGROUP:
2498 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2499 j = fxdr_unsigned(int, *tl);
2501 error = NFSERR_BADXDR;
2504 if (j > NFSV4_SMALLSTR)
2505 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2508 error = nfsrv_mtostr(nd, cp, j);
2510 if (j > NFSV4_SMALLSTR)
2511 free(cp, M_NFSSTRING);
2514 if (!nd->nd_repstat) {
2515 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2517 if (!nd->nd_repstat)
2520 if (j > NFSV4_SMALLSTR)
2521 free(cp, M_NFSSTRING);
2522 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2524 case NFSATTRBIT_SYSTEM:
2525 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2526 if (!nd->nd_repstat)
2527 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2528 attrsum += NFSX_UNSIGNED;
2530 case NFSATTRBIT_TIMEACCESSSET:
2531 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2532 attrsum += NFSX_UNSIGNED;
2533 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2534 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2535 fxdr_nfsv4time(tl, &nvap->na_atime);
2537 attrsum += NFSX_V4TIME;
2539 vfs_timestamp(&nvap->na_atime);
2540 nvap->na_vaflags |= VA_UTIMES_NULL;
2543 case NFSATTRBIT_TIMEBACKUP:
2544 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2545 if (!nd->nd_repstat)
2546 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2547 attrsum += NFSX_V4TIME;
2549 case NFSATTRBIT_TIMECREATE:
2550 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2551 if (!nd->nd_repstat)
2552 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2553 attrsum += NFSX_V4TIME;
2555 case NFSATTRBIT_TIMEMODIFYSET:
2556 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2557 attrsum += NFSX_UNSIGNED;
2558 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2559 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2560 fxdr_nfsv4time(tl, &nvap->na_mtime);
2561 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2562 attrsum += NFSX_V4TIME;
2564 vfs_timestamp(&nvap->na_mtime);
2566 nvap->na_vaflags |= VA_UTIMES_NULL;
2570 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2572 * set bitpos so we drop out of the loop.
2574 bitpos = NFSATTRBIT_MAX;
2580 * some clients pad the attrlist, so we need to skip over the
2583 if (attrsum > attrsize) {
2584 error = NFSERR_BADXDR;
2586 attrsize = NFSM_RNDUP(attrsize);
2587 if (attrsum < attrsize)
2588 error = nfsm_advance(nd, attrsize - attrsum, -1);
2591 NFSEXITCODE2(error, nd);
2596 * Check/setup export credentials.
2599 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2600 struct ucred *credanon)
2605 * Check/setup credentials.
2607 if (nd->nd_flag & ND_GSS)
2608 exp->nes_exflag &= ~MNT_EXPORTANON;
2611 * Check to see if the operation is allowed for this security flavor.
2612 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2613 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2614 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2616 if (nfsvno_testexp(nd, exp) &&
2617 nd->nd_procnum != NFSV4OP_SECINFO &&
2618 nd->nd_procnum != NFSPROC_FSINFO) {
2619 if (nd->nd_flag & ND_NFSV4)
2620 error = NFSERR_WRONGSEC;
2622 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2627 * Check to see if the file system is exported V4 only.
2629 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2630 error = NFSERR_PROGNOTV4;
2635 * Now, map the user credentials.
2636 * (Note that ND_AUTHNONE will only be set for an NFSv3
2637 * Fsinfo RPC. If set for anything else, this code might need
2640 if (NFSVNO_EXPORTED(exp)) {
2641 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2642 NFSVNO_EXPORTANON(exp) ||
2643 (nd->nd_flag & ND_AUTHNONE) != 0) {
2644 nd->nd_cred->cr_uid = credanon->cr_uid;
2645 nd->nd_cred->cr_gid = credanon->cr_gid;
2646 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2647 credanon->cr_groups);
2648 } else if ((nd->nd_flag & ND_GSS) == 0) {
2650 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2651 * if there is a replacement credential with a group
2652 * list set up by "nfsuserd -manage-gids".
2653 * If there is no replacement, nfsrv_getgrpscred()
2654 * simply returns its argument.
2656 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2661 NFSEXITCODE2(error, nd);
2669 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2670 struct ucred **credp)
2672 int i, error, *secflavors;
2674 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2675 &exp->nes_numsecflavor, &secflavors);
2677 if (nfs_rootfhset) {
2678 exp->nes_exflag = 0;
2679 exp->nes_numsecflavor = 0;
2683 /* Copy the security flavors. */
2684 for (i = 0; i < exp->nes_numsecflavor; i++)
2685 exp->nes_secflavors[i] = secflavors[i];
2692 * Get a vnode for a file handle and export stuff.
2695 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2696 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2697 struct ucred **credp)
2699 int i, error, *secflavors;
2702 exp->nes_numsecflavor = 0;
2703 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2705 /* Make sure the server replies ESTALE to the client. */
2707 if (nam && !error) {
2708 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2709 &exp->nes_numsecflavor, &secflavors);
2711 if (nfs_rootfhset) {
2712 exp->nes_exflag = 0;
2713 exp->nes_numsecflavor = 0;
2719 /* Copy the security flavors. */
2720 for (i = 0; i < exp->nes_numsecflavor; i++)
2721 exp->nes_secflavors[i] = secflavors[i];
2729 * nfsd_fhtovp() - convert a fh to a vnode ptr
2730 * - look up fsid in mount list (if not found ret error)
2731 * - get vp and export rights by calling nfsvno_fhtovp()
2732 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2734 * - if mpp != NULL, return the mount point so that it can
2735 * be used for vn_finished_write() by the caller
2738 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2739 struct vnode **vpp, struct nfsexstuff *exp,
2740 struct mount **mpp, int startwrite, struct thread *p)
2743 struct ucred *credanon;
2746 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2748 * Check for the special case of the nfsv4root_fh.
2750 mp = vfs_busyfs(&fhp->fh_fsid);
2755 nd->nd_repstat = ESTALE;
2760 vn_start_write(NULL, mpp, V_WAIT);
2761 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2762 lktype = LK_EXCLUSIVE;
2764 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2769 * For NFSv4 without a pseudo root fs, unexported file handles
2770 * can be returned, so that Lookup works everywhere.
2772 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2773 !(nd->nd_flag & ND_NFSV4)) {
2775 nd->nd_repstat = EACCES;
2779 * Personally, I've never seen any point in requiring a
2780 * reserved port#, since only in the rare case where the
2781 * clients are all boxes with secure system priviledges,
2782 * does it provide any enhanced security, but... some people
2783 * believe it to be useful and keep putting this code back in.
2784 * (There is also some "security checker" out there that
2785 * complains if the nfs server doesn't enforce this.)
2786 * However, note the following:
2787 * RFC3530 (NFSv4) specifies that a reserved port# not be
2789 * RFC2623 recommends that, if a reserved port# is checked for,
2790 * that there be a way to turn that off--> ifdef'd.
2792 #ifdef NFS_REQRSVPORT
2793 if (!nd->nd_repstat) {
2794 struct sockaddr_in *saddr;
2795 struct sockaddr_in6 *saddr6;
2797 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2798 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2799 if (!(nd->nd_flag & ND_NFSV4) &&
2800 ((saddr->sin_family == AF_INET &&
2801 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2802 (saddr6->sin6_family == AF_INET6 &&
2803 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2805 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2808 #endif /* NFS_REQRSVPORT */
2811 * Check/setup credentials.
2813 if (!nd->nd_repstat) {
2814 nd->nd_saveduid = nd->nd_cred->cr_uid;
2815 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2819 if (credanon != NULL)
2821 if (nd->nd_repstat) {
2823 vn_finished_write(mp);
2830 NFSEXITCODE2(0, nd);
2837 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2839 struct filedesc *fdp;
2843 fdp = p->td_proc->p_fd;
2844 if (fd < 0 || fd >= fdp->fd_nfiles ||
2845 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2857 * Called from nfssvc() to update the exports list. Just call
2858 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2859 * in the mount list.
2862 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2864 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2866 struct nameidata nd;
2869 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2870 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2872 else if (error == 0) {
2873 if (nfsexargp->fspec == NULL) {
2878 * If fspec != NULL, this is the v4root path.
2880 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2881 nfsexargp->fspec, p);
2882 if ((error = namei(&nd)) != 0)
2884 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2887 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2888 NFSBCOPY((caddr_t)&fh,
2889 nfs_rootfh.nfsrvfh_data,
2890 sizeof (fhandle_t));
2901 * This function needs to test to see if the system is near its limit
2902 * for memory allocation via malloc() or mget() and return True iff
2903 * either of these resources are near their limit.
2904 * XXX (For now, this is just a stub.)
2906 int nfsrv_testmalloclimit = 0;
2908 nfsrv_mallocmget_limit(void)
2910 static int printmesg = 0;
2911 static int testval = 1;
2913 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2914 if ((printmesg++ % 100) == 0)
2915 printf("nfsd: malloc/mget near limit\n");
2922 * BSD specific initialization of a mount point.
2927 static int inited = 0;
2932 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2933 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2934 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2935 nfsv4root_mnt.mnt_export = NULL;
2936 TAILQ_INIT(&nfsv4root_opt);
2937 TAILQ_INIT(&nfsv4root_newopt);
2938 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2939 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2940 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2941 nfsv4root_mnt.mnt_activevnodelistsize = 0;
2945 * Get a vnode for a file handle, without checking exports, etc.
2948 nfsvno_getvp(fhandle_t *fhp)
2954 mp = vfs_busyfs(&fhp->fh_fsid);
2957 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2965 * Do a local VOP_ADVLOCK().
2968 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2969 u_int64_t end, struct thread *td)
2975 if (nfsrv_dolocallocks == 0)
2977 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
2979 fl.l_whence = SEEK_SET;
2981 fl.l_start = (off_t)first;
2982 if (end == NFS64BITSSET) {
2986 fl.l_len = (off_t)tlen;
2989 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2990 * values for all calls, so that all locks will be held by the
2991 * nfsd server. (The nfsd server handles conflicts between the
2993 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2994 * bytes, so it can't be put in l_sysid.
2996 if (nfsv4_sysid == 0)
2997 nfsv4_sysid = nlm_acquire_next_sysid();
2998 fl.l_pid = (pid_t)0;
2999 fl.l_sysid = (int)nfsv4_sysid;
3001 if (ftype == F_UNLCK)
3002 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3003 (F_POSIX | F_REMOTE));
3005 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3006 (F_POSIX | F_REMOTE));
3014 * Check the nfsv4 root exports.
3017 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3019 struct ucred *credanon;
3020 int exflags, error = 0, numsecflavor, *secflavors, i;
3022 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3023 &credanon, &numsecflavor, &secflavors);
3025 error = NFSERR_PROGUNAVAIL;
3028 if (credanon != NULL)
3030 for (i = 0; i < numsecflavor; i++) {
3031 if (secflavors[i] == AUTH_SYS)
3032 nd->nd_flag |= ND_EXAUTHSYS;
3033 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3034 nd->nd_flag |= ND_EXGSS;
3035 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3036 nd->nd_flag |= ND_EXGSSINTEGRITY;
3037 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3038 nd->nd_flag |= ND_EXGSSPRIVACY;
3047 * Nfs server psuedo system call for the nfsd's
3053 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3056 struct nfsd_addsock_args sockarg;
3057 struct nfsd_nfsd_args nfsdarg;
3058 cap_rights_t rights;
3061 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3062 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3066 * Since we don't know what rights might be required,
3067 * pretend that we need them all. It is better to be too
3068 * careful than too reckless.
3070 error = fget(td, sockarg.sock,
3071 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3074 if (fp->f_type != DTYPE_SOCKET) {
3079 error = nfsrvd_addsock(fp);
3081 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3082 if (uap->argp == NULL) {
3086 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3090 error = nfsrvd_nfsd(td, &nfsdarg);
3092 error = nfssvc_srvcall(td, uap, td->td_ucred);
3101 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3103 struct nfsex_args export;
3104 struct file *fp = NULL;
3106 struct nfsd_clid adminrevoke;
3107 struct nfsd_dumplist dumplist;
3108 struct nfsd_dumpclients *dumpclients;
3109 struct nfsd_dumplocklist dumplocklist;
3110 struct nfsd_dumplocks *dumplocks;
3111 struct nameidata nd;
3113 int error = EINVAL, igotlock;
3115 static int suspend_nfsd = 0;
3117 if (uap->flag & NFSSVC_PUBLICFH) {
3118 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3119 sizeof (fhandle_t));
3120 error = copyin(uap->argp,
3121 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3124 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3125 error = copyin(uap->argp,(caddr_t)&export,
3126 sizeof (struct nfsex_args));
3128 error = nfsrv_v4rootexport(&export, cred, p);
3129 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3132 } else if (uap->flag & NFSSVC_STABLERESTART) {
3133 error = copyin(uap->argp, (caddr_t)&stablefd,
3136 error = fp_getfvp(p, stablefd, &fp, &vp);
3137 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3139 if (!error && newnfs_numnfsd != 0)
3142 nfsrv_stablefirst.nsf_fp = fp;
3143 nfsrv_setupstable(p);
3145 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3146 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3147 sizeof (struct nfsd_clid));
3149 error = nfsrv_adminrevoke(&adminrevoke, p);
3150 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3151 error = copyin(uap->argp, (caddr_t)&dumplist,
3152 sizeof (struct nfsd_dumplist));
3153 if (!error && (dumplist.ndl_size < 1 ||
3154 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3157 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3158 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3160 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3161 error = copyout(dumpclients,
3162 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3163 free((caddr_t)dumpclients, M_TEMP);
3165 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3166 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3167 sizeof (struct nfsd_dumplocklist));
3168 if (!error && (dumplocklist.ndllck_size < 1 ||
3169 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3172 error = nfsrv_lookupfilename(&nd,
3173 dumplocklist.ndllck_fname, p);
3175 len = sizeof (struct nfsd_dumplocks) *
3176 dumplocklist.ndllck_size;
3177 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3179 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3180 dumplocklist.ndllck_size, p);
3182 error = copyout(dumplocks,
3183 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3184 free((caddr_t)dumplocks, M_TEMP);
3186 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3189 nfsd_master_pid = procp->p_pid;
3190 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3191 nfsd_master_start = procp->p_stats->p_start;
3192 nfsd_master_proc = procp;
3194 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3195 NFSLOCKV4ROOTMUTEX();
3196 if (suspend_nfsd == 0) {
3197 /* Lock out all nfsd threads */
3199 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3200 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3201 } while (igotlock == 0 && suspend_nfsd == 0);
3204 NFSUNLOCKV4ROOTMUTEX();
3206 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3207 NFSLOCKV4ROOTMUTEX();
3208 if (suspend_nfsd != 0) {
3209 nfsv4_unlock(&nfsd_suspend_lock, 0);
3212 NFSUNLOCKV4ROOTMUTEX();
3222 * Returns 0 if ok, 1 otherwise.
3225 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3230 * This seems odd, but allow the case where the security flavor
3231 * list is empty. This happens when NFSv4 is traversing non-exported
3232 * file systems. Exported file systems should always have a non-empty
3233 * security flavor list.
3235 if (exp->nes_numsecflavor == 0)
3238 for (i = 0; i < exp->nes_numsecflavor; i++) {
3240 * The tests for privacy and integrity must be first,
3241 * since ND_GSS is set for everything but AUTH_SYS.
3243 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3244 (nd->nd_flag & ND_GSSPRIVACY))
3246 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3247 (nd->nd_flag & ND_GSSINTEGRITY))
3249 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3250 (nd->nd_flag & ND_GSS))
3252 if (exp->nes_secflavors[i] == AUTH_SYS &&
3253 (nd->nd_flag & ND_GSS) == 0)
3260 * Calculate a hash value for the fid in a file handle.
3263 nfsrv_hashfh(fhandle_t *fhp)
3267 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3272 * Calculate a hash value for the sessionid.
3275 nfsrv_hashsessionid(uint8_t *sessionid)
3279 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3284 * Signal the userland master nfsd to backup the stable restart file.
3287 nfsrv_backupstable(void)
3291 if (nfsd_master_proc != NULL) {
3292 procp = pfind(nfsd_master_pid);
3293 /* Try to make sure it is the correct process. */
3294 if (procp == nfsd_master_proc &&
3295 procp->p_stats->p_start.tv_sec ==
3296 nfsd_master_start.tv_sec &&
3297 procp->p_stats->p_start.tv_usec ==
3298 nfsd_master_start.tv_usec &&
3299 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3300 kern_psignal(procp, SIGUSR2);
3302 nfsd_master_proc = NULL;
3309 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3312 * Called once to initialize data structures...
3315 nfsd_modevent(module_t mod, int type, void *data)
3318 static int loaded = 0;
3325 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3326 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
3328 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
3331 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
3332 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
3333 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3334 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3341 #ifdef VV_DISABLEDELEG
3342 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3343 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3345 nfsd_call_servertimer = nfsrv_servertimer;
3346 nfsd_call_nfsd = nfssvc_nfsd;
3351 if (newnfs_numnfsd != 0) {
3356 #ifdef VV_DISABLEDELEG
3357 vn_deleg_ops.vndeleg_recall = NULL;
3358 vn_deleg_ops.vndeleg_disable = NULL;
3360 nfsd_call_servertimer = NULL;
3361 nfsd_call_nfsd = NULL;
3363 /* Clean out all NFSv4 state. */
3364 nfsrv_throwawayallstate(curthread);
3366 /* Clean the NFS server reply cache */
3367 nfsrvd_cleancache();
3369 /* Free up the krpc server pool. */
3370 if (nfsrvd_pool != NULL)
3371 svcpool_destroy(nfsrvd_pool);
3373 /* and get rid of the locks */
3374 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3375 mtx_destroy(&nfsrchash_table[i].mtx);
3376 mtx_destroy(&nfsrcahash_table[i].mtx);
3378 mtx_destroy(&nfsrc_udpmtx);
3379 mtx_destroy(&nfs_v4root_mutex);
3380 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3381 for (i = 0; i < nfsrv_sessionhashsize; i++)
3382 mtx_destroy(&nfssessionhash[i].mtx);
3383 lockdestroy(&nfsv4root_mnt.mnt_explock);
3384 free(nfsclienthash, M_NFSDCLIENT);
3385 free(nfslockhash, M_NFSDLOCKFILE);
3386 free(nfssessionhash, M_NFSDSESSION);
3398 static moduledata_t nfsd_mod = {
3403 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3405 /* So that loader and kldload(2) can find us, wherever we are.. */
3406 MODULE_VERSION(nfsd, 1);
3407 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3408 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3409 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3410 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3411 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);