xref: /netbsd-src/sys/miscfs/genfs/layer_subr.c (revision 75d2abaeb1baf9839167f74fdcc44faa66addc35)
1*75d2abaeSandvar /*	$NetBSD: layer_subr.c,v 1.39 2022/04/10 09:50:46 andvar Exp $	*/
29866514dSwrstuden 
39866514dSwrstuden /*
49866514dSwrstuden  * Copyright (c) 1999 National Aeronautics & Space Administration
59866514dSwrstuden  * All rights reserved.
69866514dSwrstuden  *
79866514dSwrstuden  * This software was written by William Studenmund of the
8fa87a209Swiz  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
99866514dSwrstuden  *
109866514dSwrstuden  * Redistribution and use in source and binary forms, with or without
119866514dSwrstuden  * modification, are permitted provided that the following conditions
129866514dSwrstuden  * are met:
139866514dSwrstuden  * 1. Redistributions of source code must retain the above copyright
149866514dSwrstuden  *    notice, this list of conditions and the following disclaimer.
159866514dSwrstuden  * 2. Redistributions in binary form must reproduce the above copyright
169866514dSwrstuden  *    notice, this list of conditions and the following disclaimer in the
179866514dSwrstuden  *    documentation and/or other materials provided with the distribution.
1895054da1Ssoren  * 3. Neither the name of the National Aeronautics & Space Administration
199866514dSwrstuden  *    nor the names of its contributors may be used to endorse or promote
209866514dSwrstuden  *    products derived from this software without specific prior written
219866514dSwrstuden  *    permission.
229866514dSwrstuden  *
239866514dSwrstuden  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
249866514dSwrstuden  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
259866514dSwrstuden  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
269866514dSwrstuden  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
279866514dSwrstuden  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
289866514dSwrstuden  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
299866514dSwrstuden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
309866514dSwrstuden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
319866514dSwrstuden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
329866514dSwrstuden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
339866514dSwrstuden  * POSSIBILITY OF SUCH DAMAGE.
349866514dSwrstuden  */
3597272194Srmind 
369866514dSwrstuden /*
379866514dSwrstuden  * Copyright (c) 1992, 1993
389866514dSwrstuden  *	The Regents of the University of California.  All rights reserved.
399866514dSwrstuden  *
409866514dSwrstuden  * This code is derived from software donated to Berkeley by
419866514dSwrstuden  * Jan-Simon Pendry.
429866514dSwrstuden  *
439866514dSwrstuden  * Redistribution and use in source and binary forms, with or without
449866514dSwrstuden  * modification, are permitted provided that the following conditions
459866514dSwrstuden  * are met:
469866514dSwrstuden  * 1. Redistributions of source code must retain the above copyright
479866514dSwrstuden  *    notice, this list of conditions and the following disclaimer.
489866514dSwrstuden  * 2. Redistributions in binary form must reproduce the above copyright
499866514dSwrstuden  *    notice, this list of conditions and the following disclaimer in the
509866514dSwrstuden  *    documentation and/or other materials provided with the distribution.
51aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
529866514dSwrstuden  *    may be used to endorse or promote products derived from this software
539866514dSwrstuden  *    without specific prior written permission.
549866514dSwrstuden  *
559866514dSwrstuden  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
569866514dSwrstuden  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
579866514dSwrstuden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
589866514dSwrstuden  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
599866514dSwrstuden  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
609866514dSwrstuden  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
619866514dSwrstuden  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
629866514dSwrstuden  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
639866514dSwrstuden  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
649866514dSwrstuden  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
659866514dSwrstuden  * SUCH DAMAGE.
669866514dSwrstuden  *
679866514dSwrstuden  *	from: Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp
689866514dSwrstuden  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
699866514dSwrstuden  */
709866514dSwrstuden 
71e4b00f43Slukem #include <sys/cdefs.h>
72*75d2abaeSandvar __KERNEL_RCSID(0, "$NetBSD: layer_subr.c,v 1.39 2022/04/10 09:50:46 andvar Exp $");
73e4b00f43Slukem 
749866514dSwrstuden #include <sys/param.h>
759866514dSwrstuden #include <sys/systm.h>
769866514dSwrstuden #include <sys/proc.h>
779866514dSwrstuden #include <sys/time.h>
789866514dSwrstuden #include <sys/vnode.h>
799866514dSwrstuden #include <sys/mount.h>
809866514dSwrstuden #include <sys/namei.h>
814a780c9aSad #include <sys/kmem.h>
824a780c9aSad 
839866514dSwrstuden #include <miscfs/genfs/layer.h>
849866514dSwrstuden #include <miscfs/genfs/layer_extern.h>
859866514dSwrstuden 
86fbd6fe6cSerh #ifdef LAYERFS_DIAGNOSTIC
87fbd6fe6cSerh int layerfs_debug = 1;
88fbd6fe6cSerh #endif
89fbd6fe6cSerh 
909866514dSwrstuden /*
919866514dSwrstuden  * layer cache:
929866514dSwrstuden  * Each cache entry holds a reference to the lower vnode
939866514dSwrstuden  * along with a pointer to the alias vnode.  When an
949866514dSwrstuden  * entry is added the lower vnode is VREF'd.  When the
959866514dSwrstuden  * alias is removed the lower vnode is vrele'd.
969866514dSwrstuden  */
979866514dSwrstuden 
989866514dSwrstuden void
layerfs_init(void)99b8817e4aScegger layerfs_init(void)
1009866514dSwrstuden {
10197272194Srmind 	/* Nothing. */
1029866514dSwrstuden }
1039866514dSwrstuden 
10489015c46Sjdolecek void
layerfs_done(void)105b8817e4aScegger layerfs_done(void)
10689015c46Sjdolecek {
10797272194Srmind 	/* Nothing. */
10889015c46Sjdolecek }
10989015c46Sjdolecek 
11089015c46Sjdolecek /*
111*75d2abaeSandvar  * layer_node_create: try to find an existing layerfs vnode referring to it,
11297272194Srmind  * otherwise make a new vnode which contains a reference to the lower vnode.
1139866514dSwrstuden  */
1149866514dSwrstuden int
layer_node_create(struct mount * mp,struct vnode * lowervp,struct vnode ** nvpp)11597272194Srmind layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **nvpp)
1169866514dSwrstuden {
1179866514dSwrstuden 	int error;
11855de4c18Shannken 	struct vnode *aliasvp;
1199866514dSwrstuden 
12055de4c18Shannken 	error = vcache_get(mp, &lowervp, sizeof(lowervp), &aliasvp);
12197272194Srmind 	if (error)
12297272194Srmind 		return error;
1239866514dSwrstuden 
1249866514dSwrstuden 	/*
12597272194Srmind 	 * Now that we acquired a reference on the upper vnode, release one
12697272194Srmind 	 * on the lower node.  The existence of the layer_node retains one
1279866514dSwrstuden 	 * reference to the lower node.
1289866514dSwrstuden 	 */
1299866514dSwrstuden 	vrele(lowervp);
13023bf8800Sad 	KASSERT(vrefcnt(lowervp) > 0);
1319866514dSwrstuden 
1329866514dSwrstuden #ifdef LAYERFS_DIAGNOSTIC
133fbd6fe6cSerh 	if (layerfs_debug)
1349866514dSwrstuden 		vprint("layer_node_create: alias", aliasvp);
1359866514dSwrstuden #endif
13697272194Srmind 	*nvpp = aliasvp;
13797272194Srmind 	return 0;
1389866514dSwrstuden }
1399866514dSwrstuden 
140fbd6fe6cSerh #ifdef LAYERFS_DIAGNOSTIC
1419866514dSwrstuden struct vnode *
layer_checkvp(struct vnode * vp,const char * fil,int lno)142454af1c0Sdsl layer_checkvp(struct vnode *vp, const char *fil, int lno)
1439866514dSwrstuden {
1449866514dSwrstuden 	struct layer_node *a = VTOLAYER(vp);
1459866514dSwrstuden #ifdef notyet
1469866514dSwrstuden 	/*
1479866514dSwrstuden 	 * Can't do this check because vop_reclaim runs
1489866514dSwrstuden 	 * with a funny vop vector.
1499866514dSwrstuden 	 *
1509866514dSwrstuden 	 * WRS - no it doesnt...
1519866514dSwrstuden 	 */
1529866514dSwrstuden 	if (vp->v_op != layer_vnodeop_p) {
1539866514dSwrstuden 		printf ("layer_checkvp: on non-layer-node\n");
1549866514dSwrstuden #ifdef notyet
1559866514dSwrstuden 		while (layer_checkvp_barrier) /*WAIT*/ ;
1569866514dSwrstuden #endif
1579866514dSwrstuden 		panic("layer_checkvp");
1589866514dSwrstuden 	};
1599866514dSwrstuden #endif
1609866514dSwrstuden 	if (a->layer_lowervp == NULL) {
1619866514dSwrstuden 		/* Should never happen */
1629866514dSwrstuden 		int i; u_long *p;
1639866514dSwrstuden 		printf("vp = %p, ZERO ptr\n", vp);
1649866514dSwrstuden 		for (p = (u_long *) a, i = 0; i < 8; i++)
1659866514dSwrstuden 			printf(" %lx", p[i]);
1669866514dSwrstuden 		printf("\n");
1679866514dSwrstuden 		/* wait for debugger */
1689866514dSwrstuden 		panic("layer_checkvp");
1699866514dSwrstuden 	}
17023bf8800Sad 	if (vrefcnt(a->layer_lowervp) < 1) {
1719866514dSwrstuden 		int i; u_long *p;
1729866514dSwrstuden 		printf("vp = %p, unref'ed lowervp\n", vp);
1739866514dSwrstuden 		for (p = (u_long *) a, i = 0; i < 8; i++)
1749866514dSwrstuden 			printf(" %lx", p[i]);
1759866514dSwrstuden 		printf("\n");
1769866514dSwrstuden 		/* wait for debugger */
1779866514dSwrstuden 		panic ("layer with unref'ed lowervp");
1789866514dSwrstuden 	};
1799866514dSwrstuden #ifdef notnow
1809866514dSwrstuden 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
18123bf8800Sad 	        LAYERTOV(a), vrefcnt(LAYERTOV(a)),
18223bf8800Sad 		a->layer_lowervp, vrefcnt(a->layer_lowervp),
1839866514dSwrstuden 		fil, lno);
1849866514dSwrstuden #endif
1859866514dSwrstuden 	return a->layer_lowervp;
1869866514dSwrstuden }
187fbd6fe6cSerh #endif
188