xref: /original-bsd/sys/miscfs/nullfs/null_vnops.c (revision 9a35f7df)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * John Heidemann of the UCLA Ficus project.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)null_vnops.c	8.5 (Berkeley) 05/22/95
11  *
12  * Ancestors:
13  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
14  *	$Id: lofs_vnops.c,v 1.11 1992/05/30 10:05:43 jsp Exp jsp $
15  *	...and...
16  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
17  */
18 
19 /*
20  * Null Layer
21  *
22  * (See mount_null(8) for more information.)
23  *
24  * The null layer duplicates a portion of the file system
25  * name space under a new name.  In this respect, it is
26  * similar to the loopback file system.  It differs from
27  * the loopback fs in two respects:  it is implemented using
28  * a stackable layers techniques, and it's "null-node"s stack above
29  * all lower-layer vnodes, not just over directory vnodes.
30  *
31  * The null layer has two purposes.  First, it serves as a demonstration
32  * of layering by proving a layer which does nothing.  (It actually
33  * does everything the loopback file system does, which is slightly
34  * more than nothing.)  Second, the null layer can serve as a prototype
35  * layer.  Since it provides all necessary layer framework,
36  * new file system layers can be created very easily be starting
37  * with a null layer.
38  *
39  * The remainder of this man page examines the null layer as a basis
40  * for constructing new layers.
41  *
42  *
43  * INSTANTIATING NEW NULL LAYERS
44  *
45  * New null layers are created with mount_null(8).
46  * Mount_null(8) takes two arguments, the pathname
47  * of the lower vfs (target-pn) and the pathname where the null
48  * layer will appear in the namespace (alias-pn).  After
49  * the null layer is put into place, the contents
50  * of target-pn subtree will be aliased under alias-pn.
51  *
52  *
53  * OPERATION OF A NULL LAYER
54  *
55  * The null layer is the minimum file system layer,
56  * simply bypassing all possible operations to the lower layer
57  * for processing there.  The majority of its activity centers
58  * on the bypass routine, though which nearly all vnode operations
59  * pass.
60  *
61  * The bypass routine accepts arbitrary vnode operations for
62  * handling by the lower layer.  It begins by examing vnode
63  * operation arguments and replacing any null-nodes by their
64  * lower-layer equivlants.  It then invokes the operation
65  * on the lower layer.  Finally, it replaces the null-nodes
66  * in the arguments and, if a vnode is return by the operation,
67  * stacks a null-node on top of the returned vnode.
68  *
69  * Although bypass handles most operations, vop_getattr, vop_lock,
70  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
71  * bypassed. Vop_getattr must change the fsid being returned.
72  * Vop_lock and vop_unlock must handle any locking for the
73  * current vnode as well as pass the lock request down.
74  * Vop_inactive and vop_reclaim are not bypassed so that
75  * they can handle freeing null-layer specific data. Vop_print
76  * is not bypassed to avoid excessive debugging information.
77  * Also, certain vnode operations change the locking state within
78  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
79  * and symlink). Ideally these operations should not change the
80  * lock state, but should be changed to let the caller of the
81  * function unlock them. Otherwise all intermediate vnode layers
82  * (such as union, umapfs, etc) must catch these functions to do
83  * the necessary locking at their layer.
84  *
85  *
86  * INSTANTIATING VNODE STACKS
87  *
88  * Mounting associates the null layer with a lower layer,
89  * effect stacking two VFSes.  Vnode stacks are instead
90  * created on demand as files are accessed.
91  *
92  * The initial mount creates a single vnode stack for the
93  * root of the new null layer.  All other vnode stacks
94  * are created as a result of vnode operations on
95  * this or other null vnode stacks.
96  *
97  * New vnode stacks come into existance as a result of
98  * an operation which returns a vnode.
99  * The bypass routine stacks a null-node above the new
100  * vnode before returning it to the caller.
101  *
102  * For example, imagine mounting a null layer with
103  * "mount_null /usr/include /dev/layer/null".
104  * Changing directory to /dev/layer/null will assign
105  * the root null-node (which was created when the null layer was mounted).
106  * Now consider opening "sys".  A vop_lookup would be
107  * done on the root null-node.  This operation would bypass through
108  * to the lower layer which would return a vnode representing
109  * the UFS "sys".  Null_bypass then builds a null-node
110  * aliasing the UFS "sys" and returns this to the caller.
111  * Later operations on the null-node "sys" will repeat this
112  * process when constructing other vnode stacks.
113  *
114  *
115  * CREATING OTHER FILE SYSTEM LAYERS
116  *
117  * One of the easiest ways to construct new file system layers is to make
118  * a copy of the null layer, rename all files and variables, and
119  * then begin modifing the copy.  Sed can be used to easily rename
120  * all variables.
121  *
122  * The umap layer is an example of a layer descended from the
123  * null layer.
124  *
125  *
126  * INVOKING OPERATIONS ON LOWER LAYERS
127  *
128  * There are two techniques to invoke operations on a lower layer
129  * when the operation cannot be completely bypassed.  Each method
130  * is appropriate in different situations.  In both cases,
131  * it is the responsibility of the aliasing layer to make
132  * the operation arguments "correct" for the lower layer
133  * by mapping an vnode arguments to the lower layer.
134  *
135  * The first approach is to call the aliasing layer's bypass routine.
136  * This method is most suitable when you wish to invoke the operation
137  * currently being hanldled on the lower layer.  It has the advantage
138  * that the bypass routine already must do argument mapping.
139  * An example of this is null_getattrs in the null layer.
140  *
141  * A second approach is to directly invoked vnode operations on
142  * the lower layer with the VOP_OPERATIONNAME interface.
143  * The advantage of this method is that it is easy to invoke
144  * arbitrary operations on the lower layer.  The disadvantage
145  * is that vnodes arguments must be manualy mapped.
146  *
147  */
148 
149 #include <sys/param.h>
150 #include <sys/systm.h>
151 #include <sys/proc.h>
152 #include <sys/time.h>
153 #include <sys/types.h>
154 #include <sys/vnode.h>
155 #include <sys/mount.h>
156 #include <sys/namei.h>
157 #include <sys/malloc.h>
158 #include <sys/buf.h>
159 #include <miscfs/nullfs/null.h>
160 
161 
162 int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
163 
164 /*
165  * This is the 10-Apr-92 bypass routine.
166  *    This version has been optimized for speed, throwing away some
167  * safety checks.  It should still always work, but it's not as
168  * robust to programmer errors.
169  *    Define SAFETY to include some error checking code.
170  *
171  * In general, we map all vnodes going down and unmap them on the way back.
172  * As an exception to this, vnodes can be marked "unmapped" by setting
173  * the Nth bit in operation's vdesc_flags.
174  *
175  * Also, some BSD vnode operations have the side effect of vrele'ing
176  * their arguments.  With stacking, the reference counts are held
177  * by the upper node, not the lower one, so we must handle these
178  * side-effects here.  This is not of concern in Sun-derived systems
179  * since there are no such side-effects.
180  *
181  * This makes the following assumptions:
182  * - only one returned vpp
183  * - no INOUT vpp's (Sun's vop_open has one of these)
184  * - the vnode operation vector of the first vnode should be used
185  *   to determine what implementation of the op should be invoked
186  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
187  *   problems on rmdir'ing mount points and renaming?)
188  */
189 int
190 null_bypass(ap)
191 	struct vop_generic_args /* {
192 		struct vnodeop_desc *a_desc;
193 		<other random data follows, presumably>
194 	} */ *ap;
195 {
196 	extern int (**null_vnodeop_p)();  /* not extern, really "forward" */
197 	register struct vnode **this_vp_p;
198 	int error;
199 	struct vnode *old_vps[VDESC_MAX_VPS];
200 	struct vnode **vps_p[VDESC_MAX_VPS];
201 	struct vnode ***vppp;
202 	struct vnodeop_desc *descp = ap->a_desc;
203 	int reles, i;
204 
205 	if (null_bug_bypass)
206 		printf ("null_bypass: %s\n", descp->vdesc_name);
207 
208 #ifdef SAFETY
209 	/*
210 	 * We require at least one vp.
211 	 */
212 	if (descp->vdesc_vp_offsets == NULL ||
213 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
214 		panic ("null_bypass: no vp's in map.\n");
215 #endif
216 
217 	/*
218 	 * Map the vnodes going in.
219 	 * Later, we'll invoke the operation based on
220 	 * the first mapped vnode's operation vector.
221 	 */
222 	reles = descp->vdesc_flags;
223 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
224 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
225 			break;   /* bail out at end of list */
226 		vps_p[i] = this_vp_p =
227 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
228 		/*
229 		 * We're not guaranteed that any but the first vnode
230 		 * are of our type.  Check for and don't map any
231 		 * that aren't.  (We must always map first vp or vclean fails.)
232 		 */
233 		if (i && (*this_vp_p == NULL ||
234 		    (*this_vp_p)->v_op != null_vnodeop_p)) {
235 			old_vps[i] = NULL;
236 		} else {
237 			old_vps[i] = *this_vp_p;
238 			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
239 			/*
240 			 * XXX - Several operations have the side effect
241 			 * of vrele'ing their vp's.  We must account for
242 			 * that.  (This should go away in the future.)
243 			 */
244 			if (reles & 1)
245 				VREF(*this_vp_p);
246 		}
247 
248 	}
249 
250 	/*
251 	 * Call the operation on the lower layer
252 	 * with the modified argument structure.
253 	 */
254 	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
255 
256 	/*
257 	 * Maintain the illusion of call-by-value
258 	 * by restoring vnodes in the argument structure
259 	 * to their original value.
260 	 */
261 	reles = descp->vdesc_flags;
262 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
263 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
264 			break;   /* bail out at end of list */
265 		if (old_vps[i]) {
266 			*(vps_p[i]) = old_vps[i];
267 			if (reles & 1)
268 				vrele(*(vps_p[i]));
269 		}
270 	}
271 
272 	/*
273 	 * Map the possible out-going vpp
274 	 * (Assumes that the lower layer always returns
275 	 * a VREF'ed vpp unless it gets an error.)
276 	 */
277 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
278 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
279 	    !error) {
280 		/*
281 		 * XXX - even though some ops have vpp returned vp's,
282 		 * several ops actually vrele this before returning.
283 		 * We must avoid these ops.
284 		 * (This should go away when these ops are regularized.)
285 		 */
286 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
287 			goto out;
288 		vppp = VOPARG_OFFSETTO(struct vnode***,
289 				 descp->vdesc_vpp_offset,ap);
290 		error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
291 	}
292 
293  out:
294 	return (error);
295 }
296 
297 /*
298  * We have to carry on the locking protocol on the null layer vnodes
299  * as we progress through the tree.
300  */
301 null_lookup(ap)
302 	struct vop_lookup_args /* {
303 		struct vnode * a_dvp;
304 		struct vnode ** a_vpp;
305 		struct componentname * a_cnp;
306 	} */ *ap;
307 {
308 	struct proc *p = ap->a_cnp->cn_proc;
309 	struct vop_lock_args lockargs;
310 	struct vop_unlock_args unlockargs;
311 	struct vnode *dvp, *vp;
312 	int error;
313 
314 	error = null_bypass(ap);
315 	/*
316 	 * We must do the same locking and unlocking at this layer as
317 	 * is done in the layers below us. We could figure this out
318 	 * based on the error return and the LASTCN, LOCKPARENT, and
319 	 * LOCKLEAF flags. However, it is more expidient to just find
320 	 * out the state of the lower level vnodes and set ours to the
321 	 * same state.
322 	 */
323 	dvp = ap->a_dvp;
324 	vp = *ap->a_vpp;
325 	if (dvp == vp)
326 		return (error);
327 	if (!VOP_ISLOCKED(dvp)) {
328 		unlockargs.a_vp = dvp;
329 		unlockargs.a_flags = 0;
330 		unlockargs.a_p = p;
331 		vop_nounlock(&unlockargs);
332 	}
333 	if (vp != NULL && VOP_ISLOCKED(vp)) {
334 		lockargs.a_vp = vp;
335 		lockargs.a_flags = LK_SHARED;
336 		lockargs.a_p = p;
337 		vop_nolock(&lockargs);
338 	}
339 	return (error);
340 }
341 
342 /*
343  *  We handle getattr only to change the fsid.
344  */
345 int
346 null_getattr(ap)
347 	struct vop_getattr_args /* {
348 		struct vnode *a_vp;
349 		struct vattr *a_vap;
350 		struct ucred *a_cred;
351 		struct proc *a_p;
352 	} */ *ap;
353 {
354 	int error;
355 
356 	if (error = null_bypass(ap))
357 		return (error);
358 	/* Requires that arguments be restored. */
359 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
360 	return (0);
361 }
362 
363 /*
364  * We need to process our own vnode lock and then clear the
365  * interlock flag as it applies only to our vnode, not the
366  * vnodes below us on the stack.
367  */
368 int
369 null_lock(ap)
370 	struct vop_lock_args /* {
371 		struct vnode *a_vp;
372 		int a_flags;
373 		struct proc *a_p;
374 	} */ *ap;
375 {
376 
377 	vop_nolock(ap);
378 	if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
379 		return (0);
380 	ap->a_flags &= ~LK_INTERLOCK;
381 	return (null_bypass(ap));
382 }
383 
384 /*
385  * We need to process our own vnode unlock and then clear the
386  * interlock flag as it applies only to our vnode, not the
387  * vnodes below us on the stack.
388  */
389 int
390 null_unlock(ap)
391 	struct vop_unlock_args /* {
392 		struct vnode *a_vp;
393 		int a_flags;
394 		struct proc *a_p;
395 	} */ *ap;
396 {
397 	struct vnode *vp = ap->a_vp;
398 
399 	vop_nounlock(ap);
400 	ap->a_flags &= ~LK_INTERLOCK;
401 	return (null_bypass(ap));
402 }
403 
404 int
405 null_inactive(ap)
406 	struct vop_inactive_args /* {
407 		struct vnode *a_vp;
408 		struct proc *a_p;
409 	} */ *ap;
410 {
411 	/*
412 	 * Do nothing (and _don't_ bypass).
413 	 * Wait to vrele lowervp until reclaim,
414 	 * so that until then our null_node is in the
415 	 * cache and reusable.
416 	 *
417 	 * NEEDSWORK: Someday, consider inactive'ing
418 	 * the lowervp and then trying to reactivate it
419 	 * with capabilities (v_id)
420 	 * like they do in the name lookup cache code.
421 	 * That's too much work for now.
422 	 */
423 	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
424 	return (0);
425 }
426 
427 int
428 null_reclaim(ap)
429 	struct vop_reclaim_args /* {
430 		struct vnode *a_vp;
431 		struct proc *a_p;
432 	} */ *ap;
433 {
434 	struct vnode *vp = ap->a_vp;
435 	struct null_node *xp = VTONULL(vp);
436 	struct vnode *lowervp = xp->null_lowervp;
437 
438 	/*
439 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
440 	 * so we can't call VOPs on ourself.
441 	 */
442 	/* After this assignment, this node will not be re-used. */
443 	xp->null_lowervp = NULL;
444 	LIST_REMOVE(xp, null_hash);
445 	FREE(vp->v_data, M_TEMP);
446 	vp->v_data = NULL;
447 	vrele (lowervp);
448 	return (0);
449 }
450 
451 int
452 null_print(ap)
453 	struct vop_print_args /* {
454 		struct vnode *a_vp;
455 	} */ *ap;
456 {
457 	register struct vnode *vp = ap->a_vp;
458 	printf ("\ttag VT_NULLFS, vp=%x, lowervp=%x\n", vp, NULLVPTOLOWERVP(vp));
459 	return (0);
460 }
461 
462 /*
463  * XXX - vop_strategy must be hand coded because it has no
464  * vnode in its arguments.
465  * This goes away with a merged VM/buffer cache.
466  */
467 int
468 null_strategy(ap)
469 	struct vop_strategy_args /* {
470 		struct buf *a_bp;
471 	} */ *ap;
472 {
473 	struct buf *bp = ap->a_bp;
474 	int error;
475 	struct vnode *savedvp;
476 
477 	savedvp = bp->b_vp;
478 	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
479 
480 	error = VOP_STRATEGY(bp);
481 
482 	bp->b_vp = savedvp;
483 
484 	return (error);
485 }
486 
487 /*
488  * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
489  * vnode in its arguments.
490  * This goes away with a merged VM/buffer cache.
491  */
492 int
493 null_bwrite(ap)
494 	struct vop_bwrite_args /* {
495 		struct buf *a_bp;
496 	} */ *ap;
497 {
498 	struct buf *bp = ap->a_bp;
499 	int error;
500 	struct vnode *savedvp;
501 
502 	savedvp = bp->b_vp;
503 	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
504 
505 	error = VOP_BWRITE(bp);
506 
507 	bp->b_vp = savedvp;
508 
509 	return (error);
510 }
511 
512 /*
513  * Global vfs data structures
514  */
515 int (**null_vnodeop_p)();
516 struct vnodeopv_entry_desc null_vnodeop_entries[] = {
517 	{ &vop_default_desc, null_bypass },
518 
519 	{ &vop_lookup_desc, null_lookup },
520 	{ &vop_getattr_desc, null_getattr },
521 	{ &vop_lock_desc, null_lock },
522 	{ &vop_unlock_desc, null_unlock },
523 	{ &vop_inactive_desc, null_inactive },
524 	{ &vop_reclaim_desc, null_reclaim },
525 	{ &vop_print_desc, null_print },
526 
527 	{ &vop_strategy_desc, null_strategy },
528 	{ &vop_bwrite_desc, null_bwrite },
529 
530 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
531 };
532 struct vnodeopv_desc null_vnodeop_opv_desc =
533 	{ &null_vnodeop_p, null_vnodeop_entries };
534