Home | History | Annotate | Line # | Download | only in mfs
mfs_vnops.c revision 1.55
      1  1.55  dholland /*	$NetBSD: mfs_vnops.c,v 1.55 2014/07/25 08:20:53 dholland Exp $	*/
      2  1.22   thorpej 
      3   1.1   mycroft /*
      4   1.1   mycroft  * Copyright (c) 1989, 1993
      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.34       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     17   1.1   mycroft  *    without specific prior written permission.
     18   1.1   mycroft  *
     19   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   mycroft  * SUCH DAMAGE.
     30   1.1   mycroft  *
     31  1.13      fvdl  *	@(#)mfs_vnops.c	8.11 (Berkeley) 5/22/95
     32   1.1   mycroft  */
     33  1.28     lukem 
     34  1.28     lukem #include <sys/cdefs.h>
     35  1.55  dholland __KERNEL_RCSID(0, "$NetBSD: mfs_vnops.c,v 1.55 2014/07/25 08:20:53 dholland Exp $");
     36   1.1   mycroft 
     37   1.1   mycroft #include <sys/param.h>
     38   1.1   mycroft #include <sys/systm.h>
     39   1.1   mycroft #include <sys/time.h>
     40   1.1   mycroft #include <sys/kernel.h>
     41   1.1   mycroft #include <sys/proc.h>
     42   1.1   mycroft #include <sys/buf.h>
     43  1.37      yamt #include <sys/bufq.h>
     44   1.1   mycroft #include <sys/vnode.h>
     45  1.49        ad #include <sys/kmem.h>
     46   1.1   mycroft 
     47   1.9   mycroft #include <miscfs/genfs/genfs.h>
     48   1.1   mycroft #include <miscfs/specfs/specdev.h>
     49   1.1   mycroft 
     50  1.23   thorpej #include <machine/vmparam.h>
     51  1.23   thorpej 
     52   1.1   mycroft #include <ufs/mfs/mfsnode.h>
     53   1.1   mycroft #include <ufs/mfs/mfs_extern.h>
     54   1.1   mycroft 
     55   1.1   mycroft /*
     56   1.1   mycroft  * mfs vnode operations.
     57   1.1   mycroft  */
     58  1.39   xtraeme int (**mfs_vnodeop_p)(void *);
     59  1.27  jdolecek const struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
     60   1.1   mycroft 	{ &vop_default_desc, vn_default_error },
     61   1.1   mycroft 	{ &vop_lookup_desc, mfs_lookup },		/* lookup */
     62   1.1   mycroft 	{ &vop_create_desc, mfs_create },		/* create */
     63   1.1   mycroft 	{ &vop_mknod_desc, mfs_mknod },			/* mknod */
     64   1.1   mycroft 	{ &vop_open_desc, mfs_open },			/* open */
     65   1.1   mycroft 	{ &vop_close_desc, mfs_close },			/* close */
     66   1.1   mycroft 	{ &vop_access_desc, mfs_access },		/* access */
     67   1.1   mycroft 	{ &vop_getattr_desc, mfs_getattr },		/* getattr */
     68   1.1   mycroft 	{ &vop_setattr_desc, mfs_setattr },		/* setattr */
     69   1.1   mycroft 	{ &vop_read_desc, mfs_read },			/* read */
     70   1.1   mycroft 	{ &vop_write_desc, mfs_write },			/* write */
     71  1.55  dholland 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
     72  1.55  dholland 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
     73   1.1   mycroft 	{ &vop_ioctl_desc, mfs_ioctl },			/* ioctl */
     74  1.10   mycroft 	{ &vop_poll_desc, mfs_poll },			/* poll */
     75  1.13      fvdl 	{ &vop_revoke_desc, mfs_revoke },		/* revoke */
     76   1.1   mycroft 	{ &vop_mmap_desc, mfs_mmap },			/* mmap */
     77  1.52  christos 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
     78   1.1   mycroft 	{ &vop_seek_desc, mfs_seek },			/* seek */
     79   1.1   mycroft 	{ &vop_remove_desc, mfs_remove },		/* remove */
     80   1.1   mycroft 	{ &vop_link_desc, mfs_link },			/* link */
     81   1.1   mycroft 	{ &vop_rename_desc, mfs_rename },		/* rename */
     82   1.1   mycroft 	{ &vop_mkdir_desc, mfs_mkdir },			/* mkdir */
     83   1.1   mycroft 	{ &vop_rmdir_desc, mfs_rmdir },			/* rmdir */
     84   1.1   mycroft 	{ &vop_symlink_desc, mfs_symlink },		/* symlink */
     85   1.1   mycroft 	{ &vop_readdir_desc, mfs_readdir },		/* readdir */
     86   1.1   mycroft 	{ &vop_readlink_desc, mfs_readlink },		/* readlink */
     87   1.1   mycroft 	{ &vop_abortop_desc, mfs_abortop },		/* abortop */
     88   1.1   mycroft 	{ &vop_inactive_desc, mfs_inactive },		/* inactive */
     89   1.1   mycroft 	{ &vop_reclaim_desc, mfs_reclaim },		/* reclaim */
     90  1.48        ad 	{ &vop_lock_desc, genfs_nolock },		/* lock */
     91  1.48        ad 	{ &vop_unlock_desc, genfs_nounlock },		/* unlock */
     92   1.1   mycroft 	{ &vop_bmap_desc, mfs_bmap },			/* bmap */
     93   1.1   mycroft 	{ &vop_strategy_desc, mfs_strategy },		/* strategy */
     94   1.1   mycroft 	{ &vop_print_desc, mfs_print },			/* print */
     95   1.1   mycroft 	{ &vop_islocked_desc, mfs_islocked },		/* islocked */
     96   1.1   mycroft 	{ &vop_pathconf_desc, mfs_pathconf },		/* pathconf */
     97   1.1   mycroft 	{ &vop_advlock_desc, mfs_advlock },		/* advlock */
     98   1.1   mycroft 	{ &vop_bwrite_desc, mfs_bwrite },		/* bwrite */
     99  1.29       chs 	{ &vop_putpages_desc, mfs_putpages },		/* putpages */
    100  1.29       chs 	{ NULL, NULL }
    101   1.1   mycroft };
    102  1.27  jdolecek const struct vnodeopv_desc mfs_vnodeop_opv_desc =
    103   1.1   mycroft 	{ &mfs_vnodeop_p, mfs_vnodeop_entries };
    104   1.1   mycroft 
    105   1.1   mycroft /*
    106   1.1   mycroft  * Vnode Operations.
    107   1.1   mycroft  *
    108   1.1   mycroft  * Open called to allow memory filesystem to initialize and
    109   1.1   mycroft  * validate before actual IO. Record our process identifier
    110   1.1   mycroft  * so we can tell when we are doing I/O to ourself.
    111   1.1   mycroft  */
    112   1.1   mycroft /* ARGSUSED */
    113   1.1   mycroft int
    114  1.39   xtraeme mfs_open(void *v)
    115   1.6  christos {
    116   1.1   mycroft 	struct vop_open_args /* {
    117   1.1   mycroft 		struct vnode *a_vp;
    118   1.1   mycroft 		int  a_mode;
    119  1.43      elad 		kauth_cred_t a_cred;
    120   1.6  christos 	} */ *ap = v;
    121   1.1   mycroft 
    122   1.1   mycroft 	if (ap->a_vp->v_type != VBLK) {
    123   1.1   mycroft 		panic("mfs_ioctl not VBLK");
    124   1.1   mycroft 		/* NOTREACHED */
    125   1.1   mycroft 	}
    126   1.1   mycroft 	return (0);
    127   1.1   mycroft }
    128   1.1   mycroft 
    129   1.1   mycroft /*
    130   1.1   mycroft  * Pass I/O requests to the memory filesystem process.
    131   1.1   mycroft  */
    132   1.1   mycroft int
    133  1.39   xtraeme mfs_strategy(void *v)
    134   1.6  christos {
    135   1.1   mycroft 	struct vop_strategy_args /* {
    136  1.36   hannken 		struct vnode *a_vp;
    137   1.1   mycroft 		struct buf *a_bp;
    138   1.6  christos 	} */ *ap = v;
    139  1.36   hannken 	struct vnode *vp = ap->a_vp;
    140  1.20  augustss 	struct buf *bp = ap->a_bp;
    141  1.20  augustss 	struct mfsnode *mfsp;
    142   1.1   mycroft 
    143  1.36   hannken 	if (vp->v_type != VBLK || vp->v_usecount == 0)
    144   1.1   mycroft 		panic("mfs_strategy: bad dev");
    145   1.1   mycroft 	mfsp = VTOMFS(vp);
    146  1.23   thorpej 	/* check for mini-root access */
    147  1.23   thorpej 	if (mfsp->mfs_proc == NULL) {
    148  1.44  christos 		void *base;
    149  1.22   thorpej 
    150  1.44  christos 		base = (char *)mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
    151   1.1   mycroft 		if (bp->b_flags & B_READ)
    152  1.14     perry 			memcpy(bp->b_data, base, bp->b_bcount);
    153   1.1   mycroft 		else
    154  1.14     perry 			memcpy(base, bp->b_data, bp->b_bcount);
    155  1.25   thorpej 		bp->b_resid = 0;
    156  1.23   thorpej 		biodone(bp);
    157  1.48        ad 	} else if (mfsp->mfs_proc == curproc) {
    158  1.23   thorpej 		mfs_doio(bp, mfsp->mfs_baseoff);
    159  1.24  sommerfe 	} else if (doing_shutdown) {
    160  1.38     perry 		/*
    161  1.24  sommerfe 		 * bitbucket I/O during shutdown.
    162  1.24  sommerfe 		 * Note that reads should *not* happen here, but..
    163  1.24  sommerfe 		 */
    164  1.24  sommerfe 		if (bp->b_flags & B_READ)
    165  1.24  sommerfe 			printf("warning: mfs read during shutdown\n");
    166  1.25   thorpej 		bp->b_resid = 0;
    167  1.24  sommerfe 		biodone(bp);
    168   1.1   mycroft 	} else {
    169  1.49        ad 		mutex_enter(&mfs_lock);
    170  1.53      yamt 		bufq_put(mfsp->mfs_buflist, bp);
    171  1.48        ad 		cv_broadcast(&mfsp->mfs_cv);
    172  1.49        ad 		mutex_exit(&mfs_lock);
    173  1.23   thorpej 	}
    174  1.23   thorpej 	return (0);
    175  1.23   thorpej }
    176  1.22   thorpej 
    177  1.23   thorpej /*
    178  1.23   thorpej  * Memory file system I/O.
    179  1.23   thorpej  */
    180  1.23   thorpej void
    181  1.44  christos mfs_doio(struct buf *bp, void *base)
    182  1.23   thorpej {
    183  1.48        ad 
    184  1.44  christos 	base = (char *)base + (bp->b_blkno << DEV_BSHIFT);
    185  1.23   thorpej 	if (bp->b_flags & B_READ)
    186  1.23   thorpej 		bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
    187  1.23   thorpej 	else
    188  1.23   thorpej 		bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
    189  1.45        ad 	if (bp->b_error == 0)
    190  1.16       chs 		bp->b_resid = 0;
    191   1.1   mycroft 	biodone(bp);
    192   1.1   mycroft }
    193   1.1   mycroft 
    194   1.1   mycroft /*
    195   1.1   mycroft  * This is a noop, simply returning what one has been given.
    196   1.1   mycroft  */
    197   1.1   mycroft int
    198  1.39   xtraeme mfs_bmap(void *v)
    199   1.6  christos {
    200   1.1   mycroft 	struct vop_bmap_args /* {
    201   1.1   mycroft 		struct vnode *a_vp;
    202   1.1   mycroft 		daddr_t  a_bn;
    203   1.1   mycroft 		struct vnode **a_vpp;
    204   1.1   mycroft 		daddr_t *a_bnp;
    205   1.1   mycroft 		int *a_runp;
    206   1.6  christos 	} */ *ap = v;
    207   1.1   mycroft 
    208   1.1   mycroft 	if (ap->a_vpp != NULL)
    209   1.1   mycroft 		*ap->a_vpp = ap->a_vp;
    210   1.1   mycroft 	if (ap->a_bnp != NULL)
    211   1.1   mycroft 		*ap->a_bnp = ap->a_bn;
    212  1.13      fvdl 	if (ap->a_runp != NULL)
    213  1.13      fvdl 		 *ap->a_runp = 0;
    214   1.1   mycroft 	return (0);
    215   1.1   mycroft }
    216   1.1   mycroft 
    217   1.1   mycroft /*
    218   1.1   mycroft  * Memory filesystem close routine
    219   1.1   mycroft  */
    220   1.1   mycroft /* ARGSUSED */
    221   1.1   mycroft int
    222  1.39   xtraeme mfs_close(void *v)
    223   1.6  christos {
    224   1.1   mycroft 	struct vop_close_args /* {
    225   1.1   mycroft 		struct vnode *a_vp;
    226   1.1   mycroft 		int  a_fflag;
    227  1.43      elad 		kauth_cred_t a_cred;
    228   1.6  christos 	} */ *ap = v;
    229  1.20  augustss 	struct vnode *vp = ap->a_vp;
    230  1.20  augustss 	struct mfsnode *mfsp = VTOMFS(vp);
    231  1.23   thorpej 	struct buf *bp;
    232   1.1   mycroft 	int error;
    233   1.1   mycroft 
    234   1.1   mycroft 	/*
    235  1.23   thorpej 	 * Finish any pending I/O requests.
    236  1.23   thorpej 	 */
    237  1.49        ad 	mutex_enter(&mfs_lock);
    238  1.53      yamt 	while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
    239  1.49        ad 		mutex_exit(&mfs_lock);
    240  1.23   thorpej 		mfs_doio(bp, mfsp->mfs_baseoff);
    241  1.49        ad 		mutex_enter(&mfs_lock);
    242  1.23   thorpej 	}
    243  1.49        ad 	mutex_exit(&mfs_lock);
    244  1.23   thorpej 	/*
    245   1.1   mycroft 	 * On last close of a memory filesystem
    246   1.1   mycroft 	 * we must invalidate any in core blocks, so that
    247   1.1   mycroft 	 * we can, free up its vnode.
    248   1.1   mycroft 	 */
    249  1.46     pooka 	if ((error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0)) != 0)
    250   1.1   mycroft 		return (error);
    251   1.1   mycroft 	/*
    252   1.1   mycroft 	 * There should be no way to have any more uses of this
    253   1.1   mycroft 	 * vnode, so if we find any other uses, it is a panic.
    254   1.1   mycroft 	 */
    255  1.53      yamt 	if (bufq_peek(mfsp->mfs_buflist) != NULL)
    256   1.1   mycroft 		panic("mfs_close");
    257   1.1   mycroft 	/*
    258   1.1   mycroft 	 * Send a request to the filesystem server to exit.
    259   1.1   mycroft 	 */
    260  1.49        ad 	mutex_enter(&mfs_lock);
    261  1.30   hannken 	mfsp->mfs_shutdown = 1;
    262  1.48        ad 	cv_broadcast(&mfsp->mfs_cv);
    263  1.49        ad 	mutex_exit(&mfs_lock);
    264   1.1   mycroft 	return (0);
    265   1.1   mycroft }
    266   1.1   mycroft 
    267   1.1   mycroft /*
    268   1.1   mycroft  * Memory filesystem inactive routine
    269   1.1   mycroft  */
    270   1.1   mycroft /* ARGSUSED */
    271   1.1   mycroft int
    272  1.39   xtraeme mfs_inactive(void *v)
    273   1.6  christos {
    274   1.1   mycroft 	struct vop_inactive_args /* {
    275   1.1   mycroft 		struct vnode *a_vp;
    276   1.6  christos 	} */ *ap = v;
    277  1.13      fvdl 	struct vnode *vp = ap->a_vp;
    278  1.13      fvdl 	struct mfsnode *mfsp = VTOMFS(vp);
    279   1.1   mycroft 
    280  1.53      yamt 	if (bufq_peek(mfsp->mfs_buflist) != NULL)
    281   1.8  christos 		panic("mfs_inactive: not inactive (mfs_buflist %p)",
    282  1.53      yamt 			bufq_peek(mfsp->mfs_buflist));
    283  1.54   hannken 	VOP_UNLOCK(vp);
    284   1.1   mycroft 	return (0);
    285   1.1   mycroft }
    286   1.1   mycroft 
    287   1.1   mycroft /*
    288   1.1   mycroft  * Reclaim a memory filesystem devvp so that it can be reused.
    289   1.1   mycroft  */
    290   1.1   mycroft int
    291  1.39   xtraeme mfs_reclaim(void *v)
    292   1.6  christos {
    293   1.1   mycroft 	struct vop_reclaim_args /* {
    294   1.1   mycroft 		struct vnode *a_vp;
    295   1.6  christos 	} */ *ap = v;
    296  1.20  augustss 	struct vnode *vp = ap->a_vp;
    297  1.48        ad 	struct mfsnode *mfsp = VTOMFS(vp);
    298  1.49        ad 	int refcnt;
    299  1.48        ad 
    300  1.49        ad 	mutex_enter(&mfs_lock);
    301  1.49        ad 	vp->v_data = NULL;
    302  1.49        ad 	refcnt = --mfsp->mfs_refcnt;
    303  1.49        ad 	mutex_exit(&mfs_lock);
    304  1.49        ad 
    305  1.49        ad 	if (refcnt == 0) {
    306  1.49        ad 		bufq_free(mfsp->mfs_buflist);
    307  1.49        ad 		cv_destroy(&mfsp->mfs_cv);
    308  1.49        ad 		kmem_free(mfsp, sizeof(*mfsp));
    309  1.49        ad 	}
    310   1.1   mycroft 
    311   1.1   mycroft 	return (0);
    312   1.1   mycroft }
    313   1.1   mycroft 
    314   1.1   mycroft /*
    315   1.1   mycroft  * Print out the contents of an mfsnode.
    316   1.1   mycroft  */
    317   1.1   mycroft int
    318  1.39   xtraeme mfs_print(void *v)
    319   1.6  christos {
    320   1.1   mycroft 	struct vop_print_args /* {
    321   1.1   mycroft 		struct vnode *a_vp;
    322   1.6  christos 	} */ *ap = v;
    323  1.20  augustss 	struct mfsnode *mfsp = VTOMFS(ap->a_vp);
    324   1.1   mycroft 
    325  1.21   thorpej 	printf("tag VT_MFS, pid %d, base %p, size %ld\n",
    326  1.21   thorpej 	    (mfsp->mfs_proc != NULL) ? mfsp->mfs_proc->p_pid : 0,
    327   1.8  christos 	    mfsp->mfs_baseoff, mfsp->mfs_size);
    328   1.1   mycroft 	return (0);
    329   1.1   mycroft }
    330