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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
33 *
34 * Ancestors:
35 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
36 * ...and...
37 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
38 *
39 * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 166774 2007-02-15 22:08:35Z pjd $
40 */
41
42/*
43 * Null Layer
44 *
45 * (See mount_nullfs(8) for more information.)
46 *
47 * The null layer duplicates a portion of the filesystem
48 * name space under a new name. In this respect, it is
49 * similar to the loopback filesystem. It differs from
50 * the loopback fs in two respects: it is implemented using
51 * a stackable layers techniques, and its "null-node"s stack above
52 * all lower-layer vnodes, not just over directory vnodes.
53 *
54 * The null layer has two purposes. First, it serves as a demonstration
55 * of layering by proving a layer which does nothing. (It actually
56 * does everything the loopback filesystem does, which is slightly
57 * more than nothing.) Second, the null layer can serve as a prototype
58 * layer. Since it provides all necessary layer framework,
59 * new filesystem layers can be created very easily be starting
60 * with a null layer.
61 *
62 * The remainder of this man page examines the null layer as a basis
63 * for constructing new layers.
64 *
65 *
66 * INSTANTIATING NEW NULL LAYERS
67 *
68 * New null layers are created with mount_nullfs(8).
69 * Mount_nullfs(8) takes two arguments, the pathname
70 * of the lower vfs (target-pn) and the pathname where the null
71 * layer will appear in the namespace (alias-pn). After
72 * the null layer is put into place, the contents
73 * of target-pn subtree will be aliased under alias-pn.
74 *
75 *
76 * OPERATION OF A NULL LAYER
77 *
78 * The null layer is the minimum filesystem layer,
79 * simply bypassing all possible operations to the lower layer
80 * for processing there. The majority of its activity centers
81 * on the bypass routine, through which nearly all vnode operations
82 * pass.
83 *
84 * The bypass routine accepts arbitrary vnode operations for
85 * handling by the lower layer. It begins by examing vnode
86 * operation arguments and replacing any null-nodes by their
87 * lower-layer equivlants. It then invokes the operation
88 * on the lower layer. Finally, it replaces the null-nodes
89 * in the arguments and, if a vnode is return by the operation,
90 * stacks a null-node on top of the returned vnode.
91 *
92 * Although bypass handles most operations, vop_getattr, vop_lock,
93 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
94 * bypassed. Vop_getattr must change the fsid being returned.
95 * Vop_lock and vop_unlock must handle any locking for the
96 * current vnode as well as pass the lock request down.
97 * Vop_inactive and vop_reclaim are not bypassed so that
98 * they can handle freeing null-layer specific data. Vop_print
99 * is not bypassed to avoid excessive debugging information.
100 * Also, certain vnode operations change the locking state within
101 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
102 * and symlink). Ideally these operations should not change the
103 * lock state, but should be changed to let the caller of the
104 * function unlock them. Otherwise all intermediate vnode layers
105 * (such as union, umapfs, etc) must catch these functions to do
106 * the necessary locking at their layer.
107 *
108 *
109 * INSTANTIATING VNODE STACKS
110 *
111 * Mounting associates the null layer with a lower layer,
112 * effect stacking two VFSes. Vnode stacks are instead
113 * created on demand as files are accessed.
114 *
115 * The initial mount creates a single vnode stack for the
116 * root of the new null layer. All other vnode stacks
117 * are created as a result of vnode operations on
118 * this or other null vnode stacks.
119 *
120 * New vnode stacks come into existance as a result of
121 * an operation which returns a vnode.
122 * The bypass routine stacks a null-node above the new
123 * vnode before returning it to the caller.
124 *
125 * For example, imagine mounting a null layer with
126 * "mount_nullfs /usr/include /dev/layer/null".
127 * Changing directory to /dev/layer/null will assign
128 * the root null-node (which was created when the null layer was mounted).
129 * Now consider opening "sys". A vop_lookup would be
130 * done on the root null-node. This operation would bypass through
131 * to the lower layer which would return a vnode representing
132 * the UFS "sys". Null_bypass then builds a null-node
133 * aliasing the UFS "sys" and returns this to the caller.
134 * Later operations on the null-node "sys" will repeat this
135 * process when constructing other vnode stacks.
136 *
137 *
138 * CREATING OTHER FILE SYSTEM LAYERS
139 *
140 * One of the easiest ways to construct new filesystem layers is to make
141 * a copy of the null layer, rename all files and variables, and
142 * then begin modifing the copy. Sed can be used to easily rename
143 * all variables.
144 *
145 * The umap layer is an example of a layer descended from the
146 * null layer.
147 *
148 *
149 * INVOKING OPERATIONS ON LOWER LAYERS
150 *
151 * There are two techniques to invoke operations on a lower layer
152 * when the operation cannot be completely bypassed. Each method
153 * is appropriate in different situations. In both cases,
154 * it is the responsibility of the aliasing layer to make
155 * the operation arguments "correct" for the lower layer
156 * by mapping a vnode arguments to the lower layer.
157 *
158 * The first approach is to call the aliasing layer's bypass routine.
159 * This method is most suitable when you wish to invoke the operation
160 * currently being handled on the lower layer. It has the advantage
161 * that the bypass routine already must do argument mapping.
162 * An example of this is null_getattrs in the null layer.
163 *
164 * A second approach is to directly invoke vnode operations on
165 * the lower layer with the VOP_OPERATIONNAME interface.
166 * The advantage of this method is that it is easy to invoke
167 * arbitrary operations on the lower layer. The disadvantage
168 * is that vnode arguments must be manualy mapped.
169 *
170 */
171
172#include <sys/param.h>
173#include <sys/systm.h>
174#include <sys/conf.h>
175#include <sys/kernel.h>
176#include <sys/lock.h>
177#include <sys/malloc.h>
178#include <sys/mount.h>
179#include <sys/mutex.h>
180#include <sys/namei.h>
181#include <sys/sysctl.h>
182#include <sys/vnode.h>
183#include <sys/kdb.h>
184
185#include <fs/nullfs/null.h>
186
187#include <vm/vm.h>
188#include <vm/vm_extern.h>
189#include <vm/vm_object.h>
190#include <vm/vnode_pager.h>
191
192static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
193SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
194 &null_bug_bypass, 0, "");
195
196/*
197 * This is the 10-Apr-92 bypass routine.
198 * This version has been optimized for speed, throwing away some
199 * safety checks. It should still always work, but it's not as
200 * robust to programmer errors.
201 *
202 * In general, we map all vnodes going down and unmap them on the way back.
203 * As an exception to this, vnodes can be marked "unmapped" by setting
204 * the Nth bit in operation's vdesc_flags.
205 *
206 * Also, some BSD vnode operations have the side effect of vrele'ing
207 * their arguments. With stacking, the reference counts are held
208 * by the upper node, not the lower one, so we must handle these
209 * side-effects here. This is not of concern in Sun-derived systems
210 * since there are no such side-effects.
211 *
212 * This makes the following assumptions:
213 * - only one returned vpp
214 * - no INOUT vpp's (Sun's vop_open has one of these)
215 * - the vnode operation vector of the first vnode should be used
216 * to determine what implementation of the op should be invoked
217 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
218 * problems on rmdir'ing mount points and renaming?)
219 */
220int
221null_bypass(struct vop_generic_args *ap)
222{
223 struct vnode **this_vp_p;
224 int error;
225 struct vnode *old_vps[VDESC_MAX_VPS];
226 struct vnode **vps_p[VDESC_MAX_VPS];
227 struct vnode ***vppp;
228 struct vnodeop_desc *descp = ap->a_desc;
229 int reles, i;
230
231 if (null_bug_bypass)
232 printf ("null_bypass: %s\n", descp->vdesc_name);
233
234#ifdef DIAGNOSTIC
235 /*
236 * We require at least one vp.
237 */
238 if (descp->vdesc_vp_offsets == NULL ||
239 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
240 panic ("null_bypass: no vp's in map");
241#endif
242
243 /*
244 * Map the vnodes going in.
245 * Later, we'll invoke the operation based on
246 * the first mapped vnode's operation vector.
247 */
248 reles = descp->vdesc_flags;
249 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
250 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
251 break; /* bail out at end of list */
252 vps_p[i] = this_vp_p =
253 VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
254 /*
255 * We're not guaranteed that any but the first vnode
256 * are of our type. Check for and don't map any
257 * that aren't. (We must always map first vp or vclean fails.)
258 */
259 if (i && (*this_vp_p == NULLVP ||
260 (*this_vp_p)->v_op != &null_vnodeops)) {
261 old_vps[i] = NULLVP;
262 } else {
263 old_vps[i] = *this_vp_p;
264 *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
265 /*
266 * XXX - Several operations have the side effect
267 * of vrele'ing their vp's. We must account for
268 * that. (This should go away in the future.)
269 */
270 if (reles & VDESC_VP0_WILLRELE)
271 VREF(*this_vp_p);
272 }
273
274 }
275
276 /*
277 * Call the operation on the lower layer
278 * with the modified argument structure.
279 */
280 if (vps_p[0] && *vps_p[0])
281 error = VCALL(ap);
282 else {
283 printf("null_bypass: no map for %s\n", descp->vdesc_name);
284 error = EINVAL;
285 }
286
287 /*
288 * Maintain the illusion of call-by-value
289 * by restoring vnodes in the argument structure
290 * to their original value.
291 */
292 reles = descp->vdesc_flags;
293 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
294 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
295 break; /* bail out at end of list */
296 if (old_vps[i]) {
297 *(vps_p[i]) = old_vps[i];
298#if 0
299 if (reles & VDESC_VP0_WILLUNLOCK)
300 VOP_UNLOCK(*(vps_p[i]), 0, curthread);
301#endif
302 if (reles & VDESC_VP0_WILLRELE)
303 vrele(*(vps_p[i]));
304 }
305 }
306
307 /*
308 * Map the possible out-going vpp
309 * (Assumes that the lower layer always returns
310 * a VREF'ed vpp unless it gets an error.)
311 */
312 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
313 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
314 !error) {
315 /*
316 * XXX - even though some ops have vpp returned vp's,
317 * several ops actually vrele this before returning.
318 * We must avoid these ops.
319 * (This should go away when these ops are regularized.)
320 */
321 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
322 goto out;
323 vppp = VOPARG_OFFSETTO(struct vnode***,
324 descp->vdesc_vpp_offset,ap);
325 if (*vppp)
326 error = null_nodeget(old_vps[0]->v_mount, **vppp, *vppp);
327 }
328
329 out:
330 return (error);
331}
332
333/*
334 * We have to carry on the locking protocol on the null layer vnodes
335 * as we progress through the tree. We also have to enforce read-only
336 * if this layer is mounted read-only.
337 */
338static int
339null_lookup(struct vop_lookup_args *ap)
340{
341 struct componentname *cnp = ap->a_cnp;
342 struct vnode *dvp = ap->a_dvp;
343 int flags = cnp->cn_flags;
344 struct vnode *vp, *ldvp, *lvp;
345 int error;
346
347 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
348 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
349 return (EROFS);
350 /*
351 * Although it is possible to call null_bypass(), we'll do
352 * a direct call to reduce overhead
353 */
354 ldvp = NULLVPTOLOWERVP(dvp);
355 vp = lvp = NULL;
356 error = VOP_LOOKUP(ldvp, &lvp, cnp);
357 if (error == EJUSTRETURN && (flags & ISLASTCN) &&
358 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
359 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
360 error = EROFS;
361
362 if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
363 if (ldvp == lvp) {
364 *ap->a_vpp = dvp;
365 VREF(dvp);
366 vrele(lvp);
367 } else {
368 error = null_nodeget(dvp->v_mount, lvp, &vp);
369 if (error) {
370 /* XXX Cleanup needed... */
371 panic("null_nodeget failed");
372 }
373 *ap->a_vpp = vp;
374 }
375 }
376 return (error);
377}
378
379static int
380null_open(struct vop_open_args *ap)
381{
382 int retval;
383 struct vnode *vp, *ldvp;
384
385 vp = ap->a_vp;
386 ldvp = NULLVPTOLOWERVP(vp);
387 retval = null_bypass(&ap->a_gen);
388 if (retval == 0)
389 vp->v_object = ldvp->v_object;
390 return (retval);
391}
392
393/*
394 * Setattr call. Disallow write attempts if the layer is mounted read-only.
395 */
396static int
397null_setattr(struct vop_setattr_args *ap)
398{
399 struct vnode *vp = ap->a_vp;
400 struct vattr *vap = ap->a_vap;
401
402 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
403 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
404 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
405 (vp->v_mount->mnt_flag & MNT_RDONLY))
406 return (EROFS);
407 if (vap->va_size != VNOVAL) {
408 switch (vp->v_type) {
409 case VDIR:
410 return (EISDIR);
411 case VCHR:
412 case VBLK:
413 case VSOCK:
414 case VFIFO:
415 if (vap->va_flags != VNOVAL)
416 return (EOPNOTSUPP);
417 return (0);
418 case VREG:
419 case VLNK:
420 default:
421 /*
422 * Disallow write attempts if the filesystem is
423 * mounted read-only.
424 */
425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 return (EROFS);
427 }
428 }
429
430 return (null_bypass((struct vop_generic_args *)ap));
431}
432
433/*
434 * We handle getattr only to change the fsid.
435 */
436static int
437null_getattr(struct vop_getattr_args *ap)
438{
439 int error;
440
441 if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
442 return (error);
443
444 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
445 return (0);
446}
447
448/*
449 * Handle to disallow write access if mounted read-only.
450 */
451static int
452null_access(struct vop_access_args *ap)
453{
454 struct vnode *vp = ap->a_vp;
455 mode_t mode = ap->a_mode;
456
457 /*
458 * Disallow write attempts on read-only layers;
459 * unless the file is a socket, fifo, or a block or
460 * character device resident on the filesystem.
461 */
462 if (mode & VWRITE) {
463 switch (vp->v_type) {
464 case VDIR:
465 case VLNK:
466 case VREG:
467 if (vp->v_mount->mnt_flag & MNT_RDONLY)
468 return (EROFS);
469 break;
470 default:
471 break;
472 }
473 }
474 return (null_bypass((struct vop_generic_args *)ap));
475}
476
477/*
478 * We handle this to eliminate null FS to lower FS
479 * file moving. Don't know why we don't allow this,
480 * possibly we should.
481 */
482static int
483null_rename(struct vop_rename_args *ap)
484{
485 struct vnode *tdvp = ap->a_tdvp;
486 struct vnode *fvp = ap->a_fvp;
487 struct vnode *fdvp = ap->a_fdvp;
488 struct vnode *tvp = ap->a_tvp;
489
490 /* Check for cross-device rename. */
491 if ((fvp->v_mount != tdvp->v_mount) ||
492 (tvp && (fvp->v_mount != tvp->v_mount))) {
493 if (tdvp == tvp)
494 vrele(tdvp);
495 else
496 vput(tdvp);
497 if (tvp)
498 vput(tvp);
499 vrele(fdvp);
500 vrele(fvp);
501 return (EXDEV);
502 }
503
504 return (null_bypass((struct vop_generic_args *)ap));
505}
506
507/*
508 * We need to process our own vnode lock and then clear the
509 * interlock flag as it applies only to our vnode, not the
510 * vnodes below us on the stack.
511 */
512static int
513null_lock(struct _vop_lock_args *ap)
514{
515 struct vnode *vp = ap->a_vp;
516 int flags = ap->a_flags;
517 struct thread *td = ap->a_td;
518 struct null_node *nn;
519 struct vnode *lvp;
520 int error;
521
522
523 if ((flags & LK_INTERLOCK) == 0) {
524 VI_LOCK(vp);
525 ap->a_flags = flags |= LK_INTERLOCK;
526 }
527 nn = VTONULL(vp);
528 /*
529 * If we're still active we must ask the lower layer to
530 * lock as ffs has special lock considerations in it's
531 * vop lock.
532 */
533 if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
534 VI_LOCK_FLAGS(lvp, MTX_DUPOK);
535 VI_UNLOCK(vp);
536 /*
537 * We have to hold the vnode here to solve a potential
538 * reclaim race. If we're forcibly vgone'd while we
539 * still have refs, a thread could be sleeping inside
540 * the lowervp's vop_lock routine. When we vgone we will
541 * drop our last ref to the lowervp, which would allow it
542 * to be reclaimed. The lowervp could then be recycled,
543 * in which case it is not legal to be sleeping in it's VOP.
544 * We prevent it from being recycled by holding the vnode
545 * here.
546 */
547 vholdl(lvp);
548 error = VOP_LOCK(lvp, flags, td);
549
550 /*
551 * We might have slept to get the lock and someone might have
552 * clean our vnode already, switching vnode lock from one in
553 * lowervp to v_lock in our own vnode structure. Handle this
554 * case by reacquiring correct lock in requested mode.
555 */
556 if (VTONULL(vp) == NULL && error == 0) {
557 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
558 switch (flags & LK_TYPE_MASK) {
559 case LK_SHARED:
560 ap->a_flags |= LK_SHARED;
561 break;
562 case LK_UPGRADE:
563 case LK_EXCLUSIVE:
564 ap->a_flags |= LK_EXCLUSIVE;
565 break;
566 default:
567 panic("Unsupported lock request %d\n",
568 ap->a_flags);
569 }
570 VOP_UNLOCK(lvp, 0, td);
571 error = vop_stdlock(ap);
572 }
573 vdrop(lvp);
574 } else
575 error = vop_stdlock(ap);
576
577 return (error);
578}
579
580/*
581 * We need to process our own vnode unlock and then clear the
582 * interlock flag as it applies only to our vnode, not the
583 * vnodes below us on the stack.
584 */
585static int
586null_unlock(struct vop_unlock_args *ap)
587{
588 struct vnode *vp = ap->a_vp;
589 int flags = ap->a_flags;
590 struct thread *td = ap->a_td;
591 struct null_node *nn;
592 struct vnode *lvp;
593 int error;
594
595 if ((flags & LK_INTERLOCK) != 0) {
596 VI_UNLOCK(vp);
597 ap->a_flags = flags &= ~LK_INTERLOCK;
598 }
599 nn = VTONULL(vp);
600 if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL)
601 error = VOP_UNLOCK(lvp, flags, td);
602 else
603 error = vop_stdunlock(ap);
604
605 return (error);
606}
607
608static int
609null_islocked(struct vop_islocked_args *ap)
610{
611 struct vnode *vp = ap->a_vp;
612 struct thread *td = ap->a_td;
613
614 return (lockstatus(vp->v_vnlock, td));
615}
616
617/*
618 * There is no way to tell that someone issued remove/rmdir operation
619 * on the underlying filesystem. For now we just have to release lowevrp
620 * as soon as possible.
621 *
622 * Note, we can't release any resources nor remove vnode from hash before
623 * appropriate VXLOCK stuff is is done because other process can find this
624 * vnode in hash during inactivation and may be sitting in vget() and waiting
625 * for null_inactive to unlock vnode. Thus we will do all those in VOP_RECLAIM.
626 */
627static int
628null_inactive(struct vop_inactive_args *ap)
629{
630 struct vnode *vp = ap->a_vp;
631 struct thread *td = ap->a_td;
632
633 vp->v_object = NULL;
634
635 /*
636 * If this is the last reference, then free up the vnode
637 * so as not to tie up the lower vnodes.
638 */
639 vrecycle(vp, td);
640
641 return (0);
642}
643
644/*
645 * Now, the VXLOCK is in force and we're free to destroy the null vnode.
646 */
647static int
648null_reclaim(struct vop_reclaim_args *ap)
649{
650 struct vnode *vp = ap->a_vp;
651 struct null_node *xp = VTONULL(vp);
652 struct vnode *lowervp = xp->null_lowervp;
653 struct lock *vnlock;
654
655 if (lowervp)
656 null_hashrem(xp);
657 /*
658 * Use the interlock to protect the clearing of v_data to
659 * prevent faults in null_lock().
660 */
661 VI_LOCK(vp);
662 vp->v_data = NULL;
663 vp->v_object = NULL;
664 vnlock = vp->v_vnlock;
665 vp->v_vnlock = &vp->v_lock;
666 if (lowervp) {
667 lockmgr(vp->v_vnlock,
668 LK_EXCLUSIVE|LK_INTERLOCK, VI_MTX(vp), curthread);
669 vput(lowervp);
670 } else
671 panic("null_reclaim: reclaiming an node with now lowervp");
672 FREE(xp, M_NULLFSNODE);
673
674 return (0);
675}
676
677static int
678null_print(struct vop_print_args *ap)
679{
680 struct vnode *vp = ap->a_vp;
681
682 printf("\tvp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
683 return (0);
684}
685
686/* ARGSUSED */
687static int
688null_getwritemount(struct vop_getwritemount_args *ap)
689{
690 struct null_node *xp;
691 struct vnode *lowervp;
692 struct vnode *vp;
693
694 vp = ap->a_vp;
695 VI_LOCK(vp);
696 xp = VTONULL(vp);
697 if (xp && (lowervp = xp->null_lowervp)) {
698 VI_LOCK_FLAGS(lowervp, MTX_DUPOK);
699 VI_UNLOCK(vp);
700 vholdl(lowervp);
701 VI_UNLOCK(lowervp);
702 VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
703 vdrop(lowervp);
704 } else {
705 VI_UNLOCK(vp);
706 *(ap->a_mpp) = NULL;
707 }
708 return (0);
709}
710
711static int
712null_vptofh(struct vop_vptofh_args *ap)
713{
714 struct vnode *lvp;
715
716 lvp = NULLVPTOLOWERVP(ap->a_vp);
717 return VOP_VPTOFH(lvp, ap->a_fhp);
718}
719
720/*
721 * Global vfs data structures
722 */
723struct vop_vector null_vnodeops = {
724 .vop_bypass = null_bypass,
725 .vop_access = null_access,
726 .vop_bmap = VOP_EOPNOTSUPP,
727 .vop_getattr = null_getattr,
728 .vop_getwritemount = null_getwritemount,
729 .vop_inactive = null_inactive,
730 .vop_islocked = null_islocked,
731 ._vop_lock = null_lock,
732 .vop_lookup = null_lookup,
733 .vop_open = null_open,
734 .vop_print = null_print,
735 .vop_reclaim = null_reclaim,
736 .vop_rename = null_rename,
737 .vop_setattr = null_setattr,
738 .vop_strategy = VOP_EOPNOTSUPP,
739 .vop_unlock = null_unlock,
740 .vop_vptofh = null_vptofh,
741};
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
33 *
34 * Ancestors:
35 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
36 * ...and...
37 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
38 *
39 * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 166774 2007-02-15 22:08:35Z pjd $
40 */
41
42/*
43 * Null Layer
44 *
45 * (See mount_nullfs(8) for more information.)
46 *
47 * The null layer duplicates a portion of the filesystem
48 * name space under a new name. In this respect, it is
49 * similar to the loopback filesystem. It differs from
50 * the loopback fs in two respects: it is implemented using
51 * a stackable layers techniques, and its "null-node"s stack above
52 * all lower-layer vnodes, not just over directory vnodes.
53 *
54 * The null layer has two purposes. First, it serves as a demonstration
55 * of layering by proving a layer which does nothing. (It actually
56 * does everything the loopback filesystem does, which is slightly
57 * more than nothing.) Second, the null layer can serve as a prototype
58 * layer. Since it provides all necessary layer framework,
59 * new filesystem layers can be created very easily be starting
60 * with a null layer.
61 *
62 * The remainder of this man page examines the null layer as a basis
63 * for constructing new layers.
64 *
65 *
66 * INSTANTIATING NEW NULL LAYERS
67 *
68 * New null layers are created with mount_nullfs(8).
69 * Mount_nullfs(8) takes two arguments, the pathname
70 * of the lower vfs (target-pn) and the pathname where the null
71 * layer will appear in the namespace (alias-pn). After
72 * the null layer is put into place, the contents
73 * of target-pn subtree will be aliased under alias-pn.
74 *
75 *
76 * OPERATION OF A NULL LAYER
77 *
78 * The null layer is the minimum filesystem layer,
79 * simply bypassing all possible operations to the lower layer
80 * for processing there. The majority of its activity centers
81 * on the bypass routine, through which nearly all vnode operations
82 * pass.
83 *
84 * The bypass routine accepts arbitrary vnode operations for
85 * handling by the lower layer. It begins by examing vnode
86 * operation arguments and replacing any null-nodes by their
87 * lower-layer equivlants. It then invokes the operation
88 * on the lower layer. Finally, it replaces the null-nodes
89 * in the arguments and, if a vnode is return by the operation,
90 * stacks a null-node on top of the returned vnode.
91 *
92 * Although bypass handles most operations, vop_getattr, vop_lock,
93 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
94 * bypassed. Vop_getattr must change the fsid being returned.
95 * Vop_lock and vop_unlock must handle any locking for the
96 * current vnode as well as pass the lock request down.
97 * Vop_inactive and vop_reclaim are not bypassed so that
98 * they can handle freeing null-layer specific data. Vop_print
99 * is not bypassed to avoid excessive debugging information.
100 * Also, certain vnode operations change the locking state within
101 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
102 * and symlink). Ideally these operations should not change the
103 * lock state, but should be changed to let the caller of the
104 * function unlock them. Otherwise all intermediate vnode layers
105 * (such as union, umapfs, etc) must catch these functions to do
106 * the necessary locking at their layer.
107 *
108 *
109 * INSTANTIATING VNODE STACKS
110 *
111 * Mounting associates the null layer with a lower layer,
112 * effect stacking two VFSes. Vnode stacks are instead
113 * created on demand as files are accessed.
114 *
115 * The initial mount creates a single vnode stack for the
116 * root of the new null layer. All other vnode stacks
117 * are created as a result of vnode operations on
118 * this or other null vnode stacks.
119 *
120 * New vnode stacks come into existance as a result of
121 * an operation which returns a vnode.
122 * The bypass routine stacks a null-node above the new
123 * vnode before returning it to the caller.
124 *
125 * For example, imagine mounting a null layer with
126 * "mount_nullfs /usr/include /dev/layer/null".
127 * Changing directory to /dev/layer/null will assign
128 * the root null-node (which was created when the null layer was mounted).
129 * Now consider opening "sys". A vop_lookup would be
130 * done on the root null-node. This operation would bypass through
131 * to the lower layer which would return a vnode representing
132 * the UFS "sys". Null_bypass then builds a null-node
133 * aliasing the UFS "sys" and returns this to the caller.
134 * Later operations on the null-node "sys" will repeat this
135 * process when constructing other vnode stacks.
136 *
137 *
138 * CREATING OTHER FILE SYSTEM LAYERS
139 *
140 * One of the easiest ways to construct new filesystem layers is to make
141 * a copy of the null layer, rename all files and variables, and
142 * then begin modifing the copy. Sed can be used to easily rename
143 * all variables.
144 *
145 * The umap layer is an example of a layer descended from the
146 * null layer.
147 *
148 *
149 * INVOKING OPERATIONS ON LOWER LAYERS
150 *
151 * There are two techniques to invoke operations on a lower layer
152 * when the operation cannot be completely bypassed. Each method
153 * is appropriate in different situations. In both cases,
154 * it is the responsibility of the aliasing layer to make
155 * the operation arguments "correct" for the lower layer
156 * by mapping a vnode arguments to the lower layer.
157 *
158 * The first approach is to call the aliasing layer's bypass routine.
159 * This method is most suitable when you wish to invoke the operation
160 * currently being handled on the lower layer. It has the advantage
161 * that the bypass routine already must do argument mapping.
162 * An example of this is null_getattrs in the null layer.
163 *
164 * A second approach is to directly invoke vnode operations on
165 * the lower layer with the VOP_OPERATIONNAME interface.
166 * The advantage of this method is that it is easy to invoke
167 * arbitrary operations on the lower layer. The disadvantage
168 * is that vnode arguments must be manualy mapped.
169 *
170 */
171
172#include <sys/param.h>
173#include <sys/systm.h>
174#include <sys/conf.h>
175#include <sys/kernel.h>
176#include <sys/lock.h>
177#include <sys/malloc.h>
178#include <sys/mount.h>
179#include <sys/mutex.h>
180#include <sys/namei.h>
181#include <sys/sysctl.h>
182#include <sys/vnode.h>
183#include <sys/kdb.h>
184
185#include <fs/nullfs/null.h>
186
187#include <vm/vm.h>
188#include <vm/vm_extern.h>
189#include <vm/vm_object.h>
190#include <vm/vnode_pager.h>
191
192static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
193SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
194 &null_bug_bypass, 0, "");
195
196/*
197 * This is the 10-Apr-92 bypass routine.
198 * This version has been optimized for speed, throwing away some
199 * safety checks. It should still always work, but it's not as
200 * robust to programmer errors.
201 *
202 * In general, we map all vnodes going down and unmap them on the way back.
203 * As an exception to this, vnodes can be marked "unmapped" by setting
204 * the Nth bit in operation's vdesc_flags.
205 *
206 * Also, some BSD vnode operations have the side effect of vrele'ing
207 * their arguments. With stacking, the reference counts are held
208 * by the upper node, not the lower one, so we must handle these
209 * side-effects here. This is not of concern in Sun-derived systems
210 * since there are no such side-effects.
211 *
212 * This makes the following assumptions:
213 * - only one returned vpp
214 * - no INOUT vpp's (Sun's vop_open has one of these)
215 * - the vnode operation vector of the first vnode should be used
216 * to determine what implementation of the op should be invoked
217 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
218 * problems on rmdir'ing mount points and renaming?)
219 */
220int
221null_bypass(struct vop_generic_args *ap)
222{
223 struct vnode **this_vp_p;
224 int error;
225 struct vnode *old_vps[VDESC_MAX_VPS];
226 struct vnode **vps_p[VDESC_MAX_VPS];
227 struct vnode ***vppp;
228 struct vnodeop_desc *descp = ap->a_desc;
229 int reles, i;
230
231 if (null_bug_bypass)
232 printf ("null_bypass: %s\n", descp->vdesc_name);
233
234#ifdef DIAGNOSTIC
235 /*
236 * We require at least one vp.
237 */
238 if (descp->vdesc_vp_offsets == NULL ||
239 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
240 panic ("null_bypass: no vp's in map");
241#endif
242
243 /*
244 * Map the vnodes going in.
245 * Later, we'll invoke the operation based on
246 * the first mapped vnode's operation vector.
247 */
248 reles = descp->vdesc_flags;
249 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
250 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
251 break; /* bail out at end of list */
252 vps_p[i] = this_vp_p =
253 VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
254 /*
255 * We're not guaranteed that any but the first vnode
256 * are of our type. Check for and don't map any
257 * that aren't. (We must always map first vp or vclean fails.)
258 */
259 if (i && (*this_vp_p == NULLVP ||
260 (*this_vp_p)->v_op != &null_vnodeops)) {
261 old_vps[i] = NULLVP;
262 } else {
263 old_vps[i] = *this_vp_p;
264 *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
265 /*
266 * XXX - Several operations have the side effect
267 * of vrele'ing their vp's. We must account for
268 * that. (This should go away in the future.)
269 */
270 if (reles & VDESC_VP0_WILLRELE)
271 VREF(*this_vp_p);
272 }
273
274 }
275
276 /*
277 * Call the operation on the lower layer
278 * with the modified argument structure.
279 */
280 if (vps_p[0] && *vps_p[0])
281 error = VCALL(ap);
282 else {
283 printf("null_bypass: no map for %s\n", descp->vdesc_name);
284 error = EINVAL;
285 }
286
287 /*
288 * Maintain the illusion of call-by-value
289 * by restoring vnodes in the argument structure
290 * to their original value.
291 */
292 reles = descp->vdesc_flags;
293 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
294 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
295 break; /* bail out at end of list */
296 if (old_vps[i]) {
297 *(vps_p[i]) = old_vps[i];
298#if 0
299 if (reles & VDESC_VP0_WILLUNLOCK)
300 VOP_UNLOCK(*(vps_p[i]), 0, curthread);
301#endif
302 if (reles & VDESC_VP0_WILLRELE)
303 vrele(*(vps_p[i]));
304 }
305 }
306
307 /*
308 * Map the possible out-going vpp
309 * (Assumes that the lower layer always returns
310 * a VREF'ed vpp unless it gets an error.)
311 */
312 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
313 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
314 !error) {
315 /*
316 * XXX - even though some ops have vpp returned vp's,
317 * several ops actually vrele this before returning.
318 * We must avoid these ops.
319 * (This should go away when these ops are regularized.)
320 */
321 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
322 goto out;
323 vppp = VOPARG_OFFSETTO(struct vnode***,
324 descp->vdesc_vpp_offset,ap);
325 if (*vppp)
326 error = null_nodeget(old_vps[0]->v_mount, **vppp, *vppp);
327 }
328
329 out:
330 return (error);
331}
332
333/*
334 * We have to carry on the locking protocol on the null layer vnodes
335 * as we progress through the tree. We also have to enforce read-only
336 * if this layer is mounted read-only.
337 */
338static int
339null_lookup(struct vop_lookup_args *ap)
340{
341 struct componentname *cnp = ap->a_cnp;
342 struct vnode *dvp = ap->a_dvp;
343 int flags = cnp->cn_flags;
344 struct vnode *vp, *ldvp, *lvp;
345 int error;
346
347 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
348 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
349 return (EROFS);
350 /*
351 * Although it is possible to call null_bypass(), we'll do
352 * a direct call to reduce overhead
353 */
354 ldvp = NULLVPTOLOWERVP(dvp);
355 vp = lvp = NULL;
356 error = VOP_LOOKUP(ldvp, &lvp, cnp);
357 if (error == EJUSTRETURN && (flags & ISLASTCN) &&
358 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
359 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
360 error = EROFS;
361
362 if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
363 if (ldvp == lvp) {
364 *ap->a_vpp = dvp;
365 VREF(dvp);
366 vrele(lvp);
367 } else {
368 error = null_nodeget(dvp->v_mount, lvp, &vp);
369 if (error) {
370 /* XXX Cleanup needed... */
371 panic("null_nodeget failed");
372 }
373 *ap->a_vpp = vp;
374 }
375 }
376 return (error);
377}
378
379static int
380null_open(struct vop_open_args *ap)
381{
382 int retval;
383 struct vnode *vp, *ldvp;
384
385 vp = ap->a_vp;
386 ldvp = NULLVPTOLOWERVP(vp);
387 retval = null_bypass(&ap->a_gen);
388 if (retval == 0)
389 vp->v_object = ldvp->v_object;
390 return (retval);
391}
392
393/*
394 * Setattr call. Disallow write attempts if the layer is mounted read-only.
395 */
396static int
397null_setattr(struct vop_setattr_args *ap)
398{
399 struct vnode *vp = ap->a_vp;
400 struct vattr *vap = ap->a_vap;
401
402 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
403 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
404 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
405 (vp->v_mount->mnt_flag & MNT_RDONLY))
406 return (EROFS);
407 if (vap->va_size != VNOVAL) {
408 switch (vp->v_type) {
409 case VDIR:
410 return (EISDIR);
411 case VCHR:
412 case VBLK:
413 case VSOCK:
414 case VFIFO:
415 if (vap->va_flags != VNOVAL)
416 return (EOPNOTSUPP);
417 return (0);
418 case VREG:
419 case VLNK:
420 default:
421 /*
422 * Disallow write attempts if the filesystem is
423 * mounted read-only.
424 */
425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 return (EROFS);
427 }
428 }
429
430 return (null_bypass((struct vop_generic_args *)ap));
431}
432
433/*
434 * We handle getattr only to change the fsid.
435 */
436static int
437null_getattr(struct vop_getattr_args *ap)
438{
439 int error;
440
441 if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
442 return (error);
443
444 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
445 return (0);
446}
447
448/*
449 * Handle to disallow write access if mounted read-only.
450 */
451static int
452null_access(struct vop_access_args *ap)
453{
454 struct vnode *vp = ap->a_vp;
455 mode_t mode = ap->a_mode;
456
457 /*
458 * Disallow write attempts on read-only layers;
459 * unless the file is a socket, fifo, or a block or
460 * character device resident on the filesystem.
461 */
462 if (mode & VWRITE) {
463 switch (vp->v_type) {
464 case VDIR:
465 case VLNK:
466 case VREG:
467 if (vp->v_mount->mnt_flag & MNT_RDONLY)
468 return (EROFS);
469 break;
470 default:
471 break;
472 }
473 }
474 return (null_bypass((struct vop_generic_args *)ap));
475}
476
477/*
478 * We handle this to eliminate null FS to lower FS
479 * file moving. Don't know why we don't allow this,
480 * possibly we should.
481 */
482static int
483null_rename(struct vop_rename_args *ap)
484{
485 struct vnode *tdvp = ap->a_tdvp;
486 struct vnode *fvp = ap->a_fvp;
487 struct vnode *fdvp = ap->a_fdvp;
488 struct vnode *tvp = ap->a_tvp;
489
490 /* Check for cross-device rename. */
491 if ((fvp->v_mount != tdvp->v_mount) ||
492 (tvp && (fvp->v_mount != tvp->v_mount))) {
493 if (tdvp == tvp)
494 vrele(tdvp);
495 else
496 vput(tdvp);
497 if (tvp)
498 vput(tvp);
499 vrele(fdvp);
500 vrele(fvp);
501 return (EXDEV);
502 }
503
504 return (null_bypass((struct vop_generic_args *)ap));
505}
506
507/*
508 * We need to process our own vnode lock and then clear the
509 * interlock flag as it applies only to our vnode, not the
510 * vnodes below us on the stack.
511 */
512static int
513null_lock(struct _vop_lock_args *ap)
514{
515 struct vnode *vp = ap->a_vp;
516 int flags = ap->a_flags;
517 struct thread *td = ap->a_td;
518 struct null_node *nn;
519 struct vnode *lvp;
520 int error;
521
522
523 if ((flags & LK_INTERLOCK) == 0) {
524 VI_LOCK(vp);
525 ap->a_flags = flags |= LK_INTERLOCK;
526 }
527 nn = VTONULL(vp);
528 /*
529 * If we're still active we must ask the lower layer to
530 * lock as ffs has special lock considerations in it's
531 * vop lock.
532 */
533 if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
534 VI_LOCK_FLAGS(lvp, MTX_DUPOK);
535 VI_UNLOCK(vp);
536 /*
537 * We have to hold the vnode here to solve a potential
538 * reclaim race. If we're forcibly vgone'd while we
539 * still have refs, a thread could be sleeping inside
540 * the lowervp's vop_lock routine. When we vgone we will
541 * drop our last ref to the lowervp, which would allow it
542 * to be reclaimed. The lowervp could then be recycled,
543 * in which case it is not legal to be sleeping in it's VOP.
544 * We prevent it from being recycled by holding the vnode
545 * here.
546 */
547 vholdl(lvp);
548 error = VOP_LOCK(lvp, flags, td);
549
550 /*
551 * We might have slept to get the lock and someone might have
552 * clean our vnode already, switching vnode lock from one in
553 * lowervp to v_lock in our own vnode structure. Handle this
554 * case by reacquiring correct lock in requested mode.
555 */
556 if (VTONULL(vp) == NULL && error == 0) {
557 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
558 switch (flags & LK_TYPE_MASK) {
559 case LK_SHARED:
560 ap->a_flags |= LK_SHARED;
561 break;
562 case LK_UPGRADE:
563 case LK_EXCLUSIVE:
564 ap->a_flags |= LK_EXCLUSIVE;
565 break;
566 default:
567 panic("Unsupported lock request %d\n",
568 ap->a_flags);
569 }
570 VOP_UNLOCK(lvp, 0, td);
571 error = vop_stdlock(ap);
572 }
573 vdrop(lvp);
574 } else
575 error = vop_stdlock(ap);
576
577 return (error);
578}
579
580/*
581 * We need to process our own vnode unlock and then clear the
582 * interlock flag as it applies only to our vnode, not the
583 * vnodes below us on the stack.
584 */
585static int
586null_unlock(struct vop_unlock_args *ap)
587{
588 struct vnode *vp = ap->a_vp;
589 int flags = ap->a_flags;
590 struct thread *td = ap->a_td;
591 struct null_node *nn;
592 struct vnode *lvp;
593 int error;
594
595 if ((flags & LK_INTERLOCK) != 0) {
596 VI_UNLOCK(vp);
597 ap->a_flags = flags &= ~LK_INTERLOCK;
598 }
599 nn = VTONULL(vp);
600 if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL)
601 error = VOP_UNLOCK(lvp, flags, td);
602 else
603 error = vop_stdunlock(ap);
604
605 return (error);
606}
607
608static int
609null_islocked(struct vop_islocked_args *ap)
610{
611 struct vnode *vp = ap->a_vp;
612 struct thread *td = ap->a_td;
613
614 return (lockstatus(vp->v_vnlock, td));
615}
616
617/*
618 * There is no way to tell that someone issued remove/rmdir operation
619 * on the underlying filesystem. For now we just have to release lowevrp
620 * as soon as possible.
621 *
622 * Note, we can't release any resources nor remove vnode from hash before
623 * appropriate VXLOCK stuff is is done because other process can find this
624 * vnode in hash during inactivation and may be sitting in vget() and waiting
625 * for null_inactive to unlock vnode. Thus we will do all those in VOP_RECLAIM.
626 */
627static int
628null_inactive(struct vop_inactive_args *ap)
629{
630 struct vnode *vp = ap->a_vp;
631 struct thread *td = ap->a_td;
632
633 vp->v_object = NULL;
634
635 /*
636 * If this is the last reference, then free up the vnode
637 * so as not to tie up the lower vnodes.
638 */
639 vrecycle(vp, td);
640
641 return (0);
642}
643
644/*
645 * Now, the VXLOCK is in force and we're free to destroy the null vnode.
646 */
647static int
648null_reclaim(struct vop_reclaim_args *ap)
649{
650 struct vnode *vp = ap->a_vp;
651 struct null_node *xp = VTONULL(vp);
652 struct vnode *lowervp = xp->null_lowervp;
653 struct lock *vnlock;
654
655 if (lowervp)
656 null_hashrem(xp);
657 /*
658 * Use the interlock to protect the clearing of v_data to
659 * prevent faults in null_lock().
660 */
661 VI_LOCK(vp);
662 vp->v_data = NULL;
663 vp->v_object = NULL;
664 vnlock = vp->v_vnlock;
665 vp->v_vnlock = &vp->v_lock;
666 if (lowervp) {
667 lockmgr(vp->v_vnlock,
668 LK_EXCLUSIVE|LK_INTERLOCK, VI_MTX(vp), curthread);
669 vput(lowervp);
670 } else
671 panic("null_reclaim: reclaiming an node with now lowervp");
672 FREE(xp, M_NULLFSNODE);
673
674 return (0);
675}
676
677static int
678null_print(struct vop_print_args *ap)
679{
680 struct vnode *vp = ap->a_vp;
681
682 printf("\tvp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
683 return (0);
684}
685
686/* ARGSUSED */
687static int
688null_getwritemount(struct vop_getwritemount_args *ap)
689{
690 struct null_node *xp;
691 struct vnode *lowervp;
692 struct vnode *vp;
693
694 vp = ap->a_vp;
695 VI_LOCK(vp);
696 xp = VTONULL(vp);
697 if (xp && (lowervp = xp->null_lowervp)) {
698 VI_LOCK_FLAGS(lowervp, MTX_DUPOK);
699 VI_UNLOCK(vp);
700 vholdl(lowervp);
701 VI_UNLOCK(lowervp);
702 VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
703 vdrop(lowervp);
704 } else {
705 VI_UNLOCK(vp);
706 *(ap->a_mpp) = NULL;
707 }
708 return (0);
709}
710
711static int
712null_vptofh(struct vop_vptofh_args *ap)
713{
714 struct vnode *lvp;
715
716 lvp = NULLVPTOLOWERVP(ap->a_vp);
717 return VOP_VPTOFH(lvp, ap->a_fhp);
718}
719
720/*
721 * Global vfs data structures
722 */
723struct vop_vector null_vnodeops = {
724 .vop_bypass = null_bypass,
725 .vop_access = null_access,
726 .vop_bmap = VOP_EOPNOTSUPP,
727 .vop_getattr = null_getattr,
728 .vop_getwritemount = null_getwritemount,
729 .vop_inactive = null_inactive,
730 .vop_islocked = null_islocked,
731 ._vop_lock = null_lock,
732 .vop_lookup = null_lookup,
733 .vop_open = null_open,
734 .vop_print = null_print,
735 .vop_reclaim = null_reclaim,
736 .vop_rename = null_rename,
737 .vop_setattr = null_setattr,
738 .vop_strategy = VOP_EOPNOTSUPP,
739 .vop_unlock = null_unlock,
740 .vop_vptofh = null_vptofh,
741};