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