Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.97
      1 /*	$NetBSD: mfs_vfsops.c,v 1.97 2008/05/10 02:26:11 rumble 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.97 2008/05/10 02:26:11 rumble 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, NULL);
     70 
     71 void *	mfs_rootbase;	/* address of mini-root in kernel virtual memory */
     72 u_long	mfs_rootsize;	/* size of mini-root in bytes */
     73 kmutex_t mfs_lock;	/* global lock */
     74 
     75 static int mfs_minor;	/* used for building internal dev_t */
     76 static int mfs_initcnt;
     77 
     78 extern int (**mfs_vnodeop_p)(void *);
     79 
     80 /*
     81  * mfs vfs operations.
     82  */
     83 
     84 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
     85 
     86 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
     87 	&mfs_vnodeop_opv_desc,
     88 	NULL,
     89 };
     90 
     91 struct vfsops mfs_vfsops = {
     92 	MOUNT_MFS,
     93 	sizeof (struct mfs_args),
     94 	mfs_mount,
     95 	mfs_start,
     96 	ffs_unmount,
     97 	ufs_root,
     98 	ufs_quotactl,
     99 	mfs_statvfs,
    100 	ffs_sync,
    101 	ffs_vget,
    102 	ffs_fhtovp,
    103 	ffs_vptofh,
    104 	mfs_init,
    105 	mfs_reinit,
    106 	mfs_done,
    107 	NULL,
    108 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    109 	vfs_stdextattrctl,
    110 	(void *)eopnotsupp,	/* vfs_suspendctl */
    111 	genfs_renamelock_enter,
    112 	genfs_renamelock_exit,
    113 	(void *)eopnotsupp,
    114 	mfs_vnodeopv_descs,
    115 	0,
    116 	{ NULL, NULL },
    117 };
    118 
    119 static int
    120 mfs_modcmd(modcmd_t cmd, void *arg)
    121 {
    122 
    123 	switch (cmd) {
    124 	case MODULE_CMD_INIT:
    125 		return vfs_attach(&mfs_vfsops);
    126 	case MODULE_CMD_FINI:
    127 		return vfs_detach(&mfs_vfsops);
    128 	default:
    129 		return ENOTTY;
    130 	}
    131 }
    132 
    133 SYSCTL_SETUP(sysctl_vfs_mfs_setup, "sysctl vfs.mfs subtree setup")
    134 {
    135 
    136 	sysctl_createv(clog, 0, NULL, NULL,
    137 		       CTLFLAG_PERMANENT,
    138 		       CTLTYPE_NODE, "vfs", NULL,
    139 		       NULL, 0, NULL, 0,
    140 		       CTL_VFS, CTL_EOL);
    141 	sysctl_createv(clog, 0, NULL, NULL,
    142 		       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
    143 		       CTLTYPE_NODE, "mfs",
    144 		       SYSCTL_DESCR("Memory based file system"),
    145 		       NULL, 1, NULL, 0,
    146 		       CTL_VFS, 3, CTL_EOL);
    147 	/*
    148 	 * XXX the "1" and the "3" above could be dynamic, thereby
    149 	 * eliminating one more instance of the "number to vfs"
    150 	 * mapping problem, but they are in order as taken from
    151 	 * sys/mount.h
    152 	 */
    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 	mutex_enter(&mountlist_lock);
    224 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    225 	mutex_exit(&mountlist_lock);
    226 	mp->mnt_vnodecovered = NULLVP;
    227 	ump = VFSTOUFS(mp);
    228 	fs = ump->um_fs;
    229 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    230 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    231 	vfs_unbusy(mp, false, NULL);
    232 	return (0);
    233 }
    234 
    235 /*
    236  * This is called early in boot to set the base address and size
    237  * of the mini-root.
    238  */
    239 int
    240 mfs_initminiroot(void *base)
    241 {
    242 	struct fs *fs = (struct fs *)((char *)base + SBLOCK_UFS1);
    243 
    244 	/* check for valid super block */
    245 	if (fs->fs_magic != FS_UFS1_MAGIC || fs->fs_bsize > MAXBSIZE ||
    246 	    fs->fs_bsize < sizeof(struct fs))
    247 		return (0);
    248 	mountroot = mfs_mountroot;
    249 	mfs_rootbase = base;
    250 	mfs_rootsize = fs->fs_fsize * fs->fs_size;
    251 	rootdev = makedev(255, mfs_minor);
    252 	mfs_minor++;
    253 	return (mfs_rootsize);
    254 }
    255 
    256 /*
    257  * VFS Operations.
    258  *
    259  * mount system call
    260  */
    261 /* ARGSUSED */
    262 int
    263 mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    264 {
    265 	struct lwp *l = curlwp;
    266 	struct vnode *devvp;
    267 	struct mfs_args *args = data;
    268 	struct ufsmount *ump;
    269 	struct fs *fs;
    270 	struct mfsnode *mfsp;
    271 	struct proc *p;
    272 	int flags, error = 0;
    273 
    274 	if (*data_len < sizeof *args)
    275 		return EINVAL;
    276 
    277 	p = l->l_proc;
    278 	if (mp->mnt_flag & MNT_GETARGS) {
    279 		struct vnode *vp;
    280 
    281 		ump = VFSTOUFS(mp);
    282 		if (ump == NULL)
    283 			return EIO;
    284 
    285 		vp = ump->um_devvp;
    286 		if (vp == NULL)
    287 			return EIO;
    288 
    289 		mfsp = VTOMFS(vp);
    290 		if (mfsp == NULL)
    291 			return EIO;
    292 
    293 		args->fspec = NULL;
    294 		args->base = mfsp->mfs_baseoff;
    295 		args->size = mfsp->mfs_size;
    296 		*data_len = sizeof *args;
    297 		return 0;
    298 	}
    299 	/*
    300 	 * XXX turn off async to avoid hangs when writing lots of data.
    301 	 * the problem is that MFS needs to allocate pages to clean pages,
    302 	 * so if we wait until the last minute to clean pages then there
    303 	 * may not be any pages available to do the cleaning.
    304 	 * ... and since the default partially-synchronous mode turns out
    305 	 * to not be sufficient under heavy load, make it full synchronous.
    306 	 */
    307 	mp->mnt_flag &= ~MNT_ASYNC;
    308 	mp->mnt_flag |= MNT_SYNCHRONOUS;
    309 
    310 	/*
    311 	 * If updating, check whether changing from read-only to
    312 	 * read/write; if there is no device name, that's all we do.
    313 	 */
    314 	if (mp->mnt_flag & MNT_UPDATE) {
    315 		ump = VFSTOUFS(mp);
    316 		fs = ump->um_fs;
    317 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    318 			flags = WRITECLOSE;
    319 			if (mp->mnt_flag & MNT_FORCE)
    320 				flags |= FORCECLOSE;
    321 			error = ffs_flushfiles(mp, flags, l);
    322 			if (error)
    323 				return (error);
    324 		}
    325 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
    326 			fs->fs_ronly = 0;
    327 		if (args->fspec == NULL)
    328 			return EINVAL;
    329 		return (0);
    330 	}
    331 	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
    332 	if (error)
    333 		return (error);
    334 	devvp->v_vflag |= VV_MPSAFE;
    335 	devvp->v_type = VBLK;
    336 	spec_node_init(devvp, makedev(255, mfs_minor));
    337 	mfs_minor++;
    338 	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
    339 	devvp->v_data = mfsp;
    340 	mfsp->mfs_baseoff = args->base;
    341 	mfsp->mfs_size = args->size;
    342 	mfsp->mfs_vnode = devvp;
    343 	mfsp->mfs_proc = p;
    344 	mfsp->mfs_shutdown = 0;
    345 	cv_init(&mfsp->mfs_cv, "mfsidl");
    346 	mfsp->mfs_refcnt = 1;
    347 	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
    348 	if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
    349 		mfsp->mfs_shutdown = 1;
    350 		vrele(devvp);
    351 		return (error);
    352 	}
    353 	ump = VFSTOUFS(mp);
    354 	fs = ump->um_fs;
    355 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    356 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    357 	if (error)
    358 		return error;
    359 	(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    360 		sizeof(fs->fs_fsmnt));
    361 	fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
    362 	/* XXX: cleanup on error */
    363 	return 0;
    364 }
    365 
    366 /*
    367  * Used to grab the process and keep it in the kernel to service
    368  * memory filesystem I/O requests.
    369  *
    370  * Loop servicing I/O requests.
    371  * Copy the requested data into or out of the memory filesystem
    372  * address space.
    373  */
    374 /* ARGSUSED */
    375 int
    376 mfs_start(struct mount *mp, int flags)
    377 {
    378 	struct vnode *vp;
    379 	struct mfsnode *mfsp;
    380 	struct proc *p;
    381 	struct buf *bp;
    382 	void *base;
    383 	int sleepreturn = 0, refcnt, error;
    384 	ksiginfoq_t kq;
    385 
    386 	/*
    387 	 * Ensure that file system is still mounted when getting mfsnode.
    388 	 * Add a reference to the mfsnode to prevent it disappearing in
    389 	 * this routine.
    390 	 */
    391 	if ((error = vfs_busy(mp, NULL)) != 0)
    392 		return error;
    393 	vp = VFSTOUFS(mp)->um_devvp;
    394 	mfsp = VTOMFS(vp);
    395 	mutex_enter(&mfs_lock);
    396 	mfsp->mfs_refcnt++;
    397 	mutex_exit(&mfs_lock);
    398 	vfs_unbusy(mp, false, NULL);
    399 
    400 	base = mfsp->mfs_baseoff;
    401 	mutex_enter(&mfs_lock);
    402 	while (mfsp->mfs_shutdown != 1) {
    403 		while ((bp = BUFQ_GET(mfsp->mfs_buflist)) != NULL) {
    404 			mutex_exit(&mfs_lock);
    405 			mfs_doio(bp, base);
    406 			mutex_enter(&mfs_lock);
    407 		}
    408 		/*
    409 		 * If a non-ignored signal is received, try to unmount.
    410 		 * If that fails, or the filesystem is already in the
    411 		 * process of being unmounted, clear the signal (it has been
    412 		 * "processed"), otherwise we will loop here, as tsleep
    413 		 * will always return EINTR/ERESTART.
    414 		 */
    415 		if (sleepreturn != 0) {
    416 			mutex_exit(&mfs_lock);
    417 			if (dounmount(mp, 0, curlwp) != 0) {
    418 				p = curproc;
    419 				ksiginfo_queue_init(&kq);
    420 				mutex_enter(p->p_lock);
    421 				sigclearall(p, NULL, &kq);
    422 				mutex_exit(p->p_lock);
    423 				ksiginfo_queue_drain(&kq);
    424 			}
    425 			sleepreturn = 0;
    426 			mutex_enter(&mfs_lock);
    427 			continue;
    428 		}
    429 
    430 		sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock);
    431 	}
    432 	KASSERT(BUFQ_PEEK(mfsp->mfs_buflist) == NULL);
    433 	refcnt = --mfsp->mfs_refcnt;
    434 	mutex_exit(&mfs_lock);
    435 	if (refcnt == 0) {
    436 		bufq_free(mfsp->mfs_buflist);
    437 		cv_destroy(&mfsp->mfs_cv);
    438 		kmem_free(mfsp, sizeof(*mfsp));
    439 	}
    440 	return (sleepreturn);
    441 }
    442 
    443 /*
    444  * Get file system statistics.
    445  */
    446 int
    447 mfs_statvfs(struct mount *mp, struct statvfs *sbp)
    448 {
    449 	int error;
    450 
    451 	error = ffs_statvfs(mp, sbp);
    452 	if (error)
    453 		return error;
    454 	(void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
    455 	    sizeof(sbp->f_fstypename));
    456 	sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
    457 	return 0;
    458 }
    459