Linux 4.18.10
[linux/fpc-iii.git] / fs / btrfs / delayed-inode.c
blobfe6caa7e698bedf4d86c4fe427a3a49a227dcbc6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2011 Fujitsu. All rights reserved.
4 * Written by Miao Xie <miaox@cn.fujitsu.com>
5 */
7 #include <linux/slab.h>
8 #include <linux/iversion.h>
9 #include "delayed-inode.h"
10 #include "disk-io.h"
11 #include "transaction.h"
12 #include "ctree.h"
13 #include "qgroup.h"
15 #define BTRFS_DELAYED_WRITEBACK 512
16 #define BTRFS_DELAYED_BACKGROUND 128
17 #define BTRFS_DELAYED_BATCH 16
19 static struct kmem_cache *delayed_node_cache;
21 int __init btrfs_delayed_inode_init(void)
23 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
24 sizeof(struct btrfs_delayed_node),
26 SLAB_MEM_SPREAD,
27 NULL);
28 if (!delayed_node_cache)
29 return -ENOMEM;
30 return 0;
33 void __cold btrfs_delayed_inode_exit(void)
35 kmem_cache_destroy(delayed_node_cache);
38 static inline void btrfs_init_delayed_node(
39 struct btrfs_delayed_node *delayed_node,
40 struct btrfs_root *root, u64 inode_id)
42 delayed_node->root = root;
43 delayed_node->inode_id = inode_id;
44 refcount_set(&delayed_node->refs, 0);
45 delayed_node->ins_root = RB_ROOT;
46 delayed_node->del_root = RB_ROOT;
47 mutex_init(&delayed_node->mutex);
48 INIT_LIST_HEAD(&delayed_node->n_list);
49 INIT_LIST_HEAD(&delayed_node->p_list);
52 static inline int btrfs_is_continuous_delayed_item(
53 struct btrfs_delayed_item *item1,
54 struct btrfs_delayed_item *item2)
56 if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
57 item1->key.objectid == item2->key.objectid &&
58 item1->key.type == item2->key.type &&
59 item1->key.offset + 1 == item2->key.offset)
60 return 1;
61 return 0;
64 static struct btrfs_delayed_node *btrfs_get_delayed_node(
65 struct btrfs_inode *btrfs_inode)
67 struct btrfs_root *root = btrfs_inode->root;
68 u64 ino = btrfs_ino(btrfs_inode);
69 struct btrfs_delayed_node *node;
71 node = READ_ONCE(btrfs_inode->delayed_node);
72 if (node) {
73 refcount_inc(&node->refs);
74 return node;
77 spin_lock(&root->inode_lock);
78 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
80 if (node) {
81 if (btrfs_inode->delayed_node) {
82 refcount_inc(&node->refs); /* can be accessed */
83 BUG_ON(btrfs_inode->delayed_node != node);
84 spin_unlock(&root->inode_lock);
85 return node;
89 * It's possible that we're racing into the middle of removing
90 * this node from the radix tree. In this case, the refcount
91 * was zero and it should never go back to one. Just return
92 * NULL like it was never in the radix at all; our release
93 * function is in the process of removing it.
95 * Some implementations of refcount_inc refuse to bump the
96 * refcount once it has hit zero. If we don't do this dance
97 * here, refcount_inc() may decide to just WARN_ONCE() instead
98 * of actually bumping the refcount.
100 * If this node is properly in the radix, we want to bump the
101 * refcount twice, once for the inode and once for this get
102 * operation.
104 if (refcount_inc_not_zero(&node->refs)) {
105 refcount_inc(&node->refs);
106 btrfs_inode->delayed_node = node;
107 } else {
108 node = NULL;
111 spin_unlock(&root->inode_lock);
112 return node;
114 spin_unlock(&root->inode_lock);
116 return NULL;
119 /* Will return either the node or PTR_ERR(-ENOMEM) */
120 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
121 struct btrfs_inode *btrfs_inode)
123 struct btrfs_delayed_node *node;
124 struct btrfs_root *root = btrfs_inode->root;
125 u64 ino = btrfs_ino(btrfs_inode);
126 int ret;
128 again:
129 node = btrfs_get_delayed_node(btrfs_inode);
130 if (node)
131 return node;
133 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
134 if (!node)
135 return ERR_PTR(-ENOMEM);
136 btrfs_init_delayed_node(node, root, ino);
138 /* cached in the btrfs inode and can be accessed */
139 refcount_set(&node->refs, 2);
141 ret = radix_tree_preload(GFP_NOFS);
142 if (ret) {
143 kmem_cache_free(delayed_node_cache, node);
144 return ERR_PTR(ret);
147 spin_lock(&root->inode_lock);
148 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
149 if (ret == -EEXIST) {
150 spin_unlock(&root->inode_lock);
151 kmem_cache_free(delayed_node_cache, node);
152 radix_tree_preload_end();
153 goto again;
155 btrfs_inode->delayed_node = node;
156 spin_unlock(&root->inode_lock);
157 radix_tree_preload_end();
159 return node;
163 * Call it when holding delayed_node->mutex
165 * If mod = 1, add this node into the prepared list.
167 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
168 struct btrfs_delayed_node *node,
169 int mod)
171 spin_lock(&root->lock);
172 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
173 if (!list_empty(&node->p_list))
174 list_move_tail(&node->p_list, &root->prepare_list);
175 else if (mod)
176 list_add_tail(&node->p_list, &root->prepare_list);
177 } else {
178 list_add_tail(&node->n_list, &root->node_list);
179 list_add_tail(&node->p_list, &root->prepare_list);
180 refcount_inc(&node->refs); /* inserted into list */
181 root->nodes++;
182 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
184 spin_unlock(&root->lock);
187 /* Call it when holding delayed_node->mutex */
188 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
189 struct btrfs_delayed_node *node)
191 spin_lock(&root->lock);
192 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
193 root->nodes--;
194 refcount_dec(&node->refs); /* not in the list */
195 list_del_init(&node->n_list);
196 if (!list_empty(&node->p_list))
197 list_del_init(&node->p_list);
198 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
200 spin_unlock(&root->lock);
203 static struct btrfs_delayed_node *btrfs_first_delayed_node(
204 struct btrfs_delayed_root *delayed_root)
206 struct list_head *p;
207 struct btrfs_delayed_node *node = NULL;
209 spin_lock(&delayed_root->lock);
210 if (list_empty(&delayed_root->node_list))
211 goto out;
213 p = delayed_root->node_list.next;
214 node = list_entry(p, struct btrfs_delayed_node, n_list);
215 refcount_inc(&node->refs);
216 out:
217 spin_unlock(&delayed_root->lock);
219 return node;
222 static struct btrfs_delayed_node *btrfs_next_delayed_node(
223 struct btrfs_delayed_node *node)
225 struct btrfs_delayed_root *delayed_root;
226 struct list_head *p;
227 struct btrfs_delayed_node *next = NULL;
229 delayed_root = node->root->fs_info->delayed_root;
230 spin_lock(&delayed_root->lock);
231 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
232 /* not in the list */
233 if (list_empty(&delayed_root->node_list))
234 goto out;
235 p = delayed_root->node_list.next;
236 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
237 goto out;
238 else
239 p = node->n_list.next;
241 next = list_entry(p, struct btrfs_delayed_node, n_list);
242 refcount_inc(&next->refs);
243 out:
244 spin_unlock(&delayed_root->lock);
246 return next;
249 static void __btrfs_release_delayed_node(
250 struct btrfs_delayed_node *delayed_node,
251 int mod)
253 struct btrfs_delayed_root *delayed_root;
255 if (!delayed_node)
256 return;
258 delayed_root = delayed_node->root->fs_info->delayed_root;
260 mutex_lock(&delayed_node->mutex);
261 if (delayed_node->count)
262 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
263 else
264 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
265 mutex_unlock(&delayed_node->mutex);
267 if (refcount_dec_and_test(&delayed_node->refs)) {
268 struct btrfs_root *root = delayed_node->root;
270 spin_lock(&root->inode_lock);
272 * Once our refcount goes to zero, nobody is allowed to bump it
273 * back up. We can delete it now.
275 ASSERT(refcount_read(&delayed_node->refs) == 0);
276 radix_tree_delete(&root->delayed_nodes_tree,
277 delayed_node->inode_id);
278 spin_unlock(&root->inode_lock);
279 kmem_cache_free(delayed_node_cache, delayed_node);
283 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
285 __btrfs_release_delayed_node(node, 0);
288 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
289 struct btrfs_delayed_root *delayed_root)
291 struct list_head *p;
292 struct btrfs_delayed_node *node = NULL;
294 spin_lock(&delayed_root->lock);
295 if (list_empty(&delayed_root->prepare_list))
296 goto out;
298 p = delayed_root->prepare_list.next;
299 list_del_init(p);
300 node = list_entry(p, struct btrfs_delayed_node, p_list);
301 refcount_inc(&node->refs);
302 out:
303 spin_unlock(&delayed_root->lock);
305 return node;
308 static inline void btrfs_release_prepared_delayed_node(
309 struct btrfs_delayed_node *node)
311 __btrfs_release_delayed_node(node, 1);
314 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
316 struct btrfs_delayed_item *item;
317 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
318 if (item) {
319 item->data_len = data_len;
320 item->ins_or_del = 0;
321 item->bytes_reserved = 0;
322 item->delayed_node = NULL;
323 refcount_set(&item->refs, 1);
325 return item;
329 * __btrfs_lookup_delayed_item - look up the delayed item by key
330 * @delayed_node: pointer to the delayed node
331 * @key: the key to look up
332 * @prev: used to store the prev item if the right item isn't found
333 * @next: used to store the next item if the right item isn't found
335 * Note: if we don't find the right item, we will return the prev item and
336 * the next item.
338 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
339 struct rb_root *root,
340 struct btrfs_key *key,
341 struct btrfs_delayed_item **prev,
342 struct btrfs_delayed_item **next)
344 struct rb_node *node, *prev_node = NULL;
345 struct btrfs_delayed_item *delayed_item = NULL;
346 int ret = 0;
348 node = root->rb_node;
350 while (node) {
351 delayed_item = rb_entry(node, struct btrfs_delayed_item,
352 rb_node);
353 prev_node = node;
354 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
355 if (ret < 0)
356 node = node->rb_right;
357 else if (ret > 0)
358 node = node->rb_left;
359 else
360 return delayed_item;
363 if (prev) {
364 if (!prev_node)
365 *prev = NULL;
366 else if (ret < 0)
367 *prev = delayed_item;
368 else if ((node = rb_prev(prev_node)) != NULL) {
369 *prev = rb_entry(node, struct btrfs_delayed_item,
370 rb_node);
371 } else
372 *prev = NULL;
375 if (next) {
376 if (!prev_node)
377 *next = NULL;
378 else if (ret > 0)
379 *next = delayed_item;
380 else if ((node = rb_next(prev_node)) != NULL) {
381 *next = rb_entry(node, struct btrfs_delayed_item,
382 rb_node);
383 } else
384 *next = NULL;
386 return NULL;
389 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
390 struct btrfs_delayed_node *delayed_node,
391 struct btrfs_key *key)
393 return __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
394 NULL, NULL);
397 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
398 struct btrfs_delayed_item *ins,
399 int action)
401 struct rb_node **p, *node;
402 struct rb_node *parent_node = NULL;
403 struct rb_root *root;
404 struct btrfs_delayed_item *item;
405 int cmp;
407 if (action == BTRFS_DELAYED_INSERTION_ITEM)
408 root = &delayed_node->ins_root;
409 else if (action == BTRFS_DELAYED_DELETION_ITEM)
410 root = &delayed_node->del_root;
411 else
412 BUG();
413 p = &root->rb_node;
414 node = &ins->rb_node;
416 while (*p) {
417 parent_node = *p;
418 item = rb_entry(parent_node, struct btrfs_delayed_item,
419 rb_node);
421 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
422 if (cmp < 0)
423 p = &(*p)->rb_right;
424 else if (cmp > 0)
425 p = &(*p)->rb_left;
426 else
427 return -EEXIST;
430 rb_link_node(node, parent_node, p);
431 rb_insert_color(node, root);
432 ins->delayed_node = delayed_node;
433 ins->ins_or_del = action;
435 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
436 action == BTRFS_DELAYED_INSERTION_ITEM &&
437 ins->key.offset >= delayed_node->index_cnt)
438 delayed_node->index_cnt = ins->key.offset + 1;
440 delayed_node->count++;
441 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
442 return 0;
445 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
446 struct btrfs_delayed_item *item)
448 return __btrfs_add_delayed_item(node, item,
449 BTRFS_DELAYED_INSERTION_ITEM);
452 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
453 struct btrfs_delayed_item *item)
455 return __btrfs_add_delayed_item(node, item,
456 BTRFS_DELAYED_DELETION_ITEM);
459 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
461 int seq = atomic_inc_return(&delayed_root->items_seq);
463 /* atomic_dec_return implies a barrier */
464 if ((atomic_dec_return(&delayed_root->items) <
465 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0))
466 cond_wake_up_nomb(&delayed_root->wait);
469 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
471 struct rb_root *root;
472 struct btrfs_delayed_root *delayed_root;
474 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
476 BUG_ON(!delayed_root);
477 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
478 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
480 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
481 root = &delayed_item->delayed_node->ins_root;
482 else
483 root = &delayed_item->delayed_node->del_root;
485 rb_erase(&delayed_item->rb_node, root);
486 delayed_item->delayed_node->count--;
488 finish_one_item(delayed_root);
491 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
493 if (item) {
494 __btrfs_remove_delayed_item(item);
495 if (refcount_dec_and_test(&item->refs))
496 kfree(item);
500 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
501 struct btrfs_delayed_node *delayed_node)
503 struct rb_node *p;
504 struct btrfs_delayed_item *item = NULL;
506 p = rb_first(&delayed_node->ins_root);
507 if (p)
508 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
510 return item;
513 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
514 struct btrfs_delayed_node *delayed_node)
516 struct rb_node *p;
517 struct btrfs_delayed_item *item = NULL;
519 p = rb_first(&delayed_node->del_root);
520 if (p)
521 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
523 return item;
526 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
527 struct btrfs_delayed_item *item)
529 struct rb_node *p;
530 struct btrfs_delayed_item *next = NULL;
532 p = rb_next(&item->rb_node);
533 if (p)
534 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
536 return next;
539 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
540 struct btrfs_root *root,
541 struct btrfs_delayed_item *item)
543 struct btrfs_block_rsv *src_rsv;
544 struct btrfs_block_rsv *dst_rsv;
545 struct btrfs_fs_info *fs_info = root->fs_info;
546 u64 num_bytes;
547 int ret;
549 if (!trans->bytes_reserved)
550 return 0;
552 src_rsv = trans->block_rsv;
553 dst_rsv = &fs_info->delayed_block_rsv;
555 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
558 * Here we migrate space rsv from transaction rsv, since have already
559 * reserved space when starting a transaction. So no need to reserve
560 * qgroup space here.
562 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
563 if (!ret) {
564 trace_btrfs_space_reservation(fs_info, "delayed_item",
565 item->key.objectid,
566 num_bytes, 1);
567 item->bytes_reserved = num_bytes;
570 return ret;
573 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
574 struct btrfs_delayed_item *item)
576 struct btrfs_block_rsv *rsv;
577 struct btrfs_fs_info *fs_info = root->fs_info;
579 if (!item->bytes_reserved)
580 return;
582 rsv = &fs_info->delayed_block_rsv;
584 * Check btrfs_delayed_item_reserve_metadata() to see why we don't need
585 * to release/reserve qgroup space.
587 trace_btrfs_space_reservation(fs_info, "delayed_item",
588 item->key.objectid, item->bytes_reserved,
590 btrfs_block_rsv_release(fs_info, rsv,
591 item->bytes_reserved);
594 static int btrfs_delayed_inode_reserve_metadata(
595 struct btrfs_trans_handle *trans,
596 struct btrfs_root *root,
597 struct btrfs_inode *inode,
598 struct btrfs_delayed_node *node)
600 struct btrfs_fs_info *fs_info = root->fs_info;
601 struct btrfs_block_rsv *src_rsv;
602 struct btrfs_block_rsv *dst_rsv;
603 u64 num_bytes;
604 int ret;
606 src_rsv = trans->block_rsv;
607 dst_rsv = &fs_info->delayed_block_rsv;
609 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
612 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
613 * which doesn't reserve space for speed. This is a problem since we
614 * still need to reserve space for this update, so try to reserve the
615 * space.
617 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
618 * we always reserve enough to update the inode item.
620 if (!src_rsv || (!trans->bytes_reserved &&
621 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
622 ret = btrfs_qgroup_reserve_meta_prealloc(root,
623 fs_info->nodesize, true);
624 if (ret < 0)
625 return ret;
626 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
627 BTRFS_RESERVE_NO_FLUSH);
629 * Since we're under a transaction reserve_metadata_bytes could
630 * try to commit the transaction which will make it return
631 * EAGAIN to make us stop the transaction we have, so return
632 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
634 if (ret == -EAGAIN) {
635 ret = -ENOSPC;
636 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
638 if (!ret) {
639 node->bytes_reserved = num_bytes;
640 trace_btrfs_space_reservation(fs_info,
641 "delayed_inode",
642 btrfs_ino(inode),
643 num_bytes, 1);
644 } else {
645 btrfs_qgroup_free_meta_prealloc(root, fs_info->nodesize);
647 return ret;
650 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
651 if (!ret) {
652 trace_btrfs_space_reservation(fs_info, "delayed_inode",
653 btrfs_ino(inode), num_bytes, 1);
654 node->bytes_reserved = num_bytes;
657 return ret;
660 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
661 struct btrfs_delayed_node *node,
662 bool qgroup_free)
664 struct btrfs_block_rsv *rsv;
666 if (!node->bytes_reserved)
667 return;
669 rsv = &fs_info->delayed_block_rsv;
670 trace_btrfs_space_reservation(fs_info, "delayed_inode",
671 node->inode_id, node->bytes_reserved, 0);
672 btrfs_block_rsv_release(fs_info, rsv,
673 node->bytes_reserved);
674 if (qgroup_free)
675 btrfs_qgroup_free_meta_prealloc(node->root,
676 node->bytes_reserved);
677 else
678 btrfs_qgroup_convert_reserved_meta(node->root,
679 node->bytes_reserved);
680 node->bytes_reserved = 0;
684 * This helper will insert some continuous items into the same leaf according
685 * to the free space of the leaf.
687 static int btrfs_batch_insert_items(struct btrfs_root *root,
688 struct btrfs_path *path,
689 struct btrfs_delayed_item *item)
691 struct btrfs_fs_info *fs_info = root->fs_info;
692 struct btrfs_delayed_item *curr, *next;
693 int free_space;
694 int total_data_size = 0, total_size = 0;
695 struct extent_buffer *leaf;
696 char *data_ptr;
697 struct btrfs_key *keys;
698 u32 *data_size;
699 struct list_head head;
700 int slot;
701 int nitems;
702 int i;
703 int ret = 0;
705 BUG_ON(!path->nodes[0]);
707 leaf = path->nodes[0];
708 free_space = btrfs_leaf_free_space(fs_info, leaf);
709 INIT_LIST_HEAD(&head);
711 next = item;
712 nitems = 0;
715 * count the number of the continuous items that we can insert in batch
717 while (total_size + next->data_len + sizeof(struct btrfs_item) <=
718 free_space) {
719 total_data_size += next->data_len;
720 total_size += next->data_len + sizeof(struct btrfs_item);
721 list_add_tail(&next->tree_list, &head);
722 nitems++;
724 curr = next;
725 next = __btrfs_next_delayed_item(curr);
726 if (!next)
727 break;
729 if (!btrfs_is_continuous_delayed_item(curr, next))
730 break;
733 if (!nitems) {
734 ret = 0;
735 goto out;
739 * we need allocate some memory space, but it might cause the task
740 * to sleep, so we set all locked nodes in the path to blocking locks
741 * first.
743 btrfs_set_path_blocking(path);
745 keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
746 if (!keys) {
747 ret = -ENOMEM;
748 goto out;
751 data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
752 if (!data_size) {
753 ret = -ENOMEM;
754 goto error;
757 /* get keys of all the delayed items */
758 i = 0;
759 list_for_each_entry(next, &head, tree_list) {
760 keys[i] = next->key;
761 data_size[i] = next->data_len;
762 i++;
765 /* reset all the locked nodes in the patch to spinning locks. */
766 btrfs_clear_path_blocking(path, NULL, 0);
768 /* insert the keys of the items */
769 setup_items_for_insert(root, path, keys, data_size,
770 total_data_size, total_size, nitems);
772 /* insert the dir index items */
773 slot = path->slots[0];
774 list_for_each_entry_safe(curr, next, &head, tree_list) {
775 data_ptr = btrfs_item_ptr(leaf, slot, char);
776 write_extent_buffer(leaf, &curr->data,
777 (unsigned long)data_ptr,
778 curr->data_len);
779 slot++;
781 btrfs_delayed_item_release_metadata(root, curr);
783 list_del(&curr->tree_list);
784 btrfs_release_delayed_item(curr);
787 error:
788 kfree(data_size);
789 kfree(keys);
790 out:
791 return ret;
795 * This helper can just do simple insertion that needn't extend item for new
796 * data, such as directory name index insertion, inode insertion.
798 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
799 struct btrfs_root *root,
800 struct btrfs_path *path,
801 struct btrfs_delayed_item *delayed_item)
803 struct extent_buffer *leaf;
804 char *ptr;
805 int ret;
807 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
808 delayed_item->data_len);
809 if (ret < 0 && ret != -EEXIST)
810 return ret;
812 leaf = path->nodes[0];
814 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
816 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
817 delayed_item->data_len);
818 btrfs_mark_buffer_dirty(leaf);
820 btrfs_delayed_item_release_metadata(root, delayed_item);
821 return 0;
825 * we insert an item first, then if there are some continuous items, we try
826 * to insert those items into the same leaf.
828 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
829 struct btrfs_path *path,
830 struct btrfs_root *root,
831 struct btrfs_delayed_node *node)
833 struct btrfs_delayed_item *curr, *prev;
834 int ret = 0;
836 do_again:
837 mutex_lock(&node->mutex);
838 curr = __btrfs_first_delayed_insertion_item(node);
839 if (!curr)
840 goto insert_end;
842 ret = btrfs_insert_delayed_item(trans, root, path, curr);
843 if (ret < 0) {
844 btrfs_release_path(path);
845 goto insert_end;
848 prev = curr;
849 curr = __btrfs_next_delayed_item(prev);
850 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
851 /* insert the continuous items into the same leaf */
852 path->slots[0]++;
853 btrfs_batch_insert_items(root, path, curr);
855 btrfs_release_delayed_item(prev);
856 btrfs_mark_buffer_dirty(path->nodes[0]);
858 btrfs_release_path(path);
859 mutex_unlock(&node->mutex);
860 goto do_again;
862 insert_end:
863 mutex_unlock(&node->mutex);
864 return ret;
867 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
868 struct btrfs_root *root,
869 struct btrfs_path *path,
870 struct btrfs_delayed_item *item)
872 struct btrfs_delayed_item *curr, *next;
873 struct extent_buffer *leaf;
874 struct btrfs_key key;
875 struct list_head head;
876 int nitems, i, last_item;
877 int ret = 0;
879 BUG_ON(!path->nodes[0]);
881 leaf = path->nodes[0];
883 i = path->slots[0];
884 last_item = btrfs_header_nritems(leaf) - 1;
885 if (i > last_item)
886 return -ENOENT; /* FIXME: Is errno suitable? */
888 next = item;
889 INIT_LIST_HEAD(&head);
890 btrfs_item_key_to_cpu(leaf, &key, i);
891 nitems = 0;
893 * count the number of the dir index items that we can delete in batch
895 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
896 list_add_tail(&next->tree_list, &head);
897 nitems++;
899 curr = next;
900 next = __btrfs_next_delayed_item(curr);
901 if (!next)
902 break;
904 if (!btrfs_is_continuous_delayed_item(curr, next))
905 break;
907 i++;
908 if (i > last_item)
909 break;
910 btrfs_item_key_to_cpu(leaf, &key, i);
913 if (!nitems)
914 return 0;
916 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
917 if (ret)
918 goto out;
920 list_for_each_entry_safe(curr, next, &head, tree_list) {
921 btrfs_delayed_item_release_metadata(root, curr);
922 list_del(&curr->tree_list);
923 btrfs_release_delayed_item(curr);
926 out:
927 return ret;
930 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
931 struct btrfs_path *path,
932 struct btrfs_root *root,
933 struct btrfs_delayed_node *node)
935 struct btrfs_delayed_item *curr, *prev;
936 int ret = 0;
938 do_again:
939 mutex_lock(&node->mutex);
940 curr = __btrfs_first_delayed_deletion_item(node);
941 if (!curr)
942 goto delete_fail;
944 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
945 if (ret < 0)
946 goto delete_fail;
947 else if (ret > 0) {
949 * can't find the item which the node points to, so this node
950 * is invalid, just drop it.
952 prev = curr;
953 curr = __btrfs_next_delayed_item(prev);
954 btrfs_release_delayed_item(prev);
955 ret = 0;
956 btrfs_release_path(path);
957 if (curr) {
958 mutex_unlock(&node->mutex);
959 goto do_again;
960 } else
961 goto delete_fail;
964 btrfs_batch_delete_items(trans, root, path, curr);
965 btrfs_release_path(path);
966 mutex_unlock(&node->mutex);
967 goto do_again;
969 delete_fail:
970 btrfs_release_path(path);
971 mutex_unlock(&node->mutex);
972 return ret;
975 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
977 struct btrfs_delayed_root *delayed_root;
979 if (delayed_node &&
980 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
981 BUG_ON(!delayed_node->root);
982 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
983 delayed_node->count--;
985 delayed_root = delayed_node->root->fs_info->delayed_root;
986 finish_one_item(delayed_root);
990 static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
992 struct btrfs_delayed_root *delayed_root;
994 ASSERT(delayed_node->root);
995 clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
996 delayed_node->count--;
998 delayed_root = delayed_node->root->fs_info->delayed_root;
999 finish_one_item(delayed_root);
1002 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1003 struct btrfs_root *root,
1004 struct btrfs_path *path,
1005 struct btrfs_delayed_node *node)
1007 struct btrfs_fs_info *fs_info = root->fs_info;
1008 struct btrfs_key key;
1009 struct btrfs_inode_item *inode_item;
1010 struct extent_buffer *leaf;
1011 int mod;
1012 int ret;
1014 key.objectid = node->inode_id;
1015 key.type = BTRFS_INODE_ITEM_KEY;
1016 key.offset = 0;
1018 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1019 mod = -1;
1020 else
1021 mod = 1;
1023 ret = btrfs_lookup_inode(trans, root, path, &key, mod);
1024 if (ret > 0) {
1025 btrfs_release_path(path);
1026 return -ENOENT;
1027 } else if (ret < 0) {
1028 return ret;
1031 leaf = path->nodes[0];
1032 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_inode_item);
1034 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1035 sizeof(struct btrfs_inode_item));
1036 btrfs_mark_buffer_dirty(leaf);
1038 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1039 goto no_iref;
1041 path->slots[0]++;
1042 if (path->slots[0] >= btrfs_header_nritems(leaf))
1043 goto search;
1044 again:
1045 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1046 if (key.objectid != node->inode_id)
1047 goto out;
1049 if (key.type != BTRFS_INODE_REF_KEY &&
1050 key.type != BTRFS_INODE_EXTREF_KEY)
1051 goto out;
1054 * Delayed iref deletion is for the inode who has only one link,
1055 * so there is only one iref. The case that several irefs are
1056 * in the same item doesn't exist.
1058 btrfs_del_item(trans, root, path);
1059 out:
1060 btrfs_release_delayed_iref(node);
1061 no_iref:
1062 btrfs_release_path(path);
1063 err_out:
1064 btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
1065 btrfs_release_delayed_inode(node);
1067 return ret;
1069 search:
1070 btrfs_release_path(path);
1072 key.type = BTRFS_INODE_EXTREF_KEY;
1073 key.offset = -1;
1074 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1075 if (ret < 0)
1076 goto err_out;
1077 ASSERT(ret);
1079 ret = 0;
1080 leaf = path->nodes[0];
1081 path->slots[0]--;
1082 goto again;
1085 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1086 struct btrfs_root *root,
1087 struct btrfs_path *path,
1088 struct btrfs_delayed_node *node)
1090 int ret;
1092 mutex_lock(&node->mutex);
1093 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
1094 mutex_unlock(&node->mutex);
1095 return 0;
1098 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1099 mutex_unlock(&node->mutex);
1100 return ret;
1103 static inline int
1104 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1105 struct btrfs_path *path,
1106 struct btrfs_delayed_node *node)
1108 int ret;
1110 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1111 if (ret)
1112 return ret;
1114 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1115 if (ret)
1116 return ret;
1118 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1119 return ret;
1123 * Called when committing the transaction.
1124 * Returns 0 on success.
1125 * Returns < 0 on error and returns with an aborted transaction with any
1126 * outstanding delayed items cleaned up.
1128 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
1130 struct btrfs_fs_info *fs_info = trans->fs_info;
1131 struct btrfs_delayed_root *delayed_root;
1132 struct btrfs_delayed_node *curr_node, *prev_node;
1133 struct btrfs_path *path;
1134 struct btrfs_block_rsv *block_rsv;
1135 int ret = 0;
1136 bool count = (nr > 0);
1138 if (trans->aborted)
1139 return -EIO;
1141 path = btrfs_alloc_path();
1142 if (!path)
1143 return -ENOMEM;
1144 path->leave_spinning = 1;
1146 block_rsv = trans->block_rsv;
1147 trans->block_rsv = &fs_info->delayed_block_rsv;
1149 delayed_root = fs_info->delayed_root;
1151 curr_node = btrfs_first_delayed_node(delayed_root);
1152 while (curr_node && (!count || (count && nr--))) {
1153 ret = __btrfs_commit_inode_delayed_items(trans, path,
1154 curr_node);
1155 if (ret) {
1156 btrfs_release_delayed_node(curr_node);
1157 curr_node = NULL;
1158 btrfs_abort_transaction(trans, ret);
1159 break;
1162 prev_node = curr_node;
1163 curr_node = btrfs_next_delayed_node(curr_node);
1164 btrfs_release_delayed_node(prev_node);
1167 if (curr_node)
1168 btrfs_release_delayed_node(curr_node);
1169 btrfs_free_path(path);
1170 trans->block_rsv = block_rsv;
1172 return ret;
1175 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
1177 return __btrfs_run_delayed_items(trans, -1);
1180 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
1182 return __btrfs_run_delayed_items(trans, nr);
1185 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1186 struct btrfs_inode *inode)
1188 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1189 struct btrfs_path *path;
1190 struct btrfs_block_rsv *block_rsv;
1191 int ret;
1193 if (!delayed_node)
1194 return 0;
1196 mutex_lock(&delayed_node->mutex);
1197 if (!delayed_node->count) {
1198 mutex_unlock(&delayed_node->mutex);
1199 btrfs_release_delayed_node(delayed_node);
1200 return 0;
1202 mutex_unlock(&delayed_node->mutex);
1204 path = btrfs_alloc_path();
1205 if (!path) {
1206 btrfs_release_delayed_node(delayed_node);
1207 return -ENOMEM;
1209 path->leave_spinning = 1;
1211 block_rsv = trans->block_rsv;
1212 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1214 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1216 btrfs_release_delayed_node(delayed_node);
1217 btrfs_free_path(path);
1218 trans->block_rsv = block_rsv;
1220 return ret;
1223 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
1225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1226 struct btrfs_trans_handle *trans;
1227 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1228 struct btrfs_path *path;
1229 struct btrfs_block_rsv *block_rsv;
1230 int ret;
1232 if (!delayed_node)
1233 return 0;
1235 mutex_lock(&delayed_node->mutex);
1236 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1237 mutex_unlock(&delayed_node->mutex);
1238 btrfs_release_delayed_node(delayed_node);
1239 return 0;
1241 mutex_unlock(&delayed_node->mutex);
1243 trans = btrfs_join_transaction(delayed_node->root);
1244 if (IS_ERR(trans)) {
1245 ret = PTR_ERR(trans);
1246 goto out;
1249 path = btrfs_alloc_path();
1250 if (!path) {
1251 ret = -ENOMEM;
1252 goto trans_out;
1254 path->leave_spinning = 1;
1256 block_rsv = trans->block_rsv;
1257 trans->block_rsv = &fs_info->delayed_block_rsv;
1259 mutex_lock(&delayed_node->mutex);
1260 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
1261 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1262 path, delayed_node);
1263 else
1264 ret = 0;
1265 mutex_unlock(&delayed_node->mutex);
1267 btrfs_free_path(path);
1268 trans->block_rsv = block_rsv;
1269 trans_out:
1270 btrfs_end_transaction(trans);
1271 btrfs_btree_balance_dirty(fs_info);
1272 out:
1273 btrfs_release_delayed_node(delayed_node);
1275 return ret;
1278 void btrfs_remove_delayed_node(struct btrfs_inode *inode)
1280 struct btrfs_delayed_node *delayed_node;
1282 delayed_node = READ_ONCE(inode->delayed_node);
1283 if (!delayed_node)
1284 return;
1286 inode->delayed_node = NULL;
1287 btrfs_release_delayed_node(delayed_node);
1290 struct btrfs_async_delayed_work {
1291 struct btrfs_delayed_root *delayed_root;
1292 int nr;
1293 struct btrfs_work work;
1296 static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1298 struct btrfs_async_delayed_work *async_work;
1299 struct btrfs_delayed_root *delayed_root;
1300 struct btrfs_trans_handle *trans;
1301 struct btrfs_path *path;
1302 struct btrfs_delayed_node *delayed_node = NULL;
1303 struct btrfs_root *root;
1304 struct btrfs_block_rsv *block_rsv;
1305 int total_done = 0;
1307 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1308 delayed_root = async_work->delayed_root;
1310 path = btrfs_alloc_path();
1311 if (!path)
1312 goto out;
1314 do {
1315 if (atomic_read(&delayed_root->items) <
1316 BTRFS_DELAYED_BACKGROUND / 2)
1317 break;
1319 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1320 if (!delayed_node)
1321 break;
1323 path->leave_spinning = 1;
1324 root = delayed_node->root;
1326 trans = btrfs_join_transaction(root);
1327 if (IS_ERR(trans)) {
1328 btrfs_release_path(path);
1329 btrfs_release_prepared_delayed_node(delayed_node);
1330 total_done++;
1331 continue;
1334 block_rsv = trans->block_rsv;
1335 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1337 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1339 trans->block_rsv = block_rsv;
1340 btrfs_end_transaction(trans);
1341 btrfs_btree_balance_dirty_nodelay(root->fs_info);
1343 btrfs_release_path(path);
1344 btrfs_release_prepared_delayed_node(delayed_node);
1345 total_done++;
1347 } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
1348 || total_done < async_work->nr);
1350 btrfs_free_path(path);
1351 out:
1352 wake_up(&delayed_root->wait);
1353 kfree(async_work);
1357 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1358 struct btrfs_fs_info *fs_info, int nr)
1360 struct btrfs_async_delayed_work *async_work;
1362 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1363 if (!async_work)
1364 return -ENOMEM;
1366 async_work->delayed_root = delayed_root;
1367 btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper,
1368 btrfs_async_run_delayed_root, NULL, NULL);
1369 async_work->nr = nr;
1371 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
1372 return 0;
1375 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
1377 WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
1380 static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1382 int val = atomic_read(&delayed_root->items_seq);
1384 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
1385 return 1;
1387 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1388 return 1;
1390 return 0;
1393 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1395 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1397 if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
1398 btrfs_workqueue_normal_congested(fs_info->delayed_workers))
1399 return;
1401 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1402 int seq;
1403 int ret;
1405 seq = atomic_read(&delayed_root->items_seq);
1407 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
1408 if (ret)
1409 return;
1411 wait_event_interruptible(delayed_root->wait,
1412 could_end_wait(delayed_root, seq));
1413 return;
1416 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
1419 /* Will return 0 or -ENOMEM */
1420 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1421 struct btrfs_fs_info *fs_info,
1422 const char *name, int name_len,
1423 struct btrfs_inode *dir,
1424 struct btrfs_disk_key *disk_key, u8 type,
1425 u64 index)
1427 struct btrfs_delayed_node *delayed_node;
1428 struct btrfs_delayed_item *delayed_item;
1429 struct btrfs_dir_item *dir_item;
1430 int ret;
1432 delayed_node = btrfs_get_or_create_delayed_node(dir);
1433 if (IS_ERR(delayed_node))
1434 return PTR_ERR(delayed_node);
1436 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1437 if (!delayed_item) {
1438 ret = -ENOMEM;
1439 goto release_node;
1442 delayed_item->key.objectid = btrfs_ino(dir);
1443 delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
1444 delayed_item->key.offset = index;
1446 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1447 dir_item->location = *disk_key;
1448 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1449 btrfs_set_stack_dir_data_len(dir_item, 0);
1450 btrfs_set_stack_dir_name_len(dir_item, name_len);
1451 btrfs_set_stack_dir_type(dir_item, type);
1452 memcpy((char *)(dir_item + 1), name, name_len);
1454 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item);
1456 * we have reserved enough space when we start a new transaction,
1457 * so reserving metadata failure is impossible
1459 BUG_ON(ret);
1462 mutex_lock(&delayed_node->mutex);
1463 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1464 if (unlikely(ret)) {
1465 btrfs_err(fs_info,
1466 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1467 name_len, name, delayed_node->root->objectid,
1468 delayed_node->inode_id, ret);
1469 BUG();
1471 mutex_unlock(&delayed_node->mutex);
1473 release_node:
1474 btrfs_release_delayed_node(delayed_node);
1475 return ret;
1478 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1479 struct btrfs_delayed_node *node,
1480 struct btrfs_key *key)
1482 struct btrfs_delayed_item *item;
1484 mutex_lock(&node->mutex);
1485 item = __btrfs_lookup_delayed_insertion_item(node, key);
1486 if (!item) {
1487 mutex_unlock(&node->mutex);
1488 return 1;
1491 btrfs_delayed_item_release_metadata(node->root, item);
1492 btrfs_release_delayed_item(item);
1493 mutex_unlock(&node->mutex);
1494 return 0;
1497 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1498 struct btrfs_fs_info *fs_info,
1499 struct btrfs_inode *dir, u64 index)
1501 struct btrfs_delayed_node *node;
1502 struct btrfs_delayed_item *item;
1503 struct btrfs_key item_key;
1504 int ret;
1506 node = btrfs_get_or_create_delayed_node(dir);
1507 if (IS_ERR(node))
1508 return PTR_ERR(node);
1510 item_key.objectid = btrfs_ino(dir);
1511 item_key.type = BTRFS_DIR_INDEX_KEY;
1512 item_key.offset = index;
1514 ret = btrfs_delete_delayed_insertion_item(fs_info, node, &item_key);
1515 if (!ret)
1516 goto end;
1518 item = btrfs_alloc_delayed_item(0);
1519 if (!item) {
1520 ret = -ENOMEM;
1521 goto end;
1524 item->key = item_key;
1526 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item);
1528 * we have reserved enough space when we start a new transaction,
1529 * so reserving metadata failure is impossible.
1531 BUG_ON(ret);
1533 mutex_lock(&node->mutex);
1534 ret = __btrfs_add_delayed_deletion_item(node, item);
1535 if (unlikely(ret)) {
1536 btrfs_err(fs_info,
1537 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1538 index, node->root->objectid, node->inode_id, ret);
1539 BUG();
1541 mutex_unlock(&node->mutex);
1542 end:
1543 btrfs_release_delayed_node(node);
1544 return ret;
1547 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
1549 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1551 if (!delayed_node)
1552 return -ENOENT;
1555 * Since we have held i_mutex of this directory, it is impossible that
1556 * a new directory index is added into the delayed node and index_cnt
1557 * is updated now. So we needn't lock the delayed node.
1559 if (!delayed_node->index_cnt) {
1560 btrfs_release_delayed_node(delayed_node);
1561 return -EINVAL;
1564 inode->index_cnt = delayed_node->index_cnt;
1565 btrfs_release_delayed_node(delayed_node);
1566 return 0;
1569 bool btrfs_readdir_get_delayed_items(struct inode *inode,
1570 struct list_head *ins_list,
1571 struct list_head *del_list)
1573 struct btrfs_delayed_node *delayed_node;
1574 struct btrfs_delayed_item *item;
1576 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1577 if (!delayed_node)
1578 return false;
1581 * We can only do one readdir with delayed items at a time because of
1582 * item->readdir_list.
1584 inode_unlock_shared(inode);
1585 inode_lock(inode);
1587 mutex_lock(&delayed_node->mutex);
1588 item = __btrfs_first_delayed_insertion_item(delayed_node);
1589 while (item) {
1590 refcount_inc(&item->refs);
1591 list_add_tail(&item->readdir_list, ins_list);
1592 item = __btrfs_next_delayed_item(item);
1595 item = __btrfs_first_delayed_deletion_item(delayed_node);
1596 while (item) {
1597 refcount_inc(&item->refs);
1598 list_add_tail(&item->readdir_list, del_list);
1599 item = __btrfs_next_delayed_item(item);
1601 mutex_unlock(&delayed_node->mutex);
1603 * This delayed node is still cached in the btrfs inode, so refs
1604 * must be > 1 now, and we needn't check it is going to be freed
1605 * or not.
1607 * Besides that, this function is used to read dir, we do not
1608 * insert/delete delayed items in this period. So we also needn't
1609 * requeue or dequeue this delayed node.
1611 refcount_dec(&delayed_node->refs);
1613 return true;
1616 void btrfs_readdir_put_delayed_items(struct inode *inode,
1617 struct list_head *ins_list,
1618 struct list_head *del_list)
1620 struct btrfs_delayed_item *curr, *next;
1622 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1623 list_del(&curr->readdir_list);
1624 if (refcount_dec_and_test(&curr->refs))
1625 kfree(curr);
1628 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1629 list_del(&curr->readdir_list);
1630 if (refcount_dec_and_test(&curr->refs))
1631 kfree(curr);
1635 * The VFS is going to do up_read(), so we need to downgrade back to a
1636 * read lock.
1638 downgrade_write(&inode->i_rwsem);
1641 int btrfs_should_delete_dir_index(struct list_head *del_list,
1642 u64 index)
1644 struct btrfs_delayed_item *curr;
1645 int ret = 0;
1647 list_for_each_entry(curr, del_list, readdir_list) {
1648 if (curr->key.offset > index)
1649 break;
1650 if (curr->key.offset == index) {
1651 ret = 1;
1652 break;
1655 return ret;
1659 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1662 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1663 struct list_head *ins_list)
1665 struct btrfs_dir_item *di;
1666 struct btrfs_delayed_item *curr, *next;
1667 struct btrfs_key location;
1668 char *name;
1669 int name_len;
1670 int over = 0;
1671 unsigned char d_type;
1673 if (list_empty(ins_list))
1674 return 0;
1677 * Changing the data of the delayed item is impossible. So
1678 * we needn't lock them. And we have held i_mutex of the
1679 * directory, nobody can delete any directory indexes now.
1681 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1682 list_del(&curr->readdir_list);
1684 if (curr->key.offset < ctx->pos) {
1685 if (refcount_dec_and_test(&curr->refs))
1686 kfree(curr);
1687 continue;
1690 ctx->pos = curr->key.offset;
1692 di = (struct btrfs_dir_item *)curr->data;
1693 name = (char *)(di + 1);
1694 name_len = btrfs_stack_dir_name_len(di);
1696 d_type = btrfs_filetype_table[di->type];
1697 btrfs_disk_key_to_cpu(&location, &di->location);
1699 over = !dir_emit(ctx, name, name_len,
1700 location.objectid, d_type);
1702 if (refcount_dec_and_test(&curr->refs))
1703 kfree(curr);
1705 if (over)
1706 return 1;
1707 ctx->pos++;
1709 return 0;
1712 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1713 struct btrfs_inode_item *inode_item,
1714 struct inode *inode)
1716 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1717 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1718 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1719 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1720 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1721 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1722 btrfs_set_stack_inode_generation(inode_item,
1723 BTRFS_I(inode)->generation);
1724 btrfs_set_stack_inode_sequence(inode_item,
1725 inode_peek_iversion(inode));
1726 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1727 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1728 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
1729 btrfs_set_stack_inode_block_group(inode_item, 0);
1731 btrfs_set_stack_timespec_sec(&inode_item->atime,
1732 inode->i_atime.tv_sec);
1733 btrfs_set_stack_timespec_nsec(&inode_item->atime,
1734 inode->i_atime.tv_nsec);
1736 btrfs_set_stack_timespec_sec(&inode_item->mtime,
1737 inode->i_mtime.tv_sec);
1738 btrfs_set_stack_timespec_nsec(&inode_item->mtime,
1739 inode->i_mtime.tv_nsec);
1741 btrfs_set_stack_timespec_sec(&inode_item->ctime,
1742 inode->i_ctime.tv_sec);
1743 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
1744 inode->i_ctime.tv_nsec);
1746 btrfs_set_stack_timespec_sec(&inode_item->otime,
1747 BTRFS_I(inode)->i_otime.tv_sec);
1748 btrfs_set_stack_timespec_nsec(&inode_item->otime,
1749 BTRFS_I(inode)->i_otime.tv_nsec);
1752 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1754 struct btrfs_delayed_node *delayed_node;
1755 struct btrfs_inode_item *inode_item;
1757 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1758 if (!delayed_node)
1759 return -ENOENT;
1761 mutex_lock(&delayed_node->mutex);
1762 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1763 mutex_unlock(&delayed_node->mutex);
1764 btrfs_release_delayed_node(delayed_node);
1765 return -ENOENT;
1768 inode_item = &delayed_node->inode_item;
1770 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1771 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1772 btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
1773 inode->i_mode = btrfs_stack_inode_mode(inode_item);
1774 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1775 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1776 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1777 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1779 inode_set_iversion_queried(inode,
1780 btrfs_stack_inode_sequence(inode_item));
1781 inode->i_rdev = 0;
1782 *rdev = btrfs_stack_inode_rdev(inode_item);
1783 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1785 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
1786 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
1788 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
1789 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
1791 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
1792 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
1794 BTRFS_I(inode)->i_otime.tv_sec =
1795 btrfs_stack_timespec_sec(&inode_item->otime);
1796 BTRFS_I(inode)->i_otime.tv_nsec =
1797 btrfs_stack_timespec_nsec(&inode_item->otime);
1799 inode->i_generation = BTRFS_I(inode)->generation;
1800 BTRFS_I(inode)->index_cnt = (u64)-1;
1802 mutex_unlock(&delayed_node->mutex);
1803 btrfs_release_delayed_node(delayed_node);
1804 return 0;
1807 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1808 struct btrfs_root *root, struct inode *inode)
1810 struct btrfs_delayed_node *delayed_node;
1811 int ret = 0;
1813 delayed_node = btrfs_get_or_create_delayed_node(BTRFS_I(inode));
1814 if (IS_ERR(delayed_node))
1815 return PTR_ERR(delayed_node);
1817 mutex_lock(&delayed_node->mutex);
1818 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1819 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1820 goto release_node;
1823 ret = btrfs_delayed_inode_reserve_metadata(trans, root, BTRFS_I(inode),
1824 delayed_node);
1825 if (ret)
1826 goto release_node;
1828 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1829 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
1830 delayed_node->count++;
1831 atomic_inc(&root->fs_info->delayed_root->items);
1832 release_node:
1833 mutex_unlock(&delayed_node->mutex);
1834 btrfs_release_delayed_node(delayed_node);
1835 return ret;
1838 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
1840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1841 struct btrfs_delayed_node *delayed_node;
1844 * we don't do delayed inode updates during log recovery because it
1845 * leads to enospc problems. This means we also can't do
1846 * delayed inode refs
1848 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
1849 return -EAGAIN;
1851 delayed_node = btrfs_get_or_create_delayed_node(inode);
1852 if (IS_ERR(delayed_node))
1853 return PTR_ERR(delayed_node);
1856 * We don't reserve space for inode ref deletion is because:
1857 * - We ONLY do async inode ref deletion for the inode who has only
1858 * one link(i_nlink == 1), it means there is only one inode ref.
1859 * And in most case, the inode ref and the inode item are in the
1860 * same leaf, and we will deal with them at the same time.
1861 * Since we are sure we will reserve the space for the inode item,
1862 * it is unnecessary to reserve space for inode ref deletion.
1863 * - If the inode ref and the inode item are not in the same leaf,
1864 * We also needn't worry about enospc problem, because we reserve
1865 * much more space for the inode update than it needs.
1866 * - At the worst, we can steal some space from the global reservation.
1867 * It is very rare.
1869 mutex_lock(&delayed_node->mutex);
1870 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1871 goto release_node;
1873 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1874 delayed_node->count++;
1875 atomic_inc(&fs_info->delayed_root->items);
1876 release_node:
1877 mutex_unlock(&delayed_node->mutex);
1878 btrfs_release_delayed_node(delayed_node);
1879 return 0;
1882 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1884 struct btrfs_root *root = delayed_node->root;
1885 struct btrfs_fs_info *fs_info = root->fs_info;
1886 struct btrfs_delayed_item *curr_item, *prev_item;
1888 mutex_lock(&delayed_node->mutex);
1889 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1890 while (curr_item) {
1891 btrfs_delayed_item_release_metadata(root, curr_item);
1892 prev_item = curr_item;
1893 curr_item = __btrfs_next_delayed_item(prev_item);
1894 btrfs_release_delayed_item(prev_item);
1897 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1898 while (curr_item) {
1899 btrfs_delayed_item_release_metadata(root, curr_item);
1900 prev_item = curr_item;
1901 curr_item = __btrfs_next_delayed_item(prev_item);
1902 btrfs_release_delayed_item(prev_item);
1905 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1906 btrfs_release_delayed_iref(delayed_node);
1908 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1909 btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false);
1910 btrfs_release_delayed_inode(delayed_node);
1912 mutex_unlock(&delayed_node->mutex);
1915 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
1917 struct btrfs_delayed_node *delayed_node;
1919 delayed_node = btrfs_get_delayed_node(inode);
1920 if (!delayed_node)
1921 return;
1923 __btrfs_kill_delayed_node(delayed_node);
1924 btrfs_release_delayed_node(delayed_node);
1927 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1929 u64 inode_id = 0;
1930 struct btrfs_delayed_node *delayed_nodes[8];
1931 int i, n;
1933 while (1) {
1934 spin_lock(&root->inode_lock);
1935 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1936 (void **)delayed_nodes, inode_id,
1937 ARRAY_SIZE(delayed_nodes));
1938 if (!n) {
1939 spin_unlock(&root->inode_lock);
1940 break;
1943 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1945 for (i = 0; i < n; i++)
1946 refcount_inc(&delayed_nodes[i]->refs);
1947 spin_unlock(&root->inode_lock);
1949 for (i = 0; i < n; i++) {
1950 __btrfs_kill_delayed_node(delayed_nodes[i]);
1951 btrfs_release_delayed_node(delayed_nodes[i]);
1956 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
1958 struct btrfs_delayed_node *curr_node, *prev_node;
1960 curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
1961 while (curr_node) {
1962 __btrfs_kill_delayed_node(curr_node);
1964 prev_node = curr_node;
1965 curr_node = btrfs_next_delayed_node(curr_node);
1966 btrfs_release_delayed_node(prev_node);