Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.54
      1 /*	$NetBSD: mfs_vfsops.c,v 1.54 2003/12/04 19:38:25 atatat 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.54 2003/12/04 19:38:25 atatat 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/mount.h>
     49 #include <sys/signalvar.h>
     50 #include <sys/vnode.h>
     51 #include <sys/malloc.h>
     52 
     53 #include <miscfs/syncfs/syncfs.h>
     54 
     55 #include <ufs/ufs/quota.h>
     56 #include <ufs/ufs/inode.h>
     57 #include <ufs/ufs/ufsmount.h>
     58 #include <ufs/ufs/ufs_extern.h>
     59 
     60 #include <ufs/ffs/fs.h>
     61 #include <ufs/ffs/ffs_extern.h>
     62 
     63 #include <ufs/mfs/mfsnode.h>
     64 #include <ufs/mfs/mfs_extern.h>
     65 
     66 caddr_t	mfs_rootbase;	/* address of mini-root in kernel virtual memory */
     67 u_long	mfs_rootsize;	/* size of mini-root in bytes */
     68 
     69 static	int mfs_minor;	/* used for building internal dev_t */
     70 
     71 extern int (**mfs_vnodeop_p) __P((void *));
     72 
     73 MALLOC_DEFINE(M_MFSNODE, "MFS node", "MFS vnode private part");
     74 
     75 /*
     76  * mfs vfs operations.
     77  */
     78 
     79 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
     80 
     81 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
     82 	&mfs_vnodeop_opv_desc,
     83 	NULL,
     84 };
     85 
     86 struct vfsops mfs_vfsops = {
     87 	MOUNT_MFS,
     88 	mfs_mount,
     89 	mfs_start,
     90 	ffs_unmount,
     91 	ufs_root,
     92 	ufs_quotactl,
     93 	mfs_statfs,
     94 	ffs_sync,
     95 	ffs_vget,
     96 	ffs_fhtovp,
     97 	ffs_vptofh,
     98 	mfs_init,
     99 	mfs_reinit,
    100 	mfs_done,
    101 	NULL,
    102 	NULL,
    103 	ufs_check_export,
    104 	mfs_vnodeopv_descs,
    105 };
    106 
    107 SYSCTL_SETUP(sysctl_vfs_mfs_setup, "sysctl vfs.mfs subtree setup")
    108 {
    109 
    110 	sysctl_createv(SYSCTL_PERMANENT,
    111 		       CTLTYPE_NODE, "vfs", NULL,
    112 		       NULL, 0, NULL, 0,
    113 		       CTL_VFS, CTL_EOL);
    114 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_ALIAS,
    115 		       CTLTYPE_NODE, "mfs", NULL,
    116 		       NULL, 1, NULL, 0,
    117 		       CTL_VFS, 3, CTL_EOL);
    118 	/*
    119 	 * XXX the "1" and the "3" above could be dynamic, thereby
    120 	 * eliminating one more instance of the "number to vfs"
    121 	 * mapping problem, but they are in order as taken from
    122 	 * sys/mount.h
    123 	 */
    124 }
    125 
    126 /*
    127  * Memory based filesystem initialization.
    128  */
    129 void
    130 mfs_init()
    131 {
    132 #ifdef _LKM
    133 	malloc_type_attach(M_MFSNODE);
    134 #endif
    135 	/*
    136 	 * ffs_init() ensures to initialize necessary resources
    137 	 * only once.
    138 	 */
    139 	ffs_init();
    140 }
    141 
    142 void
    143 mfs_reinit()
    144 {
    145 	ffs_reinit();
    146 }
    147 
    148 void
    149 mfs_done()
    150 {
    151 	/*
    152 	 * ffs_done() ensures to free necessary resources
    153 	 * only once, when it's no more needed.
    154 	 */
    155 	ffs_done();
    156 #ifdef _LKM
    157 	malloc_type_detach(M_MFSNODE);
    158 #endif
    159 }
    160 
    161 /*
    162  * Called by main() when mfs is going to be mounted as root.
    163  */
    164 
    165 int
    166 mfs_mountroot()
    167 {
    168 	struct fs *fs;
    169 	struct mount *mp;
    170 	struct proc *p = curproc;	/* XXX */
    171 	struct ufsmount *ump;
    172 	struct mfsnode *mfsp;
    173 	int error = 0;
    174 
    175 	/*
    176 	 * Get vnodes for rootdev.
    177 	 */
    178 	if (bdevvp(rootdev, &rootvp)) {
    179 		printf("mfs_mountroot: can't setup bdevvp's");
    180 		return (error);
    181 	}
    182 
    183 	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
    184 		vrele(rootvp);
    185 		return (error);
    186 	}
    187 
    188 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    189 	rootvp->v_data = mfsp;
    190 	rootvp->v_op = mfs_vnodeop_p;
    191 	rootvp->v_tag = VT_MFS;
    192 	mfsp->mfs_baseoff = mfs_rootbase;
    193 	mfsp->mfs_size = mfs_rootsize;
    194 	mfsp->mfs_vnode = rootvp;
    195 	mfsp->mfs_proc = NULL;		/* indicate kernel space */
    196 	mfsp->mfs_shutdown = 0;
    197 	bufq_alloc(&mfsp->mfs_buflist, BUFQ_FCFS);
    198 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    199 		mp->mnt_op->vfs_refcount--;
    200 		vfs_unbusy(mp);
    201 		bufq_free(&mfsp->mfs_buflist);
    202 		free(mp, M_MOUNT);
    203 		free(mfsp, M_MFSNODE);
    204 		vrele(rootvp);
    205 		return (error);
    206 	}
    207 	simple_lock(&mountlist_slock);
    208 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    209 	simple_unlock(&mountlist_slock);
    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_statfs(mp, &mp->mnt_stat, p);
    215 	vfs_unbusy(mp);
    216 	inittodr((time_t)0);
    217 	return (0);
    218 }
    219 
    220 /*
    221  * This is called early in boot to set the base address and size
    222  * of the mini-root.
    223  */
    224 int
    225 mfs_initminiroot(base)
    226 	caddr_t base;
    227 {
    228 	struct fs *fs = (struct fs *)(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(mp, path, data, ndp, p)
    250 	struct mount *mp;
    251 	const char *path;
    252 	void *data;
    253 	struct nameidata *ndp;
    254 	struct proc *p;
    255 {
    256 	struct vnode *devvp;
    257 	struct mfs_args args;
    258 	struct ufsmount *ump;
    259 	struct fs *fs;
    260 	struct mfsnode *mfsp;
    261 	int flags, error;
    262 
    263 	if (mp->mnt_flag & MNT_GETARGS) {
    264 		struct vnode *vp;
    265 		struct mfsnode *mfsp;
    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 		vfs_showexport(mp, &args.export, &ump->um_export);
    281 		args.base = mfsp->mfs_baseoff;
    282 		args.size = mfsp->mfs_size;
    283 		return copyout(&args, data, sizeof(args));
    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 	error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
    297 	if (error)
    298 		return (error);
    299 
    300 	/*
    301 	 * If updating, check whether changing from read-only to
    302 	 * read/write; if there is no device name, that's all we do.
    303 	 */
    304 	if (mp->mnt_flag & MNT_UPDATE) {
    305 		ump = VFSTOUFS(mp);
    306 		fs = ump->um_fs;
    307 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    308 			flags = WRITECLOSE;
    309 			if (mp->mnt_flag & MNT_FORCE)
    310 				flags |= FORCECLOSE;
    311 			error = ffs_flushfiles(mp, flags, p);
    312 			if (error)
    313 				return (error);
    314 		}
    315 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
    316 			fs->fs_ronly = 0;
    317 		if (args.fspec == 0)
    318 			return (vfs_export(mp, &ump->um_export, &args.export));
    319 		return (0);
    320 	}
    321 	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
    322 	if (error)
    323 		return (error);
    324 	devvp->v_type = VBLK;
    325 	if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
    326 		panic("mfs_mount: dup dev");
    327 	mfs_minor++;
    328 	mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    329 	devvp->v_data = mfsp;
    330 	mfsp->mfs_baseoff = args.base;
    331 	mfsp->mfs_size = args.size;
    332 	mfsp->mfs_vnode = devvp;
    333 	mfsp->mfs_proc = p;
    334 	mfsp->mfs_shutdown = 0;
    335 	bufq_alloc(&mfsp->mfs_buflist, BUFQ_FCFS);
    336 	if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
    337 		mfsp->mfs_shutdown = 1;
    338 		vrele(devvp);
    339 		return (error);
    340 	}
    341 	ump = VFSTOUFS(mp);
    342 	fs = ump->um_fs;
    343 	error = set_statfs_info(path, UIO_USERSPACE, args.fspec,
    344 	    UIO_USERSPACE, mp, p);
    345 	(void)memcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    346 	    sizeof(mp->mnt_stat.f_mntonname));
    347 	return error;
    348 }
    349 
    350 int	mfs_pri = PWAIT | PCATCH;		/* XXX prob. temp */
    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(mp, flags, p)
    363 	struct mount *mp;
    364 	int flags;
    365 	struct proc *p;
    366 {
    367 	struct vnode *vp = VFSTOUFS(mp)->um_devvp;
    368 	struct mfsnode *mfsp = VTOMFS(vp);
    369 	struct buf *bp;
    370 	caddr_t base;
    371 	int sleepreturn = 0;
    372 	struct lwp *l; /* XXX NJWLWP */
    373 
    374 	/* XXX NJWLWP the vnode interface again gives us a proc in a
    375 	 * place where we want a execution context. Cheat.
    376 	 */
    377 	KASSERT(curproc == p);
    378 	l = curlwp;
    379 	base = mfsp->mfs_baseoff;
    380 	while (mfsp->mfs_shutdown != 1) {
    381 		while ((bp = BUFQ_GET(&mfsp->mfs_buflist)) != NULL) {
    382 			mfs_doio(bp, base);
    383 			wakeup((caddr_t)bp);
    384 		}
    385 		/*
    386 		 * If a non-ignored signal is received, try to unmount.
    387 		 * If that fails, or the filesystem is already in the
    388 		 * process of being unmounted, clear the signal (it has been
    389 		 * "processed"), otherwise we will loop here, as tsleep
    390 		 * will always return EINTR/ERESTART.
    391 		 */
    392 		if (sleepreturn != 0) {
    393 			/*
    394 			 * XXX Freeze syncer.  Must do this before locking
    395 			 * the mount point.  See dounmount() for details.
    396 			 */
    397 			lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    398 			if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
    399 				lockmgr(&syncer_lock, LK_RELEASE, NULL);
    400 			else if (dounmount(mp, 0, p) != 0)
    401 				CLRSIG(p, CURSIG(l));
    402 			sleepreturn = 0;
    403 			continue;
    404 		}
    405 
    406 		sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
    407 	}
    408 	KASSERT(BUFQ_PEEK(&mfsp->mfs_buflist) == NULL);
    409 	bufq_free(&mfsp->mfs_buflist);
    410 	return (sleepreturn);
    411 }
    412 
    413 /*
    414  * Get file system statistics.
    415  */
    416 int
    417 mfs_statfs(mp, sbp, p)
    418 	struct mount *mp;
    419 	struct statfs *sbp;
    420 	struct proc *p;
    421 {
    422 	int error;
    423 
    424 	error = ffs_statfs(mp, sbp, p);
    425 #ifdef COMPAT_09
    426 	sbp->f_type = 3;
    427 #else
    428 	sbp->f_type = 0;
    429 #endif
    430 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    431 	return (error);
    432 }
    433