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