]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/ext4/mballoc.h
ext4: Use an rbtree for tracking blocks freed during transaction.
[mirror_ubuntu-bionic-kernel.git] / fs / ext4 / mballoc.h
1 /*
2 * fs/ext4/mballoc.h
3 *
4 * Written by: Alex Tomas <alex@clusterfs.com>
5 *
6 */
7 #ifndef _EXT4_MBALLOC_H
8 #define _EXT4_MBALLOC_H
9
10 #include <linux/time.h>
11 #include <linux/fs.h>
12 #include <linux/namei.h>
13 #include <linux/quotaops.h>
14 #include <linux/buffer_head.h>
15 #include <linux/module.h>
16 #include <linux/swap.h>
17 #include <linux/proc_fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/seq_file.h>
20 #include <linux/version.h>
21 #include "ext4_jbd2.h"
22 #include "ext4.h"
23 #include "group.h"
24
25 /*
26 * with AGGRESSIVE_CHECK allocator runs consistency checks over
27 * structures. these checks slow things down a lot
28 */
29 #define AGGRESSIVE_CHECK__
30
31 /*
32 * with DOUBLE_CHECK defined mballoc creates persistent in-core
33 * bitmaps, maintains and uses them to check for double allocations
34 */
35 #define DOUBLE_CHECK__
36
37 /*
38 */
39 #define MB_DEBUG__
40 #ifdef MB_DEBUG
41 #define mb_debug(fmt, a...) printk(fmt, ##a)
42 #else
43 #define mb_debug(fmt, a...)
44 #endif
45
46 /*
47 * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
48 * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
49 */
50 #define EXT4_MB_HISTORY
51 #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
52 #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
53 #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
54 #define EXT4_MB_HISTORY_FREE 8 /* free */
55
56 #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
57 EXT4_MB_HISTORY_PREALLOC)
58
59 /*
60 * How long mballoc can look for a best extent (in found extents)
61 */
62 #define MB_DEFAULT_MAX_TO_SCAN 200
63
64 /*
65 * How long mballoc must look for a best extent
66 */
67 #define MB_DEFAULT_MIN_TO_SCAN 10
68
69 /*
70 * How many groups mballoc will scan looking for the best chunk
71 */
72 #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
73
74 /*
75 * with 'ext4_mb_stats' allocator will collect stats that will be
76 * shown at umount. The collecting costs though!
77 */
78 #define MB_DEFAULT_STATS 1
79
80 /*
81 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
82 * by the stream allocator, which purpose is to pack requests
83 * as close each to other as possible to produce smooth I/O traffic
84 * We use locality group prealloc space for stream request.
85 * We can tune the same via /proc/fs/ext4/<parition>/stream_req
86 */
87 #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
88
89 /*
90 * for which requests use 2^N search using buddies
91 */
92 #define MB_DEFAULT_ORDER2_REQS 2
93
94 /*
95 * default group prealloc size 512 blocks
96 */
97 #define MB_DEFAULT_GROUP_PREALLOC 512
98
99 static struct kmem_cache *ext4_pspace_cachep;
100 static struct kmem_cache *ext4_ac_cachep;
101 static struct kmem_cache *ext4_free_ext_cachep;
102
103 struct ext4_free_data {
104 /* this links the free block information from group_info */
105 struct rb_node node;
106
107 /* this links the free block information from ext4_sb_info */
108 struct list_head list;
109
110 /* group which free block extent belongs */
111 ext4_group_t group;
112
113 /* free block extent */
114 ext4_grpblk_t start_blk;
115 ext4_grpblk_t count;
116
117 /* transaction which freed this extent */
118 tid_t t_tid;
119 };
120
121 struct ext4_group_info {
122 unsigned long bb_state;
123 struct rb_root bb_free_root;
124 unsigned short bb_first_free;
125 unsigned short bb_free;
126 unsigned short bb_fragments;
127 struct list_head bb_prealloc_list;
128 #ifdef DOUBLE_CHECK
129 void *bb_bitmap;
130 #endif
131 unsigned short bb_counters[];
132 };
133
134 #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
135 #define EXT4_GROUP_INFO_LOCKED_BIT 1
136
137 #define EXT4_MB_GRP_NEED_INIT(grp) \
138 (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
139
140
141 struct ext4_prealloc_space {
142 struct list_head pa_inode_list;
143 struct list_head pa_group_list;
144 union {
145 struct list_head pa_tmp_list;
146 struct rcu_head pa_rcu;
147 } u;
148 spinlock_t pa_lock;
149 atomic_t pa_count;
150 unsigned pa_deleted;
151 ext4_fsblk_t pa_pstart; /* phys. block */
152 ext4_lblk_t pa_lstart; /* log. block */
153 unsigned short pa_len; /* len of preallocated chunk */
154 unsigned short pa_free; /* how many blocks are free */
155 unsigned short pa_linear; /* consumed in one direction
156 * strictly, for grp prealloc */
157 spinlock_t *pa_obj_lock;
158 struct inode *pa_inode; /* hack, for history only */
159 };
160
161
162 struct ext4_free_extent {
163 ext4_lblk_t fe_logical;
164 ext4_grpblk_t fe_start;
165 ext4_group_t fe_group;
166 int fe_len;
167 };
168
169 /*
170 * Locality group:
171 * we try to group all related changes together
172 * so that writeback can flush/allocate them together as well
173 * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
174 * (512). We store prealloc space into the hash based on the pa_free blocks
175 * order value.ie, fls(pa_free)-1;
176 */
177 #define PREALLOC_TB_SIZE 10
178 struct ext4_locality_group {
179 /* for allocator */
180 /* to serialize allocates */
181 struct mutex lg_mutex;
182 /* list of preallocations */
183 struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
184 spinlock_t lg_prealloc_lock;
185 };
186
187 struct ext4_allocation_context {
188 struct inode *ac_inode;
189 struct super_block *ac_sb;
190
191 /* original request */
192 struct ext4_free_extent ac_o_ex;
193
194 /* goal request (after normalization) */
195 struct ext4_free_extent ac_g_ex;
196
197 /* the best found extent */
198 struct ext4_free_extent ac_b_ex;
199
200 /* copy of the bext found extent taken before preallocation efforts */
201 struct ext4_free_extent ac_f_ex;
202
203 /* number of iterations done. we have to track to limit searching */
204 unsigned long ac_ex_scanned;
205 __u16 ac_groups_scanned;
206 __u16 ac_found;
207 __u16 ac_tail;
208 __u16 ac_buddy;
209 __u16 ac_flags; /* allocation hints */
210 __u8 ac_status;
211 __u8 ac_criteria;
212 __u8 ac_repeats;
213 __u8 ac_2order; /* if request is to allocate 2^N blocks and
214 * N > 0, the field stores N, otherwise 0 */
215 __u8 ac_op; /* operation, for history only */
216 struct page *ac_bitmap_page;
217 struct page *ac_buddy_page;
218 struct ext4_prealloc_space *ac_pa;
219 struct ext4_locality_group *ac_lg;
220 };
221
222 #define AC_STATUS_CONTINUE 1
223 #define AC_STATUS_FOUND 2
224 #define AC_STATUS_BREAK 3
225
226 struct ext4_mb_history {
227 struct ext4_free_extent orig; /* orig allocation */
228 struct ext4_free_extent goal; /* goal allocation */
229 struct ext4_free_extent result; /* result allocation */
230 unsigned pid;
231 unsigned ino;
232 __u16 found; /* how many extents have been found */
233 __u16 groups; /* how many groups have been scanned */
234 __u16 tail; /* what tail broke some buddy */
235 __u16 buddy; /* buddy the tail ^^^ broke */
236 __u16 flags;
237 __u8 cr:3; /* which phase the result extent was found at */
238 __u8 op:4;
239 __u8 merged:1;
240 };
241
242 struct ext4_buddy {
243 struct page *bd_buddy_page;
244 void *bd_buddy;
245 struct page *bd_bitmap_page;
246 void *bd_bitmap;
247 struct ext4_group_info *bd_info;
248 struct super_block *bd_sb;
249 __u16 bd_blkbits;
250 ext4_group_t bd_group;
251 };
252 #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
253 #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
254
255 #ifndef EXT4_MB_HISTORY
256 static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
257 {
258 return;
259 }
260 #else
261 static void ext4_mb_store_history(struct ext4_allocation_context *ac);
262 #endif
263
264 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
265
266 struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
267
268 static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
269 ext4_group_t group);
270 static void ext4_mb_poll_new_transaction(struct super_block *, handle_t *);
271 static void ext4_mb_free_committed_blocks(struct super_block *);
272 static void ext4_mb_return_to_preallocation(struct inode *inode,
273 struct ext4_buddy *e4b, sector_t block,
274 int count);
275 static void ext4_mb_put_pa(struct ext4_allocation_context *,
276 struct super_block *, struct ext4_prealloc_space *pa);
277 static int ext4_mb_init_per_dev_proc(struct super_block *sb);
278 static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
279
280
281 static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
282 {
283 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
284
285 bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
286 }
287
288 static inline void ext4_unlock_group(struct super_block *sb,
289 ext4_group_t group)
290 {
291 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
292
293 bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
294 }
295
296 static inline int ext4_is_group_locked(struct super_block *sb,
297 ext4_group_t group)
298 {
299 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
300
301 return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT,
302 &(grinfo->bb_state));
303 }
304
305 static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
306 struct ext4_free_extent *fex)
307 {
308 ext4_fsblk_t block;
309
310 block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
311 + fex->fe_start
312 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
313 return block;
314 }
315 #endif