2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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>
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/taskqueue.h>
46 #include <sys/vnode.h>
47 #include <machine/atomic.h>
50 #include <fs/autofs/autofs.h>
52 static int autofs_trigger_vn(struct vnode *vp, const char *path,
53 int pathlen, struct vnode **newvp);
55 extern struct autofs_softc *autofs_softc;
58 autofs_access(struct vop_access_args *ap)
62 * Nothing to do here; the only kind of access control
63 * needed is in autofs_mkdir().
70 autofs_getattr(struct vop_getattr_args *ap)
72 struct vnode *vp, *newvp;
73 struct autofs_node *anp;
83 KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
86 * The reason we must do this is that some tree-walking software,
87 * namely fts(3), assumes that stat(".") results will not change
88 * between chdir("subdir") and chdir(".."), and fails with ENOENT
91 if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
92 autofs_ignore_thread(curthread) == false) {
93 error = autofs_trigger_vn(vp, "", 0, &newvp);
98 error = VOP_GETATTR(newvp, ap->a_vap,
107 vap->va_nlink = 3; /* XXX */
110 vap->va_rdev = NODEV;
111 vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
112 vap->va_fileid = anp->an_fileno;
113 vap->va_size = 512; /* XXX */
114 vap->va_blocksize = 512;
115 vap->va_mtime = anp->an_ctime;
116 vap->va_atime = anp->an_ctime;
117 vap->va_ctime = anp->an_ctime;
118 vap->va_birthtime = anp->an_ctime;
122 vap->va_bytes = 512; /* XXX */
130 * Unlock the vnode, request automountd(8) action, and then lock it back.
131 * If anything got mounted on top of the vnode, return the new filesystem's
132 * root vnode in 'newvp', locked.
135 autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
136 struct vnode **newvp)
138 struct autofs_node *anp;
139 struct autofs_mount *amp;
140 int error, lock_flags;
143 amp = VFSTOAUTOFS(vp->v_mount);
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.
151 lock_flags = VOP_ISLOCKED(vp);
155 sx_xlock(&autofs_softc->sc_lock);
158 * XXX: Workaround for mounting the same thing multiple times; revisit.
160 if (vp->v_mountedhere != NULL) {
165 error = autofs_trigger(anp, path, pathlen);
167 sx_xunlock(&autofs_softc->sc_lock);
168 vn_lock(vp, lock_flags | LK_RETRY);
170 if ((vp->v_iflag & VI_DOOMED) != 0) {
171 AUTOFS_DEBUG("VI_DOOMED");
178 if (vp->v_mountedhere == NULL) {
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
188 anp->an_cached = false;
191 error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
193 AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
201 autofs_vget_callback(struct mount *mp, void *arg, int flags,
206 return (autofs_node_vn(arg, mp, flags, vpp));
210 autofs_lookup(struct vop_lookup_args *ap)
212 struct vnode *dvp, *newvp, **vpp;
214 struct autofs_mount *amp;
215 struct autofs_node *anp, *child;
216 struct componentname *cnp;
217 int error, lock_flags;
222 amp = VFSTOAUTOFS(mp);
226 if (cnp->cn_flags & ISDOTDOT) {
227 KASSERT(anp->an_parent != NULL, ("NULL parent"));
229 * Note that in this case, dvp is the child vnode, and we
230 * are looking up the parent vnode - exactly reverse from
231 * normal operation. Unlocking dvp requires some rather
232 * tricky unlock/relock dance to prevent mp from being freed;
233 * use vn_vget_ino_gen() which takes care of all that.
235 error = vn_vget_ino_gen(dvp, autofs_vget_callback,
236 anp->an_parent, cnp->cn_lkflags, vpp);
238 AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
245 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
252 if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
253 autofs_ignore_thread(cnp->cn_thread) == false) {
254 error = autofs_trigger_vn(dvp,
255 cnp->cn_nameptr, cnp->cn_namelen, &newvp);
260 error = VOP_LOOKUP(newvp, ap->a_vpp, ap->a_cnp);
263 * Instead of figuring out whether our vnode should
264 * be locked or not given the error and cnp flags,
265 * just "copy" the lock status from vnode returned
266 * by mounted filesystem's VOP_LOOKUP(). Get rid
267 * of that new vnode afterwards.
269 lock_flags = VOP_ISLOCKED(newvp);
270 if (lock_flags == 0) {
281 error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
283 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
285 return (EJUSTRETURN);
293 * XXX: Dropping the node here is ok, because we never remove nodes.
297 error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
299 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
300 return (EJUSTRETURN);
309 autofs_mkdir(struct vop_mkdir_args *ap)
312 struct autofs_node *anp;
313 struct autofs_mount *amp;
314 struct autofs_node *child;
319 amp = VFSTOAUTOFS(vp->v_mount);
322 * Do not allow mkdir() if the calling thread is not
323 * automountd(8) descendant.
325 if (autofs_ignore_thread(curthread) == false)
329 error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
330 ap->a_cnp->cn_namelen, &child);
337 error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
343 autofs_readdir_one(struct uio *uio, const char *name, int fileno)
345 struct dirent dirent;
348 memset(&dirent, 0, sizeof(dirent));
349 dirent.d_type = DT_DIR;
350 dirent.d_reclen = AUTOFS_DELEN;
351 dirent.d_fileno = fileno;
352 /* PFS_DELEN was picked to fit PFS_NAMLEN */
353 for (i = 0; i < AUTOFS_NAMELEN - 1 && name[i] != '\0'; ++i)
354 dirent.d_name[i] = name[i];
355 dirent.d_name[i] = 0;
358 error = uiomove(&dirent, AUTOFS_DELEN, uio);
363 autofs_readdir(struct vop_readdir_args *ap)
365 struct vnode *vp, *newvp;
366 struct autofs_mount *amp;
367 struct autofs_node *anp, *child;
373 amp = VFSTOAUTOFS(vp->v_mount);
377 KASSERT(vp->v_type == VDIR, ("!VDIR"));
379 if (autofs_cached(anp, NULL, 0) == false &&
380 autofs_ignore_thread(curthread) == false) {
381 error = autofs_trigger_vn(vp, "", 0, &newvp);
386 error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
387 ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
393 /* only allow reading entire entries */
394 offset = uio->uio_offset;
395 resid = uio->uio_resid;
396 if (offset < 0 || offset % AUTOFS_DELEN != 0 ||
397 (resid && resid < AUTOFS_DELEN))
402 if (ap->a_eofflag != NULL)
403 *ap->a_eofflag = TRUE;
405 if (offset == 0 && resid >= AUTOFS_DELEN) {
406 error = autofs_readdir_one(uio, ".", anp->an_fileno);
409 offset += AUTOFS_DELEN;
410 resid -= AUTOFS_DELEN;
413 if (offset == AUTOFS_DELEN && resid >= AUTOFS_DELEN) {
414 if (anp->an_parent == NULL) {
418 error = autofs_readdir_one(uio, "..", anp->an_fileno);
420 error = autofs_readdir_one(uio, "..",
421 anp->an_parent->an_fileno);
425 offset += AUTOFS_DELEN;
426 resid -= AUTOFS_DELEN;
429 i = 2; /* Account for "." and "..". */
431 TAILQ_FOREACH(child, &anp->an_children, an_next) {
432 if (resid < AUTOFS_DELEN) {
433 if (ap->a_eofflag != NULL)
439 * Skip entries returned by previous call to getdents().
442 if (i * AUTOFS_DELEN <= offset)
445 error = autofs_readdir_one(uio, child->an_name,
451 offset += AUTOFS_DELEN;
452 resid -= AUTOFS_DELEN;
460 autofs_reclaim(struct vop_reclaim_args *ap)
463 struct autofs_node *anp;
469 * We do not free autofs_node here; instead we are
470 * destroying them in autofs_node_delete().
472 sx_xlock(&anp->an_vnode_lock);
473 anp->an_vnode = NULL;
475 sx_xunlock(&anp->an_vnode_lock);
480 struct vop_vector autofs_vnodeops = {
481 .vop_default = &default_vnodeops,
483 .vop_access = autofs_access,
484 .vop_lookup = autofs_lookup,
485 .vop_create = VOP_EOPNOTSUPP,
486 .vop_getattr = autofs_getattr,
487 .vop_link = VOP_EOPNOTSUPP,
488 .vop_mkdir = autofs_mkdir,
489 .vop_mknod = VOP_EOPNOTSUPP,
490 .vop_read = VOP_EOPNOTSUPP,
491 .vop_readdir = autofs_readdir,
492 .vop_remove = VOP_EOPNOTSUPP,
493 .vop_rename = VOP_EOPNOTSUPP,
494 .vop_rmdir = VOP_EOPNOTSUPP,
495 .vop_setattr = VOP_EOPNOTSUPP,
496 .vop_symlink = VOP_EOPNOTSUPP,
497 .vop_write = VOP_EOPNOTSUPP,
498 .vop_reclaim = autofs_reclaim,
502 autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
503 const char *name, int namelen, struct autofs_node **anpp)
505 struct autofs_node *anp;
507 if (parent != NULL) {
508 AUTOFS_ASSERT_XLOCKED(parent->an_mount);
510 KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
511 ("node \"%s\" already exists", name));
514 anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
516 anp->an_name = strndup(name, namelen, M_AUTOFS);
518 anp->an_name = strdup(name, M_AUTOFS);
519 anp->an_fileno = atomic_fetchadd_int(&->am_last_fileno, 1);
520 callout_init(&anp->an_callout, 1);
522 * The reason for SX_NOWITNESS here is that witness(4)
523 * cannot tell vnodes apart, so the following perfectly
524 * valid lock order...
526 * vnode lock A -> autofsvlk B -> vnode lock B
528 * ... gets reported as a LOR.
530 sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
531 getnanotime(&anp->an_ctime);
532 anp->an_parent = parent;
535 TAILQ_INSERT_TAIL(&parent->an_children, anp, an_next);
536 TAILQ_INIT(&anp->an_children);
543 autofs_node_find(struct autofs_node *parent, const char *name,
544 int namelen, struct autofs_node **anpp)
546 struct autofs_node *anp;
548 AUTOFS_ASSERT_LOCKED(parent->an_mount);
550 TAILQ_FOREACH(anp, &parent->an_children, an_next) {
552 if (strlen(anp->an_name) != namelen)
554 if (strncmp(anp->an_name, name, namelen) != 0)
557 if (strcmp(anp->an_name, name) != 0)
570 autofs_node_delete(struct autofs_node *anp)
572 struct autofs_node *parent;
574 AUTOFS_ASSERT_XLOCKED(anp->an_mount);
575 KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children"));
577 callout_drain(&anp->an_callout);
579 parent = anp->an_parent;
581 TAILQ_REMOVE(&parent->an_children, anp, an_next);
582 sx_destroy(&anp->an_vnode_lock);
583 free(anp->an_name, M_AUTOFS);
584 uma_zfree(autofs_node_zone, anp);
588 autofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
594 AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
596 sx_xlock(&anp->an_vnode_lock);
600 error = vget(vp, flags | LK_RETRY, curthread);
602 AUTOFS_WARN("vget failed with error %d", error);
603 sx_xunlock(&anp->an_vnode_lock);
606 if (vp->v_iflag & VI_DOOMED) {
608 * We got forcibly unmounted.
610 AUTOFS_DEBUG("doomed vnode");
611 sx_xunlock(&anp->an_vnode_lock);
618 sx_xunlock(&anp->an_vnode_lock);
622 error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
624 sx_xunlock(&anp->an_vnode_lock);
628 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
630 sx_xunlock(&anp->an_vnode_lock);
636 if (anp->an_parent == NULL)
637 vp->v_vflag |= VV_ROOT;
642 error = insmntque(vp, mp);
644 AUTOFS_WARN("insmntque() failed with error %d", error);
645 sx_xunlock(&anp->an_vnode_lock);
649 KASSERT(anp->an_vnode == NULL, ("lost race"));
652 sx_xunlock(&anp->an_vnode_lock);