Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_BLOCK_RSV_H
4#define BTRFS_BLOCK_RSV_H
5
6struct btrfs_trans_handle;
7enum 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);
53struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
54 unsigned short type);
55void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
56 struct btrfs_block_rsv *rsv,
57 unsigned short type);
58void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
59 struct btrfs_block_rsv *rsv);
60int btrfs_block_rsv_add(struct btrfs_root *root,
61 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
62 enum btrfs_reserve_flush_enum flush);
63int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor);
64int btrfs_block_rsv_refill(struct btrfs_root *root,
65 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
66 enum btrfs_reserve_flush_enum flush);
67int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
68 struct btrfs_block_rsv *dst_rsv, u64 num_bytes,
69 bool update_size);
70int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes);
71int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
72 struct btrfs_block_rsv *dest, u64 num_bytes,
73 int min_factor);
74void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
75 u64 num_bytes, bool update_size);
76u64 __btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
77 struct btrfs_block_rsv *block_rsv,
78 u64 num_bytes, u64 *qgroup_to_release);
79void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info);
80void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info);
81void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info);
82struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root,
84 u32 blocksize);
85
86static inline void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
87 struct btrfs_block_rsv *block_rsv,
88 u64 num_bytes)
89{
90 __btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
91}
92
93static inline void btrfs_unuse_block_rsv(struct btrfs_fs_info *fs_info,
94 struct btrfs_block_rsv *block_rsv,
95 u32 blocksize)
96{
97 btrfs_block_rsv_add_bytes(block_rsv, blocksize, false);
98 btrfs_block_rsv_release(fs_info, block_rsv, 0);
99}
100
101#endif /* BTRFS_BLOCK_RSV_H */
1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_BLOCK_RSV_H
4#define BTRFS_BLOCK_RSV_H
5
6struct btrfs_trans_handle;
7struct btrfs_root;
8enum btrfs_reserve_flush_enum;
9
10/*
11 * Types of block reserves
12 */
13enum btrfs_rsv_type {
14 BTRFS_BLOCK_RSV_GLOBAL,
15 BTRFS_BLOCK_RSV_DELALLOC,
16 BTRFS_BLOCK_RSV_TRANS,
17 BTRFS_BLOCK_RSV_CHUNK,
18 BTRFS_BLOCK_RSV_DELOPS,
19 BTRFS_BLOCK_RSV_DELREFS,
20 BTRFS_BLOCK_RSV_EMPTY,
21 BTRFS_BLOCK_RSV_TEMP,
22};
23
24struct btrfs_block_rsv {
25 u64 size;
26 u64 reserved;
27 struct btrfs_space_info *space_info;
28 spinlock_t lock;
29 bool full;
30 bool failfast;
31 /* Block reserve type, one of BTRFS_BLOCK_RSV_* */
32 enum btrfs_rsv_type type:8;
33
34 /*
35 * Qgroup equivalent for @size @reserved
36 *
37 * Unlike normal @size/@reserved for inode rsv, qgroup doesn't care
38 * about things like csum size nor how many tree blocks it will need to
39 * reserve.
40 *
41 * Qgroup cares more about net change of the extent usage.
42 *
43 * So for one newly inserted file extent, in worst case it will cause
44 * leaf split and level increase, nodesize for each file extent is
45 * already too much.
46 *
47 * In short, qgroup_size/reserved is the upper limit of possible needed
48 * qgroup metadata reservation.
49 */
50 u64 qgroup_rsv_size;
51 u64 qgroup_rsv_reserved;
52};
53
54void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, enum btrfs_rsv_type type);
55void btrfs_init_root_block_rsv(struct btrfs_root *root);
56struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
57 enum btrfs_rsv_type type);
58void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
59 struct btrfs_block_rsv *rsv,
60 enum btrfs_rsv_type type);
61void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
62 struct btrfs_block_rsv *rsv);
63int btrfs_block_rsv_add(struct btrfs_fs_info *fs_info,
64 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
65 enum btrfs_reserve_flush_enum flush);
66int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_percent);
67int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info,
68 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
69 enum btrfs_reserve_flush_enum flush);
70int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
71 struct btrfs_block_rsv *dst_rsv, u64 num_bytes,
72 bool update_size);
73int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes);
74void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
75 u64 num_bytes, bool update_size);
76u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
77 struct btrfs_block_rsv *block_rsv,
78 u64 num_bytes, u64 *qgroup_to_release);
79void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info);
80void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info);
81void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info);
82struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root,
84 u32 blocksize);
85static inline void btrfs_unuse_block_rsv(struct btrfs_fs_info *fs_info,
86 struct btrfs_block_rsv *block_rsv,
87 u32 blocksize)
88{
89 btrfs_block_rsv_add_bytes(block_rsv, blocksize, false);
90 btrfs_block_rsv_release(fs_info, block_rsv, 0, NULL);
91}
92
93/*
94 * Fast path to check if the reserve is full, may be carefully used outside of
95 * locks.
96 */
97static inline bool btrfs_block_rsv_full(const struct btrfs_block_rsv *rsv)
98{
99 return data_race(rsv->full);
100}
101
102#endif /* BTRFS_BLOCK_RSV_H */