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