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/systm.h>
37 #include <sys/condvar.h>
38 #include <sys/ioccom.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mount.h>
43 #include <sys/taskqueue.h>
44 #include <sys/vnode.h>
46 #include <fs/autofs/autofs.h>
48 static const char *autofs_opts[] = {
49 "from", "master_options", "master_prefix", NULL
52 extern struct autofs_softc *autofs_softc;
55 autofs_mount(struct mount *mp)
57 struct autofs_mount *amp;
58 char *from, *fspath, *options, *prefix;
61 if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
64 if (mp->mnt_flag & MNT_UPDATE) {
65 autofs_flush(VFSTOAUTOFS(mp));
69 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
72 if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
75 if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
78 if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
81 amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
84 strlcpy(amp->am_from, from, sizeof(amp->am_from));
85 strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
86 strlcpy(amp->am_options, options, sizeof(amp->am_options));
87 strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
88 sx_init(&->am_lock, "autofslk");
89 amp->am_last_fileno = 1;
94 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
98 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
106 vfs_mountedfrom(mp, from);
112 autofs_unmount(struct mount *mp, int mntflags)
114 struct autofs_mount *amp;
115 struct autofs_node *anp;
116 struct autofs_request *ar;
120 amp = VFSTOAUTOFS(mp);
123 if (mntflags & MNT_FORCE)
125 error = vflush(mp, 0, flags, curthread);
127 AUTOFS_WARN("vflush failed with error %d", error);
132 * All vnodes are gone, and new one will not appear - so,
133 * no new triggerings. We can iterate over outstanding
134 * autofs_requests and terminate them.
138 sx_xlock(&autofs_softc->sc_lock);
139 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
140 if (ar->ar_mount != amp)
142 ar->ar_error = ENXIO;
144 ar->ar_in_progress = false;
147 sx_xunlock(&autofs_softc->sc_lock);
151 cv_broadcast(&autofs_softc->sc_cv);
152 pause("autofs_umount", 1);
158 * Not terribly efficient, but at least not recursive.
160 while (!TAILQ_EMPTY(&->am_root->an_children)) {
161 anp = TAILQ_FIRST(&->am_root->an_children);
162 while (!TAILQ_EMPTY(&anp->an_children))
163 anp = TAILQ_FIRST(&anp->an_children);
164 autofs_node_delete(anp);
166 autofs_node_delete(amp->am_root);
171 sx_destroy(&->am_lock);
179 autofs_root(struct mount *mp, int flags, struct vnode **vpp)
181 struct autofs_mount *amp;
184 amp = VFSTOAUTOFS(mp);
186 error = autofs_node_vn(amp->am_root, mp, flags, vpp);
192 autofs_statfs(struct mount *mp, struct statfs *sbp)
206 static struct vfsops autofs_vfsops = {
207 .vfs_fhtovp = NULL, /* XXX */
208 .vfs_mount = autofs_mount,
209 .vfs_unmount = autofs_unmount,
210 .vfs_root = autofs_root,
211 .vfs_statfs = autofs_statfs,
212 .vfs_init = autofs_init,
213 .vfs_uninit = autofs_uninit,
216 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
217 MODULE_VERSION(autofs, 1);