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