2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/condvar.h>
39 #include <sys/dirent.h>
40 #include <sys/fcntl.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.h>
44 #include <sys/namei.h>
45 #include <sys/signalvar.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
50 #include <sys/vnode.h>
51 #include <machine/atomic.h>
54 #include <fs/autofs/autofs.h>
56 static int autofs_trigger_vn(struct vnode *vp, const char *path,
57 int pathlen, struct vnode **newvp);
59 extern struct autofs_softc *autofs_softc;
62 autofs_access(struct vop_access_args *ap)
66 * Nothing to do here; the only kind of access control
67 * needed is in autofs_mkdir().
74 autofs_getattr(struct vop_getattr_args *ap)
76 struct vnode *vp, *newvp;
77 struct autofs_node *anp;
87 KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
90 * The reason we must do this is that some tree-walking software,
91 * namely fts(3), assumes that stat(".") results will not change
92 * between chdir("subdir") and chdir(".."), and fails with ENOENT
95 if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
96 autofs_ignore_thread(curthread) == false) {
97 error = autofs_trigger_vn(vp, "", 0, &newvp);
102 error = VOP_GETATTR(newvp, ap->a_vap,
111 vap->va_nlink = 3; /* XXX */
114 vap->va_rdev = NODEV;
115 vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
116 vap->va_fileid = anp->an_fileno;
117 vap->va_size = S_BLKSIZE;
118 vap->va_blocksize = S_BLKSIZE;
119 vap->va_mtime = anp->an_ctime;
120 vap->va_atime = anp->an_ctime;
121 vap->va_ctime = anp->an_ctime;
122 vap->va_birthtime = anp->an_ctime;
126 vap->va_bytes = S_BLKSIZE;
134 * Unlock the vnode, request automountd(8) action, and then lock it back.
135 * If anything got mounted on top of the vnode, return the new filesystem's
136 * root vnode in 'newvp', locked.
139 autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
140 struct vnode **newvp)
142 struct autofs_node *anp;
143 int error, lock_flags;
148 * Release the vnode lock, so that other operations, in partcular
149 * mounting a filesystem on top of it, can proceed. Increase use
150 * count, to prevent the vnode from being deallocated and to prevent
151 * filesystem from being unmounted.
153 lock_flags = VOP_ISLOCKED(vp);
157 sx_xlock(&autofs_softc->sc_lock);
160 * XXX: Workaround for mounting the same thing multiple times; revisit.
162 if (vp->v_mountedhere != NULL) {
167 error = autofs_trigger(anp, path, pathlen);
169 sx_xunlock(&autofs_softc->sc_lock);
170 vn_lock(vp, lock_flags | LK_RETRY);
172 if ((vp->v_iflag & VI_DOOMED) != 0) {
173 AUTOFS_DEBUG("VI_DOOMED");
180 if (vp->v_mountedhere == NULL) {
185 * If the operation that succeeded was mount, then mark
186 * the node as non-cached. Otherwise, if someone unmounts
187 * the filesystem before the cache times out, we will fail
190 anp->an_cached = false;
193 error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
195 AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
203 autofs_vget_callback(struct mount *mp, void *arg, int flags,
208 return (autofs_node_vn(arg, mp, flags, vpp));
212 autofs_lookup(struct vop_lookup_args *ap)
214 struct vnode *dvp, *newvp, **vpp;
216 struct autofs_mount *amp;
217 struct autofs_node *anp, *child;
218 struct componentname *cnp;
224 amp = VFSTOAUTOFS(mp);
228 if (cnp->cn_flags & ISDOTDOT) {
229 KASSERT(anp->an_parent != NULL, ("NULL parent"));
231 * Note that in this case, dvp is the child vnode, and we
232 * are looking up the parent vnode - exactly reverse from
233 * normal operation. Unlocking dvp requires some rather
234 * tricky unlock/relock dance to prevent mp from being freed;
235 * use vn_vget_ino_gen() which takes care of all that.
237 error = vn_vget_ino_gen(dvp, autofs_vget_callback,
238 anp->an_parent, cnp->cn_lkflags, vpp);
240 AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
247 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
254 if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
255 autofs_ignore_thread(cnp->cn_thread) == false) {
256 error = autofs_trigger_vn(dvp,
257 cnp->cn_nameptr, cnp->cn_namelen, &newvp);
263 * The target filesystem got automounted.
264 * Let the lookup(9) go around with the same
273 error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
275 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
277 return (EJUSTRETURN);
285 * XXX: Dropping the node here is ok, because we never remove nodes.
289 error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
291 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
292 return (EJUSTRETURN);
301 autofs_mkdir(struct vop_mkdir_args *ap)
304 struct autofs_node *anp;
305 struct autofs_mount *amp;
306 struct autofs_node *child;
311 amp = VFSTOAUTOFS(vp->v_mount);
314 * Do not allow mkdir() if the calling thread is not
315 * automountd(8) descendant.
317 if (autofs_ignore_thread(curthread) == false)
321 error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
322 ap->a_cnp->cn_namelen, &child);
329 error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
335 autofs_print(struct vop_print_args *ap)
338 struct autofs_node *anp;
343 printf(" name \"%s\", fileno %d, cached %d, wildcards %d\n",
344 anp->an_name, anp->an_fileno, anp->an_cached, anp->an_wildcards);
350 * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments.
353 autofs_readdir_one(struct uio *uio, const char *name, int fileno,
356 struct dirent dirent;
357 size_t namlen, padded_namlen, reclen;
360 namlen = strlen(name);
361 padded_namlen = roundup2(namlen + 1, __alignof(struct dirent));
362 KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen));
363 reclen = offsetof(struct dirent, d_name) + padded_namlen;
371 if (uio->uio_resid < reclen)
374 dirent.d_fileno = fileno;
375 dirent.d_reclen = reclen;
376 dirent.d_type = DT_DIR;
377 dirent.d_namlen = namlen;
378 memcpy(dirent.d_name, name, namlen);
379 memset(dirent.d_name + namlen, 0, padded_namlen - namlen);
380 error = uiomove(&dirent, reclen, uio);
386 autofs_dirent_reclen(const char *name)
390 (void)autofs_readdir_one(NULL, name, -1, &reclen);
396 autofs_readdir(struct vop_readdir_args *ap)
398 struct vnode *vp, *newvp;
399 struct autofs_mount *amp;
400 struct autofs_node *anp, *child;
402 size_t reclen, reclens;
403 ssize_t initial_resid;
407 amp = VFSTOAUTOFS(vp->v_mount);
410 initial_resid = ap->a_uio->uio_resid;
412 KASSERT(vp->v_type == VDIR, ("!VDIR"));
414 if (autofs_cached(anp, NULL, 0) == false &&
415 autofs_ignore_thread(curthread) == false) {
416 error = autofs_trigger_vn(vp, "", 0, &newvp);
421 error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
422 ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
428 if (uio->uio_offset < 0)
431 if (ap->a_eofflag != NULL)
432 *ap->a_eofflag = FALSE;
435 * Write out the directory entry for ".". This is conditional
436 * on the current offset into the directory; same applies to the
437 * other two cases below.
439 if (uio->uio_offset == 0) {
440 error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen);
444 reclens = autofs_dirent_reclen(".");
447 * Write out the directory entry for "..".
449 if (uio->uio_offset <= reclens) {
450 if (uio->uio_offset != reclens)
452 if (anp->an_parent == NULL) {
453 error = autofs_readdir_one(uio, "..",
454 anp->an_fileno, &reclen);
456 error = autofs_readdir_one(uio, "..",
457 anp->an_parent->an_fileno, &reclen);
463 reclens += autofs_dirent_reclen("..");
466 * Write out the directory entries for subdirectories.
469 RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
471 * Check the offset to skip entries returned by previous
472 * calls to getdents().
474 if (uio->uio_offset > reclens) {
475 reclens += autofs_dirent_reclen(child->an_name);
480 * Prevent seeking into the middle of dirent.
482 if (uio->uio_offset != reclens) {
487 error = autofs_readdir_one(uio, child->an_name,
488 child->an_fileno, &reclen);
497 if (ap->a_eofflag != NULL)
498 *ap->a_eofflag = TRUE;
504 * Return error if the initial buffer was too small to do anything.
506 if (uio->uio_resid == initial_resid)
510 * Don't return an error if we managed to copy out some entries.
512 if (uio->uio_resid < reclen)
519 autofs_reclaim(struct vop_reclaim_args *ap)
522 struct autofs_node *anp;
528 * We do not free autofs_node here; instead we are
529 * destroying them in autofs_node_delete().
531 sx_xlock(&anp->an_vnode_lock);
532 anp->an_vnode = NULL;
534 sx_xunlock(&anp->an_vnode_lock);
539 struct vop_vector autofs_vnodeops = {
540 .vop_default = &default_vnodeops,
542 .vop_access = autofs_access,
543 .vop_lookup = autofs_lookup,
544 .vop_create = VOP_EOPNOTSUPP,
545 .vop_getattr = autofs_getattr,
546 .vop_link = VOP_EOPNOTSUPP,
547 .vop_mkdir = autofs_mkdir,
548 .vop_mknod = VOP_EOPNOTSUPP,
549 .vop_print = autofs_print,
550 .vop_read = VOP_EOPNOTSUPP,
551 .vop_readdir = autofs_readdir,
552 .vop_remove = VOP_EOPNOTSUPP,
553 .vop_rename = VOP_EOPNOTSUPP,
554 .vop_rmdir = VOP_EOPNOTSUPP,
555 .vop_setattr = VOP_EOPNOTSUPP,
556 .vop_symlink = VOP_EOPNOTSUPP,
557 .vop_write = VOP_EOPNOTSUPP,
558 .vop_reclaim = autofs_reclaim,
562 autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
563 const char *name, int namelen, struct autofs_node **anpp)
565 struct autofs_node *anp;
567 if (parent != NULL) {
568 AUTOFS_ASSERT_XLOCKED(parent->an_mount);
570 KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
571 ("node \"%s\" already exists", name));
574 anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
576 anp->an_name = strndup(name, namelen, M_AUTOFS);
578 anp->an_name = strdup(name, M_AUTOFS);
579 anp->an_fileno = atomic_fetchadd_int(&->am_last_fileno, 1);
580 callout_init(&anp->an_callout, 1);
582 * The reason for SX_NOWITNESS here is that witness(4)
583 * cannot tell vnodes apart, so the following perfectly
584 * valid lock order...
586 * vnode lock A -> autofsvlk B -> vnode lock B
588 * ... gets reported as a LOR.
590 sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
591 getnanotime(&anp->an_ctime);
592 anp->an_parent = parent;
595 RB_INSERT(autofs_node_tree, &parent->an_children, anp);
596 RB_INIT(&anp->an_children);
603 autofs_node_find(struct autofs_node *parent, const char *name,
604 int namelen, struct autofs_node **anpp)
606 struct autofs_node *anp, find;
609 AUTOFS_ASSERT_LOCKED(parent->an_mount);
612 find.an_name = strndup(name, namelen, M_AUTOFS);
614 find.an_name = strdup(name, M_AUTOFS);
616 anp = RB_FIND(autofs_node_tree, &parent->an_children, &find);
625 free(find.an_name, M_AUTOFS);
631 autofs_node_delete(struct autofs_node *anp)
633 struct autofs_node *parent;
635 AUTOFS_ASSERT_XLOCKED(anp->an_mount);
636 KASSERT(RB_EMPTY(&anp->an_children), ("have children"));
638 callout_drain(&anp->an_callout);
640 parent = anp->an_parent;
642 RB_REMOVE(autofs_node_tree, &parent->an_children, anp);
643 sx_destroy(&anp->an_vnode_lock);
644 free(anp->an_name, M_AUTOFS);
645 uma_zfree(autofs_node_zone, anp);
649 autofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
655 AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
657 sx_xlock(&anp->an_vnode_lock);
661 error = vget(vp, flags | LK_RETRY, curthread);
663 AUTOFS_WARN("vget failed with error %d", error);
664 sx_xunlock(&anp->an_vnode_lock);
667 if (vp->v_iflag & VI_DOOMED) {
669 * We got forcibly unmounted.
671 AUTOFS_DEBUG("doomed vnode");
672 sx_xunlock(&anp->an_vnode_lock);
679 sx_xunlock(&anp->an_vnode_lock);
683 error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
685 sx_xunlock(&anp->an_vnode_lock);
689 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
691 sx_xunlock(&anp->an_vnode_lock);
697 if (anp->an_parent == NULL)
698 vp->v_vflag |= VV_ROOT;
703 error = insmntque(vp, mp);
705 AUTOFS_DEBUG("insmntque() failed with error %d", error);
706 sx_xunlock(&anp->an_vnode_lock);
710 KASSERT(anp->an_vnode == NULL, ("lost race"));
713 sx_xunlock(&anp->an_vnode_lock);