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.71 2008/06/20 05:38:26 dillon Exp $
39 static int hammer_mem_add(hammer_record_t record);
40 static int hammer_mem_lookup(hammer_cursor_t cursor);
41 static int hammer_mem_first(hammer_cursor_t cursor);
42 static int hammer_rec_trunc_callback(hammer_record_t record,
44 static int hammer_record_needs_overwrite_delete(hammer_record_t record);
46 struct rec_trunc_info {
52 * Red-black tree support. Comparison code for insertion.
55 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
57 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
59 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
62 if (rec1->leaf.base.key < rec2->leaf.base.key)
64 if (rec1->leaf.base.key > rec2->leaf.base.key)
68 * Never match against an item deleted by the front-end.
70 * rec1 is greater then rec2 if rec1 is marked deleted.
71 * rec1 is less then rec2 if rec2 is marked deleted.
73 * Multiple deleted records may be present, do not return 0
74 * if both are marked deleted.
76 if (rec1->flags & HAMMER_RECF_DELETED_FE)
78 if (rec2->flags & HAMMER_RECF_DELETED_FE)
85 * Basic record comparison code similar to hammer_btree_cmp().
88 hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
90 if (elm->rec_type < rec->leaf.base.rec_type)
92 if (elm->rec_type > rec->leaf.base.rec_type)
95 if (elm->key < rec->leaf.base.key)
97 if (elm->key > rec->leaf.base.key)
101 * Never match against an item deleted by the front-end.
102 * elm is less then rec if rec is marked deleted.
104 if (rec->flags & HAMMER_RECF_DELETED_FE)
110 * Special LOOKUP_INFO to locate an overlapping record. This used by
111 * the reservation code to implement small-block records (whos keys will
112 * be different depending on data_len, when representing the same base
115 * NOTE: The base file offset of a data record is (key - data_len), not (key).
118 hammer_rec_overlap_compare(hammer_btree_leaf_elm_t leaf, hammer_record_t rec)
120 if (leaf->base.rec_type < rec->leaf.base.rec_type)
122 if (leaf->base.rec_type > rec->leaf.base.rec_type)
128 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
129 /* leaf_end <= rec_beg */
130 if (leaf->base.key <= rec->leaf.base.key - rec->leaf.data_len)
132 /* leaf_beg >= rec_end */
133 if (leaf->base.key - leaf->data_len >= rec->leaf.base.key)
136 if (leaf->base.key < rec->leaf.base.key)
138 if (leaf->base.key > rec->leaf.base.key)
143 * Never match against an item deleted by the front-end.
144 * leaf is less then rec if rec is marked deleted.
146 * We must still return the proper code for the scan to continue
147 * along the correct branches.
149 if (rec->flags & HAMMER_RECF_DELETED_FE) {
150 if (leaf->base.key < rec->leaf.base.key)
152 if (leaf->base.key > rec->leaf.base.key)
160 * RB_SCAN comparison code for hammer_mem_first(). The argument order
161 * is reversed so the comparison result has to be negated. key_beg and
162 * key_end are both range-inclusive.
164 * Localized deletions are not cached in-memory.
168 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
170 hammer_cursor_t cursor = data;
173 r = hammer_rec_cmp(&cursor->key_beg, rec);
176 r = hammer_rec_cmp(&cursor->key_end, rec);
183 * This compare function is used when simply looking up key_beg.
187 hammer_rec_find_cmp(hammer_record_t rec, void *data)
189 hammer_cursor_t cursor = data;
192 r = hammer_rec_cmp(&cursor->key_beg, rec);
201 * Locate blocks within the truncation range. Partial blocks do not count.
205 hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
207 struct rec_trunc_info *info = data;
209 if (rec->leaf.base.rec_type < info->rec_type)
211 if (rec->leaf.base.rec_type > info->rec_type)
214 switch(rec->leaf.base.rec_type) {
215 case HAMMER_RECTYPE_DB:
217 * DB record key is not beyond the truncation point, retain.
219 if (rec->leaf.base.key < info->trunc_off)
222 case HAMMER_RECTYPE_DATA:
224 * DATA record offset start is not beyond the truncation point,
227 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
231 panic("hammer_rec_trunc_cmp: unexpected record type");
235 * The record start is >= the truncation point, return match,
236 * the record should be destroyed.
241 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
242 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
243 hammer_rec_overlap_compare, hammer_btree_leaf_elm_t);
246 * Allocate a record for the caller to finish filling in. The record is
247 * returned referenced.
250 hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
252 hammer_record_t record;
254 ++hammer_count_records;
255 record = kmalloc(sizeof(*record), M_HAMMER, M_WAITOK | M_ZERO);
256 record->flush_state = HAMMER_FST_IDLE;
258 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
259 record->leaf.data_len = data_len;
260 hammer_ref(&record->lock);
263 record->data = kmalloc(data_len, M_HAMMER, M_WAITOK | M_ZERO);
264 record->flags |= HAMMER_RECF_ALLOCDATA;
265 ++hammer_count_record_datas;
272 hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
274 while (record->flush_state == HAMMER_FST_FLUSH) {
275 record->flags |= HAMMER_RECF_WANTED;
276 tsleep(record, 0, ident, 0);
281 * Called from the backend, hammer_inode.c, after a record has been
282 * flushed to disk. The record has been exclusively locked by the
283 * caller and interlocked with BE.
285 * We clean up the state, unlock, and release the record (the record
286 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
289 hammer_flush_record_done(hammer_record_t record, int error)
291 hammer_inode_t target_ip;
293 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
294 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
298 * An error occured, the backend was unable to sync the
299 * record to its media. Leave the record intact.
301 Debugger("flush_record_done error");
304 if (record->flags & HAMMER_RECF_DELETED_BE) {
305 if ((target_ip = record->target_ip) != NULL) {
306 TAILQ_REMOVE(&target_ip->target_list, record,
308 record->target_ip = NULL;
309 hammer_test_inode(target_ip);
311 record->flush_state = HAMMER_FST_IDLE;
313 if (record->target_ip) {
314 record->flush_state = HAMMER_FST_SETUP;
315 hammer_test_inode(record->ip);
316 hammer_test_inode(record->target_ip);
318 record->flush_state = HAMMER_FST_IDLE;
321 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
322 if (record->flags & HAMMER_RECF_WANTED) {
323 record->flags &= ~HAMMER_RECF_WANTED;
326 hammer_rel_mem_record(record);
330 * Release a memory record. Records marked for deletion are immediately
331 * removed from the RB-Tree but otherwise left intact until the last ref
335 hammer_rel_mem_record(struct hammer_record *record)
337 hammer_inode_t ip, target_ip;
339 hammer_unref(&record->lock);
341 if (record->lock.refs == 0) {
343 * Upon release of the last reference wakeup any waiters.
344 * The record structure may get destroyed so callers will
345 * loop up and do a relookup.
347 * WARNING! Record must be removed from RB-TREE before we
348 * might possibly block. hammer_test_inode() can block!
353 * Upon release of the last reference a record marked deleted
356 if (record->flags & HAMMER_RECF_DELETED_FE) {
357 KKASSERT(ip->lock.refs > 0);
358 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
361 * target_ip may have zero refs, we have to ref it
362 * to prevent it from being ripped out from under
365 if ((target_ip = record->target_ip) != NULL) {
366 TAILQ_REMOVE(&target_ip->target_list,
367 record, target_entry);
368 record->target_ip = NULL;
369 hammer_ref(&target_ip->lock);
372 if (record->flags & HAMMER_RECF_ONRBTREE) {
373 RB_REMOVE(hammer_rec_rb_tree,
374 &record->ip->rec_tree,
376 KKASSERT(ip->rsv_recs > 0);
379 ip->hmp->rsv_databytes -= record->leaf.data_len;
380 record->flags &= ~HAMMER_RECF_ONRBTREE;
382 if (RB_EMPTY(&record->ip->rec_tree)) {
383 record->ip->flags &= ~HAMMER_INODE_XDIRTY;
384 record->ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
385 hammer_test_inode(record->ip);
390 * Do this test after removing record from the B-Tree.
393 hammer_test_inode(target_ip);
394 hammer_rel_inode(target_ip, 0);
397 if (record->flags & HAMMER_RECF_ALLOCDATA) {
398 --hammer_count_record_datas;
399 kfree(record->data, M_HAMMER);
400 record->flags &= ~HAMMER_RECF_ALLOCDATA;
403 hammer_blockmap_reserve_complete(ip->hmp,
408 --hammer_count_records;
409 kfree(record, M_HAMMER);
415 * Record visibility depends on whether the record is being accessed by
416 * the backend or the frontend.
418 * Return non-zero if the record is visible, zero if it isn't or if it is
423 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
425 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
426 if (record->flags & HAMMER_RECF_DELETED_BE)
429 if (record->flags & HAMMER_RECF_DELETED_FE)
436 * This callback is used as part of the RB_SCAN function for in-memory
437 * records. We terminate it (return -1) as soon as we get a match.
439 * This routine is used by frontend code.
441 * The primary compare code does not account for ASOF lookups. This
442 * code handles that case as well as a few others.
446 hammer_rec_scan_callback(hammer_record_t rec, void *data)
448 hammer_cursor_t cursor = data;
451 * We terminate on success, so this should be NULL on entry.
453 KKASSERT(cursor->iprec == NULL);
456 * Skip if the record was marked deleted.
458 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
462 * Skip if not visible due to our as-of TID
464 if (cursor->flags & HAMMER_CURSOR_ASOF) {
465 if (cursor->asof < rec->leaf.base.create_tid)
467 if (rec->leaf.base.delete_tid &&
468 cursor->asof >= rec->leaf.base.delete_tid) {
474 * If the record is queued to the flusher we have to block until
475 * it isn't. Otherwise we may see duplication between our memory
476 * cache and the media.
478 hammer_ref(&rec->lock);
480 #warning "This deadlocks"
482 if (rec->flush_state == HAMMER_FST_FLUSH)
483 hammer_wait_mem_record(rec);
487 * The record may have been deleted while we were blocked.
489 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
490 hammer_rel_mem_record(rec);
495 * Set the matching record and stop the scan.
503 * Lookup an in-memory record given the key specified in the cursor. Works
504 * just like hammer_btree_lookup() but operates on an inode's in-memory
507 * The lookup must fail if the record is marked for deferred deletion.
511 hammer_mem_lookup(hammer_cursor_t cursor)
515 KKASSERT(cursor->ip);
517 hammer_rel_mem_record(cursor->iprec);
518 cursor->iprec = NULL;
520 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
521 hammer_rec_scan_callback, cursor);
523 if (cursor->iprec == NULL)
531 * hammer_mem_first() - locate the first in-memory record matching the
532 * cursor within the bounds of the key range.
536 hammer_mem_first(hammer_cursor_t cursor)
541 KKASSERT(ip != NULL);
544 hammer_rel_mem_record(cursor->iprec);
545 cursor->iprec = NULL;
548 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
549 hammer_rec_scan_callback, cursor);
552 * Adjust scan.node and keep it linked into the RB-tree so we can
553 * hold the cursor through third party modifications of the RB-tree.
561 hammer_mem_done(hammer_cursor_t cursor)
564 hammer_rel_mem_record(cursor->iprec);
565 cursor->iprec = NULL;
569 /************************************************************************
570 * HAMMER IN-MEMORY RECORD FUNCTIONS *
571 ************************************************************************
573 * These functions manipulate in-memory records. Such records typically
574 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
578 * Add a directory entry (dip,ncp) which references inode (ip).
580 * Note that the low 32 bits of the namekey are set temporarily to create
581 * a unique in-memory record, and may be modified a second time when the
582 * record is synchronized to disk. In particular, the low 32 bits cannot be
583 * all 0's when synching to disk, which is not handled here.
586 hammer_ip_add_directory(struct hammer_transaction *trans,
587 struct hammer_inode *dip, struct namecache *ncp,
588 struct hammer_inode *ip)
590 hammer_record_t record;
594 bytes = ncp->nc_nlen; /* NOTE: terminating \0 is NOT included */
595 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
596 if (++trans->hmp->namekey_iterator == 0)
597 ++trans->hmp->namekey_iterator;
599 record->type = HAMMER_MEM_RECORD_ADD;
600 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
601 record->leaf.base.obj_id = dip->obj_id;
602 record->leaf.base.key = hammer_directory_namekey(ncp->nc_name, bytes);
603 record->leaf.base.key += trans->hmp->namekey_iterator;
604 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
605 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
606 record->data->entry.obj_id = ip->obj_id;
607 bcopy(ncp->nc_name, record->data->entry.name, bytes);
609 ++ip->ino_data.nlinks;
610 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
613 * The target inode and the directory entry are bound together.
615 record->target_ip = ip;
616 record->flush_state = HAMMER_FST_SETUP;
617 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
620 * The inode now has a dependancy and must be taken out of the idle
621 * state. An inode not in an idle state is given an extra reference.
623 if (ip->flush_state == HAMMER_FST_IDLE) {
624 hammer_ref(&ip->lock);
625 ip->flush_state = HAMMER_FST_SETUP;
627 error = hammer_mem_add(record);
632 * Delete the directory entry and update the inode link count. The
633 * cursor must be seeked to the directory entry record being deleted.
635 * The related inode should be share-locked by the caller. The caller is
638 * This function can return EDEADLK requiring the caller to terminate
639 * the cursor, any locks, wait on the returned record, and retry.
642 hammer_ip_del_directory(struct hammer_transaction *trans,
643 hammer_cursor_t cursor, struct hammer_inode *dip,
644 struct hammer_inode *ip)
646 hammer_record_t record;
649 if (hammer_cursor_inmem(cursor)) {
651 * In-memory (unsynchronized) records can simply be freed.
652 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
653 * by the backend, we must still avoid races against the
654 * backend potentially syncing the record to the media.
656 * We cannot call hammer_ip_delete_record(), that routine may
657 * only be called from the backend.
659 record = cursor->iprec;
660 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
661 KKASSERT(cursor->deadlk_rec == NULL);
662 hammer_ref(&record->lock);
663 cursor->deadlk_rec = record;
666 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
667 record->flags |= HAMMER_RECF_DELETED_FE;
672 * If the record is on-disk we have to queue the deletion by
673 * the record's key. This also causes lookups to skip the
676 KKASSERT(dip->flags &
677 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
678 record = hammer_alloc_mem_record(dip, 0);
679 record->type = HAMMER_MEM_RECORD_DEL;
680 record->leaf.base = cursor->leaf->base;
682 record->target_ip = ip;
683 record->flush_state = HAMMER_FST_SETUP;
684 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
687 * The inode now has a dependancy and must be taken out of
688 * the idle state. An inode not in an idle state is given
689 * an extra reference.
691 if (ip->flush_state == HAMMER_FST_IDLE) {
692 hammer_ref(&ip->lock);
693 ip->flush_state = HAMMER_FST_SETUP;
696 error = hammer_mem_add(record);
700 * One less link. The file may still be open in the OS even after
701 * all links have gone away.
703 * We have to terminate the cursor before syncing the inode to
704 * avoid deadlocking against ourselves. XXX this may no longer
707 * If nlinks drops to zero and the vnode is inactive (or there is
708 * no vnode), call hammer_inode_unloadable_check() to zonk the
709 * inode. If we don't do this here the inode will not be destroyed
710 * on-media until we unmount.
713 --ip->ino_data.nlinks;
714 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
715 if (ip->ino_data.nlinks == 0 &&
716 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
717 hammer_done_cursor(cursor);
718 hammer_inode_unloadable_check(ip, 1);
719 hammer_flush_inode(ip, 0);
727 * Add a record to an inode.
729 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
730 * initialize the following additional fields:
732 * The related inode should be share-locked by the caller. The caller is
735 * record->rec.entry.base.base.key
736 * record->rec.entry.base.base.rec_type
737 * record->rec.entry.base.base.data_len
738 * record->data (a copy will be kmalloc'd if it cannot be embedded)
741 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
743 hammer_inode_t ip = record->ip;
746 KKASSERT(record->leaf.base.localization != 0);
747 record->leaf.base.obj_id = ip->obj_id;
748 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
749 error = hammer_mem_add(record);
754 * Locate a bulk record in-memory. Bulk records allow disk space to be
755 * reserved so the front-end can flush large data writes without having
756 * to queue the BIO to the flusher. Only the related record gets queued
759 static hammer_record_t
760 hammer_ip_get_bulk(hammer_inode_t ip, off_t file_offset, int bytes)
762 hammer_record_t record;
763 struct hammer_btree_leaf_elm leaf;
765 bzero(&leaf, sizeof(leaf));
766 leaf.base.obj_id = ip->obj_id;
767 leaf.base.key = file_offset + bytes;
768 leaf.base.create_tid = 0;
769 leaf.base.delete_tid = 0;
770 leaf.base.rec_type = HAMMER_RECTYPE_DATA;
771 leaf.base.obj_type = 0; /* unused */
772 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD; /* unused */
773 leaf.base.localization = HAMMER_LOCALIZE_MISC;
774 leaf.data_len = bytes;
776 record = hammer_rec_rb_tree_RB_LOOKUP_INFO(&ip->rec_tree, &leaf);
778 hammer_ref(&record->lock);
783 * Reserve blockmap space placemarked with an in-memory record.
785 * This routine is called by the frontend in order to be able to directly
786 * flush a buffer cache buffer. The frontend has locked the related buffer
787 * cache buffers and we should be able to manipulate any overlapping
791 hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
794 hammer_record_t record;
795 hammer_record_t conflict;
800 * Deal with conflicting in-memory records. We cannot have multiple
801 * in-memory records for the same offset without seriously confusing
802 * the backend, including but not limited to the backend issuing
803 * delete-create-delete sequences and asserting on the delete_tid
804 * being the same as the create_tid.
806 * If we encounter a record with the backend interlock set we cannot
807 * immediately delete it without confusing the backend.
809 while ((conflict = hammer_ip_get_bulk(ip, file_offset, bytes)) !=NULL) {
810 if (conflict->flags & HAMMER_RECF_INTERLOCK_BE) {
811 conflict->flags |= HAMMER_RECF_WANTED;
812 tsleep(conflict, 0, "hmrrc3", 0);
814 conflict->flags |= HAMMER_RECF_DELETED_FE;
816 hammer_rel_mem_record(conflict);
820 * Create a record to cover the direct write. This is called with
821 * the related BIO locked so there should be no possible conflict.
823 * The backend is responsible for finalizing the space reserved in
826 * XXX bytes not aligned, depend on the reservation code to
827 * align the reservation.
829 record = hammer_alloc_mem_record(ip, 0);
830 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
831 HAMMER_ZONE_SMALL_DATA_INDEX;
832 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
833 &record->leaf.data_offset,
835 if (record->resv == NULL) {
836 kprintf("hammer_ip_add_bulk: reservation failed\n");
837 hammer_rel_mem_record(record);
840 record->type = HAMMER_MEM_RECORD_DATA;
841 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
842 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
843 record->leaf.base.obj_id = ip->obj_id;
844 record->leaf.base.key = file_offset + bytes;
845 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
846 record->leaf.data_len = bytes;
847 record->leaf.data_crc = crc32(data, bytes);
848 flags = record->flags;
850 hammer_ref(&record->lock); /* mem_add eats a reference */
851 *errorp = hammer_mem_add(record);
853 conflict = hammer_ip_get_bulk(ip, file_offset, bytes);
854 kprintf("hammer_ip_add_bulk: error %d conflict %p file_offset %lld bytes %d\n",
855 *errorp, conflict, file_offset, bytes);
857 kprintf("conflict %lld %d\n", conflict->leaf.base.key, conflict->leaf.data_len);
859 hammer_rel_mem_record(conflict);
861 KKASSERT(*errorp == 0);
862 conflict = hammer_ip_get_bulk(ip, file_offset, bytes);
863 if (conflict != record) {
864 kprintf("conflict mismatch %p %p %08x\n", conflict, record, record->flags);
866 kprintf("conflict mismatch %lld/%d %lld/%d\n", conflict->leaf.base.key, conflict->leaf.data_len, record->leaf.base.key, record->leaf.data_len);
868 KKASSERT(conflict == record);
869 hammer_rel_mem_record(conflict);
875 * Frontend truncation code. Scan in-memory records only. On-disk records
876 * and records in a flushing state are handled by the backend. The vnops
877 * setattr code will handle the block containing the truncation point.
879 * Partial blocks are not deleted.
882 hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
884 struct rec_trunc_info info;
886 switch(ip->ino_data.obj_type) {
887 case HAMMER_OBJTYPE_REGFILE:
888 info.rec_type = HAMMER_RECTYPE_DATA;
890 case HAMMER_OBJTYPE_DBFILE:
891 info.rec_type = HAMMER_RECTYPE_DB;
896 info.trunc_off = file_size;
897 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
898 hammer_rec_trunc_callback, &info);
903 hammer_rec_trunc_callback(hammer_record_t record, void *data __unused)
905 if (record->flags & HAMMER_RECF_DELETED_FE)
907 if (record->flush_state == HAMMER_FST_FLUSH)
909 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
910 hammer_ref(&record->lock);
911 record->flags |= HAMMER_RECF_DELETED_FE;
912 hammer_rel_mem_record(record);
917 * Return 1 if the caller must check for and delete existing records
918 * before writing out a new data record.
920 * Return 0 if the caller can just insert the record into the B-Tree without
924 hammer_record_needs_overwrite_delete(hammer_record_t record)
926 hammer_inode_t ip = record->ip;
930 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
931 file_offset = record->leaf.base.key;
933 file_offset = record->leaf.base.key - record->leaf.data_len;
934 r = (file_offset < ip->sync_trunc_off);
935 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
936 if (ip->sync_trunc_off <= record->leaf.base.key)
937 ip->sync_trunc_off = record->leaf.base.key + 1;
939 if (ip->sync_trunc_off < record->leaf.base.key)
940 ip->sync_trunc_off = record->leaf.base.key;
946 * Backend code. Sync a record to the media.
949 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
951 hammer_transaction_t trans = cursor->trans;
957 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
958 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
959 KKASSERT(record->leaf.base.localization != 0);
962 * If this is a bulk-data record placemarker there may be an existing
963 * record on-disk, indicating a data overwrite. If there is the
964 * on-disk record must be deleted before we can insert our new record.
966 * We've synthesized this record and do not know what the create_tid
967 * on-disk is, nor how much data it represents.
969 * Keep in mind that (key) for data records is (base_offset + len),
970 * not (base_offset). Also, we only want to get rid of on-disk
971 * records since we are trying to sync our in-memory record, call
972 * hammer_ip_delete_range() with truncating set to 1 to make sure
973 * it skips in-memory records.
975 * It is ok for the lookup to return ENOENT.
977 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
978 * to call hammer_ip_delete_range() or not. This also means we must
979 * update sync_trunc_off() as we write.
981 if (record->type == HAMMER_MEM_RECORD_DATA &&
982 hammer_record_needs_overwrite_delete(record)) {
983 file_offset = record->leaf.base.key - record->leaf.data_len;
984 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
986 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
987 error = hammer_ip_delete_range(
989 file_offset, file_offset + bytes - 1,
991 if (error && error != ENOENT)
998 hammer_normalize_cursor(cursor);
999 cursor->key_beg = record->leaf.base;
1000 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1001 cursor->flags |= HAMMER_CURSOR_BACKEND;
1002 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1005 * Records can wind up on-media before the inode itself is on-media.
1008 record->ip->flags |= HAMMER_INODE_DONDISK;
1011 * If we are deleting a directory entry an exact match must be
1014 if (record->type == HAMMER_MEM_RECORD_DEL) {
1015 error = hammer_btree_lookup(cursor);
1017 error = hammer_ip_delete_record(cursor, record->ip,
1020 record->flags |= HAMMER_RECF_DELETED_FE;
1021 record->flags |= HAMMER_RECF_DELETED_BE;
1030 * Issue a lookup to position the cursor and locate the cluster. The
1031 * target key should not exist. If we are creating a directory entry
1032 * we may have to iterate the low 32 bits of the key to find an unused
1035 cursor->flags |= HAMMER_CURSOR_INSERT;
1038 error = hammer_btree_lookup(cursor);
1039 if (hammer_debug_inode)
1040 kprintf("DOINSERT LOOKUP %d\n", error);
1043 if (record->leaf.base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
1044 kprintf("hammer_ip_sync_record: duplicate rec "
1045 "at (%016llx)\n", record->leaf.base.key);
1046 Debugger("duplicate record1");
1050 if (++trans->hmp->namekey_iterator == 0)
1051 ++trans->hmp->namekey_iterator;
1052 record->leaf.base.key &= ~(0xFFFFFFFFLL);
1053 record->leaf.base.key |= trans->hmp->namekey_iterator;
1054 cursor->key_beg.key = record->leaf.base.key;
1057 if (record->type == HAMMER_MEM_RECORD_DATA)
1058 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1059 record->leaf.base.key - record->leaf.data_len,
1060 record->leaf.data_offset, error);
1064 if (error != ENOENT)
1068 * Allocate the record and data. The result buffers will be
1069 * marked as being modified and further calls to
1070 * hammer_modify_buffer() will result in unneeded UNDO records.
1072 * Support zero-fill records (data == NULL and data_len != 0)
1074 if (record->type == HAMMER_MEM_RECORD_DATA) {
1076 * The data portion of a bulk-data record has already been
1077 * committed to disk, we need only adjust the layer2
1078 * statistics in the same transaction as our B-Tree insert.
1080 KKASSERT(record->leaf.data_offset != 0);
1081 hammer_blockmap_finalize(trans, record->leaf.data_offset,
1082 record->leaf.data_len);
1084 } else if (record->data && record->leaf.data_len) {
1086 * Wholely cached record, with data. Allocate the data.
1088 bdata = hammer_alloc_data(trans, record->leaf.data_len,
1089 record->leaf.base.rec_type,
1090 &record->leaf.data_offset,
1091 &cursor->data_buffer, &error);
1094 record->leaf.data_crc = crc32(record->data,
1095 record->leaf.data_len);
1096 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
1097 bcopy(record->data, bdata, record->leaf.data_len);
1098 hammer_modify_buffer_done(cursor->data_buffer);
1101 * Wholely cached record, without data.
1103 record->leaf.data_offset = 0;
1104 record->leaf.data_crc = 0;
1107 error = hammer_btree_insert(cursor, &record->leaf);
1108 if (hammer_debug_inode && error)
1109 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n", error, cursor->node->node_offset, cursor->index, record->leaf.base.key);
1112 * Our record is on-disk, normally mark the in-memory version as
1113 * deleted. If the record represented a directory deletion but
1114 * we had to sync a valid directory entry to disk we must convert
1115 * the record to a covering delete so the frontend does not have
1116 * visibility on the synced entry.
1119 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1120 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1121 record->flags &= ~HAMMER_RECF_DELETED_FE;
1122 record->type = HAMMER_MEM_RECORD_DEL;
1123 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1124 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1125 /* hammer_flush_record_done takes care of the rest */
1127 record->flags |= HAMMER_RECF_DELETED_FE;
1128 record->flags |= HAMMER_RECF_DELETED_BE;
1131 if (record->leaf.data_offset) {
1132 hammer_blockmap_free(trans, record->leaf.data_offset,
1133 record->leaf.data_len);
1142 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1143 * entry's key is used to deal with hash collisions in the upper 32 bits.
1144 * A unique 64 bit key is generated in-memory and may be regenerated a
1145 * second time when the directory record is flushed to the on-disk B-Tree.
1147 * A referenced record is passed to this function. This function
1148 * eats the reference. If an error occurs the record will be deleted.
1150 * A copy of the temporary record->data pointer provided by the caller
1155 hammer_mem_add(hammer_record_t record)
1157 hammer_mount_t hmp = record->ip->hmp;
1160 * Make a private copy of record->data
1163 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
1166 * Insert into the RB tree, find an unused iterator if this is
1167 * a directory entry.
1169 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1170 if (record->leaf.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
1171 record->flags |= HAMMER_RECF_DELETED_FE;
1172 hammer_rel_mem_record(record);
1175 if (++hmp->namekey_iterator == 0)
1176 ++hmp->namekey_iterator;
1177 record->leaf.base.key &= ~(0xFFFFFFFFLL);
1178 record->leaf.base.key |= hmp->namekey_iterator;
1180 ++hmp->count_newrecords;
1182 ++record->ip->rsv_recs;
1183 record->ip->hmp->rsv_databytes += record->leaf.data_len;
1184 record->flags |= HAMMER_RECF_ONRBTREE;
1185 hammer_modify_inode(record->ip, HAMMER_INODE_XDIRTY);
1186 hammer_rel_mem_record(record);
1190 /************************************************************************
1191 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1192 ************************************************************************
1194 * These functions augment the B-Tree scanning functions in hammer_btree.c
1195 * by merging in-memory records with on-disk records.
1199 * Locate a particular record either in-memory or on-disk.
1201 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1202 * NOT be called to iterate results.
1205 hammer_ip_lookup(hammer_cursor_t cursor)
1210 * If the element is in-memory return it without searching the
1213 KKASSERT(cursor->ip);
1214 error = hammer_mem_lookup(cursor);
1216 cursor->leaf = &cursor->iprec->leaf;
1219 if (error != ENOENT)
1223 * If the inode has on-disk components search the on-disk B-Tree.
1225 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1227 error = hammer_btree_lookup(cursor);
1229 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1234 * Locate the first record within the cursor's key_beg/key_end range,
1235 * restricted to a particular inode. 0 is returned on success, ENOENT
1236 * if no records matched the requested range, or some other error.
1238 * When 0 is returned hammer_ip_next() may be used to iterate additional
1239 * records within the requested range.
1241 * This function can return EDEADLK, requiring the caller to terminate
1242 * the cursor and try again.
1245 hammer_ip_first(hammer_cursor_t cursor)
1247 hammer_inode_t ip = cursor->ip;
1250 KKASSERT(ip != NULL);
1253 * Clean up fields and setup for merged scan
1255 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1256 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
1257 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
1258 if (cursor->iprec) {
1259 hammer_rel_mem_record(cursor->iprec);
1260 cursor->iprec = NULL;
1264 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1265 * exact lookup so if we get ENOENT we have to call the iterate
1266 * function to validate the first record after the begin key.
1268 * The ATEDISK flag is used by hammer_btree_iterate to determine
1269 * whether it must index forwards or not. It is also used here
1270 * to select the next record from in-memory or on-disk.
1272 * EDEADLK can only occur if the lookup hit an empty internal
1273 * element and couldn't delete it. Since this could only occur
1274 * in-range, we can just iterate from the failure point.
1276 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1277 error = hammer_btree_lookup(cursor);
1278 if (error == ENOENT || error == EDEADLK) {
1279 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1280 if (hammer_debug_general & 0x2000)
1281 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
1282 error = hammer_btree_iterate(cursor);
1284 if (error && error != ENOENT)
1287 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
1288 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1290 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1295 * Search the in-memory record list (Red-Black tree). Unlike the
1296 * B-Tree search, mem_first checks for records in the range.
1298 error = hammer_mem_first(cursor);
1299 if (error && error != ENOENT)
1302 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
1303 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1304 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0)
1305 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1309 * This will return the first matching record.
1311 return(hammer_ip_next(cursor));
1315 * Retrieve the next record in a merged iteration within the bounds of the
1316 * cursor. This call may be made multiple times after the cursor has been
1317 * initially searched with hammer_ip_first().
1319 * 0 is returned on success, ENOENT if no further records match the
1320 * requested range, or some other error code is returned.
1323 hammer_ip_next(hammer_cursor_t cursor)
1325 hammer_btree_elm_t elm;
1326 hammer_record_t rec, save;
1332 * Load the current on-disk and in-memory record. If we ate any
1333 * records we have to get the next one.
1335 * If we deleted the last on-disk record we had scanned ATEDISK will
1336 * be clear and DELBTREE will be set, forcing a call to iterate. The
1337 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1338 * element. If ATEDISK is set, iterate will skip the 'current'
1341 * Get the next on-disk record
1343 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
1344 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1345 error = hammer_btree_iterate(cursor);
1346 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1348 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1349 hammer_cache_node(&cursor->ip->cache[1],
1352 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1353 HAMMER_CURSOR_ATEDISK;
1360 * Get the next in-memory record. The record can be ripped out
1361 * of the RB tree so we maintain a scan_info structure to track
1364 * hammer_rec_scan_cmp: Is the record still in our general range,
1365 * (non-inclusive of snapshot exclusions)?
1366 * hammer_rec_scan_callback: Is the record in our snapshot?
1368 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1369 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1370 save = cursor->iprec;
1371 cursor->iprec = NULL;
1372 rec = save ? hammer_rec_rb_tree_RB_NEXT(save) : NULL;
1374 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1376 if (hammer_rec_scan_callback(rec, cursor) != 0)
1378 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1381 hammer_rel_mem_record(save);
1382 if (cursor->iprec) {
1383 KKASSERT(cursor->iprec == rec);
1384 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1386 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1392 * The memory record may have become stale while being held in
1393 * cursor->iprec. We are interlocked against the backend on
1394 * with regards to B-Tree entries.
1396 if ((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0) {
1397 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0) {
1398 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1404 * Extract either the disk or memory record depending on their
1405 * relative position.
1408 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1411 * Both entries valid. Compare the entries and nominally
1412 * return the first one in the sort order. Numerous cases
1413 * require special attention, however.
1415 elm = &cursor->node->ondisk->elms[cursor->index];
1416 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1419 * If the two entries differ only by their key (-2/2) or
1420 * create_tid (-1/1), and are DATA records, we may have a
1421 * nominal match. We have to calculate the base file
1422 * offset of the data.
1424 if (r <= 2 && r >= -2 && r != 0 &&
1425 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1426 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1427 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1428 int64_t base2 = cursor->iprec->leaf.base.key -
1429 cursor->iprec->leaf.data_len;
1435 error = hammer_btree_extract(cursor,
1436 HAMMER_CURSOR_GET_LEAF);
1437 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1442 * If the entries match exactly the memory entry is either
1443 * an on-disk directory entry deletion or a bulk data
1444 * overwrite. If it is a directory entry deletion we eat
1447 * For the bulk-data overwrite case it is possible to have
1448 * visibility into both, which simply means the syncer
1449 * hasn't gotten around to doing the delete+insert sequence
1450 * on the B-Tree. Use the memory entry and throw away the
1453 * If the in-memory record is not either of these we
1454 * probably caught the syncer while it was syncing it to
1455 * the media. Since we hold a shared lock on the cursor,
1456 * the in-memory record had better be marked deleted at
1460 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1461 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1462 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1463 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1466 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1467 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1468 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1470 /* fall through to memory entry */
1472 panic("hammer_ip_next: duplicate mem/b-tree entry");
1473 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1477 /* fall through to the memory entry */
1478 case HAMMER_CURSOR_ATEDISK:
1480 * Only the memory entry is valid.
1482 cursor->leaf = &cursor->iprec->leaf;
1483 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1486 * If the memory entry is an on-disk deletion we should have
1487 * also had found a B-Tree record. If the backend beat us
1488 * to it it would have interlocked the cursor and we should
1489 * have seen the in-memory record marked DELETED_FE.
1491 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1492 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1493 panic("hammer_ip_next: del-on-disk with no b-tree entry");
1496 case HAMMER_CURSOR_ATEMEM:
1498 * Only the disk entry is valid
1500 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1501 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1505 * Neither entry is valid
1507 * XXX error not set properly
1509 cursor->leaf = NULL;
1517 * Resolve the cursor->data pointer for the current cursor position in
1518 * a merged iteration.
1521 hammer_ip_resolve_data(hammer_cursor_t cursor)
1523 hammer_record_t record;
1526 if (hammer_cursor_inmem(cursor)) {
1528 * The data associated with an in-memory record is usually
1529 * kmalloced, but reserve-ahead data records will have an
1530 * on-disk reference.
1532 * NOTE: Reserve-ahead data records must be handled in the
1533 * context of the related high level buffer cache buffer
1534 * to interlock against async writes.
1536 record = cursor->iprec;
1537 cursor->data = record->data;
1539 if (cursor->data == NULL) {
1540 KKASSERT(record->leaf.base.rec_type ==
1541 HAMMER_RECTYPE_DATA);
1542 cursor->data = hammer_bread_ext(cursor->trans->hmp,
1543 record->leaf.data_offset,
1544 record->leaf.data_len,
1546 &cursor->data_buffer);
1549 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
1550 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1556 * Backend truncation / record replacement - delete records in range.
1558 * Delete all records within the specified range for inode ip. In-memory
1559 * records still associated with the frontend are ignored.
1561 * NOTE: An unaligned range will cause new records to be added to cover
1562 * the edge cases. (XXX not implemented yet).
1564 * NOTE: Replacement via reservations (see hammer_ip_sync_record_cursor())
1565 * also do not deal with unaligned ranges.
1567 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1569 * NOTE: Record keys for regular file data have to be special-cased since
1570 * they indicate the end of the range (key = base + bytes).
1573 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1574 int64_t ran_beg, int64_t ran_end, int truncating)
1576 hammer_transaction_t trans = cursor->trans;
1577 hammer_btree_leaf_elm_t leaf;
1582 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1585 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1587 hammer_normalize_cursor(cursor);
1588 cursor->key_beg.localization = HAMMER_LOCALIZE_MISC;
1589 cursor->key_beg.obj_id = ip->obj_id;
1590 cursor->key_beg.create_tid = 0;
1591 cursor->key_beg.delete_tid = 0;
1592 cursor->key_beg.obj_type = 0;
1593 cursor->asof = ip->obj_asof;
1594 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1595 cursor->flags |= HAMMER_CURSOR_ASOF;
1596 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1597 cursor->flags |= HAMMER_CURSOR_BACKEND;
1599 cursor->key_end = cursor->key_beg;
1600 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1601 cursor->key_beg.key = ran_beg;
1602 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1603 cursor->key_end.rec_type = HAMMER_RECTYPE_DB;
1604 cursor->key_end.key = ran_end;
1607 * The key in the B-Tree is (base+bytes), so the first possible
1608 * matching key is ran_beg + 1.
1612 cursor->key_beg.key = ran_beg + 1;
1613 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1614 cursor->key_end.rec_type = HAMMER_RECTYPE_DATA;
1616 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1617 if (tmp64 < ran_end)
1618 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1620 cursor->key_end.key = ran_end + MAXPHYS + 1;
1622 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
1624 error = hammer_ip_first(cursor);
1627 * Iterate through matching records and mark them as deleted.
1629 while (error == 0) {
1630 leaf = cursor->leaf;
1632 KKASSERT(leaf->base.delete_tid == 0);
1635 * There may be overlap cases for regular file data. Also
1636 * remember the key for a regular file record is (base + len),
1639 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
1640 off = leaf->base.key - leaf->data_len;
1642 * Check the left edge case. We currently do not
1643 * split existing records.
1645 if (off < ran_beg) {
1646 panic("hammer left edge case %016llx %d\n",
1647 leaf->base.key, leaf->data_len);
1651 * Check the right edge case. Note that the
1652 * record can be completely out of bounds, which
1653 * terminates the search.
1655 * base->key is exclusive of the right edge while
1656 * ran_end is inclusive of the right edge. The
1657 * (key - data_len) left boundary is inclusive.
1659 * XXX theory-check this test at some point, are
1660 * we missing a + 1 somewhere? Note that ran_end
1663 if (leaf->base.key - 1 > ran_end) {
1664 if (leaf->base.key - leaf->data_len > ran_end)
1666 panic("hammer right edge case\n");
1671 * Delete the record. When truncating we do not delete
1672 * in-memory (data) records because they represent data
1673 * written after the truncation.
1675 * This will also physically destroy the B-Tree entry and
1676 * data if the retention policy dictates. The function
1677 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1678 * uses to perform a fixup.
1680 if (truncating == 0 || hammer_cursor_ondisk(cursor))
1681 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1684 error = hammer_ip_next(cursor);
1687 hammer_cache_node(&ip->cache[1], cursor->node);
1689 if (error == EDEADLK) {
1690 hammer_done_cursor(cursor);
1691 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1695 if (error == ENOENT)
1701 * Backend truncation - delete all records.
1703 * Delete all user records associated with an inode except the inode record
1704 * itself. Directory entries are not deleted (they must be properly disposed
1705 * of or nlinks would get upset).
1708 hammer_ip_delete_range_all(hammer_cursor_t cursor, hammer_inode_t ip,
1711 hammer_transaction_t trans = cursor->trans;
1712 hammer_btree_leaf_elm_t leaf;
1715 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1717 hammer_normalize_cursor(cursor);
1718 cursor->key_beg.localization = HAMMER_LOCALIZE_MISC;
1719 cursor->key_beg.obj_id = ip->obj_id;
1720 cursor->key_beg.create_tid = 0;
1721 cursor->key_beg.delete_tid = 0;
1722 cursor->key_beg.obj_type = 0;
1723 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1724 cursor->key_beg.key = HAMMER_MIN_KEY;
1726 cursor->key_end = cursor->key_beg;
1727 cursor->key_end.rec_type = 0xFFFF;
1728 cursor->key_end.key = HAMMER_MAX_KEY;
1730 cursor->asof = ip->obj_asof;
1731 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1732 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1733 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1734 cursor->flags |= HAMMER_CURSOR_BACKEND;
1736 error = hammer_ip_first(cursor);
1739 * Iterate through matching records and mark them as deleted.
1741 while (error == 0) {
1742 leaf = cursor->leaf;
1744 KKASSERT(leaf->base.delete_tid == 0);
1747 * Mark the record and B-Tree entry as deleted. This will
1748 * also physically delete the B-Tree entry, record, and
1749 * data if the retention policy dictates. The function
1750 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1751 * uses to perform a fixup.
1753 * Directory entries (and delete-on-disk directory entries)
1754 * must be synced and cannot be deleted.
1756 if (leaf->base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
1757 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1762 error = hammer_ip_next(cursor);
1765 hammer_cache_node(&ip->cache[1], cursor->node);
1766 if (error == EDEADLK) {
1767 hammer_done_cursor(cursor);
1768 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1772 if (error == ENOENT)
1778 * Delete the record at the current cursor. On success the cursor will
1779 * be positioned appropriately for an iteration but may no longer be at
1782 * This routine is only called from the backend.
1784 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1788 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
1791 hammer_off_t zone2_offset;
1792 hammer_record_t iprec;
1793 hammer_btree_elm_t elm;
1798 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1800 hmp = cursor->node->hmp;
1803 * In-memory (unsynchronized) records can simply be freed. This
1804 * only occurs in range iterations since all other records are
1805 * individually synchronized. Thus there should be no confusion with
1808 * An in-memory record may be deleted before being committed to disk,
1809 * but could have been accessed in the mean time. The backing store
1810 * may never been marked allocated and so hammer_blockmap_free() may
1811 * never get called on it. Because of this we have to make sure that
1812 * we've gotten rid of any related hammer_buffer or buffer cache
1815 if (hammer_cursor_inmem(cursor)) {
1816 iprec = cursor->iprec;
1817 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
1818 iprec->flags |= HAMMER_RECF_DELETED_FE;
1819 iprec->flags |= HAMMER_RECF_DELETED_BE;
1821 if (iprec->leaf.data_offset && iprec->leaf.data_len) {
1822 zone2_offset = hammer_blockmap_lookup(hmp, iprec->leaf.data_offset, &error);
1823 KKASSERT(error == 0);
1824 hammer_del_buffers(hmp,
1825 iprec->leaf.data_offset,
1827 iprec->leaf.data_len);
1833 * On-disk records are marked as deleted by updating their delete_tid.
1834 * This does not effect their position in the B-Tree (which is based
1835 * on their create_tid).
1837 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1841 * If we were mounted with the nohistory option, we physically
1842 * delete the record.
1844 dodelete = hammer_nohistory(ip);
1847 error = hammer_cursor_upgrade(cursor);
1849 elm = &cursor->node->ondisk->elms[cursor->index];
1850 hammer_modify_node(cursor->trans, cursor->node,
1851 &elm->leaf.base.delete_tid,
1852 sizeof(elm->leaf.base.delete_tid));
1853 elm->leaf.base.delete_tid = tid;
1854 hammer_modify_node_done(cursor->node);
1857 * An on-disk record cannot have the same delete_tid
1858 * as its create_tid. In a chain of record updates
1859 * this could result in a duplicate record.
1861 KKASSERT(elm->leaf.base.delete_tid != elm->leaf.base.create_tid);
1865 if (error == 0 && dodelete) {
1866 error = hammer_delete_at_cursor(cursor, NULL);
1868 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1876 hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes)
1878 hammer_btree_elm_t elm;
1879 hammer_off_t data_offset;
1884 elm = &cursor->node->ondisk->elms[cursor->index];
1885 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
1887 data_offset = elm->leaf.data_offset;
1888 data_len = elm->leaf.data_len;
1889 rec_type = elm->leaf.base.rec_type;
1891 error = hammer_btree_delete(cursor);
1894 * This forces a fixup for the iteration because
1895 * the cursor is now either sitting at the 'next'
1896 * element or sitting at the end of a leaf.
1898 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1899 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1900 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1904 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
1905 case HAMMER_ZONE_LARGE_DATA:
1906 case HAMMER_ZONE_SMALL_DATA:
1907 case HAMMER_ZONE_META:
1908 hammer_blockmap_free(cursor->trans,
1909 data_offset, data_len);
1919 * Determine whether we can remove a directory. This routine checks whether
1920 * a directory is empty or not and enforces flush connectivity.
1922 * Flush connectivity requires that we block if the target directory is
1923 * currently flushing, otherwise it may not end up in the same flush group.
1925 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
1928 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1930 struct hammer_cursor cursor;
1934 * Check directory empty
1936 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
1938 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1939 cursor.key_beg.obj_id = ip->obj_id;
1940 cursor.key_beg.create_tid = 0;
1941 cursor.key_beg.delete_tid = 0;
1942 cursor.key_beg.obj_type = 0;
1943 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1944 cursor.key_beg.key = HAMMER_MIN_KEY;
1946 cursor.key_end = cursor.key_beg;
1947 cursor.key_end.rec_type = 0xFFFF;
1948 cursor.key_end.key = HAMMER_MAX_KEY;
1950 cursor.asof = ip->obj_asof;
1951 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1953 error = hammer_ip_first(&cursor);
1954 if (error == ENOENT)
1956 else if (error == 0)
1958 hammer_done_cursor(&cursor);