Sat Jan 3 04:38:07 2009 UTC ()
Clarify a comment


(dholland)
diff -r1.35 -r1.36 src/sys/miscfs/genfs/layer_vnops.c

cvs diff -r1.35 -r1.36 src/sys/miscfs/genfs/layer_vnops.c (switch to unified diff)

--- src/sys/miscfs/genfs/layer_vnops.c 2008/01/30 09:50:23 1.35
+++ src/sys/miscfs/genfs/layer_vnops.c 2009/01/03 04:38:07 1.36
@@ -1,994 +1,995 @@ @@ -1,994 +1,995 @@
1/* $NetBSD: layer_vnops.c,v 1.35 2008/01/30 09:50:23 ad Exp $ */ 1/* $NetBSD: layer_vnops.c,v 1.36 2009/01/03 04:38:07 dholland Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1999 National Aeronautics & Space Administration 4 * Copyright (c) 1999 National Aeronautics & Space Administration
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This software was written by William Studenmund of the 7 * This software was written by William Studenmund of the
8 * Numerical Aerospace Simulation Facility, NASA Ames Research Center. 8 * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the National Aeronautics & Space Administration 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 19 * nor the names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior written 20 * products derived from this software without specific prior written
21 * permission. 21 * permission.
22 * 22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION 23 * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 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 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB- 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27 * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 27 * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE. 33 * POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35/* 35/*
36 * Copyright (c) 1992, 1993 36 * Copyright (c) 1992, 1993
37 * The Regents of the University of California. All rights reserved. 37 * The Regents of the University of California. All rights reserved.
38 * 38 *
39 * This code is derived from software contributed to Berkeley by 39 * This code is derived from software contributed to Berkeley by
40 * John Heidemann of the UCLA Ficus project. 40 * John Heidemann of the UCLA Ficus project.
41 * 41 *
42 * Redistribution and use in source and binary forms, with or without 42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions 43 * modification, are permitted provided that the following conditions
44 * are met: 44 * are met:
45 * 1. Redistributions of source code must retain the above copyright 45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer. 46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright 47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the 48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution. 49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors 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 51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission. 52 * without specific prior written permission.
53 * 53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 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 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE. 64 * SUCH DAMAGE.
65 * 65 *
66 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95 66 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
67 * 67 *
68 * Ancestors: 68 * Ancestors:
69 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92 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 70 * Id: lofs_vnops.c,v 1.11 1992/05/30 10:05:43 jsp Exp jsp
71 * ...and... 71 * ...and...
72 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project 72 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
73 */ 73 */
74 74
75/* 75/*
76 * Null Layer vnode routines. 76 * Null Layer vnode routines.
77 * 77 *
78 * (See mount_null(8) for more information.) 78 * (See mount_null(8) for more information.)
79 * 79 *
80 * The layer.h, layer_extern.h, layer_vfs.c, and layer_vnops.c files provide 80 * The layer.h, layer_extern.h, layer_vfs.c, and layer_vnops.c files provide
81 * the core implementation of the null file system and most other stacked 81 * the core implementation of the null file system and most other stacked
82 * fs's. The description below refers to the null file system, but the 82 * fs's. The description below refers to the null file system, but the
83 * services provided by the layer* files are useful for all layered fs's. 83 * services provided by the layer* files are useful for all layered fs's.
84 * 84 *
85 * The null layer duplicates a portion of the file system 85 * The null layer duplicates a portion of the file system
86 * name space under a new name. In this respect, it is 86 * name space under a new name. In this respect, it is
87 * similar to the loopback file system. It differs from 87 * similar to the loopback file system. It differs from
88 * the loopback fs in two respects: it is implemented using 88 * the loopback fs in two respects: it is implemented using
89 * a stackable layers techniques, and it's "null-node"s stack above 89 * a stackable layers techniques, and it's "null-node"s stack above
90 * all lower-layer vnodes, not just over directory vnodes. 90 * all lower-layer vnodes, not just over directory vnodes.
91 * 91 *
92 * The null layer has two purposes. First, it serves as a demonstration 92 * The null layer has two purposes. First, it serves as a demonstration
93 * of layering by proving a layer which does nothing. (It actually 93 * of layering by proving a layer which does nothing. (It actually
94 * does everything the loopback file system does, which is slightly 94 * does everything the loopback file system does, which is slightly
95 * more than nothing.) Second, the null layer can serve as a prototype 95 * more than nothing.) Second, the null layer can serve as a prototype
96 * layer. Since it provides all necessary layer framework, 96 * layer. Since it provides all necessary layer framework,
97 * new file system layers can be created very easily be starting 97 * new file system layers can be created very easily be starting
98 * with a null layer. 98 * with a null layer.
99 * 99 *
100 * The remainder of the man page examines the null layer as a basis 100 * The remainder of the man page examines the null layer as a basis
101 * for constructing new layers. 101 * for constructing new layers.
102 * 102 *
103 * 103 *
104 * INSTANTIATING NEW NULL LAYERS 104 * INSTANTIATING NEW NULL LAYERS
105 * 105 *
106 * New null layers are created with mount_null(8). 106 * New null layers are created with mount_null(8).
107 * Mount_null(8) takes two arguments, the pathname 107 * Mount_null(8) takes two arguments, the pathname
108 * of the lower vfs (target-pn) and the pathname where the null 108 * of the lower vfs (target-pn) and the pathname where the null
109 * layer will appear in the namespace (alias-pn). After 109 * layer will appear in the namespace (alias-pn). After
110 * the null layer is put into place, the contents 110 * the null layer is put into place, the contents
111 * of target-pn subtree will be aliased under alias-pn. 111 * of target-pn subtree will be aliased under alias-pn.
112 * 112 *
113 * It is conceivable that other overlay filesystems will take different 113 * It is conceivable that other overlay filesystems will take different
114 * parameters. For instance, data migration or access controll layers might 114 * parameters. For instance, data migration or access controll layers might
115 * only take one pathname which will serve both as the target-pn and 115 * only take one pathname which will serve both as the target-pn and
116 * alias-pn described above. 116 * alias-pn described above.
117 * 117 *
118 * 118 *
119 * OPERATION OF A NULL LAYER 119 * OPERATION OF A NULL LAYER
120 * 120 *
121 * The null layer is the minimum file system layer, 121 * The null layer is the minimum file system layer,
122 * simply bypassing all possible operations to the lower layer 122 * simply bypassing all possible operations to the lower layer
123 * for processing there. The majority of its activity centers 123 * for processing there. The majority of its activity centers
124 * on the bypass routine, through which nearly all vnode operations 124 * on the bypass routine, through which nearly all vnode operations
125 * pass. 125 * pass.
126 * 126 *
127 * The bypass routine accepts arbitrary vnode operations for 127 * The bypass routine accepts arbitrary vnode operations for
128 * handling by the lower layer. It begins by examing vnode 128 * handling by the lower layer. It begins by examing vnode
129 * operation arguments and replacing any layered nodes by their 129 * operation arguments and replacing any layered nodes by their
130 * lower-layer equivalents. It then invokes the operation 130 * lower-layer equivalents. It then invokes the operation
131 * on the lower layer. Finally, it replaces the layered nodes 131 * on the lower layer. Finally, it replaces the layered nodes
132 * in the arguments and, if a vnode is return by the operation, 132 * in the arguments and, if a vnode is return by the operation,
133 * stacks a layered node on top of the returned vnode. 133 * stacks a layered node on top of the returned vnode.
134 * 134 *
135 * The bypass routine in this file, layer_bypass(), is suitable for use 135 * The bypass routine in this file, layer_bypass(), is suitable for use
136 * by many different layered filesystems. It can be used by multiple 136 * by many different layered filesystems. It can be used by multiple
137 * filesystems simultaneously. Alternatively, a layered fs may provide 137 * filesystems simultaneously. Alternatively, a layered fs may provide
138 * its own bypass routine, in which case layer_bypass() should be used as 138 * its own bypass routine, in which case layer_bypass() should be used as
139 * a model. For instance, the main functionality provided by umapfs, the user 139 * a model. For instance, the main functionality provided by umapfs, the user
140 * identity mapping file system, is handled by a custom bypass routine. 140 * identity mapping file system, is handled by a custom bypass routine.
141 * 141 *
142 * Typically a layered fs registers its selected bypass routine as the 142 * Typically a layered fs registers its selected bypass routine as the
143 * default vnode operation in its vnodeopv_entry_desc table. Additionally 143 * default vnode operation in its vnodeopv_entry_desc table. Additionally
144 * the filesystem must store the bypass entry point in the layerm_bypass 144 * the filesystem must store the bypass entry point in the layerm_bypass
145 * field of struct layer_mount. All other layer routines in this file will 145 * field of struct layer_mount. All other layer routines in this file will
146 * use the layerm_bypass routine. 146 * use the layerm_bypass routine.
147 * 147 *
148 * Although the bypass routine handles most operations outright, a number 148 * Although the bypass routine handles most operations outright, a number
149 * of operations are special cased, and handled by the layered fs. One 149 * of operations are special cased, and handled by the layered fs. One
150 * group, layer_setattr, layer_getattr, layer_access, layer_open, and 150 * group, layer_setattr, layer_getattr, layer_access, layer_open, and
151 * layer_fsync, perform layer-specific manipulation in addition to calling 151 * layer_fsync, perform layer-specific manipulation in addition to calling
152 * the bypass routine. The other group 152 * the bypass routine. The other group
153 153
154 * Although bypass handles most operations, vop_getattr, vop_lock, 154 * Although bypass handles most operations, vop_getattr, vop_lock,
155 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not 155 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
156 * bypassed. Vop_getattr must change the fsid being returned. 156 * bypassed. Vop_getattr must change the fsid being returned.
157 * Vop_lock and vop_unlock must handle any locking for the 157 * Vop_lock and vop_unlock must handle any locking for the
158 * current vnode as well as pass the lock request down. 158 * current vnode as well as pass the lock request down.
159 * Vop_inactive and vop_reclaim are not bypassed so that 159 * Vop_inactive and vop_reclaim are not bypassed so that
160 * they can handle freeing null-layer specific data. Vop_print 160 * they can handle freeing null-layer specific data. Vop_print
161 * is not bypassed to avoid excessive debugging information. 161 * is not bypassed to avoid excessive debugging information.
162 * Also, certain vnode operations change the locking state within 162 * Also, certain vnode operations change the locking state within
163 * the operation (create, mknod, remove, link, rename, mkdir, rmdir, 163 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
164 * and symlink). Ideally these operations should not change the 164 * and symlink). Ideally these operations should not change the
165 * lock state, but should be changed to let the caller of the 165 * lock state, but should be changed to let the caller of the
166 * function unlock them. Otherwise all intermediate vnode layers 166 * function unlock them. Otherwise all intermediate vnode layers
167 * (such as union, umapfs, etc) must catch these functions to do 167 * (such as union, umapfs, etc) must catch these functions to do
168 * the necessary locking at their layer. 168 * the necessary locking at their layer.
169 * 169 *
170 * 170 *
171 * INSTANTIATING VNODE STACKS 171 * INSTANTIATING VNODE STACKS
172 * 172 *
173 * Mounting associates the null layer with a lower layer, 173 * Mounting associates the null layer with a lower layer,
174 * effect stacking two VFSes. Vnode stacks are instead 174 * effect stacking two VFSes. Vnode stacks are instead
175 * created on demand as files are accessed. 175 * created on demand as files are accessed.
176 * 176 *
177 * The initial mount creates a single vnode stack for the 177 * The initial mount creates a single vnode stack for the
178 * root of the new null layer. All other vnode stacks 178 * root of the new null layer. All other vnode stacks
179 * are created as a result of vnode operations on 179 * are created as a result of vnode operations on
180 * this or other null vnode stacks. 180 * this or other null vnode stacks.
181 * 181 *
182 * New vnode stacks come into existence as a result of 182 * New vnode stacks come into existence as a result of
183 * an operation which returns a vnode. 183 * an operation which returns a vnode.
184 * The bypass routine stacks a null-node above the new 184 * The bypass routine stacks a null-node above the new
185 * vnode before returning it to the caller. 185 * vnode before returning it to the caller.
186 * 186 *
187 * For example, imagine mounting a null layer with 187 * For example, imagine mounting a null layer with
188 * "mount_null /usr/include /dev/layer/null". 188 * "mount_null /usr/include /dev/layer/null".
189 * Changing directory to /dev/layer/null will assign 189 * Changing directory to /dev/layer/null will assign
190 * the root null-node (which was created when the null layer was mounted). 190 * the root null-node (which was created when the null layer was mounted).
191 * Now consider opening "sys". A vop_lookup would be 191 * Now consider opening "sys". A vop_lookup would be
192 * done on the root null-node. This operation would bypass through 192 * done on the root null-node. This operation would bypass through
193 * to the lower layer which would return a vnode representing 193 * to the lower layer which would return a vnode representing
194 * the UFS "sys". layer_bypass then builds a null-node 194 * the UFS "sys". layer_bypass then builds a null-node
195 * aliasing the UFS "sys" and returns this to the caller. 195 * aliasing the UFS "sys" and returns this to the caller.
196 * Later operations on the null-node "sys" will repeat this 196 * Later operations on the null-node "sys" will repeat this
197 * process when constructing other vnode stacks. 197 * process when constructing other vnode stacks.
198 * 198 *
199 * 199 *
200 * CREATING OTHER FILE SYSTEM LAYERS 200 * CREATING OTHER FILE SYSTEM LAYERS
201 * 201 *
202 * One of the easiest ways to construct new file system layers is to make 202 * One of the easiest ways to construct new file system layers is to make
203 * a copy of the null layer, rename all files and variables, and 203 * a copy of the null layer, rename all files and variables, and
204 * then begin modifing the copy. Sed can be used to easily rename 204 * then begin modifing the copy. Sed can be used to easily rename
205 * all variables. 205 * all variables.
206 * 206 *
207 * The umap layer is an example of a layer descended from the 207 * The umap layer is an example of a layer descended from the
208 * null layer. 208 * null layer.
209 * 209 *
210 * 210 *
211 * INVOKING OPERATIONS ON LOWER LAYERS 211 * INVOKING OPERATIONS ON LOWER LAYERS
212 * 212 *
213 * There are two techniques to invoke operations on a lower layer 213 * There are two techniques to invoke operations on a lower layer
214 * when the operation cannot be completely bypassed. Each method 214 * when the operation cannot be completely bypassed. Each method
215 * is appropriate in different situations. In both cases, 215 * is appropriate in different situations. In both cases,
216 * it is the responsibility of the aliasing layer to make 216 * it is the responsibility of the aliasing layer to make
217 * the operation arguments "correct" for the lower layer 217 * the operation arguments "correct" for the lower layer
218 * by mapping an vnode arguments to the lower layer. 218 * by mapping an vnode arguments to the lower layer.
219 * 219 *
220 * The first approach is to call the aliasing layer's bypass routine. 220 * The first approach is to call the aliasing layer's bypass routine.
221 * This method is most suitable when you wish to invoke the operation 221 * This method is most suitable when you wish to invoke the operation
222 * currently being handled on the lower layer. It has the advantage 222 * currently being handled on the lower layer. It has the advantage
223 * that the bypass routine already must do argument mapping. 223 * that the bypass routine already must do argument mapping.
224 * An example of this is null_getattrs in the null layer. 224 * An example of this is null_getattrs in the null layer.
225 * 225 *
226 * A second approach is to directly invoke vnode operations on 226 * A second approach is to directly invoke vnode operations on
227 * the lower layer with the VOP_OPERATIONNAME interface. 227 * the lower layer with the VOP_OPERATIONNAME interface.
228 * The advantage of this method is that it is easy to invoke 228 * The advantage of this method is that it is easy to invoke
229 * arbitrary operations on the lower layer. The disadvantage 229 * arbitrary operations on the lower layer. The disadvantage
230 * is that vnodes' arguments must be manually mapped. 230 * is that vnodes' arguments must be manually mapped.
231 * 231 *
232 */ 232 */
233 233
234#include <sys/cdefs.h> 234#include <sys/cdefs.h>
235__KERNEL_RCSID(0, "$NetBSD: layer_vnops.c,v 1.35 2008/01/30 09:50:23 ad Exp $"); 235__KERNEL_RCSID(0, "$NetBSD: layer_vnops.c,v 1.36 2009/01/03 04:38:07 dholland Exp $");
236 236
237#include <sys/param.h> 237#include <sys/param.h>
238#include <sys/systm.h> 238#include <sys/systm.h>
239#include <sys/proc.h> 239#include <sys/proc.h>
240#include <sys/time.h> 240#include <sys/time.h>
241#include <sys/vnode.h> 241#include <sys/vnode.h>
242#include <sys/mount.h> 242#include <sys/mount.h>
243#include <sys/namei.h> 243#include <sys/namei.h>
244#include <sys/kmem.h> 244#include <sys/kmem.h>
245#include <sys/buf.h> 245#include <sys/buf.h>
246#include <sys/kauth.h> 246#include <sys/kauth.h>
247 247
248#include <miscfs/genfs/layer.h> 248#include <miscfs/genfs/layer.h>
249#include <miscfs/genfs/layer_extern.h> 249#include <miscfs/genfs/layer_extern.h>
250#include <miscfs/genfs/genfs.h> 250#include <miscfs/genfs/genfs.h>
251 251
252 252
253/* 253/*
254 * This is the 08-June-99 bypass routine, based on the 10-Apr-92 bypass 254 * This is the 08-June-99 bypass routine, based on the 10-Apr-92 bypass
255 * routine by John Heidemann. 255 * routine by John Heidemann.
256 * The new element for this version is that the whole nullfs 256 * The new element for this version is that the whole nullfs
257 * system gained the concept of locks on the lower node, and locks on 257 * system gained the concept of locks on the lower node, and locks on
258 * our nodes. When returning from a call to the lower layer, we may 258 * our nodes. When returning from a call to the lower layer, we may
259 * need to update lock state ONLY on our layer. The LAYERFS_UPPER*LOCK() 259 * need to update lock state ONLY on our layer. The LAYERFS_UPPER*LOCK()
260 * macros provide this functionality. 260 * macros provide this functionality.
261 * The 10-Apr-92 version was optimized for speed, throwing away some 261 * The 10-Apr-92 version was optimized for speed, throwing away some
262 * safety checks. It should still always work, but it's not as 262 * safety checks. It should still always work, but it's not as
263 * robust to programmer errors. 263 * robust to programmer errors.
264 * Define SAFETY to include some error checking code. 264 * Define SAFETY to include some error checking code.
265 * 265 *
266 * In general, we map all vnodes going down and unmap them on the way back. 266 * In general, we map all vnodes going down and unmap them on the way back.
267 * 267 *
268 * Also, some BSD vnode operations have the side effect of vrele'ing 268 * Also, some BSD vnode operations have the side effect of vrele'ing
269 * their arguments. With stacking, the reference counts are held 269 * their arguments. With stacking, the reference counts are held
270 * by the upper node, not the lower one, so we must handle these 270 * by the upper node, not the lower one, so we must handle these
271 * side-effects here. This is not of concern in Sun-derived systems 271 * side-effects here. This is not of concern in Sun-derived systems
272 * since there are no such side-effects. 272 * since there are no such side-effects.
273 * 273 *
274 * New for the 08-June-99 version: we also handle operations which unlock 274 * New for the 08-June-99 version: we also handle operations which unlock
275 * the passed-in node (typically they vput the node). 275 * the passed-in node (typically they vput the node).
276 * 276 *
277 * This makes the following assumptions: 277 * This makes the following assumptions:
278 * - only one returned vpp 278 * - only one returned vpp
279 * - no INOUT vpp's (Sun's vop_open has one of these) 279 * - no INOUT vpp's (Sun's vop_open has one of these)
280 * - the vnode operation vector of the first vnode should be used 280 * - the vnode operation vector of the first vnode should be used
281 * to determine what implementation of the op should be invoked 281 * to determine what implementation of the op should be invoked
282 * - all mapped vnodes are of our vnode-type (NEEDSWORK: 282 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
283 * problems on rmdir'ing mount points and renaming?) 283 * problems on rmdir'ing mount points and renaming?)
284 */ 284 */
285int 285int
286layer_bypass(v) 286layer_bypass(v)
287 void *v; 287 void *v;
288{ 288{
289 struct vop_generic_args /* { 289 struct vop_generic_args /* {
290 struct vnodeop_desc *a_desc; 290 struct vnodeop_desc *a_desc;
291 <other random data follows, presumably> 291 <other random data follows, presumably>
292 } */ *ap = v; 292 } */ *ap = v;
293 int (**our_vnodeop_p)(void *); 293 int (**our_vnodeop_p)(void *);
294 struct vnode **this_vp_p; 294 struct vnode **this_vp_p;
295 int error, error1; 295 int error, error1;
296 struct vnode *old_vps[VDESC_MAX_VPS], *vp0; 296 struct vnode *old_vps[VDESC_MAX_VPS], *vp0;
297 struct vnode **vps_p[VDESC_MAX_VPS]; 297 struct vnode **vps_p[VDESC_MAX_VPS];
298 struct vnode ***vppp; 298 struct vnode ***vppp;
299 struct mount *mp; 299 struct mount *mp;
300 struct vnodeop_desc *descp = ap->a_desc; 300 struct vnodeop_desc *descp = ap->a_desc;
301 int reles, i, flags; 301 int reles, i, flags;
302 302
303#ifdef SAFETY 303#ifdef SAFETY
304 /* 304 /*
305 * We require at least one vp. 305 * We require at least one vp.
306 */ 306 */
307 if (descp->vdesc_vp_offsets == NULL || 307 if (descp->vdesc_vp_offsets == NULL ||
308 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET) 308 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
309 panic("%s: no vp's in map.\n", __func__); 309 panic("%s: no vp's in map.\n", __func__);
310#endif 310#endif
311 311
312 vps_p[0] = 312 vps_p[0] =
313 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 313 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
314 vp0 = *vps_p[0]; 314 vp0 = *vps_p[0];
315 mp = vp0->v_mount; 315 mp = vp0->v_mount;
316 flags = MOUNTTOLAYERMOUNT(mp)->layerm_flags; 316 flags = MOUNTTOLAYERMOUNT(mp)->layerm_flags;
317 our_vnodeop_p = vp0->v_op; 317 our_vnodeop_p = vp0->v_op;
318 318
319 if (flags & LAYERFS_MBYPASSDEBUG) 319 if (flags & LAYERFS_MBYPASSDEBUG)
320 printf("%s: %s\n", __func__, descp->vdesc_name); 320 printf("%s: %s\n", __func__, descp->vdesc_name);
321 321
322 /* 322 /*
323 * Map the vnodes going in. 323 * Map the vnodes going in.
324 * Later, we'll invoke the operation based on 324 * Later, we'll invoke the operation based on
325 * the first mapped vnode's operation vector. 325 * the first mapped vnode's operation vector.
326 */ 326 */
327 reles = descp->vdesc_flags; 327 reles = descp->vdesc_flags;
328 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 328 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
329 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 329 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
330 break; /* bail out at end of list */ 330 break; /* bail out at end of list */
331 vps_p[i] = this_vp_p = 331 vps_p[i] = this_vp_p =
332 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], 332 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i],
333 ap); 333 ap);
334 /* 334 /*
335 * We're not guaranteed that any but the first vnode 335 * We're not guaranteed that any but the first vnode
336 * are of our type. Check for and don't map any 336 * are of our type. Check for and don't map any
337 * that aren't. (We must always map first vp or vclean fails.) 337 * that aren't. (We must always map first vp or vclean fails.)
338 */ 338 */
339 if (i && (*this_vp_p == NULL || 339 if (i && (*this_vp_p == NULL ||
340 (*this_vp_p)->v_op != our_vnodeop_p)) { 340 (*this_vp_p)->v_op != our_vnodeop_p)) {
341 old_vps[i] = NULL; 341 old_vps[i] = NULL;
342 } else { 342 } else {
343 old_vps[i] = *this_vp_p; 343 old_vps[i] = *this_vp_p;
344 *(vps_p[i]) = LAYERVPTOLOWERVP(*this_vp_p); 344 *(vps_p[i]) = LAYERVPTOLOWERVP(*this_vp_p);
345 /* 345 /*
346 * XXX - Several operations have the side effect 346 * XXX - Several operations have the side effect
347 * of vrele'ing their vp's. We must account for 347 * of vrele'ing their vp's. We must account for
348 * that. (This should go away in the future.) 348 * that. (This should go away in the future.)
349 */ 349 */
350 if (reles & VDESC_VP0_WILLRELE) 350 if (reles & VDESC_VP0_WILLRELE)
351 VREF(*this_vp_p); 351 VREF(*this_vp_p);
352 } 352 }
353 353
354 } 354 }
355 355
356 /* 356 /*
357 * Call the operation on the lower layer 357 * Call the operation on the lower layer
358 * with the modified argument structure. 358 * with the modified argument structure.
359 */ 359 */
360 error = VCALL(*vps_p[0], descp->vdesc_offset, ap); 360 error = VCALL(*vps_p[0], descp->vdesc_offset, ap);
361 361
362 /* 362 /*
363 * Maintain the illusion of call-by-value 363 * Maintain the illusion of call-by-value
364 * by restoring vnodes in the argument structure 364 * by restoring vnodes in the argument structure
365 * to their original value. 365 * to their original value.
366 */ 366 */
367 reles = descp->vdesc_flags; 367 reles = descp->vdesc_flags;
368 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 368 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
369 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 369 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
370 break; /* bail out at end of list */ 370 break; /* bail out at end of list */
371 if (old_vps[i]) { 371 if (old_vps[i]) {
372 *(vps_p[i]) = old_vps[i]; 372 *(vps_p[i]) = old_vps[i];
373 if (reles & VDESC_VP0_WILLUNLOCK) 373 if (reles & VDESC_VP0_WILLUNLOCK)
374 LAYERFS_UPPERUNLOCK(*(vps_p[i]), 0, error1); 374 LAYERFS_UPPERUNLOCK(*(vps_p[i]), 0, error1);
375 if (reles & VDESC_VP0_WILLRELE) 375 if (reles & VDESC_VP0_WILLRELE)
376 vrele(*(vps_p[i])); 376 vrele(*(vps_p[i]));
377 } 377 }
378 } 378 }
379 379
380 /* 380 /*
381 * Map the possible out-going vpp 381 * Map the possible out-going vpp
382 * (Assumes that the lower layer always returns 382 * (Assumes that the lower layer always returns
383 * a VREF'ed vpp unless it gets an error.) 383 * a VREF'ed vpp unless it gets an error.)
384 */ 384 */
385 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 385 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
386 !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 386 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
387 !error) { 387 !error) {
388 /* 388 /*
389 * XXX - even though some ops have vpp returned vp's, 389 * XXX - even though some ops have vpp returned vp's,
390 * several ops actually vrele this before returning. 390 * several ops actually vrele this before returning.
391 * We must avoid these ops. 391 * We must avoid these ops.
392 * (This should go away when these ops are regularized.) 392 * (This should go away when these ops are regularized.)
393 */ 393 */
394 if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 394 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
395 goto out; 395 goto out;
396 vppp = VOPARG_OFFSETTO(struct vnode***, 396 vppp = VOPARG_OFFSETTO(struct vnode***,
397 descp->vdesc_vpp_offset, ap); 397 descp->vdesc_vpp_offset, ap);
398 /* 398 /*
399 * Only vop_lookup, vop_create, vop_makedir, vop_bmap, 399 * Only vop_lookup, vop_create, vop_makedir, vop_bmap,
400 * vop_mknod, and vop_symlink return vpp's. vop_bmap 400 * vop_mknod, and vop_symlink return vpp's. vop_bmap
401 * doesn't call bypass as the lower vpp is fine (we're just 401 * doesn't call bypass as the lower vpp is fine (we're just
402 * going to do i/o on it). vop_lookup doesn't call bypass 402 * going to do i/o on it). vop_lookup doesn't call bypass
403 * as a lookup on "." would generate a locking error. 403 * as a lookup on "." would generate a locking error.
404 * So all the calls which get us here have a locked vpp. :-) 404 * So all the calls which get us here have a locked vpp. :-)
405 */ 405 */
406 error = layer_node_create(mp, **vppp, *vppp); 406 error = layer_node_create(mp, **vppp, *vppp);
407 if (error) { 407 if (error) {
408 vput(**vppp); 408 vput(**vppp);
409 **vppp = NULL; 409 **vppp = NULL;
410 } 410 }
411 } 411 }
412 412
413 out: 413 out:
414 return (error); 414 return (error);
415} 415}
416 416
417/* 417/*
418 * We have to carry on the locking protocol on the layer vnodes 418 * We have to carry on the locking protocol on the layer vnodes
419 * as we progress through the tree. We also have to enforce read-only 419 * as we progress through the tree. We also have to enforce read-only
420 * if this layer is mounted read-only. 420 * if this layer is mounted read-only.
421 */ 421 */
422int 422int
423layer_lookup(v) 423layer_lookup(v)
424 void *v; 424 void *v;
425{ 425{
426 struct vop_lookup_args /* { 426 struct vop_lookup_args /* {
427 struct vnodeop_desc *a_desc; 427 struct vnodeop_desc *a_desc;
428 struct vnode * a_dvp; 428 struct vnode * a_dvp;
429 struct vnode ** a_vpp; 429 struct vnode ** a_vpp;
430 struct componentname * a_cnp; 430 struct componentname * a_cnp;
431 } */ *ap = v; 431 } */ *ap = v;
432 struct componentname *cnp = ap->a_cnp; 432 struct componentname *cnp = ap->a_cnp;
433 int flags = cnp->cn_flags; 433 int flags = cnp->cn_flags;
434 struct vnode *dvp, *lvp, *ldvp; 434 struct vnode *dvp, *lvp, *ldvp;
435 int error; 435 int error;
436 436
437 dvp = ap->a_dvp; 437 dvp = ap->a_dvp;
438 438
439 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 439 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
440 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 440 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
441 return (EROFS); 441 return (EROFS);
442 442
443 ldvp = LAYERVPTOLOWERVP(dvp); 443 ldvp = LAYERVPTOLOWERVP(dvp);
444 ap->a_dvp = ldvp; 444 ap->a_dvp = ldvp;
445 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap); 445 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap);
446 lvp = *ap->a_vpp; 446 lvp = *ap->a_vpp;
447 *ap->a_vpp = NULL; 447 *ap->a_vpp = NULL;
448 448
449 if (error == EJUSTRETURN && (flags & ISLASTCN) && 449 if (error == EJUSTRETURN && (flags & ISLASTCN) &&
450 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 450 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
451 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 451 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
452 error = EROFS; 452 error = EROFS;
453 453
454 /* 454 /*
455 * We must do the same locking and unlocking at this layer as 455 * We must do the same locking and unlocking at this layer as
456 * is done in the layers below us. 456 * is done in the layers below us.
457 */ 457 */
458 if (ldvp == lvp) { 458 if (ldvp == lvp) {
459 459
460 /* 460 /*
461 * Did lookup on "." or ".." in the root node of a mount point. 461 * Got the same object back, because we looked up ".",
462 * So we return dvp after a VREF. 462 * or ".." in the root node of a mount point.
 463 * So we make another reference to dvp and return it.
463 */ 464 */
464 VREF(dvp); 465 VREF(dvp);
465 *ap->a_vpp = dvp; 466 *ap->a_vpp = dvp;
466 vrele(lvp); 467 vrele(lvp);
467 } else if (lvp != NULL) { 468 } else if (lvp != NULL) {
468 /* dvp, ldvp and vp are all locked */ 469 /* dvp, ldvp and vp are all locked */
469 error = layer_node_create(dvp->v_mount, lvp, ap->a_vpp); 470 error = layer_node_create(dvp->v_mount, lvp, ap->a_vpp);
470 if (error) { 471 if (error) {
471 vput(lvp); 472 vput(lvp);
472 } 473 }
473 } 474 }
474 return (error); 475 return (error);
475} 476}
476 477
477/* 478/*
478 * Setattr call. Disallow write attempts if the layer is mounted read-only. 479 * Setattr call. Disallow write attempts if the layer is mounted read-only.
479 */ 480 */
480int 481int
481layer_setattr(v) 482layer_setattr(v)
482 void *v; 483 void *v;
483{ 484{
484 struct vop_setattr_args /* { 485 struct vop_setattr_args /* {
485 struct vnodeop_desc *a_desc; 486 struct vnodeop_desc *a_desc;
486 struct vnode *a_vp; 487 struct vnode *a_vp;
487 struct vattr *a_vap; 488 struct vattr *a_vap;
488 kauth_cred_t a_cred; 489 kauth_cred_t a_cred;
489 struct lwp *a_l; 490 struct lwp *a_l;
490 } */ *ap = v; 491 } */ *ap = v;
491 struct vnode *vp = ap->a_vp; 492 struct vnode *vp = ap->a_vp;
492 struct vattr *vap = ap->a_vap; 493 struct vattr *vap = ap->a_vap;
493 494
494 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 495 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
495 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 496 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
496 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 497 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
497 (vp->v_mount->mnt_flag & MNT_RDONLY)) 498 (vp->v_mount->mnt_flag & MNT_RDONLY))
498 return (EROFS); 499 return (EROFS);
499 if (vap->va_size != VNOVAL) { 500 if (vap->va_size != VNOVAL) {
500 switch (vp->v_type) { 501 switch (vp->v_type) {
501 case VDIR: 502 case VDIR:
502 return (EISDIR); 503 return (EISDIR);
503 case VCHR: 504 case VCHR:
504 case VBLK: 505 case VBLK:
505 case VSOCK: 506 case VSOCK:
506 case VFIFO: 507 case VFIFO:
507 return (0); 508 return (0);
508 case VREG: 509 case VREG:
509 case VLNK: 510 case VLNK:
510 default: 511 default:
511 /* 512 /*
512 * Disallow write attempts if the filesystem is 513 * Disallow write attempts if the filesystem is
513 * mounted read-only. 514 * mounted read-only.
514 */ 515 */
515 if (vp->v_mount->mnt_flag & MNT_RDONLY) 516 if (vp->v_mount->mnt_flag & MNT_RDONLY)
516 return (EROFS); 517 return (EROFS);
517 } 518 }
518 } 519 }
519 return (LAYERFS_DO_BYPASS(vp, ap)); 520 return (LAYERFS_DO_BYPASS(vp, ap));
520} 521}
521 522
522/* 523/*
523 * We handle getattr only to change the fsid. 524 * We handle getattr only to change the fsid.
524 */ 525 */
525int 526int
526layer_getattr(v) 527layer_getattr(v)
527 void *v; 528 void *v;
528{ 529{
529 struct vop_getattr_args /* { 530 struct vop_getattr_args /* {
530 struct vnode *a_vp; 531 struct vnode *a_vp;
531 struct vattr *a_vap; 532 struct vattr *a_vap;
532 kauth_cred_t a_cred; 533 kauth_cred_t a_cred;
533 struct lwp *a_l; 534 struct lwp *a_l;
534 } */ *ap = v; 535 } */ *ap = v;
535 struct vnode *vp = ap->a_vp; 536 struct vnode *vp = ap->a_vp;
536 int error; 537 int error;
537 538
538 if ((error = LAYERFS_DO_BYPASS(vp, ap)) != 0) 539 if ((error = LAYERFS_DO_BYPASS(vp, ap)) != 0)
539 return (error); 540 return (error);
540 /* Requires that arguments be restored. */ 541 /* Requires that arguments be restored. */
541 ap->a_vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 542 ap->a_vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
542 return (0); 543 return (0);
543} 544}
544 545
545int 546int
546layer_access(v) 547layer_access(v)
547 void *v; 548 void *v;
548{ 549{
549 struct vop_access_args /* { 550 struct vop_access_args /* {
550 struct vnode *a_vp; 551 struct vnode *a_vp;
551 int a_mode; 552 int a_mode;
552 kauth_cred_t a_cred; 553 kauth_cred_t a_cred;
553 struct lwp *a_l; 554 struct lwp *a_l;
554 } */ *ap = v; 555 } */ *ap = v;
555 struct vnode *vp = ap->a_vp; 556 struct vnode *vp = ap->a_vp;
556 mode_t mode = ap->a_mode; 557 mode_t mode = ap->a_mode;
557 558
558 /* 559 /*
559 * Disallow write attempts on read-only layers; 560 * Disallow write attempts on read-only layers;
560 * unless the file is a socket, fifo, or a block or 561 * unless the file is a socket, fifo, or a block or
561 * character device resident on the file system. 562 * character device resident on the file system.
562 */ 563 */
563 if (mode & VWRITE) { 564 if (mode & VWRITE) {
564 switch (vp->v_type) { 565 switch (vp->v_type) {
565 case VDIR: 566 case VDIR:
566 case VLNK: 567 case VLNK:
567 case VREG: 568 case VREG:
568 if (vp->v_mount->mnt_flag & MNT_RDONLY) 569 if (vp->v_mount->mnt_flag & MNT_RDONLY)
569 return (EROFS); 570 return (EROFS);
570 break; 571 break;
571 default: 572 default:
572 break; 573 break;
573 } 574 }
574 } 575 }
575 return (LAYERFS_DO_BYPASS(vp, ap)); 576 return (LAYERFS_DO_BYPASS(vp, ap));
576} 577}
577 578
578/* 579/*
579 * We must handle open to be able to catch MNT_NODEV and friends. 580 * We must handle open to be able to catch MNT_NODEV and friends.
580 */ 581 */
581int 582int
582layer_open(v) 583layer_open(v)
583 void *v; 584 void *v;
584{ 585{
585 struct vop_open_args *ap = v; 586 struct vop_open_args *ap = v;
586 struct vnode *vp = ap->a_vp; 587 struct vnode *vp = ap->a_vp;
587 enum vtype lower_type = LAYERVPTOLOWERVP(vp)->v_type; 588 enum vtype lower_type = LAYERVPTOLOWERVP(vp)->v_type;
588 589
589 if (((lower_type == VBLK) || (lower_type == VCHR)) && 590 if (((lower_type == VBLK) || (lower_type == VCHR)) &&
590 (vp->v_mount->mnt_flag & MNT_NODEV)) 591 (vp->v_mount->mnt_flag & MNT_NODEV))
591 return ENXIO; 592 return ENXIO;
592 593
593 return LAYERFS_DO_BYPASS(vp, ap); 594 return LAYERFS_DO_BYPASS(vp, ap);
594} 595}
595 596
596/* 597/*
597 * We need to process our own vnode lock and then clear the 598 * We need to process our own vnode lock and then clear the
598 * interlock flag as it applies only to our vnode, not the 599 * interlock flag as it applies only to our vnode, not the
599 * vnodes below us on the stack. 600 * vnodes below us on the stack.
600 */ 601 */
601int 602int
602layer_lock(v) 603layer_lock(v)
603 void *v; 604 void *v;
604{ 605{
605 struct vop_lock_args /* { 606 struct vop_lock_args /* {
606 struct vnode *a_vp; 607 struct vnode *a_vp;
607 int a_flags; 608 int a_flags;
608 struct proc *a_p; 609 struct proc *a_p;
609 } */ *ap = v; 610 } */ *ap = v;
610 struct vnode *vp = ap->a_vp, *lowervp; 611 struct vnode *vp = ap->a_vp, *lowervp;
611 int flags = ap->a_flags, error; 612 int flags = ap->a_flags, error;
612 613
613 if (flags & LK_INTERLOCK) { 614 if (flags & LK_INTERLOCK) {
614 mutex_exit(&vp->v_interlock); 615 mutex_exit(&vp->v_interlock);
615 flags &= ~LK_INTERLOCK; 616 flags &= ~LK_INTERLOCK;
616 } 617 }
617 618
618 if (vp->v_vnlock != NULL) { 619 if (vp->v_vnlock != NULL) {
619 /* 620 /*
620 * The lower level has exported a struct lock to us. Use 621 * The lower level has exported a struct lock to us. Use
621 * it so that all vnodes in the stack lock and unlock 622 * it so that all vnodes in the stack lock and unlock
622 * simultaneously. Note: we don't DRAIN the lock as DRAIN 623 * simultaneously. Note: we don't DRAIN the lock as DRAIN
623 * decommissions the lock - just because our vnode is 624 * decommissions the lock - just because our vnode is
624 * going away doesn't mean the struct lock below us is. 625 * going away doesn't mean the struct lock below us is.
625 * LK_EXCLUSIVE is fine. 626 * LK_EXCLUSIVE is fine.
626 */ 627 */
627 return (vlockmgr(vp->v_vnlock, flags)); 628 return (vlockmgr(vp->v_vnlock, flags));
628 } else { 629 } else {
629 /* 630 /*
630 * Ahh well. It would be nice if the fs we're over would 631 * Ahh well. It would be nice if the fs we're over would
631 * export a struct lock for us to use, but it doesn't. 632 * export a struct lock for us to use, but it doesn't.
632 * 633 *
633 * To prevent race conditions involving doing a lookup 634 * To prevent race conditions involving doing a lookup
634 * on "..", we have to lock the lower node, then lock our 635 * on "..", we have to lock the lower node, then lock our
635 * node. Most of the time it won't matter that we lock our 636 * node. Most of the time it won't matter that we lock our
636 * node (as any locking would need the lower one locked 637 * node (as any locking would need the lower one locked
637 * first). 638 * first).
638 */ 639 */
639 lowervp = LAYERVPTOLOWERVP(vp); 640 lowervp = LAYERVPTOLOWERVP(vp);
640 error = VOP_LOCK(lowervp, flags); 641 error = VOP_LOCK(lowervp, flags);
641 if (error) 642 if (error)
642 return (error); 643 return (error);
643 if ((error = vlockmgr(&vp->v_lock, flags))) { 644 if ((error = vlockmgr(&vp->v_lock, flags))) {
644 VOP_UNLOCK(lowervp, 0); 645 VOP_UNLOCK(lowervp, 0);
645 } 646 }
646 return (error); 647 return (error);
647 } 648 }
648} 649}
649 650
650/* 651/*
651 */ 652 */
652int 653int
653layer_unlock(v) 654layer_unlock(v)
654 void *v; 655 void *v;
655{ 656{
656 struct vop_unlock_args /* { 657 struct vop_unlock_args /* {
657 struct vnode *a_vp; 658 struct vnode *a_vp;
658 int a_flags; 659 int a_flags;
659 struct proc *a_p; 660 struct proc *a_p;
660 } */ *ap = v; 661 } */ *ap = v;
661 struct vnode *vp = ap->a_vp; 662 struct vnode *vp = ap->a_vp;
662 int flags = ap->a_flags; 663 int flags = ap->a_flags;
663 664
664 if (flags & LK_INTERLOCK) { 665 if (flags & LK_INTERLOCK) {
665 mutex_exit(&vp->v_interlock); 666 mutex_exit(&vp->v_interlock);
666 flags &= ~LK_INTERLOCK; 667 flags &= ~LK_INTERLOCK;
667 } 668 }
668 669
669 if (vp->v_vnlock != NULL) { 670 if (vp->v_vnlock != NULL) {
670 return (vlockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE)); 671 return (vlockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE));
671 } else { 672 } else {
672 VOP_UNLOCK(LAYERVPTOLOWERVP(vp), flags); 673 VOP_UNLOCK(LAYERVPTOLOWERVP(vp), flags);
673 return (vlockmgr(&vp->v_lock, flags | LK_RELEASE)); 674 return (vlockmgr(&vp->v_lock, flags | LK_RELEASE));
674 } 675 }
675} 676}
676 677
677int 678int
678layer_islocked(v) 679layer_islocked(v)
679 void *v; 680 void *v;
680{ 681{
681 struct vop_islocked_args /* { 682 struct vop_islocked_args /* {
682 struct vnode *a_vp; 683 struct vnode *a_vp;
683 } */ *ap = v; 684 } */ *ap = v;
684 struct vnode *vp = ap->a_vp; 685 struct vnode *vp = ap->a_vp;
685 int lkstatus; 686 int lkstatus;
686 687
687 if (vp->v_vnlock != NULL) 688 if (vp->v_vnlock != NULL)
688 return vlockstatus(vp->v_vnlock); 689 return vlockstatus(vp->v_vnlock);
689 690
690 lkstatus = VOP_ISLOCKED(LAYERVPTOLOWERVP(vp)); 691 lkstatus = VOP_ISLOCKED(LAYERVPTOLOWERVP(vp));
691 if (lkstatus) 692 if (lkstatus)
692 return lkstatus; 693 return lkstatus;
693 694
694 return vlockstatus(&vp->v_lock); 695 return vlockstatus(&vp->v_lock);
695} 696}
696 697
697/* 698/*
698 * If vinvalbuf is calling us, it's a "shallow fsync" -- don't bother 699 * If vinvalbuf is calling us, it's a "shallow fsync" -- don't bother
699 * syncing the underlying vnodes, since they'll be fsync'ed when 700 * syncing the underlying vnodes, since they'll be fsync'ed when
700 * reclaimed; otherwise, 701 * reclaimed; otherwise,
701 * pass it through to the underlying layer. 702 * pass it through to the underlying layer.
702 * 703 *
703 * XXX Do we still need to worry about shallow fsync? 704 * XXX Do we still need to worry about shallow fsync?
704 */ 705 */
705 706
706int 707int
707layer_fsync(v) 708layer_fsync(v)
708 void *v; 709 void *v;
709{ 710{
710 struct vop_fsync_args /* { 711 struct vop_fsync_args /* {
711 struct vnode *a_vp; 712 struct vnode *a_vp;
712 kauth_cred_t a_cred; 713 kauth_cred_t a_cred;
713 int a_flags; 714 int a_flags;
714 off_t offlo; 715 off_t offlo;
715 off_t offhi; 716 off_t offhi;
716 struct lwp *a_l; 717 struct lwp *a_l;
717 } */ *ap = v; 718 } */ *ap = v;
718 719
719 if (ap->a_flags & FSYNC_RECLAIM) { 720 if (ap->a_flags & FSYNC_RECLAIM) {
720 return 0; 721 return 0;
721 } 722 }
722 723
723 return (LAYERFS_DO_BYPASS(ap->a_vp, ap)); 724 return (LAYERFS_DO_BYPASS(ap->a_vp, ap));
724} 725}
725 726
726 727
727int 728int
728layer_inactive(v) 729layer_inactive(v)
729 void *v; 730 void *v;
730{ 731{
731 struct vop_inactive_args /* { 732 struct vop_inactive_args /* {
732 struct vnode *a_vp; 733 struct vnode *a_vp;
733 bool *a_recycle; 734 bool *a_recycle;
734 } */ *ap = v; 735 } */ *ap = v;
735 struct vnode *vp = ap->a_vp; 736 struct vnode *vp = ap->a_vp;
736 737
737 /* 738 /*
738 * ..., but don't cache the device node. Also, if we did a 739 * ..., but don't cache the device node. Also, if we did a
739 * remove, don't cache the node. 740 * remove, don't cache the node.
740 */ 741 */
741 *ap->a_recycle = (vp->v_type == VBLK || vp->v_type == VCHR 742 *ap->a_recycle = (vp->v_type == VBLK || vp->v_type == VCHR
742 || (VTOLAYER(vp)->layer_flags & LAYERFS_REMOVED)); 743 || (VTOLAYER(vp)->layer_flags & LAYERFS_REMOVED));
743 744
744 /* 745 /*
745 * Do nothing (and _don't_ bypass). 746 * Do nothing (and _don't_ bypass).
746 * Wait to vrele lowervp until reclaim, 747 * Wait to vrele lowervp until reclaim,
747 * so that until then our layer_node is in the 748 * so that until then our layer_node is in the
748 * cache and reusable. 749 * cache and reusable.
749 * 750 *
750 * NEEDSWORK: Someday, consider inactive'ing 751 * NEEDSWORK: Someday, consider inactive'ing
751 * the lowervp and then trying to reactivate it 752 * the lowervp and then trying to reactivate it
752 * with capabilities (v_id) 753 * with capabilities (v_id)
753 * like they do in the name lookup cache code. 754 * like they do in the name lookup cache code.
754 * That's too much work for now. 755 * That's too much work for now.
755 */ 756 */
756 VOP_UNLOCK(vp, 0); 757 VOP_UNLOCK(vp, 0);
757 758
758 return (0); 759 return (0);
759} 760}
760 761
761int 762int
762layer_remove(v) 763layer_remove(v)
763 void *v; 764 void *v;
764{ 765{
765 struct vop_remove_args /* { 766 struct vop_remove_args /* {
766 struct vonde *a_dvp; 767 struct vonde *a_dvp;
767 struct vnode *a_vp; 768 struct vnode *a_vp;
768 struct componentname *a_cnp; 769 struct componentname *a_cnp;
769 } */ *ap = v; 770 } */ *ap = v;
770 771
771 int error; 772 int error;
772 struct vnode *vp = ap->a_vp; 773 struct vnode *vp = ap->a_vp;
773 774
774 vref(vp); 775 vref(vp);
775 if ((error = LAYERFS_DO_BYPASS(vp, ap)) == 0) 776 if ((error = LAYERFS_DO_BYPASS(vp, ap)) == 0)
776 VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED; 777 VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED;
777 778
778 vrele(vp); 779 vrele(vp);
779 780
780 return (error); 781 return (error);
781} 782}
782 783
783int 784int
784layer_rename(v) 785layer_rename(v)
785 void *v; 786 void *v;
786{ 787{
787 struct vop_rename_args /* { 788 struct vop_rename_args /* {
788 struct vnode *a_fdvp; 789 struct vnode *a_fdvp;
789 struct vnode *a_fvp; 790 struct vnode *a_fvp;
790 struct componentname *a_fcnp; 791 struct componentname *a_fcnp;
791 struct vnode *a_tdvp; 792 struct vnode *a_tdvp;
792 struct vnode *a_tvp; 793 struct vnode *a_tvp;
793 struct componentname *a_tcnp; 794 struct componentname *a_tcnp;
794 } */ *ap = v; 795 } */ *ap = v;
795 796
796 int error; 797 int error;
797 struct vnode *fdvp = ap->a_fdvp; 798 struct vnode *fdvp = ap->a_fdvp;
798 struct vnode *tvp; 799 struct vnode *tvp;
799 800
800 tvp = ap->a_tvp; 801 tvp = ap->a_tvp;
801 if (tvp) { 802 if (tvp) {
802 if (tvp->v_mount != fdvp->v_mount) 803 if (tvp->v_mount != fdvp->v_mount)
803 tvp = NULL; 804 tvp = NULL;
804 else 805 else
805 vref(tvp); 806 vref(tvp);
806 } 807 }
807 error = LAYERFS_DO_BYPASS(fdvp, ap); 808 error = LAYERFS_DO_BYPASS(fdvp, ap);
808 if (tvp) { 809 if (tvp) {
809 if (error == 0) 810 if (error == 0)
810 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED; 811 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED;
811 vrele(tvp); 812 vrele(tvp);
812 } 813 }
813 814
814 return (error); 815 return (error);
815} 816}
816 817
817int 818int
818layer_rmdir(v) 819layer_rmdir(v)
819 void *v; 820 void *v;
820{ 821{
821 struct vop_rmdir_args /* { 822 struct vop_rmdir_args /* {
822 struct vnode *a_dvp; 823 struct vnode *a_dvp;
823 struct vnode *a_vp; 824 struct vnode *a_vp;
824 struct componentname *a_cnp; 825 struct componentname *a_cnp;
825 } */ *ap = v; 826 } */ *ap = v;
826 int error; 827 int error;
827 struct vnode *vp = ap->a_vp; 828 struct vnode *vp = ap->a_vp;
828 829
829 vref(vp); 830 vref(vp);
830 if ((error = LAYERFS_DO_BYPASS(vp, ap)) == 0) 831 if ((error = LAYERFS_DO_BYPASS(vp, ap)) == 0)
831 VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED; 832 VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED;
832 833
833 vrele(vp); 834 vrele(vp);
834 835
835 return (error); 836 return (error);
836} 837}
837 838
838int 839int
839layer_reclaim(v) 840layer_reclaim(v)
840 void *v; 841 void *v;
841{ 842{
842 struct vop_reclaim_args /* { 843 struct vop_reclaim_args /* {
843 struct vnode *a_vp; 844 struct vnode *a_vp;
844 struct lwp *a_l; 845 struct lwp *a_l;
845 } */ *ap = v; 846 } */ *ap = v;
846 struct vnode *vp = ap->a_vp; 847 struct vnode *vp = ap->a_vp;
847 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(vp->v_mount); 848 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(vp->v_mount);
848 struct layer_node *xp = VTOLAYER(vp); 849 struct layer_node *xp = VTOLAYER(vp);
849 struct vnode *lowervp = xp->layer_lowervp; 850 struct vnode *lowervp = xp->layer_lowervp;
850 851
851 /* 852 /*
852 * Note: in vop_reclaim, the node's struct lock has been 853 * Note: in vop_reclaim, the node's struct lock has been
853 * decomissioned, so we have to be careful about calling 854 * decomissioned, so we have to be careful about calling
854 * VOP's on ourself. We must be careful as VXLOCK is set. 855 * VOP's on ourself. We must be careful as VXLOCK is set.
855 */ 856 */
856 /* After this assignment, this node will not be re-used. */ 857 /* After this assignment, this node will not be re-used. */
857 if ((vp == lmp->layerm_rootvp)) { 858 if ((vp == lmp->layerm_rootvp)) {
858 /* 859 /*
859 * Oops! We no longer have a root node. Most likely reason is 860 * Oops! We no longer have a root node. Most likely reason is
860 * that someone forcably unmunted the underlying fs. 861 * that someone forcably unmunted the underlying fs.
861 * 862 *
862 * Now getting the root vnode will fail. We're dead. :-( 863 * Now getting the root vnode will fail. We're dead. :-(
863 */ 864 */
864 lmp->layerm_rootvp = NULL; 865 lmp->layerm_rootvp = NULL;
865 } 866 }
866 xp->layer_lowervp = NULL; 867 xp->layer_lowervp = NULL;
867 mutex_enter(&lmp->layerm_hashlock); 868 mutex_enter(&lmp->layerm_hashlock);
868 LIST_REMOVE(xp, layer_hash); 869 LIST_REMOVE(xp, layer_hash);
869 mutex_exit(&lmp->layerm_hashlock); 870 mutex_exit(&lmp->layerm_hashlock);
870 kmem_free(vp->v_data, lmp->layerm_size); 871 kmem_free(vp->v_data, lmp->layerm_size);
871 vp->v_data = NULL; 872 vp->v_data = NULL;
872 vrele(lowervp); 873 vrele(lowervp);
873 874
874 return (0); 875 return (0);
875} 876}
876 877
877/* 878/*
878 * We just feed the returned vnode up to the caller - there's no need 879 * We just feed the returned vnode up to the caller - there's no need
879 * to build a layer node on top of the node on which we're going to do 880 * to build a layer node on top of the node on which we're going to do
880 * i/o. :-) 881 * i/o. :-)
881 */ 882 */
882int 883int
883layer_bmap(v) 884layer_bmap(v)
884 void *v; 885 void *v;
885{ 886{
886 struct vop_bmap_args /* { 887 struct vop_bmap_args /* {
887 struct vnode *a_vp; 888 struct vnode *a_vp;
888 daddr_t a_bn; 889 daddr_t a_bn;
889 struct vnode **a_vpp; 890 struct vnode **a_vpp;
890 daddr_t *a_bnp; 891 daddr_t *a_bnp;
891 int *a_runp; 892 int *a_runp;
892 } */ *ap = v; 893 } */ *ap = v;
893 struct vnode *vp; 894 struct vnode *vp;
894 895
895 ap->a_vp = vp = LAYERVPTOLOWERVP(ap->a_vp); 896 ap->a_vp = vp = LAYERVPTOLOWERVP(ap->a_vp);
896 897
897 return (VCALL(vp, ap->a_desc->vdesc_offset, ap)); 898 return (VCALL(vp, ap->a_desc->vdesc_offset, ap));
898} 899}
899 900
900int 901int
901layer_print(v) 902layer_print(v)
902 void *v; 903 void *v;
903{ 904{
904 struct vop_print_args /* { 905 struct vop_print_args /* {
905 struct vnode *a_vp; 906 struct vnode *a_vp;
906 } */ *ap = v; 907 } */ *ap = v;
907 struct vnode *vp = ap->a_vp; 908 struct vnode *vp = ap->a_vp;
908 printf ("\ttag VT_LAYERFS, vp=%p, lowervp=%p\n", vp, LAYERVPTOLOWERVP(vp)); 909 printf ("\ttag VT_LAYERFS, vp=%p, lowervp=%p\n", vp, LAYERVPTOLOWERVP(vp));
909 return (0); 910 return (0);
910} 911}
911 912
912/* 913/*
913 * XXX - vop_bwrite must be hand coded because it has no 914 * XXX - vop_bwrite must be hand coded because it has no
914 * vnode in its arguments. 915 * vnode in its arguments.
915 * This goes away with a merged VM/buffer cache. 916 * This goes away with a merged VM/buffer cache.
916 */ 917 */
917int 918int
918layer_bwrite(v) 919layer_bwrite(v)
919 void *v; 920 void *v;
920{ 921{
921 struct vop_bwrite_args /* { 922 struct vop_bwrite_args /* {
922 struct buf *a_bp; 923 struct buf *a_bp;
923 } */ *ap = v; 924 } */ *ap = v;
924 struct buf *bp = ap->a_bp; 925 struct buf *bp = ap->a_bp;
925 int error; 926 int error;
926 struct vnode *savedvp; 927 struct vnode *savedvp;
927 928
928 savedvp = bp->b_vp; 929 savedvp = bp->b_vp;
929 bp->b_vp = LAYERVPTOLOWERVP(bp->b_vp); 930 bp->b_vp = LAYERVPTOLOWERVP(bp->b_vp);
930 931
931 error = VOP_BWRITE(bp); 932 error = VOP_BWRITE(bp);
932 933
933 bp->b_vp = savedvp; 934 bp->b_vp = savedvp;
934 935
935 return (error); 936 return (error);
936} 937}
937 938
938int 939int
939layer_getpages(v) 940layer_getpages(v)
940 void *v; 941 void *v;
941{ 942{
942 struct vop_getpages_args /* { 943 struct vop_getpages_args /* {
943 struct vnode *a_vp; 944 struct vnode *a_vp;
944 voff_t a_offset; 945 voff_t a_offset;
945 struct vm_page **a_m; 946 struct vm_page **a_m;
946 int *a_count; 947 int *a_count;
947 int a_centeridx; 948 int a_centeridx;
948 vm_prot_t a_access_type; 949 vm_prot_t a_access_type;
949 int a_advice; 950 int a_advice;
950 int a_flags; 951 int a_flags;
951 } */ *ap = v; 952 } */ *ap = v;
952 struct vnode *vp = ap->a_vp; 953 struct vnode *vp = ap->a_vp;
953 int error; 954 int error;
954 955
955 /* 956 /*
956 * just pass the request on to the underlying layer. 957 * just pass the request on to the underlying layer.
957 */ 958 */
958 959
959 if (ap->a_flags & PGO_LOCKED) { 960 if (ap->a_flags & PGO_LOCKED) {
960 return EBUSY; 961 return EBUSY;
961 } 962 }
962 ap->a_vp = LAYERVPTOLOWERVP(vp); 963 ap->a_vp = LAYERVPTOLOWERVP(vp);
963 mutex_exit(&vp->v_interlock); 964 mutex_exit(&vp->v_interlock);
964 mutex_enter(&ap->a_vp->v_interlock); 965 mutex_enter(&ap->a_vp->v_interlock);
965 error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap); 966 error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap);
966 return error; 967 return error;
967} 968}
968 969
969int 970int
970layer_putpages(v) 971layer_putpages(v)
971 void *v; 972 void *v;
972{ 973{
973 struct vop_putpages_args /* { 974 struct vop_putpages_args /* {
974 struct vnode *a_vp; 975 struct vnode *a_vp;
975 voff_t a_offlo; 976 voff_t a_offlo;
976 voff_t a_offhi; 977 voff_t a_offhi;
977 int a_flags; 978 int a_flags;
978 } */ *ap = v; 979 } */ *ap = v;
979 struct vnode *vp = ap->a_vp; 980 struct vnode *vp = ap->a_vp;
980 int error; 981 int error;
981 982
982 /* 983 /*
983 * just pass the request on to the underlying layer. 984 * just pass the request on to the underlying layer.
984 */ 985 */
985 986
986 ap->a_vp = LAYERVPTOLOWERVP(vp); 987 ap->a_vp = LAYERVPTOLOWERVP(vp);
987 mutex_exit(&vp->v_interlock); 988 mutex_exit(&vp->v_interlock);
988 if (ap->a_flags & PGO_RECLAIM) { 989 if (ap->a_flags & PGO_RECLAIM) {
989 return 0; 990 return 0;
990 } 991 }
991 mutex_enter(&ap->a_vp->v_interlock); 992 mutex_enter(&ap->a_vp->v_interlock);
992 error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap); 993 error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap);
993 return error; 994 return error;
994} 995}