/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Basic vnode support functions.31*/32#include <types.h>33#include <kern/errno.h>34#include <lib.h>35#include <synch.h>36#include <vfs.h>37#include <vnode.h>3839/*40* Initialize an abstract vnode.41* Invoked by VOP_INIT.42*/43int44vnode_init(struct vnode *vn, const struct vnode_ops *ops,45struct fs *fs, void *fsdata)46{47KASSERT(vn!=NULL);48KASSERT(ops!=NULL);4950vn->vn_ops = ops;51vn->vn_refcount = 1;52vn->vn_opencount = 0;53vn->vn_fs = fs;54vn->vn_data = fsdata;55return 0;56}5758/*59* Destroy an abstract vnode.60* Invoked by VOP_CLEANUP.61*/62void63vnode_cleanup(struct vnode *vn)64{65KASSERT(vn->vn_refcount==1);66KASSERT(vn->vn_opencount==0);6768vn->vn_ops = NULL;69vn->vn_refcount = 0;70vn->vn_opencount = 0;71vn->vn_fs = NULL;72vn->vn_data = NULL;73}747576/*77* Increment refcount.78* Called by VOP_INCREF.79*/80void81vnode_incref(struct vnode *vn)82{83KASSERT(vn != NULL);8485vfs_biglock_acquire();8687vn->vn_refcount++;8889vfs_biglock_release();90}9192/*93* Decrement refcount.94* Called by VOP_DECREF.95* Calls VOP_RECLAIM if the refcount hits zero.96*/97void98vnode_decref(struct vnode *vn)99{100int result;101102KASSERT(vn != NULL);103104vfs_biglock_acquire();105106KASSERT(vn->vn_refcount>0);107if (vn->vn_refcount>1) {108vn->vn_refcount--;109}110else {111result = VOP_RECLAIM(vn);112if (result != 0 && result != EBUSY) {113// XXX: lame.114kprintf("vfs: Warning: VOP_RECLAIM: %s\n",115strerror(result));116}117}118119vfs_biglock_release();120}121122/*123* Increment the open count.124* Called by VOP_INCOPEN.125*/126void127vnode_incopen(struct vnode *vn)128{129KASSERT(vn != NULL);130131vfs_biglock_acquire();132vn->vn_opencount++;133vfs_biglock_release();134}135136/*137* Decrement the open count.138* Called by VOP_DECOPEN.139*/140void141vnode_decopen(struct vnode *vn)142{143int result;144145KASSERT(vn != NULL);146147vfs_biglock_acquire();148149KASSERT(vn->vn_opencount>0);150vn->vn_opencount--;151152if (vn->vn_opencount > 0) {153vfs_biglock_release();154return;155}156157result = VOP_CLOSE(vn);158if (result) {159// XXX: also lame.160// The FS should do what it can to make sure this code161// doesn't get reached...162kprintf("vfs: Warning: VOP_CLOSE: %s\n", strerror(result));163}164165vfs_biglock_release();166}167168/*169* Check for various things being valid.170* Called before all VOP_* calls.171*/172void173vnode_check(struct vnode *v, const char *opstr)174{175vfs_biglock_acquire();176177if (v == NULL) {178panic("vnode_check: vop_%s: null vnode\n", opstr);179}180if (v == (void *)0xdeadbeef) {181panic("vnode_check: vop_%s: deadbeef vnode\n", opstr);182}183184if (v->vn_ops == NULL) {185panic("vnode_check: vop_%s: null ops pointer\n", opstr);186}187if (v->vn_ops == (void *)0xdeadbeef) {188panic("vnode_check: vop_%s: deadbeef ops pointer\n", opstr);189}190191if (v->vn_ops->vop_magic != VOP_MAGIC) {192panic("vnode_check: vop_%s: ops with bad magic number %lx\n",193opstr, v->vn_ops->vop_magic);194}195196// Device vnodes have null fs pointers.197//if (v->vn_fs == NULL) {198// panic("vnode_check: vop_%s: null fs pointer\n", opstr);199//}200if (v->vn_fs == (void *)0xdeadbeef) {201panic("vnode_check: vop_%s: deadbeef fs pointer\n", opstr);202}203204if (v->vn_refcount < 0) {205panic("vnode_check: vop_%s: negative refcount %d\n", opstr,206v->vn_refcount);207}208else if (v->vn_refcount == 0 && strcmp(opstr, "reclaim")) {209panic("vnode_check: vop_%s: zero refcount\n", opstr);210}211else if (v->vn_refcount > 0x100000) {212kprintf("vnode_check: vop_%s: warning: large refcount %d\n",213opstr, v->vn_refcount);214}215216if (v->vn_opencount < 0) {217panic("vnode_check: vop_%s: negative opencount %d\n", opstr,218v->vn_opencount);219}220else if (v->vn_opencount > 0x100000) {221kprintf("vnode_check: vop_%s: warning: large opencount %d\n",222opstr, v->vn_opencount);223}224225vfs_biglock_release();226}227228229