2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Functions that perform the vfs operations required by the routines in
39 * nfsd_serv.c. It is hoped that this change will make the server more
43 #include <fs/nfs/nfsport.h>
45 #include <sys/sysctl.h>
46 #include <nlm/nlm_prot.h>
49 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
50 extern int nfsv4root_set;
51 extern int nfsrv_useacl;
52 extern int newnfs_numnfsd;
53 extern struct mount nfsv4root_mnt;
54 extern struct nfsrv_stablefirst nfsrv_stablefirst;
55 extern void (*nfsd_call_servertimer)(void);
56 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
58 struct mtx nfs_cache_mutex;
59 struct mtx nfs_v4root_mutex;
60 struct nfsrvfh nfs_rootfh, nfs_pubfh;
61 int nfs_pubfhset = 0, nfs_rootfhset = 0;
62 static uint32_t nfsv4_sysid = 0;
64 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
67 static int enable_crossmntpt = 1;
68 static int nfs_commit_blks;
69 static int nfs_commit_miss;
70 extern int nfsrv_issuedelegs;
71 extern int nfsrv_dolocallocks;
73 SYSCTL_DECL(_vfs_newnfs);
74 SYSCTL_INT(_vfs_newnfs, OID_AUTO, mirrormnt, CTLFLAG_RW, &enable_crossmntpt,
75 0, "Enable nfsd to cross mount points");
76 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
78 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
80 SYSCTL_INT(_vfs_newnfs, OID_AUTO, issue_delegations, CTLFLAG_RW,
81 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
82 SYSCTL_INT(_vfs_newnfs, OID_AUTO, enable_locallocks, CTLFLAG_RW,
83 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
85 #define NUM_HEURISTIC 1017
88 #define NHUSE_MAX 2048
90 static struct nfsheur {
91 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
92 off_t nh_nextr; /* next offset for sequential detection */
93 int nh_use; /* use count for selection */
94 int nh_seqcount; /* heuristic */
95 } nfsheur[NUM_HEURISTIC];
99 * Get attributes into nfsvattr structure.
102 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
105 int error, lockedit = 0;
107 /* Since FreeBSD insists the vnode be locked... */
108 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
110 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
112 error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
114 NFSVOPUNLOCK(vp, 0, p);
119 * Get a file handle for a vnode.
122 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
126 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
127 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
128 error = VOP_VPTOFH(vp, &fhp->fh_fid);
133 * Perform access checking for vnodes obtained from file handles that would
134 * refer to files already opened by a Unix client. You cannot just use
135 * vn_writechk() and VOP_ACCESSX() for two reasons.
136 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
138 * 2 - The owner is to be given access irrespective of mode bits for some
139 * operations, so that processes that chmod after opening a file don't
143 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
144 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
145 u_int32_t *supportedtypep)
148 int error = 0, getret = 0;
150 if (accmode & VWRITE) {
151 /* Just vn_writechk() changed to check rdonly */
153 * Disallow write attempts on read-only file systems;
154 * unless the file is a socket or a block or character
155 * device resident on the file system.
157 if (NFSVNO_EXRDONLY(exp) ||
158 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
159 switch (vp->v_type) {
169 * If there's shared text associated with
170 * the inode, try to free it up once. If
171 * we fail, we can't allow writing.
173 if (vp->v_vflag & VV_TEXT)
177 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
180 * Should the override still be applied when ACLs are enabled?
182 error = VOP_ACCESSX(vp, accmode, cred, p);
183 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
185 * Try again with VEXPLICIT_DENY, to see if the test for
186 * deletion is supported.
188 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
190 if (vp->v_type == VDIR) {
191 accmode &= ~(VDELETE | VDELETE_CHILD);
193 error = VOP_ACCESSX(vp, accmode, cred, p);
194 } else if (supportedtypep != NULL) {
195 *supportedtypep &= ~NFSACCESS_DELETE;
201 * Allow certain operations for the owner (reads and writes
202 * on files that are already open).
204 if (override != NFSACCCHK_NOOVERRIDE &&
205 (error == EPERM || error == EACCES)) {
206 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
208 else if (override & NFSACCCHK_ALLOWOWNER) {
209 getret = VOP_GETATTR(vp, &vattr, cred);
210 if (getret == 0 && cred->cr_uid == vattr.va_uid)
215 NFSVOPUNLOCK(vp, 0, p);
220 * Set attribute(s) vnop.
223 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
224 struct thread *p, struct nfsexstuff *exp)
228 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
233 * Set up nameidata for a lookup() call and do it
234 * For the cases where we are crossing mount points
235 * (looking up the public fh path or the v4 root path when
236 * not using a pseudo-root fs), set/release the Giant lock,
240 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
241 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
242 struct vnode **retdirp)
244 struct componentname *cnp = &ndp->ni_cnd;
248 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
249 int error = 0, crossmnt;
253 cnp->cn_nameptr = cnp->cn_pnbuf;
255 * Extract and set starting directory.
257 if (dp->v_type != VDIR) {
262 nfsvno_relpathbuf(ndp);
266 NFSVOPUNLOCK(dp, 0, p);
269 if (NFSVNO_EXRDONLY(exp))
270 cnp->cn_flags |= RDONLY;
271 ndp->ni_segflg = UIO_SYSSPACE;
274 if (nd->nd_flag & ND_PUBLOOKUP) {
276 if (cnp->cn_pnbuf[0] == '/') {
279 * Check for degenerate pathnames here, since lookup()
282 for (i = 1; i < ndp->ni_pathlen; i++)
283 if (cnp->cn_pnbuf[i] != '/')
285 if (i == ndp->ni_pathlen) {
286 error = NFSERR_ACCES;
292 } else if ((enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
293 (nd->nd_flag & ND_NFSV4) == 0) {
295 * Only cross mount points for NFSv4 when doing a
296 * mount while traversing the file system above
297 * the mount point, unless enable_crossmntpt is set.
299 cnp->cn_flags |= NOCROSSMOUNT;
304 * Initialize for scan, set ni_startdir and bump ref on dp again
305 * becuase lookup() will dereference ni_startdir.
309 ndp->ni_startdir = dp;
310 ndp->ni_rootdir = rootvnode;
313 cnp->cn_flags |= LOCKLEAF;
315 cnp->cn_nameptr = cnp->cn_pnbuf;
317 * Call lookup() to do the real work. If an error occurs,
318 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
319 * we do not have to dereference anything before returning.
320 * In either case ni_startdir will be dereferenced and NULLed
323 if (exp->nes_vfslocked)
324 ndp->ni_cnd.cn_flags |= GIANTHELD;
327 * The Giant lock should only change when
328 * crossing mount points.
332 (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
333 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
339 * Check for encountering a symbolic link. Trivial
340 * termination occurs if no symlink encountered.
342 if ((cnp->cn_flags & ISSYMLINK) == 0) {
343 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
344 nfsvno_relpathbuf(ndp);
345 if (ndp->ni_vp && !lockleaf)
346 NFSVOPUNLOCK(ndp->ni_vp, 0, p);
353 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
354 NFSVOPUNLOCK(ndp->ni_dvp, 0, p);
355 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
360 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
364 if (ndp->ni_pathlen > 1)
365 cp = uma_zalloc(namei_zone, M_WAITOK);
369 aiov.iov_len = MAXPATHLEN;
370 auio.uio_iov = &aiov;
373 auio.uio_rw = UIO_READ;
374 auio.uio_segflg = UIO_SYSSPACE;
376 auio.uio_resid = MAXPATHLEN;
377 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
380 if (ndp->ni_pathlen > 1)
381 uma_zfree(namei_zone, cp);
387 linklen = MAXPATHLEN - auio.uio_resid;
392 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
393 error = ENAMETOOLONG;
398 * Adjust or replace path
400 if (ndp->ni_pathlen > 1) {
401 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
402 uma_zfree(namei_zone, cnp->cn_pnbuf);
405 cnp->cn_pnbuf[linklen] = '\0';
406 ndp->ni_pathlen += linklen;
409 * Cleanup refs for next loop and check if root directory
410 * should replace current directory. Normally ni_dvp
411 * becomes the new base directory and is cleaned up when
412 * we loop. Explicitly null pointers after invalidation
413 * to clarify operation.
418 if (cnp->cn_pnbuf[0] == '/') {
420 ndp->ni_dvp = ndp->ni_rootdir;
423 ndp->ni_startdir = ndp->ni_dvp;
427 cnp->cn_flags &= ~LOCKLEAF;
431 uma_zfree(namei_zone, cnp->cn_pnbuf);
434 ndp->ni_startdir = NULL;
435 cnp->cn_flags &= ~HASBUF;
436 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
443 * Set up a pathname buffer and return a pointer to it and, optionally
444 * set a hash pointer.
447 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
449 struct componentname *cnp = &ndp->ni_cnd;
451 cnp->cn_flags |= (NOMACCHECK | HASBUF);
452 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
455 *bufpp = cnp->cn_pnbuf;
459 * Release the above path buffer, if not released by nfsvno_namei().
462 nfsvno_relpathbuf(struct nameidata *ndp)
465 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
467 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
468 ndp->ni_cnd.cn_flags &= ~HASBUF;
472 * Readlink vnode op into an mbuf list.
475 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
476 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
478 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
479 struct iovec *ivp = iv;
480 struct uio io, *uiop = &io;
481 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
482 int i, len, tlen, error;
486 while (len < NFS_MAXPATHLEN) {
489 mp->m_len = NFSMSIZ(mp);
496 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
497 mp->m_len = NFS_MAXPATHLEN - len;
498 len = NFS_MAXPATHLEN;
502 ivp->iov_base = mtod(mp, caddr_t);
503 ivp->iov_len = mp->m_len;
508 uiop->uio_iovcnt = i;
509 uiop->uio_offset = 0;
510 uiop->uio_resid = len;
511 uiop->uio_rw = UIO_READ;
512 uiop->uio_segflg = UIO_SYSSPACE;
514 error = VOP_READLINK(vp, uiop, cred);
520 if (uiop->uio_resid > 0) {
521 len -= uiop->uio_resid;
522 tlen = NFSM_RNDUP(len);
523 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
532 * Read vnode op call into mbuf list.
535 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
536 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
542 int error = 0, len, left, siz, tlen, ioflag = 0, hi, try = 32;
543 struct mbuf *m2 = NULL, *m3;
544 struct uio io, *uiop = &io;
548 * Calculate seqcount for heuristic
551 * Locate best candidate
554 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
558 if (nfsheur[hi].nh_vp == vp) {
562 if (nfsheur[hi].nh_use > 0)
563 --nfsheur[hi].nh_use;
564 hi = (hi + 1) % NUM_HEURISTIC;
565 if (nfsheur[hi].nh_use < nh->nh_use)
569 if (nh->nh_vp != vp) {
572 nh->nh_use = NHUSE_INIT;
580 * Calculate heuristic
583 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
584 if (++nh->nh_seqcount > IO_SEQMAX)
585 nh->nh_seqcount = IO_SEQMAX;
586 } else if (nh->nh_seqcount > 1) {
591 nh->nh_use += NHUSE_INC;
592 if (nh->nh_use > NHUSE_MAX)
593 nh->nh_use = NHUSE_MAX;
594 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
596 len = left = NFSM_RNDUP(cnt);
599 * Generate the mbuf list with the uio_iov ref. to it.
606 siz = min(M_TRAILINGSPACE(m), left);
615 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
617 uiop->uio_iov = iv2 = iv;
623 panic("nfsvno_read iov");
624 siz = min(M_TRAILINGSPACE(m), left);
626 iv->iov_base = mtod(m, caddr_t) + m->m_len;
635 uiop->uio_iovcnt = i;
636 uiop->uio_offset = off;
637 uiop->uio_resid = len;
638 uiop->uio_rw = UIO_READ;
639 uiop->uio_segflg = UIO_SYSSPACE;
640 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
641 FREE((caddr_t)iv2, M_TEMP);
647 tlen = len - uiop->uio_resid;
648 cnt = cnt < tlen ? cnt : tlen;
649 tlen = NFSM_RNDUP(cnt);
653 } else if (len != tlen || tlen != cnt)
654 nfsrv_adj(m3, len - tlen, tlen - cnt);
661 * Write vnode op from an mbuf list.
664 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
665 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
671 struct uio io, *uiop = &io;
673 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
675 uiop->uio_iov = iv = ivp;
676 uiop->uio_iovcnt = cnt;
677 i = mtod(mp, caddr_t) + mp->m_len - cp;
681 panic("nfsvno_write");
692 cp = mtod(mp, caddr_t);
696 if (stable == NFSWRITE_UNSTABLE)
697 ioflags = IO_NODELOCKED;
699 ioflags = (IO_SYNC | IO_NODELOCKED);
700 uiop->uio_resid = retlen;
701 uiop->uio_rw = UIO_WRITE;
702 uiop->uio_segflg = UIO_SYSSPACE;
704 uiop->uio_offset = off;
705 error = VOP_WRITE(vp, uiop, ioflags, cred);
706 FREE((caddr_t)iv, M_TEMP);
711 * Common code for creating a regular file (plus special files for V2).
714 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
715 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
716 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
721 error = nd->nd_repstat;
722 if (!error && ndp->ni_vp == NULL) {
723 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
724 vrele(ndp->ni_startdir);
725 error = VOP_CREATE(ndp->ni_dvp,
726 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
728 nfsvno_relpathbuf(ndp);
730 if (*exclusive_flagp) {
731 *exclusive_flagp = 0;
732 NFSVNO_ATTRINIT(nvap);
733 nvap->na_atime.tv_sec = cverf[0];
734 nvap->na_atime.tv_nsec = cverf[1];
735 error = VOP_SETATTR(ndp->ni_vp,
736 &nvap->na_vattr, nd->nd_cred);
740 * NFS V2 Only. nfsrvd_mknod() does this for V3.
741 * (This implies, just get out on an error.)
743 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
744 nvap->na_type == VFIFO) {
745 if (nvap->na_type == VCHR && rdev == 0xffffffff)
746 nvap->na_type = VFIFO;
747 if (nvap->na_type != VFIFO &&
748 (error = priv_check_cred(nd->nd_cred,
749 PRIV_VFS_MKNOD_DEV, 0))) {
750 vrele(ndp->ni_startdir);
751 nfsvno_relpathbuf(ndp);
755 nvap->na_rdev = rdev;
756 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
757 &ndp->ni_cnd, &nvap->na_vattr);
759 nfsvno_relpathbuf(ndp);
761 vrele(ndp->ni_startdir);
765 vrele(ndp->ni_startdir);
766 nfsvno_relpathbuf(ndp);
773 * Handle cases where error is already set and/or
775 * 1 - clean up the lookup
776 * 2 - iff !error and na_size set, truncate it
778 vrele(ndp->ni_startdir);
779 nfsvno_relpathbuf(ndp);
781 if (ndp->ni_dvp == *vpp)
785 if (!error && nvap->na_size != VNOVAL) {
786 error = nfsvno_accchk(*vpp, VWRITE,
787 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
788 NFSACCCHK_VPISLOCKED, NULL);
790 tempsize = nvap->na_size;
791 NFSVNO_ATTRINIT(nvap);
792 nvap->na_size = tempsize;
793 error = VOP_SETATTR(*vpp,
794 &nvap->na_vattr, nd->nd_cred);
804 * Do a mknod vnode op.
807 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
813 vtyp = nvap->na_type;
815 * Iff doesn't exist, create it.
818 vrele(ndp->ni_startdir);
819 nfsvno_relpathbuf(ndp);
824 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
825 vrele(ndp->ni_startdir);
826 nfsvno_relpathbuf(ndp);
828 return (NFSERR_BADTYPE);
831 vrele(ndp->ni_startdir);
832 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
833 &ndp->ni_cnd, &nvap->na_vattr);
835 nfsvno_relpathbuf(ndp);
837 if (nvap->na_type != VFIFO &&
838 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
839 vrele(ndp->ni_startdir);
840 nfsvno_relpathbuf(ndp);
844 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
845 &ndp->ni_cnd, &nvap->na_vattr);
847 nfsvno_relpathbuf(ndp);
849 vrele(ndp->ni_startdir);
851 * Since VOP_MKNOD returns the ni_vp, I can't
852 * see any reason to do the lookup.
862 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
863 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
867 if (ndp->ni_vp != NULL) {
868 if (ndp->ni_dvp == ndp->ni_vp)
873 nfsvno_relpathbuf(ndp);
876 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
879 nfsvno_relpathbuf(ndp);
887 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
888 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
889 struct nfsexstuff *exp)
894 vrele(ndp->ni_startdir);
895 nfsvno_relpathbuf(ndp);
896 if (ndp->ni_dvp == ndp->ni_vp)
904 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
905 &nvap->na_vattr, pathcp);
907 vrele(ndp->ni_startdir);
908 nfsvno_relpathbuf(ndp);
910 * Although FreeBSD still had the lookup code in
911 * it for 7/current, there doesn't seem to be any
912 * point, since VOP_SYMLINK() returns the ni_vp.
913 * Just vput it for v2.
915 if (!not_v2 && !error)
921 * Parse symbolic link arguments.
922 * This function has an ugly side effect. It will MALLOC() an area for
923 * the symlink and set iov_base to point to it, only if it succeeds.
924 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
928 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
929 struct thread *p, char **pathcpp, int *lenp)
934 struct nfsv2_sattr *sp;
938 if ((nd->nd_flag & ND_NFSV3) &&
939 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
941 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
942 len = fxdr_unsigned(int, *tl);
943 if (len > NFS_MAXPATHLEN || len <= 0) {
947 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
948 error = nfsrv_mtostr(nd, pathcp, len);
951 if (nd->nd_flag & ND_NFSV2) {
952 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
953 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
960 free(pathcp, M_TEMP);
965 * Remove a non-directory object.
968 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
969 struct thread *p, struct nfsexstuff *exp)
975 if (vp->v_type == VDIR)
976 error = NFSERR_ISDIR;
978 error = nfsrv_checkremove(vp, 1, p);
980 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
981 if (ndp->ni_dvp == vp)
990 * Remove a directory.
993 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
994 struct thread *p, struct nfsexstuff *exp)
1000 if (vp->v_type != VDIR) {
1005 * No rmdir "." please.
1007 if (ndp->ni_dvp == vp) {
1012 * The root of a mounted filesystem cannot be deleted.
1014 if (vp->v_vflag & VV_ROOT)
1018 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1019 if (ndp->ni_dvp == vp)
1031 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1032 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1034 struct vnode *fvp, *tvp, *tdvp;
1037 fvp = fromndp->ni_vp;
1039 vrele(fromndp->ni_dvp);
1044 tdvp = tondp->ni_dvp;
1047 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1048 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1050 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1051 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1054 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1055 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1060 * A rename to '.' or '..' results in a prematurely
1061 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1064 if ((tondp->ni_cnd.cn_namelen == 1 &&
1065 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1066 (tondp->ni_cnd.cn_namelen == 2 &&
1067 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1068 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1073 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1074 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1077 if (fvp->v_mount != tdvp->v_mount) {
1078 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1082 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1087 * If source and destination are the same, there is nothing to
1088 * do. Set error to -1 to indicate this.
1093 if (ndflag & ND_NFSV4) {
1094 NFSVOPLOCK(fvp, LK_EXCLUSIVE | LK_RETRY, p);
1095 error = nfsrv_checkremove(fvp, 0, p);
1096 NFSVOPUNLOCK(fvp, 0, p);
1098 error = nfsrv_checkremove(tvp, 1, p);
1101 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1102 * that the NFSv4 client won't be confused by the rename.
1103 * Since nfsd_recalldelegation() can only be called on an
1104 * unlocked vnode at this point and fvp is the file that will
1105 * still exist after the rename, just do fvp.
1107 nfsd_recalldelegation(fvp, p);
1111 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1112 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1121 vrele(fromndp->ni_dvp);
1126 vrele(tondp->ni_startdir);
1127 nfsvno_relpathbuf(tondp);
1129 vrele(fromndp->ni_startdir);
1130 nfsvno_relpathbuf(fromndp);
1138 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1139 struct thread *p, struct nfsexstuff *exp)
1149 if (vp->v_mount != xp->v_mount)
1153 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
1154 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1155 if (ndp->ni_dvp == vp)
1159 NFSVOPUNLOCK(vp, 0, p);
1161 if (ndp->ni_dvp == ndp->ni_vp)
1168 nfsvno_relpathbuf(ndp);
1173 * Do the fsync() appropriate for the commit.
1176 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1181 if (cnt > MAX_COMMIT_COUNT) {
1183 * Give up and do the whole thing
1186 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1187 VM_OBJECT_LOCK(vp->v_object);
1188 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1189 VM_OBJECT_UNLOCK(vp->v_object);
1191 error = VOP_FSYNC(vp, MNT_WAIT, td);
1194 * Locate and synchronously write any buffers that fall
1195 * into the requested range. Note: we are assuming that
1196 * f_iosize is a power of 2.
1198 int iosize = vp->v_mount->mnt_stat.f_iosize;
1199 int iomask = iosize - 1;
1204 * Align to iosize boundry, super-align to page boundry.
1207 cnt += off & iomask;
1208 off &= ~(u_quad_t)iomask;
1210 if (off & PAGE_MASK) {
1211 cnt += off & PAGE_MASK;
1212 off &= ~(u_quad_t)PAGE_MASK;
1214 lblkno = off / iosize;
1217 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1218 VM_OBJECT_LOCK(vp->v_object);
1219 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
1220 VM_OBJECT_UNLOCK(vp->v_object);
1229 * If we have a buffer and it is marked B_DELWRI we
1230 * have to lock and write it. Otherwise the prior
1231 * write is assumed to have already been committed.
1233 * gbincore() can return invalid buffers now so we
1234 * have to check that bit as well (though B_DELWRI
1235 * should not be set if B_INVAL is set there could be
1236 * a race here since we haven't locked the buffer).
1238 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1239 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1240 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1242 continue; /* retry */
1244 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1247 bp->b_flags &= ~B_ASYNC;
1269 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1272 return (VFS_STATFS(vp->v_mount, sf));
1276 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1277 * must handle nfsrv_opencheck() calls after any other access checks.
1280 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1281 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1282 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1283 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1284 struct nfsexstuff *exp, struct vnode **vpp)
1286 struct vnode *vp = NULL;
1288 struct nfsexstuff nes;
1290 if (ndp->ni_vp == NULL)
1291 nd->nd_repstat = nfsrv_opencheck(clientid,
1292 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1293 if (!nd->nd_repstat) {
1294 if (ndp->ni_vp == NULL) {
1295 vrele(ndp->ni_startdir);
1296 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1297 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1299 nfsvno_relpathbuf(ndp);
1300 if (!nd->nd_repstat) {
1301 if (*exclusive_flagp) {
1302 *exclusive_flagp = 0;
1303 NFSVNO_ATTRINIT(nvap);
1304 nvap->na_atime.tv_sec = cverf[0];
1305 nvap->na_atime.tv_nsec = cverf[1];
1306 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1307 &nvap->na_vattr, cred);
1309 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1310 aclp, p, attrbitp, exp);
1315 if (ndp->ni_startdir)
1316 vrele(ndp->ni_startdir);
1317 nfsvno_relpathbuf(ndp);
1319 if (create == NFSV4OPEN_CREATE) {
1320 if (ndp->ni_dvp == vp)
1325 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1326 if (ndp->ni_cnd.cn_flags & RDONLY)
1327 NFSVNO_SETEXRDONLY(&nes);
1329 NFSVNO_EXINIT(&nes);
1330 nd->nd_repstat = nfsvno_accchk(vp,
1331 VWRITE, cred, &nes, p,
1332 NFSACCCHK_NOOVERRIDE,
1333 NFSACCCHK_VPISLOCKED, NULL);
1334 nd->nd_repstat = nfsrv_opencheck(clientid,
1335 stateidp, stp, vp, nd, p, nd->nd_repstat);
1336 if (!nd->nd_repstat) {
1337 tempsize = nvap->na_size;
1338 NFSVNO_ATTRINIT(nvap);
1339 nvap->na_size = tempsize;
1340 nd->nd_repstat = VOP_SETATTR(vp,
1341 &nvap->na_vattr, cred);
1343 } else if (vp->v_type == VREG) {
1344 nd->nd_repstat = nfsrv_opencheck(clientid,
1345 stateidp, stp, vp, nd, p, nd->nd_repstat);
1349 if (ndp->ni_cnd.cn_flags & HASBUF)
1350 nfsvno_relpathbuf(ndp);
1351 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1352 vrele(ndp->ni_startdir);
1353 if (ndp->ni_dvp == ndp->ni_vp)
1365 * Updates the file rev and sets the mtime and ctime
1366 * to the current clock time, returning the va_filerev and va_Xtime
1370 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1371 struct ucred *cred, struct thread *p)
1376 getnanotime(&va.va_mtime);
1377 (void) VOP_SETATTR(vp, &va, cred);
1378 (void) nfsvno_getattr(vp, nvap, cred, p);
1382 * Glue routine to nfsv4_fillattr().
1385 nfsvno_fillattr(struct nfsrv_descript *nd, struct vnode *vp,
1386 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1387 struct ucred *cred, struct thread *p, int isdgram, int reterr)
1391 error = nfsv4_fillattr(nd, vp, NULL, &nvap->na_vattr, fhp, rderror,
1392 attrbitp, cred, p, isdgram, reterr);
1396 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1398 * nfs readdir service
1399 * - mallocs what it thinks is enough to read
1400 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1401 * - calls VOP_READDIR()
1402 * - loops around building the reply
1403 * if the output generated exceeds count break out of loop
1404 * The NFSM_CLGET macro is used here so that the reply will be packed
1405 * tightly in mbuf clusters.
1406 * - it trims out records with d_fileno == 0
1407 * this doesn't matter for Unix clients, but they might confuse clients
1409 * - it trims out records with d_type == DT_WHT
1410 * these cannot be seen through NFS (unless we extend the protocol)
1411 * The alternate call nfsrvd_readdirplus() does lookups as well.
1412 * PS: The NFS protocol spec. does not clarify what the "count" byte
1413 * argument is a count of.. just name strings and file id's or the
1414 * entire reply rpc or ...
1415 * I tried just file name and id sizes and it confused the Sun client,
1416 * so I am using the full rpc size now. The "paranoia.." comment refers
1417 * to including the status longwords that are not a part of the dir.
1418 * "entry" structures, but are in the rpc.
1421 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1422 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1427 char *cpos, *cend, *rbuf;
1429 int nlen, error = 0, getret = 1;
1430 int siz, cnt, fullsiz, eofflag, ncookies;
1431 u_int64_t off, toff, verf;
1432 u_long *cookies = NULL, *cookiep;
1436 if (nd->nd_repstat) {
1437 nfsrv_postopattr(nd, getret, &at);
1440 if (nd->nd_flag & ND_NFSV2) {
1441 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1442 off = fxdr_unsigned(u_quad_t, *tl++);
1444 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1445 off = fxdr_hyper(tl);
1447 verf = fxdr_hyper(tl);
1451 cnt = fxdr_unsigned(int, *tl);
1452 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1453 cnt = NFS_SRVMAXDATA(nd);
1454 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1456 if (nd->nd_flag & ND_NFSV3) {
1457 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1461 * va_filerev is not sufficient as a cookie verifier,
1462 * since it is not supposed to change when entries are
1463 * removed/added unless that offset cookies returned to
1464 * the client are no longer valid.
1466 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1467 nd->nd_repstat = NFSERR_BAD_COOKIE;
1470 if (nd->nd_repstat == 0 && cnt == 0) {
1471 if (nd->nd_flag & ND_NFSV2)
1472 /* NFSv2 does not have NFSERR_TOOSMALL */
1473 nd->nd_repstat = EPERM;
1475 nd->nd_repstat = NFSERR_TOOSMALL;
1477 if (!nd->nd_repstat)
1478 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1479 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1480 NFSACCCHK_VPISLOCKED, NULL);
1481 if (nd->nd_repstat) {
1483 if (nd->nd_flag & ND_NFSV3)
1484 nfsrv_postopattr(nd, getret, &at);
1487 NFSVOPUNLOCK(vp, 0, p);
1488 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1492 free((caddr_t)cookies, M_TEMP);
1500 io.uio_offset = (off_t)off;
1502 io.uio_segflg = UIO_SYSSPACE;
1503 io.uio_rw = UIO_READ;
1505 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
1506 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1508 NFSVOPUNLOCK(vp, 0, p);
1509 off = (u_int64_t)io.uio_offset;
1511 siz -= io.uio_resid;
1513 if (!cookies && !nd->nd_repstat)
1514 nd->nd_repstat = NFSERR_PERM;
1515 if (nd->nd_flag & ND_NFSV3) {
1516 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1517 if (!nd->nd_repstat)
1518 nd->nd_repstat = getret;
1522 * Handles the failed cases. nd->nd_repstat == 0 past here.
1524 if (nd->nd_repstat) {
1526 free((caddr_t)rbuf, M_TEMP);
1528 free((caddr_t)cookies, M_TEMP);
1529 if (nd->nd_flag & ND_NFSV3)
1530 nfsrv_postopattr(nd, getret, &at);
1534 * If nothing read, return eof
1539 if (nd->nd_flag & ND_NFSV2) {
1540 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1542 nfsrv_postopattr(nd, getret, &at);
1543 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1544 txdr_hyper(at.na_filerev, tl);
1547 *tl++ = newnfs_false;
1549 FREE((caddr_t)rbuf, M_TEMP);
1550 FREE((caddr_t)cookies, M_TEMP);
1555 * Check for degenerate cases of nothing useful read.
1556 * If so go try again
1560 dp = (struct dirent *)cpos;
1564 * For some reason FreeBSD's ufs_readdir() chooses to back the
1565 * directory offset up to a block boundary, so it is necessary to
1566 * skip over the records that precede the requested offset. This
1567 * requires the assumption that file offset cookies monotonically
1570 while (cpos < cend && ncookies > 0 &&
1571 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1572 ((u_quad_t)(*cookiep)) <= toff)) {
1573 cpos += dp->d_reclen;
1574 dp = (struct dirent *)cpos;
1578 if (cpos >= cend || ncookies == 0) {
1585 * dirlen is the size of the reply, including all XDR and must
1586 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1587 * if the XDR should be included in "count", but to be safe, we do.
1588 * (Include the two booleans at the end of the reply in dirlen now.)
1590 if (nd->nd_flag & ND_NFSV3) {
1591 nfsrv_postopattr(nd, getret, &at);
1592 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1593 txdr_hyper(at.na_filerev, tl);
1594 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1596 dirlen = 2 * NFSX_UNSIGNED;
1599 /* Loop through the records and build reply */
1600 while (cpos < cend && ncookies > 0) {
1601 nlen = dp->d_namlen;
1602 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1603 nlen <= NFS_MAXNAMLEN) {
1604 if (nd->nd_flag & ND_NFSV3)
1605 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1607 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1614 * Build the directory record xdr from
1617 if (nd->nd_flag & ND_NFSV3) {
1618 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1619 *tl++ = newnfs_true;
1622 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1623 *tl++ = newnfs_true;
1625 *tl = txdr_unsigned(dp->d_fileno);
1626 (void) nfsm_strtom(nd, dp->d_name, nlen);
1627 if (nd->nd_flag & ND_NFSV3) {
1628 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1631 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1632 *tl = txdr_unsigned(*cookiep);
1634 cpos += dp->d_reclen;
1635 dp = (struct dirent *)cpos;
1642 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1643 *tl++ = newnfs_false;
1648 FREE((caddr_t)rbuf, M_TEMP);
1649 FREE((caddr_t)cookies, M_TEMP);
1657 * Readdirplus for V3 and Readdir for V4.
1660 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1661 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1666 char *cpos, *cend, *rbuf;
1669 struct nfsvattr nva, at, *nvap = &nva;
1670 struct mbuf *mb0, *mb1;
1671 struct nfsreferral *refp;
1672 int nlen, r, error = 0, getret = 1, usevget = 1;
1673 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1674 caddr_t bpos0, bpos1;
1675 u_int64_t off, toff, verf;
1676 u_long *cookies = NULL, *cookiep;
1677 nfsattrbit_t attrbits, rderrbits, savbits;
1680 struct componentname cn;
1682 if (nd->nd_repstat) {
1683 nfsrv_postopattr(nd, getret, &at);
1686 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1687 off = fxdr_hyper(tl);
1690 verf = fxdr_hyper(tl);
1692 siz = fxdr_unsigned(int, *tl++);
1693 cnt = fxdr_unsigned(int, *tl);
1696 * Use the server's maximum data transfer size as the upper bound
1699 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1700 cnt = NFS_SRVMAXDATA(nd);
1703 * siz is a "hint" of how much directory information (name, fileid,
1704 * cookie) should be in the reply. At least one client "hints" 0,
1705 * so I set it to cnt for that case. I also round it up to the
1706 * next multiple of DIRBLKSIZ.
1710 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1712 if (nd->nd_flag & ND_NFSV4) {
1713 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1716 NFSSET_ATTRBIT(&savbits, &attrbits);
1717 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1718 NFSZERO_ATTRBIT(&rderrbits);
1719 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1721 NFSZERO_ATTRBIT(&attrbits);
1724 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1725 if (!nd->nd_repstat) {
1726 if (off && verf != at.na_filerev) {
1728 * va_filerev is not sufficient as a cookie verifier,
1729 * since it is not supposed to change when entries are
1730 * removed/added unless that offset cookies returned to
1731 * the client are no longer valid.
1734 if (nd->nd_flag & ND_NFSV4) {
1735 nd->nd_repstat = NFSERR_NOTSAME;
1737 nd->nd_repstat = NFSERR_BAD_COOKIE;
1740 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1741 nd->nd_repstat = NFSERR_BAD_COOKIE;
1744 if (!nd->nd_repstat && vp->v_type != VDIR)
1745 nd->nd_repstat = NFSERR_NOTDIR;
1746 if (!nd->nd_repstat && cnt == 0)
1747 nd->nd_repstat = NFSERR_TOOSMALL;
1748 if (!nd->nd_repstat)
1749 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1750 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1751 NFSACCCHK_VPISLOCKED, NULL);
1752 if (nd->nd_repstat) {
1754 if (nd->nd_flag & ND_NFSV3)
1755 nfsrv_postopattr(nd, getret, &at);
1759 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1763 free((caddr_t)cookies, M_TEMP);
1771 io.uio_offset = (off_t)off;
1773 io.uio_segflg = UIO_SYSSPACE;
1774 io.uio_rw = UIO_READ;
1776 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1778 off = (u_int64_t)io.uio_offset;
1780 siz -= io.uio_resid;
1782 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1784 if (!cookies && !nd->nd_repstat)
1785 nd->nd_repstat = NFSERR_PERM;
1786 if (!nd->nd_repstat)
1787 nd->nd_repstat = getret;
1788 if (nd->nd_repstat) {
1791 free((caddr_t)cookies, M_TEMP);
1792 free((caddr_t)rbuf, M_TEMP);
1793 if (nd->nd_flag & ND_NFSV3)
1794 nfsrv_postopattr(nd, getret, &at);
1798 * If nothing read, return eof
1803 if (nd->nd_flag & ND_NFSV3)
1804 nfsrv_postopattr(nd, getret, &at);
1805 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1806 txdr_hyper(at.na_filerev, tl);
1808 *tl++ = newnfs_false;
1810 free((caddr_t)cookies, M_TEMP);
1811 free((caddr_t)rbuf, M_TEMP);
1816 * Check for degenerate cases of nothing useful read.
1817 * If so go try again
1821 dp = (struct dirent *)cpos;
1825 * For some reason FreeBSD's ufs_readdir() chooses to back the
1826 * directory offset up to a block boundary, so it is necessary to
1827 * skip over the records that precede the requested offset. This
1828 * requires the assumption that file offset cookies monotonically
1831 while (cpos < cend && ncookies > 0 &&
1832 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1833 ((u_quad_t)(*cookiep)) <= toff ||
1834 ((nd->nd_flag & ND_NFSV4) &&
1835 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1836 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1837 cpos += dp->d_reclen;
1838 dp = (struct dirent *)cpos;
1842 if (cpos >= cend || ncookies == 0) {
1847 NFSVOPUNLOCK(vp, 0, p);
1850 * Save this position, in case there is an error before one entry
1854 bpos0 = nd->nd_bpos;
1857 * Fill in the first part of the reply.
1858 * dirlen is the reply length in bytes and cannot exceed cnt.
1859 * (Include the two booleans at the end of the reply in dirlen now,
1860 * so we recognize when we have exceeded cnt.)
1862 if (nd->nd_flag & ND_NFSV3) {
1863 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1864 nfsrv_postopattr(nd, getret, &at);
1866 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
1868 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1869 txdr_hyper(at.na_filerev, tl);
1872 * Save this position, in case there is an empty reply needed.
1875 bpos1 = nd->nd_bpos;
1877 /* Loop through the records and build reply */
1879 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
1880 nlen = dp->d_namlen;
1881 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1882 nlen <= NFS_MAXNAMLEN &&
1883 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
1884 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
1885 || (nlen == 1 && dp->d_name[0] != '.'))) {
1887 * Save the current position in the reply, in case
1888 * this entry exceeds cnt.
1891 bpos1 = nd->nd_bpos;
1894 * For readdir_and_lookup get the vnode using
1900 if ((nd->nd_flag & ND_NFSV3) ||
1901 NFSNONZERO_ATTRBIT(&savbits)) {
1902 if (nd->nd_flag & ND_NFSV4)
1903 refp = nfsv4root_getreferral(NULL,
1907 r = VFS_VGET(vp->v_mount,
1908 dp->d_fileno, LK_EXCLUSIVE,
1912 if (r == EOPNOTSUPP) {
1915 cn.cn_nameiop = LOOKUP;
1923 cn.cn_nameptr = dp->d_name;
1924 cn.cn_namelen = nlen;
1925 cn.cn_flags = ISLASTCN |
1926 NOFOLLOW | LOCKLEAF |
1929 dp->d_name[0] == '.' &&
1930 dp->d_name[1] == '.')
1933 if (!VOP_ISLOCKED(vp))
1937 if ((vp->v_vflag & VV_ROOT) != 0
1938 && (cn.cn_flags & ISDOTDOT)
1944 r = VOP_LOOKUP(vp, &nvp,
1950 ((nd->nd_flag & ND_NFSV3) ||
1951 NFSNONZERO_ATTRBIT(&attrbits))) {
1952 r = nfsvno_getfh(nvp, &nfh, p);
1954 r = nfsvno_getattr(nvp, nvap,
1961 if (!NFSISSET_ATTRBIT(&attrbits,
1962 NFSATTRBIT_RDATTRERROR)) {
1972 * Build the directory record xdr
1974 if (nd->nd_flag & ND_NFSV3) {
1975 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1976 *tl++ = newnfs_true;
1978 *tl = txdr_unsigned(dp->d_fileno);
1979 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
1980 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1982 *tl = txdr_unsigned(*cookiep);
1983 nfsrv_postopattr(nd, 0, nvap);
1984 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
1985 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
1989 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1990 *tl++ = newnfs_true;
1992 *tl = txdr_unsigned(*cookiep);
1993 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
1995 NFSVOPUNLOCK(nvp, 0, p);
1997 dirlen += nfsrv_putreferralattr(nd,
2000 if (nd->nd_repstat) {
2006 dirlen += nfsvno_fillattr(nd, nvp, nvap,
2007 &nfh, r, &rderrbits, nd->nd_cred,
2010 dirlen += nfsvno_fillattr(nd, nvp, nvap,
2011 &nfh, r, &attrbits, nd->nd_cred,
2016 dirlen += (3 * NFSX_UNSIGNED);
2021 cpos += dp->d_reclen;
2022 dp = (struct dirent *)cpos;
2026 if (!usevget && VOP_ISLOCKED(vp))
2032 * If dirlen > cnt, we must strip off the last entry. If that
2033 * results in an empty reply, report NFSERR_TOOSMALL.
2035 if (dirlen > cnt || nd->nd_repstat) {
2036 if (!nd->nd_repstat && entrycnt == 0)
2037 nd->nd_repstat = NFSERR_TOOSMALL;
2039 newnfs_trimtrailing(nd, mb0, bpos0);
2041 newnfs_trimtrailing(nd, mb1, bpos1);
2043 } else if (cpos < cend)
2045 if (!nd->nd_repstat) {
2046 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2047 *tl++ = newnfs_false;
2053 FREE((caddr_t)cookies, M_TEMP);
2054 FREE((caddr_t)rbuf, M_TEMP);
2062 * Get the settable attributes out of the mbuf list.
2063 * (Return 0 or EBADRPC)
2066 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2067 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2070 struct nfsv2_sattr *sp;
2071 struct timeval curtime;
2072 int error = 0, toclient = 0;
2074 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2076 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2078 * Some old clients didn't fill in the high order 16bits.
2079 * --> check the low order 2 bytes for 0xffff
2081 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2082 nvap->na_mode = nfstov_mode(sp->sa_mode);
2083 if (sp->sa_uid != newnfs_xdrneg1)
2084 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2085 if (sp->sa_gid != newnfs_xdrneg1)
2086 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2087 if (sp->sa_size != newnfs_xdrneg1)
2088 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2089 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2091 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2093 nvap->na_atime.tv_sec =
2094 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2095 nvap->na_atime.tv_nsec = 0;
2098 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2099 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2102 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2103 if (*tl == newnfs_true) {
2104 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2105 nvap->na_mode = nfstov_mode(*tl);
2107 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2108 if (*tl == newnfs_true) {
2109 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2110 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2112 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2113 if (*tl == newnfs_true) {
2114 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2115 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2117 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2118 if (*tl == newnfs_true) {
2119 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2120 nvap->na_size = fxdr_hyper(tl);
2122 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2123 switch (fxdr_unsigned(int, *tl)) {
2124 case NFSV3SATTRTIME_TOCLIENT:
2125 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2126 fxdr_nfsv3time(tl, &nvap->na_atime);
2129 case NFSV3SATTRTIME_TOSERVER:
2130 NFSGETTIME(&curtime);
2131 nvap->na_atime.tv_sec = curtime.tv_sec;
2132 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2133 nvap->na_vaflags |= VA_UTIMES_NULL;
2136 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2137 switch (fxdr_unsigned(int, *tl)) {
2138 case NFSV3SATTRTIME_TOCLIENT:
2139 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2140 fxdr_nfsv3time(tl, &nvap->na_mtime);
2141 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2143 case NFSV3SATTRTIME_TOSERVER:
2144 NFSGETTIME(&curtime);
2145 nvap->na_mtime.tv_sec = curtime.tv_sec;
2146 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2148 nvap->na_vaflags |= VA_UTIMES_NULL;
2153 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2160 * Handle the setable attributes for V4.
2161 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2164 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2165 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2170 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2172 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2175 struct timeval curtime;
2177 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2180 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2181 attrsize = fxdr_unsigned(int, *tl);
2184 * Loop around getting the setable attributes. If an unsupported
2185 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2188 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2189 bitpos = NFSATTRBIT_MAX;
2193 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2194 if (attrsum > attrsize) {
2195 error = NFSERR_BADXDR;
2198 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2200 case NFSATTRBIT_SIZE:
2201 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2202 nvap->na_size = fxdr_hyper(tl);
2203 attrsum += NFSX_HYPER;
2205 case NFSATTRBIT_ACL:
2206 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2210 if (aceerr && !nd->nd_repstat)
2211 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2214 case NFSATTRBIT_ARCHIVE:
2215 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2216 if (!nd->nd_repstat)
2217 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2218 attrsum += NFSX_UNSIGNED;
2220 case NFSATTRBIT_HIDDEN:
2221 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2222 if (!nd->nd_repstat)
2223 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2224 attrsum += NFSX_UNSIGNED;
2226 case NFSATTRBIT_MIMETYPE:
2227 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2228 i = fxdr_unsigned(int, *tl);
2229 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2232 if (!nd->nd_repstat)
2233 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2234 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2236 case NFSATTRBIT_MODE:
2237 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2238 nvap->na_mode = nfstov_mode(*tl);
2239 attrsum += NFSX_UNSIGNED;
2241 case NFSATTRBIT_OWNER:
2242 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2243 j = fxdr_unsigned(int, *tl);
2245 return (NFSERR_BADXDR);
2246 if (j > NFSV4_SMALLSTR)
2247 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2250 error = nfsrv_mtostr(nd, cp, j);
2252 if (j > NFSV4_SMALLSTR)
2253 free(cp, M_NFSSTRING);
2256 if (!nd->nd_repstat) {
2257 nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p);
2258 if (!nd->nd_repstat)
2261 if (j > NFSV4_SMALLSTR)
2262 free(cp, M_NFSSTRING);
2263 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2265 case NFSATTRBIT_OWNERGROUP:
2266 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2267 j = fxdr_unsigned(int, *tl);
2269 return (NFSERR_BADXDR);
2270 if (j > NFSV4_SMALLSTR)
2271 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2274 error = nfsrv_mtostr(nd, cp, j);
2276 if (j > NFSV4_SMALLSTR)
2277 free(cp, M_NFSSTRING);
2280 if (!nd->nd_repstat) {
2281 nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p);
2282 if (!nd->nd_repstat)
2285 if (j > NFSV4_SMALLSTR)
2286 free(cp, M_NFSSTRING);
2287 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2289 case NFSATTRBIT_SYSTEM:
2290 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2291 if (!nd->nd_repstat)
2292 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2293 attrsum += NFSX_UNSIGNED;
2295 case NFSATTRBIT_TIMEACCESSSET:
2296 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2297 attrsum += NFSX_UNSIGNED;
2298 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2299 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2300 fxdr_nfsv4time(tl, &nvap->na_atime);
2302 attrsum += NFSX_V4TIME;
2304 NFSGETTIME(&curtime);
2305 nvap->na_atime.tv_sec = curtime.tv_sec;
2306 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2307 nvap->na_vaflags |= VA_UTIMES_NULL;
2310 case NFSATTRBIT_TIMEBACKUP:
2311 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2312 if (!nd->nd_repstat)
2313 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2314 attrsum += NFSX_V4TIME;
2316 case NFSATTRBIT_TIMECREATE:
2317 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2318 if (!nd->nd_repstat)
2319 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2320 attrsum += NFSX_V4TIME;
2322 case NFSATTRBIT_TIMEMODIFYSET:
2323 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2324 attrsum += NFSX_UNSIGNED;
2325 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2326 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2327 fxdr_nfsv4time(tl, &nvap->na_mtime);
2328 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2329 attrsum += NFSX_V4TIME;
2331 NFSGETTIME(&curtime);
2332 nvap->na_mtime.tv_sec = curtime.tv_sec;
2333 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2335 nvap->na_vaflags |= VA_UTIMES_NULL;
2339 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2341 * set bitpos so we drop out of the loop.
2343 bitpos = NFSATTRBIT_MAX;
2349 * some clients pad the attrlist, so we need to skip over the
2352 if (attrsum > attrsize) {
2353 error = NFSERR_BADXDR;
2355 attrsize = NFSM_RNDUP(attrsize);
2356 if (attrsum < attrsize)
2357 error = nfsm_advance(nd, attrsize - attrsum, -1);
2364 * Check/setup export credentials.
2367 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2368 struct ucred *credanon)
2373 * Check/setup credentials.
2375 if (nd->nd_flag & ND_GSS)
2376 exp->nes_exflag &= ~MNT_EXPORTANON;
2379 * Check to see if the operation is allowed for this security flavor.
2380 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2381 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2382 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2384 if (nfsvno_testexp(nd, exp) &&
2385 nd->nd_procnum != NFSV4OP_SECINFO &&
2386 nd->nd_procnum != NFSPROC_FSINFO) {
2387 if (nd->nd_flag & ND_NFSV4)
2388 error = NFSERR_WRONGSEC;
2390 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2395 * Check to see if the file system is exported V4 only.
2397 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4))
2398 return (NFSERR_PROGNOTV4);
2401 * Now, map the user credentials.
2402 * (Note that ND_AUTHNONE will only be set for an NFSv3
2403 * Fsinfo RPC. If set for anything else, this code might need
2406 if (NFSVNO_EXPORTED(exp) &&
2407 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2408 NFSVNO_EXPORTANON(exp) ||
2409 (nd->nd_flag & ND_AUTHNONE))) {
2410 nd->nd_cred->cr_uid = credanon->cr_uid;
2411 nd->nd_cred->cr_gid = credanon->cr_gid;
2412 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2413 credanon->cr_groups);
2422 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2423 struct ucred **credp)
2425 int i, error, *secflavors;
2427 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2428 &exp->nes_numsecflavor, &secflavors);
2430 if (nfs_rootfhset) {
2431 exp->nes_exflag = 0;
2432 exp->nes_numsecflavor = 0;
2436 /* Copy the security flavors. */
2437 for (i = 0; i < exp->nes_numsecflavor; i++)
2438 exp->nes_secflavors[i] = secflavors[i];
2444 * Get a vnode for a file handle and export stuff.
2447 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2448 struct vnode **vpp, struct nfsexstuff *exp, struct ucred **credp)
2450 int i, error, *secflavors;
2453 exp->nes_numsecflavor = 0;
2454 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2456 /* Make sure the server replies ESTALE to the client. */
2458 if (nam && !error) {
2459 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2460 &exp->nes_numsecflavor, &secflavors);
2462 if (nfs_rootfhset) {
2463 exp->nes_exflag = 0;
2464 exp->nes_numsecflavor = 0;
2470 /* Copy the security flavors. */
2471 for (i = 0; i < exp->nes_numsecflavor; i++)
2472 exp->nes_secflavors[i] = secflavors[i];
2479 * Do the pathconf vnode op.
2482 nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
2483 struct ucred *cred, struct thread *p)
2487 error = VOP_PATHCONF(vp, flag, retf);
2492 * nfsd_fhtovp() - convert a fh to a vnode ptr
2493 * - look up fsid in mount list (if not found ret error)
2494 * - get vp and export rights by calling nfsvno_fhtovp()
2495 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2497 * Also handle getting the Giant lock for the file system,
2499 * - if same mount point as *mpp
2501 * else if *mpp == NULL
2505 * call VFS_LOCK_GIANT()
2509 * call VFS_LOCK_GIANT()
2512 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp,
2513 struct vnode **vpp, struct nfsexstuff *exp,
2514 struct mount **mpp, int startwrite, struct thread *p)
2517 struct ucred *credanon;
2520 fhp = (fhandle_t *)nfp->nfsrvfh_data;
2522 * Check for the special case of the nfsv4root_fh.
2524 mp = vfs_getvfs(&fhp->fh_fsid);
2527 nd->nd_repstat = ESTALE;
2528 if (*mpp && exp->nes_vfslocked)
2529 VFS_UNLOCK_GIANT(*mpp);
2531 exp->nes_vfslocked = 0;
2536 * Now, handle Giant for the file system.
2538 if (*mpp != NULL && *mpp != mp && exp->nes_vfslocked) {
2539 VFS_UNLOCK_GIANT(*mpp);
2540 exp->nes_vfslocked = 0;
2542 if (!exp->nes_vfslocked && *mpp != mp)
2543 exp->nes_vfslocked = VFS_LOCK_GIANT(mp);
2547 vn_start_write(NULL, mpp, V_WAIT);
2549 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, vpp, exp,
2553 * For NFSv4 without a pseudo root fs, unexported file handles
2554 * can be returned, so that Lookup works everywhere.
2556 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2557 !(nd->nd_flag & ND_NFSV4)) {
2559 nd->nd_repstat = EACCES;
2563 * Personally, I've never seen any point in requiring a
2564 * reserved port#, since only in the rare case where the
2565 * clients are all boxes with secure system priviledges,
2566 * does it provide any enhanced security, but... some people
2567 * believe it to be useful and keep putting this code back in.
2568 * (There is also some "security checker" out there that
2569 * complains if the nfs server doesn't enforce this.)
2570 * However, note the following:
2571 * RFC3530 (NFSv4) specifies that a reserved port# not be
2573 * RFC2623 recommends that, if a reserved port# is checked for,
2574 * that there be a way to turn that off--> ifdef'd.
2576 #ifdef NFS_REQRSVPORT
2577 if (!nd->nd_repstat) {
2578 struct sockaddr_in *saddr;
2579 struct sockaddr_in6 *saddr6;
2581 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2582 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2583 if (!(nd->nd_flag & ND_NFSV4) &&
2584 ((saddr->sin_family == AF_INET &&
2585 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2586 (saddr6->sin6_family == AF_INET6 &&
2587 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2589 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2592 #endif /* NFS_REQRSVPORT */
2595 * Check/setup credentials.
2597 if (!nd->nd_repstat) {
2598 nd->nd_saveduid = nd->nd_cred->cr_uid;
2599 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2603 if (credanon != NULL)
2605 if (nd->nd_repstat) {
2607 vn_finished_write(mp);
2608 if (exp->nes_vfslocked) {
2609 VFS_UNLOCK_GIANT(mp);
2610 exp->nes_vfslocked = 0;
2624 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2626 struct filedesc *fdp;
2629 fdp = p->td_proc->p_fd;
2630 if (fd >= fdp->fd_nfiles ||
2631 (fp = fdp->fd_ofiles[fd]) == NULL)
2638 * Called from nfssvc() to update the exports list. Just call
2639 * vfs_export(). This has to be done, since the v4 root fake fs isn't
2640 * in the mount list.
2643 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2645 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2647 struct nameidata nd;
2650 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2651 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT)) {
2654 } else if (error == 0) {
2655 if (nfsexargp->fspec == NULL)
2658 * If fspec != NULL, this is the v4root path.
2660 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2661 nfsexargp->fspec, p);
2662 if ((error = namei(&nd)) != 0)
2664 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2667 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2668 NFSBCOPY((caddr_t)&fh,
2669 nfs_rootfh.nfsrvfh_data,
2670 sizeof (fhandle_t));
2678 * Get the tcp socket sequence numbers we need.
2679 * (Maybe this should be moved to the tcp sources?)
2682 nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2687 inp = sotoinpcb(so);
2688 KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2690 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2694 tp = intotcpcb(inp);
2695 if (tp->t_state != TCPS_ESTABLISHED) {
2699 *maxp = tp->snd_max;
2700 *unap = tp->snd_una;
2706 * This function needs to test to see if the system is near its limit
2707 * for memory allocation via malloc() or mget() and return True iff
2708 * either of these resources are near their limit.
2709 * XXX (For now, this is just a stub.)
2711 int nfsrv_testmalloclimit = 0;
2713 nfsrv_mallocmget_limit(void)
2715 static int printmesg = 0;
2716 static int testval = 1;
2718 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2719 if ((printmesg++ % 100) == 0)
2720 printf("nfsd: malloc/mget near limit\n");
2727 * BSD specific initialization of a mount point.
2732 static int inited = 0;
2737 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2738 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2739 nfsv4root_mnt.mnt_export = NULL;
2740 TAILQ_INIT(&nfsv4root_opt);
2741 TAILQ_INIT(&nfsv4root_newopt);
2742 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2743 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2744 nfsv4root_mnt.mnt_nvnodelistsize = 0;
2748 * Get a vnode for a file handle, without checking exports, etc.
2751 nfsvno_getvp(fhandle_t *fhp)
2757 mp = vfs_getvfs(&fhp->fh_fsid);
2760 error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp);
2767 * Check to see it a byte range lock held by a process running
2768 * locally on the server conflicts with the new lock.
2771 nfsvno_localconflict(struct vnode *vp, int ftype, u_int64_t first,
2772 u_int64_t end, struct nfslockconflict *cfp, struct thread *td)
2777 if (!nfsrv_dolocallocks)
2779 fl.l_whence = SEEK_SET;
2781 fl.l_start = (off_t)first;
2782 if (end == NFS64BITSSET)
2785 fl.l_len = (off_t)(end - first);
2787 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2788 * values for all calls, so that all locks will be held by the
2789 * nfsd server. (The nfsd server handles conflicts between the
2791 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2792 * bytes, so it can't be put in l_sysid.
2794 if (nfsv4_sysid == 0)
2795 nfsv4_sysid = nlm_acquire_next_sysid();
2796 fl.l_pid = (pid_t)0;
2797 fl.l_sysid = (int)nfsv4_sysid;
2799 NFSVOPUNLOCK(vp, 0, td);
2800 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_GETLK, &fl,
2801 (F_POSIX | F_REMOTE));
2802 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2805 if (fl.l_type == F_UNLCK)
2808 cfp->cl_clientid.lval[0] = cfp->cl_clientid.lval[1] = 0;
2809 cfp->cl_first = (u_int64_t)fl.l_start;
2811 cfp->cl_end = NFS64BITSSET;
2813 cfp->cl_end = (u_int64_t)
2814 (fl.l_start + fl.l_len);
2815 if (fl.l_type == F_WRLCK)
2816 cfp->cl_flags = NFSLCK_WRITE;
2818 cfp->cl_flags = NFSLCK_READ;
2819 sprintf(cfp->cl_owner, "LOCALID%d", fl.l_pid);
2820 cfp->cl_ownerlen = strlen(cfp->cl_owner);
2821 return (NFSERR_DENIED);
2823 return (NFSERR_INVAL);
2827 * Do a local VOP_ADVLOCK().
2830 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2831 u_int64_t end, struct thread *td)
2837 if (nfsrv_dolocallocks == 0)
2839 fl.l_whence = SEEK_SET;
2841 fl.l_start = (off_t)first;
2842 if (end == NFS64BITSSET) {
2846 fl.l_len = (off_t)tlen;
2849 * For FreeBSD8, the l_pid and l_sysid must be set to the same
2850 * values for all calls, so that all locks will be held by the
2851 * nfsd server. (The nfsd server handles conflicts between the
2853 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2854 * bytes, so it can't be put in l_sysid.
2856 if (nfsv4_sysid == 0)
2857 nfsv4_sysid = nlm_acquire_next_sysid();
2858 fl.l_pid = (pid_t)0;
2859 fl.l_sysid = (int)nfsv4_sysid;
2861 NFSVOPUNLOCK(vp, 0, td);
2862 if (ftype == F_UNLCK)
2863 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2864 (F_POSIX | F_REMOTE));
2866 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2867 (F_POSIX | F_REMOTE));
2868 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2873 * Unlock an underlying local file system.
2876 nfsvno_unlockvfs(struct mount *mp)
2879 VFS_UNLOCK_GIANT(mp);
2883 * Lock an underlying file system, as required, and return
2884 * whether or not it is locked.
2887 nfsvno_lockvfs(struct mount *mp)
2891 ret = VFS_LOCK_GIANT(mp);
2896 * Check the nfsv4 root exports.
2899 nfsvno_v4rootexport(struct nfsrv_descript *nd)
2901 struct ucred *credanon;
2902 int exflags, error, numsecflavor, *secflavors, i;
2904 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
2905 &credanon, &numsecflavor, &secflavors);
2907 return (NFSERR_PROGUNAVAIL);
2908 if (credanon != NULL)
2910 for (i = 0; i < numsecflavor; i++) {
2911 if (secflavors[i] == AUTH_SYS)
2912 nd->nd_flag |= ND_EXAUTHSYS;
2913 else if (secflavors[i] == RPCSEC_GSS_KRB5)
2914 nd->nd_flag |= ND_EXGSS;
2915 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
2916 nd->nd_flag |= ND_EXGSSINTEGRITY;
2917 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
2918 nd->nd_flag |= ND_EXGSSPRIVACY;
2924 * Nfs server psuedo system call for the nfsd's
2930 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
2933 struct nfsd_addsock_args sockarg;
2934 struct nfsd_nfsd_args nfsdarg;
2937 if (uap->flag & NFSSVC_NFSDADDSOCK) {
2938 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
2941 if ((error = fget(td, sockarg.sock, &fp)) != 0) {
2944 if (fp->f_type != DTYPE_SOCKET) {
2948 error = nfsrvd_addsock(fp);
2950 } else if (uap->flag & NFSSVC_NFSDNFSD) {
2951 if (uap->argp == NULL)
2953 error = copyin(uap->argp, (caddr_t)&nfsdarg,
2957 error = nfsrvd_nfsd(td, &nfsdarg);
2959 error = nfssvc_srvcall(td, uap, td->td_ucred);
2965 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
2967 struct nfsex_args export;
2968 struct file *fp = NULL;
2970 struct nfsd_clid adminrevoke;
2971 struct nfsd_dumplist dumplist;
2972 struct nfsd_dumpclients *dumpclients;
2973 struct nfsd_dumplocklist dumplocklist;
2974 struct nfsd_dumplocks *dumplocks;
2975 struct nameidata nd;
2979 if (uap->flag & NFSSVC_PUBLICFH) {
2980 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
2981 sizeof (fhandle_t));
2982 error = copyin(uap->argp,
2983 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
2986 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
2987 error = copyin(uap->argp,(caddr_t)&export,
2988 sizeof (struct nfsex_args));
2990 error = nfsrv_v4rootexport(&export, cred, p);
2991 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
2994 } else if (uap->flag & NFSSVC_STABLERESTART) {
2995 error = copyin(uap->argp, (caddr_t)&stablefd,
2998 error = fp_getfvp(p, stablefd, &fp, &vp);
2999 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3001 if (!error && newnfs_numnfsd != 0)
3004 nfsrv_stablefirst.nsf_fp = fp;
3005 nfsrv_setupstable(p);
3007 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3008 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3009 sizeof (struct nfsd_clid));
3011 error = nfsrv_adminrevoke(&adminrevoke, p);
3012 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3013 error = copyin(uap->argp, (caddr_t)&dumplist,
3014 sizeof (struct nfsd_dumplist));
3015 if (!error && (dumplist.ndl_size < 1 ||
3016 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3019 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3020 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3022 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3023 error = copyout(dumpclients,
3024 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3025 free((caddr_t)dumpclients, M_TEMP);
3027 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3028 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3029 sizeof (struct nfsd_dumplocklist));
3030 if (!error && (dumplocklist.ndllck_size < 1 ||
3031 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3034 error = nfsrv_lookupfilename(&nd,
3035 dumplocklist.ndllck_fname, p);
3037 len = sizeof (struct nfsd_dumplocks) *
3038 dumplocklist.ndllck_size;
3039 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3041 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3042 dumplocklist.ndllck_size, p);
3044 error = copyout(dumplocks,
3045 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3046 free((caddr_t)dumplocks, M_TEMP);
3054 * Returns 0 if ok, 1 otherwise.
3057 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3062 * This seems odd, but allow the case where the security flavor
3063 * list is empty. This happens when NFSv4 is traversing non-exported
3064 * file systems. Exported file systems should always have a non-empty
3065 * security flavor list.
3067 if (exp->nes_numsecflavor == 0)
3070 for (i = 0; i < exp->nes_numsecflavor; i++) {
3072 * The tests for privacy and integrity must be first,
3073 * since ND_GSS is set for everything but AUTH_SYS.
3075 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3076 (nd->nd_flag & ND_GSSPRIVACY))
3078 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3079 (nd->nd_flag & ND_GSSINTEGRITY))
3081 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3082 (nd->nd_flag & ND_GSS))
3084 if (exp->nes_secflavors[i] == AUTH_SYS &&
3085 (nd->nd_flag & ND_GSS) == 0)
3092 * Calculate a hash value for the fid in a file handle.
3095 nfsrv_hashfh(fhandle_t *fhp)
3099 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3103 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3106 * Called once to initialize data structures...
3109 nfsd_modevent(module_t mod, int type, void *data)
3112 static int loaded = 0;
3119 mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF);
3120 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3121 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3123 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3130 #ifdef VV_DISABLEDELEG
3131 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3132 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3134 nfsd_call_servertimer = nfsrv_servertimer;
3135 nfsd_call_nfsd = nfssvc_nfsd;
3140 if (newnfs_numnfsd != 0) {
3145 #ifdef VV_DISABLEDELEG
3146 vn_deleg_ops.vndeleg_recall = NULL;
3147 vn_deleg_ops.vndeleg_disable = NULL;
3149 nfsd_call_servertimer = NULL;
3150 nfsd_call_nfsd = NULL;
3151 /* and get rid of the locks */
3152 mtx_destroy(&nfs_cache_mutex);
3153 mtx_destroy(&nfs_v4root_mutex);
3154 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3155 lockdestroy(&nfsv4root_mnt.mnt_explock);
3164 static moduledata_t nfsd_mod = {
3169 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3171 /* So that loader and kldload(2) can find us, wherever we are.. */
3172 MODULE_VERSION(nfsd, 1);
3173 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3174 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3175 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3176 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);