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>
44 #include <sys/sysctl.h>
45 #include <nlm/nlm_prot.h>
48 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
49 extern int nfsv4root_set;
50 extern int nfsrv_useacl;
51 extern int newnfs_numnfsd;
52 extern struct mount nfsv4root_mnt;
53 extern struct nfsrv_stablefirst nfsrv_stablefirst;
54 extern void (*nfsd_call_servertimer)(void);
55 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
57 struct mtx nfs_cache_mutex;
58 struct mtx nfs_v4root_mutex;
59 struct nfsrvfh nfs_rootfh, nfs_pubfh;
60 int nfs_pubfhset = 0, nfs_rootfhset = 0;
61 static uint32_t nfsv4_sysid = 0;
63 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
66 static int enable_crossmntpt = 1;
67 static int nfs_commit_blks;
68 static int nfs_commit_miss;
69 extern int nfsrv_issuedelegs;
70 extern int nfsrv_dolocallocks;
72 SYSCTL_DECL(_vfs_newnfs);
73 SYSCTL_INT(_vfs_newnfs, OID_AUTO, mirrormnt, CTLFLAG_RW, &enable_crossmntpt,
74 0, "Enable nfsd to cross mount points");
75 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
77 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
79 SYSCTL_INT(_vfs_newnfs, OID_AUTO, issue_delegations, CTLFLAG_RW,
80 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
81 SYSCTL_INT(_vfs_newnfs, OID_AUTO, enable_locallocks, CTLFLAG_RW,
82 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
84 #define NUM_HEURISTIC 1017
87 #define NHUSE_MAX 2048
89 static struct nfsheur {
90 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
91 off_t nh_nextr; /* next offset for sequential detection */
92 int nh_use; /* use count for selection */
93 int nh_seqcount; /* heuristic */
94 } nfsheur[NUM_HEURISTIC];
98 * Get attributes into nfsvattr structure.
101 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
104 int error, lockedit = 0;
106 /* Since FreeBSD insists the vnode be locked... */
107 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
109 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
111 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
113 NFSVOPUNLOCK(vp, 0, p);
118 * Get a file handle for a vnode.
121 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
125 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
126 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
127 error = VOP_VPTOFH(vp, &fhp->fh_fid);
132 * Perform access checking for vnodes obtained from file handles that would
133 * refer to files already opened by a Unix client. You cannot just use
134 * vn_writechk() and VOP_ACCESSX() for two reasons.
135 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
137 * 2 - The owner is to be given access irrespective of mode bits for some
138 * operations, so that processes that chmod after opening a file don't
142 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
143 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
144 u_int32_t *supportedtypep)
147 int error = 0, getret = 0;
149 if (accmode & VWRITE) {
150 /* Just vn_writechk() changed to check rdonly */
152 * Disallow write attempts on read-only file systems;
153 * unless the file is a socket or a block or character
154 * device resident on the file system.
156 if (NFSVNO_EXRDONLY(exp) ||
157 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
158 switch (vp->v_type) {
168 * If there's shared text associated with
169 * the inode, try to free it up once. If
170 * we fail, we can't allow writing.
172 if (vp->v_vflag & VV_TEXT)
176 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
179 * Should the override still be applied when ACLs are enabled?
181 error = VOP_ACCESSX(vp, accmode, cred, p);
182 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
184 * Try again with VEXPLICIT_DENY, to see if the test for
185 * deletion is supported.
187 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
189 if (vp->v_type == VDIR) {
190 accmode &= ~(VDELETE | VDELETE_CHILD);
192 error = VOP_ACCESSX(vp, accmode, cred, p);
193 } else if (supportedtypep != NULL) {
194 *supportedtypep &= ~NFSACCESS_DELETE;
200 * Allow certain operations for the owner (reads and writes
201 * on files that are already open).
203 if (override != NFSACCCHK_NOOVERRIDE &&
204 (error == EPERM || error == EACCES)) {
205 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
207 else if (override & NFSACCCHK_ALLOWOWNER) {
208 getret = VOP_GETATTR(vp, &vattr, cred);
209 if (getret == 0 && cred->cr_uid == vattr.va_uid)
214 NFSVOPUNLOCK(vp, 0, p);
219 * Set attribute(s) vnop.
222 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
223 struct thread *p, struct nfsexstuff *exp)
227 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
232 * Set up nameidata for a lookup() call and do it
233 * For the cases where we are crossing mount points
234 * (looking up the public fh path or the v4 root path when
235 * not using a pseudo-root fs), set/release the Giant lock,
239 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
240 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
241 struct vnode **retdirp)
243 struct componentname *cnp = &ndp->ni_cnd;
247 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
248 int error = 0, crossmnt;
252 cnp->cn_nameptr = cnp->cn_pnbuf;
254 * Extract and set starting directory.
256 if (dp->v_type != VDIR) {
261 nfsvno_relpathbuf(ndp);
265 NFSVOPUNLOCK(dp, 0, p);
268 if (NFSVNO_EXRDONLY(exp))
269 cnp->cn_flags |= RDONLY;
270 ndp->ni_segflg = UIO_SYSSPACE;
273 if (nd->nd_flag & ND_PUBLOOKUP) {
275 if (cnp->cn_pnbuf[0] == '/') {
278 * Check for degenerate pathnames here, since lookup()
281 for (i = 1; i < ndp->ni_pathlen; i++)
282 if (cnp->cn_pnbuf[i] != '/')
284 if (i == ndp->ni_pathlen) {
285 error = NFSERR_ACCES;
291 } else if ((enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
292 (nd->nd_flag & ND_NFSV4) == 0) {
294 * Only cross mount points for NFSv4 when doing a
295 * mount while traversing the file system above
296 * the mount point, unless enable_crossmntpt is set.
298 cnp->cn_flags |= NOCROSSMOUNT;
303 * Initialize for scan, set ni_startdir and bump ref on dp again
304 * becuase lookup() will dereference ni_startdir.
308 ndp->ni_startdir = dp;
309 ndp->ni_rootdir = rootvnode;
312 cnp->cn_flags |= LOCKLEAF;
314 cnp->cn_nameptr = cnp->cn_pnbuf;
316 * Call lookup() to do the real work. If an error occurs,
317 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
318 * we do not have to dereference anything before returning.
319 * In either case ni_startdir will be dereferenced and NULLed
322 if (exp->nes_vfslocked)
323 ndp->ni_cnd.cn_flags |= GIANTHELD;
326 * The Giant lock should only change when
327 * crossing mount points.
331 (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
332 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
338 * Check for encountering a symbolic link. Trivial
339 * termination occurs if no symlink encountered.
341 if ((cnp->cn_flags & ISSYMLINK) == 0) {
342 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
343 nfsvno_relpathbuf(ndp);
344 if (ndp->ni_vp && !lockleaf)
345 NFSVOPUNLOCK(ndp->ni_vp, 0, p);
352 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
353 NFSVOPUNLOCK(ndp->ni_dvp, 0, p);
354 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
359 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
363 if (ndp->ni_pathlen > 1)
364 cp = uma_zalloc(namei_zone, M_WAITOK);
368 aiov.iov_len = MAXPATHLEN;
369 auio.uio_iov = &aiov;
372 auio.uio_rw = UIO_READ;
373 auio.uio_segflg = UIO_SYSSPACE;
375 auio.uio_resid = MAXPATHLEN;
376 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
379 if (ndp->ni_pathlen > 1)
380 uma_zfree(namei_zone, cp);
386 linklen = MAXPATHLEN - auio.uio_resid;
391 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
392 error = ENAMETOOLONG;
397 * Adjust or replace path
399 if (ndp->ni_pathlen > 1) {
400 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
401 uma_zfree(namei_zone, cnp->cn_pnbuf);
404 cnp->cn_pnbuf[linklen] = '\0';
405 ndp->ni_pathlen += linklen;
408 * Cleanup refs for next loop and check if root directory
409 * should replace current directory. Normally ni_dvp
410 * becomes the new base directory and is cleaned up when
411 * we loop. Explicitly null pointers after invalidation
412 * to clarify operation.
417 if (cnp->cn_pnbuf[0] == '/') {
419 ndp->ni_dvp = ndp->ni_rootdir;
422 ndp->ni_startdir = ndp->ni_dvp;
426 cnp->cn_flags &= ~LOCKLEAF;
430 uma_zfree(namei_zone, cnp->cn_pnbuf);
433 ndp->ni_startdir = NULL;
434 cnp->cn_flags &= ~HASBUF;
435 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
442 * Set up a pathname buffer and return a pointer to it and, optionally
443 * set a hash pointer.
446 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
448 struct componentname *cnp = &ndp->ni_cnd;
450 cnp->cn_flags |= (NOMACCHECK | HASBUF);
451 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
454 *bufpp = cnp->cn_pnbuf;
458 * Release the above path buffer, if not released by nfsvno_namei().
461 nfsvno_relpathbuf(struct nameidata *ndp)
464 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
466 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
467 ndp->ni_cnd.cn_flags &= ~HASBUF;
471 * Readlink vnode op into an mbuf list.
474 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
475 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
477 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
478 struct iovec *ivp = iv;
479 struct uio io, *uiop = &io;
480 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
481 int i, len, tlen, error;
485 while (len < NFS_MAXPATHLEN) {
488 mp->m_len = NFSMSIZ(mp);
495 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
496 mp->m_len = NFS_MAXPATHLEN - len;
497 len = NFS_MAXPATHLEN;
501 ivp->iov_base = mtod(mp, caddr_t);
502 ivp->iov_len = mp->m_len;
507 uiop->uio_iovcnt = i;
508 uiop->uio_offset = 0;
509 uiop->uio_resid = len;
510 uiop->uio_rw = UIO_READ;
511 uiop->uio_segflg = UIO_SYSSPACE;
513 error = VOP_READLINK(vp, uiop, cred);
519 if (uiop->uio_resid > 0) {
520 len -= uiop->uio_resid;
521 tlen = NFSM_RNDUP(len);
522 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
531 * Read vnode op call into mbuf list.
534 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
535 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
541 int error = 0, len, left, siz, tlen, ioflag = 0, hi, try = 32;
542 struct mbuf *m2 = NULL, *m3;
543 struct uio io, *uiop = &io;
547 * Calculate seqcount for heuristic
550 * Locate best candidate
553 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
557 if (nfsheur[hi].nh_vp == vp) {
561 if (nfsheur[hi].nh_use > 0)
562 --nfsheur[hi].nh_use;
563 hi = (hi + 1) % NUM_HEURISTIC;
564 if (nfsheur[hi].nh_use < nh->nh_use)
568 if (nh->nh_vp != vp) {
571 nh->nh_use = NHUSE_INIT;
579 * Calculate heuristic
582 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
583 if (++nh->nh_seqcount > IO_SEQMAX)
584 nh->nh_seqcount = IO_SEQMAX;
585 } else if (nh->nh_seqcount > 1) {
590 nh->nh_use += NHUSE_INC;
591 if (nh->nh_use > NHUSE_MAX)
592 nh->nh_use = NHUSE_MAX;
593 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
595 len = left = NFSM_RNDUP(cnt);
598 * Generate the mbuf list with the uio_iov ref. to it.
605 siz = min(M_TRAILINGSPACE(m), left);
614 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
616 uiop->uio_iov = iv2 = iv;
622 panic("nfsvno_read iov");
623 siz = min(M_TRAILINGSPACE(m), left);
625 iv->iov_base = mtod(m, caddr_t) + m->m_len;
634 uiop->uio_iovcnt = i;
635 uiop->uio_offset = off;
636 uiop->uio_resid = len;
637 uiop->uio_rw = UIO_READ;
638 uiop->uio_segflg = UIO_SYSSPACE;
639 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
640 FREE((caddr_t)iv2, M_TEMP);
646 tlen = len - uiop->uio_resid;
647 cnt = cnt < tlen ? cnt : tlen;
648 tlen = NFSM_RNDUP(cnt);
652 } else if (len != tlen || tlen != cnt)
653 nfsrv_adj(m3, len - tlen, tlen - cnt);
660 * Write vnode op from an mbuf list.
663 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
664 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
670 struct uio io, *uiop = &io;
672 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
674 uiop->uio_iov = iv = ivp;
675 uiop->uio_iovcnt = cnt;
676 i = mtod(mp, caddr_t) + mp->m_len - cp;
680 panic("nfsvno_write");
691 cp = mtod(mp, caddr_t);
695 if (stable == NFSWRITE_UNSTABLE)
696 ioflags = IO_NODELOCKED;
698 ioflags = (IO_SYNC | IO_NODELOCKED);
699 uiop->uio_resid = retlen;
700 uiop->uio_rw = UIO_WRITE;
701 uiop->uio_segflg = UIO_SYSSPACE;
703 uiop->uio_offset = off;
704 error = VOP_WRITE(vp, uiop, ioflags, cred);
705 FREE((caddr_t)iv, M_TEMP);
710 * Common code for creating a regular file (plus special files for V2).
713 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
714 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
715 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
720 error = nd->nd_repstat;
721 if (!error && ndp->ni_vp == NULL) {
722 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
723 vrele(ndp->ni_startdir);
724 error = VOP_CREATE(ndp->ni_dvp,
725 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
727 nfsvno_relpathbuf(ndp);
729 if (*exclusive_flagp) {
730 *exclusive_flagp = 0;
731 NFSVNO_ATTRINIT(nvap);
732 nvap->na_atime.tv_sec = cverf[0];
733 nvap->na_atime.tv_nsec = cverf[1];
734 error = VOP_SETATTR(ndp->ni_vp,
735 &nvap->na_vattr, nd->nd_cred);
739 * NFS V2 Only. nfsrvd_mknod() does this for V3.
740 * (This implies, just get out on an error.)
742 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
743 nvap->na_type == VFIFO) {
744 if (nvap->na_type == VCHR && rdev == 0xffffffff)
745 nvap->na_type = VFIFO;
746 if (nvap->na_type != VFIFO &&
747 (error = priv_check_cred(nd->nd_cred,
748 PRIV_VFS_MKNOD_DEV, 0))) {
749 vrele(ndp->ni_startdir);
750 nfsvno_relpathbuf(ndp);
754 nvap->na_rdev = rdev;
755 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
756 &ndp->ni_cnd, &nvap->na_vattr);
758 nfsvno_relpathbuf(ndp);
760 vrele(ndp->ni_startdir);
764 vrele(ndp->ni_startdir);
765 nfsvno_relpathbuf(ndp);
772 * Handle cases where error is already set and/or
774 * 1 - clean up the lookup
775 * 2 - iff !error and na_size set, truncate it
777 vrele(ndp->ni_startdir);
778 nfsvno_relpathbuf(ndp);
780 if (ndp->ni_dvp == *vpp)
784 if (!error && nvap->na_size != VNOVAL) {
785 error = nfsvno_accchk(*vpp, VWRITE,
786 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
787 NFSACCCHK_VPISLOCKED, NULL);
789 tempsize = nvap->na_size;
790 NFSVNO_ATTRINIT(nvap);
791 nvap->na_size = tempsize;
792 error = VOP_SETATTR(*vpp,
793 &nvap->na_vattr, nd->nd_cred);
803 * Do a mknod vnode op.
806 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
812 vtyp = nvap->na_type;
814 * Iff doesn't exist, create it.
817 vrele(ndp->ni_startdir);
818 nfsvno_relpathbuf(ndp);
823 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
824 vrele(ndp->ni_startdir);
825 nfsvno_relpathbuf(ndp);
827 return (NFSERR_BADTYPE);
830 vrele(ndp->ni_startdir);
831 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
832 &ndp->ni_cnd, &nvap->na_vattr);
834 nfsvno_relpathbuf(ndp);
836 if (nvap->na_type != VFIFO &&
837 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
838 vrele(ndp->ni_startdir);
839 nfsvno_relpathbuf(ndp);
843 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
844 &ndp->ni_cnd, &nvap->na_vattr);
846 nfsvno_relpathbuf(ndp);
848 vrele(ndp->ni_startdir);
850 * Since VOP_MKNOD returns the ni_vp, I can't
851 * see any reason to do the lookup.
861 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
862 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
866 if (ndp->ni_vp != NULL) {
867 if (ndp->ni_dvp == ndp->ni_vp)
872 nfsvno_relpathbuf(ndp);
875 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
878 nfsvno_relpathbuf(ndp);
886 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
887 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
888 struct nfsexstuff *exp)
893 vrele(ndp->ni_startdir);
894 nfsvno_relpathbuf(ndp);
895 if (ndp->ni_dvp == ndp->ni_vp)
903 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
904 &nvap->na_vattr, pathcp);
906 vrele(ndp->ni_startdir);
907 nfsvno_relpathbuf(ndp);
909 * Although FreeBSD still had the lookup code in
910 * it for 7/current, there doesn't seem to be any
911 * point, since VOP_SYMLINK() returns the ni_vp.
912 * Just vput it for v2.
914 if (!not_v2 && !error)
920 * Parse symbolic link arguments.
921 * This function has an ugly side effect. It will MALLOC() an area for
922 * the symlink and set iov_base to point to it, only if it succeeds.
923 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
927 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
928 struct thread *p, char **pathcpp, int *lenp)
933 struct nfsv2_sattr *sp;
937 if ((nd->nd_flag & ND_NFSV3) &&
938 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
940 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
941 len = fxdr_unsigned(int, *tl);
942 if (len > NFS_MAXPATHLEN || len <= 0) {
946 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
947 error = nfsrv_mtostr(nd, pathcp, len);
950 if (nd->nd_flag & ND_NFSV2) {
951 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
952 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
959 free(pathcp, M_TEMP);
964 * Remove a non-directory object.
967 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
968 struct thread *p, struct nfsexstuff *exp)
974 if (vp->v_type == VDIR)
975 error = NFSERR_ISDIR;
977 error = nfsrv_checkremove(vp, 1, p);
979 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
980 if (ndp->ni_dvp == vp)
989 * Remove a directory.
992 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
993 struct thread *p, struct nfsexstuff *exp)
999 if (vp->v_type != VDIR) {
1004 * No rmdir "." please.
1006 if (ndp->ni_dvp == vp) {
1011 * The root of a mounted filesystem cannot be deleted.
1013 if (vp->v_vflag & VV_ROOT)
1017 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1018 if (ndp->ni_dvp == vp)
1030 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1031 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1033 struct vnode *fvp, *tvp, *tdvp;
1036 fvp = fromndp->ni_vp;
1038 vrele(fromndp->ni_dvp);
1043 tdvp = tondp->ni_dvp;
1046 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1047 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1049 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1050 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1053 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1054 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1059 * A rename to '.' or '..' results in a prematurely
1060 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1063 if ((tondp->ni_cnd.cn_namelen == 1 &&
1064 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1065 (tondp->ni_cnd.cn_namelen == 2 &&
1066 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1067 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1072 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1073 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1076 if (fvp->v_mount != tdvp->v_mount) {
1077 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1081 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1086 * If source and destination are the same, there is nothing to
1087 * do. Set error to -1 to indicate this.
1092 if (ndflag & ND_NFSV4) {
1093 NFSVOPLOCK(fvp, LK_EXCLUSIVE | LK_RETRY, p);
1094 error = nfsrv_checkremove(fvp, 0, p);
1095 NFSVOPUNLOCK(fvp, 0, p);
1097 error = nfsrv_checkremove(tvp, 1, p);
1100 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1101 * that the NFSv4 client won't be confused by the rename.
1102 * Since nfsd_recalldelegation() can only be called on an
1103 * unlocked vnode at this point and fvp is the file that will
1104 * still exist after the rename, just do fvp.
1106 nfsd_recalldelegation(fvp, p);
1110 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1111 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1120 vrele(fromndp->ni_dvp);
1125 vrele(tondp->ni_startdir);
1126 nfsvno_relpathbuf(tondp);
1128 vrele(fromndp->ni_startdir);
1129 nfsvno_relpathbuf(fromndp);
1137 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1138 struct thread *p, struct nfsexstuff *exp)
1148 if (vp->v_mount != xp->v_mount)
1152 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
1153 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1154 if (ndp->ni_dvp == vp)
1158 NFSVOPUNLOCK(vp, 0, p);
1160 if (ndp->ni_dvp == ndp->ni_vp)
1167 nfsvno_relpathbuf(ndp);
1172 * Do the fsync() appropriate for the commit.
1175 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1180 if (cnt > MAX_COMMIT_COUNT) {
1182 * Give up and do the whole thing
1185 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1186 VM_OBJECT_LOCK(vp->v_object);
1187 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1188 VM_OBJECT_UNLOCK(vp->v_object);
1190 error = VOP_FSYNC(vp, MNT_WAIT, td);
1193 * Locate and synchronously write any buffers that fall
1194 * into the requested range. Note: we are assuming that
1195 * f_iosize is a power of 2.
1197 int iosize = vp->v_mount->mnt_stat.f_iosize;
1198 int iomask = iosize - 1;
1203 * Align to iosize boundry, super-align to page boundry.
1206 cnt += off & iomask;
1207 off &= ~(u_quad_t)iomask;
1209 if (off & PAGE_MASK) {
1210 cnt += off & PAGE_MASK;
1211 off &= ~(u_quad_t)PAGE_MASK;
1213 lblkno = off / iosize;
1216 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1217 VM_OBJECT_LOCK(vp->v_object);
1218 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
1219 VM_OBJECT_UNLOCK(vp->v_object);
1228 * If we have a buffer and it is marked B_DELWRI we
1229 * have to lock and write it. Otherwise the prior
1230 * write is assumed to have already been committed.
1232 * gbincore() can return invalid buffers now so we
1233 * have to check that bit as well (though B_DELWRI
1234 * should not be set if B_INVAL is set there could be
1235 * a race here since we haven't locked the buffer).
1237 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1238 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1239 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1241 continue; /* retry */
1243 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1246 bp->b_flags &= ~B_ASYNC;
1268 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1271 return (VFS_STATFS(vp->v_mount, sf));
1275 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1276 * must handle nfsrv_opencheck() calls after any other access checks.
1279 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1280 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1281 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1282 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1283 struct nfsexstuff *exp, struct vnode **vpp)
1285 struct vnode *vp = NULL;
1287 struct nfsexstuff nes;
1289 if (ndp->ni_vp == NULL)
1290 nd->nd_repstat = nfsrv_opencheck(clientid,
1291 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1292 if (!nd->nd_repstat) {
1293 if (ndp->ni_vp == NULL) {
1294 vrele(ndp->ni_startdir);
1295 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1296 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1298 nfsvno_relpathbuf(ndp);
1299 if (!nd->nd_repstat) {
1300 if (*exclusive_flagp) {
1301 *exclusive_flagp = 0;
1302 NFSVNO_ATTRINIT(nvap);
1303 nvap->na_atime.tv_sec = cverf[0];
1304 nvap->na_atime.tv_nsec = cverf[1];
1305 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1306 &nvap->na_vattr, cred);
1308 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1309 aclp, p, attrbitp, exp);
1314 if (ndp->ni_startdir)
1315 vrele(ndp->ni_startdir);
1316 nfsvno_relpathbuf(ndp);
1318 if (create == NFSV4OPEN_CREATE) {
1319 if (ndp->ni_dvp == vp)
1324 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1325 if (ndp->ni_cnd.cn_flags & RDONLY)
1326 NFSVNO_SETEXRDONLY(&nes);
1328 NFSVNO_EXINIT(&nes);
1329 nd->nd_repstat = nfsvno_accchk(vp,
1330 VWRITE, cred, &nes, p,
1331 NFSACCCHK_NOOVERRIDE,
1332 NFSACCCHK_VPISLOCKED, NULL);
1333 nd->nd_repstat = nfsrv_opencheck(clientid,
1334 stateidp, stp, vp, nd, p, nd->nd_repstat);
1335 if (!nd->nd_repstat) {
1336 tempsize = nvap->na_size;
1337 NFSVNO_ATTRINIT(nvap);
1338 nvap->na_size = tempsize;
1339 nd->nd_repstat = VOP_SETATTR(vp,
1340 &nvap->na_vattr, cred);
1342 } else if (vp->v_type == VREG) {
1343 nd->nd_repstat = nfsrv_opencheck(clientid,
1344 stateidp, stp, vp, nd, p, nd->nd_repstat);
1348 if (ndp->ni_cnd.cn_flags & HASBUF)
1349 nfsvno_relpathbuf(ndp);
1350 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1351 vrele(ndp->ni_startdir);
1352 if (ndp->ni_dvp == ndp->ni_vp)
1364 * Updates the file rev and sets the mtime and ctime
1365 * to the current clock time, returning the va_filerev and va_Xtime
1369 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1370 struct ucred *cred, struct thread *p)
1375 getnanotime(&va.va_mtime);
1376 (void) VOP_SETATTR(vp, &va, cred);
1377 (void) nfsvno_getattr(vp, nvap, cred, p);
1381 * Glue routine to nfsv4_fillattr().
1384 nfsvno_fillattr(struct nfsrv_descript *nd, struct vnode *vp,
1385 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1386 struct ucred *cred, struct thread *p, int isdgram, int reterr)
1390 error = nfsv4_fillattr(nd, vp, NULL, &nvap->na_vattr, fhp, rderror,
1391 attrbitp, cred, p, isdgram, reterr);
1395 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1397 * nfs readdir service
1398 * - mallocs what it thinks is enough to read
1399 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1400 * - calls VOP_READDIR()
1401 * - loops around building the reply
1402 * if the output generated exceeds count break out of loop
1403 * The NFSM_CLGET macro is used here so that the reply will be packed
1404 * tightly in mbuf clusters.
1405 * - it trims out records with d_fileno == 0
1406 * this doesn't matter for Unix clients, but they might confuse clients
1408 * - it trims out records with d_type == DT_WHT
1409 * these cannot be seen through NFS (unless we extend the protocol)
1410 * The alternate call nfsrvd_readdirplus() does lookups as well.
1411 * PS: The NFS protocol spec. does not clarify what the "count" byte
1412 * argument is a count of.. just name strings and file id's or the
1413 * entire reply rpc or ...
1414 * I tried just file name and id sizes and it confused the Sun client,
1415 * so I am using the full rpc size now. The "paranoia.." comment refers
1416 * to including the status longwords that are not a part of the dir.
1417 * "entry" structures, but are in the rpc.
1420 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1421 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1426 char *cpos, *cend, *rbuf;
1428 int nlen, error = 0, getret = 1;
1429 int siz, cnt, fullsiz, eofflag, ncookies;
1430 u_int64_t off, toff, verf;
1431 u_long *cookies = NULL, *cookiep;
1435 if (nd->nd_repstat) {
1436 nfsrv_postopattr(nd, getret, &at);
1439 if (nd->nd_flag & ND_NFSV2) {
1440 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1441 off = fxdr_unsigned(u_quad_t, *tl++);
1443 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1444 off = fxdr_hyper(tl);
1446 verf = fxdr_hyper(tl);
1450 cnt = fxdr_unsigned(int, *tl);
1451 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1452 cnt = NFS_SRVMAXDATA(nd);
1453 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1455 if (nd->nd_flag & ND_NFSV3) {
1456 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1460 * va_filerev is not sufficient as a cookie verifier,
1461 * since it is not supposed to change when entries are
1462 * removed/added unless that offset cookies returned to
1463 * the client are no longer valid.
1465 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1466 nd->nd_repstat = NFSERR_BAD_COOKIE;
1469 if (nd->nd_repstat == 0 && cnt == 0) {
1470 if (nd->nd_flag & ND_NFSV2)
1471 /* NFSv2 does not have NFSERR_TOOSMALL */
1472 nd->nd_repstat = EPERM;
1474 nd->nd_repstat = NFSERR_TOOSMALL;
1476 if (!nd->nd_repstat)
1477 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1478 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1479 NFSACCCHK_VPISLOCKED, NULL);
1480 if (nd->nd_repstat) {
1482 if (nd->nd_flag & ND_NFSV3)
1483 nfsrv_postopattr(nd, getret, &at);
1486 NFSVOPUNLOCK(vp, 0, p);
1487 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1491 free((caddr_t)cookies, M_TEMP);
1499 io.uio_offset = (off_t)off;
1501 io.uio_segflg = UIO_SYSSPACE;
1502 io.uio_rw = UIO_READ;
1504 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
1505 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1507 NFSVOPUNLOCK(vp, 0, p);
1508 off = (u_int64_t)io.uio_offset;
1510 siz -= io.uio_resid;
1512 if (!cookies && !nd->nd_repstat)
1513 nd->nd_repstat = NFSERR_PERM;
1514 if (nd->nd_flag & ND_NFSV3) {
1515 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1516 if (!nd->nd_repstat)
1517 nd->nd_repstat = getret;
1521 * Handles the failed cases. nd->nd_repstat == 0 past here.
1523 if (nd->nd_repstat) {
1525 free((caddr_t)rbuf, M_TEMP);
1527 free((caddr_t)cookies, M_TEMP);
1528 if (nd->nd_flag & ND_NFSV3)
1529 nfsrv_postopattr(nd, getret, &at);
1533 * If nothing read, return eof
1538 if (nd->nd_flag & ND_NFSV2) {
1539 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1541 nfsrv_postopattr(nd, getret, &at);
1542 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1543 txdr_hyper(at.na_filerev, tl);
1546 *tl++ = newnfs_false;
1548 FREE((caddr_t)rbuf, M_TEMP);
1549 FREE((caddr_t)cookies, M_TEMP);
1554 * Check for degenerate cases of nothing useful read.
1555 * If so go try again
1559 dp = (struct dirent *)cpos;
1563 * For some reason FreeBSD's ufs_readdir() chooses to back the
1564 * directory offset up to a block boundary, so it is necessary to
1565 * skip over the records that precede the requested offset. This
1566 * requires the assumption that file offset cookies monotonically
1569 while (cpos < cend && ncookies > 0 &&
1570 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1571 ((u_quad_t)(*cookiep)) <= toff)) {
1572 cpos += dp->d_reclen;
1573 dp = (struct dirent *)cpos;
1577 if (cpos >= cend || ncookies == 0) {
1584 * dirlen is the size of the reply, including all XDR and must
1585 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1586 * if the XDR should be included in "count", but to be safe, we do.
1587 * (Include the two booleans at the end of the reply in dirlen now.)
1589 if (nd->nd_flag & ND_NFSV3) {
1590 nfsrv_postopattr(nd, getret, &at);
1591 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1592 txdr_hyper(at.na_filerev, tl);
1593 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1595 dirlen = 2 * NFSX_UNSIGNED;
1598 /* Loop through the records and build reply */
1599 while (cpos < cend && ncookies > 0) {
1600 nlen = dp->d_namlen;
1601 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1602 nlen <= NFS_MAXNAMLEN) {
1603 if (nd->nd_flag & ND_NFSV3)
1604 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1606 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1613 * Build the directory record xdr from
1616 if (nd->nd_flag & ND_NFSV3) {
1617 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1618 *tl++ = newnfs_true;
1621 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1622 *tl++ = newnfs_true;
1624 *tl = txdr_unsigned(dp->d_fileno);
1625 (void) nfsm_strtom(nd, dp->d_name, nlen);
1626 if (nd->nd_flag & ND_NFSV3) {
1627 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1630 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1631 *tl = txdr_unsigned(*cookiep);
1633 cpos += dp->d_reclen;
1634 dp = (struct dirent *)cpos;
1641 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1642 *tl++ = newnfs_false;
1647 FREE((caddr_t)rbuf, M_TEMP);
1648 FREE((caddr_t)cookies, M_TEMP);
1656 * Readdirplus for V3 and Readdir for V4.
1659 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1660 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1665 char *cpos, *cend, *rbuf;
1668 struct nfsvattr nva, at, *nvap = &nva;
1669 struct mbuf *mb0, *mb1;
1670 struct nfsreferral *refp;
1671 int nlen, r, error = 0, getret = 1, usevget = 1;
1672 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1673 caddr_t bpos0, bpos1;
1674 u_int64_t off, toff, verf;
1675 u_long *cookies = NULL, *cookiep;
1676 nfsattrbit_t attrbits, rderrbits, savbits;
1679 struct componentname cn;
1681 if (nd->nd_repstat) {
1682 nfsrv_postopattr(nd, getret, &at);
1685 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1686 off = fxdr_hyper(tl);
1689 verf = fxdr_hyper(tl);
1691 siz = fxdr_unsigned(int, *tl++);
1692 cnt = fxdr_unsigned(int, *tl);
1695 * Use the server's maximum data transfer size as the upper bound
1698 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1699 cnt = NFS_SRVMAXDATA(nd);
1702 * siz is a "hint" of how much directory information (name, fileid,
1703 * cookie) should be in the reply. At least one client "hints" 0,
1704 * so I set it to cnt for that case. I also round it up to the
1705 * next multiple of DIRBLKSIZ.
1709 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1711 if (nd->nd_flag & ND_NFSV4) {
1712 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1715 NFSSET_ATTRBIT(&savbits, &attrbits);
1716 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1717 NFSZERO_ATTRBIT(&rderrbits);
1718 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1720 NFSZERO_ATTRBIT(&attrbits);
1723 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1724 if (!nd->nd_repstat) {
1725 if (off && verf != at.na_filerev) {
1727 * va_filerev is not sufficient as a cookie verifier,
1728 * since it is not supposed to change when entries are
1729 * removed/added unless that offset cookies returned to
1730 * the client are no longer valid.
1733 if (nd->nd_flag & ND_NFSV4) {
1734 nd->nd_repstat = NFSERR_NOTSAME;
1736 nd->nd_repstat = NFSERR_BAD_COOKIE;
1739 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1740 nd->nd_repstat = NFSERR_BAD_COOKIE;
1743 if (!nd->nd_repstat && vp->v_type != VDIR)
1744 nd->nd_repstat = NFSERR_NOTDIR;
1745 if (!nd->nd_repstat && cnt == 0)
1746 nd->nd_repstat = NFSERR_TOOSMALL;
1747 if (!nd->nd_repstat)
1748 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1749 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1750 NFSACCCHK_VPISLOCKED, NULL);
1751 if (nd->nd_repstat) {
1753 if (nd->nd_flag & ND_NFSV3)
1754 nfsrv_postopattr(nd, getret, &at);
1758 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1762 free((caddr_t)cookies, M_TEMP);
1770 io.uio_offset = (off_t)off;
1772 io.uio_segflg = UIO_SYSSPACE;
1773 io.uio_rw = UIO_READ;
1775 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1777 off = (u_int64_t)io.uio_offset;
1779 siz -= io.uio_resid;
1781 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1783 if (!cookies && !nd->nd_repstat)
1784 nd->nd_repstat = NFSERR_PERM;
1785 if (!nd->nd_repstat)
1786 nd->nd_repstat = getret;
1787 if (nd->nd_repstat) {
1790 free((caddr_t)cookies, M_TEMP);
1791 free((caddr_t)rbuf, M_TEMP);
1792 if (nd->nd_flag & ND_NFSV3)
1793 nfsrv_postopattr(nd, getret, &at);
1797 * If nothing read, return eof
1802 if (nd->nd_flag & ND_NFSV3)
1803 nfsrv_postopattr(nd, getret, &at);
1804 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1805 txdr_hyper(at.na_filerev, tl);
1807 *tl++ = newnfs_false;
1809 free((caddr_t)cookies, M_TEMP);
1810 free((caddr_t)rbuf, M_TEMP);
1815 * Check for degenerate cases of nothing useful read.
1816 * If so go try again
1820 dp = (struct dirent *)cpos;
1824 * For some reason FreeBSD's ufs_readdir() chooses to back the
1825 * directory offset up to a block boundary, so it is necessary to
1826 * skip over the records that precede the requested offset. This
1827 * requires the assumption that file offset cookies monotonically
1830 while (cpos < cend && ncookies > 0 &&
1831 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1832 ((u_quad_t)(*cookiep)) <= toff ||
1833 ((nd->nd_flag & ND_NFSV4) &&
1834 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1835 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1836 cpos += dp->d_reclen;
1837 dp = (struct dirent *)cpos;
1841 if (cpos >= cend || ncookies == 0) {
1846 NFSVOPUNLOCK(vp, 0, p);
1849 * Save this position, in case there is an error before one entry
1853 bpos0 = nd->nd_bpos;
1856 * Fill in the first part of the reply.
1857 * dirlen is the reply length in bytes and cannot exceed cnt.
1858 * (Include the two booleans at the end of the reply in dirlen now,
1859 * so we recognize when we have exceeded cnt.)
1861 if (nd->nd_flag & ND_NFSV3) {
1862 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1863 nfsrv_postopattr(nd, getret, &at);
1865 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
1867 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1868 txdr_hyper(at.na_filerev, tl);
1871 * Save this position, in case there is an empty reply needed.
1874 bpos1 = nd->nd_bpos;
1876 /* Loop through the records and build reply */
1878 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
1879 nlen = dp->d_namlen;
1880 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1881 nlen <= NFS_MAXNAMLEN &&
1882 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
1883 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
1884 || (nlen == 1 && dp->d_name[0] != '.'))) {
1886 * Save the current position in the reply, in case
1887 * this entry exceeds cnt.
1890 bpos1 = nd->nd_bpos;
1893 * For readdir_and_lookup get the vnode using
1899 if ((nd->nd_flag & ND_NFSV3) ||
1900 NFSNONZERO_ATTRBIT(&savbits)) {
1901 if (nd->nd_flag & ND_NFSV4)
1902 refp = nfsv4root_getreferral(NULL,
1906 r = VFS_VGET(vp->v_mount,
1907 dp->d_fileno, LK_EXCLUSIVE,
1911 if (r == EOPNOTSUPP) {
1914 cn.cn_nameiop = LOOKUP;
1922 cn.cn_nameptr = dp->d_name;
1923 cn.cn_namelen = nlen;
1924 cn.cn_flags = ISLASTCN |
1925 NOFOLLOW | LOCKLEAF |
1928 dp->d_name[0] == '.' &&
1929 dp->d_name[1] == '.')
1932 if (!VOP_ISLOCKED(vp))
1936 r = VOP_LOOKUP(vp, &nvp, &cn);
1941 ((nd->nd_flag & ND_NFSV3) ||
1942 NFSNONZERO_ATTRBIT(&attrbits))) {
1943 r = nfsvno_getfh(nvp, &nfh, p);
1945 r = nfsvno_getattr(nvp, nvap,
1952 if (!NFSISSET_ATTRBIT(&attrbits,
1953 NFSATTRBIT_RDATTRERROR)) {
1963 * Build the directory record xdr
1965 if (nd->nd_flag & ND_NFSV3) {
1966 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1967 *tl++ = newnfs_true;
1969 *tl = txdr_unsigned(dp->d_fileno);
1970 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
1971 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1973 *tl = txdr_unsigned(*cookiep);
1974 nfsrv_postopattr(nd, 0, nvap);
1975 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
1976 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
1980 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1981 *tl++ = newnfs_true;
1983 *tl = txdr_unsigned(*cookiep);
1984 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
1986 NFSVOPUNLOCK(nvp, 0, p);
1988 dirlen += nfsrv_putreferralattr(nd,
1991 if (nd->nd_repstat) {
1997 dirlen += nfsvno_fillattr(nd, nvp, nvap,
1998 &nfh, r, &rderrbits, nd->nd_cred,
2001 dirlen += nfsvno_fillattr(nd, nvp, nvap,
2002 &nfh, r, &attrbits, nd->nd_cred,
2007 dirlen += (3 * NFSX_UNSIGNED);
2012 cpos += dp->d_reclen;
2013 dp = (struct dirent *)cpos;
2017 if (!usevget && VOP_ISLOCKED(vp))
2023 * If dirlen > cnt, we must strip off the last entry. If that
2024 * results in an empty reply, report NFSERR_TOOSMALL.
2026 if (dirlen > cnt || nd->nd_repstat) {
2027 if (!nd->nd_repstat && entrycnt == 0)
2028 nd->nd_repstat = NFSERR_TOOSMALL;
2030 newnfs_trimtrailing(nd, mb0, bpos0);
2032 newnfs_trimtrailing(nd, mb1, bpos1);
2034 } else if (cpos < cend)
2036 if (!nd->nd_repstat) {
2037 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2038 *tl++ = newnfs_false;
2044 FREE((caddr_t)cookies, M_TEMP);
2045 FREE((caddr_t)rbuf, M_TEMP);
2053 * Get the settable attributes out of the mbuf list.
2054 * (Return 0 or EBADRPC)
2057 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2058 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2061 struct nfsv2_sattr *sp;
2062 struct timeval curtime;
2063 int error = 0, toclient = 0;
2065 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2067 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2069 * Some old clients didn't fill in the high order 16bits.
2070 * --> check the low order 2 bytes for 0xffff
2072 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2073 nvap->na_mode = nfstov_mode(sp->sa_mode);
2074 if (sp->sa_uid != newnfs_xdrneg1)
2075 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2076 if (sp->sa_gid != newnfs_xdrneg1)
2077 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2078 if (sp->sa_size != newnfs_xdrneg1)
2079 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2080 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2082 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2084 nvap->na_atime.tv_sec =
2085 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2086 nvap->na_atime.tv_nsec = 0;
2089 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2090 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2093 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2094 if (*tl == newnfs_true) {
2095 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2096 nvap->na_mode = nfstov_mode(*tl);
2098 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2099 if (*tl == newnfs_true) {
2100 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2101 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2103 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2104 if (*tl == newnfs_true) {
2105 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2106 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2108 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2109 if (*tl == newnfs_true) {
2110 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2111 nvap->na_size = fxdr_hyper(tl);
2113 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2114 switch (fxdr_unsigned(int, *tl)) {
2115 case NFSV3SATTRTIME_TOCLIENT:
2116 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2117 fxdr_nfsv3time(tl, &nvap->na_atime);
2120 case NFSV3SATTRTIME_TOSERVER:
2121 NFSGETTIME(&curtime);
2122 nvap->na_atime.tv_sec = curtime.tv_sec;
2123 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2124 nvap->na_vaflags |= VA_UTIMES_NULL;
2127 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2128 switch (fxdr_unsigned(int, *tl)) {
2129 case NFSV3SATTRTIME_TOCLIENT:
2130 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2131 fxdr_nfsv3time(tl, &nvap->na_mtime);
2132 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2134 case NFSV3SATTRTIME_TOSERVER:
2135 NFSGETTIME(&curtime);
2136 nvap->na_mtime.tv_sec = curtime.tv_sec;
2137 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2139 nvap->na_vaflags |= VA_UTIMES_NULL;
2144 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2151 * Handle the setable attributes for V4.
2152 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2155 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2156 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2161 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2163 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2166 struct timeval curtime;
2168 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2171 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2172 attrsize = fxdr_unsigned(int, *tl);
2175 * Loop around getting the setable attributes. If an unsupported
2176 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2179 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2180 bitpos = NFSATTRBIT_MAX;
2184 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2185 if (attrsum > attrsize) {
2186 error = NFSERR_BADXDR;
2189 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2191 case NFSATTRBIT_SIZE:
2192 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2193 nvap->na_size = fxdr_hyper(tl);
2194 attrsum += NFSX_HYPER;
2196 case NFSATTRBIT_ACL:
2197 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2201 if (aceerr && !nd->nd_repstat)
2202 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2205 case NFSATTRBIT_ARCHIVE:
2206 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2207 if (!nd->nd_repstat)
2208 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2209 attrsum += NFSX_UNSIGNED;
2211 case NFSATTRBIT_HIDDEN:
2212 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2213 if (!nd->nd_repstat)
2214 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2215 attrsum += NFSX_UNSIGNED;
2217 case NFSATTRBIT_MIMETYPE:
2218 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2219 i = fxdr_unsigned(int, *tl);
2220 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2223 if (!nd->nd_repstat)
2224 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2225 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2227 case NFSATTRBIT_MODE:
2228 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2229 nvap->na_mode = nfstov_mode(*tl);
2230 attrsum += NFSX_UNSIGNED;
2232 case NFSATTRBIT_OWNER:
2233 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2234 j = fxdr_unsigned(int, *tl);
2236 return (NFSERR_BADXDR);
2237 if (j > NFSV4_SMALLSTR)
2238 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2241 error = nfsrv_mtostr(nd, cp, j);
2243 if (j > NFSV4_SMALLSTR)
2244 free(cp, M_NFSSTRING);
2247 if (!nd->nd_repstat) {
2248 nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p);
2249 if (!nd->nd_repstat)
2252 if (j > NFSV4_SMALLSTR)
2253 free(cp, M_NFSSTRING);
2254 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2256 case NFSATTRBIT_OWNERGROUP:
2257 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2258 j = fxdr_unsigned(int, *tl);
2260 return (NFSERR_BADXDR);
2261 if (j > NFSV4_SMALLSTR)
2262 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2265 error = nfsrv_mtostr(nd, cp, j);
2267 if (j > NFSV4_SMALLSTR)
2268 free(cp, M_NFSSTRING);
2271 if (!nd->nd_repstat) {
2272 nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p);
2273 if (!nd->nd_repstat)
2276 if (j > NFSV4_SMALLSTR)
2277 free(cp, M_NFSSTRING);
2278 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2280 case NFSATTRBIT_SYSTEM:
2281 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2282 if (!nd->nd_repstat)
2283 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2284 attrsum += NFSX_UNSIGNED;
2286 case NFSATTRBIT_TIMEACCESSSET:
2287 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2288 attrsum += NFSX_UNSIGNED;
2289 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2290 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2291 fxdr_nfsv4time(tl, &nvap->na_atime);
2293 attrsum += NFSX_V4TIME;
2295 NFSGETTIME(&curtime);
2296 nvap->na_atime.tv_sec = curtime.tv_sec;
2297 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2298 nvap->na_vaflags |= VA_UTIMES_NULL;
2301 case NFSATTRBIT_TIMEBACKUP:
2302 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2303 if (!nd->nd_repstat)
2304 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2305 attrsum += NFSX_V4TIME;
2307 case NFSATTRBIT_TIMECREATE:
2308 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2309 if (!nd->nd_repstat)
2310 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2311 attrsum += NFSX_V4TIME;
2313 case NFSATTRBIT_TIMEMODIFYSET:
2314 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2315 attrsum += NFSX_UNSIGNED;
2316 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2317 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2318 fxdr_nfsv4time(tl, &nvap->na_mtime);
2319 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2320 attrsum += NFSX_V4TIME;
2322 NFSGETTIME(&curtime);
2323 nvap->na_mtime.tv_sec = curtime.tv_sec;
2324 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2326 nvap->na_vaflags |= VA_UTIMES_NULL;
2330 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2332 * set bitpos so we drop out of the loop.
2334 bitpos = NFSATTRBIT_MAX;
2340 * some clients pad the attrlist, so we need to skip over the
2343 if (attrsum > attrsize) {
2344 error = NFSERR_BADXDR;
2346 attrsize = NFSM_RNDUP(attrsize);
2347 if (attrsum < attrsize)
2348 error = nfsm_advance(nd, attrsize - attrsum, -1);
2355 * Check/setup export credentials.
2358 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2359 struct ucred *credanon)
2364 * Check/setup credentials.
2366 if (nd->nd_flag & ND_GSS)
2367 exp->nes_exflag &= ~MNT_EXPORTANON;
2370 * Check to see if the operation is allowed for this security flavor.
2371 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2372 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2373 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2375 if (nfsvno_testexp(nd, exp) &&
2376 nd->nd_procnum != NFSV4OP_SECINFO &&
2377 nd->nd_procnum != NFSPROC_FSINFO) {
2378 if (nd->nd_flag & ND_NFSV4)
2379 error = NFSERR_WRONGSEC;
2381 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2386 * Check to see if the file system is exported V4 only.
2388 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4))
2389 return (NFSERR_PROGNOTV4);
2392 * Now, map the user credentials.
2393 * (Note that ND_AUTHNONE will only be set for an NFSv3
2394 * Fsinfo RPC. If set for anything else, this code might need
2397 if (NFSVNO_EXPORTED(exp) &&
2398 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2399 NFSVNO_EXPORTANON(exp) ||
2400 (nd->nd_flag & ND_AUTHNONE))) {
2401 nd->nd_cred->cr_uid = credanon->cr_uid;
2402 nd->nd_cred->cr_gid = credanon->cr_gid;
2403 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2404 credanon->cr_groups);
2413 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2414 struct ucred **credp)
2416 int i, error, *secflavors;
2418 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2419 &exp->nes_numsecflavor, &secflavors);
2421 if (nfs_rootfhset) {
2422 exp->nes_exflag = 0;
2423 exp->nes_numsecflavor = 0;
2427 /* Copy the security flavors. */
2428 for (i = 0; i < exp->nes_numsecflavor; i++)
2429 exp->nes_secflavors[i] = secflavors[i];
2435 * Get a vnode for a file handle and export stuff.
2438 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2439 struct vnode **vpp, struct nfsexstuff *exp, struct ucred **credp)
2441 int i, error, *secflavors;
2444 exp->nes_numsecflavor = 0;
2445 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2447 /* Make sure the server replies ESTALE to the client. */
2449 if (nam && !error) {
2450 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2451 &exp->nes_numsecflavor, &secflavors);
2453 if (nfs_rootfhset) {
2454 exp->nes_exflag = 0;
2455 exp->nes_numsecflavor = 0;
2461 /* Copy the security flavors. */
2462 for (i = 0; i < exp->nes_numsecflavor; i++)
2463 exp->nes_secflavors[i] = secflavors[i];
2470 * Do the pathconf vnode op.
2473 nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
2474 struct ucred *cred, struct thread *p)
2478 error = VOP_PATHCONF(vp, flag, retf);
2483 * nfsd_fhtovp() - convert a fh to a vnode ptr
2484 * - look up fsid in mount list (if not found ret error)
2485 * - get vp and export rights by calling nfsvno_fhtovp()
2486 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2488 * Also handle getting the Giant lock for the file system,
2490 * - if same mount point as *mpp
2492 * else if *mpp == NULL
2496 * call VFS_LOCK_GIANT()
2500 * call VFS_LOCK_GIANT()
2503 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp,
2504 struct vnode **vpp, struct nfsexstuff *exp,
2505 struct mount **mpp, int startwrite, struct thread *p)
2508 struct ucred *credanon;
2511 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2513 * Check for the special case of the nfsv4root_fh.
2515 mp = vfs_getvfs(&fhp->fh_fsid);
2518 nd->nd_repstat = ESTALE;
2519 if (*mpp && exp->nes_vfslocked)
2520 VFS_UNLOCK_GIANT(*mpp);
2522 exp->nes_vfslocked = 0;
2527 * Now, handle Giant for the file system.
2529 if (*mpp != NULL && *mpp != mp && exp->nes_vfslocked) {
2530 VFS_UNLOCK_GIANT(*mpp);
2531 exp->nes_vfslocked = 0;
2533 if (!exp->nes_vfslocked && *mpp != mp)
2534 exp->nes_vfslocked = VFS_LOCK_GIANT(mp);
2538 vn_start_write(NULL, mpp, V_WAIT);
2540 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, vpp, exp,
2544 * For NFSv4 without a pseudo root fs, unexported file handles
2545 * can be returned, so that Lookup works everywhere.
2547 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2548 !(nd->nd_flag & ND_NFSV4)) {
2550 nd->nd_repstat = EACCES;
2554 * Personally, I've never seen any point in requiring a
2555 * reserved port#, since only in the rare case where the
2556 * clients are all boxes with secure system priviledges,
2557 * does it provide any enhanced security, but... some people
2558 * believe it to be useful and keep putting this code back in.
2559 * (There is also some "security checker" out there that
2560 * complains if the nfs server doesn't enforce this.)
2561 * However, note the following:
2562 * RFC3530 (NFSv4) specifies that a reserved port# not be
2564 * RFC2623 recommends that, if a reserved port# is checked for,
2565 * that there be a way to turn that off--> ifdef'd.
2567 #ifdef NFS_REQRSVPORT
2568 if (!nd->nd_repstat) {
2569 struct sockaddr_in *saddr;
2570 struct sockaddr_in6 *saddr6;
2572 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2573 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2574 if (!(nd->nd_flag & ND_NFSV4) &&
2575 ((saddr->sin_family == AF_INET &&
2576 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2577 (saddr6->sin6_family == AF_INET6 &&
2578 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2580 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2583 #endif /* NFS_REQRSVPORT */
2586 * Check/setup credentials.
2588 if (!nd->nd_repstat) {
2589 nd->nd_saveduid = nd->nd_cred->cr_uid;
2590 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2594 if (credanon != NULL)
2596 if (nd->nd_repstat) {
2598 vn_finished_write(mp);
2599 if (exp->nes_vfslocked) {
2600 VFS_UNLOCK_GIANT(mp);
2601 exp->nes_vfslocked = 0;
2615 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2617 struct filedesc *fdp;
2620 fdp = p->td_proc->p_fd;
2621 if (fd >= fdp->fd_nfiles ||
2622 (fp = fdp->fd_ofiles[fd]) == NULL)
2629 * Called from nfssvc() to update the exports list. Just call
2630 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2631 * in the mount list.
2634 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2636 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2638 struct nameidata nd;
2641 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2642 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT)) {
2645 } else if (error == 0) {
2646 if (nfsexargp->fspec == NULL)
2649 * If fspec != NULL, this is the v4root path.
2651 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2652 nfsexargp->fspec, p);
2653 if ((error = namei(&nd)) != 0)
2655 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2658 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2659 NFSBCOPY((caddr_t)&fh,
2660 nfs_rootfh.nfsrvfh_data,
2661 sizeof (fhandle_t));
2669 * Get the tcp socket sequence numbers we need.
2670 * (Maybe this should be moved to the tcp sources?)
2673 nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2678 inp = sotoinpcb(so);
2679 KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2681 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2685 tp = intotcpcb(inp);
2686 if (tp->t_state != TCPS_ESTABLISHED) {
2690 *maxp = tp->snd_max;
2691 *unap = tp->snd_una;
2697 * This function needs to test to see if the system is near its limit
2698 * for memory allocation via malloc() or mget() and return True iff
2699 * either of these resources are near their limit.
2700 * XXX (For now, this is just a stub.)
2702 int nfsrv_testmalloclimit = 0;
2704 nfsrv_mallocmget_limit(void)
2706 static int printmesg = 0;
2707 static int testval = 1;
2709 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2710 if ((printmesg++ % 100) == 0)
2711 printf("nfsd: malloc/mget near limit\n");
2718 * BSD specific initialization of a mount point.
2723 static int inited = 0;
2728 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2729 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2730 nfsv4root_mnt.mnt_export = NULL;
2731 TAILQ_INIT(&nfsv4root_opt);
2732 TAILQ_INIT(&nfsv4root_newopt);
2733 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2734 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2735 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2739 * Get a vnode for a file handle, without checking exports, etc.
2742 nfsvno_getvp(fhandle_t *fhp)
2748 mp = vfs_getvfs(&fhp->fh_fsid);
2751 error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp);
2758 * Check to see it a byte range lock held by a process running
2759 * locally on the server conflicts with the new lock.
2762 nfsvno_localconflict(struct vnode *vp, int ftype, u_int64_t first,
2763 u_int64_t end, struct nfslockconflict *cfp, struct thread *td)
2768 if (!nfsrv_dolocallocks)
2770 fl.l_whence = SEEK_SET;
2772 fl.l_start = (off_t)first;
2773 if (end == NFS64BITSSET)
2776 fl.l_len = (off_t)(end - first);
2778 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2779 * values for all calls, so that all locks will be held by the
2780 * nfsd server. (The nfsd server handles conflicts between the
2782 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2783 * bytes, so it can't be put in l_sysid.
2785 if (nfsv4_sysid == 0)
2786 nfsv4_sysid = nlm_acquire_next_sysid();
2787 fl.l_pid = (pid_t)0;
2788 fl.l_sysid = (int)nfsv4_sysid;
2790 NFSVOPUNLOCK(vp, 0, td);
2791 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_GETLK, &fl,
2792 (F_POSIX | F_REMOTE));
2793 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2796 if (fl.l_type == F_UNLCK)
2799 cfp->cl_clientid.lval[0] = cfp->cl_clientid.lval[1] = 0;
2800 cfp->cl_first = (u_int64_t)fl.l_start;
2802 cfp->cl_end = NFS64BITSSET;
2804 cfp->cl_end = (u_int64_t)
2805 (fl.l_start + fl.l_len);
2806 if (fl.l_type == F_WRLCK)
2807 cfp->cl_flags = NFSLCK_WRITE;
2809 cfp->cl_flags = NFSLCK_READ;
2810 sprintf(cfp->cl_owner, "LOCALID%d", fl.l_pid);
2811 cfp->cl_ownerlen = strlen(cfp->cl_owner);
2812 return (NFSERR_DENIED);
2814 return (NFSERR_INVAL);
2818 * Do a local VOP_ADVLOCK().
2821 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2822 u_int64_t end, struct thread *td)
2828 if (!nfsrv_dolocallocks)
2830 fl.l_whence = SEEK_SET;
2832 fl.l_start = (off_t)first;
2833 if (end == NFS64BITSSET) {
2837 fl.l_len = (off_t)tlen;
2840 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2841 * values for all calls, so that all locks will be held by the
2842 * nfsd server. (The nfsd server handles conflicts between the
2844 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2845 * bytes, so it can't be put in l_sysid.
2847 if (nfsv4_sysid == 0)
2848 nfsv4_sysid = nlm_acquire_next_sysid();
2849 fl.l_pid = (pid_t)0;
2850 fl.l_sysid = (int)nfsv4_sysid;
2852 NFSVOPUNLOCK(vp, 0, td);
2853 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2854 (F_POSIX | F_REMOTE));
2855 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2860 * Unlock an underlying local file system.
2863 nfsvno_unlockvfs(struct mount *mp)
2866 VFS_UNLOCK_GIANT(mp);
2870 * Lock an underlying file system, as required, and return
2871 * whether or not it is locked.
2874 nfsvno_lockvfs(struct mount *mp)
2878 ret = VFS_LOCK_GIANT(mp);
2883 * Check the nfsv4 root exports.
2886 nfsvno_v4rootexport(struct nfsrv_descript *nd)
2888 struct ucred *credanon;
2889 int exflags, error, numsecflavor, *secflavors, i;
2891 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
2892 &credanon, &numsecflavor, &secflavors);
2894 return (NFSERR_PROGUNAVAIL);
2895 if (credanon != NULL)
2897 for (i = 0; i < numsecflavor; i++) {
2898 if (secflavors[i] == AUTH_SYS)
2899 nd->nd_flag |= ND_EXAUTHSYS;
2900 else if (secflavors[i] == RPCSEC_GSS_KRB5)
2901 nd->nd_flag |= ND_EXGSS;
2902 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
2903 nd->nd_flag |= ND_EXGSSINTEGRITY;
2904 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
2905 nd->nd_flag |= ND_EXGSSPRIVACY;
2911 * Nfs server psuedo system call for the nfsd's
2917 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
2920 struct nfsd_addsock_args sockarg;
2921 struct nfsd_nfsd_args nfsdarg;
2924 if (uap->flag & NFSSVC_NFSDADDSOCK) {
2925 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
2928 if ((error = fget(td, sockarg.sock, &fp)) != 0) {
2931 if (fp->f_type != DTYPE_SOCKET) {
2935 error = nfsrvd_addsock(fp);
2937 } else if (uap->flag & NFSSVC_NFSDNFSD) {
2938 if (uap->argp == NULL)
2940 error = copyin(uap->argp, (caddr_t)&nfsdarg,
2944 error = nfsrvd_nfsd(td, &nfsdarg);
2946 error = nfssvc_srvcall(td, uap, td->td_ucred);
2952 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
2954 struct nfsex_args export;
2955 struct file *fp = NULL;
2957 struct nfsd_clid adminrevoke;
2958 struct nfsd_dumplist dumplist;
2959 struct nfsd_dumpclients *dumpclients;
2960 struct nfsd_dumplocklist dumplocklist;
2961 struct nfsd_dumplocks *dumplocks;
2962 struct nameidata nd;
2966 if (uap->flag & NFSSVC_PUBLICFH) {
2967 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
2968 sizeof (fhandle_t));
2969 error = copyin(uap->argp,
2970 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
2973 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
2974 error = copyin(uap->argp,(caddr_t)&export,
2975 sizeof (struct nfsex_args));
2977 error = nfsrv_v4rootexport(&export, cred, p);
2978 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
2981 } else if (uap->flag & NFSSVC_STABLERESTART) {
2982 error = copyin(uap->argp, (caddr_t)&stablefd,
2985 error = fp_getfvp(p, stablefd, &fp, &vp);
2986 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
2988 if (!error && newnfs_numnfsd != 0)
2991 nfsrv_stablefirst.nsf_fp = fp;
2992 nfsrv_setupstable(p);
2994 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
2995 error = copyin(uap->argp, (caddr_t)&adminrevoke,
2996 sizeof (struct nfsd_clid));
2998 error = nfsrv_adminrevoke(&adminrevoke, p);
2999 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3000 error = copyin(uap->argp, (caddr_t)&dumplist,
3001 sizeof (struct nfsd_dumplist));
3002 if (!error && (dumplist.ndl_size < 1 ||
3003 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3006 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3007 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3009 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3010 error = copyout(dumpclients,
3011 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3012 free((caddr_t)dumpclients, M_TEMP);
3014 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3015 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3016 sizeof (struct nfsd_dumplocklist));
3017 if (!error && (dumplocklist.ndllck_size < 1 ||
3018 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3021 error = nfsrv_lookupfilename(&nd,
3022 dumplocklist.ndllck_fname, p);
3024 len = sizeof (struct nfsd_dumplocks) *
3025 dumplocklist.ndllck_size;
3026 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3028 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3029 dumplocklist.ndllck_size, p);
3031 error = copyout(dumplocks,
3032 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3033 free((caddr_t)dumplocks, M_TEMP);
3041 * Returns 0 if ok, 1 otherwise.
3044 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3049 * This seems odd, but allow the case where the security flavor
3050 * list is empty. This happens when NFSv4 is traversing non-exported
3051 * file systems. Exported file systems should always have a non-empty
3052 * security flavor list.
3054 if (exp->nes_numsecflavor == 0)
3057 for (i = 0; i < exp->nes_numsecflavor; i++) {
3059 * The tests for privacy and integrity must be first,
3060 * since ND_GSS is set for everything but AUTH_SYS.
3062 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3063 (nd->nd_flag & ND_GSSPRIVACY))
3065 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3066 (nd->nd_flag & ND_GSSINTEGRITY))
3068 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3069 (nd->nd_flag & ND_GSS))
3071 if (exp->nes_secflavors[i] == AUTH_SYS &&
3072 (nd->nd_flag & ND_GSS) == 0)
3078 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3081 * Called once to initialize data structures...
3084 nfsd_modevent(module_t mod, int type, void *data)
3087 static int loaded = 0;
3094 mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF);
3095 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3096 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3098 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3105 #ifdef VV_DISABLEDELEG
3106 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3107 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3109 nfsd_call_servertimer = nfsrv_servertimer;
3110 nfsd_call_nfsd = nfssvc_nfsd;
3115 if (newnfs_numnfsd != 0) {
3120 #ifdef VV_DISABLEDELEG
3121 vn_deleg_ops.vndeleg_recall = NULL;
3122 vn_deleg_ops.vndeleg_disable = NULL;
3124 nfsd_call_servertimer = NULL;
3125 nfsd_call_nfsd = NULL;
3126 /* and get rid of the locks */
3127 mtx_destroy(&nfs_cache_mutex);
3128 mtx_destroy(&nfs_v4root_mutex);
3129 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3130 lockdestroy(&nfsv4root_mnt.mnt_explock);
3139 static moduledata_t nfsd_mod = {
3144 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3146 /* So that loader and kldload(2) can find us, wherever we are.. */
3147 MODULE_VERSION(nfsd, 1);
3148 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3149 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);