Home | History | Annotate | Line # | Download | only in mfs
      1 /*	$NetBSD: mfs_vfsops.c,v 1.117 2025/02/16 18:38:59 joe 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.117 2025/02/16 18:38:59 joe 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 		break;
    143 	case MODULE_CMD_FINI:
    144 		error = vfs_detach(&mfs_vfsops);
    145 		break;
    146 	default:
    147 		error = ENOTTY;
    148 		break;
    149 	}
    150 
    151 	return (error);
    152 }
    153 
    154 /*
    155  * Memory based filesystem initialization.
    156  */
    157 void
    158 mfs_init(void)
    159 {
    160 
    161 	if (mfs_initcnt++ == 0) {
    162 		mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE);
    163 		ffs_init();
    164 	}
    165 }
    166 
    167 void
    168 mfs_reinit(void)
    169 {
    170 
    171 	ffs_reinit();
    172 }
    173 
    174 void
    175 mfs_done(void)
    176 {
    177 
    178 	if (--mfs_initcnt == 0) {
    179 		ffs_done();
    180 		mutex_destroy(&mfs_lock);
    181 	}
    182 }
    183 
    184 /*
    185  * Called by main() when mfs is going to be mounted as root.
    186  */
    187 
    188 int
    189 mfs_mountroot(void)
    190 {
    191 	struct fs *fs;
    192 	struct mount *mp;
    193 	struct lwp *l = curlwp;		/* XXX */
    194 	struct ufsmount *ump;
    195 	struct mfsnode *mfsp;
    196 	int error = 0;
    197 
    198 	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
    199 		vrele(rootvp);
    200 		return (error);
    201 	}
    202 
    203 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    204 	rootvp->v_data = mfsp;
    205 	rootvp->v_op = mfs_vnodeop_p;
    206 	rootvp->v_tag = VT_MFS;
    207 	mfsp->mfs_baseoff = mfs_rootbase;
    208 	mfsp->mfs_size = mfs_rootsize;
    209 	mfsp->mfs_vnode = rootvp;
    210 	mfsp->mfs_proc = NULL;		/* indicate kernel space */
    211 	mfsp->mfs_shutdown = 0;
    212 	cv_init(&mfsp->mfs_cv, "mfs");
    213 	mfsp->mfs_refcnt = 1;
    214 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    215 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    216 		vfs_unbusy(mp);
    217 		bufq_free(mfsp->mfs_buflist);
    218 		vfs_rele(mp);
    219 		kmem_free(mfsp, sizeof(*mfsp));
    220 		return (error);
    221 	}
    222 	mountlist_append(mp);
    223 	mp->mnt_vnodecovered = NULLVP;
    224 	ump = VFSTOUFS(mp);
    225 	fs = ump->um_fs;
    226 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    227 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    228 	vfs_unbusy(mp);
    229 	return (0);
    230 }
    231 
    232 /*
    233  * VFS Operations.
    234  *
    235  * mount system call
    236  */
    237 /* ARGSUSED */
    238 int
    239 mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    240 {
    241 	struct lwp *l = curlwp;
    242 	struct vnode *devvp;
    243 	struct mfs_args *args = data;
    244 	struct ufsmount *ump;
    245 	struct fs *fs;
    246 	struct mfsnode *mfsp;
    247 	struct proc *p;
    248 	devminor_t minor;
    249 	int flags, error = 0;
    250 
    251 	if (args == NULL)
    252 		return EINVAL;
    253 	if (*data_len < sizeof *args)
    254 		return EINVAL;
    255 
    256 	p = l->l_proc;
    257 	if (mp->mnt_flag & MNT_GETARGS) {
    258 		struct vnode *vp;
    259 
    260 		ump = VFSTOUFS(mp);
    261 		if (ump == NULL)
    262 			return EIO;
    263 
    264 		vp = ump->um_devvp;
    265 		if (vp == NULL)
    266 			return EIO;
    267 
    268 		mfsp = VTOMFS(vp);
    269 		if (mfsp == NULL)
    270 			return EIO;
    271 
    272 		args->fspec = NULL;
    273 		args->base = mfsp->mfs_baseoff;
    274 		args->size = mfsp->mfs_size;
    275 		*data_len = sizeof *args;
    276 		return 0;
    277 	}
    278 	/*
    279 	 * XXX turn off async to avoid hangs when writing lots of data.
    280 	 * the problem is that MFS needs to allocate pages to clean pages,
    281 	 * so if we wait until the last minute to clean pages then there
    282 	 * may not be any pages available to do the cleaning.
    283 	 * ... and since the default partially-synchronous mode turns out
    284 	 * to not be sufficient under heavy load, make it full synchronous.
    285 	 */
    286 	mp->mnt_flag &= ~MNT_ASYNC;
    287 	mp->mnt_flag |= MNT_SYNCHRONOUS;
    288 
    289 	/*
    290 	 * If updating, check whether changing from read-only to
    291 	 * read/write; if there is no device name, that's all we do.
    292 	 */
    293 	if (mp->mnt_flag & MNT_UPDATE) {
    294 		ump = VFSTOUFS(mp);
    295 		fs = ump->um_fs;
    296 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    297 			flags = WRITECLOSE;
    298 			if (mp->mnt_flag & MNT_FORCE)
    299 				flags |= FORCECLOSE;
    300 			error = ffs_flushfiles(mp, flags, l);
    301 			if (error)
    302 				return (error);
    303 		}
    304 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
    305 			fs->fs_ronly = 0;
    306 		if (args->fspec == NULL)
    307 			return EINVAL;
    308 		return (0);
    309 	}
    310 	mutex_enter(&mfs_lock);
    311 	minor = mfs_minor++;
    312 	mutex_exit(&mfs_lock);
    313 	error = bdevvp(makedev(255, minor), &devvp);
    314 	if (error)
    315 		return (error);
    316 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    317 	/*
    318 	 * Changing v_op and v_data here is safe as we are
    319 	 * the exclusive owner of this device node.
    320 	 */
    321 	KASSERT(devvp->v_op == spec_vnodeop_p);
    322 	KASSERT(devvp->v_data == NULL);
    323 	devvp->v_op = mfs_vnodeop_p;
    324 	devvp->v_data = mfsp;
    325 	mfsp->mfs_baseoff = args->base;
    326 	mfsp->mfs_size = args->size;
    327 	mfsp->mfs_vnode = devvp;
    328 	mfsp->mfs_proc = p;
    329 	mfsp->mfs_shutdown = 0;
    330 	cv_init(&mfsp->mfs_cv, "mfsidl");
    331 	mfsp->mfs_refcnt = 1;
    332 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    333 	if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
    334 		mfsp->mfs_shutdown = 1;
    335 		vrele(devvp);
    336 		return (error);
    337 	}
    338 	ump = VFSTOUFS(mp);
    339 	fs = ump->um_fs;
    340 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    341 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    342 	if (error)
    343 		return error;
    344 	(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    345 		sizeof(fs->fs_fsmnt));
    346 	fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
    347 	/* XXX: cleanup on error */
    348 	return 0;
    349 }
    350 
    351 /*
    352  * Used to grab the process and keep it in the kernel to service
    353  * memory filesystem I/O requests.
    354  *
    355  * Loop servicing I/O requests.
    356  * Copy the requested data into or out of the memory filesystem
    357  * address space.
    358  */
    359 /* ARGSUSED */
    360 int
    361 mfs_start(struct mount *mp, int flags)
    362 {
    363 	struct vnode *vp;
    364 	struct mfsnode *mfsp;
    365 	struct proc *p;
    366 	struct buf *bp;
    367 	void *base;
    368 	int sleepreturn = 0, refcnt, error;
    369 	ksiginfoq_t kq;
    370 
    371 	/*
    372 	 * Ensure that file system is still mounted when getting mfsnode.
    373 	 * Add a reference to the mfsnode to prevent it disappearing in
    374 	 * this routine.
    375 	 */
    376 	if ((error = vfs_busy(mp)) != 0)
    377 		return error;
    378 	vp = VFSTOUFS(mp)->um_devvp;
    379 	mfsp = VTOMFS(vp);
    380 	mutex_enter(&mfs_lock);
    381 	mfsp->mfs_refcnt++;
    382 	mutex_exit(&mfs_lock);
    383 	vfs_unbusy(mp);
    384 
    385 	base = mfsp->mfs_baseoff;
    386 	mutex_enter(&mfs_lock);
    387 	while (mfsp->mfs_shutdown != 1) {
    388 		while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
    389 			mutex_exit(&mfs_lock);
    390 			mfs_doio(bp, base);
    391 			mutex_enter(&mfs_lock);
    392 		}
    393 		/*
    394 		 * If a non-ignored signal is received, try to unmount.
    395 		 * If that fails, or the filesystem is already in the
    396 		 * process of being unmounted, clear the signal (it has been
    397 		 * "processed"), otherwise we will loop here, as tsleep
    398 		 * will always return EINTR/ERESTART.
    399 		 */
    400 		if (sleepreturn != 0) {
    401 			mutex_exit(&mfs_lock);
    402 			if (dounmount(mp, 0, curlwp) != 0) {
    403 				p = curproc;
    404 				ksiginfo_queue_init(&kq);
    405 				mutex_enter(p->p_lock);
    406 				sigclearall(p, NULL, &kq);
    407 				mutex_exit(p->p_lock);
    408 				ksiginfo_queue_drain(&kq);
    409 			}
    410 			sleepreturn = 0;
    411 			mutex_enter(&mfs_lock);
    412 			continue;
    413 		}
    414 
    415 		sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock);
    416 	}
    417 	KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL);
    418 	refcnt = --mfsp->mfs_refcnt;
    419 	mutex_exit(&mfs_lock);
    420 	if (refcnt == 0) {
    421 		bufq_free(mfsp->mfs_buflist);
    422 		cv_destroy(&mfsp->mfs_cv);
    423 		kmem_free(mfsp, sizeof(*mfsp));
    424 	}
    425 	return (sleepreturn);
    426 }
    427 
    428 /*
    429  * Get file system statistics.
    430  */
    431 int
    432 mfs_statvfs(struct mount *mp, struct statvfs *sbp)
    433 {
    434 	int error;
    435 
    436 	error = ffs_statvfs(mp, sbp);
    437 	if (error)
    438 		return error;
    439 	(void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
    440 	    sizeof(sbp->f_fstypename));
    441 	sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
    442 	return 0;
    443 }
    444