/* $NetBSD: mfs_vfsops.c,v 1.117 2025/02/16 18:38:59 joe Exp $ */
/*
* Copyright (c) 1989, 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)mfs_vfsops.c 8.11 (Berkeley) 6/19/95
*/
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
CTLTYPE_NODE, "mfs",
SYSCTL_DESCR("Memory based file system"),
NULL, 1, NULL, 0,
CTL_VFS, 3, CTL_EOL);
/*
* XXX the "1" and the "3" above could be dynamic, thereby
* eliminating one more instance of the "number to vfs"
* mapping problem, but they are in order as taken from
* sys/mount.h
*/
}
static int
mfs_modcmd(modcmd_t cmd, void *arg)
{
int error;
if (args == NULL)
return EINVAL;
if (*data_len < sizeof *args)
return EINVAL;
p = l->l_proc;
if (mp->mnt_flag & MNT_GETARGS) {
struct vnode *vp;
ump = VFSTOUFS(mp);
if (ump == NULL)
return EIO;
vp = ump->um_devvp;
if (vp == NULL)
return EIO;
mfsp = VTOMFS(vp);
if (mfsp == NULL)
return EIO;
args->fspec = NULL;
args->base = mfsp->mfs_baseoff;
args->size = mfsp->mfs_size;
*data_len = sizeof *args;
return 0;
}
/*
* XXX turn off async to avoid hangs when writing lots of data.
* the problem is that MFS needs to allocate pages to clean pages,
* so if we wait until the last minute to clean pages then there
* may not be any pages available to do the cleaning.
* ... and since the default partially-synchronous mode turns out
* to not be sufficient under heavy load, make it full synchronous.
*/
mp->mnt_flag &= ~MNT_ASYNC;
mp->mnt_flag |= MNT_SYNCHRONOUS;
/*
* If updating, check whether changing from read-only to
* read/write; if there is no device name, that's all we do.
*/
if (mp->mnt_flag & MNT_UPDATE) {
ump = VFSTOUFS(mp);
fs = ump->um_fs;
if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
flags = WRITECLOSE;
if (mp->mnt_flag & MNT_FORCE)
flags |= FORCECLOSE;
error = ffs_flushfiles(mp, flags, l);
if (error)
return (error);
}
if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
fs->fs_ronly = 0;
if (args->fspec == NULL)
return EINVAL;
return (0);
}
mutex_enter(&mfs_lock);
minor = mfs_minor++;
mutex_exit(&mfs_lock);
error = bdevvp(makedev(255, minor), &devvp);
if (error)
return (error);
mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
/*
* Changing v_op and v_data here is safe as we are
* the exclusive owner of this device node.
*/
KASSERT(devvp->v_op == spec_vnodeop_p);
KASSERT(devvp->v_data == NULL);
devvp->v_op = mfs_vnodeop_p;
devvp->v_data = mfsp;
mfsp->mfs_baseoff = args->base;
mfsp->mfs_size = args->size;
mfsp->mfs_vnode = devvp;
mfsp->mfs_proc = p;
mfsp->mfs_shutdown = 0;
cv_init(&mfsp->mfs_cv, "mfsidl");
mfsp->mfs_refcnt = 1;
bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
mfsp->mfs_shutdown = 1;
vrele(devvp);
return (error);
}
ump = VFSTOUFS(mp);
fs = ump->um_fs;
error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
if (error)
return error;
(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
sizeof(fs->fs_fsmnt));
fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
/* XXX: cleanup on error */
return 0;
}
/*
* Used to grab the process and keep it in the kernel to service
* memory filesystem I/O requests.
*
* Loop servicing I/O requests.
* Copy the requested data into or out of the memory filesystem
* address space.
*/
/* ARGSUSED */
int
mfs_start(struct mount *mp, int flags)
{
struct vnode *vp;
struct mfsnode *mfsp;
struct proc *p;
struct buf *bp;
void *base;
int sleepreturn = 0, refcnt, error;
ksiginfoq_t kq;
/*
* Ensure that file system is still mounted when getting mfsnode.
* Add a reference to the mfsnode to prevent it disappearing in
* this routine.
*/
if ((error = vfs_busy(mp)) != 0)
return error;
vp = VFSTOUFS(mp)->um_devvp;
mfsp = VTOMFS(vp);
mutex_enter(&mfs_lock);
mfsp->mfs_refcnt++;
mutex_exit(&mfs_lock);
vfs_unbusy(mp);
base = mfsp->mfs_baseoff;
mutex_enter(&mfs_lock);
while (mfsp->mfs_shutdown != 1) {
while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
mutex_exit(&mfs_lock);
mfs_doio(bp, base);
mutex_enter(&mfs_lock);
}
/*
* If a non-ignored signal is received, try to unmount.
* If that fails, or the filesystem is already in the
* process of being unmounted, clear the signal (it has been
* "processed"), otherwise we will loop here, as tsleep
* will always return EINTR/ERESTART.
*/
if (sleepreturn != 0) {
mutex_exit(&mfs_lock);
if (dounmount(mp, 0, curlwp) != 0) {
p = curproc;
ksiginfo_queue_init(&kq);
mutex_enter(p->p_lock);
sigclearall(p, NULL, &kq);
mutex_exit(p->p_lock);
ksiginfo_queue_drain(&kq);
}
sleepreturn = 0;
mutex_enter(&mfs_lock);
continue;
}