Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.115
      1 /*	$NetBSD: mfs_vfsops.c,v 1.115 2022/03/19 13:48:42 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)mfs_vfsops.c	8.11 (Berkeley) 6/19/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: mfs_vfsops.c,v 1.115 2022/03/19 13:48:42 hannken Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_compat_netbsd.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/sysctl.h>
     44 #include <sys/time.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/buf.h>
     48 #include <sys/bufq.h>
     49 #include <sys/mount.h>
     50 #include <sys/signalvar.h>
     51 #include <sys/vnode.h>
     52 #include <sys/kmem.h>
     53 #include <sys/module.h>
     54 
     55 #include <miscfs/genfs/genfs.h>
     56 #include <miscfs/specfs/specdev.h>
     57 
     58 #include <ufs/ufs/quota.h>
     59 #include <ufs/ufs/inode.h>
     60 #include <ufs/ufs/ufsmount.h>
     61 #include <ufs/ufs/ufs_extern.h>
     62 
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 
     66 #include <ufs/mfs/mfsnode.h>
     67 #include <ufs/mfs/mfs_extern.h>
     68 
     69 MODULE(MODULE_CLASS_VFS, mfs, "ffs");
     70 
     71 kmutex_t mfs_lock;	/* global lock */
     72 
     73 /* used for building internal dev_t, minor == 0 reserved for miniroot */
     74 static devminor_t mfs_minor = 1;
     75 static int mfs_initcnt;
     76 
     77 extern int (**mfs_vnodeop_p)(void *);
     78 
     79 /*
     80  * mfs vfs operations.
     81  */
     82 
     83 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
     84 
     85 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
     86 	&mfs_vnodeop_opv_desc,
     87 	NULL,
     88 };
     89 
     90 struct vfsops mfs_vfsops = {
     91 	.vfs_name = MOUNT_MFS,
     92 	.vfs_min_mount_data = sizeof (struct mfs_args),
     93 	.vfs_mount = mfs_mount,
     94 	.vfs_start = mfs_start,
     95 	.vfs_unmount = ffs_unmount,
     96 	.vfs_root = ufs_root,
     97 	.vfs_quotactl = ufs_quotactl,
     98 	.vfs_statvfs = mfs_statvfs,
     99 	.vfs_sync = ffs_sync,
    100 	.vfs_vget = ufs_vget,
    101 	.vfs_loadvnode = ffs_loadvnode,
    102 	.vfs_newvnode = ffs_newvnode,
    103 	.vfs_fhtovp = ffs_fhtovp,
    104 	.vfs_vptofh = ffs_vptofh,
    105 	.vfs_init = mfs_init,
    106 	.vfs_reinit = mfs_reinit,
    107 	.vfs_done = mfs_done,
    108 	.vfs_snapshot = (void *)eopnotsupp,
    109 	.vfs_extattrctl = vfs_stdextattrctl,
    110 	.vfs_suspendctl = genfs_suspendctl,
    111 	.vfs_renamelock_enter = genfs_renamelock_enter,
    112 	.vfs_renamelock_exit = genfs_renamelock_exit,
    113 	.vfs_fsync = (void *)eopnotsupp,
    114 	.vfs_opv_descs = mfs_vnodeopv_descs
    115 };
    116 
    117 SYSCTL_SETUP(mfs_sysctl_setup, "mfs sysctl")
    118 {
    119 
    120 	sysctl_createv(clog, 0, NULL, NULL,
    121 		       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
    122 		       CTLTYPE_NODE, "mfs",
    123 		       SYSCTL_DESCR("Memory based file system"),
    124 		       NULL, 1, NULL, 0,
    125 		       CTL_VFS, 3, CTL_EOL);
    126 	/*
    127 	 * XXX the "1" and the "3" above could be dynamic, thereby
    128 	 * eliminating one more instance of the "number to vfs"
    129 	 * mapping problem, but they are in order as taken from
    130 	 * sys/mount.h
    131 	 */
    132 }
    133 
    134 static int
    135 mfs_modcmd(modcmd_t cmd, void *arg)
    136 {
    137 	int error;
    138 
    139 	switch (cmd) {
    140 	case MODULE_CMD_INIT:
    141 		error = vfs_attach(&mfs_vfsops);
    142 		if (error != 0)
    143 			break;
    144 		break;
    145 	case MODULE_CMD_FINI:
    146 		error = vfs_detach(&mfs_vfsops);
    147 		if (error != 0)
    148 			break;
    149 		break;
    150 	default:
    151 		error = ENOTTY;
    152 		break;
    153 	}
    154 
    155 	return (error);
    156 }
    157 
    158 /*
    159  * Memory based filesystem initialization.
    160  */
    161 void
    162 mfs_init(void)
    163 {
    164 
    165 	if (mfs_initcnt++ == 0) {
    166 		mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE);
    167 		ffs_init();
    168 	}
    169 }
    170 
    171 void
    172 mfs_reinit(void)
    173 {
    174 
    175 	ffs_reinit();
    176 }
    177 
    178 void
    179 mfs_done(void)
    180 {
    181 
    182 	if (--mfs_initcnt == 0) {
    183 		ffs_done();
    184 		mutex_destroy(&mfs_lock);
    185 	}
    186 }
    187 
    188 /*
    189  * Called by main() when mfs is going to be mounted as root.
    190  */
    191 
    192 int
    193 mfs_mountroot(void)
    194 {
    195 	struct fs *fs;
    196 	struct mount *mp;
    197 	struct lwp *l = curlwp;		/* XXX */
    198 	struct ufsmount *ump;
    199 	struct mfsnode *mfsp;
    200 	int error = 0;
    201 
    202 	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
    203 		vrele(rootvp);
    204 		return (error);
    205 	}
    206 
    207 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    208 	rootvp->v_data = mfsp;
    209 	rootvp->v_op = mfs_vnodeop_p;
    210 	rootvp->v_tag = VT_MFS;
    211 	rootvp->v_vflag |= VV_LOCKSWORK;
    212 	mfsp->mfs_baseoff = mfs_rootbase;
    213 	mfsp->mfs_size = mfs_rootsize;
    214 	mfsp->mfs_vnode = rootvp;
    215 	mfsp->mfs_proc = NULL;		/* indicate kernel space */
    216 	mfsp->mfs_shutdown = 0;
    217 	cv_init(&mfsp->mfs_cv, "mfs");
    218 	mfsp->mfs_refcnt = 1;
    219 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    220 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    221 		vfs_unbusy(mp);
    222 		bufq_free(mfsp->mfs_buflist);
    223 		vfs_rele(mp);
    224 		kmem_free(mfsp, sizeof(*mfsp));
    225 		return (error);
    226 	}
    227 	mountlist_append(mp);
    228 	mp->mnt_vnodecovered = NULLVP;
    229 	ump = VFSTOUFS(mp);
    230 	fs = ump->um_fs;
    231 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    232 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    233 	vfs_unbusy(mp);
    234 	return (0);
    235 }
    236 
    237 /*
    238  * VFS Operations.
    239  *
    240  * mount system call
    241  */
    242 /* ARGSUSED */
    243 int
    244 mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    245 {
    246 	struct lwp *l = curlwp;
    247 	struct vnode *devvp;
    248 	struct mfs_args *args = data;
    249 	struct ufsmount *ump;
    250 	struct fs *fs;
    251 	struct mfsnode *mfsp;
    252 	struct proc *p;
    253 	devminor_t minor;
    254 	int flags, error = 0;
    255 
    256 	if (args == NULL)
    257 		return EINVAL;
    258 	if (*data_len < sizeof *args)
    259 		return EINVAL;
    260 
    261 	p = l->l_proc;
    262 	if (mp->mnt_flag & MNT_GETARGS) {
    263 		struct vnode *vp;
    264 
    265 		ump = VFSTOUFS(mp);
    266 		if (ump == NULL)
    267 			return EIO;
    268 
    269 		vp = ump->um_devvp;
    270 		if (vp == NULL)
    271 			return EIO;
    272 
    273 		mfsp = VTOMFS(vp);
    274 		if (mfsp == NULL)
    275 			return EIO;
    276 
    277 		args->fspec = NULL;
    278 		args->base = mfsp->mfs_baseoff;
    279 		args->size = mfsp->mfs_size;
    280 		*data_len = sizeof *args;
    281 		return 0;
    282 	}
    283 	/*
    284 	 * XXX turn off async to avoid hangs when writing lots of data.
    285 	 * the problem is that MFS needs to allocate pages to clean pages,
    286 	 * so if we wait until the last minute to clean pages then there
    287 	 * may not be any pages available to do the cleaning.
    288 	 * ... and since the default partially-synchronous mode turns out
    289 	 * to not be sufficient under heavy load, make it full synchronous.
    290 	 */
    291 	mp->mnt_flag &= ~MNT_ASYNC;
    292 	mp->mnt_flag |= MNT_SYNCHRONOUS;
    293 
    294 	/*
    295 	 * If updating, check whether changing from read-only to
    296 	 * read/write; if there is no device name, that's all we do.
    297 	 */
    298 	if (mp->mnt_flag & MNT_UPDATE) {
    299 		ump = VFSTOUFS(mp);
    300 		fs = ump->um_fs;
    301 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    302 			flags = WRITECLOSE;
    303 			if (mp->mnt_flag & MNT_FORCE)
    304 				flags |= FORCECLOSE;
    305 			error = ffs_flushfiles(mp, flags, l);
    306 			if (error)
    307 				return (error);
    308 		}
    309 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
    310 			fs->fs_ronly = 0;
    311 		if (args->fspec == NULL)
    312 			return EINVAL;
    313 		return (0);
    314 	}
    315 	mutex_enter(&mfs_lock);
    316 	minor = mfs_minor++;
    317 	mutex_exit(&mfs_lock);
    318 	error = bdevvp(makedev(255, minor), &devvp);
    319 	if (error)
    320 		return (error);
    321 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    322 	/*
    323 	 * Changing v_op and v_data here is safe as we are
    324 	 * the exclusive owner of this device node.
    325 	 */
    326 	KASSERT(devvp->v_op == spec_vnodeop_p);
    327 	KASSERT(devvp->v_data == NULL);
    328 	devvp->v_op = mfs_vnodeop_p;
    329 	devvp->v_data = mfsp;
    330 	devvp->v_vflag |= VV_LOCKSWORK;
    331 	mfsp->mfs_baseoff = args->base;
    332 	mfsp->mfs_size = args->size;
    333 	mfsp->mfs_vnode = devvp;
    334 	mfsp->mfs_proc = p;
    335 	mfsp->mfs_shutdown = 0;
    336 	cv_init(&mfsp->mfs_cv, "mfsidl");
    337 	mfsp->mfs_refcnt = 1;
    338 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    339 	if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
    340 		mfsp->mfs_shutdown = 1;
    341 		vrele(devvp);
    342 		return (error);
    343 	}
    344 	ump = VFSTOUFS(mp);
    345 	fs = ump->um_fs;
    346 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    347 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    348 	if (error)
    349 		return error;
    350 	(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    351 		sizeof(fs->fs_fsmnt));
    352 	fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
    353 	/* XXX: cleanup on error */
    354 	return 0;
    355 }
    356 
    357 /*
    358  * Used to grab the process and keep it in the kernel to service
    359  * memory filesystem I/O requests.
    360  *
    361  * Loop servicing I/O requests.
    362  * Copy the requested data into or out of the memory filesystem
    363  * address space.
    364  */
    365 /* ARGSUSED */
    366 int
    367 mfs_start(struct mount *mp, int flags)
    368 {
    369 	struct vnode *vp;
    370 	struct mfsnode *mfsp;
    371 	struct proc *p;
    372 	struct buf *bp;
    373 	void *base;
    374 	int sleepreturn = 0, refcnt, error;
    375 	ksiginfoq_t kq;
    376 
    377 	/*
    378 	 * Ensure that file system is still mounted when getting mfsnode.
    379 	 * Add a reference to the mfsnode to prevent it disappearing in
    380 	 * this routine.
    381 	 */
    382 	if ((error = vfs_busy(mp)) != 0)
    383 		return error;
    384 	vp = VFSTOUFS(mp)->um_devvp;
    385 	mfsp = VTOMFS(vp);
    386 	mutex_enter(&mfs_lock);
    387 	mfsp->mfs_refcnt++;
    388 	mutex_exit(&mfs_lock);
    389 	vfs_unbusy(mp);
    390 
    391 	base = mfsp->mfs_baseoff;
    392 	mutex_enter(&mfs_lock);
    393 	while (mfsp->mfs_shutdown != 1) {
    394 		while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
    395 			mutex_exit(&mfs_lock);
    396 			mfs_doio(bp, base);
    397 			mutex_enter(&mfs_lock);
    398 		}
    399 		/*
    400 		 * If a non-ignored signal is received, try to unmount.
    401 		 * If that fails, or the filesystem is already in the
    402 		 * process of being unmounted, clear the signal (it has been
    403 		 * "processed"), otherwise we will loop here, as tsleep
    404 		 * will always return EINTR/ERESTART.
    405 		 */
    406 		if (sleepreturn != 0) {
    407 			mutex_exit(&mfs_lock);
    408 			if (dounmount(mp, 0, curlwp) != 0) {
    409 				p = curproc;
    410 				ksiginfo_queue_init(&kq);
    411 				mutex_enter(p->p_lock);
    412 				sigclearall(p, NULL, &kq);
    413 				mutex_exit(p->p_lock);
    414 				ksiginfo_queue_drain(&kq);
    415 			}
    416 			sleepreturn = 0;
    417 			mutex_enter(&mfs_lock);
    418 			continue;
    419 		}
    420 
    421 		sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock);
    422 	}
    423 	KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL);
    424 	refcnt = --mfsp->mfs_refcnt;
    425 	mutex_exit(&mfs_lock);
    426 	if (refcnt == 0) {
    427 		bufq_free(mfsp->mfs_buflist);
    428 		cv_destroy(&mfsp->mfs_cv);
    429 		kmem_free(mfsp, sizeof(*mfsp));
    430 	}
    431 	return (sleepreturn);
    432 }
    433 
    434 /*
    435  * Get file system statistics.
    436  */
    437 int
    438 mfs_statvfs(struct mount *mp, struct statvfs *sbp)
    439 {
    440 	int error;
    441 
    442 	error = ffs_statvfs(mp, sbp);
    443 	if (error)
    444 		return error;
    445 	(void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
    446 	    sizeof(sbp->f_fstypename));
    447 	sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
    448 	return 0;
    449 }
    450