LCOV - code coverage report
Current view: top level - fs/btrfs - block-rsv.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 234 246 95.1 %
Date: 2023-07-31 20:08:34 Functions: 19 19 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : 
       3             : #include "misc.h"
       4             : #include "ctree.h"
       5             : #include "block-rsv.h"
       6             : #include "space-info.h"
       7             : #include "transaction.h"
       8             : #include "block-group.h"
       9             : #include "disk-io.h"
      10             : #include "fs.h"
      11             : #include "accessors.h"
      12             : 
      13             : /*
      14             :  * HOW DO BLOCK RESERVES WORK
      15             :  *
      16             :  *   Think of block_rsv's as buckets for logically grouped metadata
      17             :  *   reservations.  Each block_rsv has a ->size and a ->reserved.  ->size is
      18             :  *   how large we want our block rsv to be, ->reserved is how much space is
      19             :  *   currently reserved for this block reserve.
      20             :  *
      21             :  *   ->failfast exists for the truncate case, and is described below.
      22             :  *
      23             :  * NORMAL OPERATION
      24             :  *
      25             :  *   -> Reserve
      26             :  *     Entrance: btrfs_block_rsv_add, btrfs_block_rsv_refill
      27             :  *
      28             :  *     We call into btrfs_reserve_metadata_bytes() with our bytes, which is
      29             :  *     accounted for in space_info->bytes_may_use, and then add the bytes to
      30             :  *     ->reserved, and ->size in the case of btrfs_block_rsv_add.
      31             :  *
      32             :  *     ->size is an over-estimation of how much we may use for a particular
      33             :  *     operation.
      34             :  *
      35             :  *   -> Use
      36             :  *     Entrance: btrfs_use_block_rsv
      37             :  *
      38             :  *     When we do a btrfs_alloc_tree_block() we call into btrfs_use_block_rsv()
      39             :  *     to determine the appropriate block_rsv to use, and then verify that
      40             :  *     ->reserved has enough space for our tree block allocation.  Once
      41             :  *     successful we subtract fs_info->nodesize from ->reserved.
      42             :  *
      43             :  *   -> Finish
      44             :  *     Entrance: btrfs_block_rsv_release
      45             :  *
      46             :  *     We are finished with our operation, subtract our individual reservation
      47             :  *     from ->size, and then subtract ->size from ->reserved and free up the
      48             :  *     excess if there is any.
      49             :  *
      50             :  *     There is some logic here to refill the delayed refs rsv or the global rsv
      51             :  *     as needed, otherwise the excess is subtracted from
      52             :  *     space_info->bytes_may_use.
      53             :  *
      54             :  * TYPES OF BLOCK RESERVES
      55             :  *
      56             :  * BLOCK_RSV_TRANS, BLOCK_RSV_DELOPS, BLOCK_RSV_CHUNK
      57             :  *   These behave normally, as described above, just within the confines of the
      58             :  *   lifetime of their particular operation (transaction for the whole trans
      59             :  *   handle lifetime, for example).
      60             :  *
      61             :  * BLOCK_RSV_GLOBAL
      62             :  *   It is impossible to properly account for all the space that may be required
      63             :  *   to make our extent tree updates.  This block reserve acts as an overflow
      64             :  *   buffer in case our delayed refs reserve does not reserve enough space to
      65             :  *   update the extent tree.
      66             :  *
      67             :  *   We can steal from this in some cases as well, notably on evict() or
      68             :  *   truncate() in order to help users recover from ENOSPC conditions.
      69             :  *
      70             :  * BLOCK_RSV_DELALLOC
      71             :  *   The individual item sizes are determined by the per-inode size
      72             :  *   calculations, which are described with the delalloc code.  This is pretty
      73             :  *   straightforward, it's just the calculation of ->size encodes a lot of
      74             :  *   different items, and thus it gets used when updating inodes, inserting file
      75             :  *   extents, and inserting checksums.
      76             :  *
      77             :  * BLOCK_RSV_DELREFS
      78             :  *   We keep a running tally of how many delayed refs we have on the system.
      79             :  *   We assume each one of these delayed refs are going to use a full
      80             :  *   reservation.  We use the transaction items and pre-reserve space for every
      81             :  *   operation, and use this reservation to refill any gap between ->size and
      82             :  *   ->reserved that may exist.
      83             :  *
      84             :  *   From there it's straightforward, removing a delayed ref means we remove its
      85             :  *   count from ->size and free up reservations as necessary.  Since this is
      86             :  *   the most dynamic block reserve in the system, we will try to refill this
      87             :  *   block reserve first with any excess returned by any other block reserve.
      88             :  *
      89             :  * BLOCK_RSV_EMPTY
      90             :  *   This is the fallback block reserve to make us try to reserve space if we
      91             :  *   don't have a specific bucket for this allocation.  It is mostly used for
      92             :  *   updating the device tree and such, since that is a separate pool we're
      93             :  *   content to just reserve space from the space_info on demand.
      94             :  *
      95             :  * BLOCK_RSV_TEMP
      96             :  *   This is used by things like truncate and iput.  We will temporarily
      97             :  *   allocate a block reserve, set it to some size, and then truncate bytes
      98             :  *   until we have no space left.  With ->failfast set we'll simply return
      99             :  *   ENOSPC from btrfs_use_block_rsv() to signal that we need to unwind and try
     100             :  *   to make a new reservation.  This is because these operations are
     101             :  *   unbounded, so we want to do as much work as we can, and then back off and
     102             :  *   re-reserve.
     103             :  */
     104             : 
     105   174771976 : static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
     106             :                                     struct btrfs_block_rsv *block_rsv,
     107             :                                     struct btrfs_block_rsv *dest, u64 num_bytes,
     108             :                                     u64 *qgroup_to_release_ret)
     109             : {
     110   174771976 :         struct btrfs_space_info *space_info = block_rsv->space_info;
     111   174771976 :         u64 qgroup_to_release = 0;
     112   174771976 :         u64 ret;
     113             : 
     114   174771976 :         spin_lock(&block_rsv->lock);
     115   174936727 :         if (num_bytes == (u64)-1) {
     116    20367210 :                 num_bytes = block_rsv->size;
     117    20367210 :                 qgroup_to_release = block_rsv->qgroup_rsv_size;
     118             :         }
     119   174936727 :         block_rsv->size -= num_bytes;
     120   174936727 :         if (block_rsv->reserved >= block_rsv->size) {
     121   139907177 :                 num_bytes = block_rsv->reserved - block_rsv->size;
     122   139907177 :                 block_rsv->reserved = block_rsv->size;
     123   139907177 :                 block_rsv->full = true;
     124             :         } else {
     125             :                 num_bytes = 0;
     126             :         }
     127   174936727 :         if (qgroup_to_release_ret &&
     128    61760991 :             block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
     129    56561246 :                 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
     130             :                                     block_rsv->qgroup_rsv_size;
     131    56561246 :                 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
     132             :         } else {
     133             :                 qgroup_to_release = 0;
     134             :         }
     135   174936727 :         spin_unlock(&block_rsv->lock);
     136             : 
     137   174908048 :         ret = num_bytes;
     138   174908048 :         if (num_bytes > 0) {
     139   134254792 :                 if (dest) {
     140    36838846 :                         spin_lock(&dest->lock);
     141    36842110 :                         if (!dest->full) {
     142    20745687 :                                 u64 bytes_to_add;
     143             : 
     144    20745687 :                                 bytes_to_add = dest->size - dest->reserved;
     145    20745687 :                                 bytes_to_add = min(num_bytes, bytes_to_add);
     146    20745687 :                                 dest->reserved += bytes_to_add;
     147    20745687 :                                 if (dest->reserved >= dest->size)
     148     7589579 :                                         dest->full = true;
     149    20745687 :                                 num_bytes -= bytes_to_add;
     150             :                         }
     151    36842110 :                         spin_unlock(&dest->lock);
     152             :                 }
     153   134259544 :                 if (num_bytes)
     154   120431305 :                         btrfs_space_info_free_bytes_may_use(fs_info,
     155             :                                                             space_info,
     156             :                                                             num_bytes);
     157             :         }
     158   175013148 :         if (qgroup_to_release_ret)
     159    61819115 :                 *qgroup_to_release_ret = qgroup_to_release;
     160   175013148 :         return ret;
     161             : }
     162             : 
     163    30984890 : int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
     164             :                             struct btrfs_block_rsv *dst, u64 num_bytes,
     165             :                             bool update_size)
     166             : {
     167    30984890 :         int ret;
     168             : 
     169    30984890 :         ret = btrfs_block_rsv_use_bytes(src, num_bytes);
     170    30985834 :         if (ret)
     171             :                 return ret;
     172             : 
     173    30985822 :         btrfs_block_rsv_add_bytes(dst, num_bytes, update_size);
     174    30985822 :         return 0;
     175             : }
     176             : 
     177    24253556 : void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, enum btrfs_rsv_type type)
     178             : {
     179    24253556 :         memset(rsv, 0, sizeof(*rsv));
     180    24253556 :         spin_lock_init(&rsv->lock);
     181    24254768 :         rsv->type = type;
     182    24254768 : }
     183             : 
     184    24234746 : void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
     185             :                                    struct btrfs_block_rsv *rsv,
     186             :                                    enum btrfs_rsv_type type)
     187             : {
     188    24234746 :         btrfs_init_block_rsv(rsv, type);
     189    24233535 :         rsv->space_info = btrfs_find_space_info(fs_info,
     190             :                                             BTRFS_BLOCK_GROUP_METADATA);
     191    24232499 : }
     192             : 
     193    20352816 : struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
     194             :                                               enum btrfs_rsv_type type)
     195             : {
     196    20352816 :         struct btrfs_block_rsv *block_rsv;
     197             : 
     198    20352816 :         block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
     199    20352627 :         if (!block_rsv)
     200             :                 return NULL;
     201             : 
     202    20352627 :         btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
     203    20352627 :         return block_rsv;
     204             : }
     205             : 
     206    22450918 : void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
     207             :                           struct btrfs_block_rsv *rsv)
     208             : {
     209    22450918 :         if (!rsv)
     210             :                 return;
     211    20352895 :         btrfs_block_rsv_release(fs_info, rsv, (u64)-1, NULL);
     212    20353010 :         kfree(rsv);
     213             : }
     214             : 
     215    27852382 : int btrfs_block_rsv_add(struct btrfs_fs_info *fs_info,
     216             :                         struct btrfs_block_rsv *block_rsv, u64 num_bytes,
     217             :                         enum btrfs_reserve_flush_enum flush)
     218             : {
     219    27852382 :         int ret;
     220             : 
     221    27852382 :         if (num_bytes == 0)
     222             :                 return 0;
     223             : 
     224    27850412 :         ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
     225    27900263 :         if (!ret)
     226    27786278 :                 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
     227             : 
     228             :         return ret;
     229             : }
     230             : 
     231        2906 : int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_percent)
     232             : {
     233        2906 :         u64 num_bytes = 0;
     234        2906 :         int ret = -ENOSPC;
     235             : 
     236        2906 :         spin_lock(&block_rsv->lock);
     237        2906 :         num_bytes = mult_perc(block_rsv->size, min_percent);
     238        2906 :         if (block_rsv->reserved >= num_bytes)
     239        2906 :                 ret = 0;
     240        2906 :         spin_unlock(&block_rsv->lock);
     241             : 
     242        2906 :         return ret;
     243             : }
     244             : 
     245     7696385 : int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info,
     246             :                            struct btrfs_block_rsv *block_rsv, u64 num_bytes,
     247             :                            enum btrfs_reserve_flush_enum flush)
     248             : {
     249     7696385 :         int ret = -ENOSPC;
     250             : 
     251     7696385 :         if (!block_rsv)
     252             :                 return 0;
     253             : 
     254     7696385 :         spin_lock(&block_rsv->lock);
     255     7696757 :         if (block_rsv->reserved >= num_bytes)
     256             :                 ret = 0;
     257             :         else
     258     5527971 :                 num_bytes -= block_rsv->reserved;
     259     7696757 :         spin_unlock(&block_rsv->lock);
     260             : 
     261     7696530 :         if (!ret)
     262             :                 return 0;
     263             : 
     264     5527743 :         ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
     265     5528120 :         if (!ret) {
     266     5528120 :                 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
     267     5528120 :                 return 0;
     268             :         }
     269             : 
     270             :         return ret;
     271             : }
     272             : 
     273   174798758 : u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
     274             :                             struct btrfs_block_rsv *block_rsv, u64 num_bytes,
     275             :                             u64 *qgroup_to_release)
     276             : {
     277   174798758 :         struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
     278   174798758 :         struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
     279   174798758 :         struct btrfs_block_rsv *target = NULL;
     280             : 
     281             :         /*
     282             :          * If we are the delayed_rsv then push to the global rsv, otherwise dump
     283             :          * into the delayed rsv if it is not full.
     284             :          */
     285   174798758 :         if (block_rsv == delayed_rsv)
     286             :                 target = global_rsv;
     287   123684244 :         else if (block_rsv != global_rsv && !btrfs_block_rsv_full(delayed_rsv))
     288             :                 target = delayed_rsv;
     289             : 
     290    72583853 :         if (target && block_rsv->space_info != target->space_info)
     291        1984 :                 target = NULL;
     292             : 
     293   174798758 :         return block_rsv_release_bytes(fs_info, block_rsv, target, num_bytes,
     294             :                                        qgroup_to_release);
     295             : }
     296             : 
     297    39490713 : int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes)
     298             : {
     299    39490713 :         int ret = -ENOSPC;
     300             : 
     301    39490713 :         spin_lock(&block_rsv->lock);
     302    39492779 :         if (block_rsv->reserved >= num_bytes) {
     303    39084972 :                 block_rsv->reserved -= num_bytes;
     304    39084972 :                 if (block_rsv->reserved < block_rsv->size)
     305    35493407 :                         block_rsv->full = false;
     306             :                 ret = 0;
     307             :         }
     308    39492779 :         spin_unlock(&block_rsv->lock);
     309    39492868 :         return ret;
     310             : }
     311             : 
     312   107839040 : void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
     313             :                                u64 num_bytes, bool update_size)
     314             : {
     315   107839040 :         spin_lock(&block_rsv->lock);
     316   107847216 :         block_rsv->reserved += num_bytes;
     317   107847216 :         if (update_size)
     318    40197864 :                 block_rsv->size += num_bytes;
     319    67649352 :         else if (block_rsv->reserved >= block_rsv->size)
     320    65187126 :                 block_rsv->full = true;
     321   107847216 :         spin_unlock(&block_rsv->lock);
     322   107839934 : }
     323             : 
     324        4687 : void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
     325             : {
     326        4687 :         struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
     327        4687 :         struct btrfs_space_info *sinfo = block_rsv->space_info;
     328        4687 :         struct btrfs_root *root, *tmp;
     329        4687 :         u64 num_bytes = btrfs_root_used(&fs_info->tree_root->root_item);
     330        4687 :         unsigned int min_items = 1;
     331             : 
     332             :         /*
     333             :          * The global block rsv is based on the size of the extent tree, the
     334             :          * checksum tree and the root tree.  If the fs is empty we want to set
     335             :          * it to a minimal amount for safety.
     336             :          *
     337             :          * We also are going to need to modify the minimum of the tree root and
     338             :          * any global roots we could touch.
     339             :          */
     340        4687 :         read_lock(&fs_info->global_root_lock);
     341       23428 :         rbtree_postorder_for_each_entry_safe(root, tmp, &fs_info->global_root_tree,
     342             :                                              rb_node) {
     343       14054 :                 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
     344        4680 :                     root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID ||
     345             :                     root->root_key.objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) {
     346       14054 :                         num_bytes += btrfs_root_used(&root->root_item);
     347       14054 :                         min_items++;
     348             :                 }
     349             :         }
     350        4687 :         read_unlock(&fs_info->global_root_lock);
     351             : 
     352        4687 :         if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
     353           0 :                 num_bytes += btrfs_root_used(&fs_info->block_group_root->root_item);
     354           0 :                 min_items++;
     355             :         }
     356             : 
     357             :         /*
     358             :          * But we also want to reserve enough space so we can do the fallback
     359             :          * global reserve for an unlink, which is an additional
     360             :          * BTRFS_UNLINK_METADATA_UNITS items.
     361             :          *
     362             :          * But we also need space for the delayed ref updates from the unlink,
     363             :          * so add BTRFS_UNLINK_METADATA_UNITS units for delayed refs, one for
     364             :          * each unlink metadata item.
     365             :          */
     366        4687 :         min_items += BTRFS_UNLINK_METADATA_UNITS;
     367             : 
     368        4687 :         num_bytes = max_t(u64, num_bytes,
     369             :                           btrfs_calc_insert_metadata_size(fs_info, min_items) +
     370             :                           btrfs_calc_delayed_ref_bytes(fs_info,
     371             :                                                BTRFS_UNLINK_METADATA_UNITS));
     372             : 
     373        4687 :         spin_lock(&sinfo->lock);
     374        4687 :         spin_lock(&block_rsv->lock);
     375             : 
     376        4687 :         block_rsv->size = min_t(u64, num_bytes, SZ_512M);
     377             : 
     378        4687 :         if (block_rsv->reserved < block_rsv->size) {
     379        3608 :                 num_bytes = block_rsv->size - block_rsv->reserved;
     380        3608 :                 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
     381             :                                                       num_bytes);
     382        3608 :                 block_rsv->reserved = block_rsv->size;
     383        1079 :         } else if (block_rsv->reserved > block_rsv->size) {
     384          79 :                 num_bytes = block_rsv->reserved - block_rsv->size;
     385          79 :                 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
     386          79 :                                                       -num_bytes);
     387          79 :                 block_rsv->reserved = block_rsv->size;
     388          79 :                 btrfs_try_granting_tickets(fs_info, sinfo);
     389             :         }
     390             : 
     391        4687 :         block_rsv->full = (block_rsv->reserved == block_rsv->size);
     392             : 
     393        4687 :         if (block_rsv->size >= sinfo->total_bytes)
     394           0 :                 sinfo->force_alloc = CHUNK_ALLOC_FORCE;
     395        4687 :         spin_unlock(&block_rsv->lock);
     396        4687 :         spin_unlock(&sinfo->lock);
     397        4687 : }
     398             : 
     399       66801 : void btrfs_init_root_block_rsv(struct btrfs_root *root)
     400             : {
     401       66801 :         struct btrfs_fs_info *fs_info = root->fs_info;
     402             : 
     403       66801 :         switch (root->root_key.objectid) {
     404        9647 :         case BTRFS_CSUM_TREE_OBJECTID:
     405             :         case BTRFS_EXTENT_TREE_OBJECTID:
     406             :         case BTRFS_FREE_SPACE_TREE_OBJECTID:
     407             :         case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
     408        9647 :                 root->block_rsv = &fs_info->delayed_refs_rsv;
     409        9647 :                 break;
     410        9842 :         case BTRFS_ROOT_TREE_OBJECTID:
     411             :         case BTRFS_DEV_TREE_OBJECTID:
     412             :         case BTRFS_QUOTA_TREE_OBJECTID:
     413        9842 :                 root->block_rsv = &fs_info->global_block_rsv;
     414        9842 :                 break;
     415        3244 :         case BTRFS_CHUNK_TREE_OBJECTID:
     416        3244 :                 root->block_rsv = &fs_info->chunk_block_rsv;
     417        3244 :                 break;
     418       44068 :         default:
     419       44068 :                 root->block_rsv = NULL;
     420       44068 :                 break;
     421             :         }
     422       66801 : }
     423             : 
     424        3217 : void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info)
     425             : {
     426        3217 :         struct btrfs_space_info *space_info;
     427             : 
     428        3217 :         space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
     429        3217 :         fs_info->chunk_block_rsv.space_info = space_info;
     430             : 
     431        3217 :         space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
     432        3217 :         fs_info->global_block_rsv.space_info = space_info;
     433        3217 :         fs_info->trans_block_rsv.space_info = space_info;
     434        3217 :         fs_info->empty_block_rsv.space_info = space_info;
     435        3217 :         fs_info->delayed_block_rsv.space_info = space_info;
     436        3217 :         fs_info->delayed_refs_rsv.space_info = space_info;
     437             : 
     438        3217 :         btrfs_update_global_block_rsv(fs_info);
     439        3217 : }
     440             : 
     441        3218 : void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info)
     442             : {
     443        3218 :         btrfs_block_rsv_release(fs_info, &fs_info->global_block_rsv, (u64)-1,
     444             :                                 NULL);
     445        3218 :         WARN_ON(fs_info->trans_block_rsv.size > 0);
     446        3218 :         WARN_ON(fs_info->trans_block_rsv.reserved > 0);
     447        3218 :         WARN_ON(fs_info->chunk_block_rsv.size > 0);
     448        3218 :         WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
     449        3218 :         WARN_ON(fs_info->delayed_block_rsv.size > 0);
     450        3218 :         WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
     451        3218 :         WARN_ON(fs_info->delayed_refs_rsv.reserved > 0);
     452        3218 :         WARN_ON(fs_info->delayed_refs_rsv.size > 0);
     453        3218 : }
     454             : 
     455     9451134 : static struct btrfs_block_rsv *get_block_rsv(
     456             :                                         const struct btrfs_trans_handle *trans,
     457             :                                         const struct btrfs_root *root)
     458             : {
     459     9451134 :         struct btrfs_fs_info *fs_info = root->fs_info;
     460     9451134 :         struct btrfs_block_rsv *block_rsv = NULL;
     461             : 
     462     9451134 :         if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
     463     4182416 :             (root == fs_info->uuid_root) ||
     464     4180954 :             (trans->adding_csums &&
     465      184050 :              root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID))
     466     5454230 :                 block_rsv = trans->block_rsv;
     467             : 
     468     5454230 :         if (!block_rsv)
     469     4001674 :                 block_rsv = root->block_rsv;
     470             : 
     471     9451134 :         if (!block_rsv)
     472     1112103 :                 block_rsv = &fs_info->empty_block_rsv;
     473             : 
     474     9451134 :         return block_rsv;
     475             : }
     476             : 
     477     9451011 : struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
     478             :                                             struct btrfs_root *root,
     479             :                                             u32 blocksize)
     480             : {
     481     9451011 :         struct btrfs_fs_info *fs_info = root->fs_info;
     482     9451011 :         struct btrfs_block_rsv *block_rsv;
     483     9451011 :         struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
     484     9451011 :         int ret;
     485     9451011 :         bool global_updated = false;
     486             : 
     487     9451011 :         block_rsv = get_block_rsv(trans, root);
     488             : 
     489     9451003 :         if (unlikely(block_rsv->size == 0))
     490     1112922 :                 goto try_reserve;
     491     8338081 : again:
     492     8338081 :         ret = btrfs_block_rsv_use_bytes(block_rsv, blocksize);
     493     8338210 :         if (!ret)
     494             :                 return block_rsv;
     495             : 
     496      407794 :         if (block_rsv->failfast)
     497        1372 :                 return ERR_PTR(ret);
     498             : 
     499      406422 :         if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
     500           0 :                 global_updated = true;
     501           0 :                 btrfs_update_global_block_rsv(fs_info);
     502           0 :                 goto again;
     503             :         }
     504             : 
     505             :         /*
     506             :          * The global reserve still exists to save us from ourselves, so don't
     507             :          * warn_on if we are short on our delayed refs reserve.
     508             :          */
     509      406422 :         if (block_rsv->type != BTRFS_BLOCK_RSV_DELREFS &&
     510          66 :             btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
     511           0 :                 static DEFINE_RATELIMIT_STATE(_rs,
     512             :                                 DEFAULT_RATELIMIT_INTERVAL * 10,
     513             :                                 /*DEFAULT_RATELIMIT_BURST*/ 1);
     514           0 :                 if (__ratelimit(&_rs))
     515           0 :                         WARN(1, KERN_DEBUG
     516             :                                 "BTRFS: block rsv %d returned %d\n",
     517             :                                 block_rsv->type, ret);
     518             :         }
     519      406422 : try_reserve:
     520     1519344 :         ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
     521             :                                            BTRFS_RESERVE_NO_FLUSH);
     522     1519349 :         if (!ret)
     523             :                 return block_rsv;
     524             :         /*
     525             :          * If we couldn't reserve metadata bytes try and use some from
     526             :          * the global reserve if its space type is the same as the global
     527             :          * reservation.
     528             :          */
     529      169144 :         if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
     530      169144 :             block_rsv->space_info == global_rsv->space_info) {
     531      169144 :                 ret = btrfs_block_rsv_use_bytes(global_rsv, blocksize);
     532      169144 :                 if (!ret)
     533             :                         return global_rsv;
     534             :         }
     535             : 
     536             :         /*
     537             :          * All hope is lost, but of course our reservations are overly
     538             :          * pessimistic, so instead of possibly having an ENOSPC abort here, try
     539             :          * one last time to force a reservation if there's enough actual space
     540             :          * on disk to make the reservation.
     541             :          */
     542           0 :         ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
     543             :                                            BTRFS_RESERVE_FLUSH_EMERGENCY);
     544           0 :         if (!ret)
     545             :                 return block_rsv;
     546             : 
     547           0 :         return ERR_PTR(ret);
     548             : }
     549             : 
     550          12 : int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
     551             :                                        struct btrfs_block_rsv *rsv)
     552             : {
     553          12 :         u64 needed_bytes;
     554          12 :         int ret;
     555             : 
     556             :         /* 1 for slack space, 1 for updating the inode */
     557          12 :         needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
     558             :                 btrfs_calc_metadata_size(fs_info, 1);
     559             : 
     560          12 :         spin_lock(&rsv->lock);
     561          12 :         if (rsv->reserved < needed_bytes)
     562             :                 ret = -ENOSPC;
     563             :         else
     564          12 :                 ret = 0;
     565          12 :         spin_unlock(&rsv->lock);
     566          12 :         return ret;
     567             : }

Generated by: LCOV version 1.14