144bb29ff35961439a70e1f34bdaf5e4423df5c1
[dragonfly.git] / sys / vfs / hammer / hammer_object.c
blob144bb29ff35961439a70e1f34bdaf5e4423df5c1
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.90.2.3 2008/08/02 21:24:28 dillon Exp $
37 #include "hammer.h"
39 static int hammer_mem_lookup(hammer_cursor_t cursor);
40 static int hammer_mem_first(hammer_cursor_t cursor);
41 static int hammer_frontend_trunc_callback(hammer_record_t record,
42 void *data __unused);
43 static int hammer_record_needs_overwrite_delete(hammer_record_t record);
44 static int hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
45 hammer_btree_leaf_elm_t leaf);
47 struct rec_trunc_info {
48 u_int16_t rec_type;
49 int64_t trunc_off;
53 * Red-black tree support. Comparison code for insertion.
55 static int
56 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
58 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
59 return(-1);
60 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
61 return(1);
63 if (rec1->leaf.base.key < rec2->leaf.base.key)
64 return(-1);
65 if (rec1->leaf.base.key > rec2->leaf.base.key)
66 return(1);
69 * Never match against an item deleted by the front-end.
71 * rec1 is greater then rec2 if rec1 is marked deleted.
72 * rec1 is less then rec2 if rec2 is marked deleted.
74 * Multiple deleted records may be present, do not return 0
75 * if both are marked deleted.
77 if (rec1->flags & HAMMER_RECF_DELETED_FE)
78 return(1);
79 if (rec2->flags & HAMMER_RECF_DELETED_FE)
80 return(-1);
82 return(0);
86 * Basic record comparison code similar to hammer_btree_cmp().
88 static int
89 hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
91 if (elm->rec_type < rec->leaf.base.rec_type)
92 return(-3);
93 if (elm->rec_type > rec->leaf.base.rec_type)
94 return(3);
96 if (elm->key < rec->leaf.base.key)
97 return(-2);
98 if (elm->key > rec->leaf.base.key)
99 return(2);
102 * Never match against an item deleted by the front-end.
103 * elm is less then rec if rec is marked deleted.
105 if (rec->flags & HAMMER_RECF_DELETED_FE)
106 return(-1);
107 return(0);
111 * Special LOOKUP_INFO to locate an overlapping record. This used by
112 * the reservation code to implement small-block records (whos keys will
113 * be different depending on data_len, when representing the same base
114 * offset).
116 * NOTE: The base file offset of a data record is (key - data_len), not (key).
118 static int
119 hammer_rec_overlap_compare(hammer_btree_leaf_elm_t leaf, hammer_record_t rec)
121 if (leaf->base.rec_type < rec->leaf.base.rec_type)
122 return(-3);
123 if (leaf->base.rec_type > rec->leaf.base.rec_type)
124 return(3);
127 * Overlap compare
129 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
130 /* leaf_end <= rec_beg */
131 if (leaf->base.key <= rec->leaf.base.key - rec->leaf.data_len)
132 return(-2);
133 /* leaf_beg >= rec_end */
134 if (leaf->base.key - leaf->data_len >= rec->leaf.base.key)
135 return(2);
136 } else {
137 if (leaf->base.key < rec->leaf.base.key)
138 return(-2);
139 if (leaf->base.key > rec->leaf.base.key)
140 return(2);
144 * Never match against an item deleted by the front-end.
145 * leaf is less then rec if rec is marked deleted.
147 * We must still return the proper code for the scan to continue
148 * along the correct branches.
150 if (rec->flags & HAMMER_RECF_DELETED_FE) {
151 if (leaf->base.key < rec->leaf.base.key)
152 return(-2);
153 if (leaf->base.key > rec->leaf.base.key)
154 return(2);
155 return(-1);
157 return(0);
161 * RB_SCAN comparison code for hammer_mem_first(). The argument order
162 * is reversed so the comparison result has to be negated. key_beg and
163 * key_end are both range-inclusive.
165 * Localized deletions are not cached in-memory.
167 static
169 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
171 hammer_cursor_t cursor = data;
172 int r;
174 r = hammer_rec_cmp(&cursor->key_beg, rec);
175 if (r > 1)
176 return(-1);
177 r = hammer_rec_cmp(&cursor->key_end, rec);
178 if (r < -1)
179 return(1);
180 return(0);
184 * This compare function is used when simply looking up key_beg.
186 static
188 hammer_rec_find_cmp(hammer_record_t rec, void *data)
190 hammer_cursor_t cursor = data;
191 int r;
193 r = hammer_rec_cmp(&cursor->key_beg, rec);
194 if (r > 1)
195 return(-1);
196 if (r < -1)
197 return(1);
198 return(0);
202 * Locate blocks within the truncation range. Partial blocks do not count.
204 static
206 hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
208 struct rec_trunc_info *info = data;
210 if (rec->leaf.base.rec_type < info->rec_type)
211 return(-1);
212 if (rec->leaf.base.rec_type > info->rec_type)
213 return(1);
215 switch(rec->leaf.base.rec_type) {
216 case HAMMER_RECTYPE_DB:
218 * DB record key is not beyond the truncation point, retain.
220 if (rec->leaf.base.key < info->trunc_off)
221 return(-1);
222 break;
223 case HAMMER_RECTYPE_DATA:
225 * DATA record offset start is not beyond the truncation point,
226 * retain.
228 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
229 return(-1);
230 break;
231 default:
232 panic("hammer_rec_trunc_cmp: unexpected record type");
236 * The record start is >= the truncation point, return match,
237 * the record should be destroyed.
239 return(0);
242 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
243 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
244 hammer_rec_overlap_compare, hammer_btree_leaf_elm_t);
247 * Allocate a record for the caller to finish filling in. The record is
248 * returned referenced.
250 hammer_record_t
251 hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
253 hammer_record_t record;
255 ++hammer_count_records;
256 record = kmalloc(sizeof(*record), M_HAMMER,
257 M_WAITOK | M_ZERO | M_USE_RESERVE);
258 record->flush_state = HAMMER_FST_IDLE;
259 record->ip = ip;
260 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
261 record->leaf.data_len = data_len;
262 hammer_ref(&record->lock);
264 if (data_len) {
265 record->data = kmalloc(data_len, M_HAMMER, M_WAITOK | M_ZERO);
266 record->flags |= HAMMER_RECF_ALLOCDATA;
267 ++hammer_count_record_datas;
270 return (record);
273 void
274 hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
276 while (record->flush_state == HAMMER_FST_FLUSH) {
277 record->flags |= HAMMER_RECF_WANTED;
278 tsleep(record, 0, ident, 0);
283 * Called from the backend, hammer_inode.c, after a record has been
284 * flushed to disk. The record has been exclusively locked by the
285 * caller and interlocked with BE.
287 * We clean up the state, unlock, and release the record (the record
288 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
290 void
291 hammer_flush_record_done(hammer_record_t record, int error)
293 hammer_inode_t target_ip;
295 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
296 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
298 if (error) {
300 * An error occured, the backend was unable to sync the
301 * record to its media. Leave the record intact.
303 hammer_critical_error(record->ip->hmp, record->ip, error,
304 "while flushing record");
307 --record->flush_group->refs;
308 record->flush_group = NULL;
310 if (record->flags & HAMMER_RECF_DELETED_BE) {
311 if ((target_ip = record->target_ip) != NULL) {
312 TAILQ_REMOVE(&target_ip->target_list, record,
313 target_entry);
314 record->target_ip = NULL;
315 hammer_test_inode(target_ip);
317 record->flush_state = HAMMER_FST_IDLE;
318 } else {
319 if (record->target_ip) {
320 record->flush_state = HAMMER_FST_SETUP;
321 hammer_test_inode(record->ip);
322 hammer_test_inode(record->target_ip);
323 } else {
324 record->flush_state = HAMMER_FST_IDLE;
327 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
328 if (record->flags & HAMMER_RECF_WANTED) {
329 record->flags &= ~HAMMER_RECF_WANTED;
330 wakeup(record);
332 hammer_rel_mem_record(record);
336 * Release a memory record. Records marked for deletion are immediately
337 * removed from the RB-Tree but otherwise left intact until the last ref
338 * goes away.
340 void
341 hammer_rel_mem_record(struct hammer_record *record)
343 hammer_mount_t hmp;
344 hammer_reserve_t resv;
345 hammer_inode_t ip;
346 hammer_inode_t target_ip;
348 hammer_unref(&record->lock);
350 if (record->lock.refs == 0) {
352 * Upon release of the last reference wakeup any waiters.
353 * The record structure may get destroyed so callers will
354 * loop up and do a relookup.
356 * WARNING! Record must be removed from RB-TREE before we
357 * might possibly block. hammer_test_inode() can block!
359 ip = record->ip;
360 hmp = ip->hmp;
363 * Upon release of the last reference a record marked deleted
364 * is destroyed.
366 if (record->flags & HAMMER_RECF_DELETED_FE) {
367 KKASSERT(ip->lock.refs > 0);
368 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
371 * target_ip may have zero refs, we have to ref it
372 * to prevent it from being ripped out from under
373 * us.
375 if ((target_ip = record->target_ip) != NULL) {
376 TAILQ_REMOVE(&target_ip->target_list,
377 record, target_entry);
378 record->target_ip = NULL;
379 hammer_ref(&target_ip->lock);
382 if (record->flags & HAMMER_RECF_ONRBTREE) {
383 RB_REMOVE(hammer_rec_rb_tree,
384 &record->ip->rec_tree,
385 record);
386 KKASSERT(ip->rsv_recs > 0);
387 --hmp->rsv_recs;
388 --ip->rsv_recs;
389 hmp->rsv_databytes -= record->leaf.data_len;
390 record->flags &= ~HAMMER_RECF_ONRBTREE;
392 if (RB_EMPTY(&record->ip->rec_tree)) {
393 record->ip->flags &= ~HAMMER_INODE_XDIRTY;
394 record->ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
395 hammer_test_inode(record->ip);
400 * We must wait for any direct-IO to complete before
401 * we can destroy the record because the bio may
402 * have a reference to it.
404 if (record->flags &
405 (HAMMER_RECF_DIRECT_IO | HAMMER_RECF_DIRECT_INVAL)) {
406 hammer_io_direct_wait(record);
411 * Do this test after removing record from the B-Tree.
413 if (target_ip) {
414 hammer_test_inode(target_ip);
415 hammer_rel_inode(target_ip, 0);
418 if (record->flags & HAMMER_RECF_ALLOCDATA) {
419 --hammer_count_record_datas;
420 kfree(record->data, M_HAMMER);
421 record->flags &= ~HAMMER_RECF_ALLOCDATA;
425 * Release the reservation. If the record was not
426 * committed return the reservation before
427 * releasing it.
429 if ((resv = record->resv) != NULL) {
430 if ((record->flags & HAMMER_RECF_COMMITTED) == 0) {
431 hammer_blockmap_reserve_undo(
432 resv,
433 record->leaf.data_offset,
434 record->leaf.data_len);
436 hammer_blockmap_reserve_complete(hmp, resv);
437 record->resv = NULL;
439 record->data = NULL;
440 --hammer_count_records;
441 kfree(record, M_HAMMER);
447 * Record visibility depends on whether the record is being accessed by
448 * the backend or the frontend.
450 * Return non-zero if the record is visible, zero if it isn't or if it is
451 * deleted.
453 static __inline
455 hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
457 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
458 if (record->flags & HAMMER_RECF_DELETED_BE)
459 return(0);
460 } else {
461 if (record->flags & HAMMER_RECF_DELETED_FE)
462 return(0);
464 return(1);
468 * This callback is used as part of the RB_SCAN function for in-memory
469 * records. We terminate it (return -1) as soon as we get a match.
471 * This routine is used by frontend code.
473 * The primary compare code does not account for ASOF lookups. This
474 * code handles that case as well as a few others.
476 static
478 hammer_rec_scan_callback(hammer_record_t rec, void *data)
480 hammer_cursor_t cursor = data;
483 * We terminate on success, so this should be NULL on entry.
485 KKASSERT(cursor->iprec == NULL);
488 * Skip if the record was marked deleted.
490 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
491 return(0);
494 * Skip if not visible due to our as-of TID
496 if (cursor->flags & HAMMER_CURSOR_ASOF) {
497 if (cursor->asof < rec->leaf.base.create_tid)
498 return(0);
499 if (rec->leaf.base.delete_tid &&
500 cursor->asof >= rec->leaf.base.delete_tid) {
501 return(0);
506 * ref the record. The record is protected from backend B-Tree
507 * interactions by virtue of the cursor's IP lock.
509 hammer_ref(&rec->lock);
512 * The record may have been deleted while we were blocked.
514 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
515 hammer_rel_mem_record(rec);
516 return(0);
520 * Set the matching record and stop the scan.
522 cursor->iprec = rec;
523 return(-1);
528 * Lookup an in-memory record given the key specified in the cursor. Works
529 * just like hammer_btree_lookup() but operates on an inode's in-memory
530 * record list.
532 * The lookup must fail if the record is marked for deferred deletion.
534 static
536 hammer_mem_lookup(hammer_cursor_t cursor)
538 int error;
540 KKASSERT(cursor->ip);
541 if (cursor->iprec) {
542 hammer_rel_mem_record(cursor->iprec);
543 cursor->iprec = NULL;
545 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
546 hammer_rec_scan_callback, cursor);
548 if (cursor->iprec == NULL)
549 error = ENOENT;
550 else
551 error = 0;
552 return(error);
556 * hammer_mem_first() - locate the first in-memory record matching the
557 * cursor within the bounds of the key range.
559 static
561 hammer_mem_first(hammer_cursor_t cursor)
563 hammer_inode_t ip;
565 ip = cursor->ip;
566 KKASSERT(ip != NULL);
568 if (cursor->iprec) {
569 hammer_rel_mem_record(cursor->iprec);
570 cursor->iprec = NULL;
573 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
574 hammer_rec_scan_callback, cursor);
577 * Adjust scan.node and keep it linked into the RB-tree so we can
578 * hold the cursor through third party modifications of the RB-tree.
580 if (cursor->iprec)
581 return(0);
582 return(ENOENT);
585 /************************************************************************
586 * HAMMER IN-MEMORY RECORD FUNCTIONS *
587 ************************************************************************
589 * These functions manipulate in-memory records. Such records typically
590 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
594 * Add a directory entry (dip,ncp) which references inode (ip).
596 * Note that the low 32 bits of the namekey are set temporarily to create
597 * a unique in-memory record, and may be modified a second time when the
598 * record is synchronized to disk. In particular, the low 32 bits cannot be
599 * all 0's when synching to disk, which is not handled here.
601 * NOTE: bytes does not include any terminating \0 on name, and name might
602 * not be terminated.
605 hammer_ip_add_directory(struct hammer_transaction *trans,
606 struct hammer_inode *dip, const char *name, int bytes,
607 struct hammer_inode *ip)
609 struct hammer_cursor cursor;
610 hammer_record_t record;
611 int error;
612 int count;
613 u_int32_t iterator;
615 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
616 if (++trans->hmp->namekey_iterator == 0)
617 ++trans->hmp->namekey_iterator;
619 record->type = HAMMER_MEM_RECORD_ADD;
620 record->leaf.base.localization = dip->obj_localization +
621 HAMMER_LOCALIZE_MISC;
622 record->leaf.base.obj_id = dip->obj_id;
623 record->leaf.base.key = hammer_directory_namekey(name, bytes);
624 record->leaf.base.key += trans->hmp->namekey_iterator;
625 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
626 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
627 record->data->entry.obj_id = ip->obj_id;
628 record->data->entry.localization = ip->obj_localization;
629 bcopy(name, record->data->entry.name, bytes);
631 ++ip->ino_data.nlinks;
632 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
635 * Find an unused namekey. Both the in-memory record tree and
636 * the B-Tree are checked. Exact matches also match create_tid
637 * so use an ASOF search to (mostly) ignore it.
639 * delete-visibility is set so pending deletions do not give us
640 * a false-negative on our ability to use an iterator.
642 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
643 cursor.key_beg = record->leaf.base;
644 cursor.flags |= HAMMER_CURSOR_ASOF;
645 cursor.flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
646 cursor.asof = ip->obj_asof;
648 count = 0;
649 while (hammer_ip_lookup(&cursor) == 0) {
650 iterator = (u_int32_t)record->leaf.base.key + 1;
651 if (iterator == 0)
652 iterator = 1;
653 record->leaf.base.key &= ~0xFFFFFFFFLL;
654 record->leaf.base.key |= iterator;
655 cursor.key_beg.key = record->leaf.base.key;
656 if (++count == 1000000000) {
657 hammer_rel_mem_record(record);
658 error = ENOSPC;
659 goto failed;
664 * The target inode and the directory entry are bound together.
666 record->target_ip = ip;
667 record->flush_state = HAMMER_FST_SETUP;
668 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
671 * The inode now has a dependancy and must be taken out of the idle
672 * state. An inode not in an idle state is given an extra reference.
674 * When transitioning to a SETUP state flag for an automatic reflush
675 * when the dependancies are disposed of if someone is waiting on
676 * the inode.
678 if (ip->flush_state == HAMMER_FST_IDLE) {
679 hammer_ref(&ip->lock);
680 ip->flush_state = HAMMER_FST_SETUP;
681 if (ip->flags & HAMMER_INODE_FLUSHW)
682 ip->flags |= HAMMER_INODE_REFLUSH;
684 error = hammer_mem_add(record);
685 if (error == 0) {
686 dip->ino_data.mtime = trans->time;
687 hammer_modify_inode(dip, HAMMER_INODE_MTIME);
689 failed:
690 hammer_done_cursor(&cursor);
691 return(error);
695 * Delete the directory entry and update the inode link count. The
696 * cursor must be seeked to the directory entry record being deleted.
698 * The related inode should be share-locked by the caller. The caller is
699 * on the frontend.
701 * This function can return EDEADLK requiring the caller to terminate
702 * the cursor, any locks, wait on the returned record, and retry.
705 hammer_ip_del_directory(struct hammer_transaction *trans,
706 hammer_cursor_t cursor, struct hammer_inode *dip,
707 struct hammer_inode *ip)
709 hammer_record_t record;
710 int error;
712 if (hammer_cursor_inmem(cursor)) {
714 * In-memory (unsynchronized) records can simply be freed.
715 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
716 * by the backend, we must still avoid races against the
717 * backend potentially syncing the record to the media.
719 * We cannot call hammer_ip_delete_record(), that routine may
720 * only be called from the backend.
722 record = cursor->iprec;
723 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
724 KKASSERT(cursor->deadlk_rec == NULL);
725 hammer_ref(&record->lock);
726 cursor->deadlk_rec = record;
727 error = EDEADLK;
728 } else {
729 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
730 record->flags |= HAMMER_RECF_DELETED_FE;
731 error = 0;
733 } else {
735 * If the record is on-disk we have to queue the deletion by
736 * the record's key. This also causes lookups to skip the
737 * record.
739 KKASSERT(dip->flags &
740 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
741 record = hammer_alloc_mem_record(dip, 0);
742 record->type = HAMMER_MEM_RECORD_DEL;
743 record->leaf.base = cursor->leaf->base;
745 record->target_ip = ip;
746 record->flush_state = HAMMER_FST_SETUP;
747 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
750 * The inode now has a dependancy and must be taken out of
751 * the idle state. An inode not in an idle state is given
752 * an extra reference.
754 * When transitioning to a SETUP state flag for an automatic
755 * reflush when the dependancies are disposed of if someone
756 * is waiting on the inode.
758 if (ip->flush_state == HAMMER_FST_IDLE) {
759 hammer_ref(&ip->lock);
760 ip->flush_state = HAMMER_FST_SETUP;
761 if (ip->flags & HAMMER_INODE_FLUSHW)
762 ip->flags |= HAMMER_INODE_REFLUSH;
765 error = hammer_mem_add(record);
769 * One less link. The file may still be open in the OS even after
770 * all links have gone away.
772 * We have to terminate the cursor before syncing the inode to
773 * avoid deadlocking against ourselves. XXX this may no longer
774 * be true.
776 * If nlinks drops to zero and the vnode is inactive (or there is
777 * no vnode), call hammer_inode_unloadable_check() to zonk the
778 * inode. If we don't do this here the inode will not be destroyed
779 * on-media until we unmount.
781 if (error == 0) {
782 --ip->ino_data.nlinks;
783 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
784 if (ip->ino_data.nlinks == 0 &&
785 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
786 hammer_done_cursor(cursor);
787 hammer_inode_unloadable_check(ip, 1);
788 hammer_flush_inode(ip, 0);
790 dip->ino_data.mtime = trans->time;
791 hammer_modify_inode(dip, HAMMER_INODE_MTIME);
794 return(error);
798 * Add a record to an inode.
800 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
801 * initialize the following additional fields:
803 * The related inode should be share-locked by the caller. The caller is
804 * on the frontend.
806 * record->rec.entry.base.base.key
807 * record->rec.entry.base.base.rec_type
808 * record->rec.entry.base.base.data_len
809 * record->data (a copy will be kmalloc'd if it cannot be embedded)
812 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
814 hammer_inode_t ip = record->ip;
815 int error;
817 KKASSERT(record->leaf.base.localization != 0);
818 record->leaf.base.obj_id = ip->obj_id;
819 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
820 error = hammer_mem_add(record);
821 return(error);
825 * Locate a bulk record in-memory. Bulk records allow disk space to be
826 * reserved so the front-end can flush large data writes without having
827 * to queue the BIO to the flusher. Only the related record gets queued
828 * to the flusher.
830 static hammer_record_t
831 hammer_ip_get_bulk(hammer_inode_t ip, off_t file_offset, int bytes)
833 hammer_record_t record;
834 struct hammer_btree_leaf_elm leaf;
836 bzero(&leaf, sizeof(leaf));
837 leaf.base.obj_id = ip->obj_id;
838 leaf.base.key = file_offset + bytes;
839 leaf.base.create_tid = 0;
840 leaf.base.delete_tid = 0;
841 leaf.base.rec_type = HAMMER_RECTYPE_DATA;
842 leaf.base.obj_type = 0; /* unused */
843 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD; /* unused */
844 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_MISC;
845 leaf.data_len = bytes;
847 record = hammer_rec_rb_tree_RB_LOOKUP_INFO(&ip->rec_tree, &leaf);
848 if (record)
849 hammer_ref(&record->lock);
850 return(record);
854 * Reserve blockmap space placemarked with an in-memory record.
856 * This routine is called by the frontend in order to be able to directly
857 * flush a buffer cache buffer. The frontend has locked the related buffer
858 * cache buffers and we should be able to manipulate any overlapping
859 * in-memory records.
861 * The caller is responsible for adding the returned record.
863 hammer_record_t
864 hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
865 int *errorp)
867 hammer_record_t record;
868 hammer_record_t conflict;
869 int zone;
872 * Deal with conflicting in-memory records. We cannot have multiple
873 * in-memory records for the same offset without seriously confusing
874 * the backend, including but not limited to the backend issuing
875 * delete-create-delete sequences and asserting on the delete_tid
876 * being the same as the create_tid.
878 * If we encounter a record with the backend interlock set we cannot
879 * immediately delete it without confusing the backend.
881 while ((conflict = hammer_ip_get_bulk(ip, file_offset, bytes)) !=NULL) {
882 if (conflict->flags & HAMMER_RECF_INTERLOCK_BE) {
883 conflict->flags |= HAMMER_RECF_WANTED;
884 tsleep(conflict, 0, "hmrrc3", 0);
885 } else {
886 conflict->flags |= HAMMER_RECF_DELETED_FE;
888 hammer_rel_mem_record(conflict);
892 * Create a record to cover the direct write. This is called with
893 * the related BIO locked so there should be no possible conflict.
895 * The backend is responsible for finalizing the space reserved in
896 * this record.
898 * XXX bytes not aligned, depend on the reservation code to
899 * align the reservation.
901 record = hammer_alloc_mem_record(ip, 0);
902 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
903 HAMMER_ZONE_SMALL_DATA_INDEX;
904 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
905 &record->leaf.data_offset,
906 errorp);
907 if (record->resv == NULL) {
908 kprintf("hammer_ip_add_bulk: reservation failed\n");
909 hammer_rel_mem_record(record);
910 return(NULL);
912 record->type = HAMMER_MEM_RECORD_DATA;
913 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
914 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
915 record->leaf.base.obj_id = ip->obj_id;
916 record->leaf.base.key = file_offset + bytes;
917 record->leaf.base.localization = ip->obj_localization +
918 HAMMER_LOCALIZE_MISC;
919 record->leaf.data_len = bytes;
920 hammer_crc_set_leaf(data, &record->leaf);
921 KKASSERT(*errorp == 0);
922 return(record);
926 * Frontend truncation code. Scan in-memory records only. On-disk records
927 * and records in a flushing state are handled by the backend. The vnops
928 * setattr code will handle the block containing the truncation point.
930 * Partial blocks are not deleted.
933 hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
935 struct rec_trunc_info info;
937 switch(ip->ino_data.obj_type) {
938 case HAMMER_OBJTYPE_REGFILE:
939 info.rec_type = HAMMER_RECTYPE_DATA;
940 break;
941 case HAMMER_OBJTYPE_DBFILE:
942 info.rec_type = HAMMER_RECTYPE_DB;
943 break;
944 default:
945 return(EINVAL);
947 info.trunc_off = file_size;
948 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
949 hammer_frontend_trunc_callback, &info);
950 return(0);
953 static int
954 hammer_frontend_trunc_callback(hammer_record_t record, void *data __unused)
956 if (record->flags & HAMMER_RECF_DELETED_FE)
957 return(0);
958 if (record->flush_state == HAMMER_FST_FLUSH)
959 return(0);
960 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
961 hammer_ref(&record->lock);
962 record->flags |= HAMMER_RECF_DELETED_FE;
963 hammer_rel_mem_record(record);
964 return(0);
968 * Return 1 if the caller must check for and delete existing records
969 * before writing out a new data record.
971 * Return 0 if the caller can just insert the record into the B-Tree without
972 * checking.
974 static int
975 hammer_record_needs_overwrite_delete(hammer_record_t record)
977 hammer_inode_t ip = record->ip;
978 int64_t file_offset;
979 int r;
981 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
982 file_offset = record->leaf.base.key;
983 else
984 file_offset = record->leaf.base.key - record->leaf.data_len;
985 r = (file_offset < ip->save_trunc_off);
986 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
987 if (ip->save_trunc_off <= record->leaf.base.key)
988 ip->save_trunc_off = record->leaf.base.key + 1;
989 } else {
990 if (ip->save_trunc_off < record->leaf.base.key)
991 ip->save_trunc_off = record->leaf.base.key;
993 return(r);
997 * Backend code. Sync a record to the media.
1000 hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
1002 hammer_transaction_t trans = cursor->trans;
1003 int64_t file_offset;
1004 int bytes;
1005 void *bdata;
1006 int error;
1007 int doprop;
1009 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1010 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
1011 KKASSERT(record->leaf.base.localization != 0);
1014 * Any direct-write related to the record must complete before we
1015 * can sync the record to the on-disk media.
1017 if (record->flags & (HAMMER_RECF_DIRECT_IO | HAMMER_RECF_DIRECT_INVAL))
1018 hammer_io_direct_wait(record);
1021 * If this is a bulk-data record placemarker there may be an existing
1022 * record on-disk, indicating a data overwrite. If there is the
1023 * on-disk record must be deleted before we can insert our new record.
1025 * We've synthesized this record and do not know what the create_tid
1026 * on-disk is, nor how much data it represents.
1028 * Keep in mind that (key) for data records is (base_offset + len),
1029 * not (base_offset). Also, we only want to get rid of on-disk
1030 * records since we are trying to sync our in-memory record, call
1031 * hammer_ip_delete_range() with truncating set to 1 to make sure
1032 * it skips in-memory records.
1034 * It is ok for the lookup to return ENOENT.
1036 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1037 * to call hammer_ip_delete_range() or not. This also means we must
1038 * update sync_trunc_off() as we write.
1040 if (record->type == HAMMER_MEM_RECORD_DATA &&
1041 hammer_record_needs_overwrite_delete(record)) {
1042 file_offset = record->leaf.base.key - record->leaf.data_len;
1043 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1044 ~HAMMER_BUFMASK;
1045 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
1046 error = hammer_ip_delete_range(
1047 cursor, record->ip,
1048 file_offset, file_offset + bytes - 1,
1050 if (error && error != ENOENT)
1051 goto done;
1055 * If this is a general record there may be an on-disk version
1056 * that must be deleted before we can insert the new record.
1058 if (record->type == HAMMER_MEM_RECORD_GENERAL) {
1059 error = hammer_delete_general(cursor, record->ip,
1060 &record->leaf);
1061 if (error && error != ENOENT)
1062 goto done;
1066 * Setup the cursor.
1068 hammer_normalize_cursor(cursor);
1069 cursor->key_beg = record->leaf.base;
1070 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1071 cursor->flags |= HAMMER_CURSOR_BACKEND;
1072 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1075 * Records can wind up on-media before the inode itself is on-media.
1076 * Flag the case.
1078 record->ip->flags |= HAMMER_INODE_DONDISK;
1081 * If we are deleting a directory entry an exact match must be
1082 * found on-disk.
1084 if (record->type == HAMMER_MEM_RECORD_DEL) {
1085 error = hammer_btree_lookup(cursor);
1086 if (error == 0) {
1087 /* XXX iprec? */
1088 error = hammer_ip_delete_record(cursor, record->ip,
1089 trans->tid);
1090 if (error == 0) {
1091 record->flags |= HAMMER_RECF_DELETED_FE;
1092 record->flags |= HAMMER_RECF_DELETED_BE;
1093 record->flags |= HAMMER_RECF_COMMITTED;
1096 goto done;
1100 * We are inserting.
1102 * Issue a lookup to position the cursor and locate the cluster. The
1103 * target key should not exist. If we are creating a directory entry
1104 * we may have to iterate the low 32 bits of the key to find an unused
1105 * key.
1107 hammer_sync_lock_sh(trans);
1108 cursor->flags |= HAMMER_CURSOR_INSERT;
1109 error = hammer_btree_lookup(cursor);
1110 if (hammer_debug_inode)
1111 kprintf("DOINSERT LOOKUP %d\n", error);
1112 if (error == 0) {
1113 kprintf("hammer_ip_sync_record: duplicate rec "
1114 "at (%016llx)\n", record->leaf.base.key);
1115 Debugger("duplicate record1");
1116 error = EIO;
1118 #if 0
1119 if (record->type == HAMMER_MEM_RECORD_DATA)
1120 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1121 record->leaf.base.key - record->leaf.data_len,
1122 record->leaf.data_offset, error);
1123 #endif
1125 if (error != ENOENT)
1126 goto done_unlock;
1129 * Allocate the record and data. The result buffers will be
1130 * marked as being modified and further calls to
1131 * hammer_modify_buffer() will result in unneeded UNDO records.
1133 * Support zero-fill records (data == NULL and data_len != 0)
1135 if (record->type == HAMMER_MEM_RECORD_DATA) {
1137 * The data portion of a bulk-data record has already been
1138 * committed to disk, we need only adjust the layer2
1139 * statistics in the same transaction as our B-Tree insert.
1141 KKASSERT(record->leaf.data_offset != 0);
1142 error = hammer_blockmap_finalize(trans,
1143 record->leaf.data_offset,
1144 record->leaf.data_len);
1145 } else if (record->data && record->leaf.data_len) {
1147 * Wholely cached record, with data. Allocate the data.
1149 bdata = hammer_alloc_data(trans, record->leaf.data_len,
1150 record->leaf.base.rec_type,
1151 &record->leaf.data_offset,
1152 &cursor->data_buffer, &error);
1153 if (bdata == NULL)
1154 goto done_unlock;
1155 hammer_crc_set_leaf(record->data, &record->leaf);
1156 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
1157 bcopy(record->data, bdata, record->leaf.data_len);
1158 hammer_modify_buffer_done(cursor->data_buffer);
1159 } else {
1161 * Wholely cached record, without data.
1163 record->leaf.data_offset = 0;
1164 record->leaf.data_crc = 0;
1167 error = hammer_btree_insert(cursor, &record->leaf, &doprop);
1168 if (hammer_debug_inode && error)
1169 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n", error, cursor->node->node_offset, cursor->index, record->leaf.base.key);
1172 * Our record is on-disk, normally mark the in-memory version as
1173 * deleted. If the record represented a directory deletion but
1174 * we had to sync a valid directory entry to disk we must convert
1175 * the record to a covering delete so the frontend does not have
1176 * visibility on the synced entry.
1178 if (error == 0) {
1179 if (doprop) {
1180 hammer_btree_do_propagation(cursor,
1181 record->ip->pfsm,
1182 &record->leaf);
1184 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1185 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1186 record->flags &= ~HAMMER_RECF_DELETED_FE;
1187 record->type = HAMMER_MEM_RECORD_DEL;
1188 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
1189 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1190 /* hammer_flush_record_done takes care of the rest */
1191 } else {
1192 record->flags |= HAMMER_RECF_DELETED_FE;
1193 record->flags |= HAMMER_RECF_DELETED_BE;
1195 record->flags |= HAMMER_RECF_COMMITTED;
1196 } else {
1197 if (record->leaf.data_offset) {
1198 hammer_blockmap_free(trans, record->leaf.data_offset,
1199 record->leaf.data_len);
1202 done_unlock:
1203 hammer_sync_unlock(trans);
1204 done:
1205 return(error);
1209 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1210 * entry's key is used to deal with hash collisions in the upper 32 bits.
1211 * A unique 64 bit key is generated in-memory and may be regenerated a
1212 * second time when the directory record is flushed to the on-disk B-Tree.
1214 * A referenced record is passed to this function. This function
1215 * eats the reference. If an error occurs the record will be deleted.
1217 * A copy of the temporary record->data pointer provided by the caller
1218 * will be made.
1221 hammer_mem_add(hammer_record_t record)
1223 hammer_mount_t hmp = record->ip->hmp;
1226 * Make a private copy of record->data
1228 if (record->data)
1229 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
1232 * Insert into the RB tree. A unique key should have already
1233 * been selected if this is a directory entry.
1235 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1236 record->flags |= HAMMER_RECF_DELETED_FE;
1237 hammer_rel_mem_record(record);
1238 return (EEXIST);
1240 ++hmp->count_newrecords;
1241 ++hmp->rsv_recs;
1242 ++record->ip->rsv_recs;
1243 record->ip->hmp->rsv_databytes += record->leaf.data_len;
1244 record->flags |= HAMMER_RECF_ONRBTREE;
1245 hammer_modify_inode(record->ip, HAMMER_INODE_XDIRTY);
1246 hammer_rel_mem_record(record);
1247 return(0);
1250 /************************************************************************
1251 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1252 ************************************************************************
1254 * These functions augment the B-Tree scanning functions in hammer_btree.c
1255 * by merging in-memory records with on-disk records.
1259 * Locate a particular record either in-memory or on-disk.
1261 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1262 * NOT be called to iterate results.
1265 hammer_ip_lookup(hammer_cursor_t cursor)
1267 int error;
1270 * If the element is in-memory return it without searching the
1271 * on-disk B-Tree
1273 KKASSERT(cursor->ip);
1274 error = hammer_mem_lookup(cursor);
1275 if (error == 0) {
1276 cursor->leaf = &cursor->iprec->leaf;
1277 return(error);
1279 if (error != ENOENT)
1280 return(error);
1283 * If the inode has on-disk components search the on-disk B-Tree.
1285 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
1286 return(error);
1287 error = hammer_btree_lookup(cursor);
1288 if (error == 0)
1289 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1290 return(error);
1294 * Locate the first record within the cursor's key_beg/key_end range,
1295 * restricted to a particular inode. 0 is returned on success, ENOENT
1296 * if no records matched the requested range, or some other error.
1298 * When 0 is returned hammer_ip_next() may be used to iterate additional
1299 * records within the requested range.
1301 * This function can return EDEADLK, requiring the caller to terminate
1302 * the cursor and try again.
1305 hammer_ip_first(hammer_cursor_t cursor)
1307 hammer_inode_t ip = cursor->ip;
1308 int error;
1310 KKASSERT(ip != NULL);
1313 * Clean up fields and setup for merged scan
1315 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1316 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
1317 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
1318 if (cursor->iprec) {
1319 hammer_rel_mem_record(cursor->iprec);
1320 cursor->iprec = NULL;
1324 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1325 * exact lookup so if we get ENOENT we have to call the iterate
1326 * function to validate the first record after the begin key.
1328 * The ATEDISK flag is used by hammer_btree_iterate to determine
1329 * whether it must index forwards or not. It is also used here
1330 * to select the next record from in-memory or on-disk.
1332 * EDEADLK can only occur if the lookup hit an empty internal
1333 * element and couldn't delete it. Since this could only occur
1334 * in-range, we can just iterate from the failure point.
1336 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1337 error = hammer_btree_lookup(cursor);
1338 if (error == ENOENT || error == EDEADLK) {
1339 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1340 if (hammer_debug_general & 0x2000)
1341 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
1342 error = hammer_btree_iterate(cursor);
1344 if (error && error != ENOENT)
1345 return(error);
1346 if (error == 0) {
1347 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
1348 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1349 } else {
1350 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1355 * Search the in-memory record list (Red-Black tree). Unlike the
1356 * B-Tree search, mem_first checks for records in the range.
1358 error = hammer_mem_first(cursor);
1359 if (error && error != ENOENT)
1360 return(error);
1361 if (error == 0) {
1362 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
1363 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1364 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0)
1365 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1369 * This will return the first matching record.
1371 return(hammer_ip_next(cursor));
1375 * Retrieve the next record in a merged iteration within the bounds of the
1376 * cursor. This call may be made multiple times after the cursor has been
1377 * initially searched with hammer_ip_first().
1379 * 0 is returned on success, ENOENT if no further records match the
1380 * requested range, or some other error code is returned.
1383 hammer_ip_next(hammer_cursor_t cursor)
1385 hammer_btree_elm_t elm;
1386 hammer_record_t rec, save;
1387 int error;
1388 int r;
1390 next_btree:
1392 * Load the current on-disk and in-memory record. If we ate any
1393 * records we have to get the next one.
1395 * If we deleted the last on-disk record we had scanned ATEDISK will
1396 * be clear and DELBTREE will be set, forcing a call to iterate. The
1397 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1398 * element. If ATEDISK is set, iterate will skip the 'current'
1399 * element.
1401 * Get the next on-disk record
1403 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
1404 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1405 error = hammer_btree_iterate(cursor);
1406 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
1407 if (error == 0) {
1408 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1409 hammer_cache_node(&cursor->ip->cache[1],
1410 cursor->node);
1411 } else {
1412 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1413 HAMMER_CURSOR_ATEDISK;
1418 next_memory:
1420 * Get the next in-memory record.
1422 * hammer_rec_scan_cmp: Is the record still in our general range,
1423 * (non-inclusive of snapshot exclusions)?
1424 * hammer_rec_scan_callback: Is the record in our snapshot?
1426 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1427 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1428 save = cursor->iprec;
1429 cursor->iprec = NULL;
1430 rec = save ? hammer_rec_rb_tree_RB_NEXT(save) : NULL;
1431 while (rec) {
1432 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1433 break;
1434 if (hammer_rec_scan_callback(rec, cursor) != 0)
1435 break;
1436 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1438 if (save)
1439 hammer_rel_mem_record(save);
1440 if (cursor->iprec) {
1441 KKASSERT(cursor->iprec == rec);
1442 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
1443 } else {
1444 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1450 * The memory record may have become stale while being held in
1451 * cursor->iprec. We are interlocked against the backend on
1452 * with regards to B-Tree entries.
1454 if ((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0) {
1455 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0) {
1456 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1457 goto next_memory;
1462 * Extract either the disk or memory record depending on their
1463 * relative position.
1465 error = 0;
1466 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
1467 case 0:
1469 * Both entries valid. Compare the entries and nominally
1470 * return the first one in the sort order. Numerous cases
1471 * require special attention, however.
1473 elm = &cursor->node->ondisk->elms[cursor->index];
1474 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
1477 * If the two entries differ only by their key (-2/2) or
1478 * create_tid (-1/1), and are DATA records, we may have a
1479 * nominal match. We have to calculate the base file
1480 * offset of the data.
1482 if (r <= 2 && r >= -2 && r != 0 &&
1483 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1484 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1485 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1486 int64_t base2 = cursor->iprec->leaf.base.key -
1487 cursor->iprec->leaf.data_len;
1488 if (base1 == base2)
1489 r = 0;
1492 if (r < 0) {
1493 error = hammer_btree_extract(cursor,
1494 HAMMER_CURSOR_GET_LEAF);
1495 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1496 break;
1500 * If the entries match exactly the memory entry is either
1501 * an on-disk directory entry deletion or a bulk data
1502 * overwrite. If it is a directory entry deletion we eat
1503 * both entries.
1505 * For the bulk-data overwrite case it is possible to have
1506 * visibility into both, which simply means the syncer
1507 * hasn't gotten around to doing the delete+insert sequence
1508 * on the B-Tree. Use the memory entry and throw away the
1509 * on-disk entry.
1511 * If the in-memory record is not either of these we
1512 * probably caught the syncer while it was syncing it to
1513 * the media. Since we hold a shared lock on the cursor,
1514 * the in-memory record had better be marked deleted at
1515 * this point.
1517 if (r == 0) {
1518 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1519 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1520 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1521 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1522 goto next_btree;
1524 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1525 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1526 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1528 /* fall through to memory entry */
1529 } else {
1530 panic("hammer_ip_next: duplicate mem/b-tree entry %p %d %08x", cursor->iprec, cursor->iprec->type, cursor->iprec->flags);
1531 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1532 goto next_memory;
1535 /* fall through to the memory entry */
1536 case HAMMER_CURSOR_ATEDISK:
1538 * Only the memory entry is valid.
1540 cursor->leaf = &cursor->iprec->leaf;
1541 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1544 * If the memory entry is an on-disk deletion we should have
1545 * also had found a B-Tree record. If the backend beat us
1546 * to it it would have interlocked the cursor and we should
1547 * have seen the in-memory record marked DELETED_FE.
1549 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1550 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1551 panic("hammer_ip_next: del-on-disk with no b-tree entry iprec %p flags %08x", cursor->iprec, cursor->iprec->flags);
1553 break;
1554 case HAMMER_CURSOR_ATEMEM:
1556 * Only the disk entry is valid
1558 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1559 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1560 break;
1561 default:
1563 * Neither entry is valid
1565 * XXX error not set properly
1567 cursor->leaf = NULL;
1568 error = ENOENT;
1569 break;
1571 return(error);
1575 * Resolve the cursor->data pointer for the current cursor position in
1576 * a merged iteration.
1579 hammer_ip_resolve_data(hammer_cursor_t cursor)
1581 hammer_record_t record;
1582 int error;
1584 if (hammer_cursor_inmem(cursor)) {
1586 * The data associated with an in-memory record is usually
1587 * kmalloced, but reserve-ahead data records will have an
1588 * on-disk reference.
1590 * NOTE: Reserve-ahead data records must be handled in the
1591 * context of the related high level buffer cache buffer
1592 * to interlock against async writes.
1594 record = cursor->iprec;
1595 cursor->data = record->data;
1596 error = 0;
1597 if (cursor->data == NULL) {
1598 KKASSERT(record->leaf.base.rec_type ==
1599 HAMMER_RECTYPE_DATA);
1600 cursor->data = hammer_bread_ext(cursor->trans->hmp,
1601 record->leaf.data_offset,
1602 record->leaf.data_len,
1603 &error,
1604 &cursor->data_buffer);
1606 } else {
1607 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
1608 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1610 return(error);
1614 * Backend truncation / record replacement - delete records in range.
1616 * Delete all records within the specified range for inode ip. In-memory
1617 * records still associated with the frontend are ignored.
1619 * If truncating is non-zero in-memory records associated with the back-end
1620 * are ignored. If truncating is > 1 we can return EWOULDBLOCK.
1622 * NOTES:
1624 * * An unaligned range will cause new records to be added to cover
1625 * the edge cases. (XXX not implemented yet).
1627 * * Replacement via reservations (see hammer_ip_sync_record_cursor())
1628 * also do not deal with unaligned ranges.
1630 * * ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1632 * * Record keys for regular file data have to be special-cased since
1633 * they indicate the end of the range (key = base + bytes).
1635 * * This function may be asked to delete ridiculously huge ranges, for
1636 * example if someone truncates or removes a 1TB regular file. We
1637 * must be very careful on restarts and we may have to stop w/
1638 * EWOULDBLOCK to avoid blowing out the buffer cache.
1641 hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1642 int64_t ran_beg, int64_t ran_end, int truncating)
1644 hammer_transaction_t trans = cursor->trans;
1645 hammer_btree_leaf_elm_t leaf;
1646 int error;
1647 int64_t off;
1648 int64_t tmp64;
1650 #if 0
1651 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1652 #endif
1654 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1655 retry:
1656 hammer_normalize_cursor(cursor);
1657 cursor->key_beg.localization = ip->obj_localization +
1658 HAMMER_LOCALIZE_MISC;
1659 cursor->key_beg.obj_id = ip->obj_id;
1660 cursor->key_beg.create_tid = 0;
1661 cursor->key_beg.delete_tid = 0;
1662 cursor->key_beg.obj_type = 0;
1664 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1665 cursor->key_beg.key = ran_beg;
1666 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1667 } else {
1669 * The key in the B-Tree is (base+bytes), so the first possible
1670 * matching key is ran_beg + 1.
1672 cursor->key_beg.key = ran_beg + 1;
1673 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1676 cursor->key_end = cursor->key_beg;
1677 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1678 cursor->key_end.key = ran_end;
1679 } else {
1680 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1681 if (tmp64 < ran_end)
1682 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1683 else
1684 cursor->key_end.key = ran_end + MAXPHYS + 1;
1687 cursor->asof = ip->obj_asof;
1688 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1689 cursor->flags |= HAMMER_CURSOR_ASOF;
1690 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1691 cursor->flags |= HAMMER_CURSOR_BACKEND;
1692 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
1694 error = hammer_ip_first(cursor);
1697 * Iterate through matching records and mark them as deleted.
1699 while (error == 0) {
1700 leaf = cursor->leaf;
1702 KKASSERT(leaf->base.delete_tid == 0);
1703 KKASSERT(leaf->base.obj_id == ip->obj_id);
1706 * There may be overlap cases for regular file data. Also
1707 * remember the key for a regular file record is (base + len),
1708 * NOT (base).
1710 * Note that do to duplicates (mem & media) allowed by
1711 * DELETE_VISIBILITY, off can wind up less then ran_beg.
1713 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
1714 off = leaf->base.key - leaf->data_len;
1716 * Check the left edge case. We currently do not
1717 * split existing records.
1719 if (off < ran_beg && leaf->base.key > ran_beg) {
1720 panic("hammer left edge case %016llx %d\n",
1721 leaf->base.key, leaf->data_len);
1725 * Check the right edge case. Note that the
1726 * record can be completely out of bounds, which
1727 * terminates the search.
1729 * base->key is exclusive of the right edge while
1730 * ran_end is inclusive of the right edge. The
1731 * (key - data_len) left boundary is inclusive.
1733 * XXX theory-check this test at some point, are
1734 * we missing a + 1 somewhere? Note that ran_end
1735 * could overflow.
1737 if (leaf->base.key - 1 > ran_end) {
1738 if (leaf->base.key - leaf->data_len > ran_end)
1739 break;
1740 panic("hammer right edge case\n");
1742 } else {
1743 off = leaf->base.key;
1747 * Delete the record. When truncating we do not delete
1748 * in-memory (data) records because they represent data
1749 * written after the truncation.
1751 * This will also physically destroy the B-Tree entry and
1752 * data if the retention policy dictates. The function
1753 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1754 * uses to perform a fixup.
1756 if (truncating == 0 || hammer_cursor_ondisk(cursor)) {
1757 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1759 * If we have built up too many meta-buffers we risk
1760 * deadlocking the kernel and must stop. This can
1761 * occur when deleting ridiculously huge files.
1762 * sync_trunc_off is updated so the next cycle does
1763 * not re-iterate records we have already deleted.
1765 * This is only done with formal truncations.
1767 if (truncating > 1 && error == 0 &&
1768 hammer_flusher_meta_limit(ip->hmp)) {
1769 ip->sync_trunc_off = off;
1770 error = EWOULDBLOCK;
1773 if (error)
1774 break;
1775 ran_beg = off; /* for restart */
1776 error = hammer_ip_next(cursor);
1778 if (cursor->node)
1779 hammer_cache_node(&ip->cache[1], cursor->node);
1781 if (error == EDEADLK) {
1782 hammer_done_cursor(cursor);
1783 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1784 if (error == 0)
1785 goto retry;
1787 if (error == ENOENT)
1788 error = 0;
1789 return(error);
1793 * This backend function deletes the specified record on-disk, similar to
1794 * delete_range but for a specific record. Unlike the exact deletions
1795 * used when deleting a directory entry this function uses an ASOF search
1796 * like delete_range.
1798 * This function may be called with ip->obj_asof set for a slave snapshot,
1799 * so don't use it. We always delete non-historical records only.
1801 static int
1802 hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
1803 hammer_btree_leaf_elm_t leaf)
1805 hammer_transaction_t trans = cursor->trans;
1806 int error;
1808 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1809 retry:
1810 hammer_normalize_cursor(cursor);
1811 cursor->key_beg = leaf->base;
1812 cursor->asof = HAMMER_MAX_TID;
1813 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1814 cursor->flags |= HAMMER_CURSOR_ASOF;
1815 cursor->flags |= HAMMER_CURSOR_BACKEND;
1816 cursor->flags &= ~HAMMER_CURSOR_INSERT;
1818 error = hammer_btree_lookup(cursor);
1819 if (error == 0) {
1820 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1822 if (error == EDEADLK) {
1823 hammer_done_cursor(cursor);
1824 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1825 if (error == 0)
1826 goto retry;
1828 return(error);
1832 * This function deletes remaining auxillary records when an inode is
1833 * being deleted. This function explicitly does not delete the
1834 * inode record, directory entry, data, or db records. Those must be
1835 * properly disposed of prior to this call.
1838 hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip, int *countp)
1840 hammer_transaction_t trans = cursor->trans;
1841 hammer_btree_leaf_elm_t leaf;
1842 int error;
1844 KKASSERT(trans->type == HAMMER_TRANS_FLS);
1845 retry:
1846 hammer_normalize_cursor(cursor);
1847 cursor->key_beg.localization = ip->obj_localization +
1848 HAMMER_LOCALIZE_MISC;
1849 cursor->key_beg.obj_id = ip->obj_id;
1850 cursor->key_beg.create_tid = 0;
1851 cursor->key_beg.delete_tid = 0;
1852 cursor->key_beg.obj_type = 0;
1853 cursor->key_beg.rec_type = HAMMER_RECTYPE_CLEAN_START;
1854 cursor->key_beg.key = HAMMER_MIN_KEY;
1856 cursor->key_end = cursor->key_beg;
1857 cursor->key_end.rec_type = HAMMER_RECTYPE_MAX;
1858 cursor->key_end.key = HAMMER_MAX_KEY;
1860 cursor->asof = ip->obj_asof;
1861 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1862 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1863 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1864 cursor->flags |= HAMMER_CURSOR_BACKEND;
1866 error = hammer_ip_first(cursor);
1869 * Iterate through matching records and mark them as deleted.
1871 while (error == 0) {
1872 leaf = cursor->leaf;
1874 KKASSERT(leaf->base.delete_tid == 0);
1877 * Mark the record and B-Tree entry as deleted. This will
1878 * also physically delete the B-Tree entry, record, and
1879 * data if the retention policy dictates. The function
1880 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1881 * uses to perform a fixup.
1883 * Directory entries (and delete-on-disk directory entries)
1884 * must be synced and cannot be deleted.
1886 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1887 ++*countp;
1888 if (error)
1889 break;
1890 error = hammer_ip_next(cursor);
1892 if (cursor->node)
1893 hammer_cache_node(&ip->cache[1], cursor->node);
1894 if (error == EDEADLK) {
1895 hammer_done_cursor(cursor);
1896 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
1897 if (error == 0)
1898 goto retry;
1900 if (error == ENOENT)
1901 error = 0;
1902 return(error);
1906 * Delete the record at the current cursor. On success the cursor will
1907 * be positioned appropriately for an iteration but may no longer be at
1908 * a leaf node.
1910 * This routine is only called from the backend.
1912 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1913 * cursor and retry.
1916 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
1917 hammer_tid_t tid)
1919 hammer_record_t iprec;
1920 hammer_mount_t hmp;
1921 int error;
1923 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1924 KKASSERT(tid != 0);
1925 hmp = cursor->node->hmp;
1928 * In-memory (unsynchronized) records can simply be freed. This
1929 * only occurs in range iterations since all other records are
1930 * individually synchronized. Thus there should be no confusion with
1931 * the interlock.
1933 * An in-memory record may be deleted before being committed to disk,
1934 * but could have been accessed in the mean time. The reservation
1935 * code will deal with the case.
1937 if (hammer_cursor_inmem(cursor)) {
1938 iprec = cursor->iprec;
1939 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
1940 iprec->flags |= HAMMER_RECF_DELETED_FE;
1941 iprec->flags |= HAMMER_RECF_DELETED_BE;
1942 return(0);
1946 * On-disk records are marked as deleted by updating their delete_tid.
1947 * This does not effect their position in the B-Tree (which is based
1948 * on their create_tid).
1950 * Frontend B-Tree operations track inodes so we tell
1951 * hammer_delete_at_cursor() not to.
1953 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
1955 if (error == 0) {
1956 error = hammer_delete_at_cursor(
1957 cursor,
1958 HAMMER_DELETE_ADJUST | hammer_nohistory(ip),
1959 cursor->trans->tid,
1960 cursor->trans->time32,
1961 0, NULL);
1963 return(error);
1967 * Delete the B-Tree element at the current cursor and do any necessary
1968 * mirror propagation.
1970 * The cursor must be properly positioned for an iteration on return but
1971 * may be pointing at an internal element.
1973 * An element can be un-deleted by passing a delete_tid of 0 with
1974 * HAMMER_DELETE_ADJUST.
1977 hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
1978 hammer_tid_t delete_tid, u_int32_t delete_ts,
1979 int track, int64_t *stat_bytes)
1981 struct hammer_btree_leaf_elm save_leaf;
1982 hammer_transaction_t trans;
1983 hammer_btree_leaf_elm_t leaf;
1984 hammer_node_t node;
1985 hammer_btree_elm_t elm;
1986 hammer_off_t data_offset;
1987 int32_t data_len;
1988 u_int16_t rec_type;
1989 int error;
1990 int icount;
1991 int doprop;
1993 error = hammer_cursor_upgrade(cursor);
1994 if (error)
1995 return(error);
1997 trans = cursor->trans;
1998 node = cursor->node;
1999 elm = &node->ondisk->elms[cursor->index];
2000 leaf = &elm->leaf;
2001 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
2003 hammer_sync_lock_sh(trans);
2004 doprop = 0;
2005 icount = 0;
2008 * Adjust the delete_tid. Update the mirror_tid propagation field
2009 * as well. delete_tid can be 0 (undelete -- used by mirroring).
2011 if (delete_flags & HAMMER_DELETE_ADJUST) {
2012 if (elm->base.rec_type == HAMMER_RECTYPE_INODE) {
2013 if (elm->leaf.base.delete_tid == 0 && delete_tid)
2014 icount = -1;
2015 if (elm->leaf.base.delete_tid && delete_tid == 0)
2016 icount = 1;
2019 hammer_modify_node(trans, node, elm, sizeof(*elm));
2020 elm->leaf.base.delete_tid = delete_tid;
2021 elm->leaf.delete_ts = delete_ts;
2022 hammer_modify_node_done(node);
2024 if (elm->leaf.base.delete_tid > node->ondisk->mirror_tid) {
2025 hammer_modify_node_field(trans, node, mirror_tid);
2026 node->ondisk->mirror_tid = elm->leaf.base.delete_tid;
2027 hammer_modify_node_done(node);
2028 doprop = 1;
2029 if (hammer_debug_general & 0x0002) {
2030 kprintf("delete_at_cursor: propagate %016llx"
2031 " @%016llx\n",
2032 elm->leaf.base.delete_tid,
2033 node->node_offset);
2038 * Adjust for the iteration. We have deleted the current
2039 * element and want to clear ATEDISK so the iteration does
2040 * not skip the element after, which now becomes the current
2041 * element.
2043 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2044 cursor->flags |= HAMMER_CURSOR_DELBTREE;
2045 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2049 * An on-disk record cannot have the same delete_tid
2050 * as its create_tid. In a chain of record updates
2051 * this could result in a duplicate record.
2053 KKASSERT(elm->leaf.base.delete_tid !=
2054 elm->leaf.base.create_tid);
2058 * Destroy the B-Tree element if asked (typically if a nohistory
2059 * file or mount, or when called by the pruning code).
2061 * Adjust the ATEDISK flag to properly support iterations.
2063 if (delete_flags & HAMMER_DELETE_DESTROY) {
2064 data_offset = elm->leaf.data_offset;
2065 data_len = elm->leaf.data_len;
2066 rec_type = elm->leaf.base.rec_type;
2067 if (doprop) {
2068 save_leaf = elm->leaf;
2069 leaf = &save_leaf;
2071 if (elm->base.rec_type == HAMMER_RECTYPE_INODE &&
2072 elm->leaf.base.delete_tid == 0) {
2073 icount = -1;
2076 error = hammer_btree_delete(cursor);
2077 if (error == 0) {
2079 * This forces a fixup for the iteration because
2080 * the cursor is now either sitting at the 'next'
2081 * element or sitting at the end of a leaf.
2083 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
2084 cursor->flags |= HAMMER_CURSOR_DELBTREE;
2085 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2088 if (error == 0) {
2089 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
2090 case HAMMER_ZONE_LARGE_DATA:
2091 case HAMMER_ZONE_SMALL_DATA:
2092 case HAMMER_ZONE_META:
2093 hammer_blockmap_free(trans,
2094 data_offset, data_len);
2095 break;
2096 default:
2097 break;
2103 * Track inode count and next_tid. This is used by the mirroring
2104 * and PFS code. icount can be negative, zero, or positive.
2106 if (error == 0 && track) {
2107 if (icount) {
2108 hammer_modify_volume_field(trans, trans->rootvol,
2109 vol0_stat_inodes);
2110 trans->rootvol->ondisk->vol0_stat_inodes += icount;
2111 hammer_modify_volume_done(trans->rootvol);
2113 if (trans->rootvol->ondisk->vol0_next_tid < delete_tid) {
2114 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
2115 trans->rootvol->ondisk->vol0_next_tid = delete_tid;
2116 hammer_modify_volume_done(trans->rootvol);
2121 * mirror_tid propagation occurs if the node's mirror_tid had to be
2122 * updated while adjusting the delete_tid.
2124 * This occurs when deleting even in nohistory mode, but does not
2125 * occur when pruning an already-deleted node.
2127 * cursor->ip is NULL when called from the pruning, mirroring,
2128 * and pfs code. If non-NULL propagation will be conditionalized
2129 * on whether the PFS is in no-history mode or not.
2131 if (doprop) {
2132 if (cursor->ip)
2133 hammer_btree_do_propagation(cursor, cursor->ip->pfsm, leaf);
2134 else
2135 hammer_btree_do_propagation(cursor, NULL, leaf);
2137 hammer_sync_unlock(trans);
2138 return (error);
2142 * Determine whether we can remove a directory. This routine checks whether
2143 * a directory is empty or not and enforces flush connectivity.
2145 * Flush connectivity requires that we block if the target directory is
2146 * currently flushing, otherwise it may not end up in the same flush group.
2148 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
2151 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
2153 struct hammer_cursor cursor;
2154 int error;
2157 * Check directory empty
2159 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2161 cursor.key_beg.localization = ip->obj_localization +
2162 HAMMER_LOCALIZE_MISC;
2163 cursor.key_beg.obj_id = ip->obj_id;
2164 cursor.key_beg.create_tid = 0;
2165 cursor.key_beg.delete_tid = 0;
2166 cursor.key_beg.obj_type = 0;
2167 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
2168 cursor.key_beg.key = HAMMER_MIN_KEY;
2170 cursor.key_end = cursor.key_beg;
2171 cursor.key_end.rec_type = 0xFFFF;
2172 cursor.key_end.key = HAMMER_MAX_KEY;
2174 cursor.asof = ip->obj_asof;
2175 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2177 error = hammer_ip_first(&cursor);
2178 if (error == ENOENT)
2179 error = 0;
2180 else if (error == 0)
2181 error = ENOTEMPTY;
2182 hammer_done_cursor(&cursor);
2183 return(error);