2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
46 * Adding a ref to an inode is only legal if the inode already has at least
50 hammer2_inode_ref(hammer2_inode_t *ip)
52 KKASSERT(ip->chain.refs > 0);
53 atomic_add_int(&ip->chain.refs, 1);
57 * Drop an inode reference, freeing the inode when the last reference goes
61 hammer2_inode_drop(hammer2_inode_t *ip)
63 if (atomic_fetchadd_int(&ip->chain.refs, -1) == 1)
64 hammer2_inode_free(ip);
68 * Get the vnode associated with the given inode, allocating the vnode if
71 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
74 * The vnode will be returned exclusively locked and referenced. The
75 * reference on the vnode prevents it from being reclaimed.
77 * The inode (ip) must be referenced by the caller and not locked to avoid
78 * it getting ripped out from under us or deadlocked.
81 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
91 * Attempt to reuse an existing vnode assignment. It is
92 * possible to race a reclaim so the vget() may fail. The
93 * inode must be unlocked during the vget() to avoid a
94 * deadlock against a reclaim.
99 * Lock the inode and check for a reclaim race
101 hammer2_inode_lock_ex(ip);
103 hammer2_inode_unlock_ex(ip);
108 * Inode must be unlocked during the vget() to avoid
109 * possible deadlocks, vnode is held to prevent
110 * destruction during the vget(). The vget() can
111 * still fail if we lost a reclaim race on the vnode.
113 vhold_interlocked(vp);
114 hammer2_inode_unlock_ex(ip);
115 if (vget(vp, LK_EXCLUSIVE)) {
120 /* vp still locked and ref from vget */
126 * No vnode exists, allocate a new vnode. Beware of
127 * allocation races. This function will return an
128 * exclusively locked and referenced vnode.
130 *errorp = getnewvnode(VT_HAMMER2, H2TOMP(hmp), &vp, 0, 0);
137 * Lock the inode and check for an allocation race.
139 hammer2_inode_lock_ex(ip);
140 if (ip->vp != NULL) {
143 hammer2_inode_unlock_ex(ip);
147 kprintf("igetv new\n");
148 switch (ip->data.type) {
149 case HAMMER2_OBJTYPE_DIRECTORY:
152 case HAMMER2_OBJTYPE_REGFILE:
154 vinitvmio(vp, 0, HAMMER2_LBUFSIZE,
155 (int)ip->data.size & HAMMER2_LBUFMASK);
159 panic("hammer2: unhandled objtype %d", ip->data.type);
163 if (ip == hmp->iroot)
164 vsetflags(vp, VROOT);
168 hammer2_inode_unlock_ex(ip);
173 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
175 kprintf("igetv vp %p refs %d aux %d\n",
176 vp, vp->v_sysref.refcnt, vp->v_auxrefs);
181 * Allocate a HAMMER2 inode memory structure.
183 * Returns a busied but unlocked inode
186 hammer2_inode_alloc(hammer2_mount_t *hmp, void *data)
190 ip = kmalloc(sizeof(hammer2_inode_t), hmp->inodes, M_WAITOK | M_ZERO);
192 atomic_add_int(&hmp->ninodes, 1);
194 lockinit(&ip->lk, "h2inode", 0, 0);
196 ip->data = *(struct hammer2_inode_data *)data;
202 * Free a HAMMER2 inode memory structure.
204 * The inode must be locked exclusively with one reference and will
205 * be destroyed on return.
208 hammer2_inode_free(hammer2_inode_t *ip)
210 hammer2_mount_t *hmp = ip->hmp;
212 KKASSERT(ip->hmp != NULL);
213 KKASSERT(ip->vp == NULL);
214 KKASSERT(ip->chain.refs == 0);
216 hammer2_chain_unlink(hmp, &ip->chain); /* XXX races */
218 atomic_add_int(&hmp->ninodes, -1);
219 ip->chain.u.ip = NULL;
221 kfree(ip, hmp->inodes);