📄 subr.c
字号:
/* * Copyright (c) 1997-2003 Erez Zadok * Copyright (c) 2001-2003 Stony Brook University * Copyright (c) 1997-2000 Columbia University * * For specific licensing information, see the COPYING file distributed with * this package, or get one from ftp://ftp.filesystems.org/pub/fist/COPYING. * * This Copyright notice must be kept intact and distributed with all * fistgen sources INCLUDING sources generated by fistgen. *//* * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. 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. * * @(#)wrapfs_subr.c 8.7 (Berkeley) 5/14/95 * * $Id: subr.c,v 1.4 2002/12/27 20:18:51 ezk Exp $ */#ifdef HAVE_CONFIG_H# include <config.h>#endif /* HAVE_CONFIG_H */#ifdef FISTGEN# include "fist_wrapfs.h"#endif /* FISTGEN */#include <fist.h>#include <wrapfs.h>#if 0#include "opt_debug.h"#include <sys/param.h>#include <sys/systm.h>#include <sys/proc.h>#include <sys/vnode.h>#include <sys/mount.h>#include <sys/malloc.h>#include <sys/namei.h>#include <vm/vm_zone.h>#include <wrapfs.h>#endif#define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */#define NWRAPFSNODECACHE 16/* * Cryptfs 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. */#define WRAPFS_NHASH(vp) \ (&wrapfs_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & wrapfs_node_hash])staticLIST_HEAD(wrapfs_node_hashhead, wrapfs_node) * wrapfs_node_hashtbl; static u_long wrapfs_node_hash; static int wrapfs_node_alloc __P((struct mount *mp, struct vnode * lowervp, struct vnode ** vpp)); static struct vnode * wrapfs_node_find __P((struct mount *mp, struct vnode * lowervp));/* * Initialise cache headers */intwrapfs_init(vfsp) struct vfsconf *vfsp;{ fist_dprint(4, "FXN=%s FILE=%s LINE=%d\n",__FUNCTION__,__FILE__,__LINE__);#ifdef WRAPFS_DIAGNOSTIC printf("wrapfs_init\n"); /* printed during system boot */#endif wrapfs_node_hashtbl = hashinit(NWRAPFSNODECACHE, M_CACHE, &wrapfs_node_hash); return (0);}/* * Return a VREF'ed alias for lower vnode if already exists, else 0. */static struct vnode *wrapfs_node_find(mp, lowervp) struct mount *mp; struct vnode *lowervp;{ struct proc *p = curproc; /* XXX */ struct wrapfs_node_hashhead *hd; struct wrapfs_node *a; struct vnode *vp; fist_dprint(4, "FXN=%s FILE=%s LINE=%d\n",__FUNCTION__,__FILE__,__LINE__); /* * Find hash base, and then search the (two-way) linked * list looking for a wrapfs_node structure which is referencing * the lower vnode. If found, the increment the wrapfs_node * reference count (but NOT the lower vnode's VREF counter). */ hd = WRAPFS_NHASH(lowervp);loop: for (a = hd->lh_first; a != 0; a = a->wrapfs_hash.le_next) { if (a->wrapfs_lowervp == lowervp && WRAPFS_TO_VP(a)->v_mount == mp) { vp = WRAPFS_TO_VP(a); /* * We need vget for the VXLOCK * stuff, but we don't want to lock * the lower node. */ if (vget(vp, 0, p)) { printf("wrapfs_node_find: vget failed.\n"); goto loop; }; return (vp); } } return NULLVP;}/* * Make a new wrapfs_node node. * Vp is the alias vnode, lofsvp is the lower vnode. * Maintain a reference to (lowervp). */static intwrapfs_node_alloc(mp, lowervp, vpp) struct mount *mp; struct vnode *lowervp; struct vnode **vpp;{ struct wrapfs_node_hashhead *hd; struct wrapfs_node *xp; struct vnode *othervp, *vp; int error; fist_dprint(4, "FXN=%s FILE=%s LINE=%d\n",__FUNCTION__,__FILE__,__LINE__); /* * Do the MALLOC before the getnewvnode since doing so afterward * might cause a bogus v_data pointer to get dereferenced * elsewhere if MALLOC should block. */ MALLOC(xp, struct wrapfs_node *, sizeof(struct wrapfs_node), M_TEMP, M_WAITOK); error = getnewvnode(VT_WRAPFS, mp, wrapfs_vnodeop_p, vpp); if (error) { FREE(xp, M_TEMP); return (error); } vp = *vpp; vp->v_type = lowervp->v_type; xp->wrapfs_vnode = vp; vp->v_data = xp; xp->wrapfs_lowervp = lowervp; /* * Before we insert our new node onto the hash chains, * check to see if someone else has beaten us to it. * (We could have slept in MALLOC.) */ othervp = wrapfs_node_find(mp, lowervp); if (othervp) { FREE(xp, M_TEMP); vp->v_type = VBAD; /* node is discarded */ vp->v_usecount = 0; /* XXX */ *vpp = othervp; return 0; }; VREF(lowervp); /* Extra VREF will be vrele'd in * wrapfs_node_create */ hd = WRAPFS_NHASH(lowervp); LIST_INSERT_HEAD(hd, xp, wrapfs_hash); return 0;}/* * Try to find an existing wrapfs_node vnode refering * to it, otherwise make a new wrapfs_node vnode which * contains a reference to the lower vnode. */intwrapfs_node_create(mp, lowervp, newvpp) struct mount *mp; struct vnode *lowervp; struct vnode **newvpp;{ struct vnode *aliasvp; fist_dprint(4, "FXN=%s FILE=%s LINE=%d\n",__FUNCTION__,__FILE__,__LINE__); aliasvp = wrapfs_node_find(mp, lowervp); if (aliasvp) { /* * wrapfs_node_find has taken another reference * to the alias vnode. */#ifdef WRAPFS_DIAGNOSTIC vprint("wrapfs_node_create: exists", aliasvp);#endif /* VREF(aliasvp); --- done in wrapfs_node_find */ } else { int error; /* * Get new vnode. */#ifdef WRAPFS_DIAGNOSTIC printf("wrapfs_node_create: create new alias vnode\n");#endif /* * Make new vnode reference the wrapfs_node. */ error = wrapfs_node_alloc(mp, lowervp, &aliasvp); if (error) return error; /* * aliasvp is already VREF'd by getnewvnode() */ } vrele(lowervp);#ifdef DIAGNOSTIC if (lowervp->v_usecount < 1) { /* Should never happen... */ vprint("wrapfs_node_create: alias ", aliasvp); vprint("wrapfs_node_create: lower ", lowervp); panic("wrapfs_node_create: lower has 0 usecount."); };#endif#ifdef WRAPFS_DIAGNOSTIC vprint("wrapfs_node_create: alias", aliasvp); vprint("wrapfs_node_create: lower", lowervp);#endif *newvpp = aliasvp; return (0);}#ifdef WRAPFS_DIAGNOSTIC#include "opt_ddb.h"#ifdef DDB#define wrapfs_checkvp_barrier 1#else#define wrapfs_checkvp_barrier 0#endifstruct vnode *wrapfs_checkvp(vp, fil, lno) struct vnode *vp; char *fil; int lno;{ struct wrapfs_node *a = VP_TO_WRAPFS(vp); fist_dprint(4, "FXN=%s FILE=%s LINE=%d\n",__FUNCTION__,__FILE__,__LINE__);#ifdef notyet /* * Can't do this check because vop_reclaim runs * with a funny vop vector. */ if (vp->v_op != wrapfs_vnodeop_p) { printf("wrapfs_checkvp: on non-wrapfs-node\n"); while (wrapfs_checkvp_barrier) /* WAIT */ ; panic("wrapfs_checkvp"); };#endif if (a->wrapfs_lowervp == NULLVP) { /* 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 */ while (wrapfs_checkvp_barrier) /* WAIT */ ; panic("wrapfs_checkvp"); } if (a->wrapfs_lowervp->v_usecount < 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 */ while (wrapfs_checkvp_barrier) /* WAIT */ ; panic("wrapfs with unref'ed lowervp"); };#ifdef notyet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -