2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.5 2007/11/20 07:16:28 dillon Exp $
42 hammer_vop_inactive(struct vop_inactive_args *ap)
44 struct hammer_inode *ip = VTOI(ap->a_vp);
52 hammer_vop_reclaim(struct vop_reclaim_args *ap)
54 struct hammer_inode *ip;
58 if ((ip = vp->v_data) != NULL) {
61 hammer_rel_inode(ip, 1);
67 * Obtain a vnode for the specified inode number. An exclusively locked
71 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
73 struct hammer_mount *hmp = (void *)mp->mnt_data;
74 struct hammer_inode *ip;
78 * Get/allocate the hammer_inode structure. The structure must be
79 * unlocked while we manipulate the related vnode to avoid a
82 ip = hammer_get_inode(hmp, ino, &error);
87 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
88 hammer_rel_inode(ip, 0);
93 * Return a locked vnode for the specified inode. The inode must be
94 * referenced but NOT LOCKED on entry and will remain referenced on
98 hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
104 if ((vp = ip->vp) == NULL) {
105 error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
108 hammer_lock_ex(&ip->lock);
109 if (ip->vp != NULL) {
110 hammer_unlock(&ip->lock);
115 hammer_ref(&ip->lock);
118 vp->v_type = hammer_get_vnode_type(
119 ip->ino_rec.base.base.obj_type);
120 vp->v_data = (void *)ip;
121 /* vnode locked by getnewvnode() */
122 /* make related vnode dirty if inode dirty? */
123 hammer_unlock(&ip->lock);
124 if (vp->v_type == VREG)
125 vinitvmio(vp, ip->ino_rec.ino_size);
130 * loop if the vget fails (aka races), or if the vp
131 * no longer matches ip->vp.
133 if (vget(vp, LK_EXCLUSIVE) == 0) {
144 * Acquire a HAMMER inode. The returned inode is not locked. These functions
145 * do not attach or detach the related vnode (use hammer_get_vnode() for
148 struct hammer_inode *
149 hammer_get_inode(struct hammer_mount *hmp, u_int64_t obj_id, int *errorp)
151 struct hammer_inode_info iinfo;
152 struct hammer_cursor cursor;
153 struct hammer_inode *ip;
156 * Determine if we already have an inode cached. If we do then
159 iinfo.obj_id = obj_id;
160 iinfo.obj_asof = HAMMER_MAX_TID; /* XXX */
162 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
164 hammer_ref(&ip->lock);
169 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
171 ip->obj_asof = iinfo.obj_asof;
173 RB_INIT(&ip->rec_tree);
176 * Locate the on-disk inode.
177 * If we do not have an inode cached search the HAMMER on-disk B-Tree
181 hammer_init_cursor_hmp(&cursor, hmp);
182 cursor.key_beg.obj_id = ip->obj_id;
183 cursor.key_beg.key = 0;
184 cursor.key_beg.create_tid = iinfo.obj_asof;
185 cursor.key_beg.delete_tid = 0;
186 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
187 cursor.key_beg.obj_type = 0;
188 cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA;
190 *errorp = hammer_btree_lookup(&cursor);
193 * On success the B-Tree lookup will hold the appropriate
194 * buffer cache buffers and provide a pointer to the requested
195 * information. Copy the information to the in-memory inode.
198 ip->ino_rec = cursor.record->inode;
199 ip->ino_data = cursor.data->inode;
201 hammer_cache_node(cursor.node, &ip->cache);
202 hammer_done_cursor(&cursor);
205 * On success load the inode's record and data and insert the
206 * inode into the B-Tree. It is possible to race another lookup
207 * insertion of the same inode so deal with that condition too.
210 hammer_ref(&ip->lock);
211 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
212 hammer_uncache_node(&ip->cache);
213 hammer_unref(&ip->lock);
225 * Create a new filesystem object, returning the inode in *ipp. The
226 * returned inode will be referenced but not locked.
228 * The inode is created in-memory and will be delay-synchronized to the
232 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
233 struct ucred *cred, hammer_inode_t dip,
234 struct hammer_inode **ipp)
240 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
241 ip->obj_id = hammer_alloc_tid(trans);
242 kprintf("object id %llx\n", ip->obj_id);
243 KKASSERT(ip->obj_id != 0);
244 ip->obj_asof = HAMMER_MAX_TID; /* XXX */
246 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
248 ip->last_tid = trans->tid;
250 RB_INIT(&ip->rec_tree);
252 ip->ino_rec.ino_atime = trans->tid;
253 ip->ino_rec.ino_mtime = trans->tid;
254 ip->ino_rec.ino_size = 0;
255 ip->ino_rec.ino_nlinks = 0;
257 kprintf("rootvol %p ondisk %p\n", hmp->rootvol, hmp->rootvol->ondisk);
258 ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
259 KKASSERT(ip->ino_rec.base.rec_id != 0);
260 ip->ino_rec.base.base.obj_id = ip->obj_id;
261 ip->ino_rec.base.base.key = 0;
262 ip->ino_rec.base.base.create_tid = trans->tid;
263 ip->ino_rec.base.base.delete_tid = 0;
264 ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
265 ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
267 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
268 ip->ino_data.mode = vap->va_mode;
269 ip->ino_data.ctime = trans->tid;
270 ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
271 if (vap->va_vaflags & VA_UID_UUID_VALID)
272 ip->ino_data.uid = vap->va_uid_uuid;
274 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
275 if (vap->va_vaflags & VA_GID_UUID_VALID)
276 ip->ino_data.gid = vap->va_gid_uuid;
278 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
280 hammer_ref(&ip->lock);
281 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
282 hammer_unref(&ip->lock);
283 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
290 * Release a reference on an inode and unload it if told to flush
293 hammer_rel_inode(struct hammer_inode *ip, int flush)
295 hammer_unref(&ip->lock);
297 ip->flags |= HAMMER_INODE_FLUSH;
298 if (ip->lock.refs == 0 && (ip->flags & HAMMER_INODE_FLUSH))
299 hammer_unload_inode(ip, NULL);
303 * Unload and destroy the specified inode.
305 * (called via RB_SCAN)
308 hammer_unload_inode(struct hammer_inode *ip, void *data __unused)
310 KASSERT(ip->lock.refs == 0,
311 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
312 KKASSERT(ip->vp == NULL);
313 hammer_ref(&ip->lock);
314 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
316 hammer_uncache_node(&ip->cache);
324 * A transaction has modified an inode, requiring a new record and possibly
325 * also data to be written out.
328 hammer_modify_inode(struct hammer_transaction *trans,
329 struct hammer_inode *ip, int flags)
332 ip->last_tid = trans->tid;
336 * Access the filesystem buffer containing the cluster-relative byte
337 * offset, validate the buffer type, load *bufferp and return a
338 * pointer to the requested data. The buffer is reference and locked on
341 * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
342 * no type or crc check is performed.
344 * If *bufferp is not NULL on entry it is assumed to contain a locked
345 * and referenced buffer which will then be replaced.
347 * If the caller is holding another unrelated buffer locked it must be
348 * passed in reorderbuf so we can properly order buffer locks.
350 * XXX add a flag for the buffer type and check the CRC here XXX
353 hammer_bread(hammer_cluster_t cluster, int32_t cloff,
354 u_int64_t buf_type, int *errorp,
355 struct hammer_buffer **bufferp)
357 hammer_buffer_t buffer;
362 * Load the correct filesystem buffer, replacing *bufferp.
364 buf_no = cloff / HAMMER_BUFSIZE;
366 if (buffer == NULL || buffer->cluster != cluster ||
367 buffer->buf_no != buf_no) {
369 hammer_unlock(&buffer->io.lock);
370 hammer_rel_buffer(buffer, 0);
372 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
376 hammer_lock_ex(&buffer->io.lock);
380 * Validate the buffer type
382 buf_off = cloff & HAMMER_BUFMASK;
384 if (buf_type != buffer->ondisk->head.buf_type) {
385 kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
386 buf_type, buffer->ondisk->head.buf_type);
390 if (buf_off < sizeof(buffer->ondisk->head)) {
391 kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
398 * Return a pointer to the buffer data.
401 return((char *)buffer->ondisk + buf_off);