xref: /netbsd/sys/miscfs/genfs/layer_subr.c (revision e1f25ac9)
1 /*	$NetBSD: layer_subr.c,v 1.39 2022/04/10 09:50:46 andvar Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 National Aeronautics & Space Administration
5  * All rights reserved.
6  *
7  * This software was written by William Studenmund of the
8  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the National Aeronautics & Space Administration
19  *    nor the names of its contributors may be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1992, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * This code is derived from software donated to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	from: Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp
68  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: layer_subr.c,v 1.39 2022/04/10 09:50:46 andvar Exp $");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/time.h>
78 #include <sys/vnode.h>
79 #include <sys/mount.h>
80 #include <sys/namei.h>
81 #include <sys/kmem.h>
82 
83 #include <miscfs/genfs/layer.h>
84 #include <miscfs/genfs/layer_extern.h>
85 
86 #ifdef LAYERFS_DIAGNOSTIC
87 int layerfs_debug = 1;
88 #endif
89 
90 /*
91  * layer cache:
92  * Each cache entry holds a reference to the lower vnode
93  * along with a pointer to the alias vnode.  When an
94  * entry is added the lower vnode is VREF'd.  When the
95  * alias is removed the lower vnode is vrele'd.
96  */
97 
98 void
layerfs_init(void)99 layerfs_init(void)
100 {
101 	/* Nothing. */
102 }
103 
104 void
layerfs_done(void)105 layerfs_done(void)
106 {
107 	/* Nothing. */
108 }
109 
110 /*
111  * layer_node_create: try to find an existing layerfs vnode referring to it,
112  * otherwise make a new vnode which contains a reference to the lower vnode.
113  */
114 int
layer_node_create(struct mount * mp,struct vnode * lowervp,struct vnode ** nvpp)115 layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **nvpp)
116 {
117 	int error;
118 	struct vnode *aliasvp;
119 
120 	error = vcache_get(mp, &lowervp, sizeof(lowervp), &aliasvp);
121 	if (error)
122 		return error;
123 
124 	/*
125 	 * Now that we acquired a reference on the upper vnode, release one
126 	 * on the lower node.  The existence of the layer_node retains one
127 	 * reference to the lower node.
128 	 */
129 	vrele(lowervp);
130 	KASSERT(vrefcnt(lowervp) > 0);
131 
132 #ifdef LAYERFS_DIAGNOSTIC
133 	if (layerfs_debug)
134 		vprint("layer_node_create: alias", aliasvp);
135 #endif
136 	*nvpp = aliasvp;
137 	return 0;
138 }
139 
140 #ifdef LAYERFS_DIAGNOSTIC
141 struct vnode *
layer_checkvp(struct vnode * vp,const char * fil,int lno)142 layer_checkvp(struct vnode *vp, const char *fil, int lno)
143 {
144 	struct layer_node *a = VTOLAYER(vp);
145 #ifdef notyet
146 	/*
147 	 * Can't do this check because vop_reclaim runs
148 	 * with a funny vop vector.
149 	 *
150 	 * WRS - no it doesnt...
151 	 */
152 	if (vp->v_op != layer_vnodeop_p) {
153 		printf ("layer_checkvp: on non-layer-node\n");
154 #ifdef notyet
155 		while (layer_checkvp_barrier) /*WAIT*/ ;
156 #endif
157 		panic("layer_checkvp");
158 	};
159 #endif
160 	if (a->layer_lowervp == NULL) {
161 		/* Should never happen */
162 		int i; u_long *p;
163 		printf("vp = %p, ZERO ptr\n", vp);
164 		for (p = (u_long *) a, i = 0; i < 8; i++)
165 			printf(" %lx", p[i]);
166 		printf("\n");
167 		/* wait for debugger */
168 		panic("layer_checkvp");
169 	}
170 	if (vrefcnt(a->layer_lowervp) < 1) {
171 		int i; u_long *p;
172 		printf("vp = %p, unref'ed lowervp\n", vp);
173 		for (p = (u_long *) a, i = 0; i < 8; i++)
174 			printf(" %lx", p[i]);
175 		printf("\n");
176 		/* wait for debugger */
177 		panic ("layer with unref'ed lowervp");
178 	};
179 #ifdef notnow
180 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
181 	        LAYERTOV(a), vrefcnt(LAYERTOV(a)),
182 		a->layer_lowervp, vrefcnt(a->layer_lowervp),
183 		fil, lno);
184 #endif
185 	return a->layer_lowervp;
186 }
187 #endif
188