nfs_nfsdport.c revision 205663
1193323Sed/*- 2193323Sed * Copyright (c) 1989, 1993 3193323Sed * The Regents of the University of California. All rights reserved. 4193323Sed * 5193323Sed * This code is derived from software contributed to Berkeley by 6193323Sed * Rick Macklem at The University of Guelph. 7193323Sed * 8193323Sed * Redistribution and use in source and binary forms, with or without 9193323Sed * modification, are permitted provided that the following conditions 10193323Sed * are met: 11193323Sed * 1. Redistributions of source code must retain the above copyright 12193323Sed * notice, this list of conditions and the following disclaimer. 13193323Sed * 2. Redistributions in binary form must reproduce the above copyright 14193323Sed * notice, this list of conditions and the following disclaimer in the 15193323Sed * documentation and/or other materials provided with the distribution. 16193323Sed * 4. Neither the name of the University nor the names of its contributors 17193323Sed * may be used to endorse or promote products derived from this software 18193323Sed * without specific prior written permission. 19193323Sed * 20193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23193323Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30193323Sed * SUCH DAMAGE. 31193323Sed * 32193323Sed */ 33193323Sed 34193323Sed#include <sys/cdefs.h> 35193323Sed__FBSDID("$FreeBSD: head/sys/fs/nfsserver/nfs_nfsdport.c 205663 2010-03-26 01:35:19Z rmacklem $"); 36193323Sed 37193323Sed/* 38193323Sed * Functions that perform the vfs operations required by the routines in 39193323Sed * nfsd_serv.c. It is hoped that this change will make the server more 40193323Sed * portable. 41193323Sed */ 42193323Sed 43193323Sed#include <fs/nfs/nfsport.h> 44193323Sed#include <sys/sysctl.h> 45193323Sed#include <nlm/nlm_prot.h> 46193323Sed#include <nlm/nlm.h> 47193323Sed 48193323Sedextern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1; 49193323Sedextern int nfsv4root_set; 50193323Sedextern int nfsrv_useacl; 51193323Sedextern int newnfs_numnfsd; 52193323Sedextern struct mount nfsv4root_mnt; 53193323Sedextern struct nfsrv_stablefirst nfsrv_stablefirst; 54193323Sedextern void (*nfsd_call_servertimer)(void); 55193323Sedstruct vfsoptlist nfsv4root_opt, nfsv4root_newopt; 56193323SedNFSDLOCKMUTEX; 57193323Sedstruct mtx nfs_cache_mutex; 58193323Sedstruct mtx nfs_v4root_mutex; 59193323Sedstruct nfsrvfh nfs_rootfh, nfs_pubfh; 60193323Sedint nfs_pubfhset = 0, nfs_rootfhset = 0; 61193323Sedstatic uint32_t nfsv4_sysid = 0; 62193323Sed 63193323Sedstatic int nfssvc_srvcall(struct thread *, struct nfssvc_args *, 64193323Sed struct ucred *); 65193323Sed 66193323Sedstatic int enable_crossmntpt = 1; 67193323Sedstatic int nfs_commit_blks; 68193323Sedstatic int nfs_commit_miss; 69193323Sedextern int nfsrv_issuedelegs; 70193323Sedextern int nfsrv_dolocallocks; 71193323Sed 72193323SedSYSCTL_DECL(_vfs_newnfs); 73193323SedSYSCTL_INT(_vfs_newnfs, OID_AUTO, mirrormnt, CTLFLAG_RW, &enable_crossmntpt, 74198396Srdivacky 0, "Enable nfsd to cross mount points"); 75198396SrdivackySYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 76198396Srdivacky 0, ""); 77193323SedSYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 78193323Sed 0, ""); 79193323SedSYSCTL_INT(_vfs_newnfs, OID_AUTO, issue_delegations, CTLFLAG_RW, 80193323Sed &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 81193323SedSYSCTL_INT(_vfs_newnfs, OID_AUTO, enable_locallocks, CTLFLAG_RW, 82193323Sed &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 83193323Sed 84193323Sed#define NUM_HEURISTIC 1017 85193323Sed#define NHUSE_INIT 64 86193323Sed#define NHUSE_INC 16 87193323Sed#define NHUSE_MAX 2048 88193323Sed 89193323Sedstatic struct nfsheur { 90193323Sed struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 91193323Sed off_t nh_nextr; /* next offset for sequential detection */ 92193323Sed int nh_use; /* use count for selection */ 93193323Sed int nh_seqcount; /* heuristic */ 94193323Sed} nfsheur[NUM_HEURISTIC]; 95193323Sed 96193323Sed 97193323Sed/* 98193323Sed * Get attributes into nfsvattr structure. 99193323Sed */ 100193323Sedint 101193323Sednfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 102193323Sed struct thread *p) 103193323Sed{ 104193323Sed int error, lockedit = 0; 105193323Sed 106193323Sed /* Since FreeBSD insists the vnode be locked... */ 107193323Sed if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 108193323Sed lockedit = 1; 109193323Sed NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 110193323Sed } 111198892Srdivacky error = VOP_GETATTR(vp, &nvap->na_vattr, cred); 112193323Sed if (lockedit) 113193323Sed NFSVOPUNLOCK(vp, 0, p); 114193323Sed return (error); 115193323Sed} 116193323Sed 117193323Sed/* 118193323Sed * Get a file handle for a vnode. 119193323Sed */ 120193323Sedint 121193323Sednfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 122193323Sed{ 123193323Sed int error; 124193323Sed 125193323Sed NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 126193323Sed fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 127193323Sed error = VOP_VPTOFH(vp, &fhp->fh_fid); 128193323Sed return (error); 129193323Sed} 130193323Sed 131193323Sed/* 132193323Sed * Perform access checking for vnodes obtained from file handles that would 133193323Sed * refer to files already opened by a Unix client. You cannot just use 134193323Sed * vn_writechk() and VOP_ACCESSX() for two reasons. 135193323Sed * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 136193323Sed * case. 137193323Sed * 2 - The owner is to be given access irrespective of mode bits for some 138193323Sed * operations, so that processes that chmod after opening a file don't 139193323Sed * break. 140193323Sed */ 141193323Sedint 142193323Sednfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 143193323Sed struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 144193323Sed u_int32_t *supportedtypep) 145193323Sed{ 146193323Sed struct vattr vattr; 147193323Sed int error = 0, getret = 0; 148193323Sed 149193323Sed if (accmode & VWRITE) { 150193323Sed /* Just vn_writechk() changed to check rdonly */ 151193323Sed /* 152193323Sed * Disallow write attempts on read-only file systems; 153193323Sed * unless the file is a socket or a block or character 154193323Sed * device resident on the file system. 155193323Sed */ 156193323Sed if (NFSVNO_EXRDONLY(exp) || 157193323Sed (vp->v_mount->mnt_flag & MNT_RDONLY)) { 158193323Sed switch (vp->v_type) { 159193323Sed case VREG: 160198090Srdivacky case VDIR: 161193323Sed case VLNK: 162193323Sed return (EROFS); 163193323Sed default: 164193323Sed break; 165193323Sed } 166193323Sed } 167193323Sed /* 168193323Sed * If there's shared text associated with 169193323Sed * the inode, try to free it up once. If 170193323Sed * we fail, we can't allow writing. 171193323Sed */ 172193323Sed if (vp->v_vflag & VV_TEXT) 173193323Sed return (ETXTBSY); 174193323Sed } 175193323Sed if (vpislocked == 0) 176193323Sed NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 177193323Sed 178193323Sed /* 179193323Sed * Should the override still be applied when ACLs are enabled? 180193323Sed */ 181193323Sed error = VOP_ACCESSX(vp, accmode, cred, p); 182193323Sed if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 183193323Sed /* 184193323Sed * Try again with VEXPLICIT_DENY, to see if the test for 185193323Sed * deletion is supported. 186193323Sed */ 187193323Sed error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 188193323Sed if (error == 0) { 189193323Sed if (vp->v_type == VDIR) { 190193323Sed accmode &= ~(VDELETE | VDELETE_CHILD); 191193323Sed accmode |= VWRITE; 192193323Sed error = VOP_ACCESSX(vp, accmode, cred, p); 193193323Sed } else if (supportedtypep != NULL) { 194193323Sed *supportedtypep &= ~NFSACCESS_DELETE; 195193323Sed } 196193323Sed } 197193323Sed } 198193323Sed 199193323Sed /* 200193323Sed * Allow certain operations for the owner (reads and writes 201193323Sed * on files that are already open). 202193323Sed */ 203193323Sed if (override != NFSACCCHK_NOOVERRIDE && 204193323Sed (error == EPERM || error == EACCES)) { 205193323Sed if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 206193323Sed error = 0; 207193323Sed else if (override & NFSACCCHK_ALLOWOWNER) { 208193323Sed getret = VOP_GETATTR(vp, &vattr, cred); 209193323Sed if (getret == 0 && cred->cr_uid == vattr.va_uid) 210193323Sed error = 0; 211193323Sed } 212193323Sed } 213193323Sed if (vpislocked == 0) 214193323Sed NFSVOPUNLOCK(vp, 0, p); 215193323Sed return (error); 216193323Sed} 217193323Sed 218193323Sed/* 219193323Sed * Set attribute(s) vnop. 220193323Sed */ 221193323Sedint 222193323Sednfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 223193323Sed struct thread *p, struct nfsexstuff *exp) 224193323Sed{ 225193323Sed int error; 226193323Sed 227193323Sed error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 228193323Sed return (error); 229193323Sed} 230193323Sed 231193323Sed/* 232193323Sed * Set up nameidata for a lookup() call and do it 233193323Sed * For the cases where we are crossing mount points 234193323Sed * (looking up the public fh path or the v4 root path when 235193323Sed * not using a pseudo-root fs), set/release the Giant lock, 236193323Sed * as required. 237193323Sed */ 238193323Sedint 239193323Sednfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 240193323Sed struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 241193323Sed struct vnode **retdirp) 242193323Sed{ 243193323Sed struct componentname *cnp = &ndp->ni_cnd; 244193323Sed int i; 245193323Sed struct iovec aiov; 246193323Sed struct uio auio; 247193323Sed int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 248198090Srdivacky int error = 0, crossmnt; 249198090Srdivacky char *cp; 250198090Srdivacky 251198090Srdivacky *retdirp = NULL; 252198090Srdivacky cnp->cn_nameptr = cnp->cn_pnbuf; 253198090Srdivacky /* 254198090Srdivacky * Extract and set starting directory. 255193323Sed */ 256193323Sed if (dp->v_type != VDIR) { 257193323Sed if (islocked) 258193323Sed vput(dp); 259193323Sed else 260193323Sed vrele(dp); 261193323Sed nfsvno_relpathbuf(ndp); 262193323Sed return (ENOTDIR); 263193323Sed } 264193323Sed if (islocked) 265193323Sed NFSVOPUNLOCK(dp, 0, p); 266193323Sed VREF(dp); 267193323Sed *retdirp = dp; 268193323Sed if (NFSVNO_EXRDONLY(exp)) 269193323Sed cnp->cn_flags |= RDONLY; 270193323Sed ndp->ni_segflg = UIO_SYSSPACE; 271193323Sed crossmnt = 1; 272193323Sed 273193323Sed if (nd->nd_flag & ND_PUBLOOKUP) { 274193323Sed ndp->ni_loopcnt = 0; 275193323Sed if (cnp->cn_pnbuf[0] == '/') { 276193323Sed vrele(dp); 277193323Sed /* 278193323Sed * Check for degenerate pathnames here, since lookup() 279193323Sed * panics on them. 280193323Sed */ 281193323Sed for (i = 1; i < ndp->ni_pathlen; i++) 282193323Sed if (cnp->cn_pnbuf[i] != '/') 283193323Sed break; 284193323Sed if (i == ndp->ni_pathlen) { 285193323Sed error = NFSERR_ACCES; 286193323Sed goto out; 287193323Sed } 288193323Sed dp = rootvnode; 289193323Sed VREF(dp); 290193323Sed } 291193323Sed } else if ((enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 292193323Sed (nd->nd_flag & ND_NFSV4) == 0) { 293193323Sed /* 294193323Sed * Only cross mount points for NFSv4 when doing a 295193323Sed * mount while traversing the file system above 296193323Sed * the mount point, unless enable_crossmntpt is set. 297193323Sed */ 298193323Sed cnp->cn_flags |= NOCROSSMOUNT; 299193323Sed crossmnt = 0; 300193323Sed } 301193323Sed 302193323Sed /* 303193323Sed * Initialize for scan, set ni_startdir and bump ref on dp again 304193323Sed * becuase lookup() will dereference ni_startdir. 305193323Sed */ 306193323Sed 307193323Sed cnp->cn_thread = p; 308193323Sed ndp->ni_startdir = dp; 309193323Sed ndp->ni_rootdir = rootvnode; 310193323Sed 311193323Sed if (!lockleaf) 312193323Sed cnp->cn_flags |= LOCKLEAF; 313193323Sed for (;;) { 314193323Sed cnp->cn_nameptr = cnp->cn_pnbuf; 315193323Sed /* 316193323Sed * Call lookup() to do the real work. If an error occurs, 317193323Sed * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 318193323Sed * we do not have to dereference anything before returning. 319193323Sed * In either case ni_startdir will be dereferenced and NULLed 320193323Sed * out. 321193323Sed */ 322193323Sed if (exp->nes_vfslocked) 323193323Sed ndp->ni_cnd.cn_flags |= GIANTHELD; 324193323Sed error = lookup(ndp); 325193323Sed /* 326193323Sed * The Giant lock should only change when 327193323Sed * crossing mount points. 328193323Sed */ 329193323Sed if (crossmnt) { 330193323Sed exp->nes_vfslocked = 331193323Sed (ndp->ni_cnd.cn_flags & GIANTHELD) != 0; 332193323Sed ndp->ni_cnd.cn_flags &= ~GIANTHELD; 333193323Sed } 334193323Sed if (error) 335193323Sed break; 336193323Sed 337193323Sed /* 338193323Sed * Check for encountering a symbolic link. Trivial 339193323Sed * termination occurs if no symlink encountered. 340193323Sed */ 341193323Sed if ((cnp->cn_flags & ISSYMLINK) == 0) { 342193323Sed if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 343193323Sed nfsvno_relpathbuf(ndp); 344193323Sed if (ndp->ni_vp && !lockleaf) 345193323Sed NFSVOPUNLOCK(ndp->ni_vp, 0, p); 346193323Sed break; 347193323Sed } 348193323Sed 349193323Sed /* 350193323Sed * Validate symlink 351193323Sed */ 352193323Sed if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 353193323Sed NFSVOPUNLOCK(ndp->ni_dvp, 0, p); 354193323Sed if (!(nd->nd_flag & ND_PUBLOOKUP)) { 355193323Sed error = EINVAL; 356193323Sed goto badlink2; 357193323Sed } 358193323Sed 359193323Sed if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 360193323Sed error = ELOOP; 361193323Sed goto badlink2; 362193323Sed } 363193323Sed if (ndp->ni_pathlen > 1) 364193323Sed cp = uma_zalloc(namei_zone, M_WAITOK); 365193323Sed else 366193323Sed cp = cnp->cn_pnbuf; 367193323Sed aiov.iov_base = cp; 368193323Sed aiov.iov_len = MAXPATHLEN; 369193323Sed auio.uio_iov = &aiov; 370193323Sed auio.uio_iovcnt = 1; 371193323Sed auio.uio_offset = 0; 372193323Sed auio.uio_rw = UIO_READ; 373193323Sed auio.uio_segflg = UIO_SYSSPACE; 374193323Sed auio.uio_td = NULL; 375193323Sed auio.uio_resid = MAXPATHLEN; 376193323Sed error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 377193323Sed if (error) { 378193323Sed badlink1: 379193323Sed if (ndp->ni_pathlen > 1) 380193323Sed uma_zfree(namei_zone, cp); 381193323Sed badlink2: 382193323Sed vrele(ndp->ni_dvp); 383193323Sed vput(ndp->ni_vp); 384193323Sed break; 385193323Sed } 386193323Sed linklen = MAXPATHLEN - auio.uio_resid; 387193323Sed if (linklen == 0) { 388193323Sed error = ENOENT; 389193323Sed goto badlink1; 390193323Sed } 391193323Sed if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 392193323Sed error = ENAMETOOLONG; 393193323Sed goto badlink1; 394193323Sed } 395193323Sed 396193323Sed /* 397193323Sed * Adjust or replace path 398193323Sed */ 399193323Sed if (ndp->ni_pathlen > 1) { 400193323Sed NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 401193323Sed uma_zfree(namei_zone, cnp->cn_pnbuf); 402193323Sed cnp->cn_pnbuf = cp; 403193323Sed } else 404193323Sed cnp->cn_pnbuf[linklen] = '\0'; 405193323Sed ndp->ni_pathlen += linklen; 406193323Sed 407193323Sed /* 408193323Sed * Cleanup refs for next loop and check if root directory 409193323Sed * should replace current directory. Normally ni_dvp 410193323Sed * becomes the new base directory and is cleaned up when 411193323Sed * we loop. Explicitly null pointers after invalidation 412193323Sed * to clarify operation. 413193323Sed */ 414193323Sed vput(ndp->ni_vp); 415193323Sed ndp->ni_vp = NULL; 416193323Sed 417193323Sed if (cnp->cn_pnbuf[0] == '/') { 418193323Sed vrele(ndp->ni_dvp); 419193323Sed ndp->ni_dvp = ndp->ni_rootdir; 420193323Sed VREF(ndp->ni_dvp); 421193323Sed } 422193323Sed ndp->ni_startdir = ndp->ni_dvp; 423193323Sed ndp->ni_dvp = NULL; 424193323Sed } 425193323Sed if (!lockleaf) 426193323Sed cnp->cn_flags &= ~LOCKLEAF; 427193323Sed 428193323Sedout: 429193323Sed if (error) { 430193323Sed uma_zfree(namei_zone, cnp->cn_pnbuf); 431193323Sed ndp->ni_vp = NULL; 432193323Sed ndp->ni_dvp = NULL; 433193323Sed ndp->ni_startdir = NULL; 434193323Sed cnp->cn_flags &= ~HASBUF; 435193323Sed } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 436193323Sed ndp->ni_dvp = NULL; 437193323Sed } 438193323Sed return (error); 439193323Sed} 440193323Sed 441193323Sed/* 442193323Sed * Set up a pathname buffer and return a pointer to it and, optionally 443193323Sed * set a hash pointer. 444193323Sed */ 445193323Sedvoid 446193323Sednfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 447193323Sed{ 448193323Sed struct componentname *cnp = &ndp->ni_cnd; 449193323Sed 450193323Sed cnp->cn_flags |= (NOMACCHECK | HASBUF); 451193323Sed cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 452193323Sed if (hashpp != NULL) 453193323Sed *hashpp = NULL; 454193323Sed *bufpp = cnp->cn_pnbuf; 455193323Sed} 456193323Sed 457193323Sed/* 458193323Sed * Release the above path buffer, if not released by nfsvno_namei(). 459193323Sed */ 460193323Sedvoid 461193323Sednfsvno_relpathbuf(struct nameidata *ndp) 462193323Sed{ 463193323Sed 464193323Sed if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 465193323Sed panic("nfsrelpath"); 466193323Sed uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 467193323Sed ndp->ni_cnd.cn_flags &= ~HASBUF; 468193323Sed} 469193323Sed 470193323Sed/* 471193323Sed * Readlink vnode op into an mbuf list. 472193323Sed */ 473193323Sedint 474193323Sednfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 475193323Sed struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 476193323Sed{ 477193323Sed struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 478193323Sed struct iovec *ivp = iv; 479193323Sed struct uio io, *uiop = &io; 480193323Sed struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 481193323Sed int i, len, tlen, error; 482193323Sed 483193323Sed len = 0; 484193323Sed i = 0; 485193323Sed while (len < NFS_MAXPATHLEN) { 486193323Sed NFSMGET(mp); 487193323Sed MCLGET(mp, M_WAIT); 488193323Sed mp->m_len = NFSMSIZ(mp); 489193323Sed if (len == 0) { 490193323Sed mp3 = mp2 = mp; 491193323Sed } else { 492193323Sed mp2->m_next = mp; 493193323Sed mp2 = mp; 494193323Sed } 495193323Sed if ((len + mp->m_len) > NFS_MAXPATHLEN) { 496193323Sed mp->m_len = NFS_MAXPATHLEN - len; 497193323Sed len = NFS_MAXPATHLEN; 498193323Sed } else { 499193323Sed len += mp->m_len; 500193323Sed } 501193323Sed ivp->iov_base = mtod(mp, caddr_t); 502193323Sed ivp->iov_len = mp->m_len; 503193323Sed i++; 504193323Sed ivp++; 505193323Sed } 506193323Sed uiop->uio_iov = iv; 507193323Sed uiop->uio_iovcnt = i; 508193323Sed uiop->uio_offset = 0; 509193323Sed uiop->uio_resid = len; 510193323Sed uiop->uio_rw = UIO_READ; 511193323Sed uiop->uio_segflg = UIO_SYSSPACE; 512193323Sed uiop->uio_td = NULL; 513193323Sed error = VOP_READLINK(vp, uiop, cred); 514193323Sed if (error) { 515193323Sed m_freem(mp3); 516193323Sed *lenp = 0; 517193323Sed return (error); 518193323Sed } 519193323Sed if (uiop->uio_resid > 0) { 520193323Sed len -= uiop->uio_resid; 521193323Sed tlen = NFSM_RNDUP(len); 522193323Sed nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 523193323Sed } 524193323Sed *lenp = len; 525193323Sed *mpp = mp3; 526193323Sed *mpendp = mp; 527193323Sed return (0); 528193323Sed} 529193323Sed 530193323Sed/* 531193323Sed * Read vnode op call into mbuf list. 532193323Sed */ 533193323Sedint 534193323Sednfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 535193323Sed struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 536193323Sed{ 537193323Sed struct mbuf *m; 538193323Sed int i; 539193323Sed struct iovec *iv; 540193323Sed struct iovec *iv2; 541193323Sed int error = 0, len, left, siz, tlen, ioflag = 0, hi, try = 32; 542193323Sed struct mbuf *m2 = NULL, *m3; 543193323Sed struct uio io, *uiop = &io; 544193323Sed struct nfsheur *nh; 545193323Sed 546193323Sed /* 547193323Sed * Calculate seqcount for heuristic 548193323Sed */ 549193323Sed /* 550193323Sed * Locate best candidate 551193323Sed */ 552193323Sed 553193323Sed hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 554193323Sed nh = &nfsheur[hi]; 555193323Sed 556193323Sed while (try--) { 557193323Sed if (nfsheur[hi].nh_vp == vp) { 558193323Sed nh = &nfsheur[hi]; 559193323Sed break; 560193323Sed } 561193323Sed if (nfsheur[hi].nh_use > 0) 562193323Sed --nfsheur[hi].nh_use; 563193323Sed hi = (hi + 1) % NUM_HEURISTIC; 564193323Sed if (nfsheur[hi].nh_use < nh->nh_use) 565193323Sed nh = &nfsheur[hi]; 566193323Sed } 567193323Sed 568193323Sed if (nh->nh_vp != vp) { 569193323Sed nh->nh_vp = vp; 570193323Sed nh->nh_nextr = off; 571193323Sed nh->nh_use = NHUSE_INIT; 572193323Sed if (off == 0) 573193323Sed nh->nh_seqcount = 4; 574193323Sed else 575193323Sed nh->nh_seqcount = 1; 576193323Sed } 577193323Sed 578193323Sed /* 579193323Sed * Calculate heuristic 580193323Sed */ 581193323Sed 582193323Sed if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) { 583193323Sed if (++nh->nh_seqcount > IO_SEQMAX) 584193323Sed nh->nh_seqcount = IO_SEQMAX; 585193323Sed } else if (nh->nh_seqcount > 1) { 586193323Sed nh->nh_seqcount = 1; 587193323Sed } else { 588193323Sed nh->nh_seqcount = 0; 589193323Sed } 590193323Sed nh->nh_use += NHUSE_INC; 591193323Sed if (nh->nh_use > NHUSE_MAX) 592193323Sed nh->nh_use = NHUSE_MAX; 593193323Sed ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 594193323Sed 595193323Sed len = left = NFSM_RNDUP(cnt); 596193323Sed m3 = NULL; 597193323Sed /* 598193323Sed * Generate the mbuf list with the uio_iov ref. to it. 599193323Sed */ 600193323Sed i = 0; 601193323Sed while (left > 0) { 602193323Sed NFSMGET(m); 603193323Sed MCLGET(m, M_WAIT); 604193323Sed m->m_len = 0; 605193323Sed siz = min(M_TRAILINGSPACE(m), left); 606193323Sed left -= siz; 607193323Sed i++; 608193323Sed if (m3) 609193323Sed m2->m_next = m; 610193323Sed else 611193323Sed m3 = m; 612193323Sed m2 = m; 613193323Sed } 614193323Sed MALLOC(iv, struct iovec *, i * sizeof (struct iovec), 615193323Sed M_TEMP, M_WAITOK); 616193323Sed uiop->uio_iov = iv2 = iv; 617193323Sed m = m3; 618193323Sed left = len; 619193323Sed i = 0; 620193323Sed while (left > 0) { 621193323Sed if (m == NULL) 622193323Sed panic("nfsvno_read iov"); 623193323Sed siz = min(M_TRAILINGSPACE(m), left); 624193323Sed if (siz > 0) { 625193323Sed iv->iov_base = mtod(m, caddr_t) + m->m_len; 626193323Sed iv->iov_len = siz; 627193323Sed m->m_len += siz; 628193323Sed left -= siz; 629193323Sed iv++; 630193323Sed i++; 631193323Sed } 632193323Sed m = m->m_next; 633193323Sed } 634193323Sed uiop->uio_iovcnt = i; 635193323Sed uiop->uio_offset = off; 636193323Sed uiop->uio_resid = len; 637193323Sed uiop->uio_rw = UIO_READ; 638193323Sed uiop->uio_segflg = UIO_SYSSPACE; 639193323Sed error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 640193323Sed FREE((caddr_t)iv2, M_TEMP); 641193323Sed if (error) { 642193323Sed m_freem(m3); 643193323Sed *mpp = NULL; 644193323Sed return (error); 645193323Sed } 646193323Sed tlen = len - uiop->uio_resid; 647193323Sed cnt = cnt < tlen ? cnt : tlen; 648193323Sed tlen = NFSM_RNDUP(cnt); 649193323Sed if (tlen == 0) { 650193323Sed m_freem(m3); 651193323Sed m3 = NULL; 652193323Sed } else if (len != tlen || tlen != cnt) 653193323Sed nfsrv_adj(m3, len - tlen, tlen - cnt); 654193323Sed *mpp = m3; 655193323Sed *mpendp = m2; 656193323Sed return (0); 657193323Sed} 658193323Sed 659193323Sed/* 660193323Sed * Write vnode op from an mbuf list. 661193323Sed */ 662193323Sedint 663193323Sednfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable, 664193323Sed struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 665193323Sed{ 666193323Sed struct iovec *ivp; 667193323Sed int i, len; 668193323Sed struct iovec *iv; 669193323Sed int ioflags, error; 670193323Sed struct uio io, *uiop = &io; 671193323Sed 672193323Sed MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP, 673193323Sed M_WAITOK); 674193323Sed uiop->uio_iov = iv = ivp; 675193323Sed uiop->uio_iovcnt = cnt; 676193323Sed i = mtod(mp, caddr_t) + mp->m_len - cp; 677193323Sed len = retlen; 678193323Sed while (len > 0) { 679193323Sed if (mp == NULL) 680193323Sed panic("nfsvno_write"); 681193323Sed if (i > 0) { 682193323Sed i = min(i, len); 683198090Srdivacky ivp->iov_base = cp; 684193323Sed ivp->iov_len = i; 685193323Sed ivp++; 686198090Srdivacky len -= i; 687193323Sed } 688193323Sed mp = mp->m_next; 689193323Sed if (mp) { 690198090Srdivacky i = mp->m_len; 691193323Sed cp = mtod(mp, caddr_t); 692193323Sed } 693198090Srdivacky } 694193323Sed 695193323Sed if (stable == NFSWRITE_UNSTABLE) 696193323Sed ioflags = IO_NODELOCKED; 697198090Srdivacky else 698198090Srdivacky ioflags = (IO_SYNC | IO_NODELOCKED); 699193323Sed uiop->uio_resid = retlen; 700193323Sed uiop->uio_rw = UIO_WRITE; 701198090Srdivacky uiop->uio_segflg = UIO_SYSSPACE; 702198090Srdivacky NFSUIOPROC(uiop, p); 703193323Sed uiop->uio_offset = off; 704193323Sed error = VOP_WRITE(vp, uiop, ioflags, cred); 705193323Sed FREE((caddr_t)iv, M_TEMP); 706193323Sed return (error); 707193323Sed} 708193323Sed 709193323Sed/* 710193323Sed * Common code for creating a regular file (plus special files for V2). 711193323Sed */ 712193323Sedint 713193323Sednfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 714193323Sed struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 715193323Sed int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp) 716193323Sed{ 717193323Sed u_quad_t tempsize; 718193323Sed int error; 719193323Sed 720193323Sed error = nd->nd_repstat; 721193323Sed if (!error && ndp->ni_vp == NULL) { 722193323Sed if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 723193323Sed vrele(ndp->ni_startdir); 724193323Sed error = VOP_CREATE(ndp->ni_dvp, 725193323Sed &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 726193323Sed vput(ndp->ni_dvp); 727193323Sed nfsvno_relpathbuf(ndp); 728193323Sed if (!error) { 729193323Sed if (*exclusive_flagp) { 730193323Sed *exclusive_flagp = 0; 731193323Sed NFSVNO_ATTRINIT(nvap); 732193323Sed nvap->na_atime.tv_sec = cverf[0]; 733193323Sed nvap->na_atime.tv_nsec = cverf[1]; 734193323Sed error = VOP_SETATTR(ndp->ni_vp, 735193323Sed &nvap->na_vattr, nd->nd_cred); 736193323Sed } 737193323Sed } 738193323Sed /* 739193323Sed * NFS V2 Only. nfsrvd_mknod() does this for V3. 740193323Sed * (This implies, just get out on an error.) 741193323Sed */ 742193323Sed } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 743193323Sed nvap->na_type == VFIFO) { 744193323Sed if (nvap->na_type == VCHR && rdev == 0xffffffff) 745193323Sed nvap->na_type = VFIFO; 746193323Sed if (nvap->na_type != VFIFO && 747193323Sed (error = priv_check_cred(nd->nd_cred, 748193323Sed PRIV_VFS_MKNOD_DEV, 0))) { 749193323Sed vrele(ndp->ni_startdir); 750193323Sed nfsvno_relpathbuf(ndp); 751193323Sed vput(ndp->ni_dvp); 752193323Sed return (error); 753193323Sed } 754193323Sed nvap->na_rdev = rdev; 755193323Sed error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 756193323Sed &ndp->ni_cnd, &nvap->na_vattr); 757193323Sed vput(ndp->ni_dvp); 758193323Sed nfsvno_relpathbuf(ndp); 759193323Sed if (error) { 760193323Sed vrele(ndp->ni_startdir); 761193323Sed return (error); 762193323Sed } 763193323Sed } else { 764193323Sed vrele(ndp->ni_startdir); 765193323Sed nfsvno_relpathbuf(ndp); 766193323Sed vput(ndp->ni_dvp); 767193323Sed return (ENXIO); 768193323Sed } 769193323Sed *vpp = ndp->ni_vp; 770193323Sed } else { 771193323Sed /* 772193323Sed * Handle cases where error is already set and/or 773193323Sed * the file exists. 774193323Sed * 1 - clean up the lookup 775193323Sed * 2 - iff !error and na_size set, truncate it 776193323Sed */ 777193323Sed vrele(ndp->ni_startdir); 778193323Sed nfsvno_relpathbuf(ndp); 779193323Sed *vpp = ndp->ni_vp; 780193323Sed if (ndp->ni_dvp == *vpp) 781193323Sed vrele(ndp->ni_dvp); 782193323Sed else 783193323Sed vput(ndp->ni_dvp); 784193323Sed if (!error && nvap->na_size != VNOVAL) { 785193323Sed error = nfsvno_accchk(*vpp, VWRITE, 786193323Sed nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 787193323Sed NFSACCCHK_VPISLOCKED, NULL); 788193323Sed if (!error) { 789193323Sed tempsize = nvap->na_size; 790193323Sed NFSVNO_ATTRINIT(nvap); 791193323Sed nvap->na_size = tempsize; 792193323Sed error = VOP_SETATTR(*vpp, 793193323Sed &nvap->na_vattr, nd->nd_cred); 794193323Sed } 795193323Sed } 796193323Sed if (error) 797193323Sed vput(*vpp); 798193323Sed } 799193323Sed return (error); 800193323Sed} 801193323Sed 802193323Sed/* 803193323Sed * Do a mknod vnode op. 804193323Sed */ 805193323Sedint 806193323Sednfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 807193323Sed struct thread *p) 808193323Sed{ 809193323Sed int error = 0; 810193323Sed enum vtype vtyp; 811193323Sed 812193323Sed vtyp = nvap->na_type; 813193323Sed /* 814193323Sed * Iff doesn't exist, create it. 815193323Sed */ 816193323Sed if (ndp->ni_vp) { 817193323Sed vrele(ndp->ni_startdir); 818193323Sed nfsvno_relpathbuf(ndp); 819193323Sed vput(ndp->ni_dvp); 820193323Sed vrele(ndp->ni_vp); 821193323Sed return (EEXIST); 822193323Sed } 823193323Sed if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 824193323Sed vrele(ndp->ni_startdir); 825193323Sed nfsvno_relpathbuf(ndp); 826193323Sed vput(ndp->ni_dvp); 827193323Sed return (NFSERR_BADTYPE); 828193323Sed } 829193323Sed if (vtyp == VSOCK) { 830193323Sed vrele(ndp->ni_startdir); 831193323Sed error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 832193323Sed &ndp->ni_cnd, &nvap->na_vattr); 833193323Sed vput(ndp->ni_dvp); 834193323Sed nfsvno_relpathbuf(ndp); 835193323Sed } else { 836193323Sed if (nvap->na_type != VFIFO && 837193323Sed (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) { 838193323Sed vrele(ndp->ni_startdir); 839193323Sed nfsvno_relpathbuf(ndp); 840193323Sed vput(ndp->ni_dvp); 841193323Sed return (error); 842193323Sed } 843193323Sed error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 844193323Sed &ndp->ni_cnd, &nvap->na_vattr); 845193323Sed vput(ndp->ni_dvp); 846193323Sed nfsvno_relpathbuf(ndp); 847193323Sed if (error) 848193323Sed vrele(ndp->ni_startdir); 849193323Sed /* 850193323Sed * Since VOP_MKNOD returns the ni_vp, I can't 851193323Sed * see any reason to do the lookup. 852193323Sed */ 853193323Sed } 854 return (error); 855} 856 857/* 858 * Mkdir vnode op. 859 */ 860int 861nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 862 struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 863{ 864 int error = 0; 865 866 if (ndp->ni_vp != NULL) { 867 if (ndp->ni_dvp == ndp->ni_vp) 868 vrele(ndp->ni_dvp); 869 else 870 vput(ndp->ni_dvp); 871 vrele(ndp->ni_vp); 872 return (EEXIST); 873 } 874 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 875 &nvap->na_vattr); 876 vput(ndp->ni_dvp); 877 nfsvno_relpathbuf(ndp); 878 return (error); 879} 880 881/* 882 * symlink vnode op. 883 */ 884int 885nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 886 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 887 struct nfsexstuff *exp) 888{ 889 int error = 0; 890 891 if (ndp->ni_vp) { 892 vrele(ndp->ni_startdir); 893 nfsvno_relpathbuf(ndp); 894 if (ndp->ni_dvp == ndp->ni_vp) 895 vrele(ndp->ni_dvp); 896 else 897 vput(ndp->ni_dvp); 898 vrele(ndp->ni_vp); 899 return (EEXIST); 900 } 901 902 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 903 &nvap->na_vattr, pathcp); 904 vput(ndp->ni_dvp); 905 vrele(ndp->ni_startdir); 906 nfsvno_relpathbuf(ndp); 907 /* 908 * Although FreeBSD still had the lookup code in 909 * it for 7/current, there doesn't seem to be any 910 * point, since VOP_SYMLINK() returns the ni_vp. 911 * Just vput it for v2. 912 */ 913 if (!not_v2 && !error) 914 vput(ndp->ni_vp); 915 return (error); 916} 917 918/* 919 * Parse symbolic link arguments. 920 * This function has an ugly side effect. It will MALLOC() an area for 921 * the symlink and set iov_base to point to it, only if it succeeds. 922 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 923 * be FREE'd later. 924 */ 925int 926nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 927 struct thread *p, char **pathcpp, int *lenp) 928{ 929 u_int32_t *tl; 930 char *pathcp = NULL; 931 int error = 0, len; 932 struct nfsv2_sattr *sp; 933 934 *pathcpp = NULL; 935 *lenp = 0; 936 if ((nd->nd_flag & ND_NFSV3) && 937 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p))) 938 goto nfsmout; 939 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 940 len = fxdr_unsigned(int, *tl); 941 if (len > NFS_MAXPATHLEN || len <= 0) { 942 error = EBADRPC; 943 goto nfsmout; 944 } 945 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK); 946 error = nfsrv_mtostr(nd, pathcp, len); 947 if (error) 948 goto nfsmout; 949 if (nd->nd_flag & ND_NFSV2) { 950 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 951 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 952 } 953 *pathcpp = pathcp; 954 *lenp = len; 955 return (0); 956nfsmout: 957 if (pathcp) 958 free(pathcp, M_TEMP); 959 return (error); 960} 961 962/* 963 * Remove a non-directory object. 964 */ 965int 966nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 967 struct thread *p, struct nfsexstuff *exp) 968{ 969 struct vnode *vp; 970 int error = 0; 971 972 vp = ndp->ni_vp; 973 if (vp->v_type == VDIR) 974 error = NFSERR_ISDIR; 975 else if (is_v4) 976 error = nfsrv_checkremove(vp, 1, p); 977 if (!error) 978 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 979 if (ndp->ni_dvp == vp) 980 vrele(ndp->ni_dvp); 981 else 982 vput(ndp->ni_dvp); 983 vput(vp); 984 return (error); 985} 986 987/* 988 * Remove a directory. 989 */ 990int 991nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 992 struct thread *p, struct nfsexstuff *exp) 993{ 994 struct vnode *vp; 995 int error = 0; 996 997 vp = ndp->ni_vp; 998 if (vp->v_type != VDIR) { 999 error = ENOTDIR; 1000 goto out; 1001 } 1002 /* 1003 * No rmdir "." please. 1004 */ 1005 if (ndp->ni_dvp == vp) { 1006 error = EINVAL; 1007 goto out; 1008 } 1009 /* 1010 * The root of a mounted filesystem cannot be deleted. 1011 */ 1012 if (vp->v_vflag & VV_ROOT) 1013 error = EBUSY; 1014out: 1015 if (!error) 1016 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1017 if (ndp->ni_dvp == vp) 1018 vrele(ndp->ni_dvp); 1019 else 1020 vput(ndp->ni_dvp); 1021 vput(vp); 1022 return (error); 1023} 1024 1025/* 1026 * Rename vnode op. 1027 */ 1028int 1029nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1030 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1031{ 1032 struct vnode *fvp, *tvp, *tdvp; 1033 int error = 0; 1034 1035 fvp = fromndp->ni_vp; 1036 if (ndstat) { 1037 vrele(fromndp->ni_dvp); 1038 vrele(fvp); 1039 error = ndstat; 1040 goto out1; 1041 } 1042 tdvp = tondp->ni_dvp; 1043 tvp = tondp->ni_vp; 1044 if (tvp != NULL) { 1045 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1046 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1047 goto out; 1048 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1049 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1050 goto out; 1051 } 1052 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1053 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1054 goto out; 1055 } 1056 1057 /* 1058 * A rename to '.' or '..' results in a prematurely 1059 * unlocked vnode on FreeBSD5, so I'm just going to fail that 1060 * here. 1061 */ 1062 if ((tondp->ni_cnd.cn_namelen == 1 && 1063 tondp->ni_cnd.cn_nameptr[0] == '.') || 1064 (tondp->ni_cnd.cn_namelen == 2 && 1065 tondp->ni_cnd.cn_nameptr[0] == '.' && 1066 tondp->ni_cnd.cn_nameptr[1] == '.')) { 1067 error = EINVAL; 1068 goto out; 1069 } 1070 } 1071 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1072 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1073 goto out; 1074 } 1075 if (fvp->v_mount != tdvp->v_mount) { 1076 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1077 goto out; 1078 } 1079 if (fvp == tdvp) { 1080 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1081 goto out; 1082 } 1083 if (fvp == tvp) { 1084 /* 1085 * If source and destination are the same, there is nothing to 1086 * do. Set error to -1 to indicate this. 1087 */ 1088 error = -1; 1089 goto out; 1090 } 1091 if (ndflag & ND_NFSV4) { 1092 NFSVOPLOCK(fvp, LK_EXCLUSIVE | LK_RETRY, p); 1093 error = nfsrv_checkremove(fvp, 0, p); 1094 NFSVOPUNLOCK(fvp, 0, p); 1095 if (tvp && !error) 1096 error = nfsrv_checkremove(tvp, 1, p); 1097 } else { 1098 /* 1099 * For NFSv2 and NFSv3, try to get rid of the delegation, so 1100 * that the NFSv4 client won't be confused by the rename. 1101 * Since nfsd_recalldelegation() can only be called on an 1102 * unlocked vnode at this point and fvp is the file that will 1103 * still exist after the rename, just do fvp. 1104 */ 1105 nfsd_recalldelegation(fvp, p); 1106 } 1107out: 1108 if (!error) { 1109 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1110 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1111 &tondp->ni_cnd); 1112 } else { 1113 if (tdvp == tvp) 1114 vrele(tdvp); 1115 else 1116 vput(tdvp); 1117 if (tvp) 1118 vput(tvp); 1119 vrele(fromndp->ni_dvp); 1120 vrele(fvp); 1121 if (error == -1) 1122 error = 0; 1123 } 1124 vrele(tondp->ni_startdir); 1125 nfsvno_relpathbuf(tondp); 1126out1: 1127 vrele(fromndp->ni_startdir); 1128 nfsvno_relpathbuf(fromndp); 1129 return (error); 1130} 1131 1132/* 1133 * Link vnode op. 1134 */ 1135int 1136nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1137 struct thread *p, struct nfsexstuff *exp) 1138{ 1139 struct vnode *xp; 1140 int error = 0; 1141 1142 xp = ndp->ni_vp; 1143 if (xp != NULL) { 1144 error = EEXIST; 1145 } else { 1146 xp = ndp->ni_dvp; 1147 if (vp->v_mount != xp->v_mount) 1148 error = EXDEV; 1149 } 1150 if (!error) { 1151 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 1152 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1153 if (ndp->ni_dvp == vp) 1154 vrele(ndp->ni_dvp); 1155 else 1156 vput(ndp->ni_dvp); 1157 NFSVOPUNLOCK(vp, 0, p); 1158 } else { 1159 if (ndp->ni_dvp == ndp->ni_vp) 1160 vrele(ndp->ni_dvp); 1161 else 1162 vput(ndp->ni_dvp); 1163 if (ndp->ni_vp) 1164 vrele(ndp->ni_vp); 1165 } 1166 nfsvno_relpathbuf(ndp); 1167 return (error); 1168} 1169 1170/* 1171 * Do the fsync() appropriate for the commit. 1172 */ 1173int 1174nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1175 struct thread *td) 1176{ 1177 int error = 0; 1178 1179 if (cnt > MAX_COMMIT_COUNT) { 1180 /* 1181 * Give up and do the whole thing 1182 */ 1183 if (vp->v_object && 1184 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1185 VM_OBJECT_LOCK(vp->v_object); 1186 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1187 VM_OBJECT_UNLOCK(vp->v_object); 1188 } 1189 error = VOP_FSYNC(vp, MNT_WAIT, td); 1190 } else { 1191 /* 1192 * Locate and synchronously write any buffers that fall 1193 * into the requested range. Note: we are assuming that 1194 * f_iosize is a power of 2. 1195 */ 1196 int iosize = vp->v_mount->mnt_stat.f_iosize; 1197 int iomask = iosize - 1; 1198 struct bufobj *bo; 1199 daddr_t lblkno; 1200 1201 /* 1202 * Align to iosize boundry, super-align to page boundry. 1203 */ 1204 if (off & iomask) { 1205 cnt += off & iomask; 1206 off &= ~(u_quad_t)iomask; 1207 } 1208 if (off & PAGE_MASK) { 1209 cnt += off & PAGE_MASK; 1210 off &= ~(u_quad_t)PAGE_MASK; 1211 } 1212 lblkno = off / iosize; 1213 1214 if (vp->v_object && 1215 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1216 VM_OBJECT_LOCK(vp->v_object); 1217 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC); 1218 VM_OBJECT_UNLOCK(vp->v_object); 1219 } 1220 1221 bo = &vp->v_bufobj; 1222 BO_LOCK(bo); 1223 while (cnt > 0) { 1224 struct buf *bp; 1225 1226 /* 1227 * If we have a buffer and it is marked B_DELWRI we 1228 * have to lock and write it. Otherwise the prior 1229 * write is assumed to have already been committed. 1230 * 1231 * gbincore() can return invalid buffers now so we 1232 * have to check that bit as well (though B_DELWRI 1233 * should not be set if B_INVAL is set there could be 1234 * a race here since we haven't locked the buffer). 1235 */ 1236 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1237 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1238 LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) { 1239 BO_LOCK(bo); 1240 continue; /* retry */ 1241 } 1242 if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1243 B_DELWRI) { 1244 bremfree(bp); 1245 bp->b_flags &= ~B_ASYNC; 1246 bwrite(bp); 1247 ++nfs_commit_miss; 1248 } else 1249 BUF_UNLOCK(bp); 1250 BO_LOCK(bo); 1251 } 1252 ++nfs_commit_blks; 1253 if (cnt < iosize) 1254 break; 1255 cnt -= iosize; 1256 ++lblkno; 1257 } 1258 BO_UNLOCK(bo); 1259 } 1260 return (error); 1261} 1262 1263/* 1264 * Statfs vnode op. 1265 */ 1266int 1267nfsvno_statfs(struct vnode *vp, struct statfs *sf) 1268{ 1269 1270 return (VFS_STATFS(vp->v_mount, sf)); 1271} 1272 1273/* 1274 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1275 * must handle nfsrv_opencheck() calls after any other access checks. 1276 */ 1277void 1278nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1279 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1280 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1281 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p, 1282 struct nfsexstuff *exp, struct vnode **vpp) 1283{ 1284 struct vnode *vp = NULL; 1285 u_quad_t tempsize; 1286 struct nfsexstuff nes; 1287 1288 if (ndp->ni_vp == NULL) 1289 nd->nd_repstat = nfsrv_opencheck(clientid, 1290 stateidp, stp, NULL, nd, p, nd->nd_repstat); 1291 if (!nd->nd_repstat) { 1292 if (ndp->ni_vp == NULL) { 1293 vrele(ndp->ni_startdir); 1294 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1295 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1296 vput(ndp->ni_dvp); 1297 nfsvno_relpathbuf(ndp); 1298 if (!nd->nd_repstat) { 1299 if (*exclusive_flagp) { 1300 *exclusive_flagp = 0; 1301 NFSVNO_ATTRINIT(nvap); 1302 nvap->na_atime.tv_sec = cverf[0]; 1303 nvap->na_atime.tv_nsec = cverf[1]; 1304 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1305 &nvap->na_vattr, cred); 1306 } else { 1307 nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1308 aclp, p, attrbitp, exp); 1309 } 1310 } 1311 vp = ndp->ni_vp; 1312 } else { 1313 if (ndp->ni_startdir) 1314 vrele(ndp->ni_startdir); 1315 nfsvno_relpathbuf(ndp); 1316 vp = ndp->ni_vp; 1317 if (create == NFSV4OPEN_CREATE) { 1318 if (ndp->ni_dvp == vp) 1319 vrele(ndp->ni_dvp); 1320 else 1321 vput(ndp->ni_dvp); 1322 } 1323 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1324 if (ndp->ni_cnd.cn_flags & RDONLY) 1325 NFSVNO_SETEXRDONLY(&nes); 1326 else 1327 NFSVNO_EXINIT(&nes); 1328 nd->nd_repstat = nfsvno_accchk(vp, 1329 VWRITE, cred, &nes, p, 1330 NFSACCCHK_NOOVERRIDE, 1331 NFSACCCHK_VPISLOCKED, NULL); 1332 nd->nd_repstat = nfsrv_opencheck(clientid, 1333 stateidp, stp, vp, nd, p, nd->nd_repstat); 1334 if (!nd->nd_repstat) { 1335 tempsize = nvap->na_size; 1336 NFSVNO_ATTRINIT(nvap); 1337 nvap->na_size = tempsize; 1338 nd->nd_repstat = VOP_SETATTR(vp, 1339 &nvap->na_vattr, cred); 1340 } 1341 } else if (vp->v_type == VREG) { 1342 nd->nd_repstat = nfsrv_opencheck(clientid, 1343 stateidp, stp, vp, nd, p, nd->nd_repstat); 1344 } 1345 } 1346 } else { 1347 if (ndp->ni_cnd.cn_flags & HASBUF) 1348 nfsvno_relpathbuf(ndp); 1349 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1350 vrele(ndp->ni_startdir); 1351 if (ndp->ni_dvp == ndp->ni_vp) 1352 vrele(ndp->ni_dvp); 1353 else 1354 vput(ndp->ni_dvp); 1355 if (ndp->ni_vp) 1356 vput(ndp->ni_vp); 1357 } 1358 } 1359 *vpp = vp; 1360} 1361 1362/* 1363 * Updates the file rev and sets the mtime and ctime 1364 * to the current clock time, returning the va_filerev and va_Xtime 1365 * values. 1366 */ 1367void 1368nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1369 struct ucred *cred, struct thread *p) 1370{ 1371 struct vattr va; 1372 1373 VATTR_NULL(&va); 1374 getnanotime(&va.va_mtime); 1375 (void) VOP_SETATTR(vp, &va, cred); 1376 (void) nfsvno_getattr(vp, nvap, cred, p); 1377} 1378 1379/* 1380 * Glue routine to nfsv4_fillattr(). 1381 */ 1382int 1383nfsvno_fillattr(struct nfsrv_descript *nd, struct vnode *vp, 1384 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1385 struct ucred *cred, struct thread *p, int isdgram, int reterr) 1386{ 1387 int error; 1388 1389 error = nfsv4_fillattr(nd, vp, NULL, &nvap->na_vattr, fhp, rderror, 1390 attrbitp, cred, p, isdgram, reterr); 1391 return (error); 1392} 1393 1394/* Since the Readdir vnode ops vary, put the entire functions in here. */ 1395/* 1396 * nfs readdir service 1397 * - mallocs what it thinks is enough to read 1398 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1399 * - calls nfsvno_readdir() 1400 * - loops around building the reply 1401 * if the output generated exceeds count break out of loop 1402 * The NFSM_CLGET macro is used here so that the reply will be packed 1403 * tightly in mbuf clusters. 1404 * - it only knows that it has encountered eof when the nfsvno_readdir() 1405 * reads nothing 1406 * - as such one readdir rpc will return eof false although you are there 1407 * and then the next will return eof 1408 * - it trims out records with d_fileno == 0 1409 * this doesn't matter for Unix clients, but they might confuse clients 1410 * for other os'. 1411 * - it trims out records with d_type == DT_WHT 1412 * these cannot be seen through NFS (unless we extend the protocol) 1413 * NB: It is tempting to set eof to true if the nfsvno_readdir() reads less 1414 * than requested, but this may not apply to all filesystems. For 1415 * example, client NFS does not { although it is never remote mounted 1416 * anyhow } 1417 * The alternate call nfsrvd_readdirplus() does lookups as well. 1418 * PS: The NFS protocol spec. does not clarify what the "count" byte 1419 * argument is a count of.. just name strings and file id's or the 1420 * entire reply rpc or ... 1421 * I tried just file name and id sizes and it confused the Sun client, 1422 * so I am using the full rpc size now. The "paranoia.." comment refers 1423 * to including the status longwords that are not a part of the dir. 1424 * "entry" structures, but are in the rpc. 1425 */ 1426int 1427nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1428 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1429{ 1430 struct dirent *dp; 1431 u_int32_t *tl; 1432 int dirlen; 1433 char *cpos, *cend, *rbuf; 1434 struct nfsvattr at; 1435 int nlen, error = 0, getret = 1; 1436 int siz, cnt, fullsiz, eofflag, ncookies; 1437 u_int64_t off, toff, verf; 1438 u_long *cookies = NULL, *cookiep; 1439 struct uio io; 1440 struct iovec iv; 1441 1442 if (nd->nd_repstat) { 1443 nfsrv_postopattr(nd, getret, &at); 1444 return (0); 1445 } 1446 if (nd->nd_flag & ND_NFSV2) { 1447 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1448 off = fxdr_unsigned(u_quad_t, *tl++); 1449 } else { 1450 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1451 off = fxdr_hyper(tl); 1452 tl += 2; 1453 verf = fxdr_hyper(tl); 1454 tl += 2; 1455 } 1456 toff = off; 1457 cnt = fxdr_unsigned(int, *tl); 1458 if (cnt > NFS_SRVMAXDATA(nd)) 1459 cnt = NFS_SRVMAXDATA(nd); 1460 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1461 fullsiz = siz; 1462 if (nd->nd_flag & ND_NFSV3) { 1463 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, 1464 p); 1465#if 0 1466 /* 1467 * va_filerev is not sufficient as a cookie verifier, 1468 * since it is not supposed to change when entries are 1469 * removed/added unless that offset cookies returned to 1470 * the client are no longer valid. 1471 */ 1472 if (!nd->nd_repstat && toff && verf != at.na_filerev) 1473 nd->nd_repstat = NFSERR_BAD_COOKIE; 1474#endif 1475 } 1476 if (!nd->nd_repstat) 1477 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1478 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1479 NFSACCCHK_VPISLOCKED, NULL); 1480 if (nd->nd_repstat) { 1481 vput(vp); 1482 if (nd->nd_flag & ND_NFSV3) 1483 nfsrv_postopattr(nd, getret, &at); 1484 return (0); 1485 } 1486 NFSVOPUNLOCK(vp, 0, p); 1487 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1488again: 1489 eofflag = 0; 1490 if (cookies) { 1491 free((caddr_t)cookies, M_TEMP); 1492 cookies = NULL; 1493 } 1494 1495 iv.iov_base = rbuf; 1496 iv.iov_len = siz; 1497 io.uio_iov = &iv; 1498 io.uio_iovcnt = 1; 1499 io.uio_offset = (off_t)off; 1500 io.uio_resid = siz; 1501 io.uio_segflg = UIO_SYSSPACE; 1502 io.uio_rw = UIO_READ; 1503 io.uio_td = NULL; 1504 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 1505 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1506 &cookies); 1507 NFSVOPUNLOCK(vp, 0, p); 1508 off = (u_int64_t)io.uio_offset; 1509 if (io.uio_resid) 1510 siz -= io.uio_resid; 1511 1512 if (!cookies && !nd->nd_repstat) 1513 nd->nd_repstat = NFSERR_PERM; 1514 if (nd->nd_flag & ND_NFSV3) { 1515 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p); 1516 if (!nd->nd_repstat) 1517 nd->nd_repstat = getret; 1518 } 1519 1520 /* 1521 * Handles the failed cases. nd->nd_repstat == 0 past here. 1522 */ 1523 if (nd->nd_repstat) { 1524 vrele(vp); 1525 free((caddr_t)rbuf, M_TEMP); 1526 if (cookies) 1527 free((caddr_t)cookies, M_TEMP); 1528 if (nd->nd_flag & ND_NFSV3) 1529 nfsrv_postopattr(nd, getret, &at); 1530 return (0); 1531 } 1532 /* 1533 * If nothing read, return eof 1534 * rpc reply 1535 */ 1536 if (siz == 0) { 1537 vrele(vp); 1538 if (nd->nd_flag & ND_NFSV2) { 1539 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1540 } else { 1541 nfsrv_postopattr(nd, getret, &at); 1542 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1543 txdr_hyper(at.na_filerev, tl); 1544 tl += 2; 1545 } 1546 *tl++ = newnfs_false; 1547 *tl = newnfs_true; 1548 FREE((caddr_t)rbuf, M_TEMP); 1549 FREE((caddr_t)cookies, M_TEMP); 1550 return (0); 1551 } 1552 1553 /* 1554 * Check for degenerate cases of nothing useful read. 1555 * If so go try again 1556 */ 1557 cpos = rbuf; 1558 cend = rbuf + siz; 1559 dp = (struct dirent *)cpos; 1560 cookiep = cookies; 1561 1562 /* 1563 * For some reason FreeBSD's ufs_readdir() chooses to back the 1564 * directory offset up to a block boundary, so it is necessary to 1565 * skip over the records that precede the requested offset. This 1566 * requires the assumption that file offset cookies monotonically 1567 * increase. 1568 */ 1569 while (cpos < cend && ncookies > 0 && 1570 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1571 ((u_quad_t)(*cookiep)) <= toff)) { 1572 cpos += dp->d_reclen; 1573 dp = (struct dirent *)cpos; 1574 cookiep++; 1575 ncookies--; 1576 } 1577 if (cpos >= cend || ncookies == 0) { 1578 siz = fullsiz; 1579 toff = off; 1580 goto again; 1581 } 1582 1583 /* 1584 * dirlen is the size of the reply, including all XDR and must 1585 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1586 * if the XDR should be included in "count", but to be safe, we do. 1587 * (Include the two booleans at the end of the reply in dirlen now.) 1588 */ 1589 if (nd->nd_flag & ND_NFSV3) { 1590 nfsrv_postopattr(nd, getret, &at); 1591 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1592 txdr_hyper(at.na_filerev, tl); 1593 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1594 } else { 1595 dirlen = 2 * NFSX_UNSIGNED; 1596 } 1597 1598 /* Loop through the records and build reply */ 1599 while (cpos < cend && ncookies > 0) { 1600 nlen = dp->d_namlen; 1601 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1602 nlen <= NFS_MAXNAMLEN) { 1603 if (nd->nd_flag & ND_NFSV3) 1604 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1605 else 1606 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1607 if (dirlen > cnt) { 1608 eofflag = 0; 1609 break; 1610 } 1611 1612 /* 1613 * Build the directory record xdr from 1614 * the dirent entry. 1615 */ 1616 if (nd->nd_flag & ND_NFSV3) { 1617 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1618 *tl++ = newnfs_true; 1619 *tl++ = 0; 1620 } else { 1621 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1622 *tl++ = newnfs_true; 1623 } 1624 *tl = txdr_unsigned(dp->d_fileno); 1625 (void) nfsm_strtom(nd, dp->d_name, nlen); 1626 if (nd->nd_flag & ND_NFSV3) { 1627 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1628 *tl++ = 0; 1629 } else 1630 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1631 *tl = txdr_unsigned(*cookiep); 1632 } 1633 cpos += dp->d_reclen; 1634 dp = (struct dirent *)cpos; 1635 cookiep++; 1636 ncookies--; 1637 } 1638 if (cpos < cend) 1639 eofflag = 0; 1640 vrele(vp); 1641 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1642 *tl++ = newnfs_false; 1643 if (eofflag) 1644 *tl = newnfs_true; 1645 else 1646 *tl = newnfs_false; 1647 FREE((caddr_t)rbuf, M_TEMP); 1648 FREE((caddr_t)cookies, M_TEMP); 1649 return (0); 1650nfsmout: 1651 vput(vp); 1652 return (error); 1653} 1654 1655/* 1656 * Readdirplus for V3 and Readdir for V4. 1657 */ 1658int 1659nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 1660 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1661{ 1662 struct dirent *dp; 1663 u_int32_t *tl; 1664 int dirlen; 1665 char *cpos, *cend, *rbuf; 1666 struct vnode *nvp; 1667 fhandle_t nfh; 1668 struct nfsvattr nva, at, *nvap = &nva; 1669 struct mbuf *mb0, *mb1; 1670 struct nfsreferral *refp; 1671 int nlen, r, error = 0, getret = 1, usevget = 1; 1672 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 1673 caddr_t bpos0, bpos1; 1674 u_int64_t off, toff, verf; 1675 u_long *cookies = NULL, *cookiep; 1676 nfsattrbit_t attrbits, rderrbits, savbits; 1677 struct uio io; 1678 struct iovec iv; 1679 struct componentname cn; 1680 1681 if (nd->nd_repstat) { 1682 nfsrv_postopattr(nd, getret, &at); 1683 return (0); 1684 } 1685 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 1686 off = fxdr_hyper(tl); 1687 toff = off; 1688 tl += 2; 1689 verf = fxdr_hyper(tl); 1690 tl += 2; 1691 siz = fxdr_unsigned(int, *tl++); 1692 cnt = fxdr_unsigned(int, *tl); 1693 1694 /* 1695 * Use the server's maximum data transfer size as the upper bound 1696 * on reply datalen. 1697 */ 1698 if (cnt > NFS_SRVMAXDATA(nd)) 1699 cnt = NFS_SRVMAXDATA(nd); 1700 1701 /* 1702 * siz is a "hint" of how much directory information (name, fileid, 1703 * cookie) should be in the reply. At least one client "hints" 0, 1704 * so I set it to cnt for that case. I also round it up to the 1705 * next multiple of DIRBLKSIZ. 1706 */ 1707 if (siz == 0) 1708 siz = cnt; 1709 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1710 1711 if (nd->nd_flag & ND_NFSV4) { 1712 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1713 if (error) 1714 goto nfsmout; 1715 NFSSET_ATTRBIT(&savbits, &attrbits); 1716 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits); 1717 NFSZERO_ATTRBIT(&rderrbits); 1718 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 1719 } else { 1720 NFSZERO_ATTRBIT(&attrbits); 1721 } 1722 fullsiz = siz; 1723 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p); 1724 if (!nd->nd_repstat) { 1725 if (off && verf != at.na_filerev) { 1726 /* 1727 * va_filerev is not sufficient as a cookie verifier, 1728 * since it is not supposed to change when entries are 1729 * removed/added unless that offset cookies returned to 1730 * the client are no longer valid. 1731 */ 1732#if 0 1733 if (nd->nd_flag & ND_NFSV4) { 1734 nd->nd_repstat = NFSERR_NOTSAME; 1735 } else { 1736 nd->nd_repstat = NFSERR_BAD_COOKIE; 1737 } 1738#endif 1739 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) { 1740 nd->nd_repstat = NFSERR_BAD_COOKIE; 1741 } 1742 } 1743 if (!nd->nd_repstat && vp->v_type != VDIR) 1744 nd->nd_repstat = NFSERR_NOTDIR; 1745 if (!nd->nd_repstat && cnt == 0) 1746 nd->nd_repstat = NFSERR_TOOSMALL; 1747 if (!nd->nd_repstat) 1748 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1749 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1750 NFSACCCHK_VPISLOCKED, NULL); 1751 if (nd->nd_repstat) { 1752 vput(vp); 1753 if (nd->nd_flag & ND_NFSV3) 1754 nfsrv_postopattr(nd, getret, &at); 1755 return (0); 1756 } 1757 1758 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1759again: 1760 eofflag = 0; 1761 if (cookies) { 1762 free((caddr_t)cookies, M_TEMP); 1763 cookies = NULL; 1764 } 1765 1766 iv.iov_base = rbuf; 1767 iv.iov_len = siz; 1768 io.uio_iov = &iv; 1769 io.uio_iovcnt = 1; 1770 io.uio_offset = (off_t)off; 1771 io.uio_resid = siz; 1772 io.uio_segflg = UIO_SYSSPACE; 1773 io.uio_rw = UIO_READ; 1774 io.uio_td = NULL; 1775 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1776 &cookies); 1777 off = (u_int64_t)io.uio_offset; 1778 if (io.uio_resid) 1779 siz -= io.uio_resid; 1780 1781 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p); 1782 1783 if (!cookies && !nd->nd_repstat) 1784 nd->nd_repstat = NFSERR_PERM; 1785 if (!nd->nd_repstat) 1786 nd->nd_repstat = getret; 1787 if (nd->nd_repstat) { 1788 vput(vp); 1789 if (cookies) 1790 free((caddr_t)cookies, M_TEMP); 1791 free((caddr_t)rbuf, M_TEMP); 1792 if (nd->nd_flag & ND_NFSV3) 1793 nfsrv_postopattr(nd, getret, &at); 1794 return (0); 1795 } 1796 /* 1797 * If nothing read, return eof 1798 * rpc reply 1799 */ 1800 if (siz == 0) { 1801 vput(vp); 1802 if (nd->nd_flag & ND_NFSV3) 1803 nfsrv_postopattr(nd, getret, &at); 1804 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1805 txdr_hyper(at.na_filerev, tl); 1806 tl += 2; 1807 *tl++ = newnfs_false; 1808 *tl = newnfs_true; 1809 free((caddr_t)cookies, M_TEMP); 1810 free((caddr_t)rbuf, M_TEMP); 1811 return (0); 1812 } 1813 1814 /* 1815 * Check for degenerate cases of nothing useful read. 1816 * If so go try again 1817 */ 1818 cpos = rbuf; 1819 cend = rbuf + siz; 1820 dp = (struct dirent *)cpos; 1821 cookiep = cookies; 1822 1823 /* 1824 * For some reason FreeBSD's ufs_readdir() chooses to back the 1825 * directory offset up to a block boundary, so it is necessary to 1826 * skip over the records that precede the requested offset. This 1827 * requires the assumption that file offset cookies monotonically 1828 * increase. 1829 */ 1830 while (cpos < cend && ncookies > 0 && 1831 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1832 ((u_quad_t)(*cookiep)) <= toff || 1833 ((nd->nd_flag & ND_NFSV4) && 1834 ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 1835 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 1836 cpos += dp->d_reclen; 1837 dp = (struct dirent *)cpos; 1838 cookiep++; 1839 ncookies--; 1840 } 1841 if (cpos >= cend || ncookies == 0) { 1842 siz = fullsiz; 1843 toff = off; 1844 goto again; 1845 } 1846 NFSVOPUNLOCK(vp, 0, p); 1847 1848 /* 1849 * Save this position, in case there is an error before one entry 1850 * is created. 1851 */ 1852 mb0 = nd->nd_mb; 1853 bpos0 = nd->nd_bpos; 1854 1855 /* 1856 * Fill in the first part of the reply. 1857 * dirlen is the reply length in bytes and cannot exceed cnt. 1858 * (Include the two booleans at the end of the reply in dirlen now, 1859 * so we recognize when we have exceeded cnt.) 1860 */ 1861 if (nd->nd_flag & ND_NFSV3) { 1862 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1863 nfsrv_postopattr(nd, getret, &at); 1864 } else { 1865 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 1866 } 1867 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 1868 txdr_hyper(at.na_filerev, tl); 1869 1870 /* 1871 * Save this position, in case there is an empty reply needed. 1872 */ 1873 mb1 = nd->nd_mb; 1874 bpos1 = nd->nd_bpos; 1875 1876 /* Loop through the records and build reply */ 1877 entrycnt = 0; 1878 while (cpos < cend && ncookies > 0 && dirlen < cnt) { 1879 nlen = dp->d_namlen; 1880 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1881 nlen <= NFS_MAXNAMLEN && 1882 ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 1883 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 1884 || (nlen == 1 && dp->d_name[0] != '.'))) { 1885 /* 1886 * Save the current position in the reply, in case 1887 * this entry exceeds cnt. 1888 */ 1889 mb1 = nd->nd_mb; 1890 bpos1 = nd->nd_bpos; 1891 1892 /* 1893 * For readdir_and_lookup get the vnode using 1894 * the file number. 1895 */ 1896 nvp = NULL; 1897 refp = NULL; 1898 r = 0; 1899 if ((nd->nd_flag & ND_NFSV3) || 1900 NFSNONZERO_ATTRBIT(&savbits)) { 1901 if (nd->nd_flag & ND_NFSV4) 1902 refp = nfsv4root_getreferral(NULL, 1903 vp, dp->d_fileno); 1904 if (refp == NULL) { 1905 if (usevget) 1906 r = VFS_VGET(vp->v_mount, 1907 dp->d_fileno, LK_EXCLUSIVE, 1908 &nvp); 1909 else 1910 r = EOPNOTSUPP; 1911 if (r == EOPNOTSUPP) { 1912 if (usevget) { 1913 usevget = 0; 1914 cn.cn_nameiop = LOOKUP; 1915 cn.cn_lkflags = 1916 LK_EXCLUSIVE | 1917 LK_RETRY; 1918 cn.cn_cred = 1919 nd->nd_cred; 1920 cn.cn_thread = p; 1921 } 1922 cn.cn_nameptr = dp->d_name; 1923 cn.cn_namelen = nlen; 1924 cn.cn_flags = ISLASTCN | 1925 NOFOLLOW | LOCKLEAF | 1926 MPSAFE; 1927 if (nlen == 2 && 1928 dp->d_name[0] == '.' && 1929 dp->d_name[1] == '.') 1930 cn.cn_flags |= 1931 ISDOTDOT; 1932 if (!VOP_ISLOCKED(vp)) 1933 vn_lock(vp, 1934 LK_EXCLUSIVE | 1935 LK_RETRY); 1936 r = VOP_LOOKUP(vp, &nvp, &cn); 1937 } 1938 } 1939 if (!r) { 1940 if (refp == NULL && 1941 ((nd->nd_flag & ND_NFSV3) || 1942 NFSNONZERO_ATTRBIT(&attrbits))) { 1943 r = nfsvno_getfh(nvp, &nfh, p); 1944 if (!r) 1945 r = nfsvno_getattr(nvp, nvap, 1946 nd->nd_cred, p); 1947 } 1948 } else { 1949 nvp = NULL; 1950 } 1951 if (r) { 1952 if (!NFSISSET_ATTRBIT(&attrbits, 1953 NFSATTRBIT_RDATTRERROR)) { 1954 if (nvp != NULL) 1955 vput(nvp); 1956 nd->nd_repstat = r; 1957 break; 1958 } 1959 } 1960 } 1961 1962 /* 1963 * Build the directory record xdr 1964 */ 1965 if (nd->nd_flag & ND_NFSV3) { 1966 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1967 *tl++ = newnfs_true; 1968 *tl++ = 0; 1969 *tl = txdr_unsigned(dp->d_fileno); 1970 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 1971 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1972 *tl++ = 0; 1973 *tl = txdr_unsigned(*cookiep); 1974 nfsrv_postopattr(nd, 0, nvap); 1975 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1); 1976 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 1977 if (nvp != NULL) 1978 vput(nvp); 1979 } else { 1980 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1981 *tl++ = newnfs_true; 1982 *tl++ = 0; 1983 *tl = txdr_unsigned(*cookiep); 1984 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 1985 if (nvp != NULL) 1986 NFSVOPUNLOCK(nvp, 0, p); 1987 if (refp != NULL) { 1988 dirlen += nfsrv_putreferralattr(nd, 1989 &savbits, refp, 0, 1990 &nd->nd_repstat); 1991 if (nd->nd_repstat) { 1992 if (nvp != NULL) 1993 vrele(nvp); 1994 break; 1995 } 1996 } else if (r) { 1997 dirlen += nfsvno_fillattr(nd, nvp, nvap, 1998 &nfh, r, &rderrbits, nd->nd_cred, 1999 p, isdgram, 0); 2000 } else { 2001 dirlen += nfsvno_fillattr(nd, nvp, nvap, 2002 &nfh, r, &attrbits, nd->nd_cred, 2003 p, isdgram, 0); 2004 } 2005 if (nvp != NULL) 2006 vrele(nvp); 2007 dirlen += (3 * NFSX_UNSIGNED); 2008 } 2009 if (dirlen <= cnt) 2010 entrycnt++; 2011 } 2012 cpos += dp->d_reclen; 2013 dp = (struct dirent *)cpos; 2014 cookiep++; 2015 ncookies--; 2016 } 2017 if (!usevget && VOP_ISLOCKED(vp)) 2018 vput(vp); 2019 else 2020 vrele(vp); 2021 2022 /* 2023 * If dirlen > cnt, we must strip off the last entry. If that 2024 * results in an empty reply, report NFSERR_TOOSMALL. 2025 */ 2026 if (dirlen > cnt || nd->nd_repstat) { 2027 if (!nd->nd_repstat && entrycnt == 0) 2028 nd->nd_repstat = NFSERR_TOOSMALL; 2029 if (nd->nd_repstat) 2030 newnfs_trimtrailing(nd, mb0, bpos0); 2031 else 2032 newnfs_trimtrailing(nd, mb1, bpos1); 2033 eofflag = 0; 2034 } else if (cpos < cend) 2035 eofflag = 0; 2036 if (!nd->nd_repstat) { 2037 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2038 *tl++ = newnfs_false; 2039 if (eofflag) 2040 *tl = newnfs_true; 2041 else 2042 *tl = newnfs_false; 2043 } 2044 FREE((caddr_t)cookies, M_TEMP); 2045 FREE((caddr_t)rbuf, M_TEMP); 2046 return (0); 2047nfsmout: 2048 vput(vp); 2049 return (error); 2050} 2051 2052/* 2053 * Get the settable attributes out of the mbuf list. 2054 * (Return 0 or EBADRPC) 2055 */ 2056int 2057nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2058 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2059{ 2060 u_int32_t *tl; 2061 struct nfsv2_sattr *sp; 2062 struct timeval curtime; 2063 int error = 0, toclient = 0; 2064 2065 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 2066 case ND_NFSV2: 2067 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2068 /* 2069 * Some old clients didn't fill in the high order 16bits. 2070 * --> check the low order 2 bytes for 0xffff 2071 */ 2072 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 2073 nvap->na_mode = nfstov_mode(sp->sa_mode); 2074 if (sp->sa_uid != newnfs_xdrneg1) 2075 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 2076 if (sp->sa_gid != newnfs_xdrneg1) 2077 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 2078 if (sp->sa_size != newnfs_xdrneg1) 2079 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 2080 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 2081#ifdef notyet 2082 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 2083#else 2084 nvap->na_atime.tv_sec = 2085 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 2086 nvap->na_atime.tv_nsec = 0; 2087#endif 2088 } 2089 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 2090 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 2091 break; 2092 case ND_NFSV3: 2093 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2094 if (*tl == newnfs_true) { 2095 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2096 nvap->na_mode = nfstov_mode(*tl); 2097 } 2098 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2099 if (*tl == newnfs_true) { 2100 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2101 nvap->na_uid = fxdr_unsigned(uid_t, *tl); 2102 } 2103 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2104 if (*tl == newnfs_true) { 2105 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2106 nvap->na_gid = fxdr_unsigned(gid_t, *tl); 2107 } 2108 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2109 if (*tl == newnfs_true) { 2110 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2111 nvap->na_size = fxdr_hyper(tl); 2112 } 2113 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2114 switch (fxdr_unsigned(int, *tl)) { 2115 case NFSV3SATTRTIME_TOCLIENT: 2116 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2117 fxdr_nfsv3time(tl, &nvap->na_atime); 2118 toclient = 1; 2119 break; 2120 case NFSV3SATTRTIME_TOSERVER: 2121 NFSGETTIME(&curtime); 2122 nvap->na_atime.tv_sec = curtime.tv_sec; 2123 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000; 2124 nvap->na_vaflags |= VA_UTIMES_NULL; 2125 break; 2126 }; 2127 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2128 switch (fxdr_unsigned(int, *tl)) { 2129 case NFSV3SATTRTIME_TOCLIENT: 2130 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2131 fxdr_nfsv3time(tl, &nvap->na_mtime); 2132 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2133 break; 2134 case NFSV3SATTRTIME_TOSERVER: 2135 NFSGETTIME(&curtime); 2136 nvap->na_mtime.tv_sec = curtime.tv_sec; 2137 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000; 2138 if (!toclient) 2139 nvap->na_vaflags |= VA_UTIMES_NULL; 2140 break; 2141 }; 2142 break; 2143 case ND_NFSV4: 2144 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p); 2145 }; 2146nfsmout: 2147 return (error); 2148} 2149 2150/* 2151 * Handle the setable attributes for V4. 2152 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 2153 */ 2154int 2155nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2156 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2157{ 2158 u_int32_t *tl; 2159 int attrsum = 0; 2160 int i, j; 2161 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 2162 int toclient = 0; 2163 u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 2164 uid_t uid; 2165 gid_t gid; 2166 struct timeval curtime; 2167 2168 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 2169 if (error) 2170 return (error); 2171 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2172 attrsize = fxdr_unsigned(int, *tl); 2173 2174 /* 2175 * Loop around getting the setable attributes. If an unsupported 2176 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 2177 */ 2178 if (retnotsup) { 2179 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2180 bitpos = NFSATTRBIT_MAX; 2181 } else { 2182 bitpos = 0; 2183 } 2184 for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 2185 if (attrsum > attrsize) { 2186 error = NFSERR_BADXDR; 2187 goto nfsmout; 2188 } 2189 if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 2190 switch (bitpos) { 2191 case NFSATTRBIT_SIZE: 2192 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2193 nvap->na_size = fxdr_hyper(tl); 2194 attrsum += NFSX_HYPER; 2195 break; 2196 case NFSATTRBIT_ACL: 2197 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize, 2198 p); 2199 if (error) 2200 goto nfsmout; 2201 if (aceerr && !nd->nd_repstat) 2202 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2203 attrsum += aclsize; 2204 break; 2205 case NFSATTRBIT_ARCHIVE: 2206 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2207 if (!nd->nd_repstat) 2208 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2209 attrsum += NFSX_UNSIGNED; 2210 break; 2211 case NFSATTRBIT_HIDDEN: 2212 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2213 if (!nd->nd_repstat) 2214 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2215 attrsum += NFSX_UNSIGNED; 2216 break; 2217 case NFSATTRBIT_MIMETYPE: 2218 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2219 i = fxdr_unsigned(int, *tl); 2220 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 2221 if (error) 2222 goto nfsmout; 2223 if (!nd->nd_repstat) 2224 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2225 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 2226 break; 2227 case NFSATTRBIT_MODE: 2228 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2229 nvap->na_mode = nfstov_mode(*tl); 2230 attrsum += NFSX_UNSIGNED; 2231 break; 2232 case NFSATTRBIT_OWNER: 2233 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2234 j = fxdr_unsigned(int, *tl); 2235 if (j < 0) 2236 return (NFSERR_BADXDR); 2237 if (j > NFSV4_SMALLSTR) 2238 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2239 else 2240 cp = namestr; 2241 error = nfsrv_mtostr(nd, cp, j); 2242 if (error) { 2243 if (j > NFSV4_SMALLSTR) 2244 free(cp, M_NFSSTRING); 2245 return (error); 2246 } 2247 if (!nd->nd_repstat) { 2248 nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p); 2249 if (!nd->nd_repstat) 2250 nvap->na_uid = uid; 2251 } 2252 if (j > NFSV4_SMALLSTR) 2253 free(cp, M_NFSSTRING); 2254 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2255 break; 2256 case NFSATTRBIT_OWNERGROUP: 2257 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2258 j = fxdr_unsigned(int, *tl); 2259 if (j < 0) 2260 return (NFSERR_BADXDR); 2261 if (j > NFSV4_SMALLSTR) 2262 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2263 else 2264 cp = namestr; 2265 error = nfsrv_mtostr(nd, cp, j); 2266 if (error) { 2267 if (j > NFSV4_SMALLSTR) 2268 free(cp, M_NFSSTRING); 2269 return (error); 2270 } 2271 if (!nd->nd_repstat) { 2272 nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p); 2273 if (!nd->nd_repstat) 2274 nvap->na_gid = gid; 2275 } 2276 if (j > NFSV4_SMALLSTR) 2277 free(cp, M_NFSSTRING); 2278 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2279 break; 2280 case NFSATTRBIT_SYSTEM: 2281 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2282 if (!nd->nd_repstat) 2283 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2284 attrsum += NFSX_UNSIGNED; 2285 break; 2286 case NFSATTRBIT_TIMEACCESSSET: 2287 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2288 attrsum += NFSX_UNSIGNED; 2289 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2290 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2291 fxdr_nfsv4time(tl, &nvap->na_atime); 2292 toclient = 1; 2293 attrsum += NFSX_V4TIME; 2294 } else { 2295 NFSGETTIME(&curtime); 2296 nvap->na_atime.tv_sec = curtime.tv_sec; 2297 nvap->na_atime.tv_nsec = curtime.tv_usec * 1000; 2298 nvap->na_vaflags |= VA_UTIMES_NULL; 2299 } 2300 break; 2301 case NFSATTRBIT_TIMEBACKUP: 2302 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2303 if (!nd->nd_repstat) 2304 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2305 attrsum += NFSX_V4TIME; 2306 break; 2307 case NFSATTRBIT_TIMECREATE: 2308 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2309 if (!nd->nd_repstat) 2310 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2311 attrsum += NFSX_V4TIME; 2312 break; 2313 case NFSATTRBIT_TIMEMODIFYSET: 2314 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2315 attrsum += NFSX_UNSIGNED; 2316 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2317 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2318 fxdr_nfsv4time(tl, &nvap->na_mtime); 2319 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2320 attrsum += NFSX_V4TIME; 2321 } else { 2322 NFSGETTIME(&curtime); 2323 nvap->na_mtime.tv_sec = curtime.tv_sec; 2324 nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000; 2325 if (!toclient) 2326 nvap->na_vaflags |= VA_UTIMES_NULL; 2327 } 2328 break; 2329 default: 2330 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2331 /* 2332 * set bitpos so we drop out of the loop. 2333 */ 2334 bitpos = NFSATTRBIT_MAX; 2335 break; 2336 }; 2337 } 2338 2339 /* 2340 * some clients pad the attrlist, so we need to skip over the 2341 * padding. 2342 */ 2343 if (attrsum > attrsize) { 2344 error = NFSERR_BADXDR; 2345 } else { 2346 attrsize = NFSM_RNDUP(attrsize); 2347 if (attrsum < attrsize) 2348 error = nfsm_advance(nd, attrsize - attrsum, -1); 2349 } 2350nfsmout: 2351 return (error); 2352} 2353 2354/* 2355 * Check/setup export credentials. 2356 */ 2357int 2358nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 2359 struct ucred *credanon) 2360{ 2361 int error = 0; 2362 2363 /* 2364 * Check/setup credentials. 2365 */ 2366 if (nd->nd_flag & ND_GSS) 2367 exp->nes_exflag &= ~MNT_EXPORTANON; 2368 2369 /* 2370 * Check to see if the operation is allowed for this security flavor. 2371 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to 2372 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 2373 * Also, allow Secinfo, so that it can acquire the correct flavor(s). 2374 */ 2375 if (nfsvno_testexp(nd, exp) && 2376 nd->nd_procnum != NFSV4OP_SECINFO && 2377 nd->nd_procnum != NFSPROC_FSINFO) { 2378 if (nd->nd_flag & ND_NFSV4) 2379 error = NFSERR_WRONGSEC; 2380 else 2381 error = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2382 return (error); 2383 } 2384 2385 /* 2386 * Check to see if the file system is exported V4 only. 2387 */ 2388 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) 2389 return (NFSERR_PROGNOTV4); 2390 2391 /* 2392 * Now, map the user credentials. 2393 * (Note that ND_AUTHNONE will only be set for an NFSv3 2394 * Fsinfo RPC. If set for anything else, this code might need 2395 * to change.) 2396 */ 2397 if (NFSVNO_EXPORTED(exp) && 2398 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) || 2399 NFSVNO_EXPORTANON(exp) || 2400 (nd->nd_flag & ND_AUTHNONE))) { 2401 nd->nd_cred->cr_uid = credanon->cr_uid; 2402 nd->nd_cred->cr_gid = credanon->cr_gid; 2403 crsetgroups(nd->nd_cred, credanon->cr_ngroups, 2404 credanon->cr_groups); 2405 } 2406 return (0); 2407} 2408 2409/* 2410 * Check exports. 2411 */ 2412int 2413nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 2414 struct ucred **credp) 2415{ 2416 int i, error, *secflavors; 2417 2418 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2419 &exp->nes_numsecflavor, &secflavors); 2420 if (error) { 2421 if (nfs_rootfhset) { 2422 exp->nes_exflag = 0; 2423 exp->nes_numsecflavor = 0; 2424 error = 0; 2425 } 2426 } else { 2427 /* Copy the security flavors. */ 2428 for (i = 0; i < exp->nes_numsecflavor; i++) 2429 exp->nes_secflavors[i] = secflavors[i]; 2430 } 2431 return (error); 2432} 2433 2434/* 2435 * Get a vnode for a file handle and export stuff. 2436 */ 2437int 2438nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 2439 struct vnode **vpp, struct nfsexstuff *exp, struct ucred **credp) 2440{ 2441 int i, error, *secflavors; 2442 2443 *credp = NULL; 2444 exp->nes_numsecflavor = 0; 2445 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp); 2446 if (error != 0) 2447 /* Make sure the server replies ESTALE to the client. */ 2448 error = ESTALE; 2449 if (nam && !error) { 2450 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2451 &exp->nes_numsecflavor, &secflavors); 2452 if (error) { 2453 if (nfs_rootfhset) { 2454 exp->nes_exflag = 0; 2455 exp->nes_numsecflavor = 0; 2456 error = 0; 2457 } else { 2458 vput(*vpp); 2459 } 2460 } else { 2461 /* Copy the security flavors. */ 2462 for (i = 0; i < exp->nes_numsecflavor; i++) 2463 exp->nes_secflavors[i] = secflavors[i]; 2464 } 2465 } 2466 return (error); 2467} 2468 2469/* 2470 * Do the pathconf vnode op. 2471 */ 2472int 2473nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf, 2474 struct ucred *cred, struct thread *p) 2475{ 2476 int error; 2477 2478 error = VOP_PATHCONF(vp, flag, retf); 2479 return (error); 2480} 2481 2482/* 2483 * nfsd_fhtovp() - convert a fh to a vnode ptr 2484 * - look up fsid in mount list (if not found ret error) 2485 * - get vp and export rights by calling nfsvno_fhtovp() 2486 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 2487 * for AUTH_SYS 2488 * Also handle getting the Giant lock for the file system, 2489 * as required: 2490 * - if same mount point as *mpp 2491 * do nothing 2492 * else if *mpp == NULL 2493 * if already locked 2494 * leave it locked 2495 * else 2496 * call VFS_LOCK_GIANT() 2497 * else 2498 * if already locked 2499 * unlock Giant 2500 * call VFS_LOCK_GIANT() 2501 */ 2502void 2503nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, 2504 struct vnode **vpp, struct nfsexstuff *exp, 2505 struct mount **mpp, int startwrite, struct thread *p) 2506{ 2507 struct mount *mp; 2508 struct ucred *credanon; 2509 fhandle_t *fhp; 2510 2511 fhp = (fhandle_t *)nfp->nfsrvfh_data; 2512 /* 2513 * Check for the special case of the nfsv4root_fh. 2514 */ 2515 mp = vfs_getvfs(&fhp->fh_fsid); 2516 if (!mp) { 2517 *vpp = NULL; 2518 nd->nd_repstat = ESTALE; 2519 if (*mpp && exp->nes_vfslocked) 2520 VFS_UNLOCK_GIANT(*mpp); 2521 *mpp = NULL; 2522 exp->nes_vfslocked = 0; 2523 return; 2524 } 2525 2526 /* 2527 * Now, handle Giant for the file system. 2528 */ 2529 if (*mpp != NULL && *mpp != mp && exp->nes_vfslocked) { 2530 VFS_UNLOCK_GIANT(*mpp); 2531 exp->nes_vfslocked = 0; 2532 } 2533 if (!exp->nes_vfslocked && *mpp != mp) 2534 exp->nes_vfslocked = VFS_LOCK_GIANT(mp); 2535 2536 *mpp = mp; 2537 if (startwrite) 2538 vn_start_write(NULL, mpp, V_WAIT); 2539 2540 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, vpp, exp, 2541 &credanon); 2542 2543 /* 2544 * For NFSv4 without a pseudo root fs, unexported file handles 2545 * can be returned, so that Lookup works everywhere. 2546 */ 2547 if (!nd->nd_repstat && exp->nes_exflag == 0 && 2548 !(nd->nd_flag & ND_NFSV4)) { 2549 vput(*vpp); 2550 nd->nd_repstat = EACCES; 2551 } 2552 2553 /* 2554 * Personally, I've never seen any point in requiring a 2555 * reserved port#, since only in the rare case where the 2556 * clients are all boxes with secure system priviledges, 2557 * does it provide any enhanced security, but... some people 2558 * believe it to be useful and keep putting this code back in. 2559 * (There is also some "security checker" out there that 2560 * complains if the nfs server doesn't enforce this.) 2561 * However, note the following: 2562 * RFC3530 (NFSv4) specifies that a reserved port# not be 2563 * required. 2564 * RFC2623 recommends that, if a reserved port# is checked for, 2565 * that there be a way to turn that off--> ifdef'd. 2566 */ 2567#ifdef NFS_REQRSVPORT 2568 if (!nd->nd_repstat) { 2569 struct sockaddr_in *saddr; 2570 struct sockaddr_in6 *saddr6; 2571 2572 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 2573 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 2574 if (!(nd->nd_flag & ND_NFSV4) && 2575 ((saddr->sin_family == AF_INET && 2576 ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 2577 (saddr6->sin6_family == AF_INET6 && 2578 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 2579 vput(*vpp); 2580 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2581 } 2582 } 2583#endif /* NFS_REQRSVPORT */ 2584 2585 /* 2586 * Check/setup credentials. 2587 */ 2588 if (!nd->nd_repstat) { 2589 nd->nd_saveduid = nd->nd_cred->cr_uid; 2590 nd->nd_repstat = nfsd_excred(nd, exp, credanon); 2591 if (nd->nd_repstat) 2592 vput(*vpp); 2593 } 2594 if (credanon != NULL) 2595 crfree(credanon); 2596 if (nd->nd_repstat) { 2597 if (startwrite) 2598 vn_finished_write(mp); 2599 if (exp->nes_vfslocked) { 2600 VFS_UNLOCK_GIANT(mp); 2601 exp->nes_vfslocked = 0; 2602 } 2603 vfs_rel(mp); 2604 *vpp = NULL; 2605 *mpp = NULL; 2606 } else { 2607 vfs_rel(mp); 2608 } 2609} 2610 2611/* 2612 * glue for fp. 2613 */ 2614int 2615fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 2616{ 2617 struct filedesc *fdp; 2618 struct file *fp; 2619 2620 fdp = p->td_proc->p_fd; 2621 if (fd >= fdp->fd_nfiles || 2622 (fp = fdp->fd_ofiles[fd]) == NULL) 2623 return (EBADF); 2624 *fpp = fp; 2625 return (0); 2626} 2627 2628/* 2629 * Called from nfssvc() to update the exports list. Just call 2630 * vfs_export(). This has to be done, since the v4 root fake fs isn't 2631 * in the mount list. 2632 */ 2633int 2634nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 2635{ 2636 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 2637 int error; 2638 struct nameidata nd; 2639 fhandle_t fh; 2640 2641 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export); 2642 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT)) { 2643 nfs_rootfhset = 0; 2644 nfsv4root_set = 0; 2645 } else if (error == 0) { 2646 if (nfsexargp->fspec == NULL) 2647 return (EPERM); 2648 /* 2649 * If fspec != NULL, this is the v4root path. 2650 */ 2651 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, 2652 nfsexargp->fspec, p); 2653 if ((error = namei(&nd)) != 0) 2654 return (error); 2655 error = nfsvno_getfh(nd.ni_vp, &fh, p); 2656 vrele(nd.ni_vp); 2657 if (!error) { 2658 nfs_rootfh.nfsrvfh_len = NFSX_MYFH; 2659 NFSBCOPY((caddr_t)&fh, 2660 nfs_rootfh.nfsrvfh_data, 2661 sizeof (fhandle_t)); 2662 nfs_rootfhset = 1; 2663 } 2664 } 2665 return (error); 2666} 2667 2668/* 2669 * Get the tcp socket sequence numbers we need. 2670 * (Maybe this should be moved to the tcp sources?) 2671 */ 2672int 2673nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap) 2674{ 2675 struct inpcb *inp; 2676 struct tcpcb *tp; 2677 2678 inp = sotoinpcb(so); 2679 KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL")); 2680 INP_RLOCK(inp); 2681 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 2682 INP_RUNLOCK(inp); 2683 return (EPIPE); 2684 } 2685 tp = intotcpcb(inp); 2686 if (tp->t_state != TCPS_ESTABLISHED) { 2687 INP_RUNLOCK(inp); 2688 return (EPIPE); 2689 } 2690 *maxp = tp->snd_max; 2691 *unap = tp->snd_una; 2692 INP_RUNLOCK(inp); 2693 return (0); 2694} 2695 2696/* 2697 * This function needs to test to see if the system is near its limit 2698 * for memory allocation via malloc() or mget() and return True iff 2699 * either of these resources are near their limit. 2700 * XXX (For now, this is just a stub.) 2701 */ 2702int nfsrv_testmalloclimit = 0; 2703int 2704nfsrv_mallocmget_limit(void) 2705{ 2706 static int printmesg = 0; 2707 static int testval = 1; 2708 2709 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 2710 if ((printmesg++ % 100) == 0) 2711 printf("nfsd: malloc/mget near limit\n"); 2712 return (1); 2713 } 2714 return (0); 2715} 2716 2717/* 2718 * BSD specific initialization of a mount point. 2719 */ 2720void 2721nfsd_mntinit(void) 2722{ 2723 static int inited = 0; 2724 2725 if (inited) 2726 return; 2727 inited = 1; 2728 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 2729 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist); 2730 nfsv4root_mnt.mnt_export = NULL; 2731 TAILQ_INIT(&nfsv4root_opt); 2732 TAILQ_INIT(&nfsv4root_newopt); 2733 nfsv4root_mnt.mnt_opt = &nfsv4root_opt; 2734 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt; 2735 nfsv4root_mnt.mnt_nvnodelistsize = 0; 2736} 2737 2738/* 2739 * Get a vnode for a file handle, without checking exports, etc. 2740 */ 2741struct vnode * 2742nfsvno_getvp(fhandle_t *fhp) 2743{ 2744 struct mount *mp; 2745 struct vnode *vp; 2746 int error; 2747 2748 mp = vfs_getvfs(&fhp->fh_fsid); 2749 if (mp == NULL) 2750 return (NULL); 2751 error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp); 2752 if (error) 2753 return (NULL); 2754 return (vp); 2755} 2756 2757/* 2758 * Check to see it a byte range lock held by a process running 2759 * locally on the server conflicts with the new lock. 2760 */ 2761int 2762nfsvno_localconflict(struct vnode *vp, int ftype, u_int64_t first, 2763 u_int64_t end, struct nfslockconflict *cfp, struct thread *td) 2764{ 2765 int error; 2766 struct flock fl; 2767 2768 if (!nfsrv_dolocallocks) 2769 return (0); 2770 fl.l_whence = SEEK_SET; 2771 fl.l_type = ftype; 2772 fl.l_start = (off_t)first; 2773 if (end == NFS64BITSSET) 2774 fl.l_len = 0; 2775 else 2776 fl.l_len = (off_t)(end - first); 2777 /* 2778 * For FreeBSD8, the l_pid and l_sysid must be set to the same 2779 * values for all calls, so that all locks will be held by the 2780 * nfsd server. (The nfsd server handles conflicts between the 2781 * various clients.) 2782 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 2783 * bytes, so it can't be put in l_sysid. 2784 */ 2785 if (nfsv4_sysid == 0) 2786 nfsv4_sysid = nlm_acquire_next_sysid(); 2787 fl.l_pid = (pid_t)0; 2788 fl.l_sysid = (int)nfsv4_sysid; 2789 2790 NFSVOPUNLOCK(vp, 0, td); 2791 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_GETLK, &fl, 2792 (F_POSIX | F_REMOTE)); 2793 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td); 2794 if (error) 2795 return (error); 2796 if (fl.l_type == F_UNLCK) 2797 return (0); 2798 if (cfp != NULL) { 2799 cfp->cl_clientid.lval[0] = cfp->cl_clientid.lval[1] = 0; 2800 cfp->cl_first = (u_int64_t)fl.l_start; 2801 if (fl.l_len == 0) 2802 cfp->cl_end = NFS64BITSSET; 2803 else 2804 cfp->cl_end = (u_int64_t) 2805 (fl.l_start + fl.l_len); 2806 if (fl.l_type == F_WRLCK) 2807 cfp->cl_flags = NFSLCK_WRITE; 2808 else 2809 cfp->cl_flags = NFSLCK_READ; 2810 sprintf(cfp->cl_owner, "LOCALID%d", fl.l_pid); 2811 cfp->cl_ownerlen = strlen(cfp->cl_owner); 2812 return (NFSERR_DENIED); 2813 } 2814 return (NFSERR_INVAL); 2815} 2816 2817/* 2818 * Do a local VOP_ADVLOCK(). 2819 */ 2820int 2821nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 2822 u_int64_t end, struct thread *td) 2823{ 2824 int error; 2825 struct flock fl; 2826 u_int64_t tlen; 2827 2828 if (!nfsrv_dolocallocks) 2829 return (0); 2830 fl.l_whence = SEEK_SET; 2831 fl.l_type = ftype; 2832 fl.l_start = (off_t)first; 2833 if (end == NFS64BITSSET) { 2834 fl.l_len = 0; 2835 } else { 2836 tlen = end - first; 2837 fl.l_len = (off_t)tlen; 2838 } 2839 /* 2840 * For FreeBSD8, the l_pid and l_sysid must be set to the same 2841 * values for all calls, so that all locks will be held by the 2842 * nfsd server. (The nfsd server handles conflicts between the 2843 * various clients.) 2844 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 2845 * bytes, so it can't be put in l_sysid. 2846 */ 2847 if (nfsv4_sysid == 0) 2848 nfsv4_sysid = nlm_acquire_next_sysid(); 2849 fl.l_pid = (pid_t)0; 2850 fl.l_sysid = (int)nfsv4_sysid; 2851 2852 NFSVOPUNLOCK(vp, 0, td); 2853 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 2854 (F_POSIX | F_REMOTE)); 2855 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td); 2856 return (error); 2857} 2858 2859/* 2860 * Unlock an underlying local file system. 2861 */ 2862void 2863nfsvno_unlockvfs(struct mount *mp) 2864{ 2865 2866 VFS_UNLOCK_GIANT(mp); 2867} 2868 2869/* 2870 * Lock an underlying file system, as required, and return 2871 * whether or not it is locked. 2872 */ 2873int 2874nfsvno_lockvfs(struct mount *mp) 2875{ 2876 int ret; 2877 2878 ret = VFS_LOCK_GIANT(mp); 2879 return (ret); 2880} 2881 2882/* 2883 * Check the nfsv4 root exports. 2884 */ 2885int 2886nfsvno_v4rootexport(struct nfsrv_descript *nd) 2887{ 2888 struct ucred *credanon; 2889 int exflags, error, numsecflavor, *secflavors, i; 2890 2891 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags, 2892 &credanon, &numsecflavor, &secflavors); 2893 if (error) 2894 return (NFSERR_PROGUNAVAIL); 2895 if (credanon != NULL) 2896 crfree(credanon); 2897 for (i = 0; i < numsecflavor; i++) { 2898 if (secflavors[i] == AUTH_SYS) 2899 nd->nd_flag |= ND_EXAUTHSYS; 2900 else if (secflavors[i] == RPCSEC_GSS_KRB5) 2901 nd->nd_flag |= ND_EXGSS; 2902 else if (secflavors[i] == RPCSEC_GSS_KRB5I) 2903 nd->nd_flag |= ND_EXGSSINTEGRITY; 2904 else if (secflavors[i] == RPCSEC_GSS_KRB5P) 2905 nd->nd_flag |= ND_EXGSSPRIVACY; 2906 } 2907 return (0); 2908} 2909 2910/* 2911 * Nfs server psuedo system call for the nfsd's 2912 */ 2913/* 2914 * MPSAFE 2915 */ 2916static int 2917nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 2918{ 2919 struct file *fp; 2920 struct nfsd_addsock_args sockarg; 2921 struct nfsd_nfsd_args nfsdarg; 2922 int error; 2923 2924 if (uap->flag & NFSSVC_NFSDADDSOCK) { 2925 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 2926 if (error) 2927 return (error); 2928 if ((error = fget(td, sockarg.sock, &fp)) != 0) { 2929 return (error); 2930 } 2931 if (fp->f_type != DTYPE_SOCKET) { 2932 fdrop(fp, td); 2933 return (EPERM); 2934 } 2935 error = nfsrvd_addsock(fp); 2936 fdrop(fp, td); 2937 } else if (uap->flag & NFSSVC_NFSDNFSD) { 2938 if (uap->argp == NULL) 2939 return (EINVAL); 2940 error = copyin(uap->argp, (caddr_t)&nfsdarg, 2941 sizeof (nfsdarg)); 2942 if (error) 2943 return (error); 2944 error = nfsrvd_nfsd(td, &nfsdarg); 2945 } else { 2946 error = nfssvc_srvcall(td, uap, td->td_ucred); 2947 } 2948 return (error); 2949} 2950 2951static int 2952nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 2953{ 2954 struct nfsex_args export; 2955 struct file *fp = NULL; 2956 int stablefd, len; 2957 struct nfsd_clid adminrevoke; 2958 struct nfsd_dumplist dumplist; 2959 struct nfsd_dumpclients *dumpclients; 2960 struct nfsd_dumplocklist dumplocklist; 2961 struct nfsd_dumplocks *dumplocks; 2962 struct nameidata nd; 2963 vnode_t vp; 2964 int error = EINVAL; 2965 2966 if (uap->flag & NFSSVC_PUBLICFH) { 2967 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 2968 sizeof (fhandle_t)); 2969 error = copyin(uap->argp, 2970 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 2971 if (!error) 2972 nfs_pubfhset = 1; 2973 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) { 2974 error = copyin(uap->argp,(caddr_t)&export, 2975 sizeof (struct nfsex_args)); 2976 if (!error) 2977 error = nfsrv_v4rootexport(&export, cred, p); 2978 } else if (uap->flag & NFSSVC_NOPUBLICFH) { 2979 nfs_pubfhset = 0; 2980 error = 0; 2981 } else if (uap->flag & NFSSVC_STABLERESTART) { 2982 error = copyin(uap->argp, (caddr_t)&stablefd, 2983 sizeof (int)); 2984 if (!error) 2985 error = fp_getfvp(p, stablefd, &fp, &vp); 2986 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 2987 error = EBADF; 2988 if (!error && newnfs_numnfsd != 0) 2989 error = EPERM; 2990 if (!error) { 2991 nfsrv_stablefirst.nsf_fp = fp; 2992 nfsrv_setupstable(p); 2993 } 2994 } else if (uap->flag & NFSSVC_ADMINREVOKE) { 2995 error = copyin(uap->argp, (caddr_t)&adminrevoke, 2996 sizeof (struct nfsd_clid)); 2997 if (!error) 2998 error = nfsrv_adminrevoke(&adminrevoke, p); 2999 } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 3000 error = copyin(uap->argp, (caddr_t)&dumplist, 3001 sizeof (struct nfsd_dumplist)); 3002 if (!error && (dumplist.ndl_size < 1 || 3003 dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 3004 error = EPERM; 3005 if (!error) { 3006 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 3007 dumpclients = (struct nfsd_dumpclients *)malloc(len, 3008 M_TEMP, M_WAITOK); 3009 nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 3010 error = copyout(dumpclients, 3011 CAST_USER_ADDR_T(dumplist.ndl_list), len); 3012 free((caddr_t)dumpclients, M_TEMP); 3013 } 3014 } else if (uap->flag & NFSSVC_DUMPLOCKS) { 3015 error = copyin(uap->argp, (caddr_t)&dumplocklist, 3016 sizeof (struct nfsd_dumplocklist)); 3017 if (!error && (dumplocklist.ndllck_size < 1 || 3018 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 3019 error = EPERM; 3020 if (!error) 3021 error = nfsrv_lookupfilename(&nd, 3022 dumplocklist.ndllck_fname, p); 3023 if (!error) { 3024 len = sizeof (struct nfsd_dumplocks) * 3025 dumplocklist.ndllck_size; 3026 dumplocks = (struct nfsd_dumplocks *)malloc(len, 3027 M_TEMP, M_WAITOK); 3028 nfsrv_dumplocks(nd.ni_vp, dumplocks, 3029 dumplocklist.ndllck_size, p); 3030 vput(nd.ni_vp); 3031 error = copyout(dumplocks, 3032 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len); 3033 free((caddr_t)dumplocks, M_TEMP); 3034 } 3035 } 3036 return (error); 3037} 3038 3039/* 3040 * Check exports. 3041 * Returns 0 if ok, 1 otherwise. 3042 */ 3043int 3044nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 3045{ 3046 int i; 3047 3048 /* 3049 * This seems odd, but allow the case where the security flavor 3050 * list is empty. This happens when NFSv4 is traversing non-exported 3051 * file systems. Exported file systems should always have a non-empty 3052 * security flavor list. 3053 */ 3054 if (exp->nes_numsecflavor == 0) 3055 return (0); 3056 3057 for (i = 0; i < exp->nes_numsecflavor; i++) { 3058 /* 3059 * The tests for privacy and integrity must be first, 3060 * since ND_GSS is set for everything but AUTH_SYS. 3061 */ 3062 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 3063 (nd->nd_flag & ND_GSSPRIVACY)) 3064 return (0); 3065 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 3066 (nd->nd_flag & ND_GSSINTEGRITY)) 3067 return (0); 3068 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 3069 (nd->nd_flag & ND_GSS)) 3070 return (0); 3071 if (exp->nes_secflavors[i] == AUTH_SYS && 3072 (nd->nd_flag & ND_GSS) == 0) 3073 return (0); 3074 } 3075 return (1); 3076} 3077 3078extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 3079 3080/* 3081 * Called once to initialize data structures... 3082 */ 3083static int 3084nfsd_modevent(module_t mod, int type, void *data) 3085{ 3086 int error = 0; 3087 static int loaded = 0; 3088 3089 switch (type) { 3090 case MOD_LOAD: 3091 if (loaded) 3092 return (0); 3093 newnfs_portinit(); 3094 mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF); 3095 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF); 3096 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL, 3097 MTX_DEF); 3098 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0); 3099 nfsrvd_initcache(); 3100 nfsd_init(); 3101 NFSD_LOCK(); 3102 nfsrvd_init(0); 3103 NFSD_UNLOCK(); 3104 nfsd_mntinit(); 3105#ifdef VV_DISABLEDELEG 3106 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 3107 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 3108#endif 3109 nfsd_call_servertimer = nfsrv_servertimer; 3110 nfsd_call_nfsd = nfssvc_nfsd; 3111 loaded = 1; 3112 break; 3113 3114 case MOD_UNLOAD: 3115 if (newnfs_numnfsd != 0) { 3116 error = EBUSY; 3117 break; 3118 } 3119 3120#ifdef VV_DISABLEDELEG 3121 vn_deleg_ops.vndeleg_recall = NULL; 3122 vn_deleg_ops.vndeleg_disable = NULL; 3123#endif 3124 nfsd_call_servertimer = NULL; 3125 nfsd_call_nfsd = NULL; 3126 /* and get rid of the locks */ 3127 mtx_destroy(&nfs_cache_mutex); 3128 mtx_destroy(&nfs_v4root_mutex); 3129 mtx_destroy(&nfsv4root_mnt.mnt_mtx); 3130 lockdestroy(&nfsv4root_mnt.mnt_explock); 3131 loaded = 0; 3132 break; 3133 default: 3134 error = EOPNOTSUPP; 3135 break; 3136 } 3137 return error; 3138} 3139static moduledata_t nfsd_mod = { 3140 "nfsd", 3141 nfsd_modevent, 3142 NULL, 3143}; 3144DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 3145 3146/* So that loader and kldload(2) can find us, wherever we are.. */ 3147MODULE_VERSION(nfsd, 1); 3148MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 3149MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 3150 3151