2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * John Heidemann of the UCLA Ficus project.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
64 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
66 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
72 * (See mount_null(8) for more information.)
74 * The null layer duplicates a portion of the file system
75 * name space under a new name. In this respect, it is
76 * similar to the loopback file system. It differs from
77 * the loopback fs in two respects: it is implemented using
78 * a stackable layers techniques, and it's "null-node"s stack above
79 * all lower-layer vnodes, not just over directory vnodes.
81 * The null layer has two purposes. First, it serves as a demonstration
82 * of layering by proving a layer which does nothing. (It actually
83 * does everything the loopback file system does, which is slightly
84 * more than nothing.) Second, the null layer can serve as a prototype
85 * layer. Since it provides all necessary layer framework,
86 * new file system layers can be created very easily be starting
89 * The remainder of this man page examines the null layer as a basis
90 * for constructing new layers.
93 * INSTANTIATING NEW NULL LAYERS
95 * New null layers are created with mount_null(8).
96 * Mount_null(8) takes two arguments, the pathname
97 * of the lower vfs (target-pn) and the pathname where the null
98 * layer will appear in the namespace (alias-pn). After
99 * the null layer is put into place, the contents
100 * of target-pn subtree will be aliased under alias-pn.
103 * OPERATION OF A NULL LAYER
105 * The null layer is the minimum file system layer,
106 * simply bypassing all possible operations to the lower layer
107 * for processing there. The majority of its activity centers
108 * on the bypass routine, though which nearly all vnode operations
111 * The bypass routine accepts arbitrary vnode operations for
112 * handling by the lower layer. It begins by examing vnode
113 * operation arguments and replacing any null-nodes by their
114 * lower-layer equivlants. It then invokes the operation
115 * on the lower layer. Finally, it replaces the null-nodes
116 * in the arguments and, if a vnode is return by the operation,
117 * stacks a null-node on top of the returned vnode.
119 * Although bypass handles most operations, vop_getattr, vop_lock,
120 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
121 * bypassed. Vop_getattr must change the fsid being returned.
122 * Vop_lock and vop_unlock must handle any locking for the
123 * current vnode as well as pass the lock request down.
124 * Vop_inactive and vop_reclaim are not bypassed so that
125 * they can handle freeing null-layer specific data. Vop_print
126 * is not bypassed to avoid excessive debugging information.
127 * Also, certain vnode operations change the locking state within
128 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
129 * and symlink). Ideally these operations should not change the
130 * lock state, but should be changed to let the caller of the
131 * function unlock them. Otherwise all intermediate vnode layers
132 * (such as union, umapfs, etc) must catch these functions to do
133 * the necessary locking at their layer.
136 * INSTANTIATING VNODE STACKS
138 * Mounting associates the null layer with a lower layer,
139 * effect stacking two VFSes. Vnode stacks are instead
140 * created on demand as files are accessed.
142 * The initial mount creates a single vnode stack for the
143 * root of the new null layer. All other vnode stacks
144 * are created as a result of vnode operations on
145 * this or other null vnode stacks.
147 * New vnode stacks come into existance as a result of
148 * an operation which returns a vnode.
149 * The bypass routine stacks a null-node above the new
150 * vnode before returning it to the caller.
152 * For example, imagine mounting a null layer with
153 * "mount_null /usr/include /dev/layer/null".
154 * Changing directory to /dev/layer/null will assign
155 * the root null-node (which was created when the null layer was mounted).
156 * Now consider opening "sys". A vop_lookup would be
157 * done on the root null-node. This operation would bypass through
158 * to the lower layer which would return a vnode representing
159 * the UFS "sys". Null_bypass then builds a null-node
160 * aliasing the UFS "sys" and returns this to the caller.
161 * Later operations on the null-node "sys" will repeat this
162 * process when constructing other vnode stacks.
165 * CREATING OTHER FILE SYSTEM LAYERS
167 * One of the easiest ways to construct new file system layers is to make
168 * a copy of the null layer, rename all files and variables, and
169 * then begin modifing the copy. Sed can be used to easily rename
172 * The umap layer is an example of a layer descended from the
176 * INVOKING OPERATIONS ON LOWER LAYERS
178 * There are two techniques to invoke operations on a lower layer
179 * when the operation cannot be completely bypassed. Each method
180 * is appropriate in different situations. In both cases,
181 * it is the responsibility of the aliasing layer to make
182 * the operation arguments "correct" for the lower layer
183 * by mapping an vnode arguments to the lower layer.
185 * The first approach is to call the aliasing layer's bypass routine.
186 * This method is most suitable when you wish to invoke the operation
187 * currently being hanldled on the lower layer. It has the advantage
188 * that the bypass routine already must do argument mapping.
189 * An example of this is null_getattrs in the null layer.
191 * A second approach is to directly invoked vnode operations on
192 * the lower layer with the VOP_OPERATIONNAME interface.
193 * The advantage of this method is that it is easy to invoke
194 * arbitrary operations on the lower layer. The disadvantage
195 * is that vnodes arguments must be manualy mapped.
199 #include <sys/param.h>
200 #include <sys/systm.h>
201 #include <sys/proc.h>
202 #include <sys/time.h>
203 #include <sys/types.h>
204 #include <sys/vnode.h>
205 #include <sys/mount.h>
206 #include <sys/namei.h>
207 #include <sys/malloc.h>
209 #include <miscfs/nullfs/null.h>
212 int null_bug_bypass
= 0; /* for debugging: enables bypass printf'ing */
215 * This is the 10-Apr-92 bypass routine.
216 * This version has been optimized for speed, throwing away some
217 * safety checks. It should still always work, but it's not as
218 * robust to programmer errors.
219 * Define SAFETY to include some error checking code.
221 * In general, we map all vnodes going down and unmap them on the way back.
222 * As an exception to this, vnodes can be marked "unmapped" by setting
223 * the Nth bit in operation's vdesc_flags.
225 * Also, some BSD vnode operations have the side effect of vrele'ing
226 * their arguments. With stacking, the reference counts are held
227 * by the upper node, not the lower one, so we must handle these
228 * side-effects here. This is not of concern in Sun-derived systems
229 * since there are no such side-effects.
231 * This makes the following assumptions:
232 * - only one returned vpp
233 * - no INOUT vpp's (Sun's vop_open has one of these)
234 * - the vnode operation vector of the first vnode should be used
235 * to determine what implementation of the op should be invoked
236 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
237 * problems on rmdir'ing mount points and renaming?)
241 struct vop_generic_args
/* {
242 struct vnodeop_desc *a_desc;
243 <other random data follows, presumably>
246 extern int (**null_vnodeop_p
)(void *); /* not extern, really "forward" */
247 register struct vnode
**this_vp_p
;
249 struct vnode
*old_vps
[VDESC_MAX_VPS
];
250 struct vnode
**vps_p
[VDESC_MAX_VPS
];
251 struct vnode
***vppp
;
252 struct vnodeop_desc
*descp
= ap
->a_desc
;
256 printf ("null_bypass: %s\n", descp
->vdesc_name
);
260 * We require at least one vp.
262 if (descp
->vdesc_vp_offsets
== NULL
||
263 descp
->vdesc_vp_offsets
[0] == VDESC_NO_OFFSET
)
264 panic ("null_bypass: no vp's in map.\n");
268 * Map the vnodes going in.
269 * Later, we'll invoke the operation based on
270 * the first mapped vnode's operation vector.
272 reles
= descp
->vdesc_flags
;
273 for (i
= 0; i
< VDESC_MAX_VPS
; reles
>>= 1, i
++) {
274 if (descp
->vdesc_vp_offsets
[i
] == VDESC_NO_OFFSET
)
275 break; /* bail out at end of list */
276 vps_p
[i
] = this_vp_p
=
277 VOPARG_OFFSETTO(struct vnode
**,descp
->vdesc_vp_offsets
[i
],ap
);
279 * We're not guaranteed that any but the first vnode
280 * are of our type. Check for and don't map any
281 * that aren't. (We must always map first vp or vclean fails.)
283 if (i
&& (*this_vp_p
== NULL
||
284 (*this_vp_p
)->v_op
!= null_vnodeop_p
)) {
287 old_vps
[i
] = *this_vp_p
;
288 *(vps_p
[i
]) = NULLVPTOLOWERVP(*this_vp_p
);
290 * XXX - Several operations have the side effect
291 * of vrele'ing their vp's. We must account for
292 * that. (This should go away in the future.)
301 * Call the operation on the lower layer
302 * with the modified argument structure.
304 error
= VCALL(*(vps_p
[0]), descp
->vdesc_offset
, ap
);
307 * Maintain the illusion of call-by-value
308 * by restoring vnodes in the argument structure
309 * to their original value.
311 reles
= descp
->vdesc_flags
;
312 for (i
= 0; i
< VDESC_MAX_VPS
; reles
>>= 1, i
++) {
313 if (descp
->vdesc_vp_offsets
[i
] == VDESC_NO_OFFSET
)
314 break; /* bail out at end of list */
316 *(vps_p
[i
]) = old_vps
[i
];
323 * Map the possible out-going vpp
324 * (Assumes that the lower layer always returns
325 * a VREF'ed vpp unless it gets an error.)
327 if (descp
->vdesc_vpp_offset
!= VDESC_NO_OFFSET
&&
328 !(descp
->vdesc_flags
& VDESC_NOMAP_VPP
) &&
331 * XXX - even though some ops have vpp returned vp's,
332 * several ops actually vrele this before returning.
333 * We must avoid these ops.
334 * (This should go away when these ops are regularized.)
336 if (descp
->vdesc_flags
& VDESC_VPP_WILLRELE
)
338 vppp
= VOPARG_OFFSETTO(struct vnode
***,
339 descp
->vdesc_vpp_offset
,ap
);
340 error
= null_node_create(old_vps
[0]->v_mount
, **vppp
, *vppp
);
348 * We have to carry on the locking protocol on the null layer vnodes
349 * as we progress through the tree. We also have to enforce read-only
350 * if this layer is mounted read-only.
353 struct vop_lookup_args
/* {
354 struct vnode * a_dvp;
355 struct vnode ** a_vpp;
356 struct componentname * a_cnp;
359 struct componentname
*cnp
= ap
->a_cnp
;
360 struct proc
*p
= cnp
->cn_proc
;
361 int flags
= cnp
->cn_flags
;
362 struct vop_lock_args lockargs
;
363 struct vop_unlock_args unlockargs
;
364 struct vnode
*dvp
, *vp
;
367 if ((flags
& ISLASTCN
) && (ap
->a_dvp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
368 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
370 error
= null_bypass(ap
);
371 if (error
== EJUSTRETURN
&& (flags
& ISLASTCN
) &&
372 (ap
->a_dvp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
373 (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
))
376 * We must do the same locking and unlocking at this layer as
377 * is done in the layers below us. We could figure this out
378 * based on the error return and the LASTCN, LOCKPARENT, and
379 * LOCKLEAF flags. However, it is more expidient to just find
380 * out the state of the lower level vnodes and set ours to the
387 if (!VOP_ISLOCKED(dvp
)) {
388 unlockargs
.a_vp
= dvp
;
389 unlockargs
.a_flags
= 0;
391 vop_nounlock(&unlockargs
);
393 if (vp
!= NULL
&& VOP_ISLOCKED(vp
)) {
395 lockargs
.a_flags
= LK_SHARED
;
397 vop_nolock(&lockargs
);
403 * Setattr call. Disallow write attempts if the layer is mounted read-only.
407 struct vop_setattr_args
/* {
408 struct vnodeop_desc *a_desc;
411 struct ucred *a_cred;
415 struct vnode
*vp
= ap
->a_vp
;
416 struct vattr
*vap
= ap
->a_vap
;
418 if ((vap
->va_flags
!= VNOVAL
|| vap
->va_uid
!= (uid_t
)VNOVAL
||
419 vap
->va_gid
!= (gid_t
)VNOVAL
|| vap
->va_atime
.tv_sec
!= VNOVAL
||
420 vap
->va_mtime
.tv_sec
!= VNOVAL
|| vap
->va_mode
!= (mode_t
)VNOVAL
) &&
421 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
))
423 if (vap
->va_size
!= VNOVAL
) {
424 switch (vp
->v_type
) {
436 * Disallow write attempts if the filesystem is
439 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
443 return (null_bypass(ap
));
447 * We handle getattr only to change the fsid.
451 struct vop_getattr_args
/* {
454 struct ucred *a_cred;
460 if (error
= null_bypass(ap
))
462 /* Requires that arguments be restored. */
463 ap
->a_vap
->va_fsid
= ap
->a_vp
->v_mount
->mnt_stat
.f_fsid
.val
[0];
469 struct vop_access_args
/* {
472 struct ucred *a_cred;
476 struct vnode
*vp
= ap
->a_vp
;
477 mode_t mode
= ap
->a_mode
;
480 * Disallow write attempts on read-only layers;
481 * unless the file is a socket, fifo, or a block or
482 * character device resident on the file system.
485 switch (vp
->v_type
) {
489 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
494 return (null_bypass(ap
));
498 * We need to process our own vnode lock and then clear the
499 * interlock flag as it applies only to our vnode, not the
500 * vnodes below us on the stack.
504 struct vop_lock_args
/* {
512 if ((ap
->a_flags
& LK_TYPE_MASK
) == LK_DRAIN
)
514 ap
->a_flags
&= ~LK_INTERLOCK
;
515 return (null_bypass(ap
));
519 * We need to process our own vnode unlock and then clear the
520 * interlock flag as it applies only to our vnode, not the
521 * vnodes below us on the stack.
525 struct vop_unlock_args
/* {
531 struct vnode
*vp
= ap
->a_vp
;
534 ap
->a_flags
&= ~LK_INTERLOCK
;
535 return (null_bypass(ap
));
540 struct vop_inactive_args
/* {
546 * Do nothing (and _don't_ bypass).
547 * Wait to vrele lowervp until reclaim,
548 * so that until then our null_node is in the
549 * cache and reusable.
551 * NEEDSWORK: Someday, consider inactive'ing
552 * the lowervp and then trying to reactivate it
553 * with capabilities (v_id)
554 * like they do in the name lookup cache code.
555 * That's too much work for now.
557 VOP_UNLOCK(ap
->a_vp
, 0, ap
->a_p
);
563 struct vop_reclaim_args
/* {
568 struct vnode
*vp
= ap
->a_vp
;
569 struct null_node
*xp
= VTONULL(vp
);
570 struct vnode
*lowervp
= xp
->null_lowervp
;
573 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
574 * so we can't call VOPs on ourself.
576 /* After this assignment, this node will not be re-used. */
577 xp
->null_lowervp
= NULL
;
578 LIST_REMOVE(xp
, null_hash
);
579 FREE(vp
->v_data
, M_TEMP
);
587 struct vop_print_args
/* {
591 register struct vnode
*vp
= ap
->a_vp
;
592 printf ("\ttag VT_NULLFS, vp=%x, lowervp=%x\n", vp
, NULLVPTOLOWERVP(vp
));
597 * XXX - vop_strategy must be hand coded because it has no
598 * vnode in its arguments.
599 * This goes away with a merged VM/buffer cache.
603 struct vop_strategy_args
/* {
607 struct buf
*bp
= ap
->a_bp
;
609 struct vnode
*savedvp
;
612 bp
->b_vp
= NULLVPTOLOWERVP(bp
->b_vp
);
614 error
= VOP_STRATEGY(bp
);
622 * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
623 * vnode in its arguments.
624 * This goes away with a merged VM/buffer cache.
628 struct vop_bwrite_args
/* {
632 struct buf
*bp
= ap
->a_bp
;
634 struct vnode
*savedvp
;
637 bp
->b_vp
= NULLVPTOLOWERVP(bp
->b_vp
);
639 error
= VOP_BWRITE(bp
);
647 * Global vfs data structures
650 #define VOPFUNC int (*)(void *)
652 int (**null_vnodeop_p
)(void *);
653 struct vnodeopv_entry_desc null_vnodeop_entries
[] = {
654 { &vop_default_desc
, (VOPFUNC
)null_bypass
},
656 { &vop_lookup_desc
, (VOPFUNC
)null_lookup
},
657 { &vop_setattr_desc
, (VOPFUNC
)null_setattr
},
658 { &vop_getattr_desc
, (VOPFUNC
)null_getattr
},
659 { &vop_access_desc
, (VOPFUNC
)null_access
},
660 { &vop_lock_desc
, (VOPFUNC
)null_lock
},
661 { &vop_unlock_desc
, (VOPFUNC
)null_unlock
},
662 { &vop_inactive_desc
, (VOPFUNC
)null_inactive
},
663 { &vop_reclaim_desc
, (VOPFUNC
)null_reclaim
},
664 { &vop_print_desc
, (VOPFUNC
)null_print
},
666 { &vop_strategy_desc
, (VOPFUNC
)null_strategy
},
667 { &vop_bwrite_desc
, (VOPFUNC
)null_bwrite
},
669 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
671 struct vnodeopv_desc null_vnodeop_opv_desc
=
672 { &null_vnodeop_p
, null_vnodeop_entries
};