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
37 static int hammer_mem_lookup(hammer_cursor_t cursor);
38 static int hammer_mem_first(hammer_cursor_t cursor);
39 static int hammer_frontend_trunc_callback(hammer_record_t record,
41 static int hammer_bulk_scan_callback(hammer_record_t record, void *data);
42 static int hammer_record_needs_overwrite_delete(hammer_record_t record);
43 static int hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
44 hammer_btree_leaf_elm_t leaf);
45 static int hammer_cursor_localize_data(hammer_data_ondisk_t data,
46 hammer_btree_leaf_elm_t leaf);
48 struct rec_trunc_info {
53 struct hammer_bulk_info {
54 hammer_record_t record;
55 hammer_record_t conflict;
59 * Red-black tree support. Comparison code for insertion.
62 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
64 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
66 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
69 if (rec1->leaf.base.key < rec2->leaf.base.key)
71 if (rec1->leaf.base.key > rec2->leaf.base.key)
75 * For search & insertion purposes records deleted by the
76 * frontend or deleted/committed by the backend are silently
77 * ignored. Otherwise pipelined insertions will get messed
80 * rec1 is greater then rec2 if rec1 is marked deleted.
81 * rec1 is less then rec2 if rec2 is marked deleted.
83 * Multiple deleted records may be present, do not return 0
84 * if both are marked deleted.
86 if (rec1->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
87 HAMMER_RECF_COMMITTED)) {
90 if (rec2->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
91 HAMMER_RECF_COMMITTED)) {
99 * Basic record comparison code similar to hammer_btree_cmp().
101 * obj_id is not compared and may not yet be assigned in the record.
104 hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
106 if (elm->rec_type < rec->leaf.base.rec_type)
108 if (elm->rec_type > rec->leaf.base.rec_type)
111 if (elm->key < rec->leaf.base.key)
113 if (elm->key > rec->leaf.base.key)
117 * Never match against an item deleted by the frontend
118 * or backend, or committed by the backend.
120 * elm is less then rec if rec is marked deleted.
122 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
123 HAMMER_RECF_COMMITTED)) {
130 * Ranged scan to locate overlapping record(s). This is used by
131 * hammer_ip_get_bulk() to locate an overlapping record. We have
132 * to use a ranged scan because the keys for data records with the
133 * same file base offset can be different due to differing data_len's.
135 * NOTE: The base file offset of a data record is (key - data_len), not (key).
138 hammer_rec_overlap_cmp(hammer_record_t rec, void *data)
140 struct hammer_bulk_info *info = data;
141 hammer_btree_leaf_elm_t leaf = &info->record->leaf;
143 if (rec->leaf.base.rec_type < leaf->base.rec_type)
145 if (rec->leaf.base.rec_type > leaf->base.rec_type)
151 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
152 /* rec_beg >= leaf_end */
153 if (rec->leaf.base.key - rec->leaf.data_len >= leaf->base.key)
155 /* rec_end <= leaf_beg */
156 if (rec->leaf.base.key <= leaf->base.key - leaf->data_len)
159 if (rec->leaf.base.key < leaf->base.key)
161 if (rec->leaf.base.key > leaf->base.key)
166 * We have to return 0 at this point, even if DELETED_FE is set,
167 * because returning anything else will cause the scan to ignore
168 * one of the branches when we really want it to check both.
174 * RB_SCAN comparison code for hammer_mem_first(). The argument order
175 * is reversed so the comparison result has to be negated. key_beg and
176 * key_end are both range-inclusive.
178 * Localized deletions are not cached in-memory.
182 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
184 hammer_cursor_t cursor = data;
187 r = hammer_rec_cmp(&cursor->key_beg, rec);
190 r = hammer_rec_cmp(&cursor->key_end, rec);
197 * This compare function is used when simply looking up key_beg.
201 hammer_rec_find_cmp(hammer_record_t rec, void *data)
203 hammer_cursor_t cursor = data;
206 r = hammer_rec_cmp(&cursor->key_beg, rec);
215 * Locate blocks within the truncation range. Partial blocks do not count.
219 hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
221 struct rec_trunc_info *info = data;
223 if (rec->leaf.base.rec_type < info->rec_type)
225 if (rec->leaf.base.rec_type > info->rec_type)
228 switch(rec->leaf.base.rec_type) {
229 case HAMMER_RECTYPE_DB:
231 * DB record key is not beyond the truncation point, retain.
233 if (rec->leaf.base.key < info->trunc_off)
236 case HAMMER_RECTYPE_DATA:
238 * DATA record offset start is not beyond the truncation point,
241 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
245 hpanic("unexpected record type");
249 * The record start is >= the truncation point, return match,
250 * the record should be destroyed.
255 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
258 * Allocate a record for the caller to finish filling in. The record is
259 * returned referenced. In order to manually set data call this function
260 * with data_len=0 and then manually set record->leaf.data_len and
261 * record->data later.
264 hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
266 hammer_record_t record;
270 ++hammer_count_records;
271 record = kmalloc(sizeof(*record), hmp->m_misc,
272 M_WAITOK | M_ZERO | M_USE_RESERVE);
273 record->flush_state = HAMMER_FST_IDLE;
275 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
276 record->leaf.data_len = data_len;
277 hammer_ref(&record->lock);
280 record->data = kmalloc(data_len, hmp->m_misc, M_WAITOK | M_ZERO);
281 record->flags |= HAMMER_RECF_ALLOCDATA;
282 ++hammer_count_record_datas;
289 hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
291 while (record->flush_state == HAMMER_FST_FLUSH) {
292 record->flags |= HAMMER_RECF_WANTED;
293 tsleep(record, 0, ident, 0);
298 * Called from the backend, hammer_inode.c, after a record has been
299 * flushed to disk. The record has been exclusively locked by the
300 * caller and interlocked with BE.
302 * We clean up the state, unlock, and release the record (the record
303 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
306 hammer_flush_record_done(hammer_record_t record, int error)
308 hammer_inode_t target_ip;
310 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
311 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
314 * If an error occured, the backend was unable to sync the
315 * record to its media. Leave the record intact.
318 hammer_critical_error(record->ip->hmp, record->ip, error,
319 "while flushing record");
322 --record->flush_group->refs;
323 record->flush_group = NULL;
326 * Adjust the flush state and dependancy based on success or
329 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
330 if ((target_ip = record->target_ip) != NULL) {
331 TAILQ_REMOVE(&target_ip->target_list, record,
333 record->target_ip = NULL;
334 hammer_test_inode(target_ip);
336 record->flush_state = HAMMER_FST_IDLE;
338 if (record->target_ip) {
339 record->flush_state = HAMMER_FST_SETUP;
340 hammer_test_inode(record->ip);
341 hammer_test_inode(record->target_ip);
343 record->flush_state = HAMMER_FST_IDLE;
346 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
351 if (record->flags & HAMMER_RECF_WANTED) {
352 record->flags &= ~HAMMER_RECF_WANTED;
355 hammer_rel_mem_record(record);
359 * Release a memory record. Records marked for deletion are immediately
360 * removed from the RB-Tree but otherwise left intact until the last ref
364 hammer_rel_mem_record(struct hammer_record *record)
367 hammer_reserve_t resv;
369 hammer_inode_t target_ip;
372 hammer_rel(&record->lock);
374 if (hammer_norefs(&record->lock)) {
376 * Upon release of the last reference wakeup any waiters.
377 * The record structure may get destroyed so callers will
378 * loop up and do a relookup.
380 * WARNING! Record must be removed from RB-TREE before we
381 * might possibly block. hammer_test_inode() can block!
387 * Upon release of the last reference a record marked deleted
388 * by the front or backend, or committed by the backend,
391 if (record->flags & (HAMMER_RECF_DELETED_FE |
392 HAMMER_RECF_DELETED_BE |
393 HAMMER_RECF_COMMITTED)) {
394 KKASSERT(hammer_isactive(&ip->lock) > 0);
395 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
398 * target_ip may have zero refs, we have to ref it
399 * to prevent it from being ripped out from under
402 if ((target_ip = record->target_ip) != NULL) {
403 TAILQ_REMOVE(&target_ip->target_list,
404 record, target_entry);
405 record->target_ip = NULL;
406 hammer_ref(&target_ip->lock);
410 * Remove the record from the RB-Tree
412 if (record->flags & HAMMER_RECF_ONRBTREE) {
413 RB_REMOVE(hammer_rec_rb_tree,
416 record->flags &= ~HAMMER_RECF_ONRBTREE;
417 KKASSERT(ip->rsv_recs > 0);
418 if (RB_EMPTY(&ip->rec_tree)) {
419 ip->flags &= ~HAMMER_INODE_XDIRTY;
420 ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
428 * We must wait for any direct-IO to complete before
429 * we can destroy the record because the bio may
430 * have a reference to it.
433 (HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL)) {
434 hammer_io_direct_wait(record);
438 * Account for the completion after the direct IO
444 hmp->rsv_databytes -= record->leaf.data_len;
446 if (RB_EMPTY(&ip->rec_tree))
447 hammer_test_inode(ip);
448 if ((ip->flags & HAMMER_INODE_RECSW) &&
449 ip->rsv_recs <= hammer_limit_inode_recs/2) {
450 ip->flags &= ~HAMMER_INODE_RECSW;
451 wakeup(&ip->rsv_recs);
456 * Do this test after removing record from the RB-Tree.
459 hammer_test_inode(target_ip);
460 hammer_rel_inode(target_ip, 0);
463 if (record->flags & HAMMER_RECF_ALLOCDATA) {
464 --hammer_count_record_datas;
465 kfree(record->data, hmp->m_misc);
466 record->flags &= ~HAMMER_RECF_ALLOCDATA;
470 * Release the reservation.
472 * If the record was not committed we can theoretically
473 * undo the reservation. However, doing so might
474 * create weird edge cases with the ordering of
475 * direct writes because the related buffer cache
476 * elements are per-vnode. So we don't try.
478 if ((resv = record->resv) != NULL) {
479 /* XXX undo leaf.data_offset,leaf.data_len */
480 hammer_blockmap_reserve_complete(hmp, resv);
484 --hammer_count_records;
485 kfree(record, hmp->m_misc);
491 * Record visibility depends on whether the record is being accessed by
492 * the backend or the frontend. Backend tests ignore the frontend delete
493 * flag. Frontend tests do NOT ignore the backend delete/commit flags and
494 * must also check for commit races.
496 * Return non-zero if the record is visible, zero if it isn't or if it is
497 * deleted. Returns 0 if the record has been comitted (unless the special
498 * delete-visibility flag is set). A committed record must be located
499 * via the media B-Tree. Returns non-zero if the record is good.
501 * If HAMMER_CURSOR_DELETE_VISIBILITY is set we allow deleted memory
502 * records to be returned. This is so pending deletions are detected
503 * when using an iterator to locate an unused hash key, or when we need
504 * to locate historical records on-disk to destroy.
508 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
510 if (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY)
512 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
513 if (record->flags & (HAMMER_RECF_DELETED_BE |
514 HAMMER_RECF_COMMITTED)) {
518 if (record->flags & (HAMMER_RECF_DELETED_FE |
519 HAMMER_RECF_DELETED_BE |
520 HAMMER_RECF_COMMITTED)) {
528 * This callback is used as part of the RB_SCAN function for in-memory
529 * records. We terminate it (return -1) as soon as we get a match.
531 * This routine is used by frontend code.
533 * The primary compare code does not account for ASOF lookups. This
534 * code handles that case as well as a few others.
538 hammer_rec_scan_callback(hammer_record_t rec, void *data)
540 hammer_cursor_t cursor = data;
543 * We terminate on success, so this should be NULL on entry.
545 KKASSERT(cursor->iprec == NULL);
548 * Skip if the record was marked deleted or committed.
550 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
554 * Skip if not visible due to our as-of TID
556 if (cursor->flags & HAMMER_CURSOR_ASOF) {
557 if (cursor->asof < rec->leaf.base.create_tid)
559 if (rec->leaf.base.delete_tid &&
560 cursor->asof >= rec->leaf.base.delete_tid) {
566 * ref the record. The record is protected from backend B-Tree
567 * interactions by virtue of the cursor's IP lock.
569 hammer_ref(&rec->lock);
572 * The record may have been deleted or committed while we
573 * were blocked. XXX remove?
575 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
576 hammer_rel_mem_record(rec);
581 * Set the matching record and stop the scan.
589 * Lookup an in-memory record given the key specified in the cursor. Works
590 * just like hammer_btree_lookup() but operates on an inode's in-memory
593 * The lookup must fail if the record is marked for deferred deletion.
595 * The API for mem/btree_lookup() does not mess with the ATE/EOF bits.
599 hammer_mem_lookup(hammer_cursor_t cursor)
601 KKASSERT(cursor->ip != NULL);
603 hammer_rel_mem_record(cursor->iprec);
604 cursor->iprec = NULL;
606 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
607 hammer_rec_scan_callback, cursor);
609 return (cursor->iprec ? 0 : ENOENT);
613 * hammer_mem_first() - locate the first in-memory record matching the
614 * cursor within the bounds of the key range.
616 * WARNING! API is slightly different from btree_first(). hammer_mem_first()
617 * will set ATEMEM the same as MEMEOF, and does not return any error.
621 hammer_mem_first(hammer_cursor_t cursor)
623 KKASSERT(cursor->ip != NULL);
625 hammer_rel_mem_record(cursor->iprec);
626 cursor->iprec = NULL;
628 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_scan_cmp,
629 hammer_rec_scan_callback, cursor);
632 cursor->flags &= ~(HAMMER_CURSOR_MEMEOF | HAMMER_CURSOR_ATEMEM);
634 cursor->flags |= HAMMER_CURSOR_MEMEOF | HAMMER_CURSOR_ATEMEM;
636 return (cursor->iprec ? 0 : ENOENT);
639 /************************************************************************
640 * HAMMER IN-MEMORY RECORD FUNCTIONS *
641 ************************************************************************
643 * These functions manipulate in-memory records. Such records typically
644 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
648 * Add a directory entry (dip,ncp) which references inode (ip).
650 * Note that the low 32 bits of the namekey are set temporarily to create
651 * a unique in-memory record, and may be modified a second time when the
652 * record is synchronized to disk. In particular, the low 32 bits cannot be
653 * all 0's when synching to disk, which is not handled here.
655 * NOTE: bytes does not include any terminating \0 on name, and name might
659 hammer_ip_add_direntry(struct hammer_transaction *trans,
660 struct hammer_inode *dip, const char *name, int bytes,
661 struct hammer_inode *ip)
663 struct hammer_cursor cursor;
664 hammer_record_t record;
666 uint32_t max_iterations;
668 KKASSERT(dip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY);
670 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
672 record->type = HAMMER_MEM_RECORD_ADD;
673 record->leaf.base.localization = dip->obj_localization |
674 hammer_dir_localization(dip);
675 record->leaf.base.obj_id = dip->obj_id;
676 record->leaf.base.key = hammer_direntry_namekey(dip, name, bytes,
678 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
679 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
680 record->data->entry.obj_id = ip->obj_id;
681 record->data->entry.localization = ip->obj_localization;
682 bcopy(name, record->data->entry.name, bytes);
684 ++ip->ino_data.nlinks;
685 ip->ino_data.ctime = trans->time;
686 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
689 * Find an unused namekey. Both the in-memory record tree and
690 * the B-Tree are checked. We do not want historically deleted
691 * names to create a collision as our iteration space may be limited,
692 * and since create_tid wouldn't match anyway an ASOF search
693 * must be used to locate collisions.
695 * delete-visibility is set so pending deletions do not give us
696 * a false-negative on our ability to use an iterator.
698 * The iterator must not rollover the key. Directory keys only
699 * use the positive key space.
701 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
702 cursor.key_beg = record->leaf.base;
703 cursor.flags |= HAMMER_CURSOR_ASOF;
704 cursor.flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
705 cursor.asof = ip->obj_asof;
707 while (hammer_ip_lookup(&cursor) == 0) {
708 ++record->leaf.base.key;
709 KKASSERT(record->leaf.base.key > 0);
710 cursor.key_beg.key = record->leaf.base.key;
711 if (--max_iterations == 0) {
712 hammer_rel_mem_record(record);
713 hmkprintf(trans->hmp, "Failed to find an unused namekey\n");
720 * The target inode and the directory entry are bound together.
722 record->target_ip = ip;
723 record->flush_state = HAMMER_FST_SETUP;
724 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
727 * The inode now has a dependancy and must be taken out of the idle
728 * state. An inode not in an idle state is given an extra reference.
730 * When transitioning to a SETUP state flag for an automatic reflush
731 * when the dependancies are disposed of if someone is waiting on
734 if (ip->flush_state == HAMMER_FST_IDLE) {
735 hammer_ref(&ip->lock);
736 ip->flush_state = HAMMER_FST_SETUP;
737 if (ip->flags & HAMMER_INODE_FLUSHW)
738 ip->flags |= HAMMER_INODE_REFLUSH;
740 error = hammer_mem_add(record);
742 dip->ino_data.mtime = trans->time;
743 hammer_modify_inode(trans, dip, HAMMER_INODE_MTIME);
746 hammer_done_cursor(&cursor);
751 * Delete the directory entry and update the inode link count. The
752 * cursor must be seeked to the directory entry record being deleted.
754 * The related inode should be share-locked by the caller. The caller is
755 * on the frontend. It could also be NULL indicating that the directory
756 * entry being removed has no related inode.
758 * This function can return EDEADLK requiring the caller to terminate
759 * the cursor, any locks, wait on the returned record, and retry.
762 hammer_ip_del_direntry(struct hammer_transaction *trans,
763 hammer_cursor_t cursor, struct hammer_inode *dip,
764 struct hammer_inode *ip)
766 hammer_record_t record;
769 if (hammer_cursor_inmem(cursor)) {
771 * In-memory (unsynchronized) records can simply be freed.
773 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
774 * by the backend, we must still avoid races against the
775 * backend potentially syncing the record to the media.
777 * We cannot call hammer_ip_delete_record(), that routine may
778 * only be called from the backend.
780 record = cursor->iprec;
781 if (record->flags & (HAMMER_RECF_INTERLOCK_BE |
782 HAMMER_RECF_DELETED_BE |
783 HAMMER_RECF_COMMITTED)) {
784 KKASSERT(cursor->deadlk_rec == NULL);
785 hammer_ref(&record->lock);
786 cursor->deadlk_rec = record;
789 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
790 record->flags |= HAMMER_RECF_DELETED_FE;
795 * If the record is on-disk we have to queue the deletion by
796 * the record's key. This also causes lookups to skip the
797 * record (lookups for the purposes of finding an unused
798 * directory key do not skip the record).
800 KKASSERT(dip->flags &
801 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
802 record = hammer_alloc_mem_record(dip, 0);
803 record->type = HAMMER_MEM_RECORD_DEL;
804 record->leaf.base = cursor->leaf->base;
805 KKASSERT(dip->obj_id == record->leaf.base.obj_id);
808 * ip may be NULL, indicating the deletion of a directory
809 * entry which has no related inode.
811 record->target_ip = ip;
813 record->flush_state = HAMMER_FST_SETUP;
814 TAILQ_INSERT_TAIL(&ip->target_list, record,
817 record->flush_state = HAMMER_FST_IDLE;
821 * The inode now has a dependancy and must be taken out of
822 * the idle state. An inode not in an idle state is given
823 * an extra reference.
825 * When transitioning to a SETUP state flag for an automatic
826 * reflush when the dependancies are disposed of if someone
827 * is waiting on the inode.
829 if (ip && ip->flush_state == HAMMER_FST_IDLE) {
830 hammer_ref(&ip->lock);
831 ip->flush_state = HAMMER_FST_SETUP;
832 if (ip->flags & HAMMER_INODE_FLUSHW)
833 ip->flags |= HAMMER_INODE_REFLUSH;
836 error = hammer_mem_add(record);
840 * One less link. The file may still be open in the OS even after
841 * all links have gone away.
843 * We have to terminate the cursor before syncing the inode to
844 * avoid deadlocking against ourselves. XXX this may no longer
847 * If nlinks drops to zero and the vnode is inactive (or there is
848 * no vnode), call hammer_inode_unloadable_check() to zonk the
849 * inode. If we don't do this here the inode will not be destroyed
850 * on-media until we unmount.
854 --ip->ino_data.nlinks; /* do before we might block */
855 ip->ino_data.ctime = trans->time;
857 dip->ino_data.mtime = trans->time;
858 hammer_modify_inode(trans, dip, HAMMER_INODE_MTIME);
860 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
861 if (ip->ino_data.nlinks == 0 &&
862 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
863 hammer_done_cursor(cursor);
864 hammer_inode_unloadable_check(ip, 1);
865 hammer_flush_inode(ip, 0);
874 * Add a record to an inode.
876 * The caller must allocate the record with hammer_alloc_mem_record(ip,len) and
877 * initialize the following additional fields that are not initialized by these
880 * The related inode should be share-locked by the caller. The caller is
883 * record->leaf.base.key
884 * record->leaf.base.rec_type
885 * record->leaf.base.localization
888 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
890 hammer_inode_t ip = record->ip;
893 KKASSERT(record->leaf.base.localization != 0);
894 record->leaf.base.obj_id = ip->obj_id;
895 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
896 error = hammer_mem_add(record);
901 * Locate a pre-existing bulk record in memory. The caller wishes to
902 * replace the record with a new one. The existing record may have a
903 * different length (and thus a different key) so we have to use an
904 * overlap check function.
906 static hammer_record_t
907 hammer_ip_get_bulk(hammer_record_t record)
909 struct hammer_bulk_info info;
910 hammer_inode_t ip = record->ip;
912 info.record = record;
913 info.conflict = NULL;
914 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_overlap_cmp,
915 hammer_bulk_scan_callback, &info);
917 return(info.conflict); /* may be NULL */
921 * Take records vetted by overlap_cmp. The first non-deleted record
922 * (if any) stops the scan.
925 hammer_bulk_scan_callback(hammer_record_t record, void *data)
927 struct hammer_bulk_info *info = data;
929 if (record->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
930 HAMMER_RECF_COMMITTED)) {
933 hammer_ref(&record->lock);
934 info->conflict = record;
935 return(-1); /* stop scan */
939 * Reserve blockmap space placemarked with an in-memory record.
941 * This routine is called by the frontend in order to be able to directly
942 * flush a buffer cache buffer. The frontend has locked the related buffer
943 * cache buffers and we should be able to manipulate any overlapping
946 * The caller is responsible for adding the returned record and deleting
947 * the returned conflicting record (if any), typically by calling
948 * hammer_ip_replace_bulk() (via hammer_io_direct_write()).
951 hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
954 hammer_record_t record;
955 hammer_dedup_cache_t dcp;
960 * Create a record to cover the direct write. The record cannot
961 * be added to the in-memory RB tree here as it might conflict
962 * with an existing memory record. See hammer_io_direct_write().
964 * The backend is responsible for finalizing the space reserved in
967 * XXX bytes not aligned, depend on the reservation code to
968 * align the reservation.
970 record = hammer_alloc_mem_record(ip, 0);
971 zone = hammer_data_zone_index(bytes);
975 crc = crc32(data, bytes);
977 if (hammer_live_dedup == 0)
979 if ((dcp = hammer_dedup_cache_lookup(ip->hmp, crc)) != NULL) {
980 struct hammer_dedup_cache tmp = *dcp;
982 record->resv = hammer_blockmap_reserve_dedup(ip->hmp, zone,
983 bytes, tmp.data_offset, errorp);
984 if (record->resv == NULL)
987 if (!hammer_dedup_validate(&tmp, zone, bytes, data)) {
988 hammer_blockmap_reserve_complete(ip->hmp, record->resv);
992 record->leaf.data_offset = tmp.data_offset;
993 record->flags |= HAMMER_RECF_DEDUPED;
996 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
997 &record->leaf.data_offset, errorp);
998 if (record->resv == NULL) {
999 hdkprintf("reservation failed\n");
1000 hammer_rel_mem_record(record);
1005 record->type = HAMMER_MEM_RECORD_DATA;
1006 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
1007 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
1008 record->leaf.base.obj_id = ip->obj_id;
1009 record->leaf.base.key = file_offset + bytes;
1010 record->leaf.base.localization = ip->obj_localization |
1011 HAMMER_LOCALIZE_MISC;
1012 record->leaf.data_len = bytes;
1013 record->leaf.data_crc = crc;
1014 KKASSERT(*errorp == 0);
1020 * Called by hammer_io_direct_write() prior to any possible completion
1021 * of the BIO to emplace the memory record associated with the I/O and
1022 * to replace any prior memory record which might still be active.
1024 * Setting the FE deleted flag on the old record (if any) avoids any RB
1025 * tree insertion conflict, amoung other things.
1027 * This has to be done prior to the caller completing any related buffer
1028 * cache I/O or a reinstantiation of the buffer may load data from the
1029 * old media location instead of the new media location. The holding
1030 * of the locked buffer cache buffer serves to interlock the record
1031 * replacement operation.
1034 hammer_ip_replace_bulk(hammer_mount_t hmp, hammer_record_t record)
1036 hammer_record_t conflict;
1037 int error __debugvar;
1039 while ((conflict = hammer_ip_get_bulk(record)) != NULL) {
1040 if ((conflict->flags & HAMMER_RECF_INTERLOCK_BE) == 0) {
1041 conflict->flags |= HAMMER_RECF_DELETED_FE;
1044 conflict->flags |= HAMMER_RECF_WANTED;
1045 tsleep(conflict, 0, "hmrrc3", 0);
1046 hammer_rel_mem_record(conflict);
1048 error = hammer_mem_add(record);
1050 hammer_rel_mem_record(conflict);
1051 KKASSERT(error == 0);
1055 * Frontend truncation code. Scan in-memory records only. On-disk records
1056 * and records in a flushing state are handled by the backend. The vnops
1057 * setattr code will handle the block containing the truncation point.
1059 * Partial blocks are not deleted.
1061 * This code is only called on regular files.
1064 hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
1066 struct rec_trunc_info info;
1068 switch(ip->ino_data.obj_type) {
1069 case HAMMER_OBJTYPE_REGFILE:
1070 info.rec_type = HAMMER_RECTYPE_DATA;
1072 case HAMMER_OBJTYPE_DBFILE:
1073 info.rec_type = HAMMER_RECTYPE_DB;
1078 info.trunc_off = file_size;
1079 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
1080 hammer_frontend_trunc_callback, &info);
1085 * Scan callback for frontend records to destroy during a truncation.
1086 * We must ensure that DELETED_FE is set on the record or the frontend
1087 * will get confused in future read() calls.
1089 * NOTE: DELETED_FE cannot be set while the record interlock (BE) is held.
1090 * In this rare case we must wait for the interlock to be cleared.
1092 * NOTE: This function is only called on regular files. There are further
1093 * restrictions to the setting of DELETED_FE on directory records
1094 * undergoing a flush due to sensitive inode link count calculations.
1097 hammer_frontend_trunc_callback(hammer_record_t record, void *data __unused)
1099 if (record->flags & HAMMER_RECF_DELETED_FE)
1102 if (record->flush_state == HAMMER_FST_FLUSH)
1105 hammer_ref(&record->lock);
1106 while (record->flags & HAMMER_RECF_INTERLOCK_BE)
1107 hammer_wait_mem_record_ident(record, "hmmtrr");
1108 record->flags |= HAMMER_RECF_DELETED_FE;
1109 hammer_rel_mem_record(record);
1114 * Return 1 if the caller must check for and delete existing records
1115 * before writing out a new data record.
1117 * Return 0 if the caller can just insert the record into the B-Tree without
1121 hammer_record_needs_overwrite_delete(hammer_record_t record)
1123 hammer_inode_t ip = record->ip;
1124 int64_t file_offset;
1127 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
1128 file_offset = record->leaf.base.key;
1130 file_offset = record->leaf.base.key - record->leaf.data_len;
1131 r = (file_offset < ip->save_trunc_off);
1132 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1133 if (ip->save_trunc_off <= record->leaf.base.key)
1134 ip->save_trunc_off = record->leaf.base.key + 1;
1136 if (ip->save_trunc_off < record->leaf.base.key)
1137 ip->save_trunc_off = record->leaf.base.key;
1143 * Backend code. Sync a record to the media.
1146 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
1148 hammer_transaction_t trans = cursor->trans;
1149 int64_t file_offset;
1155 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1156 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
1157 KKASSERT(record->leaf.base.localization != 0);
1160 * Any direct-write related to the record must complete before we
1161 * can sync the record to the on-disk media.
1163 if (record->gflags & (HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL))
1164 hammer_io_direct_wait(record);
1167 * If this is a bulk-data record placemarker there may be an existing
1168 * record on-disk, indicating a data overwrite. If there is the
1169 * on-disk record must be deleted before we can insert our new record.
1171 * We've synthesized this record and do not know what the create_tid
1172 * on-disk is, nor how much data it represents.
1174 * Keep in mind that (key) for data records is (base_offset + len),
1175 * not (base_offset). Also, we only want to get rid of on-disk
1176 * records since we are trying to sync our in-memory record, call
1177 * hammer_ip_delete_range() with truncating set to 1 to make sure
1178 * it skips in-memory records.
1180 * It is ok for the lookup to return ENOENT.
1182 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1183 * to call hammer_ip_delete_range() or not. This also means we must
1184 * update sync_trunc_off() as we write.
1186 if (record->type == HAMMER_MEM_RECORD_DATA &&
1187 hammer_record_needs_overwrite_delete(record)) {
1188 file_offset = record->leaf.base.key - record->leaf.data_len;
1189 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1191 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
1192 error = hammer_ip_delete_range(
1194 file_offset, file_offset + bytes - 1,
1196 if (error && error != ENOENT)
1201 * If this is a general record there may be an on-disk version
1202 * that must be deleted before we can insert the new record.
1204 if (record->type == HAMMER_MEM_RECORD_GENERAL) {
1205 error = hammer_delete_general(cursor, record->ip, &record->leaf);
1206 if (error && error != ENOENT)
1213 hammer_normalize_cursor(cursor);
1214 cursor->key_beg = record->leaf.base;
1215 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1216 cursor->flags |= HAMMER_CURSOR_BACKEND;
1217 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1220 * Records can wind up on-media before the inode itself is on-media.
1223 record->ip->flags |= HAMMER_INODE_DONDISK;
1226 * If we are deleting a directory entry an exact match must be
1229 if (record->type == HAMMER_MEM_RECORD_DEL) {
1230 error = hammer_btree_lookup(cursor);
1232 KKASSERT(cursor->iprec == NULL);
1233 error = hammer_ip_delete_record(cursor, record->ip,
1236 record->flags |= HAMMER_RECF_DELETED_BE |
1237 HAMMER_RECF_COMMITTED;
1238 ++record->ip->rec_generation;
1247 * Issue a lookup to position the cursor and locate the insertion
1248 * point. The target key should not exist. If we are creating a
1249 * directory entry we may have to iterate the low 32 bits of the
1250 * key to find an unused key.
1252 hammer_sync_lock_sh(trans);
1253 cursor->flags |= HAMMER_CURSOR_INSERT;
1254 error = hammer_btree_lookup(cursor);
1255 if (hammer_debug_inode)
1256 hdkprintf("DOINSERT LOOKUP %d\n", error);
1258 hdkprintf("duplicate rec at (%016jx)\n",
1259 (intmax_t)record->leaf.base.key);
1260 if (hammer_debug_critical)
1261 Debugger("duplicate record1");
1265 if (error != ENOENT)
1269 * Allocate the record and data. The result buffers will be
1270 * marked as being modified and further calls to
1271 * hammer_modify_buffer() will result in unneeded UNDO records.
1273 * Support zero-fill records (data == NULL and data_len != 0)
1275 if (record->type == HAMMER_MEM_RECORD_DATA) {
1277 * The data portion of a bulk-data record has already been
1278 * committed to disk, we need only adjust the layer2
1279 * statistics in the same transaction as our B-Tree insert.
1281 KKASSERT(record->leaf.data_offset != 0);
1282 error = hammer_blockmap_finalize(trans,
1284 record->leaf.data_offset,
1285 record->leaf.data_len);
1287 if (hammer_live_dedup == 2 &&
1288 (record->flags & HAMMER_RECF_DEDUPED) == 0) {
1289 hammer_dedup_cache_add(record->ip, &record->leaf);
1291 } else if (record->data && record->leaf.data_len) {
1293 * Wholely cached record, with data. Allocate the data.
1295 bdata = hammer_alloc_data(trans, record->leaf.data_len,
1296 record->leaf.base.rec_type,
1297 &record->leaf.data_offset,
1298 &cursor->data_buffer,
1302 hammer_crc_set_leaf(record->data, &record->leaf);
1303 hammer_modify_buffer_noundo(trans, cursor->data_buffer);
1304 bcopy(record->data, bdata, record->leaf.data_len);
1305 hammer_modify_buffer_done(cursor->data_buffer);
1308 * Wholely cached record, without data.
1310 record->leaf.data_offset = 0;
1311 record->leaf.data_crc = 0;
1314 error = hammer_btree_insert(cursor, &record->leaf, &doprop);
1315 if (hammer_debug_inode && error) {
1316 hdkprintf("BTREE INSERT error %d @ %016jx:%d key %016jx\n",
1318 (intmax_t)cursor->node->node_offset,
1320 (intmax_t)record->leaf.base.key);
1324 * Our record is on-disk and we normally mark the in-memory version
1325 * as having been committed (and not BE-deleted).
1327 * If the record represented a directory deletion but we had to
1328 * sync a valid directory entry to disk due to dependancies,
1329 * we must convert the record to a covering delete so the
1330 * frontend does not have visibility on the synced entry.
1332 * WARNING: cursor's leaf pointer may have changed after do_propagation
1337 hammer_btree_do_propagation(cursor,
1341 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1343 * Must convert deleted directory entry add
1344 * to a directory entry delete.
1346 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1347 record->flags &= ~HAMMER_RECF_DELETED_FE;
1348 record->type = HAMMER_MEM_RECORD_DEL;
1349 KKASSERT(record->ip->obj_id == record->leaf.base.obj_id);
1350 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1351 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1352 KKASSERT((record->flags & (HAMMER_RECF_COMMITTED |
1353 HAMMER_RECF_DELETED_BE)) == 0);
1354 /* converted record is not yet committed */
1355 /* hammer_flush_record_done takes care of the rest */
1358 * Everything went fine and we are now done with
1361 record->flags |= HAMMER_RECF_COMMITTED;
1362 ++record->ip->rec_generation;
1365 if (record->leaf.data_offset) {
1366 hammer_blockmap_free(trans, record->leaf.data_offset,
1367 record->leaf.data_len);
1371 hammer_sync_unlock(trans);
1377 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1378 * entry's key is used to deal with hash collisions in the upper 32 bits.
1379 * A unique 64 bit key is generated in-memory and may be regenerated a
1380 * second time when the directory record is flushed to the on-disk B-Tree.
1382 * A referenced record is passed to this function. This function
1383 * eats the reference. If an error occurs the record will be deleted.
1385 * A copy of the temporary record->data pointer provided by the caller
1389 hammer_mem_add(hammer_record_t record)
1391 hammer_mount_t hmp = record->ip->hmp;
1394 * Make a private copy of record->data
1397 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
1400 * Insert into the RB tree. A unique key should have already
1401 * been selected if this is a directory entry.
1403 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1404 record->flags |= HAMMER_RECF_DELETED_FE;
1405 hammer_rel_mem_record(record);
1409 ++record->ip->rsv_recs;
1410 record->ip->hmp->rsv_databytes += record->leaf.data_len;
1411 record->flags |= HAMMER_RECF_ONRBTREE;
1412 hammer_modify_inode(NULL, record->ip, HAMMER_INODE_XDIRTY);
1413 hammer_rel_mem_record(record);
1417 /************************************************************************
1418 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1419 ************************************************************************
1421 * These functions augment the B-Tree scanning functions in hammer_btree.c
1422 * by merging in-memory records with on-disk records.
1426 * Locate a particular record either in-memory or on-disk.
1428 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1429 * NOT be called to iterate results.
1432 hammer_ip_lookup(hammer_cursor_t cursor)
1437 * If the element is in-memory return it without searching the
1440 KKASSERT(cursor->ip);
1441 error = hammer_mem_lookup(cursor);
1443 cursor->leaf = &cursor->iprec->leaf;
1446 if (error != ENOENT)
1450 * If the inode has on-disk components search the on-disk B-Tree.
1452 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1454 error = hammer_btree_lookup(cursor);
1456 error = hammer_btree_extract_leaf(cursor);
1461 * Helper for hammer_ip_first()/hammer_ip_next()
1463 * NOTE: Both ATEDISK and DISKEOF will be set the same. This sets up
1464 * hammer_ip_first() for calling hammer_ip_next(), and sets up the re-seek
1465 * state if hammer_ip_next() needs to re-seek.
1469 _hammer_ip_seek_btree(hammer_cursor_t cursor)
1471 hammer_inode_t ip = cursor->ip;
1474 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1475 error = hammer_btree_lookup(cursor);
1476 if (error == ENOENT || error == EDEADLK) {
1477 if (hammer_debug_general & 0x2000) {
1478 hdkprintf("error %d node %p %016jx index %d\n",
1479 error, cursor->node,
1480 (intmax_t)cursor->node->node_offset,
1483 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1484 error = hammer_btree_iterate(cursor);
1487 cursor->flags &= ~(HAMMER_CURSOR_DISKEOF |
1488 HAMMER_CURSOR_ATEDISK);
1490 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1491 HAMMER_CURSOR_ATEDISK;
1492 if (error == ENOENT)
1496 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_ATEDISK;
1503 * Helper for hammer_ip_next()
1505 * The caller has determined that the media cursor is further along than the
1506 * memory cursor and must be reseeked after a generation number change.
1510 _hammer_ip_reseek(hammer_cursor_t cursor)
1512 struct hammer_base_elm save;
1513 hammer_btree_elm_t elm;
1514 int error __debugvar;
1521 hkprintf("Debug: re-seeked during scan @ino=%016jx\n",
1522 (intmax_t)cursor->ip->obj_id);
1523 save = cursor->key_beg;
1524 cursor->key_beg = cursor->iprec->leaf.base;
1525 error = _hammer_ip_seek_btree(cursor);
1526 KKASSERT(error == 0);
1527 cursor->key_beg = save;
1530 * If the memory record was previous returned to
1531 * the caller and the media record matches
1532 * (-1/+1: only create_tid differs), then iterate
1533 * the media record to avoid a double result.
1535 if ((cursor->flags & HAMMER_CURSOR_ATEDISK) == 0 &&
1536 (cursor->flags & HAMMER_CURSOR_LASTWASMEM)) {
1537 elm = &cursor->node->ondisk->elms[cursor->index];
1538 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1539 if (cursor->flags & HAMMER_CURSOR_ASOF) {
1540 if (r >= -1 && r <= 1) {
1541 hkprintf("Debug: iterated after "
1542 "re-seek (asof r=%d)\n", r);
1543 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1548 hkprintf("Debug: iterated after "
1550 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1559 * Locate the first record within the cursor's key_beg/key_end range,
1560 * restricted to a particular inode. 0 is returned on success, ENOENT
1561 * if no records matched the requested range, or some other error.
1563 * When 0 is returned hammer_ip_next() may be used to iterate additional
1564 * records within the requested range.
1566 * This function can return EDEADLK, requiring the caller to terminate
1567 * the cursor and try again.
1571 hammer_ip_first(hammer_cursor_t cursor)
1573 hammer_inode_t ip __debugvar = cursor->ip;
1576 KKASSERT(ip != NULL);
1579 * Clean up fields and setup for merged scan
1581 cursor->flags &= ~HAMMER_CURSOR_RETEST;
1584 * Search the in-memory record list (Red-Black tree). Unlike the
1585 * B-Tree search, mem_first checks for records in the range.
1587 * This function will setup both ATEMEM and MEMEOF properly for
1588 * the ip iteration. ATEMEM will be set if MEMEOF is set.
1590 hammer_mem_first(cursor);
1593 * Detect generation changes during blockages, including
1594 * blockages which occur on the initial btree search.
1596 cursor->rec_generation = cursor->ip->rec_generation;
1599 * Initial search and result
1601 error = _hammer_ip_seek_btree(cursor);
1603 error = hammer_ip_next(cursor);
1609 * Retrieve the next record in a merged iteration within the bounds of the
1610 * cursor. This call may be made multiple times after the cursor has been
1611 * initially searched with hammer_ip_first().
1613 * There are numerous special cases in this code to deal with races between
1614 * in-memory records and on-media records.
1616 * 0 is returned on success, ENOENT if no further records match the
1617 * requested range, or some other error code is returned.
1620 hammer_ip_next(hammer_cursor_t cursor)
1622 hammer_btree_elm_t elm;
1623 hammer_record_t rec;
1624 hammer_record_t tmprec;
1630 * Get the next on-disk record
1632 * NOTE: If we deleted the last on-disk record we had scanned
1633 * ATEDISK will be clear and RETEST will be set, forcing
1634 * a call to iterate. The fact that ATEDISK is clear causes
1635 * iterate to re-test the 'current' element. If ATEDISK is
1636 * set, iterate will skip the 'current' element.
1639 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1640 if (cursor->flags & (HAMMER_CURSOR_ATEDISK |
1641 HAMMER_CURSOR_RETEST)) {
1642 error = hammer_btree_iterate(cursor);
1643 cursor->flags &= ~HAMMER_CURSOR_RETEST;
1645 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1646 hammer_cache_node(&cursor->ip->cache[1],
1648 } else if (error == ENOENT) {
1649 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1650 HAMMER_CURSOR_ATEDISK;
1657 * If the generation changed the backend has deleted or committed
1658 * one or more memory records since our last check.
1660 * When this case occurs if the disk cursor is > current memory record
1661 * or the disk cursor is at EOF, we must re-seek the disk-cursor.
1662 * Since the cursor is ahead it must have not yet been eaten (if
1663 * not at eof anyway). (XXX data offset case?)
1665 * NOTE: we are not doing a full check here. That will be handled
1668 * If we have exhausted all memory records we do not have to do any
1671 while (cursor->rec_generation != cursor->ip->rec_generation &&
1673 hkprintf("Debug: generation changed during scan @ino=%016jx\n",
1674 (intmax_t)cursor->ip->obj_id);
1675 cursor->rec_generation = cursor->ip->rec_generation;
1676 if (cursor->flags & HAMMER_CURSOR_MEMEOF)
1678 if (cursor->flags & HAMMER_CURSOR_DISKEOF) {
1681 KKASSERT((cursor->flags & HAMMER_CURSOR_ATEDISK) == 0);
1682 elm = &cursor->node->ondisk->elms[cursor->index];
1683 r = hammer_btree_cmp(&elm->base,
1684 &cursor->iprec->leaf.base);
1688 * Do we re-seek the media cursor?
1691 if (_hammer_ip_reseek(cursor))
1697 * We can now safely get the next in-memory record. We cannot
1700 * hammer_rec_scan_cmp: Is the record still in our general range,
1701 * (non-inclusive of snapshot exclusions)?
1702 * hammer_rec_scan_callback: Is the record in our snapshot?
1705 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1707 * If the current memory record was eaten then get the next
1708 * one. Stale records are skipped.
1710 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1711 tmprec = cursor->iprec;
1712 cursor->iprec = NULL;
1713 rec = hammer_rec_rb_tree_RB_NEXT(tmprec);
1715 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1717 if (hammer_rec_scan_callback(rec, cursor) != 0)
1719 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1721 if (cursor->iprec) {
1722 KKASSERT(cursor->iprec == rec);
1723 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1725 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1727 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1732 * MEMORY RECORD VALIDITY TEST
1734 * (We still can't block, which is why tmprec is being held so
1737 * If the memory record is no longer valid we skip it. It may
1738 * have been deleted by the frontend. If it was deleted or
1739 * committed by the backend the generation change re-seeked the
1740 * disk cursor and the record will be present there.
1742 if (error == 0 && (cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1743 KKASSERT(cursor->iprec);
1744 KKASSERT((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0);
1745 if (!hammer_ip_iterate_mem_good(cursor, cursor->iprec)) {
1746 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1748 hammer_rel_mem_record(tmprec);
1753 hammer_rel_mem_record(tmprec);
1756 * Extract either the disk or memory record depending on their
1757 * relative position.
1760 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1763 * Both entries valid. Compare the entries and nominally
1764 * return the first one in the sort order. Numerous cases
1765 * require special attention, however.
1767 elm = &cursor->node->ondisk->elms[cursor->index];
1768 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1771 * If the two entries differ only by their key (-2/2) or
1772 * create_tid (-1/1), and are DATA records, we may have a
1773 * nominal match. We have to calculate the base file
1774 * offset of the data.
1776 if (r <= 2 && r >= -2 && r != 0 &&
1777 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1778 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1779 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1780 int64_t base2 = cursor->iprec->leaf.base.key -
1781 cursor->iprec->leaf.data_len;
1787 error = hammer_btree_extract_leaf(cursor);
1788 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1789 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1794 * If the entries match exactly the memory entry is either
1795 * an on-disk directory entry deletion or a bulk data
1796 * overwrite. If it is a directory entry deletion we eat
1799 * For the bulk-data overwrite case it is possible to have
1800 * visibility into both, which simply means the syncer
1801 * hasn't gotten around to doing the delete+insert sequence
1802 * on the B-Tree. Use the memory entry and throw away the
1805 * If the in-memory record is not either of these we
1806 * probably caught the syncer while it was syncing it to
1807 * the media. Since we hold a shared lock on the cursor,
1808 * the in-memory record had better be marked deleted at
1812 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1813 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1814 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1815 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1818 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1819 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1820 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1822 /* fall through to memory entry */
1824 hpanic("duplicate mem/B-Tree entry %p %d %08x",
1826 cursor->iprec->type,
1827 cursor->iprec->flags);
1828 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1832 /* fall through to the memory entry */
1833 case HAMMER_CURSOR_ATEDISK:
1835 * Only the memory entry is valid.
1837 cursor->leaf = &cursor->iprec->leaf;
1838 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1839 cursor->flags |= HAMMER_CURSOR_LASTWASMEM;
1842 * If the memory entry is an on-disk deletion we should have
1843 * also had found a B-Tree record. If the backend beat us
1844 * to it it would have interlocked the cursor and we should
1845 * have seen the in-memory record marked DELETED_FE.
1847 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1848 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1849 hpanic("del-on-disk with no B-Tree entry iprec %p flags %08x",
1851 cursor->iprec->flags);
1854 case HAMMER_CURSOR_ATEMEM:
1856 * Only the disk entry is valid
1858 error = hammer_btree_extract_leaf(cursor);
1859 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1860 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1864 * Neither entry is valid
1866 * XXX error not set properly
1868 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1869 cursor->leaf = NULL;
1877 * Resolve the cursor->data pointer for the current cursor position in
1878 * a merged iteration.
1881 hammer_ip_resolve_data(hammer_cursor_t cursor)
1883 hammer_record_t record;
1886 if (hammer_cursor_inmem(cursor)) {
1888 * The data associated with an in-memory record is usually
1889 * kmalloced, but reserve-ahead data records will have an
1890 * on-disk reference.
1892 * NOTE: Reserve-ahead data records must be handled in the
1893 * context of the related high level buffer cache buffer
1894 * to interlock against async writes.
1896 * NOTE: We might catch a direct write in-progress, in which
1897 * case we must wait for it to complete. The wait
1898 * function will also clean out any buffer aliases.
1900 * (In fact, it is possible that the write had not
1901 * even started yet).
1903 record = cursor->iprec;
1904 cursor->data = record->data;
1906 if (cursor->data == NULL) {
1907 hammer_io_direct_wait(record);
1908 KKASSERT(record->leaf.base.rec_type ==
1909 HAMMER_RECTYPE_DATA);
1910 cursor->data = hammer_bread_ext(cursor->trans->hmp,
1911 record->leaf.data_offset,
1912 record->leaf.data_len,
1914 &cursor->data_buffer);
1918 * Loading leaf here isn't necessary if it's guaranteed that
1919 * the cursor is at a leaf node (which basically should be)
1920 * because hammer_btree_extract_data() does that.
1922 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
1923 error = hammer_btree_extract_data(cursor);
1929 * Backend truncation / record replacement - delete records in range.
1931 * Delete all records within the specified range for inode ip. In-memory
1932 * records still associated with the frontend are ignored.
1934 * If truncating is non-zero in-memory records associated with the back-end
1935 * are ignored. If truncating is > 1 we can return EWOULDBLOCK.
1939 * * An unaligned range will cause new records to be added to cover
1940 * the edge cases. (XXX not implemented yet).
1942 * * Replacement via reservations (see hammer_ip_sync_record_cursor())
1943 * also do not deal with unaligned ranges.
1945 * * ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1947 * * Record keys for regular file data have to be special-cased since
1948 * they indicate the end of the range (key = base + bytes).
1950 * * This function may be asked to delete ridiculously huge ranges, for
1951 * example if someone truncates or removes a 1TB regular file. We
1952 * must be very careful on restarts and we may have to stop w/
1953 * EWOULDBLOCK to avoid blowing out the buffer cache.
1956 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1957 int64_t ran_beg, int64_t ran_end, int truncating)
1959 hammer_transaction_t trans = cursor->trans;
1960 hammer_btree_leaf_elm_t leaf;
1965 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1967 hammer_normalize_cursor(cursor);
1968 cursor->key_beg.localization = ip->obj_localization |
1969 HAMMER_LOCALIZE_MISC;
1970 cursor->key_beg.obj_id = ip->obj_id;
1971 cursor->key_beg.create_tid = 0;
1972 cursor->key_beg.delete_tid = 0;
1973 cursor->key_beg.obj_type = 0;
1975 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1976 cursor->key_beg.key = ran_beg;
1977 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1980 * The key in the B-Tree is (base+bytes), so the first possible
1981 * matching key is ran_beg + 1.
1983 cursor->key_beg.key = ran_beg + 1;
1984 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1987 cursor->key_end = cursor->key_beg;
1988 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1989 cursor->key_end.key = ran_end;
1991 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1992 if (tmp64 < ran_end)
1993 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1995 cursor->key_end.key = ran_end + MAXPHYS + 1;
1998 cursor->asof = ip->obj_asof;
1999 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2000 cursor->flags |= HAMMER_CURSOR_ASOF;
2001 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
2002 cursor->flags |= HAMMER_CURSOR_BACKEND;
2003 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
2005 error = hammer_ip_first(cursor);
2008 * Iterate through matching records and mark them as deleted.
2010 while (error == 0) {
2011 leaf = cursor->leaf;
2013 KKASSERT(leaf->base.delete_tid == 0);
2014 KKASSERT(leaf->base.obj_id == ip->obj_id);
2017 * There may be overlap cases for regular file data. Also
2018 * remember the key for a regular file record is (base + len),
2021 * Note that due to duplicates (mem & media) allowed by
2022 * DELETE_VISIBILITY, off can wind up less then ran_beg.
2024 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
2025 off = leaf->base.key - leaf->data_len;
2027 * Check the left edge case. We currently do not
2028 * split existing records.
2030 if (off < ran_beg && leaf->base.key > ran_beg) {
2031 hpanic("hammer left edge case %016jx %d",
2032 (intmax_t)leaf->base.key,
2037 * Check the right edge case. Note that the
2038 * record can be completely out of bounds, which
2039 * terminates the search.
2041 * base->key is exclusive of the right edge while
2042 * ran_end is inclusive of the right edge. The
2043 * (key - data_len) left boundary is inclusive.
2045 * XXX theory-check this test at some point, are
2046 * we missing a + 1 somewhere? Note that ran_end
2049 if (leaf->base.key - 1 > ran_end) {
2050 if (leaf->base.key - leaf->data_len > ran_end)
2052 hpanic("hammer right edge case");
2055 off = leaf->base.key;
2059 * Delete the record. When truncating we do not delete
2060 * in-memory (data) records because they represent data
2061 * written after the truncation.
2063 * This will also physically destroy the B-Tree entry and
2064 * data if the retention policy dictates. The function
2065 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
2066 * to retest the new 'current' element.
2068 if (truncating == 0 || hammer_cursor_ondisk(cursor)) {
2069 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2071 * If we have built up too many meta-buffers we risk
2072 * deadlocking the kernel and must stop. This can
2073 * occur when deleting ridiculously huge files.
2074 * sync_trunc_off is updated so the next cycle does
2075 * not re-iterate records we have already deleted.
2077 * This is only done with formal truncations.
2079 if (truncating > 1 && error == 0 &&
2080 hammer_flusher_meta_limit(ip->hmp)) {
2081 ip->sync_trunc_off = off;
2082 error = EWOULDBLOCK;
2087 ran_beg = off; /* for restart */
2088 error = hammer_ip_next(cursor);
2091 hammer_cache_node(&ip->cache[1], cursor->node);
2093 if (error == EDEADLK) {
2094 hammer_done_cursor(cursor);
2095 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2099 if (error == ENOENT)
2105 * This backend function deletes the specified record on-disk, similar to
2106 * delete_range but for a specific record. Unlike the exact deletions
2107 * used when deleting a directory entry this function uses an ASOF search
2108 * like delete_range.
2110 * This function may be called with ip->obj_asof set for a slave snapshot,
2111 * so don't use it. We always delete non-historical records only.
2114 hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
2115 hammer_btree_leaf_elm_t leaf)
2117 hammer_transaction_t trans = cursor->trans;
2120 KKASSERT(trans->type == HAMMER_TRANS_FLS);
2122 hammer_normalize_cursor(cursor);
2123 cursor->key_beg = leaf->base;
2124 cursor->asof = HAMMER_MAX_TID;
2125 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2126 cursor->flags |= HAMMER_CURSOR_ASOF;
2127 cursor->flags |= HAMMER_CURSOR_BACKEND;
2128 cursor->flags &= ~HAMMER_CURSOR_INSERT;
2130 error = hammer_btree_lookup(cursor);
2132 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2134 if (error == EDEADLK) {
2135 hammer_done_cursor(cursor);
2136 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2144 * This function deletes remaining auxillary records when an inode is
2145 * being deleted. This function explicitly does not delete the
2146 * inode record, directory entry, data, or db records. Those must be
2147 * properly disposed of prior to this call.
2150 hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip, int *countp)
2152 hammer_transaction_t trans = cursor->trans;
2153 hammer_btree_leaf_elm_t leaf __debugvar;
2156 KKASSERT(trans->type == HAMMER_TRANS_FLS);
2158 hammer_normalize_cursor(cursor);
2159 cursor->key_beg.localization = ip->obj_localization |
2160 HAMMER_LOCALIZE_MISC;
2161 cursor->key_beg.obj_id = ip->obj_id;
2162 cursor->key_beg.create_tid = 0;
2163 cursor->key_beg.delete_tid = 0;
2164 cursor->key_beg.obj_type = 0;
2165 cursor->key_beg.rec_type = HAMMER_RECTYPE_CLEAN_START;
2166 cursor->key_beg.key = HAMMER_MIN_KEY;
2168 cursor->key_end = cursor->key_beg;
2169 cursor->key_end.rec_type = HAMMER_RECTYPE_MAX;
2170 cursor->key_end.key = HAMMER_MAX_KEY;
2172 cursor->asof = ip->obj_asof;
2173 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2174 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2175 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
2176 cursor->flags |= HAMMER_CURSOR_BACKEND;
2178 error = hammer_ip_first(cursor);
2181 * Iterate through matching records and mark them as deleted.
2183 while (error == 0) {
2184 leaf = cursor->leaf;
2186 KKASSERT(leaf->base.delete_tid == 0);
2189 * Mark the record and B-Tree entry as deleted. This will
2190 * also physically delete the B-Tree entry, record, and
2191 * data if the retention policy dictates. The function
2192 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
2193 * to retest the new 'current' element.
2195 * Directory entries (and delete-on-disk directory entries)
2196 * must be synced and cannot be deleted.
2198 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2202 error = hammer_ip_next(cursor);
2205 hammer_cache_node(&ip->cache[1], cursor->node);
2206 if (error == EDEADLK) {
2207 hammer_done_cursor(cursor);
2208 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2212 if (error == ENOENT)
2218 * Delete the record at the current cursor. On success the cursor will
2219 * be positioned appropriately for an iteration but may no longer be at
2222 * This routine is only called from the backend.
2224 * NOTE: This can return EDEADLK, requiring the caller to terminate the
2228 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
2231 hammer_record_t iprec;
2234 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
2238 * In-memory (unsynchronized) records can simply be freed. This
2239 * only occurs in range iterations since all other records are
2240 * individually synchronized. Thus there should be no confusion with
2243 * An in-memory record may be deleted before being committed to disk,
2244 * but could have been accessed in the mean time. The reservation
2245 * code will deal with the case.
2247 if (hammer_cursor_inmem(cursor)) {
2248 iprec = cursor->iprec;
2249 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
2250 iprec->flags |= HAMMER_RECF_DELETED_FE;
2251 iprec->flags |= HAMMER_RECF_DELETED_BE;
2252 KKASSERT(iprec->ip == ip);
2253 ++ip->rec_generation;
2258 * On-disk records are marked as deleted by updating their delete_tid.
2259 * This does not effect their position in the B-Tree (which is based
2260 * on their create_tid).
2262 * Frontend B-Tree operations track inodes so we tell
2263 * hammer_delete_at_cursor() not to.
2265 error = hammer_btree_extract_leaf(cursor);
2267 error = hammer_delete_at_cursor(
2269 HAMMER_DELETE_ADJUST | hammer_nohistory(ip),
2271 cursor->trans->time32,
2278 * Used to write a generic record w/optional data to the media b-tree
2279 * when no inode context is available. Used by the mirroring and
2282 * Caller must set cursor->key_beg to leaf->base. The cursor must be
2283 * flagged for backend operation and not flagged ASOF (since we are
2284 * doing an insertion).
2286 * This function will acquire the appropriate sync lock and will set
2287 * the cursor insertion flag for the operation, do the btree lookup,
2288 * and the insertion, and clear the insertion flag and sync lock before
2289 * returning. The cursor state will be such that the caller can continue
2290 * scanning (used by the mirroring code).
2292 * mode: HAMMER_CREATE_MODE_UMIRROR copyin data, check crc
2293 * HAMMER_CREATE_MODE_SYS bcopy data, generate crc
2295 * NOTE: EDEADLK can be returned. The caller must do deadlock handling and
2298 * EALREADY can be returned if the record already exists (WARNING,
2299 * because ASOF cannot be used no check is made for illegal
2302 * NOTE: Do not use the function for normal inode-related records as this
2303 * functions goes directly to the media and is not integrated with
2304 * in-memory records.
2307 hammer_create_at_cursor(hammer_cursor_t cursor, hammer_btree_leaf_elm_t leaf,
2308 void *udata, int mode)
2310 hammer_transaction_t trans;
2311 hammer_buffer_t data_buffer;
2312 hammer_off_t ndata_offset;
2313 hammer_tid_t high_tid;
2318 trans = cursor->trans;
2323 KKASSERT((cursor->flags &
2324 (HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF)) ==
2325 (HAMMER_CURSOR_BACKEND));
2327 hammer_sync_lock_sh(trans);
2329 if (leaf->data_len) {
2330 ndata = hammer_alloc_data(trans, leaf->data_len,
2331 leaf->base.rec_type,
2332 &ndata_offset, &data_buffer,
2334 if (ndata == NULL) {
2335 hammer_sync_unlock(trans);
2338 leaf->data_offset = ndata_offset;
2339 hammer_modify_buffer_noundo(trans, data_buffer);
2342 case HAMMER_CREATE_MODE_UMIRROR:
2343 error = copyin(udata, ndata, leaf->data_len);
2345 if (hammer_crc_test_leaf(ndata, leaf) == 0) {
2346 hdkprintf("CRC DATA @ %016jx/%d MISMATCH ON PIPE\n",
2347 (intmax_t)ndata_offset,
2351 error = hammer_cursor_localize_data(
2356 case HAMMER_CREATE_MODE_SYS:
2357 bcopy(udata, ndata, leaf->data_len);
2359 hammer_crc_set_leaf(ndata, leaf);
2362 hpanic("bad mode %d", mode);
2363 break; /* NOT REACHED */
2365 hammer_modify_buffer_done(data_buffer);
2367 leaf->data_offset = 0;
2375 * Do the insertion. This can fail with a EDEADLK or EALREADY
2377 cursor->flags |= HAMMER_CURSOR_INSERT;
2378 error = hammer_btree_lookup(cursor);
2379 if (error != ENOENT) {
2384 error = hammer_btree_insert(cursor, leaf, &doprop);
2387 * Cursor is left on current element, we want to skip it now.
2388 * (in case the caller is scanning)
2390 cursor->flags |= HAMMER_CURSOR_ATEDISK;
2391 cursor->flags &= ~HAMMER_CURSOR_INSERT;
2394 * If the insertion happens to be creating (and not just replacing)
2395 * an inode we have to track it.
2398 leaf->base.rec_type == HAMMER_RECTYPE_INODE &&
2399 leaf->base.delete_tid == 0) {
2400 hammer_modify_volume_field(trans, trans->rootvol,
2402 ++trans->hmp->rootvol->ondisk->vol0_stat_inodes;
2403 hammer_modify_volume_done(trans->rootvol);
2407 * vol0_next_tid must track the highest TID stored in the filesystem.
2408 * We do not need to generate undo for this update.
2410 high_tid = leaf->base.create_tid;
2411 if (high_tid < leaf->base.delete_tid)
2412 high_tid = leaf->base.delete_tid;
2413 if (trans->rootvol->ondisk->vol0_next_tid < high_tid) {
2414 hammer_modify_volume_noundo(trans, trans->rootvol);
2415 trans->rootvol->ondisk->vol0_next_tid = high_tid;
2416 hammer_modify_volume_done(trans->rootvol);
2420 * WARNING! cursor's leaf pointer may have changed after
2421 * do_propagation returns.
2423 if (error == 0 && doprop)
2424 hammer_btree_do_propagation(cursor, NULL, leaf);
2430 if (error && leaf->data_offset) {
2431 hammer_blockmap_free(trans, leaf->data_offset, leaf->data_len);
2434 hammer_sync_unlock(trans);
2436 hammer_rel_buffer(data_buffer, 0);
2441 * Delete the B-Tree element at the current cursor and do any necessary
2442 * mirror propagation.
2444 * The cursor must be properly positioned for an iteration on return but
2445 * may be pointing at an internal element.
2447 * An element can be un-deleted by passing a delete_tid of 0 with
2448 * HAMMER_DELETE_ADJUST.
2450 * This function will store the number of bytes deleted in *stat_bytes
2451 * if stat_bytes is not NULL.
2454 hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
2455 hammer_tid_t delete_tid, uint32_t delete_ts,
2456 int track, int64_t *stat_bytes)
2458 struct hammer_btree_leaf_elm save_leaf;
2459 hammer_transaction_t trans;
2460 hammer_btree_leaf_elm_t leaf;
2462 hammer_btree_elm_t elm;
2463 hammer_off_t data_offset;
2471 error = hammer_cursor_upgrade(cursor);
2475 trans = cursor->trans;
2476 node = cursor->node;
2477 elm = &node->ondisk->elms[cursor->index];
2479 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
2481 hammer_sync_lock_sh(trans);
2487 * Adjust the delete_tid. Update the mirror_tid propagation field
2488 * as well. delete_tid can be 0 (undelete -- used by mirroring).
2490 if (delete_flags & HAMMER_DELETE_ADJUST) {
2491 if (elm->base.rec_type == HAMMER_RECTYPE_INODE) {
2492 if (elm->leaf.base.delete_tid == 0 && delete_tid)
2494 if (elm->leaf.base.delete_tid && delete_tid == 0)
2498 hammer_modify_node(trans, node, elm, sizeof(*elm));
2499 elm->leaf.base.delete_tid = delete_tid;
2500 elm->leaf.delete_ts = delete_ts;
2501 hammer_modify_node_done(node);
2503 if (elm->leaf.base.delete_tid > node->ondisk->mirror_tid) {
2504 hammer_modify_node_field(trans, node, mirror_tid);
2505 node->ondisk->mirror_tid = elm->leaf.base.delete_tid;
2506 hammer_modify_node_done(node);
2508 if (hammer_debug_general & 0x0002) {
2509 hdkprintf("propagate %016jx @%016jx\n",
2510 (intmax_t)elm->leaf.base.delete_tid,
2511 (intmax_t)node->node_offset);
2516 * Adjust for the iteration. We have deleted the current
2517 * element and want to clear ATEDISK so the iteration does
2518 * not skip the element after, which now becomes the current
2519 * element. This element must be re-tested if doing an
2520 * iteration, which is handled by the RETEST flag.
2522 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2523 cursor->flags |= HAMMER_CURSOR_RETEST;
2524 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2528 * An on-disk record cannot have the same delete_tid
2529 * as its create_tid. In a chain of record updates
2530 * this could result in a duplicate record.
2532 KKASSERT(elm->leaf.base.delete_tid !=
2533 elm->leaf.base.create_tid);
2537 * Destroy the B-Tree element if asked (typically if a nohistory
2538 * file or mount, or when called by the pruning code).
2540 * Adjust the ATEDISK flag to properly support iterations.
2542 if (delete_flags & HAMMER_DELETE_DESTROY) {
2543 data_offset = elm->leaf.data_offset;
2544 data_len = elm->leaf.data_len;
2546 save_leaf = elm->leaf;
2549 if (elm->base.rec_type == HAMMER_RECTYPE_INODE &&
2550 elm->leaf.base.delete_tid == 0) {
2554 error = hammer_btree_delete(cursor, &ndelete);
2557 * The deletion moves the next element (if any) to
2558 * the current element position. We must clear
2559 * ATEDISK so this element is not skipped and we
2560 * must set RETEST to force any iteration to re-test
2563 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2564 cursor->flags |= HAMMER_CURSOR_RETEST;
2565 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2567 bytes += (ndelete * sizeof(struct hammer_node_ondisk));
2569 switch(HAMMER_ZONE(data_offset)) {
2570 case HAMMER_ZONE_LARGE_DATA:
2571 case HAMMER_ZONE_SMALL_DATA:
2572 case HAMMER_ZONE_META:
2573 hammer_blockmap_free(trans,
2574 data_offset, data_len);
2584 * Track inode count and next_tid. This is used by the mirroring
2585 * and PFS code. icount can be negative, zero, or positive.
2587 if (error == 0 && track) {
2589 hammer_modify_volume_field(trans, trans->rootvol,
2591 trans->rootvol->ondisk->vol0_stat_inodes += icount;
2592 hammer_modify_volume_done(trans->rootvol);
2594 if (trans->rootvol->ondisk->vol0_next_tid < delete_tid) {
2595 hammer_modify_volume_noundo(trans, trans->rootvol);
2596 trans->rootvol->ondisk->vol0_next_tid = delete_tid;
2597 hammer_modify_volume_done(trans->rootvol);
2602 * mirror_tid propagation occurs if the node's mirror_tid had to be
2603 * updated while adjusting the delete_tid.
2605 * This occurs when deleting even in nohistory mode, but does not
2606 * occur when pruning an already-deleted node.
2608 * cursor->ip is NULL when called from the pruning, mirroring,
2609 * and pfs code. If non-NULL propagation will be conditionalized
2610 * on whether the PFS is in no-history mode or not.
2612 * WARNING: cursor's leaf pointer may have changed after do_propagation
2617 hammer_btree_do_propagation(cursor, cursor->ip->pfsm, leaf);
2619 hammer_btree_do_propagation(cursor, NULL, leaf);
2622 *stat_bytes = bytes;
2623 hammer_sync_unlock(trans);
2628 * Determine whether we can remove a directory. This routine checks whether
2629 * a directory is empty or not and enforces flush connectivity.
2631 * Flush connectivity requires that we block if the target directory is
2632 * currently flushing, otherwise it may not end up in the same flush group.
2634 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
2637 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
2639 struct hammer_cursor cursor;
2643 * Check directory empty
2645 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2647 cursor.key_beg.localization = ip->obj_localization |
2648 hammer_dir_localization(ip);
2649 cursor.key_beg.obj_id = ip->obj_id;
2650 cursor.key_beg.create_tid = 0;
2651 cursor.key_beg.delete_tid = 0;
2652 cursor.key_beg.obj_type = 0;
2653 cursor.key_beg.rec_type = HAMMER_RECTYPE_ENTRY_START;
2654 cursor.key_beg.key = HAMMER_MIN_KEY;
2656 cursor.key_end = cursor.key_beg;
2657 cursor.key_end.rec_type = HAMMER_RECTYPE_MAX;
2658 cursor.key_end.key = HAMMER_MAX_KEY;
2660 cursor.asof = ip->obj_asof;
2661 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2663 error = hammer_ip_first(&cursor);
2664 if (error == ENOENT)
2666 else if (error == 0)
2668 hammer_done_cursor(&cursor);
2673 * Localize the data payload. Directory entries may need their
2674 * localization adjusted.
2678 hammer_cursor_localize_data(hammer_data_ondisk_t data,
2679 hammer_btree_leaf_elm_t leaf)
2681 uint32_t localization;
2683 if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
2684 localization = leaf->base.localization &
2685 HAMMER_LOCALIZE_PSEUDOFS_MASK;
2686 if (data->entry.localization != localization) {
2687 data->entry.localization = localization;
2688 hammer_crc_set_leaf(data, leaf);