nfs_nfsdport.c revision 268961
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 268961 2014-07-21 22:21:09Z bdrewery $"); 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; 83extern int nfsd_enable_stringtouid; 84 85SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server"); 86SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW, 87 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points"); 88SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 89 0, ""); 90SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 91 0, ""); 92SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW, 93 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 94SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW, 95 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 96SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW, 97 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names"); 98 99#define MAX_REORDERED_RPC 16 100#define NUM_HEURISTIC 1031 101#define NHUSE_INIT 64 102#define NHUSE_INC 16 103#define NHUSE_MAX 2048 104 105static struct nfsheur { 106 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 107 off_t nh_nextoff; /* next offset for sequential detection */ 108 int nh_use; /* use count for selection */ 109 int nh_seqcount; /* heuristic */ 110} nfsheur[NUM_HEURISTIC]; 111 112 113/* 114 * Heuristic to detect sequential operation. 115 */ 116static struct nfsheur * 117nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp) 118{ 119 struct nfsheur *nh; 120 int hi, try; 121 122 /* Locate best candidate. */ 123 try = 32; 124 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 125 nh = &nfsheur[hi]; 126 while (try--) { 127 if (nfsheur[hi].nh_vp == vp) { 128 nh = &nfsheur[hi]; 129 break; 130 } 131 if (nfsheur[hi].nh_use > 0) 132 --nfsheur[hi].nh_use; 133 hi = (hi + 1) % NUM_HEURISTIC; 134 if (nfsheur[hi].nh_use < nh->nh_use) 135 nh = &nfsheur[hi]; 136 } 137 138 /* Initialize hint if this is a new file. */ 139 if (nh->nh_vp != vp) { 140 nh->nh_vp = vp; 141 nh->nh_nextoff = uio->uio_offset; 142 nh->nh_use = NHUSE_INIT; 143 if (uio->uio_offset == 0) 144 nh->nh_seqcount = 4; 145 else 146 nh->nh_seqcount = 1; 147 } 148 149 /* Calculate heuristic. */ 150 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) || 151 uio->uio_offset == nh->nh_nextoff) { 152 /* See comments in vfs_vnops.c:sequential_heuristic(). */ 153 nh->nh_seqcount += howmany(uio->uio_resid, 16384); 154 if (nh->nh_seqcount > IO_SEQMAX) 155 nh->nh_seqcount = IO_SEQMAX; 156 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC * 157 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) { 158 /* Probably a reordered RPC, leave seqcount alone. */ 159 } else if (nh->nh_seqcount > 1) { 160 nh->nh_seqcount /= 2; 161 } else { 162 nh->nh_seqcount = 0; 163 } 164 nh->nh_use += NHUSE_INC; 165 if (nh->nh_use > NHUSE_MAX) 166 nh->nh_use = NHUSE_MAX; 167 return (nh); 168} 169 170/* 171 * Get attributes into nfsvattr structure. 172 */ 173int 174nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 175 struct thread *p, int vpislocked) 176{ 177 int error, lockedit = 0; 178 179 if (vpislocked == 0) { 180 /* 181 * When vpislocked == 0, the vnode is either exclusively 182 * locked by this thread or not locked by this thread. 183 * As such, shared lock it, if not exclusively locked. 184 */ 185 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 186 lockedit = 1; 187 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY); 188 } 189 } 190 error = VOP_GETATTR(vp, &nvap->na_vattr, cred); 191 if (lockedit != 0) 192 NFSVOPUNLOCK(vp, 0); 193 194 NFSEXITCODE(error); 195 return (error); 196} 197 198/* 199 * Get a file handle for a vnode. 200 */ 201int 202nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 203{ 204 int error; 205 206 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 207 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 208 error = VOP_VPTOFH(vp, &fhp->fh_fid); 209 210 NFSEXITCODE(error); 211 return (error); 212} 213 214/* 215 * Perform access checking for vnodes obtained from file handles that would 216 * refer to files already opened by a Unix client. You cannot just use 217 * vn_writechk() and VOP_ACCESSX() for two reasons. 218 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 219 * case. 220 * 2 - The owner is to be given access irrespective of mode bits for some 221 * operations, so that processes that chmod after opening a file don't 222 * break. 223 */ 224int 225nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 226 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 227 u_int32_t *supportedtypep) 228{ 229 struct vattr vattr; 230 int error = 0, getret = 0; 231 232 if (vpislocked == 0) { 233 if (NFSVOPLOCK(vp, LK_SHARED) != 0) { 234 error = EPERM; 235 goto out; 236 } 237 } 238 if (accmode & VWRITE) { 239 /* Just vn_writechk() changed to check rdonly */ 240 /* 241 * Disallow write attempts on read-only file systems; 242 * unless the file is a socket or a block or character 243 * device resident on the file system. 244 */ 245 if (NFSVNO_EXRDONLY(exp) || 246 (vp->v_mount->mnt_flag & MNT_RDONLY)) { 247 switch (vp->v_type) { 248 case VREG: 249 case VDIR: 250 case VLNK: 251 error = EROFS; 252 default: 253 break; 254 } 255 } 256 /* 257 * If there's shared text associated with 258 * the inode, try to free it up once. If 259 * we fail, we can't allow writing. 260 */ 261 if (VOP_IS_TEXT(vp) && error == 0) 262 error = ETXTBSY; 263 } 264 if (error != 0) { 265 if (vpislocked == 0) 266 NFSVOPUNLOCK(vp, 0); 267 goto out; 268 } 269 270 /* 271 * Should the override still be applied when ACLs are enabled? 272 */ 273 error = VOP_ACCESSX(vp, accmode, cred, p); 274 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 275 /* 276 * Try again with VEXPLICIT_DENY, to see if the test for 277 * deletion is supported. 278 */ 279 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 280 if (error == 0) { 281 if (vp->v_type == VDIR) { 282 accmode &= ~(VDELETE | VDELETE_CHILD); 283 accmode |= VWRITE; 284 error = VOP_ACCESSX(vp, accmode, cred, p); 285 } else if (supportedtypep != NULL) { 286 *supportedtypep &= ~NFSACCESS_DELETE; 287 } 288 } 289 } 290 291 /* 292 * Allow certain operations for the owner (reads and writes 293 * on files that are already open). 294 */ 295 if (override != NFSACCCHK_NOOVERRIDE && 296 (error == EPERM || error == EACCES)) { 297 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 298 error = 0; 299 else if (override & NFSACCCHK_ALLOWOWNER) { 300 getret = VOP_GETATTR(vp, &vattr, cred); 301 if (getret == 0 && cred->cr_uid == vattr.va_uid) 302 error = 0; 303 } 304 } 305 if (vpislocked == 0) 306 NFSVOPUNLOCK(vp, 0); 307 308out: 309 NFSEXITCODE(error); 310 return (error); 311} 312 313/* 314 * Set attribute(s) vnop. 315 */ 316int 317nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 318 struct thread *p, struct nfsexstuff *exp) 319{ 320 int error; 321 322 error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 323 NFSEXITCODE(error); 324 return (error); 325} 326 327/* 328 * Set up nameidata for a lookup() call and do it. 329 */ 330int 331nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 332 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 333 struct vnode **retdirp) 334{ 335 struct componentname *cnp = &ndp->ni_cnd; 336 int i; 337 struct iovec aiov; 338 struct uio auio; 339 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 340 int error = 0, crossmnt; 341 char *cp; 342 343 *retdirp = NULL; 344 cnp->cn_nameptr = cnp->cn_pnbuf; 345 ndp->ni_strictrelative = 0; 346 /* 347 * Extract and set starting directory. 348 */ 349 if (dp->v_type != VDIR) { 350 if (islocked) 351 vput(dp); 352 else 353 vrele(dp); 354 nfsvno_relpathbuf(ndp); 355 error = ENOTDIR; 356 goto out1; 357 } 358 if (islocked) 359 NFSVOPUNLOCK(dp, 0); 360 VREF(dp); 361 *retdirp = dp; 362 if (NFSVNO_EXRDONLY(exp)) 363 cnp->cn_flags |= RDONLY; 364 ndp->ni_segflg = UIO_SYSSPACE; 365 crossmnt = 1; 366 367 if (nd->nd_flag & ND_PUBLOOKUP) { 368 ndp->ni_loopcnt = 0; 369 if (cnp->cn_pnbuf[0] == '/') { 370 vrele(dp); 371 /* 372 * Check for degenerate pathnames here, since lookup() 373 * panics on them. 374 */ 375 for (i = 1; i < ndp->ni_pathlen; i++) 376 if (cnp->cn_pnbuf[i] != '/') 377 break; 378 if (i == ndp->ni_pathlen) { 379 error = NFSERR_ACCES; 380 goto out; 381 } 382 dp = rootvnode; 383 VREF(dp); 384 } 385 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 386 (nd->nd_flag & ND_NFSV4) == 0) { 387 /* 388 * Only cross mount points for NFSv4 when doing a 389 * mount while traversing the file system above 390 * the mount point, unless nfsrv_enable_crossmntpt is set. 391 */ 392 cnp->cn_flags |= NOCROSSMOUNT; 393 crossmnt = 0; 394 } 395 396 /* 397 * Initialize for scan, set ni_startdir and bump ref on dp again 398 * because lookup() will dereference ni_startdir. 399 */ 400 401 cnp->cn_thread = p; 402 ndp->ni_startdir = dp; 403 ndp->ni_rootdir = rootvnode; 404 ndp->ni_topdir = NULL; 405 406 if (!lockleaf) 407 cnp->cn_flags |= LOCKLEAF; 408 for (;;) { 409 cnp->cn_nameptr = cnp->cn_pnbuf; 410 /* 411 * Call lookup() to do the real work. If an error occurs, 412 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 413 * we do not have to dereference anything before returning. 414 * In either case ni_startdir will be dereferenced and NULLed 415 * out. 416 */ 417 error = lookup(ndp); 418 if (error) 419 break; 420 421 /* 422 * Check for encountering a symbolic link. Trivial 423 * termination occurs if no symlink encountered. 424 */ 425 if ((cnp->cn_flags & ISSYMLINK) == 0) { 426 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 427 nfsvno_relpathbuf(ndp); 428 if (ndp->ni_vp && !lockleaf) 429 NFSVOPUNLOCK(ndp->ni_vp, 0); 430 break; 431 } 432 433 /* 434 * Validate symlink 435 */ 436 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 437 NFSVOPUNLOCK(ndp->ni_dvp, 0); 438 if (!(nd->nd_flag & ND_PUBLOOKUP)) { 439 error = EINVAL; 440 goto badlink2; 441 } 442 443 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 444 error = ELOOP; 445 goto badlink2; 446 } 447 if (ndp->ni_pathlen > 1) 448 cp = uma_zalloc(namei_zone, M_WAITOK); 449 else 450 cp = cnp->cn_pnbuf; 451 aiov.iov_base = cp; 452 aiov.iov_len = MAXPATHLEN; 453 auio.uio_iov = &aiov; 454 auio.uio_iovcnt = 1; 455 auio.uio_offset = 0; 456 auio.uio_rw = UIO_READ; 457 auio.uio_segflg = UIO_SYSSPACE; 458 auio.uio_td = NULL; 459 auio.uio_resid = MAXPATHLEN; 460 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 461 if (error) { 462 badlink1: 463 if (ndp->ni_pathlen > 1) 464 uma_zfree(namei_zone, cp); 465 badlink2: 466 vrele(ndp->ni_dvp); 467 vput(ndp->ni_vp); 468 break; 469 } 470 linklen = MAXPATHLEN - auio.uio_resid; 471 if (linklen == 0) { 472 error = ENOENT; 473 goto badlink1; 474 } 475 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 476 error = ENAMETOOLONG; 477 goto badlink1; 478 } 479 480 /* 481 * Adjust or replace path 482 */ 483 if (ndp->ni_pathlen > 1) { 484 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 485 uma_zfree(namei_zone, cnp->cn_pnbuf); 486 cnp->cn_pnbuf = cp; 487 } else 488 cnp->cn_pnbuf[linklen] = '\0'; 489 ndp->ni_pathlen += linklen; 490 491 /* 492 * Cleanup refs for next loop and check if root directory 493 * should replace current directory. Normally ni_dvp 494 * becomes the new base directory and is cleaned up when 495 * we loop. Explicitly null pointers after invalidation 496 * to clarify operation. 497 */ 498 vput(ndp->ni_vp); 499 ndp->ni_vp = NULL; 500 501 if (cnp->cn_pnbuf[0] == '/') { 502 vrele(ndp->ni_dvp); 503 ndp->ni_dvp = ndp->ni_rootdir; 504 VREF(ndp->ni_dvp); 505 } 506 ndp->ni_startdir = ndp->ni_dvp; 507 ndp->ni_dvp = NULL; 508 } 509 if (!lockleaf) 510 cnp->cn_flags &= ~LOCKLEAF; 511 512out: 513 if (error) { 514 nfsvno_relpathbuf(ndp); 515 ndp->ni_vp = NULL; 516 ndp->ni_dvp = NULL; 517 ndp->ni_startdir = NULL; 518 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 519 ndp->ni_dvp = NULL; 520 } 521 522out1: 523 NFSEXITCODE2(error, nd); 524 return (error); 525} 526 527/* 528 * Set up a pathname buffer and return a pointer to it and, optionally 529 * set a hash pointer. 530 */ 531void 532nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 533{ 534 struct componentname *cnp = &ndp->ni_cnd; 535 536 cnp->cn_flags |= (NOMACCHECK | HASBUF); 537 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 538 if (hashpp != NULL) 539 *hashpp = NULL; 540 *bufpp = cnp->cn_pnbuf; 541} 542 543/* 544 * Release the above path buffer, if not released by nfsvno_namei(). 545 */ 546void 547nfsvno_relpathbuf(struct nameidata *ndp) 548{ 549 550 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 551 panic("nfsrelpath"); 552 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 553 ndp->ni_cnd.cn_flags &= ~HASBUF; 554} 555 556/* 557 * Readlink vnode op into an mbuf list. 558 */ 559int 560nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 561 struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 562{ 563 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 564 struct iovec *ivp = iv; 565 struct uio io, *uiop = &io; 566 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 567 int i, len, tlen, error = 0; 568 569 len = 0; 570 i = 0; 571 while (len < NFS_MAXPATHLEN) { 572 NFSMGET(mp); 573 MCLGET(mp, M_WAITOK); 574 mp->m_len = NFSMSIZ(mp); 575 if (len == 0) { 576 mp3 = mp2 = mp; 577 } else { 578 mp2->m_next = mp; 579 mp2 = mp; 580 } 581 if ((len + mp->m_len) > NFS_MAXPATHLEN) { 582 mp->m_len = NFS_MAXPATHLEN - len; 583 len = NFS_MAXPATHLEN; 584 } else { 585 len += mp->m_len; 586 } 587 ivp->iov_base = mtod(mp, caddr_t); 588 ivp->iov_len = mp->m_len; 589 i++; 590 ivp++; 591 } 592 uiop->uio_iov = iv; 593 uiop->uio_iovcnt = i; 594 uiop->uio_offset = 0; 595 uiop->uio_resid = len; 596 uiop->uio_rw = UIO_READ; 597 uiop->uio_segflg = UIO_SYSSPACE; 598 uiop->uio_td = NULL; 599 error = VOP_READLINK(vp, uiop, cred); 600 if (error) { 601 m_freem(mp3); 602 *lenp = 0; 603 goto out; 604 } 605 if (uiop->uio_resid > 0) { 606 len -= uiop->uio_resid; 607 tlen = NFSM_RNDUP(len); 608 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 609 } 610 *lenp = len; 611 *mpp = mp3; 612 *mpendp = mp; 613 614out: 615 NFSEXITCODE(error); 616 return (error); 617} 618 619/* 620 * Read vnode op call into mbuf list. 621 */ 622int 623nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 624 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 625{ 626 struct mbuf *m; 627 int i; 628 struct iovec *iv; 629 struct iovec *iv2; 630 int error = 0, len, left, siz, tlen, ioflag = 0; 631 struct mbuf *m2 = NULL, *m3; 632 struct uio io, *uiop = &io; 633 struct nfsheur *nh; 634 635 len = left = NFSM_RNDUP(cnt); 636 m3 = NULL; 637 /* 638 * Generate the mbuf list with the uio_iov ref. to it. 639 */ 640 i = 0; 641 while (left > 0) { 642 NFSMGET(m); 643 MCLGET(m, M_WAITOK); 644 m->m_len = 0; 645 siz = min(M_TRAILINGSPACE(m), left); 646 left -= siz; 647 i++; 648 if (m3) 649 m2->m_next = m; 650 else 651 m3 = m; 652 m2 = m; 653 } 654 MALLOC(iv, struct iovec *, i * sizeof (struct iovec), 655 M_TEMP, M_WAITOK); 656 uiop->uio_iov = iv2 = iv; 657 m = m3; 658 left = len; 659 i = 0; 660 while (left > 0) { 661 if (m == NULL) 662 panic("nfsvno_read iov"); 663 siz = min(M_TRAILINGSPACE(m), left); 664 if (siz > 0) { 665 iv->iov_base = mtod(m, caddr_t) + m->m_len; 666 iv->iov_len = siz; 667 m->m_len += siz; 668 left -= siz; 669 iv++; 670 i++; 671 } 672 m = m->m_next; 673 } 674 uiop->uio_iovcnt = i; 675 uiop->uio_offset = off; 676 uiop->uio_resid = len; 677 uiop->uio_rw = UIO_READ; 678 uiop->uio_segflg = UIO_SYSSPACE; 679 uiop->uio_td = NULL; 680 nh = nfsrv_sequential_heuristic(uiop, vp); 681 ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 682 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 683 FREE((caddr_t)iv2, M_TEMP); 684 if (error) { 685 m_freem(m3); 686 *mpp = NULL; 687 goto out; 688 } 689 nh->nh_nextoff = uiop->uio_offset; 690 tlen = len - uiop->uio_resid; 691 cnt = cnt < tlen ? cnt : tlen; 692 tlen = NFSM_RNDUP(cnt); 693 if (tlen == 0) { 694 m_freem(m3); 695 m3 = NULL; 696 } else if (len != tlen || tlen != cnt) 697 nfsrv_adj(m3, len - tlen, tlen - cnt); 698 *mpp = m3; 699 *mpendp = m2; 700 701out: 702 NFSEXITCODE(error); 703 return (error); 704} 705 706/* 707 * Write vnode op from an mbuf list. 708 */ 709int 710nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable, 711 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 712{ 713 struct iovec *ivp; 714 int i, len; 715 struct iovec *iv; 716 int ioflags, error; 717 struct uio io, *uiop = &io; 718 struct nfsheur *nh; 719 720 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP, 721 M_WAITOK); 722 uiop->uio_iov = iv = ivp; 723 uiop->uio_iovcnt = cnt; 724 i = mtod(mp, caddr_t) + mp->m_len - cp; 725 len = retlen; 726 while (len > 0) { 727 if (mp == NULL) 728 panic("nfsvno_write"); 729 if (i > 0) { 730 i = min(i, len); 731 ivp->iov_base = cp; 732 ivp->iov_len = i; 733 ivp++; 734 len -= i; 735 } 736 mp = mp->m_next; 737 if (mp) { 738 i = mp->m_len; 739 cp = mtod(mp, caddr_t); 740 } 741 } 742 743 if (stable == NFSWRITE_UNSTABLE) 744 ioflags = IO_NODELOCKED; 745 else 746 ioflags = (IO_SYNC | IO_NODELOCKED); 747 uiop->uio_resid = retlen; 748 uiop->uio_rw = UIO_WRITE; 749 uiop->uio_segflg = UIO_SYSSPACE; 750 NFSUIOPROC(uiop, p); 751 uiop->uio_offset = off; 752 nh = nfsrv_sequential_heuristic(uiop, vp); 753 ioflags |= nh->nh_seqcount << IO_SEQSHIFT; 754 error = VOP_WRITE(vp, uiop, ioflags, cred); 755 if (error == 0) 756 nh->nh_nextoff = uiop->uio_offset; 757 FREE((caddr_t)iv, M_TEMP); 758 759 NFSEXITCODE(error); 760 return (error); 761} 762 763/* 764 * Common code for creating a regular file (plus special files for V2). 765 */ 766int 767nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 768 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 769 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp) 770{ 771 u_quad_t tempsize; 772 int error; 773 774 error = nd->nd_repstat; 775 if (!error && ndp->ni_vp == NULL) { 776 if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 777 vrele(ndp->ni_startdir); 778 error = VOP_CREATE(ndp->ni_dvp, 779 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 780 vput(ndp->ni_dvp); 781 nfsvno_relpathbuf(ndp); 782 if (!error) { 783 if (*exclusive_flagp) { 784 *exclusive_flagp = 0; 785 NFSVNO_ATTRINIT(nvap); 786 nvap->na_atime.tv_sec = cverf[0]; 787 nvap->na_atime.tv_nsec = cverf[1]; 788 error = VOP_SETATTR(ndp->ni_vp, 789 &nvap->na_vattr, nd->nd_cred); 790 } 791 } 792 /* 793 * NFS V2 Only. nfsrvd_mknod() does this for V3. 794 * (This implies, just get out on an error.) 795 */ 796 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 797 nvap->na_type == VFIFO) { 798 if (nvap->na_type == VCHR && rdev == 0xffffffff) 799 nvap->na_type = VFIFO; 800 if (nvap->na_type != VFIFO && 801 (error = priv_check_cred(nd->nd_cred, 802 PRIV_VFS_MKNOD_DEV, 0))) { 803 vrele(ndp->ni_startdir); 804 nfsvno_relpathbuf(ndp); 805 vput(ndp->ni_dvp); 806 goto out; 807 } 808 nvap->na_rdev = rdev; 809 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 810 &ndp->ni_cnd, &nvap->na_vattr); 811 vput(ndp->ni_dvp); 812 nfsvno_relpathbuf(ndp); 813 vrele(ndp->ni_startdir); 814 if (error) 815 goto out; 816 } else { 817 vrele(ndp->ni_startdir); 818 nfsvno_relpathbuf(ndp); 819 vput(ndp->ni_dvp); 820 error = ENXIO; 821 goto out; 822 } 823 *vpp = ndp->ni_vp; 824 } else { 825 /* 826 * Handle cases where error is already set and/or 827 * the file exists. 828 * 1 - clean up the lookup 829 * 2 - iff !error and na_size set, truncate it 830 */ 831 vrele(ndp->ni_startdir); 832 nfsvno_relpathbuf(ndp); 833 *vpp = ndp->ni_vp; 834 if (ndp->ni_dvp == *vpp) 835 vrele(ndp->ni_dvp); 836 else 837 vput(ndp->ni_dvp); 838 if (!error && nvap->na_size != VNOVAL) { 839 error = nfsvno_accchk(*vpp, VWRITE, 840 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 841 NFSACCCHK_VPISLOCKED, NULL); 842 if (!error) { 843 tempsize = nvap->na_size; 844 NFSVNO_ATTRINIT(nvap); 845 nvap->na_size = tempsize; 846 error = VOP_SETATTR(*vpp, 847 &nvap->na_vattr, nd->nd_cred); 848 } 849 } 850 if (error) 851 vput(*vpp); 852 } 853 854out: 855 NFSEXITCODE(error); 856 return (error); 857} 858 859/* 860 * Do a mknod vnode op. 861 */ 862int 863nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 864 struct thread *p) 865{ 866 int error = 0; 867 enum vtype vtyp; 868 869 vtyp = nvap->na_type; 870 /* 871 * Iff doesn't exist, create it. 872 */ 873 if (ndp->ni_vp) { 874 vrele(ndp->ni_startdir); 875 nfsvno_relpathbuf(ndp); 876 vput(ndp->ni_dvp); 877 vrele(ndp->ni_vp); 878 error = EEXIST; 879 goto out; 880 } 881 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 882 vrele(ndp->ni_startdir); 883 nfsvno_relpathbuf(ndp); 884 vput(ndp->ni_dvp); 885 error = NFSERR_BADTYPE; 886 goto out; 887 } 888 if (vtyp == VSOCK) { 889 vrele(ndp->ni_startdir); 890 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 891 &ndp->ni_cnd, &nvap->na_vattr); 892 vput(ndp->ni_dvp); 893 nfsvno_relpathbuf(ndp); 894 } else { 895 if (nvap->na_type != VFIFO && 896 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) { 897 vrele(ndp->ni_startdir); 898 nfsvno_relpathbuf(ndp); 899 vput(ndp->ni_dvp); 900 goto out; 901 } 902 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 903 &ndp->ni_cnd, &nvap->na_vattr); 904 vput(ndp->ni_dvp); 905 nfsvno_relpathbuf(ndp); 906 vrele(ndp->ni_startdir); 907 /* 908 * Since VOP_MKNOD returns the ni_vp, I can't 909 * see any reason to do the lookup. 910 */ 911 } 912 913out: 914 NFSEXITCODE(error); 915 return (error); 916} 917 918/* 919 * Mkdir vnode op. 920 */ 921int 922nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 923 struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 924{ 925 int error = 0; 926 927 if (ndp->ni_vp != NULL) { 928 if (ndp->ni_dvp == ndp->ni_vp) 929 vrele(ndp->ni_dvp); 930 else 931 vput(ndp->ni_dvp); 932 vrele(ndp->ni_vp); 933 nfsvno_relpathbuf(ndp); 934 error = EEXIST; 935 goto out; 936 } 937 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 938 &nvap->na_vattr); 939 vput(ndp->ni_dvp); 940 nfsvno_relpathbuf(ndp); 941 942out: 943 NFSEXITCODE(error); 944 return (error); 945} 946 947/* 948 * symlink vnode op. 949 */ 950int 951nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 952 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 953 struct nfsexstuff *exp) 954{ 955 int error = 0; 956 957 if (ndp->ni_vp) { 958 vrele(ndp->ni_startdir); 959 nfsvno_relpathbuf(ndp); 960 if (ndp->ni_dvp == ndp->ni_vp) 961 vrele(ndp->ni_dvp); 962 else 963 vput(ndp->ni_dvp); 964 vrele(ndp->ni_vp); 965 error = EEXIST; 966 goto out; 967 } 968 969 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 970 &nvap->na_vattr, pathcp); 971 vput(ndp->ni_dvp); 972 vrele(ndp->ni_startdir); 973 nfsvno_relpathbuf(ndp); 974 /* 975 * Although FreeBSD still had the lookup code in 976 * it for 7/current, there doesn't seem to be any 977 * point, since VOP_SYMLINK() returns the ni_vp. 978 * Just vput it for v2. 979 */ 980 if (!not_v2 && !error) 981 vput(ndp->ni_vp); 982 983out: 984 NFSEXITCODE(error); 985 return (error); 986} 987 988/* 989 * Parse symbolic link arguments. 990 * This function has an ugly side effect. It will MALLOC() an area for 991 * the symlink and set iov_base to point to it, only if it succeeds. 992 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 993 * be FREE'd later. 994 */ 995int 996nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 997 struct thread *p, char **pathcpp, int *lenp) 998{ 999 u_int32_t *tl; 1000 char *pathcp = NULL; 1001 int error = 0, len; 1002 struct nfsv2_sattr *sp; 1003 1004 *pathcpp = NULL; 1005 *lenp = 0; 1006 if ((nd->nd_flag & ND_NFSV3) && 1007 (error = nfsrv_sattr(nd, nvap, NULL, NULL, p))) 1008 goto nfsmout; 1009 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1010 len = fxdr_unsigned(int, *tl); 1011 if (len > NFS_MAXPATHLEN || len <= 0) { 1012 error = EBADRPC; 1013 goto nfsmout; 1014 } 1015 MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK); 1016 error = nfsrv_mtostr(nd, pathcp, len); 1017 if (error) 1018 goto nfsmout; 1019 if (nd->nd_flag & ND_NFSV2) { 1020 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1021 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 1022 } 1023 *pathcpp = pathcp; 1024 *lenp = len; 1025 NFSEXITCODE2(0, nd); 1026 return (0); 1027nfsmout: 1028 if (pathcp) 1029 free(pathcp, M_TEMP); 1030 NFSEXITCODE2(error, nd); 1031 return (error); 1032} 1033 1034/* 1035 * Remove a non-directory object. 1036 */ 1037int 1038nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1039 struct thread *p, struct nfsexstuff *exp) 1040{ 1041 struct vnode *vp; 1042 int error = 0; 1043 1044 vp = ndp->ni_vp; 1045 if (vp->v_type == VDIR) 1046 error = NFSERR_ISDIR; 1047 else if (is_v4) 1048 error = nfsrv_checkremove(vp, 1, p); 1049 if (!error) 1050 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 1051 if (ndp->ni_dvp == vp) 1052 vrele(ndp->ni_dvp); 1053 else 1054 vput(ndp->ni_dvp); 1055 vput(vp); 1056 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1057 nfsvno_relpathbuf(ndp); 1058 NFSEXITCODE(error); 1059 return (error); 1060} 1061 1062/* 1063 * Remove a directory. 1064 */ 1065int 1066nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1067 struct thread *p, struct nfsexstuff *exp) 1068{ 1069 struct vnode *vp; 1070 int error = 0; 1071 1072 vp = ndp->ni_vp; 1073 if (vp->v_type != VDIR) { 1074 error = ENOTDIR; 1075 goto out; 1076 } 1077 /* 1078 * No rmdir "." please. 1079 */ 1080 if (ndp->ni_dvp == vp) { 1081 error = EINVAL; 1082 goto out; 1083 } 1084 /* 1085 * The root of a mounted filesystem cannot be deleted. 1086 */ 1087 if (vp->v_vflag & VV_ROOT) 1088 error = EBUSY; 1089out: 1090 if (!error) 1091 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1092 if (ndp->ni_dvp == vp) 1093 vrele(ndp->ni_dvp); 1094 else 1095 vput(ndp->ni_dvp); 1096 vput(vp); 1097 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1098 nfsvno_relpathbuf(ndp); 1099 NFSEXITCODE(error); 1100 return (error); 1101} 1102 1103/* 1104 * Rename vnode op. 1105 */ 1106int 1107nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1108 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1109{ 1110 struct vnode *fvp, *tvp, *tdvp; 1111 int error = 0; 1112 1113 fvp = fromndp->ni_vp; 1114 if (ndstat) { 1115 vrele(fromndp->ni_dvp); 1116 vrele(fvp); 1117 error = ndstat; 1118 goto out1; 1119 } 1120 tdvp = tondp->ni_dvp; 1121 tvp = tondp->ni_vp; 1122 if (tvp != NULL) { 1123 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1124 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1125 goto out; 1126 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1127 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1128 goto out; 1129 } 1130 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1131 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1132 goto out; 1133 } 1134 1135 /* 1136 * A rename to '.' or '..' results in a prematurely 1137 * unlocked vnode on FreeBSD5, so I'm just going to fail that 1138 * here. 1139 */ 1140 if ((tondp->ni_cnd.cn_namelen == 1 && 1141 tondp->ni_cnd.cn_nameptr[0] == '.') || 1142 (tondp->ni_cnd.cn_namelen == 2 && 1143 tondp->ni_cnd.cn_nameptr[0] == '.' && 1144 tondp->ni_cnd.cn_nameptr[1] == '.')) { 1145 error = EINVAL; 1146 goto out; 1147 } 1148 } 1149 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1150 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1151 goto out; 1152 } 1153 if (fvp->v_mount != tdvp->v_mount) { 1154 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1155 goto out; 1156 } 1157 if (fvp == tdvp) { 1158 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1159 goto out; 1160 } 1161 if (fvp == tvp) { 1162 /* 1163 * If source and destination are the same, there is nothing to 1164 * do. Set error to -1 to indicate this. 1165 */ 1166 error = -1; 1167 goto out; 1168 } 1169 if (ndflag & ND_NFSV4) { 1170 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) { 1171 error = nfsrv_checkremove(fvp, 0, p); 1172 NFSVOPUNLOCK(fvp, 0); 1173 } else 1174 error = EPERM; 1175 if (tvp && !error) 1176 error = nfsrv_checkremove(tvp, 1, p); 1177 } else { 1178 /* 1179 * For NFSv2 and NFSv3, try to get rid of the delegation, so 1180 * that the NFSv4 client won't be confused by the rename. 1181 * Since nfsd_recalldelegation() can only be called on an 1182 * unlocked vnode at this point and fvp is the file that will 1183 * still exist after the rename, just do fvp. 1184 */ 1185 nfsd_recalldelegation(fvp, p); 1186 } 1187out: 1188 if (!error) { 1189 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1190 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1191 &tondp->ni_cnd); 1192 } else { 1193 if (tdvp == tvp) 1194 vrele(tdvp); 1195 else 1196 vput(tdvp); 1197 if (tvp) 1198 vput(tvp); 1199 vrele(fromndp->ni_dvp); 1200 vrele(fvp); 1201 if (error == -1) 1202 error = 0; 1203 } 1204 vrele(tondp->ni_startdir); 1205 nfsvno_relpathbuf(tondp); 1206out1: 1207 vrele(fromndp->ni_startdir); 1208 nfsvno_relpathbuf(fromndp); 1209 NFSEXITCODE(error); 1210 return (error); 1211} 1212 1213/* 1214 * Link vnode op. 1215 */ 1216int 1217nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1218 struct thread *p, struct nfsexstuff *exp) 1219{ 1220 struct vnode *xp; 1221 int error = 0; 1222 1223 xp = ndp->ni_vp; 1224 if (xp != NULL) { 1225 error = EEXIST; 1226 } else { 1227 xp = ndp->ni_dvp; 1228 if (vp->v_mount != xp->v_mount) 1229 error = EXDEV; 1230 } 1231 if (!error) { 1232 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 1233 if ((vp->v_iflag & VI_DOOMED) == 0) 1234 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1235 else 1236 error = EPERM; 1237 if (ndp->ni_dvp == vp) 1238 vrele(ndp->ni_dvp); 1239 else 1240 vput(ndp->ni_dvp); 1241 NFSVOPUNLOCK(vp, 0); 1242 } else { 1243 if (ndp->ni_dvp == ndp->ni_vp) 1244 vrele(ndp->ni_dvp); 1245 else 1246 vput(ndp->ni_dvp); 1247 if (ndp->ni_vp) 1248 vrele(ndp->ni_vp); 1249 } 1250 nfsvno_relpathbuf(ndp); 1251 NFSEXITCODE(error); 1252 return (error); 1253} 1254 1255/* 1256 * Do the fsync() appropriate for the commit. 1257 */ 1258int 1259nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1260 struct thread *td) 1261{ 1262 int error = 0; 1263 1264 /* 1265 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of 1266 * file is done. At this time VOP_FSYNC does not accept offset and 1267 * byte count parameters so call VOP_FSYNC the whole file for now. 1268 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3. 1269 */ 1270 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) { 1271 /* 1272 * Give up and do the whole thing 1273 */ 1274 if (vp->v_object && 1275 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1276 VM_OBJECT_WLOCK(vp->v_object); 1277 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1278 VM_OBJECT_WUNLOCK(vp->v_object); 1279 } 1280 error = VOP_FSYNC(vp, MNT_WAIT, td); 1281 } else { 1282 /* 1283 * Locate and synchronously write any buffers that fall 1284 * into the requested range. Note: we are assuming that 1285 * f_iosize is a power of 2. 1286 */ 1287 int iosize = vp->v_mount->mnt_stat.f_iosize; 1288 int iomask = iosize - 1; 1289 struct bufobj *bo; 1290 daddr_t lblkno; 1291 1292 /* 1293 * Align to iosize boundry, super-align to page boundry. 1294 */ 1295 if (off & iomask) { 1296 cnt += off & iomask; 1297 off &= ~(u_quad_t)iomask; 1298 } 1299 if (off & PAGE_MASK) { 1300 cnt += off & PAGE_MASK; 1301 off &= ~(u_quad_t)PAGE_MASK; 1302 } 1303 lblkno = off / iosize; 1304 1305 if (vp->v_object && 1306 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1307 VM_OBJECT_WLOCK(vp->v_object); 1308 vm_object_page_clean(vp->v_object, off, off + cnt, 1309 OBJPC_SYNC); 1310 VM_OBJECT_WUNLOCK(vp->v_object); 1311 } 1312 1313 bo = &vp->v_bufobj; 1314 BO_LOCK(bo); 1315 while (cnt > 0) { 1316 struct buf *bp; 1317 1318 /* 1319 * If we have a buffer and it is marked B_DELWRI we 1320 * have to lock and write it. Otherwise the prior 1321 * write is assumed to have already been committed. 1322 * 1323 * gbincore() can return invalid buffers now so we 1324 * have to check that bit as well (though B_DELWRI 1325 * should not be set if B_INVAL is set there could be 1326 * a race here since we haven't locked the buffer). 1327 */ 1328 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1329 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1330 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) { 1331 BO_LOCK(bo); 1332 continue; /* retry */ 1333 } 1334 if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1335 B_DELWRI) { 1336 bremfree(bp); 1337 bp->b_flags &= ~B_ASYNC; 1338 bwrite(bp); 1339 ++nfs_commit_miss; 1340 } else 1341 BUF_UNLOCK(bp); 1342 BO_LOCK(bo); 1343 } 1344 ++nfs_commit_blks; 1345 if (cnt < iosize) 1346 break; 1347 cnt -= iosize; 1348 ++lblkno; 1349 } 1350 BO_UNLOCK(bo); 1351 } 1352 NFSEXITCODE(error); 1353 return (error); 1354} 1355 1356/* 1357 * Statfs vnode op. 1358 */ 1359int 1360nfsvno_statfs(struct vnode *vp, struct statfs *sf) 1361{ 1362 int error; 1363 1364 error = VFS_STATFS(vp->v_mount, sf); 1365 if (error == 0) { 1366 /* 1367 * Since NFS handles these values as unsigned on the 1368 * wire, there is no way to represent negative values, 1369 * so set them to 0. Without this, they will appear 1370 * to be very large positive values for clients like 1371 * Solaris10. 1372 */ 1373 if (sf->f_bavail < 0) 1374 sf->f_bavail = 0; 1375 if (sf->f_ffree < 0) 1376 sf->f_ffree = 0; 1377 } 1378 NFSEXITCODE(error); 1379 return (error); 1380} 1381 1382/* 1383 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1384 * must handle nfsrv_opencheck() calls after any other access checks. 1385 */ 1386void 1387nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1388 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1389 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1390 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p, 1391 struct nfsexstuff *exp, struct vnode **vpp) 1392{ 1393 struct vnode *vp = NULL; 1394 u_quad_t tempsize; 1395 struct nfsexstuff nes; 1396 1397 if (ndp->ni_vp == NULL) 1398 nd->nd_repstat = nfsrv_opencheck(clientid, 1399 stateidp, stp, NULL, nd, p, nd->nd_repstat); 1400 if (!nd->nd_repstat) { 1401 if (ndp->ni_vp == NULL) { 1402 vrele(ndp->ni_startdir); 1403 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1404 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1405 vput(ndp->ni_dvp); 1406 nfsvno_relpathbuf(ndp); 1407 if (!nd->nd_repstat) { 1408 if (*exclusive_flagp) { 1409 *exclusive_flagp = 0; 1410 NFSVNO_ATTRINIT(nvap); 1411 nvap->na_atime.tv_sec = cverf[0]; 1412 nvap->na_atime.tv_nsec = cverf[1]; 1413 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1414 &nvap->na_vattr, cred); 1415 } else { 1416 nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1417 aclp, p, attrbitp, exp); 1418 } 1419 } 1420 vp = ndp->ni_vp; 1421 } else { 1422 if (ndp->ni_startdir) 1423 vrele(ndp->ni_startdir); 1424 nfsvno_relpathbuf(ndp); 1425 vp = ndp->ni_vp; 1426 if (create == NFSV4OPEN_CREATE) { 1427 if (ndp->ni_dvp == vp) 1428 vrele(ndp->ni_dvp); 1429 else 1430 vput(ndp->ni_dvp); 1431 } 1432 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1433 if (ndp->ni_cnd.cn_flags & RDONLY) 1434 NFSVNO_SETEXRDONLY(&nes); 1435 else 1436 NFSVNO_EXINIT(&nes); 1437 nd->nd_repstat = nfsvno_accchk(vp, 1438 VWRITE, cred, &nes, p, 1439 NFSACCCHK_NOOVERRIDE, 1440 NFSACCCHK_VPISLOCKED, NULL); 1441 nd->nd_repstat = nfsrv_opencheck(clientid, 1442 stateidp, stp, vp, nd, p, nd->nd_repstat); 1443 if (!nd->nd_repstat) { 1444 tempsize = nvap->na_size; 1445 NFSVNO_ATTRINIT(nvap); 1446 nvap->na_size = tempsize; 1447 nd->nd_repstat = VOP_SETATTR(vp, 1448 &nvap->na_vattr, cred); 1449 } 1450 } else if (vp->v_type == VREG) { 1451 nd->nd_repstat = nfsrv_opencheck(clientid, 1452 stateidp, stp, vp, nd, p, nd->nd_repstat); 1453 } 1454 } 1455 } else { 1456 if (ndp->ni_cnd.cn_flags & HASBUF) 1457 nfsvno_relpathbuf(ndp); 1458 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1459 vrele(ndp->ni_startdir); 1460 if (ndp->ni_dvp == ndp->ni_vp) 1461 vrele(ndp->ni_dvp); 1462 else 1463 vput(ndp->ni_dvp); 1464 if (ndp->ni_vp) 1465 vput(ndp->ni_vp); 1466 } 1467 } 1468 *vpp = vp; 1469 1470 NFSEXITCODE2(0, nd); 1471} 1472 1473/* 1474 * Updates the file rev and sets the mtime and ctime 1475 * to the current clock time, returning the va_filerev and va_Xtime 1476 * values. 1477 * Return ESTALE to indicate the vnode is VI_DOOMED. 1478 */ 1479int 1480nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1481 struct ucred *cred, struct thread *p) 1482{ 1483 struct vattr va; 1484 1485 VATTR_NULL(&va); 1486 vfs_timestamp(&va.va_mtime); 1487 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 1488 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY); 1489 if ((vp->v_iflag & VI_DOOMED) != 0) 1490 return (ESTALE); 1491 } 1492 (void) VOP_SETATTR(vp, &va, cred); 1493 (void) nfsvno_getattr(vp, nvap, cred, p, 1); 1494 return (0); 1495} 1496 1497/* 1498 * Glue routine to nfsv4_fillattr(). 1499 */ 1500int 1501nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp, 1502 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1503 struct ucred *cred, struct thread *p, int isdgram, int reterr, 1504 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno) 1505{ 1506 int error; 1507 1508 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 1509 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 1510 mounted_on_fileno); 1511 NFSEXITCODE2(0, nd); 1512 return (error); 1513} 1514 1515/* Since the Readdir vnode ops vary, put the entire functions in here. */ 1516/* 1517 * nfs readdir service 1518 * - mallocs what it thinks is enough to read 1519 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1520 * - calls VOP_READDIR() 1521 * - loops around building the reply 1522 * if the output generated exceeds count break out of loop 1523 * The NFSM_CLGET macro is used here so that the reply will be packed 1524 * tightly in mbuf clusters. 1525 * - it trims out records with d_fileno == 0 1526 * this doesn't matter for Unix clients, but they might confuse clients 1527 * for other os'. 1528 * - it trims out records with d_type == DT_WHT 1529 * these cannot be seen through NFS (unless we extend the protocol) 1530 * The alternate call nfsrvd_readdirplus() does lookups as well. 1531 * PS: The NFS protocol spec. does not clarify what the "count" byte 1532 * argument is a count of.. just name strings and file id's or the 1533 * entire reply rpc or ... 1534 * I tried just file name and id sizes and it confused the Sun client, 1535 * so I am using the full rpc size now. The "paranoia.." comment refers 1536 * to including the status longwords that are not a part of the dir. 1537 * "entry" structures, but are in the rpc. 1538 */ 1539int 1540nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1541 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1542{ 1543 struct dirent *dp; 1544 u_int32_t *tl; 1545 int dirlen; 1546 char *cpos, *cend, *rbuf; 1547 struct nfsvattr at; 1548 int nlen, error = 0, getret = 1; 1549 int siz, cnt, fullsiz, eofflag, ncookies; 1550 u_int64_t off, toff, verf; 1551 u_long *cookies = NULL, *cookiep; 1552 struct uio io; 1553 struct iovec iv; 1554 int is_ufs; 1555 1556 if (nd->nd_repstat) { 1557 nfsrv_postopattr(nd, getret, &at); 1558 goto out; 1559 } 1560 if (nd->nd_flag & ND_NFSV2) { 1561 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1562 off = fxdr_unsigned(u_quad_t, *tl++); 1563 } else { 1564 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1565 off = fxdr_hyper(tl); 1566 tl += 2; 1567 verf = fxdr_hyper(tl); 1568 tl += 2; 1569 } 1570 toff = off; 1571 cnt = fxdr_unsigned(int, *tl); 1572 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1573 cnt = NFS_SRVMAXDATA(nd); 1574 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1575 fullsiz = siz; 1576 if (nd->nd_flag & ND_NFSV3) { 1577 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, 1578 p, 1); 1579#if 0 1580 /* 1581 * va_filerev is not sufficient as a cookie verifier, 1582 * since it is not supposed to change when entries are 1583 * removed/added unless that offset cookies returned to 1584 * the client are no longer valid. 1585 */ 1586 if (!nd->nd_repstat && toff && verf != at.na_filerev) 1587 nd->nd_repstat = NFSERR_BAD_COOKIE; 1588#endif 1589 } 1590 if (!nd->nd_repstat && vp->v_type != VDIR) 1591 nd->nd_repstat = NFSERR_NOTDIR; 1592 if (nd->nd_repstat == 0 && cnt == 0) { 1593 if (nd->nd_flag & ND_NFSV2) 1594 /* NFSv2 does not have NFSERR_TOOSMALL */ 1595 nd->nd_repstat = EPERM; 1596 else 1597 nd->nd_repstat = NFSERR_TOOSMALL; 1598 } 1599 if (!nd->nd_repstat) 1600 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1601 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1602 NFSACCCHK_VPISLOCKED, NULL); 1603 if (nd->nd_repstat) { 1604 vput(vp); 1605 if (nd->nd_flag & ND_NFSV3) 1606 nfsrv_postopattr(nd, getret, &at); 1607 goto out; 1608 } 1609 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 1610 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1611again: 1612 eofflag = 0; 1613 if (cookies) { 1614 free((caddr_t)cookies, M_TEMP); 1615 cookies = NULL; 1616 } 1617 1618 iv.iov_base = rbuf; 1619 iv.iov_len = siz; 1620 io.uio_iov = &iv; 1621 io.uio_iovcnt = 1; 1622 io.uio_offset = (off_t)off; 1623 io.uio_resid = siz; 1624 io.uio_segflg = UIO_SYSSPACE; 1625 io.uio_rw = UIO_READ; 1626 io.uio_td = NULL; 1627 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1628 &cookies); 1629 off = (u_int64_t)io.uio_offset; 1630 if (io.uio_resid) 1631 siz -= io.uio_resid; 1632 1633 if (!cookies && !nd->nd_repstat) 1634 nd->nd_repstat = NFSERR_PERM; 1635 if (nd->nd_flag & ND_NFSV3) { 1636 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1637 if (!nd->nd_repstat) 1638 nd->nd_repstat = getret; 1639 } 1640 1641 /* 1642 * Handles the failed cases. nd->nd_repstat == 0 past here. 1643 */ 1644 if (nd->nd_repstat) { 1645 vput(vp); 1646 free((caddr_t)rbuf, M_TEMP); 1647 if (cookies) 1648 free((caddr_t)cookies, M_TEMP); 1649 if (nd->nd_flag & ND_NFSV3) 1650 nfsrv_postopattr(nd, getret, &at); 1651 goto out; 1652 } 1653 /* 1654 * If nothing read, return eof 1655 * rpc reply 1656 */ 1657 if (siz == 0) { 1658 vput(vp); 1659 if (nd->nd_flag & ND_NFSV2) { 1660 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1661 } else { 1662 nfsrv_postopattr(nd, getret, &at); 1663 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1664 txdr_hyper(at.na_filerev, tl); 1665 tl += 2; 1666 } 1667 *tl++ = newnfs_false; 1668 *tl = newnfs_true; 1669 FREE((caddr_t)rbuf, M_TEMP); 1670 FREE((caddr_t)cookies, M_TEMP); 1671 goto out; 1672 } 1673 1674 /* 1675 * Check for degenerate cases of nothing useful read. 1676 * If so go try again 1677 */ 1678 cpos = rbuf; 1679 cend = rbuf + siz; 1680 dp = (struct dirent *)cpos; 1681 cookiep = cookies; 1682 1683 /* 1684 * For some reason FreeBSD's ufs_readdir() chooses to back the 1685 * directory offset up to a block boundary, so it is necessary to 1686 * skip over the records that precede the requested offset. This 1687 * requires the assumption that file offset cookies monotonically 1688 * increase. 1689 */ 1690 while (cpos < cend && ncookies > 0 && 1691 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1692 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) { 1693 cpos += dp->d_reclen; 1694 dp = (struct dirent *)cpos; 1695 cookiep++; 1696 ncookies--; 1697 } 1698 if (cpos >= cend || ncookies == 0) { 1699 siz = fullsiz; 1700 toff = off; 1701 goto again; 1702 } 1703 vput(vp); 1704 1705 /* 1706 * dirlen is the size of the reply, including all XDR and must 1707 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1708 * if the XDR should be included in "count", but to be safe, we do. 1709 * (Include the two booleans at the end of the reply in dirlen now.) 1710 */ 1711 if (nd->nd_flag & ND_NFSV3) { 1712 nfsrv_postopattr(nd, getret, &at); 1713 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1714 txdr_hyper(at.na_filerev, tl); 1715 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1716 } else { 1717 dirlen = 2 * NFSX_UNSIGNED; 1718 } 1719 1720 /* Loop through the records and build reply */ 1721 while (cpos < cend && ncookies > 0) { 1722 nlen = dp->d_namlen; 1723 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1724 nlen <= NFS_MAXNAMLEN) { 1725 if (nd->nd_flag & ND_NFSV3) 1726 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1727 else 1728 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1729 if (dirlen > cnt) { 1730 eofflag = 0; 1731 break; 1732 } 1733 1734 /* 1735 * Build the directory record xdr from 1736 * the dirent entry. 1737 */ 1738 if (nd->nd_flag & ND_NFSV3) { 1739 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1740 *tl++ = newnfs_true; 1741 *tl++ = 0; 1742 } else { 1743 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1744 *tl++ = newnfs_true; 1745 } 1746 *tl = txdr_unsigned(dp->d_fileno); 1747 (void) nfsm_strtom(nd, dp->d_name, nlen); 1748 if (nd->nd_flag & ND_NFSV3) { 1749 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1750 *tl++ = 0; 1751 } else 1752 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1753 *tl = txdr_unsigned(*cookiep); 1754 } 1755 cpos += dp->d_reclen; 1756 dp = (struct dirent *)cpos; 1757 cookiep++; 1758 ncookies--; 1759 } 1760 if (cpos < cend) 1761 eofflag = 0; 1762 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1763 *tl++ = newnfs_false; 1764 if (eofflag) 1765 *tl = newnfs_true; 1766 else 1767 *tl = newnfs_false; 1768 FREE((caddr_t)rbuf, M_TEMP); 1769 FREE((caddr_t)cookies, M_TEMP); 1770 1771out: 1772 NFSEXITCODE2(0, nd); 1773 return (0); 1774nfsmout: 1775 vput(vp); 1776 NFSEXITCODE2(error, nd); 1777 return (error); 1778} 1779 1780/* 1781 * Readdirplus for V3 and Readdir for V4. 1782 */ 1783int 1784nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 1785 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1786{ 1787 struct dirent *dp; 1788 u_int32_t *tl; 1789 int dirlen; 1790 char *cpos, *cend, *rbuf; 1791 struct vnode *nvp; 1792 fhandle_t nfh; 1793 struct nfsvattr nva, at, *nvap = &nva; 1794 struct mbuf *mb0, *mb1; 1795 struct nfsreferral *refp; 1796 int nlen, r, error = 0, getret = 1, usevget = 1; 1797 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 1798 caddr_t bpos0, bpos1; 1799 u_int64_t off, toff, verf; 1800 u_long *cookies = NULL, *cookiep; 1801 nfsattrbit_t attrbits, rderrbits, savbits; 1802 struct uio io; 1803 struct iovec iv; 1804 struct componentname cn; 1805 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls; 1806 struct mount *mp, *new_mp; 1807 uint64_t mounted_on_fileno; 1808 1809 if (nd->nd_repstat) { 1810 nfsrv_postopattr(nd, getret, &at); 1811 goto out; 1812 } 1813 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 1814 off = fxdr_hyper(tl); 1815 toff = off; 1816 tl += 2; 1817 verf = fxdr_hyper(tl); 1818 tl += 2; 1819 siz = fxdr_unsigned(int, *tl++); 1820 cnt = fxdr_unsigned(int, *tl); 1821 1822 /* 1823 * Use the server's maximum data transfer size as the upper bound 1824 * on reply datalen. 1825 */ 1826 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1827 cnt = NFS_SRVMAXDATA(nd); 1828 1829 /* 1830 * siz is a "hint" of how much directory information (name, fileid, 1831 * cookie) should be in the reply. At least one client "hints" 0, 1832 * so I set it to cnt for that case. I also round it up to the 1833 * next multiple of DIRBLKSIZ. 1834 */ 1835 if (siz <= 0) 1836 siz = cnt; 1837 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1838 1839 if (nd->nd_flag & ND_NFSV4) { 1840 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1841 if (error) 1842 goto nfsmout; 1843 NFSSET_ATTRBIT(&savbits, &attrbits); 1844 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits); 1845 NFSZERO_ATTRBIT(&rderrbits); 1846 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 1847 } else { 1848 NFSZERO_ATTRBIT(&attrbits); 1849 } 1850 fullsiz = siz; 1851 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1852 if (!nd->nd_repstat) { 1853 if (off && verf != at.na_filerev) { 1854 /* 1855 * va_filerev is not sufficient as a cookie verifier, 1856 * since it is not supposed to change when entries are 1857 * removed/added unless that offset cookies returned to 1858 * the client are no longer valid. 1859 */ 1860#if 0 1861 if (nd->nd_flag & ND_NFSV4) { 1862 nd->nd_repstat = NFSERR_NOTSAME; 1863 } else { 1864 nd->nd_repstat = NFSERR_BAD_COOKIE; 1865 } 1866#endif 1867 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) { 1868 nd->nd_repstat = NFSERR_BAD_COOKIE; 1869 } 1870 } 1871 if (!nd->nd_repstat && vp->v_type != VDIR) 1872 nd->nd_repstat = NFSERR_NOTDIR; 1873 if (!nd->nd_repstat && cnt == 0) 1874 nd->nd_repstat = NFSERR_TOOSMALL; 1875 if (!nd->nd_repstat) 1876 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1877 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1878 NFSACCCHK_VPISLOCKED, NULL); 1879 if (nd->nd_repstat) { 1880 vput(vp); 1881 if (nd->nd_flag & ND_NFSV3) 1882 nfsrv_postopattr(nd, getret, &at); 1883 goto out; 1884 } 1885 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 1886 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0; 1887 1888 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK); 1889again: 1890 eofflag = 0; 1891 if (cookies) { 1892 free((caddr_t)cookies, M_TEMP); 1893 cookies = NULL; 1894 } 1895 1896 iv.iov_base = rbuf; 1897 iv.iov_len = siz; 1898 io.uio_iov = &iv; 1899 io.uio_iovcnt = 1; 1900 io.uio_offset = (off_t)off; 1901 io.uio_resid = siz; 1902 io.uio_segflg = UIO_SYSSPACE; 1903 io.uio_rw = UIO_READ; 1904 io.uio_td = NULL; 1905 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1906 &cookies); 1907 off = (u_int64_t)io.uio_offset; 1908 if (io.uio_resid) 1909 siz -= io.uio_resid; 1910 1911 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1912 1913 if (!cookies && !nd->nd_repstat) 1914 nd->nd_repstat = NFSERR_PERM; 1915 if (!nd->nd_repstat) 1916 nd->nd_repstat = getret; 1917 if (nd->nd_repstat) { 1918 vput(vp); 1919 if (cookies) 1920 free((caddr_t)cookies, M_TEMP); 1921 free((caddr_t)rbuf, M_TEMP); 1922 if (nd->nd_flag & ND_NFSV3) 1923 nfsrv_postopattr(nd, getret, &at); 1924 goto out; 1925 } 1926 /* 1927 * If nothing read, return eof 1928 * rpc reply 1929 */ 1930 if (siz == 0) { 1931 vput(vp); 1932 if (nd->nd_flag & ND_NFSV3) 1933 nfsrv_postopattr(nd, getret, &at); 1934 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1935 txdr_hyper(at.na_filerev, tl); 1936 tl += 2; 1937 *tl++ = newnfs_false; 1938 *tl = newnfs_true; 1939 free((caddr_t)cookies, M_TEMP); 1940 free((caddr_t)rbuf, M_TEMP); 1941 goto out; 1942 } 1943 1944 /* 1945 * Check for degenerate cases of nothing useful read. 1946 * If so go try again 1947 */ 1948 cpos = rbuf; 1949 cend = rbuf + siz; 1950 dp = (struct dirent *)cpos; 1951 cookiep = cookies; 1952 1953 /* 1954 * For some reason FreeBSD's ufs_readdir() chooses to back the 1955 * directory offset up to a block boundary, so it is necessary to 1956 * skip over the records that precede the requested offset. This 1957 * requires the assumption that file offset cookies monotonically 1958 * increase. 1959 */ 1960 while (cpos < cend && ncookies > 0 && 1961 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1962 (is_ufs == 1 && ((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 (is_zfs == 1) { 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 && is_zfs == 1 && 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