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.97 2008/09/23 22:28:56 dillon Exp $
39 static int hammer_mem_lookup(hammer_cursor_t cursor);
40 static void hammer_mem_first(hammer_cursor_t cursor);
41 static int hammer_frontend_trunc_callback(hammer_record_t record,
43 static int hammer_bulk_scan_callback(hammer_record_t record, void *data);
44 static int hammer_record_needs_overwrite_delete(hammer_record_t record);
45 static int hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
46 hammer_btree_leaf_elm_t leaf);
47 static int hammer_cursor_localize_data(hammer_data_ondisk_t data,
48 hammer_btree_leaf_elm_t leaf);
50 struct rec_trunc_info {
55 struct hammer_bulk_info {
56 hammer_record_t record;
57 hammer_record_t conflict;
61 * Red-black tree support. Comparison code for insertion.
64 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
66 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
68 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
71 if (rec1->leaf.base.key < rec2->leaf.base.key)
73 if (rec1->leaf.base.key > rec2->leaf.base.key)
77 * For search & insertion purposes records deleted by the
78 * frontend or deleted/committed by the backend are silently
79 * ignored. Otherwise pipelined insertions will get messed
82 * rec1 is greater then rec2 if rec1 is marked deleted.
83 * rec1 is less then rec2 if rec2 is marked deleted.
85 * Multiple deleted records may be present, do not return 0
86 * if both are marked deleted.
88 if (rec1->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
89 HAMMER_RECF_COMMITTED)) {
92 if (rec2->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
93 HAMMER_RECF_COMMITTED)) {
101 * Basic record comparison code similar to hammer_btree_cmp().
103 * obj_id is not compared and may not yet be assigned in the record.
106 hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
108 if (elm->rec_type < rec->leaf.base.rec_type)
110 if (elm->rec_type > rec->leaf.base.rec_type)
113 if (elm->key < rec->leaf.base.key)
115 if (elm->key > rec->leaf.base.key)
119 * Never match against an item deleted by the frontend
120 * or backend, or committed by the backend.
122 * elm is less then rec if rec is marked deleted.
124 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
125 HAMMER_RECF_COMMITTED)) {
132 * Ranged scan to locate overlapping record(s). This is used by
133 * hammer_ip_get_bulk() to locate an overlapping record. We have
134 * to use a ranged scan because the keys for data records with the
135 * same file base offset can be different due to differing data_len's.
137 * NOTE: The base file offset of a data record is (key - data_len), not (key).
140 hammer_rec_overlap_cmp(hammer_record_t rec, void *data)
142 struct hammer_bulk_info *info = data;
143 hammer_btree_leaf_elm_t leaf = &info->record->leaf;
145 if (rec->leaf.base.rec_type < leaf->base.rec_type)
147 if (rec->leaf.base.rec_type > leaf->base.rec_type)
153 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
154 /* rec_beg >= leaf_end */
155 if (rec->leaf.base.key - rec->leaf.data_len >= leaf->base.key)
157 /* rec_end <= leaf_beg */
158 if (rec->leaf.base.key <= leaf->base.key - leaf->data_len)
161 if (rec->leaf.base.key < leaf->base.key)
163 if (rec->leaf.base.key > leaf->base.key)
168 * We have to return 0 at this point, even if DELETED_FE is set,
169 * because returning anything else will cause the scan to ignore
170 * one of the branches when we really want it to check both.
176 * RB_SCAN comparison code for hammer_mem_first(). The argument order
177 * is reversed so the comparison result has to be negated. key_beg and
178 * key_end are both range-inclusive.
180 * Localized deletions are not cached in-memory.
184 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
186 hammer_cursor_t cursor = data;
189 r = hammer_rec_cmp(&cursor->key_beg, rec);
192 r = hammer_rec_cmp(&cursor->key_end, rec);
199 * This compare function is used when simply looking up key_beg.
203 hammer_rec_find_cmp(hammer_record_t rec, void *data)
205 hammer_cursor_t cursor = data;
208 r = hammer_rec_cmp(&cursor->key_beg, rec);
217 * Locate blocks within the truncation range. Partial blocks do not count.
221 hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
223 struct rec_trunc_info *info = data;
225 if (rec->leaf.base.rec_type < info->rec_type)
227 if (rec->leaf.base.rec_type > info->rec_type)
230 switch(rec->leaf.base.rec_type) {
231 case HAMMER_RECTYPE_DB:
233 * DB record key is not beyond the truncation point, retain.
235 if (rec->leaf.base.key < info->trunc_off)
238 case HAMMER_RECTYPE_DATA:
240 * DATA record offset start is not beyond the truncation point,
243 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
247 panic("hammer_rec_trunc_cmp: unexpected record type");
251 * The record start is >= the truncation point, return match,
252 * the record should be destroyed.
257 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
260 * Allocate a record for the caller to finish filling in. The record is
261 * returned referenced.
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 B-Tree
412 if (record->flags & HAMMER_RECF_ONRBTREE) {
413 RB_REMOVE(hammer_rec_rb_tree,
414 &record->ip->rec_tree,
416 record->flags &= ~HAMMER_RECF_ONRBTREE;
417 KKASSERT(ip->rsv_recs > 0);
418 if (RB_EMPTY(&record->ip->rec_tree)) {
420 ~HAMMER_INODE_XDIRTY;
421 record->ip->sync_flags &=
422 ~HAMMER_INODE_XDIRTY;
430 * We must wait for any direct-IO to complete before
431 * we can destroy the record because the bio may
432 * have a reference to it.
435 (HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL)) {
436 hammer_io_direct_wait(record);
440 * Account for the completion after the direct IO
446 hmp->rsv_databytes -= record->leaf.data_len;
448 if (RB_EMPTY(&record->ip->rec_tree))
449 hammer_test_inode(record->ip);
450 if (ip->rsv_recs == hammer_limit_inode_recs - 1)
451 wakeup(&ip->rsv_recs);
455 * Do this test after removing record from the B-Tree.
458 hammer_test_inode(target_ip);
459 hammer_rel_inode(target_ip, 0);
462 if (record->flags & HAMMER_RECF_ALLOCDATA) {
463 --hammer_count_record_datas;
464 kfree(record->data, hmp->m_misc);
465 record->flags &= ~HAMMER_RECF_ALLOCDATA;
469 * Release the reservation.
471 * If the record was not committed we can theoretically
472 * undo the reservation. However, doing so might
473 * create weird edge cases with the ordering of
474 * direct writes because the related buffer cache
475 * elements are per-vnode. So we don't try.
477 if ((resv = record->resv) != NULL) {
478 /* XXX undo leaf.data_offset,leaf.data_len */
479 hammer_blockmap_reserve_complete(hmp, resv);
483 --hammer_count_records;
484 kfree(record, hmp->m_misc);
490 * Record visibility depends on whether the record is being accessed by
491 * the backend or the frontend. Backend tests ignore the frontend delete
492 * flag. Frontend tests do NOT ignore the backend delete/commit flags and
493 * must also check for commit races.
495 * Return non-zero if the record is visible, zero if it isn't or if it is
496 * deleted. Returns 0 if the record has been comitted (unless the special
497 * delete-visibility flag is set). A committed record must be located
498 * via the media B-Tree. Returns non-zero if the record is good.
500 * If HAMMER_CURSOR_DELETE_VISIBILITY is set we allow deleted memory
501 * records to be returned. This is so pending deletions are detected
502 * when using an iterator to locate an unused hash key, or when we need
503 * to locate historical records on-disk to destroy.
507 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
509 if (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY)
511 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
512 if (record->flags & (HAMMER_RECF_DELETED_BE |
513 HAMMER_RECF_COMMITTED)) {
517 if (record->flags & (HAMMER_RECF_DELETED_FE |
518 HAMMER_RECF_DELETED_BE |
519 HAMMER_RECF_COMMITTED)) {
527 * This callback is used as part of the RB_SCAN function for in-memory
528 * records. We terminate it (return -1) as soon as we get a match.
530 * This routine is used by frontend code.
532 * The primary compare code does not account for ASOF lookups. This
533 * code handles that case as well as a few others.
537 hammer_rec_scan_callback(hammer_record_t rec, void *data)
539 hammer_cursor_t cursor = data;
542 * We terminate on success, so this should be NULL on entry.
544 KKASSERT(cursor->iprec == NULL);
547 * Skip if the record was marked deleted or committed.
549 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
553 * Skip if not visible due to our as-of TID
555 if (cursor->flags & HAMMER_CURSOR_ASOF) {
556 if (cursor->asof < rec->leaf.base.create_tid)
558 if (rec->leaf.base.delete_tid &&
559 cursor->asof >= rec->leaf.base.delete_tid) {
565 * ref the record. The record is protected from backend B-Tree
566 * interactions by virtue of the cursor's IP lock.
568 hammer_ref(&rec->lock);
571 * The record may have been deleted or committed while we
572 * were blocked. XXX remove?
574 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
575 hammer_rel_mem_record(rec);
580 * Set the matching record and stop the scan.
588 * Lookup an in-memory record given the key specified in the cursor. Works
589 * just like hammer_btree_lookup() but operates on an inode's in-memory
592 * The lookup must fail if the record is marked for deferred deletion.
594 * The API for mem/btree_lookup() does not mess with the ATE/EOF bits.
598 hammer_mem_lookup(hammer_cursor_t cursor)
600 KKASSERT(cursor->ip);
602 hammer_rel_mem_record(cursor->iprec);
603 cursor->iprec = NULL;
605 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
606 hammer_rec_scan_callback, cursor);
608 return (cursor->iprec ? 0 : ENOENT);
612 * hammer_mem_first() - locate the first in-memory record matching the
613 * cursor within the bounds of the key range.
615 * WARNING! API is slightly different from btree_first(). hammer_mem_first()
616 * will set ATEMEM the same as MEMEOF, and does not return any error.
620 hammer_mem_first(hammer_cursor_t cursor)
625 KKASSERT(ip != NULL);
628 hammer_rel_mem_record(cursor->iprec);
629 cursor->iprec = NULL;
631 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
632 hammer_rec_scan_callback, cursor);
635 cursor->flags &= ~(HAMMER_CURSOR_MEMEOF | HAMMER_CURSOR_ATEMEM);
637 cursor->flags |= HAMMER_CURSOR_MEMEOF | HAMMER_CURSOR_ATEMEM;
640 /************************************************************************
641 * HAMMER IN-MEMORY RECORD FUNCTIONS *
642 ************************************************************************
644 * These functions manipulate in-memory records. Such records typically
645 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
649 * Add a directory entry (dip,ncp) which references inode (ip).
651 * Note that the low 32 bits of the namekey are set temporarily to create
652 * a unique in-memory record, and may be modified a second time when the
653 * record is synchronized to disk. In particular, the low 32 bits cannot be
654 * all 0's when synching to disk, which is not handled here.
656 * NOTE: bytes does not include any terminating \0 on name, and name might
660 hammer_ip_add_directory(struct hammer_transaction *trans,
661 struct hammer_inode *dip, const char *name, int bytes,
662 struct hammer_inode *ip)
664 struct hammer_cursor cursor;
665 hammer_record_t record;
667 u_int32_t max_iterations;
669 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
671 record->type = HAMMER_MEM_RECORD_ADD;
672 record->leaf.base.localization = dip->obj_localization +
673 hammer_dir_localization(dip);
674 record->leaf.base.obj_id = dip->obj_id;
675 record->leaf.base.key = hammer_directory_namekey(dip, name, bytes,
677 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
678 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
679 record->data->entry.obj_id = ip->obj_id;
680 record->data->entry.localization = ip->obj_localization;
681 bcopy(name, record->data->entry.name, bytes);
683 ++ip->ino_data.nlinks;
684 ip->ino_data.ctime = trans->time;
685 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
688 * Find an unused namekey. Both the in-memory record tree and
689 * the B-Tree are checked. We do not want historically deleted
690 * names to create a collision as our iteration space may be limited,
691 * and since create_tid wouldn't match anyway an ASOF search
692 * must be used to locate collisions.
694 * delete-visibility is set so pending deletions do not give us
695 * a false-negative on our ability to use an iterator.
697 * The iterator must not rollover the key. Directory keys only
698 * use the positive key space.
700 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
701 cursor.key_beg = record->leaf.base;
702 cursor.flags |= HAMMER_CURSOR_ASOF;
703 cursor.flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
704 cursor.asof = ip->obj_asof;
706 while (hammer_ip_lookup(&cursor) == 0) {
707 ++record->leaf.base.key;
708 KKASSERT(record->leaf.base.key > 0);
709 cursor.key_beg.key = record->leaf.base.key;
710 if (--max_iterations == 0) {
711 hammer_rel_mem_record(record);
718 * The target inode and the directory entry are bound together.
720 record->target_ip = ip;
721 record->flush_state = HAMMER_FST_SETUP;
722 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
725 * The inode now has a dependancy and must be taken out of the idle
726 * state. An inode not in an idle state is given an extra reference.
728 * When transitioning to a SETUP state flag for an automatic reflush
729 * when the dependancies are disposed of if someone is waiting on
732 if (ip->flush_state == HAMMER_FST_IDLE) {
733 hammer_ref(&ip->lock);
734 ip->flush_state = HAMMER_FST_SETUP;
735 if (ip->flags & HAMMER_INODE_FLUSHW)
736 ip->flags |= HAMMER_INODE_REFLUSH;
738 error = hammer_mem_add(record);
740 dip->ino_data.mtime = trans->time;
741 hammer_modify_inode(trans, dip, HAMMER_INODE_MTIME);
744 hammer_done_cursor(&cursor);
749 * Delete the directory entry and update the inode link count. The
750 * cursor must be seeked to the directory entry record being deleted.
752 * The related inode should be share-locked by the caller. The caller is
753 * on the frontend. It could also be NULL indicating that the directory
754 * entry being removed has no related inode.
756 * This function can return EDEADLK requiring the caller to terminate
757 * the cursor, any locks, wait on the returned record, and retry.
760 hammer_ip_del_directory(struct hammer_transaction *trans,
761 hammer_cursor_t cursor, struct hammer_inode *dip,
762 struct hammer_inode *ip)
764 hammer_record_t record;
767 if (hammer_cursor_inmem(cursor)) {
769 * In-memory (unsynchronized) records can simply be freed.
771 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
772 * by the backend, we must still avoid races against the
773 * backend potentially syncing the record to the media.
775 * We cannot call hammer_ip_delete_record(), that routine may
776 * only be called from the backend.
778 record = cursor->iprec;
779 if (record->flags & (HAMMER_RECF_INTERLOCK_BE |
780 HAMMER_RECF_DELETED_BE |
781 HAMMER_RECF_COMMITTED)) {
782 KKASSERT(cursor->deadlk_rec == NULL);
783 hammer_ref(&record->lock);
784 cursor->deadlk_rec = record;
787 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
788 record->flags |= HAMMER_RECF_DELETED_FE;
793 * If the record is on-disk we have to queue the deletion by
794 * the record's key. This also causes lookups to skip the
795 * record (lookups for the purposes of finding an unused
796 * directory key do not skip the record).
798 KKASSERT(dip->flags &
799 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
800 record = hammer_alloc_mem_record(dip, 0);
801 record->type = HAMMER_MEM_RECORD_DEL;
802 record->leaf.base = cursor->leaf->base;
803 KKASSERT(dip->obj_id == record->leaf.base.obj_id);
806 * ip may be NULL, indicating the deletion of a directory
807 * entry which has no related inode.
809 record->target_ip = ip;
811 record->flush_state = HAMMER_FST_SETUP;
812 TAILQ_INSERT_TAIL(&ip->target_list, record,
815 record->flush_state = HAMMER_FST_IDLE;
819 * The inode now has a dependancy and must be taken out of
820 * the idle state. An inode not in an idle state is given
821 * an extra reference.
823 * When transitioning to a SETUP state flag for an automatic
824 * reflush when the dependancies are disposed of if someone
825 * is waiting on the inode.
827 if (ip && ip->flush_state == HAMMER_FST_IDLE) {
828 hammer_ref(&ip->lock);
829 ip->flush_state = HAMMER_FST_SETUP;
830 if (ip->flags & HAMMER_INODE_FLUSHW)
831 ip->flags |= HAMMER_INODE_REFLUSH;
834 error = hammer_mem_add(record);
838 * One less link. The file may still be open in the OS even after
839 * all links have gone away.
841 * We have to terminate the cursor before syncing the inode to
842 * avoid deadlocking against ourselves. XXX this may no longer
845 * If nlinks drops to zero and the vnode is inactive (or there is
846 * no vnode), call hammer_inode_unloadable_check() to zonk the
847 * inode. If we don't do this here the inode will not be destroyed
848 * on-media until we unmount.
852 --ip->ino_data.nlinks; /* do before we might block */
853 ip->ino_data.ctime = trans->time;
855 dip->ino_data.mtime = trans->time;
856 hammer_modify_inode(trans, dip, HAMMER_INODE_MTIME);
858 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
859 if (ip->ino_data.nlinks == 0 &&
860 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
861 hammer_done_cursor(cursor);
862 hammer_inode_unloadable_check(ip, 1);
863 hammer_flush_inode(ip, 0);
872 * Add a record to an inode.
874 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
875 * initialize the following additional fields:
877 * The related inode should be share-locked by the caller. The caller is
880 * record->rec.entry.base.base.key
881 * record->rec.entry.base.base.rec_type
882 * record->rec.entry.base.base.data_len
883 * record->data (a copy will be kmalloc'd if it cannot be embedded)
886 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
888 hammer_inode_t ip = record->ip;
891 KKASSERT(record->leaf.base.localization != 0);
892 record->leaf.base.obj_id = ip->obj_id;
893 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
894 error = hammer_mem_add(record);
899 * Locate a pre-existing bulk record in memory. The caller wishes to
900 * replace the record with a new one. The existing record may have a
901 * different length (and thus a different key) so we have to use an
902 * overlap check function.
904 static hammer_record_t
905 hammer_ip_get_bulk(hammer_record_t record)
907 struct hammer_bulk_info info;
908 hammer_inode_t ip = record->ip;
910 info.record = record;
911 info.conflict = NULL;
912 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_overlap_cmp,
913 hammer_bulk_scan_callback, &info);
915 return(info.conflict); /* may be NULL */
919 * Take records vetted by overlap_cmp. The first non-deleted record
920 * (if any) stops the scan.
923 hammer_bulk_scan_callback(hammer_record_t record, void *data)
925 struct hammer_bulk_info *info = data;
927 if (record->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
928 HAMMER_RECF_COMMITTED)) {
931 hammer_ref(&record->lock);
932 info->conflict = record;
933 return(-1); /* stop scan */
937 * Reserve blockmap space placemarked with an in-memory record.
939 * This routine is called by the frontend in order to be able to directly
940 * flush a buffer cache buffer. The frontend has locked the related buffer
941 * cache buffers and we should be able to manipulate any overlapping
944 * The caller is responsible for adding the returned record and deleting
945 * the returned conflicting record (if any), typically by calling
946 * hammer_ip_replace_bulk() (via hammer_io_direct_write()).
949 hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
952 hammer_record_t record;
956 * Create a record to cover the direct write. The record cannot
957 * be added to the in-memory RB tree here as it might conflict
958 * with an existing memory record. See hammer_io_direct_write().
960 * The backend is responsible for finalizing the space reserved in
963 * XXX bytes not aligned, depend on the reservation code to
964 * align the reservation.
966 record = hammer_alloc_mem_record(ip, 0);
967 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
968 HAMMER_ZONE_SMALL_DATA_INDEX;
969 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
970 &record->leaf.data_offset,
972 if (record->resv == NULL) {
973 kprintf("hammer_ip_add_bulk: reservation failed\n");
974 hammer_rel_mem_record(record);
977 record->type = HAMMER_MEM_RECORD_DATA;
978 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
979 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
980 record->leaf.base.obj_id = ip->obj_id;
981 record->leaf.base.key = file_offset + bytes;
982 record->leaf.base.localization = ip->obj_localization +
983 HAMMER_LOCALIZE_MISC;
984 record->leaf.data_len = bytes;
985 hammer_crc_set_leaf(data, &record->leaf);
986 KKASSERT(*errorp == 0);
992 * Called by hammer_io_direct_write() prior to any possible completion
993 * of the BIO to emplace the memory record associated with the I/O and
994 * to replace any prior memory record which might still be active.
996 * Setting the FE deleted flag on the old record (if any) avoids any RB
997 * tree insertion conflict, amoung other things.
999 * This has to be done prior to the caller completing any related buffer
1000 * cache I/O or a reinstantiation of the buffer may load data from the
1001 * old media location instead of the new media location. The holding
1002 * of the locked buffer cache buffer serves to interlock the record
1003 * replacement operation.
1006 hammer_ip_replace_bulk(hammer_mount_t hmp, hammer_record_t record)
1008 hammer_record_t conflict;
1011 while ((conflict = hammer_ip_get_bulk(record)) != NULL) {
1012 if ((conflict->flags & HAMMER_RECF_INTERLOCK_BE) == 0) {
1013 conflict->flags |= HAMMER_RECF_DELETED_FE;
1016 conflict->flags |= HAMMER_RECF_WANTED;
1017 tsleep(conflict, 0, "hmrrc3", 0);
1018 hammer_rel_mem_record(conflict);
1020 error = hammer_mem_add(record);
1022 hammer_rel_mem_record(conflict);
1023 KKASSERT(error == 0);
1027 * Frontend truncation code. Scan in-memory records only. On-disk records
1028 * and records in a flushing state are handled by the backend. The vnops
1029 * setattr code will handle the block containing the truncation point.
1031 * Partial blocks are not deleted.
1033 * This code is only called on regular files.
1036 hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
1038 struct rec_trunc_info info;
1040 switch(ip->ino_data.obj_type) {
1041 case HAMMER_OBJTYPE_REGFILE:
1042 info.rec_type = HAMMER_RECTYPE_DATA;
1044 case HAMMER_OBJTYPE_DBFILE:
1045 info.rec_type = HAMMER_RECTYPE_DB;
1050 info.trunc_off = file_size;
1051 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
1052 hammer_frontend_trunc_callback, &info);
1057 * Scan callback for frontend records to destroy during a truncation.
1058 * We must ensure that DELETED_FE is set on the record or the frontend
1059 * will get confused in future read() calls.
1061 * NOTE: DELETED_FE cannot be set while the record interlock (BE) is held.
1062 * In this rare case we must wait for the interlock to be cleared.
1064 * NOTE: This function is only called on regular files. There are further
1065 * restrictions to the setting of DELETED_FE on directory records
1066 * undergoing a flush due to sensitive inode link count calculations.
1069 hammer_frontend_trunc_callback(hammer_record_t record, void *data __unused)
1071 if (record->flags & HAMMER_RECF_DELETED_FE)
1074 if (record->flush_state == HAMMER_FST_FLUSH)
1077 hammer_ref(&record->lock);
1078 while (record->flags & HAMMER_RECF_INTERLOCK_BE)
1079 hammer_wait_mem_record_ident(record, "hmmtrr");
1080 record->flags |= HAMMER_RECF_DELETED_FE;
1081 hammer_rel_mem_record(record);
1086 * Return 1 if the caller must check for and delete existing records
1087 * before writing out a new data record.
1089 * Return 0 if the caller can just insert the record into the B-Tree without
1093 hammer_record_needs_overwrite_delete(hammer_record_t record)
1095 hammer_inode_t ip = record->ip;
1096 int64_t file_offset;
1099 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
1100 file_offset = record->leaf.base.key;
1102 file_offset = record->leaf.base.key - record->leaf.data_len;
1103 r = (file_offset < ip->save_trunc_off);
1104 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1105 if (ip->save_trunc_off <= record->leaf.base.key)
1106 ip->save_trunc_off = record->leaf.base.key + 1;
1108 if (ip->save_trunc_off < record->leaf.base.key)
1109 ip->save_trunc_off = record->leaf.base.key;
1115 * Backend code. Sync a record to the media.
1118 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
1120 hammer_transaction_t trans = cursor->trans;
1121 int64_t file_offset;
1127 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1128 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
1129 KKASSERT(record->leaf.base.localization != 0);
1132 * Any direct-write related to the record must complete before we
1133 * can sync the record to the on-disk media.
1135 if (record->gflags & (HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL))
1136 hammer_io_direct_wait(record);
1139 * If this is a bulk-data record placemarker there may be an existing
1140 * record on-disk, indicating a data overwrite. If there is the
1141 * on-disk record must be deleted before we can insert our new record.
1143 * We've synthesized this record and do not know what the create_tid
1144 * on-disk is, nor how much data it represents.
1146 * Keep in mind that (key) for data records is (base_offset + len),
1147 * not (base_offset). Also, we only want to get rid of on-disk
1148 * records since we are trying to sync our in-memory record, call
1149 * hammer_ip_delete_range() with truncating set to 1 to make sure
1150 * it skips in-memory records.
1152 * It is ok for the lookup to return ENOENT.
1154 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1155 * to call hammer_ip_delete_range() or not. This also means we must
1156 * update sync_trunc_off() as we write.
1158 if (record->type == HAMMER_MEM_RECORD_DATA &&
1159 hammer_record_needs_overwrite_delete(record)) {
1160 file_offset = record->leaf.base.key - record->leaf.data_len;
1161 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1163 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
1164 error = hammer_ip_delete_range(
1166 file_offset, file_offset + bytes - 1,
1168 if (error && error != ENOENT)
1173 * If this is a general record there may be an on-disk version
1174 * that must be deleted before we can insert the new record.
1176 if (record->type == HAMMER_MEM_RECORD_GENERAL) {
1177 error = hammer_delete_general(cursor, record->ip,
1179 if (error && error != ENOENT)
1186 hammer_normalize_cursor(cursor);
1187 cursor->key_beg = record->leaf.base;
1188 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1189 cursor->flags |= HAMMER_CURSOR_BACKEND;
1190 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1193 * Records can wind up on-media before the inode itself is on-media.
1196 record->ip->flags |= HAMMER_INODE_DONDISK;
1199 * If we are deleting a directory entry an exact match must be
1202 if (record->type == HAMMER_MEM_RECORD_DEL) {
1203 error = hammer_btree_lookup(cursor);
1205 KKASSERT(cursor->iprec == NULL);
1206 error = hammer_ip_delete_record(cursor, record->ip,
1209 record->flags |= HAMMER_RECF_DELETED_BE |
1210 HAMMER_RECF_COMMITTED;
1211 ++record->ip->rec_generation;
1220 * Issue a lookup to position the cursor and locate the insertion
1221 * point. The target key should not exist. If we are creating a
1222 * directory entry we may have to iterate the low 32 bits of the
1223 * key to find an unused key.
1225 hammer_sync_lock_sh(trans);
1226 cursor->flags |= HAMMER_CURSOR_INSERT;
1227 error = hammer_btree_lookup(cursor);
1228 if (hammer_debug_inode)
1229 kprintf("DOINSERT LOOKUP %d\n", error);
1231 kprintf("hammer_ip_sync_record: duplicate rec "
1232 "at (%016llx)\n", (long long)record->leaf.base.key);
1233 if (hammer_debug_critical)
1234 Debugger("duplicate record1");
1238 if (record->type == HAMMER_MEM_RECORD_DATA)
1239 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1240 record->leaf.base.key - record->leaf.data_len,
1241 record->leaf.data_offset, error);
1244 if (error != ENOENT)
1248 * Allocate the record and data. The result buffers will be
1249 * marked as being modified and further calls to
1250 * hammer_modify_buffer() will result in unneeded UNDO records.
1252 * Support zero-fill records (data == NULL and data_len != 0)
1254 if (record->type == HAMMER_MEM_RECORD_DATA) {
1256 * The data portion of a bulk-data record has already been
1257 * committed to disk, we need only adjust the layer2
1258 * statistics in the same transaction as our B-Tree insert.
1260 KKASSERT(record->leaf.data_offset != 0);
1261 error = hammer_blockmap_finalize(trans,
1263 record->leaf.data_offset,
1264 record->leaf.data_len);
1265 } else if (record->data && record->leaf.data_len) {
1267 * Wholely cached record, with data. Allocate the data.
1269 bdata = hammer_alloc_data(trans, record->leaf.data_len,
1270 record->leaf.base.rec_type,
1271 &record->leaf.data_offset,
1272 &cursor->data_buffer,
1276 hammer_crc_set_leaf(record->data, &record->leaf);
1277 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
1278 bcopy(record->data, bdata, record->leaf.data_len);
1279 hammer_modify_buffer_done(cursor->data_buffer);
1282 * Wholely cached record, without data.
1284 record->leaf.data_offset = 0;
1285 record->leaf.data_crc = 0;
1288 error = hammer_btree_insert(cursor, &record->leaf, &doprop);
1289 if (hammer_debug_inode && error) {
1290 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n",
1292 (long long)cursor->node->node_offset,
1294 (long long)record->leaf.base.key);
1298 * Our record is on-disk and we normally mark the in-memory version
1299 * as having been committed (and not BE-deleted).
1301 * If the record represented a directory deletion but we had to
1302 * sync a valid directory entry to disk due to dependancies,
1303 * we must convert the record to a covering delete so the
1304 * frontend does not have visibility on the synced entry.
1306 * WARNING: cursor's leaf pointer may have changed after do_propagation
1311 hammer_btree_do_propagation(cursor,
1315 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1317 * Must convert deleted directory entry add
1318 * to a directory entry delete.
1320 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1321 record->flags &= ~HAMMER_RECF_DELETED_FE;
1322 record->type = HAMMER_MEM_RECORD_DEL;
1323 KKASSERT(record->ip->obj_id == record->leaf.base.obj_id);
1324 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1325 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1326 KKASSERT((record->flags & (HAMMER_RECF_COMMITTED |
1327 HAMMER_RECF_DELETED_BE)) == 0);
1328 /* converted record is not yet committed */
1329 /* hammer_flush_record_done takes care of the rest */
1332 * Everything went fine and we are now done with
1335 record->flags |= HAMMER_RECF_COMMITTED;
1336 ++record->ip->rec_generation;
1339 if (record->leaf.data_offset) {
1340 hammer_blockmap_free(trans, record->leaf.data_offset,
1341 record->leaf.data_len);
1345 hammer_sync_unlock(trans);
1351 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1352 * entry's key is used to deal with hash collisions in the upper 32 bits.
1353 * A unique 64 bit key is generated in-memory and may be regenerated a
1354 * second time when the directory record is flushed to the on-disk B-Tree.
1356 * A referenced record is passed to this function. This function
1357 * eats the reference. If an error occurs the record will be deleted.
1359 * A copy of the temporary record->data pointer provided by the caller
1363 hammer_mem_add(hammer_record_t record)
1365 hammer_mount_t hmp = record->ip->hmp;
1368 * Make a private copy of record->data
1371 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
1374 * Insert into the RB tree. A unique key should have already
1375 * been selected if this is a directory entry.
1377 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1378 record->flags |= HAMMER_RECF_DELETED_FE;
1379 hammer_rel_mem_record(record);
1382 ++hmp->count_newrecords;
1384 ++record->ip->rsv_recs;
1385 record->ip->hmp->rsv_databytes += record->leaf.data_len;
1386 record->flags |= HAMMER_RECF_ONRBTREE;
1387 hammer_modify_inode(NULL, record->ip, HAMMER_INODE_XDIRTY);
1388 hammer_rel_mem_record(record);
1392 /************************************************************************
1393 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1394 ************************************************************************
1396 * These functions augment the B-Tree scanning functions in hammer_btree.c
1397 * by merging in-memory records with on-disk records.
1401 * Locate a particular record either in-memory or on-disk.
1403 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1404 * NOT be called to iterate results.
1407 hammer_ip_lookup(hammer_cursor_t cursor)
1412 * If the element is in-memory return it without searching the
1415 KKASSERT(cursor->ip);
1416 error = hammer_mem_lookup(cursor);
1418 cursor->leaf = &cursor->iprec->leaf;
1421 if (error != ENOENT)
1425 * If the inode has on-disk components search the on-disk B-Tree.
1427 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1429 error = hammer_btree_lookup(cursor);
1431 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1436 * Helper for hammer_ip_first()/hammer_ip_next()
1438 * NOTE: Both ATEDISK and DISKEOF will be set the same. This sets up
1439 * hammer_ip_first() for calling hammer_ip_next(), and sets up the re-seek
1440 * state if hammer_ip_next() needs to re-seek.
1444 _hammer_ip_seek_btree(hammer_cursor_t cursor)
1446 hammer_inode_t ip = cursor->ip;
1449 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1450 error = hammer_btree_lookup(cursor);
1451 if (error == ENOENT || error == EDEADLK) {
1452 if (hammer_debug_general & 0x2000) {
1453 kprintf("error %d node %p %016llx index %d\n",
1454 error, cursor->node,
1455 (long long)cursor->node->node_offset,
1458 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1459 error = hammer_btree_iterate(cursor);
1462 cursor->flags &= ~(HAMMER_CURSOR_DISKEOF |
1463 HAMMER_CURSOR_ATEDISK);
1465 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1466 HAMMER_CURSOR_ATEDISK;
1467 if (error == ENOENT)
1471 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_ATEDISK;
1478 * Helper for hammer_ip_next()
1480 * The caller has determined that the media cursor is further along than the
1481 * memory cursor and must be reseeked after a generation number change.
1485 _hammer_ip_reseek(hammer_cursor_t cursor)
1487 struct hammer_base_elm save;
1488 hammer_btree_elm_t elm;
1496 kprintf("HAMMER: Debug: re-seeked during scan @ino=%016llx\n",
1497 (long long)cursor->ip->obj_id);
1498 save = cursor->key_beg;
1499 cursor->key_beg = cursor->iprec->leaf.base;
1500 error = _hammer_ip_seek_btree(cursor);
1501 KKASSERT(error == 0);
1502 cursor->key_beg = save;
1505 * If the memory record was previous returned to
1506 * the caller and the media record matches
1507 * (-1/+1: only create_tid differs), then iterate
1508 * the media record to avoid a double result.
1510 if ((cursor->flags & HAMMER_CURSOR_ATEDISK) == 0 &&
1511 (cursor->flags & HAMMER_CURSOR_LASTWASMEM)) {
1512 elm = &cursor->node->ondisk->elms[cursor->index];
1513 r = hammer_btree_cmp(&elm->base,
1514 &cursor->iprec->leaf.base);
1515 if (cursor->flags & HAMMER_CURSOR_ASOF) {
1516 if (r >= -1 && r <= 1) {
1517 kprintf("HAMMER: Debug: iterated after "
1518 "re-seek (asof r=%d)\n", r);
1519 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1524 kprintf("HAMMER: Debug: iterated after "
1526 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1535 * Locate the first record within the cursor's key_beg/key_end range,
1536 * restricted to a particular inode. 0 is returned on success, ENOENT
1537 * if no records matched the requested range, or some other error.
1539 * When 0 is returned hammer_ip_next() may be used to iterate additional
1540 * records within the requested range.
1542 * This function can return EDEADLK, requiring the caller to terminate
1543 * the cursor and try again.
1547 hammer_ip_first(hammer_cursor_t cursor)
1549 hammer_inode_t ip __debugvar = cursor->ip;
1552 KKASSERT(ip != NULL);
1555 * Clean up fields and setup for merged scan
1557 cursor->flags &= ~HAMMER_CURSOR_RETEST;
1560 * Search the in-memory record list (Red-Black tree). Unlike the
1561 * B-Tree search, mem_first checks for records in the range.
1563 * This function will setup both ATEMEM and MEMEOF properly for
1564 * the ip iteration. ATEMEM will be set if MEMEOF is set.
1566 hammer_mem_first(cursor);
1569 * Detect generation changes during blockages, including
1570 * blockages which occur on the initial btree search.
1572 cursor->rec_generation = cursor->ip->rec_generation;
1575 * Initial search and result
1577 error = _hammer_ip_seek_btree(cursor);
1579 error = hammer_ip_next(cursor);
1585 * Retrieve the next record in a merged iteration within the bounds of the
1586 * cursor. This call may be made multiple times after the cursor has been
1587 * initially searched with hammer_ip_first().
1589 * There are numerous special cases in this code to deal with races between
1590 * in-memory records and on-media records.
1592 * 0 is returned on success, ENOENT if no further records match the
1593 * requested range, or some other error code is returned.
1596 hammer_ip_next(hammer_cursor_t cursor)
1598 hammer_btree_elm_t elm;
1599 hammer_record_t rec;
1600 hammer_record_t tmprec;
1606 * Get the next on-disk record
1608 * NOTE: If we deleted the last on-disk record we had scanned
1609 * ATEDISK will be clear and RETEST will be set, forcing
1610 * a call to iterate. The fact that ATEDISK is clear causes
1611 * iterate to re-test the 'current' element. If ATEDISK is
1612 * set, iterate will skip the 'current' element.
1615 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1616 if (cursor->flags & (HAMMER_CURSOR_ATEDISK |
1617 HAMMER_CURSOR_RETEST)) {
1618 error = hammer_btree_iterate(cursor);
1619 cursor->flags &= ~HAMMER_CURSOR_RETEST;
1621 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1622 hammer_cache_node(&cursor->ip->cache[1],
1624 } else if (error == ENOENT) {
1625 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1626 HAMMER_CURSOR_ATEDISK;
1633 * If the generation changed the backend has deleted or committed
1634 * one or more memory records since our last check.
1636 * When this case occurs if the disk cursor is > current memory record
1637 * or the disk cursor is at EOF, we must re-seek the disk-cursor.
1638 * Since the cursor is ahead it must have not yet been eaten (if
1639 * not at eof anyway). (XXX data offset case?)
1641 * NOTE: we are not doing a full check here. That will be handled
1644 * If we have exhausted all memory records we do not have to do any
1647 while (cursor->rec_generation != cursor->ip->rec_generation &&
1650 kprintf("HAMMER: Debug: generation changed during scan @ino=%016llx\n", (long long)cursor->ip->obj_id);
1651 cursor->rec_generation = cursor->ip->rec_generation;
1652 if (cursor->flags & HAMMER_CURSOR_MEMEOF)
1654 if (cursor->flags & HAMMER_CURSOR_DISKEOF) {
1657 KKASSERT((cursor->flags & HAMMER_CURSOR_ATEDISK) == 0);
1658 elm = &cursor->node->ondisk->elms[cursor->index];
1659 r = hammer_btree_cmp(&elm->base,
1660 &cursor->iprec->leaf.base);
1664 * Do we re-seek the media cursor?
1667 if (_hammer_ip_reseek(cursor))
1673 * We can now safely get the next in-memory record. We cannot
1676 * hammer_rec_scan_cmp: Is the record still in our general range,
1677 * (non-inclusive of snapshot exclusions)?
1678 * hammer_rec_scan_callback: Is the record in our snapshot?
1681 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1683 * If the current memory record was eaten then get the next
1684 * one. Stale records are skipped.
1686 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1687 tmprec = cursor->iprec;
1688 cursor->iprec = NULL;
1689 rec = hammer_rec_rb_tree_RB_NEXT(tmprec);
1691 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1693 if (hammer_rec_scan_callback(rec, cursor) != 0)
1695 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1697 if (cursor->iprec) {
1698 KKASSERT(cursor->iprec == rec);
1699 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1701 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1703 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1708 * MEMORY RECORD VALIDITY TEST
1710 * (We still can't block, which is why tmprec is being held so
1713 * If the memory record is no longer valid we skip it. It may
1714 * have been deleted by the frontend. If it was deleted or
1715 * committed by the backend the generation change re-seeked the
1716 * disk cursor and the record will be present there.
1718 if (error == 0 && (cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1719 KKASSERT(cursor->iprec);
1720 KKASSERT((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0);
1721 if (!hammer_ip_iterate_mem_good(cursor, cursor->iprec)) {
1722 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1724 hammer_rel_mem_record(tmprec);
1729 hammer_rel_mem_record(tmprec);
1732 * Extract either the disk or memory record depending on their
1733 * relative position.
1736 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1739 * Both entries valid. Compare the entries and nominally
1740 * return the first one in the sort order. Numerous cases
1741 * require special attention, however.
1743 elm = &cursor->node->ondisk->elms[cursor->index];
1744 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1747 * If the two entries differ only by their key (-2/2) or
1748 * create_tid (-1/1), and are DATA records, we may have a
1749 * nominal match. We have to calculate the base file
1750 * offset of the data.
1752 if (r <= 2 && r >= -2 && r != 0 &&
1753 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1754 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1755 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1756 int64_t base2 = cursor->iprec->leaf.base.key -
1757 cursor->iprec->leaf.data_len;
1763 error = hammer_btree_extract(cursor,
1764 HAMMER_CURSOR_GET_LEAF);
1765 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1766 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1771 * If the entries match exactly the memory entry is either
1772 * an on-disk directory entry deletion or a bulk data
1773 * overwrite. If it is a directory entry deletion we eat
1776 * For the bulk-data overwrite case it is possible to have
1777 * visibility into both, which simply means the syncer
1778 * hasn't gotten around to doing the delete+insert sequence
1779 * on the B-Tree. Use the memory entry and throw away the
1782 * If the in-memory record is not either of these we
1783 * probably caught the syncer while it was syncing it to
1784 * the media. Since we hold a shared lock on the cursor,
1785 * the in-memory record had better be marked deleted at
1789 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1790 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1791 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1792 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1795 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1796 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1797 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1799 /* fall through to memory entry */
1801 panic("hammer_ip_next: duplicate mem/b-tree entry %p %d %08x", cursor->iprec, cursor->iprec->type, cursor->iprec->flags);
1802 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1806 /* fall through to the memory entry */
1807 case HAMMER_CURSOR_ATEDISK:
1809 * Only the memory entry is valid.
1811 cursor->leaf = &cursor->iprec->leaf;
1812 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1813 cursor->flags |= HAMMER_CURSOR_LASTWASMEM;
1816 * If the memory entry is an on-disk deletion we should have
1817 * also had found a B-Tree record. If the backend beat us
1818 * to it it would have interlocked the cursor and we should
1819 * have seen the in-memory record marked DELETED_FE.
1821 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1822 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1823 panic("hammer_ip_next: del-on-disk with no b-tree entry iprec %p flags %08x", cursor->iprec, cursor->iprec->flags);
1826 case HAMMER_CURSOR_ATEMEM:
1828 * Only the disk entry is valid
1830 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1831 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1832 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1836 * Neither entry is valid
1838 * XXX error not set properly
1840 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
1841 cursor->leaf = NULL;
1849 * Resolve the cursor->data pointer for the current cursor position in
1850 * a merged iteration.
1853 hammer_ip_resolve_data(hammer_cursor_t cursor)
1855 hammer_record_t record;
1858 if (hammer_cursor_inmem(cursor)) {
1860 * The data associated with an in-memory record is usually
1861 * kmalloced, but reserve-ahead data records will have an
1862 * on-disk reference.
1864 * NOTE: Reserve-ahead data records must be handled in the
1865 * context of the related high level buffer cache buffer
1866 * to interlock against async writes.
1868 record = cursor->iprec;
1869 cursor->data = record->data;
1871 if (cursor->data == NULL) {
1872 KKASSERT(record->leaf.base.rec_type ==
1873 HAMMER_RECTYPE_DATA);
1874 cursor->data = hammer_bread_ext(cursor->trans->hmp,
1875 record->leaf.data_offset,
1876 record->leaf.data_len,
1878 &cursor->data_buffer);
1881 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
1882 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1888 * Backend truncation / record replacement - delete records in range.
1890 * Delete all records within the specified range for inode ip. In-memory
1891 * records still associated with the frontend are ignored.
1893 * If truncating is non-zero in-memory records associated with the back-end
1894 * are ignored. If truncating is > 1 we can return EWOULDBLOCK.
1898 * * An unaligned range will cause new records to be added to cover
1899 * the edge cases. (XXX not implemented yet).
1901 * * Replacement via reservations (see hammer_ip_sync_record_cursor())
1902 * also do not deal with unaligned ranges.
1904 * * ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1906 * * Record keys for regular file data have to be special-cased since
1907 * they indicate the end of the range (key = base + bytes).
1909 * * This function may be asked to delete ridiculously huge ranges, for
1910 * example if someone truncates or removes a 1TB regular file. We
1911 * must be very careful on restarts and we may have to stop w/
1912 * EWOULDBLOCK to avoid blowing out the buffer cache.
1915 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1916 int64_t ran_beg, int64_t ran_end, int truncating)
1918 hammer_transaction_t trans = cursor->trans;
1919 hammer_btree_leaf_elm_t leaf;
1925 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1928 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1930 hammer_normalize_cursor(cursor);
1931 cursor->key_beg.localization = ip->obj_localization +
1932 HAMMER_LOCALIZE_MISC;
1933 cursor->key_beg.obj_id = ip->obj_id;
1934 cursor->key_beg.create_tid = 0;
1935 cursor->key_beg.delete_tid = 0;
1936 cursor->key_beg.obj_type = 0;
1938 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1939 cursor->key_beg.key = ran_beg;
1940 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1943 * The key in the B-Tree is (base+bytes), so the first possible
1944 * matching key is ran_beg + 1.
1946 cursor->key_beg.key = ran_beg + 1;
1947 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1950 cursor->key_end = cursor->key_beg;
1951 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1952 cursor->key_end.key = ran_end;
1954 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1955 if (tmp64 < ran_end)
1956 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1958 cursor->key_end.key = ran_end + MAXPHYS + 1;
1961 cursor->asof = ip->obj_asof;
1962 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1963 cursor->flags |= HAMMER_CURSOR_ASOF;
1964 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1965 cursor->flags |= HAMMER_CURSOR_BACKEND;
1966 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
1968 error = hammer_ip_first(cursor);
1971 * Iterate through matching records and mark them as deleted.
1973 while (error == 0) {
1974 leaf = cursor->leaf;
1976 KKASSERT(leaf->base.delete_tid == 0);
1977 KKASSERT(leaf->base.obj_id == ip->obj_id);
1980 * There may be overlap cases for regular file data. Also
1981 * remember the key for a regular file record is (base + len),
1984 * Note that due to duplicates (mem & media) allowed by
1985 * DELETE_VISIBILITY, off can wind up less then ran_beg.
1987 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
1988 off = leaf->base.key - leaf->data_len;
1990 * Check the left edge case. We currently do not
1991 * split existing records.
1993 if (off < ran_beg && leaf->base.key > ran_beg) {
1994 panic("hammer left edge case %016llx %d\n",
1995 (long long)leaf->base.key,
2000 * Check the right edge case. Note that the
2001 * record can be completely out of bounds, which
2002 * terminates the search.
2004 * base->key is exclusive of the right edge while
2005 * ran_end is inclusive of the right edge. The
2006 * (key - data_len) left boundary is inclusive.
2008 * XXX theory-check this test at some point, are
2009 * we missing a + 1 somewhere? Note that ran_end
2012 if (leaf->base.key - 1 > ran_end) {
2013 if (leaf->base.key - leaf->data_len > ran_end)
2015 panic("hammer right edge case\n");
2018 off = leaf->base.key;
2022 * Delete the record. When truncating we do not delete
2023 * in-memory (data) records because they represent data
2024 * written after the truncation.
2026 * This will also physically destroy the B-Tree entry and
2027 * data if the retention policy dictates. The function
2028 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
2029 * to retest the new 'current' element.
2031 if (truncating == 0 || hammer_cursor_ondisk(cursor)) {
2032 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2034 * If we have built up too many meta-buffers we risk
2035 * deadlocking the kernel and must stop. This can
2036 * occur when deleting ridiculously huge files.
2037 * sync_trunc_off is updated so the next cycle does
2038 * not re-iterate records we have already deleted.
2040 * This is only done with formal truncations.
2042 if (truncating > 1 && error == 0 &&
2043 hammer_flusher_meta_limit(ip->hmp)) {
2044 ip->sync_trunc_off = off;
2045 error = EWOULDBLOCK;
2050 ran_beg = off; /* for restart */
2051 error = hammer_ip_next(cursor);
2054 hammer_cache_node(&ip->cache[1], cursor->node);
2056 if (error == EDEADLK) {
2057 hammer_done_cursor(cursor);
2058 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2062 if (error == ENOENT)
2068 * This backend function deletes the specified record on-disk, similar to
2069 * delete_range but for a specific record. Unlike the exact deletions
2070 * used when deleting a directory entry this function uses an ASOF search
2071 * like delete_range.
2073 * This function may be called with ip->obj_asof set for a slave snapshot,
2074 * so don't use it. We always delete non-historical records only.
2077 hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
2078 hammer_btree_leaf_elm_t leaf)
2080 hammer_transaction_t trans = cursor->trans;
2083 KKASSERT(trans->type == HAMMER_TRANS_FLS);
2085 hammer_normalize_cursor(cursor);
2086 cursor->key_beg = leaf->base;
2087 cursor->asof = HAMMER_MAX_TID;
2088 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2089 cursor->flags |= HAMMER_CURSOR_ASOF;
2090 cursor->flags |= HAMMER_CURSOR_BACKEND;
2091 cursor->flags &= ~HAMMER_CURSOR_INSERT;
2093 error = hammer_btree_lookup(cursor);
2095 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2097 if (error == EDEADLK) {
2098 hammer_done_cursor(cursor);
2099 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2107 * This function deletes remaining auxillary records when an inode is
2108 * being deleted. This function explicitly does not delete the
2109 * inode record, directory entry, data, or db records. Those must be
2110 * properly disposed of prior to this call.
2113 hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip, int *countp)
2115 hammer_transaction_t trans = cursor->trans;
2116 hammer_btree_leaf_elm_t leaf;
2119 KKASSERT(trans->type == HAMMER_TRANS_FLS);
2121 hammer_normalize_cursor(cursor);
2122 cursor->key_beg.localization = ip->obj_localization +
2123 HAMMER_LOCALIZE_MISC;
2124 cursor->key_beg.obj_id = ip->obj_id;
2125 cursor->key_beg.create_tid = 0;
2126 cursor->key_beg.delete_tid = 0;
2127 cursor->key_beg.obj_type = 0;
2128 cursor->key_beg.rec_type = HAMMER_RECTYPE_CLEAN_START;
2129 cursor->key_beg.key = HAMMER_MIN_KEY;
2131 cursor->key_end = cursor->key_beg;
2132 cursor->key_end.rec_type = HAMMER_RECTYPE_MAX;
2133 cursor->key_end.key = HAMMER_MAX_KEY;
2135 cursor->asof = ip->obj_asof;
2136 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2137 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2138 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
2139 cursor->flags |= HAMMER_CURSOR_BACKEND;
2141 error = hammer_ip_first(cursor);
2144 * Iterate through matching records and mark them as deleted.
2146 while (error == 0) {
2147 leaf = cursor->leaf;
2149 KKASSERT(leaf->base.delete_tid == 0);
2152 * Mark the record and B-Tree entry as deleted. This will
2153 * also physically delete the B-Tree entry, record, and
2154 * data if the retention policy dictates. The function
2155 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
2156 * to retest the new 'current' element.
2158 * Directory entries (and delete-on-disk directory entries)
2159 * must be synced and cannot be deleted.
2161 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2165 error = hammer_ip_next(cursor);
2168 hammer_cache_node(&ip->cache[1], cursor->node);
2169 if (error == EDEADLK) {
2170 hammer_done_cursor(cursor);
2171 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2175 if (error == ENOENT)
2181 * Delete the record at the current cursor. On success the cursor will
2182 * be positioned appropriately for an iteration but may no longer be at
2185 * This routine is only called from the backend.
2187 * NOTE: This can return EDEADLK, requiring the caller to terminate the
2191 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
2194 hammer_record_t iprec;
2198 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
2200 hmp = cursor->node->hmp;
2203 * In-memory (unsynchronized) records can simply be freed. This
2204 * only occurs in range iterations since all other records are
2205 * individually synchronized. Thus there should be no confusion with
2208 * An in-memory record may be deleted before being committed to disk,
2209 * but could have been accessed in the mean time. The reservation
2210 * code will deal with the case.
2212 if (hammer_cursor_inmem(cursor)) {
2213 iprec = cursor->iprec;
2214 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
2215 iprec->flags |= HAMMER_RECF_DELETED_FE;
2216 iprec->flags |= HAMMER_RECF_DELETED_BE;
2217 KKASSERT(iprec->ip == ip);
2218 ++ip->rec_generation;
2223 * On-disk records are marked as deleted by updating their delete_tid.
2224 * This does not effect their position in the B-Tree (which is based
2225 * on their create_tid).
2227 * Frontend B-Tree operations track inodes so we tell
2228 * hammer_delete_at_cursor() not to.
2230 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
2233 error = hammer_delete_at_cursor(
2235 HAMMER_DELETE_ADJUST | hammer_nohistory(ip),
2237 cursor->trans->time32,
2244 * Used to write a generic record w/optional data to the media b-tree
2245 * when no inode context is available. Used by the mirroring and
2248 * Caller must set cursor->key_beg to leaf->base. The cursor must be
2249 * flagged for backend operation and not flagged ASOF (since we are
2250 * doing an insertion).
2252 * This function will acquire the appropriate sync lock and will set
2253 * the cursor insertion flag for the operation, do the btree lookup,
2254 * and the insertion, and clear the insertion flag and sync lock before
2255 * returning. The cursor state will be such that the caller can continue
2256 * scanning (used by the mirroring code).
2258 * mode: HAMMER_CREATE_MODE_UMIRROR copyin data, check crc
2259 * HAMMER_CREATE_MODE_SYS bcopy data, generate crc
2261 * NOTE: EDEADLK can be returned. The caller must do deadlock handling and
2264 * EALREADY can be returned if the record already exists (WARNING,
2265 * because ASOF cannot be used no check is made for illegal
2268 * NOTE: Do not use the function for normal inode-related records as this
2269 * functions goes directly to the media and is not integrated with
2270 * in-memory records.
2273 hammer_create_at_cursor(hammer_cursor_t cursor, hammer_btree_leaf_elm_t leaf,
2274 void *udata, int mode)
2276 hammer_transaction_t trans;
2277 hammer_buffer_t data_buffer;
2278 hammer_off_t ndata_offset;
2279 hammer_tid_t high_tid;
2284 trans = cursor->trans;
2289 KKASSERT((cursor->flags &
2290 (HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF)) ==
2291 (HAMMER_CURSOR_BACKEND));
2293 hammer_sync_lock_sh(trans);
2295 if (leaf->data_len) {
2296 ndata = hammer_alloc_data(trans, leaf->data_len,
2297 leaf->base.rec_type,
2298 &ndata_offset, &data_buffer,
2300 if (ndata == NULL) {
2301 hammer_sync_unlock(trans);
2304 leaf->data_offset = ndata_offset;
2305 hammer_modify_buffer(trans, data_buffer, NULL, 0);
2308 case HAMMER_CREATE_MODE_UMIRROR:
2309 error = copyin(udata, ndata, leaf->data_len);
2311 if (hammer_crc_test_leaf(ndata, leaf) == 0) {
2312 kprintf("data crc mismatch on pipe\n");
2315 error = hammer_cursor_localize_data(
2320 case HAMMER_CREATE_MODE_SYS:
2321 bcopy(udata, ndata, leaf->data_len);
2323 hammer_crc_set_leaf(ndata, leaf);
2326 panic("hammer: hammer_create_at_cursor: bad mode %d",
2328 break; /* NOT REACHED */
2330 hammer_modify_buffer_done(data_buffer);
2332 leaf->data_offset = 0;
2340 * Do the insertion. This can fail with a EDEADLK or EALREADY
2342 cursor->flags |= HAMMER_CURSOR_INSERT;
2343 error = hammer_btree_lookup(cursor);
2344 if (error != ENOENT) {
2349 error = hammer_btree_insert(cursor, leaf, &doprop);
2352 * Cursor is left on current element, we want to skip it now.
2353 * (in case the caller is scanning)
2355 cursor->flags |= HAMMER_CURSOR_ATEDISK;
2356 cursor->flags &= ~HAMMER_CURSOR_INSERT;
2359 * If the insertion happens to be creating (and not just replacing)
2360 * an inode we have to track it.
2363 leaf->base.rec_type == HAMMER_RECTYPE_INODE &&
2364 leaf->base.delete_tid == 0) {
2365 hammer_modify_volume_field(trans, trans->rootvol,
2367 ++trans->hmp->rootvol->ondisk->vol0_stat_inodes;
2368 hammer_modify_volume_done(trans->rootvol);
2372 * vol0_next_tid must track the highest TID stored in the filesystem.
2373 * We do not need to generate undo for this update.
2375 high_tid = leaf->base.create_tid;
2376 if (high_tid < leaf->base.delete_tid)
2377 high_tid = leaf->base.delete_tid;
2378 if (trans->rootvol->ondisk->vol0_next_tid < high_tid) {
2379 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
2380 trans->rootvol->ondisk->vol0_next_tid = high_tid;
2381 hammer_modify_volume_done(trans->rootvol);
2385 * WARNING! cursor's leaf pointer may have changed after
2386 * do_propagation returns.
2388 if (error == 0 && doprop)
2389 hammer_btree_do_propagation(cursor, NULL, leaf);
2395 if (error && leaf->data_offset) {
2396 hammer_blockmap_free(trans, leaf->data_offset, leaf->data_len);
2399 hammer_sync_unlock(trans);
2401 hammer_rel_buffer(data_buffer, 0);
2406 * Delete the B-Tree element at the current cursor and do any necessary
2407 * mirror propagation.
2409 * The cursor must be properly positioned for an iteration on return but
2410 * may be pointing at an internal element.
2412 * An element can be un-deleted by passing a delete_tid of 0 with
2413 * HAMMER_DELETE_ADJUST.
2416 hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
2417 hammer_tid_t delete_tid, u_int32_t delete_ts,
2418 int track, int64_t *stat_bytes)
2420 struct hammer_btree_leaf_elm save_leaf;
2421 hammer_transaction_t trans;
2422 hammer_btree_leaf_elm_t leaf;
2424 hammer_btree_elm_t elm;
2425 hammer_off_t data_offset;
2432 error = hammer_cursor_upgrade(cursor);
2436 trans = cursor->trans;
2437 node = cursor->node;
2438 elm = &node->ondisk->elms[cursor->index];
2440 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
2442 hammer_sync_lock_sh(trans);
2447 * Adjust the delete_tid. Update the mirror_tid propagation field
2448 * as well. delete_tid can be 0 (undelete -- used by mirroring).
2450 if (delete_flags & HAMMER_DELETE_ADJUST) {
2451 if (elm->base.rec_type == HAMMER_RECTYPE_INODE) {
2452 if (elm->leaf.base.delete_tid == 0 && delete_tid)
2454 if (elm->leaf.base.delete_tid && delete_tid == 0)
2458 hammer_modify_node(trans, node, elm, sizeof(*elm));
2459 elm->leaf.base.delete_tid = delete_tid;
2460 elm->leaf.delete_ts = delete_ts;
2461 hammer_modify_node_done(node);
2463 if (elm->leaf.base.delete_tid > node->ondisk->mirror_tid) {
2464 hammer_modify_node_field(trans, node, mirror_tid);
2465 node->ondisk->mirror_tid = elm->leaf.base.delete_tid;
2466 hammer_modify_node_done(node);
2468 if (hammer_debug_general & 0x0002) {
2469 kprintf("delete_at_cursor: propagate %016llx"
2471 (long long)elm->leaf.base.delete_tid,
2472 (long long)node->node_offset);
2477 * Adjust for the iteration. We have deleted the current
2478 * element and want to clear ATEDISK so the iteration does
2479 * not skip the element after, which now becomes the current
2480 * element. This element must be re-tested if doing an
2481 * iteration, which is handled by the RETEST flag.
2483 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2484 cursor->flags |= HAMMER_CURSOR_RETEST;
2485 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2489 * An on-disk record cannot have the same delete_tid
2490 * as its create_tid. In a chain of record updates
2491 * this could result in a duplicate record.
2493 KKASSERT(elm->leaf.base.delete_tid !=
2494 elm->leaf.base.create_tid);
2498 * Destroy the B-Tree element if asked (typically if a nohistory
2499 * file or mount, or when called by the pruning code).
2501 * Adjust the ATEDISK flag to properly support iterations.
2503 if (delete_flags & HAMMER_DELETE_DESTROY) {
2504 data_offset = elm->leaf.data_offset;
2505 data_len = elm->leaf.data_len;
2506 rec_type = elm->leaf.base.rec_type;
2508 save_leaf = elm->leaf;
2511 if (elm->base.rec_type == HAMMER_RECTYPE_INODE &&
2512 elm->leaf.base.delete_tid == 0) {
2516 error = hammer_btree_delete(cursor);
2519 * The deletion moves the next element (if any) to
2520 * the current element position. We must clear
2521 * ATEDISK so this element is not skipped and we
2522 * must set RETEST to force any iteration to re-test
2525 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2526 cursor->flags |= HAMMER_CURSOR_RETEST;
2527 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2531 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
2532 case HAMMER_ZONE_LARGE_DATA:
2533 case HAMMER_ZONE_SMALL_DATA:
2534 case HAMMER_ZONE_META:
2535 hammer_blockmap_free(trans,
2536 data_offset, data_len);
2545 * Track inode count and next_tid. This is used by the mirroring
2546 * and PFS code. icount can be negative, zero, or positive.
2548 if (error == 0 && track) {
2550 hammer_modify_volume_field(trans, trans->rootvol,
2552 trans->rootvol->ondisk->vol0_stat_inodes += icount;
2553 hammer_modify_volume_done(trans->rootvol);
2555 if (trans->rootvol->ondisk->vol0_next_tid < delete_tid) {
2556 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
2557 trans->rootvol->ondisk->vol0_next_tid = delete_tid;
2558 hammer_modify_volume_done(trans->rootvol);
2563 * mirror_tid propagation occurs if the node's mirror_tid had to be
2564 * updated while adjusting the delete_tid.
2566 * This occurs when deleting even in nohistory mode, but does not
2567 * occur when pruning an already-deleted node.
2569 * cursor->ip is NULL when called from the pruning, mirroring,
2570 * and pfs code. If non-NULL propagation will be conditionalized
2571 * on whether the PFS is in no-history mode or not.
2573 * WARNING: cursor's leaf pointer may have changed after do_propagation
2578 hammer_btree_do_propagation(cursor, cursor->ip->pfsm, leaf);
2580 hammer_btree_do_propagation(cursor, NULL, leaf);
2582 hammer_sync_unlock(trans);
2587 * Determine whether we can remove a directory. This routine checks whether
2588 * a directory is empty or not and enforces flush connectivity.
2590 * Flush connectivity requires that we block if the target directory is
2591 * currently flushing, otherwise it may not end up in the same flush group.
2593 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
2596 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
2598 struct hammer_cursor cursor;
2602 * Check directory empty
2604 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2606 cursor.key_beg.localization = ip->obj_localization +
2607 hammer_dir_localization(ip);
2608 cursor.key_beg.obj_id = ip->obj_id;
2609 cursor.key_beg.create_tid = 0;
2610 cursor.key_beg.delete_tid = 0;
2611 cursor.key_beg.obj_type = 0;
2612 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
2613 cursor.key_beg.key = HAMMER_MIN_KEY;
2615 cursor.key_end = cursor.key_beg;
2616 cursor.key_end.rec_type = 0xFFFF;
2617 cursor.key_end.key = HAMMER_MAX_KEY;
2619 cursor.asof = ip->obj_asof;
2620 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2622 error = hammer_ip_first(&cursor);
2623 if (error == ENOENT)
2625 else if (error == 0)
2627 hammer_done_cursor(&cursor);
2632 * Localize the data payload. Directory entries may need their
2633 * localization adjusted.
2637 hammer_cursor_localize_data(hammer_data_ondisk_t data,
2638 hammer_btree_leaf_elm_t leaf)
2640 u_int32_t localization;
2642 if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
2643 localization = leaf->base.localization &
2644 HAMMER_LOCALIZE_PSEUDOFS_MASK;
2645 if (data->entry.localization != localization) {
2646 data->entry.localization = localization;
2647 hammer_crc_set_leaf(data, leaf);