xref: /openbmc/linux/fs/btrfs/free-space-cache.h (revision 7a670b42)
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 #include <linux/freezer.h>
10 
11 /*
12  * This is the trim state of an extent or bitmap.
13  *
14  * BTRFS_TRIM_STATE_TRIMMING is special and used to maintain the state of a
15  * bitmap as we may need several trims to fully trim a single bitmap entry.
16  * This is reset should any free space other than trimmed space be added to the
17  * bitmap.
18  */
19 enum btrfs_trim_state {
20 	BTRFS_TRIM_STATE_UNTRIMMED,
21 	BTRFS_TRIM_STATE_TRIMMED,
22 	BTRFS_TRIM_STATE_TRIMMING,
23 };
24 
25 struct btrfs_free_space {
26 	struct rb_node offset_index;
27 	struct rb_node bytes_index;
28 	u64 offset;
29 	u64 bytes;
30 	u64 max_extent_size;
31 	unsigned long *bitmap;
32 	struct list_head list;
33 	enum btrfs_trim_state trim_state;
34 	s32 bitmap_extents;
35 };
36 
btrfs_free_space_trimmed(struct btrfs_free_space * info)37 static inline bool btrfs_free_space_trimmed(struct btrfs_free_space *info)
38 {
39 	return (info->trim_state == BTRFS_TRIM_STATE_TRIMMED);
40 }
41 
btrfs_free_space_trimming_bitmap(struct btrfs_free_space * info)42 static inline bool btrfs_free_space_trimming_bitmap(
43 					    struct btrfs_free_space *info)
44 {
45 	return (info->trim_state == BTRFS_TRIM_STATE_TRIMMING);
46 }
47 
btrfs_trim_interrupted(void)48 static inline bool btrfs_trim_interrupted(void)
49 {
50 	return fatal_signal_pending(current) || freezing(current);
51 }
52 
53 /*
54  * Deltas are an effective way to populate global statistics.  Give macro names
55  * to make it clear what we're doing.  An example is discard_extents in
56  * btrfs_free_space_ctl.
57  */
58 enum {
59 	BTRFS_STAT_CURR,
60 	BTRFS_STAT_PREV,
61 	BTRFS_STAT_NR_ENTRIES,
62 };
63 
64 struct btrfs_free_space_ctl {
65 	spinlock_t tree_lock;
66 	struct rb_root free_space_offset;
67 	struct rb_root_cached free_space_bytes;
68 	u64 free_space;
69 	int extents_thresh;
70 	int free_extents;
71 	int total_bitmaps;
72 	int unit;
73 	u64 start;
74 	s32 discardable_extents[BTRFS_STAT_NR_ENTRIES];
75 	s64 discardable_bytes[BTRFS_STAT_NR_ENTRIES];
76 	const struct btrfs_free_space_op *op;
77 	struct btrfs_block_group *block_group;
78 	struct mutex cache_writeout_mutex;
79 	struct list_head trimming_ranges;
80 };
81 
82 struct btrfs_free_space_op {
83 	bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
84 			   struct btrfs_free_space *info);
85 };
86 
87 struct btrfs_io_ctl {
88 	void *cur, *orig;
89 	struct page *page;
90 	struct page **pages;
91 	struct btrfs_fs_info *fs_info;
92 	struct inode *inode;
93 	unsigned long size;
94 	int index;
95 	int num_pages;
96 	int entries;
97 	int bitmaps;
98 };
99 
100 int __init btrfs_free_space_init(void);
101 void __cold btrfs_free_space_exit(void);
102 struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
103 		struct btrfs_path *path);
104 int create_free_space_inode(struct btrfs_trans_handle *trans,
105 			    struct btrfs_block_group *block_group,
106 			    struct btrfs_path *path);
107 int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
108 				  struct inode *inode,
109 				  struct btrfs_block_group *block_group);
110 
111 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
112 				    struct btrfs_block_group *block_group,
113 				    struct inode *inode);
114 int load_free_space_cache(struct btrfs_block_group *block_group);
115 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
116 			struct btrfs_block_group *block_group,
117 			struct btrfs_path *path);
118 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
119 			  struct btrfs_block_group *block_group,
120 			  struct btrfs_path *path);
121 
122 void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
123 			       struct btrfs_free_space_ctl *ctl);
124 int __btrfs_add_free_space(struct btrfs_block_group *block_group, u64 bytenr,
125 			   u64 size, enum btrfs_trim_state trim_state);
126 int btrfs_add_free_space(struct btrfs_block_group *block_group,
127 			 u64 bytenr, u64 size);
128 int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
129 				u64 bytenr, u64 size);
130 int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
131 				       u64 bytenr, u64 size);
132 int btrfs_remove_free_space(struct btrfs_block_group *block_group,
133 			    u64 bytenr, u64 size);
134 void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group);
135 bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group);
136 u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
137 			       u64 offset, u64 bytes, u64 empty_size,
138 			       u64 *max_extent_size);
139 void btrfs_dump_free_space(struct btrfs_block_group *block_group,
140 			   u64 bytes);
141 int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
142 			     struct btrfs_free_cluster *cluster,
143 			     u64 offset, u64 bytes, u64 empty_size);
144 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster);
145 u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
146 			     struct btrfs_free_cluster *cluster, u64 bytes,
147 			     u64 min_start, u64 *max_extent_size);
148 void btrfs_return_cluster_to_free_space(
149 			       struct btrfs_block_group *block_group,
150 			       struct btrfs_free_cluster *cluster);
151 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
152 			   u64 *trimmed, u64 start, u64 end, u64 minlen);
153 int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
154 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
155 				   bool async);
156 int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
157 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
158 				   u64 maxlen, bool async);
159 
160 bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info);
161 int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active);
162 /* Support functions for running our sanity tests */
163 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
164 int test_add_free_space_entry(struct btrfs_block_group *cache,
165 			      u64 offset, u64 bytes, bool bitmap);
166 int test_check_exists(struct btrfs_block_group *cache, u64 offset, u64 bytes);
167 #endif
168 
169 #endif
170