1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2009 Oracle.  All rights reserved.
4  */
5 
6 #ifndef BTRFS_FREE_SPACE_CACHE_H
7 #define BTRFS_FREE_SPACE_CACHE_H
8 
9 /*
10  * This is the trim state of an extent or bitmap.
11  *
12  * BTRFS_TRIM_STATE_TRIMMING is special and used to maintain the state of a
13  * bitmap as we may need several trims to fully trim a single bitmap entry.
14  * This is reset should any free space other than trimmed space be added to the
15  * bitmap.
16  */
17 enum btrfs_trim_state {
18 	BTRFS_TRIM_STATE_UNTRIMMED,
19 	BTRFS_TRIM_STATE_TRIMMED,
20 	BTRFS_TRIM_STATE_TRIMMING,
21 };
22 
23 struct btrfs_free_space {
24 	struct rb_node offset_index;
25 	u64 offset;
26 	u64 bytes;
27 	u64 max_extent_size;
28 	unsigned long *bitmap;
29 	struct list_head list;
30 	enum btrfs_trim_state trim_state;
31 	s32 bitmap_extents;
32 };
33 
btrfs_free_space_trimmed(struct btrfs_free_space * info)34 static inline bool btrfs_free_space_trimmed(struct btrfs_free_space *info)
35 {
36 	return (info->trim_state == BTRFS_TRIM_STATE_TRIMMED);
37 }
38 
btrfs_free_space_trimming_bitmap(struct btrfs_free_space * info)39 static inline bool btrfs_free_space_trimming_bitmap(
40 					    struct btrfs_free_space *info)
41 {
42 	return (info->trim_state == BTRFS_TRIM_STATE_TRIMMING);
43 }
44 
45 struct btrfs_free_space_ctl {
46 	spinlock_t tree_lock;
47 	struct rb_root free_space_offset;
48 	u64 free_space;
49 	int extents_thresh;
50 	int free_extents;
51 	int total_bitmaps;
52 	int unit;
53 	u64 start;
54 	s32 discardable_extents[BTRFS_STAT_NR_ENTRIES];
55 	s64 discardable_bytes[BTRFS_STAT_NR_ENTRIES];
56 	const struct btrfs_free_space_op *op;
57 	void *private;
58 	struct mutex cache_writeout_mutex;
59 	struct list_head trimming_ranges;
60 };
61 
62 struct btrfs_free_space_op {
63 	bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
64 			   struct btrfs_free_space *info);
65 };
66 
67 struct btrfs_io_ctl {
68 	void *cur, *orig;
69 	struct page *page;
70 	struct page **pages;
71 	struct btrfs_fs_info *fs_info;
72 	struct inode *inode;
73 	unsigned long size;
74 	int index;
75 	int num_pages;
76 	int entries;
77 	int bitmaps;
78 };
79 
80 struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
81 		struct btrfs_path *path);
82 int create_free_space_inode(struct btrfs_trans_handle *trans,
83 			    struct btrfs_block_group *block_group,
84 			    struct btrfs_path *path);
85 int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
86 				  struct inode *inode,
87 				  struct btrfs_block_group *block_group);
88 
89 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
90 				       struct btrfs_block_rsv *rsv);
91 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
92 				    struct btrfs_block_group *block_group,
93 				    struct inode *inode);
94 int load_free_space_cache(struct btrfs_block_group *block_group);
95 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
96 			struct btrfs_block_group *block_group,
97 			struct btrfs_path *path);
98 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
99 			  struct btrfs_block_group *block_group,
100 			  struct btrfs_path *path);
101 
102 void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
103 			       struct btrfs_free_space_ctl *ctl);
104 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
105 			   struct btrfs_free_space_ctl *ctl,
106 			   u64 bytenr, u64 size,
107 			   enum btrfs_trim_state trim_state);
108 int btrfs_add_free_space(struct btrfs_block_group *block_group,
109 			 u64 bytenr, u64 size);
110 int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
111 				u64 bytenr, u64 size);
112 int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
113 				       u64 bytenr, u64 size);
114 int btrfs_remove_free_space(struct btrfs_block_group *block_group,
115 			    u64 bytenr, u64 size);
116 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl);
117 void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group);
118 bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group);
119 u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
120 			       u64 offset, u64 bytes, u64 empty_size,
121 			       u64 *max_extent_size);
122 void btrfs_dump_free_space(struct btrfs_block_group *block_group,
123 			   u64 bytes);
124 int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
125 			     struct btrfs_free_cluster *cluster,
126 			     u64 offset, u64 bytes, u64 empty_size);
127 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster);
128 u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
129 			     struct btrfs_free_cluster *cluster, u64 bytes,
130 			     u64 min_start, u64 *max_extent_size);
131 void btrfs_return_cluster_to_free_space(
132 			       struct btrfs_block_group *block_group,
133 			       struct btrfs_free_cluster *cluster);
134 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
135 			   u64 *trimmed, u64 start, u64 end, u64 minlen);
136 int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
137 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
138 				   bool async);
139 int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
140 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
141 				   u64 maxlen, bool async);
142 
143 bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info);
144 int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active);
145 /* Support functions for running our sanity tests */
146 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
147 int test_add_free_space_entry(struct btrfs_block_group *cache,
148 			      u64 offset, u64 bytes, bool bitmap);
149 int test_check_exists(struct btrfs_block_group *cache, u64 offset, u64 bytes);
150 #endif
151 
152 #endif
153