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>
44 #include <sys/taskqueue.h>
46 #include <sys/vnode.h>
48 #include <fs/autofs/autofs.h>
50 static const char *autofs_opts[] = {
51 "from", "master_options", "master_prefix", NULL
54 extern struct autofs_softc *autofs_softc;
57 autofs_mount(struct mount *mp)
59 struct autofs_mount *amp;
60 char *from, *fspath, *options, *prefix;
63 if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
66 if (mp->mnt_flag & MNT_UPDATE) {
67 autofs_flush(VFSTOAUTOFS(mp));
71 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
74 if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
77 if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
80 if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
83 amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
86 strlcpy(amp->am_from, from, sizeof(amp->am_from));
87 strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
88 strlcpy(amp->am_options, options, sizeof(amp->am_options));
89 strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
90 sx_init(&->am_lock, "autofslk");
91 amp->am_last_fileno = 1;
96 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
100 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
108 vfs_mountedfrom(mp, from);
114 autofs_unmount(struct mount *mp, int mntflags)
116 struct autofs_mount *amp;
117 struct autofs_node *anp;
118 struct autofs_request *ar;
122 amp = VFSTOAUTOFS(mp);
125 if (mntflags & MNT_FORCE)
127 error = vflush(mp, 0, flags, curthread);
129 AUTOFS_WARN("vflush failed with error %d", error);
134 * All vnodes are gone, and new one will not appear - so,
135 * no new triggerings. We can iterate over outstanding
136 * autofs_requests and terminate them.
140 sx_xlock(&autofs_softc->sc_lock);
141 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
142 if (ar->ar_mount != amp)
144 ar->ar_error = ENXIO;
146 ar->ar_in_progress = false;
149 sx_xunlock(&autofs_softc->sc_lock);
153 cv_broadcast(&autofs_softc->sc_cv);
154 pause("autofs_umount", 1);
160 * Not terribly efficient, but at least not recursive.
162 while (!RB_EMPTY(&->am_root->an_children)) {
163 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
164 while (!RB_EMPTY(&anp->an_children))
165 anp = RB_MIN(autofs_node_tree, &anp->an_children);
166 autofs_node_delete(anp);
168 autofs_node_delete(amp->am_root);
173 sx_destroy(&->am_lock);
181 autofs_root(struct mount *mp, int flags, struct vnode **vpp)
183 struct autofs_mount *amp;
186 amp = VFSTOAUTOFS(mp);
188 error = autofs_node_vn(amp->am_root, mp, flags, vpp);
194 autofs_statfs(struct mount *mp, struct statfs *sbp)
197 sbp->f_bsize = S_BLKSIZE;
208 static struct vfsops autofs_vfsops = {
209 .vfs_fhtovp = NULL, /* XXX */
210 .vfs_mount = autofs_mount,
211 .vfs_unmount = autofs_unmount,
212 .vfs_root = autofs_root,
213 .vfs_statfs = autofs_statfs,
214 .vfs_init = autofs_init,
215 .vfs_uninit = autofs_uninit,
218 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
219 MODULE_VERSION(autofs, 1);