1 /*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/condvar.h> 38 #include <sys/dirent.h> 39 #include <sys/fcntl.h> 40 #include <sys/lock.h> 41 #include <sys/mount.h> 42 #include <sys/mutex.h> 43 #include <sys/namei.h> 44 #include <sys/signalvar.h> 45 #include <sys/systm.h> 46 #include <sys/vnode.h> 47 #include <machine/atomic.h> 48 #include <vm/uma.h> 49 50 #include "autofs.h" 51 52 static int autofs_trigger_vn(struct vnode *vp, const char *path, 53 int pathlen, struct vnode **newvp); 54 55 static int 56 autofs_access(struct vop_access_args *ap) 57 { 58 59 /* 60 * Nothing to do here; the only kind of access control 61 * needed is in autofs_mkdir(). 62 */ 63 64 return (0); 65 } 66 67 static int 68 autofs_getattr(struct vop_getattr_args *ap) 69 { 70 struct vnode *vp, *newvp; 71 struct autofs_node *anp; 72 struct mount *mp; 73 struct vattr *vap; 74 int error; 75 76 vp = ap->a_vp; 77 anp = vp->v_data; 78 mp = vp->v_mount; 79 vap = ap->a_vap; 80 81 KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR")); 82 83 /* 84 * The reason we must do this is that some tree-walking software, 85 * namely fts(3), assumes that stat(".") results will not change 86 * between chdir("subdir") and chdir(".."), and fails with ENOENT 87 * otherwise. 88 */ 89 if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false && 90 autofs_ignore_thread(curthread) == false) { 91 error = autofs_trigger_vn(vp, "", 0, &newvp); 92 if (error != 0) 93 return (error); 94 95 if (newvp != NULL) { 96 error = VOP_GETATTR(newvp, ap->a_vap, 97 ap->a_cred); 98 vput(newvp); 99 return (error); 100 } 101 } 102 103 vap->va_type = VDIR; 104 vap->va_mode = 0755; 105 vap->va_nlink = 3; /* XXX */ 106 vap->va_uid = 0; 107 vap->va_gid = 0; 108 vap->va_rdev = NODEV; 109 vap->va_fsid = mp->mnt_stat.f_fsid.val[0]; 110 vap->va_fileid = anp->an_fileno; 111 vap->va_size = 512; /* XXX */ 112 vap->va_blocksize = 512; 113 vap->va_mtime = anp->an_ctime; 114 vap->va_atime = anp->an_ctime; 115 vap->va_ctime = anp->an_ctime; 116 vap->va_birthtime = anp->an_ctime; 117 vap->va_gen = 0; 118 vap->va_flags = 0; 119 vap->va_rdev = 0; 120 vap->va_bytes = 512; /* XXX */ 121 vap->va_filerev = 0; 122 vap->va_spare = 0; 123 124 return (0); 125 } 126 127 /* 128 * Unlock the vnode, request automountd(8) action, and then lock it back. 129 * If anything got mounted on top of the vnode, return the new filesystem's 130 * root vnode in 'newvp', locked. 131 */ 132 static int 133 autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen, 134 struct vnode **newvp) 135 { 136 struct autofs_node *anp; 137 struct autofs_mount *amp; 138 struct autofs_softc *sc; 139 int error, lock_flags; 140 141 anp = vp->v_data; 142 amp = VFSTOAUTOFS(vp->v_mount); 143 sc = amp->am_softc; 144 145 /* 146 * Release the vnode lock, so that other operations, in partcular 147 * mounting a filesystem on top of it, can proceed. Increase use 148 * count, to prevent the vnode from being deallocated and to prevent 149 * filesystem from being unmounted. 150 */ 151 lock_flags = VOP_ISLOCKED(vp); 152 vref(vp); 153 VOP_UNLOCK(vp, 0); 154 155 sx_xlock(&sc->sc_lock); 156 157 /* 158 * XXX: Workaround for mounting the same thing multiple times; revisit. 159 */ 160 if (vp->v_mountedhere != NULL) { 161 error = 0; 162 goto mounted; 163 } 164 165 error = autofs_trigger(anp, path, pathlen); 166 mounted: 167 sx_xunlock(&sc->sc_lock); 168 vn_lock(vp, lock_flags | LK_RETRY); 169 vunref(vp); 170 if ((vp->v_iflag & VI_DOOMED) != 0) { 171 AUTOFS_DEBUG("VI_DOOMED"); 172 return (ENOENT); 173 } 174 175 if (error != 0) 176 return (error); 177 178 if (vp->v_mountedhere == NULL) { 179 *newvp = NULL; 180 return (0); 181 } else { 182 /* 183 * If the operation that succeeded was mount, then mark 184 * the node as non-cached. Otherwise, if someone unmounts 185 * the filesystem before the cache times out, we will fail 186 * to trigger. 187 */ 188 anp->an_cached = false; 189 } 190 191 error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp); 192 if (error != 0) { 193 AUTOFS_WARN("VFS_ROOT() failed with error %d", error); 194 return (error); 195 } 196 197 return (0); 198 } 199 200 static int 201 autofs_lookup(struct vop_lookup_args *ap) 202 { 203 struct vnode *dvp, *newvp, **vpp; 204 struct mount *mp; 205 struct autofs_mount *amp; 206 struct autofs_node *anp, *child; 207 struct componentname *cnp; 208 int error, lock_flags; 209 210 dvp = ap->a_dvp; 211 vpp = ap->a_vpp; 212 mp = dvp->v_mount; 213 amp = VFSTOAUTOFS(mp); 214 anp = dvp->v_data; 215 cnp = ap->a_cnp; 216 217 if (cnp->cn_flags & ISDOTDOT) { 218 KASSERT(anp->an_parent != NULL, ("NULL parent")); 219 /* 220 * Note that in this case, dvp is the child vnode, and we are 221 * looking up the parent vnode - exactly reverse from normal 222 * operation. To preserve lock order, we unlock the child 223 * (dvp), obtain the lock on parent (*vpp) in autofs_node_vn(), 224 * then relock the child. We use vhold()/vdrop() to prevent 225 * dvp from being freed in the meantime. 226 */ 227 lock_flags = VOP_ISLOCKED(dvp); 228 vhold(dvp); 229 VOP_UNLOCK(dvp, 0); 230 error = autofs_node_vn(anp->an_parent, mp, vpp); 231 if (error != 0) { 232 AUTOFS_WARN("autofs_node_vn() failed with error %d", 233 error); 234 } 235 vn_lock(dvp, lock_flags | LK_RETRY); 236 vdrop(dvp); 237 238 return (error); 239 } 240 241 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 242 vref(dvp); 243 *vpp = dvp; 244 245 return (0); 246 } 247 248 if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false && 249 autofs_ignore_thread(cnp->cn_thread) == false) { 250 error = autofs_trigger_vn(dvp, 251 cnp->cn_nameptr, cnp->cn_namelen, &newvp); 252 if (error != 0) 253 return (error); 254 255 if (newvp != NULL) { 256 error = VOP_LOOKUP(newvp, ap->a_vpp, ap->a_cnp); 257 258 /* 259 * Instead of figuring out whether our vnode should 260 * be locked or not given the error and cnp flags, 261 * just "copy" the lock status from vnode returned 262 * by mounted filesystem's VOP_LOOKUP(). Get rid 263 * of that new vnode afterwards. 264 */ 265 lock_flags = VOP_ISLOCKED(newvp); 266 if (lock_flags == 0) { 267 VOP_UNLOCK(dvp, 0); 268 vrele(newvp); 269 } else { 270 vput(newvp); 271 } 272 return (error); 273 } 274 } 275 276 if (cnp->cn_nameiop == RENAME) 277 return (EOPNOTSUPP); 278 279 AUTOFS_LOCK(amp); 280 error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child); 281 if (error != 0) { 282 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) { 283 AUTOFS_UNLOCK(amp); 284 return (EJUSTRETURN); 285 } 286 287 AUTOFS_UNLOCK(amp); 288 return (ENOENT); 289 } 290 291 /* 292 * XXX: Dropping the node here is ok, because we never remove nodes. 293 */ 294 AUTOFS_UNLOCK(amp); 295 296 error = autofs_node_vn(child, mp, vpp); 297 if (error != 0) { 298 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) 299 return (EJUSTRETURN); 300 301 return (error); 302 } 303 304 return (0); 305 } 306 307 static int 308 autofs_mkdir(struct vop_mkdir_args *ap) 309 { 310 struct vnode *vp; 311 struct autofs_node *anp; 312 struct autofs_mount *amp; 313 struct autofs_node *child; 314 int error; 315 316 vp = ap->a_dvp; 317 anp = vp->v_data; 318 amp = VFSTOAUTOFS(vp->v_mount); 319 320 /* 321 * Do not allow mkdir() if the calling thread is not 322 * automountd(8) descendant. 323 */ 324 if (autofs_ignore_thread(curthread) == false) 325 return (EPERM); 326 327 AUTOFS_LOCK(amp); 328 error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr, 329 ap->a_cnp->cn_namelen, &child); 330 if (error != 0) { 331 AUTOFS_UNLOCK(amp); 332 return (error); 333 } 334 AUTOFS_UNLOCK(amp); 335 336 error = autofs_node_vn(child, vp->v_mount, ap->a_vpp); 337 338 return (error); 339 } 340 341 static int 342 autofs_readdir_one(struct uio *uio, const char *name, int fileno) 343 { 344 struct dirent dirent; 345 int error, i; 346 347 memset(&dirent, 0, sizeof(dirent)); 348 dirent.d_type = DT_DIR; 349 dirent.d_reclen = AUTOFS_DELEN; 350 dirent.d_fileno = fileno; 351 /* PFS_DELEN was picked to fit PFS_NAMLEN */ 352 for (i = 0; i < AUTOFS_NAMELEN - 1 && name[i] != '\0'; ++i) 353 dirent.d_name[i] = name[i]; 354 dirent.d_name[i] = 0; 355 dirent.d_namlen = i; 356 357 error = uiomove(&dirent, AUTOFS_DELEN, uio); 358 return (error); 359 } 360 361 static int 362 autofs_readdir(struct vop_readdir_args *ap) 363 { 364 struct vnode *vp, *newvp; 365 struct autofs_mount *amp; 366 struct autofs_node *anp, *child; 367 struct uio *uio; 368 off_t offset; 369 int error, i, resid; 370 371 vp = ap->a_vp; 372 amp = VFSTOAUTOFS(vp->v_mount); 373 anp = vp->v_data; 374 uio = ap->a_uio; 375 376 KASSERT(vp->v_type == VDIR, ("!VDIR")); 377 378 if (autofs_cached(anp, NULL, 0) == false && 379 autofs_ignore_thread(curthread) == false) { 380 error = autofs_trigger_vn(vp, "", 0, &newvp); 381 if (error != 0) 382 return (error); 383 384 if (newvp != NULL) { 385 error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred, 386 ap->a_eofflag, ap->a_ncookies, ap->a_cookies); 387 vput(newvp); 388 return (error); 389 } 390 } 391 392 /* only allow reading entire entries */ 393 offset = uio->uio_offset; 394 resid = uio->uio_resid; 395 if (offset < 0 || offset % AUTOFS_DELEN != 0 || 396 (resid && resid < AUTOFS_DELEN)) 397 return (EINVAL); 398 if (resid == 0) 399 return (0); 400 401 if (ap->a_eofflag != NULL) 402 *ap->a_eofflag = TRUE; 403 404 if (offset == 0 && resid >= AUTOFS_DELEN) { 405 error = autofs_readdir_one(uio, ".", anp->an_fileno); 406 if (error != 0) 407 return (error); 408 offset += AUTOFS_DELEN; 409 resid -= AUTOFS_DELEN; 410 } 411 412 if (offset == AUTOFS_DELEN && resid >= AUTOFS_DELEN) { 413 if (anp->an_parent == NULL) { 414 /* 415 * XXX: Right? 416 */ 417 error = autofs_readdir_one(uio, "..", anp->an_fileno); 418 } else { 419 error = autofs_readdir_one(uio, "..", 420 anp->an_parent->an_fileno); 421 } 422 if (error != 0) 423 return (error); 424 offset += AUTOFS_DELEN; 425 resid -= AUTOFS_DELEN; 426 } 427 428 i = 2; /* Account for "." and "..". */ 429 AUTOFS_LOCK(amp); 430 TAILQ_FOREACH(child, &anp->an_children, an_next) { 431 if (resid < AUTOFS_DELEN) { 432 if (ap->a_eofflag != NULL) 433 *ap->a_eofflag = 0; 434 break; 435 } 436 437 /* 438 * Skip entries returned by previous call to getdents(). 439 */ 440 i++; 441 if (i * AUTOFS_DELEN <= offset) 442 continue; 443 444 error = autofs_readdir_one(uio, child->an_name, 445 child->an_fileno); 446 if (error != 0) { 447 AUTOFS_UNLOCK(amp); 448 return (error); 449 } 450 offset += AUTOFS_DELEN; 451 resid -= AUTOFS_DELEN; 452 } 453 454 AUTOFS_UNLOCK(amp); 455 return (0); 456 } 457 458 static int 459 autofs_reclaim(struct vop_reclaim_args *ap) 460 { 461 struct vnode *vp = ap->a_vp; 462 struct autofs_node *anp = vp->v_data; 463 464 vp = ap->a_vp; 465 anp = vp->v_data; 466 467 /* 468 * We do not free autofs_node here; instead we are 469 * destroying them in autofs_node_delete(). 470 */ 471 sx_xlock(&anp->an_vnode_lock); 472 anp->an_vnode = NULL; 473 vp->v_data = NULL; 474 sx_xunlock(&anp->an_vnode_lock); 475 476 return (0); 477 } 478 479 struct vop_vector autofs_vnodeops = { 480 .vop_default = &default_vnodeops, 481 482 .vop_access = autofs_access, 483 .vop_lookup = autofs_lookup, 484 .vop_create = VOP_EOPNOTSUPP, 485 .vop_getattr = autofs_getattr, 486 .vop_link = VOP_EOPNOTSUPP, 487 .vop_mkdir = autofs_mkdir, 488 .vop_mknod = VOP_EOPNOTSUPP, 489 .vop_read = VOP_EOPNOTSUPP, 490 .vop_readdir = autofs_readdir, 491 .vop_remove = VOP_EOPNOTSUPP, 492 .vop_rename = VOP_EOPNOTSUPP, 493 .vop_rmdir = VOP_EOPNOTSUPP, 494 .vop_setattr = VOP_EOPNOTSUPP, 495 .vop_symlink = VOP_EOPNOTSUPP, 496 .vop_write = VOP_EOPNOTSUPP, 497 .vop_reclaim = autofs_reclaim, 498 }; 499 500 int 501 autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp, 502 const char *name, int namelen, struct autofs_node **anpp) 503 { 504 struct autofs_node *anp; 505 506 if (parent != NULL) 507 AUTOFS_ASSERT_LOCKED(parent->an_mount); 508 509 anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO); 510 if (namelen >= 0) 511 anp->an_name = strndup(name, namelen, M_AUTOFS); 512 else 513 anp->an_name = strdup(name, M_AUTOFS); 514 anp->an_fileno = atomic_fetchadd_int(&->am_last_fileno, 1); 515 callout_init(&anp->an_callout, 1); 516 /* 517 * The reason for SX_NOWITNESS here is that witness(4) 518 * cannot tell vnodes apart, so the following perfectly 519 * valid lock order... 520 * 521 * vnode lock A -> autofsvlk B -> vnode lock B 522 * 523 * ... gets reported as a LOR. 524 */ 525 sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS); 526 getnanotime(&anp->an_ctime); 527 anp->an_parent = parent; 528 anp->an_mount = amp; 529 if (parent != NULL) 530 TAILQ_INSERT_TAIL(&parent->an_children, anp, an_next); 531 TAILQ_INIT(&anp->an_children); 532 533 *anpp = anp; 534 return (0); 535 } 536 537 int 538 autofs_node_find(struct autofs_node *parent, const char *name, 539 int namelen, struct autofs_node **anpp) 540 { 541 struct autofs_node *anp; 542 543 AUTOFS_ASSERT_LOCKED(parent->an_mount); 544 545 TAILQ_FOREACH(anp, &parent->an_children, an_next) { 546 if (namelen >= 0) { 547 if (strncmp(anp->an_name, name, namelen) != 0) 548 continue; 549 } else { 550 if (strcmp(anp->an_name, name) != 0) 551 continue; 552 } 553 554 if (anpp != NULL) 555 *anpp = anp; 556 return (0); 557 } 558 559 return (ENOENT); 560 } 561 562 void 563 autofs_node_delete(struct autofs_node *anp) 564 { 565 struct autofs_node *parent; 566 567 AUTOFS_ASSERT_LOCKED(anp->an_mount); 568 KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children")); 569 570 callout_drain(&anp->an_callout); 571 572 parent = anp->an_parent; 573 if (parent != NULL) 574 TAILQ_REMOVE(&parent->an_children, anp, an_next); 575 sx_destroy(&anp->an_vnode_lock); 576 free(anp->an_name, M_AUTOFS); 577 uma_zfree(autofs_node_zone, anp); 578 } 579 580 int 581 autofs_node_vn(struct autofs_node *anp, struct mount *mp, struct vnode **vpp) 582 { 583 struct vnode *vp; 584 int error; 585 586 AUTOFS_ASSERT_UNLOCKED(anp->an_mount); 587 588 sx_xlock(&anp->an_vnode_lock); 589 590 vp = anp->an_vnode; 591 if (vp != NULL) { 592 error = vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread); 593 if (error != 0) { 594 AUTOFS_WARN("vget failed with error %d", error); 595 sx_xunlock(&anp->an_vnode_lock); 596 return (error); 597 } 598 if (vp->v_iflag & VI_DOOMED) { 599 /* 600 * We got forcibly unmounted. 601 */ 602 AUTOFS_DEBUG("doomed vnode"); 603 sx_xunlock(&anp->an_vnode_lock); 604 vput(vp); 605 606 return (ENOENT); 607 } 608 609 *vpp = vp; 610 sx_xunlock(&anp->an_vnode_lock); 611 return (0); 612 } 613 614 error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp); 615 if (error != 0) { 616 sx_xunlock(&anp->an_vnode_lock); 617 return (error); 618 } 619 620 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 621 if (error != 0) { 622 sx_xunlock(&anp->an_vnode_lock); 623 vdrop(vp); 624 return (error); 625 } 626 627 vp->v_type = VDIR; 628 if (anp->an_parent == NULL) 629 vp->v_vflag |= VV_ROOT; 630 vp->v_data = anp; 631 632 error = insmntque(vp, mp); 633 if (error != 0) { 634 AUTOFS_WARN("insmntque() failed with error %d", error); 635 sx_xunlock(&anp->an_vnode_lock); 636 return (error); 637 } 638 639 KASSERT(anp->an_vnode == NULL, ("lost race")); 640 anp->an_vnode = vp; 641 642 sx_xunlock(&anp->an_vnode_lock); 643 644 *vpp = vp; 645 return (0); 646 } 647