2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/condvar.h>
39 #include <sys/ioccom.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/mount.h>
45 #include <sys/taskqueue.h>
47 #include <sys/vnode.h>
49 #include <fs/autofs/autofs.h>
51 static const char *autofs_opts[] = {
52 "from", "master_options", "master_prefix", NULL
55 extern struct autofs_softc *autofs_softc;
58 autofs_mount(struct mount *mp)
60 struct autofs_mount *amp;
61 char *from, *fspath, *options, *prefix;
64 if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
67 if (mp->mnt_flag & MNT_UPDATE) {
68 autofs_flush(VFSTOAUTOFS(mp));
72 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
75 if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
78 if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
81 if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
84 amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
87 strlcpy(amp->am_from, from, sizeof(amp->am_from));
88 strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
89 strlcpy(amp->am_options, options, sizeof(amp->am_options));
90 strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
91 sx_init(&->am_lock, "autofslk");
92 amp->am_last_fileno = 1;
97 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
101 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
109 vfs_mountedfrom(mp, from);
115 autofs_unmount(struct mount *mp, int mntflags)
117 struct autofs_mount *amp;
118 struct autofs_node *anp;
119 struct autofs_request *ar;
123 amp = VFSTOAUTOFS(mp);
126 if (mntflags & MNT_FORCE)
128 error = vflush(mp, 0, flags, curthread);
130 AUTOFS_DEBUG("vflush failed with error %d", error);
135 * All vnodes are gone, and new one will not appear - so,
136 * no new triggerings. We can iterate over outstanding
137 * autofs_requests and terminate them.
141 sx_xlock(&autofs_softc->sc_lock);
142 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
143 if (ar->ar_mount != amp)
145 ar->ar_error = ENXIO;
147 ar->ar_in_progress = false;
150 sx_xunlock(&autofs_softc->sc_lock);
154 cv_broadcast(&autofs_softc->sc_cv);
155 pause("autofs_umount", 1);
161 * Not terribly efficient, but at least not recursive.
163 while (!RB_EMPTY(&->am_root->an_children)) {
164 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
165 while (!RB_EMPTY(&anp->an_children))
166 anp = RB_MIN(autofs_node_tree, &anp->an_children);
167 autofs_node_delete(anp);
169 autofs_node_delete(amp->am_root);
174 sx_destroy(&->am_lock);
182 autofs_root(struct mount *mp, int flags, struct vnode **vpp)
184 struct autofs_mount *amp;
187 amp = VFSTOAUTOFS(mp);
189 error = autofs_node_vn(amp->am_root, mp, flags, vpp);
195 autofs_statfs(struct mount *mp, struct statfs *sbp)
198 sbp->f_bsize = S_BLKSIZE;
209 static struct vfsops autofs_vfsops = {
210 .vfs_fhtovp = NULL, /* XXX */
211 .vfs_mount = autofs_mount,
212 .vfs_unmount = autofs_unmount,
213 .vfs_root = autofs_root,
214 .vfs_statfs = autofs_statfs,
215 .vfs_init = autofs_init,
216 .vfs_uninit = autofs_uninit,
219 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
220 MODULE_VERSION(autofs, 1);