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