2 * Copyright (c) 2016 Tomohiro Kusumi <tkusumi@netbsd.org>
3 * Copyright (c) 2016 The DragonFly Project
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/kernel.h>
34 #include <sys/module.h>
38 #include "autofs_mount.h"
40 static int autofs_statfs(struct mount *mp, struct statfs *sbp,
43 static struct objcache_malloc_args autofs_request_args = {
44 sizeof(struct autofs_request), M_AUTOFS,
46 static struct objcache_malloc_args autofs_node_args = {
47 sizeof(struct autofs_node), M_AUTOFS,
51 autofs_init(struct vfsconf *vfsp)
53 KASSERT(autofs_softc == NULL,
54 ("softc %p, should be NULL", autofs_softc));
56 autofs_softc = kmalloc(sizeof(*autofs_softc), M_AUTOFS,
59 autofs_request_objcache = objcache_create("autofs_request", 0, 0,
61 objcache_malloc_alloc_zero, objcache_malloc_free,
62 &autofs_request_args);
64 autofs_node_objcache = objcache_create("autofs_node", 0, 0,
66 objcache_malloc_alloc_zero, objcache_malloc_free,
69 TAILQ_INIT(&autofs_softc->sc_requests);
70 cv_init(&autofs_softc->sc_cv, "autofscv");
71 mtx_init(&autofs_softc->sc_lock, "autofssclk");
72 autofs_softc->sc_dev_opened = false;
74 autofs_softc->sc_cdev = make_dev(&autofs_ops, 0, UID_ROOT, GID_OPERATOR,
76 if (autofs_softc->sc_cdev == NULL) {
77 AUTOFS_WARN("failed to create device node");
78 objcache_destroy(autofs_request_objcache);
79 objcache_destroy(autofs_node_objcache);
80 kfree(autofs_softc, M_AUTOFS);
83 autofs_softc->sc_cdev->si_drv1 = autofs_softc;
89 autofs_uninit(struct vfsconf *vfsp)
91 mtx_lock_ex_quick(&autofs_softc->sc_lock);
92 if (autofs_softc->sc_dev_opened) {
93 mtx_unlock_ex(&autofs_softc->sc_lock);
97 if (autofs_softc->sc_cdev != NULL)
98 destroy_dev(autofs_softc->sc_cdev);
100 objcache_destroy(autofs_request_objcache);
101 objcache_destroy(autofs_node_objcache);
103 mtx_unlock_ex(&autofs_softc->sc_lock);
105 kfree(autofs_softc, M_AUTOFS); /* race with open */
112 autofs_mount(struct mount *mp, char *mntpt, caddr_t data, struct ucred *cred)
114 struct autofs_mount_info info;
115 struct autofs_mount *amp;
116 struct statfs *sbp = &mp->mnt_stat;
119 if (mp->mnt_flag & MNT_UPDATE) {
120 autofs_flush(VFSTOAUTOFS(mp));
124 error = copyin(data, &info, sizeof(info));
129 * Copy-in ->f_mntfromname string.
131 memset(sbp->f_mntfromname, 0, sizeof(sbp->f_mntfromname));
132 error = copyinstr(info.from, sbp->f_mntfromname,
133 sizeof(sbp->f_mntfromname), NULL);
137 * Copy-in ->f_mntonname string.
139 memset(sbp->f_mntonname, 0, sizeof(sbp->f_mntonname));
140 error = copyinstr(mntpt, sbp->f_mntonname, sizeof(sbp->f_mntonname),
146 * Allocate the autofs mount.
148 amp = kmalloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
149 mp->mnt_data = (qaddr_t)amp;
150 strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
151 strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
154 * Copy-in master_options string.
156 error = copyinstr(info.master_options, amp->am_options,
157 sizeof(amp->am_options), NULL);
161 * Copy-in master_prefix string.
163 error = copyinstr(info.master_prefix, amp->am_prefix,
164 sizeof(amp->am_prefix), NULL);
169 * Initialize the autofs mount.
171 mtx_init(&->am_lock, "autofsmnlk");
172 amp->am_last_ino = AUTOFS_ROOTINO;
174 mtx_lock_ex_quick(&->am_lock);
175 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
176 mtx_unlock_ex(&->am_lock);
177 KKASSERT(error == 0);
178 KKASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
181 vfs_add_vnodeops(mp, &autofs_vnode_vops, &mp->mnt_vn_norm_ops);
183 VFS_STATFS(mp, &mp->mnt_stat, cred);
188 kfree(amp, M_AUTOFS);
193 autofs_unmount(struct mount *mp, int mntflags)
195 struct autofs_mount *amp = VFSTOAUTOFS(mp);
199 if (mntflags & MNT_FORCE)
201 error = vflush(mp, 0, flags);
203 AUTOFS_WARN("vflush failed with error %d", error);
208 * All vnodes are gone, and new one will not appear - so,
209 * no new triggerings.
212 struct autofs_request *ar;
217 mtx_lock_ex_quick(&autofs_softc->sc_lock);
218 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
219 if (ar->ar_mount != amp)
221 ar->ar_error = ENXIO;
223 ar->ar_in_progress = false;
226 if (found == false) {
227 mtx_unlock_ex(&autofs_softc->sc_lock);
231 cv_broadcast(&autofs_softc->sc_cv);
232 mtx_unlock_ex(&autofs_softc->sc_lock);
234 tsleep(&dummy, 0, "autofs_umount", hz);
237 mtx_lock_ex_quick(&->am_lock);
238 while (!RB_EMPTY(&->am_root->an_children)) {
239 struct autofs_node *anp;
241 * Force delete all nodes when more than one level of
242 * directories are created via indirect map. Autofs doesn't
243 * support rmdir(2), thus this is the only way to get out.
245 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
246 while (!RB_EMPTY(&anp->an_children))
247 anp = RB_MIN(autofs_node_tree, &anp->an_children);
248 autofs_node_delete(anp);
250 autofs_node_delete(amp->am_root);
252 mtx_unlock_ex(&->am_lock);
254 mtx_uninit(&->am_lock);
256 kfree(amp, M_AUTOFS);
262 autofs_root(struct mount *mp, struct vnode **vpp)
264 struct autofs_mount *amp = VFSTOAUTOFS(mp);
267 KASSERT(amp->am_root, ("no root node"));
269 error = autofs_node_vn(amp->am_root, mp, LK_EXCLUSIVE, vpp);
271 struct vnode *vp = *vpp;
273 KKASSERT(vp->v_type == VDIR);
280 autofs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
282 sbp->f_bsize = S_BLKSIZE;
294 autofs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
296 sbp->f_bsize = S_BLKSIZE;
307 static struct vfsops autofs_vfsops = {
309 .vfs_mount = autofs_mount,
310 .vfs_unmount = autofs_unmount,
311 .vfs_root = autofs_root,
312 .vfs_statfs = autofs_statfs,
313 .vfs_statvfs = autofs_statvfs,
314 .vfs_init = autofs_init,
315 .vfs_uninit = autofs_uninit,
318 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_MPSAFE);
319 MODULE_VERSION(autofs, 1);