Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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