2 * Copyright (c) 2016 Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
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,
63 &autofs_request_args);
65 autofs_node_objcache = objcache_create("autofs_node", 0, 0,
67 objcache_malloc_alloc_zero,
71 TAILQ_INIT(&autofs_softc->sc_requests);
72 cv_init(&autofs_softc->sc_cv, "autofscv");
73 lockinit(&autofs_softc->sc_lock, "autofssclk", 0, 0);
74 autofs_softc->sc_dev_opened = false;
76 autofs_softc->sc_cdev = make_dev(&autofs_ops, 0, UID_ROOT,
77 GID_OPERATOR, 0640, "autofs");
78 if (autofs_softc->sc_cdev == NULL) {
79 AUTOFS_WARN("failed to create device node");
80 objcache_destroy(autofs_request_objcache);
81 objcache_destroy(autofs_node_objcache);
82 kfree(autofs_softc, M_AUTOFS);
86 autofs_softc->sc_cdev->si_drv1 = autofs_softc;
92 autofs_uninit(struct vfsconf *vfsp)
94 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
95 if (autofs_softc->sc_dev_opened) {
96 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
100 if (autofs_softc->sc_cdev != NULL)
101 destroy_dev(autofs_softc->sc_cdev);
103 objcache_destroy(autofs_request_objcache);
104 objcache_destroy(autofs_node_objcache);
106 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
108 kfree(autofs_softc, M_AUTOFS); /* race with open */
115 autofs_mount(struct mount *mp, char *mntpt, caddr_t data, struct ucred *cred)
117 struct autofs_mount_info info;
118 struct autofs_mount *amp;
119 struct statfs *sbp = &mp->mnt_stat;
122 if (mp->mnt_flag & MNT_UPDATE) {
123 autofs_flush(VFSTOAUTOFS(mp));
127 error = copyin(data, &info, sizeof(info));
132 * Copy-in ->f_mntfromname string.
134 memset(sbp->f_mntfromname, 0, sizeof(sbp->f_mntfromname));
135 error = copyinstr(info.from, sbp->f_mntfromname,
136 sizeof(sbp->f_mntfromname), NULL);
140 * Copy-in ->f_mntonname string.
142 memset(sbp->f_mntonname, 0, sizeof(sbp->f_mntonname));
143 error = copyinstr(mntpt, sbp->f_mntonname,
144 sizeof(sbp->f_mntonname), NULL);
149 * Allocate the autofs mount.
151 amp = kmalloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
152 mp->mnt_data = (qaddr_t)amp;
154 strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
155 strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
158 * Copy-in master_options string.
160 error = copyinstr(info.master_options, amp->am_options,
161 sizeof(amp->am_options), NULL);
165 * Copy-in master_prefix string.
167 error = copyinstr(info.master_prefix, amp->am_prefix,
168 sizeof(amp->am_prefix), NULL);
173 * Initialize the autofs mount.
175 mtx_init(&->am_lock, "autofsmnlk");
176 amp->am_last_ino = AUTOFS_ROOTINO;
179 vfs_add_vnodeops(mp, &autofs_vnode_vops, &mp->mnt_vn_norm_ops);
181 mtx_lock_ex_quick(&->am_lock);
182 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
183 mtx_unlock_ex(&->am_lock);
184 KKASSERT(error == 0);
185 KKASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
187 autofs_statfs(mp, sbp, cred);
192 kfree(amp, M_AUTOFS);
197 autofs_unmount(struct mount *mp, int mntflags)
199 struct autofs_mount *amp = VFSTOAUTOFS(mp);
203 if (mntflags & MNT_FORCE)
205 error = vflush(mp, 0, flags);
207 AUTOFS_WARN("vflush failed with error %d", error);
212 * All vnodes are gone, and new one will not appear - so,
213 * no new triggerings.
216 struct autofs_request *ar;
221 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
222 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
223 if (ar->ar_mount != amp)
225 ar->ar_error = ENXIO;
227 ar->ar_in_progress = false;
230 if (found == false) {
231 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
235 cv_broadcast(&autofs_softc->sc_cv);
236 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
238 tsleep(&dummy, 0, "autofs_umount", hz);
241 mtx_lock_ex_quick(&->am_lock);
242 while (!RB_EMPTY(&->am_root->an_children)) {
243 struct autofs_node *anp;
245 * Force delete all nodes when more than one level of
246 * directories are created via indirect map. Autofs doesn't
247 * support rmdir(2), thus this is the only way to get out.
249 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
250 while (!RB_EMPTY(&anp->an_children))
251 anp = RB_MIN(autofs_node_tree, &anp->an_children);
252 autofs_node_delete(anp);
254 autofs_node_delete(amp->am_root);
256 mtx_unlock_ex(&->am_lock);
258 mtx_uninit(&->am_lock);
260 kfree(amp, M_AUTOFS);
266 autofs_root(struct mount *mp, struct vnode **vpp)
268 struct autofs_mount *amp = VFSTOAUTOFS(mp);
271 if (amp->am_root == NULL) {
272 AUTOFS_FATAL("called without root node %p", mp);
277 error = autofs_node_vn(amp->am_root, mp, LK_EXCLUSIVE, vpp);
278 (*vpp)->v_flag |= VROOT;
279 KKASSERT((*vpp)->v_type == VDIR);
286 autofs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
288 sbp->f_bsize = S_BLKSIZE;
300 autofs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
302 sbp->f_bsize = S_BLKSIZE;
313 static struct vfsops autofs_vfsops = {
314 .vfs_mount = autofs_mount,
315 .vfs_unmount = autofs_unmount,
316 .vfs_root = autofs_root,
317 .vfs_statfs = autofs_statfs,
318 .vfs_statvfs = autofs_statvfs,
319 .vfs_init = autofs_init,
320 .vfs_uninit = autofs_uninit,
323 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_MPSAFE);
324 MODULE_VERSION(autofs, 1);