autofs_vnops.c revision 272403
1270096Strasz/*- 2270096Strasz * Copyright (c) 2014 The FreeBSD Foundation 3270096Strasz * All rights reserved. 4270096Strasz * 5270096Strasz * This software was developed by Edward Tomasz Napierala under sponsorship 6270096Strasz * from the FreeBSD Foundation. 7270096Strasz * 8270096Strasz * Redistribution and use in source and binary forms, with or without 9270096Strasz * modification, are permitted provided that the following conditions 10270096Strasz * are met: 11270096Strasz * 1. Redistributions of source code must retain the above copyright 12270096Strasz * notice, this list of conditions and the following disclaimer. 13270096Strasz * 2. Redistributions in binary form must reproduce the above copyright 14270096Strasz * notice, this list of conditions and the following disclaimer in the 15270096Strasz * documentation and/or other materials provided with the distribution. 16270096Strasz * 17270096Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20270096Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27270096Strasz * SUCH DAMAGE. 28270096Strasz * 29270096Strasz */ 30270096Strasz 31270096Strasz#include <sys/cdefs.h> 32270096Strasz__FBSDID("$FreeBSD: head/sys/fs/autofs/autofs_vnops.c 272403 2014-10-02 10:31:32Z trasz $"); 33270096Strasz 34270096Strasz#include <sys/param.h> 35270096Strasz#include <sys/kernel.h> 36270096Strasz#include <sys/condvar.h> 37270096Strasz#include <sys/dirent.h> 38270096Strasz#include <sys/fcntl.h> 39270096Strasz#include <sys/lock.h> 40270096Strasz#include <sys/mount.h> 41270096Strasz#include <sys/mutex.h> 42270096Strasz#include <sys/namei.h> 43270096Strasz#include <sys/signalvar.h> 44270096Strasz#include <sys/systm.h> 45272403Strasz#include <sys/taskqueue.h> 46270096Strasz#include <sys/vnode.h> 47270096Strasz#include <machine/atomic.h> 48270096Strasz#include <vm/uma.h> 49270096Strasz 50270281Strasz#include <fs/autofs/autofs.h> 51270096Strasz 52270096Straszstatic int autofs_trigger_vn(struct vnode *vp, const char *path, 53270096Strasz int pathlen, struct vnode **newvp); 54270096Strasz 55270402Straszextern struct autofs_softc *autofs_softc; 56270402Strasz 57270096Straszstatic int 58270096Straszautofs_access(struct vop_access_args *ap) 59270096Strasz{ 60270096Strasz 61270096Strasz /* 62270096Strasz * Nothing to do here; the only kind of access control 63270096Strasz * needed is in autofs_mkdir(). 64270096Strasz */ 65270096Strasz 66270096Strasz return (0); 67270096Strasz} 68270096Strasz 69270096Straszstatic int 70270096Straszautofs_getattr(struct vop_getattr_args *ap) 71270096Strasz{ 72270096Strasz struct vnode *vp, *newvp; 73270096Strasz struct autofs_node *anp; 74270096Strasz struct mount *mp; 75270096Strasz struct vattr *vap; 76270096Strasz int error; 77270096Strasz 78270096Strasz vp = ap->a_vp; 79270096Strasz anp = vp->v_data; 80270096Strasz mp = vp->v_mount; 81270096Strasz vap = ap->a_vap; 82270096Strasz 83270096Strasz KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR")); 84270096Strasz 85270096Strasz /* 86270096Strasz * The reason we must do this is that some tree-walking software, 87270096Strasz * namely fts(3), assumes that stat(".") results will not change 88270096Strasz * between chdir("subdir") and chdir(".."), and fails with ENOENT 89270096Strasz * otherwise. 90270096Strasz */ 91270096Strasz if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false && 92270096Strasz autofs_ignore_thread(curthread) == false) { 93270096Strasz error = autofs_trigger_vn(vp, "", 0, &newvp); 94270096Strasz if (error != 0) 95270096Strasz return (error); 96270096Strasz 97270096Strasz if (newvp != NULL) { 98270096Strasz error = VOP_GETATTR(newvp, ap->a_vap, 99270096Strasz ap->a_cred); 100270096Strasz vput(newvp); 101270096Strasz return (error); 102270096Strasz } 103270096Strasz } 104270096Strasz 105270096Strasz vap->va_type = VDIR; 106270096Strasz vap->va_mode = 0755; 107270096Strasz vap->va_nlink = 3; /* XXX */ 108270096Strasz vap->va_uid = 0; 109270096Strasz vap->va_gid = 0; 110270096Strasz vap->va_rdev = NODEV; 111270096Strasz vap->va_fsid = mp->mnt_stat.f_fsid.val[0]; 112270096Strasz vap->va_fileid = anp->an_fileno; 113270096Strasz vap->va_size = 512; /* XXX */ 114270096Strasz vap->va_blocksize = 512; 115270096Strasz vap->va_mtime = anp->an_ctime; 116270096Strasz vap->va_atime = anp->an_ctime; 117270096Strasz vap->va_ctime = anp->an_ctime; 118270096Strasz vap->va_birthtime = anp->an_ctime; 119270096Strasz vap->va_gen = 0; 120270096Strasz vap->va_flags = 0; 121270096Strasz vap->va_rdev = 0; 122270096Strasz vap->va_bytes = 512; /* XXX */ 123270096Strasz vap->va_filerev = 0; 124270096Strasz vap->va_spare = 0; 125270096Strasz 126270096Strasz return (0); 127270096Strasz} 128270096Strasz 129270096Strasz/* 130270096Strasz * Unlock the vnode, request automountd(8) action, and then lock it back. 131270096Strasz * If anything got mounted on top of the vnode, return the new filesystem's 132270096Strasz * root vnode in 'newvp', locked. 133270096Strasz */ 134270096Straszstatic int 135270096Straszautofs_trigger_vn(struct vnode *vp, const char *path, int pathlen, 136270096Strasz struct vnode **newvp) 137270096Strasz{ 138270096Strasz struct autofs_node *anp; 139270096Strasz struct autofs_mount *amp; 140270096Strasz int error, lock_flags; 141270096Strasz 142270096Strasz anp = vp->v_data; 143270096Strasz amp = VFSTOAUTOFS(vp->v_mount); 144270096Strasz 145270096Strasz /* 146270096Strasz * Release the vnode lock, so that other operations, in partcular 147270096Strasz * mounting a filesystem on top of it, can proceed. Increase use 148270096Strasz * count, to prevent the vnode from being deallocated and to prevent 149270096Strasz * filesystem from being unmounted. 150270096Strasz */ 151270096Strasz lock_flags = VOP_ISLOCKED(vp); 152270096Strasz vref(vp); 153270096Strasz VOP_UNLOCK(vp, 0); 154270096Strasz 155270402Strasz sx_xlock(&autofs_softc->sc_lock); 156270096Strasz 157270096Strasz /* 158270096Strasz * XXX: Workaround for mounting the same thing multiple times; revisit. 159270096Strasz */ 160270096Strasz if (vp->v_mountedhere != NULL) { 161270096Strasz error = 0; 162270096Strasz goto mounted; 163270096Strasz } 164270096Strasz 165270096Strasz error = autofs_trigger(anp, path, pathlen); 166270096Straszmounted: 167270402Strasz sx_xunlock(&autofs_softc->sc_lock); 168270096Strasz vn_lock(vp, lock_flags | LK_RETRY); 169270096Strasz vunref(vp); 170270096Strasz if ((vp->v_iflag & VI_DOOMED) != 0) { 171270096Strasz AUTOFS_DEBUG("VI_DOOMED"); 172270096Strasz return (ENOENT); 173270096Strasz } 174270096Strasz 175270096Strasz if (error != 0) 176270096Strasz return (error); 177270096Strasz 178270096Strasz if (vp->v_mountedhere == NULL) { 179270096Strasz *newvp = NULL; 180270096Strasz return (0); 181270096Strasz } else { 182270096Strasz /* 183270096Strasz * If the operation that succeeded was mount, then mark 184270096Strasz * the node as non-cached. Otherwise, if someone unmounts 185270096Strasz * the filesystem before the cache times out, we will fail 186270096Strasz * to trigger. 187270096Strasz */ 188270096Strasz anp->an_cached = false; 189270096Strasz } 190270096Strasz 191270096Strasz error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp); 192270096Strasz if (error != 0) { 193270096Strasz AUTOFS_WARN("VFS_ROOT() failed with error %d", error); 194270096Strasz return (error); 195270096Strasz } 196270096Strasz 197270096Strasz return (0); 198270096Strasz} 199270096Strasz 200270096Straszstatic int 201270207Straszautofs_vget_callback(struct mount *mp, void *arg, int lkflags __unused, 202270207Strasz struct vnode **vpp) 203270207Strasz{ 204270207Strasz 205270207Strasz 206270207Strasz return (autofs_node_vn(arg, mp, vpp)); 207270207Strasz} 208270207Strasz 209270207Straszstatic int 210270096Straszautofs_lookup(struct vop_lookup_args *ap) 211270096Strasz{ 212270096Strasz struct vnode *dvp, *newvp, **vpp; 213270096Strasz struct mount *mp; 214270096Strasz struct autofs_mount *amp; 215270096Strasz struct autofs_node *anp, *child; 216270096Strasz struct componentname *cnp; 217270096Strasz int error, lock_flags; 218270096Strasz 219270096Strasz dvp = ap->a_dvp; 220270096Strasz vpp = ap->a_vpp; 221270096Strasz mp = dvp->v_mount; 222270096Strasz amp = VFSTOAUTOFS(mp); 223270096Strasz anp = dvp->v_data; 224270096Strasz cnp = ap->a_cnp; 225270096Strasz 226270096Strasz if (cnp->cn_flags & ISDOTDOT) { 227270096Strasz KASSERT(anp->an_parent != NULL, ("NULL parent")); 228270096Strasz /* 229270207Strasz * Note that in this case, dvp is the child vnode, and we 230270207Strasz * are looking up the parent vnode - exactly reverse from 231270207Strasz * normal operation. Unlocking dvp requires some rather 232270207Strasz * tricky unlock/relock dance to prevent mp from being freed; 233270207Strasz * use vn_vget_ino_gen() which takes care of all that. 234270096Strasz */ 235270207Strasz error = vn_vget_ino_gen(dvp, autofs_vget_callback, 236270207Strasz anp->an_parent, 0, vpp); 237270096Strasz if (error != 0) { 238270207Strasz AUTOFS_WARN("vn_vget_ino_gen() failed with error %d", 239270096Strasz error); 240270207Strasz return (error); 241270096Strasz } 242270096Strasz return (error); 243270096Strasz } 244270096Strasz 245270096Strasz if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 246270096Strasz vref(dvp); 247270096Strasz *vpp = dvp; 248270096Strasz 249270096Strasz return (0); 250270096Strasz } 251270096Strasz 252270096Strasz if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false && 253270096Strasz autofs_ignore_thread(cnp->cn_thread) == false) { 254270096Strasz error = autofs_trigger_vn(dvp, 255270096Strasz cnp->cn_nameptr, cnp->cn_namelen, &newvp); 256270096Strasz if (error != 0) 257270096Strasz return (error); 258270096Strasz 259270096Strasz if (newvp != NULL) { 260270096Strasz error = VOP_LOOKUP(newvp, ap->a_vpp, ap->a_cnp); 261270096Strasz 262270096Strasz /* 263270096Strasz * Instead of figuring out whether our vnode should 264270096Strasz * be locked or not given the error and cnp flags, 265270096Strasz * just "copy" the lock status from vnode returned 266270096Strasz * by mounted filesystem's VOP_LOOKUP(). Get rid 267270096Strasz * of that new vnode afterwards. 268270096Strasz */ 269270096Strasz lock_flags = VOP_ISLOCKED(newvp); 270270096Strasz if (lock_flags == 0) { 271270096Strasz VOP_UNLOCK(dvp, 0); 272270096Strasz vrele(newvp); 273270096Strasz } else { 274270096Strasz vput(newvp); 275270096Strasz } 276270096Strasz return (error); 277270096Strasz } 278270096Strasz } 279270096Strasz 280270096Strasz AUTOFS_LOCK(amp); 281270096Strasz error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child); 282270096Strasz if (error != 0) { 283270096Strasz if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) { 284270096Strasz AUTOFS_UNLOCK(amp); 285270096Strasz return (EJUSTRETURN); 286270096Strasz } 287270096Strasz 288270096Strasz AUTOFS_UNLOCK(amp); 289270096Strasz return (ENOENT); 290270096Strasz } 291270096Strasz 292270096Strasz /* 293270096Strasz * XXX: Dropping the node here is ok, because we never remove nodes. 294270096Strasz */ 295270096Strasz AUTOFS_UNLOCK(amp); 296270096Strasz 297270096Strasz error = autofs_node_vn(child, mp, vpp); 298270096Strasz if (error != 0) { 299270096Strasz if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) 300270096Strasz return (EJUSTRETURN); 301270096Strasz 302270096Strasz return (error); 303270096Strasz } 304270096Strasz 305270096Strasz return (0); 306270096Strasz} 307270096Strasz 308270096Straszstatic int 309270096Straszautofs_mkdir(struct vop_mkdir_args *ap) 310270096Strasz{ 311270096Strasz struct vnode *vp; 312270096Strasz struct autofs_node *anp; 313270096Strasz struct autofs_mount *amp; 314270096Strasz struct autofs_node *child; 315270096Strasz int error; 316270096Strasz 317270096Strasz vp = ap->a_dvp; 318270096Strasz anp = vp->v_data; 319270096Strasz amp = VFSTOAUTOFS(vp->v_mount); 320270096Strasz 321270096Strasz /* 322270096Strasz * Do not allow mkdir() if the calling thread is not 323270096Strasz * automountd(8) descendant. 324270096Strasz */ 325270096Strasz if (autofs_ignore_thread(curthread) == false) 326270096Strasz return (EPERM); 327270096Strasz 328270096Strasz AUTOFS_LOCK(amp); 329270096Strasz error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr, 330270096Strasz ap->a_cnp->cn_namelen, &child); 331270096Strasz if (error != 0) { 332270096Strasz AUTOFS_UNLOCK(amp); 333270096Strasz return (error); 334270096Strasz } 335270096Strasz AUTOFS_UNLOCK(amp); 336270096Strasz 337270096Strasz error = autofs_node_vn(child, vp->v_mount, ap->a_vpp); 338270096Strasz 339270096Strasz return (error); 340270096Strasz} 341270096Strasz 342270096Straszstatic int 343270096Straszautofs_readdir_one(struct uio *uio, const char *name, int fileno) 344270096Strasz{ 345270096Strasz struct dirent dirent; 346270096Strasz int error, i; 347270096Strasz 348270096Strasz memset(&dirent, 0, sizeof(dirent)); 349270096Strasz dirent.d_type = DT_DIR; 350270096Strasz dirent.d_reclen = AUTOFS_DELEN; 351270096Strasz dirent.d_fileno = fileno; 352270096Strasz /* PFS_DELEN was picked to fit PFS_NAMLEN */ 353270096Strasz for (i = 0; i < AUTOFS_NAMELEN - 1 && name[i] != '\0'; ++i) 354270096Strasz dirent.d_name[i] = name[i]; 355270096Strasz dirent.d_name[i] = 0; 356270096Strasz dirent.d_namlen = i; 357270096Strasz 358270096Strasz error = uiomove(&dirent, AUTOFS_DELEN, uio); 359270096Strasz return (error); 360270096Strasz} 361270096Strasz 362270096Straszstatic int 363270096Straszautofs_readdir(struct vop_readdir_args *ap) 364270096Strasz{ 365270096Strasz struct vnode *vp, *newvp; 366270096Strasz struct autofs_mount *amp; 367270096Strasz struct autofs_node *anp, *child; 368270096Strasz struct uio *uio; 369270096Strasz off_t offset; 370270096Strasz int error, i, resid; 371270096Strasz 372270096Strasz vp = ap->a_vp; 373270096Strasz amp = VFSTOAUTOFS(vp->v_mount); 374270096Strasz anp = vp->v_data; 375270096Strasz uio = ap->a_uio; 376270096Strasz 377270096Strasz KASSERT(vp->v_type == VDIR, ("!VDIR")); 378270096Strasz 379270096Strasz if (autofs_cached(anp, NULL, 0) == false && 380270096Strasz autofs_ignore_thread(curthread) == false) { 381270096Strasz error = autofs_trigger_vn(vp, "", 0, &newvp); 382270096Strasz if (error != 0) 383270096Strasz return (error); 384270096Strasz 385270096Strasz if (newvp != NULL) { 386270096Strasz error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred, 387270096Strasz ap->a_eofflag, ap->a_ncookies, ap->a_cookies); 388270096Strasz vput(newvp); 389270096Strasz return (error); 390270096Strasz } 391270096Strasz } 392270096Strasz 393270096Strasz /* only allow reading entire entries */ 394270096Strasz offset = uio->uio_offset; 395270096Strasz resid = uio->uio_resid; 396270096Strasz if (offset < 0 || offset % AUTOFS_DELEN != 0 || 397270096Strasz (resid && resid < AUTOFS_DELEN)) 398270096Strasz return (EINVAL); 399270096Strasz if (resid == 0) 400270096Strasz return (0); 401270096Strasz 402270096Strasz if (ap->a_eofflag != NULL) 403270096Strasz *ap->a_eofflag = TRUE; 404270096Strasz 405270096Strasz if (offset == 0 && resid >= AUTOFS_DELEN) { 406270096Strasz error = autofs_readdir_one(uio, ".", anp->an_fileno); 407270096Strasz if (error != 0) 408270096Strasz return (error); 409270096Strasz offset += AUTOFS_DELEN; 410270096Strasz resid -= AUTOFS_DELEN; 411270096Strasz } 412270096Strasz 413270096Strasz if (offset == AUTOFS_DELEN && resid >= AUTOFS_DELEN) { 414270096Strasz if (anp->an_parent == NULL) { 415270096Strasz /* 416270096Strasz * XXX: Right? 417270096Strasz */ 418270096Strasz error = autofs_readdir_one(uio, "..", anp->an_fileno); 419270096Strasz } else { 420270096Strasz error = autofs_readdir_one(uio, "..", 421270096Strasz anp->an_parent->an_fileno); 422270096Strasz } 423270096Strasz if (error != 0) 424270096Strasz return (error); 425270096Strasz offset += AUTOFS_DELEN; 426270096Strasz resid -= AUTOFS_DELEN; 427270096Strasz } 428270096Strasz 429270096Strasz i = 2; /* Account for "." and "..". */ 430270096Strasz AUTOFS_LOCK(amp); 431270096Strasz TAILQ_FOREACH(child, &anp->an_children, an_next) { 432270096Strasz if (resid < AUTOFS_DELEN) { 433270096Strasz if (ap->a_eofflag != NULL) 434270096Strasz *ap->a_eofflag = 0; 435270096Strasz break; 436270096Strasz } 437270096Strasz 438270096Strasz /* 439270096Strasz * Skip entries returned by previous call to getdents(). 440270096Strasz */ 441270096Strasz i++; 442270096Strasz if (i * AUTOFS_DELEN <= offset) 443270096Strasz continue; 444270096Strasz 445270096Strasz error = autofs_readdir_one(uio, child->an_name, 446270096Strasz child->an_fileno); 447270096Strasz if (error != 0) { 448270096Strasz AUTOFS_UNLOCK(amp); 449270096Strasz return (error); 450270096Strasz } 451270096Strasz offset += AUTOFS_DELEN; 452270096Strasz resid -= AUTOFS_DELEN; 453270096Strasz } 454270096Strasz 455270096Strasz AUTOFS_UNLOCK(amp); 456270096Strasz return (0); 457270096Strasz} 458270096Strasz 459270096Straszstatic int 460270096Straszautofs_reclaim(struct vop_reclaim_args *ap) 461270096Strasz{ 462270096Strasz struct vnode *vp = ap->a_vp; 463270096Strasz struct autofs_node *anp = vp->v_data; 464270096Strasz 465270096Strasz vp = ap->a_vp; 466270096Strasz anp = vp->v_data; 467270096Strasz 468270096Strasz /* 469270096Strasz * We do not free autofs_node here; instead we are 470270096Strasz * destroying them in autofs_node_delete(). 471270096Strasz */ 472270096Strasz sx_xlock(&anp->an_vnode_lock); 473270096Strasz anp->an_vnode = NULL; 474270096Strasz vp->v_data = NULL; 475270096Strasz sx_xunlock(&anp->an_vnode_lock); 476270096Strasz 477270096Strasz return (0); 478270096Strasz} 479270096Strasz 480270096Straszstruct vop_vector autofs_vnodeops = { 481270096Strasz .vop_default = &default_vnodeops, 482270096Strasz 483270096Strasz .vop_access = autofs_access, 484270096Strasz .vop_lookup = autofs_lookup, 485270096Strasz .vop_create = VOP_EOPNOTSUPP, 486270096Strasz .vop_getattr = autofs_getattr, 487270096Strasz .vop_link = VOP_EOPNOTSUPP, 488270096Strasz .vop_mkdir = autofs_mkdir, 489270096Strasz .vop_mknod = VOP_EOPNOTSUPP, 490270096Strasz .vop_read = VOP_EOPNOTSUPP, 491270096Strasz .vop_readdir = autofs_readdir, 492270096Strasz .vop_remove = VOP_EOPNOTSUPP, 493270096Strasz .vop_rename = VOP_EOPNOTSUPP, 494270096Strasz .vop_rmdir = VOP_EOPNOTSUPP, 495270096Strasz .vop_setattr = VOP_EOPNOTSUPP, 496270096Strasz .vop_symlink = VOP_EOPNOTSUPP, 497270096Strasz .vop_write = VOP_EOPNOTSUPP, 498270096Strasz .vop_reclaim = autofs_reclaim, 499270096Strasz}; 500270096Strasz 501270096Straszint 502270096Straszautofs_node_new(struct autofs_node *parent, struct autofs_mount *amp, 503270096Strasz const char *name, int namelen, struct autofs_node **anpp) 504270096Strasz{ 505270096Strasz struct autofs_node *anp; 506270096Strasz 507270096Strasz if (parent != NULL) 508270096Strasz AUTOFS_ASSERT_LOCKED(parent->an_mount); 509270096Strasz 510270096Strasz anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO); 511270096Strasz if (namelen >= 0) 512270096Strasz anp->an_name = strndup(name, namelen, M_AUTOFS); 513270096Strasz else 514270096Strasz anp->an_name = strdup(name, M_AUTOFS); 515270096Strasz anp->an_fileno = atomic_fetchadd_int(&->am_last_fileno, 1); 516270096Strasz callout_init(&anp->an_callout, 1); 517270096Strasz /* 518270096Strasz * The reason for SX_NOWITNESS here is that witness(4) 519270096Strasz * cannot tell vnodes apart, so the following perfectly 520270096Strasz * valid lock order... 521270096Strasz * 522270096Strasz * vnode lock A -> autofsvlk B -> vnode lock B 523270096Strasz * 524270096Strasz * ... gets reported as a LOR. 525270096Strasz */ 526270096Strasz sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS); 527270096Strasz getnanotime(&anp->an_ctime); 528270096Strasz anp->an_parent = parent; 529270096Strasz anp->an_mount = amp; 530270096Strasz if (parent != NULL) 531270096Strasz TAILQ_INSERT_TAIL(&parent->an_children, anp, an_next); 532270096Strasz TAILQ_INIT(&anp->an_children); 533270096Strasz 534270096Strasz *anpp = anp; 535270096Strasz return (0); 536270096Strasz} 537270096Strasz 538270096Straszint 539270096Straszautofs_node_find(struct autofs_node *parent, const char *name, 540270096Strasz int namelen, struct autofs_node **anpp) 541270096Strasz{ 542270096Strasz struct autofs_node *anp; 543270096Strasz 544270096Strasz AUTOFS_ASSERT_LOCKED(parent->an_mount); 545270096Strasz 546270096Strasz TAILQ_FOREACH(anp, &parent->an_children, an_next) { 547270096Strasz if (namelen >= 0) { 548272025Strasz if (strlen(anp->an_name) != namelen) 549272025Strasz continue; 550270096Strasz if (strncmp(anp->an_name, name, namelen) != 0) 551270096Strasz continue; 552270096Strasz } else { 553270096Strasz if (strcmp(anp->an_name, name) != 0) 554270096Strasz continue; 555270096Strasz } 556270096Strasz 557270096Strasz if (anpp != NULL) 558270096Strasz *anpp = anp; 559270096Strasz return (0); 560270096Strasz } 561270096Strasz 562270096Strasz return (ENOENT); 563270096Strasz} 564270096Strasz 565270096Straszvoid 566270096Straszautofs_node_delete(struct autofs_node *anp) 567270096Strasz{ 568270096Strasz struct autofs_node *parent; 569270096Strasz 570270096Strasz AUTOFS_ASSERT_LOCKED(anp->an_mount); 571270096Strasz KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children")); 572270096Strasz 573270096Strasz callout_drain(&anp->an_callout); 574270096Strasz 575270096Strasz parent = anp->an_parent; 576270096Strasz if (parent != NULL) 577270096Strasz TAILQ_REMOVE(&parent->an_children, anp, an_next); 578270096Strasz sx_destroy(&anp->an_vnode_lock); 579270096Strasz free(anp->an_name, M_AUTOFS); 580270096Strasz uma_zfree(autofs_node_zone, anp); 581270096Strasz} 582270096Strasz 583270096Straszint 584270096Straszautofs_node_vn(struct autofs_node *anp, struct mount *mp, struct vnode **vpp) 585270096Strasz{ 586270096Strasz struct vnode *vp; 587270096Strasz int error; 588270096Strasz 589270096Strasz AUTOFS_ASSERT_UNLOCKED(anp->an_mount); 590270096Strasz 591270096Strasz sx_xlock(&anp->an_vnode_lock); 592270096Strasz 593270096Strasz vp = anp->an_vnode; 594270096Strasz if (vp != NULL) { 595270096Strasz error = vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread); 596270096Strasz if (error != 0) { 597270096Strasz AUTOFS_WARN("vget failed with error %d", error); 598270096Strasz sx_xunlock(&anp->an_vnode_lock); 599270096Strasz return (error); 600270096Strasz } 601270096Strasz if (vp->v_iflag & VI_DOOMED) { 602270096Strasz /* 603270096Strasz * We got forcibly unmounted. 604270096Strasz */ 605270096Strasz AUTOFS_DEBUG("doomed vnode"); 606270096Strasz sx_xunlock(&anp->an_vnode_lock); 607270096Strasz vput(vp); 608270096Strasz 609270096Strasz return (ENOENT); 610270096Strasz } 611270096Strasz 612270096Strasz *vpp = vp; 613270096Strasz sx_xunlock(&anp->an_vnode_lock); 614270096Strasz return (0); 615270096Strasz } 616270096Strasz 617270096Strasz error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp); 618270096Strasz if (error != 0) { 619270096Strasz sx_xunlock(&anp->an_vnode_lock); 620270096Strasz return (error); 621270096Strasz } 622270096Strasz 623270096Strasz error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 624270096Strasz if (error != 0) { 625270096Strasz sx_xunlock(&anp->an_vnode_lock); 626270096Strasz vdrop(vp); 627270096Strasz return (error); 628270096Strasz } 629270096Strasz 630270096Strasz vp->v_type = VDIR; 631270096Strasz if (anp->an_parent == NULL) 632270096Strasz vp->v_vflag |= VV_ROOT; 633270096Strasz vp->v_data = anp; 634270096Strasz 635270096Strasz error = insmntque(vp, mp); 636270096Strasz if (error != 0) { 637270096Strasz AUTOFS_WARN("insmntque() failed with error %d", error); 638270096Strasz sx_xunlock(&anp->an_vnode_lock); 639270096Strasz return (error); 640270096Strasz } 641270096Strasz 642270096Strasz KASSERT(anp->an_vnode == NULL, ("lost race")); 643270096Strasz anp->an_vnode = vp; 644270096Strasz 645270096Strasz sx_xunlock(&anp->an_vnode_lock); 646270096Strasz 647270096Strasz *vpp = vp; 648270096Strasz return (0); 649270096Strasz} 650