Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.32.2.5
      1 /*	$NetBSD: mfs_vfsops.c,v 1.32.2.5 2002/02/28 04:15:29 nathanw 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)mfs_vfsops.c	8.11 (Berkeley) 6/19/95
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: mfs_vfsops.c,v 1.32.2.5 2002/02/28 04:15:29 nathanw Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_compat_netbsd.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/time.h>
     48 #include <sys/kernel.h>
     49 #include <sys/lwp.h>
     50 #include <sys/proc.h>
     51 #include <sys/buf.h>
     52 #include <sys/mount.h>
     53 #include <sys/signalvar.h>
     54 #include <sys/vnode.h>
     55 #include <sys/malloc.h>
     56 
     57 #include <miscfs/syncfs/syncfs.h>
     58 
     59 #include <ufs/ufs/quota.h>
     60 #include <ufs/ufs/inode.h>
     61 #include <ufs/ufs/ufsmount.h>
     62 #include <ufs/ufs/ufs_extern.h>
     63 
     64 #include <ufs/ffs/fs.h>
     65 #include <ufs/ffs/ffs_extern.h>
     66 
     67 #include <ufs/mfs/mfsnode.h>
     68 #include <ufs/mfs/mfs_extern.h>
     69 
     70 caddr_t	mfs_rootbase;	/* address of mini-root in kernel virtual memory */
     71 u_long	mfs_rootsize;	/* size of mini-root in bytes */
     72 
     73 static	int mfs_minor;	/* used for building internal dev_t */
     74 
     75 extern int (**mfs_vnodeop_p) __P((void *));
     76 
     77 /*
     78  * mfs vfs operations.
     79  */
     80 
     81 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
     82 
     83 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
     84 	&mfs_vnodeop_opv_desc,
     85 	NULL,
     86 };
     87 
     88 struct vfsops mfs_vfsops = {
     89 	MOUNT_MFS,
     90 	mfs_mount,
     91 	mfs_start,
     92 	ffs_unmount,
     93 	ufs_root,
     94 	ufs_quotactl,
     95 	mfs_statfs,
     96 	ffs_sync,
     97 	ffs_vget,
     98 	ffs_fhtovp,
     99 	ffs_vptofh,
    100 	mfs_init,
    101 	mfs_reinit,
    102 	mfs_done,
    103 	ffs_sysctl,
    104 	NULL,
    105 	ufs_check_export,
    106 	mfs_vnodeopv_descs,
    107 };
    108 
    109 /*
    110  * Memory based filesystem initialization.
    111  */
    112 void
    113 mfs_init()
    114 {
    115 	/*
    116 	 * ffs_init() ensures to initialize necessary resources
    117 	 * only once.
    118 	 */
    119 	ffs_init();
    120 }
    121 
    122 void
    123 mfs_reinit()
    124 {
    125 	ffs_reinit();
    126 }
    127 
    128 void
    129 mfs_done()
    130 {
    131 	/*
    132 	 * ffs_done() ensures to free necessary resources
    133 	 * only once, when it's no more needed.
    134 	 */
    135 	ffs_done();
    136 }
    137 
    138 /*
    139  * Called by main() when mfs is going to be mounted as root.
    140  */
    141 
    142 int
    143 mfs_mountroot()
    144 {
    145 	struct fs *fs;
    146 	struct mount *mp;
    147 	struct proc *p = curproc->l_proc;	/* XXX */
    148 	struct ufsmount *ump;
    149 	struct mfsnode *mfsp;
    150 	int error = 0;
    151 
    152 	/*
    153 	 * Get vnodes for rootdev.
    154 	 */
    155 	if (bdevvp(rootdev, &rootvp)) {
    156 		printf("mfs_mountroot: can't setup bdevvp's");
    157 		return (error);
    158 	}
    159 
    160 	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
    161 		vrele(rootvp);
    162 		return (error);
    163 	}
    164 
    165 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    166 	rootvp->v_data = mfsp;
    167 	rootvp->v_op = mfs_vnodeop_p;
    168 	rootvp->v_tag = VT_MFS;
    169 	mfsp->mfs_baseoff = mfs_rootbase;
    170 	mfsp->mfs_size = mfs_rootsize;
    171 	mfsp->mfs_vnode = rootvp;
    172 	mfsp->mfs_proc = NULL;		/* indicate kernel space */
    173 	BUFQ_INIT(&mfsp->mfs_buflist);
    174 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    175 		mp->mnt_op->vfs_refcount--;
    176 		vfs_unbusy(mp);
    177 		free(mp, M_MOUNT);
    178 		free(mfsp, M_MFSNODE);
    179 		vrele(rootvp);
    180 		return (error);
    181 	}
    182 	simple_lock(&mountlist_slock);
    183 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    184 	simple_unlock(&mountlist_slock);
    185 	mp->mnt_vnodecovered = NULLVP;
    186 	ump = VFSTOUFS(mp);
    187 	fs = ump->um_fs;
    188 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    189 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    190 	vfs_unbusy(mp);
    191 	inittodr((time_t)0);
    192 	return (0);
    193 }
    194 
    195 /*
    196  * This is called early in boot to set the base address and size
    197  * of the mini-root.
    198  */
    199 int
    200 mfs_initminiroot(base)
    201 	caddr_t base;
    202 {
    203 	struct fs *fs = (struct fs *)(base + SBOFF);
    204 	extern int (*mountroot) __P((void));
    205 
    206 	/* check for valid super block */
    207 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
    208 	    fs->fs_bsize < sizeof(struct fs))
    209 		return (0);
    210 	mountroot = mfs_mountroot;
    211 	mfs_rootbase = base;
    212 	mfs_rootsize = fs->fs_fsize * fs->fs_size;
    213 	rootdev = makedev(255, mfs_minor);
    214 	mfs_minor++;
    215 	return (mfs_rootsize);
    216 }
    217 
    218 /*
    219  * VFS Operations.
    220  *
    221  * mount system call
    222  */
    223 /* ARGSUSED */
    224 int
    225 mfs_mount(mp, path, data, ndp, p)
    226 	struct mount *mp;
    227 	const char *path;
    228 	void *data;
    229 	struct nameidata *ndp;
    230 	struct proc *p;
    231 {
    232 	struct vnode *devvp;
    233 	struct mfs_args args;
    234 	struct ufsmount *ump;
    235 	struct fs *fs;
    236 	struct mfsnode *mfsp;
    237 	size_t size;
    238 	int flags, error;
    239 
    240 	/*
    241 	 * XXX turn off async to avoid hangs when writing lots of data.
    242 	 * the problem is that MFS needs to allocate pages to clean pages,
    243 	 * so if we wait until the last minute to clean pages then there
    244 	 * may not be any pages available to do the cleaning.
    245 	 */
    246 	mp->mnt_flag &= ~MNT_ASYNC;
    247 
    248 	error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
    249 	if (error)
    250 		return (error);
    251 
    252 	/*
    253 	 * If updating, check whether changing from read-only to
    254 	 * read/write; if there is no device name, that's all we do.
    255 	 */
    256 	if (mp->mnt_flag & MNT_UPDATE) {
    257 		ump = VFSTOUFS(mp);
    258 		fs = ump->um_fs;
    259 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    260 			flags = WRITECLOSE;
    261 			if (mp->mnt_flag & MNT_FORCE)
    262 				flags |= FORCECLOSE;
    263 			error = ffs_flushfiles(mp, flags, p);
    264 			if (error)
    265 				return (error);
    266 		}
    267 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
    268 			fs->fs_ronly = 0;
    269 		if (args.fspec == 0)
    270 			return (vfs_export(mp, &ump->um_export, &args.export));
    271 		return (0);
    272 	}
    273 	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
    274 	if (error)
    275 		return (error);
    276 	devvp->v_type = VBLK;
    277 	if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
    278 		panic("mfs_mount: dup dev");
    279 	mfs_minor++;
    280 	mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    281 	devvp->v_data = mfsp;
    282 	mfsp->mfs_baseoff = args.base;
    283 	mfsp->mfs_size = args.size;
    284 	mfsp->mfs_vnode = devvp;
    285 	mfsp->mfs_proc = p;
    286 	BUFQ_INIT(&mfsp->mfs_buflist);
    287 	if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
    288 		BUFQ_FIRST(&mfsp->mfs_buflist) = (struct buf *) -1;
    289 		vrele(devvp);
    290 		return (error);
    291 	}
    292 	ump = VFSTOUFS(mp);
    293 	fs = ump->um_fs;
    294 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    295 	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
    296 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
    297 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    298 	    &size);
    299 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    300 	return (0);
    301 }
    302 
    303 int	mfs_pri = PWAIT | PCATCH;		/* XXX prob. temp */
    304 
    305 /*
    306  * Used to grab the process and keep it in the kernel to service
    307  * memory filesystem I/O requests.
    308  *
    309  * Loop servicing I/O requests.
    310  * Copy the requested data into or out of the memory filesystem
    311  * address space.
    312  */
    313 /* ARGSUSED */
    314 int
    315 mfs_start(mp, flags, p)
    316 	struct mount *mp;
    317 	int flags;
    318 	struct proc *p;
    319 {
    320 	struct vnode *vp = VFSTOUFS(mp)->um_devvp;
    321 	struct mfsnode *mfsp = VTOMFS(vp);
    322 	struct buf *bp;
    323 	caddr_t base;
    324 	int sleepreturn = 0;
    325 	struct lwp *l; /* XXX NJWLWP */
    326 
    327 	/* XXX NJWLWP the vnode interface again gives us a proc in a
    328 	 * place where we want a execution context. Cheat.
    329 	 */
    330 	KASSERT(curproc->l_proc == p);
    331 	l = curproc;
    332 	base = mfsp->mfs_baseoff;
    333 	while (BUFQ_FIRST(&mfsp->mfs_buflist) != (struct buf *) -1) {
    334 		while ((bp = BUFQ_FIRST(&mfsp->mfs_buflist)) != NULL) {
    335 			BUFQ_REMOVE(&mfsp->mfs_buflist, bp);
    336 			mfs_doio(bp, base);
    337 			wakeup((caddr_t)bp);
    338 		}
    339 		/*
    340 		 * If a non-ignored signal is received, try to unmount.
    341 		 * If that fails, or the filesystem is already in the
    342 		 * process of being unmounted, clear the signal (it has been
    343 		 * "processed"), otherwise we will loop here, as tsleep
    344 		 * will always return EINTR/ERESTART.  */
    345 		if (sleepreturn != 0) {
    346 			/*
    347 			 * XXX Freeze syncer.  Must do this before locking
    348 			 * the mount point.  See dounmount() for details.
    349 			 */
    350 			lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    351 			if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
    352 				lockmgr(&syncer_lock, LK_RELEASE, NULL);
    353 			else if (dounmount(mp, 0, p) != 0)
    354 				CLRSIG(p, CURSIG(l));
    355 			sleepreturn = 0;
    356 			continue;
    357 		}
    358 
    359 		sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
    360 	}
    361 	return (sleepreturn);
    362 }
    363 
    364 /*
    365  * Get file system statistics.
    366  */
    367 int
    368 mfs_statfs(mp, sbp, p)
    369 	struct mount *mp;
    370 	struct statfs *sbp;
    371 	struct proc *p;
    372 {
    373 	int error;
    374 
    375 	error = ffs_statfs(mp, sbp, p);
    376 #ifdef COMPAT_09
    377 	sbp->f_type = 3;
    378 #else
    379 	sbp->f_type = 0;
    380 #endif
    381 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    382 	return (error);
    383 }
    384