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