blob: 3b67ff08d4348abff32a15dfbc9cf216950b4be2 [file] [log] [blame]
Josef Bacikd12ffdd2019-06-19 13:47:17 -04001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_BLOCK_RSV_H
4#define BTRFS_BLOCK_RSV_H
5
Josef Bacik67f9c222019-06-19 13:47:23 -04006struct btrfs_trans_handle;
Josef Bacikd12ffdd2019-06-19 13:47:17 -04007enum btrfs_reserve_flush_enum;
8
9/*
10 * Types of block reserves
11 */
12enum {
13 BTRFS_BLOCK_RSV_GLOBAL,
14 BTRFS_BLOCK_RSV_DELALLOC,
15 BTRFS_BLOCK_RSV_TRANS,
16 BTRFS_BLOCK_RSV_CHUNK,
17 BTRFS_BLOCK_RSV_DELOPS,
18 BTRFS_BLOCK_RSV_DELREFS,
19 BTRFS_BLOCK_RSV_EMPTY,
20 BTRFS_BLOCK_RSV_TEMP,
21};
22
23struct btrfs_block_rsv {
24 u64 size;
25 u64 reserved;
26 struct btrfs_space_info *space_info;
27 spinlock_t lock;
28 unsigned short full;
29 unsigned short type;
30 unsigned short failfast;
31
32 /*
33 * Qgroup equivalent for @size @reserved
34 *
35 * Unlike normal @size/@reserved for inode rsv, qgroup doesn't care
36 * about things like csum size nor how many tree blocks it will need to
37 * reserve.
38 *
39 * Qgroup cares more about net change of the extent usage.
40 *
41 * So for one newly inserted file extent, in worst case it will cause
42 * leaf split and level increase, nodesize for each file extent is
43 * already too much.
44 *
45 * In short, qgroup_size/reserved is the upper limit of possible needed
46 * qgroup metadata reservation.
47 */
48 u64 qgroup_rsv_size;
49 u64 qgroup_rsv_reserved;
50};
51
52void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type);
Josef Bacik2e608bd2021-11-05 16:45:44 -040053void btrfs_init_root_block_rsv(struct btrfs_root *root);
Josef Bacikd12ffdd2019-06-19 13:47:17 -040054struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
55 unsigned short type);
56void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
57 struct btrfs_block_rsv *rsv,
58 unsigned short type);
59void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
60 struct btrfs_block_rsv *rsv);
Josef Bacik92705012021-11-09 10:12:07 -050061int btrfs_block_rsv_add(struct btrfs_fs_info *fs_info,
Josef Bacikd12ffdd2019-06-19 13:47:17 -040062 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
63 enum btrfs_reserve_flush_enum flush);
64int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor);
Josef Bacik92705012021-11-09 10:12:07 -050065int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info,
Josef Bacikd12ffdd2019-06-19 13:47:17 -040066 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
67 enum btrfs_reserve_flush_enum flush);
68int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
69 struct btrfs_block_rsv *dst_rsv, u64 num_bytes,
70 bool update_size);
71int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes);
72int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
73 struct btrfs_block_rsv *dest, u64 num_bytes,
74 int min_factor);
Josef Bacik0b501742019-06-19 13:47:18 -040075void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
76 u64 num_bytes, bool update_size);
Nikolay Borisov63f018b2020-03-10 10:59:31 +020077u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Josef Bacikfed14b32019-06-19 13:47:19 -040078 struct btrfs_block_rsv *block_rsv,
79 u64 num_bytes, u64 *qgroup_to_release);
Josef Bacik67f9c222019-06-19 13:47:23 -040080void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info);
81void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info);
82void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info);
83struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
84 struct btrfs_root *root,
85 u32 blocksize);
Josef Bacik67f9c222019-06-19 13:47:23 -040086static inline void btrfs_unuse_block_rsv(struct btrfs_fs_info *fs_info,
87 struct btrfs_block_rsv *block_rsv,
88 u32 blocksize)
89{
90 btrfs_block_rsv_add_bytes(block_rsv, blocksize, false);
Nikolay Borisov63f018b2020-03-10 10:59:31 +020091 btrfs_block_rsv_release(fs_info, block_rsv, 0, NULL);
Josef Bacik67f9c222019-06-19 13:47:23 -040092}
93
Josef Bacikd12ffdd2019-06-19 13:47:17 -040094#endif /* BTRFS_BLOCK_RSV_H */