📄 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 * * $FreeBSD: src/sys/miscfs/wrapfs/wrapfs_subr.c,v 1.21.2.4 2001/06/26 04:20:09 bp 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/kernel.h>#include <sys/proc.h>#include <sys/vnode.h>#include <sys/mount.h>#include <sys/malloc.h>#include <miscfs/wrapfs/wrapfs.h>#endif#define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */#define NWRAPFSNODECACHE 16/* * Wrapfs 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])static LIST_HEAD(wrapfs_node_hashhead, wrapfs_node) *wrapfs_node_hashtbl;static u_long wrapfs_node_hash;struct lock wrapfs_hashlock;static MALLOC_DEFINE(M_WRAPFSHASH, "WRAPFS hash", "WRAPFS hash table");MALLOC_DEFINE(M_WRAPFSNODE, "WRAPFS node", "WRAPFS vnode private part");int wrapfs_init(struct vfsconf *vfsp);int wrapfs_uninit(struct vfsconf *vfsp);int wrapfs_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **newvpp);/* defined by EZK */void * kmem_zalloc(unsigned long size);int fist_uiomove(caddr_t cp, int n, enum uio_rw rwflag, struct uio *uio);#ifdef FIST_FILTER_NAMEcn_t * wrapfs_new_cnp(const vnode_t *thisvp, const cn_t *thiscnp);void wrapfs_update_cnp(const vnode_t *thisvp, cn_t **lowercnpp, cn_t *thiscnp, int error);#endif /* FIST_FILTER_NAME */#ifdef FIST_FILTER_DATAvoid fist_copy_object_attr(vm_object_t dest, const vm_object_t src);void fist_copy_page_attr(vm_page_t dest, const vm_page_t src);int wrapfs_fill_zeros(vnode_t *vp, vattr_t *vap, cred_t *cred, proc_t *p);#endif /* FIST_FILTER_DATA */static int wrapfs_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp);static struct vnode * wrapfs_node_find(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__); fist_dprint(2, "wrapfs_init\n"); /* printed during system boot */ wrapfs_node_hashtbl = hashinit(NWRAPFSNODECACHE, M_WRAPFSHASH, &wrapfs_node_hash); lockinit(&wrapfs_hashlock, PVFS, "wrapfs", 0, 0); return (0);}intwrapfs_uninit(vfsp) struct vfsconf *vfsp;{ fist_dprint(4, "FXN=%s FILE=%s LINE=%d\n",__FUNCTION__,__FILE__,__LINE__); if (wrapfs_node_hashtbl) { free(wrapfs_node_hashtbl, M_WRAPFSHASH); } return (0);}/* * Return a VREF'ed alias for lower vnode if already exists, else 0. * Lower vnode should be locked on entry and will be left locked on exit. */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: lockmgr(&wrapfs_hashlock, LK_EXCLUSIVE, NULL, p); LIST_FOREACH(a, hd, wrapfs_hash) { if (a->wrapfs_lowervp == lowervp && WRAPFS_TO_VP(a)->v_mount == mp) { vp = WRAPFS_TO_VP(a); lockmgr(&wrapfs_hashlock, LK_RELEASE, NULL, p); /* * We need vget for the VXLOCK * stuff, but we don't want to lock * the lower node. */ if (vget(vp, LK_EXCLUSIVE | LK_CANRECURSE, p)) { printf ("wrapfs_node_find: vget failed.\n"); goto loop; } VOP_UNLOCK(lowervp, 0, p); return (vp); } } lockmgr(&wrapfs_hashlock, LK_RELEASE, NULL, p); print_location(); return NULLVP; /* kiran ??? WRAPFSVP*/}/* * 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 proc *p = curproc; /* XXX */ 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_WRAPFSNODE, M_WAITOK); error = getnewvnode(VT_WRAPFS, mp, wrapfs_vnodeop_p, vpp); if (error) { FREE(xp, M_WRAPFSNODE); return (error); } vp = *vpp; vp->v_type = lowervp->v_type; lockinit(&xp->wrapfs_lock, PINOD, "wrapfsnode", 0, LK_CANRECURSE); 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) { vp->v_data = NULL; FREE(xp, M_WRAPFSNODE); vp->v_type = VBAD; /* node is discarded */ vrele(vp); *vpp = othervp; return 0; } /* * From NetBSD: * Now lock the new node. We rely on the fact that we were passed * a locked vnode. If the lower node is exporting a struct lock * (v_vnlock != NULL) then we just set the upper v_vnlock to the * lower one, and both are now locked. If the lower node is exporting * NULL, then we copy that up and manually lock the new vnode. */ lockmgr(&wrapfs_hashlock, LK_EXCLUSIVE, NULL, p); vp->v_vnlock = lowervp->v_vnlock; error = VOP_LOCK(vp, LK_EXCLUSIVE | LK_THISLAYER, p); if (error) panic("wrapfs_node_alloc: can't lock new vnode\n"); VREF(lowervp); hd = WRAPFS_NHASH(lowervp); LIST_INSERT_HEAD(hd, xp, wrapfs_hash); lockmgr(&wrapfs_hashlock, LK_RELEASE, NULL, p); print_location(); return 0;}/* * Try to find an existing wrapfs_node vnode refering to the given underlying * vnode (which should be locked). If no vnode found, create 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. */ vrele(lowervp);#ifdef FIST_DEBUG fist_dprint(4, "wrapfs_node_create: exists", aliasvp);#endif } else { int error; /* * Get new vnode. */ fist_dprint(2, "wrapfs_node_create: create new alias vnode\n"); /* * 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); - was there in 4.0 - check */#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 FIST_DEBUG fist_dprint(2, "wrapfs_node_create: alias", aliasvp); fist_dprint(2, "wrapfs_node_create: lower", lowervp);#endif *newvpp = aliasvp; print_location(); return (0);}#ifdef DIAGNOSTIC#ifdef DDB#define wrapfs_checkvp_barrier 1#else#define wrapfs_checkvp_barrier 0#endifstruct vnode *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -