nfs_nfsdport.c revision 260173
1191783Srmacklem/*- 2191783Srmacklem * Copyright (c) 1989, 1993 3191783Srmacklem * The Regents of the University of California. All rights reserved. 4191783Srmacklem * 5191783Srmacklem * This code is derived from software contributed to Berkeley by 6191783Srmacklem * Rick Macklem at The University of Guelph. 7191783Srmacklem * 8191783Srmacklem * Redistribution and use in source and binary forms, with or without 9191783Srmacklem * modification, are permitted provided that the following conditions 10191783Srmacklem * are met: 11191783Srmacklem * 1. Redistributions of source code must retain the above copyright 12191783Srmacklem * notice, this list of conditions and the following disclaimer. 13191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright 14191783Srmacklem * notice, this list of conditions and the following disclaimer in the 15191783Srmacklem * documentation and/or other materials provided with the distribution. 16191783Srmacklem * 4. Neither the name of the University nor the names of its contributors 17191783Srmacklem * may be used to endorse or promote products derived from this software 18191783Srmacklem * without specific prior written permission. 19191783Srmacklem * 20191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23191783Srmacklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30191783Srmacklem * SUCH DAMAGE. 31191783Srmacklem * 32191783Srmacklem */ 33191783Srmacklem 34191783Srmacklem#include <sys/cdefs.h> 35191783Srmacklem__FBSDID("$FreeBSD: stable/9/sys/fs/nfsserver/nfs_nfsdport.c 260173 2014-01-01 22:43:16Z rmacklem $"); 36191783Srmacklem 37224778Srwatson#include <sys/capability.h> 38224778Srwatson 39191783Srmacklem/* 40191783Srmacklem * Functions that perform the vfs operations required by the routines in 41191783Srmacklem * nfsd_serv.c. It is hoped that this change will make the server more 42191783Srmacklem * portable. 43191783Srmacklem */ 44191783Srmacklem 45191783Srmacklem#include <fs/nfs/nfsport.h> 46214255Srmacklem#include <sys/hash.h> 47191783Srmacklem#include <sys/sysctl.h> 48192503Srmacklem#include <nlm/nlm_prot.h> 49192503Srmacklem#include <nlm/nlm.h> 50191783Srmacklem 51219028SnetchildFEATURE(nfsd, "NFSv4 server"); 52219028Snetchild 53191783Srmacklemextern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1; 54191783Srmacklemextern int nfsrv_useacl; 55191783Srmacklemextern int newnfs_numnfsd; 56191783Srmacklemextern struct mount nfsv4root_mnt; 57191783Srmacklemextern struct nfsrv_stablefirst nfsrv_stablefirst; 58191783Srmacklemextern void (*nfsd_call_servertimer)(void); 59220530Srmacklemextern SVCPOOL *nfsrvd_pool; 60243738Srmacklemextern struct nfsv4lock nfsd_suspend_lock; 61191783Srmacklemstruct vfsoptlist nfsv4root_opt, nfsv4root_newopt; 62191783SrmacklemNFSDLOCKMUTEX; 63255532Srmacklemstruct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE]; 64255532Srmacklemstruct mtx nfsrc_udpmtx; 65191783Srmacklemstruct mtx nfs_v4root_mutex; 66191783Srmacklemstruct nfsrvfh nfs_rootfh, nfs_pubfh; 67191783Srmacklemint nfs_pubfhset = 0, nfs_rootfhset = 0; 68217432Srmacklemstruct proc *nfsd_master_proc = NULL; 69217432Srmacklemstatic pid_t nfsd_master_pid = (pid_t)-1; 70217432Srmacklemstatic char nfsd_master_comm[MAXCOMLEN + 1]; 71217432Srmacklemstatic struct timeval nfsd_master_start; 72192503Srmacklemstatic uint32_t nfsv4_sysid = 0; 73191783Srmacklem 74192255Srmacklemstatic int nfssvc_srvcall(struct thread *, struct nfssvc_args *, 75192255Srmacklem struct ucred *); 76191783Srmacklem 77220645Srmacklemint nfsrv_enable_crossmntpt = 1; 78191783Srmacklemstatic int nfs_commit_blks; 79191783Srmacklemstatic int nfs_commit_miss; 80191783Srmacklemextern int nfsrv_issuedelegs; 81192255Srmacklemextern int nfsrv_dolocallocks; 82192255Srmacklem 83221615SrmacklemSYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server"); 84221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW, 85220645Srmacklem &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points"); 86221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 87192255Srmacklem 0, ""); 88221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 89192255Srmacklem 0, ""); 90221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW, 91192255Srmacklem &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 92221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW, 93192255Srmacklem &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 94191783Srmacklem 95229617Sjhb#define MAX_REORDERED_RPC 16 96229617Sjhb#define NUM_HEURISTIC 1031 97191783Srmacklem#define NHUSE_INIT 64 98191783Srmacklem#define NHUSE_INC 16 99191783Srmacklem#define NHUSE_MAX 2048 100191783Srmacklem 101191783Srmacklemstatic struct nfsheur { 102191783Srmacklem struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 103229617Sjhb off_t nh_nextoff; /* next offset for sequential detection */ 104191783Srmacklem int nh_use; /* use count for selection */ 105191783Srmacklem int nh_seqcount; /* heuristic */ 106191783Srmacklem} nfsheur[NUM_HEURISTIC]; 107191783Srmacklem 108191783Srmacklem 109191783Srmacklem/* 110229617Sjhb * Heuristic to detect sequential operation. 111229617Sjhb */ 112229617Sjhbstatic struct nfsheur * 113229617Sjhbnfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp) 114229617Sjhb{ 115229617Sjhb struct nfsheur *nh; 116229617Sjhb int hi, try; 117229617Sjhb 118229617Sjhb /* Locate best candidate. */ 119229617Sjhb try = 32; 120229617Sjhb hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 121229617Sjhb nh = &nfsheur[hi]; 122229617Sjhb while (try--) { 123229617Sjhb if (nfsheur[hi].nh_vp == vp) { 124229617Sjhb nh = &nfsheur[hi]; 125229617Sjhb break; 126229617Sjhb } 127229617Sjhb if (nfsheur[hi].nh_use > 0) 128229617Sjhb --nfsheur[hi].nh_use; 129229617Sjhb hi = (hi + 1) % NUM_HEURISTIC; 130229617Sjhb if (nfsheur[hi].nh_use < nh->nh_use) 131229617Sjhb nh = &nfsheur[hi]; 132229617Sjhb } 133229617Sjhb 134229617Sjhb /* Initialize hint if this is a new file. */ 135229617Sjhb if (nh->nh_vp != vp) { 136229617Sjhb nh->nh_vp = vp; 137229617Sjhb nh->nh_nextoff = uio->uio_offset; 138229617Sjhb nh->nh_use = NHUSE_INIT; 139229617Sjhb if (uio->uio_offset == 0) 140229617Sjhb nh->nh_seqcount = 4; 141229617Sjhb else 142229617Sjhb nh->nh_seqcount = 1; 143229617Sjhb } 144229617Sjhb 145229617Sjhb /* Calculate heuristic. */ 146229617Sjhb if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) || 147229617Sjhb uio->uio_offset == nh->nh_nextoff) { 148229617Sjhb /* See comments in vfs_vnops.c:sequential_heuristic(). */ 149229617Sjhb nh->nh_seqcount += howmany(uio->uio_resid, 16384); 150229617Sjhb if (nh->nh_seqcount > IO_SEQMAX) 151229617Sjhb nh->nh_seqcount = IO_SEQMAX; 152229617Sjhb } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC * 153229617Sjhb imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) { 154229617Sjhb /* Probably a reordered RPC, leave seqcount alone. */ 155229617Sjhb } else if (nh->nh_seqcount > 1) { 156229617Sjhb nh->nh_seqcount /= 2; 157229617Sjhb } else { 158229617Sjhb nh->nh_seqcount = 0; 159229617Sjhb } 160229617Sjhb nh->nh_use += NHUSE_INC; 161229617Sjhb if (nh->nh_use > NHUSE_MAX) 162229617Sjhb nh->nh_use = NHUSE_MAX; 163229617Sjhb return (nh); 164229617Sjhb} 165229617Sjhb 166229617Sjhb/* 167191783Srmacklem * Get attributes into nfsvattr structure. 168191783Srmacklem */ 169191783Srmacklemint 170191783Srmacklemnfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 171216693Srmacklem struct thread *p, int vpislocked) 172191783Srmacklem{ 173191783Srmacklem int error, lockedit = 0; 174191783Srmacklem 175216693Srmacklem if (vpislocked == 0) { 176216693Srmacklem /* 177216693Srmacklem * When vpislocked == 0, the vnode is either exclusively 178216693Srmacklem * locked by this thread or not locked by this thread. 179216693Srmacklem * As such, shared lock it, if not exclusively locked. 180216693Srmacklem */ 181224083Szack if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 182216693Srmacklem lockedit = 1; 183224081Szack NFSVOPLOCK(vp, LK_SHARED | LK_RETRY); 184216693Srmacklem } 185191783Srmacklem } 186191783Srmacklem error = VOP_GETATTR(vp, &nvap->na_vattr, cred); 187216693Srmacklem if (lockedit != 0) 188224082Szack NFSVOPUNLOCK(vp, 0); 189224086Szack 190224086Szack NFSEXITCODE(error); 191191783Srmacklem return (error); 192191783Srmacklem} 193191783Srmacklem 194191783Srmacklem/* 195191783Srmacklem * Get a file handle for a vnode. 196191783Srmacklem */ 197191783Srmacklemint 198191783Srmacklemnfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 199191783Srmacklem{ 200191783Srmacklem int error; 201191783Srmacklem 202191783Srmacklem NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 203191783Srmacklem fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 204191783Srmacklem error = VOP_VPTOFH(vp, &fhp->fh_fid); 205224086Szack 206224086Szack NFSEXITCODE(error); 207191783Srmacklem return (error); 208191783Srmacklem} 209191783Srmacklem 210191783Srmacklem/* 211191783Srmacklem * Perform access checking for vnodes obtained from file handles that would 212191783Srmacklem * refer to files already opened by a Unix client. You cannot just use 213200999Srmacklem * vn_writechk() and VOP_ACCESSX() for two reasons. 214200999Srmacklem * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 215200999Srmacklem * case. 216191783Srmacklem * 2 - The owner is to be given access irrespective of mode bits for some 217191783Srmacklem * operations, so that processes that chmod after opening a file don't 218191783Srmacklem * break. 219191783Srmacklem */ 220191783Srmacklemint 221200999Srmacklemnfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 222200999Srmacklem struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 223200999Srmacklem u_int32_t *supportedtypep) 224191783Srmacklem{ 225191783Srmacklem struct vattr vattr; 226191783Srmacklem int error = 0, getret = 0; 227191783Srmacklem 228216893Srmacklem if (vpislocked == 0) { 229224086Szack if (NFSVOPLOCK(vp, LK_SHARED) != 0) { 230224086Szack error = EPERM; 231224086Szack goto out; 232224086Szack } 233216893Srmacklem } 234191783Srmacklem if (accmode & VWRITE) { 235191783Srmacklem /* Just vn_writechk() changed to check rdonly */ 236191783Srmacklem /* 237191783Srmacklem * Disallow write attempts on read-only file systems; 238191783Srmacklem * unless the file is a socket or a block or character 239191783Srmacklem * device resident on the file system. 240191783Srmacklem */ 241191783Srmacklem if (NFSVNO_EXRDONLY(exp) || 242200999Srmacklem (vp->v_mount->mnt_flag & MNT_RDONLY)) { 243191783Srmacklem switch (vp->v_type) { 244191783Srmacklem case VREG: 245191783Srmacklem case VDIR: 246191783Srmacklem case VLNK: 247216893Srmacklem error = EROFS; 248191783Srmacklem default: 249191783Srmacklem break; 250191783Srmacklem } 251191783Srmacklem } 252191783Srmacklem /* 253191783Srmacklem * If there's shared text associated with 254191783Srmacklem * the inode, try to free it up once. If 255191783Srmacklem * we fail, we can't allow writing. 256191783Srmacklem */ 257244658Skib if (VOP_IS_TEXT(vp) && error == 0) 258216893Srmacklem error = ETXTBSY; 259191783Srmacklem } 260216893Srmacklem if (error != 0) { 261216893Srmacklem if (vpislocked == 0) 262224082Szack NFSVOPUNLOCK(vp, 0); 263224086Szack goto out; 264216893Srmacklem } 265191783Srmacklem 266191783Srmacklem /* 267191783Srmacklem * Should the override still be applied when ACLs are enabled? 268191783Srmacklem */ 269200999Srmacklem error = VOP_ACCESSX(vp, accmode, cred, p); 270200999Srmacklem if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 271200999Srmacklem /* 272200999Srmacklem * Try again with VEXPLICIT_DENY, to see if the test for 273200999Srmacklem * deletion is supported. 274200999Srmacklem */ 275200999Srmacklem error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 276200999Srmacklem if (error == 0) { 277200999Srmacklem if (vp->v_type == VDIR) { 278200999Srmacklem accmode &= ~(VDELETE | VDELETE_CHILD); 279200999Srmacklem accmode |= VWRITE; 280200999Srmacklem error = VOP_ACCESSX(vp, accmode, cred, p); 281200999Srmacklem } else if (supportedtypep != NULL) { 282200999Srmacklem *supportedtypep &= ~NFSACCESS_DELETE; 283200999Srmacklem } 284200999Srmacklem } 285200999Srmacklem } 286191783Srmacklem 287191783Srmacklem /* 288191783Srmacklem * Allow certain operations for the owner (reads and writes 289191783Srmacklem * on files that are already open). 290191783Srmacklem */ 291191783Srmacklem if (override != NFSACCCHK_NOOVERRIDE && 292191783Srmacklem (error == EPERM || error == EACCES)) { 293191783Srmacklem if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 294191783Srmacklem error = 0; 295191783Srmacklem else if (override & NFSACCCHK_ALLOWOWNER) { 296191783Srmacklem getret = VOP_GETATTR(vp, &vattr, cred); 297191783Srmacklem if (getret == 0 && cred->cr_uid == vattr.va_uid) 298191783Srmacklem error = 0; 299191783Srmacklem } 300191783Srmacklem } 301191783Srmacklem if (vpislocked == 0) 302224082Szack NFSVOPUNLOCK(vp, 0); 303224086Szack 304224086Szackout: 305224086Szack NFSEXITCODE(error); 306191783Srmacklem return (error); 307191783Srmacklem} 308191783Srmacklem 309191783Srmacklem/* 310191783Srmacklem * Set attribute(s) vnop. 311191783Srmacklem */ 312191783Srmacklemint 313191783Srmacklemnfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 314191783Srmacklem struct thread *p, struct nfsexstuff *exp) 315191783Srmacklem{ 316191783Srmacklem int error; 317191783Srmacklem 318191783Srmacklem error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 319224086Szack NFSEXITCODE(error); 320191783Srmacklem return (error); 321191783Srmacklem} 322191783Srmacklem 323191783Srmacklem/* 324232018Srmacklem * Set up nameidata for a lookup() call and do it. 325191783Srmacklem */ 326191783Srmacklemint 327191783Srmacklemnfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 328191783Srmacklem struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 329191783Srmacklem struct vnode **retdirp) 330191783Srmacklem{ 331191783Srmacklem struct componentname *cnp = &ndp->ni_cnd; 332191783Srmacklem int i; 333191783Srmacklem struct iovec aiov; 334191783Srmacklem struct uio auio; 335191783Srmacklem int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 336191783Srmacklem int error = 0, crossmnt; 337191783Srmacklem char *cp; 338191783Srmacklem 339191783Srmacklem *retdirp = NULL; 340191783Srmacklem cnp->cn_nameptr = cnp->cn_pnbuf; 341225356Srmacklem ndp->ni_strictrelative = 0; 342191783Srmacklem /* 343191783Srmacklem * Extract and set starting directory. 344191783Srmacklem */ 345191783Srmacklem if (dp->v_type != VDIR) { 346191783Srmacklem if (islocked) 347191783Srmacklem vput(dp); 348191783Srmacklem else 349191783Srmacklem vrele(dp); 350191783Srmacklem nfsvno_relpathbuf(ndp); 351224086Szack error = ENOTDIR; 352224086Szack goto out1; 353191783Srmacklem } 354191783Srmacklem if (islocked) 355224080Szack NFSVOPUNLOCK(dp, 0); 356191783Srmacklem VREF(dp); 357191783Srmacklem *retdirp = dp; 358191783Srmacklem if (NFSVNO_EXRDONLY(exp)) 359191783Srmacklem cnp->cn_flags |= RDONLY; 360191783Srmacklem ndp->ni_segflg = UIO_SYSSPACE; 361191783Srmacklem crossmnt = 1; 362191783Srmacklem 363191783Srmacklem if (nd->nd_flag & ND_PUBLOOKUP) { 364191783Srmacklem ndp->ni_loopcnt = 0; 365191783Srmacklem if (cnp->cn_pnbuf[0] == '/') { 366191783Srmacklem vrele(dp); 367191783Srmacklem /* 368191783Srmacklem * Check for degenerate pathnames here, since lookup() 369191783Srmacklem * panics on them. 370191783Srmacklem */ 371191783Srmacklem for (i = 1; i < ndp->ni_pathlen; i++) 372191783Srmacklem if (cnp->cn_pnbuf[i] != '/') 373191783Srmacklem break; 374191783Srmacklem if (i == ndp->ni_pathlen) { 375191783Srmacklem error = NFSERR_ACCES; 376191783Srmacklem goto out; 377191783Srmacklem } 378191783Srmacklem dp = rootvnode; 379191783Srmacklem VREF(dp); 380191783Srmacklem } 381220645Srmacklem } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 382191783Srmacklem (nd->nd_flag & ND_NFSV4) == 0) { 383191783Srmacklem /* 384191783Srmacklem * Only cross mount points for NFSv4 when doing a 385191783Srmacklem * mount while traversing the file system above 386220645Srmacklem * the mount point, unless nfsrv_enable_crossmntpt is set. 387191783Srmacklem */ 388191783Srmacklem cnp->cn_flags |= NOCROSSMOUNT; 389191783Srmacklem crossmnt = 0; 390191783Srmacklem } 391191783Srmacklem 392191783Srmacklem /* 393191783Srmacklem * Initialize for scan, set ni_startdir and bump ref on dp again 394191783Srmacklem * becuase lookup() will dereference ni_startdir. 395191783Srmacklem */ 396191783Srmacklem 397191783Srmacklem cnp->cn_thread = p; 398191783Srmacklem ndp->ni_startdir = dp; 399191783Srmacklem ndp->ni_rootdir = rootvnode; 400232683Srmacklem ndp->ni_topdir = NULL; 401191783Srmacklem 402191783Srmacklem if (!lockleaf) 403191783Srmacklem cnp->cn_flags |= LOCKLEAF; 404191783Srmacklem for (;;) { 405191783Srmacklem cnp->cn_nameptr = cnp->cn_pnbuf; 406191783Srmacklem /* 407191783Srmacklem * Call lookup() to do the real work. If an error occurs, 408191783Srmacklem * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 409191783Srmacklem * we do not have to dereference anything before returning. 410191783Srmacklem * In either case ni_startdir will be dereferenced and NULLed 411191783Srmacklem * out. 412191783Srmacklem */ 413191783Srmacklem error = lookup(ndp); 414191783Srmacklem if (error) 415191783Srmacklem break; 416191783Srmacklem 417191783Srmacklem /* 418191783Srmacklem * Check for encountering a symbolic link. Trivial 419191783Srmacklem * termination occurs if no symlink encountered. 420191783Srmacklem */ 421191783Srmacklem if ((cnp->cn_flags & ISSYMLINK) == 0) { 422191783Srmacklem if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 423191783Srmacklem nfsvno_relpathbuf(ndp); 424191783Srmacklem if (ndp->ni_vp && !lockleaf) 425224080Szack NFSVOPUNLOCK(ndp->ni_vp, 0); 426191783Srmacklem break; 427191783Srmacklem } 428191783Srmacklem 429191783Srmacklem /* 430191783Srmacklem * Validate symlink 431191783Srmacklem */ 432191783Srmacklem if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 433224080Szack NFSVOPUNLOCK(ndp->ni_dvp, 0); 434191783Srmacklem if (!(nd->nd_flag & ND_PUBLOOKUP)) { 435191783Srmacklem error = EINVAL; 436191783Srmacklem goto badlink2; 437191783Srmacklem } 438191783Srmacklem 439191783Srmacklem if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 440191783Srmacklem error = ELOOP; 441191783Srmacklem goto badlink2; 442191783Srmacklem } 443191783Srmacklem if (ndp->ni_pathlen > 1) 444191783Srmacklem cp = uma_zalloc(namei_zone, M_WAITOK); 445191783Srmacklem else 446191783Srmacklem cp = cnp->cn_pnbuf; 447191783Srmacklem aiov.iov_base = cp; 448191783Srmacklem aiov.iov_len = MAXPATHLEN; 449191783Srmacklem auio.uio_iov = &aiov; 450191783Srmacklem auio.uio_iovcnt = 1; 451191783Srmacklem auio.uio_offset = 0; 452191783Srmacklem auio.uio_rw = UIO_READ; 453191783Srmacklem auio.uio_segflg = UIO_SYSSPACE; 454191783Srmacklem auio.uio_td = NULL; 455191783Srmacklem auio.uio_resid = MAXPATHLEN; 456191783Srmacklem error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 457191783Srmacklem if (error) { 458191783Srmacklem badlink1: 459191783Srmacklem if (ndp->ni_pathlen > 1) 460191783Srmacklem uma_zfree(namei_zone, cp); 461191783Srmacklem badlink2: 462191783Srmacklem vrele(ndp->ni_dvp); 463191783Srmacklem vput(ndp->ni_vp); 464191783Srmacklem break; 465191783Srmacklem } 466191783Srmacklem linklen = MAXPATHLEN - auio.uio_resid; 467191783Srmacklem if (linklen == 0) { 468191783Srmacklem error = ENOENT; 469191783Srmacklem goto badlink1; 470191783Srmacklem } 471191783Srmacklem if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 472191783Srmacklem error = ENAMETOOLONG; 473191783Srmacklem goto badlink1; 474191783Srmacklem } 475191783Srmacklem 476191783Srmacklem /* 477191783Srmacklem * Adjust or replace path 478191783Srmacklem */ 479191783Srmacklem if (ndp->ni_pathlen > 1) { 480191783Srmacklem NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 481191783Srmacklem uma_zfree(namei_zone, cnp->cn_pnbuf); 482191783Srmacklem cnp->cn_pnbuf = cp; 483191783Srmacklem } else 484191783Srmacklem cnp->cn_pnbuf[linklen] = '\0'; 485191783Srmacklem ndp->ni_pathlen += linklen; 486191783Srmacklem 487191783Srmacklem /* 488191783Srmacklem * Cleanup refs for next loop and check if root directory 489191783Srmacklem * should replace current directory. Normally ni_dvp 490191783Srmacklem * becomes the new base directory and is cleaned up when 491191783Srmacklem * we loop. Explicitly null pointers after invalidation 492191783Srmacklem * to clarify operation. 493191783Srmacklem */ 494191783Srmacklem vput(ndp->ni_vp); 495191783Srmacklem ndp->ni_vp = NULL; 496191783Srmacklem 497191783Srmacklem if (cnp->cn_pnbuf[0] == '/') { 498191783Srmacklem vrele(ndp->ni_dvp); 499191783Srmacklem ndp->ni_dvp = ndp->ni_rootdir; 500191783Srmacklem VREF(ndp->ni_dvp); 501191783Srmacklem } 502191783Srmacklem ndp->ni_startdir = ndp->ni_dvp; 503191783Srmacklem ndp->ni_dvp = NULL; 504191783Srmacklem } 505191783Srmacklem if (!lockleaf) 506191783Srmacklem cnp->cn_flags &= ~LOCKLEAF; 507191783Srmacklem 508191783Srmacklemout: 509191783Srmacklem if (error) { 510191783Srmacklem uma_zfree(namei_zone, cnp->cn_pnbuf); 511191783Srmacklem ndp->ni_vp = NULL; 512191783Srmacklem ndp->ni_dvp = NULL; 513191783Srmacklem ndp->ni_startdir = NULL; 514191783Srmacklem cnp->cn_flags &= ~HASBUF; 515191783Srmacklem } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 516191783Srmacklem ndp->ni_dvp = NULL; 517191783Srmacklem } 518224086Szack 519224086Szackout1: 520224086Szack NFSEXITCODE2(error, nd); 521191783Srmacklem return (error); 522191783Srmacklem} 523191783Srmacklem 524191783Srmacklem/* 525191783Srmacklem * Set up a pathname buffer and return a pointer to it and, optionally 526191783Srmacklem * set a hash pointer. 527191783Srmacklem */ 528191783Srmacklemvoid 529191783Srmacklemnfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 530191783Srmacklem{ 531191783Srmacklem struct componentname *cnp = &ndp->ni_cnd; 532191783Srmacklem 533191783Srmacklem cnp->cn_flags |= (NOMACCHECK | HASBUF); 534191783Srmacklem cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 535191783Srmacklem if (hashpp != NULL) 536191783Srmacklem *hashpp = NULL; 537191783Srmacklem *bufpp = cnp->cn_pnbuf; 538191783Srmacklem} 539191783Srmacklem 540191783Srmacklem/* 541191783Srmacklem * Release the above path buffer, if not released by nfsvno_namei(). 542191783Srmacklem */ 543191783Srmacklemvoid 544191783Srmacklemnfsvno_relpathbuf(struct nameidata *ndp) 545191783Srmacklem{ 546191783Srmacklem 547191783Srmacklem if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 548191783Srmacklem panic("nfsrelpath"); 549191783Srmacklem uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 550191783Srmacklem ndp->ni_cnd.cn_flags &= ~HASBUF; 551191783Srmacklem} 552191783Srmacklem 553191783Srmacklem/* 554191783Srmacklem * Readlink vnode op into an mbuf list. 555191783Srmacklem */ 556191783Srmacklemint 557191783Srmacklemnfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 558191783Srmacklem struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 559191783Srmacklem{ 560191783Srmacklem struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 561191783Srmacklem struct iovec *ivp = iv; 562191783Srmacklem struct uio io, *uiop = &io; 563191783Srmacklem struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 564224086Szack int i, len, tlen, error = 0; 565191783Srmacklem 566191783Srmacklem len = 0; 567191783Srmacklem i = 0; 568191783Srmacklem while (len < NFS_MAXPATHLEN) { 569191783Srmacklem NFSMGET(mp); 570191783Srmacklem MCLGET(mp, M_WAIT); 571191783Srmacklem mp->m_len = NFSMSIZ(mp); 572191783Srmacklem if (len == 0) { 573191783Srmacklem mp3 = mp2 = mp; 574191783Srmacklem } else { 575191783Srmacklem mp2->m_next = mp; 576191783Srmacklem mp2 = mp; 577191783Srmacklem } 578191783Srmacklem if ((len + mp->m_len) > NFS_MAXPATHLEN) { 579191783Srmacklem mp->m_len = NFS_MAXPATHLEN - len; 580191783Srmacklem len = NFS_MAXPATHLEN; 581191783Srmacklem } else { 582191783Srmacklem len += mp->m_len; 583191783Srmacklem } 584191783Srmacklem ivp->iov_base = mtod(mp, caddr_t); 585191783Srmacklem ivp->iov_len = mp->m_len; 586191783Srmacklem i++; 587191783Srmacklem ivp++; 588191783Srmacklem } 589191783Srmacklem uiop->uio_iov = iv; 590191783Srmacklem uiop->uio_iovcnt = i; 591191783Srmacklem uiop->uio_offset = 0; 592191783Srmacklem uiop->uio_resid = len; 593191783Srmacklem uiop->uio_rw = UIO_READ; 594191783Srmacklem uiop->uio_segflg = UIO_SYSSPACE; 595191783Srmacklem uiop->uio_td = NULL; 596191783Srmacklem error = VOP_READLINK(vp, uiop, cred); 597191783Srmacklem if (error) { 598191783Srmacklem m_freem(mp3); 599191783Srmacklem *lenp = 0; 600224086Szack goto out; 601191783Srmacklem } 602191783Srmacklem if (uiop->uio_resid > 0) { 603191783Srmacklem len -= uiop->uio_resid; 604191783Srmacklem tlen = NFSM_RNDUP(len); 605191783Srmacklem nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 606191783Srmacklem } 607191783Srmacklem *lenp = len; 608191783Srmacklem *mpp = mp3; 609191783Srmacklem *mpendp = mp; 610224086Szack 611224086Szackout: 612224086Szack NFSEXITCODE(error); 613224086Szack return (error); 614191783Srmacklem} 615191783Srmacklem 616191783Srmacklem/* 617191783Srmacklem * Read vnode op call into mbuf list. 618191783Srmacklem */ 619191783Srmacklemint 620191783Srmacklemnfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 621191783Srmacklem struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 622191783Srmacklem{ 623191783Srmacklem struct mbuf *m; 624191783Srmacklem int i; 625191783Srmacklem struct iovec *iv; 626191783Srmacklem struct iovec *iv2; 627229617Sjhb int error = 0, len, left, siz, tlen, ioflag = 0; 628191783Srmacklem struct mbuf *m2 = NULL, *m3; 629191783Srmacklem struct uio io, *uiop = &io; 630191783Srmacklem struct nfsheur *nh; 631191783Srmacklem 632191783Srmacklem len = left = NFSM_RNDUP(cnt); 633191783Srmacklem m3 = NULL; 634191783Srmacklem /* 635191783Srmacklem * Generate the mbuf list with the uio_iov ref. to it. 636191783Srmacklem */ 637191783Srmacklem i = 0; 638191783Srmacklem while (left > 0) { 639191783Srmacklem NFSMGET(m); 640191783Srmacklem MCLGET(m, M_WAIT); 641191783Srmacklem m->m_len = 0; 642191783Srmacklem siz = min(M_TRAILINGSPACE(m), left); 643191783Srmacklem left -= siz; 644191783Srmacklem i++; 645191783Srmacklem if (m3) 646191783Srmacklem m2->m_next = m; 647191783Srmacklem else 648191783Srmacklem m3 = m; 649191783Srmacklem m2 = m; 650191783Srmacklem } 651191783Srmacklem MALLOC(iv, struct iovec *, i * sizeof (struct iovec), 652191783Srmacklem M_TEMP, M_WAITOK); 653191783Srmacklem uiop->uio_iov = iv2 = iv; 654191783Srmacklem m = m3; 655191783Srmacklem left = len; 656191783Srmacklem i = 0; 657191783Srmacklem while (left > 0) { 658191783Srmacklem if (m == NULL) 659191783Srmacklem panic("nfsvno_read iov"); 660191783Srmacklem siz = min(M_TRAILINGSPACE(m), left); 661191783Srmacklem if (siz > 0) { 662191783Srmacklem iv->iov_base = mtod(m, caddr_t) + m->m_len; 663191783Srmacklem iv->iov_len = siz; 664191783Srmacklem m->m_len += siz; 665191783Srmacklem left -= siz; 666191783Srmacklem iv++; 667191783Srmacklem i++; 668191783Srmacklem } 669191783Srmacklem m = m->m_next; 670191783Srmacklem } 671191783Srmacklem uiop->uio_iovcnt = i; 672191783Srmacklem uiop->uio_offset = off; 673191783Srmacklem uiop->uio_resid = len; 674191783Srmacklem uiop->uio_rw = UIO_READ; 675191783Srmacklem uiop->uio_segflg = UIO_SYSSPACE; 676229617Sjhb nh = nfsrv_sequential_heuristic(uiop, vp); 677229617Sjhb ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 678191783Srmacklem error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 679191783Srmacklem FREE((caddr_t)iv2, M_TEMP); 680191783Srmacklem if (error) { 681191783Srmacklem m_freem(m3); 682191783Srmacklem *mpp = NULL; 683224086Szack goto out; 684191783Srmacklem } 685229617Sjhb nh->nh_nextoff = uiop->uio_offset; 686191783Srmacklem tlen = len - uiop->uio_resid; 687191783Srmacklem cnt = cnt < tlen ? cnt : tlen; 688191783Srmacklem tlen = NFSM_RNDUP(cnt); 689191783Srmacklem if (tlen == 0) { 690191783Srmacklem m_freem(m3); 691191783Srmacklem m3 = NULL; 692191783Srmacklem } else if (len != tlen || tlen != cnt) 693191783Srmacklem nfsrv_adj(m3, len - tlen, tlen - cnt); 694191783Srmacklem *mpp = m3; 695191783Srmacklem *mpendp = m2; 696224086Szack 697224086Szackout: 698224086Szack NFSEXITCODE(error); 699224086Szack return (error); 700191783Srmacklem} 701191783Srmacklem 702191783Srmacklem/* 703191783Srmacklem * Write vnode op from an mbuf list. 704191783Srmacklem */ 705191783Srmacklemint 706191783Srmacklemnfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable, 707191783Srmacklem struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 708191783Srmacklem{ 709191783Srmacklem struct iovec *ivp; 710191783Srmacklem int i, len; 711191783Srmacklem struct iovec *iv; 712191783Srmacklem int ioflags, error; 713191783Srmacklem struct uio io, *uiop = &io; 714229617Sjhb struct nfsheur *nh; 715191783Srmacklem 716191783Srmacklem MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP, 717191783Srmacklem M_WAITOK); 718191783Srmacklem uiop->uio_iov = iv = ivp; 719191783Srmacklem uiop->uio_iovcnt = cnt; 720191783Srmacklem i = mtod(mp, caddr_t) + mp->m_len - cp; 721191783Srmacklem len = retlen; 722191783Srmacklem while (len > 0) { 723191783Srmacklem if (mp == NULL) 724191783Srmacklem panic("nfsvno_write"); 725191783Srmacklem if (i > 0) { 726191783Srmacklem i = min(i, len); 727191783Srmacklem ivp->iov_base = cp; 728191783Srmacklem ivp->iov_len = i; 729191783Srmacklem ivp++; 730191783Srmacklem len -= i; 731191783Srmacklem } 732191783Srmacklem mp = mp->m_next; 733191783Srmacklem if (mp) { 734191783Srmacklem i = mp->m_len; 735191783Srmacklem cp = mtod(mp, caddr_t); 736191783Srmacklem } 737191783Srmacklem } 738191783Srmacklem 739191783Srmacklem if (stable == NFSWRITE_UNSTABLE) 740191783Srmacklem ioflags = IO_NODELOCKED; 741191783Srmacklem else 742191783Srmacklem ioflags = (IO_SYNC | IO_NODELOCKED); 743191783Srmacklem uiop->uio_resid = retlen; 744191783Srmacklem uiop->uio_rw = UIO_WRITE; 745191783Srmacklem uiop->uio_segflg = UIO_SYSSPACE; 746191783Srmacklem NFSUIOPROC(uiop, p); 747191783Srmacklem uiop->uio_offset = off; 748229617Sjhb nh = nfsrv_sequential_heuristic(uiop, vp); 749229617Sjhb ioflags |= nh->nh_seqcount << IO_SEQSHIFT; 750191783Srmacklem error = VOP_WRITE(vp, uiop, ioflags, cred); 751229617Sjhb if (error == 0) 752229617Sjhb nh->nh_nextoff = uiop->uio_offset; 753191783Srmacklem FREE((caddr_t)iv, M_TEMP); 754224086Szack 755224086Szack NFSEXITCODE(error); 756191783Srmacklem return (error); 757191783Srmacklem} 758191783Srmacklem 759191783Srmacklem/* 760191783Srmacklem * Common code for creating a regular file (plus special files for V2). 761191783Srmacklem */ 762191783Srmacklemint 763191783Srmacklemnfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 764191783Srmacklem struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 765199616Srmacklem int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp) 766191783Srmacklem{ 767191783Srmacklem u_quad_t tempsize; 768191783Srmacklem int error; 769191783Srmacklem 770191783Srmacklem error = nd->nd_repstat; 771191783Srmacklem if (!error && ndp->ni_vp == NULL) { 772191783Srmacklem if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 773191783Srmacklem vrele(ndp->ni_startdir); 774191783Srmacklem error = VOP_CREATE(ndp->ni_dvp, 775191783Srmacklem &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 776191783Srmacklem vput(ndp->ni_dvp); 777191783Srmacklem nfsvno_relpathbuf(ndp); 778191783Srmacklem if (!error) { 779191783Srmacklem if (*exclusive_flagp) { 780191783Srmacklem *exclusive_flagp = 0; 781191783Srmacklem NFSVNO_ATTRINIT(nvap); 782199616Srmacklem nvap->na_atime.tv_sec = cverf[0]; 783199616Srmacklem nvap->na_atime.tv_nsec = cverf[1]; 784191783Srmacklem error = VOP_SETATTR(ndp->ni_vp, 785191783Srmacklem &nvap->na_vattr, nd->nd_cred); 786191783Srmacklem } 787191783Srmacklem } 788191783Srmacklem /* 789191783Srmacklem * NFS V2 Only. nfsrvd_mknod() does this for V3. 790191783Srmacklem * (This implies, just get out on an error.) 791191783Srmacklem */ 792191783Srmacklem } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 793191783Srmacklem nvap->na_type == VFIFO) { 794191783Srmacklem if (nvap->na_type == VCHR && rdev == 0xffffffff) 795191783Srmacklem nvap->na_type = VFIFO; 796191783Srmacklem if (nvap->na_type != VFIFO && 797191783Srmacklem (error = priv_check_cred(nd->nd_cred, 798191783Srmacklem PRIV_VFS_MKNOD_DEV, 0))) { 799191783Srmacklem vrele(ndp->ni_startdir); 800191783Srmacklem nfsvno_relpathbuf(ndp); 801191783Srmacklem vput(ndp->ni_dvp); 802224086Szack goto out; 803191783Srmacklem } 804191783Srmacklem nvap->na_rdev = rdev; 805191783Srmacklem error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 806191783Srmacklem &ndp->ni_cnd, &nvap->na_vattr); 807191783Srmacklem vput(ndp->ni_dvp); 808191783Srmacklem nfsvno_relpathbuf(ndp); 809220546Srmacklem vrele(ndp->ni_startdir); 810220546Srmacklem if (error) 811224086Szack goto out; 812191783Srmacklem } else { 813191783Srmacklem vrele(ndp->ni_startdir); 814191783Srmacklem nfsvno_relpathbuf(ndp); 815191783Srmacklem vput(ndp->ni_dvp); 816224086Szack error = ENXIO; 817224086Szack goto out; 818191783Srmacklem } 819191783Srmacklem *vpp = ndp->ni_vp; 820191783Srmacklem } else { 821191783Srmacklem /* 822191783Srmacklem * Handle cases where error is already set and/or 823191783Srmacklem * the file exists. 824191783Srmacklem * 1 - clean up the lookup 825191783Srmacklem * 2 - iff !error and na_size set, truncate it 826191783Srmacklem */ 827191783Srmacklem vrele(ndp->ni_startdir); 828191783Srmacklem nfsvno_relpathbuf(ndp); 829191783Srmacklem *vpp = ndp->ni_vp; 830191783Srmacklem if (ndp->ni_dvp == *vpp) 831191783Srmacklem vrele(ndp->ni_dvp); 832191783Srmacklem else 833191783Srmacklem vput(ndp->ni_dvp); 834191783Srmacklem if (!error && nvap->na_size != VNOVAL) { 835200999Srmacklem error = nfsvno_accchk(*vpp, VWRITE, 836191783Srmacklem nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 837200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 838191783Srmacklem if (!error) { 839191783Srmacklem tempsize = nvap->na_size; 840191783Srmacklem NFSVNO_ATTRINIT(nvap); 841191783Srmacklem nvap->na_size = tempsize; 842191783Srmacklem error = VOP_SETATTR(*vpp, 843191783Srmacklem &nvap->na_vattr, nd->nd_cred); 844191783Srmacklem } 845191783Srmacklem } 846191783Srmacklem if (error) 847191783Srmacklem vput(*vpp); 848191783Srmacklem } 849224086Szack 850224086Szackout: 851224086Szack NFSEXITCODE(error); 852191783Srmacklem return (error); 853191783Srmacklem} 854191783Srmacklem 855191783Srmacklem/* 856191783Srmacklem * Do a mknod vnode op. 857191783Srmacklem */ 858191783Srmacklemint 859191783Srmacklemnfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 860191783Srmacklem struct thread *p) 861191783Srmacklem{ 862191783Srmacklem int error = 0; 863191783Srmacklem enum vtype vtyp; 864191783Srmacklem 865191783Srmacklem vtyp = nvap->na_type; 866191783Srmacklem /* 867191783Srmacklem * Iff doesn't exist, create it. 868191783Srmacklem */ 869191783Srmacklem if (ndp->ni_vp) { 870191783Srmacklem vrele(ndp->ni_startdir); 871191783Srmacklem nfsvno_relpathbuf(ndp); 872191783Srmacklem vput(ndp->ni_dvp); 873191783Srmacklem vrele(ndp->ni_vp); 874224086Szack error = EEXIST; 875224086Szack goto out; 876191783Srmacklem } 877191783Srmacklem if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 878191783Srmacklem vrele(ndp->ni_startdir); 879191783Srmacklem nfsvno_relpathbuf(ndp); 880191783Srmacklem vput(ndp->ni_dvp); 881224086Szack error = NFSERR_BADTYPE; 882224086Szack goto out; 883191783Srmacklem } 884191783Srmacklem if (vtyp == VSOCK) { 885191783Srmacklem vrele(ndp->ni_startdir); 886191783Srmacklem error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 887191783Srmacklem &ndp->ni_cnd, &nvap->na_vattr); 888191783Srmacklem vput(ndp->ni_dvp); 889191783Srmacklem nfsvno_relpathbuf(ndp); 890191783Srmacklem } else { 891191783Srmacklem if (nvap->na_type != VFIFO && 892191783Srmacklem (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) { 893191783Srmacklem vrele(ndp->ni_startdir); 894191783Srmacklem nfsvno_relpathbuf(ndp); 895191783Srmacklem vput(ndp->ni_dvp); 896224086Szack goto out; 897191783Srmacklem } 898191783Srmacklem error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 899191783Srmacklem &ndp->ni_cnd, &nvap->na_vattr); 900191783Srmacklem vput(ndp->ni_dvp); 901191783Srmacklem nfsvno_relpathbuf(ndp); 902216898Srmacklem vrele(ndp->ni_startdir); 903191783Srmacklem /* 904191783Srmacklem * Since VOP_MKNOD returns the ni_vp, I can't 905191783Srmacklem * see any reason to do the lookup. 906191783Srmacklem */ 907191783Srmacklem } 908224086Szack 909224086Szackout: 910224086Szack NFSEXITCODE(error); 911191783Srmacklem return (error); 912191783Srmacklem} 913191783Srmacklem 914191783Srmacklem/* 915191783Srmacklem * Mkdir vnode op. 916191783Srmacklem */ 917191783Srmacklemint 918191783Srmacklemnfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 919191783Srmacklem struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 920191783Srmacklem{ 921191783Srmacklem int error = 0; 922191783Srmacklem 923191783Srmacklem if (ndp->ni_vp != NULL) { 924191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 925191783Srmacklem vrele(ndp->ni_dvp); 926191783Srmacklem else 927191783Srmacklem vput(ndp->ni_dvp); 928191783Srmacklem vrele(ndp->ni_vp); 929206063Srmacklem nfsvno_relpathbuf(ndp); 930224086Szack error = EEXIST; 931224086Szack goto out; 932191783Srmacklem } 933191783Srmacklem error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 934191783Srmacklem &nvap->na_vattr); 935191783Srmacklem vput(ndp->ni_dvp); 936191783Srmacklem nfsvno_relpathbuf(ndp); 937224086Szack 938224086Szackout: 939224086Szack NFSEXITCODE(error); 940191783Srmacklem return (error); 941191783Srmacklem} 942191783Srmacklem 943191783Srmacklem/* 944191783Srmacklem * symlink vnode op. 945191783Srmacklem */ 946191783Srmacklemint 947191783Srmacklemnfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 948191783Srmacklem int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 949191783Srmacklem struct nfsexstuff *exp) 950191783Srmacklem{ 951191783Srmacklem int error = 0; 952191783Srmacklem 953191783Srmacklem if (ndp->ni_vp) { 954191783Srmacklem vrele(ndp->ni_startdir); 955191783Srmacklem nfsvno_relpathbuf(ndp); 956191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 957191783Srmacklem vrele(ndp->ni_dvp); 958191783Srmacklem else 959191783Srmacklem vput(ndp->ni_dvp); 960191783Srmacklem vrele(ndp->ni_vp); 961224086Szack error = EEXIST; 962224086Szack goto out; 963191783Srmacklem } 964191783Srmacklem 965191783Srmacklem error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 966191783Srmacklem &nvap->na_vattr, pathcp); 967191783Srmacklem vput(ndp->ni_dvp); 968191783Srmacklem vrele(ndp->ni_startdir); 969191783Srmacklem nfsvno_relpathbuf(ndp); 970191783Srmacklem /* 971191783Srmacklem * Although FreeBSD still had the lookup code in 972191783Srmacklem * it for 7/current, there doesn't seem to be any 973191783Srmacklem * point, since VOP_SYMLINK() returns the ni_vp. 974191783Srmacklem * Just vput it for v2. 975191783Srmacklem */ 976191783Srmacklem if (!not_v2 && !error) 977191783Srmacklem vput(ndp->ni_vp); 978224086Szack 979224086Szackout: 980224086Szack NFSEXITCODE(error); 981191783Srmacklem return (error); 982191783Srmacklem} 983191783Srmacklem 984191783Srmacklem/* 985191783Srmacklem * Parse symbolic link arguments. 986191783Srmacklem * This function has an ugly side effect. It will MALLOC() an area for 987191783Srmacklem * the symlink and set iov_base to point to it, only if it succeeds. 988191783Srmacklem * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 989191783Srmacklem * be FREE'd later. 990191783Srmacklem */ 991191783Srmacklemint 992191783Srmacklemnfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 993191783Srmacklem struct thread *p, char **pathcpp, int *lenp) 994191783Srmacklem{ 995191783Srmacklem u_int32_t *tl; 996191783Srmacklem char *pathcp = NULL; 997191783Srmacklem int error = 0, len; 998191783Srmacklem struct nfsv2_sattr *sp; 999191783Srmacklem 1000191783Srmacklem *pathcpp = NULL; 1001191783Srmacklem *lenp = 0; 1002191783Srmacklem if ((nd->nd_flag & ND_NFSV3) && 1003191783Srmacklem (error = nfsrv_sattr(nd, nvap, NULL, NULL, p))) 1004191783Srmacklem goto nfsmout; 1005191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1006191783Srmacklem len = fxdr_unsigned(int, *tl); 1007191783Srmacklem if (len > NFS_MAXPATHLEN || len <= 0) { 1008191783Srmacklem error = EBADRPC; 1009191783Srmacklem goto nfsmout; 1010191783Srmacklem } 1011191783Srmacklem MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK); 1012191783Srmacklem error = nfsrv_mtostr(nd, pathcp, len); 1013191783Srmacklem if (error) 1014191783Srmacklem goto nfsmout; 1015191783Srmacklem if (nd->nd_flag & ND_NFSV2) { 1016191783Srmacklem NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1017191783Srmacklem nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 1018191783Srmacklem } 1019191783Srmacklem *pathcpp = pathcp; 1020191783Srmacklem *lenp = len; 1021224086Szack NFSEXITCODE2(0, nd); 1022191783Srmacklem return (0); 1023191783Srmacklemnfsmout: 1024191783Srmacklem if (pathcp) 1025191783Srmacklem free(pathcp, M_TEMP); 1026224086Szack NFSEXITCODE2(error, nd); 1027191783Srmacklem return (error); 1028191783Srmacklem} 1029191783Srmacklem 1030191783Srmacklem/* 1031191783Srmacklem * Remove a non-directory object. 1032191783Srmacklem */ 1033191783Srmacklemint 1034191783Srmacklemnfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1035191783Srmacklem struct thread *p, struct nfsexstuff *exp) 1036191783Srmacklem{ 1037191783Srmacklem struct vnode *vp; 1038191783Srmacklem int error = 0; 1039191783Srmacklem 1040191783Srmacklem vp = ndp->ni_vp; 1041191783Srmacklem if (vp->v_type == VDIR) 1042191783Srmacklem error = NFSERR_ISDIR; 1043191783Srmacklem else if (is_v4) 1044191783Srmacklem error = nfsrv_checkremove(vp, 1, p); 1045191783Srmacklem if (!error) 1046191783Srmacklem error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 1047191783Srmacklem if (ndp->ni_dvp == vp) 1048191783Srmacklem vrele(ndp->ni_dvp); 1049191783Srmacklem else 1050191783Srmacklem vput(ndp->ni_dvp); 1051191783Srmacklem vput(vp); 1052236134Srmacklem if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1053236134Srmacklem nfsvno_relpathbuf(ndp); 1054224086Szack NFSEXITCODE(error); 1055191783Srmacklem return (error); 1056191783Srmacklem} 1057191783Srmacklem 1058191783Srmacklem/* 1059191783Srmacklem * Remove a directory. 1060191783Srmacklem */ 1061191783Srmacklemint 1062191783Srmacklemnfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1063191783Srmacklem struct thread *p, struct nfsexstuff *exp) 1064191783Srmacklem{ 1065191783Srmacklem struct vnode *vp; 1066191783Srmacklem int error = 0; 1067191783Srmacklem 1068191783Srmacklem vp = ndp->ni_vp; 1069191783Srmacklem if (vp->v_type != VDIR) { 1070191783Srmacklem error = ENOTDIR; 1071191783Srmacklem goto out; 1072191783Srmacklem } 1073191783Srmacklem /* 1074191783Srmacklem * No rmdir "." please. 1075191783Srmacklem */ 1076191783Srmacklem if (ndp->ni_dvp == vp) { 1077191783Srmacklem error = EINVAL; 1078191783Srmacklem goto out; 1079191783Srmacklem } 1080191783Srmacklem /* 1081191783Srmacklem * The root of a mounted filesystem cannot be deleted. 1082191783Srmacklem */ 1083191783Srmacklem if (vp->v_vflag & VV_ROOT) 1084191783Srmacklem error = EBUSY; 1085191783Srmacklemout: 1086191783Srmacklem if (!error) 1087191783Srmacklem error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1088191783Srmacklem if (ndp->ni_dvp == vp) 1089191783Srmacklem vrele(ndp->ni_dvp); 1090191783Srmacklem else 1091191783Srmacklem vput(ndp->ni_dvp); 1092191783Srmacklem vput(vp); 1093236134Srmacklem if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1094236134Srmacklem nfsvno_relpathbuf(ndp); 1095224086Szack NFSEXITCODE(error); 1096191783Srmacklem return (error); 1097191783Srmacklem} 1098191783Srmacklem 1099191783Srmacklem/* 1100191783Srmacklem * Rename vnode op. 1101191783Srmacklem */ 1102191783Srmacklemint 1103191783Srmacklemnfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1104191783Srmacklem u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1105191783Srmacklem{ 1106191783Srmacklem struct vnode *fvp, *tvp, *tdvp; 1107191783Srmacklem int error = 0; 1108191783Srmacklem 1109191783Srmacklem fvp = fromndp->ni_vp; 1110191783Srmacklem if (ndstat) { 1111191783Srmacklem vrele(fromndp->ni_dvp); 1112191783Srmacklem vrele(fvp); 1113191783Srmacklem error = ndstat; 1114191783Srmacklem goto out1; 1115191783Srmacklem } 1116191783Srmacklem tdvp = tondp->ni_dvp; 1117191783Srmacklem tvp = tondp->ni_vp; 1118191783Srmacklem if (tvp != NULL) { 1119191783Srmacklem if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1120191783Srmacklem error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1121191783Srmacklem goto out; 1122191783Srmacklem } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1123191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1124191783Srmacklem goto out; 1125191783Srmacklem } 1126191783Srmacklem if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1127191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1128191783Srmacklem goto out; 1129191783Srmacklem } 1130191783Srmacklem 1131191783Srmacklem /* 1132191783Srmacklem * A rename to '.' or '..' results in a prematurely 1133191783Srmacklem * unlocked vnode on FreeBSD5, so I'm just going to fail that 1134191783Srmacklem * here. 1135191783Srmacklem */ 1136191783Srmacklem if ((tondp->ni_cnd.cn_namelen == 1 && 1137191783Srmacklem tondp->ni_cnd.cn_nameptr[0] == '.') || 1138191783Srmacklem (tondp->ni_cnd.cn_namelen == 2 && 1139191783Srmacklem tondp->ni_cnd.cn_nameptr[0] == '.' && 1140191783Srmacklem tondp->ni_cnd.cn_nameptr[1] == '.')) { 1141191783Srmacklem error = EINVAL; 1142191783Srmacklem goto out; 1143191783Srmacklem } 1144191783Srmacklem } 1145191783Srmacklem if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1146191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1147191783Srmacklem goto out; 1148191783Srmacklem } 1149191783Srmacklem if (fvp->v_mount != tdvp->v_mount) { 1150191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1151191783Srmacklem goto out; 1152191783Srmacklem } 1153191783Srmacklem if (fvp == tdvp) { 1154191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1155191783Srmacklem goto out; 1156191783Srmacklem } 1157191783Srmacklem if (fvp == tvp) { 1158191783Srmacklem /* 1159191783Srmacklem * If source and destination are the same, there is nothing to 1160191783Srmacklem * do. Set error to -1 to indicate this. 1161191783Srmacklem */ 1162191783Srmacklem error = -1; 1163191783Srmacklem goto out; 1164191783Srmacklem } 1165191783Srmacklem if (ndflag & ND_NFSV4) { 1166224081Szack if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) { 1167216893Srmacklem error = nfsrv_checkremove(fvp, 0, p); 1168224082Szack NFSVOPUNLOCK(fvp, 0); 1169216893Srmacklem } else 1170216893Srmacklem error = EPERM; 1171191783Srmacklem if (tvp && !error) 1172191783Srmacklem error = nfsrv_checkremove(tvp, 1, p); 1173191783Srmacklem } else { 1174191783Srmacklem /* 1175191783Srmacklem * For NFSv2 and NFSv3, try to get rid of the delegation, so 1176191783Srmacklem * that the NFSv4 client won't be confused by the rename. 1177191783Srmacklem * Since nfsd_recalldelegation() can only be called on an 1178191783Srmacklem * unlocked vnode at this point and fvp is the file that will 1179191783Srmacklem * still exist after the rename, just do fvp. 1180191783Srmacklem */ 1181191783Srmacklem nfsd_recalldelegation(fvp, p); 1182191783Srmacklem } 1183191783Srmacklemout: 1184191783Srmacklem if (!error) { 1185191783Srmacklem error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1186191783Srmacklem &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1187191783Srmacklem &tondp->ni_cnd); 1188191783Srmacklem } else { 1189191783Srmacklem if (tdvp == tvp) 1190191783Srmacklem vrele(tdvp); 1191191783Srmacklem else 1192191783Srmacklem vput(tdvp); 1193191783Srmacklem if (tvp) 1194191783Srmacklem vput(tvp); 1195191783Srmacklem vrele(fromndp->ni_dvp); 1196191783Srmacklem vrele(fvp); 1197191783Srmacklem if (error == -1) 1198191783Srmacklem error = 0; 1199191783Srmacklem } 1200191783Srmacklem vrele(tondp->ni_startdir); 1201191783Srmacklem nfsvno_relpathbuf(tondp); 1202191783Srmacklemout1: 1203191783Srmacklem vrele(fromndp->ni_startdir); 1204191783Srmacklem nfsvno_relpathbuf(fromndp); 1205224086Szack NFSEXITCODE(error); 1206191783Srmacklem return (error); 1207191783Srmacklem} 1208191783Srmacklem 1209191783Srmacklem/* 1210191783Srmacklem * Link vnode op. 1211191783Srmacklem */ 1212191783Srmacklemint 1213191783Srmacklemnfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1214191783Srmacklem struct thread *p, struct nfsexstuff *exp) 1215191783Srmacklem{ 1216191783Srmacklem struct vnode *xp; 1217191783Srmacklem int error = 0; 1218191783Srmacklem 1219191783Srmacklem xp = ndp->ni_vp; 1220191783Srmacklem if (xp != NULL) { 1221191783Srmacklem error = EEXIST; 1222191783Srmacklem } else { 1223191783Srmacklem xp = ndp->ni_dvp; 1224191783Srmacklem if (vp->v_mount != xp->v_mount) 1225191783Srmacklem error = EXDEV; 1226191783Srmacklem } 1227191783Srmacklem if (!error) { 1228224081Szack NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 1229216893Srmacklem if ((vp->v_iflag & VI_DOOMED) == 0) 1230216893Srmacklem error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1231216893Srmacklem else 1232216893Srmacklem error = EPERM; 1233191783Srmacklem if (ndp->ni_dvp == vp) 1234191783Srmacklem vrele(ndp->ni_dvp); 1235191783Srmacklem else 1236191783Srmacklem vput(ndp->ni_dvp); 1237224082Szack NFSVOPUNLOCK(vp, 0); 1238191783Srmacklem } else { 1239191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 1240191783Srmacklem vrele(ndp->ni_dvp); 1241191783Srmacklem else 1242191783Srmacklem vput(ndp->ni_dvp); 1243191783Srmacklem if (ndp->ni_vp) 1244191783Srmacklem vrele(ndp->ni_vp); 1245191783Srmacklem } 1246191783Srmacklem nfsvno_relpathbuf(ndp); 1247224086Szack NFSEXITCODE(error); 1248191783Srmacklem return (error); 1249191783Srmacklem} 1250191783Srmacklem 1251191783Srmacklem/* 1252191783Srmacklem * Do the fsync() appropriate for the commit. 1253191783Srmacklem */ 1254191783Srmacklemint 1255191783Srmacklemnfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1256191783Srmacklem struct thread *td) 1257191783Srmacklem{ 1258191783Srmacklem int error = 0; 1259191783Srmacklem 1260229827Srmacklem /* 1261229827Srmacklem * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of 1262229827Srmacklem * file is done. At this time VOP_FSYNC does not accept offset and 1263229827Srmacklem * byte count parameters so call VOP_FSYNC the whole file for now. 1264229827Srmacklem * The same is true for NFSv4: RFC 3530 Sec. 14.2.3. 1265229827Srmacklem */ 1266229827Srmacklem if (cnt == 0 || cnt > MAX_COMMIT_COUNT) { 1267191783Srmacklem /* 1268191783Srmacklem * Give up and do the whole thing 1269191783Srmacklem */ 1270191783Srmacklem if (vp->v_object && 1271191783Srmacklem (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1272191783Srmacklem VM_OBJECT_LOCK(vp->v_object); 1273191783Srmacklem vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1274191783Srmacklem VM_OBJECT_UNLOCK(vp->v_object); 1275191783Srmacklem } 1276191783Srmacklem error = VOP_FSYNC(vp, MNT_WAIT, td); 1277191783Srmacklem } else { 1278191783Srmacklem /* 1279191783Srmacklem * Locate and synchronously write any buffers that fall 1280191783Srmacklem * into the requested range. Note: we are assuming that 1281191783Srmacklem * f_iosize is a power of 2. 1282191783Srmacklem */ 1283191783Srmacklem int iosize = vp->v_mount->mnt_stat.f_iosize; 1284191783Srmacklem int iomask = iosize - 1; 1285191783Srmacklem struct bufobj *bo; 1286191783Srmacklem daddr_t lblkno; 1287191783Srmacklem 1288191783Srmacklem /* 1289191783Srmacklem * Align to iosize boundry, super-align to page boundry. 1290191783Srmacklem */ 1291191783Srmacklem if (off & iomask) { 1292191783Srmacklem cnt += off & iomask; 1293191783Srmacklem off &= ~(u_quad_t)iomask; 1294191783Srmacklem } 1295191783Srmacklem if (off & PAGE_MASK) { 1296191783Srmacklem cnt += off & PAGE_MASK; 1297191783Srmacklem off &= ~(u_quad_t)PAGE_MASK; 1298191783Srmacklem } 1299191783Srmacklem lblkno = off / iosize; 1300191783Srmacklem 1301191783Srmacklem if (vp->v_object && 1302191783Srmacklem (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1303191783Srmacklem VM_OBJECT_LOCK(vp->v_object); 1304218345Salc vm_object_page_clean(vp->v_object, off, off + cnt, 1305218345Salc OBJPC_SYNC); 1306191783Srmacklem VM_OBJECT_UNLOCK(vp->v_object); 1307191783Srmacklem } 1308191783Srmacklem 1309191783Srmacklem bo = &vp->v_bufobj; 1310191783Srmacklem BO_LOCK(bo); 1311191783Srmacklem while (cnt > 0) { 1312191783Srmacklem struct buf *bp; 1313191783Srmacklem 1314191783Srmacklem /* 1315191783Srmacklem * If we have a buffer and it is marked B_DELWRI we 1316191783Srmacklem * have to lock and write it. Otherwise the prior 1317191783Srmacklem * write is assumed to have already been committed. 1318191783Srmacklem * 1319191783Srmacklem * gbincore() can return invalid buffers now so we 1320191783Srmacklem * have to check that bit as well (though B_DELWRI 1321191783Srmacklem * should not be set if B_INVAL is set there could be 1322191783Srmacklem * a race here since we haven't locked the buffer). 1323191783Srmacklem */ 1324191783Srmacklem if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1325191783Srmacklem if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1326191783Srmacklem LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) { 1327191783Srmacklem BO_LOCK(bo); 1328191783Srmacklem continue; /* retry */ 1329191783Srmacklem } 1330191783Srmacklem if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1331191783Srmacklem B_DELWRI) { 1332191783Srmacklem bremfree(bp); 1333191783Srmacklem bp->b_flags &= ~B_ASYNC; 1334191783Srmacklem bwrite(bp); 1335191783Srmacklem ++nfs_commit_miss; 1336191783Srmacklem } else 1337191783Srmacklem BUF_UNLOCK(bp); 1338191783Srmacklem BO_LOCK(bo); 1339191783Srmacklem } 1340191783Srmacklem ++nfs_commit_blks; 1341191783Srmacklem if (cnt < iosize) 1342191783Srmacklem break; 1343191783Srmacklem cnt -= iosize; 1344191783Srmacklem ++lblkno; 1345191783Srmacklem } 1346191783Srmacklem BO_UNLOCK(bo); 1347191783Srmacklem } 1348224086Szack NFSEXITCODE(error); 1349191783Srmacklem return (error); 1350191783Srmacklem} 1351191783Srmacklem 1352191783Srmacklem/* 1353191783Srmacklem * Statfs vnode op. 1354191783Srmacklem */ 1355191783Srmacklemint 1356191990Sattilionfsvno_statfs(struct vnode *vp, struct statfs *sf) 1357191783Srmacklem{ 1358221517Srmacklem int error; 1359191783Srmacklem 1360221517Srmacklem error = VFS_STATFS(vp->v_mount, sf); 1361221517Srmacklem if (error == 0) { 1362221517Srmacklem /* 1363221517Srmacklem * Since NFS handles these values as unsigned on the 1364221517Srmacklem * wire, there is no way to represent negative values, 1365221517Srmacklem * so set them to 0. Without this, they will appear 1366221517Srmacklem * to be very large positive values for clients like 1367221517Srmacklem * Solaris10. 1368221517Srmacklem */ 1369221517Srmacklem if (sf->f_bavail < 0) 1370221517Srmacklem sf->f_bavail = 0; 1371221517Srmacklem if (sf->f_ffree < 0) 1372221517Srmacklem sf->f_ffree = 0; 1373221517Srmacklem } 1374224086Szack NFSEXITCODE(error); 1375221517Srmacklem return (error); 1376191783Srmacklem} 1377191783Srmacklem 1378191783Srmacklem/* 1379191783Srmacklem * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1380191783Srmacklem * must handle nfsrv_opencheck() calls after any other access checks. 1381191783Srmacklem */ 1382191783Srmacklemvoid 1383191783Srmacklemnfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1384191783Srmacklem nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1385199616Srmacklem int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1386191783Srmacklem NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p, 1387191783Srmacklem struct nfsexstuff *exp, struct vnode **vpp) 1388191783Srmacklem{ 1389191783Srmacklem struct vnode *vp = NULL; 1390191783Srmacklem u_quad_t tempsize; 1391191783Srmacklem struct nfsexstuff nes; 1392191783Srmacklem 1393191783Srmacklem if (ndp->ni_vp == NULL) 1394191783Srmacklem nd->nd_repstat = nfsrv_opencheck(clientid, 1395191783Srmacklem stateidp, stp, NULL, nd, p, nd->nd_repstat); 1396191783Srmacklem if (!nd->nd_repstat) { 1397191783Srmacklem if (ndp->ni_vp == NULL) { 1398191783Srmacklem vrele(ndp->ni_startdir); 1399191783Srmacklem nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1400191783Srmacklem &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1401191783Srmacklem vput(ndp->ni_dvp); 1402191783Srmacklem nfsvno_relpathbuf(ndp); 1403191783Srmacklem if (!nd->nd_repstat) { 1404191783Srmacklem if (*exclusive_flagp) { 1405191783Srmacklem *exclusive_flagp = 0; 1406191783Srmacklem NFSVNO_ATTRINIT(nvap); 1407199616Srmacklem nvap->na_atime.tv_sec = cverf[0]; 1408199616Srmacklem nvap->na_atime.tv_nsec = cverf[1]; 1409191783Srmacklem nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1410191783Srmacklem &nvap->na_vattr, cred); 1411191783Srmacklem } else { 1412191783Srmacklem nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1413191783Srmacklem aclp, p, attrbitp, exp); 1414191783Srmacklem } 1415191783Srmacklem } 1416191783Srmacklem vp = ndp->ni_vp; 1417191783Srmacklem } else { 1418191783Srmacklem if (ndp->ni_startdir) 1419191783Srmacklem vrele(ndp->ni_startdir); 1420191783Srmacklem nfsvno_relpathbuf(ndp); 1421191783Srmacklem vp = ndp->ni_vp; 1422191783Srmacklem if (create == NFSV4OPEN_CREATE) { 1423191783Srmacklem if (ndp->ni_dvp == vp) 1424191783Srmacklem vrele(ndp->ni_dvp); 1425191783Srmacklem else 1426191783Srmacklem vput(ndp->ni_dvp); 1427191783Srmacklem } 1428191783Srmacklem if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1429191783Srmacklem if (ndp->ni_cnd.cn_flags & RDONLY) 1430191783Srmacklem NFSVNO_SETEXRDONLY(&nes); 1431191783Srmacklem else 1432191783Srmacklem NFSVNO_EXINIT(&nes); 1433191783Srmacklem nd->nd_repstat = nfsvno_accchk(vp, 1434200999Srmacklem VWRITE, cred, &nes, p, 1435200999Srmacklem NFSACCCHK_NOOVERRIDE, 1436200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 1437191783Srmacklem nd->nd_repstat = nfsrv_opencheck(clientid, 1438191783Srmacklem stateidp, stp, vp, nd, p, nd->nd_repstat); 1439191783Srmacklem if (!nd->nd_repstat) { 1440191783Srmacklem tempsize = nvap->na_size; 1441191783Srmacklem NFSVNO_ATTRINIT(nvap); 1442191783Srmacklem nvap->na_size = tempsize; 1443191783Srmacklem nd->nd_repstat = VOP_SETATTR(vp, 1444191783Srmacklem &nvap->na_vattr, cred); 1445191783Srmacklem } 1446191783Srmacklem } else if (vp->v_type == VREG) { 1447191783Srmacklem nd->nd_repstat = nfsrv_opencheck(clientid, 1448191783Srmacklem stateidp, stp, vp, nd, p, nd->nd_repstat); 1449191783Srmacklem } 1450191783Srmacklem } 1451191783Srmacklem } else { 1452191783Srmacklem if (ndp->ni_cnd.cn_flags & HASBUF) 1453191783Srmacklem nfsvno_relpathbuf(ndp); 1454191783Srmacklem if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1455191783Srmacklem vrele(ndp->ni_startdir); 1456191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 1457191783Srmacklem vrele(ndp->ni_dvp); 1458191783Srmacklem else 1459191783Srmacklem vput(ndp->ni_dvp); 1460191783Srmacklem if (ndp->ni_vp) 1461191783Srmacklem vput(ndp->ni_vp); 1462191783Srmacklem } 1463191783Srmacklem } 1464191783Srmacklem *vpp = vp; 1465224086Szack 1466224086Szack NFSEXITCODE2(0, nd); 1467191783Srmacklem} 1468191783Srmacklem 1469191783Srmacklem/* 1470191783Srmacklem * Updates the file rev and sets the mtime and ctime 1471191783Srmacklem * to the current clock time, returning the va_filerev and va_Xtime 1472191783Srmacklem * values. 1473191783Srmacklem */ 1474191783Srmacklemvoid 1475191783Srmacklemnfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1476191783Srmacklem struct ucred *cred, struct thread *p) 1477191783Srmacklem{ 1478191783Srmacklem struct vattr va; 1479191783Srmacklem 1480191783Srmacklem VATTR_NULL(&va); 1481247502Sjhb vfs_timestamp(&va.va_mtime); 1482191783Srmacklem (void) VOP_SETATTR(vp, &va, cred); 1483216693Srmacklem (void) nfsvno_getattr(vp, nvap, cred, p, 1); 1484191783Srmacklem} 1485191783Srmacklem 1486191783Srmacklem/* 1487191783Srmacklem * Glue routine to nfsv4_fillattr(). 1488191783Srmacklem */ 1489191783Srmacklemint 1490220645Srmacklemnfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp, 1491191783Srmacklem struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1492220648Srmacklem struct ucred *cred, struct thread *p, int isdgram, int reterr, 1493220648Srmacklem int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno) 1494191783Srmacklem{ 1495191783Srmacklem int error; 1496191783Srmacklem 1497220645Srmacklem error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 1498220648Srmacklem attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 1499220648Srmacklem mounted_on_fileno); 1500224086Szack NFSEXITCODE2(0, nd); 1501191783Srmacklem return (error); 1502191783Srmacklem} 1503191783Srmacklem 1504191783Srmacklem/* Since the Readdir vnode ops vary, put the entire functions in here. */ 1505191783Srmacklem/* 1506191783Srmacklem * nfs readdir service 1507191783Srmacklem * - mallocs what it thinks is enough to read 1508191783Srmacklem * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1509206170Srmacklem * - calls VOP_READDIR() 1510191783Srmacklem * - loops around building the reply 1511191783Srmacklem * if the output generated exceeds count break out of loop 1512191783Srmacklem * The NFSM_CLGET macro is used here so that the reply will be packed 1513191783Srmacklem * tightly in mbuf clusters. 1514191783Srmacklem * - it trims out records with d_fileno == 0 1515191783Srmacklem * this doesn't matter for Unix clients, but they might confuse clients 1516191783Srmacklem * for other os'. 1517191783Srmacklem * - it trims out records with d_type == DT_WHT 1518191783Srmacklem * these cannot be seen through NFS (unless we extend the protocol) 1519191783Srmacklem * The alternate call nfsrvd_readdirplus() does lookups as well. 1520191783Srmacklem * PS: The NFS protocol spec. does not clarify what the "count" byte 1521191783Srmacklem * argument is a count of.. just name strings and file id's or the 1522191783Srmacklem * entire reply rpc or ... 1523191783Srmacklem * I tried just file name and id sizes and it confused the Sun client, 1524191783Srmacklem * so I am using the full rpc size now. The "paranoia.." comment refers 1525191783Srmacklem * to including the status longwords that are not a part of the dir. 1526191783Srmacklem * "entry" structures, but are in the rpc. 1527191783Srmacklem */ 1528191783Srmacklemint 1529191783Srmacklemnfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1530191783Srmacklem struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1531191783Srmacklem{ 1532191783Srmacklem struct dirent *dp; 1533191783Srmacklem u_int32_t *tl; 1534191783Srmacklem int dirlen; 1535191783Srmacklem char *cpos, *cend, *rbuf; 1536191783Srmacklem struct nfsvattr at; 1537191783Srmacklem int nlen, error = 0, getret = 1; 1538191783Srmacklem int siz, cnt, fullsiz, eofflag, ncookies; 1539191783Srmacklem u_int64_t off, toff, verf; 1540191783Srmacklem u_long *cookies = NULL, *cookiep; 1541191783Srmacklem struct uio io; 1542191783Srmacklem struct iovec iv; 1543216691Srmacklem int not_zfs; 1544191783Srmacklem 1545191783Srmacklem if (nd->nd_repstat) { 1546191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1547224086Szack goto out; 1548191783Srmacklem } 1549191783Srmacklem if (nd->nd_flag & ND_NFSV2) { 1550191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1551191783Srmacklem off = fxdr_unsigned(u_quad_t, *tl++); 1552191783Srmacklem } else { 1553191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1554191783Srmacklem off = fxdr_hyper(tl); 1555191783Srmacklem tl += 2; 1556191783Srmacklem verf = fxdr_hyper(tl); 1557191783Srmacklem tl += 2; 1558191783Srmacklem } 1559191783Srmacklem toff = off; 1560191783Srmacklem cnt = fxdr_unsigned(int, *tl); 1561206170Srmacklem if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1562191783Srmacklem cnt = NFS_SRVMAXDATA(nd); 1563191783Srmacklem siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1564191783Srmacklem fullsiz = siz; 1565191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1566191783Srmacklem nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, 1567216693Srmacklem p, 1); 1568191783Srmacklem#if 0 1569191783Srmacklem /* 1570191783Srmacklem * va_filerev is not sufficient as a cookie verifier, 1571191783Srmacklem * since it is not supposed to change when entries are 1572191783Srmacklem * removed/added unless that offset cookies returned to 1573191783Srmacklem * the client are no longer valid. 1574191783Srmacklem */ 1575191783Srmacklem if (!nd->nd_repstat && toff && verf != at.na_filerev) 1576191783Srmacklem nd->nd_repstat = NFSERR_BAD_COOKIE; 1577191783Srmacklem#endif 1578191783Srmacklem } 1579250060Sdes if (!nd->nd_repstat && vp->v_type != VDIR) 1580250060Sdes nd->nd_repstat = NFSERR_NOTDIR; 1581206170Srmacklem if (nd->nd_repstat == 0 && cnt == 0) { 1582206170Srmacklem if (nd->nd_flag & ND_NFSV2) 1583206170Srmacklem /* NFSv2 does not have NFSERR_TOOSMALL */ 1584206170Srmacklem nd->nd_repstat = EPERM; 1585206170Srmacklem else 1586206170Srmacklem nd->nd_repstat = NFSERR_TOOSMALL; 1587206170Srmacklem } 1588191783Srmacklem if (!nd->nd_repstat) 1589200999Srmacklem nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1590191783Srmacklem nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1591200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 1592191783Srmacklem if (nd->nd_repstat) { 1593191783Srmacklem vput(vp); 1594191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1595191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1596224086Szack goto out; 1597191783Srmacklem } 1598216691Srmacklem not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs"); 1599191783Srmacklem MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1600191783Srmacklemagain: 1601191783Srmacklem eofflag = 0; 1602191783Srmacklem if (cookies) { 1603191783Srmacklem free((caddr_t)cookies, M_TEMP); 1604191783Srmacklem cookies = NULL; 1605191783Srmacklem } 1606191783Srmacklem 1607191783Srmacklem iv.iov_base = rbuf; 1608191783Srmacklem iv.iov_len = siz; 1609191783Srmacklem io.uio_iov = &iv; 1610191783Srmacklem io.uio_iovcnt = 1; 1611191783Srmacklem io.uio_offset = (off_t)off; 1612191783Srmacklem io.uio_resid = siz; 1613191783Srmacklem io.uio_segflg = UIO_SYSSPACE; 1614191783Srmacklem io.uio_rw = UIO_READ; 1615191783Srmacklem io.uio_td = NULL; 1616191783Srmacklem nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1617191783Srmacklem &cookies); 1618191783Srmacklem off = (u_int64_t)io.uio_offset; 1619191783Srmacklem if (io.uio_resid) 1620191783Srmacklem siz -= io.uio_resid; 1621191783Srmacklem 1622191783Srmacklem if (!cookies && !nd->nd_repstat) 1623191783Srmacklem nd->nd_repstat = NFSERR_PERM; 1624191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1625216693Srmacklem getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1626191783Srmacklem if (!nd->nd_repstat) 1627191783Srmacklem nd->nd_repstat = getret; 1628191783Srmacklem } 1629191783Srmacklem 1630191783Srmacklem /* 1631191783Srmacklem * Handles the failed cases. nd->nd_repstat == 0 past here. 1632191783Srmacklem */ 1633191783Srmacklem if (nd->nd_repstat) { 1634216692Srmacklem vput(vp); 1635191783Srmacklem free((caddr_t)rbuf, M_TEMP); 1636191783Srmacklem if (cookies) 1637191783Srmacklem free((caddr_t)cookies, M_TEMP); 1638191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1639191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1640224086Szack goto out; 1641191783Srmacklem } 1642191783Srmacklem /* 1643191783Srmacklem * If nothing read, return eof 1644191783Srmacklem * rpc reply 1645191783Srmacklem */ 1646191783Srmacklem if (siz == 0) { 1647216692Srmacklem vput(vp); 1648191783Srmacklem if (nd->nd_flag & ND_NFSV2) { 1649191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1650191783Srmacklem } else { 1651191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1652191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1653191783Srmacklem txdr_hyper(at.na_filerev, tl); 1654191783Srmacklem tl += 2; 1655191783Srmacklem } 1656191783Srmacklem *tl++ = newnfs_false; 1657191783Srmacklem *tl = newnfs_true; 1658191783Srmacklem FREE((caddr_t)rbuf, M_TEMP); 1659191783Srmacklem FREE((caddr_t)cookies, M_TEMP); 1660224086Szack goto out; 1661191783Srmacklem } 1662191783Srmacklem 1663191783Srmacklem /* 1664191783Srmacklem * Check for degenerate cases of nothing useful read. 1665191783Srmacklem * If so go try again 1666191783Srmacklem */ 1667191783Srmacklem cpos = rbuf; 1668191783Srmacklem cend = rbuf + siz; 1669191783Srmacklem dp = (struct dirent *)cpos; 1670191783Srmacklem cookiep = cookies; 1671191783Srmacklem 1672191783Srmacklem /* 1673191783Srmacklem * For some reason FreeBSD's ufs_readdir() chooses to back the 1674191783Srmacklem * directory offset up to a block boundary, so it is necessary to 1675191783Srmacklem * skip over the records that precede the requested offset. This 1676191783Srmacklem * requires the assumption that file offset cookies monotonically 1677191783Srmacklem * increase. 1678216691Srmacklem * Since the offset cookies don't monotonically increase for ZFS, 1679216691Srmacklem * this is not done when ZFS is the file system. 1680191783Srmacklem */ 1681191783Srmacklem while (cpos < cend && ncookies > 0 && 1682191783Srmacklem (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1683216691Srmacklem (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) { 1684191783Srmacklem cpos += dp->d_reclen; 1685191783Srmacklem dp = (struct dirent *)cpos; 1686191783Srmacklem cookiep++; 1687191783Srmacklem ncookies--; 1688191783Srmacklem } 1689191783Srmacklem if (cpos >= cend || ncookies == 0) { 1690191783Srmacklem siz = fullsiz; 1691191783Srmacklem toff = off; 1692191783Srmacklem goto again; 1693191783Srmacklem } 1694216692Srmacklem vput(vp); 1695191783Srmacklem 1696191783Srmacklem /* 1697191783Srmacklem * dirlen is the size of the reply, including all XDR and must 1698191783Srmacklem * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1699191783Srmacklem * if the XDR should be included in "count", but to be safe, we do. 1700191783Srmacklem * (Include the two booleans at the end of the reply in dirlen now.) 1701191783Srmacklem */ 1702191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1703191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1704191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1705191783Srmacklem txdr_hyper(at.na_filerev, tl); 1706191783Srmacklem dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1707191783Srmacklem } else { 1708191783Srmacklem dirlen = 2 * NFSX_UNSIGNED; 1709191783Srmacklem } 1710191783Srmacklem 1711191783Srmacklem /* Loop through the records and build reply */ 1712191783Srmacklem while (cpos < cend && ncookies > 0) { 1713191783Srmacklem nlen = dp->d_namlen; 1714191783Srmacklem if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1715191783Srmacklem nlen <= NFS_MAXNAMLEN) { 1716191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1717191783Srmacklem dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1718191783Srmacklem else 1719191783Srmacklem dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1720191783Srmacklem if (dirlen > cnt) { 1721191783Srmacklem eofflag = 0; 1722191783Srmacklem break; 1723191783Srmacklem } 1724191783Srmacklem 1725191783Srmacklem /* 1726191783Srmacklem * Build the directory record xdr from 1727191783Srmacklem * the dirent entry. 1728191783Srmacklem */ 1729191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1730191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1731191783Srmacklem *tl++ = newnfs_true; 1732191783Srmacklem *tl++ = 0; 1733191783Srmacklem } else { 1734191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1735191783Srmacklem *tl++ = newnfs_true; 1736191783Srmacklem } 1737191783Srmacklem *tl = txdr_unsigned(dp->d_fileno); 1738191783Srmacklem (void) nfsm_strtom(nd, dp->d_name, nlen); 1739191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1740191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1741191783Srmacklem *tl++ = 0; 1742191783Srmacklem } else 1743191783Srmacklem NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1744191783Srmacklem *tl = txdr_unsigned(*cookiep); 1745191783Srmacklem } 1746191783Srmacklem cpos += dp->d_reclen; 1747191783Srmacklem dp = (struct dirent *)cpos; 1748191783Srmacklem cookiep++; 1749191783Srmacklem ncookies--; 1750191783Srmacklem } 1751191783Srmacklem if (cpos < cend) 1752191783Srmacklem eofflag = 0; 1753191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1754191783Srmacklem *tl++ = newnfs_false; 1755191783Srmacklem if (eofflag) 1756191783Srmacklem *tl = newnfs_true; 1757191783Srmacklem else 1758191783Srmacklem *tl = newnfs_false; 1759191783Srmacklem FREE((caddr_t)rbuf, M_TEMP); 1760191783Srmacklem FREE((caddr_t)cookies, M_TEMP); 1761224086Szack 1762224086Szackout: 1763224086Szack NFSEXITCODE2(0, nd); 1764191783Srmacklem return (0); 1765191783Srmacklemnfsmout: 1766191783Srmacklem vput(vp); 1767224086Szack NFSEXITCODE2(error, nd); 1768191783Srmacklem return (error); 1769191783Srmacklem} 1770191783Srmacklem 1771191783Srmacklem/* 1772191783Srmacklem * Readdirplus for V3 and Readdir for V4. 1773191783Srmacklem */ 1774191783Srmacklemint 1775191783Srmacklemnfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 1776191783Srmacklem struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1777191783Srmacklem{ 1778191783Srmacklem struct dirent *dp; 1779191783Srmacklem u_int32_t *tl; 1780191783Srmacklem int dirlen; 1781191783Srmacklem char *cpos, *cend, *rbuf; 1782191783Srmacklem struct vnode *nvp; 1783191783Srmacklem fhandle_t nfh; 1784191783Srmacklem struct nfsvattr nva, at, *nvap = &nva; 1785191783Srmacklem struct mbuf *mb0, *mb1; 1786191783Srmacklem struct nfsreferral *refp; 1787199715Srmacklem int nlen, r, error = 0, getret = 1, usevget = 1; 1788191783Srmacklem int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 1789191783Srmacklem caddr_t bpos0, bpos1; 1790191783Srmacklem u_int64_t off, toff, verf; 1791191783Srmacklem u_long *cookies = NULL, *cookiep; 1792191783Srmacklem nfsattrbit_t attrbits, rderrbits, savbits; 1793191783Srmacklem struct uio io; 1794191783Srmacklem struct iovec iv; 1795199715Srmacklem struct componentname cn; 1796220648Srmacklem int at_root, needs_unbusy, not_zfs, supports_nfsv4acls; 1797220645Srmacklem struct mount *mp, *new_mp; 1798220645Srmacklem uint64_t mounted_on_fileno; 1799191783Srmacklem 1800191783Srmacklem if (nd->nd_repstat) { 1801191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1802224086Szack goto out; 1803191783Srmacklem } 1804191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 1805191783Srmacklem off = fxdr_hyper(tl); 1806191783Srmacklem toff = off; 1807191783Srmacklem tl += 2; 1808191783Srmacklem verf = fxdr_hyper(tl); 1809191783Srmacklem tl += 2; 1810191783Srmacklem siz = fxdr_unsigned(int, *tl++); 1811191783Srmacklem cnt = fxdr_unsigned(int, *tl); 1812191783Srmacklem 1813191783Srmacklem /* 1814191783Srmacklem * Use the server's maximum data transfer size as the upper bound 1815191783Srmacklem * on reply datalen. 1816191783Srmacklem */ 1817206170Srmacklem if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1818191783Srmacklem cnt = NFS_SRVMAXDATA(nd); 1819191783Srmacklem 1820191783Srmacklem /* 1821191783Srmacklem * siz is a "hint" of how much directory information (name, fileid, 1822191783Srmacklem * cookie) should be in the reply. At least one client "hints" 0, 1823191783Srmacklem * so I set it to cnt for that case. I also round it up to the 1824191783Srmacklem * next multiple of DIRBLKSIZ. 1825191783Srmacklem */ 1826206170Srmacklem if (siz <= 0) 1827191783Srmacklem siz = cnt; 1828191783Srmacklem siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1829191783Srmacklem 1830191783Srmacklem if (nd->nd_flag & ND_NFSV4) { 1831191783Srmacklem error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1832191783Srmacklem if (error) 1833191783Srmacklem goto nfsmout; 1834191783Srmacklem NFSSET_ATTRBIT(&savbits, &attrbits); 1835191783Srmacklem NFSCLRNOTFILLABLE_ATTRBIT(&attrbits); 1836191783Srmacklem NFSZERO_ATTRBIT(&rderrbits); 1837191783Srmacklem NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 1838191783Srmacklem } else { 1839191783Srmacklem NFSZERO_ATTRBIT(&attrbits); 1840191783Srmacklem } 1841191783Srmacklem fullsiz = siz; 1842216693Srmacklem nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1843191783Srmacklem if (!nd->nd_repstat) { 1844191783Srmacklem if (off && verf != at.na_filerev) { 1845191783Srmacklem /* 1846191783Srmacklem * va_filerev is not sufficient as a cookie verifier, 1847191783Srmacklem * since it is not supposed to change when entries are 1848191783Srmacklem * removed/added unless that offset cookies returned to 1849191783Srmacklem * the client are no longer valid. 1850191783Srmacklem */ 1851191783Srmacklem#if 0 1852191783Srmacklem if (nd->nd_flag & ND_NFSV4) { 1853191783Srmacklem nd->nd_repstat = NFSERR_NOTSAME; 1854191783Srmacklem } else { 1855191783Srmacklem nd->nd_repstat = NFSERR_BAD_COOKIE; 1856191783Srmacklem } 1857191783Srmacklem#endif 1858191783Srmacklem } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) { 1859191783Srmacklem nd->nd_repstat = NFSERR_BAD_COOKIE; 1860191783Srmacklem } 1861191783Srmacklem } 1862191783Srmacklem if (!nd->nd_repstat && vp->v_type != VDIR) 1863191783Srmacklem nd->nd_repstat = NFSERR_NOTDIR; 1864191783Srmacklem if (!nd->nd_repstat && cnt == 0) 1865191783Srmacklem nd->nd_repstat = NFSERR_TOOSMALL; 1866191783Srmacklem if (!nd->nd_repstat) 1867200999Srmacklem nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1868191783Srmacklem nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1869200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 1870191783Srmacklem if (nd->nd_repstat) { 1871191783Srmacklem vput(vp); 1872191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1873191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1874224086Szack goto out; 1875191783Srmacklem } 1876216691Srmacklem not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs"); 1877191783Srmacklem 1878191783Srmacklem MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1879191783Srmacklemagain: 1880191783Srmacklem eofflag = 0; 1881191783Srmacklem if (cookies) { 1882191783Srmacklem free((caddr_t)cookies, M_TEMP); 1883191783Srmacklem cookies = NULL; 1884191783Srmacklem } 1885191783Srmacklem 1886191783Srmacklem iv.iov_base = rbuf; 1887191783Srmacklem iv.iov_len = siz; 1888191783Srmacklem io.uio_iov = &iv; 1889191783Srmacklem io.uio_iovcnt = 1; 1890191783Srmacklem io.uio_offset = (off_t)off; 1891191783Srmacklem io.uio_resid = siz; 1892191783Srmacklem io.uio_segflg = UIO_SYSSPACE; 1893191783Srmacklem io.uio_rw = UIO_READ; 1894191783Srmacklem io.uio_td = NULL; 1895191783Srmacklem nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1896191783Srmacklem &cookies); 1897191783Srmacklem off = (u_int64_t)io.uio_offset; 1898191783Srmacklem if (io.uio_resid) 1899191783Srmacklem siz -= io.uio_resid; 1900191783Srmacklem 1901216693Srmacklem getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1902191783Srmacklem 1903191783Srmacklem if (!cookies && !nd->nd_repstat) 1904191783Srmacklem nd->nd_repstat = NFSERR_PERM; 1905191783Srmacklem if (!nd->nd_repstat) 1906191783Srmacklem nd->nd_repstat = getret; 1907191783Srmacklem if (nd->nd_repstat) { 1908199715Srmacklem vput(vp); 1909191783Srmacklem if (cookies) 1910191783Srmacklem free((caddr_t)cookies, M_TEMP); 1911191783Srmacklem free((caddr_t)rbuf, M_TEMP); 1912191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1913191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1914224086Szack goto out; 1915191783Srmacklem } 1916191783Srmacklem /* 1917191783Srmacklem * If nothing read, return eof 1918191783Srmacklem * rpc reply 1919191783Srmacklem */ 1920191783Srmacklem if (siz == 0) { 1921199715Srmacklem vput(vp); 1922191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1923191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1924191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1925191783Srmacklem txdr_hyper(at.na_filerev, tl); 1926191783Srmacklem tl += 2; 1927191783Srmacklem *tl++ = newnfs_false; 1928191783Srmacklem *tl = newnfs_true; 1929191783Srmacklem free((caddr_t)cookies, M_TEMP); 1930191783Srmacklem free((caddr_t)rbuf, M_TEMP); 1931224086Szack goto out; 1932191783Srmacklem } 1933191783Srmacklem 1934191783Srmacklem /* 1935191783Srmacklem * Check for degenerate cases of nothing useful read. 1936191783Srmacklem * If so go try again 1937191783Srmacklem */ 1938191783Srmacklem cpos = rbuf; 1939191783Srmacklem cend = rbuf + siz; 1940191783Srmacklem dp = (struct dirent *)cpos; 1941191783Srmacklem cookiep = cookies; 1942191783Srmacklem 1943191783Srmacklem /* 1944191783Srmacklem * For some reason FreeBSD's ufs_readdir() chooses to back the 1945191783Srmacklem * directory offset up to a block boundary, so it is necessary to 1946191783Srmacklem * skip over the records that precede the requested offset. This 1947191783Srmacklem * requires the assumption that file offset cookies monotonically 1948191783Srmacklem * increase. 1949216691Srmacklem * Since the offset cookies don't monotonically increase for ZFS, 1950216691Srmacklem * this is not done when ZFS is the file system. 1951191783Srmacklem */ 1952191783Srmacklem while (cpos < cend && ncookies > 0 && 1953191783Srmacklem (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1954216691Srmacklem (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) || 1955191783Srmacklem ((nd->nd_flag & ND_NFSV4) && 1956191783Srmacklem ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 1957191783Srmacklem (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 1958191783Srmacklem cpos += dp->d_reclen; 1959191783Srmacklem dp = (struct dirent *)cpos; 1960191783Srmacklem cookiep++; 1961191783Srmacklem ncookies--; 1962191783Srmacklem } 1963191783Srmacklem if (cpos >= cend || ncookies == 0) { 1964191783Srmacklem siz = fullsiz; 1965191783Srmacklem toff = off; 1966191783Srmacklem goto again; 1967191783Srmacklem } 1968217176Srmacklem 1969217176Srmacklem /* 1970217176Srmacklem * Busy the file system so that the mount point won't go away 1971217176Srmacklem * and, as such, VFS_VGET() can be used safely. 1972217176Srmacklem */ 1973217176Srmacklem mp = vp->v_mount; 1974217176Srmacklem vfs_ref(mp); 1975224082Szack NFSVOPUNLOCK(vp, 0); 1976217176Srmacklem nd->nd_repstat = vfs_busy(mp, 0); 1977217176Srmacklem vfs_rel(mp); 1978217176Srmacklem if (nd->nd_repstat != 0) { 1979217176Srmacklem vrele(vp); 1980217176Srmacklem free(cookies, M_TEMP); 1981217176Srmacklem free(rbuf, M_TEMP); 1982217176Srmacklem if (nd->nd_flag & ND_NFSV3) 1983217176Srmacklem nfsrv_postopattr(nd, getret, &at); 1984224086Szack goto out; 1985217176Srmacklem } 1986191783Srmacklem 1987191783Srmacklem /* 1988260173Srmacklem * Check to see if entries in this directory can be safely acquired 1989260173Srmacklem * via VFS_VGET() or if a switch to VOP_LOOKUP() is required. 1990260173Srmacklem * ZFS snapshot directories need VOP_LOOKUP(), so that any 1991260173Srmacklem * automount of the snapshot directory that is required will 1992260173Srmacklem * be done. 1993260173Srmacklem * This needs to be done here for NFSv4, since NFSv4 never does 1994260173Srmacklem * a VFS_VGET() for "." or "..". 1995260173Srmacklem */ 1996260173Srmacklem if (not_zfs == 0) { 1997260173Srmacklem r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp); 1998260173Srmacklem if (r == EOPNOTSUPP) { 1999260173Srmacklem usevget = 0; 2000260173Srmacklem cn.cn_nameiop = LOOKUP; 2001260173Srmacklem cn.cn_lkflags = LK_SHARED | LK_RETRY; 2002260173Srmacklem cn.cn_cred = nd->nd_cred; 2003260173Srmacklem cn.cn_thread = p; 2004260173Srmacklem } else if (r == 0) 2005260173Srmacklem vput(nvp); 2006260173Srmacklem } 2007260173Srmacklem 2008260173Srmacklem /* 2009191783Srmacklem * Save this position, in case there is an error before one entry 2010191783Srmacklem * is created. 2011191783Srmacklem */ 2012191783Srmacklem mb0 = nd->nd_mb; 2013191783Srmacklem bpos0 = nd->nd_bpos; 2014191783Srmacklem 2015191783Srmacklem /* 2016191783Srmacklem * Fill in the first part of the reply. 2017191783Srmacklem * dirlen is the reply length in bytes and cannot exceed cnt. 2018191783Srmacklem * (Include the two booleans at the end of the reply in dirlen now, 2019191783Srmacklem * so we recognize when we have exceeded cnt.) 2020191783Srmacklem */ 2021191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 2022191783Srmacklem dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 2023191783Srmacklem nfsrv_postopattr(nd, getret, &at); 2024191783Srmacklem } else { 2025191783Srmacklem dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 2026191783Srmacklem } 2027191783Srmacklem NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2028191783Srmacklem txdr_hyper(at.na_filerev, tl); 2029191783Srmacklem 2030191783Srmacklem /* 2031191783Srmacklem * Save this position, in case there is an empty reply needed. 2032191783Srmacklem */ 2033191783Srmacklem mb1 = nd->nd_mb; 2034191783Srmacklem bpos1 = nd->nd_bpos; 2035191783Srmacklem 2036191783Srmacklem /* Loop through the records and build reply */ 2037191783Srmacklem entrycnt = 0; 2038191783Srmacklem while (cpos < cend && ncookies > 0 && dirlen < cnt) { 2039191783Srmacklem nlen = dp->d_namlen; 2040191783Srmacklem if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2041191783Srmacklem nlen <= NFS_MAXNAMLEN && 2042191783Srmacklem ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 2043191783Srmacklem (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 2044191783Srmacklem || (nlen == 1 && dp->d_name[0] != '.'))) { 2045191783Srmacklem /* 2046191783Srmacklem * Save the current position in the reply, in case 2047191783Srmacklem * this entry exceeds cnt. 2048191783Srmacklem */ 2049191783Srmacklem mb1 = nd->nd_mb; 2050191783Srmacklem bpos1 = nd->nd_bpos; 2051191783Srmacklem 2052191783Srmacklem /* 2053191783Srmacklem * For readdir_and_lookup get the vnode using 2054191783Srmacklem * the file number. 2055191783Srmacklem */ 2056191783Srmacklem nvp = NULL; 2057191783Srmacklem refp = NULL; 2058191783Srmacklem r = 0; 2059220645Srmacklem at_root = 0; 2060220645Srmacklem needs_unbusy = 0; 2061220645Srmacklem new_mp = mp; 2062220645Srmacklem mounted_on_fileno = (uint64_t)dp->d_fileno; 2063191783Srmacklem if ((nd->nd_flag & ND_NFSV3) || 2064191783Srmacklem NFSNONZERO_ATTRBIT(&savbits)) { 2065191783Srmacklem if (nd->nd_flag & ND_NFSV4) 2066191783Srmacklem refp = nfsv4root_getreferral(NULL, 2067191783Srmacklem vp, dp->d_fileno); 2068199715Srmacklem if (refp == NULL) { 2069199715Srmacklem if (usevget) 2070217176Srmacklem r = VFS_VGET(mp, dp->d_fileno, 2071217176Srmacklem LK_SHARED, &nvp); 2072199715Srmacklem else 2073199715Srmacklem r = EOPNOTSUPP; 2074199715Srmacklem if (r == EOPNOTSUPP) { 2075199715Srmacklem if (usevget) { 2076199715Srmacklem usevget = 0; 2077199715Srmacklem cn.cn_nameiop = LOOKUP; 2078199715Srmacklem cn.cn_lkflags = 2079216700Srmacklem LK_SHARED | 2080199715Srmacklem LK_RETRY; 2081199715Srmacklem cn.cn_cred = 2082199715Srmacklem nd->nd_cred; 2083199715Srmacklem cn.cn_thread = p; 2084199715Srmacklem } 2085199715Srmacklem cn.cn_nameptr = dp->d_name; 2086199715Srmacklem cn.cn_namelen = nlen; 2087199715Srmacklem cn.cn_flags = ISLASTCN | 2088199715Srmacklem NOFOLLOW | LOCKLEAF | 2089199715Srmacklem MPSAFE; 2090199715Srmacklem if (nlen == 2 && 2091199715Srmacklem dp->d_name[0] == '.' && 2092199715Srmacklem dp->d_name[1] == '.') 2093199715Srmacklem cn.cn_flags |= 2094199715Srmacklem ISDOTDOT; 2095224081Szack if (NFSVOPLOCK(vp, LK_SHARED) 2096216692Srmacklem != 0) { 2097216692Srmacklem nd->nd_repstat = EPERM; 2098216692Srmacklem break; 2099216692Srmacklem } 2100214149Srmacklem if ((vp->v_vflag & VV_ROOT) != 0 2101214149Srmacklem && (cn.cn_flags & ISDOTDOT) 2102214149Srmacklem != 0) { 2103214149Srmacklem vref(vp); 2104214149Srmacklem nvp = vp; 2105214149Srmacklem r = 0; 2106220507Srmacklem } else { 2107214149Srmacklem r = VOP_LOOKUP(vp, &nvp, 2108214149Srmacklem &cn); 2109220507Srmacklem if (vp != nvp) 2110224082Szack NFSVOPUNLOCK(vp, 2111220507Srmacklem 0); 2112220507Srmacklem } 2113199715Srmacklem } 2114220645Srmacklem 2115220645Srmacklem /* 2116220645Srmacklem * For NFSv4, check to see if nvp is 2117220645Srmacklem * a mount point and get the mount 2118220645Srmacklem * point vnode, as required. 2119220645Srmacklem */ 2120220645Srmacklem if (r == 0 && 2121220645Srmacklem nfsrv_enable_crossmntpt != 0 && 2122220645Srmacklem (nd->nd_flag & ND_NFSV4) != 0 && 2123220645Srmacklem nvp->v_type == VDIR && 2124220645Srmacklem nvp->v_mountedhere != NULL) { 2125220645Srmacklem new_mp = nvp->v_mountedhere; 2126220645Srmacklem r = vfs_busy(new_mp, 0); 2127220645Srmacklem vput(nvp); 2128220645Srmacklem nvp = NULL; 2129220645Srmacklem if (r == 0) { 2130220645Srmacklem r = VFS_ROOT(new_mp, 2131220645Srmacklem LK_SHARED, &nvp); 2132220645Srmacklem needs_unbusy = 1; 2133220645Srmacklem if (r == 0) 2134220645Srmacklem at_root = 1; 2135220645Srmacklem } 2136220645Srmacklem } 2137199715Srmacklem } 2138191783Srmacklem if (!r) { 2139191783Srmacklem if (refp == NULL && 2140191783Srmacklem ((nd->nd_flag & ND_NFSV3) || 2141191783Srmacklem NFSNONZERO_ATTRBIT(&attrbits))) { 2142191783Srmacklem r = nfsvno_getfh(nvp, &nfh, p); 2143191783Srmacklem if (!r) 2144191783Srmacklem r = nfsvno_getattr(nvp, nvap, 2145216693Srmacklem nd->nd_cred, p, 1); 2146260173Srmacklem if (r == 0 && not_zfs == 0 && 2147260173Srmacklem nfsrv_enable_crossmntpt != 0 && 2148260173Srmacklem (nd->nd_flag & ND_NFSV4) != 0 && 2149260173Srmacklem nvp->v_type == VDIR && 2150260173Srmacklem vp->v_mount != nvp->v_mount) { 2151260173Srmacklem /* 2152260173Srmacklem * For a ZFS snapshot, there is a 2153260173Srmacklem * pseudo mount that does not set 2154260173Srmacklem * v_mountedhere, so it needs to 2155260173Srmacklem * be detected via a different 2156260173Srmacklem * mount structure. 2157260173Srmacklem */ 2158260173Srmacklem at_root = 1; 2159260173Srmacklem if (new_mp == mp) 2160260173Srmacklem new_mp = nvp->v_mount; 2161260173Srmacklem } 2162191783Srmacklem } 2163191783Srmacklem } else { 2164191783Srmacklem nvp = NULL; 2165191783Srmacklem } 2166191783Srmacklem if (r) { 2167191783Srmacklem if (!NFSISSET_ATTRBIT(&attrbits, 2168191783Srmacklem NFSATTRBIT_RDATTRERROR)) { 2169191783Srmacklem if (nvp != NULL) 2170191783Srmacklem vput(nvp); 2171220645Srmacklem if (needs_unbusy != 0) 2172220645Srmacklem vfs_unbusy(new_mp); 2173191783Srmacklem nd->nd_repstat = r; 2174191783Srmacklem break; 2175191783Srmacklem } 2176191783Srmacklem } 2177191783Srmacklem } 2178191783Srmacklem 2179191783Srmacklem /* 2180191783Srmacklem * Build the directory record xdr 2181191783Srmacklem */ 2182191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 2183191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2184191783Srmacklem *tl++ = newnfs_true; 2185191783Srmacklem *tl++ = 0; 2186191783Srmacklem *tl = txdr_unsigned(dp->d_fileno); 2187191783Srmacklem dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2188191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2189191783Srmacklem *tl++ = 0; 2190191783Srmacklem *tl = txdr_unsigned(*cookiep); 2191191783Srmacklem nfsrv_postopattr(nd, 0, nvap); 2192191783Srmacklem dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1); 2193191783Srmacklem dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 2194191783Srmacklem if (nvp != NULL) 2195191783Srmacklem vput(nvp); 2196191783Srmacklem } else { 2197191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2198191783Srmacklem *tl++ = newnfs_true; 2199191783Srmacklem *tl++ = 0; 2200191783Srmacklem *tl = txdr_unsigned(*cookiep); 2201191783Srmacklem dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2202220648Srmacklem if (nvp != NULL) { 2203220648Srmacklem supports_nfsv4acls = 2204220648Srmacklem nfs_supportsnfsv4acls(nvp); 2205224082Szack NFSVOPUNLOCK(nvp, 0); 2206220648Srmacklem } else 2207220648Srmacklem supports_nfsv4acls = 0; 2208191783Srmacklem if (refp != NULL) { 2209191783Srmacklem dirlen += nfsrv_putreferralattr(nd, 2210191783Srmacklem &savbits, refp, 0, 2211191783Srmacklem &nd->nd_repstat); 2212191783Srmacklem if (nd->nd_repstat) { 2213191783Srmacklem if (nvp != NULL) 2214191783Srmacklem vrele(nvp); 2215220645Srmacklem if (needs_unbusy != 0) 2216220645Srmacklem vfs_unbusy(new_mp); 2217191783Srmacklem break; 2218191783Srmacklem } 2219191783Srmacklem } else if (r) { 2220220645Srmacklem dirlen += nfsvno_fillattr(nd, new_mp, 2221220645Srmacklem nvp, nvap, &nfh, r, &rderrbits, 2222220648Srmacklem nd->nd_cred, p, isdgram, 0, 2223220648Srmacklem supports_nfsv4acls, at_root, 2224220645Srmacklem mounted_on_fileno); 2225191783Srmacklem } else { 2226220645Srmacklem dirlen += nfsvno_fillattr(nd, new_mp, 2227220645Srmacklem nvp, nvap, &nfh, r, &attrbits, 2228220648Srmacklem nd->nd_cred, p, isdgram, 0, 2229220648Srmacklem supports_nfsv4acls, at_root, 2230220645Srmacklem mounted_on_fileno); 2231191783Srmacklem } 2232191783Srmacklem if (nvp != NULL) 2233191783Srmacklem vrele(nvp); 2234191783Srmacklem dirlen += (3 * NFSX_UNSIGNED); 2235191783Srmacklem } 2236220645Srmacklem if (needs_unbusy != 0) 2237220645Srmacklem vfs_unbusy(new_mp); 2238191783Srmacklem if (dirlen <= cnt) 2239191783Srmacklem entrycnt++; 2240191783Srmacklem } 2241191783Srmacklem cpos += dp->d_reclen; 2242191783Srmacklem dp = (struct dirent *)cpos; 2243191783Srmacklem cookiep++; 2244191783Srmacklem ncookies--; 2245191783Srmacklem } 2246216692Srmacklem vrele(vp); 2247217176Srmacklem vfs_unbusy(mp); 2248191783Srmacklem 2249191783Srmacklem /* 2250191783Srmacklem * If dirlen > cnt, we must strip off the last entry. If that 2251191783Srmacklem * results in an empty reply, report NFSERR_TOOSMALL. 2252191783Srmacklem */ 2253191783Srmacklem if (dirlen > cnt || nd->nd_repstat) { 2254191783Srmacklem if (!nd->nd_repstat && entrycnt == 0) 2255191783Srmacklem nd->nd_repstat = NFSERR_TOOSMALL; 2256191783Srmacklem if (nd->nd_repstat) 2257191783Srmacklem newnfs_trimtrailing(nd, mb0, bpos0); 2258191783Srmacklem else 2259191783Srmacklem newnfs_trimtrailing(nd, mb1, bpos1); 2260191783Srmacklem eofflag = 0; 2261191783Srmacklem } else if (cpos < cend) 2262191783Srmacklem eofflag = 0; 2263191783Srmacklem if (!nd->nd_repstat) { 2264191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2265191783Srmacklem *tl++ = newnfs_false; 2266191783Srmacklem if (eofflag) 2267191783Srmacklem *tl = newnfs_true; 2268191783Srmacklem else 2269191783Srmacklem *tl = newnfs_false; 2270191783Srmacklem } 2271191783Srmacklem FREE((caddr_t)cookies, M_TEMP); 2272191783Srmacklem FREE((caddr_t)rbuf, M_TEMP); 2273224086Szack 2274224086Szackout: 2275224086Szack NFSEXITCODE2(0, nd); 2276191783Srmacklem return (0); 2277191783Srmacklemnfsmout: 2278191783Srmacklem vput(vp); 2279224086Szack NFSEXITCODE2(error, nd); 2280191783Srmacklem return (error); 2281191783Srmacklem} 2282191783Srmacklem 2283191783Srmacklem/* 2284191783Srmacklem * Get the settable attributes out of the mbuf list. 2285191783Srmacklem * (Return 0 or EBADRPC) 2286191783Srmacklem */ 2287191783Srmacklemint 2288191783Srmacklemnfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2289191783Srmacklem nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2290191783Srmacklem{ 2291191783Srmacklem u_int32_t *tl; 2292191783Srmacklem struct nfsv2_sattr *sp; 2293191783Srmacklem int error = 0, toclient = 0; 2294191783Srmacklem 2295191783Srmacklem switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 2296191783Srmacklem case ND_NFSV2: 2297191783Srmacklem NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2298191783Srmacklem /* 2299191783Srmacklem * Some old clients didn't fill in the high order 16bits. 2300191783Srmacklem * --> check the low order 2 bytes for 0xffff 2301191783Srmacklem */ 2302191783Srmacklem if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 2303191783Srmacklem nvap->na_mode = nfstov_mode(sp->sa_mode); 2304191783Srmacklem if (sp->sa_uid != newnfs_xdrneg1) 2305191783Srmacklem nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 2306191783Srmacklem if (sp->sa_gid != newnfs_xdrneg1) 2307191783Srmacklem nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 2308191783Srmacklem if (sp->sa_size != newnfs_xdrneg1) 2309191783Srmacklem nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 2310191783Srmacklem if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 2311191783Srmacklem#ifdef notyet 2312191783Srmacklem fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 2313191783Srmacklem#else 2314191783Srmacklem nvap->na_atime.tv_sec = 2315191783Srmacklem fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 2316191783Srmacklem nvap->na_atime.tv_nsec = 0; 2317191783Srmacklem#endif 2318191783Srmacklem } 2319191783Srmacklem if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 2320191783Srmacklem fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 2321191783Srmacklem break; 2322191783Srmacklem case ND_NFSV3: 2323191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2324191783Srmacklem if (*tl == newnfs_true) { 2325191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2326191783Srmacklem nvap->na_mode = nfstov_mode(*tl); 2327191783Srmacklem } 2328191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2329191783Srmacklem if (*tl == newnfs_true) { 2330191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2331191783Srmacklem nvap->na_uid = fxdr_unsigned(uid_t, *tl); 2332191783Srmacklem } 2333191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2334191783Srmacklem if (*tl == newnfs_true) { 2335191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2336191783Srmacklem nvap->na_gid = fxdr_unsigned(gid_t, *tl); 2337191783Srmacklem } 2338191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2339191783Srmacklem if (*tl == newnfs_true) { 2340191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2341191783Srmacklem nvap->na_size = fxdr_hyper(tl); 2342191783Srmacklem } 2343191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2344191783Srmacklem switch (fxdr_unsigned(int, *tl)) { 2345191783Srmacklem case NFSV3SATTRTIME_TOCLIENT: 2346191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2347191783Srmacklem fxdr_nfsv3time(tl, &nvap->na_atime); 2348191783Srmacklem toclient = 1; 2349191783Srmacklem break; 2350191783Srmacklem case NFSV3SATTRTIME_TOSERVER: 2351247502Sjhb vfs_timestamp(&nvap->na_atime); 2352191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2353191783Srmacklem break; 2354191783Srmacklem }; 2355191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2356191783Srmacklem switch (fxdr_unsigned(int, *tl)) { 2357191783Srmacklem case NFSV3SATTRTIME_TOCLIENT: 2358191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2359191783Srmacklem fxdr_nfsv3time(tl, &nvap->na_mtime); 2360191783Srmacklem nvap->na_vaflags &= ~VA_UTIMES_NULL; 2361191783Srmacklem break; 2362191783Srmacklem case NFSV3SATTRTIME_TOSERVER: 2363247502Sjhb vfs_timestamp(&nvap->na_mtime); 2364191783Srmacklem if (!toclient) 2365191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2366191783Srmacklem break; 2367191783Srmacklem }; 2368191783Srmacklem break; 2369191783Srmacklem case ND_NFSV4: 2370191783Srmacklem error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p); 2371191783Srmacklem }; 2372191783Srmacklemnfsmout: 2373224086Szack NFSEXITCODE2(error, nd); 2374191783Srmacklem return (error); 2375191783Srmacklem} 2376191783Srmacklem 2377191783Srmacklem/* 2378191783Srmacklem * Handle the setable attributes for V4. 2379191783Srmacklem * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 2380191783Srmacklem */ 2381191783Srmacklemint 2382191783Srmacklemnfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2383191783Srmacklem nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2384191783Srmacklem{ 2385191783Srmacklem u_int32_t *tl; 2386191783Srmacklem int attrsum = 0; 2387191783Srmacklem int i, j; 2388191783Srmacklem int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 2389191783Srmacklem int toclient = 0; 2390191783Srmacklem u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 2391191783Srmacklem uid_t uid; 2392191783Srmacklem gid_t gid; 2393191783Srmacklem 2394191783Srmacklem error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 2395191783Srmacklem if (error) 2396224086Szack goto nfsmout; 2397191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2398191783Srmacklem attrsize = fxdr_unsigned(int, *tl); 2399191783Srmacklem 2400191783Srmacklem /* 2401191783Srmacklem * Loop around getting the setable attributes. If an unsupported 2402191783Srmacklem * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 2403191783Srmacklem */ 2404191783Srmacklem if (retnotsup) { 2405191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2406191783Srmacklem bitpos = NFSATTRBIT_MAX; 2407191783Srmacklem } else { 2408191783Srmacklem bitpos = 0; 2409191783Srmacklem } 2410191783Srmacklem for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 2411191783Srmacklem if (attrsum > attrsize) { 2412191783Srmacklem error = NFSERR_BADXDR; 2413191783Srmacklem goto nfsmout; 2414191783Srmacklem } 2415191783Srmacklem if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 2416191783Srmacklem switch (bitpos) { 2417191783Srmacklem case NFSATTRBIT_SIZE: 2418191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2419191783Srmacklem nvap->na_size = fxdr_hyper(tl); 2420191783Srmacklem attrsum += NFSX_HYPER; 2421191783Srmacklem break; 2422191783Srmacklem case NFSATTRBIT_ACL: 2423191783Srmacklem error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize, 2424191783Srmacklem p); 2425191783Srmacklem if (error) 2426191783Srmacklem goto nfsmout; 2427191783Srmacklem if (aceerr && !nd->nd_repstat) 2428224077Szack nd->nd_repstat = aceerr; 2429191783Srmacklem attrsum += aclsize; 2430191783Srmacklem break; 2431191783Srmacklem case NFSATTRBIT_ARCHIVE: 2432191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2433191783Srmacklem if (!nd->nd_repstat) 2434191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2435191783Srmacklem attrsum += NFSX_UNSIGNED; 2436191783Srmacklem break; 2437191783Srmacklem case NFSATTRBIT_HIDDEN: 2438191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2439191783Srmacklem if (!nd->nd_repstat) 2440191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2441191783Srmacklem attrsum += NFSX_UNSIGNED; 2442191783Srmacklem break; 2443191783Srmacklem case NFSATTRBIT_MIMETYPE: 2444191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2445191783Srmacklem i = fxdr_unsigned(int, *tl); 2446191783Srmacklem error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 2447191783Srmacklem if (error) 2448191783Srmacklem goto nfsmout; 2449191783Srmacklem if (!nd->nd_repstat) 2450191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2451191783Srmacklem attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 2452191783Srmacklem break; 2453191783Srmacklem case NFSATTRBIT_MODE: 2454191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2455191783Srmacklem nvap->na_mode = nfstov_mode(*tl); 2456191783Srmacklem attrsum += NFSX_UNSIGNED; 2457191783Srmacklem break; 2458191783Srmacklem case NFSATTRBIT_OWNER: 2459191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2460191783Srmacklem j = fxdr_unsigned(int, *tl); 2461224086Szack if (j < 0) { 2462224086Szack error = NFSERR_BADXDR; 2463224086Szack goto nfsmout; 2464224086Szack } 2465191783Srmacklem if (j > NFSV4_SMALLSTR) 2466191783Srmacklem cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2467191783Srmacklem else 2468191783Srmacklem cp = namestr; 2469191783Srmacklem error = nfsrv_mtostr(nd, cp, j); 2470191783Srmacklem if (error) { 2471191783Srmacklem if (j > NFSV4_SMALLSTR) 2472191783Srmacklem free(cp, M_NFSSTRING); 2473224086Szack goto nfsmout; 2474191783Srmacklem } 2475191783Srmacklem if (!nd->nd_repstat) { 2476241194Srmacklem nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid, 2477241194Srmacklem p); 2478191783Srmacklem if (!nd->nd_repstat) 2479191783Srmacklem nvap->na_uid = uid; 2480191783Srmacklem } 2481191783Srmacklem if (j > NFSV4_SMALLSTR) 2482191783Srmacklem free(cp, M_NFSSTRING); 2483191783Srmacklem attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2484191783Srmacklem break; 2485191783Srmacklem case NFSATTRBIT_OWNERGROUP: 2486191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2487191783Srmacklem j = fxdr_unsigned(int, *tl); 2488224086Szack if (j < 0) { 2489224086Szack error = NFSERR_BADXDR; 2490224086Szack goto nfsmout; 2491224086Szack } 2492191783Srmacklem if (j > NFSV4_SMALLSTR) 2493191783Srmacklem cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2494191783Srmacklem else 2495191783Srmacklem cp = namestr; 2496191783Srmacklem error = nfsrv_mtostr(nd, cp, j); 2497191783Srmacklem if (error) { 2498191783Srmacklem if (j > NFSV4_SMALLSTR) 2499191783Srmacklem free(cp, M_NFSSTRING); 2500224086Szack goto nfsmout; 2501191783Srmacklem } 2502191783Srmacklem if (!nd->nd_repstat) { 2503241194Srmacklem nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid, 2504241194Srmacklem p); 2505191783Srmacklem if (!nd->nd_repstat) 2506191783Srmacklem nvap->na_gid = gid; 2507191783Srmacklem } 2508191783Srmacklem if (j > NFSV4_SMALLSTR) 2509191783Srmacklem free(cp, M_NFSSTRING); 2510191783Srmacklem attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2511191783Srmacklem break; 2512191783Srmacklem case NFSATTRBIT_SYSTEM: 2513191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2514191783Srmacklem if (!nd->nd_repstat) 2515191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2516191783Srmacklem attrsum += NFSX_UNSIGNED; 2517191783Srmacklem break; 2518191783Srmacklem case NFSATTRBIT_TIMEACCESSSET: 2519191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2520191783Srmacklem attrsum += NFSX_UNSIGNED; 2521191783Srmacklem if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2522191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2523191783Srmacklem fxdr_nfsv4time(tl, &nvap->na_atime); 2524191783Srmacklem toclient = 1; 2525191783Srmacklem attrsum += NFSX_V4TIME; 2526191783Srmacklem } else { 2527247502Sjhb vfs_timestamp(&nvap->na_atime); 2528191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2529191783Srmacklem } 2530191783Srmacklem break; 2531191783Srmacklem case NFSATTRBIT_TIMEBACKUP: 2532191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2533191783Srmacklem if (!nd->nd_repstat) 2534191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2535191783Srmacklem attrsum += NFSX_V4TIME; 2536191783Srmacklem break; 2537191783Srmacklem case NFSATTRBIT_TIMECREATE: 2538191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2539191783Srmacklem if (!nd->nd_repstat) 2540191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2541191783Srmacklem attrsum += NFSX_V4TIME; 2542191783Srmacklem break; 2543191783Srmacklem case NFSATTRBIT_TIMEMODIFYSET: 2544191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2545191783Srmacklem attrsum += NFSX_UNSIGNED; 2546191783Srmacklem if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2547191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2548191783Srmacklem fxdr_nfsv4time(tl, &nvap->na_mtime); 2549191783Srmacklem nvap->na_vaflags &= ~VA_UTIMES_NULL; 2550191783Srmacklem attrsum += NFSX_V4TIME; 2551191783Srmacklem } else { 2552247502Sjhb vfs_timestamp(&nvap->na_mtime); 2553191783Srmacklem if (!toclient) 2554191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2555191783Srmacklem } 2556191783Srmacklem break; 2557191783Srmacklem default: 2558191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2559191783Srmacklem /* 2560191783Srmacklem * set bitpos so we drop out of the loop. 2561191783Srmacklem */ 2562191783Srmacklem bitpos = NFSATTRBIT_MAX; 2563191783Srmacklem break; 2564191783Srmacklem }; 2565191783Srmacklem } 2566191783Srmacklem 2567191783Srmacklem /* 2568191783Srmacklem * some clients pad the attrlist, so we need to skip over the 2569191783Srmacklem * padding. 2570191783Srmacklem */ 2571191783Srmacklem if (attrsum > attrsize) { 2572191783Srmacklem error = NFSERR_BADXDR; 2573191783Srmacklem } else { 2574191783Srmacklem attrsize = NFSM_RNDUP(attrsize); 2575191783Srmacklem if (attrsum < attrsize) 2576191783Srmacklem error = nfsm_advance(nd, attrsize - attrsum, -1); 2577191783Srmacklem } 2578191783Srmacklemnfsmout: 2579224086Szack NFSEXITCODE2(error, nd); 2580191783Srmacklem return (error); 2581191783Srmacklem} 2582191783Srmacklem 2583191783Srmacklem/* 2584191783Srmacklem * Check/setup export credentials. 2585191783Srmacklem */ 2586191783Srmacklemint 2587191783Srmacklemnfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 2588191783Srmacklem struct ucred *credanon) 2589191783Srmacklem{ 2590191783Srmacklem int error = 0; 2591191783Srmacklem 2592191783Srmacklem /* 2593191783Srmacklem * Check/setup credentials. 2594191783Srmacklem */ 2595191783Srmacklem if (nd->nd_flag & ND_GSS) 2596192121Srmacklem exp->nes_exflag &= ~MNT_EXPORTANON; 2597191783Srmacklem 2598191783Srmacklem /* 2599192121Srmacklem * Check to see if the operation is allowed for this security flavor. 2600191783Srmacklem * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to 2601191783Srmacklem * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 2602192121Srmacklem * Also, allow Secinfo, so that it can acquire the correct flavor(s). 2603191783Srmacklem */ 2604192121Srmacklem if (nfsvno_testexp(nd, exp) && 2605192121Srmacklem nd->nd_procnum != NFSV4OP_SECINFO && 2606191783Srmacklem nd->nd_procnum != NFSPROC_FSINFO) { 2607191783Srmacklem if (nd->nd_flag & ND_NFSV4) 2608191783Srmacklem error = NFSERR_WRONGSEC; 2609191783Srmacklem else 2610191783Srmacklem error = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2611224086Szack goto out; 2612191783Srmacklem } 2613191783Srmacklem 2614191783Srmacklem /* 2615191783Srmacklem * Check to see if the file system is exported V4 only. 2616191783Srmacklem */ 2617224086Szack if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) { 2618224086Szack error = NFSERR_PROGNOTV4; 2619224086Szack goto out; 2620224086Szack } 2621191783Srmacklem 2622191783Srmacklem /* 2623191783Srmacklem * Now, map the user credentials. 2624191783Srmacklem * (Note that ND_AUTHNONE will only be set for an NFSv3 2625191783Srmacklem * Fsinfo RPC. If set for anything else, this code might need 2626191783Srmacklem * to change.) 2627191783Srmacklem */ 2628191783Srmacklem if (NFSVNO_EXPORTED(exp) && 2629191783Srmacklem ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) || 2630191783Srmacklem NFSVNO_EXPORTANON(exp) || 2631191783Srmacklem (nd->nd_flag & ND_AUTHNONE))) { 2632191783Srmacklem nd->nd_cred->cr_uid = credanon->cr_uid; 2633191783Srmacklem nd->nd_cred->cr_gid = credanon->cr_gid; 2634194498Sbrooks crsetgroups(nd->nd_cred, credanon->cr_ngroups, 2635194498Sbrooks credanon->cr_groups); 2636191783Srmacklem } 2637224086Szack 2638224086Szackout: 2639224086Szack NFSEXITCODE2(error, nd); 2640224086Szack return (error); 2641191783Srmacklem} 2642191783Srmacklem 2643191783Srmacklem/* 2644191783Srmacklem * Check exports. 2645191783Srmacklem */ 2646191783Srmacklemint 2647191783Srmacklemnfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 2648191783Srmacklem struct ucred **credp) 2649191783Srmacklem{ 2650192121Srmacklem int i, error, *secflavors; 2651191783Srmacklem 2652191783Srmacklem error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2653192121Srmacklem &exp->nes_numsecflavor, &secflavors); 2654192121Srmacklem if (error) { 2655192121Srmacklem if (nfs_rootfhset) { 2656192121Srmacklem exp->nes_exflag = 0; 2657192121Srmacklem exp->nes_numsecflavor = 0; 2658192121Srmacklem error = 0; 2659192121Srmacklem } 2660192121Srmacklem } else { 2661192121Srmacklem /* Copy the security flavors. */ 2662192121Srmacklem for (i = 0; i < exp->nes_numsecflavor; i++) 2663192121Srmacklem exp->nes_secflavors[i] = secflavors[i]; 2664191783Srmacklem } 2665224086Szack NFSEXITCODE(error); 2666191783Srmacklem return (error); 2667191783Srmacklem} 2668191783Srmacklem 2669191783Srmacklem/* 2670191783Srmacklem * Get a vnode for a file handle and export stuff. 2671191783Srmacklem */ 2672191783Srmacklemint 2673191783Srmacklemnfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 2674216700Srmacklem int lktype, struct vnode **vpp, struct nfsexstuff *exp, 2675216700Srmacklem struct ucred **credp) 2676191783Srmacklem{ 2677192121Srmacklem int i, error, *secflavors; 2678191783Srmacklem 2679191998Srmacklem *credp = NULL; 2680192121Srmacklem exp->nes_numsecflavor = 0; 2681217063Srmacklem if (VFS_NEEDSGIANT(mp)) 2682217063Srmacklem error = ESTALE; 2683217063Srmacklem else 2684222167Srmacklem error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp); 2685205663Srmacklem if (error != 0) 2686205663Srmacklem /* Make sure the server replies ESTALE to the client. */ 2687205663Srmacklem error = ESTALE; 2688191783Srmacklem if (nam && !error) { 2689191783Srmacklem error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2690192121Srmacklem &exp->nes_numsecflavor, &secflavors); 2691191783Srmacklem if (error) { 2692191783Srmacklem if (nfs_rootfhset) { 2693191783Srmacklem exp->nes_exflag = 0; 2694192121Srmacklem exp->nes_numsecflavor = 0; 2695191783Srmacklem error = 0; 2696191783Srmacklem } else { 2697191783Srmacklem vput(*vpp); 2698191783Srmacklem } 2699192121Srmacklem } else { 2700192121Srmacklem /* Copy the security flavors. */ 2701192121Srmacklem for (i = 0; i < exp->nes_numsecflavor; i++) 2702192121Srmacklem exp->nes_secflavors[i] = secflavors[i]; 2703191783Srmacklem } 2704191783Srmacklem } 2705216700Srmacklem if (error == 0 && lktype == LK_SHARED) 2706216700Srmacklem /* 2707216700Srmacklem * It would be much better to pass lktype to VFS_FHTOVP(), 2708216700Srmacklem * but this will have to do until VFS_FHTOVP() has a lock 2709216700Srmacklem * type argument like VFS_VGET(). 2710216700Srmacklem */ 2711224081Szack NFSVOPLOCK(*vpp, LK_DOWNGRADE | LK_RETRY); 2712224086Szack 2713224086Szack NFSEXITCODE(error); 2714191783Srmacklem return (error); 2715191783Srmacklem} 2716191783Srmacklem 2717191783Srmacklem/* 2718191783Srmacklem * nfsd_fhtovp() - convert a fh to a vnode ptr 2719191783Srmacklem * - look up fsid in mount list (if not found ret error) 2720191783Srmacklem * - get vp and export rights by calling nfsvno_fhtovp() 2721191783Srmacklem * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 2722191783Srmacklem * for AUTH_SYS 2723217063Srmacklem * - if mpp != NULL, return the mount point so that it can 2724217063Srmacklem * be used for vn_finished_write() by the caller 2725191783Srmacklem */ 2726191783Srmacklemvoid 2727216700Srmacklemnfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 2728191783Srmacklem struct vnode **vpp, struct nfsexstuff *exp, 2729191783Srmacklem struct mount **mpp, int startwrite, struct thread *p) 2730191783Srmacklem{ 2731191783Srmacklem struct mount *mp; 2732191783Srmacklem struct ucred *credanon; 2733191783Srmacklem fhandle_t *fhp; 2734191783Srmacklem 2735191783Srmacklem fhp = (fhandle_t *)nfp->nfsrvfh_data; 2736191783Srmacklem /* 2737191783Srmacklem * Check for the special case of the nfsv4root_fh. 2738191783Srmacklem */ 2739217017Srmacklem mp = vfs_busyfs(&fhp->fh_fsid); 2740217063Srmacklem if (mpp != NULL) 2741217063Srmacklem *mpp = mp; 2742217017Srmacklem if (mp == NULL) { 2743191783Srmacklem *vpp = NULL; 2744191783Srmacklem nd->nd_repstat = ESTALE; 2745224086Szack goto out; 2746191783Srmacklem } 2747191783Srmacklem 2748251641Sken if (startwrite) { 2749191783Srmacklem vn_start_write(NULL, mpp, V_WAIT); 2750251641Sken if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp))) 2751251641Sken lktype = LK_EXCLUSIVE; 2752251641Sken } 2753216700Srmacklem nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 2754191783Srmacklem &credanon); 2755217017Srmacklem vfs_unbusy(mp); 2756191783Srmacklem 2757191783Srmacklem /* 2758191783Srmacklem * For NFSv4 without a pseudo root fs, unexported file handles 2759191783Srmacklem * can be returned, so that Lookup works everywhere. 2760191783Srmacklem */ 2761191783Srmacklem if (!nd->nd_repstat && exp->nes_exflag == 0 && 2762191783Srmacklem !(nd->nd_flag & ND_NFSV4)) { 2763191783Srmacklem vput(*vpp); 2764191783Srmacklem nd->nd_repstat = EACCES; 2765191783Srmacklem } 2766191783Srmacklem 2767191783Srmacklem /* 2768191783Srmacklem * Personally, I've never seen any point in requiring a 2769191783Srmacklem * reserved port#, since only in the rare case where the 2770191783Srmacklem * clients are all boxes with secure system priviledges, 2771191783Srmacklem * does it provide any enhanced security, but... some people 2772191783Srmacklem * believe it to be useful and keep putting this code back in. 2773191783Srmacklem * (There is also some "security checker" out there that 2774191783Srmacklem * complains if the nfs server doesn't enforce this.) 2775191783Srmacklem * However, note the following: 2776191783Srmacklem * RFC3530 (NFSv4) specifies that a reserved port# not be 2777191783Srmacklem * required. 2778191783Srmacklem * RFC2623 recommends that, if a reserved port# is checked for, 2779191783Srmacklem * that there be a way to turn that off--> ifdef'd. 2780191783Srmacklem */ 2781191783Srmacklem#ifdef NFS_REQRSVPORT 2782191783Srmacklem if (!nd->nd_repstat) { 2783192121Srmacklem struct sockaddr_in *saddr; 2784192121Srmacklem struct sockaddr_in6 *saddr6; 2785192121Srmacklem 2786192121Srmacklem saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 2787192121Srmacklem saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 2788192121Srmacklem if (!(nd->nd_flag & ND_NFSV4) && 2789192121Srmacklem ((saddr->sin_family == AF_INET && 2790192121Srmacklem ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 2791192121Srmacklem (saddr6->sin6_family == AF_INET6 && 2792192121Srmacklem ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 2793192121Srmacklem vput(*vpp); 2794192121Srmacklem nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2795192121Srmacklem } 2796191783Srmacklem } 2797191783Srmacklem#endif /* NFS_REQRSVPORT */ 2798191783Srmacklem 2799191783Srmacklem /* 2800191783Srmacklem * Check/setup credentials. 2801191783Srmacklem */ 2802191783Srmacklem if (!nd->nd_repstat) { 2803191783Srmacklem nd->nd_saveduid = nd->nd_cred->cr_uid; 2804191783Srmacklem nd->nd_repstat = nfsd_excred(nd, exp, credanon); 2805191783Srmacklem if (nd->nd_repstat) 2806191783Srmacklem vput(*vpp); 2807191783Srmacklem } 2808191940Skan if (credanon != NULL) 2809191940Skan crfree(credanon); 2810191783Srmacklem if (nd->nd_repstat) { 2811191783Srmacklem if (startwrite) 2812191783Srmacklem vn_finished_write(mp); 2813191783Srmacklem *vpp = NULL; 2814217063Srmacklem if (mpp != NULL) 2815217063Srmacklem *mpp = NULL; 2816191783Srmacklem } 2817224086Szack 2818224086Szackout: 2819224086Szack NFSEXITCODE2(0, nd); 2820191783Srmacklem} 2821191783Srmacklem 2822191783Srmacklem/* 2823191783Srmacklem * glue for fp. 2824191783Srmacklem */ 2825191783Srmacklemint 2826191783Srmacklemfp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 2827191783Srmacklem{ 2828191783Srmacklem struct filedesc *fdp; 2829191783Srmacklem struct file *fp; 2830224086Szack int error = 0; 2831191783Srmacklem 2832191783Srmacklem fdp = p->td_proc->p_fd; 2833191783Srmacklem if (fd >= fdp->fd_nfiles || 2834224086Szack (fp = fdp->fd_ofiles[fd]) == NULL) { 2835224086Szack error = EBADF; 2836224086Szack goto out; 2837224086Szack } 2838191783Srmacklem *fpp = fp; 2839224086Szack 2840224086Szackout: 2841224086Szack NFSEXITCODE(error); 2842224086Szack return (error); 2843191783Srmacklem} 2844191783Srmacklem 2845191783Srmacklem/* 2846192121Srmacklem * Called from nfssvc() to update the exports list. Just call 2847191783Srmacklem * vfs_export(). This has to be done, since the v4 root fake fs isn't 2848191783Srmacklem * in the mount list. 2849191783Srmacklem */ 2850191783Srmacklemint 2851191783Srmacklemnfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 2852191783Srmacklem{ 2853191783Srmacklem struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 2854224086Szack int error = 0; 2855191783Srmacklem struct nameidata nd; 2856191783Srmacklem fhandle_t fh; 2857191783Srmacklem 2858191783Srmacklem error = vfs_export(&nfsv4root_mnt, &nfsexargp->export); 2859216894Srmacklem if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 2860191783Srmacklem nfs_rootfhset = 0; 2861216894Srmacklem else if (error == 0) { 2862224086Szack if (nfsexargp->fspec == NULL) { 2863224086Szack error = EPERM; 2864224086Szack goto out; 2865224086Szack } 2866191783Srmacklem /* 2867191783Srmacklem * If fspec != NULL, this is the v4root path. 2868191783Srmacklem */ 2869191783Srmacklem NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, 2870191783Srmacklem nfsexargp->fspec, p); 2871191783Srmacklem if ((error = namei(&nd)) != 0) 2872224086Szack goto out; 2873191783Srmacklem error = nfsvno_getfh(nd.ni_vp, &fh, p); 2874191783Srmacklem vrele(nd.ni_vp); 2875191783Srmacklem if (!error) { 2876191783Srmacklem nfs_rootfh.nfsrvfh_len = NFSX_MYFH; 2877191783Srmacklem NFSBCOPY((caddr_t)&fh, 2878191783Srmacklem nfs_rootfh.nfsrvfh_data, 2879191783Srmacklem sizeof (fhandle_t)); 2880191783Srmacklem nfs_rootfhset = 1; 2881191783Srmacklem } 2882191783Srmacklem } 2883224086Szack 2884224086Szackout: 2885224086Szack NFSEXITCODE(error); 2886191783Srmacklem return (error); 2887191783Srmacklem} 2888191783Srmacklem 2889191783Srmacklem/* 2890191783Srmacklem * Get the tcp socket sequence numbers we need. 2891191783Srmacklem * (Maybe this should be moved to the tcp sources?) 2892191783Srmacklem */ 2893191783Srmacklemint 2894191783Srmacklemnfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap) 2895191783Srmacklem{ 2896191783Srmacklem struct inpcb *inp; 2897191783Srmacklem struct tcpcb *tp; 2898224086Szack int error = 0; 2899191783Srmacklem 2900191783Srmacklem inp = sotoinpcb(so); 2901205010Srwatson KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL")); 2902205010Srwatson INP_RLOCK(inp); 2903205010Srwatson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 2904205010Srwatson INP_RUNLOCK(inp); 2905224086Szack error = EPIPE; 2906224086Szack goto out; 2907191783Srmacklem } 2908191783Srmacklem tp = intotcpcb(inp); 2909205010Srwatson if (tp->t_state != TCPS_ESTABLISHED) { 2910205010Srwatson INP_RUNLOCK(inp); 2911224086Szack error = EPIPE; 2912224086Szack goto out; 2913191783Srmacklem } 2914205010Srwatson *maxp = tp->snd_max; 2915205010Srwatson *unap = tp->snd_una; 2916191783Srmacklem INP_RUNLOCK(inp); 2917224086Szack 2918224086Szackout: 2919224086Szack NFSEXITCODE(error); 2920224086Szack return (error); 2921191783Srmacklem} 2922191783Srmacklem 2923191783Srmacklem/* 2924191783Srmacklem * This function needs to test to see if the system is near its limit 2925191783Srmacklem * for memory allocation via malloc() or mget() and return True iff 2926191783Srmacklem * either of these resources are near their limit. 2927191783Srmacklem * XXX (For now, this is just a stub.) 2928191783Srmacklem */ 2929191783Srmacklemint nfsrv_testmalloclimit = 0; 2930191783Srmacklemint 2931191783Srmacklemnfsrv_mallocmget_limit(void) 2932191783Srmacklem{ 2933191783Srmacklem static int printmesg = 0; 2934191783Srmacklem static int testval = 1; 2935191783Srmacklem 2936191783Srmacklem if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 2937191783Srmacklem if ((printmesg++ % 100) == 0) 2938191783Srmacklem printf("nfsd: malloc/mget near limit\n"); 2939191783Srmacklem return (1); 2940191783Srmacklem } 2941191783Srmacklem return (0); 2942191783Srmacklem} 2943191783Srmacklem 2944191783Srmacklem/* 2945191783Srmacklem * BSD specific initialization of a mount point. 2946191783Srmacklem */ 2947191783Srmacklemvoid 2948191783Srmacklemnfsd_mntinit(void) 2949191783Srmacklem{ 2950191783Srmacklem static int inited = 0; 2951191783Srmacklem 2952191783Srmacklem if (inited) 2953191783Srmacklem return; 2954191783Srmacklem inited = 1; 2955191783Srmacklem nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 2956191783Srmacklem TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist); 2957235626Smckusick TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist); 2958191783Srmacklem nfsv4root_mnt.mnt_export = NULL; 2959191783Srmacklem TAILQ_INIT(&nfsv4root_opt); 2960191783Srmacklem TAILQ_INIT(&nfsv4root_newopt); 2961191783Srmacklem nfsv4root_mnt.mnt_opt = &nfsv4root_opt; 2962191783Srmacklem nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt; 2963191783Srmacklem nfsv4root_mnt.mnt_nvnodelistsize = 0; 2964235626Smckusick nfsv4root_mnt.mnt_activevnodelistsize = 0; 2965191783Srmacklem} 2966191783Srmacklem 2967191783Srmacklem/* 2968191783Srmacklem * Get a vnode for a file handle, without checking exports, etc. 2969191783Srmacklem */ 2970191783Srmacklemstruct vnode * 2971191783Srmacklemnfsvno_getvp(fhandle_t *fhp) 2972191783Srmacklem{ 2973191783Srmacklem struct mount *mp; 2974191783Srmacklem struct vnode *vp; 2975191783Srmacklem int error; 2976191783Srmacklem 2977217017Srmacklem mp = vfs_busyfs(&fhp->fh_fsid); 2978191783Srmacklem if (mp == NULL) 2979191783Srmacklem return (NULL); 2980222167Srmacklem error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 2981217017Srmacklem vfs_unbusy(mp); 2982191783Srmacklem if (error) 2983191783Srmacklem return (NULL); 2984191783Srmacklem return (vp); 2985191783Srmacklem} 2986191783Srmacklem 2987191783Srmacklem/* 2988191783Srmacklem * Do a local VOP_ADVLOCK(). 2989191783Srmacklem */ 2990191783Srmacklemint 2991191783Srmacklemnfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 2992192256Srmacklem u_int64_t end, struct thread *td) 2993191783Srmacklem{ 2994224086Szack int error = 0; 2995191783Srmacklem struct flock fl; 2996191783Srmacklem u_int64_t tlen; 2997191783Srmacklem 2998212833Srmacklem if (nfsrv_dolocallocks == 0) 2999224086Szack goto out; 3000216893Srmacklem 3001216893Srmacklem /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */ 3002224086Szack if ((vp->v_iflag & VI_DOOMED) != 0) { 3003224086Szack error = EPERM; 3004224086Szack goto out; 3005224086Szack } 3006216893Srmacklem 3007191783Srmacklem fl.l_whence = SEEK_SET; 3008191783Srmacklem fl.l_type = ftype; 3009191783Srmacklem fl.l_start = (off_t)first; 3010191783Srmacklem if (end == NFS64BITSSET) { 3011191783Srmacklem fl.l_len = 0; 3012191783Srmacklem } else { 3013191783Srmacklem tlen = end - first; 3014191783Srmacklem fl.l_len = (off_t)tlen; 3015191783Srmacklem } 3016191783Srmacklem /* 3017192256Srmacklem * For FreeBSD8, the l_pid and l_sysid must be set to the same 3018192256Srmacklem * values for all calls, so that all locks will be held by the 3019192256Srmacklem * nfsd server. (The nfsd server handles conflicts between the 3020192256Srmacklem * various clients.) 3021192256Srmacklem * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 3022192256Srmacklem * bytes, so it can't be put in l_sysid. 3023191783Srmacklem */ 3024192503Srmacklem if (nfsv4_sysid == 0) 3025192503Srmacklem nfsv4_sysid = nlm_acquire_next_sysid(); 3026192256Srmacklem fl.l_pid = (pid_t)0; 3027192503Srmacklem fl.l_sysid = (int)nfsv4_sysid; 3028192256Srmacklem 3029224080Szack NFSVOPUNLOCK(vp, 0); 3030212833Srmacklem if (ftype == F_UNLCK) 3031212833Srmacklem error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 3032212833Srmacklem (F_POSIX | F_REMOTE)); 3033212833Srmacklem else 3034212833Srmacklem error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 3035212833Srmacklem (F_POSIX | F_REMOTE)); 3036224080Szack NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 3037224086Szack 3038224086Szackout: 3039224086Szack NFSEXITCODE(error); 3040191783Srmacklem return (error); 3041191783Srmacklem} 3042191783Srmacklem 3043191783Srmacklem/* 3044191783Srmacklem * Check the nfsv4 root exports. 3045191783Srmacklem */ 3046191783Srmacklemint 3047191783Srmacklemnfsvno_v4rootexport(struct nfsrv_descript *nd) 3048191783Srmacklem{ 3049191783Srmacklem struct ucred *credanon; 3050224086Szack int exflags, error = 0, numsecflavor, *secflavors, i; 3051191783Srmacklem 3052191783Srmacklem error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags, 3053192121Srmacklem &credanon, &numsecflavor, &secflavors); 3054224086Szack if (error) { 3055224086Szack error = NFSERR_PROGUNAVAIL; 3056224086Szack goto out; 3057224086Szack } 3058191940Skan if (credanon != NULL) 3059191940Skan crfree(credanon); 3060192121Srmacklem for (i = 0; i < numsecflavor; i++) { 3061192121Srmacklem if (secflavors[i] == AUTH_SYS) 3062192121Srmacklem nd->nd_flag |= ND_EXAUTHSYS; 3063192121Srmacklem else if (secflavors[i] == RPCSEC_GSS_KRB5) 3064192121Srmacklem nd->nd_flag |= ND_EXGSS; 3065192121Srmacklem else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3066192121Srmacklem nd->nd_flag |= ND_EXGSSINTEGRITY; 3067192121Srmacklem else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3068192121Srmacklem nd->nd_flag |= ND_EXGSSPRIVACY; 3069192121Srmacklem } 3070224086Szack 3071224086Szackout: 3072224086Szack NFSEXITCODE(error); 3073224086Szack return (error); 3074191783Srmacklem} 3075191783Srmacklem 3076191783Srmacklem/* 3077191783Srmacklem * Nfs server psuedo system call for the nfsd's 3078191783Srmacklem */ 3079191783Srmacklem/* 3080191783Srmacklem * MPSAFE 3081191783Srmacklem */ 3082191783Srmacklemstatic int 3083191783Srmacklemnfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3084191783Srmacklem{ 3085191783Srmacklem struct file *fp; 3086192017Srmacklem struct nfsd_addsock_args sockarg; 3087192017Srmacklem struct nfsd_nfsd_args nfsdarg; 3088191783Srmacklem int error; 3089191783Srmacklem 3090191783Srmacklem if (uap->flag & NFSSVC_NFSDADDSOCK) { 3091192017Srmacklem error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3092191783Srmacklem if (error) 3093224086Szack goto out; 3094224778Srwatson /* 3095224778Srwatson * Since we don't know what rights might be required, 3096224778Srwatson * pretend that we need them all. It is better to be too 3097224778Srwatson * careful than too reckless. 3098224778Srwatson */ 3099224778Srwatson if ((error = fget(td, sockarg.sock, CAP_SOCK_ALL, &fp)) != 0) 3100224086Szack goto out; 3101191783Srmacklem if (fp->f_type != DTYPE_SOCKET) { 3102191783Srmacklem fdrop(fp, td); 3103224086Szack error = EPERM; 3104224086Szack goto out; 3105191783Srmacklem } 3106191783Srmacklem error = nfsrvd_addsock(fp); 3107191783Srmacklem fdrop(fp, td); 3108191783Srmacklem } else if (uap->flag & NFSSVC_NFSDNFSD) { 3109224086Szack if (uap->argp == NULL) { 3110224086Szack error = EINVAL; 3111224086Szack goto out; 3112224086Szack } 3113192017Srmacklem error = copyin(uap->argp, (caddr_t)&nfsdarg, 3114192017Srmacklem sizeof (nfsdarg)); 3115192017Srmacklem if (error) 3116224086Szack goto out; 3117192017Srmacklem error = nfsrvd_nfsd(td, &nfsdarg); 3118191783Srmacklem } else { 3119191783Srmacklem error = nfssvc_srvcall(td, uap, td->td_ucred); 3120191783Srmacklem } 3121224086Szack 3122224086Szackout: 3123224086Szack NFSEXITCODE(error); 3124191783Srmacklem return (error); 3125191783Srmacklem} 3126191783Srmacklem 3127191783Srmacklemstatic int 3128191783Srmacklemnfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 3129191783Srmacklem{ 3130191783Srmacklem struct nfsex_args export; 3131191783Srmacklem struct file *fp = NULL; 3132191783Srmacklem int stablefd, len; 3133191783Srmacklem struct nfsd_clid adminrevoke; 3134191783Srmacklem struct nfsd_dumplist dumplist; 3135191783Srmacklem struct nfsd_dumpclients *dumpclients; 3136191783Srmacklem struct nfsd_dumplocklist dumplocklist; 3137191783Srmacklem struct nfsd_dumplocks *dumplocks; 3138191783Srmacklem struct nameidata nd; 3139191783Srmacklem vnode_t vp; 3140243738Srmacklem int error = EINVAL, igotlock; 3141217432Srmacklem struct proc *procp; 3142243738Srmacklem static int suspend_nfsd = 0; 3143191783Srmacklem 3144191783Srmacklem if (uap->flag & NFSSVC_PUBLICFH) { 3145191783Srmacklem NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 3146191783Srmacklem sizeof (fhandle_t)); 3147191783Srmacklem error = copyin(uap->argp, 3148191783Srmacklem &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 3149191783Srmacklem if (!error) 3150191783Srmacklem nfs_pubfhset = 1; 3151191783Srmacklem } else if (uap->flag & NFSSVC_V4ROOTEXPORT) { 3152191783Srmacklem error = copyin(uap->argp,(caddr_t)&export, 3153191783Srmacklem sizeof (struct nfsex_args)); 3154191783Srmacklem if (!error) 3155191783Srmacklem error = nfsrv_v4rootexport(&export, cred, p); 3156191783Srmacklem } else if (uap->flag & NFSSVC_NOPUBLICFH) { 3157191783Srmacklem nfs_pubfhset = 0; 3158191783Srmacklem error = 0; 3159191783Srmacklem } else if (uap->flag & NFSSVC_STABLERESTART) { 3160191783Srmacklem error = copyin(uap->argp, (caddr_t)&stablefd, 3161191783Srmacklem sizeof (int)); 3162191783Srmacklem if (!error) 3163191783Srmacklem error = fp_getfvp(p, stablefd, &fp, &vp); 3164191783Srmacklem if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 3165191783Srmacklem error = EBADF; 3166191783Srmacklem if (!error && newnfs_numnfsd != 0) 3167191783Srmacklem error = EPERM; 3168191783Srmacklem if (!error) { 3169191783Srmacklem nfsrv_stablefirst.nsf_fp = fp; 3170191783Srmacklem nfsrv_setupstable(p); 3171191783Srmacklem } 3172191783Srmacklem } else if (uap->flag & NFSSVC_ADMINREVOKE) { 3173191783Srmacklem error = copyin(uap->argp, (caddr_t)&adminrevoke, 3174191783Srmacklem sizeof (struct nfsd_clid)); 3175191783Srmacklem if (!error) 3176191783Srmacklem error = nfsrv_adminrevoke(&adminrevoke, p); 3177191783Srmacklem } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 3178191783Srmacklem error = copyin(uap->argp, (caddr_t)&dumplist, 3179191783Srmacklem sizeof (struct nfsd_dumplist)); 3180191783Srmacklem if (!error && (dumplist.ndl_size < 1 || 3181191783Srmacklem dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 3182191783Srmacklem error = EPERM; 3183191783Srmacklem if (!error) { 3184191783Srmacklem len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 3185191783Srmacklem dumpclients = (struct nfsd_dumpclients *)malloc(len, 3186191783Srmacklem M_TEMP, M_WAITOK); 3187191783Srmacklem nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 3188191783Srmacklem error = copyout(dumpclients, 3189191783Srmacklem CAST_USER_ADDR_T(dumplist.ndl_list), len); 3190191783Srmacklem free((caddr_t)dumpclients, M_TEMP); 3191191783Srmacklem } 3192191783Srmacklem } else if (uap->flag & NFSSVC_DUMPLOCKS) { 3193191783Srmacklem error = copyin(uap->argp, (caddr_t)&dumplocklist, 3194191783Srmacklem sizeof (struct nfsd_dumplocklist)); 3195191783Srmacklem if (!error && (dumplocklist.ndllck_size < 1 || 3196191783Srmacklem dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 3197191783Srmacklem error = EPERM; 3198191783Srmacklem if (!error) 3199191783Srmacklem error = nfsrv_lookupfilename(&nd, 3200191783Srmacklem dumplocklist.ndllck_fname, p); 3201191783Srmacklem if (!error) { 3202191783Srmacklem len = sizeof (struct nfsd_dumplocks) * 3203191783Srmacklem dumplocklist.ndllck_size; 3204191783Srmacklem dumplocks = (struct nfsd_dumplocks *)malloc(len, 3205191783Srmacklem M_TEMP, M_WAITOK); 3206191783Srmacklem nfsrv_dumplocks(nd.ni_vp, dumplocks, 3207191783Srmacklem dumplocklist.ndllck_size, p); 3208191783Srmacklem vput(nd.ni_vp); 3209191783Srmacklem error = copyout(dumplocks, 3210191783Srmacklem CAST_USER_ADDR_T(dumplocklist.ndllck_list), len); 3211191783Srmacklem free((caddr_t)dumplocks, M_TEMP); 3212191783Srmacklem } 3213217432Srmacklem } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 3214217432Srmacklem procp = p->td_proc; 3215217432Srmacklem PROC_LOCK(procp); 3216217432Srmacklem nfsd_master_pid = procp->p_pid; 3217217432Srmacklem bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 3218217432Srmacklem nfsd_master_start = procp->p_stats->p_start; 3219217432Srmacklem nfsd_master_proc = procp; 3220217432Srmacklem PROC_UNLOCK(procp); 3221243738Srmacklem } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) { 3222243738Srmacklem NFSLOCKV4ROOTMUTEX(); 3223243738Srmacklem if (suspend_nfsd == 0) { 3224243738Srmacklem /* Lock out all nfsd threads */ 3225243738Srmacklem do { 3226243738Srmacklem igotlock = nfsv4_lock(&nfsd_suspend_lock, 1, 3227243738Srmacklem NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 3228243738Srmacklem } while (igotlock == 0 && suspend_nfsd == 0); 3229243738Srmacklem suspend_nfsd = 1; 3230243738Srmacklem } 3231243738Srmacklem NFSUNLOCKV4ROOTMUTEX(); 3232243738Srmacklem error = 0; 3233243738Srmacklem } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) { 3234243738Srmacklem NFSLOCKV4ROOTMUTEX(); 3235243738Srmacklem if (suspend_nfsd != 0) { 3236243738Srmacklem nfsv4_unlock(&nfsd_suspend_lock, 0); 3237243738Srmacklem suspend_nfsd = 0; 3238243738Srmacklem } 3239243738Srmacklem NFSUNLOCKV4ROOTMUTEX(); 3240243738Srmacklem error = 0; 3241191783Srmacklem } 3242224086Szack 3243224086Szack NFSEXITCODE(error); 3244191783Srmacklem return (error); 3245191783Srmacklem} 3246191783Srmacklem 3247192121Srmacklem/* 3248192121Srmacklem * Check exports. 3249192121Srmacklem * Returns 0 if ok, 1 otherwise. 3250192121Srmacklem */ 3251192121Srmacklemint 3252192121Srmacklemnfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 3253192121Srmacklem{ 3254192121Srmacklem int i; 3255192121Srmacklem 3256192121Srmacklem /* 3257192121Srmacklem * This seems odd, but allow the case where the security flavor 3258192121Srmacklem * list is empty. This happens when NFSv4 is traversing non-exported 3259192121Srmacklem * file systems. Exported file systems should always have a non-empty 3260192121Srmacklem * security flavor list. 3261192121Srmacklem */ 3262192121Srmacklem if (exp->nes_numsecflavor == 0) 3263192121Srmacklem return (0); 3264192121Srmacklem 3265192121Srmacklem for (i = 0; i < exp->nes_numsecflavor; i++) { 3266192121Srmacklem /* 3267192121Srmacklem * The tests for privacy and integrity must be first, 3268192121Srmacklem * since ND_GSS is set for everything but AUTH_SYS. 3269192121Srmacklem */ 3270192121Srmacklem if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 3271192121Srmacklem (nd->nd_flag & ND_GSSPRIVACY)) 3272192121Srmacklem return (0); 3273192121Srmacklem if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 3274192121Srmacklem (nd->nd_flag & ND_GSSINTEGRITY)) 3275192121Srmacklem return (0); 3276192121Srmacklem if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 3277192121Srmacklem (nd->nd_flag & ND_GSS)) 3278192121Srmacklem return (0); 3279192121Srmacklem if (exp->nes_secflavors[i] == AUTH_SYS && 3280192121Srmacklem (nd->nd_flag & ND_GSS) == 0) 3281192121Srmacklem return (0); 3282192121Srmacklem } 3283192121Srmacklem return (1); 3284192121Srmacklem} 3285192121Srmacklem 3286214224Srmacklem/* 3287214224Srmacklem * Calculate a hash value for the fid in a file handle. 3288214224Srmacklem */ 3289214255Srmacklemuint32_t 3290214224Srmacklemnfsrv_hashfh(fhandle_t *fhp) 3291214224Srmacklem{ 3292214255Srmacklem uint32_t hashval; 3293214224Srmacklem 3294214255Srmacklem hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 3295214224Srmacklem return (hashval); 3296214224Srmacklem} 3297214224Srmacklem 3298217432Srmacklem/* 3299217432Srmacklem * Signal the userland master nfsd to backup the stable restart file. 3300217432Srmacklem */ 3301217432Srmacklemvoid 3302217432Srmacklemnfsrv_backupstable(void) 3303217432Srmacklem{ 3304217432Srmacklem struct proc *procp; 3305217432Srmacklem 3306217432Srmacklem if (nfsd_master_proc != NULL) { 3307217432Srmacklem procp = pfind(nfsd_master_pid); 3308217432Srmacklem /* Try to make sure it is the correct process. */ 3309217432Srmacklem if (procp == nfsd_master_proc && 3310217432Srmacklem procp->p_stats->p_start.tv_sec == 3311217432Srmacklem nfsd_master_start.tv_sec && 3312217432Srmacklem procp->p_stats->p_start.tv_usec == 3313217432Srmacklem nfsd_master_start.tv_usec && 3314217432Srmacklem strcmp(procp->p_comm, nfsd_master_comm) == 0) 3315225617Skmacy kern_psignal(procp, SIGUSR2); 3316217432Srmacklem else 3317217432Srmacklem nfsd_master_proc = NULL; 3318217432Srmacklem 3319217432Srmacklem if (procp != NULL) 3320217432Srmacklem PROC_UNLOCK(procp); 3321217432Srmacklem } 3322217432Srmacklem} 3323217432Srmacklem 3324191783Srmacklemextern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 3325191783Srmacklem 3326191783Srmacklem/* 3327191783Srmacklem * Called once to initialize data structures... 3328191783Srmacklem */ 3329191783Srmacklemstatic int 3330191783Srmacklemnfsd_modevent(module_t mod, int type, void *data) 3331191783Srmacklem{ 3332255532Srmacklem int error = 0, i; 3333191783Srmacklem static int loaded = 0; 3334191783Srmacklem 3335191783Srmacklem switch (type) { 3336191783Srmacklem case MOD_LOAD: 3337191783Srmacklem if (loaded) 3338224086Szack goto out; 3339191783Srmacklem newnfs_portinit(); 3340255532Srmacklem for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 3341255532Srmacklem snprintf(nfsrchash_table[i].lock_name, 3342255532Srmacklem sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d", 3343255532Srmacklem i); 3344255532Srmacklem mtx_init(&nfsrchash_table[i].mtx, 3345255532Srmacklem nfsrchash_table[i].lock_name, NULL, MTX_DEF); 3346255532Srmacklem } 3347255532Srmacklem mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF); 3348191783Srmacklem mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF); 3349191783Srmacklem mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL, 3350191783Srmacklem MTX_DEF); 3351191783Srmacklem lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0); 3352191783Srmacklem nfsrvd_initcache(); 3353191783Srmacklem nfsd_init(); 3354191783Srmacklem NFSD_LOCK(); 3355191783Srmacklem nfsrvd_init(0); 3356191783Srmacklem NFSD_UNLOCK(); 3357191783Srmacklem nfsd_mntinit(); 3358191783Srmacklem#ifdef VV_DISABLEDELEG 3359191783Srmacklem vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 3360191783Srmacklem vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 3361191783Srmacklem#endif 3362191783Srmacklem nfsd_call_servertimer = nfsrv_servertimer; 3363191783Srmacklem nfsd_call_nfsd = nfssvc_nfsd; 3364191783Srmacklem loaded = 1; 3365191783Srmacklem break; 3366191783Srmacklem 3367191783Srmacklem case MOD_UNLOAD: 3368191783Srmacklem if (newnfs_numnfsd != 0) { 3369191783Srmacklem error = EBUSY; 3370191783Srmacklem break; 3371191783Srmacklem } 3372191783Srmacklem 3373191783Srmacklem#ifdef VV_DISABLEDELEG 3374191783Srmacklem vn_deleg_ops.vndeleg_recall = NULL; 3375191783Srmacklem vn_deleg_ops.vndeleg_disable = NULL; 3376191783Srmacklem#endif 3377191783Srmacklem nfsd_call_servertimer = NULL; 3378191783Srmacklem nfsd_call_nfsd = NULL; 3379217335Szack 3380220530Srmacklem /* Clean out all NFSv4 state. */ 3381220530Srmacklem nfsrv_throwawayallstate(curthread); 3382220530Srmacklem 3383217335Szack /* Clean the NFS server reply cache */ 3384217335Szack nfsrvd_cleancache(); 3385217335Szack 3386220530Srmacklem /* Free up the krpc server pool. */ 3387220530Srmacklem if (nfsrvd_pool != NULL) 3388220530Srmacklem svcpool_destroy(nfsrvd_pool); 3389220530Srmacklem 3390191783Srmacklem /* and get rid of the locks */ 3391255532Srmacklem for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) 3392255532Srmacklem mtx_destroy(&nfsrchash_table[i].mtx); 3393255532Srmacklem mtx_destroy(&nfsrc_udpmtx); 3394191783Srmacklem mtx_destroy(&nfs_v4root_mutex); 3395191783Srmacklem mtx_destroy(&nfsv4root_mnt.mnt_mtx); 3396191783Srmacklem lockdestroy(&nfsv4root_mnt.mnt_explock); 3397191783Srmacklem loaded = 0; 3398191783Srmacklem break; 3399191783Srmacklem default: 3400191783Srmacklem error = EOPNOTSUPP; 3401191783Srmacklem break; 3402191783Srmacklem } 3403224086Szack 3404224086Szackout: 3405224086Szack NFSEXITCODE(error); 3406224086Szack return (error); 3407191783Srmacklem} 3408191783Srmacklemstatic moduledata_t nfsd_mod = { 3409191783Srmacklem "nfsd", 3410191783Srmacklem nfsd_modevent, 3411191783Srmacklem NULL, 3412191783Srmacklem}; 3413191783SrmacklemDECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 3414191783Srmacklem 3415191783Srmacklem/* So that loader and kldload(2) can find us, wherever we are.. */ 3416191783SrmacklemMODULE_VERSION(nfsd, 1); 3417191783SrmacklemMODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 3418216931SrmacklemMODULE_DEPEND(nfsd, nfslock, 1, 1, 1); 3419192574SrmacklemMODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 3420209191SrmacklemMODULE_DEPEND(nfsd, krpc, 1, 1, 1); 3421209191SrmacklemMODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 3422191783Srmacklem 3423