1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Fujitsu. All rights reserved.
4 * Written by Miao Xie <miaox@cn.fujitsu.com>
7 #include <linux/slab.h>
8 #include <linux/iversion.h>
10 #include "delayed-inode.h"
12 #include "transaction.h"
16 #include "inode-item.h"
18 #define BTRFS_DELAYED_WRITEBACK 512
19 #define BTRFS_DELAYED_BACKGROUND 128
20 #define BTRFS_DELAYED_BATCH 16
22 static struct kmem_cache *delayed_node_cache;
24 int __init btrfs_delayed_inode_init(void)
26 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
27 sizeof(struct btrfs_delayed_node),
31 if (!delayed_node_cache)
36 void __cold btrfs_delayed_inode_exit(void)
38 kmem_cache_destroy(delayed_node_cache);
41 static inline void btrfs_init_delayed_node(
42 struct btrfs_delayed_node *delayed_node,
43 struct btrfs_root *root, u64 inode_id)
45 delayed_node->root = root;
46 delayed_node->inode_id = inode_id;
47 refcount_set(&delayed_node->refs, 0);
48 delayed_node->ins_root = RB_ROOT_CACHED;
49 delayed_node->del_root = RB_ROOT_CACHED;
50 mutex_init(&delayed_node->mutex);
51 INIT_LIST_HEAD(&delayed_node->n_list);
52 INIT_LIST_HEAD(&delayed_node->p_list);
55 static struct btrfs_delayed_node *btrfs_get_delayed_node(
56 struct btrfs_inode *btrfs_inode)
58 struct btrfs_root *root = btrfs_inode->root;
59 u64 ino = btrfs_ino(btrfs_inode);
60 struct btrfs_delayed_node *node;
62 node = READ_ONCE(btrfs_inode->delayed_node);
64 refcount_inc(&node->refs);
68 spin_lock(&root->inode_lock);
69 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
72 if (btrfs_inode->delayed_node) {
73 refcount_inc(&node->refs); /* can be accessed */
74 BUG_ON(btrfs_inode->delayed_node != node);
75 spin_unlock(&root->inode_lock);
80 * It's possible that we're racing into the middle of removing
81 * this node from the radix tree. In this case, the refcount
82 * was zero and it should never go back to one. Just return
83 * NULL like it was never in the radix at all; our release
84 * function is in the process of removing it.
86 * Some implementations of refcount_inc refuse to bump the
87 * refcount once it has hit zero. If we don't do this dance
88 * here, refcount_inc() may decide to just WARN_ONCE() instead
89 * of actually bumping the refcount.
91 * If this node is properly in the radix, we want to bump the
92 * refcount twice, once for the inode and once for this get
95 if (refcount_inc_not_zero(&node->refs)) {
96 refcount_inc(&node->refs);
97 btrfs_inode->delayed_node = node;
102 spin_unlock(&root->inode_lock);
105 spin_unlock(&root->inode_lock);
110 /* Will return either the node or PTR_ERR(-ENOMEM) */
111 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
112 struct btrfs_inode *btrfs_inode)
114 struct btrfs_delayed_node *node;
115 struct btrfs_root *root = btrfs_inode->root;
116 u64 ino = btrfs_ino(btrfs_inode);
120 node = btrfs_get_delayed_node(btrfs_inode);
124 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
126 return ERR_PTR(-ENOMEM);
127 btrfs_init_delayed_node(node, root, ino);
129 /* cached in the btrfs inode and can be accessed */
130 refcount_set(&node->refs, 2);
132 ret = radix_tree_preload(GFP_NOFS);
134 kmem_cache_free(delayed_node_cache, node);
138 spin_lock(&root->inode_lock);
139 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
140 if (ret == -EEXIST) {
141 spin_unlock(&root->inode_lock);
142 kmem_cache_free(delayed_node_cache, node);
143 radix_tree_preload_end();
146 btrfs_inode->delayed_node = node;
147 spin_unlock(&root->inode_lock);
148 radix_tree_preload_end();
154 * Call it when holding delayed_node->mutex
156 * If mod = 1, add this node into the prepared list.
158 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
159 struct btrfs_delayed_node *node,
162 spin_lock(&root->lock);
163 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
164 if (!list_empty(&node->p_list))
165 list_move_tail(&node->p_list, &root->prepare_list);
167 list_add_tail(&node->p_list, &root->prepare_list);
169 list_add_tail(&node->n_list, &root->node_list);
170 list_add_tail(&node->p_list, &root->prepare_list);
171 refcount_inc(&node->refs); /* inserted into list */
173 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
175 spin_unlock(&root->lock);
178 /* Call it when holding delayed_node->mutex */
179 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
180 struct btrfs_delayed_node *node)
182 spin_lock(&root->lock);
183 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
185 refcount_dec(&node->refs); /* not in the list */
186 list_del_init(&node->n_list);
187 if (!list_empty(&node->p_list))
188 list_del_init(&node->p_list);
189 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
191 spin_unlock(&root->lock);
194 static struct btrfs_delayed_node *btrfs_first_delayed_node(
195 struct btrfs_delayed_root *delayed_root)
198 struct btrfs_delayed_node *node = NULL;
200 spin_lock(&delayed_root->lock);
201 if (list_empty(&delayed_root->node_list))
204 p = delayed_root->node_list.next;
205 node = list_entry(p, struct btrfs_delayed_node, n_list);
206 refcount_inc(&node->refs);
208 spin_unlock(&delayed_root->lock);
213 static struct btrfs_delayed_node *btrfs_next_delayed_node(
214 struct btrfs_delayed_node *node)
216 struct btrfs_delayed_root *delayed_root;
218 struct btrfs_delayed_node *next = NULL;
220 delayed_root = node->root->fs_info->delayed_root;
221 spin_lock(&delayed_root->lock);
222 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
223 /* not in the list */
224 if (list_empty(&delayed_root->node_list))
226 p = delayed_root->node_list.next;
227 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
230 p = node->n_list.next;
232 next = list_entry(p, struct btrfs_delayed_node, n_list);
233 refcount_inc(&next->refs);
235 spin_unlock(&delayed_root->lock);
240 static void __btrfs_release_delayed_node(
241 struct btrfs_delayed_node *delayed_node,
244 struct btrfs_delayed_root *delayed_root;
249 delayed_root = delayed_node->root->fs_info->delayed_root;
251 mutex_lock(&delayed_node->mutex);
252 if (delayed_node->count)
253 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
255 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
256 mutex_unlock(&delayed_node->mutex);
258 if (refcount_dec_and_test(&delayed_node->refs)) {
259 struct btrfs_root *root = delayed_node->root;
261 spin_lock(&root->inode_lock);
263 * Once our refcount goes to zero, nobody is allowed to bump it
264 * back up. We can delete it now.
266 ASSERT(refcount_read(&delayed_node->refs) == 0);
267 radix_tree_delete(&root->delayed_nodes_tree,
268 delayed_node->inode_id);
269 spin_unlock(&root->inode_lock);
270 kmem_cache_free(delayed_node_cache, delayed_node);
274 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
276 __btrfs_release_delayed_node(node, 0);
279 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
280 struct btrfs_delayed_root *delayed_root)
283 struct btrfs_delayed_node *node = NULL;
285 spin_lock(&delayed_root->lock);
286 if (list_empty(&delayed_root->prepare_list))
289 p = delayed_root->prepare_list.next;
291 node = list_entry(p, struct btrfs_delayed_node, p_list);
292 refcount_inc(&node->refs);
294 spin_unlock(&delayed_root->lock);
299 static inline void btrfs_release_prepared_delayed_node(
300 struct btrfs_delayed_node *node)
302 __btrfs_release_delayed_node(node, 1);
305 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u16 data_len,
306 struct btrfs_delayed_node *node,
307 enum btrfs_delayed_item_type type)
309 struct btrfs_delayed_item *item;
311 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
313 item->data_len = data_len;
315 item->bytes_reserved = 0;
316 item->delayed_node = node;
317 RB_CLEAR_NODE(&item->rb_node);
318 INIT_LIST_HEAD(&item->log_list);
319 item->logged = false;
320 refcount_set(&item->refs, 1);
326 * __btrfs_lookup_delayed_item - look up the delayed item by key
327 * @delayed_node: pointer to the delayed node
328 * @index: the dir index value to lookup (offset of a dir index key)
330 * Note: if we don't find the right item, we will return the prev item and
333 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
334 struct rb_root *root,
337 struct rb_node *node = root->rb_node;
338 struct btrfs_delayed_item *delayed_item = NULL;
341 delayed_item = rb_entry(node, struct btrfs_delayed_item,
343 if (delayed_item->index < index)
344 node = node->rb_right;
345 else if (delayed_item->index > index)
346 node = node->rb_left;
354 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
355 struct btrfs_delayed_item *ins)
357 struct rb_node **p, *node;
358 struct rb_node *parent_node = NULL;
359 struct rb_root_cached *root;
360 struct btrfs_delayed_item *item;
361 bool leftmost = true;
363 if (ins->type == BTRFS_DELAYED_INSERTION_ITEM)
364 root = &delayed_node->ins_root;
366 root = &delayed_node->del_root;
368 p = &root->rb_root.rb_node;
369 node = &ins->rb_node;
373 item = rb_entry(parent_node, struct btrfs_delayed_item,
376 if (item->index < ins->index) {
379 } else if (item->index > ins->index) {
386 rb_link_node(node, parent_node, p);
387 rb_insert_color_cached(node, root, leftmost);
389 if (ins->type == BTRFS_DELAYED_INSERTION_ITEM &&
390 ins->index >= delayed_node->index_cnt)
391 delayed_node->index_cnt = ins->index + 1;
393 delayed_node->count++;
394 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
398 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
400 int seq = atomic_inc_return(&delayed_root->items_seq);
402 /* atomic_dec_return implies a barrier */
403 if ((atomic_dec_return(&delayed_root->items) <
404 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0))
405 cond_wake_up_nomb(&delayed_root->wait);
408 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
410 struct rb_root_cached *root;
411 struct btrfs_delayed_root *delayed_root;
413 /* Not inserted, ignore it. */
414 if (RB_EMPTY_NODE(&delayed_item->rb_node))
417 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
419 BUG_ON(!delayed_root);
421 if (delayed_item->type == BTRFS_DELAYED_INSERTION_ITEM)
422 root = &delayed_item->delayed_node->ins_root;
424 root = &delayed_item->delayed_node->del_root;
426 rb_erase_cached(&delayed_item->rb_node, root);
427 RB_CLEAR_NODE(&delayed_item->rb_node);
428 delayed_item->delayed_node->count--;
430 finish_one_item(delayed_root);
433 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
436 __btrfs_remove_delayed_item(item);
437 if (refcount_dec_and_test(&item->refs))
442 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
443 struct btrfs_delayed_node *delayed_node)
446 struct btrfs_delayed_item *item = NULL;
448 p = rb_first_cached(&delayed_node->ins_root);
450 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
455 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
456 struct btrfs_delayed_node *delayed_node)
459 struct btrfs_delayed_item *item = NULL;
461 p = rb_first_cached(&delayed_node->del_root);
463 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
468 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
469 struct btrfs_delayed_item *item)
472 struct btrfs_delayed_item *next = NULL;
474 p = rb_next(&item->rb_node);
476 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
481 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
482 struct btrfs_delayed_item *item)
484 struct btrfs_block_rsv *src_rsv;
485 struct btrfs_block_rsv *dst_rsv;
486 struct btrfs_fs_info *fs_info = trans->fs_info;
490 if (!trans->bytes_reserved)
493 src_rsv = trans->block_rsv;
494 dst_rsv = &fs_info->delayed_block_rsv;
496 num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
499 * Here we migrate space rsv from transaction rsv, since have already
500 * reserved space when starting a transaction. So no need to reserve
503 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
505 trace_btrfs_space_reservation(fs_info, "delayed_item",
506 item->delayed_node->inode_id,
509 * For insertions we track reserved metadata space by accounting
510 * for the number of leaves that will be used, based on the delayed
511 * node's index_items_size field.
513 if (item->type == BTRFS_DELAYED_DELETION_ITEM)
514 item->bytes_reserved = num_bytes;
520 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
521 struct btrfs_delayed_item *item)
523 struct btrfs_block_rsv *rsv;
524 struct btrfs_fs_info *fs_info = root->fs_info;
526 if (!item->bytes_reserved)
529 rsv = &fs_info->delayed_block_rsv;
531 * Check btrfs_delayed_item_reserve_metadata() to see why we don't need
532 * to release/reserve qgroup space.
534 trace_btrfs_space_reservation(fs_info, "delayed_item",
535 item->delayed_node->inode_id,
536 item->bytes_reserved, 0);
537 btrfs_block_rsv_release(fs_info, rsv, item->bytes_reserved, NULL);
540 static void btrfs_delayed_item_release_leaves(struct btrfs_delayed_node *node,
541 unsigned int num_leaves)
543 struct btrfs_fs_info *fs_info = node->root->fs_info;
544 const u64 bytes = btrfs_calc_insert_metadata_size(fs_info, num_leaves);
546 /* There are no space reservations during log replay, bail out. */
547 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
550 trace_btrfs_space_reservation(fs_info, "delayed_item", node->inode_id,
552 btrfs_block_rsv_release(fs_info, &fs_info->delayed_block_rsv, bytes, NULL);
555 static int btrfs_delayed_inode_reserve_metadata(
556 struct btrfs_trans_handle *trans,
557 struct btrfs_root *root,
558 struct btrfs_delayed_node *node)
560 struct btrfs_fs_info *fs_info = root->fs_info;
561 struct btrfs_block_rsv *src_rsv;
562 struct btrfs_block_rsv *dst_rsv;
566 src_rsv = trans->block_rsv;
567 dst_rsv = &fs_info->delayed_block_rsv;
569 num_bytes = btrfs_calc_metadata_size(fs_info, 1);
572 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
573 * which doesn't reserve space for speed. This is a problem since we
574 * still need to reserve space for this update, so try to reserve the
577 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
578 * we always reserve enough to update the inode item.
580 if (!src_rsv || (!trans->bytes_reserved &&
581 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
582 ret = btrfs_qgroup_reserve_meta(root, num_bytes,
583 BTRFS_QGROUP_RSV_META_PREALLOC, true);
586 ret = btrfs_block_rsv_add(fs_info, dst_rsv, num_bytes,
587 BTRFS_RESERVE_NO_FLUSH);
588 /* NO_FLUSH could only fail with -ENOSPC */
589 ASSERT(ret == 0 || ret == -ENOSPC);
591 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
593 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
597 trace_btrfs_space_reservation(fs_info, "delayed_inode",
598 node->inode_id, num_bytes, 1);
599 node->bytes_reserved = num_bytes;
605 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
606 struct btrfs_delayed_node *node,
609 struct btrfs_block_rsv *rsv;
611 if (!node->bytes_reserved)
614 rsv = &fs_info->delayed_block_rsv;
615 trace_btrfs_space_reservation(fs_info, "delayed_inode",
616 node->inode_id, node->bytes_reserved, 0);
617 btrfs_block_rsv_release(fs_info, rsv, node->bytes_reserved, NULL);
619 btrfs_qgroup_free_meta_prealloc(node->root,
620 node->bytes_reserved);
622 btrfs_qgroup_convert_reserved_meta(node->root,
623 node->bytes_reserved);
624 node->bytes_reserved = 0;
628 * Insert a single delayed item or a batch of delayed items, as many as possible
629 * that fit in a leaf. The delayed items (dir index keys) are sorted by their key
630 * in the rbtree, and if there's a gap between two consecutive dir index items,
631 * then it means at some point we had delayed dir indexes to add but they got
632 * removed (by btrfs_delete_delayed_dir_index()) before we attempted to flush them
633 * into the subvolume tree. Dir index keys also have their offsets coming from a
634 * monotonically increasing counter, so we can't get new keys with an offset that
635 * fits within a gap between delayed dir index items.
637 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
638 struct btrfs_root *root,
639 struct btrfs_path *path,
640 struct btrfs_delayed_item *first_item)
642 struct btrfs_fs_info *fs_info = root->fs_info;
643 struct btrfs_delayed_node *node = first_item->delayed_node;
644 LIST_HEAD(item_list);
645 struct btrfs_delayed_item *curr;
646 struct btrfs_delayed_item *next;
647 const int max_size = BTRFS_LEAF_DATA_SIZE(fs_info);
648 struct btrfs_item_batch batch;
649 struct btrfs_key first_key;
650 const u32 first_data_size = first_item->data_len;
652 char *ins_data = NULL;
654 bool continuous_keys_only = false;
656 lockdep_assert_held(&node->mutex);
659 * During normal operation the delayed index offset is continuously
660 * increasing, so we can batch insert all items as there will not be any
661 * overlapping keys in the tree.
663 * The exception to this is log replay, where we may have interleaved
664 * offsets in the tree, so our batch needs to be continuous keys only in
665 * order to ensure we do not end up with out of order items in our leaf.
667 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
668 continuous_keys_only = true;
671 * For delayed items to insert, we track reserved metadata bytes based
672 * on the number of leaves that we will use.
673 * See btrfs_insert_delayed_dir_index() and
674 * btrfs_delayed_item_reserve_metadata()).
676 ASSERT(first_item->bytes_reserved == 0);
678 list_add_tail(&first_item->tree_list, &item_list);
679 batch.total_data_size = first_data_size;
681 total_size = first_data_size + sizeof(struct btrfs_item);
687 next = __btrfs_next_delayed_item(curr);
692 * We cannot allow gaps in the key space if we're doing log
695 if (continuous_keys_only && (next->index != curr->index + 1))
698 ASSERT(next->bytes_reserved == 0);
700 next_size = next->data_len + sizeof(struct btrfs_item);
701 if (total_size + next_size > max_size)
704 list_add_tail(&next->tree_list, &item_list);
706 total_size += next_size;
707 batch.total_data_size += next->data_len;
712 first_key.objectid = node->inode_id;
713 first_key.type = BTRFS_DIR_INDEX_KEY;
714 first_key.offset = first_item->index;
715 batch.keys = &first_key;
716 batch.data_sizes = &first_data_size;
718 struct btrfs_key *ins_keys;
722 ins_data = kmalloc(batch.nr * sizeof(u32) +
723 batch.nr * sizeof(struct btrfs_key), GFP_NOFS);
728 ins_sizes = (u32 *)ins_data;
729 ins_keys = (struct btrfs_key *)(ins_data + batch.nr * sizeof(u32));
730 batch.keys = ins_keys;
731 batch.data_sizes = ins_sizes;
732 list_for_each_entry(curr, &item_list, tree_list) {
733 ins_keys[i].objectid = node->inode_id;
734 ins_keys[i].type = BTRFS_DIR_INDEX_KEY;
735 ins_keys[i].offset = curr->index;
736 ins_sizes[i] = curr->data_len;
741 ret = btrfs_insert_empty_items(trans, root, path, &batch);
745 list_for_each_entry(curr, &item_list, tree_list) {
748 data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
749 write_extent_buffer(path->nodes[0], &curr->data,
750 (unsigned long)data_ptr, curr->data_len);
755 * Now release our path before releasing the delayed items and their
756 * metadata reservations, so that we don't block other tasks for more
759 btrfs_release_path(path);
761 ASSERT(node->index_item_leaves > 0);
764 * For normal operations we will batch an entire leaf's worth of delayed
765 * items, so if there are more items to process we can decrement
766 * index_item_leaves by 1 as we inserted 1 leaf's worth of items.
768 * However for log replay we may not have inserted an entire leaf's
769 * worth of items, we may have not had continuous items, so decrementing
770 * here would mess up the index_item_leaves accounting. For this case
771 * only clean up the accounting when there are no items left.
773 if (next && !continuous_keys_only) {
775 * We inserted one batch of items into a leaf a there are more
776 * items to flush in a future batch, now release one unit of
777 * metadata space from the delayed block reserve, corresponding
778 * the leaf we just flushed to.
780 btrfs_delayed_item_release_leaves(node, 1);
781 node->index_item_leaves--;
784 * There are no more items to insert. We can have a number of
785 * reserved leaves > 1 here - this happens when many dir index
786 * items are added and then removed before they are flushed (file
787 * names with a very short life, never span a transaction). So
788 * release all remaining leaves.
790 btrfs_delayed_item_release_leaves(node, node->index_item_leaves);
791 node->index_item_leaves = 0;
794 list_for_each_entry_safe(curr, next, &item_list, tree_list) {
795 list_del(&curr->tree_list);
796 btrfs_release_delayed_item(curr);
803 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
804 struct btrfs_path *path,
805 struct btrfs_root *root,
806 struct btrfs_delayed_node *node)
811 struct btrfs_delayed_item *curr;
813 mutex_lock(&node->mutex);
814 curr = __btrfs_first_delayed_insertion_item(node);
816 mutex_unlock(&node->mutex);
819 ret = btrfs_insert_delayed_item(trans, root, path, curr);
820 mutex_unlock(&node->mutex);
826 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_path *path,
829 struct btrfs_delayed_item *item)
831 const u64 ino = item->delayed_node->inode_id;
832 struct btrfs_fs_info *fs_info = root->fs_info;
833 struct btrfs_delayed_item *curr, *next;
834 struct extent_buffer *leaf = path->nodes[0];
835 LIST_HEAD(batch_list);
836 int nitems, slot, last_slot;
838 u64 total_reserved_size = item->bytes_reserved;
840 ASSERT(leaf != NULL);
842 slot = path->slots[0];
843 last_slot = btrfs_header_nritems(leaf) - 1;
845 * Our caller always gives us a path pointing to an existing item, so
846 * this can not happen.
848 ASSERT(slot <= last_slot);
849 if (WARN_ON(slot > last_slot))
854 list_add_tail(&curr->tree_list, &batch_list);
857 * Keep checking if the next delayed item matches the next item in the
858 * leaf - if so, we can add it to the batch of items to delete from the
861 while (slot < last_slot) {
862 struct btrfs_key key;
864 next = __btrfs_next_delayed_item(curr);
869 btrfs_item_key_to_cpu(leaf, &key, slot);
870 if (key.objectid != ino ||
871 key.type != BTRFS_DIR_INDEX_KEY ||
872 key.offset != next->index)
876 list_add_tail(&curr->tree_list, &batch_list);
877 total_reserved_size += curr->bytes_reserved;
880 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
884 /* In case of BTRFS_FS_LOG_RECOVERING items won't have reserved space */
885 if (total_reserved_size > 0) {
887 * Check btrfs_delayed_item_reserve_metadata() to see why we
888 * don't need to release/reserve qgroup space.
890 trace_btrfs_space_reservation(fs_info, "delayed_item", ino,
891 total_reserved_size, 0);
892 btrfs_block_rsv_release(fs_info, &fs_info->delayed_block_rsv,
893 total_reserved_size, NULL);
896 list_for_each_entry_safe(curr, next, &batch_list, tree_list) {
897 list_del(&curr->tree_list);
898 btrfs_release_delayed_item(curr);
904 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
905 struct btrfs_path *path,
906 struct btrfs_root *root,
907 struct btrfs_delayed_node *node)
909 struct btrfs_key key;
912 key.objectid = node->inode_id;
913 key.type = BTRFS_DIR_INDEX_KEY;
916 struct btrfs_delayed_item *item;
918 mutex_lock(&node->mutex);
919 item = __btrfs_first_delayed_deletion_item(node);
921 mutex_unlock(&node->mutex);
925 key.offset = item->index;
926 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
929 * There's no matching item in the leaf. This means we
930 * have already deleted this item in a past run of the
931 * delayed items. We ignore errors when running delayed
932 * items from an async context, through a work queue job
933 * running btrfs_async_run_delayed_root(), and don't
934 * release delayed items that failed to complete. This
935 * is because we will retry later, and at transaction
936 * commit time we always run delayed items and will
937 * then deal with errors if they fail to run again.
939 * So just release delayed items for which we can't find
940 * an item in the tree, and move to the next item.
942 btrfs_release_path(path);
943 btrfs_release_delayed_item(item);
945 } else if (ret == 0) {
946 ret = btrfs_batch_delete_items(trans, root, path, item);
947 btrfs_release_path(path);
951 * We unlock and relock on each iteration, this is to prevent
952 * blocking other tasks for too long while we are being run from
953 * the async context (work queue job). Those tasks are typically
954 * running system calls like creat/mkdir/rename/unlink/etc which
955 * need to add delayed items to this delayed node.
957 mutex_unlock(&node->mutex);
963 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
965 struct btrfs_delayed_root *delayed_root;
968 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
969 BUG_ON(!delayed_node->root);
970 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
971 delayed_node->count--;
973 delayed_root = delayed_node->root->fs_info->delayed_root;
974 finish_one_item(delayed_root);
978 static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
981 if (test_and_clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) {
982 struct btrfs_delayed_root *delayed_root;
984 ASSERT(delayed_node->root);
985 delayed_node->count--;
987 delayed_root = delayed_node->root->fs_info->delayed_root;
988 finish_one_item(delayed_root);
992 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
993 struct btrfs_root *root,
994 struct btrfs_path *path,
995 struct btrfs_delayed_node *node)
997 struct btrfs_fs_info *fs_info = root->fs_info;
998 struct btrfs_key key;
999 struct btrfs_inode_item *inode_item;
1000 struct extent_buffer *leaf;
1004 key.objectid = node->inode_id;
1005 key.type = BTRFS_INODE_ITEM_KEY;
1008 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1013 ret = btrfs_lookup_inode(trans, root, path, &key, mod);
1019 leaf = path->nodes[0];
1020 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1021 struct btrfs_inode_item);
1022 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1023 sizeof(struct btrfs_inode_item));
1024 btrfs_mark_buffer_dirty(leaf);
1026 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1030 if (path->slots[0] >= btrfs_header_nritems(leaf))
1033 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1034 if (key.objectid != node->inode_id)
1037 if (key.type != BTRFS_INODE_REF_KEY &&
1038 key.type != BTRFS_INODE_EXTREF_KEY)
1042 * Delayed iref deletion is for the inode who has only one link,
1043 * so there is only one iref. The case that several irefs are
1044 * in the same item doesn't exist.
1046 btrfs_del_item(trans, root, path);
1048 btrfs_release_delayed_iref(node);
1049 btrfs_release_path(path);
1051 btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
1052 btrfs_release_delayed_inode(node);
1055 * If we fail to update the delayed inode we need to abort the
1056 * transaction, because we could leave the inode with the improper
1059 if (ret && ret != -ENOENT)
1060 btrfs_abort_transaction(trans, ret);
1065 btrfs_release_path(path);
1067 key.type = BTRFS_INODE_EXTREF_KEY;
1070 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1076 leaf = path->nodes[0];
1081 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root,
1083 struct btrfs_path *path,
1084 struct btrfs_delayed_node *node)
1088 mutex_lock(&node->mutex);
1089 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
1090 mutex_unlock(&node->mutex);
1094 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1095 mutex_unlock(&node->mutex);
1100 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1101 struct btrfs_path *path,
1102 struct btrfs_delayed_node *node)
1106 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1110 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1114 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1119 * Called when committing the transaction.
1120 * Returns 0 on success.
1121 * Returns < 0 on error and returns with an aborted transaction with any
1122 * outstanding delayed items cleaned up.
1124 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
1126 struct btrfs_fs_info *fs_info = trans->fs_info;
1127 struct btrfs_delayed_root *delayed_root;
1128 struct btrfs_delayed_node *curr_node, *prev_node;
1129 struct btrfs_path *path;
1130 struct btrfs_block_rsv *block_rsv;
1132 bool count = (nr > 0);
1134 if (TRANS_ABORTED(trans))
1137 path = btrfs_alloc_path();
1141 block_rsv = trans->block_rsv;
1142 trans->block_rsv = &fs_info->delayed_block_rsv;
1144 delayed_root = fs_info->delayed_root;
1146 curr_node = btrfs_first_delayed_node(delayed_root);
1147 while (curr_node && (!count || nr--)) {
1148 ret = __btrfs_commit_inode_delayed_items(trans, path,
1151 btrfs_release_delayed_node(curr_node);
1153 btrfs_abort_transaction(trans, ret);
1157 prev_node = curr_node;
1158 curr_node = btrfs_next_delayed_node(curr_node);
1159 btrfs_release_delayed_node(prev_node);
1163 btrfs_release_delayed_node(curr_node);
1164 btrfs_free_path(path);
1165 trans->block_rsv = block_rsv;
1170 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
1172 return __btrfs_run_delayed_items(trans, -1);
1175 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
1177 return __btrfs_run_delayed_items(trans, nr);
1180 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1181 struct btrfs_inode *inode)
1183 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1184 struct btrfs_path *path;
1185 struct btrfs_block_rsv *block_rsv;
1191 mutex_lock(&delayed_node->mutex);
1192 if (!delayed_node->count) {
1193 mutex_unlock(&delayed_node->mutex);
1194 btrfs_release_delayed_node(delayed_node);
1197 mutex_unlock(&delayed_node->mutex);
1199 path = btrfs_alloc_path();
1201 btrfs_release_delayed_node(delayed_node);
1205 block_rsv = trans->block_rsv;
1206 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1208 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1210 btrfs_release_delayed_node(delayed_node);
1211 btrfs_free_path(path);
1212 trans->block_rsv = block_rsv;
1217 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
1219 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1220 struct btrfs_trans_handle *trans;
1221 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1222 struct btrfs_path *path;
1223 struct btrfs_block_rsv *block_rsv;
1229 mutex_lock(&delayed_node->mutex);
1230 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1231 mutex_unlock(&delayed_node->mutex);
1232 btrfs_release_delayed_node(delayed_node);
1235 mutex_unlock(&delayed_node->mutex);
1237 trans = btrfs_join_transaction(delayed_node->root);
1238 if (IS_ERR(trans)) {
1239 ret = PTR_ERR(trans);
1243 path = btrfs_alloc_path();
1249 block_rsv = trans->block_rsv;
1250 trans->block_rsv = &fs_info->delayed_block_rsv;
1252 mutex_lock(&delayed_node->mutex);
1253 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
1254 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1255 path, delayed_node);
1258 mutex_unlock(&delayed_node->mutex);
1260 btrfs_free_path(path);
1261 trans->block_rsv = block_rsv;
1263 btrfs_end_transaction(trans);
1264 btrfs_btree_balance_dirty(fs_info);
1266 btrfs_release_delayed_node(delayed_node);
1271 void btrfs_remove_delayed_node(struct btrfs_inode *inode)
1273 struct btrfs_delayed_node *delayed_node;
1275 delayed_node = READ_ONCE(inode->delayed_node);
1279 inode->delayed_node = NULL;
1280 btrfs_release_delayed_node(delayed_node);
1283 struct btrfs_async_delayed_work {
1284 struct btrfs_delayed_root *delayed_root;
1286 struct btrfs_work work;
1289 static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1291 struct btrfs_async_delayed_work *async_work;
1292 struct btrfs_delayed_root *delayed_root;
1293 struct btrfs_trans_handle *trans;
1294 struct btrfs_path *path;
1295 struct btrfs_delayed_node *delayed_node = NULL;
1296 struct btrfs_root *root;
1297 struct btrfs_block_rsv *block_rsv;
1300 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1301 delayed_root = async_work->delayed_root;
1303 path = btrfs_alloc_path();
1308 if (atomic_read(&delayed_root->items) <
1309 BTRFS_DELAYED_BACKGROUND / 2)
1312 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1316 root = delayed_node->root;
1318 trans = btrfs_join_transaction(root);
1319 if (IS_ERR(trans)) {
1320 btrfs_release_path(path);
1321 btrfs_release_prepared_delayed_node(delayed_node);
1326 block_rsv = trans->block_rsv;
1327 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1329 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1331 trans->block_rsv = block_rsv;
1332 btrfs_end_transaction(trans);
1333 btrfs_btree_balance_dirty_nodelay(root->fs_info);
1335 btrfs_release_path(path);
1336 btrfs_release_prepared_delayed_node(delayed_node);
1339 } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
1340 || total_done < async_work->nr);
1342 btrfs_free_path(path);
1344 wake_up(&delayed_root->wait);
1349 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1350 struct btrfs_fs_info *fs_info, int nr)
1352 struct btrfs_async_delayed_work *async_work;
1354 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1358 async_work->delayed_root = delayed_root;
1359 btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL,
1361 async_work->nr = nr;
1363 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
1367 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
1369 WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
1372 static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1374 int val = atomic_read(&delayed_root->items_seq);
1376 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
1379 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1385 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1387 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1389 if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
1390 btrfs_workqueue_normal_congested(fs_info->delayed_workers))
1393 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1397 seq = atomic_read(&delayed_root->items_seq);
1399 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
1403 wait_event_interruptible(delayed_root->wait,
1404 could_end_wait(delayed_root, seq));
1408 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
1411 /* Will return 0 or -ENOMEM */
1412 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1413 const char *name, int name_len,
1414 struct btrfs_inode *dir,
1415 struct btrfs_disk_key *disk_key, u8 type,
1418 struct btrfs_fs_info *fs_info = trans->fs_info;
1419 const unsigned int leaf_data_size = BTRFS_LEAF_DATA_SIZE(fs_info);
1420 struct btrfs_delayed_node *delayed_node;
1421 struct btrfs_delayed_item *delayed_item;
1422 struct btrfs_dir_item *dir_item;
1423 bool reserve_leaf_space;
1427 delayed_node = btrfs_get_or_create_delayed_node(dir);
1428 if (IS_ERR(delayed_node))
1429 return PTR_ERR(delayed_node);
1431 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len,
1433 BTRFS_DELAYED_INSERTION_ITEM);
1434 if (!delayed_item) {
1439 delayed_item->index = index;
1441 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1442 dir_item->location = *disk_key;
1443 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1444 btrfs_set_stack_dir_data_len(dir_item, 0);
1445 btrfs_set_stack_dir_name_len(dir_item, name_len);
1446 btrfs_set_stack_dir_type(dir_item, type);
1447 memcpy((char *)(dir_item + 1), name, name_len);
1449 data_len = delayed_item->data_len + sizeof(struct btrfs_item);
1451 mutex_lock(&delayed_node->mutex);
1453 if (delayed_node->index_item_leaves == 0 ||
1454 delayed_node->curr_index_batch_size + data_len > leaf_data_size) {
1455 delayed_node->curr_index_batch_size = data_len;
1456 reserve_leaf_space = true;
1458 delayed_node->curr_index_batch_size += data_len;
1459 reserve_leaf_space = false;
1462 if (reserve_leaf_space) {
1463 ret = btrfs_delayed_item_reserve_metadata(trans, delayed_item);
1465 * Space was reserved for a dir index item insertion when we
1466 * started the transaction, so getting a failure here should be
1470 mutex_unlock(&delayed_node->mutex);
1471 btrfs_release_delayed_item(delayed_item);
1475 delayed_node->index_item_leaves++;
1476 } else if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
1477 const u64 bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
1480 * Adding the new dir index item does not require touching another
1481 * leaf, so we can release 1 unit of metadata that was previously
1482 * reserved when starting the transaction. This applies only to
1483 * the case where we had a transaction start and excludes the
1484 * transaction join case (when replaying log trees).
1486 trace_btrfs_space_reservation(fs_info, "transaction",
1487 trans->transid, bytes, 0);
1488 btrfs_block_rsv_release(fs_info, trans->block_rsv, bytes, NULL);
1489 ASSERT(trans->bytes_reserved >= bytes);
1490 trans->bytes_reserved -= bytes;
1493 ret = __btrfs_add_delayed_item(delayed_node, delayed_item);
1494 if (unlikely(ret)) {
1495 btrfs_err(trans->fs_info,
1496 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1497 name_len, name, delayed_node->root->root_key.objectid,
1498 delayed_node->inode_id, ret);
1501 mutex_unlock(&delayed_node->mutex);
1504 btrfs_release_delayed_node(delayed_node);
1508 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1509 struct btrfs_delayed_node *node,
1512 struct btrfs_delayed_item *item;
1514 mutex_lock(&node->mutex);
1515 item = __btrfs_lookup_delayed_item(&node->ins_root.rb_root, index);
1517 mutex_unlock(&node->mutex);
1522 * For delayed items to insert, we track reserved metadata bytes based
1523 * on the number of leaves that we will use.
1524 * See btrfs_insert_delayed_dir_index() and
1525 * btrfs_delayed_item_reserve_metadata()).
1527 ASSERT(item->bytes_reserved == 0);
1528 ASSERT(node->index_item_leaves > 0);
1531 * If there's only one leaf reserved, we can decrement this item from the
1532 * current batch, otherwise we can not because we don't know which leaf
1533 * it belongs to. With the current limit on delayed items, we rarely
1534 * accumulate enough dir index items to fill more than one leaf (even
1535 * when using a leaf size of 4K).
1537 if (node->index_item_leaves == 1) {
1538 const u32 data_len = item->data_len + sizeof(struct btrfs_item);
1540 ASSERT(node->curr_index_batch_size >= data_len);
1541 node->curr_index_batch_size -= data_len;
1544 btrfs_release_delayed_item(item);
1546 /* If we now have no more dir index items, we can release all leaves. */
1547 if (RB_EMPTY_ROOT(&node->ins_root.rb_root)) {
1548 btrfs_delayed_item_release_leaves(node, node->index_item_leaves);
1549 node->index_item_leaves = 0;
1552 mutex_unlock(&node->mutex);
1556 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1557 struct btrfs_inode *dir, u64 index)
1559 struct btrfs_delayed_node *node;
1560 struct btrfs_delayed_item *item;
1563 node = btrfs_get_or_create_delayed_node(dir);
1565 return PTR_ERR(node);
1567 ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node, index);
1571 item = btrfs_alloc_delayed_item(0, node, BTRFS_DELAYED_DELETION_ITEM);
1577 item->index = index;
1579 ret = btrfs_delayed_item_reserve_metadata(trans, item);
1581 * we have reserved enough space when we start a new transaction,
1582 * so reserving metadata failure is impossible.
1585 btrfs_err(trans->fs_info,
1586 "metadata reservation failed for delayed dir item deltiona, should have been reserved");
1587 btrfs_release_delayed_item(item);
1591 mutex_lock(&node->mutex);
1592 ret = __btrfs_add_delayed_item(node, item);
1593 if (unlikely(ret)) {
1594 btrfs_err(trans->fs_info,
1595 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1596 index, node->root->root_key.objectid,
1597 node->inode_id, ret);
1598 btrfs_delayed_item_release_metadata(dir->root, item);
1599 btrfs_release_delayed_item(item);
1601 mutex_unlock(&node->mutex);
1603 btrfs_release_delayed_node(node);
1607 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
1609 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1615 * Since we have held i_mutex of this directory, it is impossible that
1616 * a new directory index is added into the delayed node and index_cnt
1617 * is updated now. So we needn't lock the delayed node.
1619 if (!delayed_node->index_cnt) {
1620 btrfs_release_delayed_node(delayed_node);
1624 inode->index_cnt = delayed_node->index_cnt;
1625 btrfs_release_delayed_node(delayed_node);
1629 bool btrfs_readdir_get_delayed_items(struct inode *inode,
1630 struct list_head *ins_list,
1631 struct list_head *del_list)
1633 struct btrfs_delayed_node *delayed_node;
1634 struct btrfs_delayed_item *item;
1636 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1641 * We can only do one readdir with delayed items at a time because of
1642 * item->readdir_list.
1644 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
1645 btrfs_inode_lock(inode, 0);
1647 mutex_lock(&delayed_node->mutex);
1648 item = __btrfs_first_delayed_insertion_item(delayed_node);
1650 refcount_inc(&item->refs);
1651 list_add_tail(&item->readdir_list, ins_list);
1652 item = __btrfs_next_delayed_item(item);
1655 item = __btrfs_first_delayed_deletion_item(delayed_node);
1657 refcount_inc(&item->refs);
1658 list_add_tail(&item->readdir_list, del_list);
1659 item = __btrfs_next_delayed_item(item);
1661 mutex_unlock(&delayed_node->mutex);
1663 * This delayed node is still cached in the btrfs inode, so refs
1664 * must be > 1 now, and we needn't check it is going to be freed
1667 * Besides that, this function is used to read dir, we do not
1668 * insert/delete delayed items in this period. So we also needn't
1669 * requeue or dequeue this delayed node.
1671 refcount_dec(&delayed_node->refs);
1676 void btrfs_readdir_put_delayed_items(struct inode *inode,
1677 struct list_head *ins_list,
1678 struct list_head *del_list)
1680 struct btrfs_delayed_item *curr, *next;
1682 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1683 list_del(&curr->readdir_list);
1684 if (refcount_dec_and_test(&curr->refs))
1688 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1689 list_del(&curr->readdir_list);
1690 if (refcount_dec_and_test(&curr->refs))
1695 * The VFS is going to do up_read(), so we need to downgrade back to a
1698 downgrade_write(&inode->i_rwsem);
1701 int btrfs_should_delete_dir_index(struct list_head *del_list,
1704 struct btrfs_delayed_item *curr;
1707 list_for_each_entry(curr, del_list, readdir_list) {
1708 if (curr->index > index)
1710 if (curr->index == index) {
1719 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1722 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1723 struct list_head *ins_list)
1725 struct btrfs_dir_item *di;
1726 struct btrfs_delayed_item *curr, *next;
1727 struct btrfs_key location;
1731 unsigned char d_type;
1733 if (list_empty(ins_list))
1737 * Changing the data of the delayed item is impossible. So
1738 * we needn't lock them. And we have held i_mutex of the
1739 * directory, nobody can delete any directory indexes now.
1741 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1742 list_del(&curr->readdir_list);
1744 if (curr->index < ctx->pos) {
1745 if (refcount_dec_and_test(&curr->refs))
1750 ctx->pos = curr->index;
1752 di = (struct btrfs_dir_item *)curr->data;
1753 name = (char *)(di + 1);
1754 name_len = btrfs_stack_dir_name_len(di);
1756 d_type = fs_ftype_to_dtype(di->type);
1757 btrfs_disk_key_to_cpu(&location, &di->location);
1759 over = !dir_emit(ctx, name, name_len,
1760 location.objectid, d_type);
1762 if (refcount_dec_and_test(&curr->refs))
1772 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1773 struct btrfs_inode_item *inode_item,
1774 struct inode *inode)
1778 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1779 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1780 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1781 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1782 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1783 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1784 btrfs_set_stack_inode_generation(inode_item,
1785 BTRFS_I(inode)->generation);
1786 btrfs_set_stack_inode_sequence(inode_item,
1787 inode_peek_iversion(inode));
1788 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1789 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1790 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
1791 BTRFS_I(inode)->ro_flags);
1792 btrfs_set_stack_inode_flags(inode_item, flags);
1793 btrfs_set_stack_inode_block_group(inode_item, 0);
1795 btrfs_set_stack_timespec_sec(&inode_item->atime,
1796 inode->i_atime.tv_sec);
1797 btrfs_set_stack_timespec_nsec(&inode_item->atime,
1798 inode->i_atime.tv_nsec);
1800 btrfs_set_stack_timespec_sec(&inode_item->mtime,
1801 inode->i_mtime.tv_sec);
1802 btrfs_set_stack_timespec_nsec(&inode_item->mtime,
1803 inode->i_mtime.tv_nsec);
1805 btrfs_set_stack_timespec_sec(&inode_item->ctime,
1806 inode->i_ctime.tv_sec);
1807 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
1808 inode->i_ctime.tv_nsec);
1810 btrfs_set_stack_timespec_sec(&inode_item->otime,
1811 BTRFS_I(inode)->i_otime.tv_sec);
1812 btrfs_set_stack_timespec_nsec(&inode_item->otime,
1813 BTRFS_I(inode)->i_otime.tv_nsec);
1816 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1818 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1819 struct btrfs_delayed_node *delayed_node;
1820 struct btrfs_inode_item *inode_item;
1822 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1826 mutex_lock(&delayed_node->mutex);
1827 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1828 mutex_unlock(&delayed_node->mutex);
1829 btrfs_release_delayed_node(delayed_node);
1833 inode_item = &delayed_node->inode_item;
1835 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1836 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1837 btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
1838 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
1839 round_up(i_size_read(inode), fs_info->sectorsize));
1840 inode->i_mode = btrfs_stack_inode_mode(inode_item);
1841 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1842 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1843 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1844 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1846 inode_set_iversion_queried(inode,
1847 btrfs_stack_inode_sequence(inode_item));
1849 *rdev = btrfs_stack_inode_rdev(inode_item);
1850 btrfs_inode_split_flags(btrfs_stack_inode_flags(inode_item),
1851 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
1853 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
1854 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
1856 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
1857 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
1859 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
1860 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
1862 BTRFS_I(inode)->i_otime.tv_sec =
1863 btrfs_stack_timespec_sec(&inode_item->otime);
1864 BTRFS_I(inode)->i_otime.tv_nsec =
1865 btrfs_stack_timespec_nsec(&inode_item->otime);
1867 inode->i_generation = BTRFS_I(inode)->generation;
1868 BTRFS_I(inode)->index_cnt = (u64)-1;
1870 mutex_unlock(&delayed_node->mutex);
1871 btrfs_release_delayed_node(delayed_node);
1875 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *root,
1877 struct btrfs_inode *inode)
1879 struct btrfs_delayed_node *delayed_node;
1882 delayed_node = btrfs_get_or_create_delayed_node(inode);
1883 if (IS_ERR(delayed_node))
1884 return PTR_ERR(delayed_node);
1886 mutex_lock(&delayed_node->mutex);
1887 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1888 fill_stack_inode_item(trans, &delayed_node->inode_item,
1893 ret = btrfs_delayed_inode_reserve_metadata(trans, root, delayed_node);
1897 fill_stack_inode_item(trans, &delayed_node->inode_item, &inode->vfs_inode);
1898 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
1899 delayed_node->count++;
1900 atomic_inc(&root->fs_info->delayed_root->items);
1902 mutex_unlock(&delayed_node->mutex);
1903 btrfs_release_delayed_node(delayed_node);
1907 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
1909 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1910 struct btrfs_delayed_node *delayed_node;
1913 * we don't do delayed inode updates during log recovery because it
1914 * leads to enospc problems. This means we also can't do
1915 * delayed inode refs
1917 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
1920 delayed_node = btrfs_get_or_create_delayed_node(inode);
1921 if (IS_ERR(delayed_node))
1922 return PTR_ERR(delayed_node);
1925 * We don't reserve space for inode ref deletion is because:
1926 * - We ONLY do async inode ref deletion for the inode who has only
1927 * one link(i_nlink == 1), it means there is only one inode ref.
1928 * And in most case, the inode ref and the inode item are in the
1929 * same leaf, and we will deal with them at the same time.
1930 * Since we are sure we will reserve the space for the inode item,
1931 * it is unnecessary to reserve space for inode ref deletion.
1932 * - If the inode ref and the inode item are not in the same leaf,
1933 * We also needn't worry about enospc problem, because we reserve
1934 * much more space for the inode update than it needs.
1935 * - At the worst, we can steal some space from the global reservation.
1938 mutex_lock(&delayed_node->mutex);
1939 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1942 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1943 delayed_node->count++;
1944 atomic_inc(&fs_info->delayed_root->items);
1946 mutex_unlock(&delayed_node->mutex);
1947 btrfs_release_delayed_node(delayed_node);
1951 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1953 struct btrfs_root *root = delayed_node->root;
1954 struct btrfs_fs_info *fs_info = root->fs_info;
1955 struct btrfs_delayed_item *curr_item, *prev_item;
1957 mutex_lock(&delayed_node->mutex);
1958 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1960 prev_item = curr_item;
1961 curr_item = __btrfs_next_delayed_item(prev_item);
1962 btrfs_release_delayed_item(prev_item);
1965 if (delayed_node->index_item_leaves > 0) {
1966 btrfs_delayed_item_release_leaves(delayed_node,
1967 delayed_node->index_item_leaves);
1968 delayed_node->index_item_leaves = 0;
1971 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1973 btrfs_delayed_item_release_metadata(root, curr_item);
1974 prev_item = curr_item;
1975 curr_item = __btrfs_next_delayed_item(prev_item);
1976 btrfs_release_delayed_item(prev_item);
1979 btrfs_release_delayed_iref(delayed_node);
1981 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1982 btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false);
1983 btrfs_release_delayed_inode(delayed_node);
1985 mutex_unlock(&delayed_node->mutex);
1988 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
1990 struct btrfs_delayed_node *delayed_node;
1992 delayed_node = btrfs_get_delayed_node(inode);
1996 __btrfs_kill_delayed_node(delayed_node);
1997 btrfs_release_delayed_node(delayed_node);
2000 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
2003 struct btrfs_delayed_node *delayed_nodes[8];
2007 spin_lock(&root->inode_lock);
2008 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
2009 (void **)delayed_nodes, inode_id,
2010 ARRAY_SIZE(delayed_nodes));
2012 spin_unlock(&root->inode_lock);
2016 inode_id = delayed_nodes[n - 1]->inode_id + 1;
2017 for (i = 0; i < n; i++) {
2019 * Don't increase refs in case the node is dead and
2020 * about to be removed from the tree in the loop below
2022 if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
2023 delayed_nodes[i] = NULL;
2025 spin_unlock(&root->inode_lock);
2027 for (i = 0; i < n; i++) {
2028 if (!delayed_nodes[i])
2030 __btrfs_kill_delayed_node(delayed_nodes[i]);
2031 btrfs_release_delayed_node(delayed_nodes[i]);
2036 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
2038 struct btrfs_delayed_node *curr_node, *prev_node;
2040 curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
2042 __btrfs_kill_delayed_node(curr_node);
2044 prev_node = curr_node;
2045 curr_node = btrfs_next_delayed_node(curr_node);
2046 btrfs_release_delayed_node(prev_node);
2050 void btrfs_log_get_delayed_items(struct btrfs_inode *inode,
2051 struct list_head *ins_list,
2052 struct list_head *del_list)
2054 struct btrfs_delayed_node *node;
2055 struct btrfs_delayed_item *item;
2057 node = btrfs_get_delayed_node(inode);
2061 mutex_lock(&node->mutex);
2062 item = __btrfs_first_delayed_insertion_item(node);
2065 * It's possible that the item is already in a log list. This
2066 * can happen in case two tasks are trying to log the same
2067 * directory. For example if we have tasks A and task B:
2069 * Task A collected the delayed items into a log list while
2070 * under the inode's log_mutex (at btrfs_log_inode()), but it
2071 * only releases the items after logging the inodes they point
2072 * to (if they are new inodes), which happens after unlocking
2075 * Task B enters btrfs_log_inode() and acquires the log_mutex
2076 * of the same directory inode, before task B releases the
2077 * delayed items. This can happen for example when logging some
2078 * inode we need to trigger logging of its parent directory, so
2079 * logging two files that have the same parent directory can
2082 * If this happens, just ignore delayed items already in a log
2083 * list. All the tasks logging the directory are under a log
2084 * transaction and whichever finishes first can not sync the log
2085 * before the other completes and leaves the log transaction.
2087 if (!item->logged && list_empty(&item->log_list)) {
2088 refcount_inc(&item->refs);
2089 list_add_tail(&item->log_list, ins_list);
2091 item = __btrfs_next_delayed_item(item);
2094 item = __btrfs_first_delayed_deletion_item(node);
2096 /* It may be non-empty, for the same reason mentioned above. */
2097 if (!item->logged && list_empty(&item->log_list)) {
2098 refcount_inc(&item->refs);
2099 list_add_tail(&item->log_list, del_list);
2101 item = __btrfs_next_delayed_item(item);
2103 mutex_unlock(&node->mutex);
2106 * We are called during inode logging, which means the inode is in use
2107 * and can not be evicted before we finish logging the inode. So we never
2108 * have the last reference on the delayed inode.
2109 * Also, we don't use btrfs_release_delayed_node() because that would
2110 * requeue the delayed inode (change its order in the list of prepared
2111 * nodes) and we don't want to do such change because we don't create or
2112 * delete delayed items.
2114 ASSERT(refcount_read(&node->refs) > 1);
2115 refcount_dec(&node->refs);
2118 void btrfs_log_put_delayed_items(struct btrfs_inode *inode,
2119 struct list_head *ins_list,
2120 struct list_head *del_list)
2122 struct btrfs_delayed_node *node;
2123 struct btrfs_delayed_item *item;
2124 struct btrfs_delayed_item *next;
2126 node = btrfs_get_delayed_node(inode);
2130 mutex_lock(&node->mutex);
2132 list_for_each_entry_safe(item, next, ins_list, log_list) {
2133 item->logged = true;
2134 list_del_init(&item->log_list);
2135 if (refcount_dec_and_test(&item->refs))
2139 list_for_each_entry_safe(item, next, del_list, log_list) {
2140 item->logged = true;
2141 list_del_init(&item->log_list);
2142 if (refcount_dec_and_test(&item->refs))
2146 mutex_unlock(&node->mutex);
2149 * We are called during inode logging, which means the inode is in use
2150 * and can not be evicted before we finish logging the inode. So we never
2151 * have the last reference on the delayed inode.
2152 * Also, we don't use btrfs_release_delayed_node() because that would
2153 * requeue the delayed inode (change its order in the list of prepared
2154 * nodes) and we don't want to do such change because we don't create or
2155 * delete delayed items.
2157 ASSERT(refcount_read(&node->refs) > 1);
2158 refcount_dec(&node->refs);