Home | History | Annotate | Line # | Download | only in nullfs
null_vnops.c revision 1.33
      1  1.33   xtraeme /*	$NetBSD: null_vnops.c,v 1.33 2005/08/30 20:08:01 xtraeme Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4  1.16  wrstuden  * Copyright (c) 1999 National Aeronautics & Space Administration
      5  1.16  wrstuden  * All rights reserved.
      6  1.16  wrstuden  *
      7  1.16  wrstuden  * This software was written by William Studenmund of the
      8  1.19       wiz  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
      9  1.16  wrstuden  *
     10  1.16  wrstuden  * Redistribution and use in source and binary forms, with or without
     11  1.16  wrstuden  * modification, are permitted provided that the following conditions
     12  1.16  wrstuden  * are met:
     13  1.16  wrstuden  * 1. Redistributions of source code must retain the above copyright
     14  1.16  wrstuden  *    notice, this list of conditions and the following disclaimer.
     15  1.16  wrstuden  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.16  wrstuden  *    notice, this list of conditions and the following disclaimer in the
     17  1.16  wrstuden  *    documentation and/or other materials provided with the distribution.
     18  1.17     soren  * 3. Neither the name of the National Aeronautics & Space Administration
     19  1.16  wrstuden  *    nor the names of its contributors may be used to endorse or promote
     20  1.16  wrstuden  *    products derived from this software without specific prior written
     21  1.16  wrstuden  *    permission.
     22  1.16  wrstuden  *
     23  1.16  wrstuden  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24  1.16  wrstuden  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.16  wrstuden  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.16  wrstuden  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27  1.16  wrstuden  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28  1.16  wrstuden  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.16  wrstuden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.16  wrstuden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.16  wrstuden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.16  wrstuden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.16  wrstuden  * POSSIBILITY OF SUCH DAMAGE.
     34  1.16  wrstuden  */
     35  1.16  wrstuden /*
     36   1.1   mycroft  * Copyright (c) 1992, 1993
     37   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     38   1.1   mycroft  *
     39   1.1   mycroft  * This code is derived from software contributed to Berkeley by
     40   1.1   mycroft  * John Heidemann of the UCLA Ficus project.
     41   1.1   mycroft  *
     42   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43   1.1   mycroft  * modification, are permitted provided that the following conditions
     44   1.1   mycroft  * are met:
     45   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50  1.27       agc  * 3. Neither the name of the University nor the names of its contributors
     51   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     52   1.1   mycroft  *    without specific prior written permission.
     53   1.1   mycroft  *
     54   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1   mycroft  * SUCH DAMAGE.
     65   1.1   mycroft  *
     66  1.13      fvdl  *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
     67   1.1   mycroft  *
     68   1.1   mycroft  * Ancestors:
     69   1.1   mycroft  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
     70  1.32     perry  *      Id: lofs_vnops.c,v 1.11 1992/05/30 10:05:43 jsp Exp jsp
     71   1.1   mycroft  *	...and...
     72   1.1   mycroft  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
     73   1.1   mycroft  */
     74   1.1   mycroft 
     75   1.1   mycroft /*
     76   1.1   mycroft  * Null Layer
     77   1.1   mycroft  *
     78   1.1   mycroft  * (See mount_null(8) for more information.)
     79   1.1   mycroft  *
     80   1.1   mycroft  * The null layer duplicates a portion of the file system
     81   1.1   mycroft  * name space under a new name.  In this respect, it is
     82   1.1   mycroft  * similar to the loopback file system.  It differs from
     83   1.1   mycroft  * the loopback fs in two respects:  it is implemented using
     84  1.20       wiz  * a stackable layers technique, and its "null-nodes" stack above
     85   1.1   mycroft  * all lower-layer vnodes, not just over directory vnodes.
     86   1.1   mycroft  *
     87   1.1   mycroft  * The null layer has two purposes.  First, it serves as a demonstration
     88  1.20       wiz  * of layering by providing a layer which does nothing (it actually
     89   1.1   mycroft  * does everything the loopback file system does, which is slightly
     90  1.20       wiz  * more than nothing).  Second, the null layer can serve as a prototype
     91   1.1   mycroft  * layer.  Since it provides all necessary layer framework,
     92  1.19       wiz  * new file system layers can be created very easily by starting
     93   1.1   mycroft  * with a null layer.
     94   1.1   mycroft  *
     95  1.19       wiz  * The remainder of this comment examines the null layer as a basis
     96   1.1   mycroft  * for constructing new layers.
     97   1.1   mycroft  *
     98   1.1   mycroft  *
     99   1.1   mycroft  * INSTANTIATING NEW NULL LAYERS
    100   1.1   mycroft  *
    101   1.1   mycroft  * New null layers are created with mount_null(8).
    102  1.19       wiz  * mount_null(8) takes two arguments, the pathname
    103   1.1   mycroft  * of the lower vfs (target-pn) and the pathname where the null
    104   1.1   mycroft  * layer will appear in the namespace (alias-pn).  After
    105   1.1   mycroft  * the null layer is put into place, the contents
    106   1.1   mycroft  * of target-pn subtree will be aliased under alias-pn.
    107   1.1   mycroft  *
    108   1.1   mycroft  *
    109   1.1   mycroft  * OPERATION OF A NULL LAYER
    110   1.1   mycroft  *
    111   1.1   mycroft  * The null layer is the minimum file system layer,
    112   1.1   mycroft  * simply bypassing all possible operations to the lower layer
    113   1.1   mycroft  * for processing there.  The majority of its activity centers
    114  1.19       wiz  * on the bypass routine, through which nearly all vnode operations
    115   1.1   mycroft  * pass.
    116   1.1   mycroft  *
    117   1.1   mycroft  * The bypass routine accepts arbitrary vnode operations for
    118  1.19       wiz  * handling by the lower layer.  It begins by examining vnode
    119   1.1   mycroft  * operation arguments and replacing any null-nodes by their
    120  1.19       wiz  * lower-layer equivalents.  It then invokes the operation
    121   1.1   mycroft  * on the lower layer.  Finally, it replaces the null-nodes
    122  1.19       wiz  * in the arguments and, if a vnode is returned by the operation,
    123   1.1   mycroft  * stacks a null-node on top of the returned vnode.
    124   1.1   mycroft  *
    125  1.13      fvdl  * Although bypass handles most operations, vop_getattr, vop_lock,
    126  1.13      fvdl  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
    127  1.19       wiz  * bypassed. vop_getattr must change the fsid being returned.
    128  1.19       wiz  * vop_lock and vop_unlock must handle any locking for the
    129  1.13      fvdl  * current vnode as well as pass the lock request down.
    130  1.19       wiz  * vop_inactive and vop_reclaim are not bypassed so that
    131  1.19       wiz  * they can handle freeing null-layer specific data. vop_print
    132  1.13      fvdl  * is not bypassed to avoid excessive debugging information.
    133  1.13      fvdl  * Also, certain vnode operations change the locking state within
    134  1.13      fvdl  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
    135  1.13      fvdl  * and symlink). Ideally these operations should not change the
    136  1.13      fvdl  * lock state, but should be changed to let the caller of the
    137  1.13      fvdl  * function unlock them. Otherwise all intermediate vnode layers
    138  1.13      fvdl  * (such as union, umapfs, etc) must catch these functions to do
    139  1.13      fvdl  * the necessary locking at their layer.
    140   1.1   mycroft  *
    141   1.1   mycroft  *
    142   1.1   mycroft  * INSTANTIATING VNODE STACKS
    143   1.1   mycroft  *
    144   1.1   mycroft  * Mounting associates the null layer with a lower layer,
    145  1.19       wiz  * in effect stacking two VFSes.  Vnode stacks are instead
    146   1.1   mycroft  * created on demand as files are accessed.
    147   1.1   mycroft  *
    148   1.1   mycroft  * The initial mount creates a single vnode stack for the
    149   1.1   mycroft  * root of the new null layer.  All other vnode stacks
    150   1.1   mycroft  * are created as a result of vnode operations on
    151   1.1   mycroft  * this or other null vnode stacks.
    152   1.1   mycroft  *
    153  1.19       wiz  * New vnode stacks come into existence as a result of
    154  1.32     perry  * an operation which returns a vnode.
    155   1.1   mycroft  * The bypass routine stacks a null-node above the new
    156   1.1   mycroft  * vnode before returning it to the caller.
    157   1.1   mycroft  *
    158   1.1   mycroft  * For example, imagine mounting a null layer with
    159   1.1   mycroft  * "mount_null /usr/include /dev/layer/null".
    160   1.1   mycroft  * Changing directory to /dev/layer/null will assign
    161   1.1   mycroft  * the root null-node (which was created when the null layer was mounted).
    162   1.1   mycroft  * Now consider opening "sys".  A vop_lookup would be
    163   1.1   mycroft  * done on the root null-node.  This operation would bypass through
    164  1.32     perry  * to the lower layer which would return a vnode representing
    165  1.19       wiz  * the UFS "sys".  null_bypass then builds a null-node
    166   1.1   mycroft  * aliasing the UFS "sys" and returns this to the caller.
    167   1.1   mycroft  * Later operations on the null-node "sys" will repeat this
    168   1.1   mycroft  * process when constructing other vnode stacks.
    169   1.1   mycroft  *
    170   1.1   mycroft  *
    171   1.1   mycroft  * CREATING OTHER FILE SYSTEM LAYERS
    172   1.1   mycroft  *
    173   1.1   mycroft  * One of the easiest ways to construct new file system layers is to make
    174   1.1   mycroft  * a copy of the null layer, rename all files and variables, and
    175  1.19       wiz  * then begin modifying the copy.  sed(1) can be used to easily rename
    176   1.1   mycroft  * all variables.
    177   1.1   mycroft  *
    178  1.32     perry  * The umap layer is an example of a layer descended from the
    179   1.1   mycroft  * null layer.
    180   1.1   mycroft  *
    181   1.1   mycroft  *
    182   1.1   mycroft  * INVOKING OPERATIONS ON LOWER LAYERS
    183   1.1   mycroft  *
    184  1.32     perry  * There are two techniques to invoke operations on a lower layer
    185   1.1   mycroft  * when the operation cannot be completely bypassed.  Each method
    186   1.1   mycroft  * is appropriate in different situations.  In both cases,
    187   1.1   mycroft  * it is the responsibility of the aliasing layer to make
    188   1.1   mycroft  * the operation arguments "correct" for the lower layer
    189  1.19       wiz  * by mapping any vnode arguments to the lower layer.
    190   1.1   mycroft  *
    191   1.1   mycroft  * The first approach is to call the aliasing layer's bypass routine.
    192   1.1   mycroft  * This method is most suitable when you wish to invoke the operation
    193  1.21     enami  * currently being handled on the lower layer.  It has the advantage
    194   1.1   mycroft  * that the bypass routine already must do argument mapping.
    195   1.1   mycroft  * An example of this is null_getattrs in the null layer.
    196   1.1   mycroft  *
    197  1.19       wiz  * A second approach is to directly invoke vnode operations on
    198   1.1   mycroft  * the lower layer with the VOP_OPERATIONNAME interface.
    199   1.1   mycroft  * The advantage of this method is that it is easy to invoke
    200   1.1   mycroft  * arbitrary operations on the lower layer.  The disadvantage
    201  1.19       wiz  * is that vnode arguments must be manually mapped.
    202   1.1   mycroft  *
    203   1.1   mycroft  */
    204  1.22     lukem 
    205  1.22     lukem #include <sys/cdefs.h>
    206  1.33   xtraeme __KERNEL_RCSID(0, "$NetBSD: null_vnops.c,v 1.33 2005/08/30 20:08:01 xtraeme Exp $");
    207   1.1   mycroft 
    208   1.1   mycroft #include <sys/param.h>
    209   1.1   mycroft #include <sys/systm.h>
    210   1.1   mycroft #include <sys/proc.h>
    211   1.1   mycroft #include <sys/time.h>
    212   1.1   mycroft #include <sys/vnode.h>
    213   1.1   mycroft #include <sys/mount.h>
    214   1.1   mycroft #include <sys/namei.h>
    215   1.1   mycroft #include <sys/malloc.h>
    216   1.1   mycroft #include <sys/buf.h>
    217  1.16  wrstuden #include <miscfs/genfs/genfs.h>
    218   1.1   mycroft #include <miscfs/nullfs/null.h>
    219  1.16  wrstuden #include <miscfs/genfs/layer_extern.h>
    220   1.1   mycroft 
    221   1.1   mycroft /*
    222   1.1   mycroft  * Global vfs data structures
    223   1.1   mycroft  */
    224  1.33   xtraeme int (**null_vnodeop_p)(void *);
    225  1.18  jdolecek const struct vnodeopv_entry_desc null_vnodeop_entries[] = {
    226  1.16  wrstuden 	{ &vop_default_desc,  layer_bypass },
    227   1.1   mycroft 
    228  1.16  wrstuden 	{ &vop_lookup_desc,   layer_lookup },
    229  1.16  wrstuden 	{ &vop_setattr_desc,  layer_setattr },
    230  1.16  wrstuden 	{ &vop_getattr_desc,  layer_getattr },
    231  1.16  wrstuden 	{ &vop_access_desc,   layer_access },
    232  1.16  wrstuden 	{ &vop_lock_desc,     layer_lock },
    233  1.16  wrstuden 	{ &vop_unlock_desc,   layer_unlock },
    234  1.16  wrstuden 	{ &vop_islocked_desc, layer_islocked },
    235  1.16  wrstuden 	{ &vop_fsync_desc,    layer_fsync },
    236  1.16  wrstuden 	{ &vop_inactive_desc, layer_inactive },
    237  1.16  wrstuden 	{ &vop_reclaim_desc,  layer_reclaim },
    238  1.16  wrstuden 	{ &vop_print_desc,    layer_print },
    239  1.29  wrstuden 	{ &vop_remove_desc,   layer_remove },
    240  1.30      yamt 	{ &vop_rename_desc,   layer_rename },
    241  1.31   hannken 	{ &vop_rmdir_desc,    layer_rmdir },
    242  1.16  wrstuden 
    243  1.16  wrstuden 	{ &vop_open_desc,     layer_open },	/* mount option handling */
    244  1.16  wrstuden 
    245  1.16  wrstuden 	{ &vop_bwrite_desc,   layer_bwrite },
    246  1.16  wrstuden 	{ &vop_bmap_desc,     layer_bmap },
    247  1.25       chs 	{ &vop_getpages_desc, layer_getpages },
    248  1.24       chs 	{ &vop_putpages_desc, layer_putpages },
    249   1.1   mycroft 
    250  1.24       chs 	{ NULL, NULL }
    251   1.1   mycroft };
    252  1.18  jdolecek const struct vnodeopv_desc null_vnodeop_opv_desc =
    253   1.1   mycroft 	{ &null_vnodeop_p, null_vnodeop_entries };
    254