2 * Copyright (c) 2007 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.18 2008/01/10 07:41:03 dillon Exp $
39 static int hammer_mem_add(hammer_transaction_t trans
,
40 hammer_record_t record
);
41 static int hammer_mem_lookup(hammer_cursor_t cursor
, hammer_inode_t ip
);
42 static int hammer_mem_first(hammer_cursor_t cursor
, hammer_inode_t ip
);
45 * Red-black tree support.
48 hammer_rec_rb_compare(hammer_record_t rec1
, hammer_record_t rec2
)
50 if (rec1
->rec
.base
.base
.rec_type
< rec2
->rec
.base
.base
.rec_type
)
52 if (rec1
->rec
.base
.base
.rec_type
> rec2
->rec
.base
.base
.rec_type
)
55 if (rec1
->rec
.base
.base
.key
< rec2
->rec
.base
.base
.key
)
57 if (rec1
->rec
.base
.base
.key
> rec2
->rec
.base
.base
.key
)
60 if (rec1
->rec
.base
.base
.create_tid
< rec2
->rec
.base
.base
.create_tid
)
62 if (rec1
->rec
.base
.base
.create_tid
> rec2
->rec
.base
.base
.create_tid
)
68 hammer_rec_compare(hammer_base_elm_t info
, hammer_record_t rec
)
70 if (info
->rec_type
< rec
->rec
.base
.base
.rec_type
)
72 if (info
->rec_type
> rec
->rec
.base
.base
.rec_type
)
75 if (info
->key
< rec
->rec
.base
.base
.key
)
77 if (info
->key
> rec
->rec
.base
.base
.key
)
81 * This test has a number of special cases. create_tid in key1 is
82 * the as-of transction id, and delete_tid in key1 is NOT USED.
84 * A key1->create_tid of 0 matches any record regardles of when
85 * it was created or destroyed. 0xFFFFFFFFFFFFFFFFULL should be
86 * used to search for the most current state of the object.
88 * key2->create_tid is a HAMMER record and will never be
89 * 0. key2->delete_tid is the deletion transaction id or 0 if
90 * the record has not yet been deleted.
92 if (info
->create_tid
) {
93 if (info
->create_tid
< rec
->rec
.base
.base
.create_tid
)
95 if (rec
->rec
.base
.base
.delete_tid
&&
96 info
->create_tid
>= rec
->rec
.base
.base
.delete_tid
) {
104 * RB_SCAN comparison code for hammer_mem_first(). The argument order
105 * is reversed so the comparison result has to be negated. key_beg and
106 * key_end are both range-inclusive.
108 * The creation timestamp can cause hammer_rec_compare() to return -1 or +1.
109 * These do not stop the scan.
111 * Localized deletions are not cached in-memory.
115 hammer_rec_scan_cmp(hammer_record_t rec
, void *data
)
117 hammer_cursor_t cursor
= data
;
120 r
= hammer_rec_compare(&cursor
->key_beg
, rec
);
125 r
= hammer_rec_compare(&cursor
->key_end
, rec
);
131 RB_GENERATE(hammer_rec_rb_tree
, hammer_record
, rb_node
, hammer_rec_rb_compare
);
132 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree
, INFO
, hammer_record
, rb_node
,
133 hammer_rec_compare
, hammer_base_elm_t
);
136 * Allocate a record for the caller to finish filling in. The record is
137 * returned referenced.
140 hammer_alloc_mem_record(hammer_inode_t ip
)
142 hammer_record_t record
;
144 ++hammer_count_records
;
145 record
= kmalloc(sizeof(*record
), M_HAMMER
, M_WAITOK
|M_ZERO
);
147 hammer_ref(&record
->lock
);
152 * Release a memory record. Records marked for deletion are immediately
153 * removed from the RB-Tree but otherwise left intact until the last ref
157 hammer_rel_mem_record(struct hammer_record
*record
)
159 hammer_unref(&record
->lock
);
160 if (record
->flags
& HAMMER_RECF_DELETED
) {
161 if (record
->flags
& HAMMER_RECF_ONRBTREE
) {
162 RB_REMOVE(hammer_rec_rb_tree
, &record
->ip
->rec_tree
,
164 record
->flags
&= ~HAMMER_RECF_ONRBTREE
;
166 if (record
->lock
.refs
== 0) {
167 if (record
->flags
& HAMMER_RECF_ALLOCDATA
) {
168 --hammer_count_record_datas
;
169 kfree(record
->data
, M_HAMMER
);
170 record
->flags
&= ~HAMMER_RECF_ALLOCDATA
;
173 --hammer_count_records
;
174 kfree(record
, M_HAMMER
);
180 * Lookup an in-memory record given the key specified in the cursor. Works
181 * just like hammer_btree_lookup() but operates on an inode's in-memory
184 * The lookup must fail if the record is marked for deferred deletion.
188 hammer_mem_lookup(hammer_cursor_t cursor
, hammer_inode_t ip
)
193 hammer_rel_mem_record(cursor
->iprec
);
194 cursor
->iprec
= NULL
;
197 hammer_rec_rb_tree_scan_info_done(&cursor
->scan
,
198 &cursor
->ip
->rec_tree
);
201 hammer_rec_rb_tree_scan_info_link(&cursor
->scan
, &ip
->rec_tree
);
202 cursor
->scan
.node
= NULL
;
203 cursor
->iprec
= hammer_rec_rb_tree_RB_LOOKUP_INFO(
204 &ip
->rec_tree
, &cursor
->key_beg
);
205 if (cursor
->iprec
== NULL
) {
208 hammer_ref(&cursor
->iprec
->lock
);
215 * hammer_mem_first() - locate the first in-memory record matching the
218 * The RB_SCAN function we use is designed as a callback. We terminate it
219 * (return -1) as soon as we get a match.
223 hammer_rec_scan_callback(hammer_record_t rec
, void *data
)
225 hammer_cursor_t cursor
= data
;
228 * Skip if not visible due to our as-of TID
230 if (cursor
->key_beg
.create_tid
) {
231 if (cursor
->key_beg
.create_tid
< rec
->rec
.base
.base
.create_tid
)
233 if (rec
->rec
.base
.base
.delete_tid
&&
234 cursor
->key_beg
.create_tid
>=
235 rec
->rec
.base
.base
.delete_tid
) {
241 * Return the first matching record and stop the scan
243 if (cursor
->iprec
== NULL
) {
245 hammer_ref(&rec
->lock
);
253 hammer_mem_first(hammer_cursor_t cursor
, hammer_inode_t ip
)
256 hammer_rel_mem_record(cursor
->iprec
);
257 cursor
->iprec
= NULL
;
260 hammer_rec_rb_tree_scan_info_done(&cursor
->scan
,
261 &cursor
->ip
->rec_tree
);
264 hammer_rec_rb_tree_scan_info_link(&cursor
->scan
, &ip
->rec_tree
);
266 cursor
->scan
.node
= NULL
;
267 hammer_rec_rb_tree_RB_SCAN(&ip
->rec_tree
, hammer_rec_scan_cmp
,
268 hammer_rec_scan_callback
, cursor
);
271 * Adjust scan.node and keep it linked into the RB-tree so we can
272 * hold the cursor through third party modifications of the RB-tree.
275 cursor
->scan
.node
= hammer_rec_rb_tree_RB_NEXT(cursor
->iprec
);
282 hammer_mem_done(hammer_cursor_t cursor
)
285 hammer_rec_rb_tree_scan_info_done(&cursor
->scan
,
286 &cursor
->ip
->rec_tree
);
290 hammer_rel_mem_record(cursor
->iprec
);
291 cursor
->iprec
= NULL
;
295 /************************************************************************
296 * HAMMER IN-MEMORY RECORD FUNCTIONS *
297 ************************************************************************
299 * These functions manipulate in-memory records. Such records typically
300 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
304 * Add a directory entry (dip,ncp) which references inode (ip).
306 * Note that the low 32 bits of the namekey are set temporarily to create
307 * a unique in-memory record, and may be modified a second time when the
308 * record is synchronized to disk. In particular, the low 32 bits cannot be
309 * all 0's when synching to disk, which is not handled here.
312 hammer_ip_add_directory(struct hammer_transaction
*trans
,
313 struct hammer_inode
*dip
, struct namecache
*ncp
,
314 struct hammer_inode
*ip
)
316 hammer_record_t record
;
320 record
= hammer_alloc_mem_record(dip
);
322 bytes
= ncp
->nc_nlen
; /* NOTE: terminating \0 is NOT included */
323 if (++trans
->hmp
->namekey_iterator
== 0)
324 ++trans
->hmp
->namekey_iterator
;
326 record
->rec
.entry
.base
.base
.obj_id
= dip
->obj_id
;
327 record
->rec
.entry
.base
.base
.key
=
328 hammer_directory_namekey(ncp
->nc_name
, bytes
);
329 record
->rec
.entry
.base
.base
.key
+= trans
->hmp
->namekey_iterator
;
330 record
->rec
.entry
.base
.base
.create_tid
= trans
->tid
;
331 record
->rec
.entry
.base
.base
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
332 record
->rec
.entry
.base
.base
.obj_type
= ip
->ino_rec
.base
.base
.obj_type
;
333 record
->rec
.entry
.obj_id
= ip
->obj_id
;
334 if (bytes
<= sizeof(record
->rec
.entry
.den_name
)) {
335 record
->data
= (void *)record
->rec
.entry
.den_name
;
336 record
->flags
|= HAMMER_RECF_EMBEDDED_DATA
;
338 ++hammer_count_record_datas
;
339 record
->data
= kmalloc(bytes
, M_HAMMER
, M_WAITOK
);
340 record
->flags
|= HAMMER_RECF_ALLOCDATA
;
342 bcopy(ncp
->nc_name
, record
->data
, bytes
);
343 record
->rec
.entry
.base
.data_len
= bytes
;
344 ++ip
->ino_rec
.ino_nlinks
;
345 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
346 error
= hammer_mem_add(trans
, record
);
351 * Delete the directory entry and update the inode link count. The
352 * cursor must be seeked to the directory entry record being deleted.
354 * NOTE: HAMMER_CURSOR_DELETE may not have been set. XXX remove flag.
357 hammer_ip_del_directory(struct hammer_transaction
*trans
,
358 hammer_cursor_t cursor
, struct hammer_inode
*dip
,
359 struct hammer_inode
*ip
)
363 error
= hammer_ip_delete_record(cursor
, trans
->tid
);
366 * One less link. The file may still be open in the OS even after
367 * all links have gone away so we only try to sync if the OS has
368 * no references and nlinks falls to 0.
371 --ip
->ino_rec
.ino_nlinks
;
372 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
373 if (ip
->ino_rec
.ino_nlinks
== 0 &&
374 (ip
->vp
== NULL
|| (ip
->vp
->v_flag
& VINACTIVE
))) {
375 hammer_sync_inode(ip
, MNT_NOWAIT
, 1);
383 * Add a record to an inode.
385 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
386 * initialize the following additional fields:
388 * record->rec.entry.base.base.key
389 * record->rec.entry.base.base.rec_type
390 * record->rec.entry.base.base.data_len
391 * record->data (a copy will be kmalloc'd if not embedded)
394 hammer_ip_add_record(struct hammer_transaction
*trans
, hammer_record_t record
)
396 hammer_inode_t ip
= record
->ip
;
401 record
->rec
.base
.base
.obj_id
= ip
->obj_id
;
402 record
->rec
.base
.base
.create_tid
= trans
->tid
;
403 record
->rec
.base
.base
.obj_type
= ip
->ino_rec
.base
.base
.obj_type
;
404 bytes
= record
->rec
.base
.data_len
;
407 if ((char *)record
->data
< (char *)&record
->rec
||
408 (char *)record
->data
>= (char *)(&record
->rec
+ 1)) {
409 ++hammer_count_record_datas
;
410 data
= kmalloc(bytes
, M_HAMMER
, M_WAITOK
);
411 record
->flags
|= HAMMER_RECF_ALLOCDATA
;
412 bcopy(record
->data
, data
, bytes
);
415 record
->flags
|= HAMMER_RECF_EMBEDDED_DATA
;
418 hammer_modify_inode(trans
, ip
, HAMMER_INODE_RDIRTY
);
419 error
= hammer_mem_add(trans
, record
);
424 * Sync data from a buffer cache buffer (typically) to the filesystem. This
425 * is called via the strategy called from a cached data source. This code
426 * is responsible for actually writing a data record out to the disk.
429 hammer_ip_sync_data(hammer_transaction_t trans
, hammer_inode_t ip
,
430 int64_t offset
, void *data
, int bytes
,
431 struct hammer_cursor
**spike
)
433 struct hammer_cursor cursor
;
434 hammer_record_ondisk_t rec
;
435 union hammer_btree_elm elm
;
439 error
= hammer_init_cursor_hmp(&cursor
, &ip
->cache
[0], ip
->hmp
);
442 cursor
.key_beg
.obj_id
= ip
->obj_id
;
443 cursor
.key_beg
.key
= offset
+ bytes
;
444 cursor
.key_beg
.create_tid
= trans
->tid
;
445 cursor
.key_beg
.delete_tid
= 0;
446 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
447 cursor
.flags
= HAMMER_CURSOR_INSERT
;
450 * Issue a lookup to position the cursor and locate the cluster
452 error
= hammer_btree_lookup(&cursor
);
454 kprintf("hammer_ip_sync_data: duplicate data at (%lld,%d)\n",
456 hammer_print_btree_elm(&cursor
.node
->ondisk
->elms
[cursor
.index
],
457 HAMMER_BTREE_TYPE_LEAF
, cursor
.index
);
464 * Allocate record and data space now that we know which cluster
465 * the B-Tree node ended up in.
467 bdata
= hammer_alloc_data(cursor
.node
->cluster
, bytes
, &error
,
468 &cursor
.data_buffer
);
471 rec
= hammer_alloc_record(cursor
.node
->cluster
, &error
,
472 &cursor
.record_buffer
);
477 * Fill everything in and insert our B-Tree node.
479 hammer_modify_buffer(cursor
.record_buffer
);
480 rec
->base
.base
= cursor
.key_beg
;
481 rec
->base
.data_crc
= crc32(data
, bytes
);
482 rec
->base
.rec_id
= 0; /* XXX */
483 rec
->base
.data_offset
= hammer_bclu_offset(cursor
.data_buffer
, bdata
);
484 rec
->base
.data_len
= bytes
;
486 hammer_modify_buffer(cursor
.data_buffer
);
487 bcopy(data
, bdata
, bytes
);
489 elm
.leaf
.base
= cursor
.key_beg
;
490 elm
.leaf
.rec_offset
= hammer_bclu_offset(cursor
.record_buffer
, rec
);
491 elm
.leaf
.data_offset
= rec
->base
.data_offset
;
492 elm
.leaf
.data_len
= bytes
;
493 elm
.leaf
.data_crc
= rec
->base
.data_crc
;
496 * Data records can wind up on-disk before the inode itself is
497 * on-disk. One must assume data records may be on-disk if either
498 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
500 ip
->flags
|= HAMMER_INODE_DONDISK
;
502 error
= hammer_btree_insert(&cursor
, &elm
);
504 hammer_update_syncid(cursor
.record_buffer
->cluster
, trans
->tid
);
508 hammer_free_record_ptr(cursor
.record_buffer
, rec
);
510 hammer_free_data_ptr(cursor
.data_buffer
, bdata
, bytes
);
513 * If ENOSPC in cluster fill in the spike structure and return
517 hammer_load_spike(&cursor
, spike
);
518 hammer_done_cursor(&cursor
);
523 * Sync an in-memory record to the disk. this is typically called via fsync
524 * from a cached record source. This code is responsible for actually
525 * writing a record out to the disk.
528 hammer_ip_sync_record(hammer_record_t record
, struct hammer_cursor
**spike
)
530 struct hammer_cursor cursor
;
531 hammer_record_ondisk_t rec
;
533 union hammer_btree_elm elm
;
537 error
= hammer_init_cursor_hmp(&cursor
, &record
->ip
->cache
[0],
541 cursor
.key_beg
= record
->rec
.base
.base
;
542 cursor
.flags
= HAMMER_CURSOR_INSERT
;
545 * Issue a lookup to position the cursor and locate the cluster. The
546 * target key should not exist. If we are creating a directory entry
547 * we may have to iterate the low 32 bits of the key to find an unused
550 * If we run out of space trying to adjust the B-Tree for the
551 * insert, re-lookup without the insert flag so the cursor
552 * is properly positioned for the spike.
555 error
= hammer_btree_lookup(&cursor
);
557 if (record
->rec
.base
.base
.rec_type
== HAMMER_RECTYPE_DIRENTRY
) {
558 hmp
= cursor
.node
->cluster
->volume
->hmp
;
559 if (++hmp
->namekey_iterator
== 0)
560 ++hmp
->namekey_iterator
;
561 record
->rec
.base
.base
.key
&= ~(0xFFFFFFFFLL
);
562 record
->rec
.base
.base
.key
|= hmp
->namekey_iterator
;
565 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
566 record
->rec
.base
.base
.key
);
567 Debugger("duplicate record1");
574 * Mark the record as undergoing synchronization. Our cursor is
575 * holding a locked B-Tree node for the insertion which interlocks
576 * anyone trying to access this record.
578 * XXX There is still a race present related to iterations. An
579 * iteration may process the record, a sync may occur, and then
580 * later process the B-Tree element for the same record.
582 * We do not try to synchronize a deleted record.
584 if (record
->flags
& (HAMMER_RECF_DELETED
| HAMMER_RECF_SYNCING
)) {
588 record
->flags
|= HAMMER_RECF_SYNCING
;
591 * Allocate record and data space now that we know which cluster
592 * the B-Tree node ended up in.
594 if (record
->data
== NULL
||
595 (record
->flags
& HAMMER_RECF_EMBEDDED_DATA
)) {
596 bdata
= record
->data
;
598 bdata
= hammer_alloc_data(cursor
.node
->cluster
,
599 record
->rec
.base
.data_len
, &error
,
600 &cursor
.data_buffer
);
604 rec
= hammer_alloc_record(cursor
.node
->cluster
, &error
,
605 &cursor
.record_buffer
);
610 * Fill everything in and insert our B-Tree node.
612 * XXX assign rec_id here
614 hammer_modify_buffer(cursor
.record_buffer
);
617 rec
->base
.data_crc
= crc32(record
->data
,
618 record
->rec
.base
.data_len
);
619 if (record
->flags
& HAMMER_RECF_EMBEDDED_DATA
) {
621 * Data embedded in record
623 rec
->base
.data_offset
= ((char *)bdata
-
624 (char *)&record
->rec
);
625 KKASSERT(rec
->base
.data_offset
>= 0 &&
626 rec
->base
.data_offset
+ rec
->base
.data_len
<=
628 rec
->base
.data_offset
+= hammer_bclu_offset(cursor
.record_buffer
, rec
);
631 * Data separate from record
633 rec
->base
.data_offset
= hammer_bclu_offset(cursor
.data_buffer
,bdata
);
634 hammer_modify_buffer(cursor
.data_buffer
);
635 bcopy(record
->data
, bdata
, rec
->base
.data_len
);
638 rec
->base
.rec_id
= 0; /* XXX */
640 elm
.leaf
.base
= cursor
.key_beg
;
641 elm
.leaf
.rec_offset
= hammer_bclu_offset(cursor
.record_buffer
, rec
);
642 elm
.leaf
.data_offset
= rec
->base
.data_offset
;
643 elm
.leaf
.data_len
= rec
->base
.data_len
;
644 elm
.leaf
.data_crc
= rec
->base
.data_crc
;
646 error
= hammer_btree_insert(&cursor
, &elm
);
649 * Clean up on success, or fall through on error.
652 record
->flags
|= HAMMER_RECF_DELETED
;
653 record
->flags
&= ~HAMMER_RECF_SYNCING
;
654 hammer_update_syncid(cursor
.record_buffer
->cluster
,
655 record
->rec
.base
.base
.create_tid
);
659 hammer_free_record_ptr(cursor
.record_buffer
, rec
);
661 if (record
->data
&& (record
->flags
& HAMMER_RECF_EMBEDDED_DATA
) == 0) {
662 hammer_free_data_ptr(cursor
.data_buffer
, bdata
,
663 record
->rec
.base
.data_len
);
666 record
->flags
&= ~HAMMER_RECF_SYNCING
;
669 * If ENOSPC in cluster fill in the spike structure and return
673 hammer_load_spike(&cursor
, spike
);
674 hammer_done_cursor(&cursor
);
679 * Write out a record using the specified cursor. The caller does not have
680 * to seek the cursor. The flags are used to determine whether the data
681 * (if any) is embedded in the record or not.
683 * The target cursor will be modified by this call. Note in particular
684 * that HAMMER_CURSOR_INSERT is set.
687 hammer_write_record(hammer_cursor_t cursor
, hammer_record_ondisk_t orec
,
688 void *data
, int cursor_flags
)
690 union hammer_btree_elm elm
;
691 hammer_record_ondisk_t nrec
;
695 cursor
->key_beg
= orec
->base
.base
;
696 cursor
->flags
|= HAMMER_CURSOR_INSERT
;
699 * Issue a lookup to position the cursor and locate the cluster. The
700 * target key should not exist.
702 * If we run out of space trying to adjust the B-Tree for the
703 * insert, re-lookup without the insert flag so the cursor
704 * is properly positioned for the spike.
706 error
= hammer_btree_lookup(cursor
);
708 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
709 orec
->base
.base
.key
);
710 Debugger("duplicate record2");
717 * Allocate record and data space now that we know which cluster
718 * the B-Tree node ended up in.
721 (cursor_flags
& HAMMER_RECF_EMBEDDED_DATA
)) {
724 bdata
= hammer_alloc_data(cursor
->node
->cluster
,
725 orec
->base
.data_len
, &error
,
726 &cursor
->data_buffer
);
730 nrec
= hammer_alloc_record(cursor
->node
->cluster
, &error
,
731 &cursor
->record_buffer
);
736 * Fill everything in and insert our B-Tree node.
738 * XXX assign rec_id here
740 hammer_modify_buffer(cursor
->record_buffer
);
742 nrec
->base
.data_offset
= 0;
744 nrec
->base
.data_crc
= crc32(bdata
, nrec
->base
.data_len
);
745 if (cursor_flags
& HAMMER_RECF_EMBEDDED_DATA
) {
747 * Data embedded in record
749 nrec
->base
.data_offset
= ((char *)bdata
- (char *)orec
);
750 KKASSERT(nrec
->base
.data_offset
>= 0 &&
751 nrec
->base
.data_offset
+ nrec
->base
.data_len
<
753 nrec
->base
.data_offset
+= hammer_bclu_offset(cursor
->record_buffer
, nrec
);
756 * Data separate from record
758 nrec
->base
.data_offset
= hammer_bclu_offset(cursor
->data_buffer
, bdata
);
759 hammer_modify_buffer(cursor
->data_buffer
);
760 bcopy(data
, bdata
, nrec
->base
.data_len
);
763 nrec
->base
.rec_id
= 0; /* XXX */
765 elm
.leaf
.base
= nrec
->base
.base
;
766 elm
.leaf
.rec_offset
= hammer_bclu_offset(cursor
->record_buffer
, nrec
);
767 elm
.leaf
.data_offset
= nrec
->base
.data_offset
;
768 elm
.leaf
.data_len
= nrec
->base
.data_len
;
769 elm
.leaf
.data_crc
= nrec
->base
.data_crc
;
771 error
= hammer_btree_insert(cursor
, &elm
);
773 hammer_update_syncid(cursor
->record_buffer
->cluster
,
774 nrec
->base
.base
.create_tid
);
778 hammer_free_record_ptr(cursor
->record_buffer
, nrec
);
780 if (data
&& (cursor_flags
& HAMMER_RECF_EMBEDDED_DATA
) == 0) {
781 hammer_free_data_ptr(cursor
->data_buffer
, bdata
,
782 orec
->base
.data_len
);
785 /* leave cursor intact */
790 * Add the record to the inode's rec_tree. The low 32 bits of a directory
791 * entry's key is used to deal with hash collisions in the upper 32 bits.
792 * A unique 64 bit key is generated in-memory and may be regenerated a
793 * second time when the directory record is flushed to the on-disk B-Tree.
795 * A referenced record is passed to this function. This function
796 * eats the reference. If an error occurs the record will be deleted.
800 hammer_mem_add(struct hammer_transaction
*trans
, hammer_record_t record
)
802 while (RB_INSERT(hammer_rec_rb_tree
, &record
->ip
->rec_tree
, record
)) {
803 if (record
->rec
.base
.base
.rec_type
!= HAMMER_RECTYPE_DIRENTRY
){
804 record
->flags
|= HAMMER_RECF_DELETED
;
805 hammer_rel_mem_record(record
);
808 if (++trans
->hmp
->namekey_iterator
== 0)
809 ++trans
->hmp
->namekey_iterator
;
810 record
->rec
.base
.base
.key
&= ~(0xFFFFFFFFLL
);
811 record
->rec
.base
.base
.key
|= trans
->hmp
->namekey_iterator
;
813 record
->flags
|= HAMMER_RECF_ONRBTREE
;
814 hammer_modify_inode(trans
, record
->ip
, HAMMER_INODE_XDIRTY
);
815 hammer_rel_mem_record(record
);
819 /************************************************************************
820 * HAMMER INODE MERGED-RECORD FUNCTIONS *
821 ************************************************************************
823 * These functions augment the B-Tree scanning functions in hammer_btree.c
824 * by merging in-memory records with on-disk records.
828 * Locate a particular record either in-memory or on-disk.
830 * NOTE: This is basically a standalone routine, hammer_ip_next() may
831 * NOT be called to iterate results.
834 hammer_ip_lookup(hammer_cursor_t cursor
, struct hammer_inode
*ip
)
839 * If the element is in-memory return it without searching the
842 error
= hammer_mem_lookup(cursor
, ip
);
844 cursor
->record
= &cursor
->iprec
->rec
;
851 * If the inode has on-disk components search the on-disk B-Tree.
853 if ((ip
->flags
& (HAMMER_INODE_ONDISK
|HAMMER_INODE_DONDISK
)) == 0)
855 error
= hammer_btree_lookup(cursor
);
857 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_RECORD
);
862 * Locate the first record within the cursor's key_beg/key_end range,
863 * restricted to a particular inode. 0 is returned on success, ENOENT
864 * if no records matched the requested range, or some other error.
866 * When 0 is returned hammer_ip_next() may be used to iterate additional
867 * records within the requested range.
870 hammer_ip_first(hammer_cursor_t cursor
, struct hammer_inode
*ip
)
875 * Clean up fields and setup for merged scan
877 cursor
->flags
&= ~HAMMER_CURSOR_DELBTREE
;
878 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
| HAMMER_CURSOR_ATEMEM
;
879 cursor
->flags
|= HAMMER_CURSOR_DISKEOF
| HAMMER_CURSOR_MEMEOF
;
881 hammer_rel_mem_record(cursor
->iprec
);
882 cursor
->iprec
= NULL
;
886 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
887 * exact lookup so if we get ENOENT we have to call the iterate
888 * function to validate the first record after the begin key.
890 * The ATEDISK flag is used by hammer_btree_iterate to determine
891 * whether it must index forwards or not. It is also used here
892 * to select the next record from in-memory or on-disk.
894 if (ip
->flags
& (HAMMER_INODE_ONDISK
|HAMMER_INODE_DONDISK
)) {
895 error
= hammer_btree_lookup(cursor
);
896 if (error
== ENOENT
) {
897 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
898 error
= hammer_btree_iterate(cursor
);
900 if (error
&& error
!= ENOENT
)
903 cursor
->flags
&= ~HAMMER_CURSOR_DISKEOF
;
904 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
906 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
911 * Search the in-memory record list (Red-Black tree). Unlike the
912 * B-Tree search, mem_first checks for records in the range.
914 error
= hammer_mem_first(cursor
, ip
);
915 if (error
&& error
!= ENOENT
)
918 cursor
->flags
&= ~HAMMER_CURSOR_MEMEOF
;
919 cursor
->flags
&= ~HAMMER_CURSOR_ATEMEM
;
923 * This will return the first matching record.
925 return(hammer_ip_next(cursor
));
929 * Retrieve the next record in a merged iteration within the bounds of the
930 * cursor. This call may be made multiple times after the cursor has been
931 * initially searched with hammer_ip_first().
933 * 0 is returned on success, ENOENT if no further records match the
934 * requested range, or some other error code is returned.
937 hammer_ip_next(hammer_cursor_t cursor
)
939 hammer_btree_elm_t elm
;
945 * Load the current on-disk and in-memory record. If we ate any
946 * records we have to get the next one.
948 * If we deleted the last on-disk record we had scanned ATEDISK will
949 * be clear and DELBTREE will be set, forcing a call to iterate. The
950 * fact that ATEDISK is clear causes iterate to re-test the 'current'
951 * element. If ATEDISK is set, iterate will skip the 'current'
954 * Get the next on-disk record
956 if (cursor
->flags
& (HAMMER_CURSOR_ATEDISK
|HAMMER_CURSOR_DELBTREE
)) {
957 if ((cursor
->flags
& HAMMER_CURSOR_DISKEOF
) == 0) {
958 error
= hammer_btree_iterate(cursor
);
959 cursor
->flags
&= ~HAMMER_CURSOR_DELBTREE
;
961 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
963 cursor
->flags
|= HAMMER_CURSOR_DISKEOF
|
964 HAMMER_CURSOR_ATEDISK
;
969 * Get the next in-memory record. The record can be ripped out
970 * of the RB tree so we maintain a scan_info structure to track
973 * hammer_rec_scan_cmp: Is the record still in our general range,
974 * (non-inclusive of snapshot exclusions)?
975 * hammer_rec_scan_callback: Is the record in our snapshot?
977 if (cursor
->flags
& HAMMER_CURSOR_ATEMEM
) {
978 if ((cursor
->flags
& HAMMER_CURSOR_MEMEOF
) == 0) {
980 hammer_rel_mem_record(cursor
->iprec
);
981 cursor
->iprec
= NULL
;
983 rec
= cursor
->scan
.node
; /* next node */
985 if (hammer_rec_scan_cmp(rec
, cursor
) != 0)
987 if (hammer_rec_scan_callback(rec
, cursor
) != 0)
989 rec
= hammer_rec_rb_tree_RB_NEXT(rec
);
992 KKASSERT(cursor
->iprec
== rec
);
993 cursor
->flags
&= ~HAMMER_CURSOR_ATEMEM
;
995 hammer_rec_rb_tree_RB_NEXT(rec
);
997 cursor
->flags
|= HAMMER_CURSOR_MEMEOF
;
1003 * Extract either the disk or memory record depending on their
1004 * relative position.
1007 switch(cursor
->flags
& (HAMMER_CURSOR_ATEDISK
| HAMMER_CURSOR_ATEMEM
)) {
1010 * Both entries valid
1012 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
];
1013 r
= hammer_btree_cmp(&elm
->base
, &cursor
->iprec
->rec
.base
.base
);
1015 error
= hammer_btree_extract(cursor
,
1016 HAMMER_CURSOR_GET_RECORD
);
1017 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
1020 /* fall through to the memory entry */
1021 case HAMMER_CURSOR_ATEDISK
:
1023 * Only the memory entry is valid
1025 cursor
->record
= &cursor
->iprec
->rec
;
1026 cursor
->flags
|= HAMMER_CURSOR_ATEMEM
;
1028 case HAMMER_CURSOR_ATEMEM
:
1030 * Only the disk entry is valid
1032 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_RECORD
);
1033 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
1037 * Neither entry is valid
1039 * XXX error not set properly
1041 cursor
->record
= NULL
;
1049 * Resolve the cursor->data pointer for the current cursor position in
1050 * a merged iteration.
1053 hammer_ip_resolve_data(hammer_cursor_t cursor
)
1057 if (cursor
->iprec
&& cursor
->record
== &cursor
->iprec
->rec
) {
1058 cursor
->data
= cursor
->iprec
->data
;
1061 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_DATA
);
1067 * Delete all records within the specified range for inode ip.
1069 * NOTE: An unaligned range will cause new records to be added to cover
1070 * the edge cases. (XXX not implemented yet).
1072 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1074 * NOTE: Record keys for regular file data have to be special-cased since
1075 * they indicate the end of the range (key = base + bytes).
1077 * NOTE: The spike structure must be filled in if we return ENOSPC.
1080 hammer_ip_delete_range(hammer_transaction_t trans
, hammer_inode_t ip
,
1081 int64_t ran_beg
, int64_t ran_end
,
1082 struct hammer_cursor
**spike
)
1084 struct hammer_cursor cursor
;
1085 hammer_record_ondisk_t rec
;
1086 hammer_base_elm_t base
;
1090 hammer_init_cursor_hmp(&cursor
, &ip
->cache
[0], ip
->hmp
);
1092 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1093 cursor
.key_beg
.create_tid
= ip
->obj_asof
;
1094 cursor
.key_beg
.delete_tid
= 0;
1095 cursor
.key_beg
.obj_type
= 0;
1097 cursor
.key_end
= cursor
.key_beg
;
1098 if (ip
->ino_rec
.base
.base
.obj_type
== HAMMER_OBJTYPE_DBFILE
) {
1099 cursor
.key_beg
.key
= ran_beg
;
1100 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
1101 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DB
;
1102 cursor
.key_end
.key
= ran_end
;
1105 * The key in the B-Tree is (base+bytes), so the first possible
1106 * matching key is ran_beg + 1.
1110 cursor
.key_beg
.key
= ran_beg
+ 1;
1111 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
1112 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
1114 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work around GCC-4 bug */
1115 if (tmp64
< ran_end
)
1116 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
1118 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
1120 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
1122 error
= hammer_ip_first(&cursor
, ip
);
1125 * Iterate through matching records and mark them as deleted.
1127 while (error
== 0) {
1128 rec
= cursor
.record
;
1129 base
= &rec
->base
.base
;
1131 KKASSERT(base
->delete_tid
== 0);
1134 * There may be overlap cases for regular file data. Also
1135 * remember the key for a regular file record is the offset
1136 * of the last byte of the record (base + len - 1), NOT the
1140 kprintf("delete_range rec_type %02x\n", base
->rec_type
);
1142 if (base
->rec_type
== HAMMER_RECTYPE_DATA
) {
1144 kprintf("delete_range loop key %016llx\n",
1145 base
->key
- rec
->base
.data_len
);
1147 off
= base
->key
- rec
->base
.data_len
;
1149 * Check the left edge case. We currently do not
1150 * split existing records.
1152 if (off
< ran_beg
) {
1153 panic("hammer left edge case %016llx %d\n",
1154 base
->key
, rec
->base
.data_len
);
1158 * Check the right edge case. Note that the
1159 * record can be completely out of bounds, which
1160 * terminates the search.
1162 * base->key is exclusive of the right edge while
1163 * ran_end is inclusive of the right edge. The
1164 * (key - data_len) left boundary is inclusive.
1166 * XXX theory-check this test at some point, are
1167 * we missing a + 1 somewhere? Note that ran_end
1170 if (base
->key
- 1 > ran_end
) {
1171 if (base
->key
- rec
->base
.data_len
> ran_end
)
1173 panic("hammer right edge case\n");
1178 * Mark the record and B-Tree entry as deleted. This will
1179 * also physically delete the B-Tree entry, record, and
1180 * data if the retention policy dictates. The function
1181 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1182 * uses to perform a fixup.
1184 error
= hammer_ip_delete_record(&cursor
, trans
->tid
);
1187 error
= hammer_ip_next(&cursor
);
1189 hammer_done_cursor(&cursor
);
1190 if (error
== ENOENT
)
1196 * Delete all records associated with an inode except the inode record
1200 hammer_ip_delete_range_all(hammer_transaction_t trans
, hammer_inode_t ip
)
1202 struct hammer_cursor cursor
;
1203 hammer_record_ondisk_t rec
;
1204 hammer_base_elm_t base
;
1207 hammer_init_cursor_hmp(&cursor
, &ip
->cache
[0], ip
->hmp
);
1209 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1210 cursor
.key_beg
.create_tid
= ip
->obj_asof
;
1211 cursor
.key_beg
.delete_tid
= 0;
1212 cursor
.key_beg
.obj_type
= 0;
1213 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_INODE
+ 1;
1214 cursor
.key_beg
.key
= HAMMER_MIN_KEY
;
1216 cursor
.key_end
= cursor
.key_beg
;
1217 cursor
.key_end
.rec_type
= 0xFFFF;
1218 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1220 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
1222 error
= hammer_ip_first(&cursor
, ip
);
1225 * Iterate through matching records and mark them as deleted.
1227 while (error
== 0) {
1228 rec
= cursor
.record
;
1229 base
= &rec
->base
.base
;
1231 KKASSERT(base
->delete_tid
== 0);
1234 * Mark the record and B-Tree entry as deleted. This will
1235 * also physically delete the B-Tree entry, record, and
1236 * data if the retention policy dictates. The function
1237 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1238 * uses to perform a fixup.
1240 error
= hammer_ip_delete_record(&cursor
, trans
->tid
);
1243 error
= hammer_ip_next(&cursor
);
1245 hammer_done_cursor(&cursor
);
1246 if (error
== ENOENT
)
1252 * Delete the record at the current cursor
1255 hammer_ip_delete_record(hammer_cursor_t cursor
, hammer_tid_t tid
)
1257 hammer_btree_elm_t elm
;
1262 * In-memory (unsynchronized) records can simply be freed.
1264 if (cursor
->record
== &cursor
->iprec
->rec
) {
1265 cursor
->iprec
->flags
|= HAMMER_RECF_DELETED
;
1270 * On-disk records are marked as deleted by updating their delete_tid.
1272 error
= hammer_btree_extract(cursor
, HAMMER_CURSOR_GET_RECORD
);
1274 hmp
= cursor
->node
->cluster
->volume
->hmp
;
1277 hammer_modify_buffer(cursor
->record_buffer
);
1278 cursor
->record
->base
.base
.delete_tid
= tid
;
1280 hammer_modify_node(cursor
->node
);
1281 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
];
1282 elm
->leaf
.base
.delete_tid
= tid
;
1283 hammer_update_syncid(cursor
->record_buffer
->cluster
, tid
);
1287 * If we were mounted with the nohistory option, we physically
1288 * delete the record.
1290 if (error
== 0 && (hmp
->hflags
& HMNT_NOHISTORY
)) {
1292 int32_t data_offset
;
1294 hammer_cluster_t cluster
;
1296 rec_offset
= elm
->leaf
.rec_offset
;
1297 data_offset
= elm
->leaf
.data_offset
;
1298 data_len
= elm
->leaf
.data_len
;
1300 kprintf("hammer_ip_delete_record: %08x %08x/%d\n",
1301 rec_offset
, data_offset
, data_len
);
1303 cluster
= cursor
->node
->cluster
;
1304 hammer_ref_cluster(cluster
);
1306 error
= hammer_btree_delete(cursor
);
1309 * This forces a fixup for the iteration because
1310 * the cursor is now either sitting at the 'next'
1311 * element or sitting at the end of a leaf.
1313 if ((cursor
->flags
& HAMMER_CURSOR_DISKEOF
) == 0) {
1314 cursor
->flags
|= HAMMER_CURSOR_DELBTREE
;
1315 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
1317 hammer_free_record(cluster
, rec_offset
);
1318 if (data_offset
&& (data_offset
- rec_offset
< 0 ||
1319 data_offset
- rec_offset
>= HAMMER_RECORD_SIZE
)) {
1320 hammer_free_data(cluster
, data_offset
,data_len
);
1323 hammer_rel_cluster(cluster
, 0);
1325 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1333 * Determine whether a directory is empty or not. Returns 0 if the directory
1334 * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1337 hammer_ip_check_directory_empty(hammer_transaction_t trans
, hammer_inode_t ip
)
1339 struct hammer_cursor cursor
;
1342 hammer_init_cursor_hmp(&cursor
, &ip
->cache
[0], ip
->hmp
);
1344 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1345 cursor
.key_beg
.create_tid
= ip
->obj_asof
;
1346 cursor
.key_beg
.delete_tid
= 0;
1347 cursor
.key_beg
.obj_type
= 0;
1348 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_INODE
+ 1;
1349 cursor
.key_beg
.key
= HAMMER_MIN_KEY
;
1351 cursor
.key_end
= cursor
.key_beg
;
1352 cursor
.key_end
.rec_type
= 0xFFFF;
1353 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1355 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
1357 error
= hammer_ip_first(&cursor
, ip
);
1358 if (error
== ENOENT
)
1360 else if (error
== 0)
1362 hammer_done_cursor(&cursor
);