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$");
38 * Functions that perform the vfs operations required by the routines in
39 * nfsd_serv.c. It is hoped that this change will make the server more
43 #include <fs/nfs/nfsport.h>
45 #include <sys/sysctl.h>
46 #include <nlm/nlm_prot.h>
49 FEATURE(nfsd, "NFSv4 server");
51 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
52 extern int nfsrv_useacl;
53 extern int newnfs_numnfsd;
54 extern struct mount nfsv4root_mnt;
55 extern struct nfsrv_stablefirst nfsrv_stablefirst;
56 extern void (*nfsd_call_servertimer)(void);
57 extern SVCPOOL *nfsrvd_pool;
58 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
60 struct mtx nfs_cache_mutex;
61 struct mtx nfs_v4root_mutex;
62 struct nfsrvfh nfs_rootfh, nfs_pubfh;
63 int nfs_pubfhset = 0, nfs_rootfhset = 0;
64 struct proc *nfsd_master_proc = NULL;
65 static pid_t nfsd_master_pid = (pid_t)-1;
66 static char nfsd_master_comm[MAXCOMLEN + 1];
67 static struct timeval nfsd_master_start;
68 static uint32_t nfsv4_sysid = 0;
70 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
73 int nfsrv_enable_crossmntpt = 1;
74 static int nfs_commit_blks;
75 static int nfs_commit_miss;
76 extern int nfsrv_issuedelegs;
77 extern int nfsrv_dolocallocks;
79 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
80 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
81 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
82 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
86 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
87 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
88 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
89 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
91 #define NUM_HEURISTIC 1017
94 #define NHUSE_MAX 2048
96 static struct nfsheur {
97 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
98 off_t nh_nextr; /* next offset for sequential detection */
99 int nh_use; /* use count for selection */
100 int nh_seqcount; /* heuristic */
101 } nfsheur[NUM_HEURISTIC];
105 * Get attributes into nfsvattr structure.
108 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
109 struct thread *p, int vpislocked)
111 int error, lockedit = 0;
113 if (vpislocked == 0) {
115 * When vpislocked == 0, the vnode is either exclusively
116 * locked by this thread or not locked by this thread.
117 * As such, shared lock it, if not exclusively locked.
119 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
121 vn_lock(vp, LK_SHARED | LK_RETRY);
124 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
131 * Get a file handle for a vnode.
134 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
138 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
139 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
140 error = VOP_VPTOFH(vp, &fhp->fh_fid);
145 * Perform access checking for vnodes obtained from file handles that would
146 * refer to files already opened by a Unix client. You cannot just use
147 * vn_writechk() and VOP_ACCESSX() for two reasons.
148 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
150 * 2 - The owner is to be given access irrespective of mode bits for some
151 * operations, so that processes that chmod after opening a file don't
155 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
156 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
157 u_int32_t *supportedtypep)
160 int error = 0, getret = 0;
162 if (vpislocked == 0) {
163 if (vn_lock(vp, LK_SHARED) != 0)
166 if (accmode & VWRITE) {
167 /* Just vn_writechk() changed to check rdonly */
169 * Disallow write attempts on read-only file systems;
170 * unless the file is a socket or a block or character
171 * device resident on the file system.
173 if (NFSVNO_EXRDONLY(exp) ||
174 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
175 switch (vp->v_type) {
185 * If there's shared text associated with
186 * the inode, try to free it up once. If
187 * we fail, we can't allow writing.
189 if ((vp->v_vflag & VV_TEXT) != 0 && error == 0)
199 * Should the override still be applied when ACLs are enabled?
201 error = VOP_ACCESSX(vp, accmode, cred, p);
202 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
204 * Try again with VEXPLICIT_DENY, to see if the test for
205 * deletion is supported.
207 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
209 if (vp->v_type == VDIR) {
210 accmode &= ~(VDELETE | VDELETE_CHILD);
212 error = VOP_ACCESSX(vp, accmode, cred, p);
213 } else if (supportedtypep != NULL) {
214 *supportedtypep &= ~NFSACCESS_DELETE;
220 * Allow certain operations for the owner (reads and writes
221 * on files that are already open).
223 if (override != NFSACCCHK_NOOVERRIDE &&
224 (error == EPERM || error == EACCES)) {
225 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
227 else if (override & NFSACCCHK_ALLOWOWNER) {
228 getret = VOP_GETATTR(vp, &vattr, cred);
229 if (getret == 0 && cred->cr_uid == vattr.va_uid)
239 * Set attribute(s) vnop.
242 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
243 struct thread *p, struct nfsexstuff *exp)
247 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
252 * Set up nameidata for a lookup() call and do it
253 * For the cases where we are crossing mount points
254 * (looking up the public fh path or the v4 root path when
255 * not using a pseudo-root fs), set/release the Giant lock,
259 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
260 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
261 struct vnode **retdirp)
263 struct componentname *cnp = &ndp->ni_cnd;
267 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
268 int error = 0, crossmnt;
272 cnp->cn_nameptr = cnp->cn_pnbuf;
274 * Extract and set starting directory.
276 if (dp->v_type != VDIR) {
281 nfsvno_relpathbuf(ndp);
285 NFSVOPUNLOCK(dp, 0, p);
288 if (NFSVNO_EXRDONLY(exp))
289 cnp->cn_flags |= RDONLY;
290 ndp->ni_segflg = UIO_SYSSPACE;
293 if (nd->nd_flag & ND_PUBLOOKUP) {
295 if (cnp->cn_pnbuf[0] == '/') {
298 * Check for degenerate pathnames here, since lookup()
301 for (i = 1; i < ndp->ni_pathlen; i++)
302 if (cnp->cn_pnbuf[i] != '/')
304 if (i == ndp->ni_pathlen) {
305 error = NFSERR_ACCES;
311 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
312 (nd->nd_flag & ND_NFSV4) == 0) {
314 * Only cross mount points for NFSv4 when doing a
315 * mount while traversing the file system above
316 * the mount point, unless nfsrv_enable_crossmntpt is set.
318 cnp->cn_flags |= NOCROSSMOUNT;
323 * Initialize for scan, set ni_startdir and bump ref on dp again
324 * becuase lookup() will dereference ni_startdir.
328 ndp->ni_startdir = dp;
329 ndp->ni_rootdir = rootvnode;
332 cnp->cn_flags |= LOCKLEAF;
334 cnp->cn_nameptr = cnp->cn_pnbuf;
336 * Call lookup() to do the real work. If an error occurs,
337 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
338 * we do not have to dereference anything before returning.
339 * In either case ni_startdir will be dereferenced and NULLed
347 * Check for encountering a symbolic link. Trivial
348 * termination occurs if no symlink encountered.
350 if ((cnp->cn_flags & ISSYMLINK) == 0) {
351 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
352 nfsvno_relpathbuf(ndp);
353 if (ndp->ni_vp && !lockleaf)
354 NFSVOPUNLOCK(ndp->ni_vp, 0, p);
361 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
362 NFSVOPUNLOCK(ndp->ni_dvp, 0, p);
363 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
368 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
372 if (ndp->ni_pathlen > 1)
373 cp = uma_zalloc(namei_zone, M_WAITOK);
377 aiov.iov_len = MAXPATHLEN;
378 auio.uio_iov = &aiov;
381 auio.uio_rw = UIO_READ;
382 auio.uio_segflg = UIO_SYSSPACE;
384 auio.uio_resid = MAXPATHLEN;
385 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
388 if (ndp->ni_pathlen > 1)
389 uma_zfree(namei_zone, cp);
395 linklen = MAXPATHLEN - auio.uio_resid;
400 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
401 error = ENAMETOOLONG;
406 * Adjust or replace path
408 if (ndp->ni_pathlen > 1) {
409 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
410 uma_zfree(namei_zone, cnp->cn_pnbuf);
413 cnp->cn_pnbuf[linklen] = '\0';
414 ndp->ni_pathlen += linklen;
417 * Cleanup refs for next loop and check if root directory
418 * should replace current directory. Normally ni_dvp
419 * becomes the new base directory and is cleaned up when
420 * we loop. Explicitly null pointers after invalidation
421 * to clarify operation.
426 if (cnp->cn_pnbuf[0] == '/') {
428 ndp->ni_dvp = ndp->ni_rootdir;
431 ndp->ni_startdir = ndp->ni_dvp;
435 cnp->cn_flags &= ~LOCKLEAF;
439 uma_zfree(namei_zone, cnp->cn_pnbuf);
442 ndp->ni_startdir = NULL;
443 cnp->cn_flags &= ~HASBUF;
444 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
451 * Set up a pathname buffer and return a pointer to it and, optionally
452 * set a hash pointer.
455 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
457 struct componentname *cnp = &ndp->ni_cnd;
459 cnp->cn_flags |= (NOMACCHECK | HASBUF);
460 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
463 *bufpp = cnp->cn_pnbuf;
467 * Release the above path buffer, if not released by nfsvno_namei().
470 nfsvno_relpathbuf(struct nameidata *ndp)
473 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
475 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
476 ndp->ni_cnd.cn_flags &= ~HASBUF;
480 * Readlink vnode op into an mbuf list.
483 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
484 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
486 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
487 struct iovec *ivp = iv;
488 struct uio io, *uiop = &io;
489 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
490 int i, len, tlen, error;
494 while (len < NFS_MAXPATHLEN) {
497 mp->m_len = NFSMSIZ(mp);
504 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
505 mp->m_len = NFS_MAXPATHLEN - len;
506 len = NFS_MAXPATHLEN;
510 ivp->iov_base = mtod(mp, caddr_t);
511 ivp->iov_len = mp->m_len;
516 uiop->uio_iovcnt = i;
517 uiop->uio_offset = 0;
518 uiop->uio_resid = len;
519 uiop->uio_rw = UIO_READ;
520 uiop->uio_segflg = UIO_SYSSPACE;
522 error = VOP_READLINK(vp, uiop, cred);
528 if (uiop->uio_resid > 0) {
529 len -= uiop->uio_resid;
530 tlen = NFSM_RNDUP(len);
531 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
540 * Read vnode op call into mbuf list.
543 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
544 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
550 int error = 0, len, left, siz, tlen, ioflag = 0, hi, try = 32;
551 struct mbuf *m2 = NULL, *m3;
552 struct uio io, *uiop = &io;
556 * Calculate seqcount for heuristic
559 * Locate best candidate
562 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
566 if (nfsheur[hi].nh_vp == vp) {
570 if (nfsheur[hi].nh_use > 0)
571 --nfsheur[hi].nh_use;
572 hi = (hi + 1) % NUM_HEURISTIC;
573 if (nfsheur[hi].nh_use < nh->nh_use)
577 if (nh->nh_vp != vp) {
580 nh->nh_use = NHUSE_INIT;
588 * Calculate heuristic
591 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
592 if (++nh->nh_seqcount > IO_SEQMAX)
593 nh->nh_seqcount = IO_SEQMAX;
594 } else if (nh->nh_seqcount > 1) {
599 nh->nh_use += NHUSE_INC;
600 if (nh->nh_use > NHUSE_MAX)
601 nh->nh_use = NHUSE_MAX;
602 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
604 len = left = NFSM_RNDUP(cnt);
607 * Generate the mbuf list with the uio_iov ref. to it.
614 siz = min(M_TRAILINGSPACE(m), left);
623 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
625 uiop->uio_iov = iv2 = iv;
631 panic("nfsvno_read iov");
632 siz = min(M_TRAILINGSPACE(m), left);
634 iv->iov_base = mtod(m, caddr_t) + m->m_len;
643 uiop->uio_iovcnt = i;
644 uiop->uio_offset = off;
645 uiop->uio_resid = len;
646 uiop->uio_rw = UIO_READ;
647 uiop->uio_segflg = UIO_SYSSPACE;
648 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
649 FREE((caddr_t)iv2, M_TEMP);
655 tlen = len - uiop->uio_resid;
656 cnt = cnt < tlen ? cnt : tlen;
657 tlen = NFSM_RNDUP(cnt);
661 } else if (len != tlen || tlen != cnt)
662 nfsrv_adj(m3, len - tlen, tlen - cnt);
669 * Write vnode op from an mbuf list.
672 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
673 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
679 struct uio io, *uiop = &io;
681 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
683 uiop->uio_iov = iv = ivp;
684 uiop->uio_iovcnt = cnt;
685 i = mtod(mp, caddr_t) + mp->m_len - cp;
689 panic("nfsvno_write");
700 cp = mtod(mp, caddr_t);
704 if (stable == NFSWRITE_UNSTABLE)
705 ioflags = IO_NODELOCKED;
707 ioflags = (IO_SYNC | IO_NODELOCKED);
708 uiop->uio_resid = retlen;
709 uiop->uio_rw = UIO_WRITE;
710 uiop->uio_segflg = UIO_SYSSPACE;
712 uiop->uio_offset = off;
713 error = VOP_WRITE(vp, uiop, ioflags, cred);
714 FREE((caddr_t)iv, M_TEMP);
719 * Common code for creating a regular file (plus special files for V2).
722 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
723 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
724 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
729 error = nd->nd_repstat;
730 if (!error && ndp->ni_vp == NULL) {
731 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
732 vrele(ndp->ni_startdir);
733 error = VOP_CREATE(ndp->ni_dvp,
734 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
736 nfsvno_relpathbuf(ndp);
738 if (*exclusive_flagp) {
739 *exclusive_flagp = 0;
740 NFSVNO_ATTRINIT(nvap);
741 nvap->na_atime.tv_sec = cverf[0];
742 nvap->na_atime.tv_nsec = cverf[1];
743 error = VOP_SETATTR(ndp->ni_vp,
744 &nvap->na_vattr, nd->nd_cred);
748 * NFS V2 Only. nfsrvd_mknod() does this for V3.
749 * (This implies, just get out on an error.)
751 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
752 nvap->na_type == VFIFO) {
753 if (nvap->na_type == VCHR && rdev == 0xffffffff)
754 nvap->na_type = VFIFO;
755 if (nvap->na_type != VFIFO &&
756 (error = priv_check_cred(nd->nd_cred,
757 PRIV_VFS_MKNOD_DEV, 0))) {
758 vrele(ndp->ni_startdir);
759 nfsvno_relpathbuf(ndp);
763 nvap->na_rdev = rdev;
764 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
765 &ndp->ni_cnd, &nvap->na_vattr);
767 nfsvno_relpathbuf(ndp);
768 vrele(ndp->ni_startdir);
772 vrele(ndp->ni_startdir);
773 nfsvno_relpathbuf(ndp);
780 * Handle cases where error is already set and/or
782 * 1 - clean up the lookup
783 * 2 - iff !error and na_size set, truncate it
785 vrele(ndp->ni_startdir);
786 nfsvno_relpathbuf(ndp);
788 if (ndp->ni_dvp == *vpp)
792 if (!error && nvap->na_size != VNOVAL) {
793 error = nfsvno_accchk(*vpp, VWRITE,
794 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
795 NFSACCCHK_VPISLOCKED, NULL);
797 tempsize = nvap->na_size;
798 NFSVNO_ATTRINIT(nvap);
799 nvap->na_size = tempsize;
800 error = VOP_SETATTR(*vpp,
801 &nvap->na_vattr, nd->nd_cred);
811 * Do a mknod vnode op.
814 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
820 vtyp = nvap->na_type;
822 * Iff doesn't exist, create it.
825 vrele(ndp->ni_startdir);
826 nfsvno_relpathbuf(ndp);
831 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
832 vrele(ndp->ni_startdir);
833 nfsvno_relpathbuf(ndp);
835 return (NFSERR_BADTYPE);
838 vrele(ndp->ni_startdir);
839 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
840 &ndp->ni_cnd, &nvap->na_vattr);
842 nfsvno_relpathbuf(ndp);
844 if (nvap->na_type != VFIFO &&
845 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
846 vrele(ndp->ni_startdir);
847 nfsvno_relpathbuf(ndp);
851 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
852 &ndp->ni_cnd, &nvap->na_vattr);
854 nfsvno_relpathbuf(ndp);
855 vrele(ndp->ni_startdir);
857 * Since VOP_MKNOD returns the ni_vp, I can't
858 * see any reason to do the lookup.
868 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
869 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
873 if (ndp->ni_vp != NULL) {
874 if (ndp->ni_dvp == ndp->ni_vp)
879 nfsvno_relpathbuf(ndp);
882 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
885 nfsvno_relpathbuf(ndp);
893 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
894 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
895 struct nfsexstuff *exp)
900 vrele(ndp->ni_startdir);
901 nfsvno_relpathbuf(ndp);
902 if (ndp->ni_dvp == ndp->ni_vp)
910 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
911 &nvap->na_vattr, pathcp);
913 vrele(ndp->ni_startdir);
914 nfsvno_relpathbuf(ndp);
916 * Although FreeBSD still had the lookup code in
917 * it for 7/current, there doesn't seem to be any
918 * point, since VOP_SYMLINK() returns the ni_vp.
919 * Just vput it for v2.
921 if (!not_v2 && !error)
927 * Parse symbolic link arguments.
928 * This function has an ugly side effect. It will MALLOC() an area for
929 * the symlink and set iov_base to point to it, only if it succeeds.
930 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
934 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
935 struct thread *p, char **pathcpp, int *lenp)
940 struct nfsv2_sattr *sp;
944 if ((nd->nd_flag & ND_NFSV3) &&
945 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
947 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
948 len = fxdr_unsigned(int, *tl);
949 if (len > NFS_MAXPATHLEN || len <= 0) {
953 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
954 error = nfsrv_mtostr(nd, pathcp, len);
957 if (nd->nd_flag & ND_NFSV2) {
958 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
959 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
966 free(pathcp, M_TEMP);
971 * Remove a non-directory object.
974 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
975 struct thread *p, struct nfsexstuff *exp)
981 if (vp->v_type == VDIR)
982 error = NFSERR_ISDIR;
984 error = nfsrv_checkremove(vp, 1, p);
986 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
987 if (ndp->ni_dvp == vp)
996 * Remove a directory.
999 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1000 struct thread *p, struct nfsexstuff *exp)
1006 if (vp->v_type != VDIR) {
1011 * No rmdir "." please.
1013 if (ndp->ni_dvp == vp) {
1018 * The root of a mounted filesystem cannot be deleted.
1020 if (vp->v_vflag & VV_ROOT)
1024 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1025 if (ndp->ni_dvp == vp)
1037 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1038 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1040 struct vnode *fvp, *tvp, *tdvp;
1043 fvp = fromndp->ni_vp;
1045 vrele(fromndp->ni_dvp);
1050 tdvp = tondp->ni_dvp;
1053 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1054 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1056 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1057 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1060 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1061 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1066 * A rename to '.' or '..' results in a prematurely
1067 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1070 if ((tondp->ni_cnd.cn_namelen == 1 &&
1071 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1072 (tondp->ni_cnd.cn_namelen == 2 &&
1073 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1074 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1079 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1080 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1083 if (fvp->v_mount != tdvp->v_mount) {
1084 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1088 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1093 * If source and destination are the same, there is nothing to
1094 * do. Set error to -1 to indicate this.
1099 if (ndflag & ND_NFSV4) {
1100 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1101 error = nfsrv_checkremove(fvp, 0, p);
1106 error = nfsrv_checkremove(tvp, 1, p);
1109 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1110 * that the NFSv4 client won't be confused by the rename.
1111 * Since nfsd_recalldelegation() can only be called on an
1112 * unlocked vnode at this point and fvp is the file that will
1113 * still exist after the rename, just do fvp.
1115 nfsd_recalldelegation(fvp, p);
1119 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1120 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1129 vrele(fromndp->ni_dvp);
1134 vrele(tondp->ni_startdir);
1135 nfsvno_relpathbuf(tondp);
1137 vrele(fromndp->ni_startdir);
1138 nfsvno_relpathbuf(fromndp);
1146 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1147 struct thread *p, struct nfsexstuff *exp)
1157 if (vp->v_mount != xp->v_mount)
1161 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1162 if ((vp->v_iflag & VI_DOOMED) == 0)
1163 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1166 if (ndp->ni_dvp == vp)
1172 if (ndp->ni_dvp == ndp->ni_vp)
1179 nfsvno_relpathbuf(ndp);
1184 * Do the fsync() appropriate for the commit.
1187 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1192 if (cnt > MAX_COMMIT_COUNT) {
1194 * Give up and do the whole thing
1197 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1198 VM_OBJECT_LOCK(vp->v_object);
1199 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1200 VM_OBJECT_UNLOCK(vp->v_object);
1202 error = VOP_FSYNC(vp, MNT_WAIT, td);
1205 * Locate and synchronously write any buffers that fall
1206 * into the requested range. Note: we are assuming that
1207 * f_iosize is a power of 2.
1209 int iosize = vp->v_mount->mnt_stat.f_iosize;
1210 int iomask = iosize - 1;
1215 * Align to iosize boundry, super-align to page boundry.
1218 cnt += off & iomask;
1219 off &= ~(u_quad_t)iomask;
1221 if (off & PAGE_MASK) {
1222 cnt += off & PAGE_MASK;
1223 off &= ~(u_quad_t)PAGE_MASK;
1225 lblkno = off / iosize;
1228 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1229 VM_OBJECT_LOCK(vp->v_object);
1230 vm_object_page_clean(vp->v_object, off, off + cnt,
1232 VM_OBJECT_UNLOCK(vp->v_object);
1241 * If we have a buffer and it is marked B_DELWRI we
1242 * have to lock and write it. Otherwise the prior
1243 * write is assumed to have already been committed.
1245 * gbincore() can return invalid buffers now so we
1246 * have to check that bit as well (though B_DELWRI
1247 * should not be set if B_INVAL is set there could be
1248 * a race here since we haven't locked the buffer).
1250 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1251 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1252 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1254 continue; /* retry */
1256 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1259 bp->b_flags &= ~B_ASYNC;
1281 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1285 error = VFS_STATFS(vp->v_mount, sf);
1288 * Since NFS handles these values as unsigned on the
1289 * wire, there is no way to represent negative values,
1290 * so set them to 0. Without this, they will appear
1291 * to be very large positive values for clients like
1294 if (sf->f_bavail < 0)
1296 if (sf->f_ffree < 0)
1303 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1304 * must handle nfsrv_opencheck() calls after any other access checks.
1307 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1308 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1309 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1310 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1311 struct nfsexstuff *exp, struct vnode **vpp)
1313 struct vnode *vp = NULL;
1315 struct nfsexstuff nes;
1317 if (ndp->ni_vp == NULL)
1318 nd->nd_repstat = nfsrv_opencheck(clientid,
1319 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1320 if (!nd->nd_repstat) {
1321 if (ndp->ni_vp == NULL) {
1322 vrele(ndp->ni_startdir);
1323 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1324 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1326 nfsvno_relpathbuf(ndp);
1327 if (!nd->nd_repstat) {
1328 if (*exclusive_flagp) {
1329 *exclusive_flagp = 0;
1330 NFSVNO_ATTRINIT(nvap);
1331 nvap->na_atime.tv_sec = cverf[0];
1332 nvap->na_atime.tv_nsec = cverf[1];
1333 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1334 &nvap->na_vattr, cred);
1336 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1337 aclp, p, attrbitp, exp);
1342 if (ndp->ni_startdir)
1343 vrele(ndp->ni_startdir);
1344 nfsvno_relpathbuf(ndp);
1346 if (create == NFSV4OPEN_CREATE) {
1347 if (ndp->ni_dvp == vp)
1352 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1353 if (ndp->ni_cnd.cn_flags & RDONLY)
1354 NFSVNO_SETEXRDONLY(&nes);
1356 NFSVNO_EXINIT(&nes);
1357 nd->nd_repstat = nfsvno_accchk(vp,
1358 VWRITE, cred, &nes, p,
1359 NFSACCCHK_NOOVERRIDE,
1360 NFSACCCHK_VPISLOCKED, NULL);
1361 nd->nd_repstat = nfsrv_opencheck(clientid,
1362 stateidp, stp, vp, nd, p, nd->nd_repstat);
1363 if (!nd->nd_repstat) {
1364 tempsize = nvap->na_size;
1365 NFSVNO_ATTRINIT(nvap);
1366 nvap->na_size = tempsize;
1367 nd->nd_repstat = VOP_SETATTR(vp,
1368 &nvap->na_vattr, cred);
1370 } else if (vp->v_type == VREG) {
1371 nd->nd_repstat = nfsrv_opencheck(clientid,
1372 stateidp, stp, vp, nd, p, nd->nd_repstat);
1376 if (ndp->ni_cnd.cn_flags & HASBUF)
1377 nfsvno_relpathbuf(ndp);
1378 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1379 vrele(ndp->ni_startdir);
1380 if (ndp->ni_dvp == ndp->ni_vp)
1392 * Updates the file rev and sets the mtime and ctime
1393 * to the current clock time, returning the va_filerev and va_Xtime
1397 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1398 struct ucred *cred, struct thread *p)
1403 getnanotime(&va.va_mtime);
1404 (void) VOP_SETATTR(vp, &va, cred);
1405 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1409 * Glue routine to nfsv4_fillattr().
1412 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1413 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1414 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1415 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1419 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1420 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1425 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1427 * nfs readdir service
1428 * - mallocs what it thinks is enough to read
1429 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1430 * - calls VOP_READDIR()
1431 * - loops around building the reply
1432 * if the output generated exceeds count break out of loop
1433 * The NFSM_CLGET macro is used here so that the reply will be packed
1434 * tightly in mbuf clusters.
1435 * - it trims out records with d_fileno == 0
1436 * this doesn't matter for Unix clients, but they might confuse clients
1438 * - it trims out records with d_type == DT_WHT
1439 * these cannot be seen through NFS (unless we extend the protocol)
1440 * The alternate call nfsrvd_readdirplus() does lookups as well.
1441 * PS: The NFS protocol spec. does not clarify what the "count" byte
1442 * argument is a count of.. just name strings and file id's or the
1443 * entire reply rpc or ...
1444 * I tried just file name and id sizes and it confused the Sun client,
1445 * so I am using the full rpc size now. The "paranoia.." comment refers
1446 * to including the status longwords that are not a part of the dir.
1447 * "entry" structures, but are in the rpc.
1450 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1451 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1456 char *cpos, *cend, *rbuf;
1458 int nlen, error = 0, getret = 1;
1459 int siz, cnt, fullsiz, eofflag, ncookies;
1460 u_int64_t off, toff, verf;
1461 u_long *cookies = NULL, *cookiep;
1466 if (nd->nd_repstat) {
1467 nfsrv_postopattr(nd, getret, &at);
1470 if (nd->nd_flag & ND_NFSV2) {
1471 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1472 off = fxdr_unsigned(u_quad_t, *tl++);
1474 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1475 off = fxdr_hyper(tl);
1477 verf = fxdr_hyper(tl);
1481 cnt = fxdr_unsigned(int, *tl);
1482 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1483 cnt = NFS_SRVMAXDATA(nd);
1484 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1486 if (nd->nd_flag & ND_NFSV3) {
1487 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1491 * va_filerev is not sufficient as a cookie verifier,
1492 * since it is not supposed to change when entries are
1493 * removed/added unless that offset cookies returned to
1494 * the client are no longer valid.
1496 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1497 nd->nd_repstat = NFSERR_BAD_COOKIE;
1500 if (nd->nd_repstat == 0 && cnt == 0) {
1501 if (nd->nd_flag & ND_NFSV2)
1502 /* NFSv2 does not have NFSERR_TOOSMALL */
1503 nd->nd_repstat = EPERM;
1505 nd->nd_repstat = NFSERR_TOOSMALL;
1507 if (!nd->nd_repstat)
1508 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1509 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1510 NFSACCCHK_VPISLOCKED, NULL);
1511 if (nd->nd_repstat) {
1513 if (nd->nd_flag & ND_NFSV3)
1514 nfsrv_postopattr(nd, getret, &at);
1517 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1518 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1522 free((caddr_t)cookies, M_TEMP);
1530 io.uio_offset = (off_t)off;
1532 io.uio_segflg = UIO_SYSSPACE;
1533 io.uio_rw = UIO_READ;
1535 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1537 off = (u_int64_t)io.uio_offset;
1539 siz -= io.uio_resid;
1541 if (!cookies && !nd->nd_repstat)
1542 nd->nd_repstat = NFSERR_PERM;
1543 if (nd->nd_flag & ND_NFSV3) {
1544 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1545 if (!nd->nd_repstat)
1546 nd->nd_repstat = getret;
1550 * Handles the failed cases. nd->nd_repstat == 0 past here.
1552 if (nd->nd_repstat) {
1554 free((caddr_t)rbuf, M_TEMP);
1556 free((caddr_t)cookies, M_TEMP);
1557 if (nd->nd_flag & ND_NFSV3)
1558 nfsrv_postopattr(nd, getret, &at);
1562 * If nothing read, return eof
1567 if (nd->nd_flag & ND_NFSV2) {
1568 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1570 nfsrv_postopattr(nd, getret, &at);
1571 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1572 txdr_hyper(at.na_filerev, tl);
1575 *tl++ = newnfs_false;
1577 FREE((caddr_t)rbuf, M_TEMP);
1578 FREE((caddr_t)cookies, M_TEMP);
1583 * Check for degenerate cases of nothing useful read.
1584 * If so go try again
1588 dp = (struct dirent *)cpos;
1592 * For some reason FreeBSD's ufs_readdir() chooses to back the
1593 * directory offset up to a block boundary, so it is necessary to
1594 * skip over the records that precede the requested offset. This
1595 * requires the assumption that file offset cookies monotonically
1597 * Since the offset cookies don't monotonically increase for ZFS,
1598 * this is not done when ZFS is the file system.
1600 while (cpos < cend && ncookies > 0 &&
1601 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1602 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1603 cpos += dp->d_reclen;
1604 dp = (struct dirent *)cpos;
1608 if (cpos >= cend || ncookies == 0) {
1616 * dirlen is the size of the reply, including all XDR and must
1617 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1618 * if the XDR should be included in "count", but to be safe, we do.
1619 * (Include the two booleans at the end of the reply in dirlen now.)
1621 if (nd->nd_flag & ND_NFSV3) {
1622 nfsrv_postopattr(nd, getret, &at);
1623 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1624 txdr_hyper(at.na_filerev, tl);
1625 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1627 dirlen = 2 * NFSX_UNSIGNED;
1630 /* Loop through the records and build reply */
1631 while (cpos < cend && ncookies > 0) {
1632 nlen = dp->d_namlen;
1633 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1634 nlen <= NFS_MAXNAMLEN) {
1635 if (nd->nd_flag & ND_NFSV3)
1636 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1638 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1645 * Build the directory record xdr from
1648 if (nd->nd_flag & ND_NFSV3) {
1649 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1650 *tl++ = newnfs_true;
1653 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1654 *tl++ = newnfs_true;
1656 *tl = txdr_unsigned(dp->d_fileno);
1657 (void) nfsm_strtom(nd, dp->d_name, nlen);
1658 if (nd->nd_flag & ND_NFSV3) {
1659 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1662 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1663 *tl = txdr_unsigned(*cookiep);
1665 cpos += dp->d_reclen;
1666 dp = (struct dirent *)cpos;
1672 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1673 *tl++ = newnfs_false;
1678 FREE((caddr_t)rbuf, M_TEMP);
1679 FREE((caddr_t)cookies, M_TEMP);
1687 * Readdirplus for V3 and Readdir for V4.
1690 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1691 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1696 char *cpos, *cend, *rbuf;
1699 struct nfsvattr nva, at, *nvap = &nva;
1700 struct mbuf *mb0, *mb1;
1701 struct nfsreferral *refp;
1702 int nlen, r, error = 0, getret = 1, usevget = 1;
1703 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1704 caddr_t bpos0, bpos1;
1705 u_int64_t off, toff, verf;
1706 u_long *cookies = NULL, *cookiep;
1707 nfsattrbit_t attrbits, rderrbits, savbits;
1710 struct componentname cn;
1711 int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1712 struct mount *mp, *new_mp;
1713 uint64_t mounted_on_fileno;
1715 if (nd->nd_repstat) {
1716 nfsrv_postopattr(nd, getret, &at);
1719 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1720 off = fxdr_hyper(tl);
1723 verf = fxdr_hyper(tl);
1725 siz = fxdr_unsigned(int, *tl++);
1726 cnt = fxdr_unsigned(int, *tl);
1729 * Use the server's maximum data transfer size as the upper bound
1732 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1733 cnt = NFS_SRVMAXDATA(nd);
1736 * siz is a "hint" of how much directory information (name, fileid,
1737 * cookie) should be in the reply. At least one client "hints" 0,
1738 * so I set it to cnt for that case. I also round it up to the
1739 * next multiple of DIRBLKSIZ.
1743 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1745 if (nd->nd_flag & ND_NFSV4) {
1746 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1749 NFSSET_ATTRBIT(&savbits, &attrbits);
1750 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1751 NFSZERO_ATTRBIT(&rderrbits);
1752 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1754 NFSZERO_ATTRBIT(&attrbits);
1757 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1758 if (!nd->nd_repstat) {
1759 if (off && verf != at.na_filerev) {
1761 * va_filerev is not sufficient as a cookie verifier,
1762 * since it is not supposed to change when entries are
1763 * removed/added unless that offset cookies returned to
1764 * the client are no longer valid.
1767 if (nd->nd_flag & ND_NFSV4) {
1768 nd->nd_repstat = NFSERR_NOTSAME;
1770 nd->nd_repstat = NFSERR_BAD_COOKIE;
1773 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1774 nd->nd_repstat = NFSERR_BAD_COOKIE;
1777 if (!nd->nd_repstat && vp->v_type != VDIR)
1778 nd->nd_repstat = NFSERR_NOTDIR;
1779 if (!nd->nd_repstat && cnt == 0)
1780 nd->nd_repstat = NFSERR_TOOSMALL;
1781 if (!nd->nd_repstat)
1782 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1783 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1784 NFSACCCHK_VPISLOCKED, NULL);
1785 if (nd->nd_repstat) {
1787 if (nd->nd_flag & ND_NFSV3)
1788 nfsrv_postopattr(nd, getret, &at);
1791 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1793 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1797 free((caddr_t)cookies, M_TEMP);
1805 io.uio_offset = (off_t)off;
1807 io.uio_segflg = UIO_SYSSPACE;
1808 io.uio_rw = UIO_READ;
1810 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1812 off = (u_int64_t)io.uio_offset;
1814 siz -= io.uio_resid;
1816 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1818 if (!cookies && !nd->nd_repstat)
1819 nd->nd_repstat = NFSERR_PERM;
1820 if (!nd->nd_repstat)
1821 nd->nd_repstat = getret;
1822 if (nd->nd_repstat) {
1825 free((caddr_t)cookies, M_TEMP);
1826 free((caddr_t)rbuf, M_TEMP);
1827 if (nd->nd_flag & ND_NFSV3)
1828 nfsrv_postopattr(nd, getret, &at);
1832 * If nothing read, return eof
1837 if (nd->nd_flag & ND_NFSV3)
1838 nfsrv_postopattr(nd, getret, &at);
1839 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1840 txdr_hyper(at.na_filerev, tl);
1842 *tl++ = newnfs_false;
1844 free((caddr_t)cookies, M_TEMP);
1845 free((caddr_t)rbuf, M_TEMP);
1850 * Check for degenerate cases of nothing useful read.
1851 * If so go try again
1855 dp = (struct dirent *)cpos;
1859 * For some reason FreeBSD's ufs_readdir() chooses to back the
1860 * directory offset up to a block boundary, so it is necessary to
1861 * skip over the records that precede the requested offset. This
1862 * requires the assumption that file offset cookies monotonically
1864 * Since the offset cookies don't monotonically increase for ZFS,
1865 * this is not done when ZFS is the file system.
1867 while (cpos < cend && ncookies > 0 &&
1868 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1869 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1870 ((nd->nd_flag & ND_NFSV4) &&
1871 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1872 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1873 cpos += dp->d_reclen;
1874 dp = (struct dirent *)cpos;
1878 if (cpos >= cend || ncookies == 0) {
1885 * Busy the file system so that the mount point won't go away
1886 * and, as such, VFS_VGET() can be used safely.
1891 nd->nd_repstat = vfs_busy(mp, 0);
1893 if (nd->nd_repstat != 0) {
1895 free(cookies, M_TEMP);
1897 if (nd->nd_flag & ND_NFSV3)
1898 nfsrv_postopattr(nd, getret, &at);
1903 * Save this position, in case there is an error before one entry
1907 bpos0 = nd->nd_bpos;
1910 * Fill in the first part of the reply.
1911 * dirlen is the reply length in bytes and cannot exceed cnt.
1912 * (Include the two booleans at the end of the reply in dirlen now,
1913 * so we recognize when we have exceeded cnt.)
1915 if (nd->nd_flag & ND_NFSV3) {
1916 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1917 nfsrv_postopattr(nd, getret, &at);
1919 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
1921 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1922 txdr_hyper(at.na_filerev, tl);
1925 * Save this position, in case there is an empty reply needed.
1928 bpos1 = nd->nd_bpos;
1930 /* Loop through the records and build reply */
1932 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
1933 nlen = dp->d_namlen;
1934 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1935 nlen <= NFS_MAXNAMLEN &&
1936 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
1937 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
1938 || (nlen == 1 && dp->d_name[0] != '.'))) {
1940 * Save the current position in the reply, in case
1941 * this entry exceeds cnt.
1944 bpos1 = nd->nd_bpos;
1947 * For readdir_and_lookup get the vnode using
1956 mounted_on_fileno = (uint64_t)dp->d_fileno;
1957 if ((nd->nd_flag & ND_NFSV3) ||
1958 NFSNONZERO_ATTRBIT(&savbits)) {
1959 if (nd->nd_flag & ND_NFSV4)
1960 refp = nfsv4root_getreferral(NULL,
1964 r = VFS_VGET(mp, dp->d_fileno,
1968 if (r == EOPNOTSUPP) {
1971 cn.cn_nameiop = LOOKUP;
1979 cn.cn_nameptr = dp->d_name;
1980 cn.cn_namelen = nlen;
1981 cn.cn_flags = ISLASTCN |
1982 NOFOLLOW | LOCKLEAF |
1985 dp->d_name[0] == '.' &&
1986 dp->d_name[1] == '.')
1989 if (vn_lock(vp, LK_SHARED)
1991 nd->nd_repstat = EPERM;
1994 if ((vp->v_vflag & VV_ROOT) != 0
1995 && (cn.cn_flags & ISDOTDOT)
2001 r = VOP_LOOKUP(vp, &nvp,
2010 * For NFSv4, check to see if nvp is
2011 * a mount point and get the mount
2012 * point vnode, as required.
2015 nfsrv_enable_crossmntpt != 0 &&
2016 (nd->nd_flag & ND_NFSV4) != 0 &&
2017 nvp->v_type == VDIR &&
2018 nvp->v_mountedhere != NULL) {
2019 new_mp = nvp->v_mountedhere;
2020 r = vfs_busy(new_mp, 0);
2024 r = VFS_ROOT(new_mp,
2034 ((nd->nd_flag & ND_NFSV3) ||
2035 NFSNONZERO_ATTRBIT(&attrbits))) {
2036 r = nfsvno_getfh(nvp, &nfh, p);
2038 r = nfsvno_getattr(nvp, nvap,
2045 if (!NFSISSET_ATTRBIT(&attrbits,
2046 NFSATTRBIT_RDATTRERROR)) {
2049 if (needs_unbusy != 0)
2058 * Build the directory record xdr
2060 if (nd->nd_flag & ND_NFSV3) {
2061 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2062 *tl++ = newnfs_true;
2064 *tl = txdr_unsigned(dp->d_fileno);
2065 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2066 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2068 *tl = txdr_unsigned(*cookiep);
2069 nfsrv_postopattr(nd, 0, nvap);
2070 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2071 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2075 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2076 *tl++ = newnfs_true;
2078 *tl = txdr_unsigned(*cookiep);
2079 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2081 supports_nfsv4acls =
2082 nfs_supportsnfsv4acls(nvp);
2085 supports_nfsv4acls = 0;
2087 dirlen += nfsrv_putreferralattr(nd,
2090 if (nd->nd_repstat) {
2093 if (needs_unbusy != 0)
2098 dirlen += nfsvno_fillattr(nd, new_mp,
2099 nvp, nvap, &nfh, r, &rderrbits,
2100 nd->nd_cred, p, isdgram, 0,
2101 supports_nfsv4acls, at_root,
2104 dirlen += nfsvno_fillattr(nd, new_mp,
2105 nvp, nvap, &nfh, r, &attrbits,
2106 nd->nd_cred, p, isdgram, 0,
2107 supports_nfsv4acls, at_root,
2112 dirlen += (3 * NFSX_UNSIGNED);
2114 if (needs_unbusy != 0)
2119 cpos += dp->d_reclen;
2120 dp = (struct dirent *)cpos;
2128 * If dirlen > cnt, we must strip off the last entry. If that
2129 * results in an empty reply, report NFSERR_TOOSMALL.
2131 if (dirlen > cnt || nd->nd_repstat) {
2132 if (!nd->nd_repstat && entrycnt == 0)
2133 nd->nd_repstat = NFSERR_TOOSMALL;
2135 newnfs_trimtrailing(nd, mb0, bpos0);
2137 newnfs_trimtrailing(nd, mb1, bpos1);
2139 } else if (cpos < cend)
2141 if (!nd->nd_repstat) {
2142 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2143 *tl++ = newnfs_false;
2149 FREE((caddr_t)cookies, M_TEMP);
2150 FREE((caddr_t)rbuf, M_TEMP);
2158 * Get the settable attributes out of the mbuf list.
2159 * (Return 0 or EBADRPC)
2162 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2163 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2166 struct nfsv2_sattr *sp;
2167 struct timeval curtime;
2168 int error = 0, toclient = 0;
2170 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2172 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2174 * Some old clients didn't fill in the high order 16bits.
2175 * --> check the low order 2 bytes for 0xffff
2177 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2178 nvap->na_mode = nfstov_mode(sp->sa_mode);
2179 if (sp->sa_uid != newnfs_xdrneg1)
2180 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2181 if (sp->sa_gid != newnfs_xdrneg1)
2182 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2183 if (sp->sa_size != newnfs_xdrneg1)
2184 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2185 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2187 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2189 nvap->na_atime.tv_sec =
2190 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2191 nvap->na_atime.tv_nsec = 0;
2194 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2195 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2198 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2199 if (*tl == newnfs_true) {
2200 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2201 nvap->na_mode = nfstov_mode(*tl);
2203 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2204 if (*tl == newnfs_true) {
2205 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2206 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2208 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2209 if (*tl == newnfs_true) {
2210 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2211 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2213 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2214 if (*tl == newnfs_true) {
2215 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2216 nvap->na_size = fxdr_hyper(tl);
2218 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2219 switch (fxdr_unsigned(int, *tl)) {
2220 case NFSV3SATTRTIME_TOCLIENT:
2221 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2222 fxdr_nfsv3time(tl, &nvap->na_atime);
2225 case NFSV3SATTRTIME_TOSERVER:
2226 NFSGETTIME(&curtime);
2227 nvap->na_atime.tv_sec = curtime.tv_sec;
2228 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2229 nvap->na_vaflags |= VA_UTIMES_NULL;
2232 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2233 switch (fxdr_unsigned(int, *tl)) {
2234 case NFSV3SATTRTIME_TOCLIENT:
2235 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2236 fxdr_nfsv3time(tl, &nvap->na_mtime);
2237 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2239 case NFSV3SATTRTIME_TOSERVER:
2240 NFSGETTIME(&curtime);
2241 nvap->na_mtime.tv_sec = curtime.tv_sec;
2242 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2244 nvap->na_vaflags |= VA_UTIMES_NULL;
2249 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2256 * Handle the setable attributes for V4.
2257 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2260 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2261 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2266 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2268 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2271 struct timeval curtime;
2273 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2276 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2277 attrsize = fxdr_unsigned(int, *tl);
2280 * Loop around getting the setable attributes. If an unsupported
2281 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2284 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2285 bitpos = NFSATTRBIT_MAX;
2289 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2290 if (attrsum > attrsize) {
2291 error = NFSERR_BADXDR;
2294 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2296 case NFSATTRBIT_SIZE:
2297 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2298 nvap->na_size = fxdr_hyper(tl);
2299 attrsum += NFSX_HYPER;
2301 case NFSATTRBIT_ACL:
2302 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2306 if (aceerr && !nd->nd_repstat)
2307 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2310 case NFSATTRBIT_ARCHIVE:
2311 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2312 if (!nd->nd_repstat)
2313 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2314 attrsum += NFSX_UNSIGNED;
2316 case NFSATTRBIT_HIDDEN:
2317 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2318 if (!nd->nd_repstat)
2319 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2320 attrsum += NFSX_UNSIGNED;
2322 case NFSATTRBIT_MIMETYPE:
2323 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2324 i = fxdr_unsigned(int, *tl);
2325 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2328 if (!nd->nd_repstat)
2329 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2330 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2332 case NFSATTRBIT_MODE:
2333 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2334 nvap->na_mode = nfstov_mode(*tl);
2335 attrsum += NFSX_UNSIGNED;
2337 case NFSATTRBIT_OWNER:
2338 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2339 j = fxdr_unsigned(int, *tl);
2341 return (NFSERR_BADXDR);
2342 if (j > NFSV4_SMALLSTR)
2343 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2346 error = nfsrv_mtostr(nd, cp, j);
2348 if (j > NFSV4_SMALLSTR)
2349 free(cp, M_NFSSTRING);
2352 if (!nd->nd_repstat) {
2353 nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p);
2354 if (!nd->nd_repstat)
2357 if (j > NFSV4_SMALLSTR)
2358 free(cp, M_NFSSTRING);
2359 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2361 case NFSATTRBIT_OWNERGROUP:
2362 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2363 j = fxdr_unsigned(int, *tl);
2365 return (NFSERR_BADXDR);
2366 if (j > NFSV4_SMALLSTR)
2367 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2370 error = nfsrv_mtostr(nd, cp, j);
2372 if (j > NFSV4_SMALLSTR)
2373 free(cp, M_NFSSTRING);
2376 if (!nd->nd_repstat) {
2377 nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p);
2378 if (!nd->nd_repstat)
2381 if (j > NFSV4_SMALLSTR)
2382 free(cp, M_NFSSTRING);
2383 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2385 case NFSATTRBIT_SYSTEM:
2386 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2387 if (!nd->nd_repstat)
2388 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2389 attrsum += NFSX_UNSIGNED;
2391 case NFSATTRBIT_TIMEACCESSSET:
2392 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2393 attrsum += NFSX_UNSIGNED;
2394 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2395 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2396 fxdr_nfsv4time(tl, &nvap->na_atime);
2398 attrsum += NFSX_V4TIME;
2400 NFSGETTIME(&curtime);
2401 nvap->na_atime.tv_sec = curtime.tv_sec;
2402 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2403 nvap->na_vaflags |= VA_UTIMES_NULL;
2406 case NFSATTRBIT_TIMEBACKUP:
2407 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2408 if (!nd->nd_repstat)
2409 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2410 attrsum += NFSX_V4TIME;
2412 case NFSATTRBIT_TIMECREATE:
2413 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2414 if (!nd->nd_repstat)
2415 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2416 attrsum += NFSX_V4TIME;
2418 case NFSATTRBIT_TIMEMODIFYSET:
2419 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2420 attrsum += NFSX_UNSIGNED;
2421 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2422 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2423 fxdr_nfsv4time(tl, &nvap->na_mtime);
2424 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2425 attrsum += NFSX_V4TIME;
2427 NFSGETTIME(&curtime);
2428 nvap->na_mtime.tv_sec = curtime.tv_sec;
2429 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2431 nvap->na_vaflags |= VA_UTIMES_NULL;
2435 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2437 * set bitpos so we drop out of the loop.
2439 bitpos = NFSATTRBIT_MAX;
2445 * some clients pad the attrlist, so we need to skip over the
2448 if (attrsum > attrsize) {
2449 error = NFSERR_BADXDR;
2451 attrsize = NFSM_RNDUP(attrsize);
2452 if (attrsum < attrsize)
2453 error = nfsm_advance(nd, attrsize - attrsum, -1);
2460 * Check/setup export credentials.
2463 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2464 struct ucred *credanon)
2469 * Check/setup credentials.
2471 if (nd->nd_flag & ND_GSS)
2472 exp->nes_exflag &= ~MNT_EXPORTANON;
2475 * Check to see if the operation is allowed for this security flavor.
2476 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2477 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2478 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2480 if (nfsvno_testexp(nd, exp) &&
2481 nd->nd_procnum != NFSV4OP_SECINFO &&
2482 nd->nd_procnum != NFSPROC_FSINFO) {
2483 if (nd->nd_flag & ND_NFSV4)
2484 error = NFSERR_WRONGSEC;
2486 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2491 * Check to see if the file system is exported V4 only.
2493 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4))
2494 return (NFSERR_PROGNOTV4);
2497 * Now, map the user credentials.
2498 * (Note that ND_AUTHNONE will only be set for an NFSv3
2499 * Fsinfo RPC. If set for anything else, this code might need
2502 if (NFSVNO_EXPORTED(exp) &&
2503 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2504 NFSVNO_EXPORTANON(exp) ||
2505 (nd->nd_flag & ND_AUTHNONE))) {
2506 nd->nd_cred->cr_uid = credanon->cr_uid;
2507 nd->nd_cred->cr_gid = credanon->cr_gid;
2508 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2509 credanon->cr_groups);
2518 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2519 struct ucred **credp)
2521 int i, error, *secflavors;
2523 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2524 &exp->nes_numsecflavor, &secflavors);
2526 if (nfs_rootfhset) {
2527 exp->nes_exflag = 0;
2528 exp->nes_numsecflavor = 0;
2532 /* Copy the security flavors. */
2533 for (i = 0; i < exp->nes_numsecflavor; i++)
2534 exp->nes_secflavors[i] = secflavors[i];
2540 * Get a vnode for a file handle and export stuff.
2543 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2544 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2545 struct ucred **credp)
2547 int i, error, *secflavors;
2550 exp->nes_numsecflavor = 0;
2551 if (VFS_NEEDSGIANT(mp))
2554 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp);
2556 /* Make sure the server replies ESTALE to the client. */
2558 if (nam && !error) {
2559 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2560 &exp->nes_numsecflavor, &secflavors);
2562 if (nfs_rootfhset) {
2563 exp->nes_exflag = 0;
2564 exp->nes_numsecflavor = 0;
2570 /* Copy the security flavors. */
2571 for (i = 0; i < exp->nes_numsecflavor; i++)
2572 exp->nes_secflavors[i] = secflavors[i];
2575 if (error == 0 && lktype == LK_SHARED)
2577 * It would be much better to pass lktype to VFS_FHTOVP(),
2578 * but this will have to do until VFS_FHTOVP() has a lock
2579 * type argument like VFS_VGET().
2581 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
2586 * Do the pathconf vnode op.
2589 nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
2590 struct ucred *cred, struct thread *p)
2594 error = VOP_PATHCONF(vp, flag, retf);
2595 if (error == EOPNOTSUPP || error == EINVAL) {
2597 * Some file systems return EINVAL for name arguments not
2598 * supported and some return EOPNOTSUPP for this case.
2599 * So the NFSv3 Pathconf RPC doesn't fail for these cases,
2609 case _PC_CHOWN_RESTRICTED:
2617 * Only happens if a _PC_xxx is added to the server,
2618 * but this isn't updated.
2621 printf("nfsrvd pathconf flag=%d not supp\n", flag);
2629 * nfsd_fhtovp() - convert a fh to a vnode ptr
2630 * - look up fsid in mount list (if not found ret error)
2631 * - get vp and export rights by calling nfsvno_fhtovp()
2632 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2634 * - if mpp != NULL, return the mount point so that it can
2635 * be used for vn_finished_write() by the caller
2638 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2639 struct vnode **vpp, struct nfsexstuff *exp,
2640 struct mount **mpp, int startwrite, struct thread *p)
2643 struct ucred *credanon;
2646 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2648 * Check for the special case of the nfsv4root_fh.
2650 mp = vfs_busyfs(&fhp->fh_fsid);
2655 nd->nd_repstat = ESTALE;
2660 vn_start_write(NULL, mpp, V_WAIT);
2662 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2667 * For NFSv4 without a pseudo root fs, unexported file handles
2668 * can be returned, so that Lookup works everywhere.
2670 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2671 !(nd->nd_flag & ND_NFSV4)) {
2673 nd->nd_repstat = EACCES;
2677 * Personally, I've never seen any point in requiring a
2678 * reserved port#, since only in the rare case where the
2679 * clients are all boxes with secure system priviledges,
2680 * does it provide any enhanced security, but... some people
2681 * believe it to be useful and keep putting this code back in.
2682 * (There is also some "security checker" out there that
2683 * complains if the nfs server doesn't enforce this.)
2684 * However, note the following:
2685 * RFC3530 (NFSv4) specifies that a reserved port# not be
2687 * RFC2623 recommends that, if a reserved port# is checked for,
2688 * that there be a way to turn that off--> ifdef'd.
2690 #ifdef NFS_REQRSVPORT
2691 if (!nd->nd_repstat) {
2692 struct sockaddr_in *saddr;
2693 struct sockaddr_in6 *saddr6;
2695 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2696 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2697 if (!(nd->nd_flag & ND_NFSV4) &&
2698 ((saddr->sin_family == AF_INET &&
2699 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2700 (saddr6->sin6_family == AF_INET6 &&
2701 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2703 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2706 #endif /* NFS_REQRSVPORT */
2709 * Check/setup credentials.
2711 if (!nd->nd_repstat) {
2712 nd->nd_saveduid = nd->nd_cred->cr_uid;
2713 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2717 if (credanon != NULL)
2719 if (nd->nd_repstat) {
2721 vn_finished_write(mp);
2732 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2734 struct filedesc *fdp;
2737 fdp = p->td_proc->p_fd;
2738 if (fd >= fdp->fd_nfiles ||
2739 (fp = fdp->fd_ofiles[fd]) == NULL)
2746 * Called from nfssvc() to update the exports list. Just call
2747 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2748 * in the mount list.
2751 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2753 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2755 struct nameidata nd;
2758 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2759 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2761 else if (error == 0) {
2762 if (nfsexargp->fspec == NULL)
2765 * If fspec != NULL, this is the v4root path.
2767 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2768 nfsexargp->fspec, p);
2769 if ((error = namei(&nd)) != 0)
2771 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2774 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2775 NFSBCOPY((caddr_t)&fh,
2776 nfs_rootfh.nfsrvfh_data,
2777 sizeof (fhandle_t));
2785 * Get the tcp socket sequence numbers we need.
2786 * (Maybe this should be moved to the tcp sources?)
2789 nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2794 inp = sotoinpcb(so);
2795 KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2797 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2801 tp = intotcpcb(inp);
2802 if (tp->t_state != TCPS_ESTABLISHED) {
2806 *maxp = tp->snd_max;
2807 *unap = tp->snd_una;
2813 * This function needs to test to see if the system is near its limit
2814 * for memory allocation via malloc() or mget() and return True iff
2815 * either of these resources are near their limit.
2816 * XXX (For now, this is just a stub.)
2818 int nfsrv_testmalloclimit = 0;
2820 nfsrv_mallocmget_limit(void)
2822 static int printmesg = 0;
2823 static int testval = 1;
2825 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2826 if ((printmesg++ % 100) == 0)
2827 printf("nfsd: malloc/mget near limit\n");
2834 * BSD specific initialization of a mount point.
2839 static int inited = 0;
2844 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2845 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2846 nfsv4root_mnt.mnt_export = NULL;
2847 TAILQ_INIT(&nfsv4root_opt);
2848 TAILQ_INIT(&nfsv4root_newopt);
2849 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2850 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2851 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2855 * Get a vnode for a file handle, without checking exports, etc.
2858 nfsvno_getvp(fhandle_t *fhp)
2864 mp = vfs_busyfs(&fhp->fh_fsid);
2867 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2875 * Do a local VOP_ADVLOCK().
2878 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2879 u_int64_t end, struct thread *td)
2885 if (nfsrv_dolocallocks == 0)
2888 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2889 if ((vp->v_iflag & VI_DOOMED) != 0)
2892 fl.l_whence = SEEK_SET;
2894 fl.l_start = (off_t)first;
2895 if (end == NFS64BITSSET) {
2899 fl.l_len = (off_t)tlen;
2902 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2903 * values for all calls, so that all locks will be held by the
2904 * nfsd server. (The nfsd server handles conflicts between the
2906 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2907 * bytes, so it can't be put in l_sysid.
2909 if (nfsv4_sysid == 0)
2910 nfsv4_sysid = nlm_acquire_next_sysid();
2911 fl.l_pid = (pid_t)0;
2912 fl.l_sysid = (int)nfsv4_sysid;
2914 NFSVOPUNLOCK(vp, 0, td);
2915 if (ftype == F_UNLCK)
2916 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2917 (F_POSIX | F_REMOTE));
2919 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2920 (F_POSIX | F_REMOTE));
2921 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2926 * Check the nfsv4 root exports.
2929 nfsvno_v4rootexport(struct nfsrv_descript *nd)
2931 struct ucred *credanon;
2932 int exflags, error, numsecflavor, *secflavors, i;
2934 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
2935 &credanon, &numsecflavor, &secflavors);
2937 return (NFSERR_PROGUNAVAIL);
2938 if (credanon != NULL)
2940 for (i = 0; i < numsecflavor; i++) {
2941 if (secflavors[i] == AUTH_SYS)
2942 nd->nd_flag |= ND_EXAUTHSYS;
2943 else if (secflavors[i] == RPCSEC_GSS_KRB5)
2944 nd->nd_flag |= ND_EXGSS;
2945 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
2946 nd->nd_flag |= ND_EXGSSINTEGRITY;
2947 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
2948 nd->nd_flag |= ND_EXGSSPRIVACY;
2954 * Nfs server psuedo system call for the nfsd's
2960 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
2963 struct nfsd_addsock_args sockarg;
2964 struct nfsd_nfsd_args nfsdarg;
2967 if (uap->flag & NFSSVC_NFSDADDSOCK) {
2968 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
2971 if ((error = fget(td, sockarg.sock, &fp)) != 0) {
2974 if (fp->f_type != DTYPE_SOCKET) {
2978 error = nfsrvd_addsock(fp);
2980 } else if (uap->flag & NFSSVC_NFSDNFSD) {
2981 if (uap->argp == NULL)
2983 error = copyin(uap->argp, (caddr_t)&nfsdarg,
2987 error = nfsrvd_nfsd(td, &nfsdarg);
2989 error = nfssvc_srvcall(td, uap, td->td_ucred);
2995 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
2997 struct nfsex_args export;
2998 struct file *fp = NULL;
3000 struct nfsd_clid adminrevoke;
3001 struct nfsd_dumplist dumplist;
3002 struct nfsd_dumpclients *dumpclients;
3003 struct nfsd_dumplocklist dumplocklist;
3004 struct nfsd_dumplocks *dumplocks;
3005 struct nameidata nd;
3010 if (uap->flag & NFSSVC_PUBLICFH) {
3011 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3012 sizeof (fhandle_t));
3013 error = copyin(uap->argp,
3014 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3017 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3018 error = copyin(uap->argp,(caddr_t)&export,
3019 sizeof (struct nfsex_args));
3021 error = nfsrv_v4rootexport(&export, cred, p);
3022 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3025 } else if (uap->flag & NFSSVC_STABLERESTART) {
3026 error = copyin(uap->argp, (caddr_t)&stablefd,
3029 error = fp_getfvp(p, stablefd, &fp, &vp);
3030 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3032 if (!error && newnfs_numnfsd != 0)
3035 nfsrv_stablefirst.nsf_fp = fp;
3036 nfsrv_setupstable(p);
3038 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3039 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3040 sizeof (struct nfsd_clid));
3042 error = nfsrv_adminrevoke(&adminrevoke, p);
3043 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3044 error = copyin(uap->argp, (caddr_t)&dumplist,
3045 sizeof (struct nfsd_dumplist));
3046 if (!error && (dumplist.ndl_size < 1 ||
3047 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3050 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3051 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3053 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3054 error = copyout(dumpclients,
3055 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3056 free((caddr_t)dumpclients, M_TEMP);
3058 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3059 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3060 sizeof (struct nfsd_dumplocklist));
3061 if (!error && (dumplocklist.ndllck_size < 1 ||
3062 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3065 error = nfsrv_lookupfilename(&nd,
3066 dumplocklist.ndllck_fname, p);
3068 len = sizeof (struct nfsd_dumplocks) *
3069 dumplocklist.ndllck_size;
3070 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3072 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3073 dumplocklist.ndllck_size, p);
3075 error = copyout(dumplocks,
3076 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3077 free((caddr_t)dumplocks, M_TEMP);
3079 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3082 nfsd_master_pid = procp->p_pid;
3083 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3084 nfsd_master_start = procp->p_stats->p_start;
3085 nfsd_master_proc = procp;
3093 * Returns 0 if ok, 1 otherwise.
3096 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3101 * This seems odd, but allow the case where the security flavor
3102 * list is empty. This happens when NFSv4 is traversing non-exported
3103 * file systems. Exported file systems should always have a non-empty
3104 * security flavor list.
3106 if (exp->nes_numsecflavor == 0)
3109 for (i = 0; i < exp->nes_numsecflavor; i++) {
3111 * The tests for privacy and integrity must be first,
3112 * since ND_GSS is set for everything but AUTH_SYS.
3114 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3115 (nd->nd_flag & ND_GSSPRIVACY))
3117 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3118 (nd->nd_flag & ND_GSSINTEGRITY))
3120 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3121 (nd->nd_flag & ND_GSS))
3123 if (exp->nes_secflavors[i] == AUTH_SYS &&
3124 (nd->nd_flag & ND_GSS) == 0)
3131 * Calculate a hash value for the fid in a file handle.
3134 nfsrv_hashfh(fhandle_t *fhp)
3138 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3143 * Signal the userland master nfsd to backup the stable restart file.
3146 nfsrv_backupstable(void)
3150 if (nfsd_master_proc != NULL) {
3151 procp = pfind(nfsd_master_pid);
3152 /* Try to make sure it is the correct process. */
3153 if (procp == nfsd_master_proc &&
3154 procp->p_stats->p_start.tv_sec ==
3155 nfsd_master_start.tv_sec &&
3156 procp->p_stats->p_start.tv_usec ==
3157 nfsd_master_start.tv_usec &&
3158 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3159 psignal(procp, SIGUSR2);
3161 nfsd_master_proc = NULL;
3168 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3171 * Called once to initialize data structures...
3174 nfsd_modevent(module_t mod, int type, void *data)
3177 static int loaded = 0;
3184 mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF);
3185 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3186 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3188 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3195 #ifdef VV_DISABLEDELEG
3196 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3197 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3199 nfsd_call_servertimer = nfsrv_servertimer;
3200 nfsd_call_nfsd = nfssvc_nfsd;
3205 if (newnfs_numnfsd != 0) {
3210 #ifdef VV_DISABLEDELEG
3211 vn_deleg_ops.vndeleg_recall = NULL;
3212 vn_deleg_ops.vndeleg_disable = NULL;
3214 nfsd_call_servertimer = NULL;
3215 nfsd_call_nfsd = NULL;
3217 /* Clean out all NFSv4 state. */
3218 nfsrv_throwawayallstate(curthread);
3220 /* Clean the NFS server reply cache */
3221 nfsrvd_cleancache();
3223 /* Free up the krpc server pool. */
3224 if (nfsrvd_pool != NULL)
3225 svcpool_destroy(nfsrvd_pool);
3227 /* and get rid of the locks */
3228 mtx_destroy(&nfs_cache_mutex);
3229 mtx_destroy(&nfs_v4root_mutex);
3230 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3231 lockdestroy(&nfsv4root_mnt.mnt_explock);
3240 static moduledata_t nfsd_mod = {
3245 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3247 /* So that loader and kldload(2) can find us, wherever we are.. */
3248 MODULE_VERSION(nfsd, 1);
3249 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3250 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3251 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3252 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3253 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);