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 hammer2_chain_ref(ip->hmp, &ip->chain);
56 * Drop an inode reference, freeing the inode when the last reference goes
60 hammer2_inode_drop(hammer2_inode_t *ip)
62 hammer2_chain_drop(ip->hmp, &ip->chain);
66 * Get the vnode associated with the given inode, allocating the vnode if
69 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
72 * The vnode will be returned exclusively locked and referenced. The
73 * reference on the vnode prevents it from being reclaimed.
75 * The inode (ip) must be referenced by the caller and not locked to avoid
76 * it getting ripped out from under us or deadlocked.
79 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
89 * Attempt to reuse an existing vnode assignment. It is
90 * possible to race a reclaim so the vget() may fail. The
91 * inode must be unlocked during the vget() to avoid a
92 * deadlock against a reclaim.
97 * Lock the inode and check for a reclaim race
99 hammer2_inode_lock_ex(ip);
101 hammer2_inode_unlock_ex(ip);
106 * Inode must be unlocked during the vget() to avoid
107 * possible deadlocks, vnode is held to prevent
108 * destruction during the vget(). The vget() can
109 * still fail if we lost a reclaim race on the vnode.
111 vhold_interlocked(vp);
112 hammer2_inode_unlock_ex(ip);
113 if (vget(vp, LK_EXCLUSIVE)) {
118 /* vp still locked and ref from vget */
124 * No vnode exists, allocate a new vnode. Beware of
125 * allocation races. This function will return an
126 * exclusively locked and referenced vnode.
128 *errorp = getnewvnode(VT_HAMMER2, H2TOMP(hmp), &vp, 0, 0);
135 * Lock the inode and check for an allocation race.
137 hammer2_inode_lock_ex(ip);
138 if (ip->vp != NULL) {
141 hammer2_inode_unlock_ex(ip);
145 kprintf("igetv new\n");
146 switch (ip->ip_data.type) {
147 case HAMMER2_OBJTYPE_DIRECTORY:
150 case HAMMER2_OBJTYPE_REGFILE:
152 vinitvmio(vp, 0, HAMMER2_LBUFSIZE,
153 (int)ip->ip_data.size & HAMMER2_LBUFMASK);
157 panic("hammer2: unhandled objtype %d",
162 if (ip == hmp->iroot)
163 vsetflags(vp, VROOT);
167 hammer2_chain_ref(hmp, &ip->chain); /* vp association */
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 * Create a new inode in the specified directory using the vattr to
182 * figure out the type of inode.
184 * If no error occurs the new inode with its chain locked is returned in
185 * *nipp, otherwise an error is returned and *nipp is set to NULL.
188 hammer2_create_inode(hammer2_mount_t *hmp,
189 struct vattr *vap, struct ucred *cred,
190 hammer2_inode_t *dip,
191 const uint8_t *name, size_t name_len,
192 hammer2_inode_t **nipp)
194 hammer2_chain_t *chain;
195 hammer2_chain_t *parent;
196 hammer2_inode_t *nip;
200 lhc = hammer2_dirhash(name, name_len);
203 * Locate the inode or indirect block to create the new
204 * entry in. At the same time check for key collisions
205 * and iterate until we don't get one.
207 parent = &dip->chain;
208 hammer2_chain_ref(hmp, parent);
209 hammer2_chain_lock(hmp, parent);
213 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
216 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
218 hammer2_chain_put(hmp, chain);
223 chain = hammer2_chain_create(hmp, &dip->chain, lhc, 0,
224 HAMMER2_BREF_TYPE_INODE,
225 HAMMER2_INODE_BYTES);
229 hammer2_chain_put(hmp, parent);
232 * Handle the error case
235 KKASSERT(chain == NULL);
241 * Set up the new inode
246 nip->ip_data.type = hammer2_get_obj_type(vap->va_type);
247 nip->ip_data.inum = hmp->voldata.alloc_tid++; /* XXX modify/lock */
248 nip->ip_data.version = HAMMER2_INODE_VERSION_ONE;
249 nip->ip_data.ctime = 0;
250 nip->ip_data.mtime = 0;
251 nip->ip_data.mode = vap->va_mode;
252 nip->ip_data.nlinks = 1;
255 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
256 bcopy(name, nip->ip_data.filename, name_len);
257 nip->ip_data.name_key = lhc;
258 nip->ip_data.name_len = name_len;