LCOV - code coverage report
Current view: top level - fs/btrfs - delayed-inode.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwx @ Mon Jul 31 20:08:22 PDT 2023 Lines: 1048 1110 94.4 %
Date: 2023-07-31 20:08:22 Functions: 57 58 98.3 %

          Line data    Source code
       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             :  */
       6             : 
       7             : #include <linux/slab.h>
       8             : #include <linux/iversion.h>
       9             : #include "ctree.h"
      10             : #include "fs.h"
      11             : #include "messages.h"
      12             : #include "misc.h"
      13             : #include "delayed-inode.h"
      14             : #include "disk-io.h"
      15             : #include "transaction.h"
      16             : #include "qgroup.h"
      17             : #include "locking.h"
      18             : #include "inode-item.h"
      19             : #include "space-info.h"
      20             : #include "accessors.h"
      21             : #include "file-item.h"
      22             : 
      23             : #define BTRFS_DELAYED_WRITEBACK         512
      24             : #define BTRFS_DELAYED_BACKGROUND        128
      25             : #define BTRFS_DELAYED_BATCH             16
      26             : 
      27             : static struct kmem_cache *delayed_node_cache;
      28             : 
      29          11 : int __init btrfs_delayed_inode_init(void)
      30             : {
      31          11 :         delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
      32             :                                         sizeof(struct btrfs_delayed_node),
      33             :                                         0,
      34             :                                         SLAB_MEM_SPREAD,
      35             :                                         NULL);
      36          11 :         if (!delayed_node_cache)
      37           0 :                 return -ENOMEM;
      38             :         return 0;
      39             : }
      40             : 
      41           0 : void __cold btrfs_delayed_inode_exit(void)
      42             : {
      43           0 :         kmem_cache_destroy(delayed_node_cache);
      44           0 : }
      45             : 
      46     1934969 : static inline void btrfs_init_delayed_node(
      47             :                                 struct btrfs_delayed_node *delayed_node,
      48             :                                 struct btrfs_root *root, u64 inode_id)
      49             : {
      50     1934969 :         delayed_node->root = root;
      51     1934969 :         delayed_node->inode_id = inode_id;
      52     1934969 :         refcount_set(&delayed_node->refs, 0);
      53     1934969 :         delayed_node->ins_root = RB_ROOT_CACHED;
      54     1934969 :         delayed_node->del_root = RB_ROOT_CACHED;
      55     1934969 :         mutex_init(&delayed_node->mutex);
      56     1934803 :         INIT_LIST_HEAD(&delayed_node->n_list);
      57     1934803 :         INIT_LIST_HEAD(&delayed_node->p_list);
      58     1934803 : }
      59             : 
      60    49988820 : static struct btrfs_delayed_node *btrfs_get_delayed_node(
      61             :                 struct btrfs_inode *btrfs_inode)
      62             : {
      63    49988820 :         struct btrfs_root *root = btrfs_inode->root;
      64    49988820 :         u64 ino = btrfs_ino(btrfs_inode);
      65    49988820 :         struct btrfs_delayed_node *node;
      66             : 
      67    49988820 :         node = READ_ONCE(btrfs_inode->delayed_node);
      68    49988820 :         if (node) {
      69    44397555 :                 refcount_inc(&node->refs);
      70    44397555 :                 return node;
      71             :         }
      72             : 
      73     5591265 :         spin_lock(&root->inode_lock);
      74     5592475 :         node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
      75             : 
      76     5592459 :         if (node) {
      77         131 :                 if (btrfs_inode->delayed_node) {
      78           2 :                         refcount_inc(&node->refs);       /* can be accessed */
      79           2 :                         BUG_ON(btrfs_inode->delayed_node != node);
      80           2 :                         spin_unlock(&root->inode_lock);
      81           2 :                         return node;
      82             :                 }
      83             : 
      84             :                 /*
      85             :                  * It's possible that we're racing into the middle of removing
      86             :                  * this node from the radix tree.  In this case, the refcount
      87             :                  * was zero and it should never go back to one.  Just return
      88             :                  * NULL like it was never in the radix at all; our release
      89             :                  * function is in the process of removing it.
      90             :                  *
      91             :                  * Some implementations of refcount_inc refuse to bump the
      92             :                  * refcount once it has hit zero.  If we don't do this dance
      93             :                  * here, refcount_inc() may decide to just WARN_ONCE() instead
      94             :                  * of actually bumping the refcount.
      95             :                  *
      96             :                  * If this node is properly in the radix, we want to bump the
      97             :                  * refcount twice, once for the inode and once for this get
      98             :                  * operation.
      99             :                  */
     100         129 :                 if (refcount_inc_not_zero(&node->refs)) {
     101         129 :                         refcount_inc(&node->refs);
     102         129 :                         btrfs_inode->delayed_node = node;
     103             :                 } else {
     104             :                         node = NULL;
     105             :                 }
     106             : 
     107         129 :                 spin_unlock(&root->inode_lock);
     108         129 :                 return node;
     109             :         }
     110     5592328 :         spin_unlock(&root->inode_lock);
     111             : 
     112     5592328 :         return NULL;
     113             : }
     114             : 
     115             : /* Will return either the node or PTR_ERR(-ENOMEM) */
     116    43100464 : static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
     117             :                 struct btrfs_inode *btrfs_inode)
     118             : {
     119    43100464 :         struct btrfs_delayed_node *node;
     120    43100464 :         struct btrfs_root *root = btrfs_inode->root;
     121    43100464 :         u64 ino = btrfs_ino(btrfs_inode);
     122    43100476 :         int ret;
     123             : 
     124    43100476 : again:
     125    43100476 :         node = btrfs_get_delayed_node(btrfs_inode);
     126    43101872 :         if (node)
     127    41166018 :                 return node;
     128             : 
     129     1935854 :         node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
     130     1935065 :         if (!node)
     131             :                 return ERR_PTR(-ENOMEM);
     132     1935065 :         btrfs_init_delayed_node(node, root, ino);
     133             : 
     134             :         /* cached in the btrfs inode and can be accessed */
     135     1935033 :         refcount_set(&node->refs, 2);
     136             : 
     137     1935033 :         ret = radix_tree_preload(GFP_NOFS);
     138     1933178 :         if (ret) {
     139           0 :                 kmem_cache_free(delayed_node_cache, node);
     140           0 :                 return ERR_PTR(ret);
     141             :         }
     142             : 
     143     1933178 :         spin_lock(&root->inode_lock);
     144     1935862 :         ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
     145     1935862 :         if (ret == -EEXIST) {
     146          12 :                 spin_unlock(&root->inode_lock);
     147          12 :                 kmem_cache_free(delayed_node_cache, node);
     148          12 :                 radix_tree_preload_end();
     149          12 :                 goto again;
     150             :         }
     151     1935850 :         btrfs_inode->delayed_node = node;
     152     1935850 :         spin_unlock(&root->inode_lock);
     153     1935850 :         radix_tree_preload_end();
     154             : 
     155     1935850 :         return node;
     156             : }
     157             : 
     158             : /*
     159             :  * Call it when holding delayed_node->mutex
     160             :  *
     161             :  * If mod = 1, add this node into the prepared list.
     162             :  */
     163    43222035 : static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
     164             :                                      struct btrfs_delayed_node *node,
     165             :                                      int mod)
     166             : {
     167    43222035 :         spin_lock(&root->lock);
     168    86450618 :         if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
     169    34647325 :                 if (!list_empty(&node->p_list))
     170    34463359 :                         list_move_tail(&node->p_list, &root->prepare_list);
     171      183966 :                 else if (mod)
     172      169260 :                         list_add_tail(&node->p_list, &root->prepare_list);
     173             :         } else {
     174     8577984 :                 list_add_tail(&node->n_list, &root->node_list);
     175     8577984 :                 list_add_tail(&node->p_list, &root->prepare_list);
     176     8577984 :                 refcount_inc(&node->refs);       /* inserted into list */
     177     8577984 :                 root->nodes++;
     178     8577984 :                 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
     179             :         }
     180    43225306 :         spin_unlock(&root->lock);
     181    43225175 : }
     182             : 
     183             : /* Call it when holding delayed_node->mutex */
     184    12088732 : static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
     185             :                                        struct btrfs_delayed_node *node)
     186             : {
     187    12088732 :         spin_lock(&root->lock);
     188    24184634 :         if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
     189     8577984 :                 root->nodes--;
     190     8577984 :                 refcount_dec(&node->refs);       /* not in the list */
     191     8577984 :                 list_del_init(&node->n_list);
     192     8577984 :                 if (!list_empty(&node->p_list))
     193     3824847 :                         list_del_init(&node->p_list);
     194     8577984 :                 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
     195             :         }
     196    12092317 :         spin_unlock(&root->lock);
     197    12092207 : }
     198             : 
     199     2699709 : static struct btrfs_delayed_node *btrfs_first_delayed_node(
     200             :                         struct btrfs_delayed_root *delayed_root)
     201             : {
     202     2699709 :         struct list_head *p;
     203     2699709 :         struct btrfs_delayed_node *node = NULL;
     204             : 
     205     2699709 :         spin_lock(&delayed_root->lock);
     206     2699712 :         if (list_empty(&delayed_root->node_list))
     207     1652457 :                 goto out;
     208             : 
     209     1047255 :         p = delayed_root->node_list.next;
     210     1047255 :         node = list_entry(p, struct btrfs_delayed_node, n_list);
     211     1047255 :         refcount_inc(&node->refs);
     212     2699712 : out:
     213     2699712 :         spin_unlock(&delayed_root->lock);
     214             : 
     215     2699712 :         return node;
     216             : }
     217             : 
     218     2591439 : static struct btrfs_delayed_node *btrfs_next_delayed_node(
     219             :                                                 struct btrfs_delayed_node *node)
     220             : {
     221     2591439 :         struct btrfs_delayed_root *delayed_root;
     222     2591439 :         struct list_head *p;
     223     2591439 :         struct btrfs_delayed_node *next = NULL;
     224             : 
     225     2591439 :         delayed_root = node->root->fs_info->delayed_root;
     226     2591439 :         spin_lock(&delayed_root->lock);
     227     2591450 :         if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
     228             :                 /* not in the list */
     229       30091 :                 if (list_empty(&delayed_root->node_list))
     230         705 :                         goto out;
     231       29386 :                 p = delayed_root->node_list.next;
     232     2561359 :         } else if (list_is_last(&node->n_list, &delayed_root->node_list))
     233     1028265 :                 goto out;
     234             :         else
     235             :                 p = node->n_list.next;
     236             : 
     237     1562480 :         next = list_entry(p, struct btrfs_delayed_node, n_list);
     238     1562480 :         refcount_inc(&next->refs);
     239     2591450 : out:
     240     2591450 :         spin_unlock(&delayed_root->lock);
     241             : 
     242     2591438 :         return next;
     243             : }
     244             : 
     245    55302863 : static void __btrfs_release_delayed_node(
     246             :                                 struct btrfs_delayed_node *delayed_node,
     247             :                                 int mod)
     248             : {
     249    55302863 :         struct btrfs_delayed_root *delayed_root;
     250             : 
     251    55302863 :         if (!delayed_node)
     252             :                 return;
     253             : 
     254    55302863 :         delayed_root = delayed_node->root->fs_info->delayed_root;
     255             : 
     256    55302863 :         mutex_lock(&delayed_node->mutex);
     257    55313936 :         if (delayed_node->count)
     258    43223480 :                 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
     259             :         else
     260    12090456 :                 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
     261    55317297 :         mutex_unlock(&delayed_node->mutex);
     262             : 
     263    55316876 :         if (refcount_dec_and_test(&delayed_node->refs)) {
     264     1935849 :                 struct btrfs_root *root = delayed_node->root;
     265             : 
     266     1935849 :                 spin_lock(&root->inode_lock);
     267             :                 /*
     268             :                  * Once our refcount goes to zero, nobody is allowed to bump it
     269             :                  * back up.  We can delete it now.
     270             :                  */
     271     1935850 :                 ASSERT(refcount_read(&delayed_node->refs) == 0);
     272     1935850 :                 radix_tree_delete(&root->delayed_nodes_tree,
     273     1935850 :                                   delayed_node->inode_id);
     274     1935850 :                 spin_unlock(&root->inode_lock);
     275     1935850 :                 kmem_cache_free(delayed_node_cache, delayed_node);
     276             :         }
     277             : }
     278             : 
     279             : static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
     280             : {
     281    50393325 :         __btrfs_release_delayed_node(node, 0);
     282     5917453 : }
     283             : 
     284     4961496 : static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
     285             :                                         struct btrfs_delayed_root *delayed_root)
     286             : {
     287     4961496 :         struct list_head *p;
     288     4961496 :         struct btrfs_delayed_node *node = NULL;
     289             : 
     290     4961496 :         spin_lock(&delayed_root->lock);
     291     4962240 :         if (list_empty(&delayed_root->prepare_list))
     292       39843 :                 goto out;
     293             : 
     294     4922397 :         p = delayed_root->prepare_list.next;
     295     4922397 :         list_del_init(p);
     296     4922397 :         node = list_entry(p, struct btrfs_delayed_node, p_list);
     297     4922397 :         refcount_inc(&node->refs);
     298     4962240 : out:
     299     4962240 :         spin_unlock(&delayed_root->lock);
     300             : 
     301     4962218 :         return node;
     302             : }
     303             : 
     304             : static inline void btrfs_release_prepared_delayed_node(
     305             :                                         struct btrfs_delayed_node *node)
     306             : {
     307     4919866 :         __btrfs_release_delayed_node(node, 1);
     308             : }
     309             : 
     310     4759577 : static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u16 data_len,
     311             :                                            struct btrfs_delayed_node *node,
     312             :                                            enum btrfs_delayed_item_type type)
     313             : {
     314     4759577 :         struct btrfs_delayed_item *item;
     315             : 
     316     4759577 :         item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
     317     4759464 :         if (item) {
     318     4759464 :                 item->data_len = data_len;
     319     4759464 :                 item->type = type;
     320     4759464 :                 item->bytes_reserved = 0;
     321     4759464 :                 item->delayed_node = node;
     322     4759464 :                 RB_CLEAR_NODE(&item->rb_node);
     323     4759464 :                 INIT_LIST_HEAD(&item->log_list);
     324     4759464 :                 item->logged = false;
     325     4759464 :                 refcount_set(&item->refs, 1);
     326             :         }
     327     4759464 :         return item;
     328             : }
     329             : 
     330             : /*
     331             :  * __btrfs_lookup_delayed_item - look up the delayed item by key
     332             :  * @delayed_node: pointer to the delayed node
     333             :  * @index:        the dir index value to lookup (offset of a dir index key)
     334             :  *
     335             :  * Note: if we don't find the right item, we will return the prev item and
     336             :  * the next item.
     337             :  */
     338             : static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
     339             :                                 struct rb_root *root,
     340             :                                 u64 index)
     341             : {
     342     1885582 :         struct rb_node *node = root->rb_node;
     343     1885582 :         struct btrfs_delayed_item *delayed_item = NULL;
     344             : 
     345     2233246 :         while (node) {
     346      851714 :                 delayed_item = rb_entry(node, struct btrfs_delayed_item,
     347             :                                         rb_node);
     348      851714 :                 if (delayed_item->index < index)
     349      180768 :                         node = node->rb_right;
     350      670946 :                 else if (delayed_item->index > index)
     351      166896 :                         node = node->rb_left;
     352             :                 else
     353             :                         return delayed_item;
     354             :         }
     355             : 
     356             :         return NULL;
     357             : }
     358             : 
     359     4759736 : static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
     360             :                                     struct btrfs_delayed_item *ins)
     361             : {
     362     4759736 :         struct rb_node **p, *node;
     363     4759736 :         struct rb_node *parent_node = NULL;
     364     4759736 :         struct rb_root_cached *root;
     365     4759736 :         struct btrfs_delayed_item *item;
     366     4759736 :         bool leftmost = true;
     367             : 
     368     4759736 :         if (ins->type == BTRFS_DELAYED_INSERTION_ITEM)
     369     3378212 :                 root = &delayed_node->ins_root;
     370             :         else
     371     1381524 :                 root = &delayed_node->del_root;
     372             : 
     373     4759736 :         p = &root->rb_root.rb_node;
     374     4759736 :         node = &ins->rb_node;
     375             : 
     376    28638339 :         while (*p) {
     377    23878603 :                 parent_node = *p;
     378    23878603 :                 item = rb_entry(parent_node, struct btrfs_delayed_item,
     379             :                                  rb_node);
     380             : 
     381    23878603 :                 if (item->index < ins->index) {
     382    23834147 :                         p = &(*p)->rb_right;
     383    23834147 :                         leftmost = false;
     384       44456 :                 } else if (item->index > ins->index) {
     385       44456 :                         p = &(*p)->rb_left;
     386             :                 } else {
     387             :                         return -EEXIST;
     388             :                 }
     389             :         }
     390             : 
     391     4759736 :         rb_link_node(node, parent_node, p);
     392     4759736 :         rb_insert_color_cached(node, root, leftmost);
     393             : 
     394     4759505 :         if (ins->type == BTRFS_DELAYED_INSERTION_ITEM &&
     395     3378083 :             ins->index >= delayed_node->index_cnt)
     396     3378053 :                 delayed_node->index_cnt = ins->index + 1;
     397             : 
     398     4759505 :         delayed_node->count++;
     399     4759505 :         atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
     400     4759505 :         return 0;
     401             : }
     402             : 
     403    14686233 : static void finish_one_item(struct btrfs_delayed_root *delayed_root)
     404             : {
     405    14686233 :         int seq = atomic_inc_return(&delayed_root->items_seq);
     406             : 
     407             :         /* atomic_dec_return implies a barrier */
     408    14685644 :         if ((atomic_dec_return(&delayed_root->items) <
     409     3072260 :             BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0))
     410    11805917 :                 cond_wake_up_nomb(&delayed_root->wait);
     411    14686075 : }
     412             : 
     413     4759446 : static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
     414             : {
     415     4759446 :         struct rb_root_cached *root;
     416     4759446 :         struct btrfs_delayed_root *delayed_root;
     417             : 
     418             :         /* Not inserted, ignore it. */
     419     4759446 :         if (RB_EMPTY_NODE(&delayed_item->rb_node))
     420             :                 return;
     421             : 
     422     4759446 :         delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
     423             : 
     424     4759446 :         BUG_ON(!delayed_root);
     425             : 
     426     4759446 :         if (delayed_item->type == BTRFS_DELAYED_INSERTION_ITEM)
     427     3377967 :                 root = &delayed_item->delayed_node->ins_root;
     428             :         else
     429     1381479 :                 root = &delayed_item->delayed_node->del_root;
     430             : 
     431     4759446 :         rb_erase_cached(&delayed_item->rb_node, root);
     432     4759019 :         RB_CLEAR_NODE(&delayed_item->rb_node);
     433     4759019 :         delayed_item->delayed_node->count--;
     434             : 
     435     4759019 :         finish_one_item(delayed_root);
     436             : }
     437             : 
     438     4759316 : static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
     439             : {
     440     4759316 :         if (item) {
     441     4759316 :                 __btrfs_remove_delayed_item(item);
     442     4759480 :                 if (refcount_dec_and_test(&item->refs))
     443     4759363 :                         kfree(item);
     444             :         }
     445     4759613 : }
     446             : 
     447             : static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
     448             :                                         struct btrfs_delayed_node *delayed_node)
     449             : {
     450    10026802 :         struct rb_node *p;
     451    10026802 :         struct btrfs_delayed_item *item = NULL;
     452             : 
     453    10026802 :         p = rb_first_cached(&delayed_node->ins_root);
     454    10026802 :         if (p)
     455      198715 :                 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
     456             : 
     457     8166539 :         return item;
     458             : }
     459             : 
     460             : static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
     461             :                                         struct btrfs_delayed_node *delayed_node)
     462             : {
     463     9731659 :         struct rb_node *p;
     464     9731659 :         struct btrfs_delayed_item *item = NULL;
     465             : 
     466     9731659 :         p = rb_first_cached(&delayed_node->del_root);
     467     9731659 :         if (p)
     468       31398 :                 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
     469             : 
     470     7871403 :         return item;
     471             : }
     472             : 
     473             : static struct btrfs_delayed_item *__btrfs_next_delayed_item(
     474             :                                                 struct btrfs_delayed_item *item)
     475             : {
     476    21717320 :         struct rb_node *p;
     477    21717320 :         struct btrfs_delayed_item *next = NULL;
     478             : 
     479    21717320 :         p = rb_next(&item->rb_node);
     480    21717747 :         if (p)
     481      137236 :                 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
     482             : 
     483     3274737 :         return next;
     484             : }
     485             : 
     486     2369059 : static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
     487             :                                                struct btrfs_delayed_item *item)
     488             : {
     489     2369059 :         struct btrfs_block_rsv *src_rsv;
     490     2369059 :         struct btrfs_block_rsv *dst_rsv;
     491     2369059 :         struct btrfs_fs_info *fs_info = trans->fs_info;
     492     2369059 :         u64 num_bytes;
     493     2369059 :         int ret;
     494             : 
     495     2369059 :         if (!trans->bytes_reserved)
     496             :                 return 0;
     497             : 
     498     2368902 :         src_rsv = trans->block_rsv;
     499     2368902 :         dst_rsv = &fs_info->delayed_block_rsv;
     500             : 
     501     2368902 :         num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
     502             : 
     503             :         /*
     504             :          * Here we migrate space rsv from transaction rsv, since have already
     505             :          * reserved space when starting a transaction.  So no need to reserve
     506             :          * qgroup space here.
     507             :          */
     508     2368902 :         ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
     509     2368982 :         if (!ret) {
     510     2368984 :                 trace_btrfs_space_reservation(fs_info, "delayed_item",
     511     2368984 :                                               item->delayed_node->inode_id,
     512             :                                               num_bytes, 1);
     513             :                 /*
     514             :                  * For insertions we track reserved metadata space by accounting
     515             :                  * for the number of leaves that will be used, based on the delayed
     516             :                  * node's index_items_size field.
     517             :                  */
     518     2368968 :                 if (item->type == BTRFS_DELAYED_DELETION_ITEM)
     519     1381503 :                         item->bytes_reserved = num_bytes;
     520             :         }
     521             : 
     522             :         return ret;
     523             : }
     524             : 
     525      156282 : static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
     526             :                                                 struct btrfs_delayed_item *item)
     527             : {
     528      156282 :         struct btrfs_block_rsv *rsv;
     529      156282 :         struct btrfs_fs_info *fs_info = root->fs_info;
     530             : 
     531      156282 :         if (!item->bytes_reserved)
     532             :                 return;
     533             : 
     534      156282 :         rsv = &fs_info->delayed_block_rsv;
     535             :         /*
     536             :          * Check btrfs_delayed_item_reserve_metadata() to see why we don't need
     537             :          * to release/reserve qgroup space.
     538             :          */
     539      156282 :         trace_btrfs_space_reservation(fs_info, "delayed_item",
     540      156282 :                                       item->delayed_node->inode_id,
     541             :                                       item->bytes_reserved, 0);
     542      156282 :         btrfs_block_rsv_release(fs_info, rsv, item->bytes_reserved, NULL);
     543             : }
     544             : 
     545      987608 : static void btrfs_delayed_item_release_leaves(struct btrfs_delayed_node *node,
     546             :                                               unsigned int num_leaves)
     547             : {
     548      987608 :         struct btrfs_fs_info *fs_info = node->root->fs_info;
     549      987608 :         const u64 bytes = btrfs_calc_insert_metadata_size(fs_info, num_leaves);
     550             : 
     551             :         /* There are no space reservations during log replay, bail out. */
     552     1975216 :         if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
     553             :                 return;
     554             : 
     555      987478 :         trace_btrfs_space_reservation(fs_info, "delayed_item", node->inode_id,
     556             :                                       bytes, 0);
     557      987477 :         btrfs_block_rsv_release(fs_info, &fs_info->delayed_block_rsv, bytes, NULL);
     558             : }
     559             : 
     560     8678653 : static int btrfs_delayed_inode_reserve_metadata(
     561             :                                         struct btrfs_trans_handle *trans,
     562             :                                         struct btrfs_root *root,
     563             :                                         struct btrfs_delayed_node *node)
     564             : {
     565     8678653 :         struct btrfs_fs_info *fs_info = root->fs_info;
     566     8678653 :         struct btrfs_block_rsv *src_rsv;
     567     8678653 :         struct btrfs_block_rsv *dst_rsv;
     568     8678653 :         u64 num_bytes;
     569     8678653 :         int ret;
     570             : 
     571     8678653 :         src_rsv = trans->block_rsv;
     572     8678653 :         dst_rsv = &fs_info->delayed_block_rsv;
     573             : 
     574     8678653 :         num_bytes = btrfs_calc_metadata_size(fs_info, 1);
     575             : 
     576             :         /*
     577             :          * btrfs_dirty_inode will update the inode under btrfs_join_transaction
     578             :          * which doesn't reserve space for speed.  This is a problem since we
     579             :          * still need to reserve space for this update, so try to reserve the
     580             :          * space.
     581             :          *
     582             :          * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
     583             :          * we always reserve enough to update the inode item.
     584             :          */
     585     8678653 :         if (!src_rsv || (!trans->bytes_reserved &&
     586     1792248 :                          src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
     587     2291299 :                 ret = btrfs_qgroup_reserve_meta(root, num_bytes,
     588             :                                           BTRFS_QGROUP_RSV_META_PREALLOC, true);
     589     2290303 :                 if (ret < 0)
     590             :                         return ret;
     591     2287283 :                 ret = btrfs_block_rsv_add(fs_info, dst_rsv, num_bytes,
     592             :                                           BTRFS_RESERVE_NO_FLUSH);
     593             :                 /* NO_FLUSH could only fail with -ENOSPC */
     594     2289073 :                 ASSERT(ret == 0 || ret == -ENOSPC);
     595     2289073 :                 if (ret)
     596       86113 :                         btrfs_qgroup_free_meta_prealloc(root, num_bytes);
     597             :         } else {
     598     6387354 :                 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
     599             :         }
     600             : 
     601     8676655 :         if (!ret) {
     602     8590160 :                 trace_btrfs_space_reservation(fs_info, "delayed_inode",
     603             :                                               node->inode_id, num_bytes, 1);
     604     8589919 :                 node->bytes_reserved = num_bytes;
     605             :         }
     606             : 
     607             :         return ret;
     608             : }
     609             : 
     610     8589875 : static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
     611             :                                                 struct btrfs_delayed_node *node,
     612             :                                                 bool qgroup_free)
     613             : {
     614     8589875 :         struct btrfs_block_rsv *rsv;
     615             : 
     616     8589875 :         if (!node->bytes_reserved)
     617             :                 return;
     618             : 
     619     8589859 :         rsv = &fs_info->delayed_block_rsv;
     620     8589859 :         trace_btrfs_space_reservation(fs_info, "delayed_inode",
     621             :                                       node->inode_id, node->bytes_reserved, 0);
     622     8588111 :         btrfs_block_rsv_release(fs_info, rsv, node->bytes_reserved, NULL);
     623     8590712 :         if (qgroup_free)
     624           6 :                 btrfs_qgroup_free_meta_prealloc(node->root,
     625           6 :                                 node->bytes_reserved);
     626             :         else
     627     8590706 :                 btrfs_qgroup_convert_reserved_meta(node->root,
     628     8590706 :                                 node->bytes_reserved);
     629     8590446 :         node->bytes_reserved = 0;
     630             : }
     631             : 
     632             : /*
     633             :  * Insert a single delayed item or a batch of delayed items, as many as possible
     634             :  * that fit in a leaf. The delayed items (dir index keys) are sorted by their key
     635             :  * in the rbtree, and if there's a gap between two consecutive dir index items,
     636             :  * then it means at some point we had delayed dir indexes to add but they got
     637             :  * removed (by btrfs_delete_delayed_dir_index()) before we attempted to flush them
     638             :  * into the subvolume tree. Dir index keys also have their offsets coming from a
     639             :  * monotonically increasing counter, so we can't get new keys with an offset that
     640             :  * fits within a gap between delayed dir index items.
     641             :  */
     642      653326 : static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
     643             :                                      struct btrfs_root *root,
     644             :                                      struct btrfs_path *path,
     645             :                                      struct btrfs_delayed_item *first_item)
     646             : {
     647      653326 :         struct btrfs_fs_info *fs_info = root->fs_info;
     648      653326 :         struct btrfs_delayed_node *node = first_item->delayed_node;
     649      653326 :         LIST_HEAD(item_list);
     650      653326 :         struct btrfs_delayed_item *curr;
     651      653326 :         struct btrfs_delayed_item *next;
     652      653326 :         const int max_size = BTRFS_LEAF_DATA_SIZE(fs_info);
     653      653326 :         struct btrfs_item_batch batch;
     654      653326 :         struct btrfs_key first_key;
     655      653326 :         const u32 first_data_size = first_item->data_len;
     656      653326 :         int total_size;
     657      653326 :         char *ins_data = NULL;
     658      653326 :         int ret;
     659      653326 :         bool continuous_keys_only = false;
     660             : 
     661      653326 :         lockdep_assert_held(&node->mutex);
     662             : 
     663             :         /*
     664             :          * During normal operation the delayed index offset is continuously
     665             :          * increasing, so we can batch insert all items as there will not be any
     666             :          * overlapping keys in the tree.
     667             :          *
     668             :          * The exception to this is log replay, where we may have interleaved
     669             :          * offsets in the tree, so our batch needs to be continuous keys only in
     670             :          * order to ensure we do not end up with out of order items in our leaf.
     671             :          */
     672     1306652 :         if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
     673         138 :                 continuous_keys_only = true;
     674             : 
     675             :         /*
     676             :          * For delayed items to insert, we track reserved metadata bytes based
     677             :          * on the number of leaves that we will use.
     678             :          * See btrfs_insert_delayed_dir_index() and
     679             :          * btrfs_delayed_item_reserve_metadata()).
     680             :          */
     681      653326 :         ASSERT(first_item->bytes_reserved == 0);
     682             : 
     683      653326 :         list_add_tail(&first_item->tree_list, &item_list);
     684      653290 :         batch.total_data_size = first_data_size;
     685      653290 :         batch.nr = 1;
     686      653290 :         total_size = first_data_size + sizeof(struct btrfs_item);
     687      653290 :         curr = first_item;
     688             : 
     689     5091720 :         while (true) {
     690     2872505 :                 int next_size;
     691             : 
     692     2872505 :                 next = __btrfs_next_delayed_item(curr);
     693     2219904 :                 if (!next)
     694             :                         break;
     695             : 
     696             :                 /*
     697             :                  * We cannot allow gaps in the key space if we're doing log
     698             :                  * replay.
     699             :                  */
     700     2219904 :                 if (continuous_keys_only && (next->index != curr->index + 1))
     701             :                         break;
     702             : 
     703     2219897 :                 ASSERT(next->bytes_reserved == 0);
     704             : 
     705     2219897 :                 next_size = next->data_len + sizeof(struct btrfs_item);
     706     2219897 :                 if (total_size + next_size > max_size)
     707             :                         break;
     708             : 
     709     2219611 :                 list_add_tail(&next->tree_list, &item_list);
     710     2219215 :                 batch.nr++;
     711     2219215 :                 total_size += next_size;
     712     2219215 :                 batch.total_data_size += next->data_len;
     713     2219215 :                 curr = next;
     714             :         }
     715             : 
     716      653313 :         if (batch.nr == 1) {
     717      552594 :                 first_key.objectid = node->inode_id;
     718      552594 :                 first_key.type = BTRFS_DIR_INDEX_KEY;
     719      552594 :                 first_key.offset = first_item->index;
     720      552594 :                 batch.keys = &first_key;
     721      552594 :                 batch.data_sizes = &first_data_size;
     722             :         } else {
     723      100719 :                 struct btrfs_key *ins_keys;
     724      100719 :                 u32 *ins_sizes;
     725      100719 :                 int i = 0;
     726             : 
     727      100719 :                 ins_data = kmalloc(batch.nr * sizeof(u32) +
     728             :                                    batch.nr * sizeof(struct btrfs_key), GFP_NOFS);
     729      100716 :                 if (!ins_data) {
     730           0 :                         ret = -ENOMEM;
     731           0 :                         goto out;
     732             :                 }
     733      100716 :                 ins_sizes = (u32 *)ins_data;
     734      100716 :                 ins_keys = (struct btrfs_key *)(ins_data + batch.nr * sizeof(u32));
     735      100716 :                 batch.keys = ins_keys;
     736      100716 :                 batch.data_sizes = ins_sizes;
     737     2421035 :                 list_for_each_entry(curr, &item_list, tree_list) {
     738     2320319 :                         ins_keys[i].objectid = node->inode_id;
     739     2320319 :                         ins_keys[i].type = BTRFS_DIR_INDEX_KEY;
     740     2320319 :                         ins_keys[i].offset = curr->index;
     741     2320319 :                         ins_sizes[i] = curr->data_len;
     742     2320319 :                         i++;
     743             :                 }
     744             :         }
     745             : 
     746      653310 :         ret = btrfs_insert_empty_items(trans, root, path, &batch);
     747      653321 :         if (ret)
     748           5 :                 goto out;
     749             : 
     750     3527197 :         list_for_each_entry(curr, &item_list, tree_list) {
     751     2873874 :                 char *data_ptr;
     752             : 
     753     2873874 :                 data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
     754     2873875 :                 write_extent_buffer(path->nodes[0], &curr->data,
     755     2873875 :                                     (unsigned long)data_ptr, curr->data_len);
     756     2873881 :                 path->slots[0]++;
     757             :         }
     758             : 
     759             :         /*
     760             :          * Now release our path before releasing the delayed items and their
     761             :          * metadata reservations, so that we don't block other tasks for more
     762             :          * time than needed.
     763             :          */
     764      653323 :         btrfs_release_path(path);
     765             : 
     766      653326 :         ASSERT(node->index_item_leaves > 0);
     767             : 
     768             :         /*
     769             :          * For normal operations we will batch an entire leaf's worth of delayed
     770             :          * items, so if there are more items to process we can decrement
     771             :          * index_item_leaves by 1 as we inserted 1 leaf's worth of items.
     772             :          *
     773             :          * However for log replay we may not have inserted an entire leaf's
     774             :          * worth of items, we may have not had continuous items, so decrementing
     775             :          * here would mess up the index_item_leaves accounting.  For this case
     776             :          * only clean up the accounting when there are no items left.
     777             :          */
     778      653326 :         if (next && !continuous_keys_only) {
     779             :                 /*
     780             :                  * We inserted one batch of items into a leaf a there are more
     781             :                  * items to flush in a future batch, now release one unit of
     782             :                  * metadata space from the delayed block reserve, corresponding
     783             :                  * the leaf we just flushed to.
     784             :                  */
     785         286 :                 btrfs_delayed_item_release_leaves(node, 1);
     786         286 :                 node->index_item_leaves--;
     787      653040 :         } else if (!next) {
     788             :                 /*
     789             :                  * There are no more items to insert. We can have a number of
     790             :                  * reserved leaves > 1 here - this happens when many dir index
     791             :                  * items are added and then removed before they are flushed (file
     792             :                  * names with a very short life, never span a transaction). So
     793             :                  * release all remaining leaves.
     794             :                  */
     795      653035 :                 btrfs_delayed_item_release_leaves(node, node->index_item_leaves);
     796      653046 :                 node->index_item_leaves = 0;
     797             :         }
     798             : 
     799     3527341 :         list_for_each_entry_safe(curr, next, &item_list, tree_list) {
     800     2874025 :                 list_del(&curr->tree_list);
     801     2873928 :                 btrfs_release_delayed_item(curr);
     802             :         }
     803      653316 : out:
     804      653321 :         kfree(ins_data);
     805      653314 :         return ret;
     806             : }
     807             : 
     808     7512589 : static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
     809             :                                       struct btrfs_path *path,
     810             :                                       struct btrfs_root *root,
     811             :                                       struct btrfs_delayed_node *node)
     812             : {
     813     7512589 :         int ret = 0;
     814             : 
     815     8165775 :         while (ret == 0) {
     816     8165770 :                 struct btrfs_delayed_item *curr;
     817             : 
     818     8165770 :                 mutex_lock(&node->mutex);
     819     8166539 :                 curr = __btrfs_first_delayed_insertion_item(node);
     820     8166539 :                 if (!curr) {
     821     7513202 :                         mutex_unlock(&node->mutex);
     822     7513202 :                         break;
     823             :                 }
     824      653337 :                 ret = btrfs_insert_delayed_item(trans, root, path, curr);
     825      653303 :                 mutex_unlock(&node->mutex);
     826             :         }
     827             : 
     828     7512195 :         return ret;
     829             : }
     830             : 
     831      360309 : static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
     832             :                                     struct btrfs_root *root,
     833             :                                     struct btrfs_path *path,
     834             :                                     struct btrfs_delayed_item *item)
     835             : {
     836      360309 :         const u64 ino = item->delayed_node->inode_id;
     837      360309 :         struct btrfs_fs_info *fs_info = root->fs_info;
     838      360309 :         struct btrfs_delayed_item *curr, *next;
     839      360309 :         struct extent_buffer *leaf = path->nodes[0];
     840      360309 :         LIST_HEAD(batch_list);
     841      360309 :         int nitems, slot, last_slot;
     842      360309 :         int ret;
     843      360309 :         u64 total_reserved_size = item->bytes_reserved;
     844             : 
     845      360309 :         ASSERT(leaf != NULL);
     846             : 
     847      360309 :         slot = path->slots[0];
     848      360309 :         last_slot = btrfs_header_nritems(leaf) - 1;
     849             :         /*
     850             :          * Our caller always gives us a path pointing to an existing item, so
     851             :          * this can not happen.
     852             :          */
     853      360309 :         ASSERT(slot <= last_slot);
     854      360309 :         if (WARN_ON(slot > last_slot))
     855             :                 return -ENOENT;
     856             : 
     857      360309 :         nitems = 1;
     858      360309 :         curr = item;
     859      360309 :         list_add_tail(&curr->tree_list, &batch_list);
     860             : 
     861             :         /*
     862             :          * Keep checking if the next delayed item matches the next item in the
     863             :          * leaf - if so, we can add it to the batch of items to delete from the
     864             :          * leaf.
     865             :          */
     866     1225233 :         while (slot < last_slot) {
     867     1218464 :                 struct btrfs_key key;
     868             : 
     869     1218464 :                 next = __btrfs_next_delayed_item(curr);
     870      898546 :                 if (!next)
     871             :                         break;
     872             : 
     873      898546 :                 slot++;
     874      898546 :                 btrfs_item_key_to_cpu(leaf, &key, slot);
     875      898555 :                 if (key.objectid != ino ||
     876      898555 :                     key.type != BTRFS_DIR_INDEX_KEY ||
     877      898555 :                     key.offset != next->index)
     878             :                         break;
     879      864939 :                 nitems++;
     880      864939 :                 curr = next;
     881      864939 :                 list_add_tail(&curr->tree_list, &batch_list);
     882      864924 :                 total_reserved_size += curr->bytes_reserved;
     883             :         }
     884             : 
     885      360309 :         ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
     886      360307 :         if (ret)
     887             :                 return ret;
     888             : 
     889             :         /* In case of BTRFS_FS_LOG_RECOVERING items won't have reserved space */
     890      360307 :         if (total_reserved_size > 0) {
     891             :                 /*
     892             :                  * Check btrfs_delayed_item_reserve_metadata() to see why we
     893             :                  * don't need to release/reserve qgroup space.
     894             :                  */
     895      360287 :                 trace_btrfs_space_reservation(fs_info, "delayed_item", ino,
     896             :                                               total_reserved_size, 0);
     897      360285 :                 btrfs_block_rsv_release(fs_info, &fs_info->delayed_block_rsv,
     898             :                                         total_reserved_size, NULL);
     899             :         }
     900             : 
     901     1585541 :         list_for_each_entry_safe(curr, next, &batch_list, tree_list) {
     902     1225234 :                 list_del(&curr->tree_list);
     903     1225207 :                 btrfs_release_delayed_item(curr);
     904             :         }
     905             : 
     906             :         return 0;
     907             : }
     908             : 
     909     7511708 : static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
     910             :                                       struct btrfs_path *path,
     911             :                                       struct btrfs_root *root,
     912             :                                       struct btrfs_delayed_node *node)
     913             : {
     914     7511708 :         struct btrfs_key key;
     915     7511708 :         int ret = 0;
     916             : 
     917     7511708 :         key.objectid = node->inode_id;
     918     7511708 :         key.type = BTRFS_DIR_INDEX_KEY;
     919             : 
     920     7871809 :         while (ret == 0) {
     921     7871809 :                 struct btrfs_delayed_item *item;
     922             : 
     923     7871809 :                 mutex_lock(&node->mutex);
     924     7871403 :                 item = __btrfs_first_delayed_deletion_item(node);
     925     7871403 :                 if (!item) {
     926     7511095 :                         mutex_unlock(&node->mutex);
     927     7511095 :                         break;
     928             :                 }
     929             : 
     930      360308 :                 key.offset = item->index;
     931      360308 :                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
     932      360309 :                 if (ret > 0) {
     933             :                         /*
     934             :                          * There's no matching item in the leaf. This means we
     935             :                          * have already deleted this item in a past run of the
     936             :                          * delayed items. We ignore errors when running delayed
     937             :                          * items from an async context, through a work queue job
     938             :                          * running btrfs_async_run_delayed_root(), and don't
     939             :                          * release delayed items that failed to complete. This
     940             :                          * is because we will retry later, and at transaction
     941             :                          * commit time we always run delayed items and will
     942             :                          * then deal with errors if they fail to run again.
     943             :                          *
     944             :                          * So just release delayed items for which we can't find
     945             :                          * an item in the tree, and move to the next item.
     946             :                          */
     947           0 :                         btrfs_release_path(path);
     948           0 :                         btrfs_release_delayed_item(item);
     949           0 :                         ret = 0;
     950      360309 :                 } else if (ret == 0) {
     951      360309 :                         ret = btrfs_batch_delete_items(trans, root, path, item);
     952      360307 :                         btrfs_release_path(path);
     953             :                 }
     954             : 
     955             :                 /*
     956             :                  * We unlock and relock on each iteration, this is to prevent
     957             :                  * blocking other tasks for too long while we are being run from
     958             :                  * the async context (work queue job). Those tasks are typically
     959             :                  * running system calls like creat/mkdir/rename/unlink/etc which
     960             :                  * need to add delayed items to this delayed node.
     961             :                  */
     962      360308 :                 mutex_unlock(&node->mutex);
     963             :         }
     964             : 
     965     7510662 :         return ret;
     966             : }
     967             : 
     968     8590176 : static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
     969             : {
     970     8590176 :         struct btrfs_delayed_root *delayed_root;
     971             : 
     972    17180352 :         if (delayed_node &&
     973     8590176 :             test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
     974     8590140 :                 BUG_ON(!delayed_node->root);
     975     8590140 :                 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
     976     8590604 :                 delayed_node->count--;
     977             : 
     978     8590604 :                 delayed_root = delayed_node->root->fs_info->delayed_root;
     979     8590604 :                 finish_one_item(delayed_root);
     980             :         }
     981     8589664 : }
     982             : 
     983     9960153 : static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
     984             : {
     985             : 
     986     9960153 :         if (test_and_clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) {
     987     1337224 :                 struct btrfs_delayed_root *delayed_root;
     988             : 
     989     1337224 :                 ASSERT(delayed_node->root);
     990     1337224 :                 delayed_node->count--;
     991             : 
     992     1337224 :                 delayed_root = delayed_node->root->fs_info->delayed_root;
     993     1337224 :                 finish_one_item(delayed_root);
     994             :         }
     995     9961350 : }
     996             : 
     997     8588090 : static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
     998             :                                         struct btrfs_root *root,
     999             :                                         struct btrfs_path *path,
    1000             :                                         struct btrfs_delayed_node *node)
    1001             : {
    1002     8588090 :         struct btrfs_fs_info *fs_info = root->fs_info;
    1003     8588090 :         struct btrfs_key key;
    1004     8588090 :         struct btrfs_inode_item *inode_item;
    1005     8588090 :         struct extent_buffer *leaf;
    1006     8588090 :         int mod;
    1007     8588090 :         int ret;
    1008             : 
    1009     8588090 :         key.objectid = node->inode_id;
    1010     8588090 :         key.type = BTRFS_INODE_ITEM_KEY;
    1011     8588090 :         key.offset = 0;
    1012             : 
    1013    17176180 :         if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
    1014             :                 mod = -1;
    1015             :         else
    1016     7249655 :                 mod = 1;
    1017             : 
    1018     8588090 :         ret = btrfs_lookup_inode(trans, root, path, &key, mod);
    1019     8587415 :         if (ret > 0)
    1020             :                 ret = -ENOENT;
    1021     8587530 :         if (ret < 0)
    1022           6 :                 goto out;
    1023             : 
    1024     8587409 :         leaf = path->nodes[0];
    1025     8587409 :         inode_item = btrfs_item_ptr(leaf, path->slots[0],
    1026             :                                     struct btrfs_inode_item);
    1027     8586085 :         write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
    1028             :                             sizeof(struct btrfs_inode_item));
    1029     8585851 :         btrfs_mark_buffer_dirty(leaf);
    1030             : 
    1031     8588999 :         if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
    1032     7251773 :                 goto out;
    1033             : 
    1034     1337226 :         path->slots[0]++;
    1035     1337226 :         if (path->slots[0] >= btrfs_header_nritems(leaf))
    1036       14197 :                 goto search;
    1037     1323029 : again:
    1038     1337226 :         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
    1039     1337227 :         if (key.objectid != node->inode_id)
    1040           0 :                 goto out;
    1041             : 
    1042     1337227 :         if (key.type != BTRFS_INODE_REF_KEY &&
    1043             :             key.type != BTRFS_INODE_EXTREF_KEY)
    1044           0 :                 goto out;
    1045             : 
    1046             :         /*
    1047             :          * Delayed iref deletion is for the inode who has only one link,
    1048             :          * so there is only one iref. The case that several irefs are
    1049             :          * in the same item doesn't exist.
    1050             :          */
    1051     1337227 :         ret = btrfs_del_item(trans, root, path);
    1052     8588997 : out:
    1053     8588997 :         btrfs_release_delayed_iref(node);
    1054     8589447 :         btrfs_release_path(path);
    1055     8589972 : err_out:
    1056     8589972 :         btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
    1057     8590301 :         btrfs_release_delayed_inode(node);
    1058             : 
    1059             :         /*
    1060             :          * If we fail to update the delayed inode we need to abort the
    1061             :          * transaction, because we could leave the inode with the improper
    1062             :          * counts behind.
    1063             :          */
    1064     8589634 :         if (ret && ret != -ENOENT)
    1065           6 :                 btrfs_abort_transaction(trans, ret);
    1066             : 
    1067     8589634 :         return ret;
    1068             : 
    1069             : search:
    1070       14197 :         btrfs_release_path(path);
    1071             : 
    1072       14197 :         key.type = BTRFS_INODE_EXTREF_KEY;
    1073       14197 :         key.offset = -1;
    1074             : 
    1075       14197 :         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
    1076       14197 :         if (ret < 0)
    1077           0 :                 goto err_out;
    1078       14197 :         ASSERT(ret);
    1079             : 
    1080       14197 :         ret = 0;
    1081       14197 :         leaf = path->nodes[0];
    1082       14197 :         path->slots[0]--;
    1083       14197 :         goto again;
    1084             : }
    1085             : 
    1086     7507225 : static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
    1087             :                                              struct btrfs_root *root,
    1088             :                                              struct btrfs_path *path,
    1089             :                                              struct btrfs_delayed_node *node)
    1090             : {
    1091     7507225 :         int ret;
    1092             : 
    1093     7507225 :         mutex_lock(&node->mutex);
    1094     7512714 :         if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
    1095      283681 :                 mutex_unlock(&node->mutex);
    1096      283681 :                 return 0;
    1097             :         }
    1098             : 
    1099     7229033 :         ret = __btrfs_update_delayed_inode(trans, root, path, node);
    1100     7228949 :         mutex_unlock(&node->mutex);
    1101     7228949 :         return ret;
    1102             : }
    1103             : 
    1104             : static inline int
    1105     7512748 : __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
    1106             :                                    struct btrfs_path *path,
    1107             :                                    struct btrfs_delayed_node *node)
    1108             : {
    1109     7512748 :         int ret;
    1110             : 
    1111     7512748 :         ret = btrfs_insert_delayed_items(trans, path, node->root, node);
    1112     7512085 :         if (ret)
    1113             :                 return ret;
    1114             : 
    1115     7512141 :         ret = btrfs_delete_delayed_items(trans, path, node->root, node);
    1116     7510359 :         if (ret)
    1117             :                 return ret;
    1118             : 
    1119     7508663 :         ret = btrfs_update_delayed_inode(trans, node->root, path, node);
    1120     7508663 :         return ret;
    1121             : }
    1122             : 
    1123             : /*
    1124             :  * Called when committing the transaction.
    1125             :  * Returns 0 on success.
    1126             :  * Returns < 0 on error and returns with an aborted transaction with any
    1127             :  * outstanding delayed items cleaned up.
    1128             :  */
    1129     2496640 : static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
    1130             : {
    1131     2496640 :         struct btrfs_fs_info *fs_info = trans->fs_info;
    1132     2496640 :         struct btrfs_delayed_root *delayed_root;
    1133     2496640 :         struct btrfs_delayed_node *curr_node, *prev_node;
    1134     2496640 :         struct btrfs_path *path;
    1135     2496640 :         struct btrfs_block_rsv *block_rsv;
    1136     2496640 :         int ret = 0;
    1137     2496640 :         bool count = (nr > 0);
    1138             : 
    1139     2496640 :         if (TRANS_ABORTED(trans))
    1140             :                 return -EIO;
    1141             : 
    1142     2496640 :         path = btrfs_alloc_path();
    1143     2496637 :         if (!path)
    1144             :                 return -ENOMEM;
    1145             : 
    1146     2496637 :         block_rsv = trans->block_rsv;
    1147     2496637 :         trans->block_rsv = &fs_info->delayed_block_rsv;
    1148             : 
    1149     2496637 :         delayed_root = fs_info->delayed_root;
    1150             : 
    1151     2496637 :         curr_node = btrfs_first_delayed_node(delayed_root);
    1152     5088071 :         while (curr_node && (!count || nr--)) {
    1153     2591446 :                 ret = __btrfs_commit_inode_delayed_items(trans, path,
    1154             :                                                          curr_node);
    1155     2591438 :                 if (ret) {
    1156          11 :                         btrfs_release_delayed_node(curr_node);
    1157          11 :                         curr_node = NULL;
    1158          11 :                         btrfs_abort_transaction(trans, ret);
    1159          11 :                         break;
    1160             :                 }
    1161             : 
    1162     2591427 :                 prev_node = curr_node;
    1163     2591427 :                 curr_node = btrfs_next_delayed_node(curr_node);
    1164     2591426 :                 btrfs_release_delayed_node(prev_node);
    1165             :         }
    1166             : 
    1167     2496639 :         if (curr_node)
    1168       18274 :                 btrfs_release_delayed_node(curr_node);
    1169     2496639 :         btrfs_free_path(path);
    1170     2496630 :         trans->block_rsv = block_rsv;
    1171             : 
    1172     2496630 :         return ret;
    1173             : }
    1174             : 
    1175      611753 : int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
    1176             : {
    1177      611753 :         return __btrfs_run_delayed_items(trans, -1);
    1178             : }
    1179             : 
    1180     1884887 : int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
    1181             : {
    1182     1884887 :         return __btrfs_run_delayed_items(trans, nr);
    1183             : }
    1184             : 
    1185         104 : int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
    1186             :                                      struct btrfs_inode *inode)
    1187             : {
    1188         104 :         struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
    1189         104 :         struct btrfs_path *path;
    1190         104 :         struct btrfs_block_rsv *block_rsv;
    1191         104 :         int ret;
    1192             : 
    1193         104 :         if (!delayed_node)
    1194             :                 return 0;
    1195             : 
    1196          58 :         mutex_lock(&delayed_node->mutex);
    1197          58 :         if (!delayed_node->count) {
    1198           0 :                 mutex_unlock(&delayed_node->mutex);
    1199           0 :                 btrfs_release_delayed_node(delayed_node);
    1200           0 :                 return 0;
    1201             :         }
    1202          58 :         mutex_unlock(&delayed_node->mutex);
    1203             : 
    1204          58 :         path = btrfs_alloc_path();
    1205          58 :         if (!path) {
    1206           0 :                 btrfs_release_delayed_node(delayed_node);
    1207           0 :                 return -ENOMEM;
    1208             :         }
    1209             : 
    1210          58 :         block_rsv = trans->block_rsv;
    1211          58 :         trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
    1212             : 
    1213          58 :         ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
    1214             : 
    1215          58 :         btrfs_release_delayed_node(delayed_node);
    1216          58 :         btrfs_free_path(path);
    1217          58 :         trans->block_rsv = block_rsv;
    1218             : 
    1219          58 :         return ret;
    1220             : }
    1221             : 
    1222     1773671 : int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
    1223             : {
    1224     1773671 :         struct btrfs_fs_info *fs_info = inode->root->fs_info;
    1225     1773671 :         struct btrfs_trans_handle *trans;
    1226     1773671 :         struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
    1227     1774168 :         struct btrfs_path *path;
    1228     1774168 :         struct btrfs_block_rsv *block_rsv;
    1229     1774168 :         int ret;
    1230             : 
    1231     1774168 :         if (!delayed_node)
    1232             :                 return 0;
    1233             : 
    1234     1371435 :         mutex_lock(&delayed_node->mutex);
    1235     1371595 :         if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
    1236       11163 :                 mutex_unlock(&delayed_node->mutex);
    1237       11163 :                 btrfs_release_delayed_node(delayed_node);
    1238       11163 :                 return 0;
    1239             :         }
    1240     1360432 :         mutex_unlock(&delayed_node->mutex);
    1241             : 
    1242     1360369 :         trans = btrfs_join_transaction(delayed_node->root);
    1243     1360594 :         if (IS_ERR(trans)) {
    1244           0 :                 ret = PTR_ERR(trans);
    1245           0 :                 goto out;
    1246             :         }
    1247             : 
    1248     1360594 :         path = btrfs_alloc_path();
    1249     1360517 :         if (!path) {
    1250           0 :                 ret = -ENOMEM;
    1251           0 :                 goto trans_out;
    1252             :         }
    1253             : 
    1254     1360517 :         block_rsv = trans->block_rsv;
    1255     1360517 :         trans->block_rsv = &fs_info->delayed_block_rsv;
    1256             : 
    1257     1360517 :         mutex_lock(&delayed_node->mutex);
    1258     2720856 :         if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
    1259     1360362 :                 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
    1260             :                                                    path, delayed_node);
    1261             :         else
    1262             :                 ret = 0;
    1263     1360646 :         mutex_unlock(&delayed_node->mutex);
    1264             : 
    1265     1360602 :         btrfs_free_path(path);
    1266     1360589 :         trans->block_rsv = block_rsv;
    1267     1360589 : trans_out:
    1268     1360589 :         btrfs_end_transaction(trans);
    1269     1360593 :         btrfs_btree_balance_dirty(fs_info);
    1270     1360577 : out:
    1271     1360577 :         btrfs_release_delayed_node(delayed_node);
    1272             : 
    1273     1360577 :         return ret;
    1274             : }
    1275             : 
    1276     3865380 : void btrfs_remove_delayed_node(struct btrfs_inode *inode)
    1277             : {
    1278     3865380 :         struct btrfs_delayed_node *delayed_node;
    1279             : 
    1280     3865380 :         delayed_node = READ_ONCE(inode->delayed_node);
    1281     3865380 :         if (!delayed_node)
    1282             :                 return;
    1283             : 
    1284     1935979 :         inode->delayed_node = NULL;
    1285     1935979 :         btrfs_release_delayed_node(delayed_node);
    1286             : }
    1287             : 
    1288             : struct btrfs_async_delayed_work {
    1289             :         struct btrfs_delayed_root *delayed_root;
    1290             :         int nr;
    1291             :         struct btrfs_work work;
    1292             : };
    1293             : 
    1294      747538 : static void btrfs_async_run_delayed_root(struct btrfs_work *work)
    1295             : {
    1296      747538 :         struct btrfs_async_delayed_work *async_work;
    1297      747538 :         struct btrfs_delayed_root *delayed_root;
    1298      747538 :         struct btrfs_trans_handle *trans;
    1299      747538 :         struct btrfs_path *path;
    1300      747538 :         struct btrfs_delayed_node *delayed_node = NULL;
    1301      747538 :         struct btrfs_root *root;
    1302      747538 :         struct btrfs_block_rsv *block_rsv;
    1303      747538 :         int total_done = 0;
    1304             : 
    1305      747538 :         async_work = container_of(work, struct btrfs_async_delayed_work, work);
    1306      747538 :         delayed_root = async_work->delayed_root;
    1307             : 
    1308      747538 :         path = btrfs_alloc_path();
    1309      747530 :         if (!path)
    1310           0 :                 goto out;
    1311             : 
    1312     5402012 :         do {
    1313     5402012 :                 if (atomic_read(&delayed_root->items) <
    1314             :                     BTRFS_DELAYED_BACKGROUND / 2)
    1315             :                         break;
    1316             : 
    1317     4962197 :                 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
    1318     4962193 :                 if (!delayed_node)
    1319             :                         break;
    1320             : 
    1321     4922356 :                 root = delayed_node->root;
    1322             : 
    1323     4922356 :                 trans = btrfs_join_transaction(root);
    1324     4922302 :                 if (IS_ERR(trans)) {
    1325           0 :                         btrfs_release_path(path);
    1326           0 :                         btrfs_release_prepared_delayed_node(delayed_node);
    1327           0 :                         total_done++;
    1328           0 :                         continue;
    1329             :                 }
    1330             : 
    1331     4922302 :                 block_rsv = trans->block_rsv;
    1332     4922302 :                 trans->block_rsv = &root->fs_info->delayed_block_rsv;
    1333             : 
    1334     4922302 :                 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
    1335             : 
    1336     4921122 :                 trans->block_rsv = block_rsv;
    1337     4921122 :                 btrfs_end_transaction(trans);
    1338     4920251 :                 btrfs_btree_balance_dirty_nodelay(root->fs_info);
    1339             : 
    1340     4916735 :                 btrfs_release_path(path);
    1341     4919866 :                 btrfs_release_prepared_delayed_node(delayed_node);
    1342     4922242 :                 total_done++;
    1343             : 
    1344      285246 :         } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
    1345     4922630 :                  || total_done < async_work->nr);
    1346             : 
    1347      747412 :         btrfs_free_path(path);
    1348      747307 : out:
    1349      747307 :         wake_up(&delayed_root->wait);
    1350      747609 :         kfree(async_work);
    1351      747376 : }
    1352             : 
    1353             : 
    1354      746447 : static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
    1355             :                                      struct btrfs_fs_info *fs_info, int nr)
    1356             : {
    1357      746447 :         struct btrfs_async_delayed_work *async_work;
    1358             : 
    1359      746447 :         async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
    1360      746197 :         if (!async_work)
    1361             :                 return -ENOMEM;
    1362             : 
    1363      746197 :         async_work->delayed_root = delayed_root;
    1364      746197 :         btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL,
    1365             :                         NULL);
    1366      745959 :         async_work->nr = nr;
    1367             : 
    1368      745959 :         btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
    1369      745959 :         return 0;
    1370             : }
    1371             : 
    1372      203035 : void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
    1373             : {
    1374      203035 :         WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
    1375      203035 : }
    1376             : 
    1377             : static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
    1378             : {
    1379       36415 :         int val = atomic_read(&delayed_root->items_seq);
    1380             : 
    1381       36415 :         if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
    1382             :                 return 1;
    1383             : 
    1384       26841 :         if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
    1385             :                 return 1;
    1386             : 
    1387             :         return 0;
    1388             : }
    1389             : 
    1390    16957381 : void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
    1391             : {
    1392    16957381 :         struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
    1393             : 
    1394    19221179 :         if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
    1395     2264679 :                 btrfs_workqueue_normal_congested(fs_info->delayed_workers))
    1396    16210073 :                 return;
    1397             : 
    1398      746427 :         if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
    1399        9556 :                 int seq;
    1400        9556 :                 int ret;
    1401             : 
    1402        9556 :                 seq = atomic_read(&delayed_root->items_seq);
    1403             : 
    1404        9556 :                 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
    1405        9574 :                 if (ret)
    1406             :                         return;
    1407             : 
    1408       45867 :                 wait_event_interruptible(delayed_root->wait,
    1409             :                                          could_end_wait(delayed_root, seq));
    1410        9574 :                 return;
    1411             :         }
    1412             : 
    1413      736871 :         btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
    1414             : }
    1415             : 
    1416             : /* Will return 0 or -ENOMEM */
    1417     3378246 : int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
    1418             :                                    const char *name, int name_len,
    1419             :                                    struct btrfs_inode *dir,
    1420             :                                    struct btrfs_disk_key *disk_key, u8 flags,
    1421             :                                    u64 index)
    1422             : {
    1423     3378246 :         struct btrfs_fs_info *fs_info = trans->fs_info;
    1424     3378246 :         const unsigned int leaf_data_size = BTRFS_LEAF_DATA_SIZE(fs_info);
    1425     3378246 :         struct btrfs_delayed_node *delayed_node;
    1426     3378246 :         struct btrfs_delayed_item *delayed_item;
    1427     3378246 :         struct btrfs_dir_item *dir_item;
    1428     3378246 :         bool reserve_leaf_space;
    1429     3378246 :         u32 data_len;
    1430     3378246 :         int ret;
    1431             : 
    1432     3378246 :         delayed_node = btrfs_get_or_create_delayed_node(dir);
    1433     3378180 :         if (IS_ERR(delayed_node))
    1434           0 :                 return PTR_ERR(delayed_node);
    1435             : 
    1436     3378180 :         delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len,
    1437             :                                                 delayed_node,
    1438             :                                                 BTRFS_DELAYED_INSERTION_ITEM);
    1439     3377876 :         if (!delayed_item) {
    1440           0 :                 ret = -ENOMEM;
    1441           0 :                 goto release_node;
    1442             :         }
    1443             : 
    1444     3377876 :         delayed_item->index = index;
    1445             : 
    1446     3377876 :         dir_item = (struct btrfs_dir_item *)delayed_item->data;
    1447     3377876 :         dir_item->location = *disk_key;
    1448     3377876 :         btrfs_set_stack_dir_transid(dir_item, trans->transid);
    1449     3377876 :         btrfs_set_stack_dir_data_len(dir_item, 0);
    1450     3377876 :         btrfs_set_stack_dir_name_len(dir_item, name_len);
    1451     3377876 :         btrfs_set_stack_dir_flags(dir_item, flags);
    1452     6755752 :         memcpy((char *)(dir_item + 1), name, name_len);
    1453             : 
    1454     3377876 :         data_len = delayed_item->data_len + sizeof(struct btrfs_item);
    1455             : 
    1456     3377876 :         mutex_lock(&delayed_node->mutex);
    1457             : 
    1458     3378056 :         if (delayed_node->index_item_leaves == 0 ||
    1459     2390771 :             delayed_node->curr_index_batch_size + data_len > leaf_data_size) {
    1460      987616 :                 delayed_node->curr_index_batch_size = data_len;
    1461      987616 :                 reserve_leaf_space = true;
    1462             :         } else {
    1463     2390440 :                 delayed_node->curr_index_batch_size += data_len;
    1464     2390440 :                 reserve_leaf_space = false;
    1465             :         }
    1466             : 
    1467     3378056 :         if (reserve_leaf_space) {
    1468      987616 :                 ret = btrfs_delayed_item_reserve_metadata(trans, delayed_item);
    1469             :                 /*
    1470             :                  * Space was reserved for a dir index item insertion when we
    1471             :                  * started the transaction, so getting a failure here should be
    1472             :                  * impossible.
    1473             :                  */
    1474      987596 :                 if (WARN_ON(ret)) {
    1475           0 :                         mutex_unlock(&delayed_node->mutex);
    1476           0 :                         btrfs_release_delayed_item(delayed_item);
    1477           0 :                         goto release_node;
    1478             :                 }
    1479             : 
    1480      987596 :                 delayed_node->index_item_leaves++;
    1481     2390440 :         } else if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
    1482     2390352 :                 const u64 bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
    1483             : 
    1484             :                 /*
    1485             :                  * Adding the new dir index item does not require touching another
    1486             :                  * leaf, so we can release 1 unit of metadata that was previously
    1487             :                  * reserved when starting the transaction. This applies only to
    1488             :                  * the case where we had a transaction start and excludes the
    1489             :                  * transaction join case (when replaying log trees).
    1490             :                  */
    1491     2390352 :                 trace_btrfs_space_reservation(fs_info, "transaction",
    1492             :                                               trans->transid, bytes, 0);
    1493     2390319 :                 btrfs_block_rsv_release(fs_info, trans->block_rsv, bytes, NULL);
    1494     2390591 :                 ASSERT(trans->bytes_reserved >= bytes);
    1495     2390591 :                 trans->bytes_reserved -= bytes;
    1496             :         }
    1497             : 
    1498     3378275 :         ret = __btrfs_add_delayed_item(delayed_node, delayed_item);
    1499     3378160 :         if (unlikely(ret)) {
    1500           0 :                 btrfs_err(trans->fs_info,
    1501             :                           "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
    1502             :                           name_len, name, delayed_node->root->root_key.objectid,
    1503             :                           delayed_node->inode_id, ret);
    1504           0 :                 BUG();
    1505             :         }
    1506     3378160 :         mutex_unlock(&delayed_node->mutex);
    1507             : 
    1508     3378195 : release_node:
    1509     3378195 :         btrfs_release_delayed_node(delayed_node);
    1510     3378195 :         return ret;
    1511             : }
    1512             : 
    1513     1885586 : static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
    1514             :                                                struct btrfs_delayed_node *node,
    1515             :                                                u64 index)
    1516             : {
    1517     1885586 :         struct btrfs_delayed_item *item;
    1518             : 
    1519     1885586 :         mutex_lock(&node->mutex);
    1520     1885582 :         item = __btrfs_lookup_delayed_item(&node->ins_root.rb_root, index);
    1521     1885582 :         if (!item) {
    1522     1381532 :                 mutex_unlock(&node->mutex);
    1523     1381532 :                 return 1;
    1524             :         }
    1525             : 
    1526             :         /*
    1527             :          * For delayed items to insert, we track reserved metadata bytes based
    1528             :          * on the number of leaves that we will use.
    1529             :          * See btrfs_insert_delayed_dir_index() and
    1530             :          * btrfs_delayed_item_reserve_metadata()).
    1531             :          */
    1532      504050 :         ASSERT(item->bytes_reserved == 0);
    1533      504050 :         ASSERT(node->index_item_leaves > 0);
    1534             : 
    1535             :         /*
    1536             :          * If there's only one leaf reserved, we can decrement this item from the
    1537             :          * current batch, otherwise we can not because we don't know which leaf
    1538             :          * it belongs to. With the current limit on delayed items, we rarely
    1539             :          * accumulate enough dir index items to fill more than one leaf (even
    1540             :          * when using a leaf size of 4K).
    1541             :          */
    1542      504050 :         if (node->index_item_leaves == 1) {
    1543      504050 :                 const u32 data_len = item->data_len + sizeof(struct btrfs_item);
    1544             : 
    1545      504050 :                 ASSERT(node->curr_index_batch_size >= data_len);
    1546      504050 :                 node->curr_index_batch_size -= data_len;
    1547             :         }
    1548             : 
    1549      504050 :         btrfs_release_delayed_item(item);
    1550             : 
    1551             :         /* If we now have no more dir index items, we can release all leaves. */
    1552      504050 :         if (RB_EMPTY_ROOT(&node->ins_root.rb_root)) {
    1553      334283 :                 btrfs_delayed_item_release_leaves(node, node->index_item_leaves);
    1554      334283 :                 node->index_item_leaves = 0;
    1555             :         }
    1556             : 
    1557      504050 :         mutex_unlock(&node->mutex);
    1558      504050 :         return 0;
    1559             : }
    1560             : 
    1561     1885587 : int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
    1562             :                                    struct btrfs_inode *dir, u64 index)
    1563             : {
    1564     1885587 :         struct btrfs_delayed_node *node;
    1565     1885587 :         struct btrfs_delayed_item *item;
    1566     1885587 :         int ret;
    1567             : 
    1568     1885587 :         node = btrfs_get_or_create_delayed_node(dir);
    1569     1885588 :         if (IS_ERR(node))
    1570           0 :                 return PTR_ERR(node);
    1571             : 
    1572     1885588 :         ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node, index);
    1573     1885584 :         if (!ret)
    1574      504050 :                 goto end;
    1575             : 
    1576     1381534 :         item = btrfs_alloc_delayed_item(0, node, BTRFS_DELAYED_DELETION_ITEM);
    1577     1381504 :         if (!item) {
    1578           0 :                 ret = -ENOMEM;
    1579           0 :                 goto end;
    1580             :         }
    1581             : 
    1582     1381504 :         item->index = index;
    1583             : 
    1584     1381504 :         ret = btrfs_delayed_item_reserve_metadata(trans, item);
    1585             :         /*
    1586             :          * we have reserved enough space when we start a new transaction,
    1587             :          * so reserving metadata failure is impossible.
    1588             :          */
    1589     1381525 :         if (ret < 0) {
    1590           0 :                 btrfs_err(trans->fs_info,
    1591             : "metadata reservation failed for delayed dir item deltiona, should have been reserved");
    1592           0 :                 btrfs_release_delayed_item(item);
    1593           0 :                 goto end;
    1594             :         }
    1595             : 
    1596     1381525 :         mutex_lock(&node->mutex);
    1597     1381536 :         ret = __btrfs_add_delayed_item(node, item);
    1598     1381531 :         if (unlikely(ret)) {
    1599           0 :                 btrfs_err(trans->fs_info,
    1600             :                           "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
    1601             :                           index, node->root->root_key.objectid,
    1602             :                           node->inode_id, ret);
    1603           0 :                 btrfs_delayed_item_release_metadata(dir->root, item);
    1604           0 :                 btrfs_release_delayed_item(item);
    1605             :         }
    1606     1381531 :         mutex_unlock(&node->mutex);
    1607     1885584 : end:
    1608     1885584 :         btrfs_release_delayed_node(node);
    1609     1885584 :         return ret;
    1610             : }
    1611             : 
    1612        6213 : int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
    1613             : {
    1614        6213 :         struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
    1615             : 
    1616        6213 :         if (!delayed_node)
    1617             :                 return -ENOENT;
    1618             : 
    1619             :         /*
    1620             :          * Since we have held i_mutex of this directory, it is impossible that
    1621             :          * a new directory index is added into the delayed node and index_cnt
    1622             :          * is updated now. So we needn't lock the delayed node.
    1623             :          */
    1624        1204 :         if (!delayed_node->index_cnt) {
    1625        1204 :                 btrfs_release_delayed_node(delayed_node);
    1626        1204 :                 return -EINVAL;
    1627             :         }
    1628             : 
    1629           0 :         inode->index_cnt = delayed_node->index_cnt;
    1630           0 :         btrfs_release_delayed_node(delayed_node);
    1631           0 :         return 0;
    1632             : }
    1633             : 
    1634     2773684 : bool btrfs_readdir_get_delayed_items(struct inode *inode,
    1635             :                                      struct list_head *ins_list,
    1636             :                                      struct list_head *del_list)
    1637             : {
    1638     2773684 :         struct btrfs_delayed_node *delayed_node;
    1639     2773684 :         struct btrfs_delayed_item *item;
    1640             : 
    1641     2773684 :         delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
    1642     2774015 :         if (!delayed_node)
    1643             :                 return false;
    1644             : 
    1645             :         /*
    1646             :          * We can only do one readdir with delayed items at a time because of
    1647             :          * item->readdir_list.
    1648             :          */
    1649      487866 :         btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_SHARED);
    1650      487858 :         btrfs_inode_lock(BTRFS_I(inode), 0);
    1651             : 
    1652      487856 :         mutex_lock(&delayed_node->mutex);
    1653      487863 :         item = __btrfs_first_delayed_insertion_item(delayed_node);
    1654    17708760 :         while (item) {
    1655    17419135 :                 refcount_inc(&item->refs);
    1656    17419135 :                 list_add_tail(&item->readdir_list, ins_list);
    1657    17419133 :                 item = __btrfs_next_delayed_item(item);
    1658             :         }
    1659             : 
    1660      487863 :         item = __btrfs_first_delayed_deletion_item(delayed_node);
    1661      525183 :         while (item) {
    1662       49469 :                 refcount_inc(&item->refs);
    1663       49469 :                 list_add_tail(&item->readdir_list, del_list);
    1664       49469 :                 item = __btrfs_next_delayed_item(item);
    1665             :         }
    1666      487863 :         mutex_unlock(&delayed_node->mutex);
    1667             :         /*
    1668             :          * This delayed node is still cached in the btrfs inode, so refs
    1669             :          * must be > 1 now, and we needn't check it is going to be freed
    1670             :          * or not.
    1671             :          *
    1672             :          * Besides that, this function is used to read dir, we do not
    1673             :          * insert/delete delayed items in this period. So we also needn't
    1674             :          * requeue or dequeue this delayed node.
    1675             :          */
    1676      487865 :         refcount_dec(&delayed_node->refs);
    1677             : 
    1678      487865 :         return true;
    1679             : }
    1680             : 
    1681      487869 : void btrfs_readdir_put_delayed_items(struct inode *inode,
    1682             :                                      struct list_head *ins_list,
    1683             :                                      struct list_head *del_list)
    1684             : {
    1685      487869 :         struct btrfs_delayed_item *curr, *next;
    1686             : 
    1687     8456543 :         list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
    1688     7968677 :                 list_del(&curr->readdir_list);
    1689     7968674 :                 if (refcount_dec_and_test(&curr->refs))
    1690         100 :                         kfree(curr);
    1691             :         }
    1692             : 
    1693      537335 :         list_for_each_entry_safe(curr, next, del_list, readdir_list) {
    1694       49471 :                 list_del(&curr->readdir_list);
    1695       49469 :                 if (refcount_dec_and_test(&curr->refs))
    1696          12 :                         kfree(curr);
    1697             :         }
    1698             : 
    1699             :         /*
    1700             :          * The VFS is going to do up_read(), so we need to downgrade back to a
    1701             :          * read lock.
    1702             :          */
    1703      487864 :         downgrade_write(&inode->i_rwsem);
    1704      487862 : }
    1705             : 
    1706  2660161504 : int btrfs_should_delete_dir_index(struct list_head *del_list,
    1707             :                                   u64 index)
    1708             : {
    1709  2660161504 :         struct btrfs_delayed_item *curr;
    1710  2660161504 :         int ret = 0;
    1711             : 
    1712  2678715893 :         list_for_each_entry(curr, del_list, readdir_list) {
    1713    24279350 :                 if (curr->index > index)
    1714             :                         break;
    1715    18563708 :                 if (curr->index == index) {
    1716             :                         ret = 1;
    1717             :                         break;
    1718             :                 }
    1719             :         }
    1720  2660161504 :         return ret;
    1721             : }
    1722             : 
    1723             : /*
    1724             :  * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
    1725             :  *
    1726             :  */
    1727      377972 : int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
    1728             :                                     struct list_head *ins_list)
    1729             : {
    1730      377972 :         struct btrfs_dir_item *di;
    1731      377972 :         struct btrfs_delayed_item *curr, *next;
    1732      377972 :         struct btrfs_key location;
    1733      377972 :         char *name;
    1734      377972 :         int name_len;
    1735      377972 :         int over = 0;
    1736      377972 :         unsigned char d_type;
    1737             : 
    1738      377972 :         if (list_empty(ins_list))
    1739             :                 return 0;
    1740             : 
    1741             :         /*
    1742             :          * Changing the data of the delayed item is impossible. So
    1743             :          * we needn't lock them. And we have held i_mutex of the
    1744             :          * directory, nobody can delete any directory indexes now.
    1745             :          */
    1746     9526893 :         list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
    1747     9450461 :                 list_del(&curr->readdir_list);
    1748             : 
    1749     9450460 :                 if (curr->index < ctx->pos) {
    1750     6158906 :                         if (refcount_dec_and_test(&curr->refs))
    1751           3 :                                 kfree(curr);
    1752     6158906 :                         continue;
    1753             :                 }
    1754             : 
    1755     3291554 :                 ctx->pos = curr->index;
    1756             : 
    1757     3291554 :                 di = (struct btrfs_dir_item *)curr->data;
    1758     3291554 :                 name = (char *)(di + 1);
    1759     3291554 :                 name_len = btrfs_stack_dir_name_len(di);
    1760             : 
    1761     3291554 :                 d_type = fs_ftype_to_dtype(btrfs_dir_flags_to_ftype(di->type));
    1762     3291554 :                 btrfs_disk_key_to_cpu(&location, &di->location);
    1763             : 
    1764     3291554 :                 over = !dir_emit(ctx, name, name_len,
    1765             :                                location.objectid, d_type);
    1766             : 
    1767     3291554 :                 if (refcount_dec_and_test(&curr->refs))
    1768           0 :                         kfree(curr);
    1769             : 
    1770     3291554 :                 if (over)
    1771             :                         return 1;
    1772     3259491 :                 ctx->pos++;
    1773             :         }
    1774             :         return 0;
    1775             : }
    1776             : 
    1777    36410121 : static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
    1778             :                                   struct btrfs_inode_item *inode_item,
    1779             :                                   struct inode *inode)
    1780             : {
    1781    36410121 :         u64 flags;
    1782             : 
    1783    36410121 :         btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
    1784    36409651 :         btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
    1785    36407159 :         btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
    1786    36407159 :         btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
    1787    36407159 :         btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
    1788    36407159 :         btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
    1789    36410958 :         btrfs_set_stack_inode_generation(inode_item,
    1790             :                                          BTRFS_I(inode)->generation);
    1791    36410958 :         btrfs_set_stack_inode_sequence(inode_item,
    1792             :                                        inode_peek_iversion(inode));
    1793    36410958 :         btrfs_set_stack_inode_transid(inode_item, trans->transid);
    1794    36410958 :         btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
    1795    36410958 :         flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
    1796             :                                           BTRFS_I(inode)->ro_flags);
    1797    36410958 :         btrfs_set_stack_inode_flags(inode_item, flags);
    1798    36410958 :         btrfs_set_stack_inode_block_group(inode_item, 0);
    1799             : 
    1800    36410958 :         btrfs_set_stack_timespec_sec(&inode_item->atime,
    1801    36410958 :                                      inode->i_atime.tv_sec);
    1802    36410958 :         btrfs_set_stack_timespec_nsec(&inode_item->atime,
    1803    36410958 :                                       inode->i_atime.tv_nsec);
    1804             : 
    1805    36410958 :         btrfs_set_stack_timespec_sec(&inode_item->mtime,
    1806    36410958 :                                      inode->i_mtime.tv_sec);
    1807    36410958 :         btrfs_set_stack_timespec_nsec(&inode_item->mtime,
    1808    36410958 :                                       inode->i_mtime.tv_nsec);
    1809             : 
    1810    36410958 :         btrfs_set_stack_timespec_sec(&inode_item->ctime,
    1811    36410958 :                                      inode->i_ctime.tv_sec);
    1812    36410958 :         btrfs_set_stack_timespec_nsec(&inode_item->ctime,
    1813    36410958 :                                       inode->i_ctime.tv_nsec);
    1814             : 
    1815    36410958 :         btrfs_set_stack_timespec_sec(&inode_item->otime,
    1816    36410958 :                                      BTRFS_I(inode)->i_otime.tv_sec);
    1817    36410958 :         btrfs_set_stack_timespec_nsec(&inode_item->otime,
    1818    36410958 :                                      BTRFS_I(inode)->i_otime.tv_nsec);
    1819    36410958 : }
    1820             : 
    1821      559711 : int btrfs_fill_inode(struct inode *inode, u32 *rdev)
    1822             : {
    1823      559711 :         struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
    1824      559711 :         struct btrfs_delayed_node *delayed_node;
    1825      559711 :         struct btrfs_inode_item *inode_item;
    1826             : 
    1827      559711 :         delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
    1828      559706 :         if (!delayed_node)
    1829             :                 return -ENOENT;
    1830             : 
    1831         129 :         mutex_lock(&delayed_node->mutex);
    1832         129 :         if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
    1833         115 :                 mutex_unlock(&delayed_node->mutex);
    1834         115 :                 btrfs_release_delayed_node(delayed_node);
    1835         115 :                 return -ENOENT;
    1836             :         }
    1837             : 
    1838          14 :         inode_item = &delayed_node->inode_item;
    1839             : 
    1840          14 :         i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
    1841          14 :         i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
    1842          14 :         btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
    1843          14 :         btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
    1844          14 :                         round_up(i_size_read(inode), fs_info->sectorsize));
    1845          14 :         inode->i_mode = btrfs_stack_inode_mode(inode_item);
    1846          14 :         set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
    1847          14 :         inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
    1848          14 :         BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
    1849          14 :         BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
    1850             : 
    1851          14 :         inode_set_iversion_queried(inode,
    1852             :                                    btrfs_stack_inode_sequence(inode_item));
    1853          14 :         inode->i_rdev = 0;
    1854          14 :         *rdev = btrfs_stack_inode_rdev(inode_item);
    1855          14 :         btrfs_inode_split_flags(btrfs_stack_inode_flags(inode_item),
    1856             :                                 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
    1857             : 
    1858          14 :         inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
    1859          14 :         inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
    1860             : 
    1861          14 :         inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
    1862          14 :         inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
    1863             : 
    1864          14 :         inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
    1865          14 :         inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
    1866             : 
    1867          14 :         BTRFS_I(inode)->i_otime.tv_sec =
    1868          14 :                 btrfs_stack_timespec_sec(&inode_item->otime);
    1869          14 :         BTRFS_I(inode)->i_otime.tv_nsec =
    1870          14 :                 btrfs_stack_timespec_nsec(&inode_item->otime);
    1871             : 
    1872          14 :         inode->i_generation = BTRFS_I(inode)->generation;
    1873          14 :         BTRFS_I(inode)->index_cnt = (u64)-1;
    1874             : 
    1875          14 :         mutex_unlock(&delayed_node->mutex);
    1876          14 :         btrfs_release_delayed_node(delayed_node);
    1877          14 :         return 0;
    1878             : }
    1879             : 
    1880    36501522 : int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
    1881             :                                struct btrfs_root *root,
    1882             :                                struct btrfs_inode *inode)
    1883             : {
    1884    36501522 :         struct btrfs_delayed_node *delayed_node;
    1885    36501522 :         int ret = 0;
    1886             : 
    1887    36501522 :         delayed_node = btrfs_get_or_create_delayed_node(inode);
    1888    36501254 :         if (IS_ERR(delayed_node))
    1889           0 :                 return PTR_ERR(delayed_node);
    1890             : 
    1891    36501254 :         mutex_lock(&delayed_node->mutex);
    1892    73003574 :         if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
    1893    27822691 :                 fill_stack_inode_item(trans, &delayed_node->inode_item,
    1894             :                                       &inode->vfs_inode);
    1895    27821469 :                 goto release_node;
    1896             :         }
    1897             : 
    1898     8679096 :         ret = btrfs_delayed_inode_reserve_metadata(trans, root, delayed_node);
    1899     8679013 :         if (ret)
    1900       89082 :                 goto release_node;
    1901             : 
    1902     8589931 :         fill_stack_inode_item(trans, &delayed_node->inode_item, &inode->vfs_inode);
    1903     8588345 :         set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
    1904     8590331 :         delayed_node->count++;
    1905     8590331 :         atomic_inc(&root->fs_info->delayed_root->items);
    1906    36500022 : release_node:
    1907    36500022 :         mutex_unlock(&delayed_node->mutex);
    1908    36501735 :         btrfs_release_delayed_node(delayed_node);
    1909    36501735 :         return ret;
    1910             : }
    1911             : 
    1912     1337229 : int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
    1913             : {
    1914     1337229 :         struct btrfs_fs_info *fs_info = inode->root->fs_info;
    1915     1337229 :         struct btrfs_delayed_node *delayed_node;
    1916             : 
    1917             :         /*
    1918             :          * we don't do delayed inode updates during log recovery because it
    1919             :          * leads to enospc problems.  This means we also can't do
    1920             :          * delayed inode refs
    1921             :          */
    1922     2674458 :         if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
    1923             :                 return -EAGAIN;
    1924             : 
    1925     1337222 :         delayed_node = btrfs_get_or_create_delayed_node(inode);
    1926     1337225 :         if (IS_ERR(delayed_node))
    1927           0 :                 return PTR_ERR(delayed_node);
    1928             : 
    1929             :         /*
    1930             :          * We don't reserve space for inode ref deletion is because:
    1931             :          * - We ONLY do async inode ref deletion for the inode who has only
    1932             :          *   one link(i_nlink == 1), it means there is only one inode ref.
    1933             :          *   And in most case, the inode ref and the inode item are in the
    1934             :          *   same leaf, and we will deal with them at the same time.
    1935             :          *   Since we are sure we will reserve the space for the inode item,
    1936             :          *   it is unnecessary to reserve space for inode ref deletion.
    1937             :          * - If the inode ref and the inode item are not in the same leaf,
    1938             :          *   We also needn't worry about enospc problem, because we reserve
    1939             :          *   much more space for the inode update than it needs.
    1940             :          * - At the worst, we can steal some space from the global reservation.
    1941             :          *   It is very rare.
    1942             :          */
    1943     1337225 :         mutex_lock(&delayed_node->mutex);
    1944     2674424 :         if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
    1945           0 :                 goto release_node;
    1946             : 
    1947     1337212 :         set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
    1948     1337222 :         delayed_node->count++;
    1949     1337222 :         atomic_inc(&fs_info->delayed_root->items);
    1950     1337220 : release_node:
    1951     1337220 :         mutex_unlock(&delayed_node->mutex);
    1952     1337225 :         btrfs_release_delayed_node(delayed_node);
    1953     1337225 :         return 0;
    1954             : }
    1955             : 
    1956     1371767 : static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
    1957             : {
    1958     1371767 :         struct btrfs_root *root = delayed_node->root;
    1959     1371767 :         struct btrfs_fs_info *fs_info = root->fs_info;
    1960     1371767 :         struct btrfs_delayed_item *curr_item, *prev_item;
    1961             : 
    1962     1371767 :         mutex_lock(&delayed_node->mutex);
    1963     1371768 :         curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
    1964     1371766 :         while (curr_item) {
    1965           5 :                 prev_item = curr_item;
    1966           5 :                 curr_item = __btrfs_next_delayed_item(prev_item);
    1967           5 :                 btrfs_release_delayed_item(prev_item);
    1968             :         }
    1969             : 
    1970     1371761 :         if (delayed_node->index_item_leaves > 0) {
    1971           5 :                 btrfs_delayed_item_release_leaves(delayed_node,
    1972             :                                           delayed_node->index_item_leaves);
    1973           5 :                 delayed_node->index_item_leaves = 0;
    1974             :         }
    1975             : 
    1976     1371761 :         curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
    1977     1528036 :         while (curr_item) {
    1978      156282 :                 btrfs_delayed_item_release_metadata(root, curr_item);
    1979      156282 :                 prev_item = curr_item;
    1980      156282 :                 curr_item = __btrfs_next_delayed_item(prev_item);
    1981      156282 :                 btrfs_release_delayed_item(prev_item);
    1982             :         }
    1983             : 
    1984     1371754 :         btrfs_release_delayed_iref(delayed_node);
    1985             : 
    1986     2743526 :         if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
    1987          12 :                 btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false);
    1988          12 :                 btrfs_release_delayed_inode(delayed_node);
    1989             :         }
    1990     1371763 :         mutex_unlock(&delayed_node->mutex);
    1991     1371767 : }
    1992             : 
    1993     1774487 : void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
    1994             : {
    1995     1774487 :         struct btrfs_delayed_node *delayed_node;
    1996             : 
    1997     1774487 :         delayed_node = btrfs_get_delayed_node(inode);
    1998     1774498 :         if (!delayed_node)
    1999             :                 return;
    2000             : 
    2001     1371764 :         __btrfs_kill_delayed_node(delayed_node);
    2002     1371753 :         btrfs_release_delayed_node(delayed_node);
    2003             : }
    2004             : 
    2005         118 : void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
    2006             : {
    2007         118 :         u64 inode_id = 0;
    2008         118 :         struct btrfs_delayed_node *delayed_nodes[8];
    2009         118 :         int i, n;
    2010             : 
    2011         118 :         while (1) {
    2012         118 :                 spin_lock(&root->inode_lock);
    2013         118 :                 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
    2014             :                                            (void **)delayed_nodes, inode_id,
    2015             :                                            ARRAY_SIZE(delayed_nodes));
    2016         118 :                 if (!n) {
    2017         118 :                         spin_unlock(&root->inode_lock);
    2018         118 :                         break;
    2019             :                 }
    2020             : 
    2021           0 :                 inode_id = delayed_nodes[n - 1]->inode_id + 1;
    2022           0 :                 for (i = 0; i < n; i++) {
    2023             :                         /*
    2024             :                          * Don't increase refs in case the node is dead and
    2025             :                          * about to be removed from the tree in the loop below
    2026             :                          */
    2027           0 :                         if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
    2028           0 :                                 delayed_nodes[i] = NULL;
    2029             :                 }
    2030           0 :                 spin_unlock(&root->inode_lock);
    2031             : 
    2032           0 :                 for (i = 0; i < n; i++) {
    2033           0 :                         if (!delayed_nodes[i])
    2034           0 :                                 continue;
    2035           0 :                         __btrfs_kill_delayed_node(delayed_nodes[i]);
    2036           0 :                         btrfs_release_delayed_node(delayed_nodes[i]);
    2037             :                 }
    2038             :         }
    2039         118 : }
    2040             : 
    2041          37 : void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
    2042             : {
    2043          37 :         struct btrfs_delayed_node *curr_node, *prev_node;
    2044             : 
    2045          37 :         curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
    2046          49 :         while (curr_node) {
    2047          12 :                 __btrfs_kill_delayed_node(curr_node);
    2048             : 
    2049          12 :                 prev_node = curr_node;
    2050          12 :                 curr_node = btrfs_next_delayed_node(curr_node);
    2051          12 :                 btrfs_release_delayed_node(prev_node);
    2052             :         }
    2053          37 : }
    2054             : 
    2055         705 : void btrfs_log_get_delayed_items(struct btrfs_inode *inode,
    2056             :                                  struct list_head *ins_list,
    2057             :                                  struct list_head *del_list)
    2058             : {
    2059         705 :         struct btrfs_delayed_node *node;
    2060         705 :         struct btrfs_delayed_item *item;
    2061             : 
    2062         705 :         node = btrfs_get_delayed_node(inode);
    2063         705 :         if (!node)
    2064             :                 return;
    2065             : 
    2066         632 :         mutex_lock(&node->mutex);
    2067         632 :         item = __btrfs_first_delayed_insertion_item(node);
    2068        1310 :         while (item) {
    2069             :                 /*
    2070             :                  * It's possible that the item is already in a log list. This
    2071             :                  * can happen in case two tasks are trying to log the same
    2072             :                  * directory. For example if we have tasks A and task B:
    2073             :                  *
    2074             :                  * Task A collected the delayed items into a log list while
    2075             :                  * under the inode's log_mutex (at btrfs_log_inode()), but it
    2076             :                  * only releases the items after logging the inodes they point
    2077             :                  * to (if they are new inodes), which happens after unlocking
    2078             :                  * the log mutex;
    2079             :                  *
    2080             :                  * Task B enters btrfs_log_inode() and acquires the log_mutex
    2081             :                  * of the same directory inode, before task B releases the
    2082             :                  * delayed items. This can happen for example when logging some
    2083             :                  * inode we need to trigger logging of its parent directory, so
    2084             :                  * logging two files that have the same parent directory can
    2085             :                  * lead to this.
    2086             :                  *
    2087             :                  * If this happens, just ignore delayed items already in a log
    2088             :                  * list. All the tasks logging the directory are under a log
    2089             :                  * transaction and whichever finishes first can not sync the log
    2090             :                  * before the other completes and leaves the log transaction.
    2091             :                  */
    2092        1150 :                 if (!item->logged && list_empty(&item->log_list)) {
    2093        1130 :                         refcount_inc(&item->refs);
    2094        1130 :                         list_add_tail(&item->log_list, ins_list);
    2095             :                 }
    2096        1150 :                 item = __btrfs_next_delayed_item(item);
    2097             :         }
    2098             : 
    2099         632 :         item = __btrfs_first_delayed_deletion_item(node);
    2100         741 :         while (item) {
    2101             :                 /* It may be non-empty, for the same reason mentioned above. */
    2102         312 :                 if (!item->logged && list_empty(&item->log_list)) {
    2103         304 :                         refcount_inc(&item->refs);
    2104         304 :                         list_add_tail(&item->log_list, del_list);
    2105             :                 }
    2106         312 :                 item = __btrfs_next_delayed_item(item);
    2107             :         }
    2108         632 :         mutex_unlock(&node->mutex);
    2109             : 
    2110             :         /*
    2111             :          * We are called during inode logging, which means the inode is in use
    2112             :          * and can not be evicted before we finish logging the inode. So we never
    2113             :          * have the last reference on the delayed inode.
    2114             :          * Also, we don't use btrfs_release_delayed_node() because that would
    2115             :          * requeue the delayed inode (change its order in the list of prepared
    2116             :          * nodes) and we don't want to do such change because we don't create or
    2117             :          * delete delayed items.
    2118             :          */
    2119         632 :         ASSERT(refcount_read(&node->refs) > 1);
    2120         632 :         refcount_dec(&node->refs);
    2121             : }
    2122             : 
    2123        1343 : void btrfs_log_put_delayed_items(struct btrfs_inode *inode,
    2124             :                                  struct list_head *ins_list,
    2125             :                                  struct list_head *del_list)
    2126             : {
    2127        1343 :         struct btrfs_delayed_node *node;
    2128        1343 :         struct btrfs_delayed_item *item;
    2129        1343 :         struct btrfs_delayed_item *next;
    2130             : 
    2131        1343 :         node = btrfs_get_delayed_node(inode);
    2132        1343 :         if (!node)
    2133             :                 return;
    2134             : 
    2135        1270 :         mutex_lock(&node->mutex);
    2136             : 
    2137        2400 :         list_for_each_entry_safe(item, next, ins_list, log_list) {
    2138        1130 :                 item->logged = true;
    2139        1130 :                 list_del_init(&item->log_list);
    2140        1130 :                 if (refcount_dec_and_test(&item->refs))
    2141          10 :                         kfree(item);
    2142             :         }
    2143             : 
    2144        1574 :         list_for_each_entry_safe(item, next, del_list, log_list) {
    2145         304 :                 item->logged = true;
    2146         304 :                 list_del_init(&item->log_list);
    2147         304 :                 if (refcount_dec_and_test(&item->refs))
    2148           8 :                         kfree(item);
    2149             :         }
    2150             : 
    2151        1270 :         mutex_unlock(&node->mutex);
    2152             : 
    2153             :         /*
    2154             :          * We are called during inode logging, which means the inode is in use
    2155             :          * and can not be evicted before we finish logging the inode. So we never
    2156             :          * have the last reference on the delayed inode.
    2157             :          * Also, we don't use btrfs_release_delayed_node() because that would
    2158             :          * requeue the delayed inode (change its order in the list of prepared
    2159             :          * nodes) and we don't want to do such change because we don't create or
    2160             :          * delete delayed items.
    2161             :          */
    2162        1270 :         ASSERT(refcount_read(&node->refs) > 1);
    2163        1270 :         refcount_dec(&node->refs);
    2164             : }

Generated by: LCOV version 1.14