Home | History | Annotate | Line # | Download | only in mfs
mfs_vnops.c revision 1.61
      1  1.61  christos /*	$NetBSD: mfs_vnops.c,v 1.61 2020/05/16 18:31:54 christos 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.61  christos __KERNEL_RCSID(0, "$NetBSD: mfs_vnops.c,v 1.61 2020/05/16 18:31:54 christos 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.61  christos 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
     68   1.1   mycroft 	{ &vop_getattr_desc, mfs_getattr },		/* getattr */
     69   1.1   mycroft 	{ &vop_setattr_desc, mfs_setattr },		/* setattr */
     70   1.1   mycroft 	{ &vop_read_desc, mfs_read },			/* read */
     71   1.1   mycroft 	{ &vop_write_desc, mfs_write },			/* write */
     72  1.55  dholland 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
     73  1.55  dholland 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
     74   1.1   mycroft 	{ &vop_ioctl_desc, mfs_ioctl },			/* ioctl */
     75  1.10   mycroft 	{ &vop_poll_desc, mfs_poll },			/* poll */
     76  1.13      fvdl 	{ &vop_revoke_desc, mfs_revoke },		/* revoke */
     77   1.1   mycroft 	{ &vop_mmap_desc, mfs_mmap },			/* mmap */
     78  1.52  christos 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
     79   1.1   mycroft 	{ &vop_seek_desc, mfs_seek },			/* seek */
     80   1.1   mycroft 	{ &vop_remove_desc, mfs_remove },		/* remove */
     81   1.1   mycroft 	{ &vop_link_desc, mfs_link },			/* link */
     82   1.1   mycroft 	{ &vop_rename_desc, mfs_rename },		/* rename */
     83   1.1   mycroft 	{ &vop_mkdir_desc, mfs_mkdir },			/* mkdir */
     84   1.1   mycroft 	{ &vop_rmdir_desc, mfs_rmdir },			/* rmdir */
     85   1.1   mycroft 	{ &vop_symlink_desc, mfs_symlink },		/* symlink */
     86   1.1   mycroft 	{ &vop_readdir_desc, mfs_readdir },		/* readdir */
     87   1.1   mycroft 	{ &vop_readlink_desc, mfs_readlink },		/* readlink */
     88   1.1   mycroft 	{ &vop_abortop_desc, mfs_abortop },		/* abortop */
     89   1.1   mycroft 	{ &vop_inactive_desc, mfs_inactive },		/* inactive */
     90   1.1   mycroft 	{ &vop_reclaim_desc, mfs_reclaim },		/* reclaim */
     91  1.48        ad 	{ &vop_lock_desc, genfs_nolock },		/* lock */
     92  1.48        ad 	{ &vop_unlock_desc, genfs_nounlock },		/* unlock */
     93   1.1   mycroft 	{ &vop_bmap_desc, mfs_bmap },			/* bmap */
     94   1.1   mycroft 	{ &vop_strategy_desc, mfs_strategy },		/* strategy */
     95   1.1   mycroft 	{ &vop_print_desc, mfs_print },			/* print */
     96   1.1   mycroft 	{ &vop_islocked_desc, mfs_islocked },		/* islocked */
     97   1.1   mycroft 	{ &vop_pathconf_desc, mfs_pathconf },		/* pathconf */
     98   1.1   mycroft 	{ &vop_advlock_desc, mfs_advlock },		/* advlock */
     99   1.1   mycroft 	{ &vop_bwrite_desc, mfs_bwrite },		/* bwrite */
    100  1.29       chs 	{ &vop_putpages_desc, mfs_putpages },		/* putpages */
    101  1.29       chs 	{ NULL, NULL }
    102   1.1   mycroft };
    103  1.27  jdolecek const struct vnodeopv_desc mfs_vnodeop_opv_desc =
    104   1.1   mycroft 	{ &mfs_vnodeop_p, mfs_vnodeop_entries };
    105   1.1   mycroft 
    106   1.1   mycroft /*
    107   1.1   mycroft  * Vnode Operations.
    108   1.1   mycroft  *
    109   1.1   mycroft  * Open called to allow memory filesystem to initialize and
    110   1.1   mycroft  * validate before actual IO. Record our process identifier
    111   1.1   mycroft  * so we can tell when we are doing I/O to ourself.
    112   1.1   mycroft  */
    113   1.1   mycroft /* ARGSUSED */
    114   1.1   mycroft int
    115  1.39   xtraeme mfs_open(void *v)
    116   1.6  christos {
    117   1.1   mycroft 	struct vop_open_args /* {
    118   1.1   mycroft 		struct vnode *a_vp;
    119   1.1   mycroft 		int  a_mode;
    120  1.43      elad 		kauth_cred_t a_cred;
    121   1.6  christos 	} */ *ap = v;
    122   1.1   mycroft 
    123   1.1   mycroft 	if (ap->a_vp->v_type != VBLK) {
    124   1.1   mycroft 		panic("mfs_ioctl not VBLK");
    125   1.1   mycroft 		/* NOTREACHED */
    126   1.1   mycroft 	}
    127   1.1   mycroft 	return (0);
    128   1.1   mycroft }
    129   1.1   mycroft 
    130   1.1   mycroft /*
    131   1.1   mycroft  * Pass I/O requests to the memory filesystem process.
    132   1.1   mycroft  */
    133   1.1   mycroft int
    134  1.39   xtraeme mfs_strategy(void *v)
    135   1.6  christos {
    136   1.1   mycroft 	struct vop_strategy_args /* {
    137  1.36   hannken 		struct vnode *a_vp;
    138   1.1   mycroft 		struct buf *a_bp;
    139   1.6  christos 	} */ *ap = v;
    140  1.36   hannken 	struct vnode *vp = ap->a_vp;
    141  1.20  augustss 	struct buf *bp = ap->a_bp;
    142  1.20  augustss 	struct mfsnode *mfsp;
    143   1.1   mycroft 
    144  1.60        ad 	if (vp->v_type != VBLK || vrefcnt(vp) == 0)
    145   1.1   mycroft 		panic("mfs_strategy: bad dev");
    146   1.1   mycroft 	mfsp = VTOMFS(vp);
    147  1.23   thorpej 	/* check for mini-root access */
    148  1.23   thorpej 	if (mfsp->mfs_proc == NULL) {
    149  1.44  christos 		void *base;
    150  1.22   thorpej 
    151  1.44  christos 		base = (char *)mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
    152   1.1   mycroft 		if (bp->b_flags & B_READ)
    153  1.14     perry 			memcpy(bp->b_data, base, bp->b_bcount);
    154   1.1   mycroft 		else
    155  1.14     perry 			memcpy(base, bp->b_data, bp->b_bcount);
    156  1.25   thorpej 		bp->b_resid = 0;
    157  1.23   thorpej 		biodone(bp);
    158  1.48        ad 	} else if (mfsp->mfs_proc == curproc) {
    159  1.23   thorpej 		mfs_doio(bp, mfsp->mfs_baseoff);
    160  1.24  sommerfe 	} else if (doing_shutdown) {
    161  1.38     perry 		/*
    162  1.24  sommerfe 		 * bitbucket I/O during shutdown.
    163  1.24  sommerfe 		 * Note that reads should *not* happen here, but..
    164  1.24  sommerfe 		 */
    165  1.24  sommerfe 		if (bp->b_flags & B_READ)
    166  1.24  sommerfe 			printf("warning: mfs read during shutdown\n");
    167  1.25   thorpej 		bp->b_resid = 0;
    168  1.24  sommerfe 		biodone(bp);
    169   1.1   mycroft 	} else {
    170  1.49        ad 		mutex_enter(&mfs_lock);
    171  1.53      yamt 		bufq_put(mfsp->mfs_buflist, bp);
    172  1.48        ad 		cv_broadcast(&mfsp->mfs_cv);
    173  1.49        ad 		mutex_exit(&mfs_lock);
    174  1.23   thorpej 	}
    175  1.23   thorpej 	return (0);
    176  1.23   thorpej }
    177  1.22   thorpej 
    178  1.23   thorpej /*
    179  1.23   thorpej  * Memory file system I/O.
    180  1.23   thorpej  */
    181  1.23   thorpej void
    182  1.44  christos mfs_doio(struct buf *bp, void *base)
    183  1.23   thorpej {
    184  1.48        ad 
    185  1.44  christos 	base = (char *)base + (bp->b_blkno << DEV_BSHIFT);
    186  1.23   thorpej 	if (bp->b_flags & B_READ)
    187  1.23   thorpej 		bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
    188  1.23   thorpej 	else
    189  1.23   thorpej 		bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
    190  1.45        ad 	if (bp->b_error == 0)
    191  1.16       chs 		bp->b_resid = 0;
    192   1.1   mycroft 	biodone(bp);
    193   1.1   mycroft }
    194   1.1   mycroft 
    195   1.1   mycroft /*
    196   1.1   mycroft  * This is a noop, simply returning what one has been given.
    197   1.1   mycroft  */
    198   1.1   mycroft int
    199  1.39   xtraeme mfs_bmap(void *v)
    200   1.6  christos {
    201   1.1   mycroft 	struct vop_bmap_args /* {
    202   1.1   mycroft 		struct vnode *a_vp;
    203   1.1   mycroft 		daddr_t  a_bn;
    204   1.1   mycroft 		struct vnode **a_vpp;
    205   1.1   mycroft 		daddr_t *a_bnp;
    206   1.1   mycroft 		int *a_runp;
    207   1.6  christos 	} */ *ap = v;
    208   1.1   mycroft 
    209   1.1   mycroft 	if (ap->a_vpp != NULL)
    210   1.1   mycroft 		*ap->a_vpp = ap->a_vp;
    211   1.1   mycroft 	if (ap->a_bnp != NULL)
    212   1.1   mycroft 		*ap->a_bnp = ap->a_bn;
    213  1.13      fvdl 	if (ap->a_runp != NULL)
    214  1.13      fvdl 		 *ap->a_runp = 0;
    215   1.1   mycroft 	return (0);
    216   1.1   mycroft }
    217   1.1   mycroft 
    218   1.1   mycroft /*
    219   1.1   mycroft  * Memory filesystem close routine
    220   1.1   mycroft  */
    221   1.1   mycroft /* ARGSUSED */
    222   1.1   mycroft int
    223  1.39   xtraeme mfs_close(void *v)
    224   1.6  christos {
    225   1.1   mycroft 	struct vop_close_args /* {
    226   1.1   mycroft 		struct vnode *a_vp;
    227   1.1   mycroft 		int  a_fflag;
    228  1.43      elad 		kauth_cred_t a_cred;
    229   1.6  christos 	} */ *ap = v;
    230  1.20  augustss 	struct vnode *vp = ap->a_vp;
    231  1.20  augustss 	struct mfsnode *mfsp = VTOMFS(vp);
    232  1.23   thorpej 	struct buf *bp;
    233   1.1   mycroft 	int error;
    234   1.1   mycroft 
    235   1.1   mycroft 	/*
    236  1.23   thorpej 	 * Finish any pending I/O requests.
    237  1.23   thorpej 	 */
    238  1.49        ad 	mutex_enter(&mfs_lock);
    239  1.53      yamt 	while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
    240  1.49        ad 		mutex_exit(&mfs_lock);
    241  1.23   thorpej 		mfs_doio(bp, mfsp->mfs_baseoff);
    242  1.49        ad 		mutex_enter(&mfs_lock);
    243  1.23   thorpej 	}
    244  1.49        ad 	mutex_exit(&mfs_lock);
    245  1.23   thorpej 	/*
    246   1.1   mycroft 	 * On last close of a memory filesystem
    247   1.1   mycroft 	 * we must invalidate any in core blocks, so that
    248   1.1   mycroft 	 * we can, free up its vnode.
    249   1.1   mycroft 	 */
    250  1.46     pooka 	if ((error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0)) != 0)
    251   1.1   mycroft 		return (error);
    252   1.1   mycroft 	/*
    253   1.1   mycroft 	 * There should be no way to have any more uses of this
    254   1.1   mycroft 	 * vnode, so if we find any other uses, it is a panic.
    255   1.1   mycroft 	 */
    256  1.53      yamt 	if (bufq_peek(mfsp->mfs_buflist) != NULL)
    257   1.1   mycroft 		panic("mfs_close");
    258   1.1   mycroft 	/*
    259   1.1   mycroft 	 * Send a request to the filesystem server to exit.
    260   1.1   mycroft 	 */
    261  1.49        ad 	mutex_enter(&mfs_lock);
    262  1.30   hannken 	mfsp->mfs_shutdown = 1;
    263  1.48        ad 	cv_broadcast(&mfsp->mfs_cv);
    264  1.49        ad 	mutex_exit(&mfs_lock);
    265   1.1   mycroft 	return (0);
    266   1.1   mycroft }
    267   1.1   mycroft 
    268   1.1   mycroft /*
    269   1.1   mycroft  * Memory filesystem inactive routine
    270   1.1   mycroft  */
    271   1.1   mycroft /* ARGSUSED */
    272   1.1   mycroft int
    273  1.39   xtraeme mfs_inactive(void *v)
    274   1.6  christos {
    275  1.57  riastrad 	struct vop_inactive_v2_args /* {
    276   1.1   mycroft 		struct vnode *a_vp;
    277   1.6  christos 	} */ *ap = v;
    278  1.13      fvdl 	struct vnode *vp = ap->a_vp;
    279  1.13      fvdl 	struct mfsnode *mfsp = VTOMFS(vp);
    280   1.1   mycroft 
    281  1.53      yamt 	if (bufq_peek(mfsp->mfs_buflist) != NULL)
    282   1.8  christos 		panic("mfs_inactive: not inactive (mfs_buflist %p)",
    283  1.53      yamt 			bufq_peek(mfsp->mfs_buflist));
    284  1.56   hannken 
    285  1.56   hannken 	return VOCALL(spec_vnodeop_p,  VOFFSET(vop_inactive), ap);
    286   1.1   mycroft }
    287   1.1   mycroft 
    288   1.1   mycroft /*
    289   1.1   mycroft  * Reclaim a memory filesystem devvp so that it can be reused.
    290   1.1   mycroft  */
    291   1.1   mycroft int
    292  1.39   xtraeme mfs_reclaim(void *v)
    293   1.6  christos {
    294  1.58  riastrad 	struct vop_reclaim_v2_args /* {
    295   1.1   mycroft 		struct vnode *a_vp;
    296   1.6  christos 	} */ *ap = v;
    297  1.20  augustss 	struct vnode *vp = ap->a_vp;
    298  1.48        ad 	struct mfsnode *mfsp = VTOMFS(vp);
    299  1.49        ad 	int refcnt;
    300  1.48        ad 
    301  1.49        ad 	mutex_enter(&mfs_lock);
    302  1.49        ad 	vp->v_data = NULL;
    303  1.49        ad 	refcnt = --mfsp->mfs_refcnt;
    304  1.49        ad 	mutex_exit(&mfs_lock);
    305  1.49        ad 
    306  1.49        ad 	if (refcnt == 0) {
    307  1.49        ad 		bufq_free(mfsp->mfs_buflist);
    308  1.49        ad 		cv_destroy(&mfsp->mfs_cv);
    309  1.49        ad 		kmem_free(mfsp, sizeof(*mfsp));
    310  1.49        ad 	}
    311   1.1   mycroft 
    312  1.56   hannken 	return VOCALL(spec_vnodeop_p,  VOFFSET(vop_reclaim), ap);
    313   1.1   mycroft }
    314   1.1   mycroft 
    315   1.1   mycroft /*
    316   1.1   mycroft  * Print out the contents of an mfsnode.
    317   1.1   mycroft  */
    318   1.1   mycroft int
    319  1.39   xtraeme mfs_print(void *v)
    320   1.6  christos {
    321   1.1   mycroft 	struct vop_print_args /* {
    322   1.1   mycroft 		struct vnode *a_vp;
    323   1.6  christos 	} */ *ap = v;
    324  1.20  augustss 	struct mfsnode *mfsp = VTOMFS(ap->a_vp);
    325   1.1   mycroft 
    326  1.21   thorpej 	printf("tag VT_MFS, pid %d, base %p, size %ld\n",
    327  1.21   thorpej 	    (mfsp->mfs_proc != NULL) ? mfsp->mfs_proc->p_pid : 0,
    328   1.8  christos 	    mfsp->mfs_baseoff, mfsp->mfs_size);
    329   1.1   mycroft 	return (0);
    330   1.1   mycroft }
    331