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.49 2008/05/02 06:51:57 dillon Exp $
39 static int hammer_mem_add(hammer_transaction_t trans
, hammer_record_t record
);
40 static int hammer_mem_lookup(hammer_cursor_t cursor
, hammer_inode_t ip
);
41 static int hammer_mem_first(hammer_cursor_t cursor
, hammer_inode_t ip
);
44 * Red-black tree support.
47 hammer_rec_rb_compare(hammer_record_t rec1
, hammer_record_t rec2
)
49 if (rec1
->rec
.base
.base
.rec_type
< rec2
->rec
.base
.base
.rec_type
)
51 if (rec1
->rec
.base
.base
.rec_type
> rec2
->rec
.base
.base
.rec_type
)
54 if (rec1
->rec
.base
.base
.key
< rec2
->rec
.base
.base
.key
)
56 if (rec1
->rec
.base
.base
.key
> rec2
->rec
.base
.base
.key
)
59 if (rec1
->rec
.base
.base
.create_tid
== 0) {
60 if (rec2
->rec
.base
.base
.create_tid
== 0)
64 if (rec2
->rec
.base
.base
.create_tid
== 0)
67 if (rec1
->rec
.base
.base
.create_tid
< rec2
->rec
.base
.base
.create_tid
)
69 if (rec1
->rec
.base
.base
.create_tid
> rec2
->rec
.base
.base
.create_tid
)
73 * Never match against an item deleted by the front-end.
75 if (rec1
->flags
& HAMMER_RECF_DELETED_FE
)
77 if (rec2
->flags
& HAMMER_RECF_DELETED_FE
)
84 hammer_rec_compare(hammer_base_elm_t info
, hammer_record_t rec
)
86 if (info
->rec_type
< rec
->rec
.base
.base
.rec_type
)
88 if (info
->rec_type
> rec
->rec
.base
.base
.rec_type
)
91 if (info
->key
< rec
->rec
.base
.base
.key
)
93 if (info
->key
> rec
->rec
.base
.base
.key
)
96 if (info
->create_tid
== 0) {
97 if (rec
->rec
.base
.base
.create_tid
== 0)
101 if (rec
->rec
.base
.base
.create_tid
== 0)
103 if (info
->create_tid
< rec
->rec
.base
.base
.create_tid
)
105 if (info
->create_tid
> rec
->rec
.base
.base
.create_tid
)
111 * RB_SCAN comparison code for hammer_mem_first(). The argument order
112 * is reversed so the comparison result has to be negated. key_beg and
113 * key_end are both range-inclusive.
115 * The creation timestamp can cause hammer_rec_compare() to return -1 or +1.
116 * These do not stop the scan.
118 * Localized deletions are not cached in-memory.
122 hammer_rec_scan_cmp(hammer_record_t rec
, void *data
)
124 hammer_cursor_t cursor
= data
;
127 r
= hammer_rec_compare(&cursor
->key_beg
, rec
);
130 r
= hammer_rec_compare(&cursor
->key_end
, rec
);
137 * This compare function is used when simply looking up key_beg.
141 hammer_rec_find_cmp(hammer_record_t rec
, void *data
)
143 hammer_cursor_t cursor
= data
;
146 r
= hammer_rec_compare(&cursor
->key_beg
, rec
);
154 RB_GENERATE(hammer_rec_rb_tree
, hammer_record
, rb_node
, hammer_rec_rb_compare
);
155 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree
, INFO
, hammer_record
, rb_node
,
156 hammer_rec_compare
, hammer_base_elm_t
);
159 * Allocate a record for the caller to finish filling in. The record is
160 * returned referenced.
163 hammer_alloc_mem_record(hammer_inode_t ip
)
165 hammer_record_t record
;
167 ++hammer_count_records
;
168 record
= kmalloc(sizeof(*record
), M_HAMMER
, M_WAITOK
|M_ZERO
);
169 record
->flush_state
= HAMMER_FST_IDLE
;
171 record
->rec
.base
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
172 hammer_ref(&record
->lock
);
177 hammer_wait_mem_record(hammer_record_t record
)
179 while (record
->flush_state
== HAMMER_FST_FLUSH
) {
180 record
->flags
|= HAMMER_RECF_WANTED
;
181 tsleep(record
, 0, "hmrrc2", 0);
186 * Called from the backend, hammer_inode.c, after a record has been
187 * flushed to disk. The record has been exclusively locked by the
188 * caller and interlocked with BE.
190 * We clean up the state, unlock, and release the record (the record
191 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
194 hammer_flush_record_done(hammer_record_t record
, int error
)
196 hammer_inode_t target_ip
;
199 KKASSERT(record
->flush_state
== HAMMER_FST_FLUSH
);
200 KKASSERT(record
->flags
& HAMMER_RECF_INTERLOCK_BE
);
204 * An error occured, the backend was unable to sync the
205 * record to its media. Leave the record intact.
207 Debugger("flush_record_done error");
208 } else if (record
->flags
& HAMMER_RECF_CONVERT_DELETE
) {
210 * deleted-record to delete-on-disk conversion, occurs when
211 * we sync a record to disk which is marked deleted by the
212 * frontend, but not deleted from the point of view of the
215 if (record
->flags
& HAMMER_RECF_DELETED_BE
) {
216 record
->flags
|= HAMMER_RECF_DELETED_FE
;
219 KKASSERT(record
->type
== HAMMER_MEM_RECORD_DEL
);
223 * Normal completion, record has been disposed of (by
224 * having been synchronized to the media).
226 record
->flags
|= HAMMER_RECF_DELETED_FE
;
227 record
->flags
|= HAMMER_RECF_DELETED_BE
;
231 if ((target_ip
= record
->target_ip
) != NULL
) {
232 TAILQ_REMOVE(&target_ip
->target_list
, record
,
234 record
->target_ip
= NULL
;
235 hammer_test_inode(target_ip
);
237 record
->flush_state
= HAMMER_FST_IDLE
;
239 if (record
->target_ip
)
240 record
->flush_state
= HAMMER_FST_SETUP
;
242 record
->flush_state
= HAMMER_FST_IDLE
;
245 record
->flags
&= ~HAMMER_RECF_INTERLOCK_BE
;
246 record
->flags
&= ~HAMMER_RECF_CONVERT_DELETE
;
247 if (record
->flags
& HAMMER_RECF_WANTED
) {
248 record
->flags
&= ~HAMMER_RECF_WANTED
;
251 hammer_rel_mem_record(record
);
255 * Release a memory record. Records marked for deletion are immediately
256 * removed from the RB-Tree but otherwise left intact until the last ref
260 hammer_rel_mem_record(struct hammer_record
*record
)
262 hammer_inode_t ip
, target_ip
;
264 hammer_unref(&record
->lock
);
266 if (record
->flags
& HAMMER_RECF_DELETED_FE
) {
267 if (record
->lock
.refs
== 0) {
268 KKASSERT(record
->flush_state
!= HAMMER_FST_FLUSH
);
271 if ((target_ip
= record
->target_ip
) != NULL
) {
272 TAILQ_REMOVE(&target_ip
->target_list
,
273 record
, target_entry
);
274 record
->target_ip
= NULL
;
275 hammer_test_inode(target_ip
);
278 if (record
->flags
& HAMMER_RECF_ONRBTREE
) {
279 RB_REMOVE(hammer_rec_rb_tree
,
280 &record
->ip
->rec_tree
,
282 record
->flags
&= ~HAMMER_RECF_ONRBTREE
;
284 if (record
->flags
& HAMMER_RECF_ALLOCDATA
) {
285 --hammer_count_record_datas
;
286 kfree(record
->data
, M_HAMMER
);
287 record
->flags
&= ~HAMMER_RECF_ALLOCDATA
;
290 --hammer_count_records
;
291 kfree(record
, M_HAMMER
);
298 * Record visibility depends on whether the record is being accessed by
299 * the backend or the frontend.
301 * Return non-zero if the record is visible, zero if it isn't or if it is
306 hammer_ip_iterate_mem_good(hammer_cursor_t cursor
, hammer_record_t record
)
308 if (cursor
->flags
& HAMMER_CURSOR_BACKEND
) {
309 if (record
->flags
& HAMMER_RECF_DELETED_BE
)
311 if ((record
->flags
& HAMMER_RECF_INTERLOCK_BE
) == 0)
314 if (record
->flags
& HAMMER_RECF_DELETED_FE
)
321 * This callback is used as part of the RB_SCAN function for in-memory
322 * records. We terminate it (return -1) as soon as we get a match.
324 * This routine is used by frontend code.
326 * The primary compare code does not account for ASOF lookups. This
327 * code handles that case as well as a few others.
331 hammer_rec_scan_callback(hammer_record_t rec
, void *data
)
333 hammer_cursor_t cursor
= data
;
336 * We terminate on success, so this should be NULL on entry.
338 KKASSERT(cursor
->iprec
== NULL
);
341 * Skip if the record was marked deleted.
343 if (hammer_ip_iterate_mem_good(cursor
, rec
) == 0)
347 * Skip if not visible due to our as-of TID
349 if (cursor
->flags
& HAMMER_CURSOR_ASOF
) {
350 if (cursor
->asof
< rec
->rec
.base
.base
.create_tid
)
352 if (rec
->rec
.base
.base
.delete_tid
&&
353 cursor
->asof
>= rec
->rec
.base
.base
.delete_tid
) {
359 * If the record is queued to the flusher we have to block until
360 * it isn't. Otherwise we may see duplication between our memory
361 * cache and the media.
363 hammer_ref(&rec
->lock
);
365 #warning "This deadlocks"
367 if (rec
->flush_state
== HAMMER_FST_FLUSH
)
368 hammer_wait_mem_record(rec
);
372 * The record may have been deleted while we were blocked.
374 if (hammer_ip_iterate_mem_good(cursor
, rec
) == 0) {
375 hammer_rel_mem_record(rec
);
380 * Set the matching record and stop the scan.
388 * Lookup an in-memory record given the key specified in the cursor. Works
389 * just like hammer_btree_lookup() but operates on an inode's in-memory
392 * The lookup must fail if the record is marked for deferred deletion.
396 hammer_mem_lookup(hammer_cursor_t cursor
, hammer_inode_t ip
)
401 hammer_rel_mem_record(cursor
->iprec
);
402 cursor
->iprec
= NULL
;
405 KKASSERT(cursor
->ip
->cursor_ip_refs
> 0);
406 --cursor
->ip
->cursor_ip_refs
;
408 hammer_rec_rb_tree_scan_info_done(&cursor
->scan
,
409 &cursor
->ip
->rec_tree
);
414 hammer_rec_rb_tree_scan_info_link(&cursor
->scan
, &ip
->rec_tree
);
416 ++ip
->cursor_ip_refs
;
419 cursor
->scan
.node
= NULL
;
421 hammer_rec_rb_tree_RB_SCAN(&ip
->rec_tree
, hammer_rec_find_cmp
,
422 hammer_rec_scan_callback
, cursor
);
424 if (cursor
->iprec
== NULL
)
432 * hammer_mem_first() - locate the first in-memory record matching the
433 * cursor within the bounds of the key range.
437 hammer_mem_first(hammer_cursor_t cursor
, hammer_inode_t ip
)
440 hammer_rel_mem_record(cursor
->iprec
);
441 cursor
->iprec
= NULL
;
444 KKASSERT(cursor
->ip
->cursor_ip_refs
> 0);
445 --cursor
->ip
->cursor_ip_refs
;
447 hammer_rec_rb_tree_scan_info_done(&cursor
->scan
,
448 &cursor
->ip
->rec_tree
);
453 hammer_rec_rb_tree_scan_info_link(&cursor
->scan
, &ip
->rec_tree
);
455 ++ip
->cursor_ip_refs
;
458 cursor
->scan
.node
= NULL
;
460 hammer_rec_rb_tree_RB_SCAN(&ip
->rec_tree
, hammer_rec_scan_cmp
,
461 hammer_rec_scan_callback
, cursor
);
464 * Adjust scan.node and keep it linked into the RB-tree so we can
465 * hold the cursor through third party modifications of the RB-tree.
469 cursor
->scan
.node
= hammer_rec_rb_tree_RB_NEXT(cursor
->iprec
);
477 hammer_mem_done(hammer_cursor_t cursor
)
480 KKASSERT(cursor
->ip
->cursor_ip_refs
> 0);
481 --cursor
->ip
->cursor_ip_refs
;
483 hammer_rec_rb_tree_scan_info_done(&cursor
->scan
,
484 &cursor
->ip
->rec_tree
);
489 hammer_rel_mem_record(cursor
->iprec
);
490 cursor
->iprec
= NULL
;
494 /************************************************************************
495 * HAMMER IN-MEMORY RECORD FUNCTIONS *
496 ************************************************************************
498 * These functions manipulate in-memory records. Such records typically
499 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
503 * Add a directory entry (dip,ncp) which references inode (ip).
505 * Note that the low 32 bits of the namekey are set temporarily to create
506 * a unique in-memory record, and may be modified a second time when the
507 * record is synchronized to disk. In particular, the low 32 bits cannot be
508 * all 0's when synching to disk, which is not handled here.
511 hammer_ip_add_directory(struct hammer_transaction
*trans
,
512 struct hammer_inode
*dip
, struct namecache
*ncp
,
513 struct hammer_inode
*ip
)
515 hammer_record_t record
;
519 record
= hammer_alloc_mem_record(dip
);
521 bytes
= ncp
->nc_nlen
; /* NOTE: terminating \0 is NOT included */
522 if (++trans
->hmp
->namekey_iterator
== 0)
523 ++trans
->hmp
->namekey_iterator
;
525 record
->type
= HAMMER_MEM_RECORD_ADD
;
526 record
->rec
.entry
.base
.base
.obj_id
= dip
->obj_id
;
527 record
->rec
.entry
.base
.base
.key
=
528 hammer_directory_namekey(ncp
->nc_name
, bytes
);
529 record
->rec
.entry
.base
.base
.key
+= trans
->hmp
->namekey_iterator
;
530 record
->rec
.entry
.base
.base
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
531 record
->rec
.entry
.base
.base
.obj_type
= ip
->ino_rec
.base
.base
.obj_type
;
532 record
->rec
.entry
.obj_id
= ip
->obj_id
;
533 record
->data
= (void *)ncp
->nc_name
;
534 record
->rec
.entry
.base
.data_len
= bytes
;
535 ++ip
->ino_rec
.ino_nlinks
;
536 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
539 * The target inode and the directory entry are bound together.
541 record
->target_ip
= ip
;
542 record
->flush_state
= HAMMER_FST_SETUP
;
543 TAILQ_INSERT_TAIL(&ip
->target_list
, record
, target_entry
);
546 * The inode now has a dependancy and must be taken out of the idle
547 * state. An inode not in an idle state is given an extra reference.
549 if (ip
->flush_state
== HAMMER_FST_IDLE
) {
550 hammer_ref(&ip
->lock
);
551 ip
->flush_state
= HAMMER_FST_SETUP
;
554 /* NOTE: copies record->data */
555 error
= hammer_mem_add(trans
, record
);
560 * Delete the directory entry and update the inode link count. The
561 * cursor must be seeked to the directory entry record being deleted.
563 * The related inode should be share-locked by the caller. The caller is
566 * This function can return EDEADLK requiring the caller to terminate
567 * the cursor, any locks, wait on the returned record, and retry.
570 hammer_ip_del_directory(struct hammer_transaction
*trans
,
571 hammer_cursor_t cursor
, struct hammer_inode
*dip
,
572 struct hammer_inode
*ip
)
574 hammer_record_t record
;
577 if (cursor
->record
== &cursor
->iprec
->rec
) {
579 * In-memory (unsynchronized) records can simply be freed.
580 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
581 * by the backend, we must still avoid races against the
582 * backend potentially syncing the record to the media.
584 * We cannot call hammer_ip_delete_record(), that routine may
585 * only be called from the backend.
587 record
= cursor
->iprec
;
588 if (record
->flags
& HAMMER_RECF_INTERLOCK_BE
) {
589 KKASSERT(cursor
->deadlk_rec
== NULL
);
590 hammer_ref(&record
->lock
);
591 cursor
->deadlk_rec
= record
;
594 KKASSERT(record
->type
== HAMMER_MEM_RECORD_ADD
);
595 record
->flags
|= HAMMER_RECF_DELETED_FE
;
600 * If the record is on-disk we have to queue the deletion by
601 * the record's key. This also causes lookups to skip the
604 record
= hammer_alloc_mem_record(dip
);
605 record
->type
= HAMMER_MEM_RECORD_DEL
;
606 record
->rec
.entry
.base
.base
= cursor
->record
->base
.base
;
607 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
609 record
->target_ip
= ip
;
610 record
->flush_state
= HAMMER_FST_SETUP
;
611 TAILQ_INSERT_TAIL(&ip
->target_list
, record
, target_entry
);
614 * The inode now has a dependancy and must be taken out of
615 * the idle state. An inode not in an idle state is given
616 * an extra reference.
618 if (ip
->flush_state
== HAMMER_FST_IDLE
) {
619 hammer_ref(&ip
->lock
);
620 ip
->flush_state
= HAMMER_FST_SETUP
;
623 error
= hammer_mem_add(trans
, record
);
627 * One less link. The file may still be open in the OS even after
628 * all links have gone away so we only try to sync if the OS has
629 * no references and nlinks falls to 0.
631 * We have to terminate the cursor before syncing the inode to
632 * avoid deadlocking against ourselves.
634 * XXX we can't sync the inode here because the encompassing
635 * transaction might be a rename and might update the inode
636 * again with a new link. That would force the delete_tid to be
637 * the same as the create_tid and cause a panic.
640 --ip
->ino_rec
.ino_nlinks
;
641 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
642 if (ip
->ino_rec
.ino_nlinks
== 0 &&
643 (ip
->vp
== NULL
|| (ip
->vp
->v_flag
& VINACTIVE
))) {
644 hammer_done_cursor(cursor
);
652 * Add a record to an inode.
654 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
655 * initialize the following additional fields:
657 * The related inode should be share-locked by the caller. The caller is
660 * record->rec.entry.base.base.key
661 * record->rec.entry.base.base.rec_type
662 * record->rec.entry.base.base.data_len
663 * record->data (a copy will be kmalloc'd if it cannot be embedded)
666 hammer_ip_add_record(struct hammer_transaction
*trans
, hammer_record_t record
)
668 hammer_inode_t ip
= record
->ip
;
671 record
->rec
.base
.base
.obj_id
= ip
->obj_id
;
672 record
->rec
.base
.base
.obj_type
= ip
->ino_rec
.base
.base
.obj_type
;
674 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
676 /* NOTE: copies record->data */
677 error
= hammer_mem_add(trans
, record
);
682 * Sync data from a buffer cache buffer (typically) to the filesystem. This
683 * is called via the strategy called from a cached data source. This code
684 * is responsible for actually writing a data record out to the disk.
686 * This can only occur non-historically (i.e. 'current' data only).
688 * The file offset must be HAMMER_BUFSIZE aligned but the data length
689 * can be truncated. The record (currently) always represents a BUFSIZE
690 * swath of space whether the data is truncated or not.
693 hammer_ip_sync_data(hammer_transaction_t trans
, hammer_inode_t ip
,
694 int64_t offset
, void *data
, int bytes
)
696 struct hammer_cursor cursor
;
697 hammer_record_ondisk_t rec
;
698 union hammer_btree_elm elm
;
699 hammer_off_t rec_offset
;
703 KKASSERT((offset
& HAMMER_BUFMASK
) == 0);
704 KKASSERT(trans
->type
== HAMMER_TRANS_FLS
);
706 error
= hammer_init_cursor(trans
, &cursor
, &ip
->cache
[0]);
709 cursor
.key_beg
.obj_id
= ip
->obj_id
;
710 cursor
.key_beg
.key
= offset
+ bytes
;
711 cursor
.key_beg
.create_tid
= trans
->tid
;
712 cursor
.key_beg
.delete_tid
= 0;
713 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
714 cursor
.asof
= trans
->tid
;
715 cursor
.flags
|= HAMMER_CURSOR_INSERT
;
716 cursor
.flags
|= HAMMER_CURSOR_BACKEND
;
719 * Issue a lookup to position the cursor.
721 error
= hammer_btree_lookup(&cursor
);
723 kprintf("hammer_ip_sync_data: duplicate data at "
724 "(%lld,%d) tid %016llx\n",
725 offset
, bytes
, trans
->tid
);
726 hammer_print_btree_elm(&cursor
.node
->ondisk
->elms
[cursor
.index
],
727 HAMMER_BTREE_TYPE_LEAF
, cursor
.index
);
728 panic("Duplicate data");
735 * Allocate record and data space. HAMMER_RECTYPE_DATA records
736 * can cross buffer boundaries so we may have to split our bcopy.
738 rec
= hammer_alloc_record(trans
, &rec_offset
, HAMMER_RECTYPE_DATA
,
739 &cursor
.record_buffer
,
741 &cursor
.data_buffer
, &error
);
744 if (hammer_debug_general
& 0x1000)
745 kprintf("OOB RECOR2 DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset
, rec
->base
.data_off
, rec
->base
.data_len
);
748 * Fill everything in and insert our B-Tree node.
750 * NOTE: hammer_alloc_record() has already marked the related
751 * buffers as modified. If we do it again we will generate
752 * unnecessary undo elements.
754 hammer_modify_buffer(trans
, cursor
.record_buffer
, NULL
, 0);
755 rec
->base
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
756 rec
->base
.base
.obj_id
= ip
->obj_id
;
757 rec
->base
.base
.key
= offset
+ bytes
;
758 rec
->base
.base
.create_tid
= trans
->tid
;
759 rec
->base
.base
.delete_tid
= 0;
760 rec
->base
.base
.rec_type
= HAMMER_RECTYPE_DATA
;
761 rec
->base
.data_crc
= crc32(data
, bytes
);
762 hammer_modify_buffer_done(cursor
.record_buffer
);
763 KKASSERT(rec
->base
.data_len
== bytes
);
765 hammer_modify_buffer(trans
, cursor
.data_buffer
, NULL
, 0);
766 bcopy(data
, bdata
, bytes
);
767 hammer_modify_buffer_done(cursor
.data_buffer
);
769 elm
.leaf
.base
= rec
->base
.base
;
770 elm
.leaf
.rec_offset
= rec_offset
;
771 elm
.leaf
.data_offset
= rec
->base
.data_off
;
772 elm
.leaf
.data_len
= bytes
;
773 elm
.leaf
.data_crc
= rec
->base
.data_crc
;
776 * Data records can wind up on-disk before the inode itself is
777 * on-disk. One must assume data records may be on-disk if either
778 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
780 ip
->flags
|= HAMMER_INODE_DONDISK
;
782 error
= hammer_btree_insert(&cursor
, &elm
);
786 hammer_blockmap_free(trans
, rec_offset
, HAMMER_RECORD_SIZE
);
788 hammer_done_cursor(&cursor
);
789 if (error
== EDEADLK
)
795 * Sync an in-memory record to the disk. This is called by the backend.
796 * This code is responsible for actually writing a record out to the disk.
798 * This routine can only be called by the backend and the record
799 * must have been interlocked with BE. It will remain interlocked on
800 * return. The caller is responsible for the record's disposition.
803 hammer_ip_sync_record(hammer_transaction_t trans
, hammer_record_t record
)
805 struct hammer_cursor cursor
;
806 hammer_record_ondisk_t rec
;
807 union hammer_btree_elm elm
;
808 hammer_off_t rec_offset
;
812 KKASSERT(record
->flush_state
== HAMMER_FST_FLUSH
);
813 KKASSERT(record
->flags
& HAMMER_RECF_INTERLOCK_BE
);
817 * Get a cursor, we will either be inserting or deleting.
819 error
= hammer_init_cursor(trans
, &cursor
, &record
->ip
->cache
[0]);
822 cursor
.key_beg
= record
->rec
.base
.base
;
823 cursor
.flags
|= HAMMER_CURSOR_BACKEND
;
826 * If we are deleting an exact match must be found on-disk.
828 if (record
->type
== HAMMER_MEM_RECORD_DEL
) {
829 error
= hammer_btree_lookup(&cursor
);
831 error
= hammer_ip_delete_record(&cursor
, trans
->tid
);
838 * Issue a lookup to position the cursor and locate the cluster. The
839 * target key should not exist. If we are creating a directory entry
840 * we may have to iterate the low 32 bits of the key to find an unused
843 cursor
.flags
|= HAMMER_CURSOR_INSERT
;
846 error
= hammer_btree_lookup(&cursor
);
849 if (record
->rec
.base
.base
.rec_type
!= HAMMER_RECTYPE_DIRENTRY
) {
850 kprintf("hammer_ip_sync_record: duplicate rec "
851 "at (%016llx)\n", record
->rec
.base
.base
.key
);
852 Debugger("duplicate record1");
856 if (++trans
->hmp
->namekey_iterator
== 0)
857 ++trans
->hmp
->namekey_iterator
;
858 record
->rec
.base
.base
.key
&= ~(0xFFFFFFFFLL
);
859 record
->rec
.base
.base
.key
|= trans
->hmp
->namekey_iterator
;
860 cursor
.key_beg
.key
= record
->rec
.base
.base
.key
;
866 * Allocate the record and data. The result buffers will be
867 * marked as being modified and further calls to
868 * hammer_modify_buffer() will result in unneeded UNDO records.
870 * Support zero-fill records (data == NULL and data_len != 0)
872 if (record
->data
== NULL
) {
873 rec
= hammer_alloc_record(trans
, &rec_offset
,
874 record
->rec
.base
.base
.rec_type
,
875 &cursor
.record_buffer
,
878 if (hammer_debug_general
& 0x1000)
879 kprintf("NULL RECORD DATA\n");
880 } else if (record
->flags
& HAMMER_RECF_INBAND
) {
881 rec
= hammer_alloc_record(trans
, &rec_offset
,
882 record
->rec
.base
.base
.rec_type
,
883 &cursor
.record_buffer
,
884 record
->rec
.base
.data_len
, &bdata
,
886 if (hammer_debug_general
& 0x1000)
887 kprintf("INBAND RECORD DATA %016llx DATA %016llx LEN=%d\n", rec_offset
, rec
->base
.data_off
, record
->rec
.base
.data_len
);
889 rec
= hammer_alloc_record(trans
, &rec_offset
,
890 record
->rec
.base
.base
.rec_type
,
891 &cursor
.record_buffer
,
892 record
->rec
.base
.data_len
, &bdata
,
893 &cursor
.data_buffer
, &error
);
894 if (hammer_debug_general
& 0x1000)
895 kprintf("OOB RECORD DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset
, rec
->base
.data_off
, record
->rec
.base
.data_len
);
902 * Fill in the remaining fields and insert our B-Tree node.
904 hammer_modify_buffer(trans
, cursor
.record_buffer
, NULL
, 0);
905 rec
->base
.base
= record
->rec
.base
.base
;
906 bcopy(&record
->rec
.base
+ 1, &rec
->base
+ 1,
907 HAMMER_RECORD_SIZE
- sizeof(record
->rec
.base
));
910 * Copy the data and deal with zero-fill support.
912 if (record
->data
&& (record
->flags
& HAMMER_RECF_INBAND
)) {
913 rec
->base
.data_crc
= crc32(record
->data
, rec
->base
.data_len
);
914 bcopy(record
->data
, bdata
, rec
->base
.data_len
);
915 } else if (record
->data
) {
916 rec
->base
.data_crc
= crc32(record
->data
, rec
->base
.data_len
);
917 hammer_modify_buffer(trans
, cursor
.data_buffer
, NULL
, 0);
918 bcopy(record
->data
, bdata
, rec
->base
.data_len
);
919 hammer_modify_buffer_done(cursor
.data_buffer
);
921 rec
->base
.data_len
= record
->rec
.base
.data_len
;
923 hammer_modify_buffer_done(cursor
.record_buffer
);
925 elm
.leaf
.base
= record
->rec
.base
.base
;
926 elm
.leaf
.rec_offset
= rec_offset
;
927 elm
.leaf
.data_offset
= rec
->base
.data_off
;
928 elm
.leaf
.data_len
= rec
->base
.data_len
;
929 elm
.leaf
.data_crc
= rec
->base
.data_crc
;
931 error
= hammer_btree_insert(&cursor
, &elm
);
934 * This occurs when the frontend creates a record and queues it to
935 * the backend, then tries to delete the record. The backend must
936 * still sync the record to the media as if it were not deleted,
937 * but must interlock with the frontend to ensure that the
938 * synchronized record is not visible to the frontend, which means
939 * converting it from an ADD record to a DEL record.
941 * The DEL record then masks the record synced to disk until another
942 * round can delete it for real.
944 if (error
== 0 && (record
->flags
& HAMMER_RECF_CONVERT_DELETE
)) {
945 KKASSERT(record
->type
== HAMMER_MEM_RECORD_ADD
);
946 record
->flags
&= ~HAMMER_RECF_DELETED_FE
;
947 record
->type
= HAMMER_MEM_RECORD_DEL
;
948 if (record
->flush_state
== HAMMER_FST_SETUP
) {
949 hammer_test_inode(record
->ip
);
950 hammer_test_inode(record
->target_ip
);
955 * If the error occured unwind the operation.
958 hammer_blockmap_free(trans
, rec_offset
, HAMMER_RECORD_SIZE
);
961 hammer_done_cursor(&cursor
);
962 if (error
== EDEADLK
)
968 * Add the record to the inode's rec_tree. The low 32 bits of a directory
969 * entry's key is used to deal with hash collisions in the upper 32 bits.
970 * A unique 64 bit key is generated in-memory and may be regenerated a
971 * second time when the directory record is flushed to the on-disk B-Tree.
973 * A referenced record is passed to this function. This function
974 * eats the reference. If an error occurs the record will be deleted.
976 * A copy of the temporary record->data pointer provided by the caller
981 hammer_mem_add(struct hammer_transaction
*trans
, hammer_record_t record
)
988 * Make a private copy of record->data
992 * Try to embed the data in extra space in the record
993 * union, otherwise allocate a copy.
995 bytes
= record
->rec
.base
.data_len
;
996 switch(record
->rec
.base
.base
.rec_type
) {
997 case HAMMER_RECTYPE_DIRENTRY
:
998 reclen
= offsetof(struct hammer_entry_record
, name
[0]);
1000 case HAMMER_RECTYPE_DATA
:
1001 reclen
= offsetof(struct hammer_data_record
, data
[0]);
1004 reclen
= sizeof(record
->rec
);
1007 if (reclen
+ bytes
<= HAMMER_RECORD_SIZE
) {
1008 bcopy(record
->data
, (char *)&record
->rec
+ reclen
,
1010 record
->data
= (void *)((char *)&record
->rec
+ reclen
);
1011 record
->flags
|= HAMMER_RECF_INBAND
;
1013 ++hammer_count_record_datas
;
1014 data
= kmalloc(bytes
, M_HAMMER
, M_WAITOK
);
1015 record
->flags
|= HAMMER_RECF_ALLOCDATA
;
1016 bcopy(record
->data
, data
, bytes
);
1017 record
->data
= data
;
1022 * Insert into the RB tree, find an unused iterator if this is
1023 * a directory entry.
1025 while (RB_INSERT(hammer_rec_rb_tree
, &record
->ip
->rec_tree
, record
)) {
1026 if (record
->rec
.base
.base
.rec_type
!= HAMMER_RECTYPE_DIRENTRY
){
1027 record
->flags
|= HAMMER_RECF_DELETED_FE
;
1028 hammer_rel_mem_record(record
);
1031 if (++trans
->hmp
->namekey_iterator
== 0)
1032 ++trans
->hmp
->namekey_iterator
;
1033 record
->rec
.base
.base
.key
&= ~(0xFFFFFFFFLL
);
1034 record
->rec
.base
.base
.key
|= trans
->hmp
->namekey_iterator
;
1036 record
->flags
|= HAMMER_RECF_ONRBTREE
;
1037 hammer_modify_inode(trans
, record
->ip
, HAMMER_INODE_XDIRTY
);
1038 hammer_rel_mem_record(record
);
1042 /************************************************************************
1043 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1044 ************************************************************************
1046 * These functions augment the B-Tree scanning functions in hammer_btree.c
1047 * by merging in-memory records with on-disk records.
1051 * Locate a particular record either in-memory or on-disk.
1053 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1054 * NOT be called to iterate results.
1057 hammer_ip_lookup(hammer_cursor_t cursor
, struct hammer_inode
*ip
)
1062 * If the element is in-memory return it without searching the
1065 error
= hammer_mem_lookup(cursor
, ip
);
1067 cursor
->record
= &cursor
->iprec
->rec
;
1070 if (error
!= ENOENT
)
1074 * If the inode has on-disk components search the on-disk B-Tree.
1076 if ((ip
->flags
& (HAMMER_INODE_ONDISK
|HAMMER_INODE_DONDISK
)) == 0)
1078 error
= hammer_btree_lookup(cursor
);
1080 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_RECORD
);
1085 * Locate the first record within the cursor's key_beg/key_end range,
1086 * restricted to a particular inode. 0 is returned on success, ENOENT
1087 * if no records matched the requested range, or some other error.
1089 * When 0 is returned hammer_ip_next() may be used to iterate additional
1090 * records within the requested range.
1092 * This function can return EDEADLK, requiring the caller to terminate
1093 * the cursor and try again.
1096 hammer_ip_first(hammer_cursor_t cursor
, struct hammer_inode
*ip
)
1101 * Clean up fields and setup for merged scan
1103 cursor
->flags
&= ~HAMMER_CURSOR_DELBTREE
;
1104 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
| HAMMER_CURSOR_ATEMEM
;
1105 cursor
->flags
|= HAMMER_CURSOR_DISKEOF
| HAMMER_CURSOR_MEMEOF
;
1106 if (cursor
->iprec
) {
1107 hammer_rel_mem_record(cursor
->iprec
);
1108 cursor
->iprec
= NULL
;
1112 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1113 * exact lookup so if we get ENOENT we have to call the iterate
1114 * function to validate the first record after the begin key.
1116 * The ATEDISK flag is used by hammer_btree_iterate to determine
1117 * whether it must index forwards or not. It is also used here
1118 * to select the next record from in-memory or on-disk.
1120 * EDEADLK can only occur if the lookup hit an empty internal
1121 * element and couldn't delete it. Since this could only occur
1122 * in-range, we can just iterate from the failure point.
1124 if (ip
->flags
& (HAMMER_INODE_ONDISK
|HAMMER_INODE_DONDISK
)) {
1125 error
= hammer_btree_lookup(cursor
);
1126 if (error
== ENOENT
|| error
== EDEADLK
) {
1127 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
1128 if (hammer_debug_general
& 0x2000)
1129 kprintf("error %d node %p %016llx index %d\n", error
, cursor
->node
, cursor
->node
->node_offset
, cursor
->index
);
1130 error
= hammer_btree_iterate(cursor
);
1132 if (error
&& error
!= ENOENT
)
1135 cursor
->flags
&= ~HAMMER_CURSOR_DISKEOF
;
1136 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
1138 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
1143 * Search the in-memory record list (Red-Black tree). Unlike the
1144 * B-Tree search, mem_first checks for records in the range.
1146 error
= hammer_mem_first(cursor
, ip
);
1147 if (error
&& error
!= ENOENT
)
1150 cursor
->flags
&= ~HAMMER_CURSOR_MEMEOF
;
1151 cursor
->flags
&= ~HAMMER_CURSOR_ATEMEM
;
1152 if (hammer_ip_iterate_mem_good(cursor
, cursor
->iprec
) == 0)
1153 cursor
->flags
|= HAMMER_CURSOR_ATEMEM
;
1157 * This will return the first matching record.
1159 return(hammer_ip_next(cursor
));
1163 * Retrieve the next record in a merged iteration within the bounds of the
1164 * cursor. This call may be made multiple times after the cursor has been
1165 * initially searched with hammer_ip_first().
1167 * 0 is returned on success, ENOENT if no further records match the
1168 * requested range, or some other error code is returned.
1171 hammer_ip_next(hammer_cursor_t cursor
)
1173 hammer_btree_elm_t elm
;
1174 hammer_record_t rec
, save
;
1180 * Load the current on-disk and in-memory record. If we ate any
1181 * records we have to get the next one.
1183 * If we deleted the last on-disk record we had scanned ATEDISK will
1184 * be clear and DELBTREE will be set, forcing a call to iterate. The
1185 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1186 * element. If ATEDISK is set, iterate will skip the 'current'
1189 * Get the next on-disk record
1191 if (cursor
->flags
& (HAMMER_CURSOR_ATEDISK
|HAMMER_CURSOR_DELBTREE
)) {
1192 if ((cursor
->flags
& HAMMER_CURSOR_DISKEOF
) == 0) {
1193 error
= hammer_btree_iterate(cursor
);
1194 cursor
->flags
&= ~HAMMER_CURSOR_DELBTREE
;
1196 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
1198 cursor
->flags
|= HAMMER_CURSOR_DISKEOF
|
1199 HAMMER_CURSOR_ATEDISK
;
1205 * Get the next in-memory record. The record can be ripped out
1206 * of the RB tree so we maintain a scan_info structure to track
1209 * hammer_rec_scan_cmp: Is the record still in our general range,
1210 * (non-inclusive of snapshot exclusions)?
1211 * hammer_rec_scan_callback: Is the record in our snapshot?
1213 if (cursor
->flags
& HAMMER_CURSOR_ATEMEM
) {
1214 if ((cursor
->flags
& HAMMER_CURSOR_MEMEOF
) == 0) {
1215 save
= cursor
->iprec
;
1216 cursor
->iprec
= NULL
;
1217 rec
= save
? hammer_rec_rb_tree_RB_NEXT(save
) : NULL
;
1219 if (hammer_rec_scan_cmp(rec
, cursor
) != 0)
1221 if (hammer_rec_scan_callback(rec
, cursor
) != 0)
1223 rec
= hammer_rec_rb_tree_RB_NEXT(rec
);
1226 hammer_rel_mem_record(save
);
1227 if (cursor
->iprec
) {
1228 KKASSERT(cursor
->iprec
== rec
);
1229 cursor
->flags
&= ~HAMMER_CURSOR_ATEMEM
;
1232 hammer_rec_rb_tree_RB_NEXT(rec
);
1235 cursor
->flags
|= HAMMER_CURSOR_MEMEOF
;
1241 * Extract either the disk or memory record depending on their
1242 * relative position.
1245 switch(cursor
->flags
& (HAMMER_CURSOR_ATEDISK
| HAMMER_CURSOR_ATEMEM
)) {
1248 * Both entries valid
1250 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
];
1251 r
= hammer_btree_cmp(&elm
->base
, &cursor
->iprec
->rec
.base
.base
);
1253 error
= hammer_btree_extract(cursor
,
1254 HAMMER_CURSOR_GET_RECORD
);
1255 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
1260 * If the entries match exactly the memory entry typically
1261 * specifies an on-disk deletion and we eat both entries.
1263 * If the in-memory record is not an on-disk deletion we
1264 * probably caught the syncer while it was syncing it to
1265 * the media. Since we hold a shared lock on the cursor,
1266 * the in-memory record had better be marked deleted at
1270 if (cursor
->iprec
->type
== HAMMER_MEM_RECORD_DEL
) {
1271 if ((cursor
->flags
& HAMMER_CURSOR_DELETE_VISIBILITY
) == 0) {
1272 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
1273 cursor
->flags
|= HAMMER_CURSOR_ATEMEM
;
1277 KKASSERT(hammer_ip_iterate_mem_good(cursor
, cursor
->iprec
) == 0);
1278 cursor
->flags
|= HAMMER_CURSOR_ATEMEM
;
1282 /* fall through to the memory entry */
1283 case HAMMER_CURSOR_ATEDISK
:
1285 * Only the memory entry is valid. If the record is
1286 * placemarking an on-disk deletion, we skip it unless
1287 * the caller wants special record visibility.
1289 cursor
->record
= &cursor
->iprec
->rec
;
1290 cursor
->flags
|= HAMMER_CURSOR_ATEMEM
;
1291 if (cursor
->iprec
->type
== HAMMER_MEM_RECORD_DEL
) {
1292 if ((cursor
->flags
& HAMMER_CURSOR_DELETE_VISIBILITY
) == 0)
1296 case HAMMER_CURSOR_ATEMEM
:
1298 * Only the disk entry is valid
1300 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_RECORD
);
1301 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
1305 * Neither entry is valid
1307 * XXX error not set properly
1309 cursor
->record
= NULL
;
1317 * Resolve the cursor->data pointer for the current cursor position in
1318 * a merged iteration.
1321 hammer_ip_resolve_data(hammer_cursor_t cursor
)
1325 if (cursor
->iprec
&& cursor
->record
== &cursor
->iprec
->rec
) {
1326 cursor
->data
= cursor
->iprec
->data
;
1329 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_DATA
);
1335 hammer_ip_resolve_record_and_data(hammer_cursor_t cursor
)
1339 if (cursor
->iprec
&& cursor
->record
== &cursor
->iprec
->rec
) {
1340 cursor
->data
= cursor
->iprec
->data
;
1343 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_DATA
|
1344 HAMMER_CURSOR_GET_RECORD
);
1350 * Delete all records within the specified range for inode ip.
1352 * NOTE: An unaligned range will cause new records to be added to cover
1353 * the edge cases. (XXX not implemented yet).
1355 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1357 * NOTE: Record keys for regular file data have to be special-cased since
1358 * they indicate the end of the range (key = base + bytes).
1361 hammer_ip_delete_range(hammer_transaction_t trans
, hammer_inode_t ip
,
1362 int64_t ran_beg
, int64_t ran_end
)
1364 struct hammer_cursor cursor
;
1365 hammer_record_ondisk_t rec
;
1366 hammer_base_elm_t base
;
1371 kprintf("delete_range %p %016llx-%016llx\n", ip
, ran_beg
, ran_end
);
1374 KKASSERT(trans
->type
== HAMMER_TRANS_FLS
);
1376 hammer_init_cursor(trans
, &cursor
, &ip
->cache
[0]);
1378 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1379 cursor
.key_beg
.create_tid
= 0;
1380 cursor
.key_beg
.delete_tid
= 0;
1381 cursor
.key_beg
.obj_type
= 0;
1382 cursor
.asof
= ip
->obj_asof
;
1383 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
1384 cursor
.flags
|= HAMMER_CURSOR_DELETE_VISIBILITY
;
1385 cursor
.flags
|= HAMMER_CURSOR_BACKEND
;
1387 cursor
.key_end
= cursor
.key_beg
;
1388 if (ip
->ino_rec
.base
.base
.obj_type
== HAMMER_OBJTYPE_DBFILE
) {
1389 cursor
.key_beg
.key
= ran_beg
;
1390 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
1391 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DB
;
1392 cursor
.key_end
.key
= ran_end
;
1395 * The key in the B-Tree is (base+bytes), so the first possible
1396 * matching key is ran_beg + 1.
1400 cursor
.key_beg
.key
= ran_beg
+ 1;
1401 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
1402 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
1404 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work around GCC-4 bug */
1405 if (tmp64
< ran_end
)
1406 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
1408 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
1410 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
1412 error
= hammer_ip_first(&cursor
, ip
);
1415 * Iterate through matching records and mark them as deleted.
1417 while (error
== 0) {
1418 rec
= cursor
.record
;
1419 base
= &rec
->base
.base
;
1421 KKASSERT(base
->delete_tid
== 0);
1424 * There may be overlap cases for regular file data. Also
1425 * remember the key for a regular file record is the offset
1426 * of the last byte of the record (base + len - 1), NOT the
1430 kprintf("delete_range rec_type %02x\n", base
->rec_type
);
1432 if (base
->rec_type
== HAMMER_RECTYPE_DATA
) {
1434 kprintf("delete_range loop key %016llx,%d\n",
1435 base
->key
- rec
->base
.data_len
, rec
->base
.data_len
);
1437 off
= base
->key
- rec
->base
.data_len
;
1439 * Check the left edge case. We currently do not
1440 * split existing records.
1442 if (off
< ran_beg
) {
1443 panic("hammer left edge case %016llx %d\n",
1444 base
->key
, rec
->base
.data_len
);
1448 * Check the right edge case. Note that the
1449 * record can be completely out of bounds, which
1450 * terminates the search.
1452 * base->key is exclusive of the right edge while
1453 * ran_end is inclusive of the right edge. The
1454 * (key - data_len) left boundary is inclusive.
1456 * XXX theory-check this test at some point, are
1457 * we missing a + 1 somewhere? Note that ran_end
1460 if (base
->key
- 1 > ran_end
) {
1461 if (base
->key
- rec
->base
.data_len
> ran_end
)
1463 panic("hammer right edge case\n");
1468 * Mark the record and B-Tree entry as deleted. This will
1469 * also physically delete the B-Tree entry, record, and
1470 * data if the retention policy dictates. The function
1471 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1472 * uses to perform a fixup.
1474 error
= hammer_ip_delete_record(&cursor
, trans
->tid
);
1477 error
= hammer_ip_next(&cursor
);
1479 hammer_done_cursor(&cursor
);
1480 if (error
== EDEADLK
)
1482 if (error
== ENOENT
)
1488 * Delete all user records associated with an inode except the inode record
1489 * itself. Directory entries are not deleted (they must be properly disposed
1490 * of or nlinks would get upset).
1493 hammer_ip_delete_range_all(hammer_transaction_t trans
, hammer_inode_t ip
,
1496 struct hammer_cursor cursor
;
1497 hammer_record_ondisk_t rec
;
1498 hammer_base_elm_t base
;
1501 KKASSERT(trans
->type
== HAMMER_TRANS_FLS
);
1503 hammer_init_cursor(trans
, &cursor
, &ip
->cache
[0]);
1505 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1506 cursor
.key_beg
.create_tid
= 0;
1507 cursor
.key_beg
.delete_tid
= 0;
1508 cursor
.key_beg
.obj_type
= 0;
1509 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_INODE
+ 1;
1510 cursor
.key_beg
.key
= HAMMER_MIN_KEY
;
1512 cursor
.key_end
= cursor
.key_beg
;
1513 cursor
.key_end
.rec_type
= 0xFFFF;
1514 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1516 cursor
.asof
= ip
->obj_asof
;
1517 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1518 cursor
.flags
|= HAMMER_CURSOR_DELETE_VISIBILITY
;
1519 cursor
.flags
|= HAMMER_CURSOR_BACKEND
;
1521 error
= hammer_ip_first(&cursor
, ip
);
1524 * Iterate through matching records and mark them as deleted.
1526 while (error
== 0) {
1527 rec
= cursor
.record
;
1528 base
= &rec
->base
.base
;
1530 KKASSERT(base
->delete_tid
== 0);
1533 * Mark the record and B-Tree entry as deleted. This will
1534 * also physically delete the B-Tree entry, record, and
1535 * data if the retention policy dictates. The function
1536 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1537 * uses to perform a fixup.
1539 * Directory entries (and delete-on-disk directory entries)
1540 * must be synced and cannot be deleted.
1542 if (rec
->base
.base
.rec_type
!= HAMMER_RECTYPE_DIRENTRY
) {
1543 error
= hammer_ip_delete_record(&cursor
, trans
->tid
);
1548 error
= hammer_ip_next(&cursor
);
1550 hammer_done_cursor(&cursor
);
1551 if (error
== EDEADLK
)
1553 if (error
== ENOENT
)
1559 * Delete the record at the current cursor. On success the cursor will
1560 * be positioned appropriately for an iteration but may no longer be at
1563 * This routine is only called from the backend.
1565 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1569 hammer_ip_delete_record(hammer_cursor_t cursor
, hammer_tid_t tid
)
1571 hammer_btree_elm_t elm
;
1576 KKASSERT(cursor
->flags
& HAMMER_CURSOR_BACKEND
);
1579 * In-memory (unsynchronized) records can simply be freed. This
1580 * only occurs in range iterations since all other records are
1581 * individually synchronized. Thus there should be no confusion with
1584 if (cursor
->record
== &cursor
->iprec
->rec
) {
1585 KKASSERT((cursor
->iprec
->flags
& HAMMER_RECF_INTERLOCK_BE
) ==0);
1586 cursor
->iprec
->flags
|= HAMMER_RECF_DELETED_FE
;
1587 cursor
->iprec
->flags
|= HAMMER_RECF_DELETED_BE
;
1592 * On-disk records are marked as deleted by updating their delete_tid.
1593 * This does not effect their position in the B-Tree (which is based
1594 * on their create_tid).
1596 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_RECORD
);
1598 hmp
= cursor
->node
->hmp
;
1602 error
= hammer_cursor_upgrade(cursor
);
1604 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
];
1605 hammer_modify_node(cursor
->trans
, cursor
->node
,
1606 &elm
->leaf
.base
.delete_tid
,
1607 sizeof(elm
->leaf
.base
.delete_tid
));
1608 elm
->leaf
.base
.delete_tid
= tid
;
1609 hammer_modify_node_done(cursor
->node
);
1612 * An on-disk record cannot have the same delete_tid
1613 * as its create_tid. In a chain of record updates
1614 * this could result in a duplicate record.
1616 KKASSERT(elm
->leaf
.base
.delete_tid
!= elm
->leaf
.base
.create_tid
);
1617 hammer_modify_buffer(cursor
->trans
, cursor
->record_buffer
, &cursor
->record
->base
.base
.delete_tid
, sizeof(hammer_tid_t
));
1618 cursor
->record
->base
.base
.delete_tid
= tid
;
1619 hammer_modify_buffer_done(cursor
->record_buffer
);
1624 * If we were mounted with the nohistory option, we physically
1625 * delete the record.
1627 if (hmp
->hflags
& HMNT_NOHISTORY
)
1630 if (error
== 0 && dodelete
) {
1631 error
= hammer_delete_at_cursor(cursor
, NULL
);
1633 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1641 hammer_delete_at_cursor(hammer_cursor_t cursor
, int64_t *stat_bytes
)
1643 hammer_btree_elm_t elm
;
1644 hammer_off_t rec_offset
;
1645 hammer_off_t data_offset
;
1650 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
];
1651 KKASSERT(elm
->base
.btype
== HAMMER_BTREE_TYPE_RECORD
);
1653 rec_offset
= elm
->leaf
.rec_offset
;
1654 data_offset
= elm
->leaf
.data_offset
;
1655 data_len
= elm
->leaf
.data_len
;
1656 rec_type
= elm
->leaf
.base
.rec_type
;
1658 error
= hammer_btree_delete(cursor
);
1661 * This forces a fixup for the iteration because
1662 * the cursor is now either sitting at the 'next'
1663 * element or sitting at the end of a leaf.
1665 if ((cursor
->flags
& HAMMER_CURSOR_DISKEOF
) == 0) {
1666 cursor
->flags
|= HAMMER_CURSOR_DELBTREE
;
1667 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
1671 hammer_blockmap_free(cursor
->trans
, rec_offset
,
1672 sizeof(union hammer_record_ondisk
));
1675 switch(data_offset
& HAMMER_OFF_ZONE_MASK
) {
1676 case HAMMER_ZONE_LARGE_DATA
:
1677 case HAMMER_ZONE_SMALL_DATA
:
1678 hammer_blockmap_free(cursor
->trans
,
1679 data_offset
, data_len
);
1686 kprintf("hammer_delete_at_cursor: %d:%d:%08x %08x/%d "
1687 "(%d remain in cluster)\n",
1688 cluster
->volume
->vol_no
, cluster
->clu_no
,
1689 rec_offset
, data_offset
, data_len
,
1690 cluster
->ondisk
->stat_records
);
1696 * Determine whether we can remove a directory. This routine checks whether
1697 * a directory is empty or not and enforces flush connectivity.
1699 * Flush connectivity requires that we block if the target directory is
1700 * currently flushing, otherwise it may not end up in the same flush group.
1702 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
1705 hammer_ip_check_directory_empty(hammer_transaction_t trans
,
1706 hammer_cursor_t parent_cursor
, hammer_inode_t ip
)
1708 struct hammer_cursor cursor
;
1713 * Check flush connectivity
1715 if (ip
->flush_state
!= HAMMER_FST_IDLE
) {
1717 hammer_done_cursor(parent_cursor
);
1718 hammer_flush_inode(ip
, HAMMER_FLUSH_FORCE
|HAMMER_FLUSH_SIGNAL
);
1719 hammer_wait_inode(ip
);
1725 * Check directory empty
1727 hammer_init_cursor(trans
, &cursor
, &ip
->cache
[0]);
1729 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1730 cursor
.key_beg
.create_tid
= 0;
1731 cursor
.key_beg
.delete_tid
= 0;
1732 cursor
.key_beg
.obj_type
= 0;
1733 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_INODE
+ 1;
1734 cursor
.key_beg
.key
= HAMMER_MIN_KEY
;
1736 cursor
.key_end
= cursor
.key_beg
;
1737 cursor
.key_end
.rec_type
= 0xFFFF;
1738 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1740 cursor
.asof
= ip
->obj_asof
;
1741 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1743 error
= hammer_ip_first(&cursor
, ip
);
1744 if (error
== ENOENT
)
1746 else if (error
== 0)
1748 hammer_done_cursor(&cursor
);