HAMMER 17/many: Refactor IO backend, clean up buffer cache deadlocks.
[dragonfly.git] / sys / vfs / hammer / hammer_inode.c
blob75bc1aeddf4b8a13c29f16d0e914d53d72d59334
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.18 2008/01/10 07:41:03 dillon Exp $
37 #include "hammer.h"
38 #include <sys/buf.h>
39 #include <sys/buf2.h>
42 * The kernel is not actively referencing this vnode but is still holding
43 * it cached.
45 int
46 hammer_vop_inactive(struct vop_inactive_args *ap)
48 struct hammer_inode *ip = VTOI(ap->a_vp);
51 * Degenerate case
53 if (ip == NULL) {
54 vrecycle(ap->a_vp);
55 return(0);
59 * If the inode no longer has any references we recover its
60 * in-memory resources immediately.
62 if (ip->ino_rec.ino_nlinks == 0)
63 vrecycle(ap->a_vp);
64 return(0);
68 * Release the vnode association. This is typically (but not always)
69 * the last reference on the inode and will flush the inode to the
70 * buffer cache.
72 * XXX Currently our sync code only runs through inodes with vnode
73 * associations, so we depend on hammer_rel_inode() to sync any inode
74 * record data to the block device prior to losing the association.
75 * Otherwise transactions that the user expected to be distinct by
76 * doing a manual sync may be merged.
78 int
79 hammer_vop_reclaim(struct vop_reclaim_args *ap)
81 struct hammer_inode *ip;
82 struct vnode *vp;
84 vp = ap->a_vp;
86 if ((ip = vp->v_data) != NULL) {
87 vp->v_data = NULL;
88 ip->vp = NULL;
89 hammer_rel_inode(ip, 0);
91 return(0);
95 * Obtain a vnode for the specified inode number. An exclusively locked
96 * vnode is returned.
98 int
99 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
101 struct hammer_mount *hmp = (void *)mp->mnt_data;
102 struct hammer_inode *ip;
103 int error;
106 * Get/allocate the hammer_inode structure. The structure must be
107 * unlocked while we manipulate the related vnode to avoid a
108 * deadlock.
110 ip = hammer_get_inode(hmp, NULL, ino, hmp->asof, 0, &error);
111 if (ip == NULL) {
112 *vpp = NULL;
113 return(error);
115 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
116 hammer_rel_inode(ip, 0);
117 return (error);
121 * Return a locked vnode for the specified inode. The inode must be
122 * referenced but NOT LOCKED on entry and will remain referenced on
123 * return.
126 hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
128 struct vnode *vp;
129 int error = 0;
131 for (;;) {
132 if ((vp = ip->vp) == NULL) {
133 error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
134 if (error)
135 break;
136 hammer_lock_ex(&ip->lock);
137 if (ip->vp != NULL) {
138 hammer_unlock(&ip->lock);
139 vp->v_type = VBAD;
140 vx_put(vp);
141 continue;
143 hammer_ref(&ip->lock);
144 vp = *vpp;
145 ip->vp = vp;
146 vp->v_type = hammer_get_vnode_type(
147 ip->ino_rec.base.base.obj_type);
149 switch(ip->ino_rec.base.base.obj_type) {
150 case HAMMER_OBJTYPE_CDEV:
151 case HAMMER_OBJTYPE_BDEV:
152 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
153 addaliasu(vp, ip->ino_data.rmajor,
154 ip->ino_data.rminor);
155 break;
156 case HAMMER_OBJTYPE_FIFO:
157 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
158 break;
159 default:
160 break;
162 if (ip->obj_id == HAMMER_OBJID_ROOT)
163 vp->v_flag |= VROOT;
165 vp->v_data = (void *)ip;
166 /* vnode locked by getnewvnode() */
167 /* make related vnode dirty if inode dirty? */
168 hammer_unlock(&ip->lock);
169 if (vp->v_type == VREG)
170 vinitvmio(vp, ip->ino_rec.ino_size);
171 break;
175 * loop if the vget fails (aka races), or if the vp
176 * no longer matches ip->vp.
178 if (vget(vp, LK_EXCLUSIVE) == 0) {
179 if (vp == ip->vp)
180 break;
181 vput(vp);
184 *vpp = vp;
185 return(error);
189 * Acquire a HAMMER inode. The returned inode is not locked. These functions
190 * do not attach or detach the related vnode (use hammer_get_vnode() for
191 * that).
193 * The flags argument is only applied for newly created inodes, and only
194 * certain flags are inherited.
196 struct hammer_inode *
197 hammer_get_inode(struct hammer_mount *hmp, struct hammer_node **cache,
198 u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
200 struct hammer_inode_info iinfo;
201 struct hammer_cursor cursor;
202 struct hammer_inode *ip;
205 * Determine if we already have an inode cached. If we do then
206 * we are golden.
208 iinfo.obj_id = obj_id;
209 iinfo.obj_asof = asof;
210 loop:
211 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
212 if (ip) {
213 hammer_ref(&ip->lock);
214 *errorp = 0;
215 return(ip);
218 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
219 ++hammer_count_inodes;
220 ip->obj_id = obj_id;
221 ip->obj_asof = iinfo.obj_asof;
222 ip->hmp = hmp;
223 ip->flags = flags & HAMMER_INODE_RO;
224 if (hmp->ronly)
225 ip->flags |= HAMMER_INODE_RO;
226 RB_INIT(&ip->rec_tree);
229 * Locate the on-disk inode.
231 hammer_init_cursor_hmp(&cursor, cache, hmp);
232 cursor.key_beg.obj_id = ip->obj_id;
233 cursor.key_beg.key = 0;
234 cursor.key_beg.create_tid = iinfo.obj_asof;
235 cursor.key_beg.delete_tid = 0;
236 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
237 cursor.key_beg.obj_type = 0;
238 cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA;
240 *errorp = hammer_btree_lookup(&cursor);
243 * On success the B-Tree lookup will hold the appropriate
244 * buffer cache buffers and provide a pointer to the requested
245 * information. Copy the information to the in-memory inode
246 * and cache the B-Tree node to improve future operations.
248 if (*errorp == 0) {
249 ip->ino_rec = cursor.record->inode;
250 ip->ino_data = cursor.data->inode;
251 hammer_cache_node(cursor.node, &ip->cache[0]);
252 if (cache)
253 hammer_cache_node(cursor.node, cache);
257 * On success load the inode's record and data and insert the
258 * inode into the B-Tree. It is possible to race another lookup
259 * insertion of the same inode so deal with that condition too.
261 * The cursor's locked node interlocks against others creating and
262 * destroying ip while we were blocked.
264 if (*errorp == 0) {
265 hammer_ref(&ip->lock);
266 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
267 hammer_uncache_node(&ip->cache[0]);
268 hammer_uncache_node(&ip->cache[1]);
269 hammer_unref(&ip->lock);
270 --hammer_count_inodes;
271 kfree(ip, M_HAMMER);
272 hammer_done_cursor(&cursor);
273 goto loop;
275 ip->flags |= HAMMER_INODE_ONDISK;
276 } else {
277 --hammer_count_inodes;
278 kfree(ip, M_HAMMER);
279 ip = NULL;
281 hammer_done_cursor(&cursor);
282 return (ip);
286 * Create a new filesystem object, returning the inode in *ipp. The
287 * returned inode will be referenced but not locked.
289 * The inode is created in-memory and will be delay-synchronized to the
290 * disk.
293 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
294 struct ucred *cred, hammer_inode_t dip,
295 struct hammer_inode **ipp)
297 hammer_mount_t hmp;
298 hammer_inode_t ip;
299 uid_t xuid;
301 hmp = trans->hmp;
302 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
303 ++hammer_count_inodes;
304 ip->obj_id = hammer_alloc_tid(trans);
305 KKASSERT(ip->obj_id != 0);
306 ip->obj_asof = hmp->asof;
307 ip->hmp = hmp;
308 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
309 HAMMER_INODE_ITIMES;
310 ip->last_tid = trans->tid;
312 RB_INIT(&ip->rec_tree);
314 ip->ino_rec.ino_atime = trans->tid;
315 ip->ino_rec.ino_mtime = trans->tid;
316 ip->ino_rec.ino_size = 0;
317 ip->ino_rec.ino_nlinks = 0;
318 /* XXX */
319 ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
320 KKASSERT(ip->ino_rec.base.rec_id != 0);
321 ip->ino_rec.base.base.obj_id = ip->obj_id;
322 ip->ino_rec.base.base.key = 0;
323 ip->ino_rec.base.base.create_tid = trans->tid;
324 ip->ino_rec.base.base.delete_tid = 0;
325 ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
326 ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
328 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
329 ip->ino_data.mode = vap->va_mode;
330 ip->ino_data.ctime = trans->tid;
331 ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
333 switch(ip->ino_rec.base.base.obj_type) {
334 case HAMMER_OBJTYPE_CDEV:
335 case HAMMER_OBJTYPE_BDEV:
336 ip->ino_data.rmajor = vap->va_rmajor;
337 ip->ino_data.rminor = vap->va_rminor;
338 break;
339 default:
340 break;
344 * Calculate default uid/gid and overwrite with information from
345 * the vap.
347 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
348 ip->ino_data.gid = dip->ino_data.gid;
349 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
350 &vap->va_mode);
351 ip->ino_data.mode = vap->va_mode;
353 if (vap->va_vaflags & VA_UID_UUID_VALID)
354 ip->ino_data.uid = vap->va_uid_uuid;
355 else if (vap->va_uid != (uid_t)VNOVAL)
356 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
357 if (vap->va_vaflags & VA_GID_UUID_VALID)
358 ip->ino_data.gid = vap->va_gid_uuid;
359 else if (vap->va_gid != (gid_t)VNOVAL)
360 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
362 hammer_ref(&ip->lock);
363 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
364 hammer_unref(&ip->lock);
365 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
367 *ipp = ip;
368 return(0);
372 * Called by hammer_sync_inode().
374 static int
375 hammer_update_inode(hammer_inode_t ip)
377 struct hammer_cursor cursor;
378 struct hammer_cursor *spike = NULL;
379 hammer_record_t record;
380 int error;
381 hammer_tid_t last_tid;
384 * Locate the record on-disk and mark it as deleted. Both the B-Tree
385 * node and the record must be marked deleted. The record may or
386 * may not be physically deleted, depending on the retention policy.
388 * If the inode has already been deleted on-disk we have nothing
389 * to do.
391 * XXX Update the inode record and data in-place if the retention
392 * policy allows it.
394 last_tid = ip->last_tid;
395 retry:
396 error = 0;
398 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
399 HAMMER_INODE_ONDISK) {
400 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
401 cursor.key_beg.obj_id = ip->obj_id;
402 cursor.key_beg.key = 0;
403 cursor.key_beg.create_tid = ip->obj_asof;
404 cursor.key_beg.delete_tid = 0;
405 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
406 cursor.key_beg.obj_type = 0;
407 cursor.flags = HAMMER_CURSOR_GET_RECORD;
409 error = hammer_btree_lookup(&cursor);
411 if (error == 0) {
412 error = hammer_ip_delete_record(&cursor, last_tid);
413 if (error == 0)
414 ip->flags |= HAMMER_INODE_DELONDISK;
416 hammer_cache_node(cursor.node, &ip->cache[0]);
417 hammer_done_cursor(&cursor);
421 * Write out a new record if the in-memory inode is not marked
422 * as having been deleted. Update our inode statistics if this
423 * is the first application of the inode on-disk.
425 * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
426 * will remain set and prevent further updates.
428 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
429 record = hammer_alloc_mem_record(ip);
430 record->rec.inode = ip->ino_rec;
431 record->rec.inode.base.base.create_tid = last_tid;
432 record->rec.inode.base.data_len = sizeof(ip->ino_data);
433 record->data = (void *)&ip->ino_data;
434 error = hammer_ip_sync_record(record, &spike);
435 record->flags |= HAMMER_RECF_DELETED;
436 hammer_rel_mem_record(record);
437 if (error == ENOSPC) {
438 error = hammer_spike(&spike);
439 if (error == 0)
440 goto retry;
442 KKASSERT(spike == NULL);
443 if (error == 0) {
444 ip->flags &= ~(HAMMER_INODE_RDIRTY |
445 HAMMER_INODE_DDIRTY |
446 HAMMER_INODE_DELONDISK |
447 HAMMER_INODE_ITIMES);
448 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
449 hammer_modify_volume(ip->hmp->rootvol);
450 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
451 ip->flags |= HAMMER_INODE_ONDISK;
455 return(error);
459 * Update only the itimes fields. This is done no-historically. The
460 * record is updated in-place on the disk.
462 static int
463 hammer_update_itimes(hammer_inode_t ip)
465 struct hammer_cursor cursor;
466 struct hammer_inode_record *rec;
467 int error;
469 error = 0;
470 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
471 HAMMER_INODE_ONDISK) {
472 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
473 cursor.key_beg.obj_id = ip->obj_id;
474 cursor.key_beg.key = 0;
475 cursor.key_beg.create_tid = ip->obj_asof;
476 cursor.key_beg.delete_tid = 0;
477 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
478 cursor.key_beg.obj_type = 0;
479 cursor.flags = HAMMER_CURSOR_GET_RECORD;
481 error = hammer_btree_lookup(&cursor);
483 if (error == 0) {
484 rec = &cursor.record->inode;
485 hammer_modify_buffer(cursor.record_buffer);
486 rec->ino_atime = ip->ino_rec.ino_atime;
487 rec->ino_mtime = ip->ino_rec.ino_mtime;
488 ip->flags &= ~HAMMER_INODE_ITIMES;
489 /* XXX recalculate crc */
491 hammer_cache_node(cursor.node, &ip->cache[0]);
492 hammer_done_cursor(&cursor);
494 return(error);
498 * Release a reference on an inode. If asked to flush the last release
499 * will flush the inode.
501 void
502 hammer_rel_inode(struct hammer_inode *ip, int flush)
504 hammer_unref(&ip->lock);
505 if (flush)
506 ip->flags |= HAMMER_INODE_FLUSH;
507 if (ip->lock.refs == 0) {
508 if (ip->flags & HAMMER_INODE_FLUSH)
509 hammer_unload_inode(ip, (void *)MNT_WAIT);
510 else
511 hammer_unload_inode(ip, (void *)MNT_NOWAIT);
516 * Unload and destroy the specified inode.
518 * (called via RB_SCAN)
521 hammer_unload_inode(struct hammer_inode *ip, void *data)
523 int error;
525 KASSERT(ip->lock.refs == 0,
526 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
527 KKASSERT(ip->vp == NULL);
528 hammer_ref(&ip->lock);
530 error = hammer_sync_inode(ip, (int)data, 1);
531 if (error)
532 kprintf("hammer_sync_inode failed error %d\n", error);
533 if (ip->lock.refs == 1) {
534 KKASSERT(RB_EMPTY(&ip->rec_tree));
535 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
537 hammer_uncache_node(&ip->cache[0]);
538 hammer_uncache_node(&ip->cache[1]);
539 --hammer_count_inodes;
540 kfree(ip, M_HAMMER);
541 } else {
542 hammer_unref(&ip->lock);
544 return(0);
548 * A transaction has modified an inode, requiring updates as specified by
549 * the passed flags.
551 * HAMMER_INODE_RDIRTY: Inode record has been updated
552 * HAMMER_INODE_DDIRTY: Inode data has been updated
553 * HAMMER_INODE_DELETED: Inode record/data must be deleted
554 * HAMMER_INODE_ITIMES: mtime/atime has been updated
556 * last_tid is the TID to use to generate the correct TID when the inode
557 * is synced to disk.
559 void
560 hammer_modify_inode(struct hammer_transaction *trans,
561 struct hammer_inode *ip, int flags)
563 KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
564 (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
565 HAMMER_INODE_DELETED|HAMMER_INODE_ITIMES) == 0);
567 if (flags &
568 (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|HAMMER_INODE_DELETED)) {
569 if (hammer_debug_tid) {
570 kprintf("hammer_modify_inode: %016llx (%08x)\n",
571 trans->tid, (int)(trans->tid / 1000000000LL));
573 ip->last_tid = trans->tid;
575 ip->flags |= flags;
579 * Sync any dirty buffers and records associated with an inode. The
580 * inode's last_tid field is used as the transaction id for the sync,
581 * overriding any intermediate TIDs that were used for records. Note
582 * that the dirty buffer cache buffers do not have any knowledge of
583 * the transaction id they were modified under.
585 * If we can't sync due to a cluster becoming full the spike structure
586 * will be filled in and ENOSPC returned. We must return -ENOSPC to
587 * terminate the RB_SCAN.
589 static int
590 hammer_sync_inode_callback(hammer_record_t rec, void *data)
592 struct hammer_cursor **spike = data;
593 int error;
595 hammer_ref(&rec->lock);
596 error = hammer_ip_sync_record(rec, spike);
597 hammer_rel_mem_record(rec);
599 if (error) {
600 error = -error;
601 if (error != -ENOSPC) {
602 kprintf("hammer_sync_inode_callback: sync failed rec "
603 "%p, error %d\n", rec, error);
606 return(error);
610 * XXX error handling
613 hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
615 struct hammer_transaction trans;
616 struct hammer_cursor *spike = NULL;
617 int error;
619 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
620 return(0);
623 hammer_lock_ex(&ip->lock);
626 * Use the transaction id of the last operation to sync.
628 if (ip->last_tid)
629 hammer_start_transaction_tid(&trans, ip->hmp, ip->last_tid);
630 else
631 hammer_start_transaction(&trans, ip->hmp);
634 * If the inode has been deleted (nlinks == 0), and the OS no longer
635 * has any references to it (handle_delete != 0), clean up in-memory
636 * data.
638 * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
639 * setting HAMMER_INODE_DELETED takes care of it.
641 * NOTE: Because we may sync records within this new transaction,
642 * force the inode update later on to use our transaction id or
643 * the delete_tid of the inode may be less then the create_tid of
644 * the inode update. XXX shouldn't happen but don't take the chance.
646 * NOTE: The call to hammer_ip_delete_range() cannot return ENOSPC
647 * so we can pass a NULL spike structure, because no partial data
648 * deletion can occur (yet).
650 if (ip->ino_rec.ino_nlinks == 0 && handle_delete &&
651 (ip->flags & HAMMER_INODE_GONE) == 0) {
652 ip->flags |= HAMMER_INODE_GONE;
653 if (ip->vp)
654 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
655 error = hammer_ip_delete_range_all(&trans, ip);
656 KKASSERT(RB_EMPTY(&ip->rec_tree));
657 ip->ino_rec.base.base.delete_tid = trans.tid;
658 hammer_modify_inode(&trans, ip, HAMMER_INODE_DELETED);
659 hammer_modify_volume(ip->hmp->rootvol);
660 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
664 * Sync the buffer cache
666 if (ip->vp != NULL)
667 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
668 else
669 error = 0;
672 * Now sync related records
674 for (;;) {
675 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
676 hammer_sync_inode_callback, &spike);
677 KKASSERT(error <= 0);
678 if (error < 0)
679 error = -error;
680 if (error == ENOSPC) {
681 error = hammer_spike(&spike);
682 if (error == 0)
683 continue;
685 break;
687 if (RB_EMPTY(&ip->rec_tree))
688 ip->flags &= ~HAMMER_INODE_XDIRTY;
691 * Now update the inode's on-disk inode-data and/or on-disk record.
693 switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
694 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
696 * If deleted and on-disk, don't set any additional flags.
697 * the delete flag takes care of things.
699 break;
700 case HAMMER_INODE_DELETED:
702 * Take care of the case where a deleted inode was never
703 * flushed to the disk in the first place.
705 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
706 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES);
707 while (RB_ROOT(&ip->rec_tree)) {
708 hammer_record_t rec = RB_ROOT(&ip->rec_tree);
709 hammer_ref(&rec->lock);
710 rec->flags |= HAMMER_RECF_DELETED;
711 hammer_rel_mem_record(rec);
713 break;
714 case HAMMER_INODE_ONDISK:
716 * If already on-disk, do not set any additional flags.
718 break;
719 default:
721 * If not on-disk and not deleted, set both dirty flags
722 * to force an initial record to be written.
724 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
725 break;
729 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
730 * is already on-disk the old record is marked as deleted.
732 * If DELETED is set hammer_update_inode() will delete the existing
733 * record without writing out a new one.
735 * If *ONLY* the ITIMES flag is set we can update the record in-place.
737 if ((ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
738 HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) ==
739 HAMMER_INODE_ITIMES) {
740 error = hammer_update_itimes(ip);
741 } else
742 if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
743 HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) {
744 error = hammer_update_inode(ip);
746 hammer_commit_transaction(&trans);
747 hammer_unlock(&ip->lock);
748 return(error);
752 * Access the filesystem buffer containing the cluster-relative byte
753 * offset, validate the buffer type, load *bufferp and return a
754 * pointer to the requested data. The buffer is reference and locked on
755 * return.
757 * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
758 * no type or crc check is performed.
760 * If *bufferp is not NULL on entry it is assumed to contain a locked
761 * and referenced buffer which will then be replaced.
763 * If the caller is holding another unrelated buffer locked it must be
764 * passed in reorderbuf so we can properly order buffer locks.
766 * XXX add a flag for the buffer type and check the CRC here XXX
768 void *
769 hammer_bread(hammer_cluster_t cluster, int32_t cloff,
770 u_int64_t buf_type, int *errorp,
771 struct hammer_buffer **bufferp)
773 hammer_buffer_t buffer;
774 int32_t buf_no;
775 int32_t buf_off;
778 * Load the correct filesystem buffer, replacing *bufferp.
780 buf_no = cloff / HAMMER_BUFSIZE;
781 buffer = *bufferp;
782 if (buffer == NULL || buffer->cluster != cluster ||
783 buffer->buf_no != buf_no) {
784 if (buffer) {
785 /*hammer_unlock(&buffer->io.lock);*/
786 hammer_rel_buffer(buffer, 0);
788 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
789 *bufferp = buffer;
790 if (buffer == NULL)
791 return(NULL);
792 /*hammer_lock_ex(&buffer->io.lock);*/
796 * Validate the buffer type
798 buf_off = cloff & HAMMER_BUFMASK;
799 if (buf_type) {
800 if (buf_type != buffer->ondisk->head.buf_type) {
801 kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
802 buf_type, buffer->ondisk->head.buf_type);
803 *errorp = EIO;
804 return(NULL);
806 if (buf_off < sizeof(buffer->ondisk->head)) {
807 kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
808 *errorp = EIO;
809 return(NULL);
814 * Return a pointer to the buffer data.
816 *errorp = 0;
817 return((char *)buffer->ondisk + buf_off);