2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/capability.h>
40 * Functions that perform the vfs operations required by the routines in
41 * nfsd_serv.c. It is hoped that this change will make the server more
45 #include <fs/nfs/nfsport.h>
47 #include <sys/sysctl.h>
48 #include <nlm/nlm_prot.h>
51 FEATURE(nfsd, "NFSv4 server");
53 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
54 extern int nfsrv_useacl;
55 extern int newnfs_numnfsd;
56 extern struct mount nfsv4root_mnt;
57 extern struct nfsrv_stablefirst nfsrv_stablefirst;
58 extern void (*nfsd_call_servertimer)(void);
59 extern SVCPOOL *nfsrvd_pool;
60 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
62 struct mtx nfs_cache_mutex;
63 struct mtx nfs_v4root_mutex;
64 struct nfsrvfh nfs_rootfh, nfs_pubfh;
65 int nfs_pubfhset = 0, nfs_rootfhset = 0;
66 struct proc *nfsd_master_proc = NULL;
67 static pid_t nfsd_master_pid = (pid_t)-1;
68 static char nfsd_master_comm[MAXCOMLEN + 1];
69 static struct timeval nfsd_master_start;
70 static uint32_t nfsv4_sysid = 0;
72 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
75 int nfsrv_enable_crossmntpt = 1;
76 static int nfs_commit_blks;
77 static int nfs_commit_miss;
78 extern int nfsrv_issuedelegs;
79 extern int nfsrv_dolocallocks;
81 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
82 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
83 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
86 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
88 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
89 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
91 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
93 #define NUM_HEURISTIC 1017
96 #define NHUSE_MAX 2048
98 static struct nfsheur {
99 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
100 off_t nh_nextr; /* next offset for sequential detection */
101 int nh_use; /* use count for selection */
102 int nh_seqcount; /* heuristic */
103 } nfsheur[NUM_HEURISTIC];
107 * Get attributes into nfsvattr structure.
110 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
111 struct thread *p, int vpislocked)
113 int error, lockedit = 0;
115 if (vpislocked == 0) {
117 * When vpislocked == 0, the vnode is either exclusively
118 * locked by this thread or not locked by this thread.
119 * As such, shared lock it, if not exclusively locked.
121 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
123 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
126 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
135 * Get a file handle for a vnode.
138 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
142 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
143 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
144 error = VOP_VPTOFH(vp, &fhp->fh_fid);
151 * Perform access checking for vnodes obtained from file handles that would
152 * refer to files already opened by a Unix client. You cannot just use
153 * vn_writechk() and VOP_ACCESSX() for two reasons.
154 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
156 * 2 - The owner is to be given access irrespective of mode bits for some
157 * operations, so that processes that chmod after opening a file don't
161 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
162 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
163 u_int32_t *supportedtypep)
166 int error = 0, getret = 0;
168 if (vpislocked == 0) {
169 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
174 if (accmode & VWRITE) {
175 /* Just vn_writechk() changed to check rdonly */
177 * Disallow write attempts on read-only file systems;
178 * unless the file is a socket or a block or character
179 * device resident on the file system.
181 if (NFSVNO_EXRDONLY(exp) ||
182 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
183 switch (vp->v_type) {
193 * If there's shared text associated with
194 * the inode, try to free it up once. If
195 * we fail, we can't allow writing.
197 if ((vp->v_vflag & VV_TEXT) != 0 && error == 0)
207 * Should the override still be applied when ACLs are enabled?
209 error = VOP_ACCESSX(vp, accmode, cred, p);
210 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
212 * Try again with VEXPLICIT_DENY, to see if the test for
213 * deletion is supported.
215 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
217 if (vp->v_type == VDIR) {
218 accmode &= ~(VDELETE | VDELETE_CHILD);
220 error = VOP_ACCESSX(vp, accmode, cred, p);
221 } else if (supportedtypep != NULL) {
222 *supportedtypep &= ~NFSACCESS_DELETE;
228 * Allow certain operations for the owner (reads and writes
229 * on files that are already open).
231 if (override != NFSACCCHK_NOOVERRIDE &&
232 (error == EPERM || error == EACCES)) {
233 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
235 else if (override & NFSACCCHK_ALLOWOWNER) {
236 getret = VOP_GETATTR(vp, &vattr, cred);
237 if (getret == 0 && cred->cr_uid == vattr.va_uid)
250 * Set attribute(s) vnop.
253 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
254 struct thread *p, struct nfsexstuff *exp)
258 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
264 * Set up nameidata for a lookup() call and do it
265 * For the cases where we are crossing mount points
266 * (looking up the public fh path or the v4 root path when
267 * not using a pseudo-root fs), set/release the Giant lock,
271 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
272 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
273 struct vnode **retdirp)
275 struct componentname *cnp = &ndp->ni_cnd;
279 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
280 int error = 0, crossmnt;
284 cnp->cn_nameptr = cnp->cn_pnbuf;
285 ndp->ni_strictrelative = 0;
287 * Extract and set starting directory.
289 if (dp->v_type != VDIR) {
294 nfsvno_relpathbuf(ndp);
302 if (NFSVNO_EXRDONLY(exp))
303 cnp->cn_flags |= RDONLY;
304 ndp->ni_segflg = UIO_SYSSPACE;
307 if (nd->nd_flag & ND_PUBLOOKUP) {
309 if (cnp->cn_pnbuf[0] == '/') {
312 * Check for degenerate pathnames here, since lookup()
315 for (i = 1; i < ndp->ni_pathlen; i++)
316 if (cnp->cn_pnbuf[i] != '/')
318 if (i == ndp->ni_pathlen) {
319 error = NFSERR_ACCES;
325 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
326 (nd->nd_flag & ND_NFSV4) == 0) {
328 * Only cross mount points for NFSv4 when doing a
329 * mount while traversing the file system above
330 * the mount point, unless nfsrv_enable_crossmntpt is set.
332 cnp->cn_flags |= NOCROSSMOUNT;
337 * Initialize for scan, set ni_startdir and bump ref on dp again
338 * becuase lookup() will dereference ni_startdir.
342 ndp->ni_startdir = dp;
343 ndp->ni_rootdir = rootvnode;
346 cnp->cn_flags |= LOCKLEAF;
348 cnp->cn_nameptr = cnp->cn_pnbuf;
350 * Call lookup() to do the real work. If an error occurs,
351 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
352 * we do not have to dereference anything before returning.
353 * In either case ni_startdir will be dereferenced and NULLed
361 * Check for encountering a symbolic link. Trivial
362 * termination occurs if no symlink encountered.
364 if ((cnp->cn_flags & ISSYMLINK) == 0) {
365 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
366 nfsvno_relpathbuf(ndp);
367 if (ndp->ni_vp && !lockleaf)
368 NFSVOPUNLOCK(ndp->ni_vp, 0);
375 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
376 NFSVOPUNLOCK(ndp->ni_dvp, 0);
377 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
382 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
386 if (ndp->ni_pathlen > 1)
387 cp = uma_zalloc(namei_zone, M_WAITOK);
391 aiov.iov_len = MAXPATHLEN;
392 auio.uio_iov = &aiov;
395 auio.uio_rw = UIO_READ;
396 auio.uio_segflg = UIO_SYSSPACE;
398 auio.uio_resid = MAXPATHLEN;
399 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
402 if (ndp->ni_pathlen > 1)
403 uma_zfree(namei_zone, cp);
409 linklen = MAXPATHLEN - auio.uio_resid;
414 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
415 error = ENAMETOOLONG;
420 * Adjust or replace path
422 if (ndp->ni_pathlen > 1) {
423 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
424 uma_zfree(namei_zone, cnp->cn_pnbuf);
427 cnp->cn_pnbuf[linklen] = '\0';
428 ndp->ni_pathlen += linklen;
431 * Cleanup refs for next loop and check if root directory
432 * should replace current directory. Normally ni_dvp
433 * becomes the new base directory and is cleaned up when
434 * we loop. Explicitly null pointers after invalidation
435 * to clarify operation.
440 if (cnp->cn_pnbuf[0] == '/') {
442 ndp->ni_dvp = ndp->ni_rootdir;
445 ndp->ni_startdir = ndp->ni_dvp;
449 cnp->cn_flags &= ~LOCKLEAF;
453 uma_zfree(namei_zone, cnp->cn_pnbuf);
456 ndp->ni_startdir = NULL;
457 cnp->cn_flags &= ~HASBUF;
458 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
463 NFSEXITCODE2(error, nd);
468 * Set up a pathname buffer and return a pointer to it and, optionally
469 * set a hash pointer.
472 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
474 struct componentname *cnp = &ndp->ni_cnd;
476 cnp->cn_flags |= (NOMACCHECK | HASBUF);
477 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
480 *bufpp = cnp->cn_pnbuf;
484 * Release the above path buffer, if not released by nfsvno_namei().
487 nfsvno_relpathbuf(struct nameidata *ndp)
490 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
492 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
493 ndp->ni_cnd.cn_flags &= ~HASBUF;
497 * Readlink vnode op into an mbuf list.
500 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
501 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
503 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
504 struct iovec *ivp = iv;
505 struct uio io, *uiop = &io;
506 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
507 int i, len, tlen, error = 0;
511 while (len < NFS_MAXPATHLEN) {
514 mp->m_len = NFSMSIZ(mp);
521 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
522 mp->m_len = NFS_MAXPATHLEN - len;
523 len = NFS_MAXPATHLEN;
527 ivp->iov_base = mtod(mp, caddr_t);
528 ivp->iov_len = mp->m_len;
533 uiop->uio_iovcnt = i;
534 uiop->uio_offset = 0;
535 uiop->uio_resid = len;
536 uiop->uio_rw = UIO_READ;
537 uiop->uio_segflg = UIO_SYSSPACE;
539 error = VOP_READLINK(vp, uiop, cred);
545 if (uiop->uio_resid > 0) {
546 len -= uiop->uio_resid;
547 tlen = NFSM_RNDUP(len);
548 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
560 * Read vnode op call into mbuf list.
563 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
564 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
570 int error = 0, len, left, siz, tlen, ioflag = 0, hi, try = 32;
571 struct mbuf *m2 = NULL, *m3;
572 struct uio io, *uiop = &io;
576 * Calculate seqcount for heuristic
579 * Locate best candidate
582 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
586 if (nfsheur[hi].nh_vp == vp) {
590 if (nfsheur[hi].nh_use > 0)
591 --nfsheur[hi].nh_use;
592 hi = (hi + 1) % NUM_HEURISTIC;
593 if (nfsheur[hi].nh_use < nh->nh_use)
597 if (nh->nh_vp != vp) {
600 nh->nh_use = NHUSE_INIT;
608 * Calculate heuristic
611 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
612 if (++nh->nh_seqcount > IO_SEQMAX)
613 nh->nh_seqcount = IO_SEQMAX;
614 } else if (nh->nh_seqcount > 1) {
619 nh->nh_use += NHUSE_INC;
620 if (nh->nh_use > NHUSE_MAX)
621 nh->nh_use = NHUSE_MAX;
622 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
624 len = left = NFSM_RNDUP(cnt);
627 * Generate the mbuf list with the uio_iov ref. to it.
634 siz = min(M_TRAILINGSPACE(m), left);
643 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
645 uiop->uio_iov = iv2 = iv;
651 panic("nfsvno_read iov");
652 siz = min(M_TRAILINGSPACE(m), left);
654 iv->iov_base = mtod(m, caddr_t) + m->m_len;
663 uiop->uio_iovcnt = i;
664 uiop->uio_offset = off;
665 uiop->uio_resid = len;
666 uiop->uio_rw = UIO_READ;
667 uiop->uio_segflg = UIO_SYSSPACE;
668 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
669 FREE((caddr_t)iv2, M_TEMP);
675 tlen = len - uiop->uio_resid;
676 cnt = cnt < tlen ? cnt : tlen;
677 tlen = NFSM_RNDUP(cnt);
681 } else if (len != tlen || tlen != cnt)
682 nfsrv_adj(m3, len - tlen, tlen - cnt);
692 * Write vnode op from an mbuf list.
695 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
696 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
702 struct uio io, *uiop = &io;
704 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
706 uiop->uio_iov = iv = ivp;
707 uiop->uio_iovcnt = cnt;
708 i = mtod(mp, caddr_t) + mp->m_len - cp;
712 panic("nfsvno_write");
723 cp = mtod(mp, caddr_t);
727 if (stable == NFSWRITE_UNSTABLE)
728 ioflags = IO_NODELOCKED;
730 ioflags = (IO_SYNC | IO_NODELOCKED);
731 uiop->uio_resid = retlen;
732 uiop->uio_rw = UIO_WRITE;
733 uiop->uio_segflg = UIO_SYSSPACE;
735 uiop->uio_offset = off;
736 error = VOP_WRITE(vp, uiop, ioflags, cred);
737 FREE((caddr_t)iv, M_TEMP);
744 * Common code for creating a regular file (plus special files for V2).
747 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
748 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
749 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
754 error = nd->nd_repstat;
755 if (!error && ndp->ni_vp == NULL) {
756 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
757 vrele(ndp->ni_startdir);
758 error = VOP_CREATE(ndp->ni_dvp,
759 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
761 nfsvno_relpathbuf(ndp);
763 if (*exclusive_flagp) {
764 *exclusive_flagp = 0;
765 NFSVNO_ATTRINIT(nvap);
766 nvap->na_atime.tv_sec = cverf[0];
767 nvap->na_atime.tv_nsec = cverf[1];
768 error = VOP_SETATTR(ndp->ni_vp,
769 &nvap->na_vattr, nd->nd_cred);
773 * NFS V2 Only. nfsrvd_mknod() does this for V3.
774 * (This implies, just get out on an error.)
776 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
777 nvap->na_type == VFIFO) {
778 if (nvap->na_type == VCHR && rdev == 0xffffffff)
779 nvap->na_type = VFIFO;
780 if (nvap->na_type != VFIFO &&
781 (error = priv_check_cred(nd->nd_cred,
782 PRIV_VFS_MKNOD_DEV, 0))) {
783 vrele(ndp->ni_startdir);
784 nfsvno_relpathbuf(ndp);
788 nvap->na_rdev = rdev;
789 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
790 &ndp->ni_cnd, &nvap->na_vattr);
792 nfsvno_relpathbuf(ndp);
793 vrele(ndp->ni_startdir);
797 vrele(ndp->ni_startdir);
798 nfsvno_relpathbuf(ndp);
806 * Handle cases where error is already set and/or
808 * 1 - clean up the lookup
809 * 2 - iff !error and na_size set, truncate it
811 vrele(ndp->ni_startdir);
812 nfsvno_relpathbuf(ndp);
814 if (ndp->ni_dvp == *vpp)
818 if (!error && nvap->na_size != VNOVAL) {
819 error = nfsvno_accchk(*vpp, VWRITE,
820 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
821 NFSACCCHK_VPISLOCKED, NULL);
823 tempsize = nvap->na_size;
824 NFSVNO_ATTRINIT(nvap);
825 nvap->na_size = tempsize;
826 error = VOP_SETATTR(*vpp,
827 &nvap->na_vattr, nd->nd_cred);
840 * Do a mknod vnode op.
843 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
849 vtyp = nvap->na_type;
851 * Iff doesn't exist, create it.
854 vrele(ndp->ni_startdir);
855 nfsvno_relpathbuf(ndp);
861 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
862 vrele(ndp->ni_startdir);
863 nfsvno_relpathbuf(ndp);
865 error = NFSERR_BADTYPE;
869 vrele(ndp->ni_startdir);
870 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
871 &ndp->ni_cnd, &nvap->na_vattr);
873 nfsvno_relpathbuf(ndp);
875 if (nvap->na_type != VFIFO &&
876 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
877 vrele(ndp->ni_startdir);
878 nfsvno_relpathbuf(ndp);
882 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
883 &ndp->ni_cnd, &nvap->na_vattr);
885 nfsvno_relpathbuf(ndp);
886 vrele(ndp->ni_startdir);
888 * Since VOP_MKNOD returns the ni_vp, I can't
889 * see any reason to do the lookup.
902 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
903 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
907 if (ndp->ni_vp != NULL) {
908 if (ndp->ni_dvp == ndp->ni_vp)
913 nfsvno_relpathbuf(ndp);
917 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
920 nfsvno_relpathbuf(ndp);
931 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
932 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
933 struct nfsexstuff *exp)
938 vrele(ndp->ni_startdir);
939 nfsvno_relpathbuf(ndp);
940 if (ndp->ni_dvp == ndp->ni_vp)
949 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
950 &nvap->na_vattr, pathcp);
952 vrele(ndp->ni_startdir);
953 nfsvno_relpathbuf(ndp);
955 * Although FreeBSD still had the lookup code in
956 * it for 7/current, there doesn't seem to be any
957 * point, since VOP_SYMLINK() returns the ni_vp.
958 * Just vput it for v2.
960 if (!not_v2 && !error)
969 * Parse symbolic link arguments.
970 * This function has an ugly side effect. It will MALLOC() an area for
971 * the symlink and set iov_base to point to it, only if it succeeds.
972 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
976 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
977 struct thread *p, char **pathcpp, int *lenp)
982 struct nfsv2_sattr *sp;
986 if ((nd->nd_flag & ND_NFSV3) &&
987 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
989 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
990 len = fxdr_unsigned(int, *tl);
991 if (len > NFS_MAXPATHLEN || len <= 0) {
995 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
996 error = nfsrv_mtostr(nd, pathcp, len);
999 if (nd->nd_flag & ND_NFSV2) {
1000 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1001 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1005 NFSEXITCODE2(0, nd);
1009 free(pathcp, M_TEMP);
1010 NFSEXITCODE2(error, nd);
1015 * Remove a non-directory object.
1018 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1019 struct thread *p, struct nfsexstuff *exp)
1025 if (vp->v_type == VDIR)
1026 error = NFSERR_ISDIR;
1028 error = nfsrv_checkremove(vp, 1, p);
1030 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1031 if (ndp->ni_dvp == vp)
1041 * Remove a directory.
1044 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1045 struct thread *p, struct nfsexstuff *exp)
1051 if (vp->v_type != VDIR) {
1056 * No rmdir "." please.
1058 if (ndp->ni_dvp == vp) {
1063 * The root of a mounted filesystem cannot be deleted.
1065 if (vp->v_vflag & VV_ROOT)
1069 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1070 if (ndp->ni_dvp == vp)
1083 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1084 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1086 struct vnode *fvp, *tvp, *tdvp;
1089 fvp = fromndp->ni_vp;
1091 vrele(fromndp->ni_dvp);
1096 tdvp = tondp->ni_dvp;
1099 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1100 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1102 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1103 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1106 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1107 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1112 * A rename to '.' or '..' results in a prematurely
1113 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1116 if ((tondp->ni_cnd.cn_namelen == 1 &&
1117 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1118 (tondp->ni_cnd.cn_namelen == 2 &&
1119 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1120 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1125 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1126 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1129 if (fvp->v_mount != tdvp->v_mount) {
1130 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1134 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1139 * If source and destination are the same, there is nothing to
1140 * do. Set error to -1 to indicate this.
1145 if (ndflag & ND_NFSV4) {
1146 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1147 error = nfsrv_checkremove(fvp, 0, p);
1148 NFSVOPUNLOCK(fvp, 0);
1152 error = nfsrv_checkremove(tvp, 1, p);
1155 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1156 * that the NFSv4 client won't be confused by the rename.
1157 * Since nfsd_recalldelegation() can only be called on an
1158 * unlocked vnode at this point and fvp is the file that will
1159 * still exist after the rename, just do fvp.
1161 nfsd_recalldelegation(fvp, p);
1165 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1166 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1175 vrele(fromndp->ni_dvp);
1180 vrele(tondp->ni_startdir);
1181 nfsvno_relpathbuf(tondp);
1183 vrele(fromndp->ni_startdir);
1184 nfsvno_relpathbuf(fromndp);
1193 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1194 struct thread *p, struct nfsexstuff *exp)
1204 if (vp->v_mount != xp->v_mount)
1208 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1209 if ((vp->v_iflag & VI_DOOMED) == 0)
1210 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1213 if (ndp->ni_dvp == vp)
1217 NFSVOPUNLOCK(vp, 0);
1219 if (ndp->ni_dvp == ndp->ni_vp)
1226 nfsvno_relpathbuf(ndp);
1232 * Do the fsync() appropriate for the commit.
1235 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1240 if (cnt > MAX_COMMIT_COUNT) {
1242 * Give up and do the whole thing
1245 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1246 VM_OBJECT_LOCK(vp->v_object);
1247 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1248 VM_OBJECT_UNLOCK(vp->v_object);
1250 error = VOP_FSYNC(vp, MNT_WAIT, td);
1253 * Locate and synchronously write any buffers that fall
1254 * into the requested range. Note: we are assuming that
1255 * f_iosize is a power of 2.
1257 int iosize = vp->v_mount->mnt_stat.f_iosize;
1258 int iomask = iosize - 1;
1263 * Align to iosize boundry, super-align to page boundry.
1266 cnt += off & iomask;
1267 off &= ~(u_quad_t)iomask;
1269 if (off & PAGE_MASK) {
1270 cnt += off & PAGE_MASK;
1271 off &= ~(u_quad_t)PAGE_MASK;
1273 lblkno = off / iosize;
1276 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1277 VM_OBJECT_LOCK(vp->v_object);
1278 vm_object_page_clean(vp->v_object, off, off + cnt,
1280 VM_OBJECT_UNLOCK(vp->v_object);
1289 * If we have a buffer and it is marked B_DELWRI we
1290 * have to lock and write it. Otherwise the prior
1291 * write is assumed to have already been committed.
1293 * gbincore() can return invalid buffers now so we
1294 * have to check that bit as well (though B_DELWRI
1295 * should not be set if B_INVAL is set there could be
1296 * a race here since we haven't locked the buffer).
1298 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1299 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1300 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1302 continue; /* retry */
1304 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1307 bp->b_flags &= ~B_ASYNC;
1330 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1334 error = VFS_STATFS(vp->v_mount, sf);
1337 * Since NFS handles these values as unsigned on the
1338 * wire, there is no way to represent negative values,
1339 * so set them to 0. Without this, they will appear
1340 * to be very large positive values for clients like
1343 if (sf->f_bavail < 0)
1345 if (sf->f_ffree < 0)
1353 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1354 * must handle nfsrv_opencheck() calls after any other access checks.
1357 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1358 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1359 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1360 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1361 struct nfsexstuff *exp, struct vnode **vpp)
1363 struct vnode *vp = NULL;
1365 struct nfsexstuff nes;
1367 if (ndp->ni_vp == NULL)
1368 nd->nd_repstat = nfsrv_opencheck(clientid,
1369 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1370 if (!nd->nd_repstat) {
1371 if (ndp->ni_vp == NULL) {
1372 vrele(ndp->ni_startdir);
1373 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1374 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1376 nfsvno_relpathbuf(ndp);
1377 if (!nd->nd_repstat) {
1378 if (*exclusive_flagp) {
1379 *exclusive_flagp = 0;
1380 NFSVNO_ATTRINIT(nvap);
1381 nvap->na_atime.tv_sec = cverf[0];
1382 nvap->na_atime.tv_nsec = cverf[1];
1383 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1384 &nvap->na_vattr, cred);
1386 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1387 aclp, p, attrbitp, exp);
1392 if (ndp->ni_startdir)
1393 vrele(ndp->ni_startdir);
1394 nfsvno_relpathbuf(ndp);
1396 if (create == NFSV4OPEN_CREATE) {
1397 if (ndp->ni_dvp == vp)
1402 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1403 if (ndp->ni_cnd.cn_flags & RDONLY)
1404 NFSVNO_SETEXRDONLY(&nes);
1406 NFSVNO_EXINIT(&nes);
1407 nd->nd_repstat = nfsvno_accchk(vp,
1408 VWRITE, cred, &nes, p,
1409 NFSACCCHK_NOOVERRIDE,
1410 NFSACCCHK_VPISLOCKED, NULL);
1411 nd->nd_repstat = nfsrv_opencheck(clientid,
1412 stateidp, stp, vp, nd, p, nd->nd_repstat);
1413 if (!nd->nd_repstat) {
1414 tempsize = nvap->na_size;
1415 NFSVNO_ATTRINIT(nvap);
1416 nvap->na_size = tempsize;
1417 nd->nd_repstat = VOP_SETATTR(vp,
1418 &nvap->na_vattr, cred);
1420 } else if (vp->v_type == VREG) {
1421 nd->nd_repstat = nfsrv_opencheck(clientid,
1422 stateidp, stp, vp, nd, p, nd->nd_repstat);
1426 if (ndp->ni_cnd.cn_flags & HASBUF)
1427 nfsvno_relpathbuf(ndp);
1428 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1429 vrele(ndp->ni_startdir);
1430 if (ndp->ni_dvp == ndp->ni_vp)
1440 NFSEXITCODE2(0, nd);
1444 * Updates the file rev and sets the mtime and ctime
1445 * to the current clock time, returning the va_filerev and va_Xtime
1449 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1450 struct ucred *cred, struct thread *p)
1455 getnanotime(&va.va_mtime);
1456 (void) VOP_SETATTR(vp, &va, cred);
1457 (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1461 * Glue routine to nfsv4_fillattr().
1464 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1465 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1466 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1467 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1471 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1472 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1474 NFSEXITCODE2(0, nd);
1478 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1480 * nfs readdir service
1481 * - mallocs what it thinks is enough to read
1482 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1483 * - calls VOP_READDIR()
1484 * - loops around building the reply
1485 * if the output generated exceeds count break out of loop
1486 * The NFSM_CLGET macro is used here so that the reply will be packed
1487 * tightly in mbuf clusters.
1488 * - it trims out records with d_fileno == 0
1489 * this doesn't matter for Unix clients, but they might confuse clients
1491 * - it trims out records with d_type == DT_WHT
1492 * these cannot be seen through NFS (unless we extend the protocol)
1493 * The alternate call nfsrvd_readdirplus() does lookups as well.
1494 * PS: The NFS protocol spec. does not clarify what the "count" byte
1495 * argument is a count of.. just name strings and file id's or the
1496 * entire reply rpc or ...
1497 * I tried just file name and id sizes and it confused the Sun client,
1498 * so I am using the full rpc size now. The "paranoia.." comment refers
1499 * to including the status longwords that are not a part of the dir.
1500 * "entry" structures, but are in the rpc.
1503 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1504 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1509 char *cpos, *cend, *rbuf;
1511 int nlen, error = 0, getret = 1;
1512 int siz, cnt, fullsiz, eofflag, ncookies;
1513 u_int64_t off, toff, verf;
1514 u_long *cookies = NULL, *cookiep;
1519 if (nd->nd_repstat) {
1520 nfsrv_postopattr(nd, getret, &at);
1523 if (nd->nd_flag & ND_NFSV2) {
1524 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1525 off = fxdr_unsigned(u_quad_t, *tl++);
1527 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1528 off = fxdr_hyper(tl);
1530 verf = fxdr_hyper(tl);
1534 cnt = fxdr_unsigned(int, *tl);
1535 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1536 cnt = NFS_SRVMAXDATA(nd);
1537 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1539 if (nd->nd_flag & ND_NFSV3) {
1540 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1544 * va_filerev is not sufficient as a cookie verifier,
1545 * since it is not supposed to change when entries are
1546 * removed/added unless that offset cookies returned to
1547 * the client are no longer valid.
1549 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1550 nd->nd_repstat = NFSERR_BAD_COOKIE;
1553 if (nd->nd_repstat == 0 && cnt == 0) {
1554 if (nd->nd_flag & ND_NFSV2)
1555 /* NFSv2 does not have NFSERR_TOOSMALL */
1556 nd->nd_repstat = EPERM;
1558 nd->nd_repstat = NFSERR_TOOSMALL;
1560 if (!nd->nd_repstat)
1561 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1562 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1563 NFSACCCHK_VPISLOCKED, NULL);
1564 if (nd->nd_repstat) {
1566 if (nd->nd_flag & ND_NFSV3)
1567 nfsrv_postopattr(nd, getret, &at);
1570 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1571 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1575 free((caddr_t)cookies, M_TEMP);
1583 io.uio_offset = (off_t)off;
1585 io.uio_segflg = UIO_SYSSPACE;
1586 io.uio_rw = UIO_READ;
1588 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1590 off = (u_int64_t)io.uio_offset;
1592 siz -= io.uio_resid;
1594 if (!cookies && !nd->nd_repstat)
1595 nd->nd_repstat = NFSERR_PERM;
1596 if (nd->nd_flag & ND_NFSV3) {
1597 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1598 if (!nd->nd_repstat)
1599 nd->nd_repstat = getret;
1603 * Handles the failed cases. nd->nd_repstat == 0 past here.
1605 if (nd->nd_repstat) {
1607 free((caddr_t)rbuf, M_TEMP);
1609 free((caddr_t)cookies, M_TEMP);
1610 if (nd->nd_flag & ND_NFSV3)
1611 nfsrv_postopattr(nd, getret, &at);
1615 * If nothing read, return eof
1620 if (nd->nd_flag & ND_NFSV2) {
1621 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1623 nfsrv_postopattr(nd, getret, &at);
1624 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1625 txdr_hyper(at.na_filerev, tl);
1628 *tl++ = newnfs_false;
1630 FREE((caddr_t)rbuf, M_TEMP);
1631 FREE((caddr_t)cookies, M_TEMP);
1636 * Check for degenerate cases of nothing useful read.
1637 * If so go try again
1641 dp = (struct dirent *)cpos;
1645 * For some reason FreeBSD's ufs_readdir() chooses to back the
1646 * directory offset up to a block boundary, so it is necessary to
1647 * skip over the records that precede the requested offset. This
1648 * requires the assumption that file offset cookies monotonically
1650 * Since the offset cookies don't monotonically increase for ZFS,
1651 * this is not done when ZFS is the file system.
1653 while (cpos < cend && ncookies > 0 &&
1654 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1655 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1656 cpos += dp->d_reclen;
1657 dp = (struct dirent *)cpos;
1661 if (cpos >= cend || ncookies == 0) {
1669 * dirlen is the size of the reply, including all XDR and must
1670 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1671 * if the XDR should be included in "count", but to be safe, we do.
1672 * (Include the two booleans at the end of the reply in dirlen now.)
1674 if (nd->nd_flag & ND_NFSV3) {
1675 nfsrv_postopattr(nd, getret, &at);
1676 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1677 txdr_hyper(at.na_filerev, tl);
1678 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1680 dirlen = 2 * NFSX_UNSIGNED;
1683 /* Loop through the records and build reply */
1684 while (cpos < cend && ncookies > 0) {
1685 nlen = dp->d_namlen;
1686 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1687 nlen <= NFS_MAXNAMLEN) {
1688 if (nd->nd_flag & ND_NFSV3)
1689 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1691 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1698 * Build the directory record xdr from
1701 if (nd->nd_flag & ND_NFSV3) {
1702 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1703 *tl++ = newnfs_true;
1706 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1707 *tl++ = newnfs_true;
1709 *tl = txdr_unsigned(dp->d_fileno);
1710 (void) nfsm_strtom(nd, dp->d_name, nlen);
1711 if (nd->nd_flag & ND_NFSV3) {
1712 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1715 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1716 *tl = txdr_unsigned(*cookiep);
1718 cpos += dp->d_reclen;
1719 dp = (struct dirent *)cpos;
1725 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1726 *tl++ = newnfs_false;
1731 FREE((caddr_t)rbuf, M_TEMP);
1732 FREE((caddr_t)cookies, M_TEMP);
1735 NFSEXITCODE2(0, nd);
1739 NFSEXITCODE2(error, nd);
1744 * Readdirplus for V3 and Readdir for V4.
1747 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1748 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1753 char *cpos, *cend, *rbuf;
1756 struct nfsvattr nva, at, *nvap = &nva;
1757 struct mbuf *mb0, *mb1;
1758 struct nfsreferral *refp;
1759 int nlen, r, error = 0, getret = 1, usevget = 1;
1760 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1761 caddr_t bpos0, bpos1;
1762 u_int64_t off, toff, verf;
1763 u_long *cookies = NULL, *cookiep;
1764 nfsattrbit_t attrbits, rderrbits, savbits;
1767 struct componentname cn;
1768 int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1769 struct mount *mp, *new_mp;
1770 uint64_t mounted_on_fileno;
1772 if (nd->nd_repstat) {
1773 nfsrv_postopattr(nd, getret, &at);
1776 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1777 off = fxdr_hyper(tl);
1780 verf = fxdr_hyper(tl);
1782 siz = fxdr_unsigned(int, *tl++);
1783 cnt = fxdr_unsigned(int, *tl);
1786 * Use the server's maximum data transfer size as the upper bound
1789 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1790 cnt = NFS_SRVMAXDATA(nd);
1793 * siz is a "hint" of how much directory information (name, fileid,
1794 * cookie) should be in the reply. At least one client "hints" 0,
1795 * so I set it to cnt for that case. I also round it up to the
1796 * next multiple of DIRBLKSIZ.
1800 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1802 if (nd->nd_flag & ND_NFSV4) {
1803 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1806 NFSSET_ATTRBIT(&savbits, &attrbits);
1807 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1808 NFSZERO_ATTRBIT(&rderrbits);
1809 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1811 NFSZERO_ATTRBIT(&attrbits);
1814 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1815 if (!nd->nd_repstat) {
1816 if (off && verf != at.na_filerev) {
1818 * va_filerev is not sufficient as a cookie verifier,
1819 * since it is not supposed to change when entries are
1820 * removed/added unless that offset cookies returned to
1821 * the client are no longer valid.
1824 if (nd->nd_flag & ND_NFSV4) {
1825 nd->nd_repstat = NFSERR_NOTSAME;
1827 nd->nd_repstat = NFSERR_BAD_COOKIE;
1830 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1831 nd->nd_repstat = NFSERR_BAD_COOKIE;
1834 if (!nd->nd_repstat && vp->v_type != VDIR)
1835 nd->nd_repstat = NFSERR_NOTDIR;
1836 if (!nd->nd_repstat && cnt == 0)
1837 nd->nd_repstat = NFSERR_TOOSMALL;
1838 if (!nd->nd_repstat)
1839 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1840 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1841 NFSACCCHK_VPISLOCKED, NULL);
1842 if (nd->nd_repstat) {
1844 if (nd->nd_flag & ND_NFSV3)
1845 nfsrv_postopattr(nd, getret, &at);
1848 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1850 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1854 free((caddr_t)cookies, M_TEMP);
1862 io.uio_offset = (off_t)off;
1864 io.uio_segflg = UIO_SYSSPACE;
1865 io.uio_rw = UIO_READ;
1867 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1869 off = (u_int64_t)io.uio_offset;
1871 siz -= io.uio_resid;
1873 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1875 if (!cookies && !nd->nd_repstat)
1876 nd->nd_repstat = NFSERR_PERM;
1877 if (!nd->nd_repstat)
1878 nd->nd_repstat = getret;
1879 if (nd->nd_repstat) {
1882 free((caddr_t)cookies, M_TEMP);
1883 free((caddr_t)rbuf, M_TEMP);
1884 if (nd->nd_flag & ND_NFSV3)
1885 nfsrv_postopattr(nd, getret, &at);
1889 * If nothing read, return eof
1894 if (nd->nd_flag & ND_NFSV3)
1895 nfsrv_postopattr(nd, getret, &at);
1896 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1897 txdr_hyper(at.na_filerev, tl);
1899 *tl++ = newnfs_false;
1901 free((caddr_t)cookies, M_TEMP);
1902 free((caddr_t)rbuf, M_TEMP);
1907 * Check for degenerate cases of nothing useful read.
1908 * If so go try again
1912 dp = (struct dirent *)cpos;
1916 * For some reason FreeBSD's ufs_readdir() chooses to back the
1917 * directory offset up to a block boundary, so it is necessary to
1918 * skip over the records that precede the requested offset. This
1919 * requires the assumption that file offset cookies monotonically
1921 * Since the offset cookies don't monotonically increase for ZFS,
1922 * this is not done when ZFS is the file system.
1924 while (cpos < cend && ncookies > 0 &&
1925 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1926 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1927 ((nd->nd_flag & ND_NFSV4) &&
1928 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1929 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1930 cpos += dp->d_reclen;
1931 dp = (struct dirent *)cpos;
1935 if (cpos >= cend || ncookies == 0) {
1942 * Busy the file system so that the mount point won't go away
1943 * and, as such, VFS_VGET() can be used safely.
1947 NFSVOPUNLOCK(vp, 0);
1948 nd->nd_repstat = vfs_busy(mp, 0);
1950 if (nd->nd_repstat != 0) {
1952 free(cookies, M_TEMP);
1954 if (nd->nd_flag & ND_NFSV3)
1955 nfsrv_postopattr(nd, getret, &at);
1960 * Save this position, in case there is an error before one entry
1964 bpos0 = nd->nd_bpos;
1967 * Fill in the first part of the reply.
1968 * dirlen is the reply length in bytes and cannot exceed cnt.
1969 * (Include the two booleans at the end of the reply in dirlen now,
1970 * so we recognize when we have exceeded cnt.)
1972 if (nd->nd_flag & ND_NFSV3) {
1973 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1974 nfsrv_postopattr(nd, getret, &at);
1976 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
1978 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1979 txdr_hyper(at.na_filerev, tl);
1982 * Save this position, in case there is an empty reply needed.
1985 bpos1 = nd->nd_bpos;
1987 /* Loop through the records and build reply */
1989 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
1990 nlen = dp->d_namlen;
1991 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1992 nlen <= NFS_MAXNAMLEN &&
1993 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
1994 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
1995 || (nlen == 1 && dp->d_name[0] != '.'))) {
1997 * Save the current position in the reply, in case
1998 * this entry exceeds cnt.
2001 bpos1 = nd->nd_bpos;
2004 * For readdir_and_lookup get the vnode using
2013 mounted_on_fileno = (uint64_t)dp->d_fileno;
2014 if ((nd->nd_flag & ND_NFSV3) ||
2015 NFSNONZERO_ATTRBIT(&savbits)) {
2016 if (nd->nd_flag & ND_NFSV4)
2017 refp = nfsv4root_getreferral(NULL,
2021 r = VFS_VGET(mp, dp->d_fileno,
2025 if (r == EOPNOTSUPP) {
2028 cn.cn_nameiop = LOOKUP;
2036 cn.cn_nameptr = dp->d_name;
2037 cn.cn_namelen = nlen;
2038 cn.cn_flags = ISLASTCN |
2039 NOFOLLOW | LOCKLEAF |
2042 dp->d_name[0] == '.' &&
2043 dp->d_name[1] == '.')
2046 if (NFSVOPLOCK(vp, LK_SHARED)
2048 nd->nd_repstat = EPERM;
2051 if ((vp->v_vflag & VV_ROOT) != 0
2052 && (cn.cn_flags & ISDOTDOT)
2058 r = VOP_LOOKUP(vp, &nvp,
2067 * For NFSv4, check to see if nvp is
2068 * a mount point and get the mount
2069 * point vnode, as required.
2072 nfsrv_enable_crossmntpt != 0 &&
2073 (nd->nd_flag & ND_NFSV4) != 0 &&
2074 nvp->v_type == VDIR &&
2075 nvp->v_mountedhere != NULL) {
2076 new_mp = nvp->v_mountedhere;
2077 r = vfs_busy(new_mp, 0);
2081 r = VFS_ROOT(new_mp,
2091 ((nd->nd_flag & ND_NFSV3) ||
2092 NFSNONZERO_ATTRBIT(&attrbits))) {
2093 r = nfsvno_getfh(nvp, &nfh, p);
2095 r = nfsvno_getattr(nvp, nvap,
2102 if (!NFSISSET_ATTRBIT(&attrbits,
2103 NFSATTRBIT_RDATTRERROR)) {
2106 if (needs_unbusy != 0)
2115 * Build the directory record xdr
2117 if (nd->nd_flag & ND_NFSV3) {
2118 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2119 *tl++ = newnfs_true;
2121 *tl = txdr_unsigned(dp->d_fileno);
2122 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2123 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2125 *tl = txdr_unsigned(*cookiep);
2126 nfsrv_postopattr(nd, 0, nvap);
2127 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2128 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2132 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2133 *tl++ = newnfs_true;
2135 *tl = txdr_unsigned(*cookiep);
2136 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2138 supports_nfsv4acls =
2139 nfs_supportsnfsv4acls(nvp);
2140 NFSVOPUNLOCK(nvp, 0);
2142 supports_nfsv4acls = 0;
2144 dirlen += nfsrv_putreferralattr(nd,
2147 if (nd->nd_repstat) {
2150 if (needs_unbusy != 0)
2155 dirlen += nfsvno_fillattr(nd, new_mp,
2156 nvp, nvap, &nfh, r, &rderrbits,
2157 nd->nd_cred, p, isdgram, 0,
2158 supports_nfsv4acls, at_root,
2161 dirlen += nfsvno_fillattr(nd, new_mp,
2162 nvp, nvap, &nfh, r, &attrbits,
2163 nd->nd_cred, p, isdgram, 0,
2164 supports_nfsv4acls, at_root,
2169 dirlen += (3 * NFSX_UNSIGNED);
2171 if (needs_unbusy != 0)
2176 cpos += dp->d_reclen;
2177 dp = (struct dirent *)cpos;
2185 * If dirlen > cnt, we must strip off the last entry. If that
2186 * results in an empty reply, report NFSERR_TOOSMALL.
2188 if (dirlen > cnt || nd->nd_repstat) {
2189 if (!nd->nd_repstat && entrycnt == 0)
2190 nd->nd_repstat = NFSERR_TOOSMALL;
2192 newnfs_trimtrailing(nd, mb0, bpos0);
2194 newnfs_trimtrailing(nd, mb1, bpos1);
2196 } else if (cpos < cend)
2198 if (!nd->nd_repstat) {
2199 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2200 *tl++ = newnfs_false;
2206 FREE((caddr_t)cookies, M_TEMP);
2207 FREE((caddr_t)rbuf, M_TEMP);
2210 NFSEXITCODE2(0, nd);
2214 NFSEXITCODE2(error, nd);
2219 * Get the settable attributes out of the mbuf list.
2220 * (Return 0 or EBADRPC)
2223 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2224 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2227 struct nfsv2_sattr *sp;
2228 struct timeval curtime;
2229 int error = 0, toclient = 0;
2231 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2233 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2235 * Some old clients didn't fill in the high order 16bits.
2236 * --> check the low order 2 bytes for 0xffff
2238 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2239 nvap->na_mode = nfstov_mode(sp->sa_mode);
2240 if (sp->sa_uid != newnfs_xdrneg1)
2241 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2242 if (sp->sa_gid != newnfs_xdrneg1)
2243 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2244 if (sp->sa_size != newnfs_xdrneg1)
2245 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2246 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2248 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2250 nvap->na_atime.tv_sec =
2251 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2252 nvap->na_atime.tv_nsec = 0;
2255 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2256 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2259 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2260 if (*tl == newnfs_true) {
2261 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2262 nvap->na_mode = nfstov_mode(*tl);
2264 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2265 if (*tl == newnfs_true) {
2266 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2267 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2269 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2270 if (*tl == newnfs_true) {
2271 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2272 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2274 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2275 if (*tl == newnfs_true) {
2276 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2277 nvap->na_size = fxdr_hyper(tl);
2279 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2280 switch (fxdr_unsigned(int, *tl)) {
2281 case NFSV3SATTRTIME_TOCLIENT:
2282 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2283 fxdr_nfsv3time(tl, &nvap->na_atime);
2286 case NFSV3SATTRTIME_TOSERVER:
2287 NFSGETTIME(&curtime);
2288 nvap->na_atime.tv_sec = curtime.tv_sec;
2289 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2290 nvap->na_vaflags |= VA_UTIMES_NULL;
2293 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2294 switch (fxdr_unsigned(int, *tl)) {
2295 case NFSV3SATTRTIME_TOCLIENT:
2296 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2297 fxdr_nfsv3time(tl, &nvap->na_mtime);
2298 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2300 case NFSV3SATTRTIME_TOSERVER:
2301 NFSGETTIME(&curtime);
2302 nvap->na_mtime.tv_sec = curtime.tv_sec;
2303 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2305 nvap->na_vaflags |= VA_UTIMES_NULL;
2310 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2313 NFSEXITCODE2(error, nd);
2318 * Handle the setable attributes for V4.
2319 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2322 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2323 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2328 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2330 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2333 struct timeval curtime;
2335 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2338 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2339 attrsize = fxdr_unsigned(int, *tl);
2342 * Loop around getting the setable attributes. If an unsupported
2343 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2346 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2347 bitpos = NFSATTRBIT_MAX;
2351 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2352 if (attrsum > attrsize) {
2353 error = NFSERR_BADXDR;
2356 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2358 case NFSATTRBIT_SIZE:
2359 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2360 nvap->na_size = fxdr_hyper(tl);
2361 attrsum += NFSX_HYPER;
2363 case NFSATTRBIT_ACL:
2364 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2368 if (aceerr && !nd->nd_repstat)
2369 nd->nd_repstat = aceerr;
2372 case NFSATTRBIT_ARCHIVE:
2373 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2374 if (!nd->nd_repstat)
2375 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2376 attrsum += NFSX_UNSIGNED;
2378 case NFSATTRBIT_HIDDEN:
2379 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2380 if (!nd->nd_repstat)
2381 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2382 attrsum += NFSX_UNSIGNED;
2384 case NFSATTRBIT_MIMETYPE:
2385 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2386 i = fxdr_unsigned(int, *tl);
2387 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2390 if (!nd->nd_repstat)
2391 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2392 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2394 case NFSATTRBIT_MODE:
2395 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2396 nvap->na_mode = nfstov_mode(*tl);
2397 attrsum += NFSX_UNSIGNED;
2399 case NFSATTRBIT_OWNER:
2400 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2401 j = fxdr_unsigned(int, *tl);
2403 error = NFSERR_BADXDR;
2406 if (j > NFSV4_SMALLSTR)
2407 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2410 error = nfsrv_mtostr(nd, cp, j);
2412 if (j > NFSV4_SMALLSTR)
2413 free(cp, M_NFSSTRING);
2416 if (!nd->nd_repstat) {
2417 nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p);
2418 if (!nd->nd_repstat)
2421 if (j > NFSV4_SMALLSTR)
2422 free(cp, M_NFSSTRING);
2423 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2425 case NFSATTRBIT_OWNERGROUP:
2426 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2427 j = fxdr_unsigned(int, *tl);
2429 error = NFSERR_BADXDR;
2432 if (j > NFSV4_SMALLSTR)
2433 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2436 error = nfsrv_mtostr(nd, cp, j);
2438 if (j > NFSV4_SMALLSTR)
2439 free(cp, M_NFSSTRING);
2442 if (!nd->nd_repstat) {
2443 nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p);
2444 if (!nd->nd_repstat)
2447 if (j > NFSV4_SMALLSTR)
2448 free(cp, M_NFSSTRING);
2449 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2451 case NFSATTRBIT_SYSTEM:
2452 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2453 if (!nd->nd_repstat)
2454 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2455 attrsum += NFSX_UNSIGNED;
2457 case NFSATTRBIT_TIMEACCESSSET:
2458 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2459 attrsum += NFSX_UNSIGNED;
2460 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2461 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2462 fxdr_nfsv4time(tl, &nvap->na_atime);
2464 attrsum += NFSX_V4TIME;
2466 NFSGETTIME(&curtime);
2467 nvap->na_atime.tv_sec = curtime.tv_sec;
2468 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2469 nvap->na_vaflags |= VA_UTIMES_NULL;
2472 case NFSATTRBIT_TIMEBACKUP:
2473 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2474 if (!nd->nd_repstat)
2475 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2476 attrsum += NFSX_V4TIME;
2478 case NFSATTRBIT_TIMECREATE:
2479 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2480 if (!nd->nd_repstat)
2481 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2482 attrsum += NFSX_V4TIME;
2484 case NFSATTRBIT_TIMEMODIFYSET:
2485 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2486 attrsum += NFSX_UNSIGNED;
2487 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2488 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2489 fxdr_nfsv4time(tl, &nvap->na_mtime);
2490 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2491 attrsum += NFSX_V4TIME;
2493 NFSGETTIME(&curtime);
2494 nvap->na_mtime.tv_sec = curtime.tv_sec;
2495 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2497 nvap->na_vaflags |= VA_UTIMES_NULL;
2501 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2503 * set bitpos so we drop out of the loop.
2505 bitpos = NFSATTRBIT_MAX;
2511 * some clients pad the attrlist, so we need to skip over the
2514 if (attrsum > attrsize) {
2515 error = NFSERR_BADXDR;
2517 attrsize = NFSM_RNDUP(attrsize);
2518 if (attrsum < attrsize)
2519 error = nfsm_advance(nd, attrsize - attrsum, -1);
2522 NFSEXITCODE2(error, nd);
2527 * Check/setup export credentials.
2530 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2531 struct ucred *credanon)
2536 * Check/setup credentials.
2538 if (nd->nd_flag & ND_GSS)
2539 exp->nes_exflag &= ~MNT_EXPORTANON;
2542 * Check to see if the operation is allowed for this security flavor.
2543 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2544 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2545 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2547 if (nfsvno_testexp(nd, exp) &&
2548 nd->nd_procnum != NFSV4OP_SECINFO &&
2549 nd->nd_procnum != NFSPROC_FSINFO) {
2550 if (nd->nd_flag & ND_NFSV4)
2551 error = NFSERR_WRONGSEC;
2553 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2558 * Check to see if the file system is exported V4 only.
2560 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2561 error = NFSERR_PROGNOTV4;
2566 * Now, map the user credentials.
2567 * (Note that ND_AUTHNONE will only be set for an NFSv3
2568 * Fsinfo RPC. If set for anything else, this code might need
2571 if (NFSVNO_EXPORTED(exp) &&
2572 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2573 NFSVNO_EXPORTANON(exp) ||
2574 (nd->nd_flag & ND_AUTHNONE))) {
2575 nd->nd_cred->cr_uid = credanon->cr_uid;
2576 nd->nd_cred->cr_gid = credanon->cr_gid;
2577 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2578 credanon->cr_groups);
2582 NFSEXITCODE2(error, nd);
2590 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2591 struct ucred **credp)
2593 int i, error, *secflavors;
2595 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2596 &exp->nes_numsecflavor, &secflavors);
2598 if (nfs_rootfhset) {
2599 exp->nes_exflag = 0;
2600 exp->nes_numsecflavor = 0;
2604 /* Copy the security flavors. */
2605 for (i = 0; i < exp->nes_numsecflavor; i++)
2606 exp->nes_secflavors[i] = secflavors[i];
2613 * Get a vnode for a file handle and export stuff.
2616 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2617 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2618 struct ucred **credp)
2620 int i, error, *secflavors;
2623 exp->nes_numsecflavor = 0;
2624 if (VFS_NEEDSGIANT(mp))
2627 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp);
2629 /* Make sure the server replies ESTALE to the client. */
2631 if (nam && !error) {
2632 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2633 &exp->nes_numsecflavor, &secflavors);
2635 if (nfs_rootfhset) {
2636 exp->nes_exflag = 0;
2637 exp->nes_numsecflavor = 0;
2643 /* Copy the security flavors. */
2644 for (i = 0; i < exp->nes_numsecflavor; i++)
2645 exp->nes_secflavors[i] = secflavors[i];
2648 if (error == 0 && lktype == LK_SHARED)
2650 * It would be much better to pass lktype to VFS_FHTOVP(),
2651 * but this will have to do until VFS_FHTOVP() has a lock
2652 * type argument like VFS_VGET().
2654 NFSVOPLOCK(*vpp, LK_DOWNGRADE | LK_RETRY);
2661 * nfsd_fhtovp() - convert a fh to a vnode ptr
2662 * - look up fsid in mount list (if not found ret error)
2663 * - get vp and export rights by calling nfsvno_fhtovp()
2664 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2666 * - if mpp != NULL, return the mount point so that it can
2667 * be used for vn_finished_write() by the caller
2670 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2671 struct vnode **vpp, struct nfsexstuff *exp,
2672 struct mount **mpp, int startwrite, struct thread *p)
2675 struct ucred *credanon;
2678 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2680 * Check for the special case of the nfsv4root_fh.
2682 mp = vfs_busyfs(&fhp->fh_fsid);
2687 nd->nd_repstat = ESTALE;
2692 vn_start_write(NULL, mpp, V_WAIT);
2694 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2699 * For NFSv4 without a pseudo root fs, unexported file handles
2700 * can be returned, so that Lookup works everywhere.
2702 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2703 !(nd->nd_flag & ND_NFSV4)) {
2705 nd->nd_repstat = EACCES;
2709 * Personally, I've never seen any point in requiring a
2710 * reserved port#, since only in the rare case where the
2711 * clients are all boxes with secure system priviledges,
2712 * does it provide any enhanced security, but... some people
2713 * believe it to be useful and keep putting this code back in.
2714 * (There is also some "security checker" out there that
2715 * complains if the nfs server doesn't enforce this.)
2716 * However, note the following:
2717 * RFC3530 (NFSv4) specifies that a reserved port# not be
2719 * RFC2623 recommends that, if a reserved port# is checked for,
2720 * that there be a way to turn that off--> ifdef'd.
2722 #ifdef NFS_REQRSVPORT
2723 if (!nd->nd_repstat) {
2724 struct sockaddr_in *saddr;
2725 struct sockaddr_in6 *saddr6;
2727 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2728 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2729 if (!(nd->nd_flag & ND_NFSV4) &&
2730 ((saddr->sin_family == AF_INET &&
2731 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2732 (saddr6->sin6_family == AF_INET6 &&
2733 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2735 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2738 #endif /* NFS_REQRSVPORT */
2741 * Check/setup credentials.
2743 if (!nd->nd_repstat) {
2744 nd->nd_saveduid = nd->nd_cred->cr_uid;
2745 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2749 if (credanon != NULL)
2751 if (nd->nd_repstat) {
2753 vn_finished_write(mp);
2760 NFSEXITCODE2(0, nd);
2767 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2769 struct filedesc *fdp;
2773 fdp = p->td_proc->p_fd;
2774 if (fd >= fdp->fd_nfiles ||
2775 (fp = fdp->fd_ofiles[fd]) == NULL) {
2787 * Called from nfssvc() to update the exports list. Just call
2788 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2789 * in the mount list.
2792 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2794 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2796 struct nameidata nd;
2799 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2800 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2802 else if (error == 0) {
2803 if (nfsexargp->fspec == NULL) {
2808 * If fspec != NULL, this is the v4root path.
2810 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2811 nfsexargp->fspec, p);
2812 if ((error = namei(&nd)) != 0)
2814 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2817 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2818 NFSBCOPY((caddr_t)&fh,
2819 nfs_rootfh.nfsrvfh_data,
2820 sizeof (fhandle_t));
2831 * Get the tcp socket sequence numbers we need.
2832 * (Maybe this should be moved to the tcp sources?)
2835 nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2841 inp = sotoinpcb(so);
2842 KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2844 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2849 tp = intotcpcb(inp);
2850 if (tp->t_state != TCPS_ESTABLISHED) {
2855 *maxp = tp->snd_max;
2856 *unap = tp->snd_una;
2865 * This function needs to test to see if the system is near its limit
2866 * for memory allocation via malloc() or mget() and return True iff
2867 * either of these resources are near their limit.
2868 * XXX (For now, this is just a stub.)
2870 int nfsrv_testmalloclimit = 0;
2872 nfsrv_mallocmget_limit(void)
2874 static int printmesg = 0;
2875 static int testval = 1;
2877 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2878 if ((printmesg++ % 100) == 0)
2879 printf("nfsd: malloc/mget near limit\n");
2886 * BSD specific initialization of a mount point.
2891 static int inited = 0;
2896 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2897 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2898 nfsv4root_mnt.mnt_export = NULL;
2899 TAILQ_INIT(&nfsv4root_opt);
2900 TAILQ_INIT(&nfsv4root_newopt);
2901 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2902 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2903 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2907 * Get a vnode for a file handle, without checking exports, etc.
2910 nfsvno_getvp(fhandle_t *fhp)
2916 mp = vfs_busyfs(&fhp->fh_fsid);
2919 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2927 * Do a local VOP_ADVLOCK().
2930 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2931 u_int64_t end, struct thread *td)
2937 if (nfsrv_dolocallocks == 0)
2940 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2941 if ((vp->v_iflag & VI_DOOMED) != 0) {
2946 fl.l_whence = SEEK_SET;
2948 fl.l_start = (off_t)first;
2949 if (end == NFS64BITSSET) {
2953 fl.l_len = (off_t)tlen;
2956 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2957 * values for all calls, so that all locks will be held by the
2958 * nfsd server. (The nfsd server handles conflicts between the
2960 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2961 * bytes, so it can't be put in l_sysid.
2963 if (nfsv4_sysid == 0)
2964 nfsv4_sysid = nlm_acquire_next_sysid();
2965 fl.l_pid = (pid_t)0;
2966 fl.l_sysid = (int)nfsv4_sysid;
2968 NFSVOPUNLOCK(vp, 0);
2969 if (ftype == F_UNLCK)
2970 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2971 (F_POSIX | F_REMOTE));
2973 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2974 (F_POSIX | F_REMOTE));
2975 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2983 * Check the nfsv4 root exports.
2986 nfsvno_v4rootexport(struct nfsrv_descript *nd)
2988 struct ucred *credanon;
2989 int exflags, error = 0, numsecflavor, *secflavors, i;
2991 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
2992 &credanon, &numsecflavor, &secflavors);
2994 error = NFSERR_PROGUNAVAIL;
2997 if (credanon != NULL)
2999 for (i = 0; i < numsecflavor; i++) {
3000 if (secflavors[i] == AUTH_SYS)
3001 nd->nd_flag |= ND_EXAUTHSYS;
3002 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3003 nd->nd_flag |= ND_EXGSS;
3004 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3005 nd->nd_flag |= ND_EXGSSINTEGRITY;
3006 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3007 nd->nd_flag |= ND_EXGSSPRIVACY;
3016 * Nfs server psuedo system call for the nfsd's
3022 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3025 struct nfsd_addsock_args sockarg;
3026 struct nfsd_nfsd_args nfsdarg;
3029 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3030 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3034 * Since we don't know what rights might be required,
3035 * pretend that we need them all. It is better to be too
3036 * careful than too reckless.
3038 if ((error = fget(td, sockarg.sock, CAP_SOCK_ALL, &fp)) != 0)
3040 if (fp->f_type != DTYPE_SOCKET) {
3045 error = nfsrvd_addsock(fp);
3047 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3048 if (uap->argp == NULL) {
3052 error = copyin(uap->argp, (caddr_t)&nfsdarg,
3056 error = nfsrvd_nfsd(td, &nfsdarg);
3058 error = nfssvc_srvcall(td, uap, td->td_ucred);
3067 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3069 struct nfsex_args export;
3070 struct file *fp = NULL;
3072 struct nfsd_clid adminrevoke;
3073 struct nfsd_dumplist dumplist;
3074 struct nfsd_dumpclients *dumpclients;
3075 struct nfsd_dumplocklist dumplocklist;
3076 struct nfsd_dumplocks *dumplocks;
3077 struct nameidata nd;
3082 if (uap->flag & NFSSVC_PUBLICFH) {
3083 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3084 sizeof (fhandle_t));
3085 error = copyin(uap->argp,
3086 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3089 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3090 error = copyin(uap->argp,(caddr_t)&export,
3091 sizeof (struct nfsex_args));
3093 error = nfsrv_v4rootexport(&export, cred, p);
3094 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3097 } else if (uap->flag & NFSSVC_STABLERESTART) {
3098 error = copyin(uap->argp, (caddr_t)&stablefd,
3101 error = fp_getfvp(p, stablefd, &fp, &vp);
3102 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3104 if (!error && newnfs_numnfsd != 0)
3107 nfsrv_stablefirst.nsf_fp = fp;
3108 nfsrv_setupstable(p);
3110 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3111 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3112 sizeof (struct nfsd_clid));
3114 error = nfsrv_adminrevoke(&adminrevoke, p);
3115 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3116 error = copyin(uap->argp, (caddr_t)&dumplist,
3117 sizeof (struct nfsd_dumplist));
3118 if (!error && (dumplist.ndl_size < 1 ||
3119 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3122 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3123 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3125 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3126 error = copyout(dumpclients,
3127 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3128 free((caddr_t)dumpclients, M_TEMP);
3130 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3131 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3132 sizeof (struct nfsd_dumplocklist));
3133 if (!error && (dumplocklist.ndllck_size < 1 ||
3134 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3137 error = nfsrv_lookupfilename(&nd,
3138 dumplocklist.ndllck_fname, p);
3140 len = sizeof (struct nfsd_dumplocks) *
3141 dumplocklist.ndllck_size;
3142 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3144 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3145 dumplocklist.ndllck_size, p);
3147 error = copyout(dumplocks,
3148 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3149 free((caddr_t)dumplocks, M_TEMP);
3151 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3154 nfsd_master_pid = procp->p_pid;
3155 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3156 nfsd_master_start = procp->p_stats->p_start;
3157 nfsd_master_proc = procp;
3167 * Returns 0 if ok, 1 otherwise.
3170 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3175 * This seems odd, but allow the case where the security flavor
3176 * list is empty. This happens when NFSv4 is traversing non-exported
3177 * file systems. Exported file systems should always have a non-empty
3178 * security flavor list.
3180 if (exp->nes_numsecflavor == 0)
3183 for (i = 0; i < exp->nes_numsecflavor; i++) {
3185 * The tests for privacy and integrity must be first,
3186 * since ND_GSS is set for everything but AUTH_SYS.
3188 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3189 (nd->nd_flag & ND_GSSPRIVACY))
3191 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3192 (nd->nd_flag & ND_GSSINTEGRITY))
3194 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3195 (nd->nd_flag & ND_GSS))
3197 if (exp->nes_secflavors[i] == AUTH_SYS &&
3198 (nd->nd_flag & ND_GSS) == 0)
3205 * Calculate a hash value for the fid in a file handle.
3208 nfsrv_hashfh(fhandle_t *fhp)
3212 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3217 * Signal the userland master nfsd to backup the stable restart file.
3220 nfsrv_backupstable(void)
3224 if (nfsd_master_proc != NULL) {
3225 procp = pfind(nfsd_master_pid);
3226 /* Try to make sure it is the correct process. */
3227 if (procp == nfsd_master_proc &&
3228 procp->p_stats->p_start.tv_sec ==
3229 nfsd_master_start.tv_sec &&
3230 procp->p_stats->p_start.tv_usec ==
3231 nfsd_master_start.tv_usec &&
3232 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3233 kern_psignal(procp, SIGUSR2);
3235 nfsd_master_proc = NULL;
3242 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3245 * Called once to initialize data structures...
3248 nfsd_modevent(module_t mod, int type, void *data)
3251 static int loaded = 0;
3258 mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF);
3259 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3260 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3262 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3269 #ifdef VV_DISABLEDELEG
3270 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3271 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3273 nfsd_call_servertimer = nfsrv_servertimer;
3274 nfsd_call_nfsd = nfssvc_nfsd;
3279 if (newnfs_numnfsd != 0) {
3284 #ifdef VV_DISABLEDELEG
3285 vn_deleg_ops.vndeleg_recall = NULL;
3286 vn_deleg_ops.vndeleg_disable = NULL;
3288 nfsd_call_servertimer = NULL;
3289 nfsd_call_nfsd = NULL;
3291 /* Clean out all NFSv4 state. */
3292 nfsrv_throwawayallstate(curthread);
3294 /* Clean the NFS server reply cache */
3295 nfsrvd_cleancache();
3297 /* Free up the krpc server pool. */
3298 if (nfsrvd_pool != NULL)
3299 svcpool_destroy(nfsrvd_pool);
3301 /* and get rid of the locks */
3302 mtx_destroy(&nfs_cache_mutex);
3303 mtx_destroy(&nfs_v4root_mutex);
3304 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3305 lockdestroy(&nfsv4root_mnt.mnt_explock);
3317 static moduledata_t nfsd_mod = {
3322 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3324 /* So that loader and kldload(2) can find us, wherever we are.. */
3325 MODULE_VERSION(nfsd, 1);
3326 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3327 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3328 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3329 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3330 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);