1270096Strasz/*- 2332596Strasz * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3332596Strasz * 4270096Strasz * Copyright (c) 2014 The FreeBSD Foundation 5270096Strasz * All rights reserved. 6270096Strasz * 7270096Strasz * This software was developed by Edward Tomasz Napierala under sponsorship 8270096Strasz * from the FreeBSD Foundation. 9270096Strasz * 10270096Strasz * Redistribution and use in source and binary forms, with or without 11270096Strasz * modification, are permitted provided that the following conditions 12270096Strasz * are met: 13270096Strasz * 1. Redistributions of source code must retain the above copyright 14270096Strasz * notice, this list of conditions and the following disclaimer. 15270096Strasz * 2. Redistributions in binary form must reproduce the above copyright 16270096Strasz * notice, this list of conditions and the following disclaimer in the 17270096Strasz * documentation and/or other materials provided with the distribution. 18270096Strasz * 19270096Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22270096Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29270096Strasz * SUCH DAMAGE. 30270096Strasz * 31270096Strasz */ 32270096Strasz 33270096Strasz#include <sys/cdefs.h> 34270096Strasz__FBSDID("$FreeBSD: stable/11/sys/fs/autofs/autofs_vnops.c 341074 2018-11-27 16:51:18Z markj $"); 35270096Strasz 36270096Strasz#include <sys/param.h> 37341074Smarkj#include <sys/systm.h> 38270096Strasz#include <sys/kernel.h> 39270096Strasz#include <sys/condvar.h> 40270096Strasz#include <sys/dirent.h> 41270096Strasz#include <sys/fcntl.h> 42270096Strasz#include <sys/lock.h> 43270096Strasz#include <sys/mount.h> 44270096Strasz#include <sys/mutex.h> 45270096Strasz#include <sys/namei.h> 46270096Strasz#include <sys/signalvar.h> 47296718Strasz#include <sys/stat.h> 48272403Strasz#include <sys/taskqueue.h> 49297236Strasz#include <sys/tree.h> 50270096Strasz#include <sys/vnode.h> 51270096Strasz#include <machine/atomic.h> 52270096Strasz#include <vm/uma.h> 53270096Strasz 54270281Strasz#include <fs/autofs/autofs.h> 55270096Strasz 56270096Straszstatic int autofs_trigger_vn(struct vnode *vp, const char *path, 57270096Strasz int pathlen, struct vnode **newvp); 58270096Strasz 59270402Straszextern struct autofs_softc *autofs_softc; 60270402Strasz 61270096Straszstatic int 62270096Straszautofs_access(struct vop_access_args *ap) 63270096Strasz{ 64270096Strasz 65270096Strasz /* 66270096Strasz * Nothing to do here; the only kind of access control 67270096Strasz * needed is in autofs_mkdir(). 68270096Strasz */ 69270096Strasz 70270096Strasz return (0); 71270096Strasz} 72270096Strasz 73270096Straszstatic int 74270096Straszautofs_getattr(struct vop_getattr_args *ap) 75270096Strasz{ 76270096Strasz struct vnode *vp, *newvp; 77270096Strasz struct autofs_node *anp; 78270096Strasz struct mount *mp; 79270096Strasz struct vattr *vap; 80270096Strasz int error; 81270096Strasz 82270096Strasz vp = ap->a_vp; 83270096Strasz anp = vp->v_data; 84270096Strasz mp = vp->v_mount; 85270096Strasz vap = ap->a_vap; 86270096Strasz 87270096Strasz KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR")); 88270096Strasz 89270096Strasz /* 90270096Strasz * The reason we must do this is that some tree-walking software, 91270096Strasz * namely fts(3), assumes that stat(".") results will not change 92270096Strasz * between chdir("subdir") and chdir(".."), and fails with ENOENT 93270096Strasz * otherwise. 94270096Strasz */ 95270096Strasz if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false && 96270096Strasz autofs_ignore_thread(curthread) == false) { 97270096Strasz error = autofs_trigger_vn(vp, "", 0, &newvp); 98270096Strasz if (error != 0) 99270096Strasz return (error); 100270096Strasz 101270096Strasz if (newvp != NULL) { 102270096Strasz error = VOP_GETATTR(newvp, ap->a_vap, 103270096Strasz ap->a_cred); 104270096Strasz vput(newvp); 105270096Strasz return (error); 106270096Strasz } 107270096Strasz } 108270096Strasz 109270096Strasz vap->va_type = VDIR; 110270096Strasz vap->va_mode = 0755; 111270096Strasz vap->va_nlink = 3; /* XXX */ 112270096Strasz vap->va_uid = 0; 113270096Strasz vap->va_gid = 0; 114270096Strasz vap->va_rdev = NODEV; 115270096Strasz vap->va_fsid = mp->mnt_stat.f_fsid.val[0]; 116270096Strasz vap->va_fileid = anp->an_fileno; 117296718Strasz vap->va_size = S_BLKSIZE; 118296718Strasz vap->va_blocksize = S_BLKSIZE; 119270096Strasz vap->va_mtime = anp->an_ctime; 120270096Strasz vap->va_atime = anp->an_ctime; 121270096Strasz vap->va_ctime = anp->an_ctime; 122270096Strasz vap->va_birthtime = anp->an_ctime; 123270096Strasz vap->va_gen = 0; 124270096Strasz vap->va_flags = 0; 125270096Strasz vap->va_rdev = 0; 126296718Strasz vap->va_bytes = S_BLKSIZE; 127270096Strasz vap->va_filerev = 0; 128270096Strasz vap->va_spare = 0; 129270096Strasz 130270096Strasz return (0); 131270096Strasz} 132270096Strasz 133270096Strasz/* 134270096Strasz * Unlock the vnode, request automountd(8) action, and then lock it back. 135270096Strasz * If anything got mounted on top of the vnode, return the new filesystem's 136270096Strasz * root vnode in 'newvp', locked. 137270096Strasz */ 138270096Straszstatic int 139270096Straszautofs_trigger_vn(struct vnode *vp, const char *path, int pathlen, 140270096Strasz struct vnode **newvp) 141270096Strasz{ 142270096Strasz struct autofs_node *anp; 143270096Strasz int error, lock_flags; 144270096Strasz 145270096Strasz anp = vp->v_data; 146270096Strasz 147270096Strasz /* 148270096Strasz * Release the vnode lock, so that other operations, in partcular 149270096Strasz * mounting a filesystem on top of it, can proceed. Increase use 150270096Strasz * count, to prevent the vnode from being deallocated and to prevent 151270096Strasz * filesystem from being unmounted. 152270096Strasz */ 153270096Strasz lock_flags = VOP_ISLOCKED(vp); 154270096Strasz vref(vp); 155270096Strasz VOP_UNLOCK(vp, 0); 156270096Strasz 157270402Strasz sx_xlock(&autofs_softc->sc_lock); 158270096Strasz 159270096Strasz /* 160270096Strasz * XXX: Workaround for mounting the same thing multiple times; revisit. 161270096Strasz */ 162270096Strasz if (vp->v_mountedhere != NULL) { 163270096Strasz error = 0; 164270096Strasz goto mounted; 165270096Strasz } 166270096Strasz 167270096Strasz error = autofs_trigger(anp, path, pathlen); 168270096Straszmounted: 169270402Strasz sx_xunlock(&autofs_softc->sc_lock); 170270096Strasz vn_lock(vp, lock_flags | LK_RETRY); 171270096Strasz vunref(vp); 172270096Strasz if ((vp->v_iflag & VI_DOOMED) != 0) { 173270096Strasz AUTOFS_DEBUG("VI_DOOMED"); 174270096Strasz return (ENOENT); 175270096Strasz } 176270096Strasz 177270096Strasz if (error != 0) 178270096Strasz return (error); 179270096Strasz 180270096Strasz if (vp->v_mountedhere == NULL) { 181270096Strasz *newvp = NULL; 182270096Strasz return (0); 183270096Strasz } else { 184270096Strasz /* 185270096Strasz * If the operation that succeeded was mount, then mark 186270096Strasz * the node as non-cached. Otherwise, if someone unmounts 187270096Strasz * the filesystem before the cache times out, we will fail 188270096Strasz * to trigger. 189270096Strasz */ 190270096Strasz anp->an_cached = false; 191270096Strasz } 192270096Strasz 193270096Strasz error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp); 194270096Strasz if (error != 0) { 195270096Strasz AUTOFS_WARN("VFS_ROOT() failed with error %d", error); 196270096Strasz return (error); 197270096Strasz } 198270096Strasz 199270096Strasz return (0); 200270096Strasz} 201270096Strasz 202270096Straszstatic int 203272512Straszautofs_vget_callback(struct mount *mp, void *arg, int flags, 204270207Strasz struct vnode **vpp) 205270207Strasz{ 206270207Strasz 207270207Strasz 208272512Strasz return (autofs_node_vn(arg, mp, flags, vpp)); 209270207Strasz} 210270207Strasz 211270207Straszstatic int 212270096Straszautofs_lookup(struct vop_lookup_args *ap) 213270096Strasz{ 214270096Strasz struct vnode *dvp, *newvp, **vpp; 215270096Strasz struct mount *mp; 216270096Strasz struct autofs_mount *amp; 217270096Strasz struct autofs_node *anp, *child; 218270096Strasz struct componentname *cnp; 219296715Strasz int error; 220270096Strasz 221270096Strasz dvp = ap->a_dvp; 222270096Strasz vpp = ap->a_vpp; 223270096Strasz mp = dvp->v_mount; 224270096Strasz amp = VFSTOAUTOFS(mp); 225270096Strasz anp = dvp->v_data; 226270096Strasz cnp = ap->a_cnp; 227270096Strasz 228270096Strasz if (cnp->cn_flags & ISDOTDOT) { 229270096Strasz KASSERT(anp->an_parent != NULL, ("NULL parent")); 230270096Strasz /* 231270207Strasz * Note that in this case, dvp is the child vnode, and we 232270207Strasz * are looking up the parent vnode - exactly reverse from 233270207Strasz * normal operation. Unlocking dvp requires some rather 234270207Strasz * tricky unlock/relock dance to prevent mp from being freed; 235270207Strasz * use vn_vget_ino_gen() which takes care of all that. 236270096Strasz */ 237270207Strasz error = vn_vget_ino_gen(dvp, autofs_vget_callback, 238272512Strasz anp->an_parent, cnp->cn_lkflags, vpp); 239270096Strasz if (error != 0) { 240270207Strasz AUTOFS_WARN("vn_vget_ino_gen() failed with error %d", 241270096Strasz error); 242270207Strasz return (error); 243270096Strasz } 244270096Strasz return (error); 245270096Strasz } 246270096Strasz 247270096Strasz if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 248270096Strasz vref(dvp); 249270096Strasz *vpp = dvp; 250270096Strasz 251270096Strasz return (0); 252270096Strasz } 253270096Strasz 254270096Strasz if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false && 255270096Strasz autofs_ignore_thread(cnp->cn_thread) == false) { 256270096Strasz error = autofs_trigger_vn(dvp, 257270096Strasz cnp->cn_nameptr, cnp->cn_namelen, &newvp); 258270096Strasz if (error != 0) 259270096Strasz return (error); 260270096Strasz 261270096Strasz if (newvp != NULL) { 262270096Strasz /* 263296715Strasz * The target filesystem got automounted. 264296715Strasz * Let the lookup(9) go around with the same 265296715Strasz * path component. 266270096Strasz */ 267296715Strasz vput(newvp); 268296715Strasz return (ERELOOKUP); 269270096Strasz } 270270096Strasz } 271270096Strasz 272272470Strasz AUTOFS_SLOCK(amp); 273270096Strasz error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child); 274270096Strasz if (error != 0) { 275270096Strasz if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) { 276272470Strasz AUTOFS_SUNLOCK(amp); 277270096Strasz return (EJUSTRETURN); 278270096Strasz } 279270096Strasz 280272470Strasz AUTOFS_SUNLOCK(amp); 281270096Strasz return (ENOENT); 282270096Strasz } 283270096Strasz 284270096Strasz /* 285270096Strasz * XXX: Dropping the node here is ok, because we never remove nodes. 286270096Strasz */ 287272470Strasz AUTOFS_SUNLOCK(amp); 288270096Strasz 289272512Strasz error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp); 290270096Strasz if (error != 0) { 291270096Strasz if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) 292270096Strasz return (EJUSTRETURN); 293270096Strasz 294270096Strasz return (error); 295270096Strasz } 296270096Strasz 297270096Strasz return (0); 298270096Strasz} 299270096Strasz 300270096Straszstatic int 301270096Straszautofs_mkdir(struct vop_mkdir_args *ap) 302270096Strasz{ 303270096Strasz struct vnode *vp; 304270096Strasz struct autofs_node *anp; 305270096Strasz struct autofs_mount *amp; 306270096Strasz struct autofs_node *child; 307270096Strasz int error; 308270096Strasz 309270096Strasz vp = ap->a_dvp; 310270096Strasz anp = vp->v_data; 311270096Strasz amp = VFSTOAUTOFS(vp->v_mount); 312270096Strasz 313270096Strasz /* 314270096Strasz * Do not allow mkdir() if the calling thread is not 315270096Strasz * automountd(8) descendant. 316270096Strasz */ 317270096Strasz if (autofs_ignore_thread(curthread) == false) 318270096Strasz return (EPERM); 319270096Strasz 320272470Strasz AUTOFS_XLOCK(amp); 321270096Strasz error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr, 322270096Strasz ap->a_cnp->cn_namelen, &child); 323270096Strasz if (error != 0) { 324272470Strasz AUTOFS_XUNLOCK(amp); 325270096Strasz return (error); 326270096Strasz } 327272470Strasz AUTOFS_XUNLOCK(amp); 328270096Strasz 329272512Strasz error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp); 330270096Strasz 331270096Strasz return (error); 332270096Strasz} 333270096Strasz 334308253Straszstatic int 335308253Straszautofs_print(struct vop_print_args *ap) 336308253Strasz{ 337308253Strasz struct vnode *vp; 338308253Strasz struct autofs_node *anp; 339308253Strasz 340308253Strasz vp = ap->a_vp; 341308253Strasz anp = vp->v_data; 342308253Strasz 343308253Strasz printf(" name \"%s\", fileno %d, cached %d, wildcards %d\n", 344308253Strasz anp->an_name, anp->an_fileno, anp->an_cached, anp->an_wildcards); 345308253Strasz 346308253Strasz return (0); 347308253Strasz} 348308253Strasz 349296798Strasz/* 350296798Strasz * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments. 351296798Strasz */ 352270096Straszstatic int 353296798Straszautofs_readdir_one(struct uio *uio, const char *name, int fileno, 354296798Strasz size_t *reclenp) 355270096Strasz{ 356270096Strasz struct dirent dirent; 357296798Strasz size_t namlen, padded_namlen, reclen; 358296798Strasz int error; 359270096Strasz 360296798Strasz namlen = strlen(name); 361296798Strasz padded_namlen = roundup2(namlen + 1, __alignof(struct dirent)); 362296798Strasz KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen)); 363296798Strasz reclen = offsetof(struct dirent, d_name) + padded_namlen; 364296798Strasz if (reclenp != NULL) 365296798Strasz *reclenp = reclen; 366296798Strasz 367296798Strasz if (uio == NULL) 368296798Strasz return (0); 369296798Strasz 370296798Strasz if (uio->uio_resid < reclen) 371296798Strasz return (EINVAL); 372296798Strasz 373296798Strasz dirent.d_fileno = fileno; 374296798Strasz dirent.d_reclen = reclen; 375270096Strasz dirent.d_type = DT_DIR; 376296798Strasz dirent.d_namlen = namlen; 377296798Strasz memcpy(dirent.d_name, name, namlen); 378341074Smarkj dirent_terminate(&dirent); 379296798Strasz error = uiomove(&dirent, reclen, uio); 380270096Strasz 381270096Strasz return (error); 382270096Strasz} 383270096Strasz 384296798Straszstatic size_t 385296798Straszautofs_dirent_reclen(const char *name) 386296798Strasz{ 387296798Strasz size_t reclen; 388296798Strasz 389296937Strasz (void)autofs_readdir_one(NULL, name, -1, &reclen); 390296798Strasz 391296798Strasz return (reclen); 392296798Strasz} 393296798Strasz 394270096Straszstatic int 395270096Straszautofs_readdir(struct vop_readdir_args *ap) 396270096Strasz{ 397270096Strasz struct vnode *vp, *newvp; 398270096Strasz struct autofs_mount *amp; 399270096Strasz struct autofs_node *anp, *child; 400270096Strasz struct uio *uio; 401296798Strasz size_t reclen, reclens; 402296798Strasz ssize_t initial_resid; 403296798Strasz int error; 404270096Strasz 405270096Strasz vp = ap->a_vp; 406270096Strasz amp = VFSTOAUTOFS(vp->v_mount); 407270096Strasz anp = vp->v_data; 408270096Strasz uio = ap->a_uio; 409296798Strasz initial_resid = ap->a_uio->uio_resid; 410270096Strasz 411270096Strasz KASSERT(vp->v_type == VDIR, ("!VDIR")); 412270096Strasz 413270096Strasz if (autofs_cached(anp, NULL, 0) == false && 414270096Strasz autofs_ignore_thread(curthread) == false) { 415270096Strasz error = autofs_trigger_vn(vp, "", 0, &newvp); 416270096Strasz if (error != 0) 417270096Strasz return (error); 418270096Strasz 419270096Strasz if (newvp != NULL) { 420270096Strasz error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred, 421270096Strasz ap->a_eofflag, ap->a_ncookies, ap->a_cookies); 422270096Strasz vput(newvp); 423270096Strasz return (error); 424270096Strasz } 425270096Strasz } 426270096Strasz 427296798Strasz if (uio->uio_offset < 0) 428270096Strasz return (EINVAL); 429270096Strasz 430270096Strasz if (ap->a_eofflag != NULL) 431296798Strasz *ap->a_eofflag = FALSE; 432270096Strasz 433296798Strasz /* 434296798Strasz * Write out the directory entry for ".". This is conditional 435296798Strasz * on the current offset into the directory; same applies to the 436296798Strasz * other two cases below. 437296798Strasz */ 438296798Strasz if (uio->uio_offset == 0) { 439296798Strasz error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen); 440270096Strasz if (error != 0) 441296798Strasz goto out; 442270096Strasz } 443296798Strasz reclens = autofs_dirent_reclen("."); 444270096Strasz 445296798Strasz /* 446296798Strasz * Write out the directory entry for "..". 447296798Strasz */ 448296798Strasz if (uio->uio_offset <= reclens) { 449296798Strasz if (uio->uio_offset != reclens) 450296798Strasz return (EINVAL); 451270096Strasz if (anp->an_parent == NULL) { 452296798Strasz error = autofs_readdir_one(uio, "..", 453296798Strasz anp->an_fileno, &reclen); 454270096Strasz } else { 455270096Strasz error = autofs_readdir_one(uio, "..", 456296798Strasz anp->an_parent->an_fileno, &reclen); 457270096Strasz } 458270096Strasz if (error != 0) 459296798Strasz goto out; 460270096Strasz } 461270096Strasz 462296798Strasz reclens += autofs_dirent_reclen(".."); 463296798Strasz 464296798Strasz /* 465296798Strasz * Write out the directory entries for subdirectories. 466296798Strasz */ 467272470Strasz AUTOFS_SLOCK(amp); 468297236Strasz RB_FOREACH(child, autofs_node_tree, &anp->an_children) { 469296798Strasz /* 470296798Strasz * Check the offset to skip entries returned by previous 471296798Strasz * calls to getdents(). 472296798Strasz */ 473296798Strasz if (uio->uio_offset > reclens) { 474296798Strasz reclens += autofs_dirent_reclen(child->an_name); 475296798Strasz continue; 476270096Strasz } 477270096Strasz 478270096Strasz /* 479296798Strasz * Prevent seeking into the middle of dirent. 480270096Strasz */ 481296798Strasz if (uio->uio_offset != reclens) { 482296798Strasz AUTOFS_SUNLOCK(amp); 483296798Strasz return (EINVAL); 484296798Strasz } 485270096Strasz 486270096Strasz error = autofs_readdir_one(uio, child->an_name, 487296798Strasz child->an_fileno, &reclen); 488296798Strasz reclens += reclen; 489270096Strasz if (error != 0) { 490272470Strasz AUTOFS_SUNLOCK(amp); 491296798Strasz goto out; 492270096Strasz } 493270096Strasz } 494296798Strasz AUTOFS_SUNLOCK(amp); 495270096Strasz 496296798Strasz if (ap->a_eofflag != NULL) 497296798Strasz *ap->a_eofflag = TRUE; 498296798Strasz 499270096Strasz return (0); 500296798Strasz 501296798Straszout: 502296798Strasz /* 503296798Strasz * Return error if the initial buffer was too small to do anything. 504296798Strasz */ 505296798Strasz if (uio->uio_resid == initial_resid) 506296798Strasz return (error); 507296798Strasz 508296798Strasz /* 509296798Strasz * Don't return an error if we managed to copy out some entries. 510296798Strasz */ 511296798Strasz if (uio->uio_resid < reclen) 512296798Strasz return (0); 513296798Strasz 514296798Strasz return (error); 515270096Strasz} 516270096Strasz 517270096Straszstatic int 518270096Straszautofs_reclaim(struct vop_reclaim_args *ap) 519270096Strasz{ 520272836Strasz struct vnode *vp; 521272836Strasz struct autofs_node *anp; 522270096Strasz 523270096Strasz vp = ap->a_vp; 524270096Strasz anp = vp->v_data; 525270096Strasz 526270096Strasz /* 527270096Strasz * We do not free autofs_node here; instead we are 528270096Strasz * destroying them in autofs_node_delete(). 529270096Strasz */ 530270096Strasz sx_xlock(&anp->an_vnode_lock); 531270096Strasz anp->an_vnode = NULL; 532270096Strasz vp->v_data = NULL; 533270096Strasz sx_xunlock(&anp->an_vnode_lock); 534270096Strasz 535270096Strasz return (0); 536270096Strasz} 537270096Strasz 538270096Straszstruct vop_vector autofs_vnodeops = { 539270096Strasz .vop_default = &default_vnodeops, 540270096Strasz 541270096Strasz .vop_access = autofs_access, 542270096Strasz .vop_lookup = autofs_lookup, 543270096Strasz .vop_create = VOP_EOPNOTSUPP, 544270096Strasz .vop_getattr = autofs_getattr, 545270096Strasz .vop_link = VOP_EOPNOTSUPP, 546270096Strasz .vop_mkdir = autofs_mkdir, 547270096Strasz .vop_mknod = VOP_EOPNOTSUPP, 548308253Strasz .vop_print = autofs_print, 549270096Strasz .vop_read = VOP_EOPNOTSUPP, 550270096Strasz .vop_readdir = autofs_readdir, 551270096Strasz .vop_remove = VOP_EOPNOTSUPP, 552270096Strasz .vop_rename = VOP_EOPNOTSUPP, 553270096Strasz .vop_rmdir = VOP_EOPNOTSUPP, 554270096Strasz .vop_setattr = VOP_EOPNOTSUPP, 555270096Strasz .vop_symlink = VOP_EOPNOTSUPP, 556270096Strasz .vop_write = VOP_EOPNOTSUPP, 557270096Strasz .vop_reclaim = autofs_reclaim, 558270096Strasz}; 559270096Strasz 560270096Straszint 561270096Straszautofs_node_new(struct autofs_node *parent, struct autofs_mount *amp, 562270096Strasz const char *name, int namelen, struct autofs_node **anpp) 563270096Strasz{ 564270096Strasz struct autofs_node *anp; 565270096Strasz 566272931Strasz if (parent != NULL) { 567272470Strasz AUTOFS_ASSERT_XLOCKED(parent->an_mount); 568270096Strasz 569272931Strasz KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT, 570272931Strasz ("node \"%s\" already exists", name)); 571272931Strasz } 572272931Strasz 573270096Strasz anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO); 574270096Strasz if (namelen >= 0) 575270096Strasz anp->an_name = strndup(name, namelen, M_AUTOFS); 576270096Strasz else 577270096Strasz anp->an_name = strdup(name, M_AUTOFS); 578270096Strasz anp->an_fileno = atomic_fetchadd_int(&->am_last_fileno, 1); 579270096Strasz callout_init(&anp->an_callout, 1); 580270096Strasz /* 581270096Strasz * The reason for SX_NOWITNESS here is that witness(4) 582270096Strasz * cannot tell vnodes apart, so the following perfectly 583270096Strasz * valid lock order... 584270096Strasz * 585270096Strasz * vnode lock A -> autofsvlk B -> vnode lock B 586270096Strasz * 587270096Strasz * ... gets reported as a LOR. 588270096Strasz */ 589270096Strasz sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS); 590270096Strasz getnanotime(&anp->an_ctime); 591270096Strasz anp->an_parent = parent; 592270096Strasz anp->an_mount = amp; 593270096Strasz if (parent != NULL) 594297236Strasz RB_INSERT(autofs_node_tree, &parent->an_children, anp); 595297236Strasz RB_INIT(&anp->an_children); 596270096Strasz 597270096Strasz *anpp = anp; 598270096Strasz return (0); 599270096Strasz} 600270096Strasz 601270096Straszint 602270096Straszautofs_node_find(struct autofs_node *parent, const char *name, 603270096Strasz int namelen, struct autofs_node **anpp) 604270096Strasz{ 605297236Strasz struct autofs_node *anp, find; 606297236Strasz int error; 607270096Strasz 608270096Strasz AUTOFS_ASSERT_LOCKED(parent->an_mount); 609270096Strasz 610297236Strasz if (namelen >= 0) 611297236Strasz find.an_name = strndup(name, namelen, M_AUTOFS); 612297236Strasz else 613297236Strasz find.an_name = strdup(name, M_AUTOFS); 614270096Strasz 615297236Strasz anp = RB_FIND(autofs_node_tree, &parent->an_children, &find); 616297236Strasz if (anp != NULL) { 617297236Strasz error = 0; 618270096Strasz if (anpp != NULL) 619270096Strasz *anpp = anp; 620297236Strasz } else { 621297236Strasz error = ENOENT; 622270096Strasz } 623270096Strasz 624297236Strasz free(find.an_name, M_AUTOFS); 625297236Strasz 626297236Strasz return (error); 627270096Strasz} 628270096Strasz 629270096Straszvoid 630270096Straszautofs_node_delete(struct autofs_node *anp) 631270096Strasz{ 632270096Strasz struct autofs_node *parent; 633270096Strasz 634272470Strasz AUTOFS_ASSERT_XLOCKED(anp->an_mount); 635297236Strasz KASSERT(RB_EMPTY(&anp->an_children), ("have children")); 636270096Strasz 637270096Strasz callout_drain(&anp->an_callout); 638270096Strasz 639270096Strasz parent = anp->an_parent; 640270096Strasz if (parent != NULL) 641297236Strasz RB_REMOVE(autofs_node_tree, &parent->an_children, anp); 642270096Strasz sx_destroy(&anp->an_vnode_lock); 643270096Strasz free(anp->an_name, M_AUTOFS); 644270096Strasz uma_zfree(autofs_node_zone, anp); 645270096Strasz} 646270096Strasz 647270096Straszint 648272512Straszautofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags, 649272512Strasz struct vnode **vpp) 650270096Strasz{ 651270096Strasz struct vnode *vp; 652270096Strasz int error; 653270096Strasz 654270096Strasz AUTOFS_ASSERT_UNLOCKED(anp->an_mount); 655270096Strasz 656270096Strasz sx_xlock(&anp->an_vnode_lock); 657270096Strasz 658270096Strasz vp = anp->an_vnode; 659270096Strasz if (vp != NULL) { 660272512Strasz error = vget(vp, flags | LK_RETRY, curthread); 661270096Strasz if (error != 0) { 662270096Strasz AUTOFS_WARN("vget failed with error %d", error); 663270096Strasz sx_xunlock(&anp->an_vnode_lock); 664270096Strasz return (error); 665270096Strasz } 666270096Strasz if (vp->v_iflag & VI_DOOMED) { 667270096Strasz /* 668270096Strasz * We got forcibly unmounted. 669270096Strasz */ 670270096Strasz AUTOFS_DEBUG("doomed vnode"); 671270096Strasz sx_xunlock(&anp->an_vnode_lock); 672270096Strasz vput(vp); 673270096Strasz 674270096Strasz return (ENOENT); 675270096Strasz } 676270096Strasz 677270096Strasz *vpp = vp; 678270096Strasz sx_xunlock(&anp->an_vnode_lock); 679270096Strasz return (0); 680270096Strasz } 681270096Strasz 682270096Strasz error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp); 683270096Strasz if (error != 0) { 684270096Strasz sx_xunlock(&anp->an_vnode_lock); 685270096Strasz return (error); 686270096Strasz } 687270096Strasz 688270096Strasz error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 689270096Strasz if (error != 0) { 690270096Strasz sx_xunlock(&anp->an_vnode_lock); 691270096Strasz vdrop(vp); 692270096Strasz return (error); 693270096Strasz } 694270096Strasz 695270096Strasz vp->v_type = VDIR; 696270096Strasz if (anp->an_parent == NULL) 697270096Strasz vp->v_vflag |= VV_ROOT; 698270096Strasz vp->v_data = anp; 699270096Strasz 700272512Strasz VN_LOCK_ASHARE(vp); 701272512Strasz 702270096Strasz error = insmntque(vp, mp); 703270096Strasz if (error != 0) { 704300047Strasz AUTOFS_DEBUG("insmntque() failed with error %d", error); 705270096Strasz sx_xunlock(&anp->an_vnode_lock); 706270096Strasz return (error); 707270096Strasz } 708270096Strasz 709270096Strasz KASSERT(anp->an_vnode == NULL, ("lost race")); 710270096Strasz anp->an_vnode = vp; 711270096Strasz 712270096Strasz sx_xunlock(&anp->an_vnode_lock); 713270096Strasz 714270096Strasz *vpp = vp; 715270096Strasz return (0); 716270096Strasz} 717