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.13 2007/12/31 05:33:12 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,
346 HAMMER_INODE_RDIRTY | HAMMER_INODE_TID);
347 error = hammer_mem_add(trans, record);
352 * Delete the directory entry and update the inode link count. The
353 * cursor must be seeked to the directory entry record being deleted.
355 * NOTE: HAMMER_CURSOR_DELETE may not have been set. XXX remove flag.
358 hammer_ip_del_directory(struct hammer_transaction *trans,
359 hammer_cursor_t cursor, struct hammer_inode *dip,
360 struct hammer_inode *ip)
364 error = hammer_ip_delete_record(cursor, trans->tid);
367 * One less link. The file may still be open in the OS even after
368 * all links have gone away so we don't destroy the inode's data
372 --ip->ino_rec.ino_nlinks;
373 hammer_modify_inode(trans, ip,
374 HAMMER_INODE_RDIRTY | HAMMER_INODE_TID);
375 if (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))
376 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,
419 HAMMER_INODE_RDIRTY | HAMMER_INODE_TID);
420 error = hammer_mem_add(trans, record);
425 * Sync data from a buffer cache buffer (typically) to the filesystem. This
426 * is called via the strategy called from a cached data source. This code
427 * is responsible for actually writing a data record out to the disk.
430 hammer_ip_sync_data(hammer_transaction_t trans, hammer_inode_t ip,
431 int64_t offset, void *data, int bytes,
432 struct hammer_cursor **spike)
434 struct hammer_cursor cursor;
435 hammer_record_ondisk_t rec;
436 union hammer_btree_elm elm;
440 error = hammer_init_cursor_ip(&cursor, ip);
443 cursor.key_beg.obj_id = ip->obj_id;
444 cursor.key_beg.key = offset + bytes;
445 cursor.key_beg.create_tid = trans->tid;
446 cursor.key_beg.delete_tid = 0;
447 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
448 cursor.flags = HAMMER_CURSOR_INSERT;
451 * Issue a lookup to position the cursor and locate the cluster
453 error = hammer_btree_lookup(&cursor);
455 kprintf("hammer_ip_sync_data: duplicate data at (%lld,%d)\n",
457 hammer_print_btree_elm(&cursor.node->ondisk->elms[cursor.index],
458 HAMMER_BTREE_TYPE_LEAF, cursor.index);
465 * Allocate record and data space now that we know which cluster
466 * the B-Tree node ended up in.
468 bdata = hammer_alloc_data(cursor.node->cluster, bytes, &error,
469 &cursor.data_buffer);
472 rec = hammer_alloc_record(cursor.node->cluster, &error,
473 &cursor.record_buffer);
478 * Fill everything in and insert our B-Tree node.
480 hammer_modify_buffer(cursor.record_buffer);
481 rec->base.base = cursor.key_beg;
482 rec->base.data_crc = crc32(data, bytes);
483 rec->base.rec_id = 0; /* XXX */
484 rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer, bdata);
485 rec->base.data_len = bytes;
486 hammer_modify_buffer_done(cursor.record_buffer);
488 hammer_modify_buffer(cursor.data_buffer);
489 bcopy(data, bdata, bytes);
490 hammer_modify_buffer_done(cursor.data_buffer);
492 elm.leaf.base = cursor.key_beg;
493 elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
494 elm.leaf.data_offset = rec->base.data_offset;
495 elm.leaf.data_len = bytes;
496 elm.leaf.data_crc = rec->base.data_crc;
498 error = hammer_btree_insert(&cursor, &elm);
502 hammer_free_record_ptr(cursor.record_buffer, rec);
504 hammer_free_data_ptr(cursor.data_buffer, bdata, bytes);
507 * If ENOSPC in cluster fill in the spike structure and return
511 hammer_load_spike(&cursor, spike);
512 hammer_done_cursor(&cursor);
517 * Sync an in-memory record to the disk. this is typically called via fsync
518 * from a cached record source. This code is responsible for actually
519 * writing a record out to the disk.
522 hammer_ip_sync_record(hammer_record_t record, struct hammer_cursor **spike)
524 struct hammer_cursor cursor;
525 hammer_record_ondisk_t rec;
527 union hammer_btree_elm elm;
531 error = hammer_init_cursor_ip(&cursor, record->ip);
534 cursor.key_beg = record->rec.base.base;
535 cursor.flags = HAMMER_CURSOR_INSERT;
538 * Issue a lookup to position the cursor and locate the cluster. The
539 * target key should not exist. If we are creating a directory entry
540 * we may have to iterate the low 32 bits of the key to find an unused
543 * If we run out of space trying to adjust the B-Tree for the
544 * insert, re-lookup without the insert flag so the cursor
545 * is properly positioned for the spike.
548 error = hammer_btree_lookup(&cursor);
550 if (record->rec.base.base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
551 hmp = cursor.node->cluster->volume->hmp;
552 if (++hmp->namekey_iterator == 0)
553 ++hmp->namekey_iterator;
554 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
555 record->rec.base.base.key |= hmp->namekey_iterator;
558 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
559 record->rec.base.base.key);
560 Debugger("duplicate record1");
567 * Mark the record as undergoing synchronization. Our cursor is
568 * holding a locked B-Tree node for the insertion which interlocks
569 * anyone trying to access this record.
571 * XXX There is still a race present related to iterations. An
572 * iteration may process the record, a sync may occur, and then
573 * later process the B-Tree element for the same record.
575 * We do not try to synchronize a deleted record.
577 if (record->flags & (HAMMER_RECF_DELETED | HAMMER_RECF_SYNCING)) {
581 record->flags |= HAMMER_RECF_SYNCING;
584 * Allocate record and data space now that we know which cluster
585 * the B-Tree node ended up in.
587 if (record->data == NULL ||
588 (record->flags & HAMMER_RECF_EMBEDDED_DATA)) {
589 bdata = record->data;
591 bdata = hammer_alloc_data(cursor.node->cluster,
592 record->rec.base.data_len, &error,
593 &cursor.data_buffer);
597 rec = hammer_alloc_record(cursor.node->cluster, &error,
598 &cursor.record_buffer);
603 * Fill everything in and insert our B-Tree node.
605 * XXX assign rec_id here
607 hammer_modify_buffer(cursor.record_buffer);
610 rec->base.data_crc = crc32(record->data,
611 record->rec.base.data_len);
612 if (record->flags & HAMMER_RECF_EMBEDDED_DATA) {
614 * Data embedded in record
616 rec->base.data_offset = ((char *)bdata -
617 (char *)&record->rec);
618 KKASSERT(rec->base.data_offset >= 0 &&
619 rec->base.data_offset + rec->base.data_len <=
621 rec->base.data_offset += hammer_bclu_offset(cursor.record_buffer, rec);
624 * Data separate from record
626 rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer,bdata);
627 hammer_modify_buffer(cursor.data_buffer);
628 bcopy(record->data, bdata, rec->base.data_len);
629 hammer_modify_buffer_done(cursor.data_buffer);
632 rec->base.rec_id = 0; /* XXX */
633 hammer_modify_buffer_done(cursor.record_buffer);
635 elm.leaf.base = cursor.key_beg;
636 elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
637 elm.leaf.data_offset = rec->base.data_offset;
638 elm.leaf.data_len = rec->base.data_len;
639 elm.leaf.data_crc = rec->base.data_crc;
641 error = hammer_btree_insert(&cursor, &elm);
644 * Clean up on success, or fall through on error.
647 record->flags |= HAMMER_RECF_DELETED;
648 record->flags &= ~HAMMER_RECF_SYNCING;
652 hammer_free_record_ptr(cursor.record_buffer, rec);
654 if (record->data && (record->flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
655 hammer_free_data_ptr(cursor.data_buffer, bdata,
656 record->rec.base.data_len);
659 record->flags &= ~HAMMER_RECF_SYNCING;
662 * If ENOSPC in cluster fill in the spike structure and return
666 hammer_load_spike(&cursor, spike);
667 hammer_done_cursor(&cursor);
672 * Write out a record using the specified cursor. The caller does not have
673 * to seek the cursor. The flags are used to determine whether the data
674 * (if any) is embedded in the record or not.
676 * The target cursor will be modified by this call. Note in particular
677 * that HAMMER_CURSOR_INSERT is set.
680 hammer_write_record(hammer_cursor_t cursor, hammer_record_ondisk_t orec,
681 void *data, int cursor_flags)
683 union hammer_btree_elm elm;
684 hammer_record_ondisk_t nrec;
688 cursor->key_beg = orec->base.base;
689 cursor->flags |= HAMMER_CURSOR_INSERT;
692 * Issue a lookup to position the cursor and locate the cluster. The
693 * target key should not exist.
695 * If we run out of space trying to adjust the B-Tree for the
696 * insert, re-lookup without the insert flag so the cursor
697 * is properly positioned for the spike.
699 error = hammer_btree_lookup(cursor);
701 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
702 orec->base.base.key);
703 Debugger("duplicate record2");
710 * Allocate record and data space now that we know which cluster
711 * the B-Tree node ended up in.
714 (cursor_flags & HAMMER_RECF_EMBEDDED_DATA)) {
717 bdata = hammer_alloc_data(cursor->node->cluster,
718 orec->base.data_len, &error,
719 &cursor->data_buffer);
723 nrec = hammer_alloc_record(cursor->node->cluster, &error,
724 &cursor->record_buffer);
729 * Fill everything in and insert our B-Tree node.
731 * XXX assign rec_id here
733 hammer_modify_buffer(cursor->record_buffer);
735 nrec->base.data_offset = 0;
737 nrec->base.data_crc = crc32(bdata, nrec->base.data_len);
738 if (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) {
740 * Data embedded in record
742 nrec->base.data_offset = ((char *)bdata - (char *)orec);
743 KKASSERT(nrec->base.data_offset >= 0 &&
744 nrec->base.data_offset + nrec->base.data_len <
746 nrec->base.data_offset += hammer_bclu_offset(cursor->record_buffer, nrec);
749 * Data separate from record
751 nrec->base.data_offset = hammer_bclu_offset(cursor->data_buffer, bdata);
752 hammer_modify_buffer(cursor->data_buffer);
753 bcopy(data, bdata, nrec->base.data_len);
754 hammer_modify_buffer_done(cursor->data_buffer);
757 nrec->base.rec_id = 0; /* XXX */
758 hammer_modify_buffer_done(cursor->record_buffer);
760 elm.leaf.base = nrec->base.base;
761 elm.leaf.rec_offset = hammer_bclu_offset(cursor->record_buffer, nrec);
762 elm.leaf.data_offset = nrec->base.data_offset;
763 elm.leaf.data_len = nrec->base.data_len;
764 elm.leaf.data_crc = nrec->base.data_crc;
766 error = hammer_btree_insert(cursor, &elm);
770 hammer_free_record_ptr(cursor->record_buffer, nrec);
772 if (data && (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
773 hammer_free_data_ptr(cursor->data_buffer, bdata,
774 orec->base.data_len);
777 /* leave cursor intact */
782 * Add the record to the inode's rec_tree. The low 32 bits of a directory
783 * entry's key is used to deal with hash collisions in the upper 32 bits.
784 * A unique 64 bit key is generated in-memory and may be regenerated a
785 * second time when the directory record is flushed to the on-disk B-Tree.
787 * A referenced record is passed to this function. This function
788 * eats the reference. If an error occurs the record will be deleted.
792 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
794 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
795 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
796 record->flags |= HAMMER_RECF_DELETED;
797 hammer_rel_mem_record(record);
800 if (++trans->hmp->namekey_iterator == 0)
801 ++trans->hmp->namekey_iterator;
802 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
803 record->rec.base.base.key |= trans->hmp->namekey_iterator;
805 record->flags |= HAMMER_RECF_ONRBTREE;
806 hammer_rel_mem_record(record);
810 /************************************************************************
811 * HAMMER INODE MERGED-RECORD FUNCTIONS *
812 ************************************************************************
814 * These functions augment the B-Tree scanning functions in hammer_btree.c
815 * by merging in-memory records with on-disk records.
819 * Locate a particular record either in-memory or on-disk.
821 * NOTE: This is basically a standalone routine, hammer_ip_next() may
822 * NOT be called to iterate results.
825 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
830 * If the element is in-memory return it without searching the
833 error = hammer_mem_lookup(cursor, ip);
835 cursor->record = &cursor->iprec->rec;
842 * If the inode has on-disk components search the on-disk B-Tree.
844 if ((ip->flags & HAMMER_INODE_ONDISK) == 0)
846 error = hammer_btree_lookup(cursor);
848 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
853 * Locate the first record within the cursor's key_beg/key_end range,
854 * restricted to a particular inode. 0 is returned on success, ENOENT
855 * if no records matched the requested range, or some other error.
857 * When 0 is returned hammer_ip_next() may be used to iterate additional
858 * records within the requested range.
861 hammer_ip_first(hammer_cursor_t cursor, struct hammer_inode *ip)
866 * Clean up fields and setup for merged scan
868 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
869 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
870 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
872 hammer_rel_mem_record(cursor->iprec);
873 cursor->iprec = NULL;
877 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
878 * exact lookup so if we get ENOENT we have to call the iterate
879 * function to validate the first record after the begin key.
881 * The ATEDISK flag is used by hammer_btree_iterate to determine
882 * whether it must index forwards or not. It is also used here
883 * to select the next record from in-memory or on-disk.
885 if (ip->flags & HAMMER_INODE_ONDISK) {
886 error = hammer_btree_lookup(cursor);
887 if (error == ENOENT) {
888 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
889 error = hammer_btree_iterate(cursor);
891 if (error && error != ENOENT)
894 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
895 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
897 cursor->flags |= HAMMER_CURSOR_ATEDISK;
902 * Search the in-memory record list (Red-Black tree). Unlike the
903 * B-Tree search, mem_first checks for records in the range.
905 error = hammer_mem_first(cursor, ip);
906 if (error && error != ENOENT)
909 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
910 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
914 * This will return the first matching record.
916 return(hammer_ip_next(cursor));
920 * Retrieve the next record in a merged iteration within the bounds of the
921 * cursor. This call may be made multiple times after the cursor has been
922 * initially searched with hammer_ip_first().
924 * 0 is returned on success, ENOENT if no further records match the
925 * requested range, or some other error code is returned.
928 hammer_ip_next(hammer_cursor_t cursor)
930 hammer_btree_elm_t elm;
936 * Load the current on-disk and in-memory record. If we ate any
937 * records we have to get the next one.
939 * If we deleted the last on-disk record we had scanned ATEDISK will
940 * be clear and DELBTREE will be set, forcing a call to iterate. The
941 * fact that ATEDISK is clear causes iterate to re-test the 'current'
942 * element. If ATEDISK is set, iterate will skip the 'current'
945 * Get the next on-disk record
947 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
948 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
949 error = hammer_btree_iterate(cursor);
950 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
952 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
954 cursor->flags |= HAMMER_CURSOR_DISKEOF |
955 HAMMER_CURSOR_ATEDISK;
960 * Get the next in-memory record. The record can be ripped out
961 * of the RB tree so we maintain a scan_info structure to track
964 * hammer_rec_scan_cmp: Is the record still in our general range,
965 * (non-inclusive of snapshot exclusions)?
966 * hammer_rec_scan_callback: Is the record in our snapshot?
968 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
969 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
971 hammer_rel_mem_record(cursor->iprec);
972 cursor->iprec = NULL;
974 rec = cursor->scan.node; /* next node */
976 if (hammer_rec_scan_cmp(rec, cursor) != 0)
978 if (hammer_rec_scan_callback(rec, cursor) != 0)
980 rec = hammer_rec_rb_tree_RB_NEXT(rec);
983 KKASSERT(cursor->iprec == rec);
984 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
986 hammer_rec_rb_tree_RB_NEXT(rec);
988 cursor->flags |= HAMMER_CURSOR_MEMEOF;
994 * Extract either the disk or memory record depending on their
998 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1001 * Both entries valid
1003 elm = &cursor->node->ondisk->elms[cursor->index];
1004 r = hammer_btree_cmp(&elm->base, &cursor->iprec->rec.base.base);
1006 error = hammer_btree_extract(cursor,
1007 HAMMER_CURSOR_GET_RECORD);
1008 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1011 /* fall through to the memory entry */
1012 case HAMMER_CURSOR_ATEDISK:
1014 * Only the memory entry is valid
1016 cursor->record = &cursor->iprec->rec;
1017 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1019 case HAMMER_CURSOR_ATEMEM:
1021 * Only the disk entry is valid
1023 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1024 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1028 * Neither entry is valid
1030 * XXX error not set properly
1032 cursor->record = NULL;
1040 * Resolve the cursor->data pointer for the current cursor position in
1041 * a merged iteration.
1044 hammer_ip_resolve_data(hammer_cursor_t cursor)
1048 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1049 cursor->data = cursor->iprec->data;
1052 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1058 * Delete all records within the specified range for inode ip.
1060 * NOTE: An unaligned range will cause new records to be added to cover
1061 * the edge cases. (XXX not implemented yet).
1063 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1065 * NOTE: Record keys for regular file data have to be special-cased since
1066 * they indicate the end of the range (key = base + bytes).
1068 * NOTE: The spike structure must be filled in if we return ENOSPC.
1071 hammer_ip_delete_range(hammer_transaction_t trans, hammer_inode_t ip,
1072 int64_t ran_beg, int64_t ran_end,
1073 struct hammer_cursor **spike)
1075 struct hammer_cursor cursor;
1076 hammer_record_ondisk_t rec;
1077 hammer_base_elm_t base;
1081 hammer_init_cursor_ip(&cursor, ip);
1083 cursor.key_beg.obj_id = ip->obj_id;
1084 cursor.key_beg.create_tid = ip->obj_asof;
1085 cursor.key_beg.delete_tid = 0;
1086 cursor.key_beg.obj_type = 0;
1088 cursor.key_end = cursor.key_beg;
1089 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1090 cursor.key_beg.key = ran_beg;
1091 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1092 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1093 cursor.key_end.key = ran_end;
1096 * The key in the B-Tree is (base+bytes), so the first possible
1097 * matching key is ran_beg + 1.
1101 cursor.key_beg.key = ran_beg + 1;
1102 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1103 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1105 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1106 if (tmp64 < ran_end)
1107 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1109 cursor.key_end.key = ran_end + MAXPHYS + 1;
1111 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1113 error = hammer_ip_first(&cursor, ip);
1116 * Iterate through matching records and mark them as deleted.
1118 while (error == 0) {
1119 rec = cursor.record;
1120 base = &rec->base.base;
1122 KKASSERT(base->delete_tid == 0);
1125 * There may be overlap cases for regular file data. Also
1126 * remember the key for a regular file record is the offset
1127 * of the last byte of the record (base + len - 1), NOT the
1131 kprintf("delete_range rec_type %02x\n", base->rec_type);
1133 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1135 kprintf("delete_range loop key %016llx\n",
1136 base->key - rec->base.data_len);
1138 off = base->key - rec->base.data_len;
1140 * Check the left edge case. We currently do not
1141 * split existing records.
1143 if (off < ran_beg) {
1144 panic("hammer left edge case %016llx %d\n",
1145 base->key, rec->base.data_len);
1149 * Check the right edge case. Note that the
1150 * record can be completely out of bounds, which
1151 * terminates the search.
1153 * base->key is exclusive of the right edge while
1154 * ran_end is inclusive of the right edge. The
1155 * (key - data_len) left boundary is inclusive.
1157 * XXX theory-check this test at some point, are
1158 * we missing a + 1 somewhere? Note that ran_end
1161 if (base->key > ran_end) {
1162 if (base->key - rec->base.data_len > ran_end) {
1163 kprintf("right edge OOB\n");
1166 panic("hammer right edge case\n");
1171 * Mark the record and B-Tree entry as deleted. This will
1172 * also physically delete the B-Tree entry, record, and
1173 * data if the retention policy dictates. The function
1174 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1175 * uses to perform a fixup.
1177 error = hammer_ip_delete_record(&cursor, trans->tid);
1180 error = hammer_ip_next(&cursor);
1182 hammer_done_cursor(&cursor);
1183 if (error == ENOENT)
1189 * Delete all records associated with an inode except the inode record
1193 hammer_ip_delete_range_all(hammer_transaction_t trans, hammer_inode_t ip)
1195 struct hammer_cursor cursor;
1196 hammer_record_ondisk_t rec;
1197 hammer_base_elm_t base;
1200 hammer_init_cursor_ip(&cursor, ip);
1202 cursor.key_beg.obj_id = ip->obj_id;
1203 cursor.key_beg.create_tid = ip->obj_asof;
1204 cursor.key_beg.delete_tid = 0;
1205 cursor.key_beg.obj_type = 0;
1206 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1207 cursor.key_beg.key = HAMMER_MIN_KEY;
1209 cursor.key_end = cursor.key_beg;
1210 cursor.key_end.rec_type = 0xFFFF;
1211 cursor.key_end.key = HAMMER_MAX_KEY;
1213 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1215 error = hammer_ip_first(&cursor, ip);
1218 * Iterate through matching records and mark them as deleted.
1220 while (error == 0) {
1221 rec = cursor.record;
1222 base = &rec->base.base;
1224 KKASSERT(base->delete_tid == 0);
1227 * Mark the record and B-Tree entry as deleted. This will
1228 * also physically delete the B-Tree entry, record, and
1229 * data if the retention policy dictates. The function
1230 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1231 * uses to perform a fixup.
1233 error = hammer_ip_delete_record(&cursor, trans->tid);
1236 error = hammer_ip_next(&cursor);
1238 hammer_done_cursor(&cursor);
1239 if (error == ENOENT)
1245 * Delete the record at the current cursor
1248 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1250 hammer_btree_elm_t elm;
1255 * In-memory (unsynchronized) records can simply be freed.
1257 if (cursor->record == &cursor->iprec->rec) {
1258 cursor->iprec->flags |= HAMMER_RECF_DELETED;
1263 * On-disk records are marked as deleted by updating their delete_tid.
1265 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1267 hmp = cursor->node->cluster->volume->hmp;
1270 hammer_modify_buffer(cursor->record_buffer);
1271 cursor->record->base.base.delete_tid = tid;
1272 hammer_modify_buffer_done(cursor->record_buffer);
1273 hammer_modify_node(cursor->node);
1274 elm = &cursor->node->ondisk->elms[cursor->index];
1275 elm->leaf.base.delete_tid = tid;
1276 hammer_modify_node_done(cursor->node);
1280 * If we were mounted with the nohistory option, we physically
1281 * delete the record.
1283 if (error == 0 && (hmp->hflags & HMNT_NOHISTORY)) {
1285 int32_t data_offset;
1287 hammer_cluster_t cluster;
1289 rec_offset = elm->leaf.rec_offset;
1290 data_offset = elm->leaf.data_offset;
1291 data_len = elm->leaf.data_len;
1293 kprintf("hammer_ip_delete_record: %08x %08x/%d\n",
1294 rec_offset, data_offset, data_len);
1296 cluster = cursor->node->cluster;
1297 hammer_ref_cluster(cluster);
1299 error = hammer_btree_delete(cursor);
1302 * This forces a fixup for the iteration because
1303 * the cursor is now either sitting at the 'next'
1304 * element or sitting at the end of a leaf.
1306 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1307 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1308 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1310 hammer_free_record(cluster, rec_offset);
1311 if (data_offset && (data_offset - rec_offset < 0 ||
1312 data_offset - rec_offset >= HAMMER_RECORD_SIZE)) {
1313 hammer_free_data(cluster, data_offset,data_len);
1316 hammer_rel_cluster(cluster, 0);
1318 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1326 * Determine whether a directory is empty or not. Returns 0 if the directory
1327 * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1330 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1332 struct hammer_cursor cursor;
1335 hammer_init_cursor_ip(&cursor, ip);
1337 cursor.key_beg.obj_id = ip->obj_id;
1338 cursor.key_beg.create_tid = ip->obj_asof;
1339 cursor.key_beg.delete_tid = 0;
1340 cursor.key_beg.obj_type = 0;
1341 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1342 cursor.key_beg.key = HAMMER_MIN_KEY;
1344 cursor.key_end = cursor.key_beg;
1345 cursor.key_end.rec_type = 0xFFFF;
1346 cursor.key_end.key = HAMMER_MAX_KEY;
1348 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1350 error = hammer_ip_first(&cursor, ip);
1351 if (error == ENOENT)
1353 else if (error == 0)
1355 hammer_done_cursor(&cursor);