nfs_nfsdport.c revision 265667
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: stable/10/sys/fs/nfsserver/nfs_nfsdport.c 265667 2014-05-08 11:59:23Z rmacklem $"); 36 37#include <sys/capability.h> 38 39/* 40 * Functions that perform the vfs operations required by the routines in 41 * nfsd_serv.c. It is hoped that this change will make the server more 42 * portable. 43 */ 44 45#include <fs/nfs/nfsport.h> 46#include <sys/hash.h> 47#include <sys/sysctl.h> 48#include <nlm/nlm_prot.h> 49#include <nlm/nlm.h> 50 51FEATURE(nfsd, "NFSv4 server"); 52 53extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1; 54extern int nfsrv_useacl; 55extern int newnfs_numnfsd; 56extern struct mount nfsv4root_mnt; 57extern struct nfsrv_stablefirst nfsrv_stablefirst; 58extern void (*nfsd_call_servertimer)(void); 59extern SVCPOOL *nfsrvd_pool; 60extern struct nfsv4lock nfsd_suspend_lock; 61struct vfsoptlist nfsv4root_opt, nfsv4root_newopt; 62NFSDLOCKMUTEX; 63struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE]; 64struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE]; 65struct mtx nfsrc_udpmtx; 66struct mtx nfs_v4root_mutex; 67struct nfsrvfh nfs_rootfh, nfs_pubfh; 68int nfs_pubfhset = 0, nfs_rootfhset = 0; 69struct proc *nfsd_master_proc = NULL; 70static pid_t nfsd_master_pid = (pid_t)-1; 71static char nfsd_master_comm[MAXCOMLEN + 1]; 72static struct timeval nfsd_master_start; 73static uint32_t nfsv4_sysid = 0; 74 75static int nfssvc_srvcall(struct thread *, struct nfssvc_args *, 76 struct ucred *); 77 78int nfsrv_enable_crossmntpt = 1; 79static int nfs_commit_blks; 80static int nfs_commit_miss; 81extern int nfsrv_issuedelegs; 82extern int nfsrv_dolocallocks; 83 84SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server"); 85SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW, 86 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points"); 87SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 88 0, ""); 89SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 90 0, ""); 91SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW, 92 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 93SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW, 94 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 95 96#define MAX_REORDERED_RPC 16 97#define NUM_HEURISTIC 1031 98#define NHUSE_INIT 64 99#define NHUSE_INC 16 100#define NHUSE_MAX 2048 101 102static struct nfsheur { 103 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 104 off_t nh_nextoff; /* next offset for sequential detection */ 105 int nh_use; /* use count for selection */ 106 int nh_seqcount; /* heuristic */ 107} nfsheur[NUM_HEURISTIC]; 108 109 110/* 111 * Heuristic to detect sequential operation. 112 */ 113static struct nfsheur * 114nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp) 115{ 116 struct nfsheur *nh; 117 int hi, try; 118 119 /* Locate best candidate. */ 120 try = 32; 121 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 122 nh = &nfsheur[hi]; 123 while (try--) { 124 if (nfsheur[hi].nh_vp == vp) { 125 nh = &nfsheur[hi]; 126 break; 127 } 128 if (nfsheur[hi].nh_use > 0) 129 --nfsheur[hi].nh_use; 130 hi = (hi + 1) % NUM_HEURISTIC; 131 if (nfsheur[hi].nh_use < nh->nh_use) 132 nh = &nfsheur[hi]; 133 } 134 135 /* Initialize hint if this is a new file. */ 136 if (nh->nh_vp != vp) { 137 nh->nh_vp = vp; 138 nh->nh_nextoff = uio->uio_offset; 139 nh->nh_use = NHUSE_INIT; 140 if (uio->uio_offset == 0) 141 nh->nh_seqcount = 4; 142 else 143 nh->nh_seqcount = 1; 144 } 145 146 /* Calculate heuristic. */ 147 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) || 148 uio->uio_offset == nh->nh_nextoff) { 149 /* See comments in vfs_vnops.c:sequential_heuristic(). */ 150 nh->nh_seqcount += howmany(uio->uio_resid, 16384); 151 if (nh->nh_seqcount > IO_SEQMAX) 152 nh->nh_seqcount = IO_SEQMAX; 153 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC * 154 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) { 155 /* Probably a reordered RPC, leave seqcount alone. */ 156 } else if (nh->nh_seqcount > 1) { 157 nh->nh_seqcount /= 2; 158 } else { 159 nh->nh_seqcount = 0; 160 } 161 nh->nh_use += NHUSE_INC; 162 if (nh->nh_use > NHUSE_MAX) 163 nh->nh_use = NHUSE_MAX; 164 return (nh); 165} 166 167/* 168 * Get attributes into nfsvattr structure. 169 */ 170int 171nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 172 struct thread *p, int vpislocked) 173{ 174 int error, lockedit = 0; 175 176 if (vpislocked == 0) { 177 /* 178 * When vpislocked == 0, the vnode is either exclusively 179 * locked by this thread or not locked by this thread. 180 * As such, shared lock it, if not exclusively locked. 181 */ 182 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 183 lockedit = 1; 184 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY); 185 } 186 } 187 error = VOP_GETATTR(vp, &nvap->na_vattr, cred); 188 if (lockedit != 0) 189 NFSVOPUNLOCK(vp, 0); 190 191 NFSEXITCODE(error); 192 return (error); 193} 194 195/* 196 * Get a file handle for a vnode. 197 */ 198int 199nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 200{ 201 int error; 202 203 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 204 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 205 error = VOP_VPTOFH(vp, &fhp->fh_fid); 206 207 NFSEXITCODE(error); 208 return (error); 209} 210 211/* 212 * Perform access checking for vnodes obtained from file handles that would 213 * refer to files already opened by a Unix client. You cannot just use 214 * vn_writechk() and VOP_ACCESSX() for two reasons. 215 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 216 * case. 217 * 2 - The owner is to be given access irrespective of mode bits for some 218 * operations, so that processes that chmod after opening a file don't 219 * break. 220 */ 221int 222nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 223 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 224 u_int32_t *supportedtypep) 225{ 226 struct vattr vattr; 227 int error = 0, getret = 0; 228 229 if (vpislocked == 0) { 230 if (NFSVOPLOCK(vp, LK_SHARED) != 0) { 231 error = EPERM; 232 goto out; 233 } 234 } 235 if (accmode & VWRITE) { 236 /* Just vn_writechk() changed to check rdonly */ 237 /* 238 * Disallow write attempts on read-only file systems; 239 * unless the file is a socket or a block or character 240 * device resident on the file system. 241 */ 242 if (NFSVNO_EXRDONLY(exp) || 243 (vp->v_mount->mnt_flag & MNT_RDONLY)) { 244 switch (vp->v_type) { 245 case VREG: 246 case VDIR: 247 case VLNK: 248 error = EROFS; 249 default: 250 break; 251 } 252 } 253 /* 254 * If there's shared text associated with 255 * the inode, try to free it up once. If 256 * we fail, we can't allow writing. 257 */ 258 if (VOP_IS_TEXT(vp) && error == 0) 259 error = ETXTBSY; 260 } 261 if (error != 0) { 262 if (vpislocked == 0) 263 NFSVOPUNLOCK(vp, 0); 264 goto out; 265 } 266 267 /* 268 * Should the override still be applied when ACLs are enabled? 269 */ 270 error = VOP_ACCESSX(vp, accmode, cred, p); 271 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 272 /* 273 * Try again with VEXPLICIT_DENY, to see if the test for 274 * deletion is supported. 275 */ 276 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 277 if (error == 0) { 278 if (vp->v_type == VDIR) { 279 accmode &= ~(VDELETE | VDELETE_CHILD); 280 accmode |= VWRITE; 281 error = VOP_ACCESSX(vp, accmode, cred, p); 282 } else if (supportedtypep != NULL) { 283 *supportedtypep &= ~NFSACCESS_DELETE; 284 } 285 } 286 } 287 288 /* 289 * Allow certain operations for the owner (reads and writes 290 * on files that are already open). 291 */ 292 if (override != NFSACCCHK_NOOVERRIDE && 293 (error == EPERM || error == EACCES)) { 294 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 295 error = 0; 296 else if (override & NFSACCCHK_ALLOWOWNER) { 297 getret = VOP_GETATTR(vp, &vattr, cred); 298 if (getret == 0 && cred->cr_uid == vattr.va_uid) 299 error = 0; 300 } 301 } 302 if (vpislocked == 0) 303 NFSVOPUNLOCK(vp, 0); 304 305out: 306 NFSEXITCODE(error); 307 return (error); 308} 309 310/* 311 * Set attribute(s) vnop. 312 */ 313int 314nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 315 struct thread *p, struct nfsexstuff *exp) 316{ 317 int error; 318 319 error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 320 NFSEXITCODE(error); 321 return (error); 322} 323 324/* 325 * Set up nameidata for a lookup() call and do it. 326 */ 327int 328nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 329 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 330 struct vnode **retdirp) 331{ 332 struct componentname *cnp = &ndp->ni_cnd; 333 int i; 334 struct iovec aiov; 335 struct uio auio; 336 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 337 int error = 0, crossmnt; 338 char *cp; 339 340 *retdirp = NULL; 341 cnp->cn_nameptr = cnp->cn_pnbuf; 342 ndp->ni_strictrelative = 0; 343 /* 344 * Extract and set starting directory. 345 */ 346 if (dp->v_type != VDIR) { 347 if (islocked) 348 vput(dp); 349 else 350 vrele(dp); 351 nfsvno_relpathbuf(ndp); 352 error = ENOTDIR; 353 goto out1; 354 } 355 if (islocked) 356 NFSVOPUNLOCK(dp, 0); 357 VREF(dp); 358 *retdirp = dp; 359 if (NFSVNO_EXRDONLY(exp)) 360 cnp->cn_flags |= RDONLY; 361 ndp->ni_segflg = UIO_SYSSPACE; 362 crossmnt = 1; 363 364 if (nd->nd_flag & ND_PUBLOOKUP) { 365 ndp->ni_loopcnt = 0; 366 if (cnp->cn_pnbuf[0] == '/') { 367 vrele(dp); 368 /* 369 * Check for degenerate pathnames here, since lookup() 370 * panics on them. 371 */ 372 for (i = 1; i < ndp->ni_pathlen; i++) 373 if (cnp->cn_pnbuf[i] != '/') 374 break; 375 if (i == ndp->ni_pathlen) { 376 error = NFSERR_ACCES; 377 goto out; 378 } 379 dp = rootvnode; 380 VREF(dp); 381 } 382 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 383 (nd->nd_flag & ND_NFSV4) == 0) { 384 /* 385 * Only cross mount points for NFSv4 when doing a 386 * mount while traversing the file system above 387 * the mount point, unless nfsrv_enable_crossmntpt is set. 388 */ 389 cnp->cn_flags |= NOCROSSMOUNT; 390 crossmnt = 0; 391 } 392 393 /* 394 * Initialize for scan, set ni_startdir and bump ref on dp again 395 * because lookup() will dereference ni_startdir. 396 */ 397 398 cnp->cn_thread = p; 399 ndp->ni_startdir = dp; 400 ndp->ni_rootdir = rootvnode; 401 ndp->ni_topdir = NULL; 402 403 if (!lockleaf) 404 cnp->cn_flags |= LOCKLEAF; 405 for (;;) { 406 cnp->cn_nameptr = cnp->cn_pnbuf; 407 /* 408 * Call lookup() to do the real work. If an error occurs, 409 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 410 * we do not have to dereference anything before returning. 411 * In either case ni_startdir will be dereferenced and NULLed 412 * out. 413 */ 414 error = lookup(ndp); 415 if (error) 416 break; 417 418 /* 419 * Check for encountering a symbolic link. Trivial 420 * termination occurs if no symlink encountered. 421 */ 422 if ((cnp->cn_flags & ISSYMLINK) == 0) { 423 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 424 nfsvno_relpathbuf(ndp); 425 if (ndp->ni_vp && !lockleaf) 426 NFSVOPUNLOCK(ndp->ni_vp, 0); 427 break; 428 } 429 430 /* 431 * Validate symlink 432 */ 433 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 434 NFSVOPUNLOCK(ndp->ni_dvp, 0); 435 if (!(nd->nd_flag & ND_PUBLOOKUP)) { 436 error = EINVAL; 437 goto badlink2; 438 } 439 440 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 441 error = ELOOP; 442 goto badlink2; 443 } 444 if (ndp->ni_pathlen > 1) 445 cp = uma_zalloc(namei_zone, M_WAITOK); 446 else 447 cp = cnp->cn_pnbuf; 448 aiov.iov_base = cp; 449 aiov.iov_len = MAXPATHLEN; 450 auio.uio_iov = &aiov; 451 auio.uio_iovcnt = 1; 452 auio.uio_offset = 0; 453 auio.uio_rw = UIO_READ; 454 auio.uio_segflg = UIO_SYSSPACE; 455 auio.uio_td = NULL; 456 auio.uio_resid = MAXPATHLEN; 457 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 458 if (error) { 459 badlink1: 460 if (ndp->ni_pathlen > 1) 461 uma_zfree(namei_zone, cp); 462 badlink2: 463 vrele(ndp->ni_dvp); 464 vput(ndp->ni_vp); 465 break; 466 } 467 linklen = MAXPATHLEN - auio.uio_resid; 468 if (linklen == 0) { 469 error = ENOENT; 470 goto badlink1; 471 } 472 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 473 error = ENAMETOOLONG; 474 goto badlink1; 475 } 476 477 /* 478 * Adjust or replace path 479 */ 480 if (ndp->ni_pathlen > 1) { 481 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 482 uma_zfree(namei_zone, cnp->cn_pnbuf); 483 cnp->cn_pnbuf = cp; 484 } else 485 cnp->cn_pnbuf[linklen] = '\0'; 486 ndp->ni_pathlen += linklen; 487 488 /* 489 * Cleanup refs for next loop and check if root directory 490 * should replace current directory. Normally ni_dvp 491 * becomes the new base directory and is cleaned up when 492 * we loop. Explicitly null pointers after invalidation 493 * to clarify operation. 494 */ 495 vput(ndp->ni_vp); 496 ndp->ni_vp = NULL; 497 498 if (cnp->cn_pnbuf[0] == '/') { 499 vrele(ndp->ni_dvp); 500 ndp->ni_dvp = ndp->ni_rootdir; 501 VREF(ndp->ni_dvp); 502 } 503 ndp->ni_startdir = ndp->ni_dvp; 504 ndp->ni_dvp = NULL; 505 } 506 if (!lockleaf) 507 cnp->cn_flags &= ~LOCKLEAF; 508 509out: 510 if (error) { 511 nfsvno_relpathbuf(ndp); 512 ndp->ni_vp = NULL; 513 ndp->ni_dvp = NULL; 514 ndp->ni_startdir = NULL; 515 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 516 ndp->ni_dvp = NULL; 517 } 518 519out1: 520 NFSEXITCODE2(error, nd); 521 return (error); 522} 523 524/* 525 * Set up a pathname buffer and return a pointer to it and, optionally 526 * set a hash pointer. 527 */ 528void 529nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 530{ 531 struct componentname *cnp = &ndp->ni_cnd; 532 533 cnp->cn_flags |= (NOMACCHECK | HASBUF); 534 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 535 if (hashpp != NULL) 536 *hashpp = NULL; 537 *bufpp = cnp->cn_pnbuf; 538} 539 540/* 541 * Release the above path buffer, if not released by nfsvno_namei(). 542 */ 543void 544nfsvno_relpathbuf(struct nameidata *ndp) 545{ 546 547 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 548 panic("nfsrelpath"); 549 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 550 ndp->ni_cnd.cn_flags &= ~HASBUF; 551} 552 553/* 554 * Readlink vnode op into an mbuf list. 555 */ 556int 557nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 558 struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 559{ 560 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 561 struct iovec *ivp = iv; 562 struct uio io, *uiop = &io; 563 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 564 int i, len, tlen, error = 0; 565 566 len = 0; 567 i = 0; 568 while (len < NFS_MAXPATHLEN) { 569 NFSMGET(mp); 570 MCLGET(mp, M_WAITOK); 571 mp->m_len = NFSMSIZ(mp); 572 if (len == 0) { 573 mp3 = mp2 = mp; 574 } else { 575 mp2->m_next = mp; 576 mp2 = mp; 577 } 578 if ((len + mp->m_len) > NFS_MAXPATHLEN) { 579 mp->m_len = NFS_MAXPATHLEN - len; 580 len = NFS_MAXPATHLEN; 581 } else { 582 len += mp->m_len; 583 } 584 ivp->iov_base = mtod(mp, caddr_t); 585 ivp->iov_len = mp->m_len; 586 i++; 587 ivp++; 588 } 589 uiop->uio_iov = iv; 590 uiop->uio_iovcnt = i; 591 uiop->uio_offset = 0; 592 uiop->uio_resid = len; 593 uiop->uio_rw = UIO_READ; 594 uiop->uio_segflg = UIO_SYSSPACE; 595 uiop->uio_td = NULL; 596 error = VOP_READLINK(vp, uiop, cred); 597 if (error) { 598 m_freem(mp3); 599 *lenp = 0; 600 goto out; 601 } 602 if (uiop->uio_resid > 0) { 603 len -= uiop->uio_resid; 604 tlen = NFSM_RNDUP(len); 605 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 606 } 607 *lenp = len; 608 *mpp = mp3; 609 *mpendp = mp; 610 611out: 612 NFSEXITCODE(error); 613 return (error); 614} 615 616/* 617 * Read vnode op call into mbuf list. 618 */ 619int 620nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 621 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 622{ 623 struct mbuf *m; 624 int i; 625 struct iovec *iv; 626 struct iovec *iv2; 627 int error = 0, len, left, siz, tlen, ioflag = 0; 628 struct mbuf *m2 = NULL, *m3; 629 struct uio io, *uiop = &io; 630 struct nfsheur *nh; 631 632 len = left = NFSM_RNDUP(cnt); 633 m3 = NULL; 634 /* 635 * Generate the mbuf list with the uio_iov ref. to it. 636 */ 637 i = 0; 638 while (left > 0) { 639 NFSMGET(m); 640 MCLGET(m, M_WAITOK); 641 m->m_len = 0; 642 siz = min(M_TRAILINGSPACE(m), left); 643 left -= siz; 644 i++; 645 if (m3) 646 m2->m_next = m; 647 else 648 m3 = m; 649 m2 = m; 650 } 651 MALLOC(iv, struct iovec *, i * sizeof (struct iovec), 652 M_TEMP, M_WAITOK); 653 uiop->uio_iov = iv2 = iv; 654 m = m3; 655 left = len; 656 i = 0; 657 while (left > 0) { 658 if (m == NULL) 659 panic("nfsvno_read iov"); 660 siz = min(M_TRAILINGSPACE(m), left); 661 if (siz > 0) { 662 iv->iov_base = mtod(m, caddr_t) + m->m_len; 663 iv->iov_len = siz; 664 m->m_len += siz; 665 left -= siz; 666 iv++; 667 i++; 668 } 669 m = m->m_next; 670 } 671 uiop->uio_iovcnt = i; 672 uiop->uio_offset = off; 673 uiop->uio_resid = len; 674 uiop->uio_rw = UIO_READ; 675 uiop->uio_segflg = UIO_SYSSPACE; 676 uiop->uio_td = NULL; 677 nh = nfsrv_sequential_heuristic(uiop, vp); 678 ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 679 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 680 FREE((caddr_t)iv2, M_TEMP); 681 if (error) { 682 m_freem(m3); 683 *mpp = NULL; 684 goto out; 685 } 686 nh->nh_nextoff = uiop->uio_offset; 687 tlen = len - uiop->uio_resid; 688 cnt = cnt < tlen ? cnt : tlen; 689 tlen = NFSM_RNDUP(cnt); 690 if (tlen == 0) { 691 m_freem(m3); 692 m3 = NULL; 693 } else if (len != tlen || tlen != cnt) 694 nfsrv_adj(m3, len - tlen, tlen - cnt); 695 *mpp = m3; 696 *mpendp = m2; 697 698out: 699 NFSEXITCODE(error); 700 return (error); 701} 702 703/* 704 * Write vnode op from an mbuf list. 705 */ 706int 707nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable, 708 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 709{ 710 struct iovec *ivp; 711 int i, len; 712 struct iovec *iv; 713 int ioflags, error; 714 struct uio io, *uiop = &io; 715 struct nfsheur *nh; 716 717 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP, 718 M_WAITOK); 719 uiop->uio_iov = iv = ivp; 720 uiop->uio_iovcnt = cnt; 721 i = mtod(mp, caddr_t) + mp->m_len - cp; 722 len = retlen; 723 while (len > 0) { 724 if (mp == NULL) 725 panic("nfsvno_write"); 726 if (i > 0) { 727 i = min(i, len); 728 ivp->iov_base = cp; 729 ivp->iov_len = i; 730 ivp++; 731 len -= i; 732 } 733 mp = mp->m_next; 734 if (mp) { 735 i = mp->m_len; 736 cp = mtod(mp, caddr_t); 737 } 738 } 739 740 if (stable == NFSWRITE_UNSTABLE) 741 ioflags = IO_NODELOCKED; 742 else 743 ioflags = (IO_SYNC | IO_NODELOCKED); 744 uiop->uio_resid = retlen; 745 uiop->uio_rw = UIO_WRITE; 746 uiop->uio_segflg = UIO_SYSSPACE; 747 NFSUIOPROC(uiop, p); 748 uiop->uio_offset = off; 749 nh = nfsrv_sequential_heuristic(uiop, vp); 750 ioflags |= nh->nh_seqcount << IO_SEQSHIFT; 751 error = VOP_WRITE(vp, uiop, ioflags, cred); 752 if (error == 0) 753 nh->nh_nextoff = uiop->uio_offset; 754 FREE((caddr_t)iv, M_TEMP); 755 756 NFSEXITCODE(error); 757 return (error); 758} 759 760/* 761 * Common code for creating a regular file (plus special files for V2). 762 */ 763int 764nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 765 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 766 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp) 767{ 768 u_quad_t tempsize; 769 int error; 770 771 error = nd->nd_repstat; 772 if (!error && ndp->ni_vp == NULL) { 773 if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 774 vrele(ndp->ni_startdir); 775 error = VOP_CREATE(ndp->ni_dvp, 776 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 777 vput(ndp->ni_dvp); 778 nfsvno_relpathbuf(ndp); 779 if (!error) { 780 if (*exclusive_flagp) { 781 *exclusive_flagp = 0; 782 NFSVNO_ATTRINIT(nvap); 783 nvap->na_atime.tv_sec = cverf[0]; 784 nvap->na_atime.tv_nsec = cverf[1]; 785 error = VOP_SETATTR(ndp->ni_vp, 786 &nvap->na_vattr, nd->nd_cred); 787 } 788 } 789 /* 790 * NFS V2 Only. nfsrvd_mknod() does this for V3. 791 * (This implies, just get out on an error.) 792 */ 793 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 794 nvap->na_type == VFIFO) { 795 if (nvap->na_type == VCHR && rdev == 0xffffffff) 796 nvap->na_type = VFIFO; 797 if (nvap->na_type != VFIFO && 798 (error = priv_check_cred(nd->nd_cred, 799 PRIV_VFS_MKNOD_DEV, 0))) { 800 vrele(ndp->ni_startdir); 801 nfsvno_relpathbuf(ndp); 802 vput(ndp->ni_dvp); 803 goto out; 804 } 805 nvap->na_rdev = rdev; 806 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 807 &ndp->ni_cnd, &nvap->na_vattr); 808 vput(ndp->ni_dvp); 809 nfsvno_relpathbuf(ndp); 810 vrele(ndp->ni_startdir); 811 if (error) 812 goto out; 813 } else { 814 vrele(ndp->ni_startdir); 815 nfsvno_relpathbuf(ndp); 816 vput(ndp->ni_dvp); 817 error = ENXIO; 818 goto out; 819 } 820 *vpp = ndp->ni_vp; 821 } else { 822 /* 823 * Handle cases where error is already set and/or 824 * the file exists. 825 * 1 - clean up the lookup 826 * 2 - iff !error and na_size set, truncate it 827 */ 828 vrele(ndp->ni_startdir); 829 nfsvno_relpathbuf(ndp); 830 *vpp = ndp->ni_vp; 831 if (ndp->ni_dvp == *vpp) 832 vrele(ndp->ni_dvp); 833 else 834 vput(ndp->ni_dvp); 835 if (!error && nvap->na_size != VNOVAL) { 836 error = nfsvno_accchk(*vpp, VWRITE, 837 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 838 NFSACCCHK_VPISLOCKED, NULL); 839 if (!error) { 840 tempsize = nvap->na_size; 841 NFSVNO_ATTRINIT(nvap); 842 nvap->na_size = tempsize; 843 error = VOP_SETATTR(*vpp, 844 &nvap->na_vattr, nd->nd_cred); 845 } 846 } 847 if (error) 848 vput(*vpp); 849 } 850 851out: 852 NFSEXITCODE(error); 853 return (error); 854} 855 856/* 857 * Do a mknod vnode op. 858 */ 859int 860nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 861 struct thread *p) 862{ 863 int error = 0; 864 enum vtype vtyp; 865 866 vtyp = nvap->na_type; 867 /* 868 * Iff doesn't exist, create it. 869 */ 870 if (ndp->ni_vp) { 871 vrele(ndp->ni_startdir); 872 nfsvno_relpathbuf(ndp); 873 vput(ndp->ni_dvp); 874 vrele(ndp->ni_vp); 875 error = EEXIST; 876 goto out; 877 } 878 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 879 vrele(ndp->ni_startdir); 880 nfsvno_relpathbuf(ndp); 881 vput(ndp->ni_dvp); 882 error = NFSERR_BADTYPE; 883 goto out; 884 } 885 if (vtyp == VSOCK) { 886 vrele(ndp->ni_startdir); 887 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 888 &ndp->ni_cnd, &nvap->na_vattr); 889 vput(ndp->ni_dvp); 890 nfsvno_relpathbuf(ndp); 891 } else { 892 if (nvap->na_type != VFIFO && 893 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) { 894 vrele(ndp->ni_startdir); 895 nfsvno_relpathbuf(ndp); 896 vput(ndp->ni_dvp); 897 goto out; 898 } 899 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 900 &ndp->ni_cnd, &nvap->na_vattr); 901 vput(ndp->ni_dvp); 902 nfsvno_relpathbuf(ndp); 903 vrele(ndp->ni_startdir); 904 /* 905 * Since VOP_MKNOD returns the ni_vp, I can't 906 * see any reason to do the lookup. 907 */ 908 } 909 910out: 911 NFSEXITCODE(error); 912 return (error); 913} 914 915/* 916 * Mkdir vnode op. 917 */ 918int 919nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 920 struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 921{ 922 int error = 0; 923 924 if (ndp->ni_vp != NULL) { 925 if (ndp->ni_dvp == ndp->ni_vp) 926 vrele(ndp->ni_dvp); 927 else 928 vput(ndp->ni_dvp); 929 vrele(ndp->ni_vp); 930 nfsvno_relpathbuf(ndp); 931 error = EEXIST; 932 goto out; 933 } 934 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 935 &nvap->na_vattr); 936 vput(ndp->ni_dvp); 937 nfsvno_relpathbuf(ndp); 938 939out: 940 NFSEXITCODE(error); 941 return (error); 942} 943 944/* 945 * symlink vnode op. 946 */ 947int 948nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 949 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 950 struct nfsexstuff *exp) 951{ 952 int error = 0; 953 954 if (ndp->ni_vp) { 955 vrele(ndp->ni_startdir); 956 nfsvno_relpathbuf(ndp); 957 if (ndp->ni_dvp == ndp->ni_vp) 958 vrele(ndp->ni_dvp); 959 else 960 vput(ndp->ni_dvp); 961 vrele(ndp->ni_vp); 962 error = EEXIST; 963 goto out; 964 } 965 966 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 967 &nvap->na_vattr, pathcp); 968 vput(ndp->ni_dvp); 969 vrele(ndp->ni_startdir); 970 nfsvno_relpathbuf(ndp); 971 /* 972 * Although FreeBSD still had the lookup code in 973 * it for 7/current, there doesn't seem to be any 974 * point, since VOP_SYMLINK() returns the ni_vp. 975 * Just vput it for v2. 976 */ 977 if (!not_v2 && !error) 978 vput(ndp->ni_vp); 979 980out: 981 NFSEXITCODE(error); 982 return (error); 983} 984 985/* 986 * Parse symbolic link arguments. 987 * This function has an ugly side effect. It will MALLOC() an area for 988 * the symlink and set iov_base to point to it, only if it succeeds. 989 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 990 * be FREE'd later. 991 */ 992int 993nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 994 struct thread *p, char **pathcpp, int *lenp) 995{ 996 u_int32_t *tl; 997 char *pathcp = NULL; 998 int error = 0, len; 999 struct nfsv2_sattr *sp; 1000 1001 *pathcpp = NULL; 1002 *lenp = 0; 1003 if ((nd->nd_flag & ND_NFSV3) && 1004 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p))) 1005 goto nfsmout; 1006 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1007 len = fxdr_unsigned(int, *tl); 1008 if (len > NFS_MAXPATHLEN || len <= 0) { 1009 error = EBADRPC; 1010 goto nfsmout; 1011 } 1012 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK); 1013 error = nfsrv_mtostr(nd, pathcp, len); 1014 if (error) 1015 goto nfsmout; 1016 if (nd->nd_flag & ND_NFSV2) { 1017 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1018 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 1019 } 1020 *pathcpp = pathcp; 1021 *lenp = len; 1022 NFSEXITCODE2(0, nd); 1023 return (0); 1024nfsmout: 1025 if (pathcp) 1026 free(pathcp, M_TEMP); 1027 NFSEXITCODE2(error, nd); 1028 return (error); 1029} 1030 1031/* 1032 * Remove a non-directory object. 1033 */ 1034int 1035nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1036 struct thread *p, struct nfsexstuff *exp) 1037{ 1038 struct vnode *vp; 1039 int error = 0; 1040 1041 vp = ndp->ni_vp; 1042 if (vp->v_type == VDIR) 1043 error = NFSERR_ISDIR; 1044 else if (is_v4) 1045 error = nfsrv_checkremove(vp, 1, p); 1046 if (!error) 1047 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 1048 if (ndp->ni_dvp == vp) 1049 vrele(ndp->ni_dvp); 1050 else 1051 vput(ndp->ni_dvp); 1052 vput(vp); 1053 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1054 nfsvno_relpathbuf(ndp); 1055 NFSEXITCODE(error); 1056 return (error); 1057} 1058 1059/* 1060 * Remove a directory. 1061 */ 1062int 1063nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1064 struct thread *p, struct nfsexstuff *exp) 1065{ 1066 struct vnode *vp; 1067 int error = 0; 1068 1069 vp = ndp->ni_vp; 1070 if (vp->v_type != VDIR) { 1071 error = ENOTDIR; 1072 goto out; 1073 } 1074 /* 1075 * No rmdir "." please. 1076 */ 1077 if (ndp->ni_dvp == vp) { 1078 error = EINVAL; 1079 goto out; 1080 } 1081 /* 1082 * The root of a mounted filesystem cannot be deleted. 1083 */ 1084 if (vp->v_vflag & VV_ROOT) 1085 error = EBUSY; 1086out: 1087 if (!error) 1088 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1089 if (ndp->ni_dvp == vp) 1090 vrele(ndp->ni_dvp); 1091 else 1092 vput(ndp->ni_dvp); 1093 vput(vp); 1094 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1095 nfsvno_relpathbuf(ndp); 1096 NFSEXITCODE(error); 1097 return (error); 1098} 1099 1100/* 1101 * Rename vnode op. 1102 */ 1103int 1104nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1105 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1106{ 1107 struct vnode *fvp, *tvp, *tdvp; 1108 int error = 0; 1109 1110 fvp = fromndp->ni_vp; 1111 if (ndstat) { 1112 vrele(fromndp->ni_dvp); 1113 vrele(fvp); 1114 error = ndstat; 1115 goto out1; 1116 } 1117 tdvp = tondp->ni_dvp; 1118 tvp = tondp->ni_vp; 1119 if (tvp != NULL) { 1120 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1121 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1122 goto out; 1123 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1124 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1125 goto out; 1126 } 1127 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1128 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1129 goto out; 1130 } 1131 1132 /* 1133 * A rename to '.' or '..' results in a prematurely 1134 * unlocked vnode on FreeBSD5, so I'm just going to fail that 1135 * here. 1136 */ 1137 if ((tondp->ni_cnd.cn_namelen == 1 && 1138 tondp->ni_cnd.cn_nameptr[0] == '.') || 1139 (tondp->ni_cnd.cn_namelen == 2 && 1140 tondp->ni_cnd.cn_nameptr[0] == '.' && 1141 tondp->ni_cnd.cn_nameptr[1] == '.')) { 1142 error = EINVAL; 1143 goto out; 1144 } 1145 } 1146 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1147 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1148 goto out; 1149 } 1150 if (fvp->v_mount != tdvp->v_mount) { 1151 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1152 goto out; 1153 } 1154 if (fvp == tdvp) { 1155 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1156 goto out; 1157 } 1158 if (fvp == tvp) { 1159 /* 1160 * If source and destination are the same, there is nothing to 1161 * do. Set error to -1 to indicate this. 1162 */ 1163 error = -1; 1164 goto out; 1165 } 1166 if (ndflag & ND_NFSV4) { 1167 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) { 1168 error = nfsrv_checkremove(fvp, 0, p); 1169 NFSVOPUNLOCK(fvp, 0); 1170 } else 1171 error = EPERM; 1172 if (tvp && !error) 1173 error = nfsrv_checkremove(tvp, 1, p); 1174 } else { 1175 /* 1176 * For NFSv2 and NFSv3, try to get rid of the delegation, so 1177 * that the NFSv4 client won't be confused by the rename. 1178 * Since nfsd_recalldelegation() can only be called on an 1179 * unlocked vnode at this point and fvp is the file that will 1180 * still exist after the rename, just do fvp. 1181 */ 1182 nfsd_recalldelegation(fvp, p); 1183 } 1184out: 1185 if (!error) { 1186 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1187 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1188 &tondp->ni_cnd); 1189 } else { 1190 if (tdvp == tvp) 1191 vrele(tdvp); 1192 else 1193 vput(tdvp); 1194 if (tvp) 1195 vput(tvp); 1196 vrele(fromndp->ni_dvp); 1197 vrele(fvp); 1198 if (error == -1) 1199 error = 0; 1200 } 1201 vrele(tondp->ni_startdir); 1202 nfsvno_relpathbuf(tondp); 1203out1: 1204 vrele(fromndp->ni_startdir); 1205 nfsvno_relpathbuf(fromndp); 1206 NFSEXITCODE(error); 1207 return (error); 1208} 1209 1210/* 1211 * Link vnode op. 1212 */ 1213int 1214nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1215 struct thread *p, struct nfsexstuff *exp) 1216{ 1217 struct vnode *xp; 1218 int error = 0; 1219 1220 xp = ndp->ni_vp; 1221 if (xp != NULL) { 1222 error = EEXIST; 1223 } else { 1224 xp = ndp->ni_dvp; 1225 if (vp->v_mount != xp->v_mount) 1226 error = EXDEV; 1227 } 1228 if (!error) { 1229 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 1230 if ((vp->v_iflag & VI_DOOMED) == 0) 1231 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1232 else 1233 error = EPERM; 1234 if (ndp->ni_dvp == vp) 1235 vrele(ndp->ni_dvp); 1236 else 1237 vput(ndp->ni_dvp); 1238 NFSVOPUNLOCK(vp, 0); 1239 } else { 1240 if (ndp->ni_dvp == ndp->ni_vp) 1241 vrele(ndp->ni_dvp); 1242 else 1243 vput(ndp->ni_dvp); 1244 if (ndp->ni_vp) 1245 vrele(ndp->ni_vp); 1246 } 1247 nfsvno_relpathbuf(ndp); 1248 NFSEXITCODE(error); 1249 return (error); 1250} 1251 1252/* 1253 * Do the fsync() appropriate for the commit. 1254 */ 1255int 1256nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1257 struct thread *td) 1258{ 1259 int error = 0; 1260 1261 /* 1262 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of 1263 * file is done. At this time VOP_FSYNC does not accept offset and 1264 * byte count parameters so call VOP_FSYNC the whole file for now. 1265 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3. 1266 */ 1267 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) { 1268 /* 1269 * Give up and do the whole thing 1270 */ 1271 if (vp->v_object && 1272 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1273 VM_OBJECT_WLOCK(vp->v_object); 1274 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1275 VM_OBJECT_WUNLOCK(vp->v_object); 1276 } 1277 error = VOP_FSYNC(vp, MNT_WAIT, td); 1278 } else { 1279 /* 1280 * Locate and synchronously write any buffers that fall 1281 * into the requested range. Note: we are assuming that 1282 * f_iosize is a power of 2. 1283 */ 1284 int iosize = vp->v_mount->mnt_stat.f_iosize; 1285 int iomask = iosize - 1; 1286 struct bufobj *bo; 1287 daddr_t lblkno; 1288 1289 /* 1290 * Align to iosize boundry, super-align to page boundry. 1291 */ 1292 if (off & iomask) { 1293 cnt += off & iomask; 1294 off &= ~(u_quad_t)iomask; 1295 } 1296 if (off & PAGE_MASK) { 1297 cnt += off & PAGE_MASK; 1298 off &= ~(u_quad_t)PAGE_MASK; 1299 } 1300 lblkno = off / iosize; 1301 1302 if (vp->v_object && 1303 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1304 VM_OBJECT_WLOCK(vp->v_object); 1305 vm_object_page_clean(vp->v_object, off, off + cnt, 1306 OBJPC_SYNC); 1307 VM_OBJECT_WUNLOCK(vp->v_object); 1308 } 1309 1310 bo = &vp->v_bufobj; 1311 BO_LOCK(bo); 1312 while (cnt > 0) { 1313 struct buf *bp; 1314 1315 /* 1316 * If we have a buffer and it is marked B_DELWRI we 1317 * have to lock and write it. Otherwise the prior 1318 * write is assumed to have already been committed. 1319 * 1320 * gbincore() can return invalid buffers now so we 1321 * have to check that bit as well (though B_DELWRI 1322 * should not be set if B_INVAL is set there could be 1323 * a race here since we haven't locked the buffer). 1324 */ 1325 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1326 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1327 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) { 1328 BO_LOCK(bo); 1329 continue; /* retry */ 1330 } 1331 if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1332 B_DELWRI) { 1333 bremfree(bp); 1334 bp->b_flags &= ~B_ASYNC; 1335 bwrite(bp); 1336 ++nfs_commit_miss; 1337 } else 1338 BUF_UNLOCK(bp); 1339 BO_LOCK(bo); 1340 } 1341 ++nfs_commit_blks; 1342 if (cnt < iosize) 1343 break; 1344 cnt -= iosize; 1345 ++lblkno; 1346 } 1347 BO_UNLOCK(bo); 1348 } 1349 NFSEXITCODE(error); 1350 return (error); 1351} 1352 1353/* 1354 * Statfs vnode op. 1355 */ 1356int 1357nfsvno_statfs(struct vnode *vp, struct statfs *sf) 1358{ 1359 int error; 1360 1361 error = VFS_STATFS(vp->v_mount, sf); 1362 if (error == 0) { 1363 /* 1364 * Since NFS handles these values as unsigned on the 1365 * wire, there is no way to represent negative values, 1366 * so set them to 0. Without this, they will appear 1367 * to be very large positive values for clients like 1368 * Solaris10. 1369 */ 1370 if (sf->f_bavail < 0) 1371 sf->f_bavail = 0; 1372 if (sf->f_ffree < 0) 1373 sf->f_ffree = 0; 1374 } 1375 NFSEXITCODE(error); 1376 return (error); 1377} 1378 1379/* 1380 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1381 * must handle nfsrv_opencheck() calls after any other access checks. 1382 */ 1383void 1384nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1385 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1386 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1387 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p, 1388 struct nfsexstuff *exp, struct vnode **vpp) 1389{ 1390 struct vnode *vp = NULL; 1391 u_quad_t tempsize; 1392 struct nfsexstuff nes; 1393 1394 if (ndp->ni_vp == NULL) 1395 nd->nd_repstat = nfsrv_opencheck(clientid, 1396 stateidp, stp, NULL, nd, p, nd->nd_repstat); 1397 if (!nd->nd_repstat) { 1398 if (ndp->ni_vp == NULL) { 1399 vrele(ndp->ni_startdir); 1400 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1401 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1402 vput(ndp->ni_dvp); 1403 nfsvno_relpathbuf(ndp); 1404 if (!nd->nd_repstat) { 1405 if (*exclusive_flagp) { 1406 *exclusive_flagp = 0; 1407 NFSVNO_ATTRINIT(nvap); 1408 nvap->na_atime.tv_sec = cverf[0]; 1409 nvap->na_atime.tv_nsec = cverf[1]; 1410 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1411 &nvap->na_vattr, cred); 1412 } else { 1413 nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1414 aclp, p, attrbitp, exp); 1415 } 1416 } 1417 vp = ndp->ni_vp; 1418 } else { 1419 if (ndp->ni_startdir) 1420 vrele(ndp->ni_startdir); 1421 nfsvno_relpathbuf(ndp); 1422 vp = ndp->ni_vp; 1423 if (create == NFSV4OPEN_CREATE) { 1424 if (ndp->ni_dvp == vp) 1425 vrele(ndp->ni_dvp); 1426 else 1427 vput(ndp->ni_dvp); 1428 } 1429 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1430 if (ndp->ni_cnd.cn_flags & RDONLY) 1431 NFSVNO_SETEXRDONLY(&nes); 1432 else 1433 NFSVNO_EXINIT(&nes); 1434 nd->nd_repstat = nfsvno_accchk(vp, 1435 VWRITE, cred, &nes, p, 1436 NFSACCCHK_NOOVERRIDE, 1437 NFSACCCHK_VPISLOCKED, NULL); 1438 nd->nd_repstat = nfsrv_opencheck(clientid, 1439 stateidp, stp, vp, nd, p, nd->nd_repstat); 1440 if (!nd->nd_repstat) { 1441 tempsize = nvap->na_size; 1442 NFSVNO_ATTRINIT(nvap); 1443 nvap->na_size = tempsize; 1444 nd->nd_repstat = VOP_SETATTR(vp, 1445 &nvap->na_vattr, cred); 1446 } 1447 } else if (vp->v_type == VREG) { 1448 nd->nd_repstat = nfsrv_opencheck(clientid, 1449 stateidp, stp, vp, nd, p, nd->nd_repstat); 1450 } 1451 } 1452 } else { 1453 if (ndp->ni_cnd.cn_flags & HASBUF) 1454 nfsvno_relpathbuf(ndp); 1455 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1456 vrele(ndp->ni_startdir); 1457 if (ndp->ni_dvp == ndp->ni_vp) 1458 vrele(ndp->ni_dvp); 1459 else 1460 vput(ndp->ni_dvp); 1461 if (ndp->ni_vp) 1462 vput(ndp->ni_vp); 1463 } 1464 } 1465 *vpp = vp; 1466 1467 NFSEXITCODE2(0, nd); 1468} 1469 1470/* 1471 * Updates the file rev and sets the mtime and ctime 1472 * to the current clock time, returning the va_filerev and va_Xtime 1473 * values. 1474 * Return ESTALE to indicate the vnode is VI_DOOMED. 1475 */ 1476int 1477nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1478 struct ucred *cred, struct thread *p) 1479{ 1480 struct vattr va; 1481 1482 VATTR_NULL(&va); 1483 vfs_timestamp(&va.va_mtime); 1484 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 1485 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY); 1486 if ((vp->v_iflag & VI_DOOMED) != 0) 1487 return (ESTALE); 1488 } 1489 (void) VOP_SETATTR(vp, &va, cred); 1490 (void) nfsvno_getattr(vp, nvap, cred, p, 1); 1491 return (0); 1492} 1493 1494/* 1495 * Glue routine to nfsv4_fillattr(). 1496 */ 1497int 1498nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp, 1499 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1500 struct ucred *cred, struct thread *p, int isdgram, int reterr, 1501 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno) 1502{ 1503 int error; 1504 1505 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 1506 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 1507 mounted_on_fileno); 1508 NFSEXITCODE2(0, nd); 1509 return (error); 1510} 1511 1512/* Since the Readdir vnode ops vary, put the entire functions in here. */ 1513/* 1514 * nfs readdir service 1515 * - mallocs what it thinks is enough to read 1516 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1517 * - calls VOP_READDIR() 1518 * - loops around building the reply 1519 * if the output generated exceeds count break out of loop 1520 * The NFSM_CLGET macro is used here so that the reply will be packed 1521 * tightly in mbuf clusters. 1522 * - it trims out records with d_fileno == 0 1523 * this doesn't matter for Unix clients, but they might confuse clients 1524 * for other os'. 1525 * - it trims out records with d_type == DT_WHT 1526 * these cannot be seen through NFS (unless we extend the protocol) 1527 * The alternate call nfsrvd_readdirplus() does lookups as well. 1528 * PS: The NFS protocol spec. does not clarify what the "count" byte 1529 * argument is a count of.. just name strings and file id's or the 1530 * entire reply rpc or ... 1531 * I tried just file name and id sizes and it confused the Sun client, 1532 * so I am using the full rpc size now. The "paranoia.." comment refers 1533 * to including the status longwords that are not a part of the dir. 1534 * "entry" structures, but are in the rpc. 1535 */ 1536int 1537nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1538 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1539{ 1540 struct dirent *dp; 1541 u_int32_t *tl; 1542 int dirlen; 1543 char *cpos, *cend, *rbuf; 1544 struct nfsvattr at; 1545 int nlen, error = 0, getret = 1; 1546 int siz, cnt, fullsiz, eofflag, ncookies; 1547 u_int64_t off, toff, verf; 1548 u_long *cookies = NULL, *cookiep; 1549 struct uio io; 1550 struct iovec iv; 1551 int not_zfs; 1552 1553 if (nd->nd_repstat) { 1554 nfsrv_postopattr(nd, getret, &at); 1555 goto out; 1556 } 1557 if (nd->nd_flag & ND_NFSV2) { 1558 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1559 off = fxdr_unsigned(u_quad_t, *tl++); 1560 } else { 1561 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1562 off = fxdr_hyper(tl); 1563 tl += 2; 1564 verf = fxdr_hyper(tl); 1565 tl += 2; 1566 } 1567 toff = off; 1568 cnt = fxdr_unsigned(int, *tl); 1569 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1570 cnt = NFS_SRVMAXDATA(nd); 1571 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1572 fullsiz = siz; 1573 if (nd->nd_flag & ND_NFSV3) { 1574 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, 1575 p, 1); 1576#if 0 1577 /* 1578 * va_filerev is not sufficient as a cookie verifier, 1579 * since it is not supposed to change when entries are 1580 * removed/added unless that offset cookies returned to 1581 * the client are no longer valid. 1582 */ 1583 if (!nd->nd_repstat && toff && verf != at.na_filerev) 1584 nd->nd_repstat = NFSERR_BAD_COOKIE; 1585#endif 1586 } 1587 if (!nd->nd_repstat && vp->v_type != VDIR) 1588 nd->nd_repstat = NFSERR_NOTDIR; 1589 if (nd->nd_repstat == 0 && cnt == 0) { 1590 if (nd->nd_flag & ND_NFSV2) 1591 /* NFSv2 does not have NFSERR_TOOSMALL */ 1592 nd->nd_repstat = EPERM; 1593 else 1594 nd->nd_repstat = NFSERR_TOOSMALL; 1595 } 1596 if (!nd->nd_repstat) 1597 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1598 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1599 NFSACCCHK_VPISLOCKED, NULL); 1600 if (nd->nd_repstat) { 1601 vput(vp); 1602 if (nd->nd_flag & ND_NFSV3) 1603 nfsrv_postopattr(nd, getret, &at); 1604 goto out; 1605 } 1606 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs"); 1607 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1608again: 1609 eofflag = 0; 1610 if (cookies) { 1611 free((caddr_t)cookies, M_TEMP); 1612 cookies = NULL; 1613 } 1614 1615 iv.iov_base = rbuf; 1616 iv.iov_len = siz; 1617 io.uio_iov = &iv; 1618 io.uio_iovcnt = 1; 1619 io.uio_offset = (off_t)off; 1620 io.uio_resid = siz; 1621 io.uio_segflg = UIO_SYSSPACE; 1622 io.uio_rw = UIO_READ; 1623 io.uio_td = NULL; 1624 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1625 &cookies); 1626 off = (u_int64_t)io.uio_offset; 1627 if (io.uio_resid) 1628 siz -= io.uio_resid; 1629 1630 if (!cookies && !nd->nd_repstat) 1631 nd->nd_repstat = NFSERR_PERM; 1632 if (nd->nd_flag & ND_NFSV3) { 1633 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1634 if (!nd->nd_repstat) 1635 nd->nd_repstat = getret; 1636 } 1637 1638 /* 1639 * Handles the failed cases. nd->nd_repstat == 0 past here. 1640 */ 1641 if (nd->nd_repstat) { 1642 vput(vp); 1643 free((caddr_t)rbuf, M_TEMP); 1644 if (cookies) 1645 free((caddr_t)cookies, M_TEMP); 1646 if (nd->nd_flag & ND_NFSV3) 1647 nfsrv_postopattr(nd, getret, &at); 1648 goto out; 1649 } 1650 /* 1651 * If nothing read, return eof 1652 * rpc reply 1653 */ 1654 if (siz == 0) { 1655 vput(vp); 1656 if (nd->nd_flag & ND_NFSV2) { 1657 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1658 } else { 1659 nfsrv_postopattr(nd, getret, &at); 1660 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1661 txdr_hyper(at.na_filerev, tl); 1662 tl += 2; 1663 } 1664 *tl++ = newnfs_false; 1665 *tl = newnfs_true; 1666 FREE((caddr_t)rbuf, M_TEMP); 1667 FREE((caddr_t)cookies, M_TEMP); 1668 goto out; 1669 } 1670 1671 /* 1672 * Check for degenerate cases of nothing useful read. 1673 * If so go try again 1674 */ 1675 cpos = rbuf; 1676 cend = rbuf + siz; 1677 dp = (struct dirent *)cpos; 1678 cookiep = cookies; 1679 1680 /* 1681 * For some reason FreeBSD's ufs_readdir() chooses to back the 1682 * directory offset up to a block boundary, so it is necessary to 1683 * skip over the records that precede the requested offset. This 1684 * requires the assumption that file offset cookies monotonically 1685 * increase. 1686 * Since the offset cookies don't monotonically increase for ZFS, 1687 * this is not done when ZFS is the file system. 1688 */ 1689 while (cpos < cend && ncookies > 0 && 1690 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1691 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) { 1692 cpos += dp->d_reclen; 1693 dp = (struct dirent *)cpos; 1694 cookiep++; 1695 ncookies--; 1696 } 1697 if (cpos >= cend || ncookies == 0) { 1698 siz = fullsiz; 1699 toff = off; 1700 goto again; 1701 } 1702 vput(vp); 1703 1704 /* 1705 * dirlen is the size of the reply, including all XDR and must 1706 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1707 * if the XDR should be included in "count", but to be safe, we do. 1708 * (Include the two booleans at the end of the reply in dirlen now.) 1709 */ 1710 if (nd->nd_flag & ND_NFSV3) { 1711 nfsrv_postopattr(nd, getret, &at); 1712 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1713 txdr_hyper(at.na_filerev, tl); 1714 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1715 } else { 1716 dirlen = 2 * NFSX_UNSIGNED; 1717 } 1718 1719 /* Loop through the records and build reply */ 1720 while (cpos < cend && ncookies > 0) { 1721 nlen = dp->d_namlen; 1722 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1723 nlen <= NFS_MAXNAMLEN) { 1724 if (nd->nd_flag & ND_NFSV3) 1725 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1726 else 1727 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1728 if (dirlen > cnt) { 1729 eofflag = 0; 1730 break; 1731 } 1732 1733 /* 1734 * Build the directory record xdr from 1735 * the dirent entry. 1736 */ 1737 if (nd->nd_flag & ND_NFSV3) { 1738 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1739 *tl++ = newnfs_true; 1740 *tl++ = 0; 1741 } else { 1742 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1743 *tl++ = newnfs_true; 1744 } 1745 *tl = txdr_unsigned(dp->d_fileno); 1746 (void) nfsm_strtom(nd, dp->d_name, nlen); 1747 if (nd->nd_flag & ND_NFSV3) { 1748 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1749 *tl++ = 0; 1750 } else 1751 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1752 *tl = txdr_unsigned(*cookiep); 1753 } 1754 cpos += dp->d_reclen; 1755 dp = (struct dirent *)cpos; 1756 cookiep++; 1757 ncookies--; 1758 } 1759 if (cpos < cend) 1760 eofflag = 0; 1761 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1762 *tl++ = newnfs_false; 1763 if (eofflag) 1764 *tl = newnfs_true; 1765 else 1766 *tl = newnfs_false; 1767 FREE((caddr_t)rbuf, M_TEMP); 1768 FREE((caddr_t)cookies, M_TEMP); 1769 1770out: 1771 NFSEXITCODE2(0, nd); 1772 return (0); 1773nfsmout: 1774 vput(vp); 1775 NFSEXITCODE2(error, nd); 1776 return (error); 1777} 1778 1779/* 1780 * Readdirplus for V3 and Readdir for V4. 1781 */ 1782int 1783nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 1784 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1785{ 1786 struct dirent *dp; 1787 u_int32_t *tl; 1788 int dirlen; 1789 char *cpos, *cend, *rbuf; 1790 struct vnode *nvp; 1791 fhandle_t nfh; 1792 struct nfsvattr nva, at, *nvap = &nva; 1793 struct mbuf *mb0, *mb1; 1794 struct nfsreferral *refp; 1795 int nlen, r, error = 0, getret = 1, usevget = 1; 1796 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 1797 caddr_t bpos0, bpos1; 1798 u_int64_t off, toff, verf; 1799 u_long *cookies = NULL, *cookiep; 1800 nfsattrbit_t attrbits, rderrbits, savbits; 1801 struct uio io; 1802 struct iovec iv; 1803 struct componentname cn; 1804 int at_root, needs_unbusy, not_zfs, supports_nfsv4acls; 1805 struct mount *mp, *new_mp; 1806 uint64_t mounted_on_fileno; 1807 1808 if (nd->nd_repstat) { 1809 nfsrv_postopattr(nd, getret, &at); 1810 goto out; 1811 } 1812 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 1813 off = fxdr_hyper(tl); 1814 toff = off; 1815 tl += 2; 1816 verf = fxdr_hyper(tl); 1817 tl += 2; 1818 siz = fxdr_unsigned(int, *tl++); 1819 cnt = fxdr_unsigned(int, *tl); 1820 1821 /* 1822 * Use the server's maximum data transfer size as the upper bound 1823 * on reply datalen. 1824 */ 1825 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1826 cnt = NFS_SRVMAXDATA(nd); 1827 1828 /* 1829 * siz is a "hint" of how much directory information (name, fileid, 1830 * cookie) should be in the reply. At least one client "hints" 0, 1831 * so I set it to cnt for that case. I also round it up to the 1832 * next multiple of DIRBLKSIZ. 1833 */ 1834 if (siz <= 0) 1835 siz = cnt; 1836 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1837 1838 if (nd->nd_flag & ND_NFSV4) { 1839 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1840 if (error) 1841 goto nfsmout; 1842 NFSSET_ATTRBIT(&savbits, &attrbits); 1843 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits); 1844 NFSZERO_ATTRBIT(&rderrbits); 1845 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 1846 } else { 1847 NFSZERO_ATTRBIT(&attrbits); 1848 } 1849 fullsiz = siz; 1850 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1851 if (!nd->nd_repstat) { 1852 if (off && verf != at.na_filerev) { 1853 /* 1854 * va_filerev is not sufficient as a cookie verifier, 1855 * since it is not supposed to change when entries are 1856 * removed/added unless that offset cookies returned to 1857 * the client are no longer valid. 1858 */ 1859#if 0 1860 if (nd->nd_flag & ND_NFSV4) { 1861 nd->nd_repstat = NFSERR_NOTSAME; 1862 } else { 1863 nd->nd_repstat = NFSERR_BAD_COOKIE; 1864 } 1865#endif 1866 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) { 1867 nd->nd_repstat = NFSERR_BAD_COOKIE; 1868 } 1869 } 1870 if (!nd->nd_repstat && vp->v_type != VDIR) 1871 nd->nd_repstat = NFSERR_NOTDIR; 1872 if (!nd->nd_repstat && cnt == 0) 1873 nd->nd_repstat = NFSERR_TOOSMALL; 1874 if (!nd->nd_repstat) 1875 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1876 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1877 NFSACCCHK_VPISLOCKED, NULL); 1878 if (nd->nd_repstat) { 1879 vput(vp); 1880 if (nd->nd_flag & ND_NFSV3) 1881 nfsrv_postopattr(nd, getret, &at); 1882 goto out; 1883 } 1884 not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs"); 1885 1886 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1887again: 1888 eofflag = 0; 1889 if (cookies) { 1890 free((caddr_t)cookies, M_TEMP); 1891 cookies = NULL; 1892 } 1893 1894 iv.iov_base = rbuf; 1895 iv.iov_len = siz; 1896 io.uio_iov = &iv; 1897 io.uio_iovcnt = 1; 1898 io.uio_offset = (off_t)off; 1899 io.uio_resid = siz; 1900 io.uio_segflg = UIO_SYSSPACE; 1901 io.uio_rw = UIO_READ; 1902 io.uio_td = NULL; 1903 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1904 &cookies); 1905 off = (u_int64_t)io.uio_offset; 1906 if (io.uio_resid) 1907 siz -= io.uio_resid; 1908 1909 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1910 1911 if (!cookies && !nd->nd_repstat) 1912 nd->nd_repstat = NFSERR_PERM; 1913 if (!nd->nd_repstat) 1914 nd->nd_repstat = getret; 1915 if (nd->nd_repstat) { 1916 vput(vp); 1917 if (cookies) 1918 free((caddr_t)cookies, M_TEMP); 1919 free((caddr_t)rbuf, M_TEMP); 1920 if (nd->nd_flag & ND_NFSV3) 1921 nfsrv_postopattr(nd, getret, &at); 1922 goto out; 1923 } 1924 /* 1925 * If nothing read, return eof 1926 * rpc reply 1927 */ 1928 if (siz == 0) { 1929 vput(vp); 1930 if (nd->nd_flag & ND_NFSV3) 1931 nfsrv_postopattr(nd, getret, &at); 1932 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1933 txdr_hyper(at.na_filerev, tl); 1934 tl += 2; 1935 *tl++ = newnfs_false; 1936 *tl = newnfs_true; 1937 free((caddr_t)cookies, M_TEMP); 1938 free((caddr_t)rbuf, M_TEMP); 1939 goto out; 1940 } 1941 1942 /* 1943 * Check for degenerate cases of nothing useful read. 1944 * If so go try again 1945 */ 1946 cpos = rbuf; 1947 cend = rbuf + siz; 1948 dp = (struct dirent *)cpos; 1949 cookiep = cookies; 1950 1951 /* 1952 * For some reason FreeBSD's ufs_readdir() chooses to back the 1953 * directory offset up to a block boundary, so it is necessary to 1954 * skip over the records that precede the requested offset. This 1955 * requires the assumption that file offset cookies monotonically 1956 * increase. 1957 * Since the offset cookies don't monotonically increase for ZFS, 1958 * this is not done when ZFS is the file system. 1959 */ 1960 while (cpos < cend && ncookies > 0 && 1961 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1962 (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) || 1963 ((nd->nd_flag & ND_NFSV4) && 1964 ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 1965 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 1966 cpos += dp->d_reclen; 1967 dp = (struct dirent *)cpos; 1968 cookiep++; 1969 ncookies--; 1970 } 1971 if (cpos >= cend || ncookies == 0) { 1972 siz = fullsiz; 1973 toff = off; 1974 goto again; 1975 } 1976 1977 /* 1978 * Busy the file system so that the mount point won't go away 1979 * and, as such, VFS_VGET() can be used safely. 1980 */ 1981 mp = vp->v_mount; 1982 vfs_ref(mp); 1983 NFSVOPUNLOCK(vp, 0); 1984 nd->nd_repstat = vfs_busy(mp, 0); 1985 vfs_rel(mp); 1986 if (nd->nd_repstat != 0) { 1987 vrele(vp); 1988 free(cookies, M_TEMP); 1989 free(rbuf, M_TEMP); 1990 if (nd->nd_flag & ND_NFSV3) 1991 nfsrv_postopattr(nd, getret, &at); 1992 goto out; 1993 } 1994 1995 /* 1996 * Check to see if entries in this directory can be safely acquired 1997 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required. 1998 * ZFS snapshot directories need VOP_LOOKUP(), so that any 1999 * automount of the snapshot directory that is required will 2000 * be done. 2001 * This needs to be done here for NFSv4, since NFSv4 never does 2002 * a VFS_VGET() for "." or "..". 2003 */ 2004 if (not_zfs == 0) { 2005 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp); 2006 if (r == EOPNOTSUPP) { 2007 usevget = 0; 2008 cn.cn_nameiop = LOOKUP; 2009 cn.cn_lkflags = LK_SHARED | LK_RETRY; 2010 cn.cn_cred = nd->nd_cred; 2011 cn.cn_thread = p; 2012 } else if (r == 0) 2013 vput(nvp); 2014 } 2015 2016 /* 2017 * Save this position, in case there is an error before one entry 2018 * is created. 2019 */ 2020 mb0 = nd->nd_mb; 2021 bpos0 = nd->nd_bpos; 2022 2023 /* 2024 * Fill in the first part of the reply. 2025 * dirlen is the reply length in bytes and cannot exceed cnt. 2026 * (Include the two booleans at the end of the reply in dirlen now, 2027 * so we recognize when we have exceeded cnt.) 2028 */ 2029 if (nd->nd_flag & ND_NFSV3) { 2030 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 2031 nfsrv_postopattr(nd, getret, &at); 2032 } else { 2033 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 2034 } 2035 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2036 txdr_hyper(at.na_filerev, tl); 2037 2038 /* 2039 * Save this position, in case there is an empty reply needed. 2040 */ 2041 mb1 = nd->nd_mb; 2042 bpos1 = nd->nd_bpos; 2043 2044 /* Loop through the records and build reply */ 2045 entrycnt = 0; 2046 while (cpos < cend && ncookies > 0 && dirlen < cnt) { 2047 nlen = dp->d_namlen; 2048 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2049 nlen <= NFS_MAXNAMLEN && 2050 ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 2051 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 2052 || (nlen == 1 && dp->d_name[0] != '.'))) { 2053 /* 2054 * Save the current position in the reply, in case 2055 * this entry exceeds cnt. 2056 */ 2057 mb1 = nd->nd_mb; 2058 bpos1 = nd->nd_bpos; 2059 2060 /* 2061 * For readdir_and_lookup get the vnode using 2062 * the file number. 2063 */ 2064 nvp = NULL; 2065 refp = NULL; 2066 r = 0; 2067 at_root = 0; 2068 needs_unbusy = 0; 2069 new_mp = mp; 2070 mounted_on_fileno = (uint64_t)dp->d_fileno; 2071 if ((nd->nd_flag & ND_NFSV3) || 2072 NFSNONZERO_ATTRBIT(&savbits)) { 2073 if (nd->nd_flag & ND_NFSV4) 2074 refp = nfsv4root_getreferral(NULL, 2075 vp, dp->d_fileno); 2076 if (refp == NULL) { 2077 if (usevget) 2078 r = VFS_VGET(mp, dp->d_fileno, 2079 LK_SHARED, &nvp); 2080 else 2081 r = EOPNOTSUPP; 2082 if (r == EOPNOTSUPP) { 2083 if (usevget) { 2084 usevget = 0; 2085 cn.cn_nameiop = LOOKUP; 2086 cn.cn_lkflags = 2087 LK_SHARED | 2088 LK_RETRY; 2089 cn.cn_cred = 2090 nd->nd_cred; 2091 cn.cn_thread = p; 2092 } 2093 cn.cn_nameptr = dp->d_name; 2094 cn.cn_namelen = nlen; 2095 cn.cn_flags = ISLASTCN | 2096 NOFOLLOW | LOCKLEAF; 2097 if (nlen == 2 && 2098 dp->d_name[0] == '.' && 2099 dp->d_name[1] == '.') 2100 cn.cn_flags |= 2101 ISDOTDOT; 2102 if (NFSVOPLOCK(vp, LK_SHARED) 2103 != 0) { 2104 nd->nd_repstat = EPERM; 2105 break; 2106 } 2107 if ((vp->v_vflag & VV_ROOT) != 0 2108 && (cn.cn_flags & ISDOTDOT) 2109 != 0) { 2110 vref(vp); 2111 nvp = vp; 2112 r = 0; 2113 } else { 2114 r = VOP_LOOKUP(vp, &nvp, 2115 &cn); 2116 if (vp != nvp) 2117 NFSVOPUNLOCK(vp, 2118 0); 2119 } 2120 } 2121 2122 /* 2123 * For NFSv4, check to see if nvp is 2124 * a mount point and get the mount 2125 * point vnode, as required. 2126 */ 2127 if (r == 0 && 2128 nfsrv_enable_crossmntpt != 0 && 2129 (nd->nd_flag & ND_NFSV4) != 0 && 2130 nvp->v_type == VDIR && 2131 nvp->v_mountedhere != NULL) { 2132 new_mp = nvp->v_mountedhere; 2133 r = vfs_busy(new_mp, 0); 2134 vput(nvp); 2135 nvp = NULL; 2136 if (r == 0) { 2137 r = VFS_ROOT(new_mp, 2138 LK_SHARED, &nvp); 2139 needs_unbusy = 1; 2140 if (r == 0) 2141 at_root = 1; 2142 } 2143 } 2144 } 2145 if (!r) { 2146 if (refp == NULL && 2147 ((nd->nd_flag & ND_NFSV3) || 2148 NFSNONZERO_ATTRBIT(&attrbits))) { 2149 r = nfsvno_getfh(nvp, &nfh, p); 2150 if (!r) 2151 r = nfsvno_getattr(nvp, nvap, 2152 nd->nd_cred, p, 1); 2153 if (r == 0 && not_zfs == 0 && 2154 nfsrv_enable_crossmntpt != 0 && 2155 (nd->nd_flag & ND_NFSV4) != 0 && 2156 nvp->v_type == VDIR && 2157 vp->v_mount != nvp->v_mount) { 2158 /* 2159 * For a ZFS snapshot, there is a 2160 * pseudo mount that does not set 2161 * v_mountedhere, so it needs to 2162 * be detected via a different 2163 * mount structure. 2164 */ 2165 at_root = 1; 2166 if (new_mp == mp) 2167 new_mp = nvp->v_mount; 2168 } 2169 } 2170 } else { 2171 nvp = NULL; 2172 } 2173 if (r) { 2174 if (!NFSISSET_ATTRBIT(&attrbits, 2175 NFSATTRBIT_RDATTRERROR)) { 2176 if (nvp != NULL) 2177 vput(nvp); 2178 if (needs_unbusy != 0) 2179 vfs_unbusy(new_mp); 2180 nd->nd_repstat = r; 2181 break; 2182 } 2183 } 2184 } 2185 2186 /* 2187 * Build the directory record xdr 2188 */ 2189 if (nd->nd_flag & ND_NFSV3) { 2190 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2191 *tl++ = newnfs_true; 2192 *tl++ = 0; 2193 *tl = txdr_unsigned(dp->d_fileno); 2194 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2195 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2196 *tl++ = 0; 2197 *tl = txdr_unsigned(*cookiep); 2198 nfsrv_postopattr(nd, 0, nvap); 2199 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1); 2200 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 2201 if (nvp != NULL) 2202 vput(nvp); 2203 } else { 2204 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2205 *tl++ = newnfs_true; 2206 *tl++ = 0; 2207 *tl = txdr_unsigned(*cookiep); 2208 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2209 if (nvp != NULL) { 2210 supports_nfsv4acls = 2211 nfs_supportsnfsv4acls(nvp); 2212 NFSVOPUNLOCK(nvp, 0); 2213 } else 2214 supports_nfsv4acls = 0; 2215 if (refp != NULL) { 2216 dirlen += nfsrv_putreferralattr(nd, 2217 &savbits, refp, 0, 2218 &nd->nd_repstat); 2219 if (nd->nd_repstat) { 2220 if (nvp != NULL) 2221 vrele(nvp); 2222 if (needs_unbusy != 0) 2223 vfs_unbusy(new_mp); 2224 break; 2225 } 2226 } else if (r) { 2227 dirlen += nfsvno_fillattr(nd, new_mp, 2228 nvp, nvap, &nfh, r, &rderrbits, 2229 nd->nd_cred, p, isdgram, 0, 2230 supports_nfsv4acls, at_root, 2231 mounted_on_fileno); 2232 } else { 2233 dirlen += nfsvno_fillattr(nd, new_mp, 2234 nvp, nvap, &nfh, r, &attrbits, 2235 nd->nd_cred, p, isdgram, 0, 2236 supports_nfsv4acls, at_root, 2237 mounted_on_fileno); 2238 } 2239 if (nvp != NULL) 2240 vrele(nvp); 2241 dirlen += (3 * NFSX_UNSIGNED); 2242 } 2243 if (needs_unbusy != 0) 2244 vfs_unbusy(new_mp); 2245 if (dirlen <= cnt) 2246 entrycnt++; 2247 } 2248 cpos += dp->d_reclen; 2249 dp = (struct dirent *)cpos; 2250 cookiep++; 2251 ncookies--; 2252 } 2253 vrele(vp); 2254 vfs_unbusy(mp); 2255 2256 /* 2257 * If dirlen > cnt, we must strip off the last entry. If that 2258 * results in an empty reply, report NFSERR_TOOSMALL. 2259 */ 2260 if (dirlen > cnt || nd->nd_repstat) { 2261 if (!nd->nd_repstat && entrycnt == 0) 2262 nd->nd_repstat = NFSERR_TOOSMALL; 2263 if (nd->nd_repstat) 2264 newnfs_trimtrailing(nd, mb0, bpos0); 2265 else 2266 newnfs_trimtrailing(nd, mb1, bpos1); 2267 eofflag = 0; 2268 } else if (cpos < cend) 2269 eofflag = 0; 2270 if (!nd->nd_repstat) { 2271 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2272 *tl++ = newnfs_false; 2273 if (eofflag) 2274 *tl = newnfs_true; 2275 else 2276 *tl = newnfs_false; 2277 } 2278 FREE((caddr_t)cookies, M_TEMP); 2279 FREE((caddr_t)rbuf, M_TEMP); 2280 2281out: 2282 NFSEXITCODE2(0, nd); 2283 return (0); 2284nfsmout: 2285 vput(vp); 2286 NFSEXITCODE2(error, nd); 2287 return (error); 2288} 2289 2290/* 2291 * Get the settable attributes out of the mbuf list. 2292 * (Return 0 or EBADRPC) 2293 */ 2294int 2295nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2296 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2297{ 2298 u_int32_t *tl; 2299 struct nfsv2_sattr *sp; 2300 int error = 0, toclient = 0; 2301 2302 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 2303 case ND_NFSV2: 2304 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2305 /* 2306 * Some old clients didn't fill in the high order 16bits. 2307 * --> check the low order 2 bytes for 0xffff 2308 */ 2309 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 2310 nvap->na_mode = nfstov_mode(sp->sa_mode); 2311 if (sp->sa_uid != newnfs_xdrneg1) 2312 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 2313 if (sp->sa_gid != newnfs_xdrneg1) 2314 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 2315 if (sp->sa_size != newnfs_xdrneg1) 2316 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 2317 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 2318#ifdef notyet 2319 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 2320#else 2321 nvap->na_atime.tv_sec = 2322 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 2323 nvap->na_atime.tv_nsec = 0; 2324#endif 2325 } 2326 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 2327 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 2328 break; 2329 case ND_NFSV3: 2330 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2331 if (*tl == newnfs_true) { 2332 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2333 nvap->na_mode = nfstov_mode(*tl); 2334 } 2335 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2336 if (*tl == newnfs_true) { 2337 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2338 nvap->na_uid = fxdr_unsigned(uid_t, *tl); 2339 } 2340 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2341 if (*tl == newnfs_true) { 2342 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2343 nvap->na_gid = fxdr_unsigned(gid_t, *tl); 2344 } 2345 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2346 if (*tl == newnfs_true) { 2347 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2348 nvap->na_size = fxdr_hyper(tl); 2349 } 2350 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2351 switch (fxdr_unsigned(int, *tl)) { 2352 case NFSV3SATTRTIME_TOCLIENT: 2353 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2354 fxdr_nfsv3time(tl, &nvap->na_atime); 2355 toclient = 1; 2356 break; 2357 case NFSV3SATTRTIME_TOSERVER: 2358 vfs_timestamp(&nvap->na_atime); 2359 nvap->na_vaflags |= VA_UTIMES_NULL; 2360 break; 2361 }; 2362 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2363 switch (fxdr_unsigned(int, *tl)) { 2364 case NFSV3SATTRTIME_TOCLIENT: 2365 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2366 fxdr_nfsv3time(tl, &nvap->na_mtime); 2367 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2368 break; 2369 case NFSV3SATTRTIME_TOSERVER: 2370 vfs_timestamp(&nvap->na_mtime); 2371 if (!toclient) 2372 nvap->na_vaflags |= VA_UTIMES_NULL; 2373 break; 2374 }; 2375 break; 2376 case ND_NFSV4: 2377 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p); 2378 }; 2379nfsmout: 2380 NFSEXITCODE2(error, nd); 2381 return (error); 2382} 2383 2384/* 2385 * Handle the setable attributes for V4. 2386 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 2387 */ 2388int 2389nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap, 2390 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2391{ 2392 u_int32_t *tl; 2393 int attrsum = 0; 2394 int i, j; 2395 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 2396 int toclient = 0; 2397 u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 2398 uid_t uid; 2399 gid_t gid; 2400 2401 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 2402 if (error) 2403 goto nfsmout; 2404 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2405 attrsize = fxdr_unsigned(int, *tl); 2406 2407 /* 2408 * Loop around getting the setable attributes. If an unsupported 2409 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 2410 */ 2411 if (retnotsup) { 2412 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2413 bitpos = NFSATTRBIT_MAX; 2414 } else { 2415 bitpos = 0; 2416 } 2417 for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 2418 if (attrsum > attrsize) { 2419 error = NFSERR_BADXDR; 2420 goto nfsmout; 2421 } 2422 if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 2423 switch (bitpos) { 2424 case NFSATTRBIT_SIZE: 2425 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2426 nvap->na_size = fxdr_hyper(tl); 2427 attrsum += NFSX_HYPER; 2428 break; 2429 case NFSATTRBIT_ACL: 2430 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize, 2431 p); 2432 if (error) 2433 goto nfsmout; 2434 if (aceerr && !nd->nd_repstat) 2435 nd->nd_repstat = aceerr; 2436 attrsum += aclsize; 2437 break; 2438 case NFSATTRBIT_ARCHIVE: 2439 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2440 if (!nd->nd_repstat) 2441 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2442 attrsum += NFSX_UNSIGNED; 2443 break; 2444 case NFSATTRBIT_HIDDEN: 2445 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2446 if (!nd->nd_repstat) 2447 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2448 attrsum += NFSX_UNSIGNED; 2449 break; 2450 case NFSATTRBIT_MIMETYPE: 2451 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2452 i = fxdr_unsigned(int, *tl); 2453 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 2454 if (error) 2455 goto nfsmout; 2456 if (!nd->nd_repstat) 2457 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2458 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 2459 break; 2460 case NFSATTRBIT_MODE: 2461 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2462 nvap->na_mode = nfstov_mode(*tl); 2463 attrsum += NFSX_UNSIGNED; 2464 break; 2465 case NFSATTRBIT_OWNER: 2466 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2467 j = fxdr_unsigned(int, *tl); 2468 if (j < 0) { 2469 error = NFSERR_BADXDR; 2470 goto nfsmout; 2471 } 2472 if (j > NFSV4_SMALLSTR) 2473 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2474 else 2475 cp = namestr; 2476 error = nfsrv_mtostr(nd, cp, j); 2477 if (error) { 2478 if (j > NFSV4_SMALLSTR) 2479 free(cp, M_NFSSTRING); 2480 goto nfsmout; 2481 } 2482 if (!nd->nd_repstat) { 2483 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid, 2484 p); 2485 if (!nd->nd_repstat) 2486 nvap->na_uid = uid; 2487 } 2488 if (j > NFSV4_SMALLSTR) 2489 free(cp, M_NFSSTRING); 2490 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2491 break; 2492 case NFSATTRBIT_OWNERGROUP: 2493 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2494 j = fxdr_unsigned(int, *tl); 2495 if (j < 0) { 2496 error = NFSERR_BADXDR; 2497 goto nfsmout; 2498 } 2499 if (j > NFSV4_SMALLSTR) 2500 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2501 else 2502 cp = namestr; 2503 error = nfsrv_mtostr(nd, cp, j); 2504 if (error) { 2505 if (j > NFSV4_SMALLSTR) 2506 free(cp, M_NFSSTRING); 2507 goto nfsmout; 2508 } 2509 if (!nd->nd_repstat) { 2510 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid, 2511 p); 2512 if (!nd->nd_repstat) 2513 nvap->na_gid = gid; 2514 } 2515 if (j > NFSV4_SMALLSTR) 2516 free(cp, M_NFSSTRING); 2517 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2518 break; 2519 case NFSATTRBIT_SYSTEM: 2520 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2521 if (!nd->nd_repstat) 2522 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2523 attrsum += NFSX_UNSIGNED; 2524 break; 2525 case NFSATTRBIT_TIMEACCESSSET: 2526 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2527 attrsum += NFSX_UNSIGNED; 2528 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2529 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2530 fxdr_nfsv4time(tl, &nvap->na_atime); 2531 toclient = 1; 2532 attrsum += NFSX_V4TIME; 2533 } else { 2534 vfs_timestamp(&nvap->na_atime); 2535 nvap->na_vaflags |= VA_UTIMES_NULL; 2536 } 2537 break; 2538 case NFSATTRBIT_TIMEBACKUP: 2539 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2540 if (!nd->nd_repstat) 2541 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2542 attrsum += NFSX_V4TIME; 2543 break; 2544 case NFSATTRBIT_TIMECREATE: 2545 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2546 if (!nd->nd_repstat) 2547 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2548 attrsum += NFSX_V4TIME; 2549 break; 2550 case NFSATTRBIT_TIMEMODIFYSET: 2551 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2552 attrsum += NFSX_UNSIGNED; 2553 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2554 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2555 fxdr_nfsv4time(tl, &nvap->na_mtime); 2556 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2557 attrsum += NFSX_V4TIME; 2558 } else { 2559 vfs_timestamp(&nvap->na_mtime); 2560 if (!toclient) 2561 nvap->na_vaflags |= VA_UTIMES_NULL; 2562 } 2563 break; 2564 default: 2565 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2566 /* 2567 * set bitpos so we drop out of the loop. 2568 */ 2569 bitpos = NFSATTRBIT_MAX; 2570 break; 2571 }; 2572 } 2573 2574 /* 2575 * some clients pad the attrlist, so we need to skip over the 2576 * padding. 2577 */ 2578 if (attrsum > attrsize) { 2579 error = NFSERR_BADXDR; 2580 } else { 2581 attrsize = NFSM_RNDUP(attrsize); 2582 if (attrsum < attrsize) 2583 error = nfsm_advance(nd, attrsize - attrsum, -1); 2584 } 2585nfsmout: 2586 NFSEXITCODE2(error, nd); 2587 return (error); 2588} 2589 2590/* 2591 * Check/setup export credentials. 2592 */ 2593int 2594nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 2595 struct ucred *credanon) 2596{ 2597 int error = 0; 2598 2599 /* 2600 * Check/setup credentials. 2601 */ 2602 if (nd->nd_flag & ND_GSS) 2603 exp->nes_exflag &= ~MNT_EXPORTANON; 2604 2605 /* 2606 * Check to see if the operation is allowed for this security flavor. 2607 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to 2608 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 2609 * Also, allow Secinfo, so that it can acquire the correct flavor(s). 2610 */ 2611 if (nfsvno_testexp(nd, exp) && 2612 nd->nd_procnum != NFSV4OP_SECINFO && 2613 nd->nd_procnum != NFSPROC_FSINFO) { 2614 if (nd->nd_flag & ND_NFSV4) 2615 error = NFSERR_WRONGSEC; 2616 else 2617 error = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2618 goto out; 2619 } 2620 2621 /* 2622 * Check to see if the file system is exported V4 only. 2623 */ 2624 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) { 2625 error = NFSERR_PROGNOTV4; 2626 goto out; 2627 } 2628 2629 /* 2630 * Now, map the user credentials. 2631 * (Note that ND_AUTHNONE will only be set for an NFSv3 2632 * Fsinfo RPC. If set for anything else, this code might need 2633 * to change.) 2634 */ 2635 if (NFSVNO_EXPORTED(exp) && 2636 ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) || 2637 NFSVNO_EXPORTANON(exp) || 2638 (nd->nd_flag & ND_AUTHNONE))) { 2639 nd->nd_cred->cr_uid = credanon->cr_uid; 2640 nd->nd_cred->cr_gid = credanon->cr_gid; 2641 crsetgroups(nd->nd_cred, credanon->cr_ngroups, 2642 credanon->cr_groups); 2643 } 2644 2645out: 2646 NFSEXITCODE2(error, nd); 2647 return (error); 2648} 2649 2650/* 2651 * Check exports. 2652 */ 2653int 2654nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 2655 struct ucred **credp) 2656{ 2657 int i, error, *secflavors; 2658 2659 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2660 &exp->nes_numsecflavor, &secflavors); 2661 if (error) { 2662 if (nfs_rootfhset) { 2663 exp->nes_exflag = 0; 2664 exp->nes_numsecflavor = 0; 2665 error = 0; 2666 } 2667 } else { 2668 /* Copy the security flavors. */ 2669 for (i = 0; i < exp->nes_numsecflavor; i++) 2670 exp->nes_secflavors[i] = secflavors[i]; 2671 } 2672 NFSEXITCODE(error); 2673 return (error); 2674} 2675 2676/* 2677 * Get a vnode for a file handle and export stuff. 2678 */ 2679int 2680nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 2681 int lktype, struct vnode **vpp, struct nfsexstuff *exp, 2682 struct ucred **credp) 2683{ 2684 int i, error, *secflavors; 2685 2686 *credp = NULL; 2687 exp->nes_numsecflavor = 0; 2688 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp); 2689 if (error != 0) 2690 /* Make sure the server replies ESTALE to the client. */ 2691 error = ESTALE; 2692 if (nam && !error) { 2693 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2694 &exp->nes_numsecflavor, &secflavors); 2695 if (error) { 2696 if (nfs_rootfhset) { 2697 exp->nes_exflag = 0; 2698 exp->nes_numsecflavor = 0; 2699 error = 0; 2700 } else { 2701 vput(*vpp); 2702 } 2703 } else { 2704 /* Copy the security flavors. */ 2705 for (i = 0; i < exp->nes_numsecflavor; i++) 2706 exp->nes_secflavors[i] = secflavors[i]; 2707 } 2708 } 2709 NFSEXITCODE(error); 2710 return (error); 2711} 2712 2713/* 2714 * nfsd_fhtovp() - convert a fh to a vnode ptr 2715 * - look up fsid in mount list (if not found ret error) 2716 * - get vp and export rights by calling nfsvno_fhtovp() 2717 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 2718 * for AUTH_SYS 2719 * - if mpp != NULL, return the mount point so that it can 2720 * be used for vn_finished_write() by the caller 2721 */ 2722void 2723nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 2724 struct vnode **vpp, struct nfsexstuff *exp, 2725 struct mount **mpp, int startwrite, struct thread *p) 2726{ 2727 struct mount *mp; 2728 struct ucred *credanon; 2729 fhandle_t *fhp; 2730 2731 fhp = (fhandle_t *)nfp->nfsrvfh_data; 2732 /* 2733 * Check for the special case of the nfsv4root_fh. 2734 */ 2735 mp = vfs_busyfs(&fhp->fh_fsid); 2736 if (mpp != NULL) 2737 *mpp = mp; 2738 if (mp == NULL) { 2739 *vpp = NULL; 2740 nd->nd_repstat = ESTALE; 2741 goto out; 2742 } 2743 2744 if (startwrite) { 2745 vn_start_write(NULL, mpp, V_WAIT); 2746 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp))) 2747 lktype = LK_EXCLUSIVE; 2748 } 2749 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 2750 &credanon); 2751 vfs_unbusy(mp); 2752 2753 /* 2754 * For NFSv4 without a pseudo root fs, unexported file handles 2755 * can be returned, so that Lookup works everywhere. 2756 */ 2757 if (!nd->nd_repstat && exp->nes_exflag == 0 && 2758 !(nd->nd_flag & ND_NFSV4)) { 2759 vput(*vpp); 2760 nd->nd_repstat = EACCES; 2761 } 2762 2763 /* 2764 * Personally, I've never seen any point in requiring a 2765 * reserved port#, since only in the rare case where the 2766 * clients are all boxes with secure system priviledges, 2767 * does it provide any enhanced security, but... some people 2768 * believe it to be useful and keep putting this code back in. 2769 * (There is also some "security checker" out there that 2770 * complains if the nfs server doesn't enforce this.) 2771 * However, note the following: 2772 * RFC3530 (NFSv4) specifies that a reserved port# not be 2773 * required. 2774 * RFC2623 recommends that, if a reserved port# is checked for, 2775 * that there be a way to turn that off--> ifdef'd. 2776 */ 2777#ifdef NFS_REQRSVPORT 2778 if (!nd->nd_repstat) { 2779 struct sockaddr_in *saddr; 2780 struct sockaddr_in6 *saddr6; 2781 2782 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 2783 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 2784 if (!(nd->nd_flag & ND_NFSV4) && 2785 ((saddr->sin_family == AF_INET && 2786 ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 2787 (saddr6->sin6_family == AF_INET6 && 2788 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 2789 vput(*vpp); 2790 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2791 } 2792 } 2793#endif /* NFS_REQRSVPORT */ 2794 2795 /* 2796 * Check/setup credentials. 2797 */ 2798 if (!nd->nd_repstat) { 2799 nd->nd_saveduid = nd->nd_cred->cr_uid; 2800 nd->nd_repstat = nfsd_excred(nd, exp, credanon); 2801 if (nd->nd_repstat) 2802 vput(*vpp); 2803 } 2804 if (credanon != NULL) 2805 crfree(credanon); 2806 if (nd->nd_repstat) { 2807 if (startwrite) 2808 vn_finished_write(mp); 2809 *vpp = NULL; 2810 if (mpp != NULL) 2811 *mpp = NULL; 2812 } 2813 2814out: 2815 NFSEXITCODE2(0, nd); 2816} 2817 2818/* 2819 * glue for fp. 2820 */ 2821static int 2822fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 2823{ 2824 struct filedesc *fdp; 2825 struct file *fp; 2826 int error = 0; 2827 2828 fdp = p->td_proc->p_fd; 2829 if (fd < 0 || fd >= fdp->fd_nfiles || 2830 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) { 2831 error = EBADF; 2832 goto out; 2833 } 2834 *fpp = fp; 2835 2836out: 2837 NFSEXITCODE(error); 2838 return (error); 2839} 2840 2841/* 2842 * Called from nfssvc() to update the exports list. Just call 2843 * vfs_export(). This has to be done, since the v4 root fake fs isn't 2844 * in the mount list. 2845 */ 2846int 2847nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 2848{ 2849 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 2850 int error = 0; 2851 struct nameidata nd; 2852 fhandle_t fh; 2853 2854 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export); 2855 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 2856 nfs_rootfhset = 0; 2857 else if (error == 0) { 2858 if (nfsexargp->fspec == NULL) { 2859 error = EPERM; 2860 goto out; 2861 } 2862 /* 2863 * If fspec != NULL, this is the v4root path. 2864 */ 2865 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, 2866 nfsexargp->fspec, p); 2867 if ((error = namei(&nd)) != 0) 2868 goto out; 2869 error = nfsvno_getfh(nd.ni_vp, &fh, p); 2870 vrele(nd.ni_vp); 2871 if (!error) { 2872 nfs_rootfh.nfsrvfh_len = NFSX_MYFH; 2873 NFSBCOPY((caddr_t)&fh, 2874 nfs_rootfh.nfsrvfh_data, 2875 sizeof (fhandle_t)); 2876 nfs_rootfhset = 1; 2877 } 2878 } 2879 2880out: 2881 NFSEXITCODE(error); 2882 return (error); 2883} 2884 2885/* 2886 * This function needs to test to see if the system is near its limit 2887 * for memory allocation via malloc() or mget() and return True iff 2888 * either of these resources are near their limit. 2889 * XXX (For now, this is just a stub.) 2890 */ 2891int nfsrv_testmalloclimit = 0; 2892int 2893nfsrv_mallocmget_limit(void) 2894{ 2895 static int printmesg = 0; 2896 static int testval = 1; 2897 2898 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 2899 if ((printmesg++ % 100) == 0) 2900 printf("nfsd: malloc/mget near limit\n"); 2901 return (1); 2902 } 2903 return (0); 2904} 2905 2906/* 2907 * BSD specific initialization of a mount point. 2908 */ 2909void 2910nfsd_mntinit(void) 2911{ 2912 static int inited = 0; 2913 2914 if (inited) 2915 return; 2916 inited = 1; 2917 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 2918 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist); 2919 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist); 2920 nfsv4root_mnt.mnt_export = NULL; 2921 TAILQ_INIT(&nfsv4root_opt); 2922 TAILQ_INIT(&nfsv4root_newopt); 2923 nfsv4root_mnt.mnt_opt = &nfsv4root_opt; 2924 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt; 2925 nfsv4root_mnt.mnt_nvnodelistsize = 0; 2926 nfsv4root_mnt.mnt_activevnodelistsize = 0; 2927} 2928 2929/* 2930 * Get a vnode for a file handle, without checking exports, etc. 2931 */ 2932struct vnode * 2933nfsvno_getvp(fhandle_t *fhp) 2934{ 2935 struct mount *mp; 2936 struct vnode *vp; 2937 int error; 2938 2939 mp = vfs_busyfs(&fhp->fh_fsid); 2940 if (mp == NULL) 2941 return (NULL); 2942 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 2943 vfs_unbusy(mp); 2944 if (error) 2945 return (NULL); 2946 return (vp); 2947} 2948 2949/* 2950 * Do a local VOP_ADVLOCK(). 2951 */ 2952int 2953nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 2954 u_int64_t end, struct thread *td) 2955{ 2956 int error = 0; 2957 struct flock fl; 2958 u_int64_t tlen; 2959 2960 if (nfsrv_dolocallocks == 0) 2961 goto out; 2962 2963 /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */ 2964 if ((vp->v_iflag & VI_DOOMED) != 0) { 2965 error = EPERM; 2966 goto out; 2967 } 2968 2969 fl.l_whence = SEEK_SET; 2970 fl.l_type = ftype; 2971 fl.l_start = (off_t)first; 2972 if (end == NFS64BITSSET) { 2973 fl.l_len = 0; 2974 } else { 2975 tlen = end - first; 2976 fl.l_len = (off_t)tlen; 2977 } 2978 /* 2979 * For FreeBSD8, the l_pid and l_sysid must be set to the same 2980 * values for all calls, so that all locks will be held by the 2981 * nfsd server. (The nfsd server handles conflicts between the 2982 * various clients.) 2983 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 2984 * bytes, so it can't be put in l_sysid. 2985 */ 2986 if (nfsv4_sysid == 0) 2987 nfsv4_sysid = nlm_acquire_next_sysid(); 2988 fl.l_pid = (pid_t)0; 2989 fl.l_sysid = (int)nfsv4_sysid; 2990 2991 NFSVOPUNLOCK(vp, 0); 2992 if (ftype == F_UNLCK) 2993 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 2994 (F_POSIX | F_REMOTE)); 2995 else 2996 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 2997 (F_POSIX | F_REMOTE)); 2998 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 2999 3000out: 3001 NFSEXITCODE(error); 3002 return (error); 3003} 3004 3005/* 3006 * Check the nfsv4 root exports. 3007 */ 3008int 3009nfsvno_v4rootexport(struct nfsrv_descript *nd) 3010{ 3011 struct ucred *credanon; 3012 int exflags, error = 0, numsecflavor, *secflavors, i; 3013 3014 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags, 3015 &credanon, &numsecflavor, &secflavors); 3016 if (error) { 3017 error = NFSERR_PROGUNAVAIL; 3018 goto out; 3019 } 3020 if (credanon != NULL) 3021 crfree(credanon); 3022 for (i = 0; i < numsecflavor; i++) { 3023 if (secflavors[i] == AUTH_SYS) 3024 nd->nd_flag |= ND_EXAUTHSYS; 3025 else if (secflavors[i] == RPCSEC_GSS_KRB5) 3026 nd->nd_flag |= ND_EXGSS; 3027 else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3028 nd->nd_flag |= ND_EXGSSINTEGRITY; 3029 else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3030 nd->nd_flag |= ND_EXGSSPRIVACY; 3031 } 3032 3033out: 3034 NFSEXITCODE(error); 3035 return (error); 3036} 3037 3038/* 3039 * Nfs server psuedo system call for the nfsd's 3040 */ 3041/* 3042 * MPSAFE 3043 */ 3044static int 3045nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3046{ 3047 struct file *fp; 3048 struct nfsd_addsock_args sockarg; 3049 struct nfsd_nfsd_args nfsdarg; 3050 cap_rights_t rights; 3051 int error; 3052 3053 if (uap->flag & NFSSVC_NFSDADDSOCK) { 3054 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3055 if (error) 3056 goto out; 3057 /* 3058 * Since we don't know what rights might be required, 3059 * pretend that we need them all. It is better to be too 3060 * careful than too reckless. 3061 */ 3062 error = fget(td, sockarg.sock, 3063 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp); 3064 if (error != 0) 3065 goto out; 3066 if (fp->f_type != DTYPE_SOCKET) { 3067 fdrop(fp, td); 3068 error = EPERM; 3069 goto out; 3070 } 3071 error = nfsrvd_addsock(fp); 3072 fdrop(fp, td); 3073 } else if (uap->flag & NFSSVC_NFSDNFSD) { 3074 if (uap->argp == NULL) { 3075 error = EINVAL; 3076 goto out; 3077 } 3078 error = copyin(uap->argp, (caddr_t)&nfsdarg, 3079 sizeof (nfsdarg)); 3080 if (error) 3081 goto out; 3082 error = nfsrvd_nfsd(td, &nfsdarg); 3083 } else { 3084 error = nfssvc_srvcall(td, uap, td->td_ucred); 3085 } 3086 3087out: 3088 NFSEXITCODE(error); 3089 return (error); 3090} 3091 3092static int 3093nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 3094{ 3095 struct nfsex_args export; 3096 struct file *fp = NULL; 3097 int stablefd, len; 3098 struct nfsd_clid adminrevoke; 3099 struct nfsd_dumplist dumplist; 3100 struct nfsd_dumpclients *dumpclients; 3101 struct nfsd_dumplocklist dumplocklist; 3102 struct nfsd_dumplocks *dumplocks; 3103 struct nameidata nd; 3104 vnode_t vp; 3105 int error = EINVAL, igotlock; 3106 struct proc *procp; 3107 static int suspend_nfsd = 0; 3108 3109 if (uap->flag & NFSSVC_PUBLICFH) { 3110 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 3111 sizeof (fhandle_t)); 3112 error = copyin(uap->argp, 3113 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 3114 if (!error) 3115 nfs_pubfhset = 1; 3116 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) { 3117 error = copyin(uap->argp,(caddr_t)&export, 3118 sizeof (struct nfsex_args)); 3119 if (!error) 3120 error = nfsrv_v4rootexport(&export, cred, p); 3121 } else if (uap->flag & NFSSVC_NOPUBLICFH) { 3122 nfs_pubfhset = 0; 3123 error = 0; 3124 } else if (uap->flag & NFSSVC_STABLERESTART) { 3125 error = copyin(uap->argp, (caddr_t)&stablefd, 3126 sizeof (int)); 3127 if (!error) 3128 error = fp_getfvp(p, stablefd, &fp, &vp); 3129 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 3130 error = EBADF; 3131 if (!error && newnfs_numnfsd != 0) 3132 error = EPERM; 3133 if (!error) { 3134 nfsrv_stablefirst.nsf_fp = fp; 3135 nfsrv_setupstable(p); 3136 } 3137 } else if (uap->flag & NFSSVC_ADMINREVOKE) { 3138 error = copyin(uap->argp, (caddr_t)&adminrevoke, 3139 sizeof (struct nfsd_clid)); 3140 if (!error) 3141 error = nfsrv_adminrevoke(&adminrevoke, p); 3142 } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 3143 error = copyin(uap->argp, (caddr_t)&dumplist, 3144 sizeof (struct nfsd_dumplist)); 3145 if (!error && (dumplist.ndl_size < 1 || 3146 dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 3147 error = EPERM; 3148 if (!error) { 3149 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 3150 dumpclients = (struct nfsd_dumpclients *)malloc(len, 3151 M_TEMP, M_WAITOK); 3152 nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 3153 error = copyout(dumpclients, 3154 CAST_USER_ADDR_T(dumplist.ndl_list), len); 3155 free((caddr_t)dumpclients, M_TEMP); 3156 } 3157 } else if (uap->flag & NFSSVC_DUMPLOCKS) { 3158 error = copyin(uap->argp, (caddr_t)&dumplocklist, 3159 sizeof (struct nfsd_dumplocklist)); 3160 if (!error && (dumplocklist.ndllck_size < 1 || 3161 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 3162 error = EPERM; 3163 if (!error) 3164 error = nfsrv_lookupfilename(&nd, 3165 dumplocklist.ndllck_fname, p); 3166 if (!error) { 3167 len = sizeof (struct nfsd_dumplocks) * 3168 dumplocklist.ndllck_size; 3169 dumplocks = (struct nfsd_dumplocks *)malloc(len, 3170 M_TEMP, M_WAITOK); 3171 nfsrv_dumplocks(nd.ni_vp, dumplocks, 3172 dumplocklist.ndllck_size, p); 3173 vput(nd.ni_vp); 3174 error = copyout(dumplocks, 3175 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len); 3176 free((caddr_t)dumplocks, M_TEMP); 3177 } 3178 } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 3179 procp = p->td_proc; 3180 PROC_LOCK(procp); 3181 nfsd_master_pid = procp->p_pid; 3182 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 3183 nfsd_master_start = procp->p_stats->p_start; 3184 nfsd_master_proc = procp; 3185 PROC_UNLOCK(procp); 3186 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) { 3187 NFSLOCKV4ROOTMUTEX(); 3188 if (suspend_nfsd == 0) { 3189 /* Lock out all nfsd threads */ 3190 do { 3191 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1, 3192 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 3193 } while (igotlock == 0 && suspend_nfsd == 0); 3194 suspend_nfsd = 1; 3195 } 3196 NFSUNLOCKV4ROOTMUTEX(); 3197 error = 0; 3198 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) { 3199 NFSLOCKV4ROOTMUTEX(); 3200 if (suspend_nfsd != 0) { 3201 nfsv4_unlock(&nfsd_suspend_lock, 0); 3202 suspend_nfsd = 0; 3203 } 3204 NFSUNLOCKV4ROOTMUTEX(); 3205 error = 0; 3206 } 3207 3208 NFSEXITCODE(error); 3209 return (error); 3210} 3211 3212/* 3213 * Check exports. 3214 * Returns 0 if ok, 1 otherwise. 3215 */ 3216int 3217nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 3218{ 3219 int i; 3220 3221 /* 3222 * This seems odd, but allow the case where the security flavor 3223 * list is empty. This happens when NFSv4 is traversing non-exported 3224 * file systems. Exported file systems should always have a non-empty 3225 * security flavor list. 3226 */ 3227 if (exp->nes_numsecflavor == 0) 3228 return (0); 3229 3230 for (i = 0; i < exp->nes_numsecflavor; i++) { 3231 /* 3232 * The tests for privacy and integrity must be first, 3233 * since ND_GSS is set for everything but AUTH_SYS. 3234 */ 3235 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 3236 (nd->nd_flag & ND_GSSPRIVACY)) 3237 return (0); 3238 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 3239 (nd->nd_flag & ND_GSSINTEGRITY)) 3240 return (0); 3241 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 3242 (nd->nd_flag & ND_GSS)) 3243 return (0); 3244 if (exp->nes_secflavors[i] == AUTH_SYS && 3245 (nd->nd_flag & ND_GSS) == 0) 3246 return (0); 3247 } 3248 return (1); 3249} 3250 3251/* 3252 * Calculate a hash value for the fid in a file handle. 3253 */ 3254uint32_t 3255nfsrv_hashfh(fhandle_t *fhp) 3256{ 3257 uint32_t hashval; 3258 3259 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 3260 return (hashval); 3261} 3262 3263/* 3264 * Signal the userland master nfsd to backup the stable restart file. 3265 */ 3266void 3267nfsrv_backupstable(void) 3268{ 3269 struct proc *procp; 3270 3271 if (nfsd_master_proc != NULL) { 3272 procp = pfind(nfsd_master_pid); 3273 /* Try to make sure it is the correct process. */ 3274 if (procp == nfsd_master_proc && 3275 procp->p_stats->p_start.tv_sec == 3276 nfsd_master_start.tv_sec && 3277 procp->p_stats->p_start.tv_usec == 3278 nfsd_master_start.tv_usec && 3279 strcmp(procp->p_comm, nfsd_master_comm) == 0) 3280 kern_psignal(procp, SIGUSR2); 3281 else 3282 nfsd_master_proc = NULL; 3283 3284 if (procp != NULL) 3285 PROC_UNLOCK(procp); 3286 } 3287} 3288 3289extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 3290 3291/* 3292 * Called once to initialize data structures... 3293 */ 3294static int 3295nfsd_modevent(module_t mod, int type, void *data) 3296{ 3297 int error = 0, i; 3298 static int loaded = 0; 3299 3300 switch (type) { 3301 case MOD_LOAD: 3302 if (loaded) 3303 goto out; 3304 newnfs_portinit(); 3305 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 3306 snprintf(nfsrchash_table[i].lock_name, 3307 sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d", 3308 i); 3309 mtx_init(&nfsrchash_table[i].mtx, 3310 nfsrchash_table[i].lock_name, NULL, MTX_DEF); 3311 snprintf(nfsrcahash_table[i].lock_name, 3312 sizeof(nfsrcahash_table[i].lock_name), "nfsrc_tcpa%d", 3313 i); 3314 mtx_init(&nfsrcahash_table[i].mtx, 3315 nfsrcahash_table[i].lock_name, NULL, MTX_DEF); 3316 } 3317 mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF); 3318 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF); 3319 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL, 3320 MTX_DEF); 3321 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0); 3322 nfsrvd_initcache(); 3323 nfsd_init(); 3324 NFSD_LOCK(); 3325 nfsrvd_init(0); 3326 NFSD_UNLOCK(); 3327 nfsd_mntinit(); 3328#ifdef VV_DISABLEDELEG 3329 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 3330 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 3331#endif 3332 nfsd_call_servertimer = nfsrv_servertimer; 3333 nfsd_call_nfsd = nfssvc_nfsd; 3334 loaded = 1; 3335 break; 3336 3337 case MOD_UNLOAD: 3338 if (newnfs_numnfsd != 0) { 3339 error = EBUSY; 3340 break; 3341 } 3342 3343#ifdef VV_DISABLEDELEG 3344 vn_deleg_ops.vndeleg_recall = NULL; 3345 vn_deleg_ops.vndeleg_disable = NULL; 3346#endif 3347 nfsd_call_servertimer = NULL; 3348 nfsd_call_nfsd = NULL; 3349 3350 /* Clean out all NFSv4 state. */ 3351 nfsrv_throwawayallstate(curthread); 3352 3353 /* Clean the NFS server reply cache */ 3354 nfsrvd_cleancache(); 3355 3356 /* Free up the krpc server pool. */ 3357 if (nfsrvd_pool != NULL) 3358 svcpool_destroy(nfsrvd_pool); 3359 3360 /* and get rid of the locks */ 3361 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 3362 mtx_destroy(&nfsrchash_table[i].mtx); 3363 mtx_destroy(&nfsrcahash_table[i].mtx); 3364 } 3365 mtx_destroy(&nfsrc_udpmtx); 3366 mtx_destroy(&nfs_v4root_mutex); 3367 mtx_destroy(&nfsv4root_mnt.mnt_mtx); 3368 lockdestroy(&nfsv4root_mnt.mnt_explock); 3369 loaded = 0; 3370 break; 3371 default: 3372 error = EOPNOTSUPP; 3373 break; 3374 } 3375 3376out: 3377 NFSEXITCODE(error); 3378 return (error); 3379} 3380static moduledata_t nfsd_mod = { 3381 "nfsd", 3382 nfsd_modevent, 3383 NULL, 3384}; 3385DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 3386 3387/* So that loader and kldload(2) can find us, wherever we are.. */ 3388MODULE_VERSION(nfsd, 1); 3389MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 3390MODULE_DEPEND(nfsd, nfslock, 1, 1, 1); 3391MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 3392MODULE_DEPEND(nfsd, krpc, 1, 1, 1); 3393MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 3394 3395