nfs_nfsdport.c revision 229617
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 229617 2012-01-05 18:50:12Z jhb $"); 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; 60191783Srmacklemstruct vfsoptlist nfsv4root_opt, nfsv4root_newopt; 61191783SrmacklemNFSDLOCKMUTEX; 62191783Srmacklemstruct mtx nfs_cache_mutex; 63191783Srmacklemstruct mtx nfs_v4root_mutex; 64191783Srmacklemstruct nfsrvfh nfs_rootfh, nfs_pubfh; 65191783Srmacklemint nfs_pubfhset = 0, nfs_rootfhset = 0; 66217432Srmacklemstruct proc *nfsd_master_proc = NULL; 67217432Srmacklemstatic pid_t nfsd_master_pid = (pid_t)-1; 68217432Srmacklemstatic char nfsd_master_comm[MAXCOMLEN + 1]; 69217432Srmacklemstatic struct timeval nfsd_master_start; 70192503Srmacklemstatic uint32_t nfsv4_sysid = 0; 71191783Srmacklem 72192255Srmacklemstatic int nfssvc_srvcall(struct thread *, struct nfssvc_args *, 73192255Srmacklem struct ucred *); 74191783Srmacklem 75220645Srmacklemint nfsrv_enable_crossmntpt = 1; 76191783Srmacklemstatic int nfs_commit_blks; 77191783Srmacklemstatic int nfs_commit_miss; 78191783Srmacklemextern int nfsrv_issuedelegs; 79192255Srmacklemextern int nfsrv_dolocallocks; 80192255Srmacklem 81221615SrmacklemSYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server"); 82221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW, 83220645Srmacklem &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points"); 84221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 85192255Srmacklem 0, ""); 86221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 87192255Srmacklem 0, ""); 88221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW, 89192255Srmacklem &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 90221615SrmacklemSYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW, 91192255Srmacklem &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 92191783Srmacklem 93229617Sjhb#define MAX_REORDERED_RPC 16 94229617Sjhb#define NUM_HEURISTIC 1031 95191783Srmacklem#define NHUSE_INIT 64 96191783Srmacklem#define NHUSE_INC 16 97191783Srmacklem#define NHUSE_MAX 2048 98191783Srmacklem 99191783Srmacklemstatic struct nfsheur { 100191783Srmacklem struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 101229617Sjhb off_t nh_nextoff; /* next offset for sequential detection */ 102191783Srmacklem int nh_use; /* use count for selection */ 103191783Srmacklem int nh_seqcount; /* heuristic */ 104191783Srmacklem} nfsheur[NUM_HEURISTIC]; 105191783Srmacklem 106191783Srmacklem 107191783Srmacklem/* 108229617Sjhb * Heuristic to detect sequential operation. 109229617Sjhb */ 110229617Sjhbstatic struct nfsheur * 111229617Sjhbnfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp) 112229617Sjhb{ 113229617Sjhb struct nfsheur *nh; 114229617Sjhb int hi, try; 115229617Sjhb 116229617Sjhb /* Locate best candidate. */ 117229617Sjhb try = 32; 118229617Sjhb hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 119229617Sjhb nh = &nfsheur[hi]; 120229617Sjhb while (try--) { 121229617Sjhb if (nfsheur[hi].nh_vp == vp) { 122229617Sjhb nh = &nfsheur[hi]; 123229617Sjhb break; 124229617Sjhb } 125229617Sjhb if (nfsheur[hi].nh_use > 0) 126229617Sjhb --nfsheur[hi].nh_use; 127229617Sjhb hi = (hi + 1) % NUM_HEURISTIC; 128229617Sjhb if (nfsheur[hi].nh_use < nh->nh_use) 129229617Sjhb nh = &nfsheur[hi]; 130229617Sjhb } 131229617Sjhb 132229617Sjhb /* Initialize hint if this is a new file. */ 133229617Sjhb if (nh->nh_vp != vp) { 134229617Sjhb nh->nh_vp = vp; 135229617Sjhb nh->nh_nextoff = uio->uio_offset; 136229617Sjhb nh->nh_use = NHUSE_INIT; 137229617Sjhb if (uio->uio_offset == 0) 138229617Sjhb nh->nh_seqcount = 4; 139229617Sjhb else 140229617Sjhb nh->nh_seqcount = 1; 141229617Sjhb } 142229617Sjhb 143229617Sjhb /* Calculate heuristic. */ 144229617Sjhb if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) || 145229617Sjhb uio->uio_offset == nh->nh_nextoff) { 146229617Sjhb /* See comments in vfs_vnops.c:sequential_heuristic(). */ 147229617Sjhb nh->nh_seqcount += howmany(uio->uio_resid, 16384); 148229617Sjhb if (nh->nh_seqcount > IO_SEQMAX) 149229617Sjhb nh->nh_seqcount = IO_SEQMAX; 150229617Sjhb } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC * 151229617Sjhb imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) { 152229617Sjhb /* Probably a reordered RPC, leave seqcount alone. */ 153229617Sjhb } else if (nh->nh_seqcount > 1) { 154229617Sjhb nh->nh_seqcount /= 2; 155229617Sjhb } else { 156229617Sjhb nh->nh_seqcount = 0; 157229617Sjhb } 158229617Sjhb nh->nh_use += NHUSE_INC; 159229617Sjhb if (nh->nh_use > NHUSE_MAX) 160229617Sjhb nh->nh_use = NHUSE_MAX; 161229617Sjhb return (nh); 162229617Sjhb} 163229617Sjhb 164229617Sjhb/* 165191783Srmacklem * Get attributes into nfsvattr structure. 166191783Srmacklem */ 167191783Srmacklemint 168191783Srmacklemnfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 169216693Srmacklem struct thread *p, int vpislocked) 170191783Srmacklem{ 171191783Srmacklem int error, lockedit = 0; 172191783Srmacklem 173216693Srmacklem if (vpislocked == 0) { 174216693Srmacklem /* 175216693Srmacklem * When vpislocked == 0, the vnode is either exclusively 176216693Srmacklem * locked by this thread or not locked by this thread. 177216693Srmacklem * As such, shared lock it, if not exclusively locked. 178216693Srmacklem */ 179224083Szack if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 180216693Srmacklem lockedit = 1; 181224081Szack NFSVOPLOCK(vp, LK_SHARED | LK_RETRY); 182216693Srmacklem } 183191783Srmacklem } 184191783Srmacklem error = VOP_GETATTR(vp, &nvap->na_vattr, cred); 185216693Srmacklem if (lockedit != 0) 186224082Szack NFSVOPUNLOCK(vp, 0); 187224086Szack 188224086Szack NFSEXITCODE(error); 189191783Srmacklem return (error); 190191783Srmacklem} 191191783Srmacklem 192191783Srmacklem/* 193191783Srmacklem * Get a file handle for a vnode. 194191783Srmacklem */ 195191783Srmacklemint 196191783Srmacklemnfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 197191783Srmacklem{ 198191783Srmacklem int error; 199191783Srmacklem 200191783Srmacklem NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 201191783Srmacklem fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 202191783Srmacklem error = VOP_VPTOFH(vp, &fhp->fh_fid); 203224086Szack 204224086Szack NFSEXITCODE(error); 205191783Srmacklem return (error); 206191783Srmacklem} 207191783Srmacklem 208191783Srmacklem/* 209191783Srmacklem * Perform access checking for vnodes obtained from file handles that would 210191783Srmacklem * refer to files already opened by a Unix client. You cannot just use 211200999Srmacklem * vn_writechk() and VOP_ACCESSX() for two reasons. 212200999Srmacklem * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 213200999Srmacklem * case. 214191783Srmacklem * 2 - The owner is to be given access irrespective of mode bits for some 215191783Srmacklem * operations, so that processes that chmod after opening a file don't 216191783Srmacklem * break. 217191783Srmacklem */ 218191783Srmacklemint 219200999Srmacklemnfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 220200999Srmacklem struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 221200999Srmacklem u_int32_t *supportedtypep) 222191783Srmacklem{ 223191783Srmacklem struct vattr vattr; 224191783Srmacklem int error = 0, getret = 0; 225191783Srmacklem 226216893Srmacklem if (vpislocked == 0) { 227224086Szack if (NFSVOPLOCK(vp, LK_SHARED) != 0) { 228224086Szack error = EPERM; 229224086Szack goto out; 230224086Szack } 231216893Srmacklem } 232191783Srmacklem if (accmode & VWRITE) { 233191783Srmacklem /* Just vn_writechk() changed to check rdonly */ 234191783Srmacklem /* 235191783Srmacklem * Disallow write attempts on read-only file systems; 236191783Srmacklem * unless the file is a socket or a block or character 237191783Srmacklem * device resident on the file system. 238191783Srmacklem */ 239191783Srmacklem if (NFSVNO_EXRDONLY(exp) || 240200999Srmacklem (vp->v_mount->mnt_flag & MNT_RDONLY)) { 241191783Srmacklem switch (vp->v_type) { 242191783Srmacklem case VREG: 243191783Srmacklem case VDIR: 244191783Srmacklem case VLNK: 245216893Srmacklem error = EROFS; 246191783Srmacklem default: 247191783Srmacklem break; 248191783Srmacklem } 249191783Srmacklem } 250191783Srmacklem /* 251191783Srmacklem * If there's shared text associated with 252191783Srmacklem * the inode, try to free it up once. If 253191783Srmacklem * we fail, we can't allow writing. 254191783Srmacklem */ 255216893Srmacklem if ((vp->v_vflag & VV_TEXT) != 0 && error == 0) 256216893Srmacklem error = ETXTBSY; 257191783Srmacklem } 258216893Srmacklem if (error != 0) { 259216893Srmacklem if (vpislocked == 0) 260224082Szack NFSVOPUNLOCK(vp, 0); 261224086Szack goto out; 262216893Srmacklem } 263191783Srmacklem 264191783Srmacklem /* 265191783Srmacklem * Should the override still be applied when ACLs are enabled? 266191783Srmacklem */ 267200999Srmacklem error = VOP_ACCESSX(vp, accmode, cred, p); 268200999Srmacklem if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 269200999Srmacklem /* 270200999Srmacklem * Try again with VEXPLICIT_DENY, to see if the test for 271200999Srmacklem * deletion is supported. 272200999Srmacklem */ 273200999Srmacklem error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 274200999Srmacklem if (error == 0) { 275200999Srmacklem if (vp->v_type == VDIR) { 276200999Srmacklem accmode &= ~(VDELETE | VDELETE_CHILD); 277200999Srmacklem accmode |= VWRITE; 278200999Srmacklem error = VOP_ACCESSX(vp, accmode, cred, p); 279200999Srmacklem } else if (supportedtypep != NULL) { 280200999Srmacklem *supportedtypep &= ~NFSACCESS_DELETE; 281200999Srmacklem } 282200999Srmacklem } 283200999Srmacklem } 284191783Srmacklem 285191783Srmacklem /* 286191783Srmacklem * Allow certain operations for the owner (reads and writes 287191783Srmacklem * on files that are already open). 288191783Srmacklem */ 289191783Srmacklem if (override != NFSACCCHK_NOOVERRIDE && 290191783Srmacklem (error == EPERM || error == EACCES)) { 291191783Srmacklem if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 292191783Srmacklem error = 0; 293191783Srmacklem else if (override & NFSACCCHK_ALLOWOWNER) { 294191783Srmacklem getret = VOP_GETATTR(vp, &vattr, cred); 295191783Srmacklem if (getret == 0 && cred->cr_uid == vattr.va_uid) 296191783Srmacklem error = 0; 297191783Srmacklem } 298191783Srmacklem } 299191783Srmacklem if (vpislocked == 0) 300224082Szack NFSVOPUNLOCK(vp, 0); 301224086Szack 302224086Szackout: 303224086Szack NFSEXITCODE(error); 304191783Srmacklem return (error); 305191783Srmacklem} 306191783Srmacklem 307191783Srmacklem/* 308191783Srmacklem * Set attribute(s) vnop. 309191783Srmacklem */ 310191783Srmacklemint 311191783Srmacklemnfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 312191783Srmacklem struct thread *p, struct nfsexstuff *exp) 313191783Srmacklem{ 314191783Srmacklem int error; 315191783Srmacklem 316191783Srmacklem error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 317224086Szack NFSEXITCODE(error); 318191783Srmacklem return (error); 319191783Srmacklem} 320191783Srmacklem 321191783Srmacklem/* 322191783Srmacklem * Set up nameidata for a lookup() call and do it 323191783Srmacklem * For the cases where we are crossing mount points 324191783Srmacklem * (looking up the public fh path or the v4 root path when 325191783Srmacklem * not using a pseudo-root fs), set/release the Giant lock, 326191783Srmacklem * as required. 327191783Srmacklem */ 328191783Srmacklemint 329191783Srmacklemnfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 330191783Srmacklem struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 331191783Srmacklem struct vnode **retdirp) 332191783Srmacklem{ 333191783Srmacklem struct componentname *cnp = &ndp->ni_cnd; 334191783Srmacklem int i; 335191783Srmacklem struct iovec aiov; 336191783Srmacklem struct uio auio; 337191783Srmacklem int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 338191783Srmacklem int error = 0, crossmnt; 339191783Srmacklem char *cp; 340191783Srmacklem 341191783Srmacklem *retdirp = NULL; 342191783Srmacklem cnp->cn_nameptr = cnp->cn_pnbuf; 343225356Srmacklem ndp->ni_strictrelative = 0; 344191783Srmacklem /* 345191783Srmacklem * Extract and set starting directory. 346191783Srmacklem */ 347191783Srmacklem if (dp->v_type != VDIR) { 348191783Srmacklem if (islocked) 349191783Srmacklem vput(dp); 350191783Srmacklem else 351191783Srmacklem vrele(dp); 352191783Srmacklem nfsvno_relpathbuf(ndp); 353224086Szack error = ENOTDIR; 354224086Szack goto out1; 355191783Srmacklem } 356191783Srmacklem if (islocked) 357224080Szack NFSVOPUNLOCK(dp, 0); 358191783Srmacklem VREF(dp); 359191783Srmacklem *retdirp = dp; 360191783Srmacklem if (NFSVNO_EXRDONLY(exp)) 361191783Srmacklem cnp->cn_flags |= RDONLY; 362191783Srmacklem ndp->ni_segflg = UIO_SYSSPACE; 363191783Srmacklem crossmnt = 1; 364191783Srmacklem 365191783Srmacklem if (nd->nd_flag & ND_PUBLOOKUP) { 366191783Srmacklem ndp->ni_loopcnt = 0; 367191783Srmacklem if (cnp->cn_pnbuf[0] == '/') { 368191783Srmacklem vrele(dp); 369191783Srmacklem /* 370191783Srmacklem * Check for degenerate pathnames here, since lookup() 371191783Srmacklem * panics on them. 372191783Srmacklem */ 373191783Srmacklem for (i = 1; i < ndp->ni_pathlen; i++) 374191783Srmacklem if (cnp->cn_pnbuf[i] != '/') 375191783Srmacklem break; 376191783Srmacklem if (i == ndp->ni_pathlen) { 377191783Srmacklem error = NFSERR_ACCES; 378191783Srmacklem goto out; 379191783Srmacklem } 380191783Srmacklem dp = rootvnode; 381191783Srmacklem VREF(dp); 382191783Srmacklem } 383220645Srmacklem } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 384191783Srmacklem (nd->nd_flag & ND_NFSV4) == 0) { 385191783Srmacklem /* 386191783Srmacklem * Only cross mount points for NFSv4 when doing a 387191783Srmacklem * mount while traversing the file system above 388220645Srmacklem * the mount point, unless nfsrv_enable_crossmntpt is set. 389191783Srmacklem */ 390191783Srmacklem cnp->cn_flags |= NOCROSSMOUNT; 391191783Srmacklem crossmnt = 0; 392191783Srmacklem } 393191783Srmacklem 394191783Srmacklem /* 395191783Srmacklem * Initialize for scan, set ni_startdir and bump ref on dp again 396191783Srmacklem * becuase lookup() will dereference ni_startdir. 397191783Srmacklem */ 398191783Srmacklem 399191783Srmacklem cnp->cn_thread = p; 400191783Srmacklem ndp->ni_startdir = dp; 401191783Srmacklem ndp->ni_rootdir = rootvnode; 402191783Srmacklem 403191783Srmacklem if (!lockleaf) 404191783Srmacklem cnp->cn_flags |= LOCKLEAF; 405191783Srmacklem for (;;) { 406191783Srmacklem cnp->cn_nameptr = cnp->cn_pnbuf; 407191783Srmacklem /* 408191783Srmacklem * Call lookup() to do the real work. If an error occurs, 409191783Srmacklem * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 410191783Srmacklem * we do not have to dereference anything before returning. 411191783Srmacklem * In either case ni_startdir will be dereferenced and NULLed 412191783Srmacklem * out. 413191783Srmacklem */ 414191783Srmacklem error = lookup(ndp); 415191783Srmacklem if (error) 416191783Srmacklem break; 417191783Srmacklem 418191783Srmacklem /* 419191783Srmacklem * Check for encountering a symbolic link. Trivial 420191783Srmacklem * termination occurs if no symlink encountered. 421191783Srmacklem */ 422191783Srmacklem if ((cnp->cn_flags & ISSYMLINK) == 0) { 423191783Srmacklem if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 424191783Srmacklem nfsvno_relpathbuf(ndp); 425191783Srmacklem if (ndp->ni_vp && !lockleaf) 426224080Szack NFSVOPUNLOCK(ndp->ni_vp, 0); 427191783Srmacklem break; 428191783Srmacklem } 429191783Srmacklem 430191783Srmacklem /* 431191783Srmacklem * Validate symlink 432191783Srmacklem */ 433191783Srmacklem if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 434224080Szack NFSVOPUNLOCK(ndp->ni_dvp, 0); 435191783Srmacklem if (!(nd->nd_flag & ND_PUBLOOKUP)) { 436191783Srmacklem error = EINVAL; 437191783Srmacklem goto badlink2; 438191783Srmacklem } 439191783Srmacklem 440191783Srmacklem if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 441191783Srmacklem error = ELOOP; 442191783Srmacklem goto badlink2; 443191783Srmacklem } 444191783Srmacklem if (ndp->ni_pathlen > 1) 445191783Srmacklem cp = uma_zalloc(namei_zone, M_WAITOK); 446191783Srmacklem else 447191783Srmacklem cp = cnp->cn_pnbuf; 448191783Srmacklem aiov.iov_base = cp; 449191783Srmacklem aiov.iov_len = MAXPATHLEN; 450191783Srmacklem auio.uio_iov = &aiov; 451191783Srmacklem auio.uio_iovcnt = 1; 452191783Srmacklem auio.uio_offset = 0; 453191783Srmacklem auio.uio_rw = UIO_READ; 454191783Srmacklem auio.uio_segflg = UIO_SYSSPACE; 455191783Srmacklem auio.uio_td = NULL; 456191783Srmacklem auio.uio_resid = MAXPATHLEN; 457191783Srmacklem error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 458191783Srmacklem if (error) { 459191783Srmacklem badlink1: 460191783Srmacklem if (ndp->ni_pathlen > 1) 461191783Srmacklem uma_zfree(namei_zone, cp); 462191783Srmacklem badlink2: 463191783Srmacklem vrele(ndp->ni_dvp); 464191783Srmacklem vput(ndp->ni_vp); 465191783Srmacklem break; 466191783Srmacklem } 467191783Srmacklem linklen = MAXPATHLEN - auio.uio_resid; 468191783Srmacklem if (linklen == 0) { 469191783Srmacklem error = ENOENT; 470191783Srmacklem goto badlink1; 471191783Srmacklem } 472191783Srmacklem if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 473191783Srmacklem error = ENAMETOOLONG; 474191783Srmacklem goto badlink1; 475191783Srmacklem } 476191783Srmacklem 477191783Srmacklem /* 478191783Srmacklem * Adjust or replace path 479191783Srmacklem */ 480191783Srmacklem if (ndp->ni_pathlen > 1) { 481191783Srmacklem NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 482191783Srmacklem uma_zfree(namei_zone, cnp->cn_pnbuf); 483191783Srmacklem cnp->cn_pnbuf = cp; 484191783Srmacklem } else 485191783Srmacklem cnp->cn_pnbuf[linklen] = '\0'; 486191783Srmacklem ndp->ni_pathlen += linklen; 487191783Srmacklem 488191783Srmacklem /* 489191783Srmacklem * Cleanup refs for next loop and check if root directory 490191783Srmacklem * should replace current directory. Normally ni_dvp 491191783Srmacklem * becomes the new base directory and is cleaned up when 492191783Srmacklem * we loop. Explicitly null pointers after invalidation 493191783Srmacklem * to clarify operation. 494191783Srmacklem */ 495191783Srmacklem vput(ndp->ni_vp); 496191783Srmacklem ndp->ni_vp = NULL; 497191783Srmacklem 498191783Srmacklem if (cnp->cn_pnbuf[0] == '/') { 499191783Srmacklem vrele(ndp->ni_dvp); 500191783Srmacklem ndp->ni_dvp = ndp->ni_rootdir; 501191783Srmacklem VREF(ndp->ni_dvp); 502191783Srmacklem } 503191783Srmacklem ndp->ni_startdir = ndp->ni_dvp; 504191783Srmacklem ndp->ni_dvp = NULL; 505191783Srmacklem } 506191783Srmacklem if (!lockleaf) 507191783Srmacklem cnp->cn_flags &= ~LOCKLEAF; 508191783Srmacklem 509191783Srmacklemout: 510191783Srmacklem if (error) { 511191783Srmacklem uma_zfree(namei_zone, cnp->cn_pnbuf); 512191783Srmacklem ndp->ni_vp = NULL; 513191783Srmacklem ndp->ni_dvp = NULL; 514191783Srmacklem ndp->ni_startdir = NULL; 515191783Srmacklem cnp->cn_flags &= ~HASBUF; 516191783Srmacklem } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 517191783Srmacklem ndp->ni_dvp = NULL; 518191783Srmacklem } 519224086Szack 520224086Szackout1: 521224086Szack NFSEXITCODE2(error, nd); 522191783Srmacklem return (error); 523191783Srmacklem} 524191783Srmacklem 525191783Srmacklem/* 526191783Srmacklem * Set up a pathname buffer and return a pointer to it and, optionally 527191783Srmacklem * set a hash pointer. 528191783Srmacklem */ 529191783Srmacklemvoid 530191783Srmacklemnfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 531191783Srmacklem{ 532191783Srmacklem struct componentname *cnp = &ndp->ni_cnd; 533191783Srmacklem 534191783Srmacklem cnp->cn_flags |= (NOMACCHECK | HASBUF); 535191783Srmacklem cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 536191783Srmacklem if (hashpp != NULL) 537191783Srmacklem *hashpp = NULL; 538191783Srmacklem *bufpp = cnp->cn_pnbuf; 539191783Srmacklem} 540191783Srmacklem 541191783Srmacklem/* 542191783Srmacklem * Release the above path buffer, if not released by nfsvno_namei(). 543191783Srmacklem */ 544191783Srmacklemvoid 545191783Srmacklemnfsvno_relpathbuf(struct nameidata *ndp) 546191783Srmacklem{ 547191783Srmacklem 548191783Srmacklem if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 549191783Srmacklem panic("nfsrelpath"); 550191783Srmacklem uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 551191783Srmacklem ndp->ni_cnd.cn_flags &= ~HASBUF; 552191783Srmacklem} 553191783Srmacklem 554191783Srmacklem/* 555191783Srmacklem * Readlink vnode op into an mbuf list. 556191783Srmacklem */ 557191783Srmacklemint 558191783Srmacklemnfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 559191783Srmacklem struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 560191783Srmacklem{ 561191783Srmacklem struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 562191783Srmacklem struct iovec *ivp = iv; 563191783Srmacklem struct uio io, *uiop = &io; 564191783Srmacklem struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 565224086Szack int i, len, tlen, error = 0; 566191783Srmacklem 567191783Srmacklem len = 0; 568191783Srmacklem i = 0; 569191783Srmacklem while (len < NFS_MAXPATHLEN) { 570191783Srmacklem NFSMGET(mp); 571191783Srmacklem MCLGET(mp, M_WAIT); 572191783Srmacklem mp->m_len = NFSMSIZ(mp); 573191783Srmacklem if (len == 0) { 574191783Srmacklem mp3 = mp2 = mp; 575191783Srmacklem } else { 576191783Srmacklem mp2->m_next = mp; 577191783Srmacklem mp2 = mp; 578191783Srmacklem } 579191783Srmacklem if ((len + mp->m_len) > NFS_MAXPATHLEN) { 580191783Srmacklem mp->m_len = NFS_MAXPATHLEN - len; 581191783Srmacklem len = NFS_MAXPATHLEN; 582191783Srmacklem } else { 583191783Srmacklem len += mp->m_len; 584191783Srmacklem } 585191783Srmacklem ivp->iov_base = mtod(mp, caddr_t); 586191783Srmacklem ivp->iov_len = mp->m_len; 587191783Srmacklem i++; 588191783Srmacklem ivp++; 589191783Srmacklem } 590191783Srmacklem uiop->uio_iov = iv; 591191783Srmacklem uiop->uio_iovcnt = i; 592191783Srmacklem uiop->uio_offset = 0; 593191783Srmacklem uiop->uio_resid = len; 594191783Srmacklem uiop->uio_rw = UIO_READ; 595191783Srmacklem uiop->uio_segflg = UIO_SYSSPACE; 596191783Srmacklem uiop->uio_td = NULL; 597191783Srmacklem error = VOP_READLINK(vp, uiop, cred); 598191783Srmacklem if (error) { 599191783Srmacklem m_freem(mp3); 600191783Srmacklem *lenp = 0; 601224086Szack goto out; 602191783Srmacklem } 603191783Srmacklem if (uiop->uio_resid > 0) { 604191783Srmacklem len -= uiop->uio_resid; 605191783Srmacklem tlen = NFSM_RNDUP(len); 606191783Srmacklem nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 607191783Srmacklem } 608191783Srmacklem *lenp = len; 609191783Srmacklem *mpp = mp3; 610191783Srmacklem *mpendp = mp; 611224086Szack 612224086Szackout: 613224086Szack NFSEXITCODE(error); 614224086Szack return (error); 615191783Srmacklem} 616191783Srmacklem 617191783Srmacklem/* 618191783Srmacklem * Read vnode op call into mbuf list. 619191783Srmacklem */ 620191783Srmacklemint 621191783Srmacklemnfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 622191783Srmacklem struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 623191783Srmacklem{ 624191783Srmacklem struct mbuf *m; 625191783Srmacklem int i; 626191783Srmacklem struct iovec *iv; 627191783Srmacklem struct iovec *iv2; 628229617Sjhb int error = 0, len, left, siz, tlen, ioflag = 0; 629191783Srmacklem struct mbuf *m2 = NULL, *m3; 630191783Srmacklem struct uio io, *uiop = &io; 631191783Srmacklem struct nfsheur *nh; 632191783Srmacklem 633191783Srmacklem len = left = NFSM_RNDUP(cnt); 634191783Srmacklem m3 = NULL; 635191783Srmacklem /* 636191783Srmacklem * Generate the mbuf list with the uio_iov ref. to it. 637191783Srmacklem */ 638191783Srmacklem i = 0; 639191783Srmacklem while (left > 0) { 640191783Srmacklem NFSMGET(m); 641191783Srmacklem MCLGET(m, M_WAIT); 642191783Srmacklem m->m_len = 0; 643191783Srmacklem siz = min(M_TRAILINGSPACE(m), left); 644191783Srmacklem left -= siz; 645191783Srmacklem i++; 646191783Srmacklem if (m3) 647191783Srmacklem m2->m_next = m; 648191783Srmacklem else 649191783Srmacklem m3 = m; 650191783Srmacklem m2 = m; 651191783Srmacklem } 652191783Srmacklem MALLOC(iv, struct iovec *, i * sizeof (struct iovec), 653191783Srmacklem M_TEMP, M_WAITOK); 654191783Srmacklem uiop->uio_iov = iv2 = iv; 655191783Srmacklem m = m3; 656191783Srmacklem left = len; 657191783Srmacklem i = 0; 658191783Srmacklem while (left > 0) { 659191783Srmacklem if (m == NULL) 660191783Srmacklem panic("nfsvno_read iov"); 661191783Srmacklem siz = min(M_TRAILINGSPACE(m), left); 662191783Srmacklem if (siz > 0) { 663191783Srmacklem iv->iov_base = mtod(m, caddr_t) + m->m_len; 664191783Srmacklem iv->iov_len = siz; 665191783Srmacklem m->m_len += siz; 666191783Srmacklem left -= siz; 667191783Srmacklem iv++; 668191783Srmacklem i++; 669191783Srmacklem } 670191783Srmacklem m = m->m_next; 671191783Srmacklem } 672191783Srmacklem uiop->uio_iovcnt = i; 673191783Srmacklem uiop->uio_offset = off; 674191783Srmacklem uiop->uio_resid = len; 675191783Srmacklem uiop->uio_rw = UIO_READ; 676191783Srmacklem uiop->uio_segflg = UIO_SYSSPACE; 677229617Sjhb nh = nfsrv_sequential_heuristic(uiop, vp); 678229617Sjhb ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 679191783Srmacklem error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 680191783Srmacklem FREE((caddr_t)iv2, M_TEMP); 681191783Srmacklem if (error) { 682191783Srmacklem m_freem(m3); 683191783Srmacklem *mpp = NULL; 684224086Szack goto out; 685191783Srmacklem } 686229617Sjhb nh->nh_nextoff = uiop->uio_offset; 687191783Srmacklem tlen = len - uiop->uio_resid; 688191783Srmacklem cnt = cnt < tlen ? cnt : tlen; 689191783Srmacklem tlen = NFSM_RNDUP(cnt); 690191783Srmacklem if (tlen == 0) { 691191783Srmacklem m_freem(m3); 692191783Srmacklem m3 = NULL; 693191783Srmacklem } else if (len != tlen || tlen != cnt) 694191783Srmacklem nfsrv_adj(m3, len - tlen, tlen - cnt); 695191783Srmacklem *mpp = m3; 696191783Srmacklem *mpendp = m2; 697224086Szack 698224086Szackout: 699224086Szack NFSEXITCODE(error); 700224086Szack return (error); 701191783Srmacklem} 702191783Srmacklem 703191783Srmacklem/* 704191783Srmacklem * Write vnode op from an mbuf list. 705191783Srmacklem */ 706191783Srmacklemint 707191783Srmacklemnfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable, 708191783Srmacklem struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 709191783Srmacklem{ 710191783Srmacklem struct iovec *ivp; 711191783Srmacklem int i, len; 712191783Srmacklem struct iovec *iv; 713191783Srmacklem int ioflags, error; 714191783Srmacklem struct uio io, *uiop = &io; 715229617Sjhb struct nfsheur *nh; 716191783Srmacklem 717191783Srmacklem MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP, 718191783Srmacklem M_WAITOK); 719191783Srmacklem uiop->uio_iov = iv = ivp; 720191783Srmacklem uiop->uio_iovcnt = cnt; 721191783Srmacklem i = mtod(mp, caddr_t) + mp->m_len - cp; 722191783Srmacklem len = retlen; 723191783Srmacklem while (len > 0) { 724191783Srmacklem if (mp == NULL) 725191783Srmacklem panic("nfsvno_write"); 726191783Srmacklem if (i > 0) { 727191783Srmacklem i = min(i, len); 728191783Srmacklem ivp->iov_base = cp; 729191783Srmacklem ivp->iov_len = i; 730191783Srmacklem ivp++; 731191783Srmacklem len -= i; 732191783Srmacklem } 733191783Srmacklem mp = mp->m_next; 734191783Srmacklem if (mp) { 735191783Srmacklem i = mp->m_len; 736191783Srmacklem cp = mtod(mp, caddr_t); 737191783Srmacklem } 738191783Srmacklem } 739191783Srmacklem 740191783Srmacklem if (stable == NFSWRITE_UNSTABLE) 741191783Srmacklem ioflags = IO_NODELOCKED; 742191783Srmacklem else 743191783Srmacklem ioflags = (IO_SYNC | IO_NODELOCKED); 744191783Srmacklem uiop->uio_resid = retlen; 745191783Srmacklem uiop->uio_rw = UIO_WRITE; 746191783Srmacklem uiop->uio_segflg = UIO_SYSSPACE; 747191783Srmacklem NFSUIOPROC(uiop, p); 748191783Srmacklem uiop->uio_offset = off; 749229617Sjhb nh = nfsrv_sequential_heuristic(uiop, vp); 750229617Sjhb ioflags |= nh->nh_seqcount << IO_SEQSHIFT; 751191783Srmacklem error = VOP_WRITE(vp, uiop, ioflags, cred); 752229617Sjhb if (error == 0) 753229617Sjhb nh->nh_nextoff = uiop->uio_offset; 754191783Srmacklem FREE((caddr_t)iv, M_TEMP); 755224086Szack 756224086Szack NFSEXITCODE(error); 757191783Srmacklem return (error); 758191783Srmacklem} 759191783Srmacklem 760191783Srmacklem/* 761191783Srmacklem * Common code for creating a regular file (plus special files for V2). 762191783Srmacklem */ 763191783Srmacklemint 764191783Srmacklemnfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 765191783Srmacklem struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 766199616Srmacklem int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp) 767191783Srmacklem{ 768191783Srmacklem u_quad_t tempsize; 769191783Srmacklem int error; 770191783Srmacklem 771191783Srmacklem error = nd->nd_repstat; 772191783Srmacklem if (!error && ndp->ni_vp == NULL) { 773191783Srmacklem if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 774191783Srmacklem vrele(ndp->ni_startdir); 775191783Srmacklem error = VOP_CREATE(ndp->ni_dvp, 776191783Srmacklem &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 777191783Srmacklem vput(ndp->ni_dvp); 778191783Srmacklem nfsvno_relpathbuf(ndp); 779191783Srmacklem if (!error) { 780191783Srmacklem if (*exclusive_flagp) { 781191783Srmacklem *exclusive_flagp = 0; 782191783Srmacklem NFSVNO_ATTRINIT(nvap); 783199616Srmacklem nvap->na_atime.tv_sec = cverf[0]; 784199616Srmacklem nvap->na_atime.tv_nsec = cverf[1]; 785191783Srmacklem error = VOP_SETATTR(ndp->ni_vp, 786191783Srmacklem &nvap->na_vattr, nd->nd_cred); 787191783Srmacklem } 788191783Srmacklem } 789191783Srmacklem /* 790191783Srmacklem * NFS V2 Only. nfsrvd_mknod() does this for V3. 791191783Srmacklem * (This implies, just get out on an error.) 792191783Srmacklem */ 793191783Srmacklem } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 794191783Srmacklem nvap->na_type == VFIFO) { 795191783Srmacklem if (nvap->na_type == VCHR && rdev == 0xffffffff) 796191783Srmacklem nvap->na_type = VFIFO; 797191783Srmacklem if (nvap->na_type != VFIFO && 798191783Srmacklem (error = priv_check_cred(nd->nd_cred, 799191783Srmacklem PRIV_VFS_MKNOD_DEV, 0))) { 800191783Srmacklem vrele(ndp->ni_startdir); 801191783Srmacklem nfsvno_relpathbuf(ndp); 802191783Srmacklem vput(ndp->ni_dvp); 803224086Szack goto out; 804191783Srmacklem } 805191783Srmacklem nvap->na_rdev = rdev; 806191783Srmacklem error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 807191783Srmacklem &ndp->ni_cnd, &nvap->na_vattr); 808191783Srmacklem vput(ndp->ni_dvp); 809191783Srmacklem nfsvno_relpathbuf(ndp); 810220546Srmacklem vrele(ndp->ni_startdir); 811220546Srmacklem if (error) 812224086Szack goto out; 813191783Srmacklem } else { 814191783Srmacklem vrele(ndp->ni_startdir); 815191783Srmacklem nfsvno_relpathbuf(ndp); 816191783Srmacklem vput(ndp->ni_dvp); 817224086Szack error = ENXIO; 818224086Szack goto out; 819191783Srmacklem } 820191783Srmacklem *vpp = ndp->ni_vp; 821191783Srmacklem } else { 822191783Srmacklem /* 823191783Srmacklem * Handle cases where error is already set and/or 824191783Srmacklem * the file exists. 825191783Srmacklem * 1 - clean up the lookup 826191783Srmacklem * 2 - iff !error and na_size set, truncate it 827191783Srmacklem */ 828191783Srmacklem vrele(ndp->ni_startdir); 829191783Srmacklem nfsvno_relpathbuf(ndp); 830191783Srmacklem *vpp = ndp->ni_vp; 831191783Srmacklem if (ndp->ni_dvp == *vpp) 832191783Srmacklem vrele(ndp->ni_dvp); 833191783Srmacklem else 834191783Srmacklem vput(ndp->ni_dvp); 835191783Srmacklem if (!error && nvap->na_size != VNOVAL) { 836200999Srmacklem error = nfsvno_accchk(*vpp, VWRITE, 837191783Srmacklem nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 838200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 839191783Srmacklem if (!error) { 840191783Srmacklem tempsize = nvap->na_size; 841191783Srmacklem NFSVNO_ATTRINIT(nvap); 842191783Srmacklem nvap->na_size = tempsize; 843191783Srmacklem error = VOP_SETATTR(*vpp, 844191783Srmacklem &nvap->na_vattr, nd->nd_cred); 845191783Srmacklem } 846191783Srmacklem } 847191783Srmacklem if (error) 848191783Srmacklem vput(*vpp); 849191783Srmacklem } 850224086Szack 851224086Szackout: 852224086Szack NFSEXITCODE(error); 853191783Srmacklem return (error); 854191783Srmacklem} 855191783Srmacklem 856191783Srmacklem/* 857191783Srmacklem * Do a mknod vnode op. 858191783Srmacklem */ 859191783Srmacklemint 860191783Srmacklemnfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 861191783Srmacklem struct thread *p) 862191783Srmacklem{ 863191783Srmacklem int error = 0; 864191783Srmacklem enum vtype vtyp; 865191783Srmacklem 866191783Srmacklem vtyp = nvap->na_type; 867191783Srmacklem /* 868191783Srmacklem * Iff doesn't exist, create it. 869191783Srmacklem */ 870191783Srmacklem if (ndp->ni_vp) { 871191783Srmacklem vrele(ndp->ni_startdir); 872191783Srmacklem nfsvno_relpathbuf(ndp); 873191783Srmacklem vput(ndp->ni_dvp); 874191783Srmacklem vrele(ndp->ni_vp); 875224086Szack error = EEXIST; 876224086Szack goto out; 877191783Srmacklem } 878191783Srmacklem if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 879191783Srmacklem vrele(ndp->ni_startdir); 880191783Srmacklem nfsvno_relpathbuf(ndp); 881191783Srmacklem vput(ndp->ni_dvp); 882224086Szack error = NFSERR_BADTYPE; 883224086Szack goto out; 884191783Srmacklem } 885191783Srmacklem if (vtyp == VSOCK) { 886191783Srmacklem vrele(ndp->ni_startdir); 887191783Srmacklem error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 888191783Srmacklem &ndp->ni_cnd, &nvap->na_vattr); 889191783Srmacklem vput(ndp->ni_dvp); 890191783Srmacklem nfsvno_relpathbuf(ndp); 891191783Srmacklem } else { 892191783Srmacklem if (nvap->na_type != VFIFO && 893191783Srmacklem (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) { 894191783Srmacklem vrele(ndp->ni_startdir); 895191783Srmacklem nfsvno_relpathbuf(ndp); 896191783Srmacklem vput(ndp->ni_dvp); 897224086Szack goto out; 898191783Srmacklem } 899191783Srmacklem error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 900191783Srmacklem &ndp->ni_cnd, &nvap->na_vattr); 901191783Srmacklem vput(ndp->ni_dvp); 902191783Srmacklem nfsvno_relpathbuf(ndp); 903216898Srmacklem vrele(ndp->ni_startdir); 904191783Srmacklem /* 905191783Srmacklem * Since VOP_MKNOD returns the ni_vp, I can't 906191783Srmacklem * see any reason to do the lookup. 907191783Srmacklem */ 908191783Srmacklem } 909224086Szack 910224086Szackout: 911224086Szack NFSEXITCODE(error); 912191783Srmacklem return (error); 913191783Srmacklem} 914191783Srmacklem 915191783Srmacklem/* 916191783Srmacklem * Mkdir vnode op. 917191783Srmacklem */ 918191783Srmacklemint 919191783Srmacklemnfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 920191783Srmacklem struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 921191783Srmacklem{ 922191783Srmacklem int error = 0; 923191783Srmacklem 924191783Srmacklem if (ndp->ni_vp != NULL) { 925191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 926191783Srmacklem vrele(ndp->ni_dvp); 927191783Srmacklem else 928191783Srmacklem vput(ndp->ni_dvp); 929191783Srmacklem vrele(ndp->ni_vp); 930206063Srmacklem nfsvno_relpathbuf(ndp); 931224086Szack error = EEXIST; 932224086Szack goto out; 933191783Srmacklem } 934191783Srmacklem error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 935191783Srmacklem &nvap->na_vattr); 936191783Srmacklem vput(ndp->ni_dvp); 937191783Srmacklem nfsvno_relpathbuf(ndp); 938224086Szack 939224086Szackout: 940224086Szack NFSEXITCODE(error); 941191783Srmacklem return (error); 942191783Srmacklem} 943191783Srmacklem 944191783Srmacklem/* 945191783Srmacklem * symlink vnode op. 946191783Srmacklem */ 947191783Srmacklemint 948191783Srmacklemnfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 949191783Srmacklem int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 950191783Srmacklem struct nfsexstuff *exp) 951191783Srmacklem{ 952191783Srmacklem int error = 0; 953191783Srmacklem 954191783Srmacklem if (ndp->ni_vp) { 955191783Srmacklem vrele(ndp->ni_startdir); 956191783Srmacklem nfsvno_relpathbuf(ndp); 957191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 958191783Srmacklem vrele(ndp->ni_dvp); 959191783Srmacklem else 960191783Srmacklem vput(ndp->ni_dvp); 961191783Srmacklem vrele(ndp->ni_vp); 962224086Szack error = EEXIST; 963224086Szack goto out; 964191783Srmacklem } 965191783Srmacklem 966191783Srmacklem error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 967191783Srmacklem &nvap->na_vattr, pathcp); 968191783Srmacklem vput(ndp->ni_dvp); 969191783Srmacklem vrele(ndp->ni_startdir); 970191783Srmacklem nfsvno_relpathbuf(ndp); 971191783Srmacklem /* 972191783Srmacklem * Although FreeBSD still had the lookup code in 973191783Srmacklem * it for 7/current, there doesn't seem to be any 974191783Srmacklem * point, since VOP_SYMLINK() returns the ni_vp. 975191783Srmacklem * Just vput it for v2. 976191783Srmacklem */ 977191783Srmacklem if (!not_v2 && !error) 978191783Srmacklem vput(ndp->ni_vp); 979224086Szack 980224086Szackout: 981224086Szack NFSEXITCODE(error); 982191783Srmacklem return (error); 983191783Srmacklem} 984191783Srmacklem 985191783Srmacklem/* 986191783Srmacklem * Parse symbolic link arguments. 987191783Srmacklem * This function has an ugly side effect. It will MALLOC() an area for 988191783Srmacklem * the symlink and set iov_base to point to it, only if it succeeds. 989191783Srmacklem * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 990191783Srmacklem * be FREE'd later. 991191783Srmacklem */ 992191783Srmacklemint 993191783Srmacklemnfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 994191783Srmacklem struct thread *p, char **pathcpp, int *lenp) 995191783Srmacklem{ 996191783Srmacklem u_int32_t *tl; 997191783Srmacklem char *pathcp = NULL; 998191783Srmacklem int error = 0, len; 999191783Srmacklem struct nfsv2_sattr *sp; 1000191783Srmacklem 1001191783Srmacklem *pathcpp = NULL; 1002191783Srmacklem *lenp = 0; 1003191783Srmacklem if ((nd->nd_flag & ND_NFSV3) && 1004191783Srmacklem (error = nfsrv_sattr(nd, nvap, NULL, NULL, p))) 1005191783Srmacklem goto nfsmout; 1006191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1007191783Srmacklem len = fxdr_unsigned(int, *tl); 1008191783Srmacklem if (len > NFS_MAXPATHLEN || len <= 0) { 1009191783Srmacklem error = EBADRPC; 1010191783Srmacklem goto nfsmout; 1011191783Srmacklem } 1012191783Srmacklem MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK); 1013191783Srmacklem error = nfsrv_mtostr(nd, pathcp, len); 1014191783Srmacklem if (error) 1015191783Srmacklem goto nfsmout; 1016191783Srmacklem if (nd->nd_flag & ND_NFSV2) { 1017191783Srmacklem NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1018191783Srmacklem nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 1019191783Srmacklem } 1020191783Srmacklem *pathcpp = pathcp; 1021191783Srmacklem *lenp = len; 1022224086Szack NFSEXITCODE2(0, nd); 1023191783Srmacklem return (0); 1024191783Srmacklemnfsmout: 1025191783Srmacklem if (pathcp) 1026191783Srmacklem free(pathcp, M_TEMP); 1027224086Szack NFSEXITCODE2(error, nd); 1028191783Srmacklem return (error); 1029191783Srmacklem} 1030191783Srmacklem 1031191783Srmacklem/* 1032191783Srmacklem * Remove a non-directory object. 1033191783Srmacklem */ 1034191783Srmacklemint 1035191783Srmacklemnfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1036191783Srmacklem struct thread *p, struct nfsexstuff *exp) 1037191783Srmacklem{ 1038191783Srmacklem struct vnode *vp; 1039191783Srmacklem int error = 0; 1040191783Srmacklem 1041191783Srmacklem vp = ndp->ni_vp; 1042191783Srmacklem if (vp->v_type == VDIR) 1043191783Srmacklem error = NFSERR_ISDIR; 1044191783Srmacklem else if (is_v4) 1045191783Srmacklem error = nfsrv_checkremove(vp, 1, p); 1046191783Srmacklem if (!error) 1047191783Srmacklem error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 1048191783Srmacklem if (ndp->ni_dvp == vp) 1049191783Srmacklem vrele(ndp->ni_dvp); 1050191783Srmacklem else 1051191783Srmacklem vput(ndp->ni_dvp); 1052191783Srmacklem vput(vp); 1053224086Szack NFSEXITCODE(error); 1054191783Srmacklem return (error); 1055191783Srmacklem} 1056191783Srmacklem 1057191783Srmacklem/* 1058191783Srmacklem * Remove a directory. 1059191783Srmacklem */ 1060191783Srmacklemint 1061191783Srmacklemnfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1062191783Srmacklem struct thread *p, struct nfsexstuff *exp) 1063191783Srmacklem{ 1064191783Srmacklem struct vnode *vp; 1065191783Srmacklem int error = 0; 1066191783Srmacklem 1067191783Srmacklem vp = ndp->ni_vp; 1068191783Srmacklem if (vp->v_type != VDIR) { 1069191783Srmacklem error = ENOTDIR; 1070191783Srmacklem goto out; 1071191783Srmacklem } 1072191783Srmacklem /* 1073191783Srmacklem * No rmdir "." please. 1074191783Srmacklem */ 1075191783Srmacklem if (ndp->ni_dvp == vp) { 1076191783Srmacklem error = EINVAL; 1077191783Srmacklem goto out; 1078191783Srmacklem } 1079191783Srmacklem /* 1080191783Srmacklem * The root of a mounted filesystem cannot be deleted. 1081191783Srmacklem */ 1082191783Srmacklem if (vp->v_vflag & VV_ROOT) 1083191783Srmacklem error = EBUSY; 1084191783Srmacklemout: 1085191783Srmacklem if (!error) 1086191783Srmacklem error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1087191783Srmacklem if (ndp->ni_dvp == vp) 1088191783Srmacklem vrele(ndp->ni_dvp); 1089191783Srmacklem else 1090191783Srmacklem vput(ndp->ni_dvp); 1091191783Srmacklem vput(vp); 1092224086Szack NFSEXITCODE(error); 1093191783Srmacklem return (error); 1094191783Srmacklem} 1095191783Srmacklem 1096191783Srmacklem/* 1097191783Srmacklem * Rename vnode op. 1098191783Srmacklem */ 1099191783Srmacklemint 1100191783Srmacklemnfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1101191783Srmacklem u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1102191783Srmacklem{ 1103191783Srmacklem struct vnode *fvp, *tvp, *tdvp; 1104191783Srmacklem int error = 0; 1105191783Srmacklem 1106191783Srmacklem fvp = fromndp->ni_vp; 1107191783Srmacklem if (ndstat) { 1108191783Srmacklem vrele(fromndp->ni_dvp); 1109191783Srmacklem vrele(fvp); 1110191783Srmacklem error = ndstat; 1111191783Srmacklem goto out1; 1112191783Srmacklem } 1113191783Srmacklem tdvp = tondp->ni_dvp; 1114191783Srmacklem tvp = tondp->ni_vp; 1115191783Srmacklem if (tvp != NULL) { 1116191783Srmacklem if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1117191783Srmacklem error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1118191783Srmacklem goto out; 1119191783Srmacklem } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1120191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1121191783Srmacklem goto out; 1122191783Srmacklem } 1123191783Srmacklem if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1124191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1125191783Srmacklem goto out; 1126191783Srmacklem } 1127191783Srmacklem 1128191783Srmacklem /* 1129191783Srmacklem * A rename to '.' or '..' results in a prematurely 1130191783Srmacklem * unlocked vnode on FreeBSD5, so I'm just going to fail that 1131191783Srmacklem * here. 1132191783Srmacklem */ 1133191783Srmacklem if ((tondp->ni_cnd.cn_namelen == 1 && 1134191783Srmacklem tondp->ni_cnd.cn_nameptr[0] == '.') || 1135191783Srmacklem (tondp->ni_cnd.cn_namelen == 2 && 1136191783Srmacklem tondp->ni_cnd.cn_nameptr[0] == '.' && 1137191783Srmacklem tondp->ni_cnd.cn_nameptr[1] == '.')) { 1138191783Srmacklem error = EINVAL; 1139191783Srmacklem goto out; 1140191783Srmacklem } 1141191783Srmacklem } 1142191783Srmacklem if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1143191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1144191783Srmacklem goto out; 1145191783Srmacklem } 1146191783Srmacklem if (fvp->v_mount != tdvp->v_mount) { 1147191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1148191783Srmacklem goto out; 1149191783Srmacklem } 1150191783Srmacklem if (fvp == tdvp) { 1151191783Srmacklem error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1152191783Srmacklem goto out; 1153191783Srmacklem } 1154191783Srmacklem if (fvp == tvp) { 1155191783Srmacklem /* 1156191783Srmacklem * If source and destination are the same, there is nothing to 1157191783Srmacklem * do. Set error to -1 to indicate this. 1158191783Srmacklem */ 1159191783Srmacklem error = -1; 1160191783Srmacklem goto out; 1161191783Srmacklem } 1162191783Srmacklem if (ndflag & ND_NFSV4) { 1163224081Szack if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) { 1164216893Srmacklem error = nfsrv_checkremove(fvp, 0, p); 1165224082Szack NFSVOPUNLOCK(fvp, 0); 1166216893Srmacklem } else 1167216893Srmacklem error = EPERM; 1168191783Srmacklem if (tvp && !error) 1169191783Srmacklem error = nfsrv_checkremove(tvp, 1, p); 1170191783Srmacklem } else { 1171191783Srmacklem /* 1172191783Srmacklem * For NFSv2 and NFSv3, try to get rid of the delegation, so 1173191783Srmacklem * that the NFSv4 client won't be confused by the rename. 1174191783Srmacklem * Since nfsd_recalldelegation() can only be called on an 1175191783Srmacklem * unlocked vnode at this point and fvp is the file that will 1176191783Srmacklem * still exist after the rename, just do fvp. 1177191783Srmacklem */ 1178191783Srmacklem nfsd_recalldelegation(fvp, p); 1179191783Srmacklem } 1180191783Srmacklemout: 1181191783Srmacklem if (!error) { 1182191783Srmacklem error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1183191783Srmacklem &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1184191783Srmacklem &tondp->ni_cnd); 1185191783Srmacklem } else { 1186191783Srmacklem if (tdvp == tvp) 1187191783Srmacklem vrele(tdvp); 1188191783Srmacklem else 1189191783Srmacklem vput(tdvp); 1190191783Srmacklem if (tvp) 1191191783Srmacklem vput(tvp); 1192191783Srmacklem vrele(fromndp->ni_dvp); 1193191783Srmacklem vrele(fvp); 1194191783Srmacklem if (error == -1) 1195191783Srmacklem error = 0; 1196191783Srmacklem } 1197191783Srmacklem vrele(tondp->ni_startdir); 1198191783Srmacklem nfsvno_relpathbuf(tondp); 1199191783Srmacklemout1: 1200191783Srmacklem vrele(fromndp->ni_startdir); 1201191783Srmacklem nfsvno_relpathbuf(fromndp); 1202224086Szack NFSEXITCODE(error); 1203191783Srmacklem return (error); 1204191783Srmacklem} 1205191783Srmacklem 1206191783Srmacklem/* 1207191783Srmacklem * Link vnode op. 1208191783Srmacklem */ 1209191783Srmacklemint 1210191783Srmacklemnfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1211191783Srmacklem struct thread *p, struct nfsexstuff *exp) 1212191783Srmacklem{ 1213191783Srmacklem struct vnode *xp; 1214191783Srmacklem int error = 0; 1215191783Srmacklem 1216191783Srmacklem xp = ndp->ni_vp; 1217191783Srmacklem if (xp != NULL) { 1218191783Srmacklem error = EEXIST; 1219191783Srmacklem } else { 1220191783Srmacklem xp = ndp->ni_dvp; 1221191783Srmacklem if (vp->v_mount != xp->v_mount) 1222191783Srmacklem error = EXDEV; 1223191783Srmacklem } 1224191783Srmacklem if (!error) { 1225224081Szack NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 1226216893Srmacklem if ((vp->v_iflag & VI_DOOMED) == 0) 1227216893Srmacklem error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1228216893Srmacklem else 1229216893Srmacklem error = EPERM; 1230191783Srmacklem if (ndp->ni_dvp == vp) 1231191783Srmacklem vrele(ndp->ni_dvp); 1232191783Srmacklem else 1233191783Srmacklem vput(ndp->ni_dvp); 1234224082Szack NFSVOPUNLOCK(vp, 0); 1235191783Srmacklem } else { 1236191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 1237191783Srmacklem vrele(ndp->ni_dvp); 1238191783Srmacklem else 1239191783Srmacklem vput(ndp->ni_dvp); 1240191783Srmacklem if (ndp->ni_vp) 1241191783Srmacklem vrele(ndp->ni_vp); 1242191783Srmacklem } 1243191783Srmacklem nfsvno_relpathbuf(ndp); 1244224086Szack NFSEXITCODE(error); 1245191783Srmacklem return (error); 1246191783Srmacklem} 1247191783Srmacklem 1248191783Srmacklem/* 1249191783Srmacklem * Do the fsync() appropriate for the commit. 1250191783Srmacklem */ 1251191783Srmacklemint 1252191783Srmacklemnfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1253191783Srmacklem struct thread *td) 1254191783Srmacklem{ 1255191783Srmacklem int error = 0; 1256191783Srmacklem 1257191783Srmacklem if (cnt > MAX_COMMIT_COUNT) { 1258191783Srmacklem /* 1259191783Srmacklem * Give up and do the whole thing 1260191783Srmacklem */ 1261191783Srmacklem if (vp->v_object && 1262191783Srmacklem (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1263191783Srmacklem VM_OBJECT_LOCK(vp->v_object); 1264191783Srmacklem vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1265191783Srmacklem VM_OBJECT_UNLOCK(vp->v_object); 1266191783Srmacklem } 1267191783Srmacklem error = VOP_FSYNC(vp, MNT_WAIT, td); 1268191783Srmacklem } else { 1269191783Srmacklem /* 1270191783Srmacklem * Locate and synchronously write any buffers that fall 1271191783Srmacklem * into the requested range. Note: we are assuming that 1272191783Srmacklem * f_iosize is a power of 2. 1273191783Srmacklem */ 1274191783Srmacklem int iosize = vp->v_mount->mnt_stat.f_iosize; 1275191783Srmacklem int iomask = iosize - 1; 1276191783Srmacklem struct bufobj *bo; 1277191783Srmacklem daddr_t lblkno; 1278191783Srmacklem 1279191783Srmacklem /* 1280191783Srmacklem * Align to iosize boundry, super-align to page boundry. 1281191783Srmacklem */ 1282191783Srmacklem if (off & iomask) { 1283191783Srmacklem cnt += off & iomask; 1284191783Srmacklem off &= ~(u_quad_t)iomask; 1285191783Srmacklem } 1286191783Srmacklem if (off & PAGE_MASK) { 1287191783Srmacklem cnt += off & PAGE_MASK; 1288191783Srmacklem off &= ~(u_quad_t)PAGE_MASK; 1289191783Srmacklem } 1290191783Srmacklem lblkno = off / iosize; 1291191783Srmacklem 1292191783Srmacklem if (vp->v_object && 1293191783Srmacklem (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1294191783Srmacklem VM_OBJECT_LOCK(vp->v_object); 1295218345Salc vm_object_page_clean(vp->v_object, off, off + cnt, 1296218345Salc OBJPC_SYNC); 1297191783Srmacklem VM_OBJECT_UNLOCK(vp->v_object); 1298191783Srmacklem } 1299191783Srmacklem 1300191783Srmacklem bo = &vp->v_bufobj; 1301191783Srmacklem BO_LOCK(bo); 1302191783Srmacklem while (cnt > 0) { 1303191783Srmacklem struct buf *bp; 1304191783Srmacklem 1305191783Srmacklem /* 1306191783Srmacklem * If we have a buffer and it is marked B_DELWRI we 1307191783Srmacklem * have to lock and write it. Otherwise the prior 1308191783Srmacklem * write is assumed to have already been committed. 1309191783Srmacklem * 1310191783Srmacklem * gbincore() can return invalid buffers now so we 1311191783Srmacklem * have to check that bit as well (though B_DELWRI 1312191783Srmacklem * should not be set if B_INVAL is set there could be 1313191783Srmacklem * a race here since we haven't locked the buffer). 1314191783Srmacklem */ 1315191783Srmacklem if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1316191783Srmacklem if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1317191783Srmacklem LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) { 1318191783Srmacklem BO_LOCK(bo); 1319191783Srmacklem continue; /* retry */ 1320191783Srmacklem } 1321191783Srmacklem if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1322191783Srmacklem B_DELWRI) { 1323191783Srmacklem bremfree(bp); 1324191783Srmacklem bp->b_flags &= ~B_ASYNC; 1325191783Srmacklem bwrite(bp); 1326191783Srmacklem ++nfs_commit_miss; 1327191783Srmacklem } else 1328191783Srmacklem BUF_UNLOCK(bp); 1329191783Srmacklem BO_LOCK(bo); 1330191783Srmacklem } 1331191783Srmacklem ++nfs_commit_blks; 1332191783Srmacklem if (cnt < iosize) 1333191783Srmacklem break; 1334191783Srmacklem cnt -= iosize; 1335191783Srmacklem ++lblkno; 1336191783Srmacklem } 1337191783Srmacklem BO_UNLOCK(bo); 1338191783Srmacklem } 1339224086Szack NFSEXITCODE(error); 1340191783Srmacklem return (error); 1341191783Srmacklem} 1342191783Srmacklem 1343191783Srmacklem/* 1344191783Srmacklem * Statfs vnode op. 1345191783Srmacklem */ 1346191783Srmacklemint 1347191990Sattilionfsvno_statfs(struct vnode *vp, struct statfs *sf) 1348191783Srmacklem{ 1349221517Srmacklem int error; 1350191783Srmacklem 1351221517Srmacklem error = VFS_STATFS(vp->v_mount, sf); 1352221517Srmacklem if (error == 0) { 1353221517Srmacklem /* 1354221517Srmacklem * Since NFS handles these values as unsigned on the 1355221517Srmacklem * wire, there is no way to represent negative values, 1356221517Srmacklem * so set them to 0. Without this, they will appear 1357221517Srmacklem * to be very large positive values for clients like 1358221517Srmacklem * Solaris10. 1359221517Srmacklem */ 1360221517Srmacklem if (sf->f_bavail < 0) 1361221517Srmacklem sf->f_bavail = 0; 1362221517Srmacklem if (sf->f_ffree < 0) 1363221517Srmacklem sf->f_ffree = 0; 1364221517Srmacklem } 1365224086Szack NFSEXITCODE(error); 1366221517Srmacklem return (error); 1367191783Srmacklem} 1368191783Srmacklem 1369191783Srmacklem/* 1370191783Srmacklem * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1371191783Srmacklem * must handle nfsrv_opencheck() calls after any other access checks. 1372191783Srmacklem */ 1373191783Srmacklemvoid 1374191783Srmacklemnfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1375191783Srmacklem nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1376199616Srmacklem int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1377191783Srmacklem NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p, 1378191783Srmacklem struct nfsexstuff *exp, struct vnode **vpp) 1379191783Srmacklem{ 1380191783Srmacklem struct vnode *vp = NULL; 1381191783Srmacklem u_quad_t tempsize; 1382191783Srmacklem struct nfsexstuff nes; 1383191783Srmacklem 1384191783Srmacklem if (ndp->ni_vp == NULL) 1385191783Srmacklem nd->nd_repstat = nfsrv_opencheck(clientid, 1386191783Srmacklem stateidp, stp, NULL, nd, p, nd->nd_repstat); 1387191783Srmacklem if (!nd->nd_repstat) { 1388191783Srmacklem if (ndp->ni_vp == NULL) { 1389191783Srmacklem vrele(ndp->ni_startdir); 1390191783Srmacklem nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1391191783Srmacklem &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1392191783Srmacklem vput(ndp->ni_dvp); 1393191783Srmacklem nfsvno_relpathbuf(ndp); 1394191783Srmacklem if (!nd->nd_repstat) { 1395191783Srmacklem if (*exclusive_flagp) { 1396191783Srmacklem *exclusive_flagp = 0; 1397191783Srmacklem NFSVNO_ATTRINIT(nvap); 1398199616Srmacklem nvap->na_atime.tv_sec = cverf[0]; 1399199616Srmacklem nvap->na_atime.tv_nsec = cverf[1]; 1400191783Srmacklem nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1401191783Srmacklem &nvap->na_vattr, cred); 1402191783Srmacklem } else { 1403191783Srmacklem nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1404191783Srmacklem aclp, p, attrbitp, exp); 1405191783Srmacklem } 1406191783Srmacklem } 1407191783Srmacklem vp = ndp->ni_vp; 1408191783Srmacklem } else { 1409191783Srmacklem if (ndp->ni_startdir) 1410191783Srmacklem vrele(ndp->ni_startdir); 1411191783Srmacklem nfsvno_relpathbuf(ndp); 1412191783Srmacklem vp = ndp->ni_vp; 1413191783Srmacklem if (create == NFSV4OPEN_CREATE) { 1414191783Srmacklem if (ndp->ni_dvp == vp) 1415191783Srmacklem vrele(ndp->ni_dvp); 1416191783Srmacklem else 1417191783Srmacklem vput(ndp->ni_dvp); 1418191783Srmacklem } 1419191783Srmacklem if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1420191783Srmacklem if (ndp->ni_cnd.cn_flags & RDONLY) 1421191783Srmacklem NFSVNO_SETEXRDONLY(&nes); 1422191783Srmacklem else 1423191783Srmacklem NFSVNO_EXINIT(&nes); 1424191783Srmacklem nd->nd_repstat = nfsvno_accchk(vp, 1425200999Srmacklem VWRITE, cred, &nes, p, 1426200999Srmacklem NFSACCCHK_NOOVERRIDE, 1427200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 1428191783Srmacklem nd->nd_repstat = nfsrv_opencheck(clientid, 1429191783Srmacklem stateidp, stp, vp, nd, p, nd->nd_repstat); 1430191783Srmacklem if (!nd->nd_repstat) { 1431191783Srmacklem tempsize = nvap->na_size; 1432191783Srmacklem NFSVNO_ATTRINIT(nvap); 1433191783Srmacklem nvap->na_size = tempsize; 1434191783Srmacklem nd->nd_repstat = VOP_SETATTR(vp, 1435191783Srmacklem &nvap->na_vattr, cred); 1436191783Srmacklem } 1437191783Srmacklem } else if (vp->v_type == VREG) { 1438191783Srmacklem nd->nd_repstat = nfsrv_opencheck(clientid, 1439191783Srmacklem stateidp, stp, vp, nd, p, nd->nd_repstat); 1440191783Srmacklem } 1441191783Srmacklem } 1442191783Srmacklem } else { 1443191783Srmacklem if (ndp->ni_cnd.cn_flags & HASBUF) 1444191783Srmacklem nfsvno_relpathbuf(ndp); 1445191783Srmacklem if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1446191783Srmacklem vrele(ndp->ni_startdir); 1447191783Srmacklem if (ndp->ni_dvp == ndp->ni_vp) 1448191783Srmacklem vrele(ndp->ni_dvp); 1449191783Srmacklem else 1450191783Srmacklem vput(ndp->ni_dvp); 1451191783Srmacklem if (ndp->ni_vp) 1452191783Srmacklem vput(ndp->ni_vp); 1453191783Srmacklem } 1454191783Srmacklem } 1455191783Srmacklem *vpp = vp; 1456224086Szack 1457224086Szack NFSEXITCODE2(0, nd); 1458191783Srmacklem} 1459191783Srmacklem 1460191783Srmacklem/* 1461191783Srmacklem * Updates the file rev and sets the mtime and ctime 1462191783Srmacklem * to the current clock time, returning the va_filerev and va_Xtime 1463191783Srmacklem * values. 1464191783Srmacklem */ 1465191783Srmacklemvoid 1466191783Srmacklemnfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1467191783Srmacklem struct ucred *cred, struct thread *p) 1468191783Srmacklem{ 1469191783Srmacklem struct vattr va; 1470191783Srmacklem 1471191783Srmacklem VATTR_NULL(&va); 1472191783Srmacklem getnanotime(&va.va_mtime); 1473191783Srmacklem (void) VOP_SETATTR(vp, &va, cred); 1474216693Srmacklem (void) nfsvno_getattr(vp, nvap, cred, p, 1); 1475191783Srmacklem} 1476191783Srmacklem 1477191783Srmacklem/* 1478191783Srmacklem * Glue routine to nfsv4_fillattr(). 1479191783Srmacklem */ 1480191783Srmacklemint 1481220645Srmacklemnfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp, 1482191783Srmacklem struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1483220648Srmacklem struct ucred *cred, struct thread *p, int isdgram, int reterr, 1484220648Srmacklem int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno) 1485191783Srmacklem{ 1486191783Srmacklem int error; 1487191783Srmacklem 1488220645Srmacklem error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 1489220648Srmacklem attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 1490220648Srmacklem mounted_on_fileno); 1491224086Szack NFSEXITCODE2(0, nd); 1492191783Srmacklem return (error); 1493191783Srmacklem} 1494191783Srmacklem 1495191783Srmacklem/* Since the Readdir vnode ops vary, put the entire functions in here. */ 1496191783Srmacklem/* 1497191783Srmacklem * nfs readdir service 1498191783Srmacklem * - mallocs what it thinks is enough to read 1499191783Srmacklem * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1500206170Srmacklem * - calls VOP_READDIR() 1501191783Srmacklem * - loops around building the reply 1502191783Srmacklem * if the output generated exceeds count break out of loop 1503191783Srmacklem * The NFSM_CLGET macro is used here so that the reply will be packed 1504191783Srmacklem * tightly in mbuf clusters. 1505191783Srmacklem * - it trims out records with d_fileno == 0 1506191783Srmacklem * this doesn't matter for Unix clients, but they might confuse clients 1507191783Srmacklem * for other os'. 1508191783Srmacklem * - it trims out records with d_type == DT_WHT 1509191783Srmacklem * these cannot be seen through NFS (unless we extend the protocol) 1510191783Srmacklem * The alternate call nfsrvd_readdirplus() does lookups as well. 1511191783Srmacklem * PS: The NFS protocol spec. does not clarify what the "count" byte 1512191783Srmacklem * argument is a count of.. just name strings and file id's or the 1513191783Srmacklem * entire reply rpc or ... 1514191783Srmacklem * I tried just file name and id sizes and it confused the Sun client, 1515191783Srmacklem * so I am using the full rpc size now. The "paranoia.." comment refers 1516191783Srmacklem * to including the status longwords that are not a part of the dir. 1517191783Srmacklem * "entry" structures, but are in the rpc. 1518191783Srmacklem */ 1519191783Srmacklemint 1520191783Srmacklemnfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1521191783Srmacklem struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1522191783Srmacklem{ 1523191783Srmacklem struct dirent *dp; 1524191783Srmacklem u_int32_t *tl; 1525191783Srmacklem int dirlen; 1526191783Srmacklem char *cpos, *cend, *rbuf; 1527191783Srmacklem struct nfsvattr at; 1528191783Srmacklem int nlen, error = 0, getret = 1; 1529191783Srmacklem int siz, cnt, fullsiz, eofflag, ncookies; 1530191783Srmacklem u_int64_t off, toff, verf; 1531191783Srmacklem u_long *cookies = NULL, *cookiep; 1532191783Srmacklem struct uio io; 1533191783Srmacklem struct iovec iv; 1534216691Srmacklem int not_zfs; 1535191783Srmacklem 1536191783Srmacklem if (nd->nd_repstat) { 1537191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1538224086Szack goto out; 1539191783Srmacklem } 1540191783Srmacklem if (nd->nd_flag & ND_NFSV2) { 1541191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1542191783Srmacklem off = fxdr_unsigned(u_quad_t, *tl++); 1543191783Srmacklem } else { 1544191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1545191783Srmacklem off = fxdr_hyper(tl); 1546191783Srmacklem tl += 2; 1547191783Srmacklem verf = fxdr_hyper(tl); 1548191783Srmacklem tl += 2; 1549191783Srmacklem } 1550191783Srmacklem toff = off; 1551191783Srmacklem cnt = fxdr_unsigned(int, *tl); 1552206170Srmacklem if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1553191783Srmacklem cnt = NFS_SRVMAXDATA(nd); 1554191783Srmacklem siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1555191783Srmacklem fullsiz = siz; 1556191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1557191783Srmacklem nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, 1558216693Srmacklem p, 1); 1559191783Srmacklem#if 0 1560191783Srmacklem /* 1561191783Srmacklem * va_filerev is not sufficient as a cookie verifier, 1562191783Srmacklem * since it is not supposed to change when entries are 1563191783Srmacklem * removed/added unless that offset cookies returned to 1564191783Srmacklem * the client are no longer valid. 1565191783Srmacklem */ 1566191783Srmacklem if (!nd->nd_repstat && toff && verf != at.na_filerev) 1567191783Srmacklem nd->nd_repstat = NFSERR_BAD_COOKIE; 1568191783Srmacklem#endif 1569191783Srmacklem } 1570206170Srmacklem if (nd->nd_repstat == 0 && cnt == 0) { 1571206170Srmacklem if (nd->nd_flag & ND_NFSV2) 1572206170Srmacklem /* NFSv2 does not have NFSERR_TOOSMALL */ 1573206170Srmacklem nd->nd_repstat = EPERM; 1574206170Srmacklem else 1575206170Srmacklem nd->nd_repstat = NFSERR_TOOSMALL; 1576206170Srmacklem } 1577191783Srmacklem if (!nd->nd_repstat) 1578200999Srmacklem nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1579191783Srmacklem nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1580200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 1581191783Srmacklem if (nd->nd_repstat) { 1582191783Srmacklem vput(vp); 1583191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1584191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1585224086Szack goto out; 1586191783Srmacklem } 1587216691Srmacklem not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs"); 1588191783Srmacklem MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1589191783Srmacklemagain: 1590191783Srmacklem eofflag = 0; 1591191783Srmacklem if (cookies) { 1592191783Srmacklem free((caddr_t)cookies, M_TEMP); 1593191783Srmacklem cookies = NULL; 1594191783Srmacklem } 1595191783Srmacklem 1596191783Srmacklem iv.iov_base = rbuf; 1597191783Srmacklem iv.iov_len = siz; 1598191783Srmacklem io.uio_iov = &iv; 1599191783Srmacklem io.uio_iovcnt = 1; 1600191783Srmacklem io.uio_offset = (off_t)off; 1601191783Srmacklem io.uio_resid = siz; 1602191783Srmacklem io.uio_segflg = UIO_SYSSPACE; 1603191783Srmacklem io.uio_rw = UIO_READ; 1604191783Srmacklem io.uio_td = NULL; 1605191783Srmacklem nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1606191783Srmacklem &cookies); 1607191783Srmacklem off = (u_int64_t)io.uio_offset; 1608191783Srmacklem if (io.uio_resid) 1609191783Srmacklem siz -= io.uio_resid; 1610191783Srmacklem 1611191783Srmacklem if (!cookies && !nd->nd_repstat) 1612191783Srmacklem nd->nd_repstat = NFSERR_PERM; 1613191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1614216693Srmacklem getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1615191783Srmacklem if (!nd->nd_repstat) 1616191783Srmacklem nd->nd_repstat = getret; 1617191783Srmacklem } 1618191783Srmacklem 1619191783Srmacklem /* 1620191783Srmacklem * Handles the failed cases. nd->nd_repstat == 0 past here. 1621191783Srmacklem */ 1622191783Srmacklem if (nd->nd_repstat) { 1623216692Srmacklem vput(vp); 1624191783Srmacklem free((caddr_t)rbuf, M_TEMP); 1625191783Srmacklem if (cookies) 1626191783Srmacklem free((caddr_t)cookies, M_TEMP); 1627191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1628191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1629224086Szack goto out; 1630191783Srmacklem } 1631191783Srmacklem /* 1632191783Srmacklem * If nothing read, return eof 1633191783Srmacklem * rpc reply 1634191783Srmacklem */ 1635191783Srmacklem if (siz == 0) { 1636216692Srmacklem vput(vp); 1637191783Srmacklem if (nd->nd_flag & ND_NFSV2) { 1638191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1639191783Srmacklem } else { 1640191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1641191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1642191783Srmacklem txdr_hyper(at.na_filerev, tl); 1643191783Srmacklem tl += 2; 1644191783Srmacklem } 1645191783Srmacklem *tl++ = newnfs_false; 1646191783Srmacklem *tl = newnfs_true; 1647191783Srmacklem FREE((caddr_t)rbuf, M_TEMP); 1648191783Srmacklem FREE((caddr_t)cookies, M_TEMP); 1649224086Szack goto out; 1650191783Srmacklem } 1651191783Srmacklem 1652191783Srmacklem /* 1653191783Srmacklem * Check for degenerate cases of nothing useful read. 1654191783Srmacklem * If so go try again 1655191783Srmacklem */ 1656191783Srmacklem cpos = rbuf; 1657191783Srmacklem cend = rbuf + siz; 1658191783Srmacklem dp = (struct dirent *)cpos; 1659191783Srmacklem cookiep = cookies; 1660191783Srmacklem 1661191783Srmacklem /* 1662191783Srmacklem * For some reason FreeBSD's ufs_readdir() chooses to back the 1663191783Srmacklem * directory offset up to a block boundary, so it is necessary to 1664191783Srmacklem * skip over the records that precede the requested offset. This 1665191783Srmacklem * requires the assumption that file offset cookies monotonically 1666191783Srmacklem * increase. 1667216691Srmacklem * Since the offset cookies don't monotonically increase for ZFS, 1668216691Srmacklem * this is not done when ZFS is the file system. 1669191783Srmacklem */ 1670191783Srmacklem while (cpos < cend && ncookies > 0 && 1671191783Srmacklem (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1672216691Srmacklem (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) { 1673191783Srmacklem cpos += dp->d_reclen; 1674191783Srmacklem dp = (struct dirent *)cpos; 1675191783Srmacklem cookiep++; 1676191783Srmacklem ncookies--; 1677191783Srmacklem } 1678191783Srmacklem if (cpos >= cend || ncookies == 0) { 1679191783Srmacklem siz = fullsiz; 1680191783Srmacklem toff = off; 1681191783Srmacklem goto again; 1682191783Srmacklem } 1683216692Srmacklem vput(vp); 1684191783Srmacklem 1685191783Srmacklem /* 1686191783Srmacklem * dirlen is the size of the reply, including all XDR and must 1687191783Srmacklem * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1688191783Srmacklem * if the XDR should be included in "count", but to be safe, we do. 1689191783Srmacklem * (Include the two booleans at the end of the reply in dirlen now.) 1690191783Srmacklem */ 1691191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1692191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1693191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1694191783Srmacklem txdr_hyper(at.na_filerev, tl); 1695191783Srmacklem dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1696191783Srmacklem } else { 1697191783Srmacklem dirlen = 2 * NFSX_UNSIGNED; 1698191783Srmacklem } 1699191783Srmacklem 1700191783Srmacklem /* Loop through the records and build reply */ 1701191783Srmacklem while (cpos < cend && ncookies > 0) { 1702191783Srmacklem nlen = dp->d_namlen; 1703191783Srmacklem if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1704191783Srmacklem nlen <= NFS_MAXNAMLEN) { 1705191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1706191783Srmacklem dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1707191783Srmacklem else 1708191783Srmacklem dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1709191783Srmacklem if (dirlen > cnt) { 1710191783Srmacklem eofflag = 0; 1711191783Srmacklem break; 1712191783Srmacklem } 1713191783Srmacklem 1714191783Srmacklem /* 1715191783Srmacklem * Build the directory record xdr from 1716191783Srmacklem * the dirent entry. 1717191783Srmacklem */ 1718191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1719191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1720191783Srmacklem *tl++ = newnfs_true; 1721191783Srmacklem *tl++ = 0; 1722191783Srmacklem } else { 1723191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1724191783Srmacklem *tl++ = newnfs_true; 1725191783Srmacklem } 1726191783Srmacklem *tl = txdr_unsigned(dp->d_fileno); 1727191783Srmacklem (void) nfsm_strtom(nd, dp->d_name, nlen); 1728191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1729191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1730191783Srmacklem *tl++ = 0; 1731191783Srmacklem } else 1732191783Srmacklem NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1733191783Srmacklem *tl = txdr_unsigned(*cookiep); 1734191783Srmacklem } 1735191783Srmacklem cpos += dp->d_reclen; 1736191783Srmacklem dp = (struct dirent *)cpos; 1737191783Srmacklem cookiep++; 1738191783Srmacklem ncookies--; 1739191783Srmacklem } 1740191783Srmacklem if (cpos < cend) 1741191783Srmacklem eofflag = 0; 1742191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1743191783Srmacklem *tl++ = newnfs_false; 1744191783Srmacklem if (eofflag) 1745191783Srmacklem *tl = newnfs_true; 1746191783Srmacklem else 1747191783Srmacklem *tl = newnfs_false; 1748191783Srmacklem FREE((caddr_t)rbuf, M_TEMP); 1749191783Srmacklem FREE((caddr_t)cookies, M_TEMP); 1750224086Szack 1751224086Szackout: 1752224086Szack NFSEXITCODE2(0, nd); 1753191783Srmacklem return (0); 1754191783Srmacklemnfsmout: 1755191783Srmacklem vput(vp); 1756224086Szack NFSEXITCODE2(error, nd); 1757191783Srmacklem return (error); 1758191783Srmacklem} 1759191783Srmacklem 1760191783Srmacklem/* 1761191783Srmacklem * Readdirplus for V3 and Readdir for V4. 1762191783Srmacklem */ 1763191783Srmacklemint 1764191783Srmacklemnfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 1765191783Srmacklem struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1766191783Srmacklem{ 1767191783Srmacklem struct dirent *dp; 1768191783Srmacklem u_int32_t *tl; 1769191783Srmacklem int dirlen; 1770191783Srmacklem char *cpos, *cend, *rbuf; 1771191783Srmacklem struct vnode *nvp; 1772191783Srmacklem fhandle_t nfh; 1773191783Srmacklem struct nfsvattr nva, at, *nvap = &nva; 1774191783Srmacklem struct mbuf *mb0, *mb1; 1775191783Srmacklem struct nfsreferral *refp; 1776199715Srmacklem int nlen, r, error = 0, getret = 1, usevget = 1; 1777191783Srmacklem int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 1778191783Srmacklem caddr_t bpos0, bpos1; 1779191783Srmacklem u_int64_t off, toff, verf; 1780191783Srmacklem u_long *cookies = NULL, *cookiep; 1781191783Srmacklem nfsattrbit_t attrbits, rderrbits, savbits; 1782191783Srmacklem struct uio io; 1783191783Srmacklem struct iovec iv; 1784199715Srmacklem struct componentname cn; 1785220648Srmacklem int at_root, needs_unbusy, not_zfs, supports_nfsv4acls; 1786220645Srmacklem struct mount *mp, *new_mp; 1787220645Srmacklem uint64_t mounted_on_fileno; 1788191783Srmacklem 1789191783Srmacklem if (nd->nd_repstat) { 1790191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1791224086Szack goto out; 1792191783Srmacklem } 1793191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 1794191783Srmacklem off = fxdr_hyper(tl); 1795191783Srmacklem toff = off; 1796191783Srmacklem tl += 2; 1797191783Srmacklem verf = fxdr_hyper(tl); 1798191783Srmacklem tl += 2; 1799191783Srmacklem siz = fxdr_unsigned(int, *tl++); 1800191783Srmacklem cnt = fxdr_unsigned(int, *tl); 1801191783Srmacklem 1802191783Srmacklem /* 1803191783Srmacklem * Use the server's maximum data transfer size as the upper bound 1804191783Srmacklem * on reply datalen. 1805191783Srmacklem */ 1806206170Srmacklem if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1807191783Srmacklem cnt = NFS_SRVMAXDATA(nd); 1808191783Srmacklem 1809191783Srmacklem /* 1810191783Srmacklem * siz is a "hint" of how much directory information (name, fileid, 1811191783Srmacklem * cookie) should be in the reply. At least one client "hints" 0, 1812191783Srmacklem * so I set it to cnt for that case. I also round it up to the 1813191783Srmacklem * next multiple of DIRBLKSIZ. 1814191783Srmacklem */ 1815206170Srmacklem if (siz <= 0) 1816191783Srmacklem siz = cnt; 1817191783Srmacklem siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1818191783Srmacklem 1819191783Srmacklem if (nd->nd_flag & ND_NFSV4) { 1820191783Srmacklem error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1821191783Srmacklem if (error) 1822191783Srmacklem goto nfsmout; 1823191783Srmacklem NFSSET_ATTRBIT(&savbits, &attrbits); 1824191783Srmacklem NFSCLRNOTFILLABLE_ATTRBIT(&attrbits); 1825191783Srmacklem NFSZERO_ATTRBIT(&rderrbits); 1826191783Srmacklem NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 1827191783Srmacklem } else { 1828191783Srmacklem NFSZERO_ATTRBIT(&attrbits); 1829191783Srmacklem } 1830191783Srmacklem fullsiz = siz; 1831216693Srmacklem nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1832191783Srmacklem if (!nd->nd_repstat) { 1833191783Srmacklem if (off && verf != at.na_filerev) { 1834191783Srmacklem /* 1835191783Srmacklem * va_filerev is not sufficient as a cookie verifier, 1836191783Srmacklem * since it is not supposed to change when entries are 1837191783Srmacklem * removed/added unless that offset cookies returned to 1838191783Srmacklem * the client are no longer valid. 1839191783Srmacklem */ 1840191783Srmacklem#if 0 1841191783Srmacklem if (nd->nd_flag & ND_NFSV4) { 1842191783Srmacklem nd->nd_repstat = NFSERR_NOTSAME; 1843191783Srmacklem } else { 1844191783Srmacklem nd->nd_repstat = NFSERR_BAD_COOKIE; 1845191783Srmacklem } 1846191783Srmacklem#endif 1847191783Srmacklem } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) { 1848191783Srmacklem nd->nd_repstat = NFSERR_BAD_COOKIE; 1849191783Srmacklem } 1850191783Srmacklem } 1851191783Srmacklem if (!nd->nd_repstat && vp->v_type != VDIR) 1852191783Srmacklem nd->nd_repstat = NFSERR_NOTDIR; 1853191783Srmacklem if (!nd->nd_repstat && cnt == 0) 1854191783Srmacklem nd->nd_repstat = NFSERR_TOOSMALL; 1855191783Srmacklem if (!nd->nd_repstat) 1856200999Srmacklem nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1857191783Srmacklem nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1858200999Srmacklem NFSACCCHK_VPISLOCKED, NULL); 1859191783Srmacklem if (nd->nd_repstat) { 1860191783Srmacklem vput(vp); 1861191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1862191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1863224086Szack goto out; 1864191783Srmacklem } 1865216691Srmacklem not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs"); 1866191783Srmacklem 1867191783Srmacklem MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1868191783Srmacklemagain: 1869191783Srmacklem eofflag = 0; 1870191783Srmacklem if (cookies) { 1871191783Srmacklem free((caddr_t)cookies, M_TEMP); 1872191783Srmacklem cookies = NULL; 1873191783Srmacklem } 1874191783Srmacklem 1875191783Srmacklem iv.iov_base = rbuf; 1876191783Srmacklem iv.iov_len = siz; 1877191783Srmacklem io.uio_iov = &iv; 1878191783Srmacklem io.uio_iovcnt = 1; 1879191783Srmacklem io.uio_offset = (off_t)off; 1880191783Srmacklem io.uio_resid = siz; 1881191783Srmacklem io.uio_segflg = UIO_SYSSPACE; 1882191783Srmacklem io.uio_rw = UIO_READ; 1883191783Srmacklem io.uio_td = NULL; 1884191783Srmacklem nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1885191783Srmacklem &cookies); 1886191783Srmacklem off = (u_int64_t)io.uio_offset; 1887191783Srmacklem if (io.uio_resid) 1888191783Srmacklem siz -= io.uio_resid; 1889191783Srmacklem 1890216693Srmacklem getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1891191783Srmacklem 1892191783Srmacklem if (!cookies && !nd->nd_repstat) 1893191783Srmacklem nd->nd_repstat = NFSERR_PERM; 1894191783Srmacklem if (!nd->nd_repstat) 1895191783Srmacklem nd->nd_repstat = getret; 1896191783Srmacklem if (nd->nd_repstat) { 1897199715Srmacklem vput(vp); 1898191783Srmacklem if (cookies) 1899191783Srmacklem free((caddr_t)cookies, M_TEMP); 1900191783Srmacklem free((caddr_t)rbuf, M_TEMP); 1901191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1902191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1903224086Szack goto out; 1904191783Srmacklem } 1905191783Srmacklem /* 1906191783Srmacklem * If nothing read, return eof 1907191783Srmacklem * rpc reply 1908191783Srmacklem */ 1909191783Srmacklem if (siz == 0) { 1910199715Srmacklem vput(vp); 1911191783Srmacklem if (nd->nd_flag & ND_NFSV3) 1912191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1913191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1914191783Srmacklem txdr_hyper(at.na_filerev, tl); 1915191783Srmacklem tl += 2; 1916191783Srmacklem *tl++ = newnfs_false; 1917191783Srmacklem *tl = newnfs_true; 1918191783Srmacklem free((caddr_t)cookies, M_TEMP); 1919191783Srmacklem free((caddr_t)rbuf, M_TEMP); 1920224086Szack goto out; 1921191783Srmacklem } 1922191783Srmacklem 1923191783Srmacklem /* 1924191783Srmacklem * Check for degenerate cases of nothing useful read. 1925191783Srmacklem * If so go try again 1926191783Srmacklem */ 1927191783Srmacklem cpos = rbuf; 1928191783Srmacklem cend = rbuf + siz; 1929191783Srmacklem dp = (struct dirent *)cpos; 1930191783Srmacklem cookiep = cookies; 1931191783Srmacklem 1932191783Srmacklem /* 1933191783Srmacklem * For some reason FreeBSD's ufs_readdir() chooses to back the 1934191783Srmacklem * directory offset up to a block boundary, so it is necessary to 1935191783Srmacklem * skip over the records that precede the requested offset. This 1936191783Srmacklem * requires the assumption that file offset cookies monotonically 1937191783Srmacklem * increase. 1938216691Srmacklem * Since the offset cookies don't monotonically increase for ZFS, 1939216691Srmacklem * this is not done when ZFS is the file system. 1940191783Srmacklem */ 1941191783Srmacklem while (cpos < cend && ncookies > 0 && 1942191783Srmacklem (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1943216691Srmacklem (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) || 1944191783Srmacklem ((nd->nd_flag & ND_NFSV4) && 1945191783Srmacklem ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 1946191783Srmacklem (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 1947191783Srmacklem cpos += dp->d_reclen; 1948191783Srmacklem dp = (struct dirent *)cpos; 1949191783Srmacklem cookiep++; 1950191783Srmacklem ncookies--; 1951191783Srmacklem } 1952191783Srmacklem if (cpos >= cend || ncookies == 0) { 1953191783Srmacklem siz = fullsiz; 1954191783Srmacklem toff = off; 1955191783Srmacklem goto again; 1956191783Srmacklem } 1957217176Srmacklem 1958217176Srmacklem /* 1959217176Srmacklem * Busy the file system so that the mount point won't go away 1960217176Srmacklem * and, as such, VFS_VGET() can be used safely. 1961217176Srmacklem */ 1962217176Srmacklem mp = vp->v_mount; 1963217176Srmacklem vfs_ref(mp); 1964224082Szack NFSVOPUNLOCK(vp, 0); 1965217176Srmacklem nd->nd_repstat = vfs_busy(mp, 0); 1966217176Srmacklem vfs_rel(mp); 1967217176Srmacklem if (nd->nd_repstat != 0) { 1968217176Srmacklem vrele(vp); 1969217176Srmacklem free(cookies, M_TEMP); 1970217176Srmacklem free(rbuf, M_TEMP); 1971217176Srmacklem if (nd->nd_flag & ND_NFSV3) 1972217176Srmacklem nfsrv_postopattr(nd, getret, &at); 1973224086Szack goto out; 1974217176Srmacklem } 1975191783Srmacklem 1976191783Srmacklem /* 1977191783Srmacklem * Save this position, in case there is an error before one entry 1978191783Srmacklem * is created. 1979191783Srmacklem */ 1980191783Srmacklem mb0 = nd->nd_mb; 1981191783Srmacklem bpos0 = nd->nd_bpos; 1982191783Srmacklem 1983191783Srmacklem /* 1984191783Srmacklem * Fill in the first part of the reply. 1985191783Srmacklem * dirlen is the reply length in bytes and cannot exceed cnt. 1986191783Srmacklem * (Include the two booleans at the end of the reply in dirlen now, 1987191783Srmacklem * so we recognize when we have exceeded cnt.) 1988191783Srmacklem */ 1989191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 1990191783Srmacklem dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1991191783Srmacklem nfsrv_postopattr(nd, getret, &at); 1992191783Srmacklem } else { 1993191783Srmacklem dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 1994191783Srmacklem } 1995191783Srmacklem NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 1996191783Srmacklem txdr_hyper(at.na_filerev, tl); 1997191783Srmacklem 1998191783Srmacklem /* 1999191783Srmacklem * Save this position, in case there is an empty reply needed. 2000191783Srmacklem */ 2001191783Srmacklem mb1 = nd->nd_mb; 2002191783Srmacklem bpos1 = nd->nd_bpos; 2003191783Srmacklem 2004191783Srmacklem /* Loop through the records and build reply */ 2005191783Srmacklem entrycnt = 0; 2006191783Srmacklem while (cpos < cend && ncookies > 0 && dirlen < cnt) { 2007191783Srmacklem nlen = dp->d_namlen; 2008191783Srmacklem if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2009191783Srmacklem nlen <= NFS_MAXNAMLEN && 2010191783Srmacklem ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 2011191783Srmacklem (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 2012191783Srmacklem || (nlen == 1 && dp->d_name[0] != '.'))) { 2013191783Srmacklem /* 2014191783Srmacklem * Save the current position in the reply, in case 2015191783Srmacklem * this entry exceeds cnt. 2016191783Srmacklem */ 2017191783Srmacklem mb1 = nd->nd_mb; 2018191783Srmacklem bpos1 = nd->nd_bpos; 2019191783Srmacklem 2020191783Srmacklem /* 2021191783Srmacklem * For readdir_and_lookup get the vnode using 2022191783Srmacklem * the file number. 2023191783Srmacklem */ 2024191783Srmacklem nvp = NULL; 2025191783Srmacklem refp = NULL; 2026191783Srmacklem r = 0; 2027220645Srmacklem at_root = 0; 2028220645Srmacklem needs_unbusy = 0; 2029220645Srmacklem new_mp = mp; 2030220645Srmacklem mounted_on_fileno = (uint64_t)dp->d_fileno; 2031191783Srmacklem if ((nd->nd_flag & ND_NFSV3) || 2032191783Srmacklem NFSNONZERO_ATTRBIT(&savbits)) { 2033191783Srmacklem if (nd->nd_flag & ND_NFSV4) 2034191783Srmacklem refp = nfsv4root_getreferral(NULL, 2035191783Srmacklem vp, dp->d_fileno); 2036199715Srmacklem if (refp == NULL) { 2037199715Srmacklem if (usevget) 2038217176Srmacklem r = VFS_VGET(mp, dp->d_fileno, 2039217176Srmacklem LK_SHARED, &nvp); 2040199715Srmacklem else 2041199715Srmacklem r = EOPNOTSUPP; 2042199715Srmacklem if (r == EOPNOTSUPP) { 2043199715Srmacklem if (usevget) { 2044199715Srmacklem usevget = 0; 2045199715Srmacklem cn.cn_nameiop = LOOKUP; 2046199715Srmacklem cn.cn_lkflags = 2047216700Srmacklem LK_SHARED | 2048199715Srmacklem LK_RETRY; 2049199715Srmacklem cn.cn_cred = 2050199715Srmacklem nd->nd_cred; 2051199715Srmacklem cn.cn_thread = p; 2052199715Srmacklem } 2053199715Srmacklem cn.cn_nameptr = dp->d_name; 2054199715Srmacklem cn.cn_namelen = nlen; 2055199715Srmacklem cn.cn_flags = ISLASTCN | 2056199715Srmacklem NOFOLLOW | LOCKLEAF | 2057199715Srmacklem MPSAFE; 2058199715Srmacklem if (nlen == 2 && 2059199715Srmacklem dp->d_name[0] == '.' && 2060199715Srmacklem dp->d_name[1] == '.') 2061199715Srmacklem cn.cn_flags |= 2062199715Srmacklem ISDOTDOT; 2063224081Szack if (NFSVOPLOCK(vp, LK_SHARED) 2064216692Srmacklem != 0) { 2065216692Srmacklem nd->nd_repstat = EPERM; 2066216692Srmacklem break; 2067216692Srmacklem } 2068214149Srmacklem if ((vp->v_vflag & VV_ROOT) != 0 2069214149Srmacklem && (cn.cn_flags & ISDOTDOT) 2070214149Srmacklem != 0) { 2071214149Srmacklem vref(vp); 2072214149Srmacklem nvp = vp; 2073214149Srmacklem r = 0; 2074220507Srmacklem } else { 2075214149Srmacklem r = VOP_LOOKUP(vp, &nvp, 2076214149Srmacklem &cn); 2077220507Srmacklem if (vp != nvp) 2078224082Szack NFSVOPUNLOCK(vp, 2079220507Srmacklem 0); 2080220507Srmacklem } 2081199715Srmacklem } 2082220645Srmacklem 2083220645Srmacklem /* 2084220645Srmacklem * For NFSv4, check to see if nvp is 2085220645Srmacklem * a mount point and get the mount 2086220645Srmacklem * point vnode, as required. 2087220645Srmacklem */ 2088220645Srmacklem if (r == 0 && 2089220645Srmacklem nfsrv_enable_crossmntpt != 0 && 2090220645Srmacklem (nd->nd_flag & ND_NFSV4) != 0 && 2091220645Srmacklem nvp->v_type == VDIR && 2092220645Srmacklem nvp->v_mountedhere != NULL) { 2093220645Srmacklem new_mp = nvp->v_mountedhere; 2094220645Srmacklem r = vfs_busy(new_mp, 0); 2095220645Srmacklem vput(nvp); 2096220645Srmacklem nvp = NULL; 2097220645Srmacklem if (r == 0) { 2098220645Srmacklem r = VFS_ROOT(new_mp, 2099220645Srmacklem LK_SHARED, &nvp); 2100220645Srmacklem needs_unbusy = 1; 2101220645Srmacklem if (r == 0) 2102220645Srmacklem at_root = 1; 2103220645Srmacklem } 2104220645Srmacklem } 2105199715Srmacklem } 2106191783Srmacklem if (!r) { 2107191783Srmacklem if (refp == NULL && 2108191783Srmacklem ((nd->nd_flag & ND_NFSV3) || 2109191783Srmacklem NFSNONZERO_ATTRBIT(&attrbits))) { 2110191783Srmacklem r = nfsvno_getfh(nvp, &nfh, p); 2111191783Srmacklem if (!r) 2112191783Srmacklem r = nfsvno_getattr(nvp, nvap, 2113216693Srmacklem nd->nd_cred, p, 1); 2114191783Srmacklem } 2115191783Srmacklem } else { 2116191783Srmacklem nvp = NULL; 2117191783Srmacklem } 2118191783Srmacklem if (r) { 2119191783Srmacklem if (!NFSISSET_ATTRBIT(&attrbits, 2120191783Srmacklem NFSATTRBIT_RDATTRERROR)) { 2121191783Srmacklem if (nvp != NULL) 2122191783Srmacklem vput(nvp); 2123220645Srmacklem if (needs_unbusy != 0) 2124220645Srmacklem vfs_unbusy(new_mp); 2125191783Srmacklem nd->nd_repstat = r; 2126191783Srmacklem break; 2127191783Srmacklem } 2128191783Srmacklem } 2129191783Srmacklem } 2130191783Srmacklem 2131191783Srmacklem /* 2132191783Srmacklem * Build the directory record xdr 2133191783Srmacklem */ 2134191783Srmacklem if (nd->nd_flag & ND_NFSV3) { 2135191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2136191783Srmacklem *tl++ = newnfs_true; 2137191783Srmacklem *tl++ = 0; 2138191783Srmacklem *tl = txdr_unsigned(dp->d_fileno); 2139191783Srmacklem dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2140191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2141191783Srmacklem *tl++ = 0; 2142191783Srmacklem *tl = txdr_unsigned(*cookiep); 2143191783Srmacklem nfsrv_postopattr(nd, 0, nvap); 2144191783Srmacklem dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1); 2145191783Srmacklem dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 2146191783Srmacklem if (nvp != NULL) 2147191783Srmacklem vput(nvp); 2148191783Srmacklem } else { 2149191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2150191783Srmacklem *tl++ = newnfs_true; 2151191783Srmacklem *tl++ = 0; 2152191783Srmacklem *tl = txdr_unsigned(*cookiep); 2153191783Srmacklem dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2154220648Srmacklem if (nvp != NULL) { 2155220648Srmacklem supports_nfsv4acls = 2156220648Srmacklem nfs_supportsnfsv4acls(nvp); 2157224082Szack NFSVOPUNLOCK(nvp, 0); 2158220648Srmacklem } else 2159220648Srmacklem supports_nfsv4acls = 0; 2160191783Srmacklem if (refp != NULL) { 2161191783Srmacklem dirlen += nfsrv_putreferralattr(nd, 2162191783Srmacklem &savbits, refp, 0, 2163191783Srmacklem &nd->nd_repstat); 2164191783Srmacklem if (nd->nd_repstat) { 2165191783Srmacklem if (nvp != NULL) 2166191783Srmacklem vrele(nvp); 2167220645Srmacklem if (needs_unbusy != 0) 2168220645Srmacklem vfs_unbusy(new_mp); 2169191783Srmacklem break; 2170191783Srmacklem } 2171191783Srmacklem } else if (r) { 2172220645Srmacklem dirlen += nfsvno_fillattr(nd, new_mp, 2173220645Srmacklem nvp, nvap, &nfh, r, &rderrbits, 2174220648Srmacklem nd->nd_cred, p, isdgram, 0, 2175220648Srmacklem supports_nfsv4acls, at_root, 2176220645Srmacklem mounted_on_fileno); 2177191783Srmacklem } else { 2178220645Srmacklem dirlen += nfsvno_fillattr(nd, new_mp, 2179220645Srmacklem nvp, nvap, &nfh, r, &attrbits, 2180220648Srmacklem nd->nd_cred, p, isdgram, 0, 2181220648Srmacklem supports_nfsv4acls, at_root, 2182220645Srmacklem mounted_on_fileno); 2183191783Srmacklem } 2184191783Srmacklem if (nvp != NULL) 2185191783Srmacklem vrele(nvp); 2186191783Srmacklem dirlen += (3 * NFSX_UNSIGNED); 2187191783Srmacklem } 2188220645Srmacklem if (needs_unbusy != 0) 2189220645Srmacklem vfs_unbusy(new_mp); 2190191783Srmacklem if (dirlen <= cnt) 2191191783Srmacklem entrycnt++; 2192191783Srmacklem } 2193191783Srmacklem cpos += dp->d_reclen; 2194191783Srmacklem dp = (struct dirent *)cpos; 2195191783Srmacklem cookiep++; 2196191783Srmacklem ncookies--; 2197191783Srmacklem } 2198216692Srmacklem vrele(vp); 2199217176Srmacklem vfs_unbusy(mp); 2200191783Srmacklem 2201191783Srmacklem /* 2202191783Srmacklem * If dirlen > cnt, we must strip off the last entry. If that 2203191783Srmacklem * results in an empty reply, report NFSERR_TOOSMALL. 2204191783Srmacklem */ 2205191783Srmacklem if (dirlen > cnt || nd->nd_repstat) { 2206191783Srmacklem if (!nd->nd_repstat && entrycnt == 0) 2207191783Srmacklem nd->nd_repstat = NFSERR_TOOSMALL; 2208191783Srmacklem if (nd->nd_repstat) 2209191783Srmacklem newnfs_trimtrailing(nd, mb0, bpos0); 2210191783Srmacklem else 2211191783Srmacklem newnfs_trimtrailing(nd, mb1, bpos1); 2212191783Srmacklem eofflag = 0; 2213191783Srmacklem } else if (cpos < cend) 2214191783Srmacklem eofflag = 0; 2215191783Srmacklem if (!nd->nd_repstat) { 2216191783Srmacklem NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2217191783Srmacklem *tl++ = newnfs_false; 2218191783Srmacklem if (eofflag) 2219191783Srmacklem *tl = newnfs_true; 2220191783Srmacklem else 2221191783Srmacklem *tl = newnfs_false; 2222191783Srmacklem } 2223191783Srmacklem FREE((caddr_t)cookies, M_TEMP); 2224191783Srmacklem FREE((caddr_t)rbuf, M_TEMP); 2225224086Szack 2226224086Szackout: 2227224086Szack NFSEXITCODE2(0, nd); 2228191783Srmacklem return (0); 2229191783Srmacklemnfsmout: 2230191783Srmacklem vput(vp); 2231224086Szack NFSEXITCODE2(error, nd); 2232191783Srmacklem return (error); 2233191783Srmacklem} 2234191783Srmacklem 2235191783Srmacklem/* 2236191783Srmacklem * Get the settable attributes out of the mbuf list. 2237191783Srmacklem * (Return 0 or EBADRPC) 2238191783Srmacklem */ 2239191783Srmacklemint 2240191783Srmacklemnfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2241191783Srmacklem nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2242191783Srmacklem{ 2243191783Srmacklem u_int32_t *tl; 2244191783Srmacklem struct nfsv2_sattr *sp; 2245191783Srmacklem struct timeval curtime; 2246191783Srmacklem int error = 0, toclient = 0; 2247191783Srmacklem 2248191783Srmacklem switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 2249191783Srmacklem case ND_NFSV2: 2250191783Srmacklem NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2251191783Srmacklem /* 2252191783Srmacklem * Some old clients didn't fill in the high order 16bits. 2253191783Srmacklem * --> check the low order 2 bytes for 0xffff 2254191783Srmacklem */ 2255191783Srmacklem if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 2256191783Srmacklem nvap->na_mode = nfstov_mode(sp->sa_mode); 2257191783Srmacklem if (sp->sa_uid != newnfs_xdrneg1) 2258191783Srmacklem nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 2259191783Srmacklem if (sp->sa_gid != newnfs_xdrneg1) 2260191783Srmacklem nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 2261191783Srmacklem if (sp->sa_size != newnfs_xdrneg1) 2262191783Srmacklem nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 2263191783Srmacklem if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 2264191783Srmacklem#ifdef notyet 2265191783Srmacklem fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 2266191783Srmacklem#else 2267191783Srmacklem nvap->na_atime.tv_sec = 2268191783Srmacklem fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 2269191783Srmacklem nvap->na_atime.tv_nsec = 0; 2270191783Srmacklem#endif 2271191783Srmacklem } 2272191783Srmacklem if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 2273191783Srmacklem fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 2274191783Srmacklem break; 2275191783Srmacklem case ND_NFSV3: 2276191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2277191783Srmacklem if (*tl == newnfs_true) { 2278191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2279191783Srmacklem nvap->na_mode = nfstov_mode(*tl); 2280191783Srmacklem } 2281191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2282191783Srmacklem if (*tl == newnfs_true) { 2283191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2284191783Srmacklem nvap->na_uid = fxdr_unsigned(uid_t, *tl); 2285191783Srmacklem } 2286191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2287191783Srmacklem if (*tl == newnfs_true) { 2288191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2289191783Srmacklem nvap->na_gid = fxdr_unsigned(gid_t, *tl); 2290191783Srmacklem } 2291191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2292191783Srmacklem if (*tl == newnfs_true) { 2293191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2294191783Srmacklem nvap->na_size = fxdr_hyper(tl); 2295191783Srmacklem } 2296191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2297191783Srmacklem switch (fxdr_unsigned(int, *tl)) { 2298191783Srmacklem case NFSV3SATTRTIME_TOCLIENT: 2299191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2300191783Srmacklem fxdr_nfsv3time(tl, &nvap->na_atime); 2301191783Srmacklem toclient = 1; 2302191783Srmacklem break; 2303191783Srmacklem case NFSV3SATTRTIME_TOSERVER: 2304191783Srmacklem NFSGETTIME(&curtime); 2305191783Srmacklem nvap->na_atime.tv_sec = curtime.tv_sec; 2306191783Srmacklem nvap->na_atime.tv_nsec = curtime.tv_usec * 1000; 2307191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2308191783Srmacklem break; 2309191783Srmacklem }; 2310191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2311191783Srmacklem switch (fxdr_unsigned(int, *tl)) { 2312191783Srmacklem case NFSV3SATTRTIME_TOCLIENT: 2313191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2314191783Srmacklem fxdr_nfsv3time(tl, &nvap->na_mtime); 2315191783Srmacklem nvap->na_vaflags &= ~VA_UTIMES_NULL; 2316191783Srmacklem break; 2317191783Srmacklem case NFSV3SATTRTIME_TOSERVER: 2318191783Srmacklem NFSGETTIME(&curtime); 2319191783Srmacklem nvap->na_mtime.tv_sec = curtime.tv_sec; 2320191783Srmacklem nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000; 2321191783Srmacklem if (!toclient) 2322191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2323191783Srmacklem break; 2324191783Srmacklem }; 2325191783Srmacklem break; 2326191783Srmacklem case ND_NFSV4: 2327191783Srmacklem error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p); 2328191783Srmacklem }; 2329191783Srmacklemnfsmout: 2330224086Szack NFSEXITCODE2(error, nd); 2331191783Srmacklem return (error); 2332191783Srmacklem} 2333191783Srmacklem 2334191783Srmacklem/* 2335191783Srmacklem * Handle the setable attributes for V4. 2336191783Srmacklem * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 2337191783Srmacklem */ 2338191783Srmacklemint 2339191783Srmacklemnfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2340191783Srmacklem nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2341191783Srmacklem{ 2342191783Srmacklem u_int32_t *tl; 2343191783Srmacklem int attrsum = 0; 2344191783Srmacklem int i, j; 2345191783Srmacklem int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 2346191783Srmacklem int toclient = 0; 2347191783Srmacklem u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 2348191783Srmacklem uid_t uid; 2349191783Srmacklem gid_t gid; 2350191783Srmacklem struct timeval curtime; 2351191783Srmacklem 2352191783Srmacklem error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 2353191783Srmacklem if (error) 2354224086Szack goto nfsmout; 2355191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2356191783Srmacklem attrsize = fxdr_unsigned(int, *tl); 2357191783Srmacklem 2358191783Srmacklem /* 2359191783Srmacklem * Loop around getting the setable attributes. If an unsupported 2360191783Srmacklem * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 2361191783Srmacklem */ 2362191783Srmacklem if (retnotsup) { 2363191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2364191783Srmacklem bitpos = NFSATTRBIT_MAX; 2365191783Srmacklem } else { 2366191783Srmacklem bitpos = 0; 2367191783Srmacklem } 2368191783Srmacklem for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 2369191783Srmacklem if (attrsum > attrsize) { 2370191783Srmacklem error = NFSERR_BADXDR; 2371191783Srmacklem goto nfsmout; 2372191783Srmacklem } 2373191783Srmacklem if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 2374191783Srmacklem switch (bitpos) { 2375191783Srmacklem case NFSATTRBIT_SIZE: 2376191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2377191783Srmacklem nvap->na_size = fxdr_hyper(tl); 2378191783Srmacklem attrsum += NFSX_HYPER; 2379191783Srmacklem break; 2380191783Srmacklem case NFSATTRBIT_ACL: 2381191783Srmacklem error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize, 2382191783Srmacklem p); 2383191783Srmacklem if (error) 2384191783Srmacklem goto nfsmout; 2385191783Srmacklem if (aceerr && !nd->nd_repstat) 2386224077Szack nd->nd_repstat = aceerr; 2387191783Srmacklem attrsum += aclsize; 2388191783Srmacklem break; 2389191783Srmacklem case NFSATTRBIT_ARCHIVE: 2390191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2391191783Srmacklem if (!nd->nd_repstat) 2392191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2393191783Srmacklem attrsum += NFSX_UNSIGNED; 2394191783Srmacklem break; 2395191783Srmacklem case NFSATTRBIT_HIDDEN: 2396191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2397191783Srmacklem if (!nd->nd_repstat) 2398191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2399191783Srmacklem attrsum += NFSX_UNSIGNED; 2400191783Srmacklem break; 2401191783Srmacklem case NFSATTRBIT_MIMETYPE: 2402191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2403191783Srmacklem i = fxdr_unsigned(int, *tl); 2404191783Srmacklem error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 2405191783Srmacklem if (error) 2406191783Srmacklem goto nfsmout; 2407191783Srmacklem if (!nd->nd_repstat) 2408191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2409191783Srmacklem attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 2410191783Srmacklem break; 2411191783Srmacklem case NFSATTRBIT_MODE: 2412191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2413191783Srmacklem nvap->na_mode = nfstov_mode(*tl); 2414191783Srmacklem attrsum += NFSX_UNSIGNED; 2415191783Srmacklem break; 2416191783Srmacklem case NFSATTRBIT_OWNER: 2417191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2418191783Srmacklem j = fxdr_unsigned(int, *tl); 2419224086Szack if (j < 0) { 2420224086Szack error = NFSERR_BADXDR; 2421224086Szack goto nfsmout; 2422224086Szack } 2423191783Srmacklem if (j > NFSV4_SMALLSTR) 2424191783Srmacklem cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2425191783Srmacklem else 2426191783Srmacklem cp = namestr; 2427191783Srmacklem error = nfsrv_mtostr(nd, cp, j); 2428191783Srmacklem if (error) { 2429191783Srmacklem if (j > NFSV4_SMALLSTR) 2430191783Srmacklem free(cp, M_NFSSTRING); 2431224086Szack goto nfsmout; 2432191783Srmacklem } 2433191783Srmacklem if (!nd->nd_repstat) { 2434191783Srmacklem nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p); 2435191783Srmacklem if (!nd->nd_repstat) 2436191783Srmacklem nvap->na_uid = uid; 2437191783Srmacklem } 2438191783Srmacklem if (j > NFSV4_SMALLSTR) 2439191783Srmacklem free(cp, M_NFSSTRING); 2440191783Srmacklem attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2441191783Srmacklem break; 2442191783Srmacklem case NFSATTRBIT_OWNERGROUP: 2443191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2444191783Srmacklem j = fxdr_unsigned(int, *tl); 2445224086Szack if (j < 0) { 2446224086Szack error = NFSERR_BADXDR; 2447224086Szack goto nfsmout; 2448224086Szack } 2449191783Srmacklem if (j > NFSV4_SMALLSTR) 2450191783Srmacklem cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2451191783Srmacklem else 2452191783Srmacklem cp = namestr; 2453191783Srmacklem error = nfsrv_mtostr(nd, cp, j); 2454191783Srmacklem if (error) { 2455191783Srmacklem if (j > NFSV4_SMALLSTR) 2456191783Srmacklem free(cp, M_NFSSTRING); 2457224086Szack goto nfsmout; 2458191783Srmacklem } 2459191783Srmacklem if (!nd->nd_repstat) { 2460191783Srmacklem nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p); 2461191783Srmacklem if (!nd->nd_repstat) 2462191783Srmacklem nvap->na_gid = gid; 2463191783Srmacklem } 2464191783Srmacklem if (j > NFSV4_SMALLSTR) 2465191783Srmacklem free(cp, M_NFSSTRING); 2466191783Srmacklem attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2467191783Srmacklem break; 2468191783Srmacklem case NFSATTRBIT_SYSTEM: 2469191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2470191783Srmacklem if (!nd->nd_repstat) 2471191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2472191783Srmacklem attrsum += NFSX_UNSIGNED; 2473191783Srmacklem break; 2474191783Srmacklem case NFSATTRBIT_TIMEACCESSSET: 2475191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2476191783Srmacklem attrsum += NFSX_UNSIGNED; 2477191783Srmacklem if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2478191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2479191783Srmacklem fxdr_nfsv4time(tl, &nvap->na_atime); 2480191783Srmacklem toclient = 1; 2481191783Srmacklem attrsum += NFSX_V4TIME; 2482191783Srmacklem } else { 2483191783Srmacklem NFSGETTIME(&curtime); 2484191783Srmacklem nvap->na_atime.tv_sec = curtime.tv_sec; 2485191783Srmacklem nvap->na_atime.tv_nsec = curtime.tv_usec * 1000; 2486191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2487191783Srmacklem } 2488191783Srmacklem break; 2489191783Srmacklem case NFSATTRBIT_TIMEBACKUP: 2490191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2491191783Srmacklem if (!nd->nd_repstat) 2492191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2493191783Srmacklem attrsum += NFSX_V4TIME; 2494191783Srmacklem break; 2495191783Srmacklem case NFSATTRBIT_TIMECREATE: 2496191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2497191783Srmacklem if (!nd->nd_repstat) 2498191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2499191783Srmacklem attrsum += NFSX_V4TIME; 2500191783Srmacklem break; 2501191783Srmacklem case NFSATTRBIT_TIMEMODIFYSET: 2502191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2503191783Srmacklem attrsum += NFSX_UNSIGNED; 2504191783Srmacklem if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2505191783Srmacklem NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2506191783Srmacklem fxdr_nfsv4time(tl, &nvap->na_mtime); 2507191783Srmacklem nvap->na_vaflags &= ~VA_UTIMES_NULL; 2508191783Srmacklem attrsum += NFSX_V4TIME; 2509191783Srmacklem } else { 2510191783Srmacklem NFSGETTIME(&curtime); 2511191783Srmacklem nvap->na_mtime.tv_sec = curtime.tv_sec; 2512191783Srmacklem nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000; 2513191783Srmacklem if (!toclient) 2514191783Srmacklem nvap->na_vaflags |= VA_UTIMES_NULL; 2515191783Srmacklem } 2516191783Srmacklem break; 2517191783Srmacklem default: 2518191783Srmacklem nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2519191783Srmacklem /* 2520191783Srmacklem * set bitpos so we drop out of the loop. 2521191783Srmacklem */ 2522191783Srmacklem bitpos = NFSATTRBIT_MAX; 2523191783Srmacklem break; 2524191783Srmacklem }; 2525191783Srmacklem } 2526191783Srmacklem 2527191783Srmacklem /* 2528191783Srmacklem * some clients pad the attrlist, so we need to skip over the 2529191783Srmacklem * padding. 2530191783Srmacklem */ 2531191783Srmacklem if (attrsum > attrsize) { 2532191783Srmacklem error = NFSERR_BADXDR; 2533191783Srmacklem } else { 2534191783Srmacklem attrsize = NFSM_RNDUP(attrsize); 2535191783Srmacklem if (attrsum < attrsize) 2536191783Srmacklem error = nfsm_advance(nd, attrsize - attrsum, -1); 2537191783Srmacklem } 2538191783Srmacklemnfsmout: 2539224086Szack NFSEXITCODE2(error, nd); 2540191783Srmacklem return (error); 2541191783Srmacklem} 2542191783Srmacklem 2543191783Srmacklem/* 2544191783Srmacklem * Check/setup export credentials. 2545191783Srmacklem */ 2546191783Srmacklemint 2547191783Srmacklemnfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 2548191783Srmacklem struct ucred *credanon) 2549191783Srmacklem{ 2550191783Srmacklem int error = 0; 2551191783Srmacklem 2552191783Srmacklem /* 2553191783Srmacklem * Check/setup credentials. 2554191783Srmacklem */ 2555191783Srmacklem if (nd->nd_flag & ND_GSS) 2556192121Srmacklem exp->nes_exflag &= ~MNT_EXPORTANON; 2557191783Srmacklem 2558191783Srmacklem /* 2559192121Srmacklem * Check to see if the operation is allowed for this security flavor. 2560191783Srmacklem * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to 2561191783Srmacklem * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 2562192121Srmacklem * Also, allow Secinfo, so that it can acquire the correct flavor(s). 2563191783Srmacklem */ 2564192121Srmacklem if (nfsvno_testexp(nd, exp) && 2565192121Srmacklem nd->nd_procnum != NFSV4OP_SECINFO && 2566191783Srmacklem nd->nd_procnum != NFSPROC_FSINFO) { 2567191783Srmacklem if (nd->nd_flag & ND_NFSV4) 2568191783Srmacklem error = NFSERR_WRONGSEC; 2569191783Srmacklem else 2570191783Srmacklem error = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2571224086Szack goto out; 2572191783Srmacklem } 2573191783Srmacklem 2574191783Srmacklem /* 2575191783Srmacklem * Check to see if the file system is exported V4 only. 2576191783Srmacklem */ 2577224086Szack if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) { 2578224086Szack error = NFSERR_PROGNOTV4; 2579224086Szack goto out; 2580224086Szack } 2581191783Srmacklem 2582191783Srmacklem /* 2583191783Srmacklem * Now, map the user credentials. 2584191783Srmacklem * (Note that ND_AUTHNONE will only be set for an NFSv3 2585191783Srmacklem * Fsinfo RPC. If set for anything else, this code might need 2586191783Srmacklem * to change.) 2587191783Srmacklem */ 2588191783Srmacklem if (NFSVNO_EXPORTED(exp) && 2589191783Srmacklem ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) || 2590191783Srmacklem NFSVNO_EXPORTANON(exp) || 2591191783Srmacklem (nd->nd_flag & ND_AUTHNONE))) { 2592191783Srmacklem nd->nd_cred->cr_uid = credanon->cr_uid; 2593191783Srmacklem nd->nd_cred->cr_gid = credanon->cr_gid; 2594194498Sbrooks crsetgroups(nd->nd_cred, credanon->cr_ngroups, 2595194498Sbrooks credanon->cr_groups); 2596191783Srmacklem } 2597224086Szack 2598224086Szackout: 2599224086Szack NFSEXITCODE2(error, nd); 2600224086Szack return (error); 2601191783Srmacklem} 2602191783Srmacklem 2603191783Srmacklem/* 2604191783Srmacklem * Check exports. 2605191783Srmacklem */ 2606191783Srmacklemint 2607191783Srmacklemnfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 2608191783Srmacklem struct ucred **credp) 2609191783Srmacklem{ 2610192121Srmacklem int i, error, *secflavors; 2611191783Srmacklem 2612191783Srmacklem error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2613192121Srmacklem &exp->nes_numsecflavor, &secflavors); 2614192121Srmacklem if (error) { 2615192121Srmacklem if (nfs_rootfhset) { 2616192121Srmacklem exp->nes_exflag = 0; 2617192121Srmacklem exp->nes_numsecflavor = 0; 2618192121Srmacklem error = 0; 2619192121Srmacklem } 2620192121Srmacklem } else { 2621192121Srmacklem /* Copy the security flavors. */ 2622192121Srmacklem for (i = 0; i < exp->nes_numsecflavor; i++) 2623192121Srmacklem exp->nes_secflavors[i] = secflavors[i]; 2624191783Srmacklem } 2625224086Szack NFSEXITCODE(error); 2626191783Srmacklem return (error); 2627191783Srmacklem} 2628191783Srmacklem 2629191783Srmacklem/* 2630191783Srmacklem * Get a vnode for a file handle and export stuff. 2631191783Srmacklem */ 2632191783Srmacklemint 2633191783Srmacklemnfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 2634216700Srmacklem int lktype, struct vnode **vpp, struct nfsexstuff *exp, 2635216700Srmacklem struct ucred **credp) 2636191783Srmacklem{ 2637192121Srmacklem int i, error, *secflavors; 2638191783Srmacklem 2639191998Srmacklem *credp = NULL; 2640192121Srmacklem exp->nes_numsecflavor = 0; 2641217063Srmacklem if (VFS_NEEDSGIANT(mp)) 2642217063Srmacklem error = ESTALE; 2643217063Srmacklem else 2644222167Srmacklem error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp); 2645205663Srmacklem if (error != 0) 2646205663Srmacklem /* Make sure the server replies ESTALE to the client. */ 2647205663Srmacklem error = ESTALE; 2648191783Srmacklem if (nam && !error) { 2649191783Srmacklem error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2650192121Srmacklem &exp->nes_numsecflavor, &secflavors); 2651191783Srmacklem if (error) { 2652191783Srmacklem if (nfs_rootfhset) { 2653191783Srmacklem exp->nes_exflag = 0; 2654192121Srmacklem exp->nes_numsecflavor = 0; 2655191783Srmacklem error = 0; 2656191783Srmacklem } else { 2657191783Srmacklem vput(*vpp); 2658191783Srmacklem } 2659192121Srmacklem } else { 2660192121Srmacklem /* Copy the security flavors. */ 2661192121Srmacklem for (i = 0; i < exp->nes_numsecflavor; i++) 2662192121Srmacklem exp->nes_secflavors[i] = secflavors[i]; 2663191783Srmacklem } 2664191783Srmacklem } 2665216700Srmacklem if (error == 0 && lktype == LK_SHARED) 2666216700Srmacklem /* 2667216700Srmacklem * It would be much better to pass lktype to VFS_FHTOVP(), 2668216700Srmacklem * but this will have to do until VFS_FHTOVP() has a lock 2669216700Srmacklem * type argument like VFS_VGET(). 2670216700Srmacklem */ 2671224081Szack NFSVOPLOCK(*vpp, LK_DOWNGRADE | LK_RETRY); 2672224086Szack 2673224086Szack NFSEXITCODE(error); 2674191783Srmacklem return (error); 2675191783Srmacklem} 2676191783Srmacklem 2677191783Srmacklem/* 2678191783Srmacklem * nfsd_fhtovp() - convert a fh to a vnode ptr 2679191783Srmacklem * - look up fsid in mount list (if not found ret error) 2680191783Srmacklem * - get vp and export rights by calling nfsvno_fhtovp() 2681191783Srmacklem * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 2682191783Srmacklem * for AUTH_SYS 2683217063Srmacklem * - if mpp != NULL, return the mount point so that it can 2684217063Srmacklem * be used for vn_finished_write() by the caller 2685191783Srmacklem */ 2686191783Srmacklemvoid 2687216700Srmacklemnfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 2688191783Srmacklem struct vnode **vpp, struct nfsexstuff *exp, 2689191783Srmacklem struct mount **mpp, int startwrite, struct thread *p) 2690191783Srmacklem{ 2691191783Srmacklem struct mount *mp; 2692191783Srmacklem struct ucred *credanon; 2693191783Srmacklem fhandle_t *fhp; 2694191783Srmacklem 2695191783Srmacklem fhp = (fhandle_t *)nfp->nfsrvfh_data; 2696191783Srmacklem /* 2697191783Srmacklem * Check for the special case of the nfsv4root_fh. 2698191783Srmacklem */ 2699217017Srmacklem mp = vfs_busyfs(&fhp->fh_fsid); 2700217063Srmacklem if (mpp != NULL) 2701217063Srmacklem *mpp = mp; 2702217017Srmacklem if (mp == NULL) { 2703191783Srmacklem *vpp = NULL; 2704191783Srmacklem nd->nd_repstat = ESTALE; 2705224086Szack goto out; 2706191783Srmacklem } 2707191783Srmacklem 2708191783Srmacklem if (startwrite) 2709191783Srmacklem vn_start_write(NULL, mpp, V_WAIT); 2710191783Srmacklem 2711216700Srmacklem nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 2712191783Srmacklem &credanon); 2713217017Srmacklem vfs_unbusy(mp); 2714191783Srmacklem 2715191783Srmacklem /* 2716191783Srmacklem * For NFSv4 without a pseudo root fs, unexported file handles 2717191783Srmacklem * can be returned, so that Lookup works everywhere. 2718191783Srmacklem */ 2719191783Srmacklem if (!nd->nd_repstat && exp->nes_exflag == 0 && 2720191783Srmacklem !(nd->nd_flag & ND_NFSV4)) { 2721191783Srmacklem vput(*vpp); 2722191783Srmacklem nd->nd_repstat = EACCES; 2723191783Srmacklem } 2724191783Srmacklem 2725191783Srmacklem /* 2726191783Srmacklem * Personally, I've never seen any point in requiring a 2727191783Srmacklem * reserved port#, since only in the rare case where the 2728191783Srmacklem * clients are all boxes with secure system priviledges, 2729191783Srmacklem * does it provide any enhanced security, but... some people 2730191783Srmacklem * believe it to be useful and keep putting this code back in. 2731191783Srmacklem * (There is also some "security checker" out there that 2732191783Srmacklem * complains if the nfs server doesn't enforce this.) 2733191783Srmacklem * However, note the following: 2734191783Srmacklem * RFC3530 (NFSv4) specifies that a reserved port# not be 2735191783Srmacklem * required. 2736191783Srmacklem * RFC2623 recommends that, if a reserved port# is checked for, 2737191783Srmacklem * that there be a way to turn that off--> ifdef'd. 2738191783Srmacklem */ 2739191783Srmacklem#ifdef NFS_REQRSVPORT 2740191783Srmacklem if (!nd->nd_repstat) { 2741192121Srmacklem struct sockaddr_in *saddr; 2742192121Srmacklem struct sockaddr_in6 *saddr6; 2743192121Srmacklem 2744192121Srmacklem saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 2745192121Srmacklem saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 2746192121Srmacklem if (!(nd->nd_flag & ND_NFSV4) && 2747192121Srmacklem ((saddr->sin_family == AF_INET && 2748192121Srmacklem ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 2749192121Srmacklem (saddr6->sin6_family == AF_INET6 && 2750192121Srmacklem ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 2751192121Srmacklem vput(*vpp); 2752192121Srmacklem nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2753192121Srmacklem } 2754191783Srmacklem } 2755191783Srmacklem#endif /* NFS_REQRSVPORT */ 2756191783Srmacklem 2757191783Srmacklem /* 2758191783Srmacklem * Check/setup credentials. 2759191783Srmacklem */ 2760191783Srmacklem if (!nd->nd_repstat) { 2761191783Srmacklem nd->nd_saveduid = nd->nd_cred->cr_uid; 2762191783Srmacklem nd->nd_repstat = nfsd_excred(nd, exp, credanon); 2763191783Srmacklem if (nd->nd_repstat) 2764191783Srmacklem vput(*vpp); 2765191783Srmacklem } 2766191940Skan if (credanon != NULL) 2767191940Skan crfree(credanon); 2768191783Srmacklem if (nd->nd_repstat) { 2769191783Srmacklem if (startwrite) 2770191783Srmacklem vn_finished_write(mp); 2771191783Srmacklem *vpp = NULL; 2772217063Srmacklem if (mpp != NULL) 2773217063Srmacklem *mpp = NULL; 2774191783Srmacklem } 2775224086Szack 2776224086Szackout: 2777224086Szack NFSEXITCODE2(0, nd); 2778191783Srmacklem} 2779191783Srmacklem 2780191783Srmacklem/* 2781191783Srmacklem * glue for fp. 2782191783Srmacklem */ 2783191783Srmacklemint 2784191783Srmacklemfp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 2785191783Srmacklem{ 2786191783Srmacklem struct filedesc *fdp; 2787191783Srmacklem struct file *fp; 2788224086Szack int error = 0; 2789191783Srmacklem 2790191783Srmacklem fdp = p->td_proc->p_fd; 2791191783Srmacklem if (fd >= fdp->fd_nfiles || 2792224086Szack (fp = fdp->fd_ofiles[fd]) == NULL) { 2793224086Szack error = EBADF; 2794224086Szack goto out; 2795224086Szack } 2796191783Srmacklem *fpp = fp; 2797224086Szack 2798224086Szackout: 2799224086Szack NFSEXITCODE(error); 2800224086Szack return (error); 2801191783Srmacklem} 2802191783Srmacklem 2803191783Srmacklem/* 2804192121Srmacklem * Called from nfssvc() to update the exports list. Just call 2805191783Srmacklem * vfs_export(). This has to be done, since the v4 root fake fs isn't 2806191783Srmacklem * in the mount list. 2807191783Srmacklem */ 2808191783Srmacklemint 2809191783Srmacklemnfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 2810191783Srmacklem{ 2811191783Srmacklem struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 2812224086Szack int error = 0; 2813191783Srmacklem struct nameidata nd; 2814191783Srmacklem fhandle_t fh; 2815191783Srmacklem 2816191783Srmacklem error = vfs_export(&nfsv4root_mnt, &nfsexargp->export); 2817216894Srmacklem if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 2818191783Srmacklem nfs_rootfhset = 0; 2819216894Srmacklem else if (error == 0) { 2820224086Szack if (nfsexargp->fspec == NULL) { 2821224086Szack error = EPERM; 2822224086Szack goto out; 2823224086Szack } 2824191783Srmacklem /* 2825191783Srmacklem * If fspec != NULL, this is the v4root path. 2826191783Srmacklem */ 2827191783Srmacklem NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, 2828191783Srmacklem nfsexargp->fspec, p); 2829191783Srmacklem if ((error = namei(&nd)) != 0) 2830224086Szack goto out; 2831191783Srmacklem error = nfsvno_getfh(nd.ni_vp, &fh, p); 2832191783Srmacklem vrele(nd.ni_vp); 2833191783Srmacklem if (!error) { 2834191783Srmacklem nfs_rootfh.nfsrvfh_len = NFSX_MYFH; 2835191783Srmacklem NFSBCOPY((caddr_t)&fh, 2836191783Srmacklem nfs_rootfh.nfsrvfh_data, 2837191783Srmacklem sizeof (fhandle_t)); 2838191783Srmacklem nfs_rootfhset = 1; 2839191783Srmacklem } 2840191783Srmacklem } 2841224086Szack 2842224086Szackout: 2843224086Szack NFSEXITCODE(error); 2844191783Srmacklem return (error); 2845191783Srmacklem} 2846191783Srmacklem 2847191783Srmacklem/* 2848191783Srmacklem * Get the tcp socket sequence numbers we need. 2849191783Srmacklem * (Maybe this should be moved to the tcp sources?) 2850191783Srmacklem */ 2851191783Srmacklemint 2852191783Srmacklemnfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap) 2853191783Srmacklem{ 2854191783Srmacklem struct inpcb *inp; 2855191783Srmacklem struct tcpcb *tp; 2856224086Szack int error = 0; 2857191783Srmacklem 2858191783Srmacklem inp = sotoinpcb(so); 2859205010Srwatson KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL")); 2860205010Srwatson INP_RLOCK(inp); 2861205010Srwatson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 2862205010Srwatson INP_RUNLOCK(inp); 2863224086Szack error = EPIPE; 2864224086Szack goto out; 2865191783Srmacklem } 2866191783Srmacklem tp = intotcpcb(inp); 2867205010Srwatson if (tp->t_state != TCPS_ESTABLISHED) { 2868205010Srwatson INP_RUNLOCK(inp); 2869224086Szack error = EPIPE; 2870224086Szack goto out; 2871191783Srmacklem } 2872205010Srwatson *maxp = tp->snd_max; 2873205010Srwatson *unap = tp->snd_una; 2874191783Srmacklem INP_RUNLOCK(inp); 2875224086Szack 2876224086Szackout: 2877224086Szack NFSEXITCODE(error); 2878224086Szack return (error); 2879191783Srmacklem} 2880191783Srmacklem 2881191783Srmacklem/* 2882191783Srmacklem * This function needs to test to see if the system is near its limit 2883191783Srmacklem * for memory allocation via malloc() or mget() and return True iff 2884191783Srmacklem * either of these resources are near their limit. 2885191783Srmacklem * XXX (For now, this is just a stub.) 2886191783Srmacklem */ 2887191783Srmacklemint nfsrv_testmalloclimit = 0; 2888191783Srmacklemint 2889191783Srmacklemnfsrv_mallocmget_limit(void) 2890191783Srmacklem{ 2891191783Srmacklem static int printmesg = 0; 2892191783Srmacklem static int testval = 1; 2893191783Srmacklem 2894191783Srmacklem if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 2895191783Srmacklem if ((printmesg++ % 100) == 0) 2896191783Srmacklem printf("nfsd: malloc/mget near limit\n"); 2897191783Srmacklem return (1); 2898191783Srmacklem } 2899191783Srmacklem return (0); 2900191783Srmacklem} 2901191783Srmacklem 2902191783Srmacklem/* 2903191783Srmacklem * BSD specific initialization of a mount point. 2904191783Srmacklem */ 2905191783Srmacklemvoid 2906191783Srmacklemnfsd_mntinit(void) 2907191783Srmacklem{ 2908191783Srmacklem static int inited = 0; 2909191783Srmacklem 2910191783Srmacklem if (inited) 2911191783Srmacklem return; 2912191783Srmacklem inited = 1; 2913191783Srmacklem nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 2914191783Srmacklem TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist); 2915191783Srmacklem nfsv4root_mnt.mnt_export = NULL; 2916191783Srmacklem TAILQ_INIT(&nfsv4root_opt); 2917191783Srmacklem TAILQ_INIT(&nfsv4root_newopt); 2918191783Srmacklem nfsv4root_mnt.mnt_opt = &nfsv4root_opt; 2919191783Srmacklem nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt; 2920191783Srmacklem nfsv4root_mnt.mnt_nvnodelistsize = 0; 2921191783Srmacklem} 2922191783Srmacklem 2923191783Srmacklem/* 2924191783Srmacklem * Get a vnode for a file handle, without checking exports, etc. 2925191783Srmacklem */ 2926191783Srmacklemstruct vnode * 2927191783Srmacklemnfsvno_getvp(fhandle_t *fhp) 2928191783Srmacklem{ 2929191783Srmacklem struct mount *mp; 2930191783Srmacklem struct vnode *vp; 2931191783Srmacklem int error; 2932191783Srmacklem 2933217017Srmacklem mp = vfs_busyfs(&fhp->fh_fsid); 2934191783Srmacklem if (mp == NULL) 2935191783Srmacklem return (NULL); 2936222167Srmacklem error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 2937217017Srmacklem vfs_unbusy(mp); 2938191783Srmacklem if (error) 2939191783Srmacklem return (NULL); 2940191783Srmacklem return (vp); 2941191783Srmacklem} 2942191783Srmacklem 2943191783Srmacklem/* 2944191783Srmacklem * Do a local VOP_ADVLOCK(). 2945191783Srmacklem */ 2946191783Srmacklemint 2947191783Srmacklemnfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 2948192256Srmacklem u_int64_t end, struct thread *td) 2949191783Srmacklem{ 2950224086Szack int error = 0; 2951191783Srmacklem struct flock fl; 2952191783Srmacklem u_int64_t tlen; 2953191783Srmacklem 2954212833Srmacklem if (nfsrv_dolocallocks == 0) 2955224086Szack goto out; 2956216893Srmacklem 2957216893Srmacklem /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */ 2958224086Szack if ((vp->v_iflag & VI_DOOMED) != 0) { 2959224086Szack error = EPERM; 2960224086Szack goto out; 2961224086Szack } 2962216893Srmacklem 2963191783Srmacklem fl.l_whence = SEEK_SET; 2964191783Srmacklem fl.l_type = ftype; 2965191783Srmacklem fl.l_start = (off_t)first; 2966191783Srmacklem if (end == NFS64BITSSET) { 2967191783Srmacklem fl.l_len = 0; 2968191783Srmacklem } else { 2969191783Srmacklem tlen = end - first; 2970191783Srmacklem fl.l_len = (off_t)tlen; 2971191783Srmacklem } 2972191783Srmacklem /* 2973192256Srmacklem * For FreeBSD8, the l_pid and l_sysid must be set to the same 2974192256Srmacklem * values for all calls, so that all locks will be held by the 2975192256Srmacklem * nfsd server. (The nfsd server handles conflicts between the 2976192256Srmacklem * various clients.) 2977192256Srmacklem * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 2978192256Srmacklem * bytes, so it can't be put in l_sysid. 2979191783Srmacklem */ 2980192503Srmacklem if (nfsv4_sysid == 0) 2981192503Srmacklem nfsv4_sysid = nlm_acquire_next_sysid(); 2982192256Srmacklem fl.l_pid = (pid_t)0; 2983192503Srmacklem fl.l_sysid = (int)nfsv4_sysid; 2984192256Srmacklem 2985224080Szack NFSVOPUNLOCK(vp, 0); 2986212833Srmacklem if (ftype == F_UNLCK) 2987212833Srmacklem error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 2988212833Srmacklem (F_POSIX | F_REMOTE)); 2989212833Srmacklem else 2990212833Srmacklem error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 2991212833Srmacklem (F_POSIX | F_REMOTE)); 2992224080Szack NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 2993224086Szack 2994224086Szackout: 2995224086Szack NFSEXITCODE(error); 2996191783Srmacklem return (error); 2997191783Srmacklem} 2998191783Srmacklem 2999191783Srmacklem/* 3000191783Srmacklem * Check the nfsv4 root exports. 3001191783Srmacklem */ 3002191783Srmacklemint 3003191783Srmacklemnfsvno_v4rootexport(struct nfsrv_descript *nd) 3004191783Srmacklem{ 3005191783Srmacklem struct ucred *credanon; 3006224086Szack int exflags, error = 0, numsecflavor, *secflavors, i; 3007191783Srmacklem 3008191783Srmacklem error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags, 3009192121Srmacklem &credanon, &numsecflavor, &secflavors); 3010224086Szack if (error) { 3011224086Szack error = NFSERR_PROGUNAVAIL; 3012224086Szack goto out; 3013224086Szack } 3014191940Skan if (credanon != NULL) 3015191940Skan crfree(credanon); 3016192121Srmacklem for (i = 0; i < numsecflavor; i++) { 3017192121Srmacklem if (secflavors[i] == AUTH_SYS) 3018192121Srmacklem nd->nd_flag |= ND_EXAUTHSYS; 3019192121Srmacklem else if (secflavors[i] == RPCSEC_GSS_KRB5) 3020192121Srmacklem nd->nd_flag |= ND_EXGSS; 3021192121Srmacklem else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3022192121Srmacklem nd->nd_flag |= ND_EXGSSINTEGRITY; 3023192121Srmacklem else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3024192121Srmacklem nd->nd_flag |= ND_EXGSSPRIVACY; 3025192121Srmacklem } 3026224086Szack 3027224086Szackout: 3028224086Szack NFSEXITCODE(error); 3029224086Szack return (error); 3030191783Srmacklem} 3031191783Srmacklem 3032191783Srmacklem/* 3033191783Srmacklem * Nfs server psuedo system call for the nfsd's 3034191783Srmacklem */ 3035191783Srmacklem/* 3036191783Srmacklem * MPSAFE 3037191783Srmacklem */ 3038191783Srmacklemstatic int 3039191783Srmacklemnfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3040191783Srmacklem{ 3041191783Srmacklem struct file *fp; 3042192017Srmacklem struct nfsd_addsock_args sockarg; 3043192017Srmacklem struct nfsd_nfsd_args nfsdarg; 3044191783Srmacklem int error; 3045191783Srmacklem 3046191783Srmacklem if (uap->flag & NFSSVC_NFSDADDSOCK) { 3047192017Srmacklem error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3048191783Srmacklem if (error) 3049224086Szack goto out; 3050224778Srwatson /* 3051224778Srwatson * Since we don't know what rights might be required, 3052224778Srwatson * pretend that we need them all. It is better to be too 3053224778Srwatson * careful than too reckless. 3054224778Srwatson */ 3055224778Srwatson if ((error = fget(td, sockarg.sock, CAP_SOCK_ALL, &fp)) != 0) 3056224086Szack goto out; 3057191783Srmacklem if (fp->f_type != DTYPE_SOCKET) { 3058191783Srmacklem fdrop(fp, td); 3059224086Szack error = EPERM; 3060224086Szack goto out; 3061191783Srmacklem } 3062191783Srmacklem error = nfsrvd_addsock(fp); 3063191783Srmacklem fdrop(fp, td); 3064191783Srmacklem } else if (uap->flag & NFSSVC_NFSDNFSD) { 3065224086Szack if (uap->argp == NULL) { 3066224086Szack error = EINVAL; 3067224086Szack goto out; 3068224086Szack } 3069192017Srmacklem error = copyin(uap->argp, (caddr_t)&nfsdarg, 3070192017Srmacklem sizeof (nfsdarg)); 3071192017Srmacklem if (error) 3072224086Szack goto out; 3073192017Srmacklem error = nfsrvd_nfsd(td, &nfsdarg); 3074191783Srmacklem } else { 3075191783Srmacklem error = nfssvc_srvcall(td, uap, td->td_ucred); 3076191783Srmacklem } 3077224086Szack 3078224086Szackout: 3079224086Szack NFSEXITCODE(error); 3080191783Srmacklem return (error); 3081191783Srmacklem} 3082191783Srmacklem 3083191783Srmacklemstatic int 3084191783Srmacklemnfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 3085191783Srmacklem{ 3086191783Srmacklem struct nfsex_args export; 3087191783Srmacklem struct file *fp = NULL; 3088191783Srmacklem int stablefd, len; 3089191783Srmacklem struct nfsd_clid adminrevoke; 3090191783Srmacklem struct nfsd_dumplist dumplist; 3091191783Srmacklem struct nfsd_dumpclients *dumpclients; 3092191783Srmacklem struct nfsd_dumplocklist dumplocklist; 3093191783Srmacklem struct nfsd_dumplocks *dumplocks; 3094191783Srmacklem struct nameidata nd; 3095191783Srmacklem vnode_t vp; 3096191783Srmacklem int error = EINVAL; 3097217432Srmacklem struct proc *procp; 3098191783Srmacklem 3099191783Srmacklem if (uap->flag & NFSSVC_PUBLICFH) { 3100191783Srmacklem NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 3101191783Srmacklem sizeof (fhandle_t)); 3102191783Srmacklem error = copyin(uap->argp, 3103191783Srmacklem &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 3104191783Srmacklem if (!error) 3105191783Srmacklem nfs_pubfhset = 1; 3106191783Srmacklem } else if (uap->flag & NFSSVC_V4ROOTEXPORT) { 3107191783Srmacklem error = copyin(uap->argp,(caddr_t)&export, 3108191783Srmacklem sizeof (struct nfsex_args)); 3109191783Srmacklem if (!error) 3110191783Srmacklem error = nfsrv_v4rootexport(&export, cred, p); 3111191783Srmacklem } else if (uap->flag & NFSSVC_NOPUBLICFH) { 3112191783Srmacklem nfs_pubfhset = 0; 3113191783Srmacklem error = 0; 3114191783Srmacklem } else if (uap->flag & NFSSVC_STABLERESTART) { 3115191783Srmacklem error = copyin(uap->argp, (caddr_t)&stablefd, 3116191783Srmacklem sizeof (int)); 3117191783Srmacklem if (!error) 3118191783Srmacklem error = fp_getfvp(p, stablefd, &fp, &vp); 3119191783Srmacklem if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 3120191783Srmacklem error = EBADF; 3121191783Srmacklem if (!error && newnfs_numnfsd != 0) 3122191783Srmacklem error = EPERM; 3123191783Srmacklem if (!error) { 3124191783Srmacklem nfsrv_stablefirst.nsf_fp = fp; 3125191783Srmacklem nfsrv_setupstable(p); 3126191783Srmacklem } 3127191783Srmacklem } else if (uap->flag & NFSSVC_ADMINREVOKE) { 3128191783Srmacklem error = copyin(uap->argp, (caddr_t)&adminrevoke, 3129191783Srmacklem sizeof (struct nfsd_clid)); 3130191783Srmacklem if (!error) 3131191783Srmacklem error = nfsrv_adminrevoke(&adminrevoke, p); 3132191783Srmacklem } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 3133191783Srmacklem error = copyin(uap->argp, (caddr_t)&dumplist, 3134191783Srmacklem sizeof (struct nfsd_dumplist)); 3135191783Srmacklem if (!error && (dumplist.ndl_size < 1 || 3136191783Srmacklem dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 3137191783Srmacklem error = EPERM; 3138191783Srmacklem if (!error) { 3139191783Srmacklem len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 3140191783Srmacklem dumpclients = (struct nfsd_dumpclients *)malloc(len, 3141191783Srmacklem M_TEMP, M_WAITOK); 3142191783Srmacklem nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 3143191783Srmacklem error = copyout(dumpclients, 3144191783Srmacklem CAST_USER_ADDR_T(dumplist.ndl_list), len); 3145191783Srmacklem free((caddr_t)dumpclients, M_TEMP); 3146191783Srmacklem } 3147191783Srmacklem } else if (uap->flag & NFSSVC_DUMPLOCKS) { 3148191783Srmacklem error = copyin(uap->argp, (caddr_t)&dumplocklist, 3149191783Srmacklem sizeof (struct nfsd_dumplocklist)); 3150191783Srmacklem if (!error && (dumplocklist.ndllck_size < 1 || 3151191783Srmacklem dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 3152191783Srmacklem error = EPERM; 3153191783Srmacklem if (!error) 3154191783Srmacklem error = nfsrv_lookupfilename(&nd, 3155191783Srmacklem dumplocklist.ndllck_fname, p); 3156191783Srmacklem if (!error) { 3157191783Srmacklem len = sizeof (struct nfsd_dumplocks) * 3158191783Srmacklem dumplocklist.ndllck_size; 3159191783Srmacklem dumplocks = (struct nfsd_dumplocks *)malloc(len, 3160191783Srmacklem M_TEMP, M_WAITOK); 3161191783Srmacklem nfsrv_dumplocks(nd.ni_vp, dumplocks, 3162191783Srmacklem dumplocklist.ndllck_size, p); 3163191783Srmacklem vput(nd.ni_vp); 3164191783Srmacklem error = copyout(dumplocks, 3165191783Srmacklem CAST_USER_ADDR_T(dumplocklist.ndllck_list), len); 3166191783Srmacklem free((caddr_t)dumplocks, M_TEMP); 3167191783Srmacklem } 3168217432Srmacklem } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 3169217432Srmacklem procp = p->td_proc; 3170217432Srmacklem PROC_LOCK(procp); 3171217432Srmacklem nfsd_master_pid = procp->p_pid; 3172217432Srmacklem bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 3173217432Srmacklem nfsd_master_start = procp->p_stats->p_start; 3174217432Srmacklem nfsd_master_proc = procp; 3175217432Srmacklem PROC_UNLOCK(procp); 3176191783Srmacklem } 3177224086Szack 3178224086Szack NFSEXITCODE(error); 3179191783Srmacklem return (error); 3180191783Srmacklem} 3181191783Srmacklem 3182192121Srmacklem/* 3183192121Srmacklem * Check exports. 3184192121Srmacklem * Returns 0 if ok, 1 otherwise. 3185192121Srmacklem */ 3186192121Srmacklemint 3187192121Srmacklemnfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 3188192121Srmacklem{ 3189192121Srmacklem int i; 3190192121Srmacklem 3191192121Srmacklem /* 3192192121Srmacklem * This seems odd, but allow the case where the security flavor 3193192121Srmacklem * list is empty. This happens when NFSv4 is traversing non-exported 3194192121Srmacklem * file systems. Exported file systems should always have a non-empty 3195192121Srmacklem * security flavor list. 3196192121Srmacklem */ 3197192121Srmacklem if (exp->nes_numsecflavor == 0) 3198192121Srmacklem return (0); 3199192121Srmacklem 3200192121Srmacklem for (i = 0; i < exp->nes_numsecflavor; i++) { 3201192121Srmacklem /* 3202192121Srmacklem * The tests for privacy and integrity must be first, 3203192121Srmacklem * since ND_GSS is set for everything but AUTH_SYS. 3204192121Srmacklem */ 3205192121Srmacklem if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 3206192121Srmacklem (nd->nd_flag & ND_GSSPRIVACY)) 3207192121Srmacklem return (0); 3208192121Srmacklem if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 3209192121Srmacklem (nd->nd_flag & ND_GSSINTEGRITY)) 3210192121Srmacklem return (0); 3211192121Srmacklem if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 3212192121Srmacklem (nd->nd_flag & ND_GSS)) 3213192121Srmacklem return (0); 3214192121Srmacklem if (exp->nes_secflavors[i] == AUTH_SYS && 3215192121Srmacklem (nd->nd_flag & ND_GSS) == 0) 3216192121Srmacklem return (0); 3217192121Srmacklem } 3218192121Srmacklem return (1); 3219192121Srmacklem} 3220192121Srmacklem 3221214224Srmacklem/* 3222214224Srmacklem * Calculate a hash value for the fid in a file handle. 3223214224Srmacklem */ 3224214255Srmacklemuint32_t 3225214224Srmacklemnfsrv_hashfh(fhandle_t *fhp) 3226214224Srmacklem{ 3227214255Srmacklem uint32_t hashval; 3228214224Srmacklem 3229214255Srmacklem hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 3230214224Srmacklem return (hashval); 3231214224Srmacklem} 3232214224Srmacklem 3233217432Srmacklem/* 3234217432Srmacklem * Signal the userland master nfsd to backup the stable restart file. 3235217432Srmacklem */ 3236217432Srmacklemvoid 3237217432Srmacklemnfsrv_backupstable(void) 3238217432Srmacklem{ 3239217432Srmacklem struct proc *procp; 3240217432Srmacklem 3241217432Srmacklem if (nfsd_master_proc != NULL) { 3242217432Srmacklem procp = pfind(nfsd_master_pid); 3243217432Srmacklem /* Try to make sure it is the correct process. */ 3244217432Srmacklem if (procp == nfsd_master_proc && 3245217432Srmacklem procp->p_stats->p_start.tv_sec == 3246217432Srmacklem nfsd_master_start.tv_sec && 3247217432Srmacklem procp->p_stats->p_start.tv_usec == 3248217432Srmacklem nfsd_master_start.tv_usec && 3249217432Srmacklem strcmp(procp->p_comm, nfsd_master_comm) == 0) 3250225617Skmacy kern_psignal(procp, SIGUSR2); 3251217432Srmacklem else 3252217432Srmacklem nfsd_master_proc = NULL; 3253217432Srmacklem 3254217432Srmacklem if (procp != NULL) 3255217432Srmacklem PROC_UNLOCK(procp); 3256217432Srmacklem } 3257217432Srmacklem} 3258217432Srmacklem 3259191783Srmacklemextern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 3260191783Srmacklem 3261191783Srmacklem/* 3262191783Srmacklem * Called once to initialize data structures... 3263191783Srmacklem */ 3264191783Srmacklemstatic int 3265191783Srmacklemnfsd_modevent(module_t mod, int type, void *data) 3266191783Srmacklem{ 3267191783Srmacklem int error = 0; 3268191783Srmacklem static int loaded = 0; 3269191783Srmacklem 3270191783Srmacklem switch (type) { 3271191783Srmacklem case MOD_LOAD: 3272191783Srmacklem if (loaded) 3273224086Szack goto out; 3274191783Srmacklem newnfs_portinit(); 3275191783Srmacklem mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF); 3276191783Srmacklem mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF); 3277191783Srmacklem mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL, 3278191783Srmacklem MTX_DEF); 3279191783Srmacklem lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0); 3280191783Srmacklem nfsrvd_initcache(); 3281191783Srmacklem nfsd_init(); 3282191783Srmacklem NFSD_LOCK(); 3283191783Srmacklem nfsrvd_init(0); 3284191783Srmacklem NFSD_UNLOCK(); 3285191783Srmacklem nfsd_mntinit(); 3286191783Srmacklem#ifdef VV_DISABLEDELEG 3287191783Srmacklem vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 3288191783Srmacklem vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 3289191783Srmacklem#endif 3290191783Srmacklem nfsd_call_servertimer = nfsrv_servertimer; 3291191783Srmacklem nfsd_call_nfsd = nfssvc_nfsd; 3292191783Srmacklem loaded = 1; 3293191783Srmacklem break; 3294191783Srmacklem 3295191783Srmacklem case MOD_UNLOAD: 3296191783Srmacklem if (newnfs_numnfsd != 0) { 3297191783Srmacklem error = EBUSY; 3298191783Srmacklem break; 3299191783Srmacklem } 3300191783Srmacklem 3301191783Srmacklem#ifdef VV_DISABLEDELEG 3302191783Srmacklem vn_deleg_ops.vndeleg_recall = NULL; 3303191783Srmacklem vn_deleg_ops.vndeleg_disable = NULL; 3304191783Srmacklem#endif 3305191783Srmacklem nfsd_call_servertimer = NULL; 3306191783Srmacklem nfsd_call_nfsd = NULL; 3307217335Szack 3308220530Srmacklem /* Clean out all NFSv4 state. */ 3309220530Srmacklem nfsrv_throwawayallstate(curthread); 3310220530Srmacklem 3311217335Szack /* Clean the NFS server reply cache */ 3312217335Szack nfsrvd_cleancache(); 3313217335Szack 3314220530Srmacklem /* Free up the krpc server pool. */ 3315220530Srmacklem if (nfsrvd_pool != NULL) 3316220530Srmacklem svcpool_destroy(nfsrvd_pool); 3317220530Srmacklem 3318191783Srmacklem /* and get rid of the locks */ 3319191783Srmacklem mtx_destroy(&nfs_cache_mutex); 3320191783Srmacklem mtx_destroy(&nfs_v4root_mutex); 3321191783Srmacklem mtx_destroy(&nfsv4root_mnt.mnt_mtx); 3322191783Srmacklem lockdestroy(&nfsv4root_mnt.mnt_explock); 3323191783Srmacklem loaded = 0; 3324191783Srmacklem break; 3325191783Srmacklem default: 3326191783Srmacklem error = EOPNOTSUPP; 3327191783Srmacklem break; 3328191783Srmacklem } 3329224086Szack 3330224086Szackout: 3331224086Szack NFSEXITCODE(error); 3332224086Szack return (error); 3333191783Srmacklem} 3334191783Srmacklemstatic moduledata_t nfsd_mod = { 3335191783Srmacklem "nfsd", 3336191783Srmacklem nfsd_modevent, 3337191783Srmacklem NULL, 3338191783Srmacklem}; 3339191783SrmacklemDECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 3340191783Srmacklem 3341191783Srmacklem/* So that loader and kldload(2) can find us, wherever we are.. */ 3342191783SrmacklemMODULE_VERSION(nfsd, 1); 3343191783SrmacklemMODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 3344216931SrmacklemMODULE_DEPEND(nfsd, nfslock, 1, 1, 1); 3345192574SrmacklemMODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 3346209191SrmacklemMODULE_DEPEND(nfsd, krpc, 1, 1, 1); 3347209191SrmacklemMODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 3348191783Srmacklem 3349