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/systm.h>
39 #include <sys/condvar.h>
40 #include <sys/ioccom.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mount.h>
46 #include <sys/taskqueue.h>
48 #include <sys/vnode.h>
50 #include <fs/autofs/autofs.h>
52 static const char *autofs_opts[] = {
53 "from", "master_options", "master_prefix", NULL
56 extern struct autofs_softc *autofs_softc;
59 autofs_mount(struct mount *mp)
61 struct autofs_mount *amp;
62 char *from, *fspath, *options, *prefix;
65 if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
68 if (mp->mnt_flag & MNT_UPDATE) {
69 autofs_flush(VFSTOAUTOFS(mp));
73 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
76 if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
79 if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
82 if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
85 amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
88 strlcpy(amp->am_from, from, sizeof(amp->am_from));
89 strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
90 strlcpy(amp->am_options, options, sizeof(amp->am_options));
91 strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
92 sx_init(&->am_lock, "autofslk");
93 amp->am_last_fileno = 1;
98 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
102 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
110 vfs_mountedfrom(mp, from);
116 autofs_unmount(struct mount *mp, int mntflags)
118 struct autofs_mount *amp;
119 struct autofs_node *anp;
120 struct autofs_request *ar;
124 amp = VFSTOAUTOFS(mp);
127 if (mntflags & MNT_FORCE)
129 error = vflush(mp, 0, flags, curthread);
131 AUTOFS_WARN("vflush failed with error %d", error);
136 * All vnodes are gone, and new one will not appear - so,
137 * no new triggerings. We can iterate over outstanding
138 * autofs_requests and terminate them.
142 sx_xlock(&autofs_softc->sc_lock);
143 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
144 if (ar->ar_mount != amp)
146 ar->ar_error = ENXIO;
148 ar->ar_in_progress = false;
151 sx_xunlock(&autofs_softc->sc_lock);
155 cv_broadcast(&autofs_softc->sc_cv);
156 pause("autofs_umount", 1);
162 * Not terribly efficient, but at least not recursive.
164 while (!RB_EMPTY(&->am_root->an_children)) {
165 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
166 while (!RB_EMPTY(&anp->an_children))
167 anp = RB_MIN(autofs_node_tree, &anp->an_children);
168 autofs_node_delete(anp);
170 autofs_node_delete(amp->am_root);
175 sx_destroy(&->am_lock);
183 autofs_root(struct mount *mp, int flags, struct vnode **vpp)
185 struct autofs_mount *amp;
188 amp = VFSTOAUTOFS(mp);
190 error = autofs_node_vn(amp->am_root, mp, flags, vpp);
196 autofs_statfs(struct mount *mp, struct statfs *sbp)
199 sbp->f_bsize = S_BLKSIZE;
210 static struct vfsops autofs_vfsops = {
211 .vfs_fhtovp = NULL, /* XXX */
212 .vfs_mount = autofs_mount,
213 .vfs_unmount = autofs_unmount,
214 .vfs_root = autofs_root,
215 .vfs_statfs = autofs_statfs,
216 .vfs_init = autofs_init,
217 .vfs_uninit = autofs_uninit,
220 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
221 MODULE_VERSION(autofs, 1);