Deleted Added
full compact
null_vnops.c (97072) null_vnops.c (98175)
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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
37 *
38 * Ancestors:
39 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
40 * ...and...
41 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
42 *
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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
37 *
38 * Ancestors:
39 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
40 * ...and...
41 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
42 *
43 * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 97072 2002-05-21 18:07:33Z semenu $
43 * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 98175 2002-06-13 17:30:40Z semenu $
44 */
45
46/*
47 * Null Layer
48 *
49 * (See mount_nullfs(8) for more information.)
50 *
51 * The null layer duplicates a portion of the filesystem
52 * name space under a new name. In this respect, it is
53 * similar to the loopback filesystem. It differs from
54 * the loopback fs in two respects: it is implemented using
55 * a stackable layers techniques, and its "null-node"s stack above
56 * all lower-layer vnodes, not just over directory vnodes.
57 *
58 * The null layer has two purposes. First, it serves as a demonstration
59 * of layering by proving a layer which does nothing. (It actually
60 * does everything the loopback filesystem does, which is slightly
61 * more than nothing.) Second, the null layer can serve as a prototype
62 * layer. Since it provides all necessary layer framework,
63 * new filesystem layers can be created very easily be starting
64 * with a null layer.
65 *
66 * The remainder of this man page examines the null layer as a basis
67 * for constructing new layers.
68 *
69 *
70 * INSTANTIATING NEW NULL LAYERS
71 *
72 * New null layers are created with mount_nullfs(8).
73 * Mount_nullfs(8) takes two arguments, the pathname
74 * of the lower vfs (target-pn) and the pathname where the null
75 * layer will appear in the namespace (alias-pn). After
76 * the null layer is put into place, the contents
77 * of target-pn subtree will be aliased under alias-pn.
78 *
79 *
80 * OPERATION OF A NULL LAYER
81 *
82 * The null layer is the minimum filesystem layer,
83 * simply bypassing all possible operations to the lower layer
84 * for processing there. The majority of its activity centers
85 * on the bypass routine, through which nearly all vnode operations
86 * pass.
87 *
88 * The bypass routine accepts arbitrary vnode operations for
89 * handling by the lower layer. It begins by examing vnode
90 * operation arguments and replacing any null-nodes by their
91 * lower-layer equivlants. It then invokes the operation
92 * on the lower layer. Finally, it replaces the null-nodes
93 * in the arguments and, if a vnode is return by the operation,
94 * stacks a null-node on top of the returned vnode.
95 *
96 * Although bypass handles most operations, vop_getattr, vop_lock,
97 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
98 * bypassed. Vop_getattr must change the fsid being returned.
99 * Vop_lock and vop_unlock must handle any locking for the
100 * current vnode as well as pass the lock request down.
101 * Vop_inactive and vop_reclaim are not bypassed so that
102 * they can handle freeing null-layer specific data. Vop_print
103 * is not bypassed to avoid excessive debugging information.
104 * Also, certain vnode operations change the locking state within
105 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
106 * and symlink). Ideally these operations should not change the
107 * lock state, but should be changed to let the caller of the
108 * function unlock them. Otherwise all intermediate vnode layers
109 * (such as union, umapfs, etc) must catch these functions to do
110 * the necessary locking at their layer.
111 *
112 *
113 * INSTANTIATING VNODE STACKS
114 *
115 * Mounting associates the null layer with a lower layer,
116 * effect stacking two VFSes. Vnode stacks are instead
117 * created on demand as files are accessed.
118 *
119 * The initial mount creates a single vnode stack for the
120 * root of the new null layer. All other vnode stacks
121 * are created as a result of vnode operations on
122 * this or other null vnode stacks.
123 *
124 * New vnode stacks come into existance as a result of
125 * an operation which returns a vnode.
126 * The bypass routine stacks a null-node above the new
127 * vnode before returning it to the caller.
128 *
129 * For example, imagine mounting a null layer with
130 * "mount_nullfs /usr/include /dev/layer/null".
131 * Changing directory to /dev/layer/null will assign
132 * the root null-node (which was created when the null layer was mounted).
133 * Now consider opening "sys". A vop_lookup would be
134 * done on the root null-node. This operation would bypass through
135 * to the lower layer which would return a vnode representing
136 * the UFS "sys". Null_bypass then builds a null-node
137 * aliasing the UFS "sys" and returns this to the caller.
138 * Later operations on the null-node "sys" will repeat this
139 * process when constructing other vnode stacks.
140 *
141 *
142 * CREATING OTHER FILE SYSTEM LAYERS
143 *
144 * One of the easiest ways to construct new filesystem layers is to make
145 * a copy of the null layer, rename all files and variables, and
146 * then begin modifing the copy. Sed can be used to easily rename
147 * all variables.
148 *
149 * The umap layer is an example of a layer descended from the
150 * null layer.
151 *
152 *
153 * INVOKING OPERATIONS ON LOWER LAYERS
154 *
155 * There are two techniques to invoke operations on a lower layer
156 * when the operation cannot be completely bypassed. Each method
157 * is appropriate in different situations. In both cases,
158 * it is the responsibility of the aliasing layer to make
159 * the operation arguments "correct" for the lower layer
160 * by mapping an vnode arguments to the lower layer.
161 *
162 * The first approach is to call the aliasing layer's bypass routine.
163 * This method is most suitable when you wish to invoke the operation
164 * currently being handled on the lower layer. It has the advantage
165 * that the bypass routine already must do argument mapping.
166 * An example of this is null_getattrs in the null layer.
167 *
168 * A second approach is to directly invoke vnode operations on
169 * the lower layer with the VOP_OPERATIONNAME interface.
170 * The advantage of this method is that it is easy to invoke
171 * arbitrary operations on the lower layer. The disadvantage
172 * is that vnode arguments must be manualy mapped.
173 *
174 */
175
176#include <sys/param.h>
177#include <sys/systm.h>
178#include <sys/conf.h>
179#include <sys/kernel.h>
180#include <sys/lock.h>
181#include <sys/malloc.h>
182#include <sys/mount.h>
183#include <sys/mutex.h>
184#include <sys/namei.h>
185#include <sys/sysctl.h>
186#include <sys/vnode.h>
187
188#include <fs/nullfs/null.h>
189
190#include <vm/vm.h>
191#include <vm/vm_extern.h>
192#include <vm/vm_object.h>
193#include <vm/vnode_pager.h>
194
195static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
196SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
197 &null_bug_bypass, 0, "");
198
199static int null_access(struct vop_access_args *ap);
200static int null_createvobject(struct vop_createvobject_args *ap);
201static int null_destroyvobject(struct vop_destroyvobject_args *ap);
202static int null_getattr(struct vop_getattr_args *ap);
203static int null_getvobject(struct vop_getvobject_args *ap);
204static int null_inactive(struct vop_inactive_args *ap);
205static int null_islocked(struct vop_islocked_args *ap);
206static int null_lock(struct vop_lock_args *ap);
207static int null_lookup(struct vop_lookup_args *ap);
208static int null_open(struct vop_open_args *ap);
209static int null_print(struct vop_print_args *ap);
210static int null_reclaim(struct vop_reclaim_args *ap);
211static int null_rename(struct vop_rename_args *ap);
212static int null_setattr(struct vop_setattr_args *ap);
213static int null_unlock(struct vop_unlock_args *ap);
214
215/*
216 * This is the 10-Apr-92 bypass routine.
217 * This version has been optimized for speed, throwing away some
218 * safety checks. It should still always work, but it's not as
219 * robust to programmer errors.
220 *
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.
224 *
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.
230 *
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?)
238 */
239int
240null_bypass(ap)
241 struct vop_generic_args /* {
242 struct vnodeop_desc *a_desc;
243 <other random data follows, presumably>
244 } */ *ap;
245{
246 register struct vnode **this_vp_p;
247 int error;
248 struct vnode *old_vps[VDESC_MAX_VPS];
249 struct vnode **vps_p[VDESC_MAX_VPS];
250 struct vnode ***vppp;
251 struct vnodeop_desc *descp = ap->a_desc;
252 int reles, i;
253
254 if (null_bug_bypass)
255 printf ("null_bypass: %s\n", descp->vdesc_name);
256
257#ifdef DIAGNOSTIC
258 /*
259 * We require at least one vp.
260 */
261 if (descp->vdesc_vp_offsets == NULL ||
262 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
263 panic ("null_bypass: no vp's in map");
264#endif
265
266 /*
267 * Map the vnodes going in.
268 * Later, we'll invoke the operation based on
269 * the first mapped vnode's operation vector.
270 */
271 reles = descp->vdesc_flags;
272 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
273 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
274 break; /* bail out at end of list */
275 vps_p[i] = this_vp_p =
276 VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
277 /*
278 * We're not guaranteed that any but the first vnode
279 * are of our type. Check for and don't map any
280 * that aren't. (We must always map first vp or vclean fails.)
281 */
282 if (i && (*this_vp_p == NULLVP ||
283 (*this_vp_p)->v_op != null_vnodeop_p)) {
284 old_vps[i] = NULLVP;
285 } else {
286 old_vps[i] = *this_vp_p;
287 *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
288 /*
289 * XXX - Several operations have the side effect
290 * of vrele'ing their vp's. We must account for
291 * that. (This should go away in the future.)
292 */
293 if (reles & VDESC_VP0_WILLRELE)
294 VREF(*this_vp_p);
295 }
296
297 }
298
299 /*
300 * Call the operation on the lower layer
301 * with the modified argument structure.
302 */
303 if (vps_p[0] && *vps_p[0])
304 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
305 else {
306 printf("null_bypass: no map for %s\n", descp->vdesc_name);
307 error = EINVAL;
308 }
309
310 /*
311 * Maintain the illusion of call-by-value
312 * by restoring vnodes in the argument structure
313 * to their original value.
314 */
315 reles = descp->vdesc_flags;
316 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
317 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
318 break; /* bail out at end of list */
319 if (old_vps[i]) {
320 *(vps_p[i]) = old_vps[i];
321#if 0
322 if (reles & VDESC_VP0_WILLUNLOCK)
323 VOP_UNLOCK(*(vps_p[i]), LK_THISLAYER, curthread);
324#endif
325 if (reles & VDESC_VP0_WILLRELE)
326 vrele(*(vps_p[i]));
327 }
328 }
329
330 /*
331 * Map the possible out-going vpp
332 * (Assumes that the lower layer always returns
333 * a VREF'ed vpp unless it gets an error.)
334 */
335 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
336 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
337 !error) {
338 /*
339 * XXX - even though some ops have vpp returned vp's,
340 * several ops actually vrele this before returning.
341 * We must avoid these ops.
342 * (This should go away when these ops are regularized.)
343 */
344 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
345 goto out;
346 vppp = VOPARG_OFFSETTO(struct vnode***,
347 descp->vdesc_vpp_offset,ap);
348 if (*vppp)
349 error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
350 }
351
352 out:
353 return (error);
354}
355
356/*
357 * We have to carry on the locking protocol on the null layer vnodes
358 * as we progress through the tree. We also have to enforce read-only
359 * if this layer is mounted read-only.
360 */
361static int
362null_lookup(ap)
363 struct vop_lookup_args /* {
364 struct vnode * a_dvp;
365 struct vnode ** a_vpp;
366 struct componentname * a_cnp;
367 } */ *ap;
368{
369 struct componentname *cnp = ap->a_cnp;
370 struct vnode *dvp = ap->a_dvp;
371 struct thread *td = cnp->cn_thread;
372 int flags = cnp->cn_flags;
373 struct vnode *vp, *ldvp, *lvp;
374 int error;
375
376 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
377 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
378 return (EROFS);
379 /*
380 * Although it is possible to call null_bypass(), we'll do
381 * a direct call to reduce overhead
382 */
383 ldvp = NULLVPTOLOWERVP(dvp);
384 vp = lvp = NULL;
385 error = VOP_LOOKUP(ldvp, &lvp, cnp);
386 if (error == EJUSTRETURN && (flags & ISLASTCN) &&
387 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
388 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
389 error = EROFS;
390
391 /*
392 * Rely only on the PDIRUNLOCK flag which should be carefully
393 * tracked by underlying filesystem.
394 */
395 if (cnp->cn_flags & PDIRUNLOCK)
396 VOP_UNLOCK(dvp, LK_THISLAYER, td);
397 if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
398 if (ldvp == lvp) {
399 *ap->a_vpp = dvp;
400 VREF(dvp);
401 vrele(lvp);
402 } else {
403 error = null_node_create(dvp->v_mount, lvp, &vp);
404 if (error == 0)
405 *ap->a_vpp = vp;
406 }
407 }
408 return (error);
409}
410
411/*
412 * Setattr call. Disallow write attempts if the layer is mounted read-only.
413 */
414int
415null_setattr(ap)
416 struct vop_setattr_args /* {
417 struct vnodeop_desc *a_desc;
418 struct vnode *a_vp;
419 struct vattr *a_vap;
420 struct ucred *a_cred;
421 struct thread *a_td;
422 } */ *ap;
423{
424 struct vnode *vp = ap->a_vp;
425 struct vattr *vap = ap->a_vap;
426
427 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
428 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
429 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
430 (vp->v_mount->mnt_flag & MNT_RDONLY))
431 return (EROFS);
432 if (vap->va_size != VNOVAL) {
433 switch (vp->v_type) {
434 case VDIR:
435 return (EISDIR);
436 case VCHR:
437 case VBLK:
438 case VSOCK:
439 case VFIFO:
440 if (vap->va_flags != VNOVAL)
441 return (EOPNOTSUPP);
442 return (0);
443 case VREG:
444 case VLNK:
445 default:
446 /*
447 * Disallow write attempts if the filesystem is
448 * mounted read-only.
449 */
450 if (vp->v_mount->mnt_flag & MNT_RDONLY)
451 return (EROFS);
452 }
453 }
454
455 return (null_bypass((struct vop_generic_args *)ap));
456}
457
458/*
459 * We handle getattr only to change the fsid.
460 */
461static int
462null_getattr(ap)
463 struct vop_getattr_args /* {
464 struct vnode *a_vp;
465 struct vattr *a_vap;
466 struct ucred *a_cred;
467 struct thread *a_td;
468 } */ *ap;
469{
470 int error;
471
472 if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
473 return (error);
474
475 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
476 return (0);
477}
478
479/*
480 * Handle to disallow write access if mounted read-only.
481 */
482static int
483null_access(ap)
484 struct vop_access_args /* {
485 struct vnode *a_vp;
486 int a_mode;
487 struct ucred *a_cred;
488 struct thread *a_td;
489 } */ *ap;
490{
491 struct vnode *vp = ap->a_vp;
492 mode_t mode = ap->a_mode;
493
494 /*
495 * Disallow write attempts on read-only layers;
496 * unless the file is a socket, fifo, or a block or
497 * character device resident on the filesystem.
498 */
499 if (mode & VWRITE) {
500 switch (vp->v_type) {
501 case VDIR:
502 case VLNK:
503 case VREG:
504 if (vp->v_mount->mnt_flag & MNT_RDONLY)
505 return (EROFS);
506 break;
507 default:
508 break;
509 }
510 }
511 return (null_bypass((struct vop_generic_args *)ap));
512}
513
514/*
515 * We must handle open to be able to catch MNT_NODEV and friends.
516 */
517static int
518null_open(ap)
519 struct vop_open_args /* {
520 struct vnode *a_vp;
521 int a_mode;
522 struct ucred *a_cred;
523 struct thread *a_td;
524 } */ *ap;
525{
526 struct vnode *vp = ap->a_vp;
527 struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
528
529 if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
530 (lvp->v_type == VBLK || lvp->v_type == VCHR))
531 return ENXIO;
532
533 return (null_bypass((struct vop_generic_args *)ap));
534}
535
536/*
537 * We handle this to eliminate null FS to lower FS
538 * file moving. Don't know why we don't allow this,
539 * possibly we should.
540 */
541static int
542null_rename(ap)
543 struct vop_rename_args /* {
544 struct vnode *a_fdvp;
545 struct vnode *a_fvp;
546 struct componentname *a_fcnp;
547 struct vnode *a_tdvp;
548 struct vnode *a_tvp;
549 struct componentname *a_tcnp;
550 } */ *ap;
551{
552 struct vnode *tdvp = ap->a_tdvp;
553 struct vnode *fvp = ap->a_fvp;
554 struct vnode *fdvp = ap->a_fdvp;
555 struct vnode *tvp = ap->a_tvp;
556
557 /* Check for cross-device rename. */
558 if ((fvp->v_mount != tdvp->v_mount) ||
559 (tvp && (fvp->v_mount != tvp->v_mount))) {
560 if (tdvp == tvp)
561 vrele(tdvp);
562 else
563 vput(tdvp);
564 if (tvp)
565 vput(tvp);
566 vrele(fdvp);
567 vrele(fvp);
568 return (EXDEV);
569 }
570
571 return (null_bypass((struct vop_generic_args *)ap));
572}
573
574/*
575 * We need to process our own vnode lock and then clear the
576 * interlock flag as it applies only to our vnode, not the
577 * vnodes below us on the stack.
578 */
579static int
580null_lock(ap)
581 struct vop_lock_args /* {
582 struct vnode *a_vp;
583 int a_flags;
584 struct thread *a_td;
585 } */ *ap;
586{
587 struct vnode *vp = ap->a_vp;
588 int flags = ap->a_flags;
589 struct thread *td = ap->a_td;
590 struct vnode *lvp;
591 int error;
592
593 if (flags & LK_THISLAYER) {
594 if (vp->v_vnlock != NULL) {
595 /* lock is shared across layers */
596 if (flags & LK_INTERLOCK)
597 mtx_unlock(&vp->v_interlock);
598 return 0;
599 }
600 error = lockmgr(&vp->v_lock, flags & ~LK_THISLAYER,
601 &vp->v_interlock, td);
602 return (error);
603 }
604
605 if (vp->v_vnlock != NULL) {
606 /*
607 * The lower level has exported a struct lock to us. Use
608 * it so that all vnodes in the stack lock and unlock
609 * simultaneously. Note: we don't DRAIN the lock as DRAIN
610 * decommissions the lock - just because our vnode is
611 * going away doesn't mean the struct lock below us is.
612 * LK_EXCLUSIVE is fine.
613 */
614 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
615 NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
616 return(lockmgr(vp->v_vnlock,
617 (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
618 &vp->v_interlock, td));
619 }
620 return(lockmgr(vp->v_vnlock, flags, &vp->v_interlock, td));
621 } else {
622 /*
623 * To prevent race conditions involving doing a lookup
624 * on "..", we have to lock the lower node, then lock our
625 * node. Most of the time it won't matter that we lock our
626 * node (as any locking would need the lower one locked
627 * first). But we can LK_DRAIN the upper lock as a step
628 * towards decomissioning it.
629 */
630 lvp = NULLVPTOLOWERVP(vp);
631 if (lvp == NULL)
632 return (lockmgr(&vp->v_lock, flags, &vp->v_interlock, td));
633 if (flags & LK_INTERLOCK) {
634 mtx_unlock(&vp->v_interlock);
635 flags &= ~LK_INTERLOCK;
636 }
637 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
638 error = VOP_LOCK(lvp,
639 (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE, td);
640 } else
641 error = VOP_LOCK(lvp, flags, td);
642 if (error)
643 return (error);
644 error = lockmgr(&vp->v_lock, flags, &vp->v_interlock, td);
645 if (error)
646 VOP_UNLOCK(lvp, 0, td);
647 return (error);
648 }
649}
650
651/*
652 * We need to process our own vnode unlock and then clear the
653 * interlock flag as it applies only to our vnode, not the
654 * vnodes below us on the stack.
655 */
656static int
657null_unlock(ap)
658 struct vop_unlock_args /* {
659 struct vnode *a_vp;
660 int a_flags;
661 struct thread *a_td;
662 } */ *ap;
663{
664 struct vnode *vp = ap->a_vp;
665 int flags = ap->a_flags;
666 struct thread *td = ap->a_td;
667 struct vnode *lvp;
668
669 if (vp->v_vnlock != NULL) {
670 if (flags & LK_THISLAYER)
671 return 0; /* the lock is shared across layers */
672 flags &= ~LK_THISLAYER;
673 return (lockmgr(vp->v_vnlock, flags | LK_RELEASE,
674 &vp->v_interlock, td));
675 }
676 lvp = NULLVPTOLOWERVP(vp);
677 if (lvp == NULL)
678 return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, td));
679 if ((flags & LK_THISLAYER) == 0) {
680 if (flags & LK_INTERLOCK) {
681 mtx_unlock(&vp->v_interlock);
682 flags &= ~LK_INTERLOCK;
683 }
684 VOP_UNLOCK(lvp, flags & ~LK_INTERLOCK, td);
685 } else
686 flags &= ~LK_THISLAYER;
687 return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, td));
688}
689
690static int
691null_islocked(ap)
692 struct vop_islocked_args /* {
693 struct vnode *a_vp;
694 struct thread *a_td;
695 } */ *ap;
696{
697 struct vnode *vp = ap->a_vp;
698 struct thread *td = ap->a_td;
699
700 if (vp->v_vnlock != NULL)
701 return (lockstatus(vp->v_vnlock, td));
702 return (lockstatus(&vp->v_lock, td));
703}
704
705/*
706 * There is no way to tell that someone issued remove/rmdir operation
707 * on the underlying filesystem. For now we just have to release lowevrp
708 * as soon as possible.
709 */
710static int
711null_inactive(ap)
712 struct vop_inactive_args /* {
713 struct vnode *a_vp;
714 struct thread *a_td;
715 } */ *ap;
716{
717 struct vnode *vp = ap->a_vp;
44 */
45
46/*
47 * Null Layer
48 *
49 * (See mount_nullfs(8) for more information.)
50 *
51 * The null layer duplicates a portion of the filesystem
52 * name space under a new name. In this respect, it is
53 * similar to the loopback filesystem. It differs from
54 * the loopback fs in two respects: it is implemented using
55 * a stackable layers techniques, and its "null-node"s stack above
56 * all lower-layer vnodes, not just over directory vnodes.
57 *
58 * The null layer has two purposes. First, it serves as a demonstration
59 * of layering by proving a layer which does nothing. (It actually
60 * does everything the loopback filesystem does, which is slightly
61 * more than nothing.) Second, the null layer can serve as a prototype
62 * layer. Since it provides all necessary layer framework,
63 * new filesystem layers can be created very easily be starting
64 * with a null layer.
65 *
66 * The remainder of this man page examines the null layer as a basis
67 * for constructing new layers.
68 *
69 *
70 * INSTANTIATING NEW NULL LAYERS
71 *
72 * New null layers are created with mount_nullfs(8).
73 * Mount_nullfs(8) takes two arguments, the pathname
74 * of the lower vfs (target-pn) and the pathname where the null
75 * layer will appear in the namespace (alias-pn). After
76 * the null layer is put into place, the contents
77 * of target-pn subtree will be aliased under alias-pn.
78 *
79 *
80 * OPERATION OF A NULL LAYER
81 *
82 * The null layer is the minimum filesystem layer,
83 * simply bypassing all possible operations to the lower layer
84 * for processing there. The majority of its activity centers
85 * on the bypass routine, through which nearly all vnode operations
86 * pass.
87 *
88 * The bypass routine accepts arbitrary vnode operations for
89 * handling by the lower layer. It begins by examing vnode
90 * operation arguments and replacing any null-nodes by their
91 * lower-layer equivlants. It then invokes the operation
92 * on the lower layer. Finally, it replaces the null-nodes
93 * in the arguments and, if a vnode is return by the operation,
94 * stacks a null-node on top of the returned vnode.
95 *
96 * Although bypass handles most operations, vop_getattr, vop_lock,
97 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
98 * bypassed. Vop_getattr must change the fsid being returned.
99 * Vop_lock and vop_unlock must handle any locking for the
100 * current vnode as well as pass the lock request down.
101 * Vop_inactive and vop_reclaim are not bypassed so that
102 * they can handle freeing null-layer specific data. Vop_print
103 * is not bypassed to avoid excessive debugging information.
104 * Also, certain vnode operations change the locking state within
105 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
106 * and symlink). Ideally these operations should not change the
107 * lock state, but should be changed to let the caller of the
108 * function unlock them. Otherwise all intermediate vnode layers
109 * (such as union, umapfs, etc) must catch these functions to do
110 * the necessary locking at their layer.
111 *
112 *
113 * INSTANTIATING VNODE STACKS
114 *
115 * Mounting associates the null layer with a lower layer,
116 * effect stacking two VFSes. Vnode stacks are instead
117 * created on demand as files are accessed.
118 *
119 * The initial mount creates a single vnode stack for the
120 * root of the new null layer. All other vnode stacks
121 * are created as a result of vnode operations on
122 * this or other null vnode stacks.
123 *
124 * New vnode stacks come into existance as a result of
125 * an operation which returns a vnode.
126 * The bypass routine stacks a null-node above the new
127 * vnode before returning it to the caller.
128 *
129 * For example, imagine mounting a null layer with
130 * "mount_nullfs /usr/include /dev/layer/null".
131 * Changing directory to /dev/layer/null will assign
132 * the root null-node (which was created when the null layer was mounted).
133 * Now consider opening "sys". A vop_lookup would be
134 * done on the root null-node. This operation would bypass through
135 * to the lower layer which would return a vnode representing
136 * the UFS "sys". Null_bypass then builds a null-node
137 * aliasing the UFS "sys" and returns this to the caller.
138 * Later operations on the null-node "sys" will repeat this
139 * process when constructing other vnode stacks.
140 *
141 *
142 * CREATING OTHER FILE SYSTEM LAYERS
143 *
144 * One of the easiest ways to construct new filesystem layers is to make
145 * a copy of the null layer, rename all files and variables, and
146 * then begin modifing the copy. Sed can be used to easily rename
147 * all variables.
148 *
149 * The umap layer is an example of a layer descended from the
150 * null layer.
151 *
152 *
153 * INVOKING OPERATIONS ON LOWER LAYERS
154 *
155 * There are two techniques to invoke operations on a lower layer
156 * when the operation cannot be completely bypassed. Each method
157 * is appropriate in different situations. In both cases,
158 * it is the responsibility of the aliasing layer to make
159 * the operation arguments "correct" for the lower layer
160 * by mapping an vnode arguments to the lower layer.
161 *
162 * The first approach is to call the aliasing layer's bypass routine.
163 * This method is most suitable when you wish to invoke the operation
164 * currently being handled on the lower layer. It has the advantage
165 * that the bypass routine already must do argument mapping.
166 * An example of this is null_getattrs in the null layer.
167 *
168 * A second approach is to directly invoke vnode operations on
169 * the lower layer with the VOP_OPERATIONNAME interface.
170 * The advantage of this method is that it is easy to invoke
171 * arbitrary operations on the lower layer. The disadvantage
172 * is that vnode arguments must be manualy mapped.
173 *
174 */
175
176#include <sys/param.h>
177#include <sys/systm.h>
178#include <sys/conf.h>
179#include <sys/kernel.h>
180#include <sys/lock.h>
181#include <sys/malloc.h>
182#include <sys/mount.h>
183#include <sys/mutex.h>
184#include <sys/namei.h>
185#include <sys/sysctl.h>
186#include <sys/vnode.h>
187
188#include <fs/nullfs/null.h>
189
190#include <vm/vm.h>
191#include <vm/vm_extern.h>
192#include <vm/vm_object.h>
193#include <vm/vnode_pager.h>
194
195static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
196SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
197 &null_bug_bypass, 0, "");
198
199static int null_access(struct vop_access_args *ap);
200static int null_createvobject(struct vop_createvobject_args *ap);
201static int null_destroyvobject(struct vop_destroyvobject_args *ap);
202static int null_getattr(struct vop_getattr_args *ap);
203static int null_getvobject(struct vop_getvobject_args *ap);
204static int null_inactive(struct vop_inactive_args *ap);
205static int null_islocked(struct vop_islocked_args *ap);
206static int null_lock(struct vop_lock_args *ap);
207static int null_lookup(struct vop_lookup_args *ap);
208static int null_open(struct vop_open_args *ap);
209static int null_print(struct vop_print_args *ap);
210static int null_reclaim(struct vop_reclaim_args *ap);
211static int null_rename(struct vop_rename_args *ap);
212static int null_setattr(struct vop_setattr_args *ap);
213static int null_unlock(struct vop_unlock_args *ap);
214
215/*
216 * This is the 10-Apr-92 bypass routine.
217 * This version has been optimized for speed, throwing away some
218 * safety checks. It should still always work, but it's not as
219 * robust to programmer errors.
220 *
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.
224 *
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.
230 *
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?)
238 */
239int
240null_bypass(ap)
241 struct vop_generic_args /* {
242 struct vnodeop_desc *a_desc;
243 <other random data follows, presumably>
244 } */ *ap;
245{
246 register struct vnode **this_vp_p;
247 int error;
248 struct vnode *old_vps[VDESC_MAX_VPS];
249 struct vnode **vps_p[VDESC_MAX_VPS];
250 struct vnode ***vppp;
251 struct vnodeop_desc *descp = ap->a_desc;
252 int reles, i;
253
254 if (null_bug_bypass)
255 printf ("null_bypass: %s\n", descp->vdesc_name);
256
257#ifdef DIAGNOSTIC
258 /*
259 * We require at least one vp.
260 */
261 if (descp->vdesc_vp_offsets == NULL ||
262 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
263 panic ("null_bypass: no vp's in map");
264#endif
265
266 /*
267 * Map the vnodes going in.
268 * Later, we'll invoke the operation based on
269 * the first mapped vnode's operation vector.
270 */
271 reles = descp->vdesc_flags;
272 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
273 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
274 break; /* bail out at end of list */
275 vps_p[i] = this_vp_p =
276 VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
277 /*
278 * We're not guaranteed that any but the first vnode
279 * are of our type. Check for and don't map any
280 * that aren't. (We must always map first vp or vclean fails.)
281 */
282 if (i && (*this_vp_p == NULLVP ||
283 (*this_vp_p)->v_op != null_vnodeop_p)) {
284 old_vps[i] = NULLVP;
285 } else {
286 old_vps[i] = *this_vp_p;
287 *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
288 /*
289 * XXX - Several operations have the side effect
290 * of vrele'ing their vp's. We must account for
291 * that. (This should go away in the future.)
292 */
293 if (reles & VDESC_VP0_WILLRELE)
294 VREF(*this_vp_p);
295 }
296
297 }
298
299 /*
300 * Call the operation on the lower layer
301 * with the modified argument structure.
302 */
303 if (vps_p[0] && *vps_p[0])
304 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
305 else {
306 printf("null_bypass: no map for %s\n", descp->vdesc_name);
307 error = EINVAL;
308 }
309
310 /*
311 * Maintain the illusion of call-by-value
312 * by restoring vnodes in the argument structure
313 * to their original value.
314 */
315 reles = descp->vdesc_flags;
316 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
317 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
318 break; /* bail out at end of list */
319 if (old_vps[i]) {
320 *(vps_p[i]) = old_vps[i];
321#if 0
322 if (reles & VDESC_VP0_WILLUNLOCK)
323 VOP_UNLOCK(*(vps_p[i]), LK_THISLAYER, curthread);
324#endif
325 if (reles & VDESC_VP0_WILLRELE)
326 vrele(*(vps_p[i]));
327 }
328 }
329
330 /*
331 * Map the possible out-going vpp
332 * (Assumes that the lower layer always returns
333 * a VREF'ed vpp unless it gets an error.)
334 */
335 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
336 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
337 !error) {
338 /*
339 * XXX - even though some ops have vpp returned vp's,
340 * several ops actually vrele this before returning.
341 * We must avoid these ops.
342 * (This should go away when these ops are regularized.)
343 */
344 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
345 goto out;
346 vppp = VOPARG_OFFSETTO(struct vnode***,
347 descp->vdesc_vpp_offset,ap);
348 if (*vppp)
349 error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
350 }
351
352 out:
353 return (error);
354}
355
356/*
357 * We have to carry on the locking protocol on the null layer vnodes
358 * as we progress through the tree. We also have to enforce read-only
359 * if this layer is mounted read-only.
360 */
361static int
362null_lookup(ap)
363 struct vop_lookup_args /* {
364 struct vnode * a_dvp;
365 struct vnode ** a_vpp;
366 struct componentname * a_cnp;
367 } */ *ap;
368{
369 struct componentname *cnp = ap->a_cnp;
370 struct vnode *dvp = ap->a_dvp;
371 struct thread *td = cnp->cn_thread;
372 int flags = cnp->cn_flags;
373 struct vnode *vp, *ldvp, *lvp;
374 int error;
375
376 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
377 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
378 return (EROFS);
379 /*
380 * Although it is possible to call null_bypass(), we'll do
381 * a direct call to reduce overhead
382 */
383 ldvp = NULLVPTOLOWERVP(dvp);
384 vp = lvp = NULL;
385 error = VOP_LOOKUP(ldvp, &lvp, cnp);
386 if (error == EJUSTRETURN && (flags & ISLASTCN) &&
387 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
388 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
389 error = EROFS;
390
391 /*
392 * Rely only on the PDIRUNLOCK flag which should be carefully
393 * tracked by underlying filesystem.
394 */
395 if (cnp->cn_flags & PDIRUNLOCK)
396 VOP_UNLOCK(dvp, LK_THISLAYER, td);
397 if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
398 if (ldvp == lvp) {
399 *ap->a_vpp = dvp;
400 VREF(dvp);
401 vrele(lvp);
402 } else {
403 error = null_node_create(dvp->v_mount, lvp, &vp);
404 if (error == 0)
405 *ap->a_vpp = vp;
406 }
407 }
408 return (error);
409}
410
411/*
412 * Setattr call. Disallow write attempts if the layer is mounted read-only.
413 */
414int
415null_setattr(ap)
416 struct vop_setattr_args /* {
417 struct vnodeop_desc *a_desc;
418 struct vnode *a_vp;
419 struct vattr *a_vap;
420 struct ucred *a_cred;
421 struct thread *a_td;
422 } */ *ap;
423{
424 struct vnode *vp = ap->a_vp;
425 struct vattr *vap = ap->a_vap;
426
427 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
428 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
429 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
430 (vp->v_mount->mnt_flag & MNT_RDONLY))
431 return (EROFS);
432 if (vap->va_size != VNOVAL) {
433 switch (vp->v_type) {
434 case VDIR:
435 return (EISDIR);
436 case VCHR:
437 case VBLK:
438 case VSOCK:
439 case VFIFO:
440 if (vap->va_flags != VNOVAL)
441 return (EOPNOTSUPP);
442 return (0);
443 case VREG:
444 case VLNK:
445 default:
446 /*
447 * Disallow write attempts if the filesystem is
448 * mounted read-only.
449 */
450 if (vp->v_mount->mnt_flag & MNT_RDONLY)
451 return (EROFS);
452 }
453 }
454
455 return (null_bypass((struct vop_generic_args *)ap));
456}
457
458/*
459 * We handle getattr only to change the fsid.
460 */
461static int
462null_getattr(ap)
463 struct vop_getattr_args /* {
464 struct vnode *a_vp;
465 struct vattr *a_vap;
466 struct ucred *a_cred;
467 struct thread *a_td;
468 } */ *ap;
469{
470 int error;
471
472 if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
473 return (error);
474
475 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
476 return (0);
477}
478
479/*
480 * Handle to disallow write access if mounted read-only.
481 */
482static int
483null_access(ap)
484 struct vop_access_args /* {
485 struct vnode *a_vp;
486 int a_mode;
487 struct ucred *a_cred;
488 struct thread *a_td;
489 } */ *ap;
490{
491 struct vnode *vp = ap->a_vp;
492 mode_t mode = ap->a_mode;
493
494 /*
495 * Disallow write attempts on read-only layers;
496 * unless the file is a socket, fifo, or a block or
497 * character device resident on the filesystem.
498 */
499 if (mode & VWRITE) {
500 switch (vp->v_type) {
501 case VDIR:
502 case VLNK:
503 case VREG:
504 if (vp->v_mount->mnt_flag & MNT_RDONLY)
505 return (EROFS);
506 break;
507 default:
508 break;
509 }
510 }
511 return (null_bypass((struct vop_generic_args *)ap));
512}
513
514/*
515 * We must handle open to be able to catch MNT_NODEV and friends.
516 */
517static int
518null_open(ap)
519 struct vop_open_args /* {
520 struct vnode *a_vp;
521 int a_mode;
522 struct ucred *a_cred;
523 struct thread *a_td;
524 } */ *ap;
525{
526 struct vnode *vp = ap->a_vp;
527 struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
528
529 if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
530 (lvp->v_type == VBLK || lvp->v_type == VCHR))
531 return ENXIO;
532
533 return (null_bypass((struct vop_generic_args *)ap));
534}
535
536/*
537 * We handle this to eliminate null FS to lower FS
538 * file moving. Don't know why we don't allow this,
539 * possibly we should.
540 */
541static int
542null_rename(ap)
543 struct vop_rename_args /* {
544 struct vnode *a_fdvp;
545 struct vnode *a_fvp;
546 struct componentname *a_fcnp;
547 struct vnode *a_tdvp;
548 struct vnode *a_tvp;
549 struct componentname *a_tcnp;
550 } */ *ap;
551{
552 struct vnode *tdvp = ap->a_tdvp;
553 struct vnode *fvp = ap->a_fvp;
554 struct vnode *fdvp = ap->a_fdvp;
555 struct vnode *tvp = ap->a_tvp;
556
557 /* Check for cross-device rename. */
558 if ((fvp->v_mount != tdvp->v_mount) ||
559 (tvp && (fvp->v_mount != tvp->v_mount))) {
560 if (tdvp == tvp)
561 vrele(tdvp);
562 else
563 vput(tdvp);
564 if (tvp)
565 vput(tvp);
566 vrele(fdvp);
567 vrele(fvp);
568 return (EXDEV);
569 }
570
571 return (null_bypass((struct vop_generic_args *)ap));
572}
573
574/*
575 * We need to process our own vnode lock and then clear the
576 * interlock flag as it applies only to our vnode, not the
577 * vnodes below us on the stack.
578 */
579static int
580null_lock(ap)
581 struct vop_lock_args /* {
582 struct vnode *a_vp;
583 int a_flags;
584 struct thread *a_td;
585 } */ *ap;
586{
587 struct vnode *vp = ap->a_vp;
588 int flags = ap->a_flags;
589 struct thread *td = ap->a_td;
590 struct vnode *lvp;
591 int error;
592
593 if (flags & LK_THISLAYER) {
594 if (vp->v_vnlock != NULL) {
595 /* lock is shared across layers */
596 if (flags & LK_INTERLOCK)
597 mtx_unlock(&vp->v_interlock);
598 return 0;
599 }
600 error = lockmgr(&vp->v_lock, flags & ~LK_THISLAYER,
601 &vp->v_interlock, td);
602 return (error);
603 }
604
605 if (vp->v_vnlock != NULL) {
606 /*
607 * The lower level has exported a struct lock to us. Use
608 * it so that all vnodes in the stack lock and unlock
609 * simultaneously. Note: we don't DRAIN the lock as DRAIN
610 * decommissions the lock - just because our vnode is
611 * going away doesn't mean the struct lock below us is.
612 * LK_EXCLUSIVE is fine.
613 */
614 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
615 NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
616 return(lockmgr(vp->v_vnlock,
617 (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
618 &vp->v_interlock, td));
619 }
620 return(lockmgr(vp->v_vnlock, flags, &vp->v_interlock, td));
621 } else {
622 /*
623 * To prevent race conditions involving doing a lookup
624 * on "..", we have to lock the lower node, then lock our
625 * node. Most of the time it won't matter that we lock our
626 * node (as any locking would need the lower one locked
627 * first). But we can LK_DRAIN the upper lock as a step
628 * towards decomissioning it.
629 */
630 lvp = NULLVPTOLOWERVP(vp);
631 if (lvp == NULL)
632 return (lockmgr(&vp->v_lock, flags, &vp->v_interlock, td));
633 if (flags & LK_INTERLOCK) {
634 mtx_unlock(&vp->v_interlock);
635 flags &= ~LK_INTERLOCK;
636 }
637 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
638 error = VOP_LOCK(lvp,
639 (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE, td);
640 } else
641 error = VOP_LOCK(lvp, flags, td);
642 if (error)
643 return (error);
644 error = lockmgr(&vp->v_lock, flags, &vp->v_interlock, td);
645 if (error)
646 VOP_UNLOCK(lvp, 0, td);
647 return (error);
648 }
649}
650
651/*
652 * We need to process our own vnode unlock and then clear the
653 * interlock flag as it applies only to our vnode, not the
654 * vnodes below us on the stack.
655 */
656static int
657null_unlock(ap)
658 struct vop_unlock_args /* {
659 struct vnode *a_vp;
660 int a_flags;
661 struct thread *a_td;
662 } */ *ap;
663{
664 struct vnode *vp = ap->a_vp;
665 int flags = ap->a_flags;
666 struct thread *td = ap->a_td;
667 struct vnode *lvp;
668
669 if (vp->v_vnlock != NULL) {
670 if (flags & LK_THISLAYER)
671 return 0; /* the lock is shared across layers */
672 flags &= ~LK_THISLAYER;
673 return (lockmgr(vp->v_vnlock, flags | LK_RELEASE,
674 &vp->v_interlock, td));
675 }
676 lvp = NULLVPTOLOWERVP(vp);
677 if (lvp == NULL)
678 return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, td));
679 if ((flags & LK_THISLAYER) == 0) {
680 if (flags & LK_INTERLOCK) {
681 mtx_unlock(&vp->v_interlock);
682 flags &= ~LK_INTERLOCK;
683 }
684 VOP_UNLOCK(lvp, flags & ~LK_INTERLOCK, td);
685 } else
686 flags &= ~LK_THISLAYER;
687 return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, td));
688}
689
690static int
691null_islocked(ap)
692 struct vop_islocked_args /* {
693 struct vnode *a_vp;
694 struct thread *a_td;
695 } */ *ap;
696{
697 struct vnode *vp = ap->a_vp;
698 struct thread *td = ap->a_td;
699
700 if (vp->v_vnlock != NULL)
701 return (lockstatus(vp->v_vnlock, td));
702 return (lockstatus(&vp->v_lock, td));
703}
704
705/*
706 * There is no way to tell that someone issued remove/rmdir operation
707 * on the underlying filesystem. For now we just have to release lowevrp
708 * as soon as possible.
709 */
710static int
711null_inactive(ap)
712 struct vop_inactive_args /* {
713 struct vnode *a_vp;
714 struct thread *a_td;
715 } */ *ap;
716{
717 struct vnode *vp = ap->a_vp;
718 struct thread *td = ap->a_td;
718
719
720 VOP_UNLOCK(vp, 0, td);
721
719 /*
720 * If this is the last reference, then free up the vnode
721 * so as not to tie up the lower vnodes.
722 */
722 /*
723 * If this is the last reference, then free up the vnode
724 * so as not to tie up the lower vnodes.
725 */
723 if (vp->v_usecount == 0)
724 vrecycle(vp, NULL, ap->a_td);
726 vrecycle(vp, NULL, td);
727
725 return (0);
726}
727
728/*
729 * We can free memory in null_inactive, but we do this
730 * here. (Possible to guard vp->v_data to point somewhere)
731 */
732static int
733null_reclaim(ap)
734 struct vop_reclaim_args /* {
735 struct vnode *a_vp;
736 struct thread *a_td;
737 } */ *ap;
738{
739 struct thread *td = ap->a_td;
740 struct vnode *vp = ap->a_vp;
741 struct null_node *xp = VTONULL(vp);
742 struct vnode *lowervp = xp->null_lowervp;
743 void *vdata;
744
745 lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, td);
746 LIST_REMOVE(xp, null_hash);
747 lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
748
728 return (0);
729}
730
731/*
732 * We can free memory in null_inactive, but we do this
733 * here. (Possible to guard vp->v_data to point somewhere)
734 */
735static int
736null_reclaim(ap)
737 struct vop_reclaim_args /* {
738 struct vnode *a_vp;
739 struct thread *a_td;
740 } */ *ap;
741{
742 struct thread *td = ap->a_td;
743 struct vnode *vp = ap->a_vp;
744 struct null_node *xp = VTONULL(vp);
745 struct vnode *lowervp = xp->null_lowervp;
746 void *vdata;
747
748 lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, td);
749 LIST_REMOVE(xp, null_hash);
750 lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
751
749 xp->null_lowervp = NULLVP;
750 if (vp->v_vnlock != NULL) {
751 vp->v_vnlock = &vp->v_lock; /* we no longer share the lock */
752 } else
753 VOP_UNLOCK(vp, LK_THISLAYER, td);
754
755 /*
756 * Now it is safe to drop references to the lower vnode.
757 * VOP_INACTIVE() will be called by vrele() if necessary.
758 */
752 /*
753 * Now it is safe to drop references to the lower vnode.
754 * VOP_INACTIVE() will be called by vrele() if necessary.
755 */
759 vput(lowervp);
760 vrele (lowervp);
756 vrele(lowervp);
757 vrele(lowervp);
761
762 vdata = vp->v_data;
763 vp->v_data = NULL;
764 FREE(vdata, M_NULLFSNODE);
765
766 return (0);
767}
768
769static int
770null_print(ap)
771 struct vop_print_args /* {
772 struct vnode *a_vp;
773 } */ *ap;
774{
775 register struct vnode *vp = ap->a_vp;
776 printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
777 return (0);
778}
779
780/*
781 * Let an underlying filesystem do the work
782 */
783static int
784null_createvobject(ap)
785 struct vop_createvobject_args /* {
786 struct vnode *vp;
787 struct ucred *cred;
788 struct thread *td;
789 } */ *ap;
790{
791 struct vnode *vp = ap->a_vp;
792 struct vnode *lowervp = VTONULL(vp) ? NULLVPTOLOWERVP(vp) : NULL;
793 int error;
794
795 if (vp->v_type == VNON || lowervp == NULL)
796 return 0;
797 error = VOP_CREATEVOBJECT(lowervp, ap->a_cred, ap->a_td);
798 if (error)
799 return (error);
800 vp->v_flag |= VOBJBUF;
801 return (0);
802}
803
804/*
805 * We have nothing to destroy and this operation shouldn't be bypassed.
806 */
807static int
808null_destroyvobject(ap)
809 struct vop_destroyvobject_args /* {
810 struct vnode *vp;
811 } */ *ap;
812{
813 struct vnode *vp = ap->a_vp;
814
815 vp->v_flag &= ~VOBJBUF;
816 return (0);
817}
818
819static int
820null_getvobject(ap)
821 struct vop_getvobject_args /* {
822 struct vnode *vp;
823 struct vm_object **objpp;
824 } */ *ap;
825{
826 struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
827
828 if (lvp == NULL)
829 return EINVAL;
830 return (VOP_GETVOBJECT(lvp, ap->a_objpp));
831}
832
833/*
834 * Global vfs data structures
835 */
836vop_t **null_vnodeop_p;
837static struct vnodeopv_entry_desc null_vnodeop_entries[] = {
838 { &vop_default_desc, (vop_t *) null_bypass },
839
840 { &vop_access_desc, (vop_t *) null_access },
841 { &vop_bmap_desc, (vop_t *) vop_eopnotsupp },
842 { &vop_createvobject_desc, (vop_t *) null_createvobject },
843 { &vop_destroyvobject_desc, (vop_t *) null_destroyvobject },
844 { &vop_getattr_desc, (vop_t *) null_getattr },
845 { &vop_getvobject_desc, (vop_t *) null_getvobject },
846 { &vop_getwritemount_desc, (vop_t *) vop_stdgetwritemount},
847 { &vop_inactive_desc, (vop_t *) null_inactive },
848 { &vop_islocked_desc, (vop_t *) null_islocked },
849 { &vop_lock_desc, (vop_t *) null_lock },
850 { &vop_lookup_desc, (vop_t *) null_lookup },
851 { &vop_open_desc, (vop_t *) null_open },
852 { &vop_print_desc, (vop_t *) null_print },
853 { &vop_reclaim_desc, (vop_t *) null_reclaim },
854 { &vop_rename_desc, (vop_t *) null_rename },
855 { &vop_setattr_desc, (vop_t *) null_setattr },
856 { &vop_strategy_desc, (vop_t *) vop_eopnotsupp },
857 { &vop_unlock_desc, (vop_t *) null_unlock },
858 { NULL, NULL }
859};
860static struct vnodeopv_desc null_vnodeop_opv_desc =
861 { &null_vnodeop_p, null_vnodeop_entries };
862
863VNODEOP_SET(null_vnodeop_opv_desc);
758
759 vdata = vp->v_data;
760 vp->v_data = NULL;
761 FREE(vdata, M_NULLFSNODE);
762
763 return (0);
764}
765
766static int
767null_print(ap)
768 struct vop_print_args /* {
769 struct vnode *a_vp;
770 } */ *ap;
771{
772 register struct vnode *vp = ap->a_vp;
773 printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
774 return (0);
775}
776
777/*
778 * Let an underlying filesystem do the work
779 */
780static int
781null_createvobject(ap)
782 struct vop_createvobject_args /* {
783 struct vnode *vp;
784 struct ucred *cred;
785 struct thread *td;
786 } */ *ap;
787{
788 struct vnode *vp = ap->a_vp;
789 struct vnode *lowervp = VTONULL(vp) ? NULLVPTOLOWERVP(vp) : NULL;
790 int error;
791
792 if (vp->v_type == VNON || lowervp == NULL)
793 return 0;
794 error = VOP_CREATEVOBJECT(lowervp, ap->a_cred, ap->a_td);
795 if (error)
796 return (error);
797 vp->v_flag |= VOBJBUF;
798 return (0);
799}
800
801/*
802 * We have nothing to destroy and this operation shouldn't be bypassed.
803 */
804static int
805null_destroyvobject(ap)
806 struct vop_destroyvobject_args /* {
807 struct vnode *vp;
808 } */ *ap;
809{
810 struct vnode *vp = ap->a_vp;
811
812 vp->v_flag &= ~VOBJBUF;
813 return (0);
814}
815
816static int
817null_getvobject(ap)
818 struct vop_getvobject_args /* {
819 struct vnode *vp;
820 struct vm_object **objpp;
821 } */ *ap;
822{
823 struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
824
825 if (lvp == NULL)
826 return EINVAL;
827 return (VOP_GETVOBJECT(lvp, ap->a_objpp));
828}
829
830/*
831 * Global vfs data structures
832 */
833vop_t **null_vnodeop_p;
834static struct vnodeopv_entry_desc null_vnodeop_entries[] = {
835 { &vop_default_desc, (vop_t *) null_bypass },
836
837 { &vop_access_desc, (vop_t *) null_access },
838 { &vop_bmap_desc, (vop_t *) vop_eopnotsupp },
839 { &vop_createvobject_desc, (vop_t *) null_createvobject },
840 { &vop_destroyvobject_desc, (vop_t *) null_destroyvobject },
841 { &vop_getattr_desc, (vop_t *) null_getattr },
842 { &vop_getvobject_desc, (vop_t *) null_getvobject },
843 { &vop_getwritemount_desc, (vop_t *) vop_stdgetwritemount},
844 { &vop_inactive_desc, (vop_t *) null_inactive },
845 { &vop_islocked_desc, (vop_t *) null_islocked },
846 { &vop_lock_desc, (vop_t *) null_lock },
847 { &vop_lookup_desc, (vop_t *) null_lookup },
848 { &vop_open_desc, (vop_t *) null_open },
849 { &vop_print_desc, (vop_t *) null_print },
850 { &vop_reclaim_desc, (vop_t *) null_reclaim },
851 { &vop_rename_desc, (vop_t *) null_rename },
852 { &vop_setattr_desc, (vop_t *) null_setattr },
853 { &vop_strategy_desc, (vop_t *) vop_eopnotsupp },
854 { &vop_unlock_desc, (vop_t *) null_unlock },
855 { NULL, NULL }
856};
857static struct vnodeopv_desc null_vnodeop_opv_desc =
858 { &null_vnodeop_p, null_vnodeop_entries };
859
860VNODEOP_SET(null_vnodeop_opv_desc);