Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.108
      1 /*	$NetBSD: mfs_vfsops.c,v 1.108 2014/05/08 08:21:53 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.108 2014/05/08 08:21:53 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 int mfs_minor = 1;
     75 static int mfs_initcnt;
     76 
     77 extern int (**mfs_vnodeop_p)(void *);
     78 
     79 static struct sysctllog *mfs_sysctl_log;
     80 
     81 /*
     82  * mfs vfs operations.
     83  */
     84 
     85 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
     86 
     87 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
     88 	&mfs_vnodeop_opv_desc,
     89 	NULL,
     90 };
     91 
     92 struct vfsops mfs_vfsops = {
     93 	.vfs_name = MOUNT_MFS,
     94 	.vfs_min_mount_data = sizeof (struct mfs_args),
     95 	.vfs_mount = mfs_mount,
     96 	.vfs_start = mfs_start,
     97 	.vfs_unmount = ffs_unmount,
     98 	.vfs_root = ufs_root,
     99 	.vfs_quotactl = ufs_quotactl,
    100 	.vfs_statvfs = mfs_statvfs,
    101 	.vfs_sync = ffs_sync,
    102 	.vfs_vget = ufs_vget,
    103 	.vfs_loadvnode = ffs_loadvnode,
    104 	.vfs_fhtovp = ffs_fhtovp,
    105 	.vfs_vptofh = ffs_vptofh,
    106 	.vfs_init = mfs_init,
    107 	.vfs_reinit = mfs_reinit,
    108 	.vfs_done = mfs_done,
    109 	.vfs_snapshot = (void *)eopnotsupp,
    110 	.vfs_extattrctl = vfs_stdextattrctl,
    111 	.vfs_suspendctl = (void *)eopnotsupp,
    112 	.vfs_renamelock_enter = genfs_renamelock_enter,
    113 	.vfs_renamelock_exit = genfs_renamelock_exit,
    114 	.vfs_fsync = (void *)eopnotsupp,
    115 	.vfs_opv_descs = mfs_vnodeopv_descs
    116 };
    117 
    118 static int
    119 mfs_modcmd(modcmd_t cmd, void *arg)
    120 {
    121 	int error;
    122 
    123 	switch (cmd) {
    124 	case MODULE_CMD_INIT:
    125 		error = vfs_attach(&mfs_vfsops);
    126 		if (error != 0)
    127 			break;
    128 		sysctl_createv(&mfs_sysctl_log, 0, NULL, NULL,
    129 			       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
    130 			       CTLTYPE_NODE, "mfs",
    131 			       SYSCTL_DESCR("Memory based file system"),
    132 			       NULL, 1, NULL, 0,
    133 			       CTL_VFS, 3, CTL_EOL);
    134 		/*
    135 		 * XXX the "1" and the "3" above could be dynamic, thereby
    136 		 * eliminating one more instance of the "number to vfs"
    137 		 * mapping problem, but they are in order as taken from
    138 		 * sys/mount.h
    139 		 */
    140 		break;
    141 	case MODULE_CMD_FINI:
    142 		error = vfs_detach(&mfs_vfsops);
    143 		if (error != 0)
    144 			break;
    145 		sysctl_teardown(&mfs_sysctl_log);
    146 		break;
    147 	default:
    148 		error = ENOTTY;
    149 		break;
    150 	}
    151 
    152 	return (error);
    153 }
    154 
    155 /*
    156  * Memory based filesystem initialization.
    157  */
    158 void
    159 mfs_init(void)
    160 {
    161 
    162 	if (mfs_initcnt++ == 0) {
    163 		mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE);
    164 		ffs_init();
    165 	}
    166 }
    167 
    168 void
    169 mfs_reinit(void)
    170 {
    171 
    172 	ffs_reinit();
    173 }
    174 
    175 void
    176 mfs_done(void)
    177 {
    178 
    179 	if (--mfs_initcnt == 0) {
    180 		ffs_done();
    181 		mutex_destroy(&mfs_lock);
    182 	}
    183 }
    184 
    185 /*
    186  * Called by main() when mfs is going to be mounted as root.
    187  */
    188 
    189 int
    190 mfs_mountroot(void)
    191 {
    192 	struct fs *fs;
    193 	struct mount *mp;
    194 	struct lwp *l = curlwp;		/* XXX */
    195 	struct ufsmount *ump;
    196 	struct mfsnode *mfsp;
    197 	int error = 0;
    198 
    199 	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
    200 		vrele(rootvp);
    201 		return (error);
    202 	}
    203 
    204 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    205 	rootvp->v_data = mfsp;
    206 	rootvp->v_op = mfs_vnodeop_p;
    207 	rootvp->v_tag = VT_MFS;
    208 	mfsp->mfs_baseoff = mfs_rootbase;
    209 	mfsp->mfs_size = mfs_rootsize;
    210 	mfsp->mfs_vnode = rootvp;
    211 	mfsp->mfs_proc = NULL;		/* indicate kernel space */
    212 	mfsp->mfs_shutdown = 0;
    213 	cv_init(&mfsp->mfs_cv, "mfs");
    214 	mfsp->mfs_refcnt = 1;
    215 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    216 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    217 		vfs_unbusy(mp, false, NULL);
    218 		bufq_free(mfsp->mfs_buflist);
    219 		vfs_destroy(mp);
    220 		kmem_free(mfsp, sizeof(*mfsp));
    221 		return (error);
    222 	}
    223 	mountlist_append(mp);
    224 	mp->mnt_vnodecovered = NULLVP;
    225 	ump = VFSTOUFS(mp);
    226 	fs = ump->um_fs;
    227 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    228 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    229 	vfs_unbusy(mp, false, NULL);
    230 	return (0);
    231 }
    232 
    233 /*
    234  * VFS Operations.
    235  *
    236  * mount system call
    237  */
    238 /* ARGSUSED */
    239 int
    240 mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    241 {
    242 	struct lwp *l = curlwp;
    243 	struct vnode *devvp;
    244 	struct mfs_args *args = data;
    245 	struct ufsmount *ump;
    246 	struct fs *fs;
    247 	struct mfsnode *mfsp;
    248 	struct proc *p;
    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 	error = getnewvnode(VT_MFS, NULL, mfs_vnodeop_p, NULL, &devvp);
    311 	if (error)
    312 		return (error);
    313 	devvp->v_vflag |= VV_MPSAFE;
    314 	devvp->v_type = VBLK;
    315 	spec_node_init(devvp, makedev(255, mfs_minor));
    316 	mfs_minor++;
    317 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    318 	devvp->v_data = mfsp;
    319 	mfsp->mfs_baseoff = args->base;
    320 	mfsp->mfs_size = args->size;
    321 	mfsp->mfs_vnode = devvp;
    322 	mfsp->mfs_proc = p;
    323 	mfsp->mfs_shutdown = 0;
    324 	cv_init(&mfsp->mfs_cv, "mfsidl");
    325 	mfsp->mfs_refcnt = 1;
    326 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    327 	if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
    328 		mfsp->mfs_shutdown = 1;
    329 		vrele(devvp);
    330 		return (error);
    331 	}
    332 	ump = VFSTOUFS(mp);
    333 	fs = ump->um_fs;
    334 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    335 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    336 	if (error)
    337 		return error;
    338 	(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    339 		sizeof(fs->fs_fsmnt));
    340 	fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
    341 	/* XXX: cleanup on error */
    342 	return 0;
    343 }
    344 
    345 /*
    346  * Used to grab the process and keep it in the kernel to service
    347  * memory filesystem I/O requests.
    348  *
    349  * Loop servicing I/O requests.
    350  * Copy the requested data into or out of the memory filesystem
    351  * address space.
    352  */
    353 /* ARGSUSED */
    354 int
    355 mfs_start(struct mount *mp, int flags)
    356 {
    357 	struct vnode *vp;
    358 	struct mfsnode *mfsp;
    359 	struct proc *p;
    360 	struct buf *bp;
    361 	void *base;
    362 	int sleepreturn = 0, refcnt, error;
    363 	ksiginfoq_t kq;
    364 
    365 	/*
    366 	 * Ensure that file system is still mounted when getting mfsnode.
    367 	 * Add a reference to the mfsnode to prevent it disappearing in
    368 	 * this routine.
    369 	 */
    370 	if ((error = vfs_busy(mp, NULL)) != 0)
    371 		return error;
    372 	vp = VFSTOUFS(mp)->um_devvp;
    373 	mfsp = VTOMFS(vp);
    374 	mutex_enter(&mfs_lock);
    375 	mfsp->mfs_refcnt++;
    376 	mutex_exit(&mfs_lock);
    377 	vfs_unbusy(mp, false, NULL);
    378 
    379 	base = mfsp->mfs_baseoff;
    380 	mutex_enter(&mfs_lock);
    381 	while (mfsp->mfs_shutdown != 1) {
    382 		while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
    383 			mutex_exit(&mfs_lock);
    384 			mfs_doio(bp, base);
    385 			mutex_enter(&mfs_lock);
    386 		}
    387 		/*
    388 		 * If a non-ignored signal is received, try to unmount.
    389 		 * If that fails, or the filesystem is already in the
    390 		 * process of being unmounted, clear the signal (it has been
    391 		 * "processed"), otherwise we will loop here, as tsleep
    392 		 * will always return EINTR/ERESTART.
    393 		 */
    394 		if (sleepreturn != 0) {
    395 			mutex_exit(&mfs_lock);
    396 			if (dounmount(mp, 0, curlwp) != 0) {
    397 				p = curproc;
    398 				ksiginfo_queue_init(&kq);
    399 				mutex_enter(p->p_lock);
    400 				sigclearall(p, NULL, &kq);
    401 				mutex_exit(p->p_lock);
    402 				ksiginfo_queue_drain(&kq);
    403 			}
    404 			sleepreturn = 0;
    405 			mutex_enter(&mfs_lock);
    406 			continue;
    407 		}
    408 
    409 		sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock);
    410 	}
    411 	KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL);
    412 	refcnt = --mfsp->mfs_refcnt;
    413 	mutex_exit(&mfs_lock);
    414 	if (refcnt == 0) {
    415 		bufq_free(mfsp->mfs_buflist);
    416 		cv_destroy(&mfsp->mfs_cv);
    417 		kmem_free(mfsp, sizeof(*mfsp));
    418 	}
    419 	return (sleepreturn);
    420 }
    421 
    422 /*
    423  * Get file system statistics.
    424  */
    425 int
    426 mfs_statvfs(struct mount *mp, struct statvfs *sbp)
    427 {
    428 	int error;
    429 
    430 	error = ffs_statvfs(mp, sbp);
    431 	if (error)
    432 		return error;
    433 	(void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
    434 	    sizeof(sbp->f_fstypename));
    435 	sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
    436 	return 0;
    437 }
    438