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.34 2008/02/24 19:48:45 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.
404 --ip->ino_rec.ino_nlinks;
405 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
406 if (ip->ino_rec.ino_nlinks == 0 &&
407 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
408 hammer_done_cursor(cursor);
409 hammer_sync_inode(ip, MNT_NOWAIT, 1);
417 * Add a record to an inode.
419 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
420 * initialize the following additional fields:
422 * record->rec.entry.base.base.key
423 * record->rec.entry.base.base.rec_type
424 * record->rec.entry.base.base.data_len
425 * record->data (a copy will be kmalloc'd if it cannot be embedded)
428 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
430 hammer_inode_t ip = record->ip;
433 record->rec.base.base.obj_id = ip->obj_id;
434 record->rec.base.base.create_tid = trans->tid;
435 record->rec.base.base.obj_type = ip->ino_rec.base.base.obj_type;
437 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
438 /* NOTE: copies record->data */
439 error = hammer_mem_add(trans, record);
444 * Sync data from a buffer cache buffer (typically) to the filesystem. This
445 * is called via the strategy called from a cached data source. This code
446 * is responsible for actually writing a data record out to the disk.
448 * This can only occur non-historically (i.e. 'current' data only).
450 * The file offset must be HAMMER_BUFSIZE aligned but the data length
451 * can be truncated. The record (currently) always represents a BUFSIZE
452 * swath of space whether the data is truncated or not.
455 hammer_ip_sync_data(hammer_transaction_t trans, hammer_inode_t ip,
456 int64_t offset, void *data, int bytes)
458 struct hammer_cursor cursor;
459 hammer_record_ondisk_t rec;
460 union hammer_btree_elm elm;
461 hammer_off_t rec_offset;
465 KKASSERT((offset & HAMMER_BUFMASK) == 0);
467 error = hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
470 cursor.key_beg.obj_id = ip->obj_id;
471 cursor.key_beg.key = offset + bytes;
472 cursor.key_beg.create_tid = trans->tid;
473 cursor.key_beg.delete_tid = 0;
474 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
475 cursor.asof = trans->tid;
476 cursor.flags |= HAMMER_CURSOR_INSERT;
479 * Issue a lookup to position the cursor.
481 error = hammer_btree_lookup(&cursor);
483 kprintf("hammer_ip_sync_data: duplicate data at "
484 "(%lld,%d) tid %016llx\n",
485 offset, bytes, trans->tid);
486 hammer_print_btree_elm(&cursor.node->ondisk->elms[cursor.index],
487 HAMMER_BTREE_TYPE_LEAF, cursor.index);
488 panic("Duplicate data");
495 * Allocate record and data space. HAMMER_RECTYPE_DATA records
496 * can cross buffer boundaries so we may have to split our bcopy.
498 rec = hammer_alloc_record(ip->hmp, &rec_offset, HAMMER_RECTYPE_DATA,
499 &cursor.record_buffer,
501 &cursor.data_buffer, &error);
504 if (hammer_debug_general & 0x1000)
505 kprintf("OOB RECOR2 DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, rec->base.data_len);
508 * Fill everything in and insert our B-Tree node.
510 * NOTE: hammer_alloc_record() has already marked the related
511 * buffers as modified. If we do it again we will generate
512 * unnecessary undo elements.
514 rec->base.base.btype = HAMMER_BTREE_TYPE_RECORD;
515 rec->base.base.obj_id = ip->obj_id;
516 rec->base.base.key = offset + bytes;
517 rec->base.base.create_tid = trans->tid;
518 rec->base.base.delete_tid = 0;
519 rec->base.base.rec_type = HAMMER_RECTYPE_DATA;
520 rec->base.data_crc = crc32(data, bytes);
521 KKASSERT(rec->base.data_len == bytes);
523 bcopy(data, bdata, bytes);
525 elm.leaf.base = rec->base.base;
526 elm.leaf.rec_offset = rec_offset;
527 elm.leaf.data_offset = rec->base.data_off;
528 elm.leaf.data_len = bytes;
529 elm.leaf.data_crc = rec->base.data_crc;
532 * Data records can wind up on-disk before the inode itself is
533 * on-disk. One must assume data records may be on-disk if either
534 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
536 ip->flags |= HAMMER_INODE_DONDISK;
538 error = hammer_btree_insert(&cursor, &elm);
542 hammer_blockmap_free(ip->hmp, rec_offset, HAMMER_RECORD_SIZE);
544 hammer_done_cursor(&cursor);
545 if (error == EDEADLK)
551 * Sync an in-memory record to the disk. This is typically called via fsync
552 * from a cached record source. This code is responsible for actually
553 * writing a record out to the disk.
556 hammer_ip_sync_record(hammer_record_t record)
558 struct hammer_cursor cursor;
559 hammer_record_ondisk_t rec;
561 union hammer_btree_elm elm;
562 hammer_off_t rec_offset;
566 hmp = record->ip->hmp;
569 * If the record has been deleted or is being synchronized, stop.
570 * Interlock with the syncing flag.
572 if (record->flags & (HAMMER_RECF_DELETED | HAMMER_RECF_SYNCING))
574 record->flags |= HAMMER_RECF_SYNCING;
577 * If someone other then us is referencing the record and not
578 * blocking waiting for us, we have to wait until they finish.
580 * It is possible the record got destroyed while we were blocked.
582 if (record->lock.refs > record->blocked + 1) {
583 while (record->lock.refs > record->blocked + 1) {
584 record->flags |= HAMMER_RECF_WANTED;
585 tsleep(record, 0, "hmrrc1", 0);
587 if (record->flags & HAMMER_RECF_DELETED)
594 error = hammer_init_cursor_hmp(&cursor, &record->ip->cache[0], hmp);
597 cursor.key_beg = record->rec.base.base;
598 cursor.flags |= HAMMER_CURSOR_INSERT;
601 * Issue a lookup to position the cursor and locate the cluster. The
602 * target key should not exist. If we are creating a directory entry
603 * we may have to iterate the low 32 bits of the key to find an unused
607 error = hammer_btree_lookup(&cursor);
610 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
611 kprintf("hammer_ip_sync_record: duplicate rec "
612 "at (%016llx)\n", record->rec.base.base.key);
613 Debugger("duplicate record1");
617 if (++hmp->namekey_iterator == 0)
618 ++hmp->namekey_iterator;
619 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
620 record->rec.base.base.key |= hmp->namekey_iterator;
621 cursor.key_beg.key = record->rec.base.base.key;
627 * Mark the record as undergoing synchronization. Our cursor is
628 * holding a locked B-Tree node for the insertion which interlocks
629 * anyone trying to access this record.
631 * XXX There is still a race present related to iterations. An
632 * iteration may process the record, a sync may occur, and then
633 * later process the B-Tree element for the same record.
635 * We do not try to synchronize a deleted record.
637 if (record->flags & HAMMER_RECF_DELETED) {
643 * Allocate the record and data. The result buffers will be
644 * marked as being modified and further calls to
645 * hammer_modify_buffer() will result in unneeded UNDO records.
647 * Support zero-fill records (data == NULL and data_len != 0)
649 if (record->data == NULL) {
650 rec = hammer_alloc_record(hmp, &rec_offset,
651 record->rec.base.base.rec_type,
652 &cursor.record_buffer,
655 if (hammer_debug_general & 0x1000)
656 kprintf("NULL RECORD DATA\n");
657 } else if (record->flags & HAMMER_RECF_INBAND) {
658 rec = hammer_alloc_record(hmp, &rec_offset,
659 record->rec.base.base.rec_type,
660 &cursor.record_buffer,
661 record->rec.base.data_len, &bdata,
663 if (hammer_debug_general & 0x1000)
664 kprintf("INBAND RECORD DATA %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, record->rec.base.data_len);
666 rec = hammer_alloc_record(hmp, &rec_offset,
667 record->rec.base.base.rec_type,
668 &cursor.record_buffer,
669 record->rec.base.data_len, &bdata,
670 &cursor.data_buffer, &error);
671 if (hammer_debug_general & 0x1000)
672 kprintf("OOB RECORD DATA REC %016llx DATA %016llx LEN=%d\n", rec_offset, rec->base.data_off, record->rec.base.data_len);
679 * Fill in the remaining fields and insert our B-Tree node.
681 rec->base.base = record->rec.base.base;
682 bcopy(&record->rec.base + 1, &rec->base + 1,
683 HAMMER_RECORD_SIZE - sizeof(record->rec.base));
686 * Copy the data and deal with zero-fill support.
689 rec->base.data_crc = crc32(record->data, rec->base.data_len);
690 bcopy(record->data, bdata, rec->base.data_len);
692 rec->base.data_len = record->rec.base.data_len;
695 elm.leaf.base = record->rec.base.base;
696 elm.leaf.rec_offset = rec_offset;
697 elm.leaf.data_offset = rec->base.data_off;
698 elm.leaf.data_len = rec->base.data_len;
699 elm.leaf.data_crc = rec->base.data_crc;
701 error = hammer_btree_insert(&cursor, &elm);
704 * Clean up on success, or fall through on error.
707 record->flags |= HAMMER_RECF_DELETED;
712 * Try to unwind the allocation
714 hammer_blockmap_free(hmp, rec_offset, HAMMER_RECORD_SIZE);
716 record->flags &= ~HAMMER_RECF_SYNCING;
717 hammer_done_cursor(&cursor);
718 if (error == EDEADLK)
724 * Add the record to the inode's rec_tree. The low 32 bits of a directory
725 * entry's key is used to deal with hash collisions in the upper 32 bits.
726 * A unique 64 bit key is generated in-memory and may be regenerated a
727 * second time when the directory record is flushed to the on-disk B-Tree.
729 * A referenced record is passed to this function. This function
730 * eats the reference. If an error occurs the record will be deleted.
732 * A copy of the temporary record->data pointer provided by the caller
737 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
744 * Make a private copy of record->data
748 * Try to embed the data in extra space in the record
749 * union, otherwise allocate a copy.
751 bytes = record->rec.base.data_len;
752 switch(record->rec.base.base.rec_type) {
753 case HAMMER_RECTYPE_DIRENTRY:
754 reclen = offsetof(struct hammer_entry_record, name[0]);
756 case HAMMER_RECTYPE_DATA:
757 reclen = offsetof(struct hammer_data_record, data[0]);
760 reclen = sizeof(record->rec);
763 if (reclen + bytes <= HAMMER_RECORD_SIZE) {
764 bcopy(record->data, (char *)&record->rec + reclen,
766 record->data = (void *)((char *)&record->rec + reclen);
767 record->flags |= HAMMER_RECF_INBAND;
769 ++hammer_count_record_datas;
770 data = kmalloc(bytes, M_HAMMER, M_WAITOK);
771 record->flags |= HAMMER_RECF_ALLOCDATA;
772 bcopy(record->data, data, bytes);
778 * Insert into the RB tree, find an unused iterator if this is
781 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
782 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
783 record->flags |= HAMMER_RECF_DELETED;
784 hammer_rel_mem_record(record);
787 if (++trans->hmp->namekey_iterator == 0)
788 ++trans->hmp->namekey_iterator;
789 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
790 record->rec.base.base.key |= trans->hmp->namekey_iterator;
792 record->flags |= HAMMER_RECF_ONRBTREE;
793 hammer_modify_inode(trans, record->ip, HAMMER_INODE_XDIRTY);
794 hammer_rel_mem_record(record);
798 /************************************************************************
799 * HAMMER INODE MERGED-RECORD FUNCTIONS *
800 ************************************************************************
802 * These functions augment the B-Tree scanning functions in hammer_btree.c
803 * by merging in-memory records with on-disk records.
807 * Locate a particular record either in-memory or on-disk.
809 * NOTE: This is basically a standalone routine, hammer_ip_next() may
810 * NOT be called to iterate results.
813 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
818 * If the element is in-memory return it without searching the
821 error = hammer_mem_lookup(cursor, ip);
823 cursor->record = &cursor->iprec->rec;
830 * If the inode has on-disk components search the on-disk B-Tree.
832 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
834 error = hammer_btree_lookup(cursor);
836 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
841 * Locate the first record within the cursor's key_beg/key_end range,
842 * restricted to a particular inode. 0 is returned on success, ENOENT
843 * if no records matched the requested range, or some other error.
845 * When 0 is returned hammer_ip_next() may be used to iterate additional
846 * records within the requested range.
848 * This function can return EDEADLK, requiring the caller to terminate
849 * the cursor and try again.
852 hammer_ip_first(hammer_cursor_t cursor, struct hammer_inode *ip)
857 * Clean up fields and setup for merged scan
859 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
860 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
861 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
863 hammer_rel_mem_record(cursor->iprec);
864 cursor->iprec = NULL;
868 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
869 * exact lookup so if we get ENOENT we have to call the iterate
870 * function to validate the first record after the begin key.
872 * The ATEDISK flag is used by hammer_btree_iterate to determine
873 * whether it must index forwards or not. It is also used here
874 * to select the next record from in-memory or on-disk.
876 * EDEADLK can only occur if the lookup hit an empty internal
877 * element and couldn't delete it. Since this could only occur
878 * in-range, we can just iterate from the failure point.
880 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
881 error = hammer_btree_lookup(cursor);
882 if (error == ENOENT || error == EDEADLK) {
883 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
884 error = hammer_btree_iterate(cursor);
886 if (error && error != ENOENT)
889 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
890 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
892 cursor->flags |= HAMMER_CURSOR_ATEDISK;
897 * Search the in-memory record list (Red-Black tree). Unlike the
898 * B-Tree search, mem_first checks for records in the range.
900 error = hammer_mem_first(cursor, ip);
901 if (error && error != ENOENT)
904 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
905 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
909 * This will return the first matching record.
911 return(hammer_ip_next(cursor));
915 * Retrieve the next record in a merged iteration within the bounds of the
916 * cursor. This call may be made multiple times after the cursor has been
917 * initially searched with hammer_ip_first().
919 * 0 is returned on success, ENOENT if no further records match the
920 * requested range, or some other error code is returned.
923 hammer_ip_next(hammer_cursor_t cursor)
925 hammer_btree_elm_t elm;
931 * Load the current on-disk and in-memory record. If we ate any
932 * records we have to get the next one.
934 * If we deleted the last on-disk record we had scanned ATEDISK will
935 * be clear and DELBTREE will be set, forcing a call to iterate. The
936 * fact that ATEDISK is clear causes iterate to re-test the 'current'
937 * element. If ATEDISK is set, iterate will skip the 'current'
940 * Get the next on-disk record
942 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
943 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
944 error = hammer_btree_iterate(cursor);
945 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
947 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
949 cursor->flags |= HAMMER_CURSOR_DISKEOF |
950 HAMMER_CURSOR_ATEDISK;
955 * Get the next in-memory record. The record can be ripped out
956 * of the RB tree so we maintain a scan_info structure to track
959 * hammer_rec_scan_cmp: Is the record still in our general range,
960 * (non-inclusive of snapshot exclusions)?
961 * hammer_rec_scan_callback: Is the record in our snapshot?
963 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
964 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
966 hammer_rel_mem_record(cursor->iprec);
967 cursor->iprec = NULL;
969 rec = cursor->scan.node; /* next node */
971 if (hammer_rec_scan_cmp(rec, cursor) != 0)
973 if (hammer_rec_scan_callback(rec, cursor) != 0)
975 rec = hammer_rec_rb_tree_RB_NEXT(rec);
978 KKASSERT(cursor->iprec == rec);
979 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
981 hammer_rec_rb_tree_RB_NEXT(rec);
983 cursor->flags |= HAMMER_CURSOR_MEMEOF;
989 * Extract either the disk or memory record depending on their
993 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
998 elm = &cursor->node->ondisk->elms[cursor->index];
999 r = hammer_btree_cmp(&elm->base, &cursor->iprec->rec.base.base);
1001 error = hammer_btree_extract(cursor,
1002 HAMMER_CURSOR_GET_RECORD);
1003 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1006 /* fall through to the memory entry */
1007 case HAMMER_CURSOR_ATEDISK:
1009 * Only the memory entry is valid
1011 cursor->record = &cursor->iprec->rec;
1012 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1014 case HAMMER_CURSOR_ATEMEM:
1016 * Only the disk entry is valid
1018 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1019 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1023 * Neither entry is valid
1025 * XXX error not set properly
1027 cursor->record = NULL;
1035 * Resolve the cursor->data pointer for the current cursor position in
1036 * a merged iteration.
1039 hammer_ip_resolve_data(hammer_cursor_t cursor)
1043 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1044 cursor->data = cursor->iprec->data;
1047 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1053 hammer_ip_resolve_record_and_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 |
1062 HAMMER_CURSOR_GET_RECORD);
1068 * Delete all records within the specified range for inode ip.
1070 * NOTE: An unaligned range will cause new records to be added to cover
1071 * the edge cases. (XXX not implemented yet).
1073 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1075 * NOTE: Record keys for regular file data have to be special-cased since
1076 * they indicate the end of the range (key = base + bytes).
1079 hammer_ip_delete_range(hammer_transaction_t trans, hammer_inode_t ip,
1080 int64_t ran_beg, int64_t ran_end)
1082 struct hammer_cursor cursor;
1083 hammer_record_ondisk_t rec;
1084 hammer_base_elm_t base;
1089 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1091 cursor.key_beg.obj_id = ip->obj_id;
1092 cursor.key_beg.create_tid = 0;
1093 cursor.key_beg.delete_tid = 0;
1094 cursor.key_beg.obj_type = 0;
1095 cursor.asof = ip->obj_asof;
1096 cursor.flags |= HAMMER_CURSOR_ASOF;
1098 cursor.key_end = cursor.key_beg;
1099 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1100 cursor.key_beg.key = ran_beg;
1101 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1102 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1103 cursor.key_end.key = ran_end;
1106 * The key in the B-Tree is (base+bytes), so the first possible
1107 * matching key is ran_beg + 1.
1111 cursor.key_beg.key = ran_beg + 1;
1112 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1113 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1115 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1116 if (tmp64 < ran_end)
1117 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1119 cursor.key_end.key = ran_end + MAXPHYS + 1;
1121 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1123 error = hammer_ip_first(&cursor, ip);
1126 * Iterate through matching records and mark them as deleted.
1128 while (error == 0) {
1129 rec = cursor.record;
1130 base = &rec->base.base;
1132 KKASSERT(base->delete_tid == 0);
1135 * There may be overlap cases for regular file data. Also
1136 * remember the key for a regular file record is the offset
1137 * of the last byte of the record (base + len - 1), NOT the
1141 kprintf("delete_range rec_type %02x\n", base->rec_type);
1143 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1145 kprintf("delete_range loop key %016llx\n",
1146 base->key - rec->base.data_len);
1148 off = base->key - rec->base.data_len;
1150 * Check the left edge case. We currently do not
1151 * split existing records.
1153 if (off < ran_beg) {
1154 panic("hammer left edge case %016llx %d\n",
1155 base->key, rec->base.data_len);
1159 * Check the right edge case. Note that the
1160 * record can be completely out of bounds, which
1161 * terminates the search.
1163 * base->key is exclusive of the right edge while
1164 * ran_end is inclusive of the right edge. The
1165 * (key - data_len) left boundary is inclusive.
1167 * XXX theory-check this test at some point, are
1168 * we missing a + 1 somewhere? Note that ran_end
1171 if (base->key - 1 > ran_end) {
1172 if (base->key - rec->base.data_len > ran_end)
1174 panic("hammer right edge case\n");
1179 * Mark the record and B-Tree entry as deleted. This will
1180 * also physically delete the B-Tree entry, record, and
1181 * data if the retention policy dictates. The function
1182 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1183 * uses to perform a fixup.
1185 error = hammer_ip_delete_record(&cursor, trans->tid);
1188 error = hammer_ip_next(&cursor);
1190 hammer_done_cursor(&cursor);
1191 if (error == EDEADLK)
1193 if (error == ENOENT)
1199 * Delete all records associated with an inode except the inode record
1203 hammer_ip_delete_range_all(hammer_transaction_t trans, hammer_inode_t ip)
1205 struct hammer_cursor cursor;
1206 hammer_record_ondisk_t rec;
1207 hammer_base_elm_t base;
1211 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1213 cursor.key_beg.obj_id = ip->obj_id;
1214 cursor.key_beg.create_tid = 0;
1215 cursor.key_beg.delete_tid = 0;
1216 cursor.key_beg.obj_type = 0;
1217 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1218 cursor.key_beg.key = HAMMER_MIN_KEY;
1220 cursor.key_end = cursor.key_beg;
1221 cursor.key_end.rec_type = 0xFFFF;
1222 cursor.key_end.key = HAMMER_MAX_KEY;
1224 cursor.asof = ip->obj_asof;
1225 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1227 error = hammer_ip_first(&cursor, ip);
1230 * Iterate through matching records and mark them as deleted.
1232 while (error == 0) {
1233 rec = cursor.record;
1234 base = &rec->base.base;
1236 KKASSERT(base->delete_tid == 0);
1239 * Mark the record and B-Tree entry as deleted. This will
1240 * also physically delete the B-Tree entry, record, and
1241 * data if the retention policy dictates. The function
1242 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1243 * uses to perform a fixup.
1245 error = hammer_ip_delete_record(&cursor, trans->tid);
1248 error = hammer_ip_next(&cursor);
1250 hammer_done_cursor(&cursor);
1251 if (error == EDEADLK)
1253 if (error == ENOENT)
1259 * Delete the record at the current cursor. On success the cursor will
1260 * be positioned appropriately for an iteration but may no longer be at
1263 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1267 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1269 hammer_btree_elm_t elm;
1275 * In-memory (unsynchronized) records can simply be freed.
1277 if (cursor->record == &cursor->iprec->rec) {
1278 cursor->iprec->flags |= HAMMER_RECF_DELETED;
1283 * On-disk records are marked as deleted by updating their delete_tid.
1284 * This does not effect their position in the B-Tree (which is based
1285 * on their create_tid).
1287 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1289 hmp = cursor->node->hmp;
1293 error = hammer_cursor_upgrade(cursor);
1295 hammer_modify_node(cursor->node);
1296 elm = &cursor->node->ondisk->elms[cursor->index];
1297 elm->leaf.base.delete_tid = tid;
1298 hammer_modify_buffer(cursor->record_buffer, &cursor->record->base.base.delete_tid, sizeof(hammer_tid_t));
1299 cursor->record->base.base.delete_tid = tid;
1304 * If we were mounted with the nohistory option, we physically
1305 * delete the record.
1307 if (hmp->hflags & HMNT_NOHISTORY)
1310 if (error == 0 && dodelete) {
1311 error = hammer_delete_at_cursor(cursor, NULL);
1313 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1321 hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes)
1323 hammer_btree_elm_t elm;
1324 hammer_off_t rec_offset;
1325 hammer_off_t data_offset;
1330 elm = &cursor->node->ondisk->elms[cursor->index];
1331 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
1333 rec_offset = elm->leaf.rec_offset;
1334 data_offset = elm->leaf.data_offset;
1335 data_len = elm->leaf.data_len;
1336 rec_type = elm->leaf.base.rec_type;
1338 error = hammer_btree_delete(cursor);
1341 * This forces a fixup for the iteration because
1342 * the cursor is now either sitting at the 'next'
1343 * element or sitting at the end of a leaf.
1345 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1346 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1347 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1351 hammer_blockmap_free(cursor->node->hmp, rec_offset,
1352 sizeof(union hammer_record_ondisk));
1355 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
1356 case HAMMER_ZONE_LARGE_DATA:
1357 case HAMMER_ZONE_SMALL_DATA:
1358 hammer_blockmap_free(cursor->node->hmp,
1359 data_offset, data_len);
1366 kprintf("hammer_delete_at_cursor: %d:%d:%08x %08x/%d "
1367 "(%d remain in cluster)\n",
1368 cluster->volume->vol_no, cluster->clu_no,
1369 rec_offset, data_offset, data_len,
1370 cluster->ondisk->stat_records);
1376 * Determine whether a directory is empty or not. Returns 0 if the directory
1377 * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1380 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1382 struct hammer_cursor cursor;
1385 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1387 cursor.key_beg.obj_id = ip->obj_id;
1388 cursor.key_beg.create_tid = 0;
1389 cursor.key_beg.delete_tid = 0;
1390 cursor.key_beg.obj_type = 0;
1391 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1392 cursor.key_beg.key = HAMMER_MIN_KEY;
1394 cursor.key_end = cursor.key_beg;
1395 cursor.key_end.rec_type = 0xFFFF;
1396 cursor.key_end.key = HAMMER_MAX_KEY;
1398 cursor.asof = ip->obj_asof;
1399 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1401 error = hammer_ip_first(&cursor, ip);
1402 if (error == ENOENT)
1404 else if (error == 0)
1406 hammer_done_cursor(&cursor);