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.22 2008/01/21 00:00:19 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.delete_tid == 0) {
61 if (rec2->rec.base.base.delete_tid == 0)
65 if (rec2->rec.base.base.delete_tid == 0)
68 if (rec1->rec.base.base.delete_tid < rec2->rec.base.base.delete_tid)
70 if (rec1->rec.base.base.delete_tid > rec2->rec.base.base.delete_tid)
76 hammer_rec_compare(hammer_base_elm_t info, hammer_record_t rec)
78 if (info->rec_type < rec->rec.base.base.rec_type)
80 if (info->rec_type > rec->rec.base.base.rec_type)
83 if (info->key < rec->rec.base.base.key)
85 if (info->key > rec->rec.base.base.key)
88 if (info->delete_tid == 0) {
89 if (rec->rec.base.base.delete_tid == 0)
93 if (rec->rec.base.base.delete_tid == 0)
95 if (info->delete_tid < rec->rec.base.base.delete_tid)
97 if (info->delete_tid > rec->rec.base.base.delete_tid)
103 * RB_SCAN comparison code for hammer_mem_first(). The argument order
104 * is reversed so the comparison result has to be negated. key_beg and
105 * key_end are both range-inclusive.
107 * The creation timestamp can cause hammer_rec_compare() to return -1 or +1.
108 * These do not stop the scan.
110 * Localized deletions are not cached in-memory.
114 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
116 hammer_cursor_t cursor = data;
119 r = hammer_rec_compare(&cursor->key_beg, rec);
122 r = hammer_rec_compare(&cursor->key_end, rec);
128 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
129 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
130 hammer_rec_compare, hammer_base_elm_t);
133 * Allocate a record for the caller to finish filling in. The record is
134 * returned referenced.
137 hammer_alloc_mem_record(hammer_inode_t ip)
139 hammer_record_t record;
141 ++hammer_count_records;
142 record = kmalloc(sizeof(*record), M_HAMMER, M_WAITOK|M_ZERO);
144 record->rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
145 hammer_ref(&record->lock);
150 * Release a memory record. Records marked for deletion are immediately
151 * removed from the RB-Tree but otherwise left intact until the last ref
155 hammer_rel_mem_record(struct hammer_record *record)
157 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 * Lookup an in-memory record given the key specified in the cursor. Works
179 * just like hammer_btree_lookup() but operates on an inode's in-memory
182 * The lookup must fail if the record is marked for deferred deletion.
186 hammer_mem_lookup(hammer_cursor_t cursor, hammer_inode_t ip)
191 hammer_rel_mem_record(cursor->iprec);
192 cursor->iprec = NULL;
195 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
196 &cursor->ip->rec_tree);
199 hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
200 cursor->scan.node = NULL;
201 cursor->iprec = hammer_rec_rb_tree_RB_LOOKUP_INFO(
202 &ip->rec_tree, &cursor->key_beg);
203 if (cursor->iprec == NULL) {
206 hammer_ref(&cursor->iprec->lock);
213 * hammer_mem_first() - locate the first in-memory record matching the
216 * The RB_SCAN function we use is designed as a callback. We terminate it
217 * (return -1) as soon as we get a match.
221 hammer_rec_scan_callback(hammer_record_t rec, void *data)
223 hammer_cursor_t cursor = data;
226 * Skip if not visible due to our as-of TID
228 if (cursor->flags & HAMMER_CURSOR_ASOF) {
229 if (cursor->asof < rec->rec.base.base.create_tid)
231 if (rec->rec.base.base.delete_tid &&
232 cursor->asof >= rec->rec.base.base.delete_tid) {
238 * Return the first matching record and stop the scan
240 if (cursor->iprec == NULL) {
242 hammer_ref(&rec->lock);
250 hammer_mem_first(hammer_cursor_t cursor, hammer_inode_t ip)
253 hammer_rel_mem_record(cursor->iprec);
254 cursor->iprec = NULL;
257 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
258 &cursor->ip->rec_tree);
261 hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
263 cursor->scan.node = NULL;
264 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
265 hammer_rec_scan_callback, cursor);
268 * Adjust scan.node and keep it linked into the RB-tree so we can
269 * hold the cursor through third party modifications of the RB-tree.
272 cursor->scan.node = hammer_rec_rb_tree_RB_NEXT(cursor->iprec);
279 hammer_mem_done(hammer_cursor_t cursor)
282 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
283 &cursor->ip->rec_tree);
287 hammer_rel_mem_record(cursor->iprec);
288 cursor->iprec = NULL;
292 /************************************************************************
293 * HAMMER IN-MEMORY RECORD FUNCTIONS *
294 ************************************************************************
296 * These functions manipulate in-memory records. Such records typically
297 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
301 * Add a directory entry (dip,ncp) which references inode (ip).
303 * Note that the low 32 bits of the namekey are set temporarily to create
304 * a unique in-memory record, and may be modified a second time when the
305 * record is synchronized to disk. In particular, the low 32 bits cannot be
306 * all 0's when synching to disk, which is not handled here.
309 hammer_ip_add_directory(struct hammer_transaction *trans,
310 struct hammer_inode *dip, struct namecache *ncp,
311 struct hammer_inode *ip)
313 hammer_record_t record;
317 record = hammer_alloc_mem_record(dip);
319 bytes = ncp->nc_nlen; /* NOTE: terminating \0 is NOT included */
320 if (++trans->hmp->namekey_iterator == 0)
321 ++trans->hmp->namekey_iterator;
323 record->rec.entry.base.base.obj_id = dip->obj_id;
324 record->rec.entry.base.base.key =
325 hammer_directory_namekey(ncp->nc_name, bytes);
326 record->rec.entry.base.base.key += trans->hmp->namekey_iterator;
327 record->rec.entry.base.base.create_tid = trans->tid;
328 record->rec.entry.base.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
329 record->rec.entry.base.base.obj_type = ip->ino_rec.base.base.obj_type;
330 record->rec.entry.obj_id = ip->obj_id;
331 if (bytes <= sizeof(record->rec.entry.den_name)) {
332 record->data = (void *)record->rec.entry.den_name;
333 record->flags |= HAMMER_RECF_EMBEDDED_DATA;
335 ++hammer_count_record_datas;
336 record->data = kmalloc(bytes, M_HAMMER, M_WAITOK);
337 record->flags |= HAMMER_RECF_ALLOCDATA;
339 bcopy(ncp->nc_name, record->data, bytes);
340 record->rec.entry.base.data_len = bytes;
341 ++ip->ino_rec.ino_nlinks;
342 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
343 error = hammer_mem_add(trans, record);
348 * Delete the directory entry and update the inode link count. The
349 * cursor must be seeked to the directory entry record being deleted.
351 * NOTE: HAMMER_CURSOR_DELETE may not have been set. XXX remove flag.
353 * This function can return EDEADLK requiring the caller to terminate
354 * the cursor and retry.
357 hammer_ip_del_directory(struct hammer_transaction *trans,
358 hammer_cursor_t cursor, struct hammer_inode *dip,
359 struct hammer_inode *ip)
363 error = hammer_ip_delete_record(cursor, trans->tid);
366 * One less link. The file may still be open in the OS even after
367 * all links have gone away so we only try to sync if the OS has
368 * no references and nlinks falls to 0.
370 * We have to terminate the cursor before syncing the inode to
371 * avoid deadlocking against ourselves.
374 --ip->ino_rec.ino_nlinks;
375 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
376 if (ip->ino_rec.ino_nlinks == 0 &&
377 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
378 hammer_done_cursor(cursor);
379 hammer_sync_inode(ip, MNT_NOWAIT, 1);
387 * Add a record to an inode.
389 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
390 * initialize the following additional fields:
392 * record->rec.entry.base.base.key
393 * record->rec.entry.base.base.rec_type
394 * record->rec.entry.base.base.data_len
395 * record->data (a copy will be kmalloc'd if not embedded)
398 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
400 hammer_inode_t ip = record->ip;
405 record->rec.base.base.obj_id = ip->obj_id;
406 record->rec.base.base.create_tid = trans->tid;
407 record->rec.base.base.obj_type = ip->ino_rec.base.base.obj_type;
408 bytes = record->rec.base.data_len;
411 if ((char *)record->data < (char *)&record->rec ||
412 (char *)record->data >= (char *)(&record->rec + 1)) {
413 ++hammer_count_record_datas;
414 data = kmalloc(bytes, M_HAMMER, M_WAITOK);
415 record->flags |= HAMMER_RECF_ALLOCDATA;
416 bcopy(record->data, data, bytes);
419 record->flags |= HAMMER_RECF_EMBEDDED_DATA;
422 hammer_modify_inode(trans, ip, HAMMER_INODE_RDIRTY);
423 error = hammer_mem_add(trans, record);
428 * Sync data from a buffer cache buffer (typically) to the filesystem. This
429 * is called via the strategy called from a cached data source. This code
430 * is responsible for actually writing a data record out to the disk.
432 * This can only occur non-historically (i.e. 'current' data only).
435 hammer_ip_sync_data(hammer_transaction_t trans, hammer_inode_t ip,
436 int64_t offset, void *data, int bytes,
437 struct hammer_cursor **spike)
439 struct hammer_cursor cursor;
440 hammer_record_ondisk_t rec;
441 union hammer_btree_elm elm;
446 error = hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
449 cursor.key_beg.obj_id = ip->obj_id;
450 cursor.key_beg.key = offset + bytes;
451 cursor.key_beg.create_tid = 0;
452 cursor.key_beg.delete_tid = 0;
453 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
454 cursor.asof = trans->tid;
455 cursor.flags |= HAMMER_CURSOR_INSERT;
458 * Issue a lookup to position the cursor and locate the cluster
460 error = hammer_btree_lookup(&cursor);
462 kprintf("hammer_ip_sync_data: duplicate data at (%lld,%d)\n",
464 hammer_print_btree_elm(&cursor.node->ondisk->elms[cursor.index],
465 HAMMER_BTREE_TYPE_LEAF, cursor.index);
472 * Allocate record and data space now that we know which cluster
473 * the B-Tree node ended up in.
475 bdata = hammer_alloc_data(cursor.node->cluster, bytes, &error,
476 &cursor.data_buffer);
479 rec = hammer_alloc_record(cursor.node->cluster, &error,
480 &cursor.record_buffer);
485 * Fill everything in and insert our B-Tree node.
487 hammer_modify_buffer(cursor.record_buffer);
488 rec->base.base.btype = HAMMER_BTREE_TYPE_RECORD;
489 rec->base.base.obj_id = ip->obj_id;
490 rec->base.base.key = offset + bytes;
491 rec->base.base.create_tid = trans->tid;
492 rec->base.base.delete_tid = 0;
493 rec->base.base.rec_type = HAMMER_RECTYPE_DATA;
494 rec->base.data_crc = crc32(data, bytes);
495 rec->base.rec_id = hammer_alloc_recid(cursor.node->cluster);
496 rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer, bdata);
497 rec->base.data_len = bytes;
499 hammer_modify_buffer(cursor.data_buffer);
500 bcopy(data, bdata, bytes);
502 elm.leaf.base = rec->base.base;
503 elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
504 elm.leaf.data_offset = rec->base.data_offset;
505 elm.leaf.data_len = bytes;
506 elm.leaf.data_crc = rec->base.data_crc;
509 * Data records can wind up on-disk before the inode itself is
510 * on-disk. One must assume data records may be on-disk if either
511 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
513 ip->flags |= HAMMER_INODE_DONDISK;
515 error = hammer_btree_insert(&cursor, &elm);
519 hammer_free_record_ptr(cursor.record_buffer, rec);
521 hammer_free_data_ptr(cursor.data_buffer, bdata, bytes);
524 * If ENOSPC in cluster fill in the spike structure and return
528 hammer_load_spike(&cursor, spike);
529 hammer_done_cursor(&cursor);
530 if (error == EDEADLK)
536 * Sync an in-memory record to the disk. this is typically called via fsync
537 * from a cached record source. This code is responsible for actually
538 * writing a record out to the disk.
541 hammer_ip_sync_record(hammer_record_t record, struct hammer_cursor **spike)
543 struct hammer_cursor cursor;
544 hammer_record_ondisk_t rec;
546 union hammer_btree_elm elm;
551 error = hammer_init_cursor_hmp(&cursor, &record->ip->cache[0],
555 cursor.key_beg = record->rec.base.base;
556 cursor.flags |= HAMMER_CURSOR_INSERT;
559 * Issue a lookup to position the cursor and locate the cluster. The
560 * target key should not exist. If we are creating a directory entry
561 * we may have to iterate the low 32 bits of the key to find an unused
564 * If we run out of space trying to adjust the B-Tree for the
565 * insert, re-lookup without the insert flag so the cursor
566 * is properly positioned for the spike.
569 error = hammer_btree_lookup(&cursor);
572 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
573 kprintf("hammer_ip_sync_record: duplicate rec "
574 "at (%016llx)\n", record->rec.base.base.key);
575 Debugger("duplicate record1");
579 hmp = cursor.node->cluster->volume->hmp;
580 if (++hmp->namekey_iterator == 0)
581 ++hmp->namekey_iterator;
582 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
583 record->rec.base.base.key |= hmp->namekey_iterator;
584 cursor.key_beg.key = record->rec.base.base.key;
590 * Mark the record as undergoing synchronization. Our cursor is
591 * holding a locked B-Tree node for the insertion which interlocks
592 * anyone trying to access this record.
594 * XXX There is still a race present related to iterations. An
595 * iteration may process the record, a sync may occur, and then
596 * later process the B-Tree element for the same record.
598 * We do not try to synchronize a deleted record.
600 if (record->flags & (HAMMER_RECF_DELETED | HAMMER_RECF_SYNCING)) {
604 record->flags |= HAMMER_RECF_SYNCING;
607 * Allocate record and data space now that we know which cluster
608 * the B-Tree node ended up in.
610 if (record->data == NULL ||
611 (record->flags & HAMMER_RECF_EMBEDDED_DATA)) {
612 bdata = record->data;
614 bdata = hammer_alloc_data(cursor.node->cluster,
615 record->rec.base.data_len, &error,
616 &cursor.data_buffer);
620 rec = hammer_alloc_record(cursor.node->cluster, &error,
621 &cursor.record_buffer);
626 * Fill everything in and insert our B-Tree node.
628 hammer_modify_buffer(cursor.record_buffer);
631 rec->base.data_crc = crc32(record->data,
632 record->rec.base.data_len);
633 if (record->flags & HAMMER_RECF_EMBEDDED_DATA) {
635 * Data embedded in record
637 rec->base.data_offset = ((char *)bdata -
638 (char *)&record->rec);
639 KKASSERT(rec->base.data_offset >= 0 &&
640 rec->base.data_offset + rec->base.data_len <=
642 rec->base.data_offset += hammer_bclu_offset(cursor.record_buffer, rec);
645 * Data separate from record
647 rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer,bdata);
648 hammer_modify_buffer(cursor.data_buffer);
649 bcopy(record->data, bdata, rec->base.data_len);
652 rec->base.rec_id = hammer_alloc_recid(cursor.node->cluster);
654 elm.leaf.base = record->rec.base.base;
655 elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
656 elm.leaf.data_offset = rec->base.data_offset;
657 elm.leaf.data_len = rec->base.data_len;
658 elm.leaf.data_crc = rec->base.data_crc;
660 error = hammer_btree_insert(&cursor, &elm);
663 * Clean up on success, or fall through on error.
666 record->flags |= HAMMER_RECF_DELETED;
667 record->flags &= ~HAMMER_RECF_SYNCING;
671 hammer_free_record_ptr(cursor.record_buffer, rec);
673 if (record->data && (record->flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
674 hammer_free_data_ptr(cursor.data_buffer, bdata,
675 record->rec.base.data_len);
678 record->flags &= ~HAMMER_RECF_SYNCING;
681 * If ENOSPC in cluster fill in the spike structure and return
685 hammer_load_spike(&cursor, spike);
686 hammer_done_cursor(&cursor);
687 if (error == EDEADLK)
693 * Write out a record using the specified cursor. The caller does not have
694 * to seek the cursor. The flags are used to determine whether the data
695 * (if any) is embedded in the record or not.
697 * The target cursor will be modified by this call. Note in particular
698 * that HAMMER_CURSOR_INSERT is set.
700 * NOTE: This can return EDEADLK, requiring the caller to release its cursor
701 * and retry the operation.
704 hammer_write_record(hammer_cursor_t cursor, hammer_record_ondisk_t orec,
705 void *data, int cursor_flags)
707 union hammer_btree_elm elm;
708 hammer_record_ondisk_t nrec;
712 cursor->key_beg = orec->base.base;
713 cursor->flags |= HAMMER_CURSOR_INSERT;
716 * Issue a lookup to position the cursor and locate the cluster. The
717 * target key should not exist.
719 * If we run out of space trying to adjust the B-Tree for the
720 * insert, re-lookup without the insert flag so the cursor
721 * is properly positioned for the spike.
723 error = hammer_btree_lookup(cursor);
725 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
726 orec->base.base.key);
727 Debugger("duplicate record2");
734 * Allocate record and data space now that we know which cluster
735 * the B-Tree node ended up in.
738 (cursor_flags & HAMMER_RECF_EMBEDDED_DATA)) {
741 bdata = hammer_alloc_data(cursor->node->cluster,
742 orec->base.data_len, &error,
743 &cursor->data_buffer);
747 nrec = hammer_alloc_record(cursor->node->cluster, &error,
748 &cursor->record_buffer);
753 * Fill everything in and insert our B-Tree node.
755 hammer_modify_buffer(cursor->record_buffer);
757 nrec->base.data_offset = 0;
759 nrec->base.data_crc = crc32(bdata, nrec->base.data_len);
760 if (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) {
762 * Data embedded in record
764 nrec->base.data_offset = ((char *)bdata - (char *)orec);
765 KKASSERT(nrec->base.data_offset >= 0 &&
766 nrec->base.data_offset + nrec->base.data_len <
768 nrec->base.data_offset += hammer_bclu_offset(cursor->record_buffer, nrec);
771 * Data separate from record
773 nrec->base.data_offset = hammer_bclu_offset(cursor->data_buffer, bdata);
774 hammer_modify_buffer(cursor->data_buffer);
775 bcopy(data, bdata, nrec->base.data_len);
778 nrec->base.rec_id = hammer_alloc_recid(cursor->node->cluster);
780 elm.leaf.base = nrec->base.base;
781 elm.leaf.rec_offset = hammer_bclu_offset(cursor->record_buffer, nrec);
782 elm.leaf.data_offset = nrec->base.data_offset;
783 elm.leaf.data_len = nrec->base.data_len;
784 elm.leaf.data_crc = nrec->base.data_crc;
786 error = hammer_btree_insert(cursor, &elm);
790 hammer_free_record_ptr(cursor->record_buffer, nrec);
792 if (data && (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
793 hammer_free_data_ptr(cursor->data_buffer, bdata,
794 orec->base.data_len);
797 /* leave cursor intact */
802 * Add the record to the inode's rec_tree. The low 32 bits of a directory
803 * entry's key is used to deal with hash collisions in the upper 32 bits.
804 * A unique 64 bit key is generated in-memory and may be regenerated a
805 * second time when the directory record is flushed to the on-disk B-Tree.
807 * A referenced record is passed to this function. This function
808 * eats the reference. If an error occurs the record will be deleted.
812 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
814 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
815 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
816 record->flags |= HAMMER_RECF_DELETED;
817 hammer_rel_mem_record(record);
820 if (++trans->hmp->namekey_iterator == 0)
821 ++trans->hmp->namekey_iterator;
822 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
823 record->rec.base.base.key |= trans->hmp->namekey_iterator;
825 record->flags |= HAMMER_RECF_ONRBTREE;
826 hammer_modify_inode(trans, record->ip, HAMMER_INODE_XDIRTY);
827 hammer_rel_mem_record(record);
831 /************************************************************************
832 * HAMMER INODE MERGED-RECORD FUNCTIONS *
833 ************************************************************************
835 * These functions augment the B-Tree scanning functions in hammer_btree.c
836 * by merging in-memory records with on-disk records.
840 * Locate a particular record either in-memory or on-disk.
842 * NOTE: This is basically a standalone routine, hammer_ip_next() may
843 * NOT be called to iterate results.
846 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
851 * If the element is in-memory return it without searching the
854 error = hammer_mem_lookup(cursor, ip);
856 cursor->record = &cursor->iprec->rec;
863 * If the inode has on-disk components search the on-disk B-Tree.
865 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
867 error = hammer_btree_lookup(cursor);
869 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
874 * Locate the first record within the cursor's key_beg/key_end range,
875 * restricted to a particular inode. 0 is returned on success, ENOENT
876 * if no records matched the requested range, or some other error.
878 * When 0 is returned hammer_ip_next() may be used to iterate additional
879 * records within the requested range.
881 * This function can return EDEADLK, requiring the caller to terminate
882 * the cursor and try again.
885 hammer_ip_first(hammer_cursor_t cursor, struct hammer_inode *ip)
890 * Clean up fields and setup for merged scan
892 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
893 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
894 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
896 hammer_rel_mem_record(cursor->iprec);
897 cursor->iprec = NULL;
901 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
902 * exact lookup so if we get ENOENT we have to call the iterate
903 * function to validate the first record after the begin key.
905 * The ATEDISK flag is used by hammer_btree_iterate to determine
906 * whether it must index forwards or not. It is also used here
907 * to select the next record from in-memory or on-disk.
909 * EDEADLK can only occur if the lookup hit an empty internal
910 * element and couldn't delete it. Since this could only occur
911 * in-range, we can just iterate from the failure point.
913 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
914 error = hammer_btree_lookup(cursor);
915 if (error == ENOENT || error == EDEADLK) {
916 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
917 error = hammer_btree_iterate(cursor);
919 if (error && error != ENOENT)
922 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
923 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
925 cursor->flags |= HAMMER_CURSOR_ATEDISK;
930 * Search the in-memory record list (Red-Black tree). Unlike the
931 * B-Tree search, mem_first checks for records in the range.
933 error = hammer_mem_first(cursor, ip);
934 if (error && error != ENOENT)
937 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
938 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
942 * This will return the first matching record.
944 return(hammer_ip_next(cursor));
948 * Retrieve the next record in a merged iteration within the bounds of the
949 * cursor. This call may be made multiple times after the cursor has been
950 * initially searched with hammer_ip_first().
952 * 0 is returned on success, ENOENT if no further records match the
953 * requested range, or some other error code is returned.
956 hammer_ip_next(hammer_cursor_t cursor)
958 hammer_btree_elm_t elm;
964 * Load the current on-disk and in-memory record. If we ate any
965 * records we have to get the next one.
967 * If we deleted the last on-disk record we had scanned ATEDISK will
968 * be clear and DELBTREE will be set, forcing a call to iterate. The
969 * fact that ATEDISK is clear causes iterate to re-test the 'current'
970 * element. If ATEDISK is set, iterate will skip the 'current'
973 * Get the next on-disk record
975 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
976 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
977 error = hammer_btree_iterate(cursor);
978 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
980 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
982 cursor->flags |= HAMMER_CURSOR_DISKEOF |
983 HAMMER_CURSOR_ATEDISK;
988 * Get the next in-memory record. The record can be ripped out
989 * of the RB tree so we maintain a scan_info structure to track
992 * hammer_rec_scan_cmp: Is the record still in our general range,
993 * (non-inclusive of snapshot exclusions)?
994 * hammer_rec_scan_callback: Is the record in our snapshot?
996 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
997 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
999 hammer_rel_mem_record(cursor->iprec);
1000 cursor->iprec = NULL;
1002 rec = cursor->scan.node; /* next node */
1004 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1006 if (hammer_rec_scan_callback(rec, cursor) != 0)
1008 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1010 if (cursor->iprec) {
1011 KKASSERT(cursor->iprec == rec);
1012 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1014 hammer_rec_rb_tree_RB_NEXT(rec);
1016 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1022 * Extract either the disk or memory record depending on their
1023 * relative position.
1026 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1029 * Both entries valid
1031 elm = &cursor->node->ondisk->elms[cursor->index];
1032 r = hammer_btree_cmp(&elm->base, &cursor->iprec->rec.base.base);
1034 error = hammer_btree_extract(cursor,
1035 HAMMER_CURSOR_GET_RECORD);
1036 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1039 /* fall through to the memory entry */
1040 case HAMMER_CURSOR_ATEDISK:
1042 * Only the memory entry is valid
1044 cursor->record = &cursor->iprec->rec;
1045 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1047 case HAMMER_CURSOR_ATEMEM:
1049 * Only the disk entry is valid
1051 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1052 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1056 * Neither entry is valid
1058 * XXX error not set properly
1060 cursor->record = NULL;
1068 * Resolve the cursor->data pointer for the current cursor position in
1069 * a merged iteration.
1072 hammer_ip_resolve_data(hammer_cursor_t cursor)
1076 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1077 cursor->data = cursor->iprec->data;
1080 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1086 * Delete all records within the specified range for inode ip.
1088 * NOTE: An unaligned range will cause new records to be added to cover
1089 * the edge cases. (XXX not implemented yet).
1091 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1093 * NOTE: Record keys for regular file data have to be special-cased since
1094 * they indicate the end of the range (key = base + bytes).
1096 * NOTE: The spike structure must be filled in if we return ENOSPC.
1099 hammer_ip_delete_range(hammer_transaction_t trans, hammer_inode_t ip,
1100 int64_t ran_beg, int64_t ran_end,
1101 struct hammer_cursor **spike)
1103 struct hammer_cursor cursor;
1104 hammer_record_ondisk_t rec;
1105 hammer_base_elm_t base;
1110 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1112 cursor.key_beg.obj_id = ip->obj_id;
1113 cursor.key_beg.create_tid = 0;
1114 cursor.key_beg.delete_tid = 0;
1115 cursor.key_beg.obj_type = 0;
1116 cursor.asof = ip->obj_asof;
1117 cursor.flags |= HAMMER_CURSOR_ASOF;
1119 cursor.key_end = cursor.key_beg;
1120 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1121 cursor.key_beg.key = ran_beg;
1122 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1123 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1124 cursor.key_end.key = ran_end;
1127 * The key in the B-Tree is (base+bytes), so the first possible
1128 * matching key is ran_beg + 1.
1132 cursor.key_beg.key = ran_beg + 1;
1133 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1134 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1136 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1137 if (tmp64 < ran_end)
1138 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1140 cursor.key_end.key = ran_end + MAXPHYS + 1;
1142 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1144 error = hammer_ip_first(&cursor, ip);
1147 * Iterate through matching records and mark them as deleted.
1149 while (error == 0) {
1150 rec = cursor.record;
1151 base = &rec->base.base;
1153 KKASSERT(base->delete_tid == 0);
1156 * There may be overlap cases for regular file data. Also
1157 * remember the key for a regular file record is the offset
1158 * of the last byte of the record (base + len - 1), NOT the
1162 kprintf("delete_range rec_type %02x\n", base->rec_type);
1164 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1166 kprintf("delete_range loop key %016llx\n",
1167 base->key - rec->base.data_len);
1169 off = base->key - rec->base.data_len;
1171 * Check the left edge case. We currently do not
1172 * split existing records.
1174 if (off < ran_beg) {
1175 panic("hammer left edge case %016llx %d\n",
1176 base->key, rec->base.data_len);
1180 * Check the right edge case. Note that the
1181 * record can be completely out of bounds, which
1182 * terminates the search.
1184 * base->key is exclusive of the right edge while
1185 * ran_end is inclusive of the right edge. The
1186 * (key - data_len) left boundary is inclusive.
1188 * XXX theory-check this test at some point, are
1189 * we missing a + 1 somewhere? Note that ran_end
1192 if (base->key - 1 > ran_end) {
1193 if (base->key - rec->base.data_len > ran_end)
1195 panic("hammer right edge case\n");
1200 * Mark the record and B-Tree entry as deleted. This will
1201 * also physically delete the B-Tree entry, record, and
1202 * data if the retention policy dictates. The function
1203 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1204 * uses to perform a fixup.
1206 error = hammer_ip_delete_record(&cursor, trans->tid);
1209 error = hammer_ip_next(&cursor);
1211 hammer_done_cursor(&cursor);
1212 if (error == EDEADLK)
1214 if (error == ENOENT)
1220 * Delete all records associated with an inode except the inode record
1224 hammer_ip_delete_range_all(hammer_transaction_t trans, hammer_inode_t ip)
1226 struct hammer_cursor cursor;
1227 hammer_record_ondisk_t rec;
1228 hammer_base_elm_t base;
1232 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1234 cursor.key_beg.obj_id = ip->obj_id;
1235 cursor.key_beg.create_tid = 0;
1236 cursor.key_beg.delete_tid = 0;
1237 cursor.key_beg.obj_type = 0;
1238 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1239 cursor.key_beg.key = HAMMER_MIN_KEY;
1241 cursor.key_end = cursor.key_beg;
1242 cursor.key_end.rec_type = 0xFFFF;
1243 cursor.key_end.key = HAMMER_MAX_KEY;
1245 cursor.asof = ip->obj_asof;
1246 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1248 error = hammer_ip_first(&cursor, ip);
1251 * Iterate through matching records and mark them as deleted.
1253 while (error == 0) {
1254 rec = cursor.record;
1255 base = &rec->base.base;
1257 KKASSERT(base->delete_tid == 0);
1260 * Mark the record and B-Tree entry as deleted. This will
1261 * also physically delete the B-Tree entry, record, and
1262 * data if the retention policy dictates. The function
1263 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1264 * uses to perform a fixup.
1266 error = hammer_ip_delete_record(&cursor, trans->tid);
1269 error = hammer_ip_next(&cursor);
1271 hammer_done_cursor(&cursor);
1272 if (error == EDEADLK)
1274 if (error == ENOENT)
1280 * Delete the record at the current cursor.
1282 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1286 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1288 hammer_btree_elm_t elm;
1293 * In-memory (unsynchronized) records can simply be freed.
1295 if (cursor->record == &cursor->iprec->rec) {
1296 cursor->iprec->flags |= HAMMER_RECF_DELETED;
1301 * On-disk records are marked as deleted by updating their delete_tid.
1303 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1305 hmp = cursor->node->cluster->volume->hmp;
1308 hammer_modify_buffer(cursor->record_buffer);
1309 cursor->record->base.base.delete_tid = tid;
1311 error = hammer_cursor_upgrade(cursor);
1313 hammer_modify_node(cursor->node);
1314 elm = &cursor->node->ondisk->elms[cursor->index];
1315 elm->leaf.base.delete_tid = tid;
1320 * If we were mounted with the nohistory option, we physically
1321 * delete the record.
1323 if (error == 0 && (hmp->hflags & HMNT_NOHISTORY)) {
1325 int32_t data_offset;
1327 hammer_cluster_t cluster;
1329 rec_offset = elm->leaf.rec_offset;
1330 data_offset = elm->leaf.data_offset;
1331 data_len = elm->leaf.data_len;
1333 kprintf("hammer_ip_delete_record: %08x %08x/%d\n",
1334 rec_offset, data_offset, data_len);
1336 cluster = cursor->node->cluster;
1337 hammer_ref_cluster(cluster);
1339 error = hammer_btree_delete(cursor);
1342 * This forces a fixup for the iteration because
1343 * the cursor is now either sitting at the 'next'
1344 * element or sitting at the end of a leaf.
1346 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1347 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1348 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1350 hammer_free_record(cluster, rec_offset);
1351 if (data_offset && (data_offset - rec_offset < 0 ||
1352 data_offset - rec_offset >= HAMMER_RECORD_SIZE)) {
1353 hammer_free_data(cluster, data_offset,data_len);
1356 hammer_rel_cluster(cluster, 0);
1358 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1366 * Determine whether a directory is empty or not. Returns 0 if the directory
1367 * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1370 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1372 struct hammer_cursor cursor;
1375 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1377 cursor.key_beg.obj_id = ip->obj_id;
1378 cursor.key_beg.create_tid = 0;
1379 cursor.key_beg.delete_tid = 0;
1380 cursor.key_beg.obj_type = 0;
1381 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1382 cursor.key_beg.key = HAMMER_MIN_KEY;
1384 cursor.key_end = cursor.key_beg;
1385 cursor.key_end.rec_type = 0xFFFF;
1386 cursor.key_end.key = HAMMER_MAX_KEY;
1388 cursor.asof = ip->obj_asof;
1389 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1391 error = hammer_ip_first(&cursor, ip);
1392 if (error == ENOENT)
1394 else if (error == 0)
1396 hammer_done_cursor(&cursor);