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