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 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/condvar.h> 37 #include <sys/dirent.h> 38 #include <sys/fcntl.h> 39 #include <sys/lock.h> 40 #include <sys/mount.h> 41 #include <sys/mutex.h> 42 #include <sys/namei.h> 43 #include <sys/signalvar.h> 44 #include <sys/systm.h> 45 #include <sys/vnode.h> 46 #include <machine/atomic.h> 47 #include <vm/uma.h> 48 49 #include <fs/autofs/autofs.h> 50 51 static int autofs_trigger_vn(struct vnode *vp, const char *path, 52 int pathlen, struct vnode **newvp); 53 54 extern struct autofs_softc *autofs_softc; 55 56 static int 57 autofs_access(struct vop_access_args *ap) 58 { 59 60 /* 61 * Nothing to do here; the only kind of access control 62 * needed is in autofs_mkdir(). 63 */ 64 65 return (0); 66 } 67 68 static int 69 autofs_getattr(struct vop_getattr_args *ap) 70 { 71 struct vnode *vp, *newvp; 72 struct autofs_node *anp; 73 struct mount *mp; 74 struct vattr *vap; 75 int error; 76 77 vp = ap->a_vp; 78 anp = vp->v_data; 79 mp = vp->v_mount; 80 vap = ap->a_vap; 81 82 KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR")); 83 84 /* 85 * The reason we must do this is that some tree-walking software, 86 * namely fts(3), assumes that stat(".") results will not change 87 * between chdir("subdir") and chdir(".."), and fails with ENOENT 88 * otherwise. 89 */ 90 if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false && 91 autofs_ignore_thread(curthread) == false) { 92 error = autofs_trigger_vn(vp, "", 0, &newvp); 93 if (error != 0) 94 return (error); 95 96 if (newvp != NULL) { 97 error = VOP_GETATTR(newvp, ap->a_vap, 98 ap->a_cred); 99 vput(newvp); 100 return (error); 101 } 102 } 103 104 vap->va_type = VDIR; 105 vap->va_mode = 0755; 106 vap->va_nlink = 3; /* XXX */ 107 vap->va_uid = 0; 108 vap->va_gid = 0; 109 vap->va_rdev = NODEV; 110 vap->va_fsid = mp->mnt_stat.f_fsid.val[0]; 111 vap->va_fileid = anp->an_fileno; 112 vap->va_size = 512; /* XXX */ 113 vap->va_blocksize = 512; 114 vap->va_mtime = anp->an_ctime; 115 vap->va_atime = anp->an_ctime; 116 vap->va_ctime = anp->an_ctime; 117 vap->va_birthtime = anp->an_ctime; 118 vap->va_gen = 0; 119 vap->va_flags = 0; 120 vap->va_rdev = 0; 121 vap->va_bytes = 512; /* XXX */ 122 vap->va_filerev = 0; 123 vap->va_spare = 0; 124 125 return (0); 126 } 127 128 /* 129 * Unlock the vnode, request automountd(8) action, and then lock it back. 130 * If anything got mounted on top of the vnode, return the new filesystem's 131 * root vnode in 'newvp', locked. 132 */ 133 static int 134 autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen, 135 struct vnode **newvp) 136 { 137 struct autofs_node *anp; 138 struct autofs_mount *amp; 139 int error, lock_flags; 140 141 anp = vp->v_data; 142 amp = VFSTOAUTOFS(vp->v_mount); 143 144 /* 145 * Release the vnode lock, so that other operations, in partcular 146 * mounting a filesystem on top of it, can proceed. Increase use 147 * count, to prevent the vnode from being deallocated and to prevent 148 * filesystem from being unmounted. 149 */ 150 lock_flags = VOP_ISLOCKED(vp); 151 vref(vp); 152 VOP_UNLOCK(vp, 0); 153 154 sx_xlock(&autofs_softc->sc_lock); 155 156 /* 157 * XXX: Workaround for mounting the same thing multiple times; revisit. 158 */ 159 if (vp->v_mountedhere != NULL) { 160 error = 0; 161 goto mounted; 162 } 163 164 error = autofs_trigger(anp, path, pathlen); 165 mounted: 166 sx_xunlock(&autofs_softc->sc_lock); 167 vn_lock(vp, lock_flags | LK_RETRY); 168 vunref(vp); 169 if ((vp->v_iflag & VI_DOOMED) != 0) { 170 AUTOFS_DEBUG("VI_DOOMED"); 171 return (ENOENT); 172 } 173 174 if (error != 0) 175 return (error); 176 177 if (vp->v_mountedhere == NULL) { 178 *newvp = NULL; 179 return (0); 180 } else { 181 /* 182 * If the operation that succeeded was mount, then mark 183 * the node as non-cached. Otherwise, if someone unmounts 184 * the filesystem before the cache times out, we will fail 185 * to trigger. 186 */ 187 anp->an_cached = false; 188 } 189 190 error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp); 191 if (error != 0) { 192 AUTOFS_WARN("VFS_ROOT() failed with error %d", error); 193 return (error); 194 } 195 196 return (0); 197 } 198 199 static int 200 autofs_vget_callback(struct mount *mp, void *arg, int lkflags __unused, 201 struct vnode **vpp) 202 { 203 204 205 return (autofs_node_vn(arg, mp, vpp)); 206 } 207 208 static int 209 autofs_lookup(struct vop_lookup_args *ap) 210 { 211 struct vnode *dvp, *newvp, **vpp; 212 struct mount *mp; 213 struct autofs_mount *amp; 214 struct autofs_node *anp, *child; 215 struct componentname *cnp; 216 int error, lock_flags; 217 218 dvp = ap->a_dvp; 219 vpp = ap->a_vpp; 220 mp = dvp->v_mount; 221 amp = VFSTOAUTOFS(mp); 222 anp = dvp->v_data; 223 cnp = ap->a_cnp; 224 225 if (cnp->cn_flags & ISDOTDOT) { 226 KASSERT(anp->an_parent != NULL, ("NULL parent")); 227 /* 228 * Note that in this case, dvp is the child vnode, and we 229 * are looking up the parent vnode - exactly reverse from 230 * normal operation. Unlocking dvp requires some rather 231 * tricky unlock/relock dance to prevent mp from being freed; 232 * use vn_vget_ino_gen() which takes care of all that. 233 */ 234 error = vn_vget_ino_gen(dvp, autofs_vget_callback, 235 anp->an_parent, 0, vpp); 236 if (error != 0) { 237 AUTOFS_WARN("vn_vget_ino_gen() failed with error %d", 238 error); 239 return (error); 240 } 241 return (error); 242 } 243 244 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 245 vref(dvp); 246 *vpp = dvp; 247 248 return (0); 249 } 250 251 if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false && 252 autofs_ignore_thread(cnp->cn_thread) == false) { 253 error = autofs_trigger_vn(dvp, 254 cnp->cn_nameptr, cnp->cn_namelen, &newvp); 255 if (error != 0) 256 return (error); 257 258 if (newvp != NULL) { 259 error = VOP_LOOKUP(newvp, ap->a_vpp, ap->a_cnp); 260 261 /* 262 * Instead of figuring out whether our vnode should 263 * be locked or not given the error and cnp flags, 264 * just "copy" the lock status from vnode returned 265 * by mounted filesystem's VOP_LOOKUP(). Get rid 266 * of that new vnode afterwards. 267 */ 268 lock_flags = VOP_ISLOCKED(newvp); 269 if (lock_flags == 0) { 270 VOP_UNLOCK(dvp, 0); 271 vrele(newvp); 272 } else { 273 vput(newvp); 274 } 275 return (error); 276 } 277 } 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 (strlen(anp->an_name) != namelen) 548 continue; 549 if (strncmp(anp->an_name, name, namelen) != 0) 550 continue; 551 } else { 552 if (strcmp(anp->an_name, name) != 0) 553 continue; 554 } 555 556 if (anpp != NULL) 557 *anpp = anp; 558 return (0); 559 } 560 561 return (ENOENT); 562 } 563 564 void 565 autofs_node_delete(struct autofs_node *anp) 566 { 567 struct autofs_node *parent; 568 569 AUTOFS_ASSERT_LOCKED(anp->an_mount); 570 KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children")); 571 572 callout_drain(&anp->an_callout); 573 574 parent = anp->an_parent; 575 if (parent != NULL) 576 TAILQ_REMOVE(&parent->an_children, anp, an_next); 577 sx_destroy(&anp->an_vnode_lock); 578 free(anp->an_name, M_AUTOFS); 579 uma_zfree(autofs_node_zone, anp); 580 } 581 582 int 583 autofs_node_vn(struct autofs_node *anp, struct mount *mp, struct vnode **vpp) 584 { 585 struct vnode *vp; 586 int error; 587 588 AUTOFS_ASSERT_UNLOCKED(anp->an_mount); 589 590 sx_xlock(&anp->an_vnode_lock); 591 592 vp = anp->an_vnode; 593 if (vp != NULL) { 594 error = vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread); 595 if (error != 0) { 596 AUTOFS_WARN("vget failed with error %d", error); 597 sx_xunlock(&anp->an_vnode_lock); 598 return (error); 599 } 600 if (vp->v_iflag & VI_DOOMED) { 601 /* 602 * We got forcibly unmounted. 603 */ 604 AUTOFS_DEBUG("doomed vnode"); 605 sx_xunlock(&anp->an_vnode_lock); 606 vput(vp); 607 608 return (ENOENT); 609 } 610 611 *vpp = vp; 612 sx_xunlock(&anp->an_vnode_lock); 613 return (0); 614 } 615 616 error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp); 617 if (error != 0) { 618 sx_xunlock(&anp->an_vnode_lock); 619 return (error); 620 } 621 622 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 623 if (error != 0) { 624 sx_xunlock(&anp->an_vnode_lock); 625 vdrop(vp); 626 return (error); 627 } 628 629 vp->v_type = VDIR; 630 if (anp->an_parent == NULL) 631 vp->v_vflag |= VV_ROOT; 632 vp->v_data = anp; 633 634 error = insmntque(vp, mp); 635 if (error != 0) { 636 AUTOFS_WARN("insmntque() failed with error %d", error); 637 sx_xunlock(&anp->an_vnode_lock); 638 return (error); 639 } 640 641 KASSERT(anp->an_vnode == NULL, ("lost race")); 642 anp->an_vnode = vp; 643 644 sx_xunlock(&anp->an_vnode_lock); 645 646 *vpp = vp; 647 return (0); 648 } 649