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