Home | History | Annotate | Line # | Download | only in autofs
autofs_vfsops.c revision 1.1
      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 (at) 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.1 2018/01/09 03:31:14 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 
     50 static void
     51 autofs_init(void)
     52 {
     53 
     54 	KASSERT(!autofs_softc);
     55 
     56 	autofs_softc = kmem_zalloc(sizeof(*autofs_softc), KM_SLEEP);
     57 
     58 	pool_init(&autofs_request_pool, sizeof(struct autofs_request), 0, 0, 0,
     59 	    "autofs_request", &pool_allocator_nointr, IPL_NONE);
     60 	pool_init(&autofs_node_pool, sizeof(struct autofs_node), 0, 0, 0,
     61 	    "autofs_node", &pool_allocator_nointr, IPL_NONE);
     62 
     63 	TAILQ_INIT(&autofs_softc->sc_requests);
     64 	cv_init(&autofs_softc->sc_cv, "autofscv");
     65 	mutex_init(&autofs_softc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
     66 	autofs_softc->sc_dev_opened = false;
     67 }
     68 
     69 static void
     70 autofs_done(void)
     71 {
     72 	KASSERT(autofs_softc);
     73 	KASSERT(!autofs_softc->sc_dev_opened);
     74 
     75 	struct autofs_softc *sc = autofs_softc;
     76 	autofs_softc = NULL;
     77 
     78 	cv_destroy(&sc->sc_cv);
     79 	mutex_destroy(&sc->sc_lock);
     80 
     81 	pool_destroy(&autofs_request_pool);
     82 	pool_destroy(&autofs_node_pool);
     83 
     84 	kmem_free(sc, sizeof(*sc));
     85 }
     86 
     87 static int
     88 autofs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     89 {
     90 	struct autofs_args *args = data;
     91 	struct autofs_mount *amp;
     92 	struct statvfs *sbp = &mp->mnt_stat;
     93 	int error;
     94 
     95 	if (!args)
     96 		return EINVAL;
     97 
     98 	/*
     99 	 * MNT_GETARGS is unsupported.  Autofs is mounted via automount(8) by
    100 	 * parsing /etc/auto_master instead of regular mount(8) variants with
    101 	 * -o getargs support, thus not really needed either.
    102 	 */
    103 	if (mp->mnt_flag & MNT_GETARGS)
    104 		return EOPNOTSUPP;
    105 
    106 	if (mp->mnt_flag & MNT_UPDATE) {
    107 		autofs_flush(VFSTOAUTOFS(mp));
    108 		return 0;
    109 	}
    110 
    111 	/*
    112 	 * Allocate the autofs mount.
    113 	 */
    114 	amp = kmem_zalloc(sizeof(*amp), KM_SLEEP);
    115 	mp->mnt_data = amp;
    116 	amp->am_mp = mp;
    117 
    118 	/*
    119 	 * Copy-in master_options string.
    120 	 */
    121 	error = copyinstr(args->master_options, amp->am_options,
    122 	    sizeof(amp->am_options), NULL);
    123 	if (error)
    124 		goto fail;
    125 
    126 	/*
    127 	 * Copy-in master_prefix string.
    128 	 */
    129 	error = copyinstr(args->master_prefix, amp->am_prefix,
    130 	    sizeof(amp->am_prefix), NULL);
    131 	if (error)
    132 		goto fail;
    133 
    134 	/*
    135 	 * Initialize the autofs mount.
    136 	 */
    137 	mutex_init(&amp->am_lock, MUTEX_DEFAULT, IPL_NONE);
    138 	amp->am_last_ino = AUTOFS_ROOTINO;
    139 
    140 	mutex_enter(&amp->am_lock);
    141 	error = autofs_node_new(NULL, amp, ".", -1, &amp->am_root);
    142 	if (error) {
    143 		mutex_exit(&amp->am_lock);
    144 		goto fail;
    145 	}
    146 	mutex_exit(&amp->am_lock);
    147 	KASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
    148 
    149 	autofs_statvfs(mp, sbp);
    150 	vfs_getnewfsid(mp);
    151 
    152 	error = set_statvfs_info(path, UIO_USERSPACE, args->from, UIO_USERSPACE,
    153 	    mp->mnt_op->vfs_name, mp, curlwp);
    154 	if (error)
    155 		goto fail;
    156 	strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
    157 	strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
    158 
    159 	return 0;
    160 
    161 fail:
    162 	kmem_free(amp, sizeof(*amp));
    163 	return error;
    164 }
    165 
    166 static int
    167 autofs_unmount(struct mount *mp, int mntflags)
    168 {
    169 	struct autofs_mount *amp = VFSTOAUTOFS(mp);
    170 	int error, flags;
    171 
    172 	flags = 0;
    173 	if (mntflags & MNT_FORCE)
    174 		flags |= FORCECLOSE;
    175 	error = vflush(mp, NULL, flags);
    176 	if (error) {
    177 		AUTOFS_WARN("vflush failed with error %d", error);
    178 		return error;
    179 	}
    180 
    181 	/*
    182 	 * All vnodes are gone, and new one will not appear - so,
    183 	 * no new triggerings.
    184 	 */
    185 	for (;;) {
    186 		struct autofs_request *ar;
    187 		int dummy;
    188 		bool found;
    189 
    190 		found = false;
    191 		mutex_enter(&autofs_softc->sc_lock);
    192 		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
    193 			if (ar->ar_mount != amp)
    194 				continue;
    195 			ar->ar_error = ENXIO;
    196 			ar->ar_done = true;
    197 			ar->ar_in_progress = false;
    198 			found = true;
    199 		}
    200 		if (found == false) {
    201 			mutex_exit(&autofs_softc->sc_lock);
    202 			break;
    203 		}
    204 
    205 		cv_broadcast(&autofs_softc->sc_cv);
    206 		mutex_exit(&autofs_softc->sc_lock);
    207 
    208 		tsleep(&dummy, 0, "autofs_umount", hz);
    209 	}
    210 
    211 	mutex_enter(&amp->am_lock);
    212 	while (!RB_EMPTY(&amp->am_root->an_children)) {
    213 		struct autofs_node *anp;
    214 		anp = RB_MIN(autofs_node_tree, &amp->am_root->an_children);
    215 		autofs_node_delete(anp);
    216 	}
    217 	autofs_node_delete(amp->am_root);
    218 	mp->mnt_data = NULL;
    219 	mutex_exit(&amp->am_lock);
    220 
    221 	mutex_destroy(&amp->am_lock);
    222 
    223 	kmem_free(amp, sizeof(*amp));
    224 
    225 	return 0;
    226 }
    227 
    228 static int
    229 autofs_start(struct mount *mp, int flags)
    230 {
    231 
    232 	return 0;
    233 }
    234 
    235 static int
    236 autofs_root(struct mount *mp, struct vnode **vpp)
    237 {
    238 	struct autofs_node *anp = VFSTOAUTOFS(mp)->am_root;
    239 	int error;
    240 
    241 	error = vcache_get(mp, &anp, sizeof(anp), vpp);
    242 	if (error)
    243 		return error;
    244 	error = vn_lock(*vpp, LK_EXCLUSIVE);
    245 	if (error) {
    246 		vrele(*vpp);
    247 		*vpp = NULL;
    248 		return error;
    249 	}
    250 
    251 	return 0;
    252 }
    253 
    254 static int
    255 autofs_statvfs(struct mount *mp, struct statvfs *sbp)
    256 {
    257 
    258 	sbp->f_bsize = S_BLKSIZE;
    259 	sbp->f_frsize = S_BLKSIZE;
    260 	sbp->f_iosize = 0;
    261 	sbp->f_blocks = 0;
    262 	sbp->f_bfree = 0;
    263 	sbp->f_bavail = 0;
    264 	sbp->f_bresvd = 0;
    265 	sbp->f_files = 0;
    266 	sbp->f_ffree = 0;
    267 	sbp->f_favail = 0;
    268 	sbp->f_fresvd = 0;
    269 
    270 	copy_statvfs_info(sbp, mp);
    271 
    272 	return 0;
    273 }
    274 
    275 static int
    276 autofs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
    277 {
    278 
    279 	return 0;
    280 }
    281 
    282 static int
    283 autofs_loadvnode(struct mount *mp, struct vnode *vp,
    284     const void *key, size_t key_len, const void **new_key)
    285 {
    286 	struct autofs_node *anp;
    287 
    288 	KASSERT(key_len == sizeof(anp));
    289 	memcpy(&anp, key, key_len);
    290 	KASSERT(!anp->an_vnode);
    291 
    292 	vp->v_tag = VT_AUTOFS;
    293 	vp->v_type = VDIR;
    294 	vp->v_op = autofs_vnodeop_p;
    295 	vp->v_data = anp;
    296 
    297 	if (anp->an_ino == AUTOFS_ROOTINO)
    298 		vp->v_vflag |= VV_ROOT;
    299 
    300 	anp->an_vnode = vp;
    301 	uvm_vnp_setsize(vp, 0);
    302 
    303 	*new_key = &vp->v_data;
    304 
    305 	return 0;
    306 }
    307 
    308 static const struct vnodeopv_desc * const autofs_vnodeopv_descs[] = {
    309 	&autofs_vnodeop_opv_desc,
    310 	NULL,
    311 };
    312 
    313 static struct vfsops autofs_vfsops = {
    314 	.vfs_name = MOUNT_AUTOFS,
    315 	.vfs_min_mount_data = sizeof(struct autofs_args),
    316 	.vfs_mount = autofs_mount,
    317 	.vfs_start = autofs_start,
    318 	.vfs_unmount = autofs_unmount,
    319 	.vfs_root = autofs_root,
    320 	.vfs_quotactl = (void *)eopnotsupp,
    321 	.vfs_statvfs = autofs_statvfs,
    322 	.vfs_sync = autofs_sync,
    323 	.vfs_vget = (void *)eopnotsupp,
    324 	.vfs_loadvnode = (void *)autofs_loadvnode,
    325 	.vfs_newvnode = (void *)eopnotsupp,
    326 	.vfs_fhtovp = (void *)eopnotsupp,
    327 	.vfs_vptofh = (void *)eopnotsupp,
    328 	.vfs_init = autofs_init,
    329 	.vfs_reinit = (void *)eopnotsupp,
    330 	.vfs_done = autofs_done,
    331 	.vfs_mountroot = (void *)eopnotsupp,
    332 	.vfs_snapshot = (void *)eopnotsupp,
    333 	.vfs_extattrctl = (void *)eopnotsupp,
    334 	.vfs_suspendctl = (void *)genfs_suspendctl,
    335 	.vfs_renamelock_enter = (void *)eopnotsupp,
    336 	.vfs_renamelock_exit = (void *)eopnotsupp,
    337 	.vfs_fsync = (void *)eopnotsupp,
    338 	.vfs_opv_descs = autofs_vnodeopv_descs
    339 };
    340 
    341 #define AUTOFS_SYSCTL_DEBUG		1
    342 #define AUTOFS_SYSCTL_MOUNT_ON_STAT	2
    343 #define AUTOFS_SYSCTL_TIMEOUT		3
    344 #define AUTOFS_SYSCTL_CACHE		4
    345 #define AUTOFS_SYSCTL_RETRY_ATTEMPTS	5
    346 #define AUTOFS_SYSCTL_RETRY_DELAY	6
    347 #define AUTOFS_SYSCTL_INTERRUPTIBLE	7
    348 
    349 static struct sysctllog *autofs_sysctl_log;
    350 
    351 static int
    352 autofs_sysctl_create(void)
    353 {
    354 	int error;
    355 
    356 	/*
    357 	 * XXX the "33" below could be dynamic, thereby eliminating one
    358 	 * more instance of the "number to vfs" mapping problem, but
    359 	 * "33" is the order as taken from sys/mount.h
    360 	 */
    361 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    362 	    CTLFLAG_PERMANENT,
    363 	    CTLTYPE_NODE, "autofs",
    364 	    SYSCTL_DESCR("Automounter filesystem"),
    365 	    NULL, 0, NULL, 0,
    366 	    CTL_VFS, 33, CTL_EOL);
    367 	if (error)
    368 		goto fail;
    369 
    370 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    371 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    372 	    CTLTYPE_INT, "autofs_debug",
    373 	    SYSCTL_DESCR("Enable debug messages"),
    374 	    NULL, 0, &autofs_debug, 0,
    375 	    CTL_VFS, 33, AUTOFS_SYSCTL_DEBUG, CTL_EOL);
    376 	if (error)
    377 		goto fail;
    378 
    379 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    380 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    381 	    CTLTYPE_INT, "autofs_mount_on_stat",
    382 	    SYSCTL_DESCR("Trigger mount on stat(2) on mountpoint"),
    383 	    NULL, 0, &autofs_mount_on_stat, 0,
    384 	    CTL_VFS, 33, AUTOFS_SYSCTL_MOUNT_ON_STAT, CTL_EOL);
    385 	if (error)
    386 		goto fail;
    387 
    388 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    389 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    390 	    CTLTYPE_INT, "autofs_timeout",
    391 	    SYSCTL_DESCR("Number of seconds to wait for automountd(8)"),
    392 	    NULL, 0, &autofs_timeout, 0,
    393 	    CTL_VFS, 33, AUTOFS_SYSCTL_TIMEOUT, CTL_EOL);
    394 	if (error)
    395 		goto fail;
    396 
    397 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    398 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    399 	    CTLTYPE_INT, "autofs_cache",
    400 	    SYSCTL_DESCR("Number of seconds to wait before reinvoking"),
    401 	    NULL, 0, &autofs_cache, 0,
    402 	    CTL_VFS, 33, AUTOFS_SYSCTL_CACHE, CTL_EOL);
    403 	if (error)
    404 		goto fail;
    405 
    406 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    407 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    408 	    CTLTYPE_INT, "autofs_retry_attempts",
    409 	    SYSCTL_DESCR("Number of attempts before failing mount"),
    410 	    NULL, 0, &autofs_retry_attempts, 0,
    411 	    CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_ATTEMPTS, CTL_EOL);
    412 	if (error)
    413 		goto fail;
    414 
    415 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    416 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    417 	    CTLTYPE_INT, "autofs_retry_delay",
    418 	    SYSCTL_DESCR("Number of seconds before retrying"),
    419 	    NULL, 0, &autofs_retry_delay, 0,
    420 	    CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_DELAY, CTL_EOL);
    421 	if (error)
    422 		goto fail;
    423 
    424 	error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
    425 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    426 	    CTLTYPE_INT, "autofs_interruptible",
    427 	    SYSCTL_DESCR("Allow requests to be interrupted by signal"),
    428 	    NULL, 0, &autofs_interruptible, 0,
    429 	    CTL_VFS, 33, AUTOFS_SYSCTL_INTERRUPTIBLE, CTL_EOL);
    430 	if (error)
    431 		goto fail;
    432 
    433 	return 0;
    434 fail:
    435 	AUTOFS_WARN("sysctl_createv failed with error %d", error);
    436 
    437 	return error;
    438 }
    439 
    440 static int
    441 autofs_modcmd(modcmd_t cmd, void *arg)
    442 {
    443 #ifdef _MODULE
    444 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
    445 #endif
    446 	int error = 0;
    447 
    448 	switch (cmd) {
    449 	case MODULE_CMD_INIT:
    450 		error = vfs_attach(&autofs_vfsops);
    451 		if (error)
    452 			break;
    453 #ifdef _MODULE
    454 		error = devsw_attach("autofs", NULL, &bmajor, &autofs_ops,
    455 		    &cmajor);
    456 		if (error) {
    457 			vfs_detach(&autofs_vfsops);
    458 			break;
    459 		}
    460 #endif
    461 		error = workqueue_create(&autofs_tmo_wq, "autofstmo",
    462 		    autofs_timeout_wq, NULL, 0, 0, WQ_MPSAFE);
    463 		if (error) {
    464 			devsw_detach(NULL, &autofs_ops);
    465 			vfs_detach(&autofs_vfsops);
    466 			break;
    467 		}
    468 
    469 		error = autofs_sysctl_create();
    470 		if (error) {
    471 			workqueue_destroy(autofs_tmo_wq);
    472 			devsw_detach(NULL, &autofs_ops);
    473 			vfs_detach(&autofs_vfsops);
    474 			break;
    475 		}
    476 		break;
    477 	case MODULE_CMD_FINI:
    478 		KASSERT(autofs_softc);
    479 		mutex_enter(&autofs_softc->sc_lock);
    480 		if (autofs_softc->sc_dev_opened) {
    481 			mutex_exit(&autofs_softc->sc_lock);
    482 			error = EBUSY;
    483 			break;
    484 		}
    485 		mutex_exit(&autofs_softc->sc_lock);
    486 
    487 		workqueue_destroy(autofs_tmo_wq);
    488 #ifdef _MODULE
    489 		error = devsw_detach(NULL, &autofs_ops);
    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