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.21 2008/01/18 07:02:41 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.
433 hammer_ip_sync_data(hammer_transaction_t trans, hammer_inode_t ip,
434 int64_t offset, void *data, int bytes,
435 struct hammer_cursor **spike)
437 struct hammer_cursor cursor;
438 hammer_record_ondisk_t rec;
439 union hammer_btree_elm elm;
444 error = hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
447 cursor.key_beg.obj_id = ip->obj_id;
448 cursor.key_beg.key = offset + bytes;
449 cursor.key_beg.create_tid = 0;
450 cursor.key_beg.delete_tid = 0;
451 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
452 cursor.asof = trans->tid;
453 cursor.flags |= HAMMER_CURSOR_INSERT | HAMMER_CURSOR_ASOF;
456 * Issue a lookup to position the cursor and locate the cluster
458 error = hammer_btree_lookup(&cursor);
460 kprintf("hammer_ip_sync_data: duplicate data at (%lld,%d)\n",
462 hammer_print_btree_elm(&cursor.node->ondisk->elms[cursor.index],
463 HAMMER_BTREE_TYPE_LEAF, cursor.index);
470 * Allocate record and data space now that we know which cluster
471 * the B-Tree node ended up in.
473 bdata = hammer_alloc_data(cursor.node->cluster, bytes, &error,
474 &cursor.data_buffer);
477 rec = hammer_alloc_record(cursor.node->cluster, &error,
478 &cursor.record_buffer);
483 * Fill everything in and insert our B-Tree node.
485 hammer_modify_buffer(cursor.record_buffer);
486 rec->base.base.btype = HAMMER_BTREE_TYPE_RECORD;
487 rec->base.base.obj_id = ip->obj_id;
488 rec->base.base.key = offset + bytes;
489 rec->base.base.create_tid = trans->tid;
490 rec->base.base.delete_tid = 0;
491 rec->base.base.rec_type = HAMMER_RECTYPE_DATA;
492 rec->base.data_crc = crc32(data, bytes);
493 rec->base.rec_id = 0; /* XXX */
494 rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer, bdata);
495 rec->base.data_len = bytes;
497 hammer_modify_buffer(cursor.data_buffer);
498 bcopy(data, bdata, bytes);
500 elm.leaf.base = rec->base.base;
501 elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
502 elm.leaf.data_offset = rec->base.data_offset;
503 elm.leaf.data_len = bytes;
504 elm.leaf.data_crc = rec->base.data_crc;
507 * Data records can wind up on-disk before the inode itself is
508 * on-disk. One must assume data records may be on-disk if either
509 * HAMMER_INODE_DONDISK or HAMMER_INODE_ONDISK is set
511 ip->flags |= HAMMER_INODE_DONDISK;
513 error = hammer_btree_insert(&cursor, &elm);
515 hammer_update_syncid(cursor.record_buffer->cluster, trans->tid);
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 * XXX assign rec_id here
630 hammer_modify_buffer(cursor.record_buffer);
633 rec->base.data_crc = crc32(record->data,
634 record->rec.base.data_len);
635 if (record->flags & HAMMER_RECF_EMBEDDED_DATA) {
637 * Data embedded in record
639 rec->base.data_offset = ((char *)bdata -
640 (char *)&record->rec);
641 KKASSERT(rec->base.data_offset >= 0 &&
642 rec->base.data_offset + rec->base.data_len <=
644 rec->base.data_offset += hammer_bclu_offset(cursor.record_buffer, rec);
647 * Data separate from record
649 rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer,bdata);
650 hammer_modify_buffer(cursor.data_buffer);
651 bcopy(record->data, bdata, rec->base.data_len);
654 rec->base.rec_id = 0; /* XXX */
656 elm.leaf.base = record->rec.base.base;
657 elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
658 elm.leaf.data_offset = rec->base.data_offset;
659 elm.leaf.data_len = rec->base.data_len;
660 elm.leaf.data_crc = rec->base.data_crc;
662 error = hammer_btree_insert(&cursor, &elm);
665 * Clean up on success, or fall through on error.
668 record->flags |= HAMMER_RECF_DELETED;
669 record->flags &= ~HAMMER_RECF_SYNCING;
670 hammer_update_syncid(cursor.record_buffer->cluster,
671 record->rec.base.base.create_tid);
675 hammer_free_record_ptr(cursor.record_buffer, rec);
677 if (record->data && (record->flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
678 hammer_free_data_ptr(cursor.data_buffer, bdata,
679 record->rec.base.data_len);
682 record->flags &= ~HAMMER_RECF_SYNCING;
685 * If ENOSPC in cluster fill in the spike structure and return
689 hammer_load_spike(&cursor, spike);
690 hammer_done_cursor(&cursor);
691 if (error == EDEADLK)
697 * Write out a record using the specified cursor. The caller does not have
698 * to seek the cursor. The flags are used to determine whether the data
699 * (if any) is embedded in the record or not.
701 * The target cursor will be modified by this call. Note in particular
702 * that HAMMER_CURSOR_INSERT is set.
704 * NOTE: This can return EDEADLK, requiring the caller to release its cursor
705 * and retry the operation.
708 hammer_write_record(hammer_cursor_t cursor, hammer_record_ondisk_t orec,
709 void *data, int cursor_flags)
711 union hammer_btree_elm elm;
712 hammer_record_ondisk_t nrec;
716 cursor->key_beg = orec->base.base;
717 cursor->flags |= HAMMER_CURSOR_INSERT;
720 * Issue a lookup to position the cursor and locate the cluster. The
721 * target key should not exist.
723 * If we run out of space trying to adjust the B-Tree for the
724 * insert, re-lookup without the insert flag so the cursor
725 * is properly positioned for the spike.
727 error = hammer_btree_lookup(cursor);
729 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
730 orec->base.base.key);
731 Debugger("duplicate record2");
738 * Allocate record and data space now that we know which cluster
739 * the B-Tree node ended up in.
742 (cursor_flags & HAMMER_RECF_EMBEDDED_DATA)) {
745 bdata = hammer_alloc_data(cursor->node->cluster,
746 orec->base.data_len, &error,
747 &cursor->data_buffer);
751 nrec = hammer_alloc_record(cursor->node->cluster, &error,
752 &cursor->record_buffer);
757 * Fill everything in and insert our B-Tree node.
759 * XXX assign rec_id here
761 hammer_modify_buffer(cursor->record_buffer);
763 nrec->base.data_offset = 0;
765 nrec->base.data_crc = crc32(bdata, nrec->base.data_len);
766 if (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) {
768 * Data embedded in record
770 nrec->base.data_offset = ((char *)bdata - (char *)orec);
771 KKASSERT(nrec->base.data_offset >= 0 &&
772 nrec->base.data_offset + nrec->base.data_len <
774 nrec->base.data_offset += hammer_bclu_offset(cursor->record_buffer, nrec);
777 * Data separate from record
779 nrec->base.data_offset = hammer_bclu_offset(cursor->data_buffer, bdata);
780 hammer_modify_buffer(cursor->data_buffer);
781 bcopy(data, bdata, nrec->base.data_len);
784 nrec->base.rec_id = 0; /* XXX */
786 elm.leaf.base = nrec->base.base;
787 elm.leaf.rec_offset = hammer_bclu_offset(cursor->record_buffer, nrec);
788 elm.leaf.data_offset = nrec->base.data_offset;
789 elm.leaf.data_len = nrec->base.data_len;
790 elm.leaf.data_crc = nrec->base.data_crc;
792 error = hammer_btree_insert(cursor, &elm);
794 hammer_update_syncid(cursor->record_buffer->cluster,
795 nrec->base.base.create_tid);
799 hammer_free_record_ptr(cursor->record_buffer, nrec);
801 if (data && (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
802 hammer_free_data_ptr(cursor->data_buffer, bdata,
803 orec->base.data_len);
806 /* leave cursor intact */
811 * Add the record to the inode's rec_tree. The low 32 bits of a directory
812 * entry's key is used to deal with hash collisions in the upper 32 bits.
813 * A unique 64 bit key is generated in-memory and may be regenerated a
814 * second time when the directory record is flushed to the on-disk B-Tree.
816 * A referenced record is passed to this function. This function
817 * eats the reference. If an error occurs the record will be deleted.
821 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
823 while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
824 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
825 record->flags |= HAMMER_RECF_DELETED;
826 hammer_rel_mem_record(record);
829 if (++trans->hmp->namekey_iterator == 0)
830 ++trans->hmp->namekey_iterator;
831 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
832 record->rec.base.base.key |= trans->hmp->namekey_iterator;
834 record->flags |= HAMMER_RECF_ONRBTREE;
835 hammer_modify_inode(trans, record->ip, HAMMER_INODE_XDIRTY);
836 hammer_rel_mem_record(record);
840 /************************************************************************
841 * HAMMER INODE MERGED-RECORD FUNCTIONS *
842 ************************************************************************
844 * These functions augment the B-Tree scanning functions in hammer_btree.c
845 * by merging in-memory records with on-disk records.
849 * Locate a particular record either in-memory or on-disk.
851 * NOTE: This is basically a standalone routine, hammer_ip_next() may
852 * NOT be called to iterate results.
855 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
860 * If the element is in-memory return it without searching the
863 error = hammer_mem_lookup(cursor, ip);
865 cursor->record = &cursor->iprec->rec;
872 * If the inode has on-disk components search the on-disk B-Tree.
874 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
876 error = hammer_btree_lookup(cursor);
878 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
883 * Locate the first record within the cursor's key_beg/key_end range,
884 * restricted to a particular inode. 0 is returned on success, ENOENT
885 * if no records matched the requested range, or some other error.
887 * When 0 is returned hammer_ip_next() may be used to iterate additional
888 * records within the requested range.
890 * This function can return EDEADLK, requiring the caller to terminate
891 * the cursor and try again.
894 hammer_ip_first(hammer_cursor_t cursor, struct hammer_inode *ip)
899 * Clean up fields and setup for merged scan
901 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
902 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
903 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
905 hammer_rel_mem_record(cursor->iprec);
906 cursor->iprec = NULL;
910 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
911 * exact lookup so if we get ENOENT we have to call the iterate
912 * function to validate the first record after the begin key.
914 * The ATEDISK flag is used by hammer_btree_iterate to determine
915 * whether it must index forwards or not. It is also used here
916 * to select the next record from in-memory or on-disk.
918 * EDEADLK can only occur if the lookup hit an empty internal
919 * element and couldn't delete it. Since this could only occur
920 * in-range, we can just iterate from the failure point.
922 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
923 error = hammer_btree_lookup(cursor);
924 if (error == ENOENT || error == EDEADLK) {
925 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
926 error = hammer_btree_iterate(cursor);
928 if (error && error != ENOENT)
931 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
932 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
934 cursor->flags |= HAMMER_CURSOR_ATEDISK;
939 * Search the in-memory record list (Red-Black tree). Unlike the
940 * B-Tree search, mem_first checks for records in the range.
942 error = hammer_mem_first(cursor, ip);
943 if (error && error != ENOENT)
946 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
947 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
951 * This will return the first matching record.
953 return(hammer_ip_next(cursor));
957 * Retrieve the next record in a merged iteration within the bounds of the
958 * cursor. This call may be made multiple times after the cursor has been
959 * initially searched with hammer_ip_first().
961 * 0 is returned on success, ENOENT if no further records match the
962 * requested range, or some other error code is returned.
965 hammer_ip_next(hammer_cursor_t cursor)
967 hammer_btree_elm_t elm;
973 * Load the current on-disk and in-memory record. If we ate any
974 * records we have to get the next one.
976 * If we deleted the last on-disk record we had scanned ATEDISK will
977 * be clear and DELBTREE will be set, forcing a call to iterate. The
978 * fact that ATEDISK is clear causes iterate to re-test the 'current'
979 * element. If ATEDISK is set, iterate will skip the 'current'
982 * Get the next on-disk record
984 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
985 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
986 error = hammer_btree_iterate(cursor);
987 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
989 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
991 cursor->flags |= HAMMER_CURSOR_DISKEOF |
992 HAMMER_CURSOR_ATEDISK;
997 * Get the next in-memory record. The record can be ripped out
998 * of the RB tree so we maintain a scan_info structure to track
1001 * hammer_rec_scan_cmp: Is the record still in our general range,
1002 * (non-inclusive of snapshot exclusions)?
1003 * hammer_rec_scan_callback: Is the record in our snapshot?
1005 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1006 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1007 if (cursor->iprec) {
1008 hammer_rel_mem_record(cursor->iprec);
1009 cursor->iprec = NULL;
1011 rec = cursor->scan.node; /* next node */
1013 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1015 if (hammer_rec_scan_callback(rec, cursor) != 0)
1017 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1019 if (cursor->iprec) {
1020 KKASSERT(cursor->iprec == rec);
1021 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1023 hammer_rec_rb_tree_RB_NEXT(rec);
1025 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1031 * Extract either the disk or memory record depending on their
1032 * relative position.
1035 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1038 * Both entries valid
1040 elm = &cursor->node->ondisk->elms[cursor->index];
1041 r = hammer_btree_cmp(&elm->base, &cursor->iprec->rec.base.base);
1043 error = hammer_btree_extract(cursor,
1044 HAMMER_CURSOR_GET_RECORD);
1045 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1048 /* fall through to the memory entry */
1049 case HAMMER_CURSOR_ATEDISK:
1051 * Only the memory entry is valid
1053 cursor->record = &cursor->iprec->rec;
1054 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1056 case HAMMER_CURSOR_ATEMEM:
1058 * Only the disk entry is valid
1060 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1061 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1065 * Neither entry is valid
1067 * XXX error not set properly
1069 cursor->record = NULL;
1077 * Resolve the cursor->data pointer for the current cursor position in
1078 * a merged iteration.
1081 hammer_ip_resolve_data(hammer_cursor_t cursor)
1085 if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1086 cursor->data = cursor->iprec->data;
1089 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1095 * Delete all records within the specified range for inode ip.
1097 * NOTE: An unaligned range will cause new records to be added to cover
1098 * the edge cases. (XXX not implemented yet).
1100 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1102 * NOTE: Record keys for regular file data have to be special-cased since
1103 * they indicate the end of the range (key = base + bytes).
1105 * NOTE: The spike structure must be filled in if we return ENOSPC.
1108 hammer_ip_delete_range(hammer_transaction_t trans, hammer_inode_t ip,
1109 int64_t ran_beg, int64_t ran_end,
1110 struct hammer_cursor **spike)
1112 struct hammer_cursor cursor;
1113 hammer_record_ondisk_t rec;
1114 hammer_base_elm_t base;
1119 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1121 cursor.key_beg.obj_id = ip->obj_id;
1122 cursor.key_beg.create_tid = 0;
1123 cursor.key_beg.delete_tid = 0;
1124 cursor.key_beg.obj_type = 0;
1125 cursor.asof = ip->obj_asof;
1126 cursor.flags |= HAMMER_CURSOR_ASOF;
1128 cursor.key_end = cursor.key_beg;
1129 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1130 cursor.key_beg.key = ran_beg;
1131 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1132 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1133 cursor.key_end.key = ran_end;
1136 * The key in the B-Tree is (base+bytes), so the first possible
1137 * matching key is ran_beg + 1.
1141 cursor.key_beg.key = ran_beg + 1;
1142 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1143 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1145 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1146 if (tmp64 < ran_end)
1147 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1149 cursor.key_end.key = ran_end + MAXPHYS + 1;
1151 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1153 error = hammer_ip_first(&cursor, ip);
1156 * Iterate through matching records and mark them as deleted.
1158 while (error == 0) {
1159 rec = cursor.record;
1160 base = &rec->base.base;
1162 KKASSERT(base->delete_tid == 0);
1165 * There may be overlap cases for regular file data. Also
1166 * remember the key for a regular file record is the offset
1167 * of the last byte of the record (base + len - 1), NOT the
1171 kprintf("delete_range rec_type %02x\n", base->rec_type);
1173 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1175 kprintf("delete_range loop key %016llx\n",
1176 base->key - rec->base.data_len);
1178 off = base->key - rec->base.data_len;
1180 * Check the left edge case. We currently do not
1181 * split existing records.
1183 if (off < ran_beg) {
1184 panic("hammer left edge case %016llx %d\n",
1185 base->key, rec->base.data_len);
1189 * Check the right edge case. Note that the
1190 * record can be completely out of bounds, which
1191 * terminates the search.
1193 * base->key is exclusive of the right edge while
1194 * ran_end is inclusive of the right edge. The
1195 * (key - data_len) left boundary is inclusive.
1197 * XXX theory-check this test at some point, are
1198 * we missing a + 1 somewhere? Note that ran_end
1201 if (base->key - 1 > ran_end) {
1202 if (base->key - rec->base.data_len > ran_end)
1204 panic("hammer right edge case\n");
1209 * Mark the record and B-Tree entry as deleted. This will
1210 * also physically delete the B-Tree entry, record, and
1211 * data if the retention policy dictates. The function
1212 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1213 * uses to perform a fixup.
1215 error = hammer_ip_delete_record(&cursor, trans->tid);
1218 error = hammer_ip_next(&cursor);
1220 hammer_done_cursor(&cursor);
1221 if (error == EDEADLK)
1223 if (error == ENOENT)
1229 * Delete all records associated with an inode except the inode record
1233 hammer_ip_delete_range_all(hammer_transaction_t trans, hammer_inode_t ip)
1235 struct hammer_cursor cursor;
1236 hammer_record_ondisk_t rec;
1237 hammer_base_elm_t base;
1241 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1243 cursor.key_beg.obj_id = ip->obj_id;
1244 cursor.key_beg.create_tid = 0;
1245 cursor.key_beg.delete_tid = 0;
1246 cursor.key_beg.obj_type = 0;
1247 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1248 cursor.key_beg.key = HAMMER_MIN_KEY;
1250 cursor.key_end = cursor.key_beg;
1251 cursor.key_end.rec_type = 0xFFFF;
1252 cursor.key_end.key = HAMMER_MAX_KEY;
1254 cursor.asof = ip->obj_asof;
1255 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1257 error = hammer_ip_first(&cursor, ip);
1260 * Iterate through matching records and mark them as deleted.
1262 while (error == 0) {
1263 rec = cursor.record;
1264 base = &rec->base.base;
1266 KKASSERT(base->delete_tid == 0);
1269 * Mark the record and B-Tree entry as deleted. This will
1270 * also physically delete the B-Tree entry, record, and
1271 * data if the retention policy dictates. The function
1272 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1273 * uses to perform a fixup.
1275 error = hammer_ip_delete_record(&cursor, trans->tid);
1278 error = hammer_ip_next(&cursor);
1280 hammer_done_cursor(&cursor);
1281 if (error == EDEADLK)
1283 if (error == ENOENT)
1289 * Delete the record at the current cursor.
1291 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1295 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1297 hammer_btree_elm_t elm;
1302 * In-memory (unsynchronized) records can simply be freed.
1304 if (cursor->record == &cursor->iprec->rec) {
1305 cursor->iprec->flags |= HAMMER_RECF_DELETED;
1310 * On-disk records are marked as deleted by updating their delete_tid.
1312 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1314 hmp = cursor->node->cluster->volume->hmp;
1317 hammer_modify_buffer(cursor->record_buffer);
1318 cursor->record->base.base.delete_tid = tid;
1320 error = hammer_cursor_upgrade(cursor);
1322 hammer_modify_node(cursor->node);
1323 elm = &cursor->node->ondisk->elms[cursor->index];
1324 elm->leaf.base.delete_tid = tid;
1325 hammer_update_syncid(cursor->record_buffer->cluster,
1331 * If we were mounted with the nohistory option, we physically
1332 * delete the record.
1334 if (error == 0 && (hmp->hflags & HMNT_NOHISTORY)) {
1336 int32_t data_offset;
1338 hammer_cluster_t cluster;
1340 rec_offset = elm->leaf.rec_offset;
1341 data_offset = elm->leaf.data_offset;
1342 data_len = elm->leaf.data_len;
1344 kprintf("hammer_ip_delete_record: %08x %08x/%d\n",
1345 rec_offset, data_offset, data_len);
1347 cluster = cursor->node->cluster;
1348 hammer_ref_cluster(cluster);
1350 error = hammer_btree_delete(cursor);
1353 * This forces a fixup for the iteration because
1354 * the cursor is now either sitting at the 'next'
1355 * element or sitting at the end of a leaf.
1357 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1358 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1359 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1361 hammer_free_record(cluster, rec_offset);
1362 if (data_offset && (data_offset - rec_offset < 0 ||
1363 data_offset - rec_offset >= HAMMER_RECORD_SIZE)) {
1364 hammer_free_data(cluster, data_offset,data_len);
1367 hammer_rel_cluster(cluster, 0);
1369 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1377 * Determine whether a directory is empty or not. Returns 0 if the directory
1378 * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1381 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1383 struct hammer_cursor cursor;
1386 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1388 cursor.key_beg.obj_id = ip->obj_id;
1389 cursor.key_beg.create_tid = 0;
1390 cursor.key_beg.delete_tid = 0;
1391 cursor.key_beg.obj_type = 0;
1392 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1393 cursor.key_beg.key = HAMMER_MIN_KEY;
1395 cursor.key_end = cursor.key_beg;
1396 cursor.key_end.rec_type = 0xFFFF;
1397 cursor.key_end.key = HAMMER_MAX_KEY;
1399 cursor.asof = ip->obj_asof;
1400 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1402 error = hammer_ip_first(&cursor, ip);
1403 if (error == ENOENT)
1405 else if (error == 0)
1407 hammer_done_cursor(&cursor);