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