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