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"
17 #define BTRFS_DELAYED_WRITEBACK 512
18 #define BTRFS_DELAYED_BACKGROUND 128
19 #define BTRFS_DELAYED_BATCH 16
21 static struct kmem_cache *delayed_node_cache;
23 int __init btrfs_delayed_inode_init(void)
25 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
26 sizeof(struct btrfs_delayed_node),
30 if (!delayed_node_cache)
35 void __cold btrfs_delayed_inode_exit(void)
37 kmem_cache_destroy(delayed_node_cache);
40 static inline void btrfs_init_delayed_node(
41 struct btrfs_delayed_node *delayed_node,
42 struct btrfs_root *root, u64 inode_id)
44 delayed_node->root = root;
45 delayed_node->inode_id = inode_id;
46 refcount_set(&delayed_node->refs, 0);
47 delayed_node->ins_root = RB_ROOT_CACHED;
48 delayed_node->del_root = RB_ROOT_CACHED;
49 mutex_init(&delayed_node->mutex);
50 INIT_LIST_HEAD(&delayed_node->n_list);
51 INIT_LIST_HEAD(&delayed_node->p_list);
54 static inline int btrfs_is_continuous_delayed_item(
55 struct btrfs_delayed_item *item1,
56 struct btrfs_delayed_item *item2)
58 if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
59 item1->key.objectid == item2->key.objectid &&
60 item1->key.type == item2->key.type &&
61 item1->key.offset + 1 == item2->key.offset)
66 static struct btrfs_delayed_node *btrfs_get_delayed_node(
67 struct btrfs_inode *btrfs_inode)
69 struct btrfs_root *root = btrfs_inode->root;
70 u64 ino = btrfs_ino(btrfs_inode);
71 struct btrfs_delayed_node *node;
73 node = READ_ONCE(btrfs_inode->delayed_node);
75 refcount_inc(&node->refs);
79 spin_lock(&root->inode_lock);
80 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
83 if (btrfs_inode->delayed_node) {
84 refcount_inc(&node->refs); /* can be accessed */
85 BUG_ON(btrfs_inode->delayed_node != node);
86 spin_unlock(&root->inode_lock);
91 * It's possible that we're racing into the middle of removing
92 * this node from the radix tree. In this case, the refcount
93 * was zero and it should never go back to one. Just return
94 * NULL like it was never in the radix at all; our release
95 * function is in the process of removing it.
97 * Some implementations of refcount_inc refuse to bump the
98 * refcount once it has hit zero. If we don't do this dance
99 * here, refcount_inc() may decide to just WARN_ONCE() instead
100 * of actually bumping the refcount.
102 * If this node is properly in the radix, we want to bump the
103 * refcount twice, once for the inode and once for this get
106 if (refcount_inc_not_zero(&node->refs)) {
107 refcount_inc(&node->refs);
108 btrfs_inode->delayed_node = node;
113 spin_unlock(&root->inode_lock);
116 spin_unlock(&root->inode_lock);
121 /* Will return either the node or PTR_ERR(-ENOMEM) */
122 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
123 struct btrfs_inode *btrfs_inode)
125 struct btrfs_delayed_node *node;
126 struct btrfs_root *root = btrfs_inode->root;
127 u64 ino = btrfs_ino(btrfs_inode);
131 node = btrfs_get_delayed_node(btrfs_inode);
135 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
137 return ERR_PTR(-ENOMEM);
138 btrfs_init_delayed_node(node, root, ino);
140 /* cached in the btrfs inode and can be accessed */
141 refcount_set(&node->refs, 2);
143 ret = radix_tree_preload(GFP_NOFS);
145 kmem_cache_free(delayed_node_cache, node);
149 spin_lock(&root->inode_lock);
150 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
151 if (ret == -EEXIST) {
152 spin_unlock(&root->inode_lock);
153 kmem_cache_free(delayed_node_cache, node);
154 radix_tree_preload_end();
157 btrfs_inode->delayed_node = node;
158 spin_unlock(&root->inode_lock);
159 radix_tree_preload_end();
165 * Call it when holding delayed_node->mutex
167 * If mod = 1, add this node into the prepared list.
169 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
170 struct btrfs_delayed_node *node,
173 spin_lock(&root->lock);
174 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
175 if (!list_empty(&node->p_list))
176 list_move_tail(&node->p_list, &root->prepare_list);
178 list_add_tail(&node->p_list, &root->prepare_list);
180 list_add_tail(&node->n_list, &root->node_list);
181 list_add_tail(&node->p_list, &root->prepare_list);
182 refcount_inc(&node->refs); /* inserted into list */
184 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
186 spin_unlock(&root->lock);
189 /* Call it when holding delayed_node->mutex */
190 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
191 struct btrfs_delayed_node *node)
193 spin_lock(&root->lock);
194 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
196 refcount_dec(&node->refs); /* not in the list */
197 list_del_init(&node->n_list);
198 if (!list_empty(&node->p_list))
199 list_del_init(&node->p_list);
200 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
202 spin_unlock(&root->lock);
205 static struct btrfs_delayed_node *btrfs_first_delayed_node(
206 struct btrfs_delayed_root *delayed_root)
209 struct btrfs_delayed_node *node = NULL;
211 spin_lock(&delayed_root->lock);
212 if (list_empty(&delayed_root->node_list))
215 p = delayed_root->node_list.next;
216 node = list_entry(p, struct btrfs_delayed_node, n_list);
217 refcount_inc(&node->refs);
219 spin_unlock(&delayed_root->lock);
224 static struct btrfs_delayed_node *btrfs_next_delayed_node(
225 struct btrfs_delayed_node *node)
227 struct btrfs_delayed_root *delayed_root;
229 struct btrfs_delayed_node *next = NULL;
231 delayed_root = node->root->fs_info->delayed_root;
232 spin_lock(&delayed_root->lock);
233 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
234 /* not in the list */
235 if (list_empty(&delayed_root->node_list))
237 p = delayed_root->node_list.next;
238 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
241 p = node->n_list.next;
243 next = list_entry(p, struct btrfs_delayed_node, n_list);
244 refcount_inc(&next->refs);
246 spin_unlock(&delayed_root->lock);
251 static void __btrfs_release_delayed_node(
252 struct btrfs_delayed_node *delayed_node,
255 struct btrfs_delayed_root *delayed_root;
260 delayed_root = delayed_node->root->fs_info->delayed_root;
262 mutex_lock(&delayed_node->mutex);
263 if (delayed_node->count)
264 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
266 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
267 mutex_unlock(&delayed_node->mutex);
269 if (refcount_dec_and_test(&delayed_node->refs)) {
270 struct btrfs_root *root = delayed_node->root;
272 spin_lock(&root->inode_lock);
274 * Once our refcount goes to zero, nobody is allowed to bump it
275 * back up. We can delete it now.
277 ASSERT(refcount_read(&delayed_node->refs) == 0);
278 radix_tree_delete(&root->delayed_nodes_tree,
279 delayed_node->inode_id);
280 spin_unlock(&root->inode_lock);
281 kmem_cache_free(delayed_node_cache, delayed_node);
285 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
287 __btrfs_release_delayed_node(node, 0);
290 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
291 struct btrfs_delayed_root *delayed_root)
294 struct btrfs_delayed_node *node = NULL;
296 spin_lock(&delayed_root->lock);
297 if (list_empty(&delayed_root->prepare_list))
300 p = delayed_root->prepare_list.next;
302 node = list_entry(p, struct btrfs_delayed_node, p_list);
303 refcount_inc(&node->refs);
305 spin_unlock(&delayed_root->lock);
310 static inline void btrfs_release_prepared_delayed_node(
311 struct btrfs_delayed_node *node)
313 __btrfs_release_delayed_node(node, 1);
316 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
318 struct btrfs_delayed_item *item;
319 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
321 item->data_len = data_len;
322 item->ins_or_del = 0;
323 item->bytes_reserved = 0;
324 item->delayed_node = NULL;
325 refcount_set(&item->refs, 1);
331 * __btrfs_lookup_delayed_item - look up the delayed item by key
332 * @delayed_node: pointer to the delayed node
333 * @key: the key to look up
334 * @prev: used to store the prev item if the right item isn't found
335 * @next: used to store the next item if the right item isn't found
337 * Note: if we don't find the right item, we will return the prev item and
340 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
341 struct rb_root *root,
342 struct btrfs_key *key,
343 struct btrfs_delayed_item **prev,
344 struct btrfs_delayed_item **next)
346 struct rb_node *node, *prev_node = NULL;
347 struct btrfs_delayed_item *delayed_item = NULL;
350 node = root->rb_node;
353 delayed_item = rb_entry(node, struct btrfs_delayed_item,
356 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
358 node = node->rb_right;
360 node = node->rb_left;
369 *prev = delayed_item;
370 else if ((node = rb_prev(prev_node)) != NULL) {
371 *prev = rb_entry(node, struct btrfs_delayed_item,
381 *next = delayed_item;
382 else if ((node = rb_next(prev_node)) != NULL) {
383 *next = rb_entry(node, struct btrfs_delayed_item,
391 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
392 struct btrfs_delayed_node *delayed_node,
393 struct btrfs_key *key)
395 return __btrfs_lookup_delayed_item(&delayed_node->ins_root.rb_root, key,
399 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
400 struct btrfs_delayed_item *ins,
403 struct rb_node **p, *node;
404 struct rb_node *parent_node = NULL;
405 struct rb_root_cached *root;
406 struct btrfs_delayed_item *item;
408 bool leftmost = true;
410 if (action == BTRFS_DELAYED_INSERTION_ITEM)
411 root = &delayed_node->ins_root;
412 else if (action == BTRFS_DELAYED_DELETION_ITEM)
413 root = &delayed_node->del_root;
416 p = &root->rb_root.rb_node;
417 node = &ins->rb_node;
421 item = rb_entry(parent_node, struct btrfs_delayed_item,
424 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
428 } else if (cmp > 0) {
435 rb_link_node(node, parent_node, p);
436 rb_insert_color_cached(node, root, leftmost);
437 ins->delayed_node = delayed_node;
438 ins->ins_or_del = action;
440 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
441 action == BTRFS_DELAYED_INSERTION_ITEM &&
442 ins->key.offset >= delayed_node->index_cnt)
443 delayed_node->index_cnt = ins->key.offset + 1;
445 delayed_node->count++;
446 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
450 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
451 struct btrfs_delayed_item *item)
453 return __btrfs_add_delayed_item(node, item,
454 BTRFS_DELAYED_INSERTION_ITEM);
457 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
458 struct btrfs_delayed_item *item)
460 return __btrfs_add_delayed_item(node, item,
461 BTRFS_DELAYED_DELETION_ITEM);
464 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
466 int seq = atomic_inc_return(&delayed_root->items_seq);
468 /* atomic_dec_return implies a barrier */
469 if ((atomic_dec_return(&delayed_root->items) <
470 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0))
471 cond_wake_up_nomb(&delayed_root->wait);
474 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
476 struct rb_root_cached *root;
477 struct btrfs_delayed_root *delayed_root;
479 /* Not associated with any delayed_node */
480 if (!delayed_item->delayed_node)
482 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
484 BUG_ON(!delayed_root);
485 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
486 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
488 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
489 root = &delayed_item->delayed_node->ins_root;
491 root = &delayed_item->delayed_node->del_root;
493 rb_erase_cached(&delayed_item->rb_node, root);
494 delayed_item->delayed_node->count--;
496 finish_one_item(delayed_root);
499 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
502 __btrfs_remove_delayed_item(item);
503 if (refcount_dec_and_test(&item->refs))
508 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
509 struct btrfs_delayed_node *delayed_node)
512 struct btrfs_delayed_item *item = NULL;
514 p = rb_first_cached(&delayed_node->ins_root);
516 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
521 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
522 struct btrfs_delayed_node *delayed_node)
525 struct btrfs_delayed_item *item = NULL;
527 p = rb_first_cached(&delayed_node->del_root);
529 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
534 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
535 struct btrfs_delayed_item *item)
538 struct btrfs_delayed_item *next = NULL;
540 p = rb_next(&item->rb_node);
542 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
547 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
548 struct btrfs_root *root,
549 struct btrfs_delayed_item *item)
551 struct btrfs_block_rsv *src_rsv;
552 struct btrfs_block_rsv *dst_rsv;
553 struct btrfs_fs_info *fs_info = root->fs_info;
557 if (!trans->bytes_reserved)
560 src_rsv = trans->block_rsv;
561 dst_rsv = &fs_info->delayed_block_rsv;
563 num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
566 * Here we migrate space rsv from transaction rsv, since have already
567 * reserved space when starting a transaction. So no need to reserve
570 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
572 trace_btrfs_space_reservation(fs_info, "delayed_item",
575 item->bytes_reserved = num_bytes;
581 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
582 struct btrfs_delayed_item *item)
584 struct btrfs_block_rsv *rsv;
585 struct btrfs_fs_info *fs_info = root->fs_info;
587 if (!item->bytes_reserved)
590 rsv = &fs_info->delayed_block_rsv;
592 * Check btrfs_delayed_item_reserve_metadata() to see why we don't need
593 * to release/reserve qgroup space.
595 trace_btrfs_space_reservation(fs_info, "delayed_item",
596 item->key.objectid, item->bytes_reserved,
598 btrfs_block_rsv_release(fs_info, rsv, item->bytes_reserved, NULL);
601 static int btrfs_delayed_inode_reserve_metadata(
602 struct btrfs_trans_handle *trans,
603 struct btrfs_root *root,
604 struct btrfs_delayed_node *node)
606 struct btrfs_fs_info *fs_info = root->fs_info;
607 struct btrfs_block_rsv *src_rsv;
608 struct btrfs_block_rsv *dst_rsv;
612 src_rsv = trans->block_rsv;
613 dst_rsv = &fs_info->delayed_block_rsv;
615 num_bytes = btrfs_calc_metadata_size(fs_info, 1);
618 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
619 * which doesn't reserve space for speed. This is a problem since we
620 * still need to reserve space for this update, so try to reserve the
623 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
624 * we always reserve enough to update the inode item.
626 if (!src_rsv || (!trans->bytes_reserved &&
627 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
628 ret = btrfs_qgroup_reserve_meta(root, num_bytes,
629 BTRFS_QGROUP_RSV_META_PREALLOC, true);
632 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
633 BTRFS_RESERVE_NO_FLUSH);
634 /* NO_FLUSH could only fail with -ENOSPC */
635 ASSERT(ret == 0 || ret == -ENOSPC);
637 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
639 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
643 trace_btrfs_space_reservation(fs_info, "delayed_inode",
644 node->inode_id, num_bytes, 1);
645 node->bytes_reserved = num_bytes;
651 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
652 struct btrfs_delayed_node *node,
655 struct btrfs_block_rsv *rsv;
657 if (!node->bytes_reserved)
660 rsv = &fs_info->delayed_block_rsv;
661 trace_btrfs_space_reservation(fs_info, "delayed_inode",
662 node->inode_id, node->bytes_reserved, 0);
663 btrfs_block_rsv_release(fs_info, rsv, node->bytes_reserved, NULL);
665 btrfs_qgroup_free_meta_prealloc(node->root,
666 node->bytes_reserved);
668 btrfs_qgroup_convert_reserved_meta(node->root,
669 node->bytes_reserved);
670 node->bytes_reserved = 0;
674 * Insert a single delayed item or a batch of delayed items that have consecutive
675 * keys if they exist.
677 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
678 struct btrfs_root *root,
679 struct btrfs_path *path,
680 struct btrfs_delayed_item *first_item)
683 struct btrfs_delayed_item *curr;
684 struct btrfs_delayed_item *next;
685 const int max_size = BTRFS_LEAF_DATA_SIZE(root->fs_info);
688 char *ins_data = NULL;
689 struct btrfs_key *ins_keys;
693 list_add_tail(&first_item->tree_list, &batch);
695 total_size = first_item->data_len + sizeof(struct btrfs_item);
701 next = __btrfs_next_delayed_item(curr);
702 if (!next || !btrfs_is_continuous_delayed_item(curr, next))
705 next_size = next->data_len + sizeof(struct btrfs_item);
706 if (total_size + next_size > max_size)
709 list_add_tail(&next->tree_list, &batch);
711 total_size += next_size;
716 ins_keys = &first_item->key;
717 ins_sizes = &first_item->data_len;
721 ins_data = kmalloc(nitems * sizeof(u32) +
722 nitems * sizeof(struct btrfs_key), GFP_NOFS);
727 ins_sizes = (u32 *)ins_data;
728 ins_keys = (struct btrfs_key *)(ins_data + nitems * sizeof(u32));
729 list_for_each_entry(curr, &batch, tree_list) {
730 ins_keys[i] = curr->key;
731 ins_sizes[i] = curr->data_len;
736 ret = btrfs_insert_empty_items(trans, root, path, ins_keys, ins_sizes,
741 list_for_each_entry(curr, &batch, tree_list) {
744 data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
745 write_extent_buffer(path->nodes[0], &curr->data,
746 (unsigned long)data_ptr, curr->data_len);
751 * Now release our path before releasing the delayed items and their
752 * metadata reservations, so that we don't block other tasks for more
755 btrfs_release_path(path);
757 list_for_each_entry_safe(curr, next, &batch, tree_list) {
758 list_del(&curr->tree_list);
759 btrfs_delayed_item_release_metadata(root, curr);
760 btrfs_release_delayed_item(curr);
767 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
768 struct btrfs_path *path,
769 struct btrfs_root *root,
770 struct btrfs_delayed_node *node)
775 struct btrfs_delayed_item *curr;
777 mutex_lock(&node->mutex);
778 curr = __btrfs_first_delayed_insertion_item(node);
780 mutex_unlock(&node->mutex);
783 ret = btrfs_insert_delayed_item(trans, root, path, curr);
784 mutex_unlock(&node->mutex);
790 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
791 struct btrfs_root *root,
792 struct btrfs_path *path,
793 struct btrfs_delayed_item *item)
795 struct btrfs_delayed_item *curr, *next;
796 struct extent_buffer *leaf;
797 struct btrfs_key key;
798 struct list_head head;
799 int nitems, i, last_item;
802 BUG_ON(!path->nodes[0]);
804 leaf = path->nodes[0];
807 last_item = btrfs_header_nritems(leaf) - 1;
809 return -ENOENT; /* FIXME: Is errno suitable? */
812 INIT_LIST_HEAD(&head);
813 btrfs_item_key_to_cpu(leaf, &key, i);
816 * count the number of the dir index items that we can delete in batch
818 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
819 list_add_tail(&next->tree_list, &head);
823 next = __btrfs_next_delayed_item(curr);
827 if (!btrfs_is_continuous_delayed_item(curr, next))
833 btrfs_item_key_to_cpu(leaf, &key, i);
839 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
843 list_for_each_entry_safe(curr, next, &head, tree_list) {
844 btrfs_delayed_item_release_metadata(root, curr);
845 list_del(&curr->tree_list);
846 btrfs_release_delayed_item(curr);
853 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
854 struct btrfs_path *path,
855 struct btrfs_root *root,
856 struct btrfs_delayed_node *node)
858 struct btrfs_delayed_item *curr, *prev;
862 mutex_lock(&node->mutex);
863 curr = __btrfs_first_delayed_deletion_item(node);
867 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
872 * can't find the item which the node points to, so this node
873 * is invalid, just drop it.
876 curr = __btrfs_next_delayed_item(prev);
877 btrfs_release_delayed_item(prev);
879 btrfs_release_path(path);
881 mutex_unlock(&node->mutex);
887 btrfs_batch_delete_items(trans, root, path, curr);
888 btrfs_release_path(path);
889 mutex_unlock(&node->mutex);
893 btrfs_release_path(path);
894 mutex_unlock(&node->mutex);
898 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
900 struct btrfs_delayed_root *delayed_root;
903 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
904 BUG_ON(!delayed_node->root);
905 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
906 delayed_node->count--;
908 delayed_root = delayed_node->root->fs_info->delayed_root;
909 finish_one_item(delayed_root);
913 static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
916 if (test_and_clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) {
917 struct btrfs_delayed_root *delayed_root;
919 ASSERT(delayed_node->root);
920 delayed_node->count--;
922 delayed_root = delayed_node->root->fs_info->delayed_root;
923 finish_one_item(delayed_root);
927 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
928 struct btrfs_root *root,
929 struct btrfs_path *path,
930 struct btrfs_delayed_node *node)
932 struct btrfs_fs_info *fs_info = root->fs_info;
933 struct btrfs_key key;
934 struct btrfs_inode_item *inode_item;
935 struct extent_buffer *leaf;
939 key.objectid = node->inode_id;
940 key.type = BTRFS_INODE_ITEM_KEY;
943 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
948 ret = btrfs_lookup_inode(trans, root, path, &key, mod);
954 leaf = path->nodes[0];
955 inode_item = btrfs_item_ptr(leaf, path->slots[0],
956 struct btrfs_inode_item);
957 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
958 sizeof(struct btrfs_inode_item));
959 btrfs_mark_buffer_dirty(leaf);
961 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
965 if (path->slots[0] >= btrfs_header_nritems(leaf))
968 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
969 if (key.objectid != node->inode_id)
972 if (key.type != BTRFS_INODE_REF_KEY &&
973 key.type != BTRFS_INODE_EXTREF_KEY)
977 * Delayed iref deletion is for the inode who has only one link,
978 * so there is only one iref. The case that several irefs are
979 * in the same item doesn't exist.
981 btrfs_del_item(trans, root, path);
983 btrfs_release_delayed_iref(node);
984 btrfs_release_path(path);
986 btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
987 btrfs_release_delayed_inode(node);
990 * If we fail to update the delayed inode we need to abort the
991 * transaction, because we could leave the inode with the improper
994 if (ret && ret != -ENOENT)
995 btrfs_abort_transaction(trans, ret);
1000 btrfs_release_path(path);
1002 key.type = BTRFS_INODE_EXTREF_KEY;
1005 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1011 leaf = path->nodes[0];
1016 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1017 struct btrfs_root *root,
1018 struct btrfs_path *path,
1019 struct btrfs_delayed_node *node)
1023 mutex_lock(&node->mutex);
1024 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
1025 mutex_unlock(&node->mutex);
1029 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1030 mutex_unlock(&node->mutex);
1035 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1036 struct btrfs_path *path,
1037 struct btrfs_delayed_node *node)
1041 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1045 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1049 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1054 * Called when committing the transaction.
1055 * Returns 0 on success.
1056 * Returns < 0 on error and returns with an aborted transaction with any
1057 * outstanding delayed items cleaned up.
1059 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
1061 struct btrfs_fs_info *fs_info = trans->fs_info;
1062 struct btrfs_delayed_root *delayed_root;
1063 struct btrfs_delayed_node *curr_node, *prev_node;
1064 struct btrfs_path *path;
1065 struct btrfs_block_rsv *block_rsv;
1067 bool count = (nr > 0);
1069 if (TRANS_ABORTED(trans))
1072 path = btrfs_alloc_path();
1076 block_rsv = trans->block_rsv;
1077 trans->block_rsv = &fs_info->delayed_block_rsv;
1079 delayed_root = fs_info->delayed_root;
1081 curr_node = btrfs_first_delayed_node(delayed_root);
1082 while (curr_node && (!count || nr--)) {
1083 ret = __btrfs_commit_inode_delayed_items(trans, path,
1086 btrfs_release_delayed_node(curr_node);
1088 btrfs_abort_transaction(trans, ret);
1092 prev_node = curr_node;
1093 curr_node = btrfs_next_delayed_node(curr_node);
1094 btrfs_release_delayed_node(prev_node);
1098 btrfs_release_delayed_node(curr_node);
1099 btrfs_free_path(path);
1100 trans->block_rsv = block_rsv;
1105 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
1107 return __btrfs_run_delayed_items(trans, -1);
1110 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
1112 return __btrfs_run_delayed_items(trans, nr);
1115 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1116 struct btrfs_inode *inode)
1118 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1119 struct btrfs_path *path;
1120 struct btrfs_block_rsv *block_rsv;
1126 mutex_lock(&delayed_node->mutex);
1127 if (!delayed_node->count) {
1128 mutex_unlock(&delayed_node->mutex);
1129 btrfs_release_delayed_node(delayed_node);
1132 mutex_unlock(&delayed_node->mutex);
1134 path = btrfs_alloc_path();
1136 btrfs_release_delayed_node(delayed_node);
1140 block_rsv = trans->block_rsv;
1141 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1143 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1145 btrfs_release_delayed_node(delayed_node);
1146 btrfs_free_path(path);
1147 trans->block_rsv = block_rsv;
1152 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
1154 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1155 struct btrfs_trans_handle *trans;
1156 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1157 struct btrfs_path *path;
1158 struct btrfs_block_rsv *block_rsv;
1164 mutex_lock(&delayed_node->mutex);
1165 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1166 mutex_unlock(&delayed_node->mutex);
1167 btrfs_release_delayed_node(delayed_node);
1170 mutex_unlock(&delayed_node->mutex);
1172 trans = btrfs_join_transaction(delayed_node->root);
1173 if (IS_ERR(trans)) {
1174 ret = PTR_ERR(trans);
1178 path = btrfs_alloc_path();
1184 block_rsv = trans->block_rsv;
1185 trans->block_rsv = &fs_info->delayed_block_rsv;
1187 mutex_lock(&delayed_node->mutex);
1188 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
1189 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1190 path, delayed_node);
1193 mutex_unlock(&delayed_node->mutex);
1195 btrfs_free_path(path);
1196 trans->block_rsv = block_rsv;
1198 btrfs_end_transaction(trans);
1199 btrfs_btree_balance_dirty(fs_info);
1201 btrfs_release_delayed_node(delayed_node);
1206 void btrfs_remove_delayed_node(struct btrfs_inode *inode)
1208 struct btrfs_delayed_node *delayed_node;
1210 delayed_node = READ_ONCE(inode->delayed_node);
1214 inode->delayed_node = NULL;
1215 btrfs_release_delayed_node(delayed_node);
1218 struct btrfs_async_delayed_work {
1219 struct btrfs_delayed_root *delayed_root;
1221 struct btrfs_work work;
1224 static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1226 struct btrfs_async_delayed_work *async_work;
1227 struct btrfs_delayed_root *delayed_root;
1228 struct btrfs_trans_handle *trans;
1229 struct btrfs_path *path;
1230 struct btrfs_delayed_node *delayed_node = NULL;
1231 struct btrfs_root *root;
1232 struct btrfs_block_rsv *block_rsv;
1235 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1236 delayed_root = async_work->delayed_root;
1238 path = btrfs_alloc_path();
1243 if (atomic_read(&delayed_root->items) <
1244 BTRFS_DELAYED_BACKGROUND / 2)
1247 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1251 root = delayed_node->root;
1253 trans = btrfs_join_transaction(root);
1254 if (IS_ERR(trans)) {
1255 btrfs_release_path(path);
1256 btrfs_release_prepared_delayed_node(delayed_node);
1261 block_rsv = trans->block_rsv;
1262 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1264 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1266 trans->block_rsv = block_rsv;
1267 btrfs_end_transaction(trans);
1268 btrfs_btree_balance_dirty_nodelay(root->fs_info);
1270 btrfs_release_path(path);
1271 btrfs_release_prepared_delayed_node(delayed_node);
1274 } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
1275 || total_done < async_work->nr);
1277 btrfs_free_path(path);
1279 wake_up(&delayed_root->wait);
1284 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1285 struct btrfs_fs_info *fs_info, int nr)
1287 struct btrfs_async_delayed_work *async_work;
1289 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1293 async_work->delayed_root = delayed_root;
1294 btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL,
1296 async_work->nr = nr;
1298 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
1302 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
1304 WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
1307 static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1309 int val = atomic_read(&delayed_root->items_seq);
1311 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
1314 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1320 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1322 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1324 if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
1325 btrfs_workqueue_normal_congested(fs_info->delayed_workers))
1328 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1332 seq = atomic_read(&delayed_root->items_seq);
1334 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
1338 wait_event_interruptible(delayed_root->wait,
1339 could_end_wait(delayed_root, seq));
1343 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
1346 /* Will return 0 or -ENOMEM */
1347 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1348 const char *name, int name_len,
1349 struct btrfs_inode *dir,
1350 struct btrfs_disk_key *disk_key, u8 type,
1353 struct btrfs_delayed_node *delayed_node;
1354 struct btrfs_delayed_item *delayed_item;
1355 struct btrfs_dir_item *dir_item;
1358 delayed_node = btrfs_get_or_create_delayed_node(dir);
1359 if (IS_ERR(delayed_node))
1360 return PTR_ERR(delayed_node);
1362 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1363 if (!delayed_item) {
1368 delayed_item->key.objectid = btrfs_ino(dir);
1369 delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
1370 delayed_item->key.offset = index;
1372 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1373 dir_item->location = *disk_key;
1374 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1375 btrfs_set_stack_dir_data_len(dir_item, 0);
1376 btrfs_set_stack_dir_name_len(dir_item, name_len);
1377 btrfs_set_stack_dir_type(dir_item, type);
1378 memcpy((char *)(dir_item + 1), name, name_len);
1380 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item);
1382 * we have reserved enough space when we start a new transaction,
1383 * so reserving metadata failure is impossible
1387 mutex_lock(&delayed_node->mutex);
1388 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1389 if (unlikely(ret)) {
1390 btrfs_err(trans->fs_info,
1391 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1392 name_len, name, delayed_node->root->root_key.objectid,
1393 delayed_node->inode_id, ret);
1396 mutex_unlock(&delayed_node->mutex);
1399 btrfs_release_delayed_node(delayed_node);
1403 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1404 struct btrfs_delayed_node *node,
1405 struct btrfs_key *key)
1407 struct btrfs_delayed_item *item;
1409 mutex_lock(&node->mutex);
1410 item = __btrfs_lookup_delayed_insertion_item(node, key);
1412 mutex_unlock(&node->mutex);
1416 btrfs_delayed_item_release_metadata(node->root, item);
1417 btrfs_release_delayed_item(item);
1418 mutex_unlock(&node->mutex);
1422 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1423 struct btrfs_inode *dir, u64 index)
1425 struct btrfs_delayed_node *node;
1426 struct btrfs_delayed_item *item;
1427 struct btrfs_key item_key;
1430 node = btrfs_get_or_create_delayed_node(dir);
1432 return PTR_ERR(node);
1434 item_key.objectid = btrfs_ino(dir);
1435 item_key.type = BTRFS_DIR_INDEX_KEY;
1436 item_key.offset = index;
1438 ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node,
1443 item = btrfs_alloc_delayed_item(0);
1449 item->key = item_key;
1451 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item);
1453 * we have reserved enough space when we start a new transaction,
1454 * so reserving metadata failure is impossible.
1457 btrfs_err(trans->fs_info,
1458 "metadata reservation failed for delayed dir item deltiona, should have been reserved");
1459 btrfs_release_delayed_item(item);
1463 mutex_lock(&node->mutex);
1464 ret = __btrfs_add_delayed_deletion_item(node, item);
1465 if (unlikely(ret)) {
1466 btrfs_err(trans->fs_info,
1467 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1468 index, node->root->root_key.objectid,
1469 node->inode_id, ret);
1470 btrfs_delayed_item_release_metadata(dir->root, item);
1471 btrfs_release_delayed_item(item);
1473 mutex_unlock(&node->mutex);
1475 btrfs_release_delayed_node(node);
1479 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
1481 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1487 * Since we have held i_mutex of this directory, it is impossible that
1488 * a new directory index is added into the delayed node and index_cnt
1489 * is updated now. So we needn't lock the delayed node.
1491 if (!delayed_node->index_cnt) {
1492 btrfs_release_delayed_node(delayed_node);
1496 inode->index_cnt = delayed_node->index_cnt;
1497 btrfs_release_delayed_node(delayed_node);
1501 bool btrfs_readdir_get_delayed_items(struct inode *inode,
1502 struct list_head *ins_list,
1503 struct list_head *del_list)
1505 struct btrfs_delayed_node *delayed_node;
1506 struct btrfs_delayed_item *item;
1508 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1513 * We can only do one readdir with delayed items at a time because of
1514 * item->readdir_list.
1516 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
1517 btrfs_inode_lock(inode, 0);
1519 mutex_lock(&delayed_node->mutex);
1520 item = __btrfs_first_delayed_insertion_item(delayed_node);
1522 refcount_inc(&item->refs);
1523 list_add_tail(&item->readdir_list, ins_list);
1524 item = __btrfs_next_delayed_item(item);
1527 item = __btrfs_first_delayed_deletion_item(delayed_node);
1529 refcount_inc(&item->refs);
1530 list_add_tail(&item->readdir_list, del_list);
1531 item = __btrfs_next_delayed_item(item);
1533 mutex_unlock(&delayed_node->mutex);
1535 * This delayed node is still cached in the btrfs inode, so refs
1536 * must be > 1 now, and we needn't check it is going to be freed
1539 * Besides that, this function is used to read dir, we do not
1540 * insert/delete delayed items in this period. So we also needn't
1541 * requeue or dequeue this delayed node.
1543 refcount_dec(&delayed_node->refs);
1548 void btrfs_readdir_put_delayed_items(struct inode *inode,
1549 struct list_head *ins_list,
1550 struct list_head *del_list)
1552 struct btrfs_delayed_item *curr, *next;
1554 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1555 list_del(&curr->readdir_list);
1556 if (refcount_dec_and_test(&curr->refs))
1560 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1561 list_del(&curr->readdir_list);
1562 if (refcount_dec_and_test(&curr->refs))
1567 * The VFS is going to do up_read(), so we need to downgrade back to a
1570 downgrade_write(&inode->i_rwsem);
1573 int btrfs_should_delete_dir_index(struct list_head *del_list,
1576 struct btrfs_delayed_item *curr;
1579 list_for_each_entry(curr, del_list, readdir_list) {
1580 if (curr->key.offset > index)
1582 if (curr->key.offset == index) {
1591 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1594 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1595 struct list_head *ins_list)
1597 struct btrfs_dir_item *di;
1598 struct btrfs_delayed_item *curr, *next;
1599 struct btrfs_key location;
1603 unsigned char d_type;
1605 if (list_empty(ins_list))
1609 * Changing the data of the delayed item is impossible. So
1610 * we needn't lock them. And we have held i_mutex of the
1611 * directory, nobody can delete any directory indexes now.
1613 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1614 list_del(&curr->readdir_list);
1616 if (curr->key.offset < ctx->pos) {
1617 if (refcount_dec_and_test(&curr->refs))
1622 ctx->pos = curr->key.offset;
1624 di = (struct btrfs_dir_item *)curr->data;
1625 name = (char *)(di + 1);
1626 name_len = btrfs_stack_dir_name_len(di);
1628 d_type = fs_ftype_to_dtype(di->type);
1629 btrfs_disk_key_to_cpu(&location, &di->location);
1631 over = !dir_emit(ctx, name, name_len,
1632 location.objectid, d_type);
1634 if (refcount_dec_and_test(&curr->refs))
1644 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1645 struct btrfs_inode_item *inode_item,
1646 struct inode *inode)
1650 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1651 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1652 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1653 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1654 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1655 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1656 btrfs_set_stack_inode_generation(inode_item,
1657 BTRFS_I(inode)->generation);
1658 btrfs_set_stack_inode_sequence(inode_item,
1659 inode_peek_iversion(inode));
1660 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1661 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1662 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
1663 BTRFS_I(inode)->ro_flags);
1664 btrfs_set_stack_inode_flags(inode_item, flags);
1665 btrfs_set_stack_inode_block_group(inode_item, 0);
1667 btrfs_set_stack_timespec_sec(&inode_item->atime,
1668 inode->i_atime.tv_sec);
1669 btrfs_set_stack_timespec_nsec(&inode_item->atime,
1670 inode->i_atime.tv_nsec);
1672 btrfs_set_stack_timespec_sec(&inode_item->mtime,
1673 inode->i_mtime.tv_sec);
1674 btrfs_set_stack_timespec_nsec(&inode_item->mtime,
1675 inode->i_mtime.tv_nsec);
1677 btrfs_set_stack_timespec_sec(&inode_item->ctime,
1678 inode->i_ctime.tv_sec);
1679 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
1680 inode->i_ctime.tv_nsec);
1682 btrfs_set_stack_timespec_sec(&inode_item->otime,
1683 BTRFS_I(inode)->i_otime.tv_sec);
1684 btrfs_set_stack_timespec_nsec(&inode_item->otime,
1685 BTRFS_I(inode)->i_otime.tv_nsec);
1688 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1690 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1691 struct btrfs_delayed_node *delayed_node;
1692 struct btrfs_inode_item *inode_item;
1694 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1698 mutex_lock(&delayed_node->mutex);
1699 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1700 mutex_unlock(&delayed_node->mutex);
1701 btrfs_release_delayed_node(delayed_node);
1705 inode_item = &delayed_node->inode_item;
1707 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1708 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1709 btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
1710 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
1711 round_up(i_size_read(inode), fs_info->sectorsize));
1712 inode->i_mode = btrfs_stack_inode_mode(inode_item);
1713 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1714 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1715 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1716 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1718 inode_set_iversion_queried(inode,
1719 btrfs_stack_inode_sequence(inode_item));
1721 *rdev = btrfs_stack_inode_rdev(inode_item);
1722 btrfs_inode_split_flags(btrfs_stack_inode_flags(inode_item),
1723 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
1725 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
1726 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
1728 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
1729 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
1731 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
1732 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
1734 BTRFS_I(inode)->i_otime.tv_sec =
1735 btrfs_stack_timespec_sec(&inode_item->otime);
1736 BTRFS_I(inode)->i_otime.tv_nsec =
1737 btrfs_stack_timespec_nsec(&inode_item->otime);
1739 inode->i_generation = BTRFS_I(inode)->generation;
1740 BTRFS_I(inode)->index_cnt = (u64)-1;
1742 mutex_unlock(&delayed_node->mutex);
1743 btrfs_release_delayed_node(delayed_node);
1747 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1748 struct btrfs_root *root,
1749 struct btrfs_inode *inode)
1751 struct btrfs_delayed_node *delayed_node;
1754 delayed_node = btrfs_get_or_create_delayed_node(inode);
1755 if (IS_ERR(delayed_node))
1756 return PTR_ERR(delayed_node);
1758 mutex_lock(&delayed_node->mutex);
1759 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1760 fill_stack_inode_item(trans, &delayed_node->inode_item,
1765 ret = btrfs_delayed_inode_reserve_metadata(trans, root, delayed_node);
1769 fill_stack_inode_item(trans, &delayed_node->inode_item, &inode->vfs_inode);
1770 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
1771 delayed_node->count++;
1772 atomic_inc(&root->fs_info->delayed_root->items);
1774 mutex_unlock(&delayed_node->mutex);
1775 btrfs_release_delayed_node(delayed_node);
1779 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
1781 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1782 struct btrfs_delayed_node *delayed_node;
1785 * we don't do delayed inode updates during log recovery because it
1786 * leads to enospc problems. This means we also can't do
1787 * delayed inode refs
1789 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
1792 delayed_node = btrfs_get_or_create_delayed_node(inode);
1793 if (IS_ERR(delayed_node))
1794 return PTR_ERR(delayed_node);
1797 * We don't reserve space for inode ref deletion is because:
1798 * - We ONLY do async inode ref deletion for the inode who has only
1799 * one link(i_nlink == 1), it means there is only one inode ref.
1800 * And in most case, the inode ref and the inode item are in the
1801 * same leaf, and we will deal with them at the same time.
1802 * Since we are sure we will reserve the space for the inode item,
1803 * it is unnecessary to reserve space for inode ref deletion.
1804 * - If the inode ref and the inode item are not in the same leaf,
1805 * We also needn't worry about enospc problem, because we reserve
1806 * much more space for the inode update than it needs.
1807 * - At the worst, we can steal some space from the global reservation.
1810 mutex_lock(&delayed_node->mutex);
1811 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1814 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1815 delayed_node->count++;
1816 atomic_inc(&fs_info->delayed_root->items);
1818 mutex_unlock(&delayed_node->mutex);
1819 btrfs_release_delayed_node(delayed_node);
1823 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1825 struct btrfs_root *root = delayed_node->root;
1826 struct btrfs_fs_info *fs_info = root->fs_info;
1827 struct btrfs_delayed_item *curr_item, *prev_item;
1829 mutex_lock(&delayed_node->mutex);
1830 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1832 btrfs_delayed_item_release_metadata(root, curr_item);
1833 prev_item = curr_item;
1834 curr_item = __btrfs_next_delayed_item(prev_item);
1835 btrfs_release_delayed_item(prev_item);
1838 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1840 btrfs_delayed_item_release_metadata(root, curr_item);
1841 prev_item = curr_item;
1842 curr_item = __btrfs_next_delayed_item(prev_item);
1843 btrfs_release_delayed_item(prev_item);
1846 btrfs_release_delayed_iref(delayed_node);
1848 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1849 btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false);
1850 btrfs_release_delayed_inode(delayed_node);
1852 mutex_unlock(&delayed_node->mutex);
1855 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
1857 struct btrfs_delayed_node *delayed_node;
1859 delayed_node = btrfs_get_delayed_node(inode);
1863 __btrfs_kill_delayed_node(delayed_node);
1864 btrfs_release_delayed_node(delayed_node);
1867 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1870 struct btrfs_delayed_node *delayed_nodes[8];
1874 spin_lock(&root->inode_lock);
1875 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1876 (void **)delayed_nodes, inode_id,
1877 ARRAY_SIZE(delayed_nodes));
1879 spin_unlock(&root->inode_lock);
1883 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1884 for (i = 0; i < n; i++) {
1886 * Don't increase refs in case the node is dead and
1887 * about to be removed from the tree in the loop below
1889 if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
1890 delayed_nodes[i] = NULL;
1892 spin_unlock(&root->inode_lock);
1894 for (i = 0; i < n; i++) {
1895 if (!delayed_nodes[i])
1897 __btrfs_kill_delayed_node(delayed_nodes[i]);
1898 btrfs_release_delayed_node(delayed_nodes[i]);
1903 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
1905 struct btrfs_delayed_node *curr_node, *prev_node;
1907 curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
1909 __btrfs_kill_delayed_node(curr_node);
1911 prev_node = curr_node;
1912 curr_node = btrfs_next_delayed_node(curr_node);
1913 btrfs_release_delayed_node(prev_node);