xref: /netbsd-src/sys/fs/autofs/autofs_vfsops.c (revision 87d689fb734c654d2486f87f7be32f1b53ecdbec)
1 /*-
2  * Copyright (c) 2017 The NetBSD Foundation, Inc.
3  * Copyright (c) 2016 The DragonFly Project
4  * Copyright (c) 2014 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
9  *
10  * This software was developed by Edward Tomasz Napierala under sponsorship
11  * from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: autofs_vfsops.c,v 1.3 2018/01/13 22:06:21 christos Exp $");
37 
38 
39 #include "autofs.h"
40 #include "autofs_mount.h"
41 
42 #include <sys/stat.h>
43 #include <sys/sysctl.h>
44 #include <miscfs/genfs/genfs.h>
45 
46 MODULE(MODULE_CLASS_VFS, autofs, NULL);
47 
48 static int	autofs_statvfs(struct mount *, struct statvfs *);
49 static int	autofs_sysctl_create(void);
50 
51 static void
52 autofs_init(void)
53 {
54 
55 	KASSERT(!autofs_softc);
56 
57 	autofs_softc = kmem_zalloc(sizeof(*autofs_softc), KM_SLEEP);
58 
59 	pool_init(&autofs_request_pool, sizeof(struct autofs_request), 0, 0, 0,
60 	    "autofs_request", &pool_allocator_nointr, IPL_NONE);
61 	pool_init(&autofs_node_pool, sizeof(struct autofs_node), 0, 0, 0,
62 	    "autofs_node", &pool_allocator_nointr, IPL_NONE);
63 
64 	TAILQ_INIT(&autofs_softc->sc_requests);
65 	cv_init(&autofs_softc->sc_cv, "autofscv");
66 	mutex_init(&autofs_softc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
67 	autofs_softc->sc_dev_opened = false;
68 
69 	autofs_sysctl_create();
70 	workqueue_create(&autofs_tmo_wq, "autofstmo",
71 	    autofs_timeout_wq, NULL, 0, 0, WQ_MPSAFE);
72 }
73 
74 static void
75 autofs_done(void)
76 {
77 	KASSERT(autofs_softc);
78 	KASSERT(!autofs_softc->sc_dev_opened);
79 
80 	workqueue_destroy(autofs_tmo_wq);
81 
82 	struct autofs_softc *sc = autofs_softc;
83 	autofs_softc = NULL;
84 
85 	cv_destroy(&sc->sc_cv);
86 	mutex_destroy(&sc->sc_lock);
87 
88 	pool_destroy(&autofs_request_pool);
89 	pool_destroy(&autofs_node_pool);
90 
91 	kmem_free(sc, sizeof(*sc));
92 }
93 
94 static int
95 autofs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
96 {
97 	struct autofs_args *args = data;
98 	struct autofs_mount *amp;
99 	struct statvfs *sbp = &mp->mnt_stat;
100 	int error;
101 
102 	if (!args)
103 		return EINVAL;
104 
105 	/*
106 	 * MNT_GETARGS is unsupported.  Autofs is mounted via automount(8) by
107 	 * parsing /etc/auto_master instead of regular mount(8) variants with
108 	 * -o getargs support, thus not really needed either.
109 	 */
110 	if (mp->mnt_flag & MNT_GETARGS)
111 		return EOPNOTSUPP;
112 
113 	if (mp->mnt_flag & MNT_UPDATE) {
114 		autofs_flush(VFSTOAUTOFS(mp));
115 		return 0;
116 	}
117 
118 	/*
119 	 * Allocate the autofs mount.
120 	 */
121 	amp = kmem_zalloc(sizeof(*amp), KM_SLEEP);
122 	mp->mnt_data = amp;
123 	amp->am_mp = mp;
124 
125 	/*
126 	 * Copy-in master_options string.
127 	 */
128 	error = copyinstr(args->master_options, amp->am_options,
129 	    sizeof(amp->am_options), NULL);
130 	if (error)
131 		goto fail;
132 
133 	/*
134 	 * Copy-in master_prefix string.
135 	 */
136 	error = copyinstr(args->master_prefix, amp->am_prefix,
137 	    sizeof(amp->am_prefix), NULL);
138 	if (error)
139 		goto fail;
140 
141 	/*
142 	 * Initialize the autofs mount.
143 	 */
144 	mutex_init(&amp->am_lock, MUTEX_DEFAULT, IPL_NONE);
145 	amp->am_last_ino = AUTOFS_ROOTINO;
146 
147 	mutex_enter(&amp->am_lock);
148 	error = autofs_node_new(NULL, amp, ".", -1, &amp->am_root);
149 	if (error) {
150 		mutex_exit(&amp->am_lock);
151 		goto fail;
152 	}
153 	mutex_exit(&amp->am_lock);
154 	KASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
155 
156 	autofs_statvfs(mp, sbp);
157 	vfs_getnewfsid(mp);
158 
159 	error = set_statvfs_info(path, UIO_USERSPACE, args->from, UIO_USERSPACE,
160 	    mp->mnt_op->vfs_name, mp, curlwp);
161 	if (error)
162 		goto fail;
163 	strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
164 	strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
165 
166 	return 0;
167 
168 fail:
169 	kmem_free(amp, sizeof(*amp));
170 	return error;
171 }
172 
173 static int
174 autofs_unmount(struct mount *mp, int mntflags)
175 {
176 	struct autofs_mount *amp = VFSTOAUTOFS(mp);
177 	int error, flags;
178 
179 	flags = 0;
180 	if (mntflags & MNT_FORCE)
181 		flags |= FORCECLOSE;
182 	error = vflush(mp, NULL, flags);
183 	if (error) {
184 		AUTOFS_WARN("vflush failed with error %d", error);
185 		return error;
186 	}
187 
188 	/*
189 	 * All vnodes are gone, and new one will not appear - so,
190 	 * no new triggerings.
191 	 */
192 	for (;;) {
193 		struct autofs_request *ar;
194 		int dummy;
195 		bool found;
196 
197 		found = false;
198 		mutex_enter(&autofs_softc->sc_lock);
199 		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
200 			if (ar->ar_mount != amp)
201 				continue;
202 			ar->ar_error = ENXIO;
203 			ar->ar_done = true;
204 			ar->ar_in_progress = false;
205 			found = true;
206 		}
207 		if (found == false) {
208 			mutex_exit(&autofs_softc->sc_lock);
209 			break;
210 		}
211 
212 		cv_broadcast(&autofs_softc->sc_cv);
213 		mutex_exit(&autofs_softc->sc_lock);
214 
215 		tsleep(&dummy, 0, "autofs_umount", hz);
216 	}
217 
218 	mutex_enter(&amp->am_lock);
219 	while (!RB_EMPTY(&amp->am_root->an_children)) {
220 		struct autofs_node *anp;
221 		anp = RB_MIN(autofs_node_tree, &amp->am_root->an_children);
222 		if (!RB_EMPTY(&anp->an_children)) {
223 			AUTOFS_DEBUG("%s: %s has children", __func__,
224 			    anp->an_name);
225 			mutex_exit(&amp->am_lock);
226 			return EBUSY;
227 		}
228 
229 		autofs_node_delete(anp);
230 	}
231 	autofs_node_delete(amp->am_root);
232 	mp->mnt_data = NULL;
233 	mutex_exit(&amp->am_lock);
234 
235 	mutex_destroy(&amp->am_lock);
236 
237 	kmem_free(amp, sizeof(*amp));
238 
239 	return 0;
240 }
241 
242 static int
243 autofs_start(struct mount *mp, int flags)
244 {
245 
246 	return 0;
247 }
248 
249 static int
250 autofs_root(struct mount *mp, struct vnode **vpp)
251 {
252 	struct autofs_node *anp = VFSTOAUTOFS(mp)->am_root;
253 	int error;
254 
255 	error = vcache_get(mp, &anp, sizeof(anp), vpp);
256 	if (error)
257 		return error;
258 	error = vn_lock(*vpp, LK_EXCLUSIVE);
259 	if (error) {
260 		vrele(*vpp);
261 		*vpp = NULL;
262 		return error;
263 	}
264 
265 	return 0;
266 }
267 
268 static int
269 autofs_statvfs(struct mount *mp, struct statvfs *sbp)
270 {
271 
272 	sbp->f_bsize = S_BLKSIZE;
273 	sbp->f_frsize = S_BLKSIZE;
274 	sbp->f_iosize = 0;
275 	sbp->f_blocks = 0;
276 	sbp->f_bfree = 0;
277 	sbp->f_bavail = 0;
278 	sbp->f_bresvd = 0;
279 	sbp->f_files = 0;
280 	sbp->f_ffree = 0;
281 	sbp->f_favail = 0;
282 	sbp->f_fresvd = 0;
283 
284 	copy_statvfs_info(sbp, mp);
285 
286 	return 0;
287 }
288 
289 static int
290 autofs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
291 {
292 
293 	return 0;
294 }
295 
296 static int
297 autofs_loadvnode(struct mount *mp, struct vnode *vp,
298     const void *key, size_t key_len, const void **new_key)
299 {
300 	struct autofs_node *anp;
301 
302 	KASSERT(key_len == sizeof(anp));
303 	memcpy(&anp, key, key_len);
304 	KASSERT(!anp->an_vnode);
305 
306 	vp->v_tag = VT_AUTOFS;
307 	vp->v_type = VDIR;
308 	vp->v_op = autofs_vnodeop_p;
309 	vp->v_data = anp;
310 
311 	if (anp->an_ino == AUTOFS_ROOTINO)
312 		vp->v_vflag |= VV_ROOT;
313 
314 	anp->an_vnode = vp;
315 	uvm_vnp_setsize(vp, 0);
316 
317 	*new_key = &vp->v_data;
318 
319 	return 0;
320 }
321 
322 static const struct vnodeopv_desc * const autofs_vnodeopv_descs[] = {
323 	&autofs_vnodeop_opv_desc,
324 	NULL,
325 };
326 
327 static struct vfsops autofs_vfsops = {
328 	.vfs_name = MOUNT_AUTOFS,
329 	.vfs_min_mount_data = sizeof(struct autofs_args),
330 	.vfs_mount = autofs_mount,
331 	.vfs_start = autofs_start,
332 	.vfs_unmount = autofs_unmount,
333 	.vfs_root = autofs_root,
334 	.vfs_quotactl = (void *)eopnotsupp,
335 	.vfs_statvfs = autofs_statvfs,
336 	.vfs_sync = autofs_sync,
337 	.vfs_vget = (void *)eopnotsupp,
338 	.vfs_loadvnode = (void *)autofs_loadvnode,
339 	.vfs_newvnode = (void *)eopnotsupp,
340 	.vfs_fhtovp = (void *)eopnotsupp,
341 	.vfs_vptofh = (void *)eopnotsupp,
342 	.vfs_init = autofs_init,
343 	.vfs_reinit = (void *)eopnotsupp,
344 	.vfs_done = autofs_done,
345 	.vfs_mountroot = (void *)eopnotsupp,
346 	.vfs_snapshot = (void *)eopnotsupp,
347 	.vfs_extattrctl = (void *)eopnotsupp,
348 	.vfs_suspendctl = (void *)genfs_suspendctl,
349 	.vfs_renamelock_enter = (void *)eopnotsupp,
350 	.vfs_renamelock_exit = (void *)eopnotsupp,
351 	.vfs_fsync = (void *)eopnotsupp,
352 	.vfs_opv_descs = autofs_vnodeopv_descs
353 };
354 
355 #define AUTOFS_SYSCTL_DEBUG		1
356 #define AUTOFS_SYSCTL_MOUNT_ON_STAT	2
357 #define AUTOFS_SYSCTL_TIMEOUT		3
358 #define AUTOFS_SYSCTL_CACHE		4
359 #define AUTOFS_SYSCTL_RETRY_ATTEMPTS	5
360 #define AUTOFS_SYSCTL_RETRY_DELAY	6
361 #define AUTOFS_SYSCTL_INTERRUPTIBLE	7
362 
363 static struct sysctllog *autofs_sysctl_log;
364 
365 static int
366 autofs_sysctl_create(void)
367 {
368 	int error;
369 
370 	/*
371 	 * XXX the "33" below could be dynamic, thereby eliminating one
372 	 * more instance of the "number to vfs" mapping problem, but
373 	 * "33" is the order as taken from sys/mount.h
374 	 */
375 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
376 	    CTLFLAG_PERMANENT,
377 	    CTLTYPE_NODE, "autofs",
378 	    SYSCTL_DESCR("Automounter filesystem"),
379 	    NULL, 0, NULL, 0,
380 	    CTL_VFS, 33, CTL_EOL);
381 	if (error)
382 		goto fail;
383 
384 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
385 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
386 	    CTLTYPE_INT, "autofs_debug",
387 	    SYSCTL_DESCR("Enable debug messages"),
388 	    NULL, 0, &autofs_debug, 0,
389 	    CTL_VFS, 33, AUTOFS_SYSCTL_DEBUG, CTL_EOL);
390 	if (error)
391 		goto fail;
392 
393 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
394 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
395 	    CTLTYPE_INT, "autofs_mount_on_stat",
396 	    SYSCTL_DESCR("Trigger mount on stat(2) on mountpoint"),
397 	    NULL, 0, &autofs_mount_on_stat, 0,
398 	    CTL_VFS, 33, AUTOFS_SYSCTL_MOUNT_ON_STAT, CTL_EOL);
399 	if (error)
400 		goto fail;
401 
402 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
403 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
404 	    CTLTYPE_INT, "autofs_timeout",
405 	    SYSCTL_DESCR("Number of seconds to wait for automountd(8)"),
406 	    NULL, 0, &autofs_timeout, 0,
407 	    CTL_VFS, 33, AUTOFS_SYSCTL_TIMEOUT, CTL_EOL);
408 	if (error)
409 		goto fail;
410 
411 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
412 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
413 	    CTLTYPE_INT, "autofs_cache",
414 	    SYSCTL_DESCR("Number of seconds to wait before reinvoking"),
415 	    NULL, 0, &autofs_cache, 0,
416 	    CTL_VFS, 33, AUTOFS_SYSCTL_CACHE, CTL_EOL);
417 	if (error)
418 		goto fail;
419 
420 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
421 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
422 	    CTLTYPE_INT, "autofs_retry_attempts",
423 	    SYSCTL_DESCR("Number of attempts before failing mount"),
424 	    NULL, 0, &autofs_retry_attempts, 0,
425 	    CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_ATTEMPTS, CTL_EOL);
426 	if (error)
427 		goto fail;
428 
429 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
430 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
431 	    CTLTYPE_INT, "autofs_retry_delay",
432 	    SYSCTL_DESCR("Number of seconds before retrying"),
433 	    NULL, 0, &autofs_retry_delay, 0,
434 	    CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_DELAY, CTL_EOL);
435 	if (error)
436 		goto fail;
437 
438 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
439 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
440 	    CTLTYPE_INT, "autofs_interruptible",
441 	    SYSCTL_DESCR("Allow requests to be interrupted by signal"),
442 	    NULL, 0, &autofs_interruptible, 0,
443 	    CTL_VFS, 33, AUTOFS_SYSCTL_INTERRUPTIBLE, CTL_EOL);
444 	if (error)
445 		goto fail;
446 
447 	return 0;
448 fail:
449 	AUTOFS_WARN("sysctl_createv failed with error %d", error);
450 
451 	return error;
452 }
453 
454 extern const struct cdevsw autofs_cdevsw;
455 
456 static int
457 autofs_modcmd(modcmd_t cmd, void *arg)
458 {
459 #ifdef _MODULE
460 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
461 #endif
462 	int error = 0;
463 
464 	switch (cmd) {
465 	case MODULE_CMD_INIT:
466 		error = vfs_attach(&autofs_vfsops);
467 		if (error)
468 			break;
469 #ifdef _MODULE
470 		error = devsw_attach("autofs", NULL, &bmajor, &autofs_cdevsw,
471 		    &cmajor);
472 		if (error) {
473 			vfs_detach(&autofs_vfsops);
474 			break;
475 		}
476 #endif
477 		break;
478 	case MODULE_CMD_FINI:
479 #ifdef _MODULE
480 		KASSERT(autofs_softc);
481 		mutex_enter(&autofs_softc->sc_lock);
482 		if (autofs_softc->sc_dev_opened) {
483 			mutex_exit(&autofs_softc->sc_lock);
484 			error = EBUSY;
485 			break;
486 		}
487 		mutex_exit(&autofs_softc->sc_lock);
488 
489 		error = devsw_detach(NULL, &autofs_cdevsw);
490 		if (error)
491 			break;
492 #endif
493 		error = vfs_detach(&autofs_vfsops);
494 		if (error)
495 			break;
496 		break;
497 	default:
498 		error = ENOTTY;
499 		break;
500 	}
501 
502 	return error;
503 }
504