Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.32.2.3
      1 /*	$NetBSD: mfs_vfsops.c,v 1.32.2.3 2001/09/21 22:37:08 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 #if defined(_KERNEL_OPT)
     39 #include "opt_compat_netbsd.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/time.h>
     45 #include <sys/kernel.h>
     46 #include <sys/lwp.h>
     47 #include <sys/proc.h>
     48 #include <sys/buf.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 /*
     75  * mfs vfs operations.
     76  */
     77 
     78 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
     79 
     80 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
     81 	&mfs_vnodeop_opv_desc,
     82 	NULL,
     83 };
     84 
     85 struct vfsops mfs_vfsops = {
     86 	MOUNT_MFS,
     87 	mfs_mount,
     88 	mfs_start,
     89 	ffs_unmount,
     90 	ufs_root,
     91 	ufs_quotactl,
     92 	mfs_statfs,
     93 	ffs_sync,
     94 	ffs_vget,
     95 	ffs_fhtovp,
     96 	ffs_vptofh,
     97 	mfs_init,
     98 	mfs_reinit,
     99 	mfs_done,
    100 	ffs_sysctl,
    101 	NULL,
    102 	ufs_check_export,
    103 	mfs_vnodeopv_descs,
    104 };
    105 
    106 /*
    107  * Memory based filesystem initialization.
    108  */
    109 void
    110 mfs_init()
    111 {
    112 	/*
    113 	 * ffs_init() ensures to initialize necessary resources
    114 	 * only once.
    115 	 */
    116 	ffs_init();
    117 }
    118 
    119 void
    120 mfs_reinit()
    121 {
    122 	ffs_reinit();
    123 }
    124 
    125 void
    126 mfs_done()
    127 {
    128 	/*
    129 	 * ffs_done() ensures to free necessary resources
    130 	 * only once, when it's no more needed.
    131 	 */
    132 	ffs_done();
    133 }
    134 
    135 /*
    136  * Called by main() when mfs is going to be mounted as root.
    137  */
    138 
    139 int
    140 mfs_mountroot()
    141 {
    142 	struct fs *fs;
    143 	struct mount *mp;
    144 	struct proc *p = curproc->l_proc;	/* XXX */
    145 	struct ufsmount *ump;
    146 	struct mfsnode *mfsp;
    147 	int error = 0;
    148 
    149 	/*
    150 	 * Get vnodes for rootdev.
    151 	 */
    152 	if (bdevvp(rootdev, &rootvp)) {
    153 		printf("mfs_mountroot: can't setup bdevvp's");
    154 		return (error);
    155 	}
    156 
    157 	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
    158 		vrele(rootvp);
    159 		return (error);
    160 	}
    161 
    162 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    163 	rootvp->v_data = mfsp;
    164 	rootvp->v_op = mfs_vnodeop_p;
    165 	rootvp->v_tag = VT_MFS;
    166 	mfsp->mfs_baseoff = mfs_rootbase;
    167 	mfsp->mfs_size = mfs_rootsize;
    168 	mfsp->mfs_vnode = rootvp;
    169 	mfsp->mfs_proc = NULL;		/* indicate kernel space */
    170 	BUFQ_INIT(&mfsp->mfs_buflist);
    171 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    172 		mp->mnt_op->vfs_refcount--;
    173 		vfs_unbusy(mp);
    174 		free(mp, M_MOUNT);
    175 		free(mfsp, M_MFSNODE);
    176 		vrele(rootvp);
    177 		return (error);
    178 	}
    179 	simple_lock(&mountlist_slock);
    180 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    181 	simple_unlock(&mountlist_slock);
    182 	mp->mnt_vnodecovered = NULLVP;
    183 	ump = VFSTOUFS(mp);
    184 	fs = ump->um_fs;
    185 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    186 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    187 	vfs_unbusy(mp);
    188 	inittodr((time_t)0);
    189 	return (0);
    190 }
    191 
    192 /*
    193  * This is called early in boot to set the base address and size
    194  * of the mini-root.
    195  */
    196 int
    197 mfs_initminiroot(base)
    198 	caddr_t base;
    199 {
    200 	struct fs *fs = (struct fs *)(base + SBOFF);
    201 	extern int (*mountroot) __P((void));
    202 
    203 	/* check for valid super block */
    204 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
    205 	    fs->fs_bsize < sizeof(struct fs))
    206 		return (0);
    207 	mountroot = mfs_mountroot;
    208 	mfs_rootbase = base;
    209 	mfs_rootsize = fs->fs_fsize * fs->fs_size;
    210 	rootdev = makedev(255, mfs_minor);
    211 	mfs_minor++;
    212 	return (mfs_rootsize);
    213 }
    214 
    215 /*
    216  * VFS Operations.
    217  *
    218  * mount system call
    219  */
    220 /* ARGSUSED */
    221 int
    222 mfs_mount(mp, path, data, ndp, p)
    223 	struct mount *mp;
    224 	const char *path;
    225 	void *data;
    226 	struct nameidata *ndp;
    227 	struct proc *p;
    228 {
    229 	struct vnode *devvp;
    230 	struct mfs_args args;
    231 	struct ufsmount *ump;
    232 	struct fs *fs;
    233 	struct mfsnode *mfsp;
    234 	size_t size;
    235 	int flags, error;
    236 
    237 	error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
    238 	if (error)
    239 		return (error);
    240 
    241 	/*
    242 	 * If updating, check whether changing from read-only to
    243 	 * read/write; if there is no device name, that's all we do.
    244 	 */
    245 	if (mp->mnt_flag & MNT_UPDATE) {
    246 		ump = VFSTOUFS(mp);
    247 		fs = ump->um_fs;
    248 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    249 			flags = WRITECLOSE;
    250 			if (mp->mnt_flag & MNT_FORCE)
    251 				flags |= FORCECLOSE;
    252 			error = ffs_flushfiles(mp, flags, p);
    253 			if (error)
    254 				return (error);
    255 		}
    256 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
    257 			fs->fs_ronly = 0;
    258 		if (args.fspec == 0)
    259 			return (vfs_export(mp, &ump->um_export, &args.export));
    260 		return (0);
    261 	}
    262 	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
    263 	if (error)
    264 		return (error);
    265 	devvp->v_type = VBLK;
    266 	if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
    267 		panic("mfs_mount: dup dev");
    268 	mfs_minor++;
    269 	mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    270 	devvp->v_data = mfsp;
    271 	mfsp->mfs_baseoff = args.base;
    272 	mfsp->mfs_size = args.size;
    273 	mfsp->mfs_vnode = devvp;
    274 	mfsp->mfs_proc = p;
    275 	BUFQ_INIT(&mfsp->mfs_buflist);
    276 	if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
    277 		BUFQ_FIRST(&mfsp->mfs_buflist) = (struct buf *) -1;
    278 		vrele(devvp);
    279 		return (error);
    280 	}
    281 	ump = VFSTOUFS(mp);
    282 	fs = ump->um_fs;
    283 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    284 	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
    285 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
    286 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    287 	    &size);
    288 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    289 	return (0);
    290 }
    291 
    292 int	mfs_pri = PWAIT | PCATCH;		/* XXX prob. temp */
    293 
    294 /*
    295  * Used to grab the process and keep it in the kernel to service
    296  * memory filesystem I/O requests.
    297  *
    298  * Loop servicing I/O requests.
    299  * Copy the requested data into or out of the memory filesystem
    300  * address space.
    301  */
    302 /* ARGSUSED */
    303 int
    304 mfs_start(mp, flags, p)
    305 	struct mount *mp;
    306 	int flags;
    307 	struct proc *p;
    308 {
    309 	struct vnode *vp = VFSTOUFS(mp)->um_devvp;
    310 	struct mfsnode *mfsp = VTOMFS(vp);
    311 	struct buf *bp;
    312 	caddr_t base;
    313 	int sleepreturn = 0;
    314 	struct lwp *l; /* XXX NJWLWP */
    315 
    316 	/* XXX NJWLWP the vnode interface again gives us a proc in a
    317 	 * place where we want a execution context. Cheat.
    318 	 */
    319 	KASSERT(curproc->l_proc == p);
    320 	l = curproc;
    321 	base = mfsp->mfs_baseoff;
    322 	while (BUFQ_FIRST(&mfsp->mfs_buflist) != (struct buf *) -1) {
    323 		while ((bp = BUFQ_FIRST(&mfsp->mfs_buflist)) != NULL) {
    324 			BUFQ_REMOVE(&mfsp->mfs_buflist, bp);
    325 			mfs_doio(bp, base);
    326 			wakeup((caddr_t)bp);
    327 		}
    328 		/*
    329 		 * If a non-ignored signal is received, try to unmount.
    330 		 * If that fails, or the filesystem is already in the
    331 		 * process of being unmounted, clear the signal (it has been
    332 		 * "processed"), otherwise we will loop here, as tsleep
    333 		 * will always return EINTR/ERESTART.  */
    334 		if (sleepreturn != 0) {
    335 			/*
    336 			 * XXX Freeze syncer.  Must do this before locking
    337 			 * the mount point.  See dounmount() for details.
    338 			 */
    339 			lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    340 			if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
    341 				lockmgr(&syncer_lock, LK_RELEASE, NULL);
    342 			else if (dounmount(mp, 0, p) != 0)
    343 				CLRSIG(p, CURSIG(l));
    344 			sleepreturn = 0;
    345 			continue;
    346 		}
    347 
    348 		sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
    349 	}
    350 	return (sleepreturn);
    351 }
    352 
    353 /*
    354  * Get file system statistics.
    355  */
    356 int
    357 mfs_statfs(mp, sbp, p)
    358 	struct mount *mp;
    359 	struct statfs *sbp;
    360 	struct proc *p;
    361 {
    362 	int error;
    363 
    364 	error = ffs_statfs(mp, sbp, p);
    365 #ifdef COMPAT_09
    366 	sbp->f_type = 3;
    367 #else
    368 	sbp->f_type = 0;
    369 #endif
    370 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    371 	return (error);
    372 }
    373