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 int 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);
48 struct rec_trunc_info {
53 struct hammer_bulk_info {
54 hammer_record_t record;
55 struct hammer_btree_leaf_elm leaf;
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 * Never match against an item deleted by the front-end.
77 * rec1 is greater then rec2 if rec1 is marked deleted.
78 * rec1 is less then rec2 if rec2 is marked deleted.
80 * Multiple deleted records may be present, do not return 0
81 * if both are marked deleted.
83 if (rec1->flags & HAMMER_RECF_DELETED_FE)
85 if (rec2->flags & HAMMER_RECF_DELETED_FE)
92 * Basic record comparison code similar to hammer_btree_cmp().
95 hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
97 if (elm->rec_type < rec->leaf.base.rec_type)
99 if (elm->rec_type > rec->leaf.base.rec_type)
102 if (elm->key < rec->leaf.base.key)
104 if (elm->key > rec->leaf.base.key)
108 * Never match against an item deleted by the front-end.
109 * elm is less then rec if rec is marked deleted.
111 if (rec->flags & HAMMER_RECF_DELETED_FE)
117 * Ranged scan to locate overlapping record(s). This is used by
118 * hammer_ip_get_bulk() to locate an overlapping record. We have
119 * to use a ranged scan because the keys for data records with the
120 * same file base offset can be different due to differing data_len's.
122 * NOTE: The base file offset of a data record is (key - data_len), not (key).
125 hammer_rec_overlap_cmp(hammer_record_t rec, void *data)
127 struct hammer_bulk_info *info = data;
128 hammer_btree_leaf_elm_t leaf = &info->leaf;
130 if (rec->leaf.base.rec_type < leaf->base.rec_type)
132 if (rec->leaf.base.rec_type > leaf->base.rec_type)
138 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
139 /* rec_beg >= leaf_end */
140 if (rec->leaf.base.key - rec->leaf.data_len >= leaf->base.key)
142 /* rec_end <= leaf_beg */
143 if (rec->leaf.base.key <= leaf->base.key - leaf->data_len)
146 if (rec->leaf.base.key < leaf->base.key)
148 if (rec->leaf.base.key > leaf->base.key)
153 * We have to return 0 at this point, even if DELETED_FE is set,
154 * because returning anything else will cause the scan to ignore
155 * one of the branches when we really want it to check both.
161 * RB_SCAN comparison code for hammer_mem_first(). The argument order
162 * is reversed so the comparison result has to be negated. key_beg and
163 * key_end are both range-inclusive.
165 * Localized deletions are not cached in-memory.
169 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
171 hammer_cursor_t cursor = data;
174 r = hammer_rec_cmp(&cursor->key_beg, rec);
177 r = hammer_rec_cmp(&cursor->key_end, rec);
184 * This compare function is used when simply looking up key_beg.
188 hammer_rec_find_cmp(hammer_record_t rec, void *data)
190 hammer_cursor_t cursor = data;
193 r = hammer_rec_cmp(&cursor->key_beg, rec);
202 * Locate blocks within the truncation range. Partial blocks do not count.
206 hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
208 struct rec_trunc_info *info = data;
210 if (rec->leaf.base.rec_type < info->rec_type)
212 if (rec->leaf.base.rec_type > info->rec_type)
215 switch(rec->leaf.base.rec_type) {
216 case HAMMER_RECTYPE_DB:
218 * DB record key is not beyond the truncation point, retain.
220 if (rec->leaf.base.key < info->trunc_off)
223 case HAMMER_RECTYPE_DATA:
225 * DATA record offset start is not beyond the truncation point,
228 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
232 panic("hammer_rec_trunc_cmp: unexpected record type");
236 * The record start is >= the truncation point, return match,
237 * the record should be destroyed.
242 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
245 * Allocate a record for the caller to finish filling in. The record is
246 * returned referenced.
249 hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
251 hammer_record_t record;
255 ++hammer_count_records;
256 record = kmalloc(sizeof(*record), hmp->m_misc,
257 M_WAITOK | M_ZERO | M_USE_RESERVE);
258 record->flush_state = HAMMER_FST_IDLE;
260 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
261 record->leaf.data_len = data_len;
262 hammer_ref(&record->lock);
265 record->data = kmalloc(data_len, hmp->m_misc, M_WAITOK | M_ZERO);
266 record->flags |= HAMMER_RECF_ALLOCDATA;
267 ++hammer_count_record_datas;
274 hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
276 while (record->flush_state == HAMMER_FST_FLUSH) {
277 record->flags |= HAMMER_RECF_WANTED;
278 tsleep(record, 0, ident, 0);
283 * Called from the backend, hammer_inode.c, after a record has been
284 * flushed to disk. The record has been exclusively locked by the
285 * caller and interlocked with BE.
287 * We clean up the state, unlock, and release the record (the record
288 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
291 hammer_flush_record_done(hammer_record_t record, int error)
293 hammer_inode_t target_ip;
295 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
296 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
300 * An error occured, the backend was unable to sync the
301 * record to its media. Leave the record intact.
303 hammer_critical_error(record->ip->hmp, record->ip, error,
304 "while flushing record");
307 --record->flush_group->refs;
308 record->flush_group = NULL;
310 if (record->flags & HAMMER_RECF_DELETED_BE) {
311 if ((target_ip = record->target_ip) != NULL) {
312 TAILQ_REMOVE(&target_ip->target_list, record,
314 record->target_ip = NULL;
315 hammer_test_inode(target_ip);
317 record->flush_state = HAMMER_FST_IDLE;
319 if (record->target_ip) {
320 record->flush_state = HAMMER_FST_SETUP;
321 hammer_test_inode(record->ip);
322 hammer_test_inode(record->target_ip);
324 record->flush_state = HAMMER_FST_IDLE;
327 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
328 if (record->flags & HAMMER_RECF_WANTED) {
329 record->flags &= ~HAMMER_RECF_WANTED;
332 hammer_rel_mem_record(record);
336 * Release a memory record. Records marked for deletion are immediately
337 * removed from the RB-Tree but otherwise left intact until the last ref
341 hammer_rel_mem_record(struct hammer_record *record)
344 hammer_reserve_t resv;
346 hammer_inode_t target_ip;
348 hammer_unref(&record->lock);
350 if (record->lock.refs == 0) {
352 * Upon release of the last reference wakeup any waiters.
353 * The record structure may get destroyed so callers will
354 * loop up and do a relookup.
356 * WARNING! Record must be removed from RB-TREE before we
357 * might possibly block. hammer_test_inode() can block!
363 * Upon release of the last reference a record marked deleted
366 if (record->flags & HAMMER_RECF_DELETED_FE) {
367 KKASSERT(ip->lock.refs > 0);
368 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
371 * target_ip may have zero refs, we have to ref it
372 * to prevent it from being ripped out from under
375 if ((target_ip = record->target_ip) != NULL) {
376 TAILQ_REMOVE(&target_ip->target_list,
377 record, target_entry);
378 record->target_ip = NULL;
379 hammer_ref(&target_ip->lock);
382 if (record->flags & HAMMER_RECF_ONRBTREE) {
383 RB_REMOVE(hammer_rec_rb_tree,
384 &record->ip->rec_tree,
386 KKASSERT(ip->rsv_recs > 0);
389 hmp->rsv_databytes -= record->leaf.data_len;
390 record->flags &= ~HAMMER_RECF_ONRBTREE;
392 if (RB_EMPTY(&record->ip->rec_tree)) {
393 record->ip->flags &= ~HAMMER_INODE_XDIRTY;
394 record->ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
395 hammer_test_inode(record->ip);
400 * We must wait for any direct-IO to complete before
401 * we can destroy the record because the bio may
402 * have a reference to it.
405 (HAMMER_RECF_DIRECT_IO | HAMMER_RECF_DIRECT_INVAL)) {
406 hammer_io_direct_wait(record);
411 * Do this test after removing record from the B-Tree.
414 hammer_test_inode(target_ip);
415 hammer_rel_inode(target_ip, 0);
418 if (record->flags & HAMMER_RECF_ALLOCDATA) {
419 --hammer_count_record_datas;
420 kfree(record->data, hmp->m_misc);
421 record->flags &= ~HAMMER_RECF_ALLOCDATA;
425 * Release the reservation. If the record was not
426 * committed return the reservation before
429 if ((resv = record->resv) != NULL) {
431 if ((record->flags & HAMMER_RECF_COMMITTED) == 0) {
432 hammer_blockmap_reserve_undo(
434 record->leaf.data_offset,
435 record->leaf.data_len);
438 hammer_blockmap_reserve_complete(hmp, resv);
442 --hammer_count_records;
443 kfree(record, hmp->m_misc);
449 * Record visibility depends on whether the record is being accessed by
450 * the backend or the frontend.
452 * Return non-zero if the record is visible, zero if it isn't or if it is
455 * If HAMMER_CURSOR_DELETE_VISIBILITY is set we allow deleted memory
456 * records to be returned. This is so pending deletions are detected
457 * when using an iterator to locate an unused hash key, or when we need
458 * to locate historical records on-disk to destroy.
462 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
464 if (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY)
466 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
467 if (record->flags & HAMMER_RECF_DELETED_BE)
470 if (record->flags & HAMMER_RECF_DELETED_FE)
477 * This callback is used as part of the RB_SCAN function for in-memory
478 * records. We terminate it (return -1) as soon as we get a match.
480 * This routine is used by frontend code.
482 * The primary compare code does not account for ASOF lookups. This
483 * code handles that case as well as a few others.
487 hammer_rec_scan_callback(hammer_record_t rec, void *data)
489 hammer_cursor_t cursor = data;
492 * We terminate on success, so this should be NULL on entry.
494 KKASSERT(cursor->iprec == NULL);
497 * Skip if the record was marked deleted.
499 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
503 * Skip if not visible due to our as-of TID
505 if (cursor->flags & HAMMER_CURSOR_ASOF) {
506 if (cursor->asof < rec->leaf.base.create_tid)
508 if (rec->leaf.base.delete_tid &&
509 cursor->asof >= rec->leaf.base.delete_tid) {
515 * ref the record. The record is protected from backend B-Tree
516 * interactions by virtue of the cursor's IP lock.
518 hammer_ref(&rec->lock);
521 * The record may have been deleted while we were blocked.
523 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
524 hammer_rel_mem_record(rec);
529 * Set the matching record and stop the scan.
537 * Lookup an in-memory record given the key specified in the cursor. Works
538 * just like hammer_btree_lookup() but operates on an inode's in-memory
541 * The lookup must fail if the record is marked for deferred deletion.
545 hammer_mem_lookup(hammer_cursor_t cursor)
549 KKASSERT(cursor->ip);
551 hammer_rel_mem_record(cursor->iprec);
552 cursor->iprec = NULL;
554 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
555 hammer_rec_scan_callback, cursor);
557 if (cursor->iprec == NULL)
565 * hammer_mem_first() - locate the first in-memory record matching the
566 * cursor within the bounds of the key range.
570 hammer_mem_first(hammer_cursor_t cursor)
575 KKASSERT(ip != NULL);
578 hammer_rel_mem_record(cursor->iprec);
579 cursor->iprec = NULL;
582 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
583 hammer_rec_scan_callback, cursor);
586 * Adjust scan.node and keep it linked into the RB-tree so we can
587 * hold the cursor through third party modifications of the RB-tree.
594 /************************************************************************
595 * HAMMER IN-MEMORY RECORD FUNCTIONS *
596 ************************************************************************
598 * These functions manipulate in-memory records. Such records typically
599 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
603 * Add a directory entry (dip,ncp) which references inode (ip).
605 * Note that the low 32 bits of the namekey are set temporarily to create
606 * a unique in-memory record, and may be modified a second time when the
607 * record is synchronized to disk. In particular, the low 32 bits cannot be
608 * all 0's when synching to disk, which is not handled here.
610 * NOTE: bytes does not include any terminating \0 on name, and name might
614 hammer_ip_add_directory(struct hammer_transaction *trans,
615 struct hammer_inode *dip, const char *name, int bytes,
616 struct hammer_inode *ip)
618 struct hammer_cursor cursor;
619 hammer_record_t record;
621 u_int32_t max_iterations;
623 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
625 record->type = HAMMER_MEM_RECORD_ADD;
626 record->leaf.base.localization = dip->obj_localization +
627 HAMMER_LOCALIZE_MISC;
628 record->leaf.base.obj_id = dip->obj_id;
629 record->leaf.base.key = hammer_directory_namekey(dip, name, bytes,
631 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
632 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
633 record->data->entry.obj_id = ip->obj_id;
634 record->data->entry.localization = ip->obj_localization;
635 bcopy(name, record->data->entry.name, bytes);
637 ++ip->ino_data.nlinks;
638 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
641 * Find an unused namekey. Both the in-memory record tree and
642 * the B-Tree are checked. We do not want historically deleted
643 * names to create a collision as our iteration space may be limited,
644 * and since create_tid wouldn't match anyway an ASOF search
645 * must be used to locate collisions.
647 * delete-visibility is set so pending deletions do not give us
648 * a false-negative on our ability to use an iterator.
650 * The iterator must not rollover the key. Directory keys only
651 * use the positive key space.
653 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
654 cursor.key_beg = record->leaf.base;
655 cursor.flags |= HAMMER_CURSOR_ASOF;
656 cursor.flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
657 cursor.asof = ip->obj_asof;
659 while (hammer_ip_lookup(&cursor) == 0) {
660 ++record->leaf.base.key;
661 KKASSERT(record->leaf.base.key > 0);
662 cursor.key_beg.key = record->leaf.base.key;
663 if (--max_iterations == 0) {
664 hammer_rel_mem_record(record);
671 * The target inode and the directory entry are bound together.
673 record->target_ip = ip;
674 record->flush_state = HAMMER_FST_SETUP;
675 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
678 * The inode now has a dependancy and must be taken out of the idle
679 * state. An inode not in an idle state is given an extra reference.
681 * When transitioning to a SETUP state flag for an automatic reflush
682 * when the dependancies are disposed of if someone is waiting on
685 if (ip->flush_state == HAMMER_FST_IDLE) {
686 hammer_ref(&ip->lock);
687 ip->flush_state = HAMMER_FST_SETUP;
688 if (ip->flags & HAMMER_INODE_FLUSHW)
689 ip->flags |= HAMMER_INODE_REFLUSH;
691 error = hammer_mem_add(record);
693 dip->ino_data.mtime = trans->time;
694 hammer_modify_inode(dip, HAMMER_INODE_MTIME);
697 hammer_done_cursor(&cursor);
702 * Delete the directory entry and update the inode link count. The
703 * cursor must be seeked to the directory entry record being deleted.
705 * The related inode should be share-locked by the caller. The caller is
706 * on the frontend. It could also be NULL indicating that the directory
707 * entry being removed has no related inode.
709 * This function can return EDEADLK requiring the caller to terminate
710 * the cursor, any locks, wait on the returned record, and retry.
713 hammer_ip_del_directory(struct hammer_transaction *trans,
714 hammer_cursor_t cursor, struct hammer_inode *dip,
715 struct hammer_inode *ip)
717 hammer_record_t record;
720 if (hammer_cursor_inmem(cursor)) {
722 * In-memory (unsynchronized) records can simply be freed.
723 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
724 * by the backend, we must still avoid races against the
725 * backend potentially syncing the record to the media.
727 * We cannot call hammer_ip_delete_record(), that routine may
728 * only be called from the backend.
730 record = cursor->iprec;
731 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
732 KKASSERT(cursor->deadlk_rec == NULL);
733 hammer_ref(&record->lock);
734 cursor->deadlk_rec = record;
737 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
738 record->flags |= HAMMER_RECF_DELETED_FE;
743 * If the record is on-disk we have to queue the deletion by
744 * the record's key. This also causes lookups to skip the
747 KKASSERT(dip->flags &
748 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
749 record = hammer_alloc_mem_record(dip, 0);
750 record->type = HAMMER_MEM_RECORD_DEL;
751 record->leaf.base = cursor->leaf->base;
754 * ip may be NULL, indicating the deletion of a directory
755 * entry which has no related inode.
757 record->target_ip = ip;
759 record->flush_state = HAMMER_FST_SETUP;
760 TAILQ_INSERT_TAIL(&ip->target_list, record,
763 record->flush_state = HAMMER_FST_IDLE;
767 * The inode now has a dependancy and must be taken out of
768 * the idle state. An inode not in an idle state is given
769 * an extra reference.
771 * When transitioning to a SETUP state flag for an automatic
772 * reflush when the dependancies are disposed of if someone
773 * is waiting on the inode.
775 if (ip && ip->flush_state == HAMMER_FST_IDLE) {
776 hammer_ref(&ip->lock);
777 ip->flush_state = HAMMER_FST_SETUP;
778 if (ip->flags & HAMMER_INODE_FLUSHW)
779 ip->flags |= HAMMER_INODE_REFLUSH;
782 error = hammer_mem_add(record);
786 * One less link. The file may still be open in the OS even after
787 * all links have gone away.
789 * We have to terminate the cursor before syncing the inode to
790 * avoid deadlocking against ourselves. XXX this may no longer
793 * If nlinks drops to zero and the vnode is inactive (or there is
794 * no vnode), call hammer_inode_unloadable_check() to zonk the
795 * inode. If we don't do this here the inode will not be destroyed
796 * on-media until we unmount.
800 --ip->ino_data.nlinks; /* do before we might block */
801 dip->ino_data.mtime = trans->time;
802 hammer_modify_inode(dip, HAMMER_INODE_MTIME);
804 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
805 if (ip->ino_data.nlinks == 0 &&
806 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
807 hammer_done_cursor(cursor);
808 hammer_inode_unloadable_check(ip, 1);
809 hammer_flush_inode(ip, 0);
818 * Add a record to an inode.
820 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
821 * initialize the following additional fields:
823 * The related inode should be share-locked by the caller. The caller is
826 * record->rec.entry.base.base.key
827 * record->rec.entry.base.base.rec_type
828 * record->rec.entry.base.base.data_len
829 * record->data (a copy will be kmalloc'd if it cannot be embedded)
832 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
834 hammer_inode_t ip = record->ip;
837 KKASSERT(record->leaf.base.localization != 0);
838 record->leaf.base.obj_id = ip->obj_id;
839 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
840 error = hammer_mem_add(record);
845 * Locate a bulk record in-memory. Bulk records allow disk space to be
846 * reserved so the front-end can flush large data writes without having
847 * to queue the BIO to the flusher. Only the related record gets queued
851 static hammer_record_t
852 hammer_ip_get_bulk(hammer_inode_t ip, off_t file_offset, int bytes)
854 struct hammer_bulk_info info;
856 bzero(&info, sizeof(info));
857 info.leaf.base.obj_id = ip->obj_id;
858 info.leaf.base.key = file_offset + bytes;
859 info.leaf.base.create_tid = 0;
860 info.leaf.base.delete_tid = 0;
861 info.leaf.base.rec_type = HAMMER_RECTYPE_DATA;
862 info.leaf.base.obj_type = 0; /* unused */
863 info.leaf.base.btype = HAMMER_BTREE_TYPE_RECORD; /* unused */
864 info.leaf.base.localization = ip->obj_localization + /* unused */
865 HAMMER_LOCALIZE_MISC;
866 info.leaf.data_len = bytes;
868 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_overlap_cmp,
869 hammer_bulk_scan_callback, &info);
871 return(info.record); /* may be NULL */
875 * Take records vetted by overlap_cmp. The first non-deleted record
876 * (if any) stops the scan.
879 hammer_bulk_scan_callback(hammer_record_t record, void *data)
881 struct hammer_bulk_info *info = data;
883 if (record->flags & HAMMER_RECF_DELETED_FE)
885 hammer_ref(&record->lock);
886 info->record = record;
887 return(-1); /* stop scan */
891 * Reserve blockmap space placemarked with an in-memory record.
893 * This routine is called by the frontend in order to be able to directly
894 * flush a buffer cache buffer. The frontend has locked the related buffer
895 * cache buffers and we should be able to manipulate any overlapping
898 * The caller is responsible for adding the returned record.
901 hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
904 hammer_record_t record;
905 hammer_record_t conflict;
909 * Deal with conflicting in-memory records. We cannot have multiple
910 * in-memory records for the same base offset without seriously
911 * confusing the backend, including but not limited to the backend
912 * issuing delete-create-delete or create-delete-create sequences
913 * and asserting on the delete_tid being the same as the create_tid.
915 * If we encounter a record with the backend interlock set we cannot
916 * immediately delete it without confusing the backend.
918 while ((conflict = hammer_ip_get_bulk(ip, file_offset, bytes)) !=NULL) {
919 if (conflict->flags & HAMMER_RECF_INTERLOCK_BE) {
920 conflict->flags |= HAMMER_RECF_WANTED;
921 tsleep(conflict, 0, "hmrrc3", 0);
923 conflict->flags |= HAMMER_RECF_DELETED_FE;
925 hammer_rel_mem_record(conflict);
929 * Create a record to cover the direct write. This is called with
930 * the related BIO locked so there should be no possible conflict.
932 * The backend is responsible for finalizing the space reserved in
935 * XXX bytes not aligned, depend on the reservation code to
936 * align the reservation.
938 record = hammer_alloc_mem_record(ip, 0);
939 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
940 HAMMER_ZONE_SMALL_DATA_INDEX;
941 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
942 &record->leaf.data_offset,
944 if (record->resv == NULL) {
945 kprintf("hammer_ip_add_bulk: reservation failed\n");
946 hammer_rel_mem_record(record);
949 record->type = HAMMER_MEM_RECORD_DATA;
950 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
951 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
952 record->leaf.base.obj_id = ip->obj_id;
953 record->leaf.base.key = file_offset + bytes;
954 record->leaf.base.localization = ip->obj_localization +
955 HAMMER_LOCALIZE_MISC;
956 record->leaf.data_len = bytes;
957 hammer_crc_set_leaf(data, &record->leaf);
958 KKASSERT(*errorp == 0);
963 * Frontend truncation code. Scan in-memory records only. On-disk records
964 * and records in a flushing state are handled by the backend. The vnops
965 * setattr code will handle the block containing the truncation point.
967 * Partial blocks are not deleted.
970 hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
972 struct rec_trunc_info info;
974 switch(ip->ino_data.obj_type) {
975 case HAMMER_OBJTYPE_REGFILE:
976 info.rec_type = HAMMER_RECTYPE_DATA;
978 case HAMMER_OBJTYPE_DBFILE:
979 info.rec_type = HAMMER_RECTYPE_DB;
984 info.trunc_off = file_size;
985 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
986 hammer_frontend_trunc_callback, &info);
991 hammer_frontend_trunc_callback(hammer_record_t record, void *data __unused)
993 if (record->flags & HAMMER_RECF_DELETED_FE)
995 if (record->flush_state == HAMMER_FST_FLUSH)
997 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
998 hammer_ref(&record->lock);
999 record->flags |= HAMMER_RECF_DELETED_FE;
1000 hammer_rel_mem_record(record);
1005 * Return 1 if the caller must check for and delete existing records
1006 * before writing out a new data record.
1008 * Return 0 if the caller can just insert the record into the B-Tree without
1012 hammer_record_needs_overwrite_delete(hammer_record_t record)
1014 hammer_inode_t ip = record->ip;
1015 int64_t file_offset;
1018 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
1019 file_offset = record->leaf.base.key;
1021 file_offset = record->leaf.base.key - record->leaf.data_len;
1022 r = (file_offset < ip->save_trunc_off);
1023 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1024 if (ip->save_trunc_off <= record->leaf.base.key)
1025 ip->save_trunc_off = record->leaf.base.key + 1;
1027 if (ip->save_trunc_off < record->leaf.base.key)
1028 ip->save_trunc_off = record->leaf.base.key;
1034 * Backend code. Sync a record to the media.
1037 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
1039 hammer_transaction_t trans = cursor->trans;
1040 int64_t file_offset;
1046 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1047 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
1048 KKASSERT(record->leaf.base.localization != 0);
1051 * Any direct-write related to the record must complete before we
1052 * can sync the record to the on-disk media.
1054 if (record->flags & (HAMMER_RECF_DIRECT_IO | HAMMER_RECF_DIRECT_INVAL))
1055 hammer_io_direct_wait(record);
1058 * If this is a bulk-data record placemarker there may be an existing
1059 * record on-disk, indicating a data overwrite. If there is the
1060 * on-disk record must be deleted before we can insert our new record.
1062 * We've synthesized this record and do not know what the create_tid
1063 * on-disk is, nor how much data it represents.
1065 * Keep in mind that (key) for data records is (base_offset + len),
1066 * not (base_offset). Also, we only want to get rid of on-disk
1067 * records since we are trying to sync our in-memory record, call
1068 * hammer_ip_delete_range() with truncating set to 1 to make sure
1069 * it skips in-memory records.
1071 * It is ok for the lookup to return ENOENT.
1073 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1074 * to call hammer_ip_delete_range() or not. This also means we must
1075 * update sync_trunc_off() as we write.
1077 if (record->type == HAMMER_MEM_RECORD_DATA &&
1078 hammer_record_needs_overwrite_delete(record)) {
1079 file_offset = record->leaf.base.key - record->leaf.data_len;
1080 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1082 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
1083 error = hammer_ip_delete_range(
1085 file_offset, file_offset + bytes - 1,
1087 if (error && error != ENOENT)
1092 * If this is a general record there may be an on-disk version
1093 * that must be deleted before we can insert the new record.
1095 if (record->type == HAMMER_MEM_RECORD_GENERAL) {
1096 error = hammer_delete_general(cursor, record->ip,
1098 if (error && error != ENOENT)
1105 hammer_normalize_cursor(cursor);
1106 cursor->key_beg = record->leaf.base;
1107 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1108 cursor->flags |= HAMMER_CURSOR_BACKEND;
1109 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1112 * Records can wind up on-media before the inode itself is on-media.
1115 record->ip->flags |= HAMMER_INODE_DONDISK;
1118 * If we are deleting a directory entry an exact match must be
1121 if (record->type == HAMMER_MEM_RECORD_DEL) {
1122 error = hammer_btree_lookup(cursor);
1124 KKASSERT(cursor->iprec == NULL);
1125 error = hammer_ip_delete_record(cursor, record->ip,
1128 record->flags |= HAMMER_RECF_DELETED_FE;
1129 record->flags |= HAMMER_RECF_DELETED_BE;
1130 record->flags |= HAMMER_RECF_COMMITTED;
1139 * Issue a lookup to position the cursor and locate the cluster. The
1140 * target key should not exist. If we are creating a directory entry
1141 * we may have to iterate the low 32 bits of the key to find an unused
1144 hammer_sync_lock_sh(trans);
1145 cursor->flags |= HAMMER_CURSOR_INSERT;
1146 error = hammer_btree_lookup(cursor);
1147 if (hammer_debug_inode)
1148 kprintf("DOINSERT LOOKUP %d\n", error);
1150 kprintf("hammer_ip_sync_record: duplicate rec "
1151 "at (%016llx)\n", record->leaf.base.key);
1152 Debugger("duplicate record1");
1156 if (record->type == HAMMER_MEM_RECORD_DATA)
1157 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1158 record->leaf.base.key - record->leaf.data_len,
1159 record->leaf.data_offset, error);
1162 if (error != ENOENT)
1166 * Allocate the record and data. The result buffers will be
1167 * marked as being modified and further calls to
1168 * hammer_modify_buffer() will result in unneeded UNDO records.
1170 * Support zero-fill records (data == NULL and data_len != 0)
1172 if (record->type == HAMMER_MEM_RECORD_DATA) {
1174 * The data portion of a bulk-data record has already been
1175 * committed to disk, we need only adjust the layer2
1176 * statistics in the same transaction as our B-Tree insert.
1178 KKASSERT(record->leaf.data_offset != 0);
1179 error = hammer_blockmap_finalize(trans,
1181 record->leaf.data_offset,
1182 record->leaf.data_len);
1183 } else if (record->data && record->leaf.data_len) {
1185 * Wholely cached record, with data. Allocate the data.
1187 bdata = hammer_alloc_data(trans, record->leaf.data_len,
1188 record->leaf.base.rec_type,
1189 &record->leaf.data_offset,
1190 &cursor->data_buffer, &error);
1193 hammer_crc_set_leaf(record->data, &record->leaf);
1194 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
1195 bcopy(record->data, bdata, record->leaf.data_len);
1196 hammer_modify_buffer_done(cursor->data_buffer);
1199 * Wholely cached record, without data.
1201 record->leaf.data_offset = 0;
1202 record->leaf.data_crc = 0;
1205 error = hammer_btree_insert(cursor, &record->leaf, &doprop);
1206 if (hammer_debug_inode && error)
1207 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n", error, cursor->node->node_offset, cursor->index, record->leaf.base.key);
1210 * Our record is on-disk, normally mark the in-memory version as
1211 * deleted. If the record represented a directory deletion but
1212 * we had to sync a valid directory entry to disk we must convert
1213 * the record to a covering delete so the frontend does not have
1214 * visibility on the synced entry.
1218 hammer_btree_do_propagation(cursor,
1222 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1223 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1224 record->flags &= ~HAMMER_RECF_DELETED_FE;
1225 record->type = HAMMER_MEM_RECORD_DEL;
1226 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1227 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1228 /* hammer_flush_record_done takes care of the rest */
1230 record->flags |= HAMMER_RECF_DELETED_FE;
1231 record->flags |= HAMMER_RECF_DELETED_BE;
1233 record->flags |= HAMMER_RECF_COMMITTED;
1235 if (record->leaf.data_offset) {
1236 hammer_blockmap_free(trans, record->leaf.data_offset,
1237 record->leaf.data_len);
1241 hammer_sync_unlock(trans);
1247 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1248 * entry's key is used to deal with hash collisions in the upper 32 bits.
1249 * A unique 64 bit key is generated in-memory and may be regenerated a
1250 * second time when the directory record is flushed to the on-disk B-Tree.
1252 * A referenced record is passed to this function. This function
1253 * eats the reference. If an error occurs the record will be deleted.
1255 * A copy of the temporary record->data pointer provided by the caller
1259 hammer_mem_add(hammer_record_t record)
1261 hammer_mount_t hmp = record->ip->hmp;
1264 * Make a private copy of record->data
1267 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
1270 * Insert into the RB tree. A unique key should have already
1271 * been selected if this is a directory entry.
1273 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1274 record->flags |= HAMMER_RECF_DELETED_FE;
1275 hammer_rel_mem_record(record);
1278 ++hmp->count_newrecords;
1280 ++record->ip->rsv_recs;
1281 record->ip->hmp->rsv_databytes += record->leaf.data_len;
1282 record->flags |= HAMMER_RECF_ONRBTREE;
1283 hammer_modify_inode(record->ip, HAMMER_INODE_XDIRTY);
1284 hammer_rel_mem_record(record);
1288 /************************************************************************
1289 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1290 ************************************************************************
1292 * These functions augment the B-Tree scanning functions in hammer_btree.c
1293 * by merging in-memory records with on-disk records.
1297 * Locate a particular record either in-memory or on-disk.
1299 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1300 * NOT be called to iterate results.
1303 hammer_ip_lookup(hammer_cursor_t cursor)
1308 * If the element is in-memory return it without searching the
1311 KKASSERT(cursor->ip);
1312 error = hammer_mem_lookup(cursor);
1314 cursor->leaf = &cursor->iprec->leaf;
1317 if (error != ENOENT)
1321 * If the inode has on-disk components search the on-disk B-Tree.
1323 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1325 error = hammer_btree_lookup(cursor);
1327 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1332 * Locate the first record within the cursor's key_beg/key_end range,
1333 * restricted to a particular inode. 0 is returned on success, ENOENT
1334 * if no records matched the requested range, or some other error.
1336 * When 0 is returned hammer_ip_next() may be used to iterate additional
1337 * records within the requested range.
1339 * This function can return EDEADLK, requiring the caller to terminate
1340 * the cursor and try again.
1343 hammer_ip_first(hammer_cursor_t cursor)
1345 hammer_inode_t ip = cursor->ip;
1348 KKASSERT(ip != NULL);
1351 * Clean up fields and setup for merged scan
1353 cursor->flags &= ~HAMMER_CURSOR_RETEST;
1354 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
1355 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
1356 if (cursor->iprec) {
1357 hammer_rel_mem_record(cursor->iprec);
1358 cursor->iprec = NULL;
1362 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1363 * exact lookup so if we get ENOENT we have to call the iterate
1364 * function to validate the first record after the begin key.
1366 * The ATEDISK flag is used by hammer_btree_iterate to determine
1367 * whether it must index forwards or not. It is also used here
1368 * to select the next record from in-memory or on-disk.
1370 * EDEADLK can only occur if the lookup hit an empty internal
1371 * element and couldn't delete it. Since this could only occur
1372 * in-range, we can just iterate from the failure point.
1374 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1375 error = hammer_btree_lookup(cursor);
1376 if (error == ENOENT || error == EDEADLK) {
1377 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1378 if (hammer_debug_general & 0x2000)
1379 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
1380 error = hammer_btree_iterate(cursor);
1382 if (error && error != ENOENT)
1385 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
1386 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1388 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1393 * Search the in-memory record list (Red-Black tree). Unlike the
1394 * B-Tree search, mem_first checks for records in the range.
1396 error = hammer_mem_first(cursor);
1397 if (error && error != ENOENT)
1400 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
1401 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1402 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0)
1403 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1407 * This will return the first matching record.
1409 return(hammer_ip_next(cursor));
1413 * Retrieve the next record in a merged iteration within the bounds of the
1414 * cursor. This call may be made multiple times after the cursor has been
1415 * initially searched with hammer_ip_first().
1417 * 0 is returned on success, ENOENT if no further records match the
1418 * requested range, or some other error code is returned.
1421 hammer_ip_next(hammer_cursor_t cursor)
1423 hammer_btree_elm_t elm;
1424 hammer_record_t rec, save;
1430 * Load the current on-disk and in-memory record. If we ate any
1431 * records we have to get the next one.
1433 * If we deleted the last on-disk record we had scanned ATEDISK will
1434 * be clear and RETEST will be set, forcing a call to iterate. The
1435 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1436 * element. If ATEDISK is set, iterate will skip the 'current'
1439 * Get the next on-disk record
1441 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_RETEST)) {
1442 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1443 error = hammer_btree_iterate(cursor);
1444 cursor->flags &= ~HAMMER_CURSOR_RETEST;
1446 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1447 hammer_cache_node(&cursor->ip->cache[1],
1450 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1451 HAMMER_CURSOR_ATEDISK;
1458 * Get the next in-memory record.
1460 * hammer_rec_scan_cmp: Is the record still in our general range,
1461 * (non-inclusive of snapshot exclusions)?
1462 * hammer_rec_scan_callback: Is the record in our snapshot?
1464 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1465 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1466 save = cursor->iprec;
1467 cursor->iprec = NULL;
1468 rec = save ? hammer_rec_rb_tree_RB_NEXT(save) : NULL;
1470 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1472 if (hammer_rec_scan_callback(rec, cursor) != 0)
1474 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1477 hammer_rel_mem_record(save);
1478 if (cursor->iprec) {
1479 KKASSERT(cursor->iprec == rec);
1480 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1482 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1488 * The memory record may have become stale while being held in
1489 * cursor->iprec. We are interlocked against the backend on
1490 * with regards to B-Tree entries.
1492 if ((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0) {
1493 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0) {
1494 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1500 * Extract either the disk or memory record depending on their
1501 * relative position.
1504 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1507 * Both entries valid. Compare the entries and nominally
1508 * return the first one in the sort order. Numerous cases
1509 * require special attention, however.
1511 elm = &cursor->node->ondisk->elms[cursor->index];
1512 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1515 * If the two entries differ only by their key (-2/2) or
1516 * create_tid (-1/1), and are DATA records, we may have a
1517 * nominal match. We have to calculate the base file
1518 * offset of the data.
1520 if (r <= 2 && r >= -2 && r != 0 &&
1521 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1522 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1523 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1524 int64_t base2 = cursor->iprec->leaf.base.key -
1525 cursor->iprec->leaf.data_len;
1531 error = hammer_btree_extract(cursor,
1532 HAMMER_CURSOR_GET_LEAF);
1533 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1538 * If the entries match exactly the memory entry is either
1539 * an on-disk directory entry deletion or a bulk data
1540 * overwrite. If it is a directory entry deletion we eat
1543 * For the bulk-data overwrite case it is possible to have
1544 * visibility into both, which simply means the syncer
1545 * hasn't gotten around to doing the delete+insert sequence
1546 * on the B-Tree. Use the memory entry and throw away the
1549 * If the in-memory record is not either of these we
1550 * probably caught the syncer while it was syncing it to
1551 * the media. Since we hold a shared lock on the cursor,
1552 * the in-memory record had better be marked deleted at
1556 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1557 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1558 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1559 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1562 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1563 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1564 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1566 /* fall through to memory entry */
1568 panic("hammer_ip_next: duplicate mem/b-tree entry %p %d %08x", cursor->iprec, cursor->iprec->type, cursor->iprec->flags);
1569 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1573 /* fall through to the memory entry */
1574 case HAMMER_CURSOR_ATEDISK:
1576 * Only the memory entry is valid.
1578 cursor->leaf = &cursor->iprec->leaf;
1579 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1582 * If the memory entry is an on-disk deletion we should have
1583 * also had found a B-Tree record. If the backend beat us
1584 * to it it would have interlocked the cursor and we should
1585 * have seen the in-memory record marked DELETED_FE.
1587 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1588 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1589 panic("hammer_ip_next: del-on-disk with no b-tree entry iprec %p flags %08x", cursor->iprec, cursor->iprec->flags);
1592 case HAMMER_CURSOR_ATEMEM:
1594 * Only the disk entry is valid
1596 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1597 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1601 * Neither entry is valid
1603 * XXX error not set properly
1605 cursor->leaf = NULL;
1613 * Resolve the cursor->data pointer for the current cursor position in
1614 * a merged iteration.
1617 hammer_ip_resolve_data(hammer_cursor_t cursor)
1619 hammer_record_t record;
1622 if (hammer_cursor_inmem(cursor)) {
1624 * The data associated with an in-memory record is usually
1625 * kmalloced, but reserve-ahead data records will have an
1626 * on-disk reference.
1628 * NOTE: Reserve-ahead data records must be handled in the
1629 * context of the related high level buffer cache buffer
1630 * to interlock against async writes.
1632 record = cursor->iprec;
1633 cursor->data = record->data;
1635 if (cursor->data == NULL) {
1636 KKASSERT(record->leaf.base.rec_type ==
1637 HAMMER_RECTYPE_DATA);
1638 cursor->data = hammer_bread_ext(cursor->trans->hmp,
1639 record->leaf.data_offset,
1640 record->leaf.data_len,
1642 &cursor->data_buffer);
1645 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
1646 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1652 * Backend truncation / record replacement - delete records in range.
1654 * Delete all records within the specified range for inode ip. In-memory
1655 * records still associated with the frontend are ignored.
1657 * If truncating is non-zero in-memory records associated with the back-end
1658 * are ignored. If truncating is > 1 we can return EWOULDBLOCK.
1662 * * An unaligned range will cause new records to be added to cover
1663 * the edge cases. (XXX not implemented yet).
1665 * * Replacement via reservations (see hammer_ip_sync_record_cursor())
1666 * also do not deal with unaligned ranges.
1668 * * ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1670 * * Record keys for regular file data have to be special-cased since
1671 * they indicate the end of the range (key = base + bytes).
1673 * * This function may be asked to delete ridiculously huge ranges, for
1674 * example if someone truncates or removes a 1TB regular file. We
1675 * must be very careful on restarts and we may have to stop w/
1676 * EWOULDBLOCK to avoid blowing out the buffer cache.
1679 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1680 int64_t ran_beg, int64_t ran_end, int truncating)
1682 hammer_transaction_t trans = cursor->trans;
1683 hammer_btree_leaf_elm_t leaf;
1689 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1692 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1694 hammer_normalize_cursor(cursor);
1695 cursor->key_beg.localization = ip->obj_localization +
1696 HAMMER_LOCALIZE_MISC;
1697 cursor->key_beg.obj_id = ip->obj_id;
1698 cursor->key_beg.create_tid = 0;
1699 cursor->key_beg.delete_tid = 0;
1700 cursor->key_beg.obj_type = 0;
1702 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1703 cursor->key_beg.key = ran_beg;
1704 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1707 * The key in the B-Tree is (base+bytes), so the first possible
1708 * matching key is ran_beg + 1.
1710 cursor->key_beg.key = ran_beg + 1;
1711 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1714 cursor->key_end = cursor->key_beg;
1715 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1716 cursor->key_end.key = ran_end;
1718 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1719 if (tmp64 < ran_end)
1720 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1722 cursor->key_end.key = ran_end + MAXPHYS + 1;
1725 cursor->asof = ip->obj_asof;
1726 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1727 cursor->flags |= HAMMER_CURSOR_ASOF;
1728 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1729 cursor->flags |= HAMMER_CURSOR_BACKEND;
1730 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
1732 error = hammer_ip_first(cursor);
1735 * Iterate through matching records and mark them as deleted.
1737 while (error == 0) {
1738 leaf = cursor->leaf;
1740 KKASSERT(leaf->base.delete_tid == 0);
1741 KKASSERT(leaf->base.obj_id == ip->obj_id);
1744 * There may be overlap cases for regular file data. Also
1745 * remember the key for a regular file record is (base + len),
1748 * Note that do to duplicates (mem & media) allowed by
1749 * DELETE_VISIBILITY, off can wind up less then ran_beg.
1751 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
1752 off = leaf->base.key - leaf->data_len;
1754 * Check the left edge case. We currently do not
1755 * split existing records.
1757 if (off < ran_beg && leaf->base.key > ran_beg) {
1758 panic("hammer left edge case %016llx %d\n",
1759 leaf->base.key, leaf->data_len);
1763 * Check the right edge case. Note that the
1764 * record can be completely out of bounds, which
1765 * terminates the search.
1767 * base->key is exclusive of the right edge while
1768 * ran_end is inclusive of the right edge. The
1769 * (key - data_len) left boundary is inclusive.
1771 * XXX theory-check this test at some point, are
1772 * we missing a + 1 somewhere? Note that ran_end
1775 if (leaf->base.key - 1 > ran_end) {
1776 if (leaf->base.key - leaf->data_len > ran_end)
1778 panic("hammer right edge case\n");
1781 off = leaf->base.key;
1785 * Delete the record. When truncating we do not delete
1786 * in-memory (data) records because they represent data
1787 * written after the truncation.
1789 * This will also physically destroy the B-Tree entry and
1790 * data if the retention policy dictates. The function
1791 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
1792 * to retest the new 'current' element.
1794 if (truncating == 0 || hammer_cursor_ondisk(cursor)) {
1795 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1797 * If we have built up too many meta-buffers we risk
1798 * deadlocking the kernel and must stop. This can
1799 * occur when deleting ridiculously huge files.
1800 * sync_trunc_off is updated so the next cycle does
1801 * not re-iterate records we have already deleted.
1803 * This is only done with formal truncations.
1805 if (truncating > 1 && error == 0 &&
1806 hammer_flusher_meta_limit(ip->hmp)) {
1807 ip->sync_trunc_off = off;
1808 error = EWOULDBLOCK;
1813 ran_beg = off; /* for restart */
1814 error = hammer_ip_next(cursor);
1817 hammer_cache_node(&ip->cache[1], cursor->node);
1819 if (error == EDEADLK) {
1820 hammer_done_cursor(cursor);
1821 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1825 if (error == ENOENT)
1831 * This backend function deletes the specified record on-disk, similar to
1832 * delete_range but for a specific record. Unlike the exact deletions
1833 * used when deleting a directory entry this function uses an ASOF search
1834 * like delete_range.
1836 * This function may be called with ip->obj_asof set for a slave snapshot,
1837 * so don't use it. We always delete non-historical records only.
1840 hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
1841 hammer_btree_leaf_elm_t leaf)
1843 hammer_transaction_t trans = cursor->trans;
1846 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1848 hammer_normalize_cursor(cursor);
1849 cursor->key_beg = leaf->base;
1850 cursor->asof = HAMMER_MAX_TID;
1851 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1852 cursor->flags |= HAMMER_CURSOR_ASOF;
1853 cursor->flags |= HAMMER_CURSOR_BACKEND;
1854 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1856 error = hammer_btree_lookup(cursor);
1858 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1860 if (error == EDEADLK) {
1861 hammer_done_cursor(cursor);
1862 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1870 * This function deletes remaining auxillary records when an inode is
1871 * being deleted. This function explicitly does not delete the
1872 * inode record, directory entry, data, or db records. Those must be
1873 * properly disposed of prior to this call.
1876 hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip, int *countp)
1878 hammer_transaction_t trans = cursor->trans;
1879 hammer_btree_leaf_elm_t leaf;
1882 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1884 hammer_normalize_cursor(cursor);
1885 cursor->key_beg.localization = ip->obj_localization +
1886 HAMMER_LOCALIZE_MISC;
1887 cursor->key_beg.obj_id = ip->obj_id;
1888 cursor->key_beg.create_tid = 0;
1889 cursor->key_beg.delete_tid = 0;
1890 cursor->key_beg.obj_type = 0;
1891 cursor->key_beg.rec_type = HAMMER_RECTYPE_CLEAN_START;
1892 cursor->key_beg.key = HAMMER_MIN_KEY;
1894 cursor->key_end = cursor->key_beg;
1895 cursor->key_end.rec_type = HAMMER_RECTYPE_MAX;
1896 cursor->key_end.key = HAMMER_MAX_KEY;
1898 cursor->asof = ip->obj_asof;
1899 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1900 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1901 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1902 cursor->flags |= HAMMER_CURSOR_BACKEND;
1904 error = hammer_ip_first(cursor);
1907 * Iterate through matching records and mark them as deleted.
1909 while (error == 0) {
1910 leaf = cursor->leaf;
1912 KKASSERT(leaf->base.delete_tid == 0);
1915 * Mark the record and B-Tree entry as deleted. This will
1916 * also physically delete the B-Tree entry, record, and
1917 * data if the retention policy dictates. The function
1918 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
1919 * to retest the new 'current' element.
1921 * Directory entries (and delete-on-disk directory entries)
1922 * must be synced and cannot be deleted.
1924 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1928 error = hammer_ip_next(cursor);
1931 hammer_cache_node(&ip->cache[1], cursor->node);
1932 if (error == EDEADLK) {
1933 hammer_done_cursor(cursor);
1934 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1938 if (error == ENOENT)
1944 * Delete the record at the current cursor. On success the cursor will
1945 * be positioned appropriately for an iteration but may no longer be at
1948 * This routine is only called from the backend.
1950 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1954 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
1957 hammer_record_t iprec;
1961 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1963 hmp = cursor->node->hmp;
1966 * In-memory (unsynchronized) records can simply be freed. This
1967 * only occurs in range iterations since all other records are
1968 * individually synchronized. Thus there should be no confusion with
1971 * An in-memory record may be deleted before being committed to disk,
1972 * but could have been accessed in the mean time. The reservation
1973 * code will deal with the case.
1975 if (hammer_cursor_inmem(cursor)) {
1976 iprec = cursor->iprec;
1977 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
1978 iprec->flags |= HAMMER_RECF_DELETED_FE;
1979 iprec->flags |= HAMMER_RECF_DELETED_BE;
1984 * On-disk records are marked as deleted by updating their delete_tid.
1985 * This does not effect their position in the B-Tree (which is based
1986 * on their create_tid).
1988 * Frontend B-Tree operations track inodes so we tell
1989 * hammer_delete_at_cursor() not to.
1991 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1994 error = hammer_delete_at_cursor(
1996 HAMMER_DELETE_ADJUST | hammer_nohistory(ip),
1998 cursor->trans->time32,
2005 * Delete the B-Tree element at the current cursor and do any necessary
2006 * mirror propagation.
2008 * The cursor must be properly positioned for an iteration on return but
2009 * may be pointing at an internal element.
2011 * An element can be un-deleted by passing a delete_tid of 0 with
2012 * HAMMER_DELETE_ADJUST.
2015 hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
2016 hammer_tid_t delete_tid, u_int32_t delete_ts,
2017 int track, int64_t *stat_bytes)
2019 struct hammer_btree_leaf_elm save_leaf;
2020 hammer_transaction_t trans;
2021 hammer_btree_leaf_elm_t leaf;
2023 hammer_btree_elm_t elm;
2024 hammer_off_t data_offset;
2031 error = hammer_cursor_upgrade(cursor);
2035 trans = cursor->trans;
2036 node = cursor->node;
2037 elm = &node->ondisk->elms[cursor->index];
2039 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
2041 hammer_sync_lock_sh(trans);
2046 * Adjust the delete_tid. Update the mirror_tid propagation field
2047 * as well. delete_tid can be 0 (undelete -- used by mirroring).
2049 if (delete_flags & HAMMER_DELETE_ADJUST) {
2050 if (elm->base.rec_type == HAMMER_RECTYPE_INODE) {
2051 if (elm->leaf.base.delete_tid == 0 && delete_tid)
2053 if (elm->leaf.base.delete_tid && delete_tid == 0)
2057 hammer_modify_node(trans, node, elm, sizeof(*elm));
2058 elm->leaf.base.delete_tid = delete_tid;
2059 elm->leaf.delete_ts = delete_ts;
2060 hammer_modify_node_done(node);
2062 if (elm->leaf.base.delete_tid > node->ondisk->mirror_tid) {
2063 hammer_modify_node_field(trans, node, mirror_tid);
2064 node->ondisk->mirror_tid = elm->leaf.base.delete_tid;
2065 hammer_modify_node_done(node);
2067 if (hammer_debug_general & 0x0002) {
2068 kprintf("delete_at_cursor: propagate %016llx"
2070 elm->leaf.base.delete_tid,
2076 * Adjust for the iteration. We have deleted the current
2077 * element and want to clear ATEDISK so the iteration does
2078 * not skip the element after, which now becomes the current
2079 * element. This element must be re-tested if doing an
2080 * iteration, which is handled by the RETEST flag.
2082 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2083 cursor->flags |= HAMMER_CURSOR_RETEST;
2084 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2088 * An on-disk record cannot have the same delete_tid
2089 * as its create_tid. In a chain of record updates
2090 * this could result in a duplicate record.
2092 KKASSERT(elm->leaf.base.delete_tid !=
2093 elm->leaf.base.create_tid);
2097 * Destroy the B-Tree element if asked (typically if a nohistory
2098 * file or mount, or when called by the pruning code).
2100 * Adjust the ATEDISK flag to properly support iterations.
2102 if (delete_flags & HAMMER_DELETE_DESTROY) {
2103 data_offset = elm->leaf.data_offset;
2104 data_len = elm->leaf.data_len;
2105 rec_type = elm->leaf.base.rec_type;
2107 save_leaf = elm->leaf;
2110 if (elm->base.rec_type == HAMMER_RECTYPE_INODE &&
2111 elm->leaf.base.delete_tid == 0) {
2115 error = hammer_btree_delete(cursor);
2118 * The deletion moves the next element (if any) to
2119 * the current element position. We must clear
2120 * ATEDISK so this element is not skipped and we
2121 * must set RETEST to force any iteration to re-test
2124 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2125 cursor->flags |= HAMMER_CURSOR_RETEST;
2126 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2130 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
2131 case HAMMER_ZONE_LARGE_DATA:
2132 case HAMMER_ZONE_SMALL_DATA:
2133 case HAMMER_ZONE_META:
2134 hammer_blockmap_free(trans,
2135 data_offset, data_len);
2144 * Track inode count and next_tid. This is used by the mirroring
2145 * and PFS code. icount can be negative, zero, or positive.
2147 if (error == 0 && track) {
2149 hammer_modify_volume_field(trans, trans->rootvol,
2151 trans->rootvol->ondisk->vol0_stat_inodes += icount;
2152 hammer_modify_volume_done(trans->rootvol);
2154 if (trans->rootvol->ondisk->vol0_next_tid < delete_tid) {
2155 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
2156 trans->rootvol->ondisk->vol0_next_tid = delete_tid;
2157 hammer_modify_volume_done(trans->rootvol);
2162 * mirror_tid propagation occurs if the node's mirror_tid had to be
2163 * updated while adjusting the delete_tid.
2165 * This occurs when deleting even in nohistory mode, but does not
2166 * occur when pruning an already-deleted node.
2168 * cursor->ip is NULL when called from the pruning, mirroring,
2169 * and pfs code. If non-NULL propagation will be conditionalized
2170 * on whether the PFS is in no-history mode or not.
2174 hammer_btree_do_propagation(cursor, cursor->ip->pfsm, leaf);
2176 hammer_btree_do_propagation(cursor, NULL, leaf);
2178 hammer_sync_unlock(trans);
2183 * Determine whether we can remove a directory. This routine checks whether
2184 * a directory is empty or not and enforces flush connectivity.
2186 * Flush connectivity requires that we block if the target directory is
2187 * currently flushing, otherwise it may not end up in the same flush group.
2189 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
2192 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
2194 struct hammer_cursor cursor;
2198 * Check directory empty
2200 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2202 cursor.key_beg.localization = ip->obj_localization +
2203 HAMMER_LOCALIZE_MISC;
2204 cursor.key_beg.obj_id = ip->obj_id;
2205 cursor.key_beg.create_tid = 0;
2206 cursor.key_beg.delete_tid = 0;
2207 cursor.key_beg.obj_type = 0;
2208 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
2209 cursor.key_beg.key = HAMMER_MIN_KEY;
2211 cursor.key_end = cursor.key_beg;
2212 cursor.key_end.rec_type = 0xFFFF;
2213 cursor.key_end.key = HAMMER_MAX_KEY;
2215 cursor.asof = ip->obj_asof;
2216 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2218 error = hammer_ip_first(&cursor);
2219 if (error == ENOENT)
2221 else if (error == 0)
2223 hammer_done_cursor(&cursor);