HAMMER 60A/many: Mirroring work
[dragonfly.git] / sys / vfs / hammer / hammer_object.c
blob4cf883c150c9feeddcda2418e74cbeeb8ed5e474
1 /*
2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_object.c,v 1.81 2008/07/02 21:57:54 dillon Exp $
37 #include "hammer.h"
39 static int hammer_mem_add(hammer_record_t record);
40 static int hammer_mem_lookup(hammer_cursor_t cursor);
41 static int hammer_mem_first(hammer_cursor_t cursor);
42 static int hammer_frontend_trunc_callback(hammer_record_t record,
43 void *data __unused);
44 static int hammer_record_needs_overwrite_delete(hammer_record_t record);
45 static int hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
46 hammer_btree_leaf_elm_t leaf);
48 struct rec_trunc_info {
49 u_int16_t rec_type;
50 int64_t trunc_off;
54 * Red-black tree support. Comparison code for insertion.
56 static int
57 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
59 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
60 return(-1);
61 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
62 return(1);
64 if (rec1->leaf.base.key < rec2->leaf.base.key)
65 return(-1);
66 if (rec1->leaf.base.key > rec2->leaf.base.key)
67 return(1);
70 * Never match against an item deleted by the front-end.
72 * rec1 is greater then rec2 if rec1 is marked deleted.
73 * rec1 is less then rec2 if rec2 is marked deleted.
75 * Multiple deleted records may be present, do not return 0
76 * if both are marked deleted.
78 if (rec1->flags & HAMMER_RECF_DELETED_FE)
79 return(1);
80 if (rec2->flags & HAMMER_RECF_DELETED_FE)
81 return(-1);
83 return(0);
87 * Basic record comparison code similar to hammer_btree_cmp().
89 static int
90 hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
92 if (elm->rec_type < rec->leaf.base.rec_type)
93 return(-3);
94 if (elm->rec_type > rec->leaf.base.rec_type)
95 return(3);
97 if (elm->key < rec->leaf.base.key)
98 return(-2);
99 if (elm->key > rec->leaf.base.key)
100 return(2);
103 * Never match against an item deleted by the front-end.
104 * elm is less then rec if rec is marked deleted.
106 if (rec->flags & HAMMER_RECF_DELETED_FE)
107 return(-1);
108 return(0);
112 * Special LOOKUP_INFO to locate an overlapping record. This used by
113 * the reservation code to implement small-block records (whos keys will
114 * be different depending on data_len, when representing the same base
115 * offset).
117 * NOTE: The base file offset of a data record is (key - data_len), not (key).
119 static int
120 hammer_rec_overlap_compare(hammer_btree_leaf_elm_t leaf, hammer_record_t rec)
122 if (leaf->base.rec_type < rec->leaf.base.rec_type)
123 return(-3);
124 if (leaf->base.rec_type > rec->leaf.base.rec_type)
125 return(3);
128 * Overlap compare
130 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
131 /* leaf_end <= rec_beg */
132 if (leaf->base.key <= rec->leaf.base.key - rec->leaf.data_len)
133 return(-2);
134 /* leaf_beg >= rec_end */
135 if (leaf->base.key - leaf->data_len >= rec->leaf.base.key)
136 return(2);
137 } else {
138 if (leaf->base.key < rec->leaf.base.key)
139 return(-2);
140 if (leaf->base.key > rec->leaf.base.key)
141 return(2);
145 * Never match against an item deleted by the front-end.
146 * leaf is less then rec if rec is marked deleted.
148 * We must still return the proper code for the scan to continue
149 * along the correct branches.
151 if (rec->flags & HAMMER_RECF_DELETED_FE) {
152 if (leaf->base.key < rec->leaf.base.key)
153 return(-2);
154 if (leaf->base.key > rec->leaf.base.key)
155 return(2);
156 return(-1);
158 return(0);
162 * RB_SCAN comparison code for hammer_mem_first(). The argument order
163 * is reversed so the comparison result has to be negated. key_beg and
164 * key_end are both range-inclusive.
166 * Localized deletions are not cached in-memory.
168 static
170 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
172 hammer_cursor_t cursor = data;
173 int r;
175 r = hammer_rec_cmp(&cursor->key_beg, rec);
176 if (r > 1)
177 return(-1);
178 r = hammer_rec_cmp(&cursor->key_end, rec);
179 if (r < -1)
180 return(1);
181 return(0);
185 * This compare function is used when simply looking up key_beg.
187 static
189 hammer_rec_find_cmp(hammer_record_t rec, void *data)
191 hammer_cursor_t cursor = data;
192 int r;
194 r = hammer_rec_cmp(&cursor->key_beg, rec);
195 if (r > 1)
196 return(-1);
197 if (r < -1)
198 return(1);
199 return(0);
203 * Locate blocks within the truncation range. Partial blocks do not count.
205 static
207 hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
209 struct rec_trunc_info *info = data;
211 if (rec->leaf.base.rec_type < info->rec_type)
212 return(-1);
213 if (rec->leaf.base.rec_type > info->rec_type)
214 return(1);
216 switch(rec->leaf.base.rec_type) {
217 case HAMMER_RECTYPE_DB:
219 * DB record key is not beyond the truncation point, retain.
221 if (rec->leaf.base.key < info->trunc_off)
222 return(-1);
223 break;
224 case HAMMER_RECTYPE_DATA:
226 * DATA record offset start is not beyond the truncation point,
227 * retain.
229 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
230 return(-1);
231 break;
232 default:
233 panic("hammer_rec_trunc_cmp: unexpected record type");
237 * The record start is >= the truncation point, return match,
238 * the record should be destroyed.
240 return(0);
243 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
244 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
245 hammer_rec_overlap_compare, hammer_btree_leaf_elm_t);
248 * Allocate a record for the caller to finish filling in. The record is
249 * returned referenced.
251 hammer_record_t
252 hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
254 hammer_record_t record;
256 ++hammer_count_records;
257 record = kmalloc(sizeof(*record), M_HAMMER,
258 M_WAITOK | M_ZERO | M_USE_RESERVE);
259 record->flush_state = HAMMER_FST_IDLE;
260 record->ip = ip;
261 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
262 record->leaf.data_len = data_len;
263 hammer_ref(&record->lock);
265 if (data_len) {
266 record->data = kmalloc(data_len, M_HAMMER, M_WAITOK | M_ZERO);
267 record->flags |= HAMMER_RECF_ALLOCDATA;
268 ++hammer_count_record_datas;
271 return (record);
274 void
275 hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
277 while (record->flush_state == HAMMER_FST_FLUSH) {
278 record->flags |= HAMMER_RECF_WANTED;
279 tsleep(record, 0, ident, 0);
284 * Called from the backend, hammer_inode.c, after a record has been
285 * flushed to disk. The record has been exclusively locked by the
286 * caller and interlocked with BE.
288 * We clean up the state, unlock, and release the record (the record
289 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
291 void
292 hammer_flush_record_done(hammer_record_t record, int error)
294 hammer_inode_t target_ip;
296 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
297 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
299 if (error) {
301 * An error occured, the backend was unable to sync the
302 * record to its media. Leave the record intact.
304 Debugger("flush_record_done error");
307 if (record->flags & HAMMER_RECF_DELETED_BE) {
308 if ((target_ip = record->target_ip) != NULL) {
309 TAILQ_REMOVE(&target_ip->target_list, record,
310 target_entry);
311 record->target_ip = NULL;
312 hammer_test_inode(target_ip);
314 record->flush_state = HAMMER_FST_IDLE;
315 } else {
316 if (record->target_ip) {
317 record->flush_state = HAMMER_FST_SETUP;
318 hammer_test_inode(record->ip);
319 hammer_test_inode(record->target_ip);
320 } else {
321 record->flush_state = HAMMER_FST_IDLE;
324 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
325 if (record->flags & HAMMER_RECF_WANTED) {
326 record->flags &= ~HAMMER_RECF_WANTED;
327 wakeup(record);
329 hammer_rel_mem_record(record);
333 * Release a memory record. Records marked for deletion are immediately
334 * removed from the RB-Tree but otherwise left intact until the last ref
335 * goes away.
337 void
338 hammer_rel_mem_record(struct hammer_record *record)
340 hammer_inode_t ip, target_ip;
342 hammer_unref(&record->lock);
344 if (record->lock.refs == 0) {
346 * Upon release of the last reference wakeup any waiters.
347 * The record structure may get destroyed so callers will
348 * loop up and do a relookup.
350 * WARNING! Record must be removed from RB-TREE before we
351 * might possibly block. hammer_test_inode() can block!
353 ip = record->ip;
356 * Upon release of the last reference a record marked deleted
357 * is destroyed.
359 if (record->flags & HAMMER_RECF_DELETED_FE) {
360 KKASSERT(ip->lock.refs > 0);
361 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
364 * target_ip may have zero refs, we have to ref it
365 * to prevent it from being ripped out from under
366 * us.
368 if ((target_ip = record->target_ip) != NULL) {
369 TAILQ_REMOVE(&target_ip->target_list,
370 record, target_entry);
371 record->target_ip = NULL;
372 hammer_ref(&target_ip->lock);
375 if (record->flags & HAMMER_RECF_ONRBTREE) {
376 RB_REMOVE(hammer_rec_rb_tree,
377 &record->ip->rec_tree,
378 record);
379 KKASSERT(ip->rsv_recs > 0);
380 --ip->hmp->rsv_recs;
381 --ip->rsv_recs;
382 ip->hmp->rsv_databytes -= record->leaf.data_len;
383 record->flags &= ~HAMMER_RECF_ONRBTREE;
385 if (RB_EMPTY(&record->ip->rec_tree)) {
386 record->ip->flags &= ~HAMMER_INODE_XDIRTY;
387 record->ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
388 hammer_test_inode(record->ip);
393 * Do this test after removing record from the B-Tree.
395 if (target_ip) {
396 hammer_test_inode(target_ip);
397 hammer_rel_inode(target_ip, 0);
400 if (record->flags & HAMMER_RECF_ALLOCDATA) {
401 --hammer_count_record_datas;
402 kfree(record->data, M_HAMMER);
403 record->flags &= ~HAMMER_RECF_ALLOCDATA;
405 if (record->resv) {
406 hammer_blockmap_reserve_complete(ip->hmp,
407 record->resv);
408 record->resv = NULL;
410 record->data = NULL;
411 --hammer_count_records;
412 kfree(record, M_HAMMER);
418 * Record visibility depends on whether the record is being accessed by
419 * the backend or the frontend.
421 * Return non-zero if the record is visible, zero if it isn't or if it is
422 * deleted.
424 static __inline
426 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
428 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
429 if (record->flags & HAMMER_RECF_DELETED_BE)
430 return(0);
431 } else {
432 if (record->flags & HAMMER_RECF_DELETED_FE)
433 return(0);
435 return(1);
439 * This callback is used as part of the RB_SCAN function for in-memory
440 * records. We terminate it (return -1) as soon as we get a match.
442 * This routine is used by frontend code.
444 * The primary compare code does not account for ASOF lookups. This
445 * code handles that case as well as a few others.
447 static
449 hammer_rec_scan_callback(hammer_record_t rec, void *data)
451 hammer_cursor_t cursor = data;
454 * We terminate on success, so this should be NULL on entry.
456 KKASSERT(cursor->iprec == NULL);
459 * Skip if the record was marked deleted.
461 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
462 return(0);
465 * Skip if not visible due to our as-of TID
467 if (cursor->flags & HAMMER_CURSOR_ASOF) {
468 if (cursor->asof < rec->leaf.base.create_tid)
469 return(0);
470 if (rec->leaf.base.delete_tid &&
471 cursor->asof >= rec->leaf.base.delete_tid) {
472 return(0);
477 * If the record is queued to the flusher we have to block until
478 * it isn't. Otherwise we may see duplication between our memory
479 * cache and the media.
481 hammer_ref(&rec->lock);
483 #warning "This deadlocks"
484 #if 0
485 if (rec->flush_state == HAMMER_FST_FLUSH)
486 hammer_wait_mem_record(rec);
487 #endif
490 * The record may have been deleted while we were blocked.
492 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
493 hammer_rel_mem_record(rec);
494 return(0);
498 * Set the matching record and stop the scan.
500 cursor->iprec = rec;
501 return(-1);
506 * Lookup an in-memory record given the key specified in the cursor. Works
507 * just like hammer_btree_lookup() but operates on an inode's in-memory
508 * record list.
510 * The lookup must fail if the record is marked for deferred deletion.
512 static
514 hammer_mem_lookup(hammer_cursor_t cursor)
516 int error;
518 KKASSERT(cursor->ip);
519 if (cursor->iprec) {
520 hammer_rel_mem_record(cursor->iprec);
521 cursor->iprec = NULL;
523 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
524 hammer_rec_scan_callback, cursor);
526 if (cursor->iprec == NULL)
527 error = ENOENT;
528 else
529 error = 0;
530 return(error);
534 * hammer_mem_first() - locate the first in-memory record matching the
535 * cursor within the bounds of the key range.
537 static
539 hammer_mem_first(hammer_cursor_t cursor)
541 hammer_inode_t ip;
543 ip = cursor->ip;
544 KKASSERT(ip != NULL);
546 if (cursor->iprec) {
547 hammer_rel_mem_record(cursor->iprec);
548 cursor->iprec = NULL;
551 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
552 hammer_rec_scan_callback, cursor);
555 * Adjust scan.node and keep it linked into the RB-tree so we can
556 * hold the cursor through third party modifications of the RB-tree.
558 if (cursor->iprec)
559 return(0);
560 return(ENOENT);
563 void
564 hammer_mem_done(hammer_cursor_t cursor)
566 if (cursor->iprec) {
567 hammer_rel_mem_record(cursor->iprec);
568 cursor->iprec = NULL;
572 /************************************************************************
573 * HAMMER IN-MEMORY RECORD FUNCTIONS *
574 ************************************************************************
576 * These functions manipulate in-memory records. Such records typically
577 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
581 * Add a directory entry (dip,ncp) which references inode (ip).
583 * Note that the low 32 bits of the namekey are set temporarily to create
584 * a unique in-memory record, and may be modified a second time when the
585 * record is synchronized to disk. In particular, the low 32 bits cannot be
586 * all 0's when synching to disk, which is not handled here.
588 * NOTE: bytes does not include any terminating \0 on name, and name might
589 * not be terminated.
592 hammer_ip_add_directory(struct hammer_transaction *trans,
593 struct hammer_inode *dip, const char *name, int bytes,
594 struct hammer_inode *ip)
596 struct hammer_cursor cursor;
597 hammer_record_t record;
598 int error;
599 int count;
600 u_int32_t iterator;
602 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
603 if (++trans->hmp->namekey_iterator == 0)
604 ++trans->hmp->namekey_iterator;
606 record->type = HAMMER_MEM_RECORD_ADD;
607 record->leaf.base.localization = dip->obj_localization +
608 HAMMER_LOCALIZE_MISC;
609 record->leaf.base.obj_id = dip->obj_id;
610 record->leaf.base.key = hammer_directory_namekey(name, bytes);
611 record->leaf.base.key += trans->hmp->namekey_iterator;
612 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
613 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
614 record->data->entry.obj_id = ip->obj_id;
615 record->data->entry.localization = ip->obj_localization;
616 bcopy(name, record->data->entry.name, bytes);
618 ++ip->ino_data.nlinks;
619 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
622 * Find an unused namekey. Both the in-memory record tree and
623 * the B-Tree are checked. Exact matches also match create_tid
624 * so use an ASOF search to (mostly) ignore it.
626 * delete-visibility is set so pending deletions do not give us
627 * a false-negative on our ability to use an iterator.
629 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
630 cursor.key_beg = record->leaf.base;
631 cursor.flags |= HAMMER_CURSOR_ASOF;
632 cursor.flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
633 cursor.asof = ip->obj_asof;
635 count = 0;
636 while (hammer_ip_lookup(&cursor) == 0) {
637 iterator = (u_int32_t)record->leaf.base.key + 1;
638 if (iterator == 0)
639 iterator = 1;
640 record->leaf.base.key &= ~0xFFFFFFFFLL;
641 record->leaf.base.key |= iterator;
642 cursor.key_beg.key = record->leaf.base.key;
643 if (++count == 1000000000) {
644 hammer_rel_mem_record(record);
645 error = ENOSPC;
646 goto failed;
651 * The target inode and the directory entry are bound together.
653 record->target_ip = ip;
654 record->flush_state = HAMMER_FST_SETUP;
655 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
658 * The inode now has a dependancy and must be taken out of the idle
659 * state. An inode not in an idle state is given an extra reference.
661 if (ip->flush_state == HAMMER_FST_IDLE) {
662 hammer_ref(&ip->lock);
663 ip->flush_state = HAMMER_FST_SETUP;
665 error = hammer_mem_add(record);
666 failed:
667 hammer_done_cursor(&cursor);
668 return(error);
672 * Delete the directory entry and update the inode link count. The
673 * cursor must be seeked to the directory entry record being deleted.
675 * The related inode should be share-locked by the caller. The caller is
676 * on the frontend.
678 * This function can return EDEADLK requiring the caller to terminate
679 * the cursor, any locks, wait on the returned record, and retry.
682 hammer_ip_del_directory(struct hammer_transaction *trans,
683 hammer_cursor_t cursor, struct hammer_inode *dip,
684 struct hammer_inode *ip)
686 hammer_record_t record;
687 int error;
689 if (hammer_cursor_inmem(cursor)) {
691 * In-memory (unsynchronized) records can simply be freed.
692 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
693 * by the backend, we must still avoid races against the
694 * backend potentially syncing the record to the media.
696 * We cannot call hammer_ip_delete_record(), that routine may
697 * only be called from the backend.
699 record = cursor->iprec;
700 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
701 KKASSERT(cursor->deadlk_rec == NULL);
702 hammer_ref(&record->lock);
703 cursor->deadlk_rec = record;
704 error = EDEADLK;
705 } else {
706 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
707 record->flags |= HAMMER_RECF_DELETED_FE;
708 error = 0;
710 } else {
712 * If the record is on-disk we have to queue the deletion by
713 * the record's key. This also causes lookups to skip the
714 * record.
716 KKASSERT(dip->flags &
717 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
718 record = hammer_alloc_mem_record(dip, 0);
719 record->type = HAMMER_MEM_RECORD_DEL;
720 record->leaf.base = cursor->leaf->base;
722 record->target_ip = ip;
723 record->flush_state = HAMMER_FST_SETUP;
724 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
727 * The inode now has a dependancy and must be taken out of
728 * the idle state. An inode not in an idle state is given
729 * an extra reference.
731 if (ip->flush_state == HAMMER_FST_IDLE) {
732 hammer_ref(&ip->lock);
733 ip->flush_state = HAMMER_FST_SETUP;
736 error = hammer_mem_add(record);
740 * One less link. The file may still be open in the OS even after
741 * all links have gone away.
743 * We have to terminate the cursor before syncing the inode to
744 * avoid deadlocking against ourselves. XXX this may no longer
745 * be true.
747 * If nlinks drops to zero and the vnode is inactive (or there is
748 * no vnode), call hammer_inode_unloadable_check() to zonk the
749 * inode. If we don't do this here the inode will not be destroyed
750 * on-media until we unmount.
752 if (error == 0) {
753 --ip->ino_data.nlinks;
754 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
755 if (ip->ino_data.nlinks == 0 &&
756 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
757 hammer_done_cursor(cursor);
758 hammer_inode_unloadable_check(ip, 1);
759 hammer_flush_inode(ip, 0);
763 return(error);
767 * Add a record to an inode.
769 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
770 * initialize the following additional fields:
772 * The related inode should be share-locked by the caller. The caller is
773 * on the frontend.
775 * record->rec.entry.base.base.key
776 * record->rec.entry.base.base.rec_type
777 * record->rec.entry.base.base.data_len
778 * record->data (a copy will be kmalloc'd if it cannot be embedded)
781 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
783 hammer_inode_t ip = record->ip;
784 int error;
786 KKASSERT(record->leaf.base.localization != 0);
787 record->leaf.base.obj_id = ip->obj_id;
788 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
789 error = hammer_mem_add(record);
790 return(error);
794 * Locate a bulk record in-memory. Bulk records allow disk space to be
795 * reserved so the front-end can flush large data writes without having
796 * to queue the BIO to the flusher. Only the related record gets queued
797 * to the flusher.
799 static hammer_record_t
800 hammer_ip_get_bulk(hammer_inode_t ip, off_t file_offset, int bytes)
802 hammer_record_t record;
803 struct hammer_btree_leaf_elm leaf;
805 bzero(&leaf, sizeof(leaf));
806 leaf.base.obj_id = ip->obj_id;
807 leaf.base.key = file_offset + bytes;
808 leaf.base.create_tid = 0;
809 leaf.base.delete_tid = 0;
810 leaf.base.rec_type = HAMMER_RECTYPE_DATA;
811 leaf.base.obj_type = 0; /* unused */
812 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD; /* unused */
813 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_MISC;
814 leaf.data_len = bytes;
816 record = hammer_rec_rb_tree_RB_LOOKUP_INFO(&ip->rec_tree, &leaf);
817 if (record)
818 hammer_ref(&record->lock);
819 return(record);
823 * Reserve blockmap space placemarked with an in-memory record.
825 * This routine is called by the frontend in order to be able to directly
826 * flush a buffer cache buffer. The frontend has locked the related buffer
827 * cache buffers and we should be able to manipulate any overlapping
828 * in-memory records.
830 hammer_record_t
831 hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
832 int *errorp)
834 hammer_record_t record;
835 hammer_record_t conflict;
836 int zone;
837 int flags;
840 * Deal with conflicting in-memory records. We cannot have multiple
841 * in-memory records for the same offset without seriously confusing
842 * the backend, including but not limited to the backend issuing
843 * delete-create-delete sequences and asserting on the delete_tid
844 * being the same as the create_tid.
846 * If we encounter a record with the backend interlock set we cannot
847 * immediately delete it without confusing the backend.
849 while ((conflict = hammer_ip_get_bulk(ip, file_offset, bytes)) !=NULL) {
850 if (conflict->flags & HAMMER_RECF_INTERLOCK_BE) {
851 conflict->flags |= HAMMER_RECF_WANTED;
852 tsleep(conflict, 0, "hmrrc3", 0);
853 } else {
854 conflict->flags |= HAMMER_RECF_DELETED_FE;
856 hammer_rel_mem_record(conflict);
860 * Create a record to cover the direct write. This is called with
861 * the related BIO locked so there should be no possible conflict.
863 * The backend is responsible for finalizing the space reserved in
864 * this record.
866 * XXX bytes not aligned, depend on the reservation code to
867 * align the reservation.
869 record = hammer_alloc_mem_record(ip, 0);
870 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
871 HAMMER_ZONE_SMALL_DATA_INDEX;
872 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
873 &record->leaf.data_offset,
874 errorp);
875 if (record->resv == NULL) {
876 kprintf("hammer_ip_add_bulk: reservation failed\n");
877 hammer_rel_mem_record(record);
878 return(NULL);
880 record->type = HAMMER_MEM_RECORD_DATA;
881 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
882 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
883 record->leaf.base.obj_id = ip->obj_id;
884 record->leaf.base.key = file_offset + bytes;
885 record->leaf.base.localization = ip->obj_localization +
886 HAMMER_LOCALIZE_MISC;
887 record->leaf.data_len = bytes;
888 hammer_crc_set_leaf(data, &record->leaf);
889 flags = record->flags;
891 hammer_ref(&record->lock); /* mem_add eats a reference */
892 *errorp = hammer_mem_add(record);
893 if (*errorp) {
894 conflict = hammer_ip_get_bulk(ip, file_offset, bytes);
895 kprintf("hammer_ip_add_bulk: error %d conflict %p file_offset %lld bytes %d\n",
896 *errorp, conflict, file_offset, bytes);
897 if (conflict)
898 kprintf("conflict %lld %d\n", conflict->leaf.base.key, conflict->leaf.data_len);
899 if (conflict)
900 hammer_rel_mem_record(conflict);
902 KKASSERT(*errorp == 0);
903 conflict = hammer_ip_get_bulk(ip, file_offset, bytes);
904 if (conflict != record) {
905 kprintf("conflict mismatch %p %p %08x\n", conflict, record, record->flags);
906 if (conflict)
907 kprintf("conflict mismatch %lld/%d %lld/%d\n", conflict->leaf.base.key, conflict->leaf.data_len, record->leaf.base.key, record->leaf.data_len);
909 KKASSERT(conflict == record);
910 hammer_rel_mem_record(conflict);
912 return (record);
916 * Frontend truncation code. Scan in-memory records only. On-disk records
917 * and records in a flushing state are handled by the backend. The vnops
918 * setattr code will handle the block containing the truncation point.
920 * Partial blocks are not deleted.
923 hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
925 struct rec_trunc_info info;
927 switch(ip->ino_data.obj_type) {
928 case HAMMER_OBJTYPE_REGFILE:
929 info.rec_type = HAMMER_RECTYPE_DATA;
930 break;
931 case HAMMER_OBJTYPE_DBFILE:
932 info.rec_type = HAMMER_RECTYPE_DB;
933 break;
934 default:
935 return(EINVAL);
937 info.trunc_off = file_size;
938 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
939 hammer_frontend_trunc_callback, &info);
940 return(0);
943 static int
944 hammer_frontend_trunc_callback(hammer_record_t record, void *data __unused)
946 if (record->flags & HAMMER_RECF_DELETED_FE)
947 return(0);
948 if (record->flush_state == HAMMER_FST_FLUSH)
949 return(0);
950 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
951 hammer_ref(&record->lock);
952 record->flags |= HAMMER_RECF_DELETED_FE;
953 hammer_rel_mem_record(record);
954 return(0);
958 * Return 1 if the caller must check for and delete existing records
959 * before writing out a new data record.
961 * Return 0 if the caller can just insert the record into the B-Tree without
962 * checking.
964 static int
965 hammer_record_needs_overwrite_delete(hammer_record_t record)
967 hammer_inode_t ip = record->ip;
968 int64_t file_offset;
969 int r;
971 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
972 file_offset = record->leaf.base.key;
973 else
974 file_offset = record->leaf.base.key - record->leaf.data_len;
975 r = (file_offset < ip->save_trunc_off);
976 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
977 if (ip->save_trunc_off <= record->leaf.base.key)
978 ip->save_trunc_off = record->leaf.base.key + 1;
979 } else {
980 if (ip->save_trunc_off < record->leaf.base.key)
981 ip->save_trunc_off = record->leaf.base.key;
983 return(r);
987 * Backend code. Sync a record to the media.
990 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
992 hammer_transaction_t trans = cursor->trans;
993 int64_t file_offset;
994 int bytes;
995 void *bdata;
996 int error;
998 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
999 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
1000 KKASSERT(record->leaf.base.localization != 0);
1003 * If this is a bulk-data record placemarker there may be an existing
1004 * record on-disk, indicating a data overwrite. If there is the
1005 * on-disk record must be deleted before we can insert our new record.
1007 * We've synthesized this record and do not know what the create_tid
1008 * on-disk is, nor how much data it represents.
1010 * Keep in mind that (key) for data records is (base_offset + len),
1011 * not (base_offset). Also, we only want to get rid of on-disk
1012 * records since we are trying to sync our in-memory record, call
1013 * hammer_ip_delete_range() with truncating set to 1 to make sure
1014 * it skips in-memory records.
1016 * It is ok for the lookup to return ENOENT.
1018 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1019 * to call hammer_ip_delete_range() or not. This also means we must
1020 * update sync_trunc_off() as we write.
1022 if (record->type == HAMMER_MEM_RECORD_DATA &&
1023 hammer_record_needs_overwrite_delete(record)) {
1024 file_offset = record->leaf.base.key - record->leaf.data_len;
1025 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1026 ~HAMMER_BUFMASK;
1027 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
1028 error = hammer_ip_delete_range(
1029 cursor, record->ip,
1030 file_offset, file_offset + bytes - 1,
1032 if (error && error != ENOENT)
1033 goto done;
1037 * If this is a general record there may be an on-disk version
1038 * that must be deleted before we can insert the new record.
1040 if (record->type == HAMMER_MEM_RECORD_GENERAL) {
1041 error = hammer_delete_general(cursor, record->ip,
1042 &record->leaf);
1043 if (error && error != ENOENT)
1044 goto done;
1048 * Setup the cursor.
1050 hammer_normalize_cursor(cursor);
1051 cursor->key_beg = record->leaf.base;
1052 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1053 cursor->flags |= HAMMER_CURSOR_BACKEND;
1054 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1057 * Records can wind up on-media before the inode itself is on-media.
1058 * Flag the case.
1060 record->ip->flags |= HAMMER_INODE_DONDISK;
1063 * If we are deleting a directory entry an exact match must be
1064 * found on-disk.
1066 if (record->type == HAMMER_MEM_RECORD_DEL) {
1067 error = hammer_btree_lookup(cursor);
1068 if (error == 0) {
1069 error = hammer_ip_delete_record(cursor, record->ip,
1070 trans->tid);
1071 if (error == 0) {
1072 record->flags |= HAMMER_RECF_DELETED_FE;
1073 record->flags |= HAMMER_RECF_DELETED_BE;
1076 goto done;
1080 * We are inserting.
1082 * Issue a lookup to position the cursor and locate the cluster. The
1083 * target key should not exist. If we are creating a directory entry
1084 * we may have to iterate the low 32 bits of the key to find an unused
1085 * key.
1087 cursor->flags |= HAMMER_CURSOR_INSERT;
1089 error = hammer_btree_lookup(cursor);
1090 if (hammer_debug_inode)
1091 kprintf("DOINSERT LOOKUP %d\n", error);
1092 if (error == 0) {
1093 kprintf("hammer_ip_sync_record: duplicate rec "
1094 "at (%016llx)\n", record->leaf.base.key);
1095 Debugger("duplicate record1");
1096 error = EIO;
1098 #if 0
1099 if (record->type == HAMMER_MEM_RECORD_DATA)
1100 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1101 record->leaf.base.key - record->leaf.data_len,
1102 record->leaf.data_offset, error);
1103 #endif
1105 if (error != ENOENT)
1106 goto done;
1109 * Allocate the record and data. The result buffers will be
1110 * marked as being modified and further calls to
1111 * hammer_modify_buffer() will result in unneeded UNDO records.
1113 * Support zero-fill records (data == NULL and data_len != 0)
1115 if (record->type == HAMMER_MEM_RECORD_DATA) {
1117 * The data portion of a bulk-data record has already been
1118 * committed to disk, we need only adjust the layer2
1119 * statistics in the same transaction as our B-Tree insert.
1121 KKASSERT(record->leaf.data_offset != 0);
1122 hammer_blockmap_finalize(trans, record->leaf.data_offset,
1123 record->leaf.data_len);
1124 error = 0;
1125 } else if (record->data && record->leaf.data_len) {
1127 * Wholely cached record, with data. Allocate the data.
1129 bdata = hammer_alloc_data(trans, record->leaf.data_len,
1130 record->leaf.base.rec_type,
1131 &record->leaf.data_offset,
1132 &cursor->data_buffer, &error);
1133 if (bdata == NULL)
1134 goto done;
1135 hammer_crc_set_leaf(record->data, &record->leaf);
1136 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
1137 bcopy(record->data, bdata, record->leaf.data_len);
1138 hammer_modify_buffer_done(cursor->data_buffer);
1139 } else {
1141 * Wholely cached record, without data.
1143 record->leaf.data_offset = 0;
1144 record->leaf.data_crc = 0;
1147 error = hammer_btree_insert(cursor, &record->leaf);
1148 if (hammer_debug_inode && error)
1149 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n", error, cursor->node->node_offset, cursor->index, record->leaf.base.key);
1152 * Our record is on-disk, normally mark the in-memory version as
1153 * deleted. If the record represented a directory deletion but
1154 * we had to sync a valid directory entry to disk we must convert
1155 * the record to a covering delete so the frontend does not have
1156 * visibility on the synced entry.
1158 if (error == 0) {
1159 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1160 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1161 record->flags &= ~HAMMER_RECF_DELETED_FE;
1162 record->type = HAMMER_MEM_RECORD_DEL;
1163 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1164 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1165 /* hammer_flush_record_done takes care of the rest */
1166 } else {
1167 record->flags |= HAMMER_RECF_DELETED_FE;
1168 record->flags |= HAMMER_RECF_DELETED_BE;
1170 } else {
1171 if (record->leaf.data_offset) {
1172 hammer_blockmap_free(trans, record->leaf.data_offset,
1173 record->leaf.data_len);
1177 done:
1178 return(error);
1182 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1183 * entry's key is used to deal with hash collisions in the upper 32 bits.
1184 * A unique 64 bit key is generated in-memory and may be regenerated a
1185 * second time when the directory record is flushed to the on-disk B-Tree.
1187 * A referenced record is passed to this function. This function
1188 * eats the reference. If an error occurs the record will be deleted.
1190 * A copy of the temporary record->data pointer provided by the caller
1191 * will be made.
1193 static
1195 hammer_mem_add(hammer_record_t record)
1197 hammer_mount_t hmp = record->ip->hmp;
1200 * Make a private copy of record->data
1202 if (record->data)
1203 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
1206 * Insert into the RB tree. A unique key should have already
1207 * been selected if this is a directory entry.
1209 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1210 record->flags |= HAMMER_RECF_DELETED_FE;
1211 hammer_rel_mem_record(record);
1212 return (EEXIST);
1214 ++hmp->count_newrecords;
1215 ++hmp->rsv_recs;
1216 ++record->ip->rsv_recs;
1217 record->ip->hmp->rsv_databytes += record->leaf.data_len;
1218 record->flags |= HAMMER_RECF_ONRBTREE;
1219 hammer_modify_inode(record->ip, HAMMER_INODE_XDIRTY);
1220 hammer_rel_mem_record(record);
1221 return(0);
1224 /************************************************************************
1225 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1226 ************************************************************************
1228 * These functions augment the B-Tree scanning functions in hammer_btree.c
1229 * by merging in-memory records with on-disk records.
1233 * Locate a particular record either in-memory or on-disk.
1235 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1236 * NOT be called to iterate results.
1239 hammer_ip_lookup(hammer_cursor_t cursor)
1241 int error;
1244 * If the element is in-memory return it without searching the
1245 * on-disk B-Tree
1247 KKASSERT(cursor->ip);
1248 error = hammer_mem_lookup(cursor);
1249 if (error == 0) {
1250 cursor->leaf = &cursor->iprec->leaf;
1251 return(error);
1253 if (error != ENOENT)
1254 return(error);
1257 * If the inode has on-disk components search the on-disk B-Tree.
1259 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1260 return(error);
1261 error = hammer_btree_lookup(cursor);
1262 if (error == 0)
1263 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1264 return(error);
1268 * Locate the first record within the cursor's key_beg/key_end range,
1269 * restricted to a particular inode. 0 is returned on success, ENOENT
1270 * if no records matched the requested range, or some other error.
1272 * When 0 is returned hammer_ip_next() may be used to iterate additional
1273 * records within the requested range.
1275 * This function can return EDEADLK, requiring the caller to terminate
1276 * the cursor and try again.
1279 hammer_ip_first(hammer_cursor_t cursor)
1281 hammer_inode_t ip = cursor->ip;
1282 int error;
1284 KKASSERT(ip != NULL);
1287 * Clean up fields and setup for merged scan
1289 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1290 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
1291 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
1292 if (cursor->iprec) {
1293 hammer_rel_mem_record(cursor->iprec);
1294 cursor->iprec = NULL;
1298 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1299 * exact lookup so if we get ENOENT we have to call the iterate
1300 * function to validate the first record after the begin key.
1302 * The ATEDISK flag is used by hammer_btree_iterate to determine
1303 * whether it must index forwards or not. It is also used here
1304 * to select the next record from in-memory or on-disk.
1306 * EDEADLK can only occur if the lookup hit an empty internal
1307 * element and couldn't delete it. Since this could only occur
1308 * in-range, we can just iterate from the failure point.
1310 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1311 error = hammer_btree_lookup(cursor);
1312 if (error == ENOENT || error == EDEADLK) {
1313 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1314 if (hammer_debug_general & 0x2000)
1315 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
1316 error = hammer_btree_iterate(cursor);
1318 if (error && error != ENOENT)
1319 return(error);
1320 if (error == 0) {
1321 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
1322 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1323 } else {
1324 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1329 * Search the in-memory record list (Red-Black tree). Unlike the
1330 * B-Tree search, mem_first checks for records in the range.
1332 error = hammer_mem_first(cursor);
1333 if (error && error != ENOENT)
1334 return(error);
1335 if (error == 0) {
1336 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
1337 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1338 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0)
1339 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1343 * This will return the first matching record.
1345 return(hammer_ip_next(cursor));
1349 * Retrieve the next record in a merged iteration within the bounds of the
1350 * cursor. This call may be made multiple times after the cursor has been
1351 * initially searched with hammer_ip_first().
1353 * 0 is returned on success, ENOENT if no further records match the
1354 * requested range, or some other error code is returned.
1357 hammer_ip_next(hammer_cursor_t cursor)
1359 hammer_btree_elm_t elm;
1360 hammer_record_t rec, save;
1361 int error;
1362 int r;
1364 next_btree:
1366 * Load the current on-disk and in-memory record. If we ate any
1367 * records we have to get the next one.
1369 * If we deleted the last on-disk record we had scanned ATEDISK will
1370 * be clear and DELBTREE will be set, forcing a call to iterate. The
1371 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1372 * element. If ATEDISK is set, iterate will skip the 'current'
1373 * element.
1375 * Get the next on-disk record
1377 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
1378 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1379 error = hammer_btree_iterate(cursor);
1380 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1381 if (error == 0) {
1382 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1383 hammer_cache_node(&cursor->ip->cache[1],
1384 cursor->node);
1385 } else {
1386 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1387 HAMMER_CURSOR_ATEDISK;
1392 next_memory:
1394 * Get the next in-memory record. The record can be ripped out
1395 * of the RB tree so we maintain a scan_info structure to track
1396 * the next node.
1398 * hammer_rec_scan_cmp: Is the record still in our general range,
1399 * (non-inclusive of snapshot exclusions)?
1400 * hammer_rec_scan_callback: Is the record in our snapshot?
1402 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1403 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1404 save = cursor->iprec;
1405 cursor->iprec = NULL;
1406 rec = save ? hammer_rec_rb_tree_RB_NEXT(save) : NULL;
1407 while (rec) {
1408 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1409 break;
1410 if (hammer_rec_scan_callback(rec, cursor) != 0)
1411 break;
1412 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1414 if (save)
1415 hammer_rel_mem_record(save);
1416 if (cursor->iprec) {
1417 KKASSERT(cursor->iprec == rec);
1418 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1419 } else {
1420 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1426 * The memory record may have become stale while being held in
1427 * cursor->iprec. We are interlocked against the backend on
1428 * with regards to B-Tree entries.
1430 if ((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0) {
1431 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0) {
1432 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1433 goto next_memory;
1438 * Extract either the disk or memory record depending on their
1439 * relative position.
1441 error = 0;
1442 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1443 case 0:
1445 * Both entries valid. Compare the entries and nominally
1446 * return the first one in the sort order. Numerous cases
1447 * require special attention, however.
1449 elm = &cursor->node->ondisk->elms[cursor->index];
1450 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1453 * If the two entries differ only by their key (-2/2) or
1454 * create_tid (-1/1), and are DATA records, we may have a
1455 * nominal match. We have to calculate the base file
1456 * offset of the data.
1458 if (r <= 2 && r >= -2 && r != 0 &&
1459 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1460 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1461 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1462 int64_t base2 = cursor->iprec->leaf.base.key -
1463 cursor->iprec->leaf.data_len;
1464 if (base1 == base2)
1465 r = 0;
1468 if (r < 0) {
1469 error = hammer_btree_extract(cursor,
1470 HAMMER_CURSOR_GET_LEAF);
1471 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1472 break;
1476 * If the entries match exactly the memory entry is either
1477 * an on-disk directory entry deletion or a bulk data
1478 * overwrite. If it is a directory entry deletion we eat
1479 * both entries.
1481 * For the bulk-data overwrite case it is possible to have
1482 * visibility into both, which simply means the syncer
1483 * hasn't gotten around to doing the delete+insert sequence
1484 * on the B-Tree. Use the memory entry and throw away the
1485 * on-disk entry.
1487 * If the in-memory record is not either of these we
1488 * probably caught the syncer while it was syncing it to
1489 * the media. Since we hold a shared lock on the cursor,
1490 * the in-memory record had better be marked deleted at
1491 * this point.
1493 if (r == 0) {
1494 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1495 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1496 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1497 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1498 goto next_btree;
1500 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1501 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1502 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1504 /* fall through to memory entry */
1505 } else {
1506 panic("hammer_ip_next: duplicate mem/b-tree entry");
1507 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1508 goto next_memory;
1511 /* fall through to the memory entry */
1512 case HAMMER_CURSOR_ATEDISK:
1514 * Only the memory entry is valid.
1516 cursor->leaf = &cursor->iprec->leaf;
1517 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1520 * If the memory entry is an on-disk deletion we should have
1521 * also had found a B-Tree record. If the backend beat us
1522 * to it it would have interlocked the cursor and we should
1523 * have seen the in-memory record marked DELETED_FE.
1525 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1526 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1527 panic("hammer_ip_next: del-on-disk with no b-tree entry");
1529 break;
1530 case HAMMER_CURSOR_ATEMEM:
1532 * Only the disk entry is valid
1534 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1535 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1536 break;
1537 default:
1539 * Neither entry is valid
1541 * XXX error not set properly
1543 cursor->leaf = NULL;
1544 error = ENOENT;
1545 break;
1547 return(error);
1551 * Resolve the cursor->data pointer for the current cursor position in
1552 * a merged iteration.
1555 hammer_ip_resolve_data(hammer_cursor_t cursor)
1557 hammer_record_t record;
1558 int error;
1560 if (hammer_cursor_inmem(cursor)) {
1562 * The data associated with an in-memory record is usually
1563 * kmalloced, but reserve-ahead data records will have an
1564 * on-disk reference.
1566 * NOTE: Reserve-ahead data records must be handled in the
1567 * context of the related high level buffer cache buffer
1568 * to interlock against async writes.
1570 record = cursor->iprec;
1571 cursor->data = record->data;
1572 error = 0;
1573 if (cursor->data == NULL) {
1574 KKASSERT(record->leaf.base.rec_type ==
1575 HAMMER_RECTYPE_DATA);
1576 cursor->data = hammer_bread_ext(cursor->trans->hmp,
1577 record->leaf.data_offset,
1578 record->leaf.data_len,
1579 &error,
1580 &cursor->data_buffer);
1582 } else {
1583 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
1584 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1586 return(error);
1590 * Backend truncation / record replacement - delete records in range.
1592 * Delete all records within the specified range for inode ip. In-memory
1593 * records still associated with the frontend are ignored.
1595 * If truncating is non-zero in-memory records associated with the back-end
1596 * are ignored. If truncating is > 1 we can return EWOULDBLOCK.
1598 * NOTES:
1600 * * An unaligned range will cause new records to be added to cover
1601 * the edge cases. (XXX not implemented yet).
1603 * * Replacement via reservations (see hammer_ip_sync_record_cursor())
1604 * also do not deal with unaligned ranges.
1606 * * ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1608 * * Record keys for regular file data have to be special-cased since
1609 * they indicate the end of the range (key = base + bytes).
1611 * * This function may be asked to delete ridiculously huge ranges, for
1612 * example if someone truncates or removes a 1TB regular file. We
1613 * must be very careful on restarts and we may have to stop w/
1614 * EWOULDBLOCK to avoid blowing out the buffer cache.
1617 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1618 int64_t ran_beg, int64_t ran_end, int truncating)
1620 hammer_transaction_t trans = cursor->trans;
1621 hammer_btree_leaf_elm_t leaf;
1622 int error;
1623 int64_t off;
1624 int64_t tmp64;
1626 #if 0
1627 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1628 #endif
1630 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1631 retry:
1632 hammer_normalize_cursor(cursor);
1633 cursor->key_beg.localization = ip->obj_localization +
1634 HAMMER_LOCALIZE_MISC;
1635 cursor->key_beg.obj_id = ip->obj_id;
1636 cursor->key_beg.create_tid = 0;
1637 cursor->key_beg.delete_tid = 0;
1638 cursor->key_beg.obj_type = 0;
1640 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1641 cursor->key_beg.key = ran_beg;
1642 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1643 } else {
1645 * The key in the B-Tree is (base+bytes), so the first possible
1646 * matching key is ran_beg + 1.
1648 cursor->key_beg.key = ran_beg + 1;
1649 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1652 cursor->key_end = cursor->key_beg;
1653 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1654 cursor->key_end.key = ran_end;
1655 } else {
1656 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1657 if (tmp64 < ran_end)
1658 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1659 else
1660 cursor->key_end.key = ran_end + MAXPHYS + 1;
1663 cursor->asof = ip->obj_asof;
1664 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1665 cursor->flags |= HAMMER_CURSOR_ASOF;
1666 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1667 cursor->flags |= HAMMER_CURSOR_BACKEND;
1668 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
1670 error = hammer_ip_first(cursor);
1673 * Iterate through matching records and mark them as deleted.
1675 while (error == 0) {
1676 leaf = cursor->leaf;
1678 KKASSERT(leaf->base.delete_tid == 0);
1681 * There may be overlap cases for regular file data. Also
1682 * remember the key for a regular file record is (base + len),
1683 * NOT (base).
1685 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
1686 off = leaf->base.key - leaf->data_len;
1688 * Check the left edge case. We currently do not
1689 * split existing records.
1691 if (off < ran_beg) {
1692 panic("hammer left edge case %016llx %d\n",
1693 leaf->base.key, leaf->data_len);
1697 * Check the right edge case. Note that the
1698 * record can be completely out of bounds, which
1699 * terminates the search.
1701 * base->key is exclusive of the right edge while
1702 * ran_end is inclusive of the right edge. The
1703 * (key - data_len) left boundary is inclusive.
1705 * XXX theory-check this test at some point, are
1706 * we missing a + 1 somewhere? Note that ran_end
1707 * could overflow.
1709 if (leaf->base.key - 1 > ran_end) {
1710 if (leaf->base.key - leaf->data_len > ran_end)
1711 break;
1712 panic("hammer right edge case\n");
1714 } else {
1715 off = leaf->base.key;
1719 * Delete the record. When truncating we do not delete
1720 * in-memory (data) records because they represent data
1721 * written after the truncation.
1723 * This will also physically destroy the B-Tree entry and
1724 * data if the retention policy dictates. The function
1725 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1726 * uses to perform a fixup.
1728 if (truncating == 0 || hammer_cursor_ondisk(cursor)) {
1729 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1731 * If we have built up too many meta-buffers we risk
1732 * deadlocking the kernel and must stop. This can
1733 * occur when deleting ridiculously huge files.
1734 * sync_trunc_off is updated so the next cycle does
1735 * not re-iterate records we have already deleted.
1737 * This is only done with formal truncations.
1739 if (truncating > 1 && error == 0 &&
1740 hammer_flusher_meta_limit(ip->hmp)) {
1741 ip->sync_trunc_off = off;
1742 error = EWOULDBLOCK;
1745 if (error)
1746 break;
1747 ran_beg = off; /* for restart */
1748 error = hammer_ip_next(cursor);
1750 if (cursor->node)
1751 hammer_cache_node(&ip->cache[1], cursor->node);
1753 if (error == EDEADLK) {
1754 hammer_done_cursor(cursor);
1755 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1756 if (error == 0)
1757 goto retry;
1759 if (error == ENOENT)
1760 error = 0;
1761 return(error);
1765 * This backend function deletes the specified record on-disk, similar to
1766 * delete_range but for a specific record. Unlike the exact deletions
1767 * used when deleting a directory entry this function uses an ASOF search
1768 * like delete_range.
1770 * This function may be called with ip->obj_asof set for a slave snapshot,
1771 * so don't use it. We always delete non-historical records only.
1773 static int
1774 hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
1775 hammer_btree_leaf_elm_t leaf)
1777 hammer_transaction_t trans = cursor->trans;
1778 int error;
1780 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1781 retry:
1782 hammer_normalize_cursor(cursor);
1783 cursor->key_beg = leaf->base;
1784 cursor->asof = HAMMER_MAX_TID;
1785 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1786 cursor->flags |= HAMMER_CURSOR_ASOF;
1787 cursor->flags |= HAMMER_CURSOR_BACKEND;
1788 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1790 error = hammer_btree_lookup(cursor);
1791 if (error == 0) {
1792 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1794 if (error == EDEADLK) {
1795 hammer_done_cursor(cursor);
1796 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1797 if (error == 0)
1798 goto retry;
1800 return(error);
1804 * This function deletes remaining auxillary records when an inode is
1805 * being deleted. This function explicitly does not delete the
1806 * inode record, directory entry, data, or db records. Those must be
1807 * properly disposed of prior to this call.
1810 hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip, int *countp)
1812 hammer_transaction_t trans = cursor->trans;
1813 hammer_btree_leaf_elm_t leaf;
1814 int error;
1816 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1817 retry:
1818 hammer_normalize_cursor(cursor);
1819 cursor->key_beg.localization = ip->obj_localization +
1820 HAMMER_LOCALIZE_MISC;
1821 cursor->key_beg.obj_id = ip->obj_id;
1822 cursor->key_beg.create_tid = 0;
1823 cursor->key_beg.delete_tid = 0;
1824 cursor->key_beg.obj_type = 0;
1825 cursor->key_beg.rec_type = HAMMER_RECTYPE_CLEAN_START;
1826 cursor->key_beg.key = HAMMER_MIN_KEY;
1828 cursor->key_end = cursor->key_beg;
1829 cursor->key_end.rec_type = HAMMER_RECTYPE_MAX;
1830 cursor->key_end.key = HAMMER_MAX_KEY;
1832 cursor->asof = ip->obj_asof;
1833 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1834 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1835 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1836 cursor->flags |= HAMMER_CURSOR_BACKEND;
1838 error = hammer_ip_first(cursor);
1841 * Iterate through matching records and mark them as deleted.
1843 while (error == 0) {
1844 leaf = cursor->leaf;
1846 KKASSERT(leaf->base.delete_tid == 0);
1849 * Mark the record and B-Tree entry as deleted. This will
1850 * also physically delete the B-Tree entry, record, and
1851 * data if the retention policy dictates. The function
1852 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1853 * uses to perform a fixup.
1855 * Directory entries (and delete-on-disk directory entries)
1856 * must be synced and cannot be deleted.
1858 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1859 ++*countp;
1860 if (error)
1861 break;
1862 error = hammer_ip_next(cursor);
1864 if (cursor->node)
1865 hammer_cache_node(&ip->cache[1], cursor->node);
1866 if (error == EDEADLK) {
1867 hammer_done_cursor(cursor);
1868 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1869 if (error == 0)
1870 goto retry;
1872 if (error == ENOENT)
1873 error = 0;
1874 return(error);
1878 * Delete the record at the current cursor. On success the cursor will
1879 * be positioned appropriately for an iteration but may no longer be at
1880 * a leaf node.
1882 * This routine is only called from the backend.
1884 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1885 * cursor and retry.
1888 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
1889 hammer_tid_t tid)
1891 hammer_off_t zone2_offset;
1892 hammer_record_t iprec;
1893 hammer_btree_elm_t elm;
1894 hammer_mount_t hmp;
1895 int error;
1896 int dodelete;
1898 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1899 KKASSERT(tid != 0);
1900 hmp = cursor->node->hmp;
1903 * In-memory (unsynchronized) records can simply be freed. This
1904 * only occurs in range iterations since all other records are
1905 * individually synchronized. Thus there should be no confusion with
1906 * the interlock.
1908 * An in-memory record may be deleted before being committed to disk,
1909 * but could have been accessed in the mean time. The backing store
1910 * may never been marked allocated and so hammer_blockmap_free() may
1911 * never get called on it. Because of this we have to make sure that
1912 * we've gotten rid of any related hammer_buffer or buffer cache
1913 * buffer.
1915 if (hammer_cursor_inmem(cursor)) {
1916 iprec = cursor->iprec;
1917 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
1918 iprec->flags |= HAMMER_RECF_DELETED_FE;
1919 iprec->flags |= HAMMER_RECF_DELETED_BE;
1921 if (iprec->leaf.data_offset && iprec->leaf.data_len) {
1922 zone2_offset = hammer_blockmap_lookup(hmp, iprec->leaf.data_offset, &error);
1923 KKASSERT(error == 0);
1924 hammer_del_buffers(hmp,
1925 iprec->leaf.data_offset,
1926 zone2_offset,
1927 iprec->leaf.data_len);
1929 return(0);
1933 * On-disk records are marked as deleted by updating their delete_tid.
1934 * This does not effect their position in the B-Tree (which is based
1935 * on their create_tid).
1937 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1938 elm = NULL;
1941 * If we were mounted with the nohistory option, we physically
1942 * delete the record.
1944 dodelete = hammer_nohistory(ip);
1946 if (error == 0) {
1947 error = hammer_cursor_upgrade(cursor);
1948 if (error == 0) {
1949 elm = &cursor->node->ondisk->elms[cursor->index];
1950 hammer_modify_node(cursor->trans, cursor->node,
1951 elm, sizeof(*elm));
1952 elm->leaf.base.delete_tid = tid;
1953 elm->leaf.delete_ts = cursor->trans->time32;
1954 hammer_modify_node_done(cursor->node);
1957 * An on-disk record cannot have the same delete_tid
1958 * as its create_tid. In a chain of record updates
1959 * this could result in a duplicate record.
1961 KKASSERT(elm->leaf.base.delete_tid != elm->leaf.base.create_tid);
1965 if (error == 0 && dodelete) {
1966 error = hammer_delete_at_cursor(cursor, NULL);
1967 if (error) {
1968 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1969 error = 0;
1972 return(error);
1976 hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes)
1978 hammer_btree_elm_t elm;
1979 hammer_off_t data_offset;
1980 int32_t data_len;
1981 u_int16_t rec_type;
1982 int error;
1984 elm = &cursor->node->ondisk->elms[cursor->index];
1985 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
1987 data_offset = elm->leaf.data_offset;
1988 data_len = elm->leaf.data_len;
1989 rec_type = elm->leaf.base.rec_type;
1991 error = hammer_btree_delete(cursor);
1992 if (error == 0) {
1994 * This forces a fixup for the iteration because
1995 * the cursor is now either sitting at the 'next'
1996 * element or sitting at the end of a leaf.
1998 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1999 cursor->flags |= HAMMER_CURSOR_DELBTREE;
2000 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2003 if (error == 0) {
2004 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
2005 case HAMMER_ZONE_LARGE_DATA:
2006 case HAMMER_ZONE_SMALL_DATA:
2007 case HAMMER_ZONE_META:
2008 hammer_blockmap_free(cursor->trans,
2009 data_offset, data_len);
2010 break;
2011 default:
2012 break;
2015 return (error);
2019 * Determine whether we can remove a directory. This routine checks whether
2020 * a directory is empty or not and enforces flush connectivity.
2022 * Flush connectivity requires that we block if the target directory is
2023 * currently flushing, otherwise it may not end up in the same flush group.
2025 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
2028 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
2030 struct hammer_cursor cursor;
2031 int error;
2034 * Check directory empty
2036 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2038 cursor.key_beg.localization = ip->obj_localization +
2039 HAMMER_LOCALIZE_MISC;
2040 cursor.key_beg.obj_id = ip->obj_id;
2041 cursor.key_beg.create_tid = 0;
2042 cursor.key_beg.delete_tid = 0;
2043 cursor.key_beg.obj_type = 0;
2044 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
2045 cursor.key_beg.key = HAMMER_MIN_KEY;
2047 cursor.key_end = cursor.key_beg;
2048 cursor.key_end.rec_type = 0xFFFF;
2049 cursor.key_end.key = HAMMER_MAX_KEY;
2051 cursor.asof = ip->obj_asof;
2052 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2054 error = hammer_ip_first(&cursor);
2055 if (error == ENOENT)
2056 error = 0;
2057 else if (error == 0)
2058 error = ENOTEMPTY;
2059 hammer_done_cursor(&cursor);
2060 return(error);