root/sys/miscfs/genfs/layer_subr.c
/*      $NetBSD: layer_subr.c,v 1.39 2022/04/10 09:50:46 andvar Exp $   */

/*
 * Copyright (c) 1999 National Aeronautics & Space Administration
 * All rights reserved.
 *
 * This software was written by William Studenmund of the
 * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the National Aeronautics & Space Administration
 *    nor the names of its contributors may be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
 * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1992, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software donated to Berkeley by
 * Jan-Simon Pendry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      from: Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp
 *      @(#)null_subr.c 8.7 (Berkeley) 5/14/95
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: layer_subr.c,v 1.39 2022/04/10 09:50:46 andvar Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/kmem.h>

#include <miscfs/genfs/layer.h>
#include <miscfs/genfs/layer_extern.h>

#ifdef LAYERFS_DIAGNOSTIC
int layerfs_debug = 1;
#endif

/*
 * layer cache:
 * Each cache entry holds a reference to the lower vnode
 * along with a pointer to the alias vnode.  When an
 * entry is added the lower vnode is VREF'd.  When the
 * alias is removed the lower vnode is vrele'd.
 */

void
layerfs_init(void)
{
        /* Nothing. */
}

void
layerfs_done(void)
{
        /* Nothing. */
}

/*
 * layer_node_create: try to find an existing layerfs vnode referring to it,
 * otherwise make a new vnode which contains a reference to the lower vnode.
 */
int
layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **nvpp)
{
        int error;
        struct vnode *aliasvp;

        error = vcache_get(mp, &lowervp, sizeof(lowervp), &aliasvp);
        if (error)
                return error;

        /*
         * Now that we acquired a reference on the upper vnode, release one
         * on the lower node.  The existence of the layer_node retains one
         * reference to the lower node.
         */
        vrele(lowervp);
        KASSERT(vrefcnt(lowervp) > 0);

#ifdef LAYERFS_DIAGNOSTIC
        if (layerfs_debug)
                vprint("layer_node_create: alias", aliasvp);
#endif
        *nvpp = aliasvp;
        return 0;
}

#ifdef LAYERFS_DIAGNOSTIC
struct vnode *
layer_checkvp(struct vnode *vp, const char *fil, int lno)
{
        struct layer_node *a = VTOLAYER(vp);
#ifdef notyet
        /*
         * Can't do this check because vop_reclaim runs
         * with a funny vop vector.
         *
         * WRS - no it doesnt...
         */
        if (vp->v_op != layer_vnodeop_p) {
                printf ("layer_checkvp: on non-layer-node\n");
#ifdef notyet
                while (layer_checkvp_barrier) /*WAIT*/ ;
#endif
                panic("layer_checkvp");
        };
#endif
        if (a->layer_lowervp == NULL) {
                /* Should never happen */
                int i; u_long *p;
                printf("vp = %p, ZERO ptr\n", vp);
                for (p = (u_long *) a, i = 0; i < 8; i++)
                        printf(" %lx", p[i]);
                printf("\n");
                /* wait for debugger */
                panic("layer_checkvp");
        }
        if (vrefcnt(a->layer_lowervp) < 1) {
                int i; u_long *p;
                printf("vp = %p, unref'ed lowervp\n", vp);
                for (p = (u_long *) a, i = 0; i < 8; i++)
                        printf(" %lx", p[i]);
                printf("\n");
                /* wait for debugger */
                panic ("layer with unref'ed lowervp");
        };
#ifdef notnow
        printf("layer %p/%d -> %p/%d [%s, %d]\n",
                LAYERTOV(a), vrefcnt(LAYERTOV(a)),
                a->layer_lowervp, vrefcnt(a->layer_lowervp),
                fil, lno);
#endif
        return a->layer_lowervp;
}
#endif