2 * Copyright (c) 2007-2008 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_object.c,v 1.51 2008/05/03 05:28:55 dillon Exp $
39 static int hammer_mem_add(hammer_transaction_t trans, hammer_record_t record);
40 static int hammer_mem_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
41 static int hammer_mem_first(hammer_cursor_t cursor);
44 * Red-black tree support.
47 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
49 if (rec1->rec.base.base.rec_type < rec2->rec.base.base.rec_type)
51 if (rec1->rec.base.base.rec_type > rec2->rec.base.base.rec_type)
54 if (rec1->rec.base.base.key < rec2->rec.base.base.key)
56 if (rec1->rec.base.base.key > rec2->rec.base.base.key)
59 if (rec1->rec.base.base.create_tid == 0) {
60 if (rec2->rec.base.base.create_tid == 0)
64 if (rec2->rec.base.base.create_tid == 0)
67 if (rec1->rec.base.base.create_tid < rec2->rec.base.base.create_tid)
69 if (rec1->rec.base.base.create_tid > rec2->rec.base.base.create_tid)
73 * Never match against an item deleted by the front-end.
75 if (rec1->flags & HAMMER_RECF_DELETED_FE)
77 if (rec2->flags & HAMMER_RECF_DELETED_FE)
84 hammer_rec_compare(hammer_base_elm_t info, hammer_record_t rec)
86 if (info->rec_type < rec->rec.base.base.rec_type)
88 if (info->rec_type > rec->rec.base.base.rec_type)
91 if (info->key < rec->rec.base.base.key)
93 if (info->key > rec->rec.base.base.key)
96 if (info->create_tid == 0) {
97 if (rec->rec.base.base.create_tid == 0)
101 if (rec->rec.base.base.create_tid == 0)
103 if (info->create_tid < rec->rec.base.base.create_tid)
105 if (info->create_tid > rec->rec.base.base.create_tid)
111 * RB_SCAN comparison code for hammer_mem_first(). The argument order
112 * is reversed so the comparison result has to be negated. key_beg and
113 * key_end are both range-inclusive.
115 * The creation timestamp can cause hammer_rec_compare() to return -1 or +1.
116 * These do not stop the scan.
118 * Localized deletions are not cached in-memory.
122 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
124 hammer_cursor_t cursor = data;
127 r = hammer_rec_compare(&cursor->key_beg, rec);
130 r = hammer_rec_compare(&cursor->key_end, rec);
137 * This compare function is used when simply looking up key_beg.
141 hammer_rec_find_cmp(hammer_record_t rec, void *data)
143 hammer_cursor_t cursor = data;
146 r = hammer_rec_compare(&cursor->key_beg, rec);
154 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
155 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
156 hammer_rec_compare, hammer_base_elm_t);
159 * Allocate a record for the caller to finish filling in. The record is
160 * returned referenced.
163 hammer_alloc_mem_record(hammer_inode_t ip)
165 hammer_record_t record;
167 ++hammer_count_records;
168 record = kmalloc(sizeof(*record), M_HAMMER, M_WAITOK|M_ZERO);
169 record->flush_state = HAMMER_FST_IDLE;
171 record->rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
172 hammer_ref(&record->lock);
177 hammer_wait_mem_record(hammer_record_t record)
179 while (record->flush_state == HAMMER_FST_FLUSH) {
180 record->flags |= HAMMER_RECF_WANTED;
181 tsleep(record, 0, "hmrrc2", 0);
186 * Called from the backend, hammer_inode.c, after a record has been
187 * flushed to disk. The record has been exclusively locked by the
188 * caller and interlocked with BE.
190 * We clean up the state, unlock, and release the record (the record
191 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
194 hammer_flush_record_done(hammer_record_t record, int error)
196 hammer_inode_t target_ip;
198 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
199 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
203 * An error occured, the backend was unable to sync the
204 * record to its media. Leave the record intact.
206 Debugger("flush_record_done error");
209 if (record->flags & HAMMER_RECF_DELETED_BE) {
210 if ((target_ip = record->target_ip) != NULL) {
211 TAILQ_REMOVE(&target_ip->target_list, record,
213 record->target_ip = NULL;
214 hammer_test_inode(target_ip);
216 record->flush_state = HAMMER_FST_IDLE;
218 if (record->target_ip)
219 record->flush_state = HAMMER_FST_SETUP;
221 record->flush_state = HAMMER_FST_IDLE;
223 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
224 if (record->flags & HAMMER_RECF_WANTED) {
225 record->flags &= ~HAMMER_RECF_WANTED;
228 hammer_rel_mem_record(record);
232 * Release a memory record. Records marked for deletion are immediately
233 * removed from the RB-Tree but otherwise left intact until the last ref
237 hammer_rel_mem_record(struct hammer_record *record)
239 hammer_inode_t ip, target_ip;
241 hammer_unref(&record->lock);
243 if (record->flags & HAMMER_RECF_DELETED_FE) {
244 if (record->lock.refs == 0) {
245 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
248 if ((target_ip = record->target_ip) != NULL) {
249 TAILQ_REMOVE(&target_ip->target_list,
250 record, target_entry);
251 record->target_ip = NULL;
252 hammer_test_inode(target_ip);
255 if (record->flags & HAMMER_RECF_ONRBTREE) {
256 RB_REMOVE(hammer_rec_rb_tree,
257 &record->ip->rec_tree,
259 record->flags &= ~HAMMER_RECF_ONRBTREE;
260 if (RB_EMPTY(&record->ip->rec_tree)) {
261 record->ip->flags &= ~HAMMER_INODE_XDIRTY;
262 hammer_test_inode(record->ip);
265 if (record->flags & HAMMER_RECF_ALLOCDATA) {
266 --hammer_count_record_datas;
267 kfree(record->data, M_HAMMER);
268 record->flags &= ~HAMMER_RECF_ALLOCDATA;
271 --hammer_count_records;
272 kfree(record, M_HAMMER);
279 * Record visibility depends on whether the record is being accessed by
280 * the backend or the frontend.
282 * Return non-zero if the record is visible, zero if it isn't or if it is
287 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
289 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
290 if (record->flags & HAMMER_RECF_DELETED_BE)
293 if ((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0)
297 if (record->flags & HAMMER_RECF_DELETED_FE)
304 * This callback is used as part of the RB_SCAN function for in-memory
305 * records. We terminate it (return -1) as soon as we get a match.
307 * This routine is used by frontend code.
309 * The primary compare code does not account for ASOF lookups. This
310 * code handles that case as well as a few others.
314 hammer_rec_scan_callback(hammer_record_t rec, void *data)
316 hammer_cursor_t cursor = data;
319 * We terminate on success, so this should be NULL on entry.
321 KKASSERT(cursor->iprec == NULL);
324 * Skip if the record was marked deleted.
326 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
330 * Skip if not visible due to our as-of TID
332 if (cursor->flags & HAMMER_CURSOR_ASOF) {
333 if (cursor->asof < rec->rec.base.base.create_tid)
335 if (rec->rec.base.base.delete_tid &&
336 cursor->asof >= rec->rec.base.base.delete_tid) {
342 * If the record is queued to the flusher we have to block until
343 * it isn't. Otherwise we may see duplication between our memory
344 * cache and the media.
346 hammer_ref(&rec->lock);
348 #warning "This deadlocks"
350 if (rec->flush_state == HAMMER_FST_FLUSH)
351 hammer_wait_mem_record(rec);
355 * The record may have been deleted while we were blocked.
357 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
358 hammer_rel_mem_record(rec);
363 * Set the matching record and stop the scan.
371 * Lookup an in-memory record given the key specified in the cursor. Works
372 * just like hammer_btree_lookup() but operates on an inode's in-memory
375 * The lookup must fail if the record is marked for deferred deletion.
379 hammer_mem_lookup(hammer_cursor_t cursor, hammer_inode_t ip)
384 hammer_rel_mem_record(cursor->iprec);
385 cursor->iprec = NULL;
388 KKASSERT(cursor->ip->cursor_ip_refs > 0);
389 --cursor->ip->cursor_ip_refs;
391 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
392 &cursor->ip->rec_tree);
397 hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
399 ++ip->cursor_ip_refs;
402 cursor->scan.node = NULL;
404 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_find_cmp,
405 hammer_rec_scan_callback, cursor);
407 if (cursor->iprec == NULL)
415 * hammer_mem_first() - locate the first in-memory record matching the
416 * cursor within the bounds of the key range.
420 hammer_mem_first(hammer_cursor_t cursor)
425 KKASSERT(ip != NULL);
428 hammer_rel_mem_record(cursor->iprec);
429 cursor->iprec = NULL;
432 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
433 hammer_rec_scan_callback, cursor);
436 * Adjust scan.node and keep it linked into the RB-tree so we can
437 * hold the cursor through third party modifications of the RB-tree.
445 hammer_mem_done(hammer_cursor_t cursor)
448 hammer_rel_mem_record(cursor->iprec);
449 cursor->iprec = NULL;
453 /************************************************************************
454 * HAMMER IN-MEMORY RECORD FUNCTIONS *
455 ************************************************************************
457 * These functions manipulate in-memory records. Such records typically
458 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
462 * Add a directory entry (dip,ncp) which references inode (ip).
464 * Note that the low 32 bits of the namekey are set temporarily to create
465 * a unique in-memory record, and may be modified a second time when the
466 * record is synchronized to disk. In particular, the low 32 bits cannot be
467 * all 0's when synching to disk, which is not handled here.
470 hammer_ip_add_directory(struct hammer_transaction *trans,
471 struct hammer_inode *dip, struct namecache *ncp,
472 struct hammer_inode *ip)
474 hammer_record_t record;
478 record = hammer_alloc_mem_record(dip);
480 bytes = ncp->nc_nlen; /* NOTE: terminating \0 is NOT included */
481 if (++trans->hmp->namekey_iterator == 0)
482 ++trans->hmp->namekey_iterator;
484 record->type = HAMMER_MEM_RECORD_ADD;
485 record->rec.entry.base.base.obj_id = dip->obj_id;
486 record->rec.entry.base.base.key =
487 hammer_directory_namekey(ncp->nc_name, bytes);
488 record->rec.entry.base.base.key += trans->hmp->namekey_iterator;
489 record->rec.entry.base.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
490 record->rec.entry.base.base.obj_type = ip->ino_rec.base.base.obj_type;
491 record->rec.entry.obj_id = ip->obj_id;
492 record->data = (void *)ncp->nc_name;
493 record->rec.entry.base.data_len = bytes;
494 ++ip->ino_rec.ino_nlinks;
495 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
498 * The target inode and the directory entry are bound together.
500 record->target_ip = ip;
501 record->flush_state = HAMMER_FST_SETUP;
502 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
505 * The inode now has a dependancy and must be taken out of the idle
506 * state. An inode not in an idle state is given an extra reference.
508 if (ip->flush_state == HAMMER_FST_IDLE) {
509 hammer_ref(&ip->lock);
510 ip->flush_state = HAMMER_FST_SETUP;
513 /* NOTE: copies record->data */
514 error = hammer_mem_add(trans, record);
519 * Delete the directory entry and update the inode link count. The
520 * cursor must be seeked to the directory entry record being deleted.
522 * The related inode should be share-locked by the caller. The caller is
525 * This function can return EDEADLK requiring the caller to terminate
526 * the cursor, any locks, wait on the returned record, and retry.
529 hammer_ip_del_directory(struct hammer_transaction *trans,
530 hammer_cursor_t cursor, struct hammer_inode *dip,
531 struct hammer_inode *ip)
533 hammer_record_t record;
536 if (cursor->record == &cursor->iprec->rec) {
538 * In-memory (unsynchronized) records can simply be freed.
539 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
540 * by the backend, we must still avoid races against the
541 * backend potentially syncing the record to the media.
543 * We cannot call hammer_ip_delete_record(), that routine may
544 * only be called from the backend.
546 record = cursor->iprec;
547 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
548 KKASSERT(cursor->deadlk_rec == NULL);
549 hammer_ref(&record->lock);
550 cursor->deadlk_rec = record;
553 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
554 record->flags |= HAMMER_RECF_DELETED_FE;
559 * If the record is on-disk we have to queue the deletion by
560 * the record's key. This also causes lookups to skip the
563 record = hammer_alloc_mem_record(dip);
564 record->type = HAMMER_MEM_RECORD_DEL;
565 record->rec.entry.base.base = cursor->record->base.base;
566 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
568 record->target_ip = ip;
569 record->flush_state = HAMMER_FST_SETUP;
570 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
573 * The inode now has a dependancy and must be taken out of
574 * the idle state. An inode not in an idle state is given
575 * an extra reference.
577 if (ip->flush_state == HAMMER_FST_IDLE) {
578 hammer_ref(&ip->lock);
579 ip->flush_state = HAMMER_FST_SETUP;
582 error = hammer_mem_add(trans, record);
586 * One less link. The file may still be open in the OS even after
587 * all links have gone away so we only try to sync if the OS has
588 * no references and nlinks falls to 0.
590 * We have to terminate the cursor before syncing the inode to
591 * avoid deadlocking against ourselves.
593 * XXX we can't sync the inode here because the encompassing
594 * transaction might be a rename and might update the inode
595 * again with a new link. That would force the delete_tid to be
596 * the same as the create_tid and cause a panic.
599 --ip->ino_rec.ino_nlinks;
600 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
601 if (ip->ino_rec.ino_nlinks == 0 &&
602 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
603 hammer_done_cursor(cursor);
611 * Add a record to an inode.
613 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
614 * initialize the following additional fields:
616 * The related inode should be share-locked by the caller. The caller is
619 * record->rec.entry.base.base.key
620 * record->rec.entry.base.base.rec_type
621 * record->rec.entry.base.base.data_len
622 * record->data (a copy will be kmalloc'd if it cannot be embedded)
625 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
627 hammer_inode_t ip = record->ip;
630 record->rec.base.base.obj_id = ip->obj_id;
631 record->rec.base.base.obj_type = ip->ino_rec.base.base.obj_type;
633 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
635 /* NOTE: copies record->data */
636 error = hammer_mem_add(trans, record);
641 * Sync data from a buffer cache buffer (typically) to the filesystem. This
642 * is called via the strategy called from a cached data source. This code
643 * is responsible for actually writing a data record out to the disk.
645 * This can only occur non-historically (i.e. 'current' data only).
647 * The file offset must be HAMMER_BUFSIZE aligned but the data length
648 * can be truncated. The record (currently) always represents a BUFSIZE
649 * swath of space whether the data is truncated or not.
652 hammer_ip_sync_data(hammer_cursor_t cursor, hammer_inode_t ip,
653 int64_t offset, void *data, int bytes)
655 hammer_transaction_t trans = cursor->trans;
656 hammer_record_ondisk_t rec;
657 union hammer_btree_elm elm;
658 hammer_off_t rec_offset;
662 KKASSERT((offset & HAMMER_BUFMASK) == 0);
663 KKASSERT(trans->type == HAMMER_TRANS_FLS);
664 KKASSERT(bytes != 0);
666 hammer_normalize_cursor(cursor);
667 cursor->key_beg.obj_id = ip->obj_id;
668 cursor->key_beg.key = offset + bytes;
669 cursor->key_beg.create_tid = trans->tid;
670 cursor->key_beg.delete_tid = 0;
671 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
672 cursor->asof = trans->tid;
673 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
674 cursor->flags |= HAMMER_CURSOR_INSERT;
675 cursor->flags |= HAMMER_CURSOR_BACKEND;
678 * Issue a lookup to position the cursor.
680 error = hammer_btree_lookup(cursor);
682 kprintf("hammer_ip_sync_data: duplicate data at "
683 "(%lld,%d) tid %016llx\n",
684 offset, bytes, trans->tid);
685 hammer_print_btree_elm(&cursor->node->ondisk->
687 HAMMER_BTREE_TYPE_LEAF, cursor->index);
688 panic("Duplicate data");
695 * Allocate record and data space. HAMMER_RECTYPE_DATA records
696 * can cross buffer boundaries so we may have to split our bcopy.
698 rec = hammer_alloc_record(trans, &rec_offset, HAMMER_RECTYPE_DATA,
699 &cursor->record_buffer,
701 &cursor->data_buffer, &error);
704 if (hammer_debug_general & 0x1000)
705 kprintf("OOB RECOR2 DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, rec->base.data_len);
708 * Fill everything in and insert our B-Tree node.
710 * NOTE: hammer_alloc_record() has already marked the related
711 * buffers as modified. If we do it again we will generate
712 * unnecessary undo elements.
714 hammer_modify_buffer(trans, cursor->record_buffer, NULL, 0);
715 rec->base.base.btype = HAMMER_BTREE_TYPE_RECORD;
716 rec->base.base.obj_id = ip->obj_id;
717 rec->base.base.key = offset + bytes;
718 rec->base.base.create_tid = trans->tid;
719 rec->base.base.delete_tid = 0;
720 rec->base.base.rec_type = HAMMER_RECTYPE_DATA;
721 rec->base.data_crc = crc32(data, bytes);
722 hammer_modify_buffer_done(cursor->record_buffer);
723 KKASSERT(rec->base.data_len == bytes);
725 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
726 bcopy(data, bdata, bytes);
727 hammer_modify_buffer_done(cursor->data_buffer);
729 elm.leaf.base = rec->base.base;
730 elm.leaf.rec_offset = rec_offset;
731 elm.leaf.data_offset = rec->base.data_off;
732 elm.leaf.data_len = bytes;
733 elm.leaf.data_crc = rec->base.data_crc;
736 * Data records can wind up on-disk before the inode itself is
737 * on-disk. One must assume data records may be on-disk if either
738 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
740 ip->flags |= HAMMER_INODE_DONDISK;
742 error = hammer_btree_insert(cursor, &elm);
746 hammer_blockmap_free(trans, rec_offset, HAMMER_RECORD_SIZE);
748 if (error == EDEADLK) {
749 hammer_done_cursor(cursor);
750 error = hammer_init_cursor(trans, cursor, &ip->cache[0], ip);
758 * Sync an in-memory record to the disk. This is called by the backend.
759 * This code is responsible for actually writing a record out to the disk.
761 * This routine can only be called by the backend and the record
762 * must have been interlocked with BE. It will remain interlocked on
763 * return. If no error occurs the record will be marked deleted but
764 * the caller is responsible for its final disposition.
766 * Multiple calls may be aggregated with the same cursor using
767 * hammer_ip_sync_record_cursor(). The caller must handle EDEADLK
771 hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t record)
773 struct hammer_cursor cursor;
777 error = hammer_init_cursor(trans, &cursor,
778 &record->ip->cache[0], record->ip);
781 error = hammer_ip_sync_record_cursor(&cursor, record);
782 hammer_done_cursor(&cursor);
783 } while (error == EDEADLK);
788 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
790 hammer_transaction_t trans = cursor->trans;
791 hammer_record_ondisk_t rec;
792 union hammer_btree_elm elm;
793 hammer_off_t rec_offset;
797 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
798 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
800 hammer_normalize_cursor(cursor);
801 cursor->key_beg = record->rec.base.base;
802 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
803 cursor->flags |= HAMMER_CURSOR_BACKEND;
804 cursor->flags &= ~HAMMER_CURSOR_INSERT;
807 * If we are deleting an exact match must be found on-disk.
809 if (record->type == HAMMER_MEM_RECORD_DEL) {
810 error = hammer_btree_lookup(cursor);
812 error = hammer_ip_delete_record(cursor, trans->tid);
814 record->flags |= HAMMER_RECF_DELETED_FE;
815 record->flags |= HAMMER_RECF_DELETED_BE;
824 * Issue a lookup to position the cursor and locate the cluster. The
825 * target key should not exist. If we are creating a directory entry
826 * we may have to iterate the low 32 bits of the key to find an unused
829 cursor->flags |= HAMMER_CURSOR_INSERT;
832 error = hammer_btree_lookup(cursor);
835 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
836 kprintf("hammer_ip_sync_record: duplicate rec "
837 "at (%016llx)\n", record->rec.base.base.key);
838 Debugger("duplicate record1");
842 if (++trans->hmp->namekey_iterator == 0)
843 ++trans->hmp->namekey_iterator;
844 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
845 record->rec.base.base.key |= trans->hmp->namekey_iterator;
846 cursor->key_beg.key = record->rec.base.base.key;
852 * Allocate the record and data. The result buffers will be
853 * marked as being modified and further calls to
854 * hammer_modify_buffer() will result in unneeded UNDO records.
856 * Support zero-fill records (data == NULL and data_len != 0)
858 if (record->data == NULL) {
859 rec = hammer_alloc_record(trans, &rec_offset,
860 record->rec.base.base.rec_type,
861 &cursor->record_buffer,
864 if (hammer_debug_general & 0x1000)
865 kprintf("NULL RECORD DATA\n");
866 } else if (record->flags & HAMMER_RECF_INBAND) {
867 rec = hammer_alloc_record(trans, &rec_offset,
868 record->rec.base.base.rec_type,
869 &cursor->record_buffer,
870 record->rec.base.data_len, &bdata,
872 if (hammer_debug_general & 0x1000)
873 kprintf("INBAND RECORD DATA %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, record->rec.base.data_len);
875 rec = hammer_alloc_record(trans, &rec_offset,
876 record->rec.base.base.rec_type,
877 &cursor->record_buffer,
878 record->rec.base.data_len, &bdata,
879 &cursor->data_buffer, &error);
880 if (hammer_debug_general & 0x1000)
881 kprintf("OOB RECORD DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, record->rec.base.data_len);
888 * Fill in the remaining fields and insert our B-Tree node.
890 hammer_modify_buffer(trans, cursor->record_buffer, NULL, 0);
891 rec->base.base = record->rec.base.base;
892 bcopy(&record->rec.base + 1, &rec->base + 1,
893 HAMMER_RECORD_SIZE - sizeof(record->rec.base));
896 * Copy the data and deal with zero-fill support.
898 if (record->data && (record->flags & HAMMER_RECF_INBAND)) {
899 rec->base.data_crc = crc32(record->data, rec->base.data_len);
900 bcopy(record->data, bdata, rec->base.data_len);
901 } else if (record->data) {
902 rec->base.data_crc = crc32(record->data, rec->base.data_len);
903 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
904 bcopy(record->data, bdata, rec->base.data_len);
905 hammer_modify_buffer_done(cursor->data_buffer);
907 rec->base.data_len = record->rec.base.data_len;
909 hammer_modify_buffer_done(cursor->record_buffer);
911 elm.leaf.base = record->rec.base.base;
912 elm.leaf.rec_offset = rec_offset;
913 elm.leaf.data_offset = rec->base.data_off;
914 elm.leaf.data_len = rec->base.data_len;
915 elm.leaf.data_crc = rec->base.data_crc;
917 error = hammer_btree_insert(cursor, &elm);
920 * This occurs when the frontend creates a record and queues it to
921 * the backend, then tries to delete the record. The backend must
922 * still sync the record to the media as if it were not deleted,
923 * but must interlock with the frontend to ensure that the
924 * synchronized record is not visible to the frontend, which means
925 * converting it from an ADD record to a DEL record.
927 * The DEL record then masks the record synced to disk until another
928 * round can delete it for real.
931 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
932 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
933 record->flags &= ~HAMMER_RECF_DELETED_FE;
934 record->type = HAMMER_MEM_RECORD_DEL;
935 if (record->flush_state == HAMMER_FST_SETUP) {
936 hammer_test_inode(record->ip);
937 hammer_test_inode(record->target_ip);
939 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
941 record->flags |= HAMMER_RECF_DELETED_FE;
942 record->flags |= HAMMER_RECF_DELETED_BE;
945 hammer_blockmap_free(trans, rec_offset, HAMMER_RECORD_SIZE);
946 /* XXX free data buffer? */
954 * Add the record to the inode's rec_tree. The low 32 bits of a directory
955 * entry's key is used to deal with hash collisions in the upper 32 bits.
956 * A unique 64 bit key is generated in-memory and may be regenerated a
957 * second time when the directory record is flushed to the on-disk B-Tree.
959 * A referenced record is passed to this function. This function
960 * eats the reference. If an error occurs the record will be deleted.
962 * A copy of the temporary record->data pointer provided by the caller
967 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
974 * Make a private copy of record->data
978 * Try to embed the data in extra space in the record
979 * union, otherwise allocate a copy.
981 bytes = record->rec.base.data_len;
982 switch(record->rec.base.base.rec_type) {
983 case HAMMER_RECTYPE_DIRENTRY:
984 reclen = offsetof(struct hammer_entry_record, name[0]);
986 case HAMMER_RECTYPE_DATA:
987 reclen = offsetof(struct hammer_data_record, data[0]);
990 reclen = sizeof(record->rec);
993 if (reclen + bytes <= HAMMER_RECORD_SIZE) {
994 bcopy(record->data, (char *)&record->rec + reclen,
996 record->data = (void *)((char *)&record->rec + reclen);
997 record->flags |= HAMMER_RECF_INBAND;
999 ++hammer_count_record_datas;
1000 data = kmalloc(bytes, M_HAMMER, M_WAITOK);
1001 record->flags |= HAMMER_RECF_ALLOCDATA;
1002 bcopy(record->data, data, bytes);
1003 record->data = data;
1008 * Insert into the RB tree, find an unused iterator if this is
1009 * a directory entry.
1011 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1012 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
1013 record->flags |= HAMMER_RECF_DELETED_FE;
1014 hammer_rel_mem_record(record);
1017 if (++trans->hmp->namekey_iterator == 0)
1018 ++trans->hmp->namekey_iterator;
1019 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
1020 record->rec.base.base.key |= trans->hmp->namekey_iterator;
1022 record->flags |= HAMMER_RECF_ONRBTREE;
1023 hammer_modify_inode(trans, record->ip, HAMMER_INODE_XDIRTY);
1024 hammer_rel_mem_record(record);
1028 /************************************************************************
1029 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1030 ************************************************************************
1032 * These functions augment the B-Tree scanning functions in hammer_btree.c
1033 * by merging in-memory records with on-disk records.
1037 * Locate a particular record either in-memory or on-disk.
1039 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1040 * NOT be called to iterate results.
1043 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
1048 * If the element is in-memory return it without searching the
1051 error = hammer_mem_lookup(cursor, ip);
1053 cursor->record = &cursor->iprec->rec;
1056 if (error != ENOENT)
1060 * If the inode has on-disk components search the on-disk B-Tree.
1062 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1064 error = hammer_btree_lookup(cursor);
1066 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1071 * Locate the first record within the cursor's key_beg/key_end range,
1072 * restricted to a particular inode. 0 is returned on success, ENOENT
1073 * if no records matched the requested range, or some other error.
1075 * When 0 is returned hammer_ip_next() may be used to iterate additional
1076 * records within the requested range.
1078 * This function can return EDEADLK, requiring the caller to terminate
1079 * the cursor and try again.
1082 hammer_ip_first(hammer_cursor_t cursor)
1084 hammer_inode_t ip = cursor->ip;
1087 KKASSERT(ip != NULL);
1090 * Clean up fields and setup for merged scan
1092 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1093 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
1094 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
1095 if (cursor->iprec) {
1096 hammer_rel_mem_record(cursor->iprec);
1097 cursor->iprec = NULL;
1101 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1102 * exact lookup so if we get ENOENT we have to call the iterate
1103 * function to validate the first record after the begin key.
1105 * The ATEDISK flag is used by hammer_btree_iterate to determine
1106 * whether it must index forwards or not. It is also used here
1107 * to select the next record from in-memory or on-disk.
1109 * EDEADLK can only occur if the lookup hit an empty internal
1110 * element and couldn't delete it. Since this could only occur
1111 * in-range, we can just iterate from the failure point.
1113 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1114 error = hammer_btree_lookup(cursor);
1115 if (error == ENOENT || error == EDEADLK) {
1116 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1117 if (hammer_debug_general & 0x2000)
1118 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
1119 error = hammer_btree_iterate(cursor);
1121 if (error && error != ENOENT)
1124 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
1125 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1127 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1132 * Search the in-memory record list (Red-Black tree). Unlike the
1133 * B-Tree search, mem_first checks for records in the range.
1135 error = hammer_mem_first(cursor);
1136 if (error && error != ENOENT)
1139 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
1140 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1141 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0)
1142 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1146 * This will return the first matching record.
1148 return(hammer_ip_next(cursor));
1152 * Retrieve the next record in a merged iteration within the bounds of the
1153 * cursor. This call may be made multiple times after the cursor has been
1154 * initially searched with hammer_ip_first().
1156 * 0 is returned on success, ENOENT if no further records match the
1157 * requested range, or some other error code is returned.
1160 hammer_ip_next(hammer_cursor_t cursor)
1162 hammer_btree_elm_t elm;
1163 hammer_record_t rec, save;
1169 * Load the current on-disk and in-memory record. If we ate any
1170 * records we have to get the next one.
1172 * If we deleted the last on-disk record we had scanned ATEDISK will
1173 * be clear and DELBTREE will be set, forcing a call to iterate. The
1174 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1175 * element. If ATEDISK is set, iterate will skip the 'current'
1178 * Get the next on-disk record
1180 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
1181 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1182 error = hammer_btree_iterate(cursor);
1183 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1185 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1187 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1188 HAMMER_CURSOR_ATEDISK;
1194 * Get the next in-memory record. The record can be ripped out
1195 * of the RB tree so we maintain a scan_info structure to track
1198 * hammer_rec_scan_cmp: Is the record still in our general range,
1199 * (non-inclusive of snapshot exclusions)?
1200 * hammer_rec_scan_callback: Is the record in our snapshot?
1202 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1203 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1204 save = cursor->iprec;
1205 cursor->iprec = NULL;
1206 rec = save ? hammer_rec_rb_tree_RB_NEXT(save) : NULL;
1208 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1210 if (hammer_rec_scan_callback(rec, cursor) != 0)
1212 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1215 hammer_rel_mem_record(save);
1216 if (cursor->iprec) {
1217 KKASSERT(cursor->iprec == rec);
1218 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1220 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1226 * The memory record may have become stale while being held in
1227 * cursor->iprec. We are interlocked against the backend on
1228 * with regards to B-Tree entries.
1230 if ((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0) {
1231 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0) {
1232 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1238 * Extract either the disk or memory record depending on their
1239 * relative position.
1242 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1245 * Both entries valid. Return the btree entry if it is
1246 * in front of the memory entry.
1248 elm = &cursor->node->ondisk->elms[cursor->index];
1249 r = hammer_btree_cmp(&elm->base, &cursor->iprec->rec.base.base);
1251 error = hammer_btree_extract(cursor,
1252 HAMMER_CURSOR_GET_RECORD);
1253 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1258 * If the entries match exactly the memory entry typically
1259 * specifies an on-disk deletion and we eat both entries.
1261 * If the in-memory record is not an on-disk deletion we
1262 * probably caught the syncer while it was syncing it to
1263 * the media. Since we hold a shared lock on the cursor,
1264 * the in-memory record had better be marked deleted at
1268 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1269 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1270 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1271 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1275 panic("hammer_ip_next: duplicate mem/b-tree entry");
1276 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1280 /* fall through to the memory entry */
1281 case HAMMER_CURSOR_ATEDISK:
1283 * Only the memory entry is valid.
1285 cursor->record = &cursor->iprec->rec;
1286 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1289 * If the memory entry is an on-disk deletion we should have
1290 * also had found a B-Tree record. If the backend beat us
1291 * to it it would have interlocked the cursor and we should
1292 * have seen the in-memory record marked DELETED_FE.
1294 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1295 panic("hammer_ip_next: del-on-disk with no b-tree entry");
1298 case HAMMER_CURSOR_ATEMEM:
1300 * Only the disk entry is valid
1302 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1303 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1307 * Neither entry is valid
1309 * XXX error not set properly
1311 cursor->record = NULL;
1319 * Resolve the cursor->data pointer for the current cursor position in
1320 * a merged iteration.
1323 hammer_ip_resolve_data(hammer_cursor_t cursor)
1327 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1328 cursor->data = cursor->iprec->data;
1331 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1337 hammer_ip_resolve_record_and_data(hammer_cursor_t cursor)
1341 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1342 cursor->data = cursor->iprec->data;
1345 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA |
1346 HAMMER_CURSOR_GET_RECORD);
1352 * Delete all records within the specified range for inode ip.
1354 * NOTE: An unaligned range will cause new records to be added to cover
1355 * the edge cases. (XXX not implemented yet).
1357 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1359 * NOTE: Record keys for regular file data have to be special-cased since
1360 * they indicate the end of the range (key = base + bytes).
1363 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1364 int64_t ran_beg, int64_t ran_end)
1366 hammer_transaction_t trans = cursor->trans;
1367 hammer_record_ondisk_t rec;
1368 hammer_base_elm_t base;
1373 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1376 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1378 hammer_normalize_cursor(cursor);
1379 cursor->key_beg.obj_id = ip->obj_id;
1380 cursor->key_beg.create_tid = 0;
1381 cursor->key_beg.delete_tid = 0;
1382 cursor->key_beg.obj_type = 0;
1383 cursor->asof = ip->obj_asof;
1384 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1385 cursor->flags |= HAMMER_CURSOR_ASOF;
1386 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1387 cursor->flags |= HAMMER_CURSOR_BACKEND;
1389 cursor->key_end = cursor->key_beg;
1390 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1391 cursor->key_beg.key = ran_beg;
1392 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1393 cursor->key_end.rec_type = HAMMER_RECTYPE_DB;
1394 cursor->key_end.key = ran_end;
1397 * The key in the B-Tree is (base+bytes), so the first possible
1398 * matching key is ran_beg + 1.
1402 cursor->key_beg.key = ran_beg + 1;
1403 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1404 cursor->key_end.rec_type = HAMMER_RECTYPE_DATA;
1406 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1407 if (tmp64 < ran_end)
1408 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1410 cursor->key_end.key = ran_end + MAXPHYS + 1;
1412 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
1414 error = hammer_ip_first(cursor);
1417 * Iterate through matching records and mark them as deleted.
1419 while (error == 0) {
1420 rec = cursor->record;
1421 base = &rec->base.base;
1423 KKASSERT(base->delete_tid == 0);
1426 * There may be overlap cases for regular file data. Also
1427 * remember the key for a regular file record is the offset
1428 * of the last byte of the record (base + len - 1), NOT the
1432 kprintf("delete_range rec_type %02x\n", base->rec_type);
1434 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1436 kprintf("delete_range loop key %016llx,%d\n",
1437 base->key - rec->base.data_len, rec->base.data_len);
1439 off = base->key - rec->base.data_len;
1441 * Check the left edge case. We currently do not
1442 * split existing records.
1444 if (off < ran_beg) {
1445 panic("hammer left edge case %016llx %d\n",
1446 base->key, rec->base.data_len);
1450 * Check the right edge case. Note that the
1451 * record can be completely out of bounds, which
1452 * terminates the search.
1454 * base->key is exclusive of the right edge while
1455 * ran_end is inclusive of the right edge. The
1456 * (key - data_len) left boundary is inclusive.
1458 * XXX theory-check this test at some point, are
1459 * we missing a + 1 somewhere? Note that ran_end
1462 if (base->key - 1 > ran_end) {
1463 if (base->key - rec->base.data_len > ran_end)
1465 panic("hammer right edge case\n");
1470 * Mark the record and B-Tree entry as deleted. This will
1471 * also physically delete the B-Tree entry, record, and
1472 * data if the retention policy dictates. The function
1473 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1474 * uses to perform a fixup.
1476 error = hammer_ip_delete_record(cursor, trans->tid);
1479 error = hammer_ip_next(cursor);
1481 if (error == EDEADLK) {
1482 hammer_done_cursor(cursor);
1483 error = hammer_init_cursor(trans, cursor, &ip->cache[0], ip);
1487 if (error == ENOENT)
1493 * Delete all user records associated with an inode except the inode record
1494 * itself. Directory entries are not deleted (they must be properly disposed
1495 * of or nlinks would get upset).
1498 hammer_ip_delete_range_all(hammer_cursor_t cursor, hammer_inode_t ip,
1501 hammer_transaction_t trans = cursor->trans;
1502 hammer_record_ondisk_t rec;
1503 hammer_base_elm_t base;
1506 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1508 hammer_normalize_cursor(cursor);
1509 cursor->key_beg.obj_id = ip->obj_id;
1510 cursor->key_beg.create_tid = 0;
1511 cursor->key_beg.delete_tid = 0;
1512 cursor->key_beg.obj_type = 0;
1513 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1514 cursor->key_beg.key = HAMMER_MIN_KEY;
1516 cursor->key_end = cursor->key_beg;
1517 cursor->key_end.rec_type = 0xFFFF;
1518 cursor->key_end.key = HAMMER_MAX_KEY;
1520 cursor->asof = ip->obj_asof;
1521 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1522 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1523 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1524 cursor->flags |= HAMMER_CURSOR_BACKEND;
1526 error = hammer_ip_first(cursor);
1529 * Iterate through matching records and mark them as deleted.
1531 while (error == 0) {
1532 rec = cursor->record;
1533 base = &rec->base.base;
1535 KKASSERT(base->delete_tid == 0);
1538 * Mark the record and B-Tree entry as deleted. This will
1539 * also physically delete the B-Tree entry, record, and
1540 * data if the retention policy dictates. The function
1541 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1542 * uses to perform a fixup.
1544 * Directory entries (and delete-on-disk directory entries)
1545 * must be synced and cannot be deleted.
1547 if (rec->base.base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
1548 error = hammer_ip_delete_record(cursor, trans->tid);
1553 error = hammer_ip_next(cursor);
1555 if (error == EDEADLK) {
1556 hammer_done_cursor(cursor);
1557 error = hammer_init_cursor(trans, cursor, &ip->cache[0], ip);
1561 if (error == ENOENT)
1567 * Delete the record at the current cursor. On success the cursor will
1568 * be positioned appropriately for an iteration but may no longer be at
1571 * This routine is only called from the backend.
1573 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1577 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1579 hammer_btree_elm_t elm;
1584 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1587 * In-memory (unsynchronized) records can simply be freed. This
1588 * only occurs in range iterations since all other records are
1589 * individually synchronized. Thus there should be no confusion with
1592 if (cursor->record == &cursor->iprec->rec) {
1593 KKASSERT((cursor->iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
1594 cursor->iprec->flags |= HAMMER_RECF_DELETED_FE;
1595 cursor->iprec->flags |= HAMMER_RECF_DELETED_BE;
1600 * On-disk records are marked as deleted by updating their delete_tid.
1601 * This does not effect their position in the B-Tree (which is based
1602 * on their create_tid).
1604 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1606 hmp = cursor->node->hmp;
1610 error = hammer_cursor_upgrade(cursor);
1612 elm = &cursor->node->ondisk->elms[cursor->index];
1613 hammer_modify_node(cursor->trans, cursor->node,
1614 &elm->leaf.base.delete_tid,
1615 sizeof(elm->leaf.base.delete_tid));
1616 elm->leaf.base.delete_tid = tid;
1617 hammer_modify_node_done(cursor->node);
1620 * An on-disk record cannot have the same delete_tid
1621 * as its create_tid. In a chain of record updates
1622 * this could result in a duplicate record.
1624 KKASSERT(elm->leaf.base.delete_tid != elm->leaf.base.create_tid);
1625 hammer_modify_buffer(cursor->trans, cursor->record_buffer, &cursor->record->base.base.delete_tid, sizeof(hammer_tid_t));
1626 cursor->record->base.base.delete_tid = tid;
1627 hammer_modify_buffer_done(cursor->record_buffer);
1632 * If we were mounted with the nohistory option, we physically
1633 * delete the record.
1635 if (hmp->hflags & HMNT_NOHISTORY)
1638 if (error == 0 && dodelete) {
1639 error = hammer_delete_at_cursor(cursor, NULL);
1641 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1649 hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes)
1651 hammer_btree_elm_t elm;
1652 hammer_off_t rec_offset;
1653 hammer_off_t data_offset;
1658 elm = &cursor->node->ondisk->elms[cursor->index];
1659 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
1661 rec_offset = elm->leaf.rec_offset;
1662 data_offset = elm->leaf.data_offset;
1663 data_len = elm->leaf.data_len;
1664 rec_type = elm->leaf.base.rec_type;
1666 error = hammer_btree_delete(cursor);
1669 * This forces a fixup for the iteration because
1670 * the cursor is now either sitting at the 'next'
1671 * element or sitting at the end of a leaf.
1673 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1674 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1675 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1679 hammer_blockmap_free(cursor->trans, rec_offset,
1680 sizeof(union hammer_record_ondisk));
1683 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
1684 case HAMMER_ZONE_LARGE_DATA:
1685 case HAMMER_ZONE_SMALL_DATA:
1686 hammer_blockmap_free(cursor->trans,
1687 data_offset, data_len);
1694 kprintf("hammer_delete_at_cursor: %d:%d:%08x %08x/%d "
1695 "(%d remain in cluster)\n",
1696 cluster->volume->vol_no, cluster->clu_no,
1697 rec_offset, data_offset, data_len,
1698 cluster->ondisk->stat_records);
1704 * Determine whether we can remove a directory. This routine checks whether
1705 * a directory is empty or not and enforces flush connectivity.
1707 * Flush connectivity requires that we block if the target directory is
1708 * currently flushing, otherwise it may not end up in the same flush group.
1710 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
1713 hammer_ip_check_directory_empty(hammer_transaction_t trans,
1714 hammer_cursor_t parent_cursor, hammer_inode_t ip)
1716 struct hammer_cursor cursor;
1721 * Check flush connectivity
1723 if (ip->flush_state != HAMMER_FST_IDLE) {
1725 hammer_done_cursor(parent_cursor);
1726 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1727 hammer_wait_inode(ip);
1733 * Check directory empty
1735 hammer_init_cursor(trans, &cursor, &ip->cache[0], ip);
1737 cursor.key_beg.obj_id = ip->obj_id;
1738 cursor.key_beg.create_tid = 0;
1739 cursor.key_beg.delete_tid = 0;
1740 cursor.key_beg.obj_type = 0;
1741 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1742 cursor.key_beg.key = HAMMER_MIN_KEY;
1744 cursor.key_end = cursor.key_beg;
1745 cursor.key_end.rec_type = 0xFFFF;
1746 cursor.key_end.key = HAMMER_MAX_KEY;
1748 cursor.asof = ip->obj_asof;
1749 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1751 error = hammer_ip_first(&cursor);
1752 if (error == ENOENT)
1754 else if (error == 0)
1756 hammer_done_cursor(&cursor);