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