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.37 2008/03/20 06:08:40 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)
75 hammer_rec_compare(hammer_base_elm_t info, hammer_record_t rec)
77 if (info->rec_type < rec->rec.base.base.rec_type)
79 if (info->rec_type > rec->rec.base.base.rec_type)
82 if (info->key < rec->rec.base.base.key)
84 if (info->key > rec->rec.base.base.key)
87 if (info->create_tid == 0) {
88 if (rec->rec.base.base.create_tid == 0)
92 if (rec->rec.base.base.create_tid == 0)
94 if (info->create_tid < rec->rec.base.base.create_tid)
96 if (info->create_tid > rec->rec.base.base.create_tid)
102 * RB_SCAN comparison code for hammer_mem_first(). The argument order
103 * is reversed so the comparison result has to be negated. key_beg and
104 * key_end are both range-inclusive.
106 * The creation timestamp can cause hammer_rec_compare() to return -1 or +1.
107 * These do not stop the scan.
109 * Localized deletions are not cached in-memory.
113 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
115 hammer_cursor_t cursor = data;
118 r = hammer_rec_compare(&cursor->key_beg, rec);
121 r = hammer_rec_compare(&cursor->key_end, rec);
127 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
128 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
129 hammer_rec_compare, hammer_base_elm_t);
132 * Allocate a record for the caller to finish filling in. The record is
133 * returned referenced.
136 hammer_alloc_mem_record(hammer_inode_t ip)
138 hammer_record_t record;
140 ++hammer_count_records;
141 record = kmalloc(sizeof(*record), M_HAMMER, M_WAITOK|M_ZERO);
143 record->rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
144 hammer_ref(&record->lock);
149 * Release a memory record. Records marked for deletion are immediately
150 * removed from the RB-Tree but otherwise left intact until the last ref
154 hammer_rel_mem_record(struct hammer_record *record)
156 hammer_unref(&record->lock);
158 if (record->flags & HAMMER_RECF_DELETED) {
159 if (record->flags & HAMMER_RECF_ONRBTREE) {
160 RB_REMOVE(hammer_rec_rb_tree, &record->ip->rec_tree,
162 record->flags &= ~HAMMER_RECF_ONRBTREE;
164 if (record->lock.refs == 0) {
165 if (record->flags & HAMMER_RECF_ALLOCDATA) {
166 --hammer_count_record_datas;
167 kfree(record->data, M_HAMMER);
168 record->flags &= ~HAMMER_RECF_ALLOCDATA;
171 --hammer_count_records;
172 kfree(record, M_HAMMER);
178 * If someone wanted the record wake them up.
180 if (record->flags & HAMMER_RECF_WANTED) {
181 record->flags &= ~HAMMER_RECF_WANTED;
187 * Lookup an in-memory record given the key specified in the cursor. Works
188 * just like hammer_btree_lookup() but operates on an inode's in-memory
191 * The lookup must fail if the record is marked for deferred deletion.
195 hammer_mem_lookup(hammer_cursor_t cursor, hammer_inode_t ip)
200 hammer_rel_mem_record(cursor->iprec);
201 cursor->iprec = NULL;
204 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
205 &cursor->ip->rec_tree);
208 hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
209 cursor->scan.node = NULL;
210 cursor->iprec = hammer_rec_rb_tree_RB_LOOKUP_INFO(
211 &ip->rec_tree, &cursor->key_beg);
212 if (cursor->iprec == NULL) {
215 hammer_ref(&cursor->iprec->lock);
222 * hammer_mem_first() - locate the first in-memory record matching the
225 * The RB_SCAN function we use is designed as a callback. We terminate it
226 * (return -1) as soon as we get a match.
230 hammer_rec_scan_callback(hammer_record_t rec, void *data)
232 hammer_cursor_t cursor = data;
235 * We terminate on success, so this should be NULL on entry.
237 KKASSERT(cursor->iprec == NULL);
240 * Skip if the record was marked deleted
242 if (rec->flags & HAMMER_RECF_DELETED)
246 * Skip if not visible due to our as-of TID
248 if (cursor->flags & HAMMER_CURSOR_ASOF) {
249 if (cursor->asof < rec->rec.base.base.create_tid)
251 if (rec->rec.base.base.delete_tid &&
252 cursor->asof >= rec->rec.base.base.delete_tid) {
258 * Block if currently being synchronized to disk, otherwise we
259 * may get a duplicate. Wakeup the syncer if it's stuck on
262 hammer_ref(&rec->lock);
264 while (rec->flags & HAMMER_RECF_SYNCING) {
265 rec->flags |= HAMMER_RECF_WANTED;
266 tsleep(rec, 0, "hmrrc2", 0);
271 * The record may have been deleted while we were blocked.
273 if (rec->flags & HAMMER_RECF_DELETED) {
274 hammer_rel_mem_record(cursor->iprec);
279 * Set the matching record and stop the scan.
287 hammer_mem_first(hammer_cursor_t cursor, hammer_inode_t ip)
290 hammer_rel_mem_record(cursor->iprec);
291 cursor->iprec = NULL;
294 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
295 &cursor->ip->rec_tree);
298 hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
300 cursor->scan.node = NULL;
301 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
302 hammer_rec_scan_callback, cursor);
305 * Adjust scan.node and keep it linked into the RB-tree so we can
306 * hold the cursor through third party modifications of the RB-tree.
309 cursor->scan.node = hammer_rec_rb_tree_RB_NEXT(cursor->iprec);
316 hammer_mem_done(hammer_cursor_t cursor)
319 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
320 &cursor->ip->rec_tree);
324 hammer_rel_mem_record(cursor->iprec);
325 cursor->iprec = NULL;
329 /************************************************************************
330 * HAMMER IN-MEMORY RECORD FUNCTIONS *
331 ************************************************************************
333 * These functions manipulate in-memory records. Such records typically
334 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
338 * Add a directory entry (dip,ncp) which references inode (ip).
340 * Note that the low 32 bits of the namekey are set temporarily to create
341 * a unique in-memory record, and may be modified a second time when the
342 * record is synchronized to disk. In particular, the low 32 bits cannot be
343 * all 0's when synching to disk, which is not handled here.
346 hammer_ip_add_directory(struct hammer_transaction *trans,
347 struct hammer_inode *dip, struct namecache *ncp,
348 struct hammer_inode *ip)
350 hammer_record_t record;
354 record = hammer_alloc_mem_record(dip);
356 bytes = ncp->nc_nlen; /* NOTE: terminating \0 is NOT included */
357 if (++trans->hmp->namekey_iterator == 0)
358 ++trans->hmp->namekey_iterator;
360 record->rec.entry.base.base.obj_id = dip->obj_id;
361 record->rec.entry.base.base.key =
362 hammer_directory_namekey(ncp->nc_name, bytes);
363 record->rec.entry.base.base.key += trans->hmp->namekey_iterator;
364 record->rec.entry.base.base.create_tid = trans->tid;
365 record->rec.entry.base.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
366 record->rec.entry.base.base.obj_type = ip->ino_rec.base.base.obj_type;
367 record->rec.entry.obj_id = ip->obj_id;
368 record->data = (void *)ncp->nc_name;
369 record->rec.entry.base.data_len = bytes;
370 ++ip->ino_rec.ino_nlinks;
371 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
372 /* NOTE: copies record->data */
373 error = hammer_mem_add(trans, record);
378 * Delete the directory entry and update the inode link count. The
379 * cursor must be seeked to the directory entry record being deleted.
381 * NOTE: HAMMER_CURSOR_DELETE may not have been set. XXX remove flag.
383 * This function can return EDEADLK requiring the caller to terminate
384 * the cursor and retry.
387 hammer_ip_del_directory(struct hammer_transaction *trans,
388 hammer_cursor_t cursor, struct hammer_inode *dip,
389 struct hammer_inode *ip)
393 error = hammer_ip_delete_record(cursor, trans->tid);
396 * One less link. The file may still be open in the OS even after
397 * all links have gone away so we only try to sync if the OS has
398 * no references and nlinks falls to 0.
400 * We have to terminate the cursor before syncing the inode to
401 * avoid deadlocking against ourselves.
403 * XXX we can't sync the inode here because the encompassing
404 * transaction might be a rename and might update the inode
405 * again with a new link. That would force the delete_tid to be
406 * the same as the create_tid and cause a panic.
409 --ip->ino_rec.ino_nlinks;
410 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
411 if (ip->ino_rec.ino_nlinks == 0 &&
412 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
413 hammer_done_cursor(cursor);
414 /*hammer_sync_inode(ip, MNT_NOWAIT, 1);*/
422 * Add a record to an inode.
424 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
425 * initialize the following additional fields:
427 * record->rec.entry.base.base.key
428 * record->rec.entry.base.base.rec_type
429 * record->rec.entry.base.base.data_len
430 * record->data (a copy will be kmalloc'd if it cannot be embedded)
433 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
435 hammer_inode_t ip = record->ip;
438 record->rec.base.base.obj_id = ip->obj_id;
439 record->rec.base.base.create_tid = trans->tid;
440 record->rec.base.base.obj_type = ip->ino_rec.base.base.obj_type;
442 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
443 /* NOTE: copies record->data */
444 error = hammer_mem_add(trans, record);
449 * Sync data from a buffer cache buffer (typically) to the filesystem. This
450 * is called via the strategy called from a cached data source. This code
451 * is responsible for actually writing a data record out to the disk.
453 * This can only occur non-historically (i.e. 'current' data only).
455 * The file offset must be HAMMER_BUFSIZE aligned but the data length
456 * can be truncated. The record (currently) always represents a BUFSIZE
457 * swath of space whether the data is truncated or not.
460 hammer_ip_sync_data(hammer_transaction_t trans, hammer_inode_t ip,
461 int64_t offset, void *data, int bytes)
463 struct hammer_cursor cursor;
464 hammer_record_ondisk_t rec;
465 union hammer_btree_elm elm;
466 hammer_off_t rec_offset;
470 KKASSERT((offset & HAMMER_BUFMASK) == 0);
472 error = hammer_init_cursor(trans, &cursor, &ip->cache[0]);
475 cursor.key_beg.obj_id = ip->obj_id;
476 cursor.key_beg.key = offset + bytes;
477 cursor.key_beg.create_tid = trans->tid;
478 cursor.key_beg.delete_tid = 0;
479 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
480 cursor.asof = trans->tid;
481 cursor.flags |= HAMMER_CURSOR_INSERT;
484 * Issue a lookup to position the cursor.
486 error = hammer_btree_lookup(&cursor);
488 kprintf("hammer_ip_sync_data: duplicate data at "
489 "(%lld,%d) tid %016llx\n",
490 offset, bytes, trans->tid);
491 hammer_print_btree_elm(&cursor.node->ondisk->elms[cursor.index],
492 HAMMER_BTREE_TYPE_LEAF, cursor.index);
493 panic("Duplicate data");
500 * Allocate record and data space. HAMMER_RECTYPE_DATA records
501 * can cross buffer boundaries so we may have to split our bcopy.
503 rec = hammer_alloc_record(trans, &rec_offset, HAMMER_RECTYPE_DATA,
504 &cursor.record_buffer,
506 &cursor.data_buffer, &error);
509 if (hammer_debug_general & 0x1000)
510 kprintf("OOB RECOR2 DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, rec->base.data_len);
513 * Fill everything in and insert our B-Tree node.
515 * NOTE: hammer_alloc_record() has already marked the related
516 * buffers as modified. If we do it again we will generate
517 * unnecessary undo elements.
519 rec->base.base.btype = HAMMER_BTREE_TYPE_RECORD;
520 rec->base.base.obj_id = ip->obj_id;
521 rec->base.base.key = offset + bytes;
522 rec->base.base.create_tid = trans->tid;
523 rec->base.base.delete_tid = 0;
524 rec->base.base.rec_type = HAMMER_RECTYPE_DATA;
525 rec->base.data_crc = crc32(data, bytes);
526 KKASSERT(rec->base.data_len == bytes);
528 bcopy(data, bdata, bytes);
530 elm.leaf.base = rec->base.base;
531 elm.leaf.rec_offset = rec_offset;
532 elm.leaf.data_offset = rec->base.data_off;
533 elm.leaf.data_len = bytes;
534 elm.leaf.data_crc = rec->base.data_crc;
537 * Data records can wind up on-disk before the inode itself is
538 * on-disk. One must assume data records may be on-disk if either
539 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
541 ip->flags |= HAMMER_INODE_DONDISK;
543 error = hammer_btree_insert(&cursor, &elm);
547 hammer_blockmap_free(trans, rec_offset, HAMMER_RECORD_SIZE);
549 hammer_done_cursor(&cursor);
550 if (error == EDEADLK)
556 * Sync an in-memory record to the disk. This is typically called via fsync
557 * from a cached record source. This code is responsible for actually
558 * writing a record out to the disk.
561 hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t record)
563 struct hammer_cursor cursor;
564 hammer_record_ondisk_t rec;
565 union hammer_btree_elm elm;
566 hammer_off_t rec_offset;
572 * If the record has been deleted or is being synchronized, stop.
573 * Interlock with the syncing flag.
575 if (record->flags & (HAMMER_RECF_DELETED | HAMMER_RECF_SYNCING))
577 record->flags |= HAMMER_RECF_SYNCING;
580 * If someone other then us is referencing the record and not
581 * blocking waiting for us, we have to wait until they finish.
583 * It is possible the record got destroyed while we were blocked.
585 if (record->lock.refs > record->blocked + 1) {
586 while (record->lock.refs > record->blocked + 1) {
587 record->flags |= HAMMER_RECF_WANTED;
588 tsleep(record, 0, "hmrrc1", 0);
590 if (record->flags & HAMMER_RECF_DELETED)
597 error = hammer_init_cursor(trans, &cursor, &record->ip->cache[0]);
600 cursor.key_beg = record->rec.base.base;
601 cursor.flags |= HAMMER_CURSOR_INSERT;
604 * Issue a lookup to position the cursor and locate the cluster. The
605 * target key should not exist. If we are creating a directory entry
606 * we may have to iterate the low 32 bits of the key to find an unused
610 error = hammer_btree_lookup(&cursor);
613 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
614 kprintf("hammer_ip_sync_record: duplicate rec "
615 "at (%016llx)\n", record->rec.base.base.key);
616 Debugger("duplicate record1");
620 if (++trans->hmp->namekey_iterator == 0)
621 ++trans->hmp->namekey_iterator;
622 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
623 record->rec.base.base.key |= trans->hmp->namekey_iterator;
624 cursor.key_beg.key = record->rec.base.base.key;
630 * Mark the record as undergoing synchronization. Our cursor is
631 * holding a locked B-Tree node for the insertion which interlocks
632 * anyone trying to access this record.
634 * XXX There is still a race present related to iterations. An
635 * iteration may process the record, a sync may occur, and then
636 * later process the B-Tree element for the same record.
638 * We do not try to synchronize a deleted record.
640 if (record->flags & HAMMER_RECF_DELETED) {
646 * Allocate the record and data. The result buffers will be
647 * marked as being modified and further calls to
648 * hammer_modify_buffer() will result in unneeded UNDO records.
650 * Support zero-fill records (data == NULL and data_len != 0)
652 if (record->data == NULL) {
653 rec = hammer_alloc_record(trans, &rec_offset,
654 record->rec.base.base.rec_type,
655 &cursor.record_buffer,
658 if (hammer_debug_general & 0x1000)
659 kprintf("NULL RECORD DATA\n");
660 } else if (record->flags & HAMMER_RECF_INBAND) {
661 rec = hammer_alloc_record(trans, &rec_offset,
662 record->rec.base.base.rec_type,
663 &cursor.record_buffer,
664 record->rec.base.data_len, &bdata,
666 if (hammer_debug_general & 0x1000)
667 kprintf("INBAND RECORD DATA %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, record->rec.base.data_len);
669 rec = hammer_alloc_record(trans, &rec_offset,
670 record->rec.base.base.rec_type,
671 &cursor.record_buffer,
672 record->rec.base.data_len, &bdata,
673 &cursor.data_buffer, &error);
674 if (hammer_debug_general & 0x1000)
675 kprintf("OOB RECORD DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, record->rec.base.data_len);
682 * Fill in the remaining fields and insert our B-Tree node.
684 rec->base.base = record->rec.base.base;
685 bcopy(&record->rec.base + 1, &rec->base + 1,
686 HAMMER_RECORD_SIZE - sizeof(record->rec.base));
689 * Copy the data and deal with zero-fill support.
692 rec->base.data_crc = crc32(record->data, rec->base.data_len);
693 bcopy(record->data, bdata, rec->base.data_len);
695 rec->base.data_len = record->rec.base.data_len;
698 elm.leaf.base = record->rec.base.base;
699 elm.leaf.rec_offset = rec_offset;
700 elm.leaf.data_offset = rec->base.data_off;
701 elm.leaf.data_len = rec->base.data_len;
702 elm.leaf.data_crc = rec->base.data_crc;
704 error = hammer_btree_insert(&cursor, &elm);
707 * Clean up on success, or fall through on error.
710 record->flags |= HAMMER_RECF_DELETED;
715 * Try to unwind the allocation
717 hammer_blockmap_free(trans, rec_offset, HAMMER_RECORD_SIZE);
719 record->flags &= ~HAMMER_RECF_SYNCING;
720 hammer_done_cursor(&cursor);
721 if (error == EDEADLK)
727 * Add the record to the inode's rec_tree. The low 32 bits of a directory
728 * entry's key is used to deal with hash collisions in the upper 32 bits.
729 * A unique 64 bit key is generated in-memory and may be regenerated a
730 * second time when the directory record is flushed to the on-disk B-Tree.
732 * A referenced record is passed to this function. This function
733 * eats the reference. If an error occurs the record will be deleted.
735 * A copy of the temporary record->data pointer provided by the caller
740 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
747 * Make a private copy of record->data
751 * Try to embed the data in extra space in the record
752 * union, otherwise allocate a copy.
754 bytes = record->rec.base.data_len;
755 switch(record->rec.base.base.rec_type) {
756 case HAMMER_RECTYPE_DIRENTRY:
757 reclen = offsetof(struct hammer_entry_record, name[0]);
759 case HAMMER_RECTYPE_DATA:
760 reclen = offsetof(struct hammer_data_record, data[0]);
763 reclen = sizeof(record->rec);
766 if (reclen + bytes <= HAMMER_RECORD_SIZE) {
767 bcopy(record->data, (char *)&record->rec + reclen,
769 record->data = (void *)((char *)&record->rec + reclen);
770 record->flags |= HAMMER_RECF_INBAND;
772 ++hammer_count_record_datas;
773 data = kmalloc(bytes, M_HAMMER, M_WAITOK);
774 record->flags |= HAMMER_RECF_ALLOCDATA;
775 bcopy(record->data, data, bytes);
781 * Insert into the RB tree, find an unused iterator if this is
784 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
785 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
786 record->flags |= HAMMER_RECF_DELETED;
787 hammer_rel_mem_record(record);
790 if (++trans->hmp->namekey_iterator == 0)
791 ++trans->hmp->namekey_iterator;
792 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
793 record->rec.base.base.key |= trans->hmp->namekey_iterator;
795 record->flags |= HAMMER_RECF_ONRBTREE;
796 hammer_modify_inode(trans, record->ip, HAMMER_INODE_XDIRTY);
797 hammer_rel_mem_record(record);
801 /************************************************************************
802 * HAMMER INODE MERGED-RECORD FUNCTIONS *
803 ************************************************************************
805 * These functions augment the B-Tree scanning functions in hammer_btree.c
806 * by merging in-memory records with on-disk records.
810 * Locate a particular record either in-memory or on-disk.
812 * NOTE: This is basically a standalone routine, hammer_ip_next() may
813 * NOT be called to iterate results.
816 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
821 * If the element is in-memory return it without searching the
824 error = hammer_mem_lookup(cursor, ip);
826 cursor->record = &cursor->iprec->rec;
833 * If the inode has on-disk components search the on-disk B-Tree.
835 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
837 error = hammer_btree_lookup(cursor);
839 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
844 * Locate the first record within the cursor's key_beg/key_end range,
845 * restricted to a particular inode. 0 is returned on success, ENOENT
846 * if no records matched the requested range, or some other error.
848 * When 0 is returned hammer_ip_next() may be used to iterate additional
849 * records within the requested range.
851 * This function can return EDEADLK, requiring the caller to terminate
852 * the cursor and try again.
855 hammer_ip_first(hammer_cursor_t cursor, struct hammer_inode *ip)
860 * Clean up fields and setup for merged scan
862 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
863 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
864 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
866 hammer_rel_mem_record(cursor->iprec);
867 cursor->iprec = NULL;
871 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
872 * exact lookup so if we get ENOENT we have to call the iterate
873 * function to validate the first record after the begin key.
875 * The ATEDISK flag is used by hammer_btree_iterate to determine
876 * whether it must index forwards or not. It is also used here
877 * to select the next record from in-memory or on-disk.
879 * EDEADLK can only occur if the lookup hit an empty internal
880 * element and couldn't delete it. Since this could only occur
881 * in-range, we can just iterate from the failure point.
883 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
884 error = hammer_btree_lookup(cursor);
885 if (error == ENOENT || error == EDEADLK) {
886 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
887 if (hammer_debug_general & 0x2000)
888 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
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 hammer_ip_resolve_record_and_data(hammer_cursor_t cursor)
1062 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1063 cursor->data = cursor->iprec->data;
1066 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA |
1067 HAMMER_CURSOR_GET_RECORD);
1073 * Delete all records within the specified range for inode ip.
1075 * NOTE: An unaligned range will cause new records to be added to cover
1076 * the edge cases. (XXX not implemented yet).
1078 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1080 * NOTE: Record keys for regular file data have to be special-cased since
1081 * they indicate the end of the range (key = base + bytes).
1084 hammer_ip_delete_range(hammer_transaction_t trans, hammer_inode_t ip,
1085 int64_t ran_beg, int64_t ran_end)
1087 struct hammer_cursor cursor;
1088 hammer_record_ondisk_t rec;
1089 hammer_base_elm_t base;
1094 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
1096 cursor.key_beg.obj_id = ip->obj_id;
1097 cursor.key_beg.create_tid = 0;
1098 cursor.key_beg.delete_tid = 0;
1099 cursor.key_beg.obj_type = 0;
1100 cursor.asof = ip->obj_asof;
1101 cursor.flags |= HAMMER_CURSOR_ASOF;
1103 cursor.key_end = cursor.key_beg;
1104 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1105 cursor.key_beg.key = ran_beg;
1106 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1107 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1108 cursor.key_end.key = ran_end;
1111 * The key in the B-Tree is (base+bytes), so the first possible
1112 * matching key is ran_beg + 1.
1116 cursor.key_beg.key = ran_beg + 1;
1117 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1118 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1120 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1121 if (tmp64 < ran_end)
1122 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1124 cursor.key_end.key = ran_end + MAXPHYS + 1;
1126 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1128 error = hammer_ip_first(&cursor, ip);
1131 * Iterate through matching records and mark them as deleted.
1133 while (error == 0) {
1134 rec = cursor.record;
1135 base = &rec->base.base;
1137 KKASSERT(base->delete_tid == 0);
1140 * There may be overlap cases for regular file data. Also
1141 * remember the key for a regular file record is the offset
1142 * of the last byte of the record (base + len - 1), NOT the
1146 kprintf("delete_range rec_type %02x\n", base->rec_type);
1148 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1150 kprintf("delete_range loop key %016llx\n",
1151 base->key - rec->base.data_len);
1153 off = base->key - rec->base.data_len;
1155 * Check the left edge case. We currently do not
1156 * split existing records.
1158 if (off < ran_beg) {
1159 panic("hammer left edge case %016llx %d\n",
1160 base->key, rec->base.data_len);
1164 * Check the right edge case. Note that the
1165 * record can be completely out of bounds, which
1166 * terminates the search.
1168 * base->key is exclusive of the right edge while
1169 * ran_end is inclusive of the right edge. The
1170 * (key - data_len) left boundary is inclusive.
1172 * XXX theory-check this test at some point, are
1173 * we missing a + 1 somewhere? Note that ran_end
1176 if (base->key - 1 > ran_end) {
1177 if (base->key - rec->base.data_len > ran_end)
1179 panic("hammer right edge case\n");
1184 * Mark the record and B-Tree entry as deleted. This will
1185 * also physically delete the B-Tree entry, record, and
1186 * data if the retention policy dictates. The function
1187 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1188 * uses to perform a fixup.
1190 error = hammer_ip_delete_record(&cursor, trans->tid);
1193 error = hammer_ip_next(&cursor);
1195 hammer_done_cursor(&cursor);
1196 if (error == EDEADLK)
1198 if (error == ENOENT)
1204 * Delete all records associated with an inode except the inode record
1208 hammer_ip_delete_range_all(hammer_transaction_t trans, hammer_inode_t ip)
1210 struct hammer_cursor cursor;
1211 hammer_record_ondisk_t rec;
1212 hammer_base_elm_t base;
1216 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
1218 cursor.key_beg.obj_id = ip->obj_id;
1219 cursor.key_beg.create_tid = 0;
1220 cursor.key_beg.delete_tid = 0;
1221 cursor.key_beg.obj_type = 0;
1222 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1223 cursor.key_beg.key = HAMMER_MIN_KEY;
1225 cursor.key_end = cursor.key_beg;
1226 cursor.key_end.rec_type = 0xFFFF;
1227 cursor.key_end.key = HAMMER_MAX_KEY;
1229 cursor.asof = ip->obj_asof;
1230 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1232 error = hammer_ip_first(&cursor, ip);
1235 * Iterate through matching records and mark them as deleted.
1237 while (error == 0) {
1238 rec = cursor.record;
1239 base = &rec->base.base;
1241 KKASSERT(base->delete_tid == 0);
1244 * Mark the record and B-Tree entry as deleted. This will
1245 * also physically delete the B-Tree entry, record, and
1246 * data if the retention policy dictates. The function
1247 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1248 * uses to perform a fixup.
1250 error = hammer_ip_delete_record(&cursor, trans->tid);
1253 error = hammer_ip_next(&cursor);
1255 hammer_done_cursor(&cursor);
1256 if (error == EDEADLK)
1258 if (error == ENOENT)
1264 * Delete the record at the current cursor. On success the cursor will
1265 * be positioned appropriately for an iteration but may no longer be at
1268 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1272 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1274 hammer_btree_elm_t elm;
1280 * In-memory (unsynchronized) records can simply be freed.
1282 if (cursor->record == &cursor->iprec->rec) {
1283 cursor->iprec->flags |= HAMMER_RECF_DELETED;
1288 * On-disk records are marked as deleted by updating their delete_tid.
1289 * This does not effect their position in the B-Tree (which is based
1290 * on their create_tid).
1292 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1294 hmp = cursor->node->hmp;
1298 error = hammer_cursor_upgrade(cursor);
1300 elm = &cursor->node->ondisk->elms[cursor->index];
1301 hammer_modify_node(cursor->trans, cursor->node,
1303 elm->leaf.base.delete_tid = tid;
1306 * An on-disk record cannot have the same delete_tid
1307 * as its create_tid. In a chain of record updates
1308 * this could result in a duplicate record.
1310 KKASSERT(elm->leaf.base.delete_tid != elm->leaf.base.create_tid);
1311 hammer_modify_buffer(cursor->trans, cursor->record_buffer, &cursor->record->base.base.delete_tid, sizeof(hammer_tid_t));
1312 cursor->record->base.base.delete_tid = tid;
1317 * If we were mounted with the nohistory option, we physically
1318 * delete the record.
1320 if (hmp->hflags & HMNT_NOHISTORY)
1323 if (error == 0 && dodelete) {
1324 error = hammer_delete_at_cursor(cursor, NULL);
1326 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1334 hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes)
1336 hammer_btree_elm_t elm;
1337 hammer_off_t rec_offset;
1338 hammer_off_t data_offset;
1343 elm = &cursor->node->ondisk->elms[cursor->index];
1344 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
1346 rec_offset = elm->leaf.rec_offset;
1347 data_offset = elm->leaf.data_offset;
1348 data_len = elm->leaf.data_len;
1349 rec_type = elm->leaf.base.rec_type;
1351 error = hammer_btree_delete(cursor);
1354 * This forces a fixup for the iteration because
1355 * the cursor is now either sitting at the 'next'
1356 * element or sitting at the end of a leaf.
1358 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1359 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1360 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1364 hammer_blockmap_free(cursor->trans, rec_offset,
1365 sizeof(union hammer_record_ondisk));
1368 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
1369 case HAMMER_ZONE_LARGE_DATA:
1370 case HAMMER_ZONE_SMALL_DATA:
1371 hammer_blockmap_free(cursor->trans,
1372 data_offset, data_len);
1379 kprintf("hammer_delete_at_cursor: %d:%d:%08x %08x/%d "
1380 "(%d remain in cluster)\n",
1381 cluster->volume->vol_no, cluster->clu_no,
1382 rec_offset, data_offset, data_len,
1383 cluster->ondisk->stat_records);
1389 * Determine whether a directory is empty or not. Returns 0 if the directory
1390 * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1393 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1395 struct hammer_cursor cursor;
1398 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
1400 cursor.key_beg.obj_id = ip->obj_id;
1401 cursor.key_beg.create_tid = 0;
1402 cursor.key_beg.delete_tid = 0;
1403 cursor.key_beg.obj_type = 0;
1404 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1405 cursor.key_beg.key = HAMMER_MIN_KEY;
1407 cursor.key_end = cursor.key_beg;
1408 cursor.key_end.rec_type = 0xFFFF;
1409 cursor.key_end.key = HAMMER_MAX_KEY;
1411 cursor.asof = ip->obj_asof;
1412 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1414 error = hammer_ip_first(&cursor, ip);
1415 if (error == ENOENT)
1417 else if (error == 0)
1419 hammer_done_cursor(&cursor);