]>
Commit | Line | Data |
---|---|---|
f5166768 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c9de560d AT |
2 | /* |
3 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com | |
4 | * Written by Alex Tomas <alex@clusterfs.com> | |
c9de560d AT |
5 | */ |
6 | ||
7 | ||
8 | /* | |
9 | * mballoc.c contains the multiblocks allocation routines | |
10 | */ | |
11 | ||
18aadd47 | 12 | #include "ext4_jbd2.h" |
8f6e39a7 | 13 | #include "mballoc.h" |
28623c2f | 14 | #include <linux/log2.h> |
a0b30c12 | 15 | #include <linux/module.h> |
5a0e3ad6 | 16 | #include <linux/slab.h> |
66114cad | 17 | #include <linux/backing-dev.h> |
9bffad1e TT |
18 | #include <trace/events/ext4.h> |
19 | ||
a0b30c12 TT |
20 | #ifdef CONFIG_EXT4_DEBUG |
21 | ushort ext4_mballoc_debug __read_mostly; | |
22 | ||
23 | module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644); | |
24 | MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc"); | |
25 | #endif | |
26 | ||
c9de560d AT |
27 | /* |
28 | * MUSTDO: | |
29 | * - test ext4_ext_search_left() and ext4_ext_search_right() | |
30 | * - search for metadata in few groups | |
31 | * | |
32 | * TODO v4: | |
33 | * - normalization should take into account whether file is still open | |
34 | * - discard preallocations if no free space left (policy?) | |
35 | * - don't normalize tails | |
36 | * - quota | |
37 | * - reservation for superuser | |
38 | * | |
39 | * TODO v3: | |
40 | * - bitmap read-ahead (proposed by Oleg Drokin aka green) | |
41 | * - track min/max extents in each group for better group selection | |
42 | * - mb_mark_used() may allocate chunk right after splitting buddy | |
43 | * - tree of groups sorted by number of free blocks | |
44 | * - error handling | |
45 | */ | |
46 | ||
47 | /* | |
48 | * The allocation request involve request for multiple number of blocks | |
49 | * near to the goal(block) value specified. | |
50 | * | |
b713a5ec TT |
51 | * During initialization phase of the allocator we decide to use the |
52 | * group preallocation or inode preallocation depending on the size of | |
53 | * the file. The size of the file could be the resulting file size we | |
54 | * would have after allocation, or the current file size, which ever | |
55 | * is larger. If the size is less than sbi->s_mb_stream_request we | |
56 | * select to use the group preallocation. The default value of | |
57 | * s_mb_stream_request is 16 blocks. This can also be tuned via | |
58 | * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in | |
59 | * terms of number of blocks. | |
c9de560d AT |
60 | * |
61 | * The main motivation for having small file use group preallocation is to | |
b713a5ec | 62 | * ensure that we have small files closer together on the disk. |
c9de560d | 63 | * |
b713a5ec TT |
64 | * First stage the allocator looks at the inode prealloc list, |
65 | * ext4_inode_info->i_prealloc_list, which contains list of prealloc | |
66 | * spaces for this particular inode. The inode prealloc space is | |
67 | * represented as: | |
c9de560d AT |
68 | * |
69 | * pa_lstart -> the logical start block for this prealloc space | |
70 | * pa_pstart -> the physical start block for this prealloc space | |
53accfa9 TT |
71 | * pa_len -> length for this prealloc space (in clusters) |
72 | * pa_free -> free space available in this prealloc space (in clusters) | |
c9de560d AT |
73 | * |
74 | * The inode preallocation space is used looking at the _logical_ start | |
75 | * block. If only the logical file block falls within the range of prealloc | |
caaf7a29 TM |
76 | * space we will consume the particular prealloc space. This makes sure that |
77 | * we have contiguous physical blocks representing the file blocks | |
c9de560d AT |
78 | * |
79 | * The important thing to be noted in case of inode prealloc space is that | |
80 | * we don't modify the values associated to inode prealloc space except | |
81 | * pa_free. | |
82 | * | |
83 | * If we are not able to find blocks in the inode prealloc space and if we | |
84 | * have the group allocation flag set then we look at the locality group | |
caaf7a29 | 85 | * prealloc space. These are per CPU prealloc list represented as |
c9de560d AT |
86 | * |
87 | * ext4_sb_info.s_locality_groups[smp_processor_id()] | |
88 | * | |
89 | * The reason for having a per cpu locality group is to reduce the contention | |
90 | * between CPUs. It is possible to get scheduled at this point. | |
91 | * | |
92 | * The locality group prealloc space is used looking at whether we have | |
25985edc | 93 | * enough free space (pa_free) within the prealloc space. |
c9de560d AT |
94 | * |
95 | * If we can't allocate blocks via inode prealloc or/and locality group | |
96 | * prealloc then we look at the buddy cache. The buddy cache is represented | |
97 | * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets | |
98 | * mapped to the buddy and bitmap information regarding different | |
99 | * groups. The buddy information is attached to buddy cache inode so that | |
100 | * we can access them through the page cache. The information regarding | |
101 | * each group is loaded via ext4_mb_load_buddy. The information involve | |
102 | * block bitmap and buddy information. The information are stored in the | |
103 | * inode as: | |
104 | * | |
105 | * { page } | |
c3a326a6 | 106 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
c9de560d AT |
107 | * |
108 | * | |
109 | * one block each for bitmap and buddy information. So for each group we | |
ea1754a0 | 110 | * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE / |
c9de560d AT |
111 | * blocksize) blocks. So it can have information regarding groups_per_page |
112 | * which is blocks_per_page/2 | |
113 | * | |
114 | * The buddy cache inode is not stored on disk. The inode is thrown | |
115 | * away when the filesystem is unmounted. | |
116 | * | |
117 | * We look for count number of blocks in the buddy cache. If we were able | |
118 | * to locate that many free blocks we return with additional information | |
119 | * regarding rest of the contiguous physical block available | |
120 | * | |
121 | * Before allocating blocks via buddy cache we normalize the request | |
122 | * blocks. This ensure we ask for more blocks that we needed. The extra | |
123 | * blocks that we get after allocation is added to the respective prealloc | |
124 | * list. In case of inode preallocation we follow a list of heuristics | |
125 | * based on file size. This can be found in ext4_mb_normalize_request. If | |
126 | * we are doing a group prealloc we try to normalize the request to | |
27baebb8 TT |
127 | * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is |
128 | * dependent on the cluster size; for non-bigalloc file systems, it is | |
c9de560d | 129 | * 512 blocks. This can be tuned via |
d7a1fee1 | 130 | * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in |
c9de560d AT |
131 | * terms of number of blocks. If we have mounted the file system with -O |
132 | * stripe=<value> option the group prealloc request is normalized to the | |
d7a1fee1 DE |
133 | * the smallest multiple of the stripe value (sbi->s_stripe) which is |
134 | * greater than the default mb_group_prealloc. | |
c9de560d | 135 | * |
d7a1fee1 | 136 | * The regular allocator (using the buddy cache) supports a few tunables. |
c9de560d | 137 | * |
b713a5ec TT |
138 | * /sys/fs/ext4/<partition>/mb_min_to_scan |
139 | * /sys/fs/ext4/<partition>/mb_max_to_scan | |
140 | * /sys/fs/ext4/<partition>/mb_order2_req | |
c9de560d | 141 | * |
b713a5ec | 142 | * The regular allocator uses buddy scan only if the request len is power of |
c9de560d AT |
143 | * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The |
144 | * value of s_mb_order2_reqs can be tuned via | |
b713a5ec | 145 | * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to |
af901ca1 | 146 | * stripe size (sbi->s_stripe), we try to search for contiguous block in |
b713a5ec TT |
147 | * stripe size. This should result in better allocation on RAID setups. If |
148 | * not, we search in the specific group using bitmap for best extents. The | |
149 | * tunable min_to_scan and max_to_scan control the behaviour here. | |
c9de560d | 150 | * min_to_scan indicate how long the mballoc __must__ look for a best |
b713a5ec | 151 | * extent and max_to_scan indicates how long the mballoc __can__ look for a |
c9de560d AT |
152 | * best extent in the found extents. Searching for the blocks starts with |
153 | * the group specified as the goal value in allocation context via | |
154 | * ac_g_ex. Each group is first checked based on the criteria whether it | |
caaf7a29 | 155 | * can be used for allocation. ext4_mb_good_group explains how the groups are |
c9de560d AT |
156 | * checked. |
157 | * | |
158 | * Both the prealloc space are getting populated as above. So for the first | |
159 | * request we will hit the buddy cache which will result in this prealloc | |
160 | * space getting filled. The prealloc space is then later used for the | |
161 | * subsequent request. | |
162 | */ | |
163 | ||
164 | /* | |
165 | * mballoc operates on the following data: | |
166 | * - on-disk bitmap | |
167 | * - in-core buddy (actually includes buddy and bitmap) | |
168 | * - preallocation descriptors (PAs) | |
169 | * | |
170 | * there are two types of preallocations: | |
171 | * - inode | |
172 | * assiged to specific inode and can be used for this inode only. | |
173 | * it describes part of inode's space preallocated to specific | |
174 | * physical blocks. any block from that preallocated can be used | |
175 | * independent. the descriptor just tracks number of blocks left | |
176 | * unused. so, before taking some block from descriptor, one must | |
177 | * make sure corresponded logical block isn't allocated yet. this | |
178 | * also means that freeing any block within descriptor's range | |
179 | * must discard all preallocated blocks. | |
180 | * - locality group | |
181 | * assigned to specific locality group which does not translate to | |
182 | * permanent set of inodes: inode can join and leave group. space | |
183 | * from this type of preallocation can be used for any inode. thus | |
184 | * it's consumed from the beginning to the end. | |
185 | * | |
186 | * relation between them can be expressed as: | |
187 | * in-core buddy = on-disk bitmap + preallocation descriptors | |
188 | * | |
189 | * this mean blocks mballoc considers used are: | |
190 | * - allocated blocks (persistent) | |
191 | * - preallocated blocks (non-persistent) | |
192 | * | |
193 | * consistency in mballoc world means that at any time a block is either | |
194 | * free or used in ALL structures. notice: "any time" should not be read | |
195 | * literally -- time is discrete and delimited by locks. | |
196 | * | |
197 | * to keep it simple, we don't use block numbers, instead we count number of | |
198 | * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA. | |
199 | * | |
200 | * all operations can be expressed as: | |
201 | * - init buddy: buddy = on-disk + PAs | |
202 | * - new PA: buddy += N; PA = N | |
203 | * - use inode PA: on-disk += N; PA -= N | |
204 | * - discard inode PA buddy -= on-disk - PA; PA = 0 | |
205 | * - use locality group PA on-disk += N; PA -= N | |
206 | * - discard locality group PA buddy -= PA; PA = 0 | |
207 | * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap | |
208 | * is used in real operation because we can't know actual used | |
209 | * bits from PA, only from on-disk bitmap | |
210 | * | |
211 | * if we follow this strict logic, then all operations above should be atomic. | |
212 | * given some of them can block, we'd have to use something like semaphores | |
213 | * killing performance on high-end SMP hardware. let's try to relax it using | |
214 | * the following knowledge: | |
215 | * 1) if buddy is referenced, it's already initialized | |
216 | * 2) while block is used in buddy and the buddy is referenced, | |
217 | * nobody can re-allocate that block | |
218 | * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has | |
219 | * bit set and PA claims same block, it's OK. IOW, one can set bit in | |
220 | * on-disk bitmap if buddy has same bit set or/and PA covers corresponded | |
221 | * block | |
222 | * | |
223 | * so, now we're building a concurrency table: | |
224 | * - init buddy vs. | |
225 | * - new PA | |
226 | * blocks for PA are allocated in the buddy, buddy must be referenced | |
227 | * until PA is linked to allocation group to avoid concurrent buddy init | |
228 | * - use inode PA | |
229 | * we need to make sure that either on-disk bitmap or PA has uptodate data | |
230 | * given (3) we care that PA-=N operation doesn't interfere with init | |
231 | * - discard inode PA | |
232 | * the simplest way would be to have buddy initialized by the discard | |
233 | * - use locality group PA | |
234 | * again PA-=N must be serialized with init | |
235 | * - discard locality group PA | |
236 | * the simplest way would be to have buddy initialized by the discard | |
237 | * - new PA vs. | |
238 | * - use inode PA | |
239 | * i_data_sem serializes them | |
240 | * - discard inode PA | |
241 | * discard process must wait until PA isn't used by another process | |
242 | * - use locality group PA | |
243 | * some mutex should serialize them | |
244 | * - discard locality group PA | |
245 | * discard process must wait until PA isn't used by another process | |
246 | * - use inode PA | |
247 | * - use inode PA | |
248 | * i_data_sem or another mutex should serializes them | |
249 | * - discard inode PA | |
250 | * discard process must wait until PA isn't used by another process | |
251 | * - use locality group PA | |
252 | * nothing wrong here -- they're different PAs covering different blocks | |
253 | * - discard locality group PA | |
254 | * discard process must wait until PA isn't used by another process | |
255 | * | |
256 | * now we're ready to make few consequences: | |
257 | * - PA is referenced and while it is no discard is possible | |
258 | * - PA is referenced until block isn't marked in on-disk bitmap | |
259 | * - PA changes only after on-disk bitmap | |
260 | * - discard must not compete with init. either init is done before | |
261 | * any discard or they're serialized somehow | |
262 | * - buddy init as sum of on-disk bitmap and PAs is done atomically | |
263 | * | |
264 | * a special case when we've used PA to emptiness. no need to modify buddy | |
265 | * in this case, but we should care about concurrent init | |
266 | * | |
267 | */ | |
268 | ||
269 | /* | |
270 | * Logic in few words: | |
271 | * | |
272 | * - allocation: | |
273 | * load group | |
274 | * find blocks | |
275 | * mark bits in on-disk bitmap | |
276 | * release group | |
277 | * | |
278 | * - use preallocation: | |
279 | * find proper PA (per-inode or group) | |
280 | * load group | |
281 | * mark bits in on-disk bitmap | |
282 | * release group | |
283 | * release PA | |
284 | * | |
285 | * - free: | |
286 | * load group | |
287 | * mark bits in on-disk bitmap | |
288 | * release group | |
289 | * | |
290 | * - discard preallocations in group: | |
291 | * mark PAs deleted | |
292 | * move them onto local list | |
293 | * load on-disk bitmap | |
294 | * load group | |
295 | * remove PA from object (inode or locality group) | |
296 | * mark free blocks in-core | |
297 | * | |
298 | * - discard inode's preallocations: | |
299 | */ | |
300 | ||
301 | /* | |
302 | * Locking rules | |
303 | * | |
304 | * Locks: | |
305 | * - bitlock on a group (group) | |
306 | * - object (inode/locality) (object) | |
307 | * - per-pa lock (pa) | |
308 | * | |
309 | * Paths: | |
310 | * - new pa | |
311 | * object | |
312 | * group | |
313 | * | |
314 | * - find and use pa: | |
315 | * pa | |
316 | * | |
317 | * - release consumed pa: | |
318 | * pa | |
319 | * group | |
320 | * object | |
321 | * | |
322 | * - generate in-core bitmap: | |
323 | * group | |
324 | * pa | |
325 | * | |
326 | * - discard all for given object (inode, locality group): | |
327 | * object | |
328 | * pa | |
329 | * group | |
330 | * | |
331 | * - discard all for given group: | |
332 | * group | |
333 | * pa | |
334 | * group | |
335 | * object | |
336 | * | |
337 | */ | |
c3a326a6 AK |
338 | static struct kmem_cache *ext4_pspace_cachep; |
339 | static struct kmem_cache *ext4_ac_cachep; | |
18aadd47 | 340 | static struct kmem_cache *ext4_free_data_cachep; |
fb1813f4 CW |
341 | |
342 | /* We create slab caches for groupinfo data structures based on the | |
343 | * superblock block size. There will be one per mounted filesystem for | |
344 | * each unique s_blocksize_bits */ | |
2892c15d | 345 | #define NR_GRPINFO_CACHES 8 |
fb1813f4 CW |
346 | static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES]; |
347 | ||
d6006186 | 348 | static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = { |
2892c15d ES |
349 | "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k", |
350 | "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k", | |
351 | "ext4_groupinfo_64k", "ext4_groupinfo_128k" | |
352 | }; | |
353 | ||
c3a326a6 AK |
354 | static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
355 | ext4_group_t group); | |
7a2fcbf7 AK |
356 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
357 | ext4_group_t group); | |
c3a326a6 | 358 | |
ffad0a44 AK |
359 | static inline void *mb_correct_addr_and_bit(int *bit, void *addr) |
360 | { | |
c9de560d | 361 | #if BITS_PER_LONG == 64 |
ffad0a44 AK |
362 | *bit += ((unsigned long) addr & 7UL) << 3; |
363 | addr = (void *) ((unsigned long) addr & ~7UL); | |
c9de560d | 364 | #elif BITS_PER_LONG == 32 |
ffad0a44 AK |
365 | *bit += ((unsigned long) addr & 3UL) << 3; |
366 | addr = (void *) ((unsigned long) addr & ~3UL); | |
c9de560d AT |
367 | #else |
368 | #error "how many bits you are?!" | |
369 | #endif | |
ffad0a44 AK |
370 | return addr; |
371 | } | |
c9de560d AT |
372 | |
373 | static inline int mb_test_bit(int bit, void *addr) | |
374 | { | |
375 | /* | |
376 | * ext4_test_bit on architecture like powerpc | |
377 | * needs unsigned long aligned address | |
378 | */ | |
ffad0a44 | 379 | addr = mb_correct_addr_and_bit(&bit, addr); |
c9de560d AT |
380 | return ext4_test_bit(bit, addr); |
381 | } | |
382 | ||
383 | static inline void mb_set_bit(int bit, void *addr) | |
384 | { | |
ffad0a44 | 385 | addr = mb_correct_addr_and_bit(&bit, addr); |
c9de560d AT |
386 | ext4_set_bit(bit, addr); |
387 | } | |
388 | ||
c9de560d AT |
389 | static inline void mb_clear_bit(int bit, void *addr) |
390 | { | |
ffad0a44 | 391 | addr = mb_correct_addr_and_bit(&bit, addr); |
c9de560d AT |
392 | ext4_clear_bit(bit, addr); |
393 | } | |
394 | ||
eabe0444 AS |
395 | static inline int mb_test_and_clear_bit(int bit, void *addr) |
396 | { | |
397 | addr = mb_correct_addr_and_bit(&bit, addr); | |
398 | return ext4_test_and_clear_bit(bit, addr); | |
399 | } | |
400 | ||
ffad0a44 AK |
401 | static inline int mb_find_next_zero_bit(void *addr, int max, int start) |
402 | { | |
e7dfb246 | 403 | int fix = 0, ret, tmpmax; |
ffad0a44 | 404 | addr = mb_correct_addr_and_bit(&fix, addr); |
e7dfb246 | 405 | tmpmax = max + fix; |
ffad0a44 AK |
406 | start += fix; |
407 | ||
e7dfb246 AK |
408 | ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix; |
409 | if (ret > max) | |
410 | return max; | |
411 | return ret; | |
ffad0a44 AK |
412 | } |
413 | ||
414 | static inline int mb_find_next_bit(void *addr, int max, int start) | |
415 | { | |
e7dfb246 | 416 | int fix = 0, ret, tmpmax; |
ffad0a44 | 417 | addr = mb_correct_addr_and_bit(&fix, addr); |
e7dfb246 | 418 | tmpmax = max + fix; |
ffad0a44 AK |
419 | start += fix; |
420 | ||
e7dfb246 AK |
421 | ret = ext4_find_next_bit(addr, tmpmax, start) - fix; |
422 | if (ret > max) | |
423 | return max; | |
424 | return ret; | |
ffad0a44 AK |
425 | } |
426 | ||
c9de560d AT |
427 | static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max) |
428 | { | |
429 | char *bb; | |
430 | ||
c5e8f3f3 | 431 | BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); |
c9de560d AT |
432 | BUG_ON(max == NULL); |
433 | ||
434 | if (order > e4b->bd_blkbits + 1) { | |
435 | *max = 0; | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | /* at order 0 we see each particular block */ | |
84b775a3 CL |
440 | if (order == 0) { |
441 | *max = 1 << (e4b->bd_blkbits + 3); | |
c5e8f3f3 | 442 | return e4b->bd_bitmap; |
84b775a3 | 443 | } |
c9de560d | 444 | |
c5e8f3f3 | 445 | bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order]; |
c9de560d AT |
446 | *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order]; |
447 | ||
448 | return bb; | |
449 | } | |
450 | ||
451 | #ifdef DOUBLE_CHECK | |
452 | static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b, | |
453 | int first, int count) | |
454 | { | |
455 | int i; | |
456 | struct super_block *sb = e4b->bd_sb; | |
457 | ||
458 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) | |
459 | return; | |
bc8e6740 | 460 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
c9de560d AT |
461 | for (i = 0; i < count; i++) { |
462 | if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) { | |
463 | ext4_fsblk_t blocknr; | |
5661bd68 AM |
464 | |
465 | blocknr = ext4_group_first_block_no(sb, e4b->bd_group); | |
53accfa9 | 466 | blocknr += EXT4_C2B(EXT4_SB(sb), first + i); |
5d1b1b3f | 467 | ext4_grp_locked_error(sb, e4b->bd_group, |
e29136f8 TT |
468 | inode ? inode->i_ino : 0, |
469 | blocknr, | |
470 | "freeing block already freed " | |
471 | "(bit %u)", | |
472 | first + i); | |
736dedbb WS |
473 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
474 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); | |
c9de560d AT |
475 | } |
476 | mb_clear_bit(first + i, e4b->bd_info->bb_bitmap); | |
477 | } | |
478 | } | |
479 | ||
480 | static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count) | |
481 | { | |
482 | int i; | |
483 | ||
484 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) | |
485 | return; | |
bc8e6740 | 486 | assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); |
c9de560d AT |
487 | for (i = 0; i < count; i++) { |
488 | BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap)); | |
489 | mb_set_bit(first + i, e4b->bd_info->bb_bitmap); | |
490 | } | |
491 | } | |
492 | ||
493 | static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) | |
494 | { | |
495 | if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) { | |
496 | unsigned char *b1, *b2; | |
497 | int i; | |
498 | b1 = (unsigned char *) e4b->bd_info->bb_bitmap; | |
499 | b2 = (unsigned char *) bitmap; | |
500 | for (i = 0; i < e4b->bd_sb->s_blocksize; i++) { | |
501 | if (b1[i] != b2[i]) { | |
9d8b9ec4 TT |
502 | ext4_msg(e4b->bd_sb, KERN_ERR, |
503 | "corruption in group %u " | |
504 | "at byte %u(%u): %x in copy != %x " | |
505 | "on disk/prealloc", | |
506 | e4b->bd_group, i, i * 8, b1[i], b2[i]); | |
c9de560d AT |
507 | BUG(); |
508 | } | |
509 | } | |
510 | } | |
511 | } | |
512 | ||
513 | #else | |
514 | static inline void mb_free_blocks_double(struct inode *inode, | |
515 | struct ext4_buddy *e4b, int first, int count) | |
516 | { | |
517 | return; | |
518 | } | |
519 | static inline void mb_mark_used_double(struct ext4_buddy *e4b, | |
520 | int first, int count) | |
521 | { | |
522 | return; | |
523 | } | |
524 | static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) | |
525 | { | |
526 | return; | |
527 | } | |
528 | #endif | |
529 | ||
530 | #ifdef AGGRESSIVE_CHECK | |
531 | ||
532 | #define MB_CHECK_ASSERT(assert) \ | |
533 | do { \ | |
534 | if (!(assert)) { \ | |
535 | printk(KERN_EMERG \ | |
536 | "Assertion failure in %s() at %s:%d: \"%s\"\n", \ | |
537 | function, file, line, # assert); \ | |
538 | BUG(); \ | |
539 | } \ | |
540 | } while (0) | |
541 | ||
542 | static int __mb_check_buddy(struct ext4_buddy *e4b, char *file, | |
543 | const char *function, int line) | |
544 | { | |
545 | struct super_block *sb = e4b->bd_sb; | |
546 | int order = e4b->bd_blkbits + 1; | |
547 | int max; | |
548 | int max2; | |
549 | int i; | |
550 | int j; | |
551 | int k; | |
552 | int count; | |
553 | struct ext4_group_info *grp; | |
554 | int fragments = 0; | |
555 | int fstart; | |
556 | struct list_head *cur; | |
557 | void *buddy; | |
558 | void *buddy2; | |
559 | ||
c9de560d AT |
560 | { |
561 | static int mb_check_counter; | |
562 | if (mb_check_counter++ % 100 != 0) | |
563 | return 0; | |
564 | } | |
565 | ||
566 | while (order > 1) { | |
567 | buddy = mb_find_buddy(e4b, order, &max); | |
568 | MB_CHECK_ASSERT(buddy); | |
569 | buddy2 = mb_find_buddy(e4b, order - 1, &max2); | |
570 | MB_CHECK_ASSERT(buddy2); | |
571 | MB_CHECK_ASSERT(buddy != buddy2); | |
572 | MB_CHECK_ASSERT(max * 2 == max2); | |
573 | ||
574 | count = 0; | |
575 | for (i = 0; i < max; i++) { | |
576 | ||
577 | if (mb_test_bit(i, buddy)) { | |
578 | /* only single bit in buddy2 may be 1 */ | |
579 | if (!mb_test_bit(i << 1, buddy2)) { | |
580 | MB_CHECK_ASSERT( | |
581 | mb_test_bit((i<<1)+1, buddy2)); | |
582 | } else if (!mb_test_bit((i << 1) + 1, buddy2)) { | |
583 | MB_CHECK_ASSERT( | |
584 | mb_test_bit(i << 1, buddy2)); | |
585 | } | |
586 | continue; | |
587 | } | |
588 | ||
0a10da73 | 589 | /* both bits in buddy2 must be 1 */ |
c9de560d AT |
590 | MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2)); |
591 | MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2)); | |
592 | ||
593 | for (j = 0; j < (1 << order); j++) { | |
594 | k = (i * (1 << order)) + j; | |
595 | MB_CHECK_ASSERT( | |
c5e8f3f3 | 596 | !mb_test_bit(k, e4b->bd_bitmap)); |
c9de560d AT |
597 | } |
598 | count++; | |
599 | } | |
600 | MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count); | |
601 | order--; | |
602 | } | |
603 | ||
604 | fstart = -1; | |
605 | buddy = mb_find_buddy(e4b, 0, &max); | |
606 | for (i = 0; i < max; i++) { | |
607 | if (!mb_test_bit(i, buddy)) { | |
608 | MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free); | |
609 | if (fstart == -1) { | |
610 | fragments++; | |
611 | fstart = i; | |
612 | } | |
613 | continue; | |
614 | } | |
615 | fstart = -1; | |
616 | /* check used bits only */ | |
617 | for (j = 0; j < e4b->bd_blkbits + 1; j++) { | |
618 | buddy2 = mb_find_buddy(e4b, j, &max2); | |
619 | k = i >> j; | |
620 | MB_CHECK_ASSERT(k < max2); | |
621 | MB_CHECK_ASSERT(mb_test_bit(k, buddy2)); | |
622 | } | |
623 | } | |
624 | MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info)); | |
625 | MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments); | |
626 | ||
627 | grp = ext4_get_group_info(sb, e4b->bd_group); | |
c9de560d AT |
628 | list_for_each(cur, &grp->bb_prealloc_list) { |
629 | ext4_group_t groupnr; | |
630 | struct ext4_prealloc_space *pa; | |
60bd63d1 SR |
631 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
632 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k); | |
c9de560d | 633 | MB_CHECK_ASSERT(groupnr == e4b->bd_group); |
60bd63d1 | 634 | for (i = 0; i < pa->pa_len; i++) |
c9de560d AT |
635 | MB_CHECK_ASSERT(mb_test_bit(k + i, buddy)); |
636 | } | |
637 | return 0; | |
638 | } | |
639 | #undef MB_CHECK_ASSERT | |
640 | #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \ | |
46e665e9 | 641 | __FILE__, __func__, __LINE__) |
c9de560d AT |
642 | #else |
643 | #define mb_check_buddy(e4b) | |
644 | #endif | |
645 | ||
7c786059 CL |
646 | /* |
647 | * Divide blocks started from @first with length @len into | |
648 | * smaller chunks with power of 2 blocks. | |
649 | * Clear the bits in bitmap which the blocks of the chunk(s) covered, | |
650 | * then increase bb_counters[] for corresponded chunk size. | |
651 | */ | |
c9de560d | 652 | static void ext4_mb_mark_free_simple(struct super_block *sb, |
a36b4498 | 653 | void *buddy, ext4_grpblk_t first, ext4_grpblk_t len, |
c9de560d AT |
654 | struct ext4_group_info *grp) |
655 | { | |
656 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
a36b4498 ES |
657 | ext4_grpblk_t min; |
658 | ext4_grpblk_t max; | |
659 | ext4_grpblk_t chunk; | |
69e43e8c | 660 | unsigned int border; |
c9de560d | 661 | |
7137d7a4 | 662 | BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb)); |
c9de560d AT |
663 | |
664 | border = 2 << sb->s_blocksize_bits; | |
665 | ||
666 | while (len > 0) { | |
667 | /* find how many blocks can be covered since this position */ | |
668 | max = ffs(first | border) - 1; | |
669 | ||
670 | /* find how many blocks of power 2 we need to mark */ | |
671 | min = fls(len) - 1; | |
672 | ||
673 | if (max < min) | |
674 | min = max; | |
675 | chunk = 1 << min; | |
676 | ||
677 | /* mark multiblock chunks only */ | |
678 | grp->bb_counters[min]++; | |
679 | if (min > 0) | |
680 | mb_clear_bit(first >> min, | |
681 | buddy + sbi->s_mb_offsets[min]); | |
682 | ||
683 | len -= chunk; | |
684 | first += chunk; | |
685 | } | |
686 | } | |
687 | ||
8a57d9d6 CW |
688 | /* |
689 | * Cache the order of the largest free extent we have available in this block | |
690 | * group. | |
691 | */ | |
692 | static void | |
693 | mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp) | |
694 | { | |
695 | int i; | |
696 | int bits; | |
697 | ||
698 | grp->bb_largest_free_order = -1; /* uninit */ | |
699 | ||
700 | bits = sb->s_blocksize_bits + 1; | |
701 | for (i = bits; i >= 0; i--) { | |
702 | if (grp->bb_counters[i] > 0) { | |
703 | grp->bb_largest_free_order = i; | |
704 | break; | |
705 | } | |
706 | } | |
707 | } | |
708 | ||
089ceecc ES |
709 | static noinline_for_stack |
710 | void ext4_mb_generate_buddy(struct super_block *sb, | |
c9de560d AT |
711 | void *buddy, void *bitmap, ext4_group_t group) |
712 | { | |
713 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
e43bb4e6 | 714 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
7137d7a4 | 715 | ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb); |
a36b4498 ES |
716 | ext4_grpblk_t i = 0; |
717 | ext4_grpblk_t first; | |
718 | ext4_grpblk_t len; | |
c9de560d AT |
719 | unsigned free = 0; |
720 | unsigned fragments = 0; | |
721 | unsigned long long period = get_cycles(); | |
722 | ||
723 | /* initialize buddy from bitmap which is aggregation | |
724 | * of on-disk bitmap and preallocations */ | |
ffad0a44 | 725 | i = mb_find_next_zero_bit(bitmap, max, 0); |
c9de560d AT |
726 | grp->bb_first_free = i; |
727 | while (i < max) { | |
728 | fragments++; | |
729 | first = i; | |
ffad0a44 | 730 | i = mb_find_next_bit(bitmap, max, i); |
c9de560d AT |
731 | len = i - first; |
732 | free += len; | |
733 | if (len > 1) | |
734 | ext4_mb_mark_free_simple(sb, buddy, first, len, grp); | |
735 | else | |
736 | grp->bb_counters[0]++; | |
737 | if (i < max) | |
ffad0a44 | 738 | i = mb_find_next_zero_bit(bitmap, max, i); |
c9de560d AT |
739 | } |
740 | grp->bb_fragments = fragments; | |
741 | ||
742 | if (free != grp->bb_free) { | |
e29136f8 | 743 | ext4_grp_locked_error(sb, group, 0, 0, |
94d4c066 TT |
744 | "block bitmap and bg descriptor " |
745 | "inconsistent: %u vs %u free clusters", | |
e29136f8 | 746 | free, grp->bb_free); |
e56eb659 | 747 | /* |
163a203d | 748 | * If we intend to continue, we consider group descriptor |
e56eb659 AK |
749 | * corrupt and update bb_free using bitmap value |
750 | */ | |
c9de560d | 751 | grp->bb_free = free; |
db79e6d1 WS |
752 | ext4_mark_group_bitmap_corrupted(sb, group, |
753 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); | |
c9de560d | 754 | } |
8a57d9d6 | 755 | mb_set_largest_free_order(sb, grp); |
c9de560d AT |
756 | |
757 | clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state)); | |
758 | ||
759 | period = get_cycles() - period; | |
49598e04 JP |
760 | spin_lock(&sbi->s_bal_lock); |
761 | sbi->s_mb_buddies_generated++; | |
762 | sbi->s_mb_generation_time += period; | |
763 | spin_unlock(&sbi->s_bal_lock); | |
c9de560d AT |
764 | } |
765 | ||
eabe0444 AS |
766 | static void mb_regenerate_buddy(struct ext4_buddy *e4b) |
767 | { | |
768 | int count; | |
769 | int order = 1; | |
770 | void *buddy; | |
771 | ||
772 | while ((buddy = mb_find_buddy(e4b, order++, &count))) { | |
773 | ext4_set_bits(buddy, 0, count); | |
774 | } | |
775 | e4b->bd_info->bb_fragments = 0; | |
776 | memset(e4b->bd_info->bb_counters, 0, | |
777 | sizeof(*e4b->bd_info->bb_counters) * | |
778 | (e4b->bd_sb->s_blocksize_bits + 2)); | |
779 | ||
780 | ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy, | |
781 | e4b->bd_bitmap, e4b->bd_group); | |
782 | } | |
783 | ||
c9de560d AT |
784 | /* The buddy information is attached the buddy cache inode |
785 | * for convenience. The information regarding each group | |
786 | * is loaded via ext4_mb_load_buddy. The information involve | |
787 | * block bitmap and buddy information. The information are | |
788 | * stored in the inode as | |
789 | * | |
790 | * { page } | |
c3a326a6 | 791 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
c9de560d AT |
792 | * |
793 | * | |
794 | * one block each for bitmap and buddy information. | |
795 | * So for each group we take up 2 blocks. A page can | |
ea1754a0 | 796 | * contain blocks_per_page (PAGE_SIZE / blocksize) blocks. |
c9de560d AT |
797 | * So it can have information regarding groups_per_page which |
798 | * is blocks_per_page/2 | |
8a57d9d6 CW |
799 | * |
800 | * Locking note: This routine takes the block group lock of all groups | |
801 | * for this page; do not hold this lock when calling this routine! | |
c9de560d AT |
802 | */ |
803 | ||
adb7ef60 | 804 | static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp) |
c9de560d | 805 | { |
8df9675f | 806 | ext4_group_t ngroups; |
c9de560d AT |
807 | int blocksize; |
808 | int blocks_per_page; | |
809 | int groups_per_page; | |
810 | int err = 0; | |
811 | int i; | |
813e5727 | 812 | ext4_group_t first_group, group; |
c9de560d AT |
813 | int first_block; |
814 | struct super_block *sb; | |
815 | struct buffer_head *bhs; | |
fa77dcfa | 816 | struct buffer_head **bh = NULL; |
c9de560d AT |
817 | struct inode *inode; |
818 | char *data; | |
819 | char *bitmap; | |
9b8b7d35 | 820 | struct ext4_group_info *grinfo; |
c9de560d | 821 | |
6ba495e9 | 822 | mb_debug(1, "init page %lu\n", page->index); |
c9de560d AT |
823 | |
824 | inode = page->mapping->host; | |
825 | sb = inode->i_sb; | |
8df9675f | 826 | ngroups = ext4_get_groups_count(sb); |
93407472 | 827 | blocksize = i_blocksize(inode); |
09cbfeaf | 828 | blocks_per_page = PAGE_SIZE / blocksize; |
c9de560d AT |
829 | |
830 | groups_per_page = blocks_per_page >> 1; | |
831 | if (groups_per_page == 0) | |
832 | groups_per_page = 1; | |
833 | ||
834 | /* allocate buffer_heads to read bitmaps */ | |
835 | if (groups_per_page > 1) { | |
c9de560d | 836 | i = sizeof(struct buffer_head *) * groups_per_page; |
adb7ef60 | 837 | bh = kzalloc(i, gfp); |
813e5727 TT |
838 | if (bh == NULL) { |
839 | err = -ENOMEM; | |
c9de560d | 840 | goto out; |
813e5727 | 841 | } |
c9de560d AT |
842 | } else |
843 | bh = &bhs; | |
844 | ||
845 | first_group = page->index * blocks_per_page / 2; | |
846 | ||
847 | /* read all groups the page covers into the cache */ | |
813e5727 TT |
848 | for (i = 0, group = first_group; i < groups_per_page; i++, group++) { |
849 | if (group >= ngroups) | |
c9de560d AT |
850 | break; |
851 | ||
813e5727 | 852 | grinfo = ext4_get_group_info(sb, group); |
9b8b7d35 AG |
853 | /* |
854 | * If page is uptodate then we came here after online resize | |
855 | * which added some new uninitialized group info structs, so | |
856 | * we must skip all initialized uptodate buddies on the page, | |
857 | * which may be currently in use by an allocating task. | |
858 | */ | |
859 | if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) { | |
860 | bh[i] = NULL; | |
861 | continue; | |
862 | } | |
9008a58e DW |
863 | bh[i] = ext4_read_block_bitmap_nowait(sb, group); |
864 | if (IS_ERR(bh[i])) { | |
865 | err = PTR_ERR(bh[i]); | |
866 | bh[i] = NULL; | |
c9de560d | 867 | goto out; |
2ccb5fb9 | 868 | } |
813e5727 | 869 | mb_debug(1, "read bitmap for group %u\n", group); |
c9de560d AT |
870 | } |
871 | ||
872 | /* wait for I/O completion */ | |
813e5727 | 873 | for (i = 0, group = first_group; i < groups_per_page; i++, group++) { |
9008a58e DW |
874 | int err2; |
875 | ||
876 | if (!bh[i]) | |
877 | continue; | |
878 | err2 = ext4_wait_block_bitmap(sb, group, bh[i]); | |
879 | if (!err) | |
880 | err = err2; | |
813e5727 | 881 | } |
c9de560d AT |
882 | |
883 | first_block = page->index * blocks_per_page; | |
884 | for (i = 0; i < blocks_per_page; i++) { | |
c9de560d | 885 | group = (first_block + i) >> 1; |
8df9675f | 886 | if (group >= ngroups) |
c9de560d AT |
887 | break; |
888 | ||
9b8b7d35 AG |
889 | if (!bh[group - first_group]) |
890 | /* skip initialized uptodate buddy */ | |
891 | continue; | |
892 | ||
bbdc322f LC |
893 | if (!buffer_verified(bh[group - first_group])) |
894 | /* Skip faulty bitmaps */ | |
895 | continue; | |
896 | err = 0; | |
897 | ||
c9de560d AT |
898 | /* |
899 | * data carry information regarding this | |
900 | * particular group in the format specified | |
901 | * above | |
902 | * | |
903 | */ | |
904 | data = page_address(page) + (i * blocksize); | |
905 | bitmap = bh[group - first_group]->b_data; | |
906 | ||
907 | /* | |
908 | * We place the buddy block and bitmap block | |
909 | * close together | |
910 | */ | |
911 | if ((first_block + i) & 1) { | |
912 | /* this is block of buddy */ | |
913 | BUG_ON(incore == NULL); | |
6ba495e9 | 914 | mb_debug(1, "put buddy for group %u in page %lu/%x\n", |
c9de560d | 915 | group, page->index, i * blocksize); |
f307333e | 916 | trace_ext4_mb_buddy_bitmap_load(sb, group); |
c9de560d AT |
917 | grinfo = ext4_get_group_info(sb, group); |
918 | grinfo->bb_fragments = 0; | |
919 | memset(grinfo->bb_counters, 0, | |
1927805e ES |
920 | sizeof(*grinfo->bb_counters) * |
921 | (sb->s_blocksize_bits+2)); | |
c9de560d AT |
922 | /* |
923 | * incore got set to the group block bitmap below | |
924 | */ | |
7a2fcbf7 | 925 | ext4_lock_group(sb, group); |
9b8b7d35 AG |
926 | /* init the buddy */ |
927 | memset(data, 0xff, blocksize); | |
c9de560d | 928 | ext4_mb_generate_buddy(sb, data, incore, group); |
7a2fcbf7 | 929 | ext4_unlock_group(sb, group); |
c9de560d AT |
930 | incore = NULL; |
931 | } else { | |
932 | /* this is block of bitmap */ | |
933 | BUG_ON(incore != NULL); | |
6ba495e9 | 934 | mb_debug(1, "put bitmap for group %u in page %lu/%x\n", |
c9de560d | 935 | group, page->index, i * blocksize); |
f307333e | 936 | trace_ext4_mb_bitmap_load(sb, group); |
c9de560d AT |
937 | |
938 | /* see comments in ext4_mb_put_pa() */ | |
939 | ext4_lock_group(sb, group); | |
940 | memcpy(data, bitmap, blocksize); | |
941 | ||
942 | /* mark all preallocated blks used in in-core bitmap */ | |
943 | ext4_mb_generate_from_pa(sb, data, group); | |
7a2fcbf7 | 944 | ext4_mb_generate_from_freelist(sb, data, group); |
c9de560d AT |
945 | ext4_unlock_group(sb, group); |
946 | ||
947 | /* set incore so that the buddy information can be | |
948 | * generated using this | |
949 | */ | |
950 | incore = data; | |
951 | } | |
952 | } | |
953 | SetPageUptodate(page); | |
954 | ||
955 | out: | |
956 | if (bh) { | |
9b8b7d35 | 957 | for (i = 0; i < groups_per_page; i++) |
c9de560d AT |
958 | brelse(bh[i]); |
959 | if (bh != &bhs) | |
960 | kfree(bh); | |
961 | } | |
962 | return err; | |
963 | } | |
964 | ||
eee4adc7 | 965 | /* |
2de8807b AG |
966 | * Lock the buddy and bitmap pages. This make sure other parallel init_group |
967 | * on the same buddy page doesn't happen whild holding the buddy page lock. | |
968 | * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap | |
969 | * are on the same page e4b->bd_buddy_page is NULL and return value is 0. | |
eee4adc7 | 970 | */ |
2de8807b | 971 | static int ext4_mb_get_buddy_page_lock(struct super_block *sb, |
adb7ef60 | 972 | ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp) |
eee4adc7 | 973 | { |
2de8807b AG |
974 | struct inode *inode = EXT4_SB(sb)->s_buddy_cache; |
975 | int block, pnum, poff; | |
eee4adc7 | 976 | int blocks_per_page; |
2de8807b AG |
977 | struct page *page; |
978 | ||
979 | e4b->bd_buddy_page = NULL; | |
980 | e4b->bd_bitmap_page = NULL; | |
eee4adc7 | 981 | |
09cbfeaf | 982 | blocks_per_page = PAGE_SIZE / sb->s_blocksize; |
eee4adc7 ES |
983 | /* |
984 | * the buddy cache inode stores the block bitmap | |
985 | * and buddy information in consecutive blocks. | |
986 | * So for each group we need two blocks. | |
987 | */ | |
988 | block = group * 2; | |
989 | pnum = block / blocks_per_page; | |
2de8807b | 990 | poff = block % blocks_per_page; |
adb7ef60 | 991 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
2de8807b | 992 | if (!page) |
c57ab39b | 993 | return -ENOMEM; |
2de8807b AG |
994 | BUG_ON(page->mapping != inode->i_mapping); |
995 | e4b->bd_bitmap_page = page; | |
996 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); | |
997 | ||
998 | if (blocks_per_page >= 2) { | |
999 | /* buddy and bitmap are on the same page */ | |
1000 | return 0; | |
eee4adc7 | 1001 | } |
2de8807b AG |
1002 | |
1003 | block++; | |
1004 | pnum = block / blocks_per_page; | |
adb7ef60 | 1005 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
2de8807b | 1006 | if (!page) |
c57ab39b | 1007 | return -ENOMEM; |
2de8807b AG |
1008 | BUG_ON(page->mapping != inode->i_mapping); |
1009 | e4b->bd_buddy_page = page; | |
1010 | return 0; | |
eee4adc7 ES |
1011 | } |
1012 | ||
2de8807b | 1013 | static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b) |
eee4adc7 | 1014 | { |
2de8807b AG |
1015 | if (e4b->bd_bitmap_page) { |
1016 | unlock_page(e4b->bd_bitmap_page); | |
09cbfeaf | 1017 | put_page(e4b->bd_bitmap_page); |
2de8807b AG |
1018 | } |
1019 | if (e4b->bd_buddy_page) { | |
1020 | unlock_page(e4b->bd_buddy_page); | |
09cbfeaf | 1021 | put_page(e4b->bd_buddy_page); |
eee4adc7 | 1022 | } |
eee4adc7 ES |
1023 | } |
1024 | ||
8a57d9d6 CW |
1025 | /* |
1026 | * Locking note: This routine calls ext4_mb_init_cache(), which takes the | |
1027 | * block group lock of all groups for this page; do not hold the BG lock when | |
1028 | * calling this routine! | |
1029 | */ | |
b6a758ec | 1030 | static noinline_for_stack |
adb7ef60 | 1031 | int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp) |
b6a758ec AK |
1032 | { |
1033 | ||
b6a758ec | 1034 | struct ext4_group_info *this_grp; |
2de8807b AG |
1035 | struct ext4_buddy e4b; |
1036 | struct page *page; | |
1037 | int ret = 0; | |
b6a758ec | 1038 | |
b10a44c3 | 1039 | might_sleep(); |
b6a758ec | 1040 | mb_debug(1, "init group %u\n", group); |
b6a758ec AK |
1041 | this_grp = ext4_get_group_info(sb, group); |
1042 | /* | |
08c3a813 AK |
1043 | * This ensures that we don't reinit the buddy cache |
1044 | * page which map to the group from which we are already | |
1045 | * allocating. If we are looking at the buddy cache we would | |
1046 | * have taken a reference using ext4_mb_load_buddy and that | |
2de8807b | 1047 | * would have pinned buddy page to page cache. |
2457aec6 MG |
1048 | * The call to ext4_mb_get_buddy_page_lock will mark the |
1049 | * page accessed. | |
b6a758ec | 1050 | */ |
adb7ef60 | 1051 | ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp); |
2de8807b | 1052 | if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) { |
b6a758ec AK |
1053 | /* |
1054 | * somebody initialized the group | |
1055 | * return without doing anything | |
1056 | */ | |
b6a758ec AK |
1057 | goto err; |
1058 | } | |
2de8807b AG |
1059 | |
1060 | page = e4b.bd_bitmap_page; | |
adb7ef60 | 1061 | ret = ext4_mb_init_cache(page, NULL, gfp); |
2de8807b AG |
1062 | if (ret) |
1063 | goto err; | |
1064 | if (!PageUptodate(page)) { | |
b6a758ec AK |
1065 | ret = -EIO; |
1066 | goto err; | |
1067 | } | |
b6a758ec | 1068 | |
2de8807b | 1069 | if (e4b.bd_buddy_page == NULL) { |
b6a758ec AK |
1070 | /* |
1071 | * If both the bitmap and buddy are in | |
1072 | * the same page we don't need to force | |
1073 | * init the buddy | |
1074 | */ | |
2de8807b AG |
1075 | ret = 0; |
1076 | goto err; | |
b6a758ec | 1077 | } |
2de8807b AG |
1078 | /* init buddy cache */ |
1079 | page = e4b.bd_buddy_page; | |
adb7ef60 | 1080 | ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp); |
2de8807b AG |
1081 | if (ret) |
1082 | goto err; | |
1083 | if (!PageUptodate(page)) { | |
b6a758ec AK |
1084 | ret = -EIO; |
1085 | goto err; | |
1086 | } | |
b6a758ec | 1087 | err: |
2de8807b | 1088 | ext4_mb_put_buddy_page_lock(&e4b); |
b6a758ec AK |
1089 | return ret; |
1090 | } | |
1091 | ||
8a57d9d6 CW |
1092 | /* |
1093 | * Locking note: This routine calls ext4_mb_init_cache(), which takes the | |
1094 | * block group lock of all groups for this page; do not hold the BG lock when | |
1095 | * calling this routine! | |
1096 | */ | |
4ddfef7b | 1097 | static noinline_for_stack int |
adb7ef60 KK |
1098 | ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group, |
1099 | struct ext4_buddy *e4b, gfp_t gfp) | |
c9de560d | 1100 | { |
c9de560d AT |
1101 | int blocks_per_page; |
1102 | int block; | |
1103 | int pnum; | |
1104 | int poff; | |
1105 | struct page *page; | |
fdf6c7a7 | 1106 | int ret; |
920313a7 AK |
1107 | struct ext4_group_info *grp; |
1108 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
1109 | struct inode *inode = sbi->s_buddy_cache; | |
c9de560d | 1110 | |
b10a44c3 | 1111 | might_sleep(); |
6ba495e9 | 1112 | mb_debug(1, "load group %u\n", group); |
c9de560d | 1113 | |
09cbfeaf | 1114 | blocks_per_page = PAGE_SIZE / sb->s_blocksize; |
920313a7 | 1115 | grp = ext4_get_group_info(sb, group); |
c9de560d AT |
1116 | |
1117 | e4b->bd_blkbits = sb->s_blocksize_bits; | |
529da704 | 1118 | e4b->bd_info = grp; |
c9de560d AT |
1119 | e4b->bd_sb = sb; |
1120 | e4b->bd_group = group; | |
1121 | e4b->bd_buddy_page = NULL; | |
1122 | e4b->bd_bitmap_page = NULL; | |
1123 | ||
f41c0750 | 1124 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
f41c0750 AK |
1125 | /* |
1126 | * we need full data about the group | |
1127 | * to make a good selection | |
1128 | */ | |
adb7ef60 | 1129 | ret = ext4_mb_init_group(sb, group, gfp); |
f41c0750 AK |
1130 | if (ret) |
1131 | return ret; | |
f41c0750 AK |
1132 | } |
1133 | ||
c9de560d AT |
1134 | /* |
1135 | * the buddy cache inode stores the block bitmap | |
1136 | * and buddy information in consecutive blocks. | |
1137 | * So for each group we need two blocks. | |
1138 | */ | |
1139 | block = group * 2; | |
1140 | pnum = block / blocks_per_page; | |
1141 | poff = block % blocks_per_page; | |
1142 | ||
1143 | /* we could use find_or_create_page(), but it locks page | |
1144 | * what we'd like to avoid in fast path ... */ | |
2457aec6 | 1145 | page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED); |
c9de560d AT |
1146 | if (page == NULL || !PageUptodate(page)) { |
1147 | if (page) | |
920313a7 AK |
1148 | /* |
1149 | * drop the page reference and try | |
1150 | * to get the page with lock. If we | |
1151 | * are not uptodate that implies | |
1152 | * somebody just created the page but | |
1153 | * is yet to initialize the same. So | |
1154 | * wait for it to initialize. | |
1155 | */ | |
09cbfeaf | 1156 | put_page(page); |
adb7ef60 | 1157 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
c9de560d AT |
1158 | if (page) { |
1159 | BUG_ON(page->mapping != inode->i_mapping); | |
1160 | if (!PageUptodate(page)) { | |
adb7ef60 | 1161 | ret = ext4_mb_init_cache(page, NULL, gfp); |
fdf6c7a7 SF |
1162 | if (ret) { |
1163 | unlock_page(page); | |
1164 | goto err; | |
1165 | } | |
c9de560d AT |
1166 | mb_cmp_bitmaps(e4b, page_address(page) + |
1167 | (poff * sb->s_blocksize)); | |
1168 | } | |
1169 | unlock_page(page); | |
1170 | } | |
1171 | } | |
c57ab39b YL |
1172 | if (page == NULL) { |
1173 | ret = -ENOMEM; | |
1174 | goto err; | |
1175 | } | |
1176 | if (!PageUptodate(page)) { | |
fdf6c7a7 | 1177 | ret = -EIO; |
c9de560d | 1178 | goto err; |
fdf6c7a7 | 1179 | } |
2457aec6 MG |
1180 | |
1181 | /* Pages marked accessed already */ | |
c9de560d AT |
1182 | e4b->bd_bitmap_page = page; |
1183 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); | |
c9de560d AT |
1184 | |
1185 | block++; | |
1186 | pnum = block / blocks_per_page; | |
1187 | poff = block % blocks_per_page; | |
1188 | ||
2457aec6 | 1189 | page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED); |
c9de560d AT |
1190 | if (page == NULL || !PageUptodate(page)) { |
1191 | if (page) | |
09cbfeaf | 1192 | put_page(page); |
adb7ef60 | 1193 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
c9de560d AT |
1194 | if (page) { |
1195 | BUG_ON(page->mapping != inode->i_mapping); | |
fdf6c7a7 | 1196 | if (!PageUptodate(page)) { |
adb7ef60 KK |
1197 | ret = ext4_mb_init_cache(page, e4b->bd_bitmap, |
1198 | gfp); | |
fdf6c7a7 SF |
1199 | if (ret) { |
1200 | unlock_page(page); | |
1201 | goto err; | |
1202 | } | |
1203 | } | |
c9de560d AT |
1204 | unlock_page(page); |
1205 | } | |
1206 | } | |
c57ab39b YL |
1207 | if (page == NULL) { |
1208 | ret = -ENOMEM; | |
1209 | goto err; | |
1210 | } | |
1211 | if (!PageUptodate(page)) { | |
fdf6c7a7 | 1212 | ret = -EIO; |
c9de560d | 1213 | goto err; |
fdf6c7a7 | 1214 | } |
2457aec6 MG |
1215 | |
1216 | /* Pages marked accessed already */ | |
c9de560d AT |
1217 | e4b->bd_buddy_page = page; |
1218 | e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize); | |
c9de560d AT |
1219 | |
1220 | BUG_ON(e4b->bd_bitmap_page == NULL); | |
1221 | BUG_ON(e4b->bd_buddy_page == NULL); | |
1222 | ||
1223 | return 0; | |
1224 | ||
1225 | err: | |
26626f11 | 1226 | if (page) |
09cbfeaf | 1227 | put_page(page); |
c9de560d | 1228 | if (e4b->bd_bitmap_page) |
09cbfeaf | 1229 | put_page(e4b->bd_bitmap_page); |
c9de560d | 1230 | if (e4b->bd_buddy_page) |
09cbfeaf | 1231 | put_page(e4b->bd_buddy_page); |
c9de560d AT |
1232 | e4b->bd_buddy = NULL; |
1233 | e4b->bd_bitmap = NULL; | |
fdf6c7a7 | 1234 | return ret; |
c9de560d AT |
1235 | } |
1236 | ||
adb7ef60 KK |
1237 | static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group, |
1238 | struct ext4_buddy *e4b) | |
1239 | { | |
1240 | return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS); | |
1241 | } | |
1242 | ||
e39e07fd | 1243 | static void ext4_mb_unload_buddy(struct ext4_buddy *e4b) |
c9de560d AT |
1244 | { |
1245 | if (e4b->bd_bitmap_page) | |
09cbfeaf | 1246 | put_page(e4b->bd_bitmap_page); |
c9de560d | 1247 | if (e4b->bd_buddy_page) |
09cbfeaf | 1248 | put_page(e4b->bd_buddy_page); |
c9de560d AT |
1249 | } |
1250 | ||
1251 | ||
1252 | static int mb_find_order_for_block(struct ext4_buddy *e4b, int block) | |
1253 | { | |
1254 | int order = 1; | |
b5cb316c | 1255 | int bb_incr = 1 << (e4b->bd_blkbits - 1); |
c9de560d AT |
1256 | void *bb; |
1257 | ||
c5e8f3f3 | 1258 | BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); |
c9de560d AT |
1259 | BUG_ON(block >= (1 << (e4b->bd_blkbits + 3))); |
1260 | ||
c5e8f3f3 | 1261 | bb = e4b->bd_buddy; |
c9de560d AT |
1262 | while (order <= e4b->bd_blkbits + 1) { |
1263 | block = block >> 1; | |
1264 | if (!mb_test_bit(block, bb)) { | |
1265 | /* this block is part of buddy of order 'order' */ | |
1266 | return order; | |
1267 | } | |
b5cb316c NS |
1268 | bb += bb_incr; |
1269 | bb_incr >>= 1; | |
c9de560d AT |
1270 | order++; |
1271 | } | |
1272 | return 0; | |
1273 | } | |
1274 | ||
955ce5f5 | 1275 | static void mb_clear_bits(void *bm, int cur, int len) |
c9de560d AT |
1276 | { |
1277 | __u32 *addr; | |
1278 | ||
1279 | len = cur + len; | |
1280 | while (cur < len) { | |
1281 | if ((cur & 31) == 0 && (len - cur) >= 32) { | |
1282 | /* fast path: clear whole word at once */ | |
1283 | addr = bm + (cur >> 3); | |
1284 | *addr = 0; | |
1285 | cur += 32; | |
1286 | continue; | |
1287 | } | |
955ce5f5 | 1288 | mb_clear_bit(cur, bm); |
c9de560d AT |
1289 | cur++; |
1290 | } | |
1291 | } | |
1292 | ||
eabe0444 AS |
1293 | /* clear bits in given range |
1294 | * will return first found zero bit if any, -1 otherwise | |
1295 | */ | |
1296 | static int mb_test_and_clear_bits(void *bm, int cur, int len) | |
1297 | { | |
1298 | __u32 *addr; | |
1299 | int zero_bit = -1; | |
1300 | ||
1301 | len = cur + len; | |
1302 | while (cur < len) { | |
1303 | if ((cur & 31) == 0 && (len - cur) >= 32) { | |
1304 | /* fast path: clear whole word at once */ | |
1305 | addr = bm + (cur >> 3); | |
1306 | if (*addr != (__u32)(-1) && zero_bit == -1) | |
1307 | zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0); | |
1308 | *addr = 0; | |
1309 | cur += 32; | |
1310 | continue; | |
1311 | } | |
1312 | if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1) | |
1313 | zero_bit = cur; | |
1314 | cur++; | |
1315 | } | |
1316 | ||
1317 | return zero_bit; | |
1318 | } | |
1319 | ||
c3e94d1d | 1320 | void ext4_set_bits(void *bm, int cur, int len) |
c9de560d AT |
1321 | { |
1322 | __u32 *addr; | |
1323 | ||
1324 | len = cur + len; | |
1325 | while (cur < len) { | |
1326 | if ((cur & 31) == 0 && (len - cur) >= 32) { | |
1327 | /* fast path: set whole word at once */ | |
1328 | addr = bm + (cur >> 3); | |
1329 | *addr = 0xffffffff; | |
1330 | cur += 32; | |
1331 | continue; | |
1332 | } | |
955ce5f5 | 1333 | mb_set_bit(cur, bm); |
c9de560d AT |
1334 | cur++; |
1335 | } | |
1336 | } | |
1337 | ||
eabe0444 AS |
1338 | /* |
1339 | * _________________________________________________________________ */ | |
1340 | ||
1341 | static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side) | |
1342 | { | |
1343 | if (mb_test_bit(*bit + side, bitmap)) { | |
1344 | mb_clear_bit(*bit, bitmap); | |
1345 | (*bit) -= side; | |
1346 | return 1; | |
1347 | } | |
1348 | else { | |
1349 | (*bit) += side; | |
1350 | mb_set_bit(*bit, bitmap); | |
1351 | return -1; | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last) | |
1356 | { | |
1357 | int max; | |
1358 | int order = 1; | |
1359 | void *buddy = mb_find_buddy(e4b, order, &max); | |
1360 | ||
1361 | while (buddy) { | |
1362 | void *buddy2; | |
1363 | ||
1364 | /* Bits in range [first; last] are known to be set since | |
1365 | * corresponding blocks were allocated. Bits in range | |
1366 | * (first; last) will stay set because they form buddies on | |
1367 | * upper layer. We just deal with borders if they don't | |
1368 | * align with upper layer and then go up. | |
1369 | * Releasing entire group is all about clearing | |
1370 | * single bit of highest order buddy. | |
1371 | */ | |
1372 | ||
1373 | /* Example: | |
1374 | * --------------------------------- | |
1375 | * | 1 | 1 | 1 | 1 | | |
1376 | * --------------------------------- | |
1377 | * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | |
1378 | * --------------------------------- | |
1379 | * 0 1 2 3 4 5 6 7 | |
1380 | * \_____________________/ | |
1381 | * | |
1382 | * Neither [1] nor [6] is aligned to above layer. | |
1383 | * Left neighbour [0] is free, so mark it busy, | |
1384 | * decrease bb_counters and extend range to | |
1385 | * [0; 6] | |
1386 | * Right neighbour [7] is busy. It can't be coaleasced with [6], so | |
1387 | * mark [6] free, increase bb_counters and shrink range to | |
1388 | * [0; 5]. | |
1389 | * Then shift range to [0; 2], go up and do the same. | |
1390 | */ | |
1391 | ||
1392 | ||
1393 | if (first & 1) | |
1394 | e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1); | |
1395 | if (!(last & 1)) | |
1396 | e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1); | |
1397 | if (first > last) | |
1398 | break; | |
1399 | order++; | |
1400 | ||
1401 | if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) { | |
1402 | mb_clear_bits(buddy, first, last - first + 1); | |
1403 | e4b->bd_info->bb_counters[order - 1] += last - first + 1; | |
1404 | break; | |
1405 | } | |
1406 | first >>= 1; | |
1407 | last >>= 1; | |
1408 | buddy = buddy2; | |
1409 | } | |
1410 | } | |
1411 | ||
7e5a8cdd | 1412 | static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b, |
eabe0444 | 1413 | int first, int count) |
c9de560d | 1414 | { |
eabe0444 AS |
1415 | int left_is_free = 0; |
1416 | int right_is_free = 0; | |
1417 | int block; | |
1418 | int last = first + count - 1; | |
c9de560d AT |
1419 | struct super_block *sb = e4b->bd_sb; |
1420 | ||
c99d1e6e TT |
1421 | if (WARN_ON(count == 0)) |
1422 | return; | |
eabe0444 | 1423 | BUG_ON(last >= (sb->s_blocksize << 3)); |
bc8e6740 | 1424 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
163a203d DW |
1425 | /* Don't bother if the block group is corrupt. */ |
1426 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) | |
1427 | return; | |
1428 | ||
c9de560d AT |
1429 | mb_check_buddy(e4b); |
1430 | mb_free_blocks_double(inode, e4b, first, count); | |
1431 | ||
1432 | e4b->bd_info->bb_free += count; | |
1433 | if (first < e4b->bd_info->bb_first_free) | |
1434 | e4b->bd_info->bb_first_free = first; | |
1435 | ||
eabe0444 AS |
1436 | /* access memory sequentially: check left neighbour, |
1437 | * clear range and then check right neighbour | |
1438 | */ | |
c9de560d | 1439 | if (first != 0) |
eabe0444 AS |
1440 | left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap); |
1441 | block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count); | |
1442 | if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0]) | |
1443 | right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap); | |
1444 | ||
1445 | if (unlikely(block != -1)) { | |
e43bb4e6 | 1446 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
eabe0444 AS |
1447 | ext4_fsblk_t blocknr; |
1448 | ||
1449 | blocknr = ext4_group_first_block_no(sb, e4b->bd_group); | |
49598e04 | 1450 | blocknr += EXT4_C2B(sbi, block); |
eabe0444 AS |
1451 | ext4_grp_locked_error(sb, e4b->bd_group, |
1452 | inode ? inode->i_ino : 0, | |
1453 | blocknr, | |
1454 | "freeing already freed block " | |
163a203d DW |
1455 | "(bit %u); block bitmap corrupt.", |
1456 | block); | |
db79e6d1 WS |
1457 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
1458 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); | |
eabe0444 AS |
1459 | mb_regenerate_buddy(e4b); |
1460 | goto done; | |
1461 | } | |
1462 | ||
1463 | /* let's maintain fragments counter */ | |
1464 | if (left_is_free && right_is_free) | |
c9de560d | 1465 | e4b->bd_info->bb_fragments--; |
eabe0444 | 1466 | else if (!left_is_free && !right_is_free) |
c9de560d AT |
1467 | e4b->bd_info->bb_fragments++; |
1468 | ||
eabe0444 AS |
1469 | /* buddy[0] == bd_bitmap is a special case, so handle |
1470 | * it right away and let mb_buddy_mark_free stay free of | |
1471 | * zero order checks. | |
1472 | * Check if neighbours are to be coaleasced, | |
1473 | * adjust bitmap bb_counters and borders appropriately. | |
1474 | */ | |
1475 | if (first & 1) { | |
1476 | first += !left_is_free; | |
1477 | e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1; | |
1478 | } | |
1479 | if (!(last & 1)) { | |
1480 | last -= !right_is_free; | |
1481 | e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1; | |
1482 | } | |
c9de560d | 1483 | |
eabe0444 AS |
1484 | if (first <= last) |
1485 | mb_buddy_mark_free(e4b, first >> 1, last >> 1); | |
c9de560d | 1486 | |
eabe0444 | 1487 | done: |
8a57d9d6 | 1488 | mb_set_largest_free_order(sb, e4b->bd_info); |
c9de560d | 1489 | mb_check_buddy(e4b); |
c9de560d AT |
1490 | } |
1491 | ||
15c006a2 | 1492 | static int mb_find_extent(struct ext4_buddy *e4b, int block, |
c9de560d AT |
1493 | int needed, struct ext4_free_extent *ex) |
1494 | { | |
1495 | int next = block; | |
15c006a2 | 1496 | int max, order; |
c9de560d AT |
1497 | void *buddy; |
1498 | ||
bc8e6740 | 1499 | assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); |
c9de560d AT |
1500 | BUG_ON(ex == NULL); |
1501 | ||
15c006a2 | 1502 | buddy = mb_find_buddy(e4b, 0, &max); |
c9de560d AT |
1503 | BUG_ON(buddy == NULL); |
1504 | BUG_ON(block >= max); | |
1505 | if (mb_test_bit(block, buddy)) { | |
1506 | ex->fe_len = 0; | |
1507 | ex->fe_start = 0; | |
1508 | ex->fe_group = 0; | |
1509 | return 0; | |
1510 | } | |
1511 | ||
15c006a2 RD |
1512 | /* find actual order */ |
1513 | order = mb_find_order_for_block(e4b, block); | |
1514 | block = block >> order; | |
c9de560d AT |
1515 | |
1516 | ex->fe_len = 1 << order; | |
1517 | ex->fe_start = block << order; | |
1518 | ex->fe_group = e4b->bd_group; | |
1519 | ||
1520 | /* calc difference from given start */ | |
1521 | next = next - ex->fe_start; | |
1522 | ex->fe_len -= next; | |
1523 | ex->fe_start += next; | |
1524 | ||
1525 | while (needed > ex->fe_len && | |
d8ec0c39 | 1526 | mb_find_buddy(e4b, order, &max)) { |
c9de560d AT |
1527 | |
1528 | if (block + 1 >= max) | |
1529 | break; | |
1530 | ||
1531 | next = (block + 1) * (1 << order); | |
c5e8f3f3 | 1532 | if (mb_test_bit(next, e4b->bd_bitmap)) |
c9de560d AT |
1533 | break; |
1534 | ||
b051d8dc | 1535 | order = mb_find_order_for_block(e4b, next); |
c9de560d | 1536 | |
c9de560d AT |
1537 | block = next >> order; |
1538 | ex->fe_len += 1 << order; | |
1539 | } | |
1540 | ||
43c73221 TT |
1541 | if (ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3))) { |
1542 | /* Should never happen! (but apparently sometimes does?!?) */ | |
1543 | WARN_ON(1); | |
1544 | ext4_error(e4b->bd_sb, "corruption or bug in mb_find_extent " | |
1545 | "block=%d, order=%d needed=%d ex=%u/%d/%d@%u", | |
1546 | block, order, needed, ex->fe_group, ex->fe_start, | |
1547 | ex->fe_len, ex->fe_logical); | |
1548 | ex->fe_len = 0; | |
1549 | ex->fe_start = 0; | |
1550 | ex->fe_group = 0; | |
1551 | } | |
c9de560d AT |
1552 | return ex->fe_len; |
1553 | } | |
1554 | ||
1555 | static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) | |
1556 | { | |
1557 | int ord; | |
1558 | int mlen = 0; | |
1559 | int max = 0; | |
1560 | int cur; | |
1561 | int start = ex->fe_start; | |
1562 | int len = ex->fe_len; | |
1563 | unsigned ret = 0; | |
1564 | int len0 = len; | |
1565 | void *buddy; | |
1566 | ||
1567 | BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3)); | |
1568 | BUG_ON(e4b->bd_group != ex->fe_group); | |
bc8e6740 | 1569 | assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); |
c9de560d AT |
1570 | mb_check_buddy(e4b); |
1571 | mb_mark_used_double(e4b, start, len); | |
1572 | ||
1573 | e4b->bd_info->bb_free -= len; | |
1574 | if (e4b->bd_info->bb_first_free == start) | |
1575 | e4b->bd_info->bb_first_free += len; | |
1576 | ||
1577 | /* let's maintain fragments counter */ | |
1578 | if (start != 0) | |
c5e8f3f3 | 1579 | mlen = !mb_test_bit(start - 1, e4b->bd_bitmap); |
c9de560d | 1580 | if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0]) |
c5e8f3f3 | 1581 | max = !mb_test_bit(start + len, e4b->bd_bitmap); |
c9de560d AT |
1582 | if (mlen && max) |
1583 | e4b->bd_info->bb_fragments++; | |
1584 | else if (!mlen && !max) | |
1585 | e4b->bd_info->bb_fragments--; | |
1586 | ||
1587 | /* let's maintain buddy itself */ | |
1588 | while (len) { | |
1589 | ord = mb_find_order_for_block(e4b, start); | |
1590 | ||
1591 | if (((start >> ord) << ord) == start && len >= (1 << ord)) { | |
1592 | /* the whole chunk may be allocated at once! */ | |
1593 | mlen = 1 << ord; | |
1594 | buddy = mb_find_buddy(e4b, ord, &max); | |
1595 | BUG_ON((start >> ord) >= max); | |
1596 | mb_set_bit(start >> ord, buddy); | |
1597 | e4b->bd_info->bb_counters[ord]--; | |
1598 | start += mlen; | |
1599 | len -= mlen; | |
1600 | BUG_ON(len < 0); | |
1601 | continue; | |
1602 | } | |
1603 | ||
1604 | /* store for history */ | |
1605 | if (ret == 0) | |
1606 | ret = len | (ord << 16); | |
1607 | ||
1608 | /* we have to split large buddy */ | |
1609 | BUG_ON(ord <= 0); | |
1610 | buddy = mb_find_buddy(e4b, ord, &max); | |
1611 | mb_set_bit(start >> ord, buddy); | |
1612 | e4b->bd_info->bb_counters[ord]--; | |
1613 | ||
1614 | ord--; | |
1615 | cur = (start >> ord) & ~1U; | |
1616 | buddy = mb_find_buddy(e4b, ord, &max); | |
1617 | mb_clear_bit(cur, buddy); | |
1618 | mb_clear_bit(cur + 1, buddy); | |
1619 | e4b->bd_info->bb_counters[ord]++; | |
1620 | e4b->bd_info->bb_counters[ord]++; | |
1621 | } | |
8a57d9d6 | 1622 | mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info); |
c9de560d | 1623 | |
c5e8f3f3 | 1624 | ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0); |
c9de560d AT |
1625 | mb_check_buddy(e4b); |
1626 | ||
1627 | return ret; | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * Must be called under group lock! | |
1632 | */ | |
1633 | static void ext4_mb_use_best_found(struct ext4_allocation_context *ac, | |
1634 | struct ext4_buddy *e4b) | |
1635 | { | |
1636 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); | |
1637 | int ret; | |
1638 | ||
1639 | BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); | |
1640 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); | |
1641 | ||
1642 | ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len); | |
1643 | ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical; | |
1644 | ret = mb_mark_used(e4b, &ac->ac_b_ex); | |
1645 | ||
1646 | /* preallocation can change ac_b_ex, thus we store actually | |
1647 | * allocated blocks for history */ | |
1648 | ac->ac_f_ex = ac->ac_b_ex; | |
1649 | ||
1650 | ac->ac_status = AC_STATUS_FOUND; | |
1651 | ac->ac_tail = ret & 0xffff; | |
1652 | ac->ac_buddy = ret >> 16; | |
1653 | ||
c3a326a6 AK |
1654 | /* |
1655 | * take the page reference. We want the page to be pinned | |
1656 | * so that we don't get a ext4_mb_init_cache_call for this | |
1657 | * group until we update the bitmap. That would mean we | |
1658 | * double allocate blocks. The reference is dropped | |
1659 | * in ext4_mb_release_context | |
1660 | */ | |
c9de560d AT |
1661 | ac->ac_bitmap_page = e4b->bd_bitmap_page; |
1662 | get_page(ac->ac_bitmap_page); | |
1663 | ac->ac_buddy_page = e4b->bd_buddy_page; | |
1664 | get_page(ac->ac_buddy_page); | |
c9de560d | 1665 | /* store last allocated for subsequent stream allocation */ |
4ba74d00 | 1666 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
c9de560d AT |
1667 | spin_lock(&sbi->s_md_lock); |
1668 | sbi->s_mb_last_group = ac->ac_f_ex.fe_group; | |
1669 | sbi->s_mb_last_start = ac->ac_f_ex.fe_start; | |
1670 | spin_unlock(&sbi->s_md_lock); | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | /* | |
1675 | * regular allocator, for general purposes allocation | |
1676 | */ | |
1677 | ||
1678 | static void ext4_mb_check_limits(struct ext4_allocation_context *ac, | |
1679 | struct ext4_buddy *e4b, | |
1680 | int finish_group) | |
1681 | { | |
1682 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); | |
1683 | struct ext4_free_extent *bex = &ac->ac_b_ex; | |
1684 | struct ext4_free_extent *gex = &ac->ac_g_ex; | |
1685 | struct ext4_free_extent ex; | |
1686 | int max; | |
1687 | ||
032115fc AK |
1688 | if (ac->ac_status == AC_STATUS_FOUND) |
1689 | return; | |
c9de560d AT |
1690 | /* |
1691 | * We don't want to scan for a whole year | |
1692 | */ | |
1693 | if (ac->ac_found > sbi->s_mb_max_to_scan && | |
1694 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { | |
1695 | ac->ac_status = AC_STATUS_BREAK; | |
1696 | return; | |
1697 | } | |
1698 | ||
1699 | /* | |
1700 | * Haven't found good chunk so far, let's continue | |
1701 | */ | |
1702 | if (bex->fe_len < gex->fe_len) | |
1703 | return; | |
1704 | ||
1705 | if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan) | |
1706 | && bex->fe_group == e4b->bd_group) { | |
1707 | /* recheck chunk's availability - we don't know | |
1708 | * when it was found (within this lock-unlock | |
1709 | * period or not) */ | |
15c006a2 | 1710 | max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex); |
c9de560d AT |
1711 | if (max >= gex->fe_len) { |
1712 | ext4_mb_use_best_found(ac, e4b); | |
1713 | return; | |
1714 | } | |
1715 | } | |
1716 | } | |
1717 | ||
1718 | /* | |
1719 | * The routine checks whether found extent is good enough. If it is, | |
1720 | * then the extent gets marked used and flag is set to the context | |
1721 | * to stop scanning. Otherwise, the extent is compared with the | |
1722 | * previous found extent and if new one is better, then it's stored | |
1723 | * in the context. Later, the best found extent will be used, if | |
1724 | * mballoc can't find good enough extent. | |
1725 | * | |
1726 | * FIXME: real allocation policy is to be designed yet! | |
1727 | */ | |
1728 | static void ext4_mb_measure_extent(struct ext4_allocation_context *ac, | |
1729 | struct ext4_free_extent *ex, | |
1730 | struct ext4_buddy *e4b) | |
1731 | { | |
1732 | struct ext4_free_extent *bex = &ac->ac_b_ex; | |
1733 | struct ext4_free_extent *gex = &ac->ac_g_ex; | |
1734 | ||
1735 | BUG_ON(ex->fe_len <= 0); | |
7137d7a4 TT |
1736 | BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); |
1737 | BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); | |
c9de560d AT |
1738 | BUG_ON(ac->ac_status != AC_STATUS_CONTINUE); |
1739 | ||
1740 | ac->ac_found++; | |
1741 | ||
1742 | /* | |
1743 | * The special case - take what you catch first | |
1744 | */ | |
1745 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) { | |
1746 | *bex = *ex; | |
1747 | ext4_mb_use_best_found(ac, e4b); | |
1748 | return; | |
1749 | } | |
1750 | ||
1751 | /* | |
1752 | * Let's check whether the chuck is good enough | |
1753 | */ | |
1754 | if (ex->fe_len == gex->fe_len) { | |
1755 | *bex = *ex; | |
1756 | ext4_mb_use_best_found(ac, e4b); | |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | /* | |
1761 | * If this is first found extent, just store it in the context | |
1762 | */ | |
1763 | if (bex->fe_len == 0) { | |
1764 | *bex = *ex; | |
1765 | return; | |
1766 | } | |
1767 | ||
1768 | /* | |
1769 | * If new found extent is better, store it in the context | |
1770 | */ | |
1771 | if (bex->fe_len < gex->fe_len) { | |
1772 | /* if the request isn't satisfied, any found extent | |
1773 | * larger than previous best one is better */ | |
1774 | if (ex->fe_len > bex->fe_len) | |
1775 | *bex = *ex; | |
1776 | } else if (ex->fe_len > gex->fe_len) { | |
1777 | /* if the request is satisfied, then we try to find | |
1778 | * an extent that still satisfy the request, but is | |
1779 | * smaller than previous one */ | |
1780 | if (ex->fe_len < bex->fe_len) | |
1781 | *bex = *ex; | |
1782 | } | |
1783 | ||
1784 | ext4_mb_check_limits(ac, e4b, 0); | |
1785 | } | |
1786 | ||
089ceecc ES |
1787 | static noinline_for_stack |
1788 | int ext4_mb_try_best_found(struct ext4_allocation_context *ac, | |
c9de560d AT |
1789 | struct ext4_buddy *e4b) |
1790 | { | |
1791 | struct ext4_free_extent ex = ac->ac_b_ex; | |
1792 | ext4_group_t group = ex.fe_group; | |
1793 | int max; | |
1794 | int err; | |
1795 | ||
1796 | BUG_ON(ex.fe_len <= 0); | |
1797 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); | |
1798 | if (err) | |
1799 | return err; | |
1800 | ||
1801 | ext4_lock_group(ac->ac_sb, group); | |
15c006a2 | 1802 | max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex); |
c9de560d AT |
1803 | |
1804 | if (max > 0) { | |
1805 | ac->ac_b_ex = ex; | |
1806 | ext4_mb_use_best_found(ac, e4b); | |
1807 | } | |
1808 | ||
1809 | ext4_unlock_group(ac->ac_sb, group); | |
e39e07fd | 1810 | ext4_mb_unload_buddy(e4b); |
c9de560d AT |
1811 | |
1812 | return 0; | |
1813 | } | |
1814 | ||
089ceecc ES |
1815 | static noinline_for_stack |
1816 | int ext4_mb_find_by_goal(struct ext4_allocation_context *ac, | |
c9de560d AT |
1817 | struct ext4_buddy *e4b) |
1818 | { | |
1819 | ext4_group_t group = ac->ac_g_ex.fe_group; | |
1820 | int max; | |
1821 | int err; | |
1822 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); | |
838cd0cf | 1823 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
c9de560d AT |
1824 | struct ext4_free_extent ex; |
1825 | ||
1826 | if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL)) | |
1827 | return 0; | |
838cd0cf YY |
1828 | if (grp->bb_free == 0) |
1829 | return 0; | |
c9de560d AT |
1830 | |
1831 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); | |
1832 | if (err) | |
1833 | return err; | |
1834 | ||
163a203d DW |
1835 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) { |
1836 | ext4_mb_unload_buddy(e4b); | |
1837 | return 0; | |
1838 | } | |
1839 | ||
c9de560d | 1840 | ext4_lock_group(ac->ac_sb, group); |
15c006a2 | 1841 | max = mb_find_extent(e4b, ac->ac_g_ex.fe_start, |
c9de560d | 1842 | ac->ac_g_ex.fe_len, &ex); |
ab0c00fc | 1843 | ex.fe_logical = 0xDEADFA11; /* debug value */ |
c9de560d AT |
1844 | |
1845 | if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) { | |
1846 | ext4_fsblk_t start; | |
1847 | ||
5661bd68 AM |
1848 | start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) + |
1849 | ex.fe_start; | |
c9de560d AT |
1850 | /* use do_div to get remainder (would be 64-bit modulo) */ |
1851 | if (do_div(start, sbi->s_stripe) == 0) { | |
1852 | ac->ac_found++; | |
1853 | ac->ac_b_ex = ex; | |
1854 | ext4_mb_use_best_found(ac, e4b); | |
1855 | } | |
1856 | } else if (max >= ac->ac_g_ex.fe_len) { | |
1857 | BUG_ON(ex.fe_len <= 0); | |
1858 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); | |
1859 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); | |
1860 | ac->ac_found++; | |
1861 | ac->ac_b_ex = ex; | |
1862 | ext4_mb_use_best_found(ac, e4b); | |
1863 | } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) { | |
1864 | /* Sometimes, caller may want to merge even small | |
1865 | * number of blocks to an existing extent */ | |
1866 | BUG_ON(ex.fe_len <= 0); | |
1867 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); | |
1868 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); | |
1869 | ac->ac_found++; | |
1870 | ac->ac_b_ex = ex; | |
1871 | ext4_mb_use_best_found(ac, e4b); | |
1872 | } | |
1873 | ext4_unlock_group(ac->ac_sb, group); | |
e39e07fd | 1874 | ext4_mb_unload_buddy(e4b); |
c9de560d AT |
1875 | |
1876 | return 0; | |
1877 | } | |
1878 | ||
1879 | /* | |
1880 | * The routine scans buddy structures (not bitmap!) from given order | |
1881 | * to max order and tries to find big enough chunk to satisfy the req | |
1882 | */ | |
089ceecc ES |
1883 | static noinline_for_stack |
1884 | void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, | |
c9de560d AT |
1885 | struct ext4_buddy *e4b) |
1886 | { | |
1887 | struct super_block *sb = ac->ac_sb; | |
1888 | struct ext4_group_info *grp = e4b->bd_info; | |
1889 | void *buddy; | |
1890 | int i; | |
1891 | int k; | |
1892 | int max; | |
1893 | ||
1894 | BUG_ON(ac->ac_2order <= 0); | |
1895 | for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) { | |
1896 | if (grp->bb_counters[i] == 0) | |
1897 | continue; | |
1898 | ||
1899 | buddy = mb_find_buddy(e4b, i, &max); | |
1900 | BUG_ON(buddy == NULL); | |
1901 | ||
ffad0a44 | 1902 | k = mb_find_next_zero_bit(buddy, max, 0); |
c9de560d AT |
1903 | BUG_ON(k >= max); |
1904 | ||
1905 | ac->ac_found++; | |
1906 | ||
1907 | ac->ac_b_ex.fe_len = 1 << i; | |
1908 | ac->ac_b_ex.fe_start = k << i; | |
1909 | ac->ac_b_ex.fe_group = e4b->bd_group; | |
1910 | ||
1911 | ext4_mb_use_best_found(ac, e4b); | |
1912 | ||
1913 | BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len); | |
1914 | ||
1915 | if (EXT4_SB(sb)->s_mb_stats) | |
1916 | atomic_inc(&EXT4_SB(sb)->s_bal_2orders); | |
1917 | ||
1918 | break; | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | /* | |
1923 | * The routine scans the group and measures all found extents. | |
1924 | * In order to optimize scanning, caller must pass number of | |
1925 | * free blocks in the group, so the routine can know upper limit. | |
1926 | */ | |
089ceecc ES |
1927 | static noinline_for_stack |
1928 | void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac, | |
c9de560d AT |
1929 | struct ext4_buddy *e4b) |
1930 | { | |
1931 | struct super_block *sb = ac->ac_sb; | |
c5e8f3f3 | 1932 | void *bitmap = e4b->bd_bitmap; |
c9de560d AT |
1933 | struct ext4_free_extent ex; |
1934 | int i; | |
1935 | int free; | |
1936 | ||
1937 | free = e4b->bd_info->bb_free; | |
1938 | BUG_ON(free <= 0); | |
1939 | ||
1940 | i = e4b->bd_info->bb_first_free; | |
1941 | ||
1942 | while (free && ac->ac_status == AC_STATUS_CONTINUE) { | |
ffad0a44 | 1943 | i = mb_find_next_zero_bit(bitmap, |
7137d7a4 TT |
1944 | EXT4_CLUSTERS_PER_GROUP(sb), i); |
1945 | if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) { | |
26346ff6 | 1946 | /* |
e56eb659 | 1947 | * IF we have corrupt bitmap, we won't find any |
26346ff6 AK |
1948 | * free blocks even though group info says we |
1949 | * we have free blocks | |
1950 | */ | |
e29136f8 | 1951 | ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, |
53accfa9 | 1952 | "%d free clusters as per " |
fde4d95a | 1953 | "group info. But bitmap says 0", |
26346ff6 | 1954 | free); |
736dedbb WS |
1955 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
1956 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); | |
c9de560d AT |
1957 | break; |
1958 | } | |
1959 | ||
15c006a2 | 1960 | mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex); |
c9de560d | 1961 | BUG_ON(ex.fe_len <= 0); |
26346ff6 | 1962 | if (free < ex.fe_len) { |
e29136f8 | 1963 | ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, |
53accfa9 | 1964 | "%d free clusters as per " |
fde4d95a | 1965 | "group info. But got %d blocks", |
26346ff6 | 1966 | free, ex.fe_len); |
736dedbb WS |
1967 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
1968 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); | |
e56eb659 AK |
1969 | /* |
1970 | * The number of free blocks differs. This mostly | |
1971 | * indicate that the bitmap is corrupt. So exit | |
1972 | * without claiming the space. | |
1973 | */ | |
1974 | break; | |
26346ff6 | 1975 | } |
ab0c00fc | 1976 | ex.fe_logical = 0xDEADC0DE; /* debug value */ |
c9de560d AT |
1977 | ext4_mb_measure_extent(ac, &ex, e4b); |
1978 | ||
1979 | i += ex.fe_len; | |
1980 | free -= ex.fe_len; | |
1981 | } | |
1982 | ||
1983 | ext4_mb_check_limits(ac, e4b, 1); | |
1984 | } | |
1985 | ||
1986 | /* | |
1987 | * This is a special case for storages like raid5 | |
506bf2d8 | 1988 | * we try to find stripe-aligned chunks for stripe-size-multiple requests |
c9de560d | 1989 | */ |
089ceecc ES |
1990 | static noinline_for_stack |
1991 | void ext4_mb_scan_aligned(struct ext4_allocation_context *ac, | |
c9de560d AT |
1992 | struct ext4_buddy *e4b) |
1993 | { | |
1994 | struct super_block *sb = ac->ac_sb; | |
1995 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
c5e8f3f3 | 1996 | void *bitmap = e4b->bd_bitmap; |
c9de560d AT |
1997 | struct ext4_free_extent ex; |
1998 | ext4_fsblk_t first_group_block; | |
1999 | ext4_fsblk_t a; | |
2000 | ext4_grpblk_t i; | |
2001 | int max; | |
2002 | ||
2003 | BUG_ON(sbi->s_stripe == 0); | |
2004 | ||
2005 | /* find first stripe-aligned block in group */ | |
5661bd68 AM |
2006 | first_group_block = ext4_group_first_block_no(sb, e4b->bd_group); |
2007 | ||
c9de560d AT |
2008 | a = first_group_block + sbi->s_stripe - 1; |
2009 | do_div(a, sbi->s_stripe); | |
2010 | i = (a * sbi->s_stripe) - first_group_block; | |
2011 | ||
7137d7a4 | 2012 | while (i < EXT4_CLUSTERS_PER_GROUP(sb)) { |
c9de560d | 2013 | if (!mb_test_bit(i, bitmap)) { |
15c006a2 | 2014 | max = mb_find_extent(e4b, i, sbi->s_stripe, &ex); |
c9de560d AT |
2015 | if (max >= sbi->s_stripe) { |
2016 | ac->ac_found++; | |
ab0c00fc | 2017 | ex.fe_logical = 0xDEADF00D; /* debug value */ |
c9de560d AT |
2018 | ac->ac_b_ex = ex; |
2019 | ext4_mb_use_best_found(ac, e4b); | |
2020 | break; | |
2021 | } | |
2022 | } | |
2023 | i += sbi->s_stripe; | |
2024 | } | |
2025 | } | |
2026 | ||
42ac1848 LC |
2027 | /* |
2028 | * This is now called BEFORE we load the buddy bitmap. | |
2029 | * Returns either 1 or 0 indicating that the group is either suitable | |
2030 | * for the allocation or not. In addition it can also return negative | |
2031 | * error code when something goes wrong. | |
2032 | */ | |
c9de560d AT |
2033 | static int ext4_mb_good_group(struct ext4_allocation_context *ac, |
2034 | ext4_group_t group, int cr) | |
2035 | { | |
2036 | unsigned free, fragments; | |
a4912123 | 2037 | int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb)); |
c9de560d AT |
2038 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
2039 | ||
2040 | BUG_ON(cr < 0 || cr >= 4); | |
8a57d9d6 | 2041 | |
01fc48e8 TT |
2042 | free = grp->bb_free; |
2043 | if (free == 0) | |
2044 | return 0; | |
2045 | if (cr <= 2 && free < ac->ac_g_ex.fe_len) | |
2046 | return 0; | |
2047 | ||
163a203d DW |
2048 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) |
2049 | return 0; | |
2050 | ||
8a57d9d6 CW |
2051 | /* We only do this if the grp has never been initialized */ |
2052 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { | |
adb7ef60 | 2053 | int ret = ext4_mb_init_group(ac->ac_sb, group, GFP_NOFS); |
8a57d9d6 | 2054 | if (ret) |
42ac1848 | 2055 | return ret; |
8a57d9d6 | 2056 | } |
c9de560d | 2057 | |
c9de560d | 2058 | fragments = grp->bb_fragments; |
c9de560d AT |
2059 | if (fragments == 0) |
2060 | return 0; | |
2061 | ||
2062 | switch (cr) { | |
2063 | case 0: | |
2064 | BUG_ON(ac->ac_2order == 0); | |
c9de560d | 2065 | |
a4912123 TT |
2066 | /* Avoid using the first bg of a flexgroup for data files */ |
2067 | if ((ac->ac_flags & EXT4_MB_HINT_DATA) && | |
2068 | (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) && | |
2069 | ((group % flex_size) == 0)) | |
2070 | return 0; | |
2071 | ||
40ae3487 TT |
2072 | if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) || |
2073 | (free / fragments) >= ac->ac_g_ex.fe_len) | |
2074 | return 1; | |
2075 | ||
2076 | if (grp->bb_largest_free_order < ac->ac_2order) | |
2077 | return 0; | |
2078 | ||
8a57d9d6 | 2079 | return 1; |
c9de560d AT |
2080 | case 1: |
2081 | if ((free / fragments) >= ac->ac_g_ex.fe_len) | |
2082 | return 1; | |
2083 | break; | |
2084 | case 2: | |
2085 | if (free >= ac->ac_g_ex.fe_len) | |
2086 | return 1; | |
2087 | break; | |
2088 | case 3: | |
2089 | return 1; | |
2090 | default: | |
2091 | BUG(); | |
2092 | } | |
2093 | ||
2094 | return 0; | |
2095 | } | |
2096 | ||
4ddfef7b ES |
2097 | static noinline_for_stack int |
2098 | ext4_mb_regular_allocator(struct ext4_allocation_context *ac) | |
c9de560d | 2099 | { |
8df9675f | 2100 | ext4_group_t ngroups, group, i; |
c9de560d | 2101 | int cr; |
42ac1848 | 2102 | int err = 0, first_err = 0; |
c9de560d AT |
2103 | struct ext4_sb_info *sbi; |
2104 | struct super_block *sb; | |
2105 | struct ext4_buddy e4b; | |
c9de560d AT |
2106 | |
2107 | sb = ac->ac_sb; | |
2108 | sbi = EXT4_SB(sb); | |
8df9675f | 2109 | ngroups = ext4_get_groups_count(sb); |
fb0a387d | 2110 | /* non-extent files are limited to low blocks/groups */ |
12e9b892 | 2111 | if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS))) |
fb0a387d ES |
2112 | ngroups = sbi->s_blockfile_groups; |
2113 | ||
c9de560d AT |
2114 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
2115 | ||
2116 | /* first, try the goal */ | |
2117 | err = ext4_mb_find_by_goal(ac, &e4b); | |
2118 | if (err || ac->ac_status == AC_STATUS_FOUND) | |
2119 | goto out; | |
2120 | ||
2121 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) | |
2122 | goto out; | |
2123 | ||
2124 | /* | |
2125 | * ac->ac2_order is set only if the fe_len is a power of 2 | |
2126 | * if ac2_order is set we also set criteria to 0 so that we | |
2127 | * try exact allocation using buddy. | |
2128 | */ | |
2129 | i = fls(ac->ac_g_ex.fe_len); | |
2130 | ac->ac_2order = 0; | |
2131 | /* | |
2132 | * We search using buddy data only if the order of the request | |
2133 | * is greater than equal to the sbi_s_mb_order2_reqs | |
b713a5ec | 2134 | * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req |
d9b22cf9 JK |
2135 | * We also support searching for power-of-two requests only for |
2136 | * requests upto maximum buddy size we have constructed. | |
c9de560d | 2137 | */ |
d9b22cf9 | 2138 | if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) { |
c9de560d AT |
2139 | /* |
2140 | * This should tell if fe_len is exactly power of 2 | |
2141 | */ | |
2142 | if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0) | |
2143 | ac->ac_2order = i - 1; | |
2144 | } | |
2145 | ||
4ba74d00 TT |
2146 | /* if stream allocation is enabled, use global goal */ |
2147 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { | |
c9de560d AT |
2148 | /* TBD: may be hot point */ |
2149 | spin_lock(&sbi->s_md_lock); | |
2150 | ac->ac_g_ex.fe_group = sbi->s_mb_last_group; | |
2151 | ac->ac_g_ex.fe_start = sbi->s_mb_last_start; | |
2152 | spin_unlock(&sbi->s_md_lock); | |
2153 | } | |
4ba74d00 | 2154 | |
c9de560d AT |
2155 | /* Let's just scan groups to find more-less suitable blocks */ |
2156 | cr = ac->ac_2order ? 0 : 1; | |
2157 | /* | |
2158 | * cr == 0 try to get exact allocation, | |
2159 | * cr == 3 try to get anything | |
2160 | */ | |
2161 | repeat: | |
2162 | for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) { | |
2163 | ac->ac_criteria = cr; | |
ed8f9c75 AK |
2164 | /* |
2165 | * searching for the right group start | |
2166 | * from the goal value specified | |
2167 | */ | |
2168 | group = ac->ac_g_ex.fe_group; | |
2169 | ||
8df9675f | 2170 | for (i = 0; i < ngroups; group++, i++) { |
42ac1848 | 2171 | int ret = 0; |
2ed5724d | 2172 | cond_resched(); |
e6155736 LM |
2173 | /* |
2174 | * Artificially restricted ngroups for non-extent | |
2175 | * files makes group > ngroups possible on first loop. | |
2176 | */ | |
2177 | if (group >= ngroups) | |
c9de560d AT |
2178 | group = 0; |
2179 | ||
8a57d9d6 | 2180 | /* This now checks without needing the buddy page */ |
42ac1848 LC |
2181 | ret = ext4_mb_good_group(ac, group, cr); |
2182 | if (ret <= 0) { | |
2183 | if (!first_err) | |
2184 | first_err = ret; | |
c9de560d | 2185 | continue; |
42ac1848 | 2186 | } |
c9de560d | 2187 | |
c9de560d AT |
2188 | err = ext4_mb_load_buddy(sb, group, &e4b); |
2189 | if (err) | |
2190 | goto out; | |
2191 | ||
2192 | ext4_lock_group(sb, group); | |
8a57d9d6 CW |
2193 | |
2194 | /* | |
2195 | * We need to check again after locking the | |
2196 | * block group | |
2197 | */ | |
42ac1848 LC |
2198 | ret = ext4_mb_good_group(ac, group, cr); |
2199 | if (ret <= 0) { | |
c9de560d | 2200 | ext4_unlock_group(sb, group); |
e39e07fd | 2201 | ext4_mb_unload_buddy(&e4b); |
42ac1848 LC |
2202 | if (!first_err) |
2203 | first_err = ret; | |
c9de560d AT |
2204 | continue; |
2205 | } | |
2206 | ||
2207 | ac->ac_groups_scanned++; | |
d9b22cf9 | 2208 | if (cr == 0) |
c9de560d | 2209 | ext4_mb_simple_scan_group(ac, &e4b); |
506bf2d8 ES |
2210 | else if (cr == 1 && sbi->s_stripe && |
2211 | !(ac->ac_g_ex.fe_len % sbi->s_stripe)) | |
c9de560d AT |
2212 | ext4_mb_scan_aligned(ac, &e4b); |
2213 | else | |
2214 | ext4_mb_complex_scan_group(ac, &e4b); | |
2215 | ||
2216 | ext4_unlock_group(sb, group); | |
e39e07fd | 2217 | ext4_mb_unload_buddy(&e4b); |
c9de560d AT |
2218 | |
2219 | if (ac->ac_status != AC_STATUS_CONTINUE) | |
2220 | break; | |
2221 | } | |
2222 | } | |
2223 | ||
2224 | if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND && | |
2225 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { | |
2226 | /* | |
2227 | * We've been searching too long. Let's try to allocate | |
2228 | * the best chunk we've found so far | |
2229 | */ | |
2230 | ||
2231 | ext4_mb_try_best_found(ac, &e4b); | |
2232 | if (ac->ac_status != AC_STATUS_FOUND) { | |
2233 | /* | |
2234 | * Someone more lucky has already allocated it. | |
2235 | * The only thing we can do is just take first | |
2236 | * found block(s) | |
2237 | printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n"); | |
2238 | */ | |
2239 | ac->ac_b_ex.fe_group = 0; | |
2240 | ac->ac_b_ex.fe_start = 0; | |
2241 | ac->ac_b_ex.fe_len = 0; | |
2242 | ac->ac_status = AC_STATUS_CONTINUE; | |
2243 | ac->ac_flags |= EXT4_MB_HINT_FIRST; | |
2244 | cr = 3; | |
2245 | atomic_inc(&sbi->s_mb_lost_chunks); | |
2246 | goto repeat; | |
2247 | } | |
2248 | } | |
2249 | out: | |
42ac1848 LC |
2250 | if (!err && ac->ac_status != AC_STATUS_FOUND && first_err) |
2251 | err = first_err; | |
c9de560d AT |
2252 | return err; |
2253 | } | |
2254 | ||
c9de560d AT |
2255 | static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos) |
2256 | { | |
2257 | struct super_block *sb = seq->private; | |
c9de560d AT |
2258 | ext4_group_t group; |
2259 | ||
8df9675f | 2260 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
c9de560d | 2261 | return NULL; |
c9de560d | 2262 | group = *pos + 1; |
a9df9a49 | 2263 | return (void *) ((unsigned long) group); |
c9de560d AT |
2264 | } |
2265 | ||
2266 | static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos) | |
2267 | { | |
2268 | struct super_block *sb = seq->private; | |
c9de560d AT |
2269 | ext4_group_t group; |
2270 | ||
2271 | ++*pos; | |
8df9675f | 2272 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
c9de560d AT |
2273 | return NULL; |
2274 | group = *pos + 1; | |
a9df9a49 | 2275 | return (void *) ((unsigned long) group); |
c9de560d AT |
2276 | } |
2277 | ||
2278 | static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) | |
2279 | { | |
2280 | struct super_block *sb = seq->private; | |
a9df9a49 | 2281 | ext4_group_t group = (ext4_group_t) ((unsigned long) v); |
c9de560d | 2282 | int i; |
1c8457ca | 2283 | int err, buddy_loaded = 0; |
c9de560d | 2284 | struct ext4_buddy e4b; |
1c8457ca | 2285 | struct ext4_group_info *grinfo; |
2df2c340 AB |
2286 | unsigned char blocksize_bits = min_t(unsigned char, |
2287 | sb->s_blocksize_bits, | |
2288 | EXT4_MAX_BLOCK_LOG_SIZE); | |
c9de560d AT |
2289 | struct sg { |
2290 | struct ext4_group_info info; | |
b80b32b6 | 2291 | ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2]; |
c9de560d AT |
2292 | } sg; |
2293 | ||
2294 | group--; | |
2295 | if (group == 0) | |
97b4af2f RV |
2296 | seq_puts(seq, "#group: free frags first [" |
2297 | " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 " | |
802cf1f9 | 2298 | " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n"); |
c9de560d | 2299 | |
b80b32b6 TT |
2300 | i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) + |
2301 | sizeof(struct ext4_group_info); | |
2302 | ||
1c8457ca AK |
2303 | grinfo = ext4_get_group_info(sb, group); |
2304 | /* Load the group info in memory only if not already loaded. */ | |
2305 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) { | |
2306 | err = ext4_mb_load_buddy(sb, group, &e4b); | |
2307 | if (err) { | |
2308 | seq_printf(seq, "#%-5u: I/O error\n", group); | |
2309 | return 0; | |
2310 | } | |
2311 | buddy_loaded = 1; | |
c9de560d | 2312 | } |
1c8457ca | 2313 | |
b80b32b6 | 2314 | memcpy(&sg, ext4_get_group_info(sb, group), i); |
1c8457ca AK |
2315 | |
2316 | if (buddy_loaded) | |
2317 | ext4_mb_unload_buddy(&e4b); | |
c9de560d | 2318 | |
a9df9a49 | 2319 | seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free, |
c9de560d AT |
2320 | sg.info.bb_fragments, sg.info.bb_first_free); |
2321 | for (i = 0; i <= 13; i++) | |
2df2c340 | 2322 | seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ? |
c9de560d AT |
2323 | sg.info.bb_counters[i] : 0); |
2324 | seq_printf(seq, " ]\n"); | |
2325 | ||
2326 | return 0; | |
2327 | } | |
2328 | ||
2329 | static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v) | |
2330 | { | |
2331 | } | |
2332 | ||
7f1346a9 | 2333 | static const struct seq_operations ext4_mb_seq_groups_ops = { |
c9de560d AT |
2334 | .start = ext4_mb_seq_groups_start, |
2335 | .next = ext4_mb_seq_groups_next, | |
2336 | .stop = ext4_mb_seq_groups_stop, | |
2337 | .show = ext4_mb_seq_groups_show, | |
2338 | }; | |
2339 | ||
2340 | static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file) | |
2341 | { | |
d9dda78b | 2342 | struct super_block *sb = PDE_DATA(inode); |
c9de560d AT |
2343 | int rc; |
2344 | ||
2345 | rc = seq_open(file, &ext4_mb_seq_groups_ops); | |
2346 | if (rc == 0) { | |
a271fe85 | 2347 | struct seq_file *m = file->private_data; |
c9de560d AT |
2348 | m->private = sb; |
2349 | } | |
2350 | return rc; | |
2351 | ||
2352 | } | |
2353 | ||
ebd173be | 2354 | const struct file_operations ext4_seq_mb_groups_fops = { |
c9de560d AT |
2355 | .open = ext4_mb_seq_groups_open, |
2356 | .read = seq_read, | |
2357 | .llseek = seq_lseek, | |
2358 | .release = seq_release, | |
2359 | }; | |
2360 | ||
fb1813f4 CW |
2361 | static struct kmem_cache *get_groupinfo_cache(int blocksize_bits) |
2362 | { | |
2363 | int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; | |
2364 | struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index]; | |
2365 | ||
2366 | BUG_ON(!cachep); | |
2367 | return cachep; | |
2368 | } | |
5f21b0e6 | 2369 | |
28623c2f TT |
2370 | /* |
2371 | * Allocate the top-level s_group_info array for the specified number | |
2372 | * of groups | |
2373 | */ | |
2374 | int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups) | |
2375 | { | |
2376 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
2377 | unsigned size; | |
2378 | struct ext4_group_info ***new_groupinfo; | |
2379 | ||
2380 | size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >> | |
2381 | EXT4_DESC_PER_BLOCK_BITS(sb); | |
2382 | if (size <= sbi->s_group_info_size) | |
2383 | return 0; | |
2384 | ||
2385 | size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size); | |
a7c3e901 | 2386 | new_groupinfo = kvzalloc(size, GFP_KERNEL); |
28623c2f TT |
2387 | if (!new_groupinfo) { |
2388 | ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group"); | |
2389 | return -ENOMEM; | |
2390 | } | |
2391 | if (sbi->s_group_info) { | |
2392 | memcpy(new_groupinfo, sbi->s_group_info, | |
2393 | sbi->s_group_info_size * sizeof(*sbi->s_group_info)); | |
b93b41d4 | 2394 | kvfree(sbi->s_group_info); |
28623c2f TT |
2395 | } |
2396 | sbi->s_group_info = new_groupinfo; | |
2397 | sbi->s_group_info_size = size / sizeof(*sbi->s_group_info); | |
2398 | ext4_debug("allocated s_groupinfo array for %d meta_bg's\n", | |
2399 | sbi->s_group_info_size); | |
2400 | return 0; | |
2401 | } | |
2402 | ||
5f21b0e6 | 2403 | /* Create and initialize ext4_group_info data for the given group. */ |
920313a7 | 2404 | int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group, |
5f21b0e6 FB |
2405 | struct ext4_group_desc *desc) |
2406 | { | |
fb1813f4 | 2407 | int i; |
5f21b0e6 FB |
2408 | int metalen = 0; |
2409 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
2410 | struct ext4_group_info **meta_group_info; | |
fb1813f4 | 2411 | struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
5f21b0e6 FB |
2412 | |
2413 | /* | |
2414 | * First check if this group is the first of a reserved block. | |
2415 | * If it's true, we have to allocate a new table of pointers | |
2416 | * to ext4_group_info structures | |
2417 | */ | |
2418 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { | |
2419 | metalen = sizeof(*meta_group_info) << | |
2420 | EXT4_DESC_PER_BLOCK_BITS(sb); | |
4fdb5543 | 2421 | meta_group_info = kmalloc(metalen, GFP_NOFS); |
5f21b0e6 | 2422 | if (meta_group_info == NULL) { |
7f6a11e7 | 2423 | ext4_msg(sb, KERN_ERR, "can't allocate mem " |
9d8b9ec4 | 2424 | "for a buddy group"); |
5f21b0e6 FB |
2425 | goto exit_meta_group_info; |
2426 | } | |
2427 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = | |
2428 | meta_group_info; | |
2429 | } | |
2430 | ||
5f21b0e6 FB |
2431 | meta_group_info = |
2432 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]; | |
2433 | i = group & (EXT4_DESC_PER_BLOCK(sb) - 1); | |
2434 | ||
4fdb5543 | 2435 | meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS); |
5f21b0e6 | 2436 | if (meta_group_info[i] == NULL) { |
7f6a11e7 | 2437 | ext4_msg(sb, KERN_ERR, "can't allocate buddy mem"); |
5f21b0e6 FB |
2438 | goto exit_group_info; |
2439 | } | |
2440 | set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, | |
2441 | &(meta_group_info[i]->bb_state)); | |
2442 | ||
2443 | /* | |
2444 | * initialize bb_free to be able to skip | |
2445 | * empty groups without initialization | |
2446 | */ | |
2447 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
2448 | meta_group_info[i]->bb_free = | |
cff1dfd7 | 2449 | ext4_free_clusters_after_init(sb, group, desc); |
5f21b0e6 FB |
2450 | } else { |
2451 | meta_group_info[i]->bb_free = | |
021b65bb | 2452 | ext4_free_group_clusters(sb, desc); |
5f21b0e6 FB |
2453 | } |
2454 | ||
2455 | INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list); | |
920313a7 | 2456 | init_rwsem(&meta_group_info[i]->alloc_sem); |
64e290ec | 2457 | meta_group_info[i]->bb_free_root = RB_ROOT; |
8a57d9d6 | 2458 | meta_group_info[i]->bb_largest_free_order = -1; /* uninit */ |
5f21b0e6 FB |
2459 | |
2460 | #ifdef DOUBLE_CHECK | |
2461 | { | |
2462 | struct buffer_head *bh; | |
2463 | meta_group_info[i]->bb_bitmap = | |
4fdb5543 | 2464 | kmalloc(sb->s_blocksize, GFP_NOFS); |
5f21b0e6 FB |
2465 | BUG_ON(meta_group_info[i]->bb_bitmap == NULL); |
2466 | bh = ext4_read_block_bitmap(sb, group); | |
9008a58e | 2467 | BUG_ON(IS_ERR_OR_NULL(bh)); |
5f21b0e6 FB |
2468 | memcpy(meta_group_info[i]->bb_bitmap, bh->b_data, |
2469 | sb->s_blocksize); | |
2470 | put_bh(bh); | |
2471 | } | |
2472 | #endif | |
2473 | ||
2474 | return 0; | |
2475 | ||
2476 | exit_group_info: | |
2477 | /* If a meta_group_info table has been allocated, release it now */ | |
caaf7a29 | 2478 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
5f21b0e6 | 2479 | kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]); |
caaf7a29 TM |
2480 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL; |
2481 | } | |
5f21b0e6 FB |
2482 | exit_meta_group_info: |
2483 | return -ENOMEM; | |
2484 | } /* ext4_mb_add_groupinfo */ | |
2485 | ||
c9de560d AT |
2486 | static int ext4_mb_init_backend(struct super_block *sb) |
2487 | { | |
8df9675f | 2488 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
c9de560d | 2489 | ext4_group_t i; |
c9de560d | 2490 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
28623c2f | 2491 | int err; |
5f21b0e6 | 2492 | struct ext4_group_desc *desc; |
fb1813f4 | 2493 | struct kmem_cache *cachep; |
5f21b0e6 | 2494 | |
28623c2f TT |
2495 | err = ext4_mb_alloc_groupinfo(sb, ngroups); |
2496 | if (err) | |
2497 | return err; | |
c9de560d | 2498 | |
c9de560d AT |
2499 | sbi->s_buddy_cache = new_inode(sb); |
2500 | if (sbi->s_buddy_cache == NULL) { | |
9d8b9ec4 | 2501 | ext4_msg(sb, KERN_ERR, "can't get new inode"); |
c9de560d AT |
2502 | goto err_freesgi; |
2503 | } | |
48e6061b YJ |
2504 | /* To avoid potentially colliding with an valid on-disk inode number, |
2505 | * use EXT4_BAD_INO for the buddy cache inode number. This inode is | |
2506 | * not in the inode hash, so it should never be found by iget(), but | |
2507 | * this will avoid confusion if it ever shows up during debugging. */ | |
2508 | sbi->s_buddy_cache->i_ino = EXT4_BAD_INO; | |
c9de560d | 2509 | EXT4_I(sbi->s_buddy_cache)->i_disksize = 0; |
8df9675f | 2510 | for (i = 0; i < ngroups; i++) { |
c9de560d AT |
2511 | desc = ext4_get_group_desc(sb, i, NULL); |
2512 | if (desc == NULL) { | |
9d8b9ec4 | 2513 | ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i); |
c9de560d AT |
2514 | goto err_freebuddy; |
2515 | } | |
5f21b0e6 FB |
2516 | if (ext4_mb_add_groupinfo(sb, i, desc) != 0) |
2517 | goto err_freebuddy; | |
c9de560d AT |
2518 | } |
2519 | ||
2520 | return 0; | |
2521 | ||
2522 | err_freebuddy: | |
fb1813f4 | 2523 | cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
f1fa3342 | 2524 | while (i-- > 0) |
fb1813f4 | 2525 | kmem_cache_free(cachep, ext4_get_group_info(sb, i)); |
28623c2f | 2526 | i = sbi->s_group_info_size; |
f1fa3342 | 2527 | while (i-- > 0) |
c9de560d AT |
2528 | kfree(sbi->s_group_info[i]); |
2529 | iput(sbi->s_buddy_cache); | |
2530 | err_freesgi: | |
b93b41d4 | 2531 | kvfree(sbi->s_group_info); |
c9de560d AT |
2532 | return -ENOMEM; |
2533 | } | |
2534 | ||
2892c15d ES |
2535 | static void ext4_groupinfo_destroy_slabs(void) |
2536 | { | |
2537 | int i; | |
2538 | ||
2539 | for (i = 0; i < NR_GRPINFO_CACHES; i++) { | |
21c580d8 | 2540 | kmem_cache_destroy(ext4_groupinfo_caches[i]); |
2892c15d ES |
2541 | ext4_groupinfo_caches[i] = NULL; |
2542 | } | |
2543 | } | |
2544 | ||
2545 | static int ext4_groupinfo_create_slab(size_t size) | |
2546 | { | |
2547 | static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex); | |
2548 | int slab_size; | |
2549 | int blocksize_bits = order_base_2(size); | |
2550 | int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; | |
2551 | struct kmem_cache *cachep; | |
2552 | ||
2553 | if (cache_index >= NR_GRPINFO_CACHES) | |
2554 | return -EINVAL; | |
2555 | ||
2556 | if (unlikely(cache_index < 0)) | |
2557 | cache_index = 0; | |
2558 | ||
2559 | mutex_lock(&ext4_grpinfo_slab_create_mutex); | |
2560 | if (ext4_groupinfo_caches[cache_index]) { | |
2561 | mutex_unlock(&ext4_grpinfo_slab_create_mutex); | |
2562 | return 0; /* Already created */ | |
2563 | } | |
2564 | ||
2565 | slab_size = offsetof(struct ext4_group_info, | |
2566 | bb_counters[blocksize_bits + 2]); | |
2567 | ||
2568 | cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index], | |
2569 | slab_size, 0, SLAB_RECLAIM_ACCOUNT, | |
2570 | NULL); | |
2571 | ||
823ba01f TM |
2572 | ext4_groupinfo_caches[cache_index] = cachep; |
2573 | ||
2892c15d ES |
2574 | mutex_unlock(&ext4_grpinfo_slab_create_mutex); |
2575 | if (!cachep) { | |
9d8b9ec4 TT |
2576 | printk(KERN_EMERG |
2577 | "EXT4-fs: no memory for groupinfo slab cache\n"); | |
2892c15d ES |
2578 | return -ENOMEM; |
2579 | } | |
2580 | ||
2892c15d ES |
2581 | return 0; |
2582 | } | |
2583 | ||
9d99012f | 2584 | int ext4_mb_init(struct super_block *sb) |
c9de560d AT |
2585 | { |
2586 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
6be2ded1 | 2587 | unsigned i, j; |
935244cd | 2588 | unsigned offset, offset_incr; |
c9de560d | 2589 | unsigned max; |
74767c5a | 2590 | int ret; |
c9de560d | 2591 | |
1927805e | 2592 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets); |
c9de560d AT |
2593 | |
2594 | sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL); | |
2595 | if (sbi->s_mb_offsets == NULL) { | |
fb1813f4 CW |
2596 | ret = -ENOMEM; |
2597 | goto out; | |
c9de560d | 2598 | } |
ff7ef329 | 2599 | |
1927805e | 2600 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs); |
c9de560d AT |
2601 | sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL); |
2602 | if (sbi->s_mb_maxs == NULL) { | |
fb1813f4 CW |
2603 | ret = -ENOMEM; |
2604 | goto out; | |
2605 | } | |
2606 | ||
2892c15d ES |
2607 | ret = ext4_groupinfo_create_slab(sb->s_blocksize); |
2608 | if (ret < 0) | |
2609 | goto out; | |
c9de560d AT |
2610 | |
2611 | /* order 0 is regular bitmap */ | |
2612 | sbi->s_mb_maxs[0] = sb->s_blocksize << 3; | |
2613 | sbi->s_mb_offsets[0] = 0; | |
2614 | ||
2615 | i = 1; | |
2616 | offset = 0; | |
935244cd | 2617 | offset_incr = 1 << (sb->s_blocksize_bits - 1); |
c9de560d AT |
2618 | max = sb->s_blocksize << 2; |
2619 | do { | |
2620 | sbi->s_mb_offsets[i] = offset; | |
2621 | sbi->s_mb_maxs[i] = max; | |
935244cd NS |
2622 | offset += offset_incr; |
2623 | offset_incr = offset_incr >> 1; | |
c9de560d AT |
2624 | max = max >> 1; |
2625 | i++; | |
2626 | } while (i <= sb->s_blocksize_bits + 1); | |
2627 | ||
c9de560d | 2628 | spin_lock_init(&sbi->s_md_lock); |
c9de560d | 2629 | spin_lock_init(&sbi->s_bal_lock); |
d08854f5 | 2630 | sbi->s_mb_free_pending = 0; |
a0154344 | 2631 | INIT_LIST_HEAD(&sbi->s_freed_data_list); |
c9de560d AT |
2632 | |
2633 | sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; | |
2634 | sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; | |
2635 | sbi->s_mb_stats = MB_DEFAULT_STATS; | |
2636 | sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD; | |
2637 | sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS; | |
27baebb8 TT |
2638 | /* |
2639 | * The default group preallocation is 512, which for 4k block | |
2640 | * sizes translates to 2 megabytes. However for bigalloc file | |
2641 | * systems, this is probably too big (i.e, if the cluster size | |
2642 | * is 1 megabyte, then group preallocation size becomes half a | |
2643 | * gigabyte!). As a default, we will keep a two megabyte | |
2644 | * group pralloc size for cluster sizes up to 64k, and after | |
2645 | * that, we will force a minimum group preallocation size of | |
2646 | * 32 clusters. This translates to 8 megs when the cluster | |
2647 | * size is 256k, and 32 megs when the cluster size is 1 meg, | |
2648 | * which seems reasonable as a default. | |
2649 | */ | |
2650 | sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >> | |
2651 | sbi->s_cluster_bits, 32); | |
d7a1fee1 DE |
2652 | /* |
2653 | * If there is a s_stripe > 1, then we set the s_mb_group_prealloc | |
2654 | * to the lowest multiple of s_stripe which is bigger than | |
2655 | * the s_mb_group_prealloc as determined above. We want | |
2656 | * the preallocation size to be an exact multiple of the | |
2657 | * RAID stripe size so that preallocations don't fragment | |
2658 | * the stripes. | |
2659 | */ | |
2660 | if (sbi->s_stripe > 1) { | |
2661 | sbi->s_mb_group_prealloc = roundup( | |
2662 | sbi->s_mb_group_prealloc, sbi->s_stripe); | |
2663 | } | |
c9de560d | 2664 | |
730c213c | 2665 | sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group); |
c9de560d | 2666 | if (sbi->s_locality_groups == NULL) { |
fb1813f4 | 2667 | ret = -ENOMEM; |
029b10c5 | 2668 | goto out; |
c9de560d | 2669 | } |
730c213c | 2670 | for_each_possible_cpu(i) { |
c9de560d | 2671 | struct ext4_locality_group *lg; |
730c213c | 2672 | lg = per_cpu_ptr(sbi->s_locality_groups, i); |
c9de560d | 2673 | mutex_init(&lg->lg_mutex); |
6be2ded1 AK |
2674 | for (j = 0; j < PREALLOC_TB_SIZE; j++) |
2675 | INIT_LIST_HEAD(&lg->lg_prealloc_list[j]); | |
c9de560d AT |
2676 | spin_lock_init(&lg->lg_prealloc_lock); |
2677 | } | |
2678 | ||
79a77c5a YJ |
2679 | /* init file for buddy data */ |
2680 | ret = ext4_mb_init_backend(sb); | |
7aa0baea TM |
2681 | if (ret != 0) |
2682 | goto out_free_locality_groups; | |
79a77c5a | 2683 | |
7aa0baea TM |
2684 | return 0; |
2685 | ||
2686 | out_free_locality_groups: | |
2687 | free_percpu(sbi->s_locality_groups); | |
2688 | sbi->s_locality_groups = NULL; | |
fb1813f4 | 2689 | out: |
7aa0baea TM |
2690 | kfree(sbi->s_mb_offsets); |
2691 | sbi->s_mb_offsets = NULL; | |
2692 | kfree(sbi->s_mb_maxs); | |
2693 | sbi->s_mb_maxs = NULL; | |
fb1813f4 | 2694 | return ret; |
c9de560d AT |
2695 | } |
2696 | ||
955ce5f5 | 2697 | /* need to called with the ext4 group lock held */ |
c9de560d AT |
2698 | static void ext4_mb_cleanup_pa(struct ext4_group_info *grp) |
2699 | { | |
2700 | struct ext4_prealloc_space *pa; | |
2701 | struct list_head *cur, *tmp; | |
2702 | int count = 0; | |
2703 | ||
2704 | list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) { | |
2705 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); | |
2706 | list_del(&pa->pa_group_list); | |
2707 | count++; | |
688f05a0 | 2708 | kmem_cache_free(ext4_pspace_cachep, pa); |
c9de560d AT |
2709 | } |
2710 | if (count) | |
6ba495e9 | 2711 | mb_debug(1, "mballoc: %u PAs left\n", count); |
c9de560d AT |
2712 | |
2713 | } | |
2714 | ||
2715 | int ext4_mb_release(struct super_block *sb) | |
2716 | { | |
8df9675f | 2717 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
c9de560d AT |
2718 | ext4_group_t i; |
2719 | int num_meta_group_infos; | |
2720 | struct ext4_group_info *grinfo; | |
2721 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
fb1813f4 | 2722 | struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
c9de560d | 2723 | |
c9de560d | 2724 | if (sbi->s_group_info) { |
8df9675f | 2725 | for (i = 0; i < ngroups; i++) { |
c9de560d AT |
2726 | grinfo = ext4_get_group_info(sb, i); |
2727 | #ifdef DOUBLE_CHECK | |
2728 | kfree(grinfo->bb_bitmap); | |
2729 | #endif | |
2730 | ext4_lock_group(sb, i); | |
2731 | ext4_mb_cleanup_pa(grinfo); | |
2732 | ext4_unlock_group(sb, i); | |
fb1813f4 | 2733 | kmem_cache_free(cachep, grinfo); |
c9de560d | 2734 | } |
8df9675f | 2735 | num_meta_group_infos = (ngroups + |
c9de560d AT |
2736 | EXT4_DESC_PER_BLOCK(sb) - 1) >> |
2737 | EXT4_DESC_PER_BLOCK_BITS(sb); | |
2738 | for (i = 0; i < num_meta_group_infos; i++) | |
2739 | kfree(sbi->s_group_info[i]); | |
b93b41d4 | 2740 | kvfree(sbi->s_group_info); |
c9de560d AT |
2741 | } |
2742 | kfree(sbi->s_mb_offsets); | |
2743 | kfree(sbi->s_mb_maxs); | |
bfcba2d0 | 2744 | iput(sbi->s_buddy_cache); |
c9de560d | 2745 | if (sbi->s_mb_stats) { |
9d8b9ec4 TT |
2746 | ext4_msg(sb, KERN_INFO, |
2747 | "mballoc: %u blocks %u reqs (%u success)", | |
c9de560d AT |
2748 | atomic_read(&sbi->s_bal_allocated), |
2749 | atomic_read(&sbi->s_bal_reqs), | |
2750 | atomic_read(&sbi->s_bal_success)); | |
9d8b9ec4 TT |
2751 | ext4_msg(sb, KERN_INFO, |
2752 | "mballoc: %u extents scanned, %u goal hits, " | |
2753 | "%u 2^N hits, %u breaks, %u lost", | |
c9de560d AT |
2754 | atomic_read(&sbi->s_bal_ex_scanned), |
2755 | atomic_read(&sbi->s_bal_goals), | |
2756 | atomic_read(&sbi->s_bal_2orders), | |
2757 | atomic_read(&sbi->s_bal_breaks), | |
2758 | atomic_read(&sbi->s_mb_lost_chunks)); | |
9d8b9ec4 TT |
2759 | ext4_msg(sb, KERN_INFO, |
2760 | "mballoc: %lu generated and it took %Lu", | |
ced156e4 | 2761 | sbi->s_mb_buddies_generated, |
c9de560d | 2762 | sbi->s_mb_generation_time); |
9d8b9ec4 TT |
2763 | ext4_msg(sb, KERN_INFO, |
2764 | "mballoc: %u preallocated, %u discarded", | |
c9de560d AT |
2765 | atomic_read(&sbi->s_mb_preallocated), |
2766 | atomic_read(&sbi->s_mb_discarded)); | |
2767 | } | |
2768 | ||
730c213c | 2769 | free_percpu(sbi->s_locality_groups); |
c9de560d AT |
2770 | |
2771 | return 0; | |
2772 | } | |
2773 | ||
77ca6cdf | 2774 | static inline int ext4_issue_discard(struct super_block *sb, |
a0154344 DJ |
2775 | ext4_group_t block_group, ext4_grpblk_t cluster, int count, |
2776 | struct bio **biop) | |
5c521830 | 2777 | { |
5c521830 JZ |
2778 | ext4_fsblk_t discard_block; |
2779 | ||
84130193 TT |
2780 | discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) + |
2781 | ext4_group_first_block_no(sb, block_group)); | |
2782 | count = EXT4_C2B(EXT4_SB(sb), count); | |
5c521830 JZ |
2783 | trace_ext4_discard_blocks(sb, |
2784 | (unsigned long long) discard_block, count); | |
a0154344 DJ |
2785 | if (biop) { |
2786 | return __blkdev_issue_discard(sb->s_bdev, | |
2787 | (sector_t)discard_block << (sb->s_blocksize_bits - 9), | |
2788 | (sector_t)count << (sb->s_blocksize_bits - 9), | |
2789 | GFP_NOFS, 0, biop); | |
2790 | } else | |
2791 | return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0); | |
5c521830 JZ |
2792 | } |
2793 | ||
a0154344 DJ |
2794 | static void ext4_free_data_in_buddy(struct super_block *sb, |
2795 | struct ext4_free_data *entry) | |
c9de560d | 2796 | { |
c9de560d | 2797 | struct ext4_buddy e4b; |
c894058d | 2798 | struct ext4_group_info *db; |
d9f34504 | 2799 | int err, count = 0, count2 = 0; |
c9de560d | 2800 | |
18aadd47 BJ |
2801 | mb_debug(1, "gonna free %u blocks in group %u (0x%p):", |
2802 | entry->efd_count, entry->efd_group, entry); | |
c9de560d | 2803 | |
18aadd47 BJ |
2804 | err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b); |
2805 | /* we expect to find existing buddy because it's pinned */ | |
2806 | BUG_ON(err != 0); | |
b90f6870 | 2807 | |
d08854f5 TT |
2808 | spin_lock(&EXT4_SB(sb)->s_md_lock); |
2809 | EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count; | |
2810 | spin_unlock(&EXT4_SB(sb)->s_md_lock); | |
c9de560d | 2811 | |
18aadd47 BJ |
2812 | db = e4b.bd_info; |
2813 | /* there are blocks to put in buddy to make them really free */ | |
2814 | count += entry->efd_count; | |
2815 | count2++; | |
2816 | ext4_lock_group(sb, entry->efd_group); | |
2817 | /* Take it out of per group rb tree */ | |
2818 | rb_erase(&entry->efd_node, &(db->bb_free_root)); | |
2819 | mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count); | |
c894058d | 2820 | |
18aadd47 BJ |
2821 | /* |
2822 | * Clear the trimmed flag for the group so that the next | |
2823 | * ext4_trim_fs can trim it. | |
2824 | * If the volume is mounted with -o discard, online discard | |
2825 | * is supported and the free blocks will be trimmed online. | |
2826 | */ | |
2827 | if (!test_opt(sb, DISCARD)) | |
2828 | EXT4_MB_GRP_CLEAR_TRIMMED(db); | |
3d56b8d2 | 2829 | |
18aadd47 BJ |
2830 | if (!db->bb_free_root.rb_node) { |
2831 | /* No more items in the per group rb tree | |
2832 | * balance refcounts from ext4_mb_free_metadata() | |
2833 | */ | |
09cbfeaf KS |
2834 | put_page(e4b.bd_buddy_page); |
2835 | put_page(e4b.bd_bitmap_page); | |
3e624fc7 | 2836 | } |
18aadd47 BJ |
2837 | ext4_unlock_group(sb, entry->efd_group); |
2838 | kmem_cache_free(ext4_free_data_cachep, entry); | |
2839 | ext4_mb_unload_buddy(&e4b); | |
c9de560d | 2840 | |
6ba495e9 | 2841 | mb_debug(1, "freed %u blocks in %u structures\n", count, count2); |
c9de560d AT |
2842 | } |
2843 | ||
a0154344 DJ |
2844 | /* |
2845 | * This function is called by the jbd2 layer once the commit has finished, | |
2846 | * so we know we can free the blocks that were released with that commit. | |
2847 | */ | |
2848 | void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid) | |
2849 | { | |
2850 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
2851 | struct ext4_free_data *entry, *tmp; | |
2852 | struct bio *discard_bio = NULL; | |
2853 | struct list_head freed_data_list; | |
2854 | struct list_head *cut_pos = NULL; | |
2855 | int err; | |
2856 | ||
2857 | INIT_LIST_HEAD(&freed_data_list); | |
2858 | ||
2859 | spin_lock(&sbi->s_md_lock); | |
2860 | list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) { | |
2861 | if (entry->efd_tid != commit_tid) | |
2862 | break; | |
2863 | cut_pos = &entry->efd_list; | |
2864 | } | |
2865 | if (cut_pos) | |
2866 | list_cut_position(&freed_data_list, &sbi->s_freed_data_list, | |
2867 | cut_pos); | |
2868 | spin_unlock(&sbi->s_md_lock); | |
2869 | ||
2870 | if (test_opt(sb, DISCARD)) { | |
2871 | list_for_each_entry(entry, &freed_data_list, efd_list) { | |
2872 | err = ext4_issue_discard(sb, entry->efd_group, | |
2873 | entry->efd_start_cluster, | |
2874 | entry->efd_count, | |
2875 | &discard_bio); | |
2876 | if (err && err != -EOPNOTSUPP) { | |
2877 | ext4_msg(sb, KERN_WARNING, "discard request in" | |
2878 | " group:%d block:%d count:%d failed" | |
2879 | " with %d", entry->efd_group, | |
2880 | entry->efd_start_cluster, | |
2881 | entry->efd_count, err); | |
2882 | } else if (err == -EOPNOTSUPP) | |
2883 | break; | |
2884 | } | |
2885 | ||
e4510577 | 2886 | if (discard_bio) { |
a0154344 | 2887 | submit_bio_wait(discard_bio); |
e4510577 DJ |
2888 | bio_put(discard_bio); |
2889 | } | |
a0154344 DJ |
2890 | } |
2891 | ||
2892 | list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list) | |
2893 | ext4_free_data_in_buddy(sb, entry); | |
2894 | } | |
2895 | ||
5dabfc78 | 2896 | int __init ext4_init_mballoc(void) |
c9de560d | 2897 | { |
16828088 TT |
2898 | ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space, |
2899 | SLAB_RECLAIM_ACCOUNT); | |
c9de560d AT |
2900 | if (ext4_pspace_cachep == NULL) |
2901 | return -ENOMEM; | |
2902 | ||
16828088 TT |
2903 | ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context, |
2904 | SLAB_RECLAIM_ACCOUNT); | |
256bdb49 ES |
2905 | if (ext4_ac_cachep == NULL) { |
2906 | kmem_cache_destroy(ext4_pspace_cachep); | |
2907 | return -ENOMEM; | |
2908 | } | |
c894058d | 2909 | |
18aadd47 BJ |
2910 | ext4_free_data_cachep = KMEM_CACHE(ext4_free_data, |
2911 | SLAB_RECLAIM_ACCOUNT); | |
2912 | if (ext4_free_data_cachep == NULL) { | |
c894058d AK |
2913 | kmem_cache_destroy(ext4_pspace_cachep); |
2914 | kmem_cache_destroy(ext4_ac_cachep); | |
2915 | return -ENOMEM; | |
2916 | } | |
c9de560d AT |
2917 | return 0; |
2918 | } | |
2919 | ||
5dabfc78 | 2920 | void ext4_exit_mballoc(void) |
c9de560d | 2921 | { |
60e6679e | 2922 | /* |
3e03f9ca JDB |
2923 | * Wait for completion of call_rcu()'s on ext4_pspace_cachep |
2924 | * before destroying the slab cache. | |
2925 | */ | |
2926 | rcu_barrier(); | |
c9de560d | 2927 | kmem_cache_destroy(ext4_pspace_cachep); |
256bdb49 | 2928 | kmem_cache_destroy(ext4_ac_cachep); |
18aadd47 | 2929 | kmem_cache_destroy(ext4_free_data_cachep); |
2892c15d | 2930 | ext4_groupinfo_destroy_slabs(); |
c9de560d AT |
2931 | } |
2932 | ||
2933 | ||
2934 | /* | |
73b2c716 | 2935 | * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps |
c9de560d AT |
2936 | * Returns 0 if success or error code |
2937 | */ | |
4ddfef7b ES |
2938 | static noinline_for_stack int |
2939 | ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, | |
53accfa9 | 2940 | handle_t *handle, unsigned int reserv_clstrs) |
c9de560d AT |
2941 | { |
2942 | struct buffer_head *bitmap_bh = NULL; | |
c9de560d AT |
2943 | struct ext4_group_desc *gdp; |
2944 | struct buffer_head *gdp_bh; | |
2945 | struct ext4_sb_info *sbi; | |
2946 | struct super_block *sb; | |
2947 | ext4_fsblk_t block; | |
519deca0 | 2948 | int err, len; |
c9de560d AT |
2949 | |
2950 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); | |
2951 | BUG_ON(ac->ac_b_ex.fe_len <= 0); | |
2952 | ||
2953 | sb = ac->ac_sb; | |
2954 | sbi = EXT4_SB(sb); | |
c9de560d | 2955 | |
574ca174 | 2956 | bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group); |
9008a58e DW |
2957 | if (IS_ERR(bitmap_bh)) { |
2958 | err = PTR_ERR(bitmap_bh); | |
2959 | bitmap_bh = NULL; | |
c9de560d | 2960 | goto out_err; |
9008a58e | 2961 | } |
c9de560d | 2962 | |
5d601255 | 2963 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
c9de560d AT |
2964 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
2965 | if (err) | |
2966 | goto out_err; | |
2967 | ||
2968 | err = -EIO; | |
2969 | gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh); | |
2970 | if (!gdp) | |
2971 | goto out_err; | |
2972 | ||
a9df9a49 | 2973 | ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group, |
021b65bb | 2974 | ext4_free_group_clusters(sb, gdp)); |
03cddb80 | 2975 | |
5d601255 | 2976 | BUFFER_TRACE(gdp_bh, "get_write_access"); |
c9de560d AT |
2977 | err = ext4_journal_get_write_access(handle, gdp_bh); |
2978 | if (err) | |
2979 | goto out_err; | |
2980 | ||
bda00de7 | 2981 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
c9de560d | 2982 | |
53accfa9 | 2983 | len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
6fd058f7 | 2984 | if (!ext4_data_block_valid(sbi, block, len)) { |
12062ddd | 2985 | ext4_error(sb, "Allocating blocks %llu-%llu which overlap " |
1084f252 | 2986 | "fs metadata", block, block+len); |
519deca0 | 2987 | /* File system mounted not to panic on error |
554a5ccc | 2988 | * Fix the bitmap and return EFSCORRUPTED |
519deca0 AK |
2989 | * We leak some of the blocks here. |
2990 | */ | |
955ce5f5 | 2991 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
c3e94d1d YY |
2992 | ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
2993 | ac->ac_b_ex.fe_len); | |
955ce5f5 | 2994 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
0390131b | 2995 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
519deca0 | 2996 | if (!err) |
554a5ccc | 2997 | err = -EFSCORRUPTED; |
519deca0 | 2998 | goto out_err; |
c9de560d | 2999 | } |
955ce5f5 AK |
3000 | |
3001 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); | |
c9de560d AT |
3002 | #ifdef AGGRESSIVE_CHECK |
3003 | { | |
3004 | int i; | |
3005 | for (i = 0; i < ac->ac_b_ex.fe_len; i++) { | |
3006 | BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i, | |
3007 | bitmap_bh->b_data)); | |
3008 | } | |
3009 | } | |
3010 | #endif | |
c3e94d1d YY |
3011 | ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
3012 | ac->ac_b_ex.fe_len); | |
c9de560d AT |
3013 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
3014 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); | |
021b65bb | 3015 | ext4_free_group_clusters_set(sb, gdp, |
cff1dfd7 | 3016 | ext4_free_clusters_after_init(sb, |
021b65bb | 3017 | ac->ac_b_ex.fe_group, gdp)); |
c9de560d | 3018 | } |
021b65bb TT |
3019 | len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len; |
3020 | ext4_free_group_clusters_set(sb, gdp, len); | |
79f1ba49 | 3021 | ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh); |
feb0ab32 | 3022 | ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp); |
955ce5f5 AK |
3023 | |
3024 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); | |
57042651 | 3025 | percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len); |
d2a17637 | 3026 | /* |
6bc6e63f | 3027 | * Now reduce the dirty block count also. Should not go negative |
d2a17637 | 3028 | */ |
6bc6e63f AK |
3029 | if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED)) |
3030 | /* release all the reserved blocks if non delalloc */ | |
57042651 TT |
3031 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
3032 | reserv_clstrs); | |
c9de560d | 3033 | |
772cb7c8 JS |
3034 | if (sbi->s_log_groups_per_flex) { |
3035 | ext4_group_t flex_group = ext4_flex_group(sbi, | |
3036 | ac->ac_b_ex.fe_group); | |
90ba983f TT |
3037 | atomic64_sub(ac->ac_b_ex.fe_len, |
3038 | &sbi->s_flex_groups[flex_group].free_clusters); | |
772cb7c8 JS |
3039 | } |
3040 | ||
0390131b | 3041 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
c9de560d AT |
3042 | if (err) |
3043 | goto out_err; | |
0390131b | 3044 | err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); |
c9de560d AT |
3045 | |
3046 | out_err: | |
42a10add | 3047 | brelse(bitmap_bh); |
c9de560d AT |
3048 | return err; |
3049 | } | |
3050 | ||
3051 | /* | |
3052 | * here we normalize request for locality group | |
d7a1fee1 DE |
3053 | * Group request are normalized to s_mb_group_prealloc, which goes to |
3054 | * s_strip if we set the same via mount option. | |
3055 | * s_mb_group_prealloc can be configured via | |
b713a5ec | 3056 | * /sys/fs/ext4/<partition>/mb_group_prealloc |
c9de560d AT |
3057 | * |
3058 | * XXX: should we try to preallocate more than the group has now? | |
3059 | */ | |
3060 | static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac) | |
3061 | { | |
3062 | struct super_block *sb = ac->ac_sb; | |
3063 | struct ext4_locality_group *lg = ac->ac_lg; | |
3064 | ||
3065 | BUG_ON(lg == NULL); | |
d7a1fee1 | 3066 | ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc; |
6ba495e9 | 3067 | mb_debug(1, "#%u: goal %u blocks for locality group\n", |
c9de560d AT |
3068 | current->pid, ac->ac_g_ex.fe_len); |
3069 | } | |
3070 | ||
3071 | /* | |
3072 | * Normalization means making request better in terms of | |
3073 | * size and alignment | |
3074 | */ | |
4ddfef7b ES |
3075 | static noinline_for_stack void |
3076 | ext4_mb_normalize_request(struct ext4_allocation_context *ac, | |
c9de560d AT |
3077 | struct ext4_allocation_request *ar) |
3078 | { | |
53accfa9 | 3079 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
c9de560d AT |
3080 | int bsbits, max; |
3081 | ext4_lblk_t end; | |
1592d2c5 CW |
3082 | loff_t size, start_off; |
3083 | loff_t orig_size __maybe_unused; | |
5a0790c2 | 3084 | ext4_lblk_t start; |
c9de560d | 3085 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
9a0762c5 | 3086 | struct ext4_prealloc_space *pa; |
c9de560d AT |
3087 | |
3088 | /* do normalize only data requests, metadata requests | |
3089 | do not need preallocation */ | |
3090 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) | |
3091 | return; | |
3092 | ||
3093 | /* sometime caller may want exact blocks */ | |
3094 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) | |
3095 | return; | |
3096 | ||
3097 | /* caller may indicate that preallocation isn't | |
3098 | * required (it's a tail, for example) */ | |
3099 | if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) | |
3100 | return; | |
3101 | ||
3102 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { | |
3103 | ext4_mb_normalize_group_request(ac); | |
3104 | return ; | |
3105 | } | |
3106 | ||
3107 | bsbits = ac->ac_sb->s_blocksize_bits; | |
3108 | ||
3109 | /* first, let's learn actual file size | |
3110 | * given current request is allocated */ | |
53accfa9 | 3111 | size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len); |
c9de560d AT |
3112 | size = size << bsbits; |
3113 | if (size < i_size_read(ac->ac_inode)) | |
3114 | size = i_size_read(ac->ac_inode); | |
5a0790c2 | 3115 | orig_size = size; |
c9de560d | 3116 | |
1930479c VC |
3117 | /* max size of free chunks */ |
3118 | max = 2 << bsbits; | |
c9de560d | 3119 | |
1930479c VC |
3120 | #define NRL_CHECK_SIZE(req, size, max, chunk_size) \ |
3121 | (req <= (size) || max <= (chunk_size)) | |
c9de560d AT |
3122 | |
3123 | /* first, try to predict filesize */ | |
3124 | /* XXX: should this table be tunable? */ | |
3125 | start_off = 0; | |
3126 | if (size <= 16 * 1024) { | |
3127 | size = 16 * 1024; | |
3128 | } else if (size <= 32 * 1024) { | |
3129 | size = 32 * 1024; | |
3130 | } else if (size <= 64 * 1024) { | |
3131 | size = 64 * 1024; | |
3132 | } else if (size <= 128 * 1024) { | |
3133 | size = 128 * 1024; | |
3134 | } else if (size <= 256 * 1024) { | |
3135 | size = 256 * 1024; | |
3136 | } else if (size <= 512 * 1024) { | |
3137 | size = 512 * 1024; | |
3138 | } else if (size <= 1024 * 1024) { | |
3139 | size = 1024 * 1024; | |
1930479c | 3140 | } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) { |
c9de560d | 3141 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
1930479c VC |
3142 | (21 - bsbits)) << 21; |
3143 | size = 2 * 1024 * 1024; | |
3144 | } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) { | |
c9de560d AT |
3145 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
3146 | (22 - bsbits)) << 22; | |
3147 | size = 4 * 1024 * 1024; | |
3148 | } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len, | |
1930479c | 3149 | (8<<20)>>bsbits, max, 8 * 1024)) { |
c9de560d AT |
3150 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
3151 | (23 - bsbits)) << 23; | |
3152 | size = 8 * 1024 * 1024; | |
3153 | } else { | |
b27b1535 XW |
3154 | start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits; |
3155 | size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb), | |
3156 | ac->ac_o_ex.fe_len) << bsbits; | |
c9de560d | 3157 | } |
5a0790c2 AK |
3158 | size = size >> bsbits; |
3159 | start = start_off >> bsbits; | |
c9de560d AT |
3160 | |
3161 | /* don't cover already allocated blocks in selected range */ | |
3162 | if (ar->pleft && start <= ar->lleft) { | |
3163 | size -= ar->lleft + 1 - start; | |
3164 | start = ar->lleft + 1; | |
3165 | } | |
3166 | if (ar->pright && start + size - 1 >= ar->lright) | |
3167 | size -= start + size - ar->lright; | |
3168 | ||
cd648b8a JK |
3169 | /* |
3170 | * Trim allocation request for filesystems with artificially small | |
3171 | * groups. | |
3172 | */ | |
3173 | if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) | |
3174 | size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb); | |
3175 | ||
c9de560d AT |
3176 | end = start + size; |
3177 | ||
3178 | /* check we don't cross already preallocated blocks */ | |
3179 | rcu_read_lock(); | |
9a0762c5 | 3180 | list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) { |
498e5f24 | 3181 | ext4_lblk_t pa_end; |
c9de560d | 3182 | |
c9de560d AT |
3183 | if (pa->pa_deleted) |
3184 | continue; | |
3185 | spin_lock(&pa->pa_lock); | |
3186 | if (pa->pa_deleted) { | |
3187 | spin_unlock(&pa->pa_lock); | |
3188 | continue; | |
3189 | } | |
3190 | ||
53accfa9 TT |
3191 | pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb), |
3192 | pa->pa_len); | |
c9de560d AT |
3193 | |
3194 | /* PA must not overlap original request */ | |
3195 | BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end || | |
3196 | ac->ac_o_ex.fe_logical < pa->pa_lstart)); | |
3197 | ||
38877f4e ES |
3198 | /* skip PAs this normalized request doesn't overlap with */ |
3199 | if (pa->pa_lstart >= end || pa_end <= start) { | |
c9de560d AT |
3200 | spin_unlock(&pa->pa_lock); |
3201 | continue; | |
3202 | } | |
3203 | BUG_ON(pa->pa_lstart <= start && pa_end >= end); | |
3204 | ||
38877f4e | 3205 | /* adjust start or end to be adjacent to this pa */ |
c9de560d AT |
3206 | if (pa_end <= ac->ac_o_ex.fe_logical) { |
3207 | BUG_ON(pa_end < start); | |
3208 | start = pa_end; | |
38877f4e | 3209 | } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) { |
c9de560d AT |
3210 | BUG_ON(pa->pa_lstart > end); |
3211 | end = pa->pa_lstart; | |
3212 | } | |
3213 | spin_unlock(&pa->pa_lock); | |
3214 | } | |
3215 | rcu_read_unlock(); | |
3216 | size = end - start; | |
3217 | ||
3218 | /* XXX: extra loop to check we really don't overlap preallocations */ | |
3219 | rcu_read_lock(); | |
9a0762c5 | 3220 | list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) { |
498e5f24 | 3221 | ext4_lblk_t pa_end; |
53accfa9 | 3222 | |
c9de560d AT |
3223 | spin_lock(&pa->pa_lock); |
3224 | if (pa->pa_deleted == 0) { | |
53accfa9 TT |
3225 | pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb), |
3226 | pa->pa_len); | |
c9de560d AT |
3227 | BUG_ON(!(start >= pa_end || end <= pa->pa_lstart)); |
3228 | } | |
3229 | spin_unlock(&pa->pa_lock); | |
3230 | } | |
3231 | rcu_read_unlock(); | |
3232 | ||
3233 | if (start + size <= ac->ac_o_ex.fe_logical && | |
3234 | start > ac->ac_o_ex.fe_logical) { | |
9d8b9ec4 TT |
3235 | ext4_msg(ac->ac_sb, KERN_ERR, |
3236 | "start %lu, size %lu, fe_logical %lu", | |
3237 | (unsigned long) start, (unsigned long) size, | |
3238 | (unsigned long) ac->ac_o_ex.fe_logical); | |
dfe076c1 | 3239 | BUG(); |
c9de560d | 3240 | } |
b5b60778 | 3241 | BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
c9de560d AT |
3242 | |
3243 | /* now prepare goal request */ | |
3244 | ||
3245 | /* XXX: is it better to align blocks WRT to logical | |
3246 | * placement or satisfy big request as is */ | |
3247 | ac->ac_g_ex.fe_logical = start; | |
53accfa9 | 3248 | ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size); |
c9de560d AT |
3249 | |
3250 | /* define goal start in order to merge */ | |
3251 | if (ar->pright && (ar->lright == (start + size))) { | |
3252 | /* merge to the right */ | |
3253 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size, | |
3254 | &ac->ac_f_ex.fe_group, | |
3255 | &ac->ac_f_ex.fe_start); | |
3256 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; | |
3257 | } | |
3258 | if (ar->pleft && (ar->lleft + 1 == start)) { | |
3259 | /* merge to the left */ | |
3260 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1, | |
3261 | &ac->ac_f_ex.fe_group, | |
3262 | &ac->ac_f_ex.fe_start); | |
3263 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; | |
3264 | } | |
3265 | ||
6ba495e9 | 3266 | mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size, |
c9de560d AT |
3267 | (unsigned) orig_size, (unsigned) start); |
3268 | } | |
3269 | ||
3270 | static void ext4_mb_collect_stats(struct ext4_allocation_context *ac) | |
3271 | { | |
3272 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); | |
3273 | ||
3274 | if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) { | |
3275 | atomic_inc(&sbi->s_bal_reqs); | |
3276 | atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); | |
291dae47 | 3277 | if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len) |
c9de560d AT |
3278 | atomic_inc(&sbi->s_bal_success); |
3279 | atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned); | |
3280 | if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && | |
3281 | ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) | |
3282 | atomic_inc(&sbi->s_bal_goals); | |
3283 | if (ac->ac_found > sbi->s_mb_max_to_scan) | |
3284 | atomic_inc(&sbi->s_bal_breaks); | |
3285 | } | |
3286 | ||
296c355c TT |
3287 | if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) |
3288 | trace_ext4_mballoc_alloc(ac); | |
3289 | else | |
3290 | trace_ext4_mballoc_prealloc(ac); | |
c9de560d AT |
3291 | } |
3292 | ||
b844167e CW |
3293 | /* |
3294 | * Called on failure; free up any blocks from the inode PA for this | |
3295 | * context. We don't need this for MB_GROUP_PA because we only change | |
3296 | * pa_free in ext4_mb_release_context(), but on failure, we've already | |
3297 | * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed. | |
3298 | */ | |
3299 | static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac) | |
3300 | { | |
3301 | struct ext4_prealloc_space *pa = ac->ac_pa; | |
86f0afd4 TT |
3302 | struct ext4_buddy e4b; |
3303 | int err; | |
b844167e | 3304 | |
86f0afd4 | 3305 | if (pa == NULL) { |
c99d1e6e TT |
3306 | if (ac->ac_f_ex.fe_len == 0) |
3307 | return; | |
86f0afd4 TT |
3308 | err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b); |
3309 | if (err) { | |
3310 | /* | |
3311 | * This should never happen since we pin the | |
3312 | * pages in the ext4_allocation_context so | |
3313 | * ext4_mb_load_buddy() should never fail. | |
3314 | */ | |
3315 | WARN(1, "mb_load_buddy failed (%d)", err); | |
3316 | return; | |
3317 | } | |
3318 | ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group); | |
3319 | mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start, | |
3320 | ac->ac_f_ex.fe_len); | |
3321 | ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group); | |
c99d1e6e | 3322 | ext4_mb_unload_buddy(&e4b); |
86f0afd4 TT |
3323 | return; |
3324 | } | |
3325 | if (pa->pa_type == MB_INODE_PA) | |
400db9d3 | 3326 | pa->pa_free += ac->ac_b_ex.fe_len; |
b844167e CW |
3327 | } |
3328 | ||
c9de560d AT |
3329 | /* |
3330 | * use blocks preallocated to inode | |
3331 | */ | |
3332 | static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac, | |
3333 | struct ext4_prealloc_space *pa) | |
3334 | { | |
53accfa9 | 3335 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
c9de560d AT |
3336 | ext4_fsblk_t start; |
3337 | ext4_fsblk_t end; | |
3338 | int len; | |
3339 | ||
3340 | /* found preallocated blocks, use them */ | |
3341 | start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart); | |
53accfa9 TT |
3342 | end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len), |
3343 | start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len)); | |
3344 | len = EXT4_NUM_B2C(sbi, end - start); | |
c9de560d AT |
3345 | ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group, |
3346 | &ac->ac_b_ex.fe_start); | |
3347 | ac->ac_b_ex.fe_len = len; | |
3348 | ac->ac_status = AC_STATUS_FOUND; | |
3349 | ac->ac_pa = pa; | |
3350 | ||
3351 | BUG_ON(start < pa->pa_pstart); | |
53accfa9 | 3352 | BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len)); |
c9de560d AT |
3353 | BUG_ON(pa->pa_free < len); |
3354 | pa->pa_free -= len; | |
3355 | ||
6ba495e9 | 3356 | mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa); |
c9de560d AT |
3357 | } |
3358 | ||
3359 | /* | |
3360 | * use blocks preallocated to locality group | |
3361 | */ | |
3362 | static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac, | |
3363 | struct ext4_prealloc_space *pa) | |
3364 | { | |
03cddb80 | 3365 | unsigned int len = ac->ac_o_ex.fe_len; |
6be2ded1 | 3366 | |
c9de560d AT |
3367 | ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart, |
3368 | &ac->ac_b_ex.fe_group, | |
3369 | &ac->ac_b_ex.fe_start); | |
3370 | ac->ac_b_ex.fe_len = len; | |
3371 | ac->ac_status = AC_STATUS_FOUND; | |
3372 | ac->ac_pa = pa; | |
3373 | ||
3374 | /* we don't correct pa_pstart or pa_plen here to avoid | |
26346ff6 | 3375 | * possible race when the group is being loaded concurrently |
c9de560d | 3376 | * instead we correct pa later, after blocks are marked |
26346ff6 AK |
3377 | * in on-disk bitmap -- see ext4_mb_release_context() |
3378 | * Other CPUs are prevented from allocating from this pa by lg_mutex | |
c9de560d | 3379 | */ |
6ba495e9 | 3380 | mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa); |
c9de560d AT |
3381 | } |
3382 | ||
5e745b04 AK |
3383 | /* |
3384 | * Return the prealloc space that have minimal distance | |
3385 | * from the goal block. @cpa is the prealloc | |
3386 | * space that is having currently known minimal distance | |
3387 | * from the goal block. | |
3388 | */ | |
3389 | static struct ext4_prealloc_space * | |
3390 | ext4_mb_check_group_pa(ext4_fsblk_t goal_block, | |
3391 | struct ext4_prealloc_space *pa, | |
3392 | struct ext4_prealloc_space *cpa) | |
3393 | { | |
3394 | ext4_fsblk_t cur_distance, new_distance; | |
3395 | ||
3396 | if (cpa == NULL) { | |
3397 | atomic_inc(&pa->pa_count); | |
3398 | return pa; | |
3399 | } | |
79211c8e AM |
3400 | cur_distance = abs(goal_block - cpa->pa_pstart); |
3401 | new_distance = abs(goal_block - pa->pa_pstart); | |
5e745b04 | 3402 | |
5a54b2f1 | 3403 | if (cur_distance <= new_distance) |
5e745b04 AK |
3404 | return cpa; |
3405 | ||
3406 | /* drop the previous reference */ | |
3407 | atomic_dec(&cpa->pa_count); | |
3408 | atomic_inc(&pa->pa_count); | |
3409 | return pa; | |
3410 | } | |
3411 | ||
c9de560d AT |
3412 | /* |
3413 | * search goal blocks in preallocated space | |
3414 | */ | |
4ddfef7b ES |
3415 | static noinline_for_stack int |
3416 | ext4_mb_use_preallocated(struct ext4_allocation_context *ac) | |
c9de560d | 3417 | { |
53accfa9 | 3418 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
6be2ded1 | 3419 | int order, i; |
c9de560d AT |
3420 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
3421 | struct ext4_locality_group *lg; | |
5e745b04 AK |
3422 | struct ext4_prealloc_space *pa, *cpa = NULL; |
3423 | ext4_fsblk_t goal_block; | |
c9de560d AT |
3424 | |
3425 | /* only data can be preallocated */ | |
3426 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) | |
3427 | return 0; | |
3428 | ||
3429 | /* first, try per-file preallocation */ | |
3430 | rcu_read_lock(); | |
9a0762c5 | 3431 | list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) { |
c9de560d AT |
3432 | |
3433 | /* all fields in this condition don't change, | |
3434 | * so we can skip locking for them */ | |
3435 | if (ac->ac_o_ex.fe_logical < pa->pa_lstart || | |
53accfa9 TT |
3436 | ac->ac_o_ex.fe_logical >= (pa->pa_lstart + |
3437 | EXT4_C2B(sbi, pa->pa_len))) | |
c9de560d AT |
3438 | continue; |
3439 | ||
fb0a387d | 3440 | /* non-extent files can't have physical blocks past 2^32 */ |
12e9b892 | 3441 | if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) && |
53accfa9 TT |
3442 | (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) > |
3443 | EXT4_MAX_BLOCK_FILE_PHYS)) | |
fb0a387d ES |
3444 | continue; |
3445 | ||
c9de560d AT |
3446 | /* found preallocated blocks, use them */ |
3447 | spin_lock(&pa->pa_lock); | |
3448 | if (pa->pa_deleted == 0 && pa->pa_free) { | |
3449 | atomic_inc(&pa->pa_count); | |
3450 | ext4_mb_use_inode_pa(ac, pa); | |
3451 | spin_unlock(&pa->pa_lock); | |
3452 | ac->ac_criteria = 10; | |
3453 | rcu_read_unlock(); | |
3454 | return 1; | |
3455 | } | |
3456 | spin_unlock(&pa->pa_lock); | |
3457 | } | |
3458 | rcu_read_unlock(); | |
3459 | ||
3460 | /* can we use group allocation? */ | |
3461 | if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)) | |
3462 | return 0; | |
3463 | ||
3464 | /* inode may have no locality group for some reason */ | |
3465 | lg = ac->ac_lg; | |
3466 | if (lg == NULL) | |
3467 | return 0; | |
6be2ded1 AK |
3468 | order = fls(ac->ac_o_ex.fe_len) - 1; |
3469 | if (order > PREALLOC_TB_SIZE - 1) | |
3470 | /* The max size of hash table is PREALLOC_TB_SIZE */ | |
3471 | order = PREALLOC_TB_SIZE - 1; | |
3472 | ||
bda00de7 | 3473 | goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex); |
5e745b04 AK |
3474 | /* |
3475 | * search for the prealloc space that is having | |
3476 | * minimal distance from the goal block. | |
3477 | */ | |
6be2ded1 AK |
3478 | for (i = order; i < PREALLOC_TB_SIZE; i++) { |
3479 | rcu_read_lock(); | |
3480 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i], | |
3481 | pa_inode_list) { | |
3482 | spin_lock(&pa->pa_lock); | |
3483 | if (pa->pa_deleted == 0 && | |
3484 | pa->pa_free >= ac->ac_o_ex.fe_len) { | |
5e745b04 AK |
3485 | |
3486 | cpa = ext4_mb_check_group_pa(goal_block, | |
3487 | pa, cpa); | |
6be2ded1 | 3488 | } |
c9de560d | 3489 | spin_unlock(&pa->pa_lock); |
c9de560d | 3490 | } |
6be2ded1 | 3491 | rcu_read_unlock(); |
c9de560d | 3492 | } |
5e745b04 AK |
3493 | if (cpa) { |
3494 | ext4_mb_use_group_pa(ac, cpa); | |
3495 | ac->ac_criteria = 20; | |
3496 | return 1; | |
3497 | } | |
c9de560d AT |
3498 | return 0; |
3499 | } | |
3500 | ||
7a2fcbf7 AK |
3501 | /* |
3502 | * the function goes through all block freed in the group | |
3503 | * but not yet committed and marks them used in in-core bitmap. | |
3504 | * buddy must be generated from this bitmap | |
955ce5f5 | 3505 | * Need to be called with the ext4 group lock held |
7a2fcbf7 AK |
3506 | */ |
3507 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, | |
3508 | ext4_group_t group) | |
3509 | { | |
3510 | struct rb_node *n; | |
3511 | struct ext4_group_info *grp; | |
3512 | struct ext4_free_data *entry; | |
3513 | ||
3514 | grp = ext4_get_group_info(sb, group); | |
3515 | n = rb_first(&(grp->bb_free_root)); | |
3516 | ||
3517 | while (n) { | |
18aadd47 BJ |
3518 | entry = rb_entry(n, struct ext4_free_data, efd_node); |
3519 | ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count); | |
7a2fcbf7 AK |
3520 | n = rb_next(n); |
3521 | } | |
3522 | return; | |
3523 | } | |
3524 | ||
c9de560d AT |
3525 | /* |
3526 | * the function goes through all preallocation in this group and marks them | |
3527 | * used in in-core bitmap. buddy must be generated from this bitmap | |
955ce5f5 | 3528 | * Need to be called with ext4 group lock held |
c9de560d | 3529 | */ |
089ceecc ES |
3530 | static noinline_for_stack |
3531 | void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, | |
c9de560d AT |
3532 | ext4_group_t group) |
3533 | { | |
3534 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
3535 | struct ext4_prealloc_space *pa; | |
3536 | struct list_head *cur; | |
3537 | ext4_group_t groupnr; | |
3538 | ext4_grpblk_t start; | |
3539 | int preallocated = 0; | |
c9de560d AT |
3540 | int len; |
3541 | ||
3542 | /* all form of preallocation discards first load group, | |
3543 | * so the only competing code is preallocation use. | |
3544 | * we don't need any locking here | |
3545 | * notice we do NOT ignore preallocations with pa_deleted | |
3546 | * otherwise we could leave used blocks available for | |
3547 | * allocation in buddy when concurrent ext4_mb_put_pa() | |
3548 | * is dropping preallocation | |
3549 | */ | |
3550 | list_for_each(cur, &grp->bb_prealloc_list) { | |
3551 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); | |
3552 | spin_lock(&pa->pa_lock); | |
3553 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, | |
3554 | &groupnr, &start); | |
3555 | len = pa->pa_len; | |
3556 | spin_unlock(&pa->pa_lock); | |
3557 | if (unlikely(len == 0)) | |
3558 | continue; | |
3559 | BUG_ON(groupnr != group); | |
c3e94d1d | 3560 | ext4_set_bits(bitmap, start, len); |
c9de560d | 3561 | preallocated += len; |
c9de560d | 3562 | } |
ff950156 | 3563 | mb_debug(1, "preallocated %u for group %u\n", preallocated, group); |
c9de560d AT |
3564 | } |
3565 | ||
3566 | static void ext4_mb_pa_callback(struct rcu_head *head) | |
3567 | { | |
3568 | struct ext4_prealloc_space *pa; | |
3569 | pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu); | |
4e8d2139 JR |
3570 | |
3571 | BUG_ON(atomic_read(&pa->pa_count)); | |
3572 | BUG_ON(pa->pa_deleted == 0); | |
c9de560d AT |
3573 | kmem_cache_free(ext4_pspace_cachep, pa); |
3574 | } | |
3575 | ||
3576 | /* | |
3577 | * drops a reference to preallocated space descriptor | |
3578 | * if this was the last reference and the space is consumed | |
3579 | */ | |
3580 | static void ext4_mb_put_pa(struct ext4_allocation_context *ac, | |
3581 | struct super_block *sb, struct ext4_prealloc_space *pa) | |
3582 | { | |
a9df9a49 | 3583 | ext4_group_t grp; |
d33a1976 | 3584 | ext4_fsblk_t grp_blk; |
c9de560d | 3585 | |
c9de560d AT |
3586 | /* in this short window concurrent discard can set pa_deleted */ |
3587 | spin_lock(&pa->pa_lock); | |
4e8d2139 JR |
3588 | if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) { |
3589 | spin_unlock(&pa->pa_lock); | |
3590 | return; | |
3591 | } | |
3592 | ||
c9de560d AT |
3593 | if (pa->pa_deleted == 1) { |
3594 | spin_unlock(&pa->pa_lock); | |
3595 | return; | |
3596 | } | |
3597 | ||
3598 | pa->pa_deleted = 1; | |
3599 | spin_unlock(&pa->pa_lock); | |
3600 | ||
d33a1976 | 3601 | grp_blk = pa->pa_pstart; |
60e6679e | 3602 | /* |
cc0fb9ad AK |
3603 | * If doing group-based preallocation, pa_pstart may be in the |
3604 | * next group when pa is used up | |
3605 | */ | |
3606 | if (pa->pa_type == MB_GROUP_PA) | |
d33a1976 ES |
3607 | grp_blk--; |
3608 | ||
bd86298e | 3609 | grp = ext4_get_group_number(sb, grp_blk); |
c9de560d AT |
3610 | |
3611 | /* | |
3612 | * possible race: | |
3613 | * | |
3614 | * P1 (buddy init) P2 (regular allocation) | |
3615 | * find block B in PA | |
3616 | * copy on-disk bitmap to buddy | |
3617 | * mark B in on-disk bitmap | |
3618 | * drop PA from group | |
3619 | * mark all PAs in buddy | |
3620 | * | |
3621 | * thus, P1 initializes buddy with B available. to prevent this | |
3622 | * we make "copy" and "mark all PAs" atomic and serialize "drop PA" | |
3623 | * against that pair | |
3624 | */ | |
3625 | ext4_lock_group(sb, grp); | |
3626 | list_del(&pa->pa_group_list); | |
3627 | ext4_unlock_group(sb, grp); | |
3628 | ||
3629 | spin_lock(pa->pa_obj_lock); | |
3630 | list_del_rcu(&pa->pa_inode_list); | |
3631 | spin_unlock(pa->pa_obj_lock); | |
3632 | ||
3633 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); | |
3634 | } | |
3635 | ||
3636 | /* | |
3637 | * creates new preallocated space for given inode | |
3638 | */ | |
4ddfef7b ES |
3639 | static noinline_for_stack int |
3640 | ext4_mb_new_inode_pa(struct ext4_allocation_context *ac) | |
c9de560d AT |
3641 | { |
3642 | struct super_block *sb = ac->ac_sb; | |
53accfa9 | 3643 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
c9de560d AT |
3644 | struct ext4_prealloc_space *pa; |
3645 | struct ext4_group_info *grp; | |
3646 | struct ext4_inode_info *ei; | |
3647 | ||
3648 | /* preallocate only when found space is larger then requested */ | |
3649 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); | |
3650 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); | |
3651 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); | |
3652 | ||
3653 | pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS); | |
3654 | if (pa == NULL) | |
3655 | return -ENOMEM; | |
3656 | ||
3657 | if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) { | |
3658 | int winl; | |
3659 | int wins; | |
3660 | int win; | |
3661 | int offs; | |
3662 | ||
3663 | /* we can't allocate as much as normalizer wants. | |
3664 | * so, found space must get proper lstart | |
3665 | * to cover original request */ | |
3666 | BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical); | |
3667 | BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len); | |
3668 | ||
3669 | /* we're limited by original request in that | |
3670 | * logical block must be covered any way | |
3671 | * winl is window we can move our chunk within */ | |
3672 | winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical; | |
3673 | ||
3674 | /* also, we should cover whole original request */ | |
53accfa9 | 3675 | wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len); |
c9de560d AT |
3676 | |
3677 | /* the smallest one defines real window */ | |
3678 | win = min(winl, wins); | |
3679 | ||
53accfa9 TT |
3680 | offs = ac->ac_o_ex.fe_logical % |
3681 | EXT4_C2B(sbi, ac->ac_b_ex.fe_len); | |
c9de560d AT |
3682 | if (offs && offs < win) |
3683 | win = offs; | |
3684 | ||
53accfa9 | 3685 | ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - |
810da240 | 3686 | EXT4_NUM_B2C(sbi, win); |
c9de560d AT |
3687 | BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical); |
3688 | BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len); | |
3689 | } | |
3690 | ||
3691 | /* preallocation can change ac_b_ex, thus we store actually | |
3692 | * allocated blocks for history */ | |
3693 | ac->ac_f_ex = ac->ac_b_ex; | |
3694 | ||
3695 | pa->pa_lstart = ac->ac_b_ex.fe_logical; | |
3696 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); | |
3697 | pa->pa_len = ac->ac_b_ex.fe_len; | |
3698 | pa->pa_free = pa->pa_len; | |
3699 | atomic_set(&pa->pa_count, 1); | |
3700 | spin_lock_init(&pa->pa_lock); | |
d794bf8e AK |
3701 | INIT_LIST_HEAD(&pa->pa_inode_list); |
3702 | INIT_LIST_HEAD(&pa->pa_group_list); | |
c9de560d | 3703 | pa->pa_deleted = 0; |
cc0fb9ad | 3704 | pa->pa_type = MB_INODE_PA; |
c9de560d | 3705 | |
6ba495e9 | 3706 | mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa, |
c9de560d | 3707 | pa->pa_pstart, pa->pa_len, pa->pa_lstart); |
9bffad1e | 3708 | trace_ext4_mb_new_inode_pa(ac, pa); |
c9de560d AT |
3709 | |
3710 | ext4_mb_use_inode_pa(ac, pa); | |
53accfa9 | 3711 | atomic_add(pa->pa_free, &sbi->s_mb_preallocated); |
c9de560d AT |
3712 | |
3713 | ei = EXT4_I(ac->ac_inode); | |
3714 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); | |
3715 | ||
3716 | pa->pa_obj_lock = &ei->i_prealloc_lock; | |
3717 | pa->pa_inode = ac->ac_inode; | |
3718 | ||
3719 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); | |
3720 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); | |
3721 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); | |
3722 | ||
3723 | spin_lock(pa->pa_obj_lock); | |
3724 | list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list); | |
3725 | spin_unlock(pa->pa_obj_lock); | |
3726 | ||
3727 | return 0; | |
3728 | } | |
3729 | ||
3730 | /* | |
3731 | * creates new preallocated space for locality group inodes belongs to | |
3732 | */ | |
4ddfef7b ES |
3733 | static noinline_for_stack int |
3734 | ext4_mb_new_group_pa(struct ext4_allocation_context *ac) | |
c9de560d AT |
3735 | { |
3736 | struct super_block *sb = ac->ac_sb; | |
3737 | struct ext4_locality_group *lg; | |
3738 | struct ext4_prealloc_space *pa; | |
3739 | struct ext4_group_info *grp; | |
3740 | ||
3741 | /* preallocate only when found space is larger then requested */ | |
3742 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); | |
3743 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); | |
3744 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); | |
3745 | ||
3746 | BUG_ON(ext4_pspace_cachep == NULL); | |
3747 | pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS); | |
3748 | if (pa == NULL) | |
3749 | return -ENOMEM; | |
3750 | ||
3751 | /* preallocation can change ac_b_ex, thus we store actually | |
3752 | * allocated blocks for history */ | |
3753 | ac->ac_f_ex = ac->ac_b_ex; | |
3754 | ||
3755 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); | |
3756 | pa->pa_lstart = pa->pa_pstart; | |
3757 | pa->pa_len = ac->ac_b_ex.fe_len; | |
3758 | pa->pa_free = pa->pa_len; | |
3759 | atomic_set(&pa->pa_count, 1); | |
3760 | spin_lock_init(&pa->pa_lock); | |
6be2ded1 | 3761 | INIT_LIST_HEAD(&pa->pa_inode_list); |
d794bf8e | 3762 | INIT_LIST_HEAD(&pa->pa_group_list); |
c9de560d | 3763 | pa->pa_deleted = 0; |
cc0fb9ad | 3764 | pa->pa_type = MB_GROUP_PA; |
c9de560d | 3765 | |
6ba495e9 | 3766 | mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa, |
9bffad1e TT |
3767 | pa->pa_pstart, pa->pa_len, pa->pa_lstart); |
3768 | trace_ext4_mb_new_group_pa(ac, pa); | |
c9de560d AT |
3769 | |
3770 | ext4_mb_use_group_pa(ac, pa); | |
3771 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); | |
3772 | ||
3773 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); | |
3774 | lg = ac->ac_lg; | |
3775 | BUG_ON(lg == NULL); | |
3776 | ||
3777 | pa->pa_obj_lock = &lg->lg_prealloc_lock; | |
3778 | pa->pa_inode = NULL; | |
3779 | ||
3780 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); | |
3781 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); | |
3782 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); | |
3783 | ||
6be2ded1 AK |
3784 | /* |
3785 | * We will later add the new pa to the right bucket | |
3786 | * after updating the pa_free in ext4_mb_release_context | |
3787 | */ | |
c9de560d AT |
3788 | return 0; |
3789 | } | |
3790 | ||
3791 | static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac) | |
3792 | { | |
3793 | int err; | |
3794 | ||
3795 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) | |
3796 | err = ext4_mb_new_group_pa(ac); | |
3797 | else | |
3798 | err = ext4_mb_new_inode_pa(ac); | |
3799 | return err; | |
3800 | } | |
3801 | ||
3802 | /* | |
3803 | * finds all unused blocks in on-disk bitmap, frees them in | |
3804 | * in-core bitmap and buddy. | |
3805 | * @pa must be unlinked from inode and group lists, so that | |
3806 | * nobody else can find/use it. | |
3807 | * the caller MUST hold group/inode locks. | |
3808 | * TODO: optimize the case when there are no in-core structures yet | |
3809 | */ | |
4ddfef7b ES |
3810 | static noinline_for_stack int |
3811 | ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh, | |
3e1e5f50 | 3812 | struct ext4_prealloc_space *pa) |
c9de560d | 3813 | { |
c9de560d AT |
3814 | struct super_block *sb = e4b->bd_sb; |
3815 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
498e5f24 TT |
3816 | unsigned int end; |
3817 | unsigned int next; | |
c9de560d AT |
3818 | ext4_group_t group; |
3819 | ext4_grpblk_t bit; | |
ba80b101 | 3820 | unsigned long long grp_blk_start; |
c9de560d AT |
3821 | int err = 0; |
3822 | int free = 0; | |
3823 | ||
3824 | BUG_ON(pa->pa_deleted == 0); | |
3825 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); | |
53accfa9 | 3826 | grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit); |
c9de560d AT |
3827 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
3828 | end = bit + pa->pa_len; | |
3829 | ||
c9de560d | 3830 | while (bit < end) { |
ffad0a44 | 3831 | bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit); |
c9de560d AT |
3832 | if (bit >= end) |
3833 | break; | |
ffad0a44 | 3834 | next = mb_find_next_bit(bitmap_bh->b_data, end, bit); |
6ba495e9 | 3835 | mb_debug(1, " free preallocated %u/%u in group %u\n", |
5a0790c2 AK |
3836 | (unsigned) ext4_group_first_block_no(sb, group) + bit, |
3837 | (unsigned) next - bit, (unsigned) group); | |
c9de560d AT |
3838 | free += next - bit; |
3839 | ||
3e1e5f50 | 3840 | trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit); |
53accfa9 TT |
3841 | trace_ext4_mb_release_inode_pa(pa, (grp_blk_start + |
3842 | EXT4_C2B(sbi, bit)), | |
a9c667f8 | 3843 | next - bit); |
c9de560d AT |
3844 | mb_free_blocks(pa->pa_inode, e4b, bit, next - bit); |
3845 | bit = next + 1; | |
3846 | } | |
3847 | if (free != pa->pa_free) { | |
9d8b9ec4 TT |
3848 | ext4_msg(e4b->bd_sb, KERN_CRIT, |
3849 | "pa %p: logic %lu, phys. %lu, len %lu", | |
3850 | pa, (unsigned long) pa->pa_lstart, | |
3851 | (unsigned long) pa->pa_pstart, | |
3852 | (unsigned long) pa->pa_len); | |
e29136f8 | 3853 | ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u", |
5d1b1b3f | 3854 | free, pa->pa_free); |
e56eb659 AK |
3855 | /* |
3856 | * pa is already deleted so we use the value obtained | |
3857 | * from the bitmap and continue. | |
3858 | */ | |
c9de560d | 3859 | } |
c9de560d AT |
3860 | atomic_add(free, &sbi->s_mb_discarded); |
3861 | ||
3862 | return err; | |
3863 | } | |
3864 | ||
4ddfef7b ES |
3865 | static noinline_for_stack int |
3866 | ext4_mb_release_group_pa(struct ext4_buddy *e4b, | |
3e1e5f50 | 3867 | struct ext4_prealloc_space *pa) |
c9de560d | 3868 | { |
c9de560d AT |
3869 | struct super_block *sb = e4b->bd_sb; |
3870 | ext4_group_t group; | |
3871 | ext4_grpblk_t bit; | |
3872 | ||
60e07cf5 | 3873 | trace_ext4_mb_release_group_pa(sb, pa); |
c9de560d AT |
3874 | BUG_ON(pa->pa_deleted == 0); |
3875 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); | |
3876 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); | |
3877 | mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len); | |
3878 | atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded); | |
3e1e5f50 | 3879 | trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len); |
c9de560d AT |
3880 | |
3881 | return 0; | |
3882 | } | |
3883 | ||
3884 | /* | |
3885 | * releases all preallocations in given group | |
3886 | * | |
3887 | * first, we need to decide discard policy: | |
3888 | * - when do we discard | |
3889 | * 1) ENOSPC | |
3890 | * - how many do we discard | |
3891 | * 1) how many requested | |
3892 | */ | |
4ddfef7b ES |
3893 | static noinline_for_stack int |
3894 | ext4_mb_discard_group_preallocations(struct super_block *sb, | |
c9de560d AT |
3895 | ext4_group_t group, int needed) |
3896 | { | |
3897 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
3898 | struct buffer_head *bitmap_bh = NULL; | |
3899 | struct ext4_prealloc_space *pa, *tmp; | |
3900 | struct list_head list; | |
3901 | struct ext4_buddy e4b; | |
3902 | int err; | |
3903 | int busy = 0; | |
3904 | int free = 0; | |
3905 | ||
6ba495e9 | 3906 | mb_debug(1, "discard preallocation for group %u\n", group); |
c9de560d AT |
3907 | |
3908 | if (list_empty(&grp->bb_prealloc_list)) | |
3909 | return 0; | |
3910 | ||
574ca174 | 3911 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
9008a58e DW |
3912 | if (IS_ERR(bitmap_bh)) { |
3913 | err = PTR_ERR(bitmap_bh); | |
3914 | ext4_error(sb, "Error %d reading block bitmap for %u", | |
3915 | err, group); | |
ce89f46c | 3916 | return 0; |
c9de560d AT |
3917 | } |
3918 | ||
3919 | err = ext4_mb_load_buddy(sb, group, &e4b); | |
ce89f46c | 3920 | if (err) { |
9651e6b2 KK |
3921 | ext4_warning(sb, "Error %d loading buddy information for %u", |
3922 | err, group); | |
ce89f46c AK |
3923 | put_bh(bitmap_bh); |
3924 | return 0; | |
3925 | } | |
c9de560d AT |
3926 | |
3927 | if (needed == 0) | |
7137d7a4 | 3928 | needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1; |
c9de560d | 3929 | |
c9de560d | 3930 | INIT_LIST_HEAD(&list); |
c9de560d AT |
3931 | repeat: |
3932 | ext4_lock_group(sb, group); | |
3933 | list_for_each_entry_safe(pa, tmp, | |
3934 | &grp->bb_prealloc_list, pa_group_list) { | |
3935 | spin_lock(&pa->pa_lock); | |
3936 | if (atomic_read(&pa->pa_count)) { | |
3937 | spin_unlock(&pa->pa_lock); | |
3938 | busy = 1; | |
3939 | continue; | |
3940 | } | |
3941 | if (pa->pa_deleted) { | |
3942 | spin_unlock(&pa->pa_lock); | |
3943 | continue; | |
3944 | } | |
3945 | ||
3946 | /* seems this one can be freed ... */ | |
3947 | pa->pa_deleted = 1; | |
3948 | ||
3949 | /* we can trust pa_free ... */ | |
3950 | free += pa->pa_free; | |
3951 | ||
3952 | spin_unlock(&pa->pa_lock); | |
3953 | ||
3954 | list_del(&pa->pa_group_list); | |
3955 | list_add(&pa->u.pa_tmp_list, &list); | |
3956 | } | |
3957 | ||
3958 | /* if we still need more blocks and some PAs were used, try again */ | |
3959 | if (free < needed && busy) { | |
3960 | busy = 0; | |
3961 | ext4_unlock_group(sb, group); | |
bb8b20ed | 3962 | cond_resched(); |
c9de560d AT |
3963 | goto repeat; |
3964 | } | |
3965 | ||
3966 | /* found anything to free? */ | |
3967 | if (list_empty(&list)) { | |
3968 | BUG_ON(free != 0); | |
3969 | goto out; | |
3970 | } | |
3971 | ||
3972 | /* now free all selected PAs */ | |
3973 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { | |
3974 | ||
3975 | /* remove from object (inode or locality group) */ | |
3976 | spin_lock(pa->pa_obj_lock); | |
3977 | list_del_rcu(&pa->pa_inode_list); | |
3978 | spin_unlock(pa->pa_obj_lock); | |
3979 | ||
cc0fb9ad | 3980 | if (pa->pa_type == MB_GROUP_PA) |
3e1e5f50 | 3981 | ext4_mb_release_group_pa(&e4b, pa); |
c9de560d | 3982 | else |
3e1e5f50 | 3983 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); |
c9de560d AT |
3984 | |
3985 | list_del(&pa->u.pa_tmp_list); | |
3986 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); | |
3987 | } | |
3988 | ||
3989 | out: | |
3990 | ext4_unlock_group(sb, group); | |
e39e07fd | 3991 | ext4_mb_unload_buddy(&e4b); |
c9de560d AT |
3992 | put_bh(bitmap_bh); |
3993 | return free; | |
3994 | } | |
3995 | ||
3996 | /* | |
3997 | * releases all non-used preallocated blocks for given inode | |
3998 | * | |
3999 | * It's important to discard preallocations under i_data_sem | |
4000 | * We don't want another block to be served from the prealloc | |
4001 | * space when we are discarding the inode prealloc space. | |
4002 | * | |
4003 | * FIXME!! Make sure it is valid at all the call sites | |
4004 | */ | |
c2ea3fde | 4005 | void ext4_discard_preallocations(struct inode *inode) |
c9de560d AT |
4006 | { |
4007 | struct ext4_inode_info *ei = EXT4_I(inode); | |
4008 | struct super_block *sb = inode->i_sb; | |
4009 | struct buffer_head *bitmap_bh = NULL; | |
4010 | struct ext4_prealloc_space *pa, *tmp; | |
4011 | ext4_group_t group = 0; | |
4012 | struct list_head list; | |
4013 | struct ext4_buddy e4b; | |
4014 | int err; | |
4015 | ||
c2ea3fde | 4016 | if (!S_ISREG(inode->i_mode)) { |
c9de560d AT |
4017 | /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/ |
4018 | return; | |
4019 | } | |
4020 | ||
6ba495e9 | 4021 | mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino); |
9bffad1e | 4022 | trace_ext4_discard_preallocations(inode); |
c9de560d AT |
4023 | |
4024 | INIT_LIST_HEAD(&list); | |
4025 | ||
4026 | repeat: | |
4027 | /* first, collect all pa's in the inode */ | |
4028 | spin_lock(&ei->i_prealloc_lock); | |
4029 | while (!list_empty(&ei->i_prealloc_list)) { | |
4030 | pa = list_entry(ei->i_prealloc_list.next, | |
4031 | struct ext4_prealloc_space, pa_inode_list); | |
4032 | BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock); | |
4033 | spin_lock(&pa->pa_lock); | |
4034 | if (atomic_read(&pa->pa_count)) { | |
4035 | /* this shouldn't happen often - nobody should | |
4036 | * use preallocation while we're discarding it */ | |
4037 | spin_unlock(&pa->pa_lock); | |
4038 | spin_unlock(&ei->i_prealloc_lock); | |
9d8b9ec4 TT |
4039 | ext4_msg(sb, KERN_ERR, |
4040 | "uh-oh! used pa while discarding"); | |
c9de560d AT |
4041 | WARN_ON(1); |
4042 | schedule_timeout_uninterruptible(HZ); | |
4043 | goto repeat; | |
4044 | ||
4045 | } | |
4046 | if (pa->pa_deleted == 0) { | |
4047 | pa->pa_deleted = 1; | |
4048 | spin_unlock(&pa->pa_lock); | |
4049 | list_del_rcu(&pa->pa_inode_list); | |
4050 | list_add(&pa->u.pa_tmp_list, &list); | |
4051 | continue; | |
4052 | } | |
4053 | ||
4054 | /* someone is deleting pa right now */ | |
4055 | spin_unlock(&pa->pa_lock); | |
4056 | spin_unlock(&ei->i_prealloc_lock); | |
4057 | ||
4058 | /* we have to wait here because pa_deleted | |
4059 | * doesn't mean pa is already unlinked from | |
4060 | * the list. as we might be called from | |
4061 | * ->clear_inode() the inode will get freed | |
4062 | * and concurrent thread which is unlinking | |
4063 | * pa from inode's list may access already | |
4064 | * freed memory, bad-bad-bad */ | |
4065 | ||
4066 | /* XXX: if this happens too often, we can | |
4067 | * add a flag to force wait only in case | |
4068 | * of ->clear_inode(), but not in case of | |
4069 | * regular truncate */ | |
4070 | schedule_timeout_uninterruptible(HZ); | |
4071 | goto repeat; | |
4072 | } | |
4073 | spin_unlock(&ei->i_prealloc_lock); | |
4074 | ||
4075 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { | |
cc0fb9ad | 4076 | BUG_ON(pa->pa_type != MB_INODE_PA); |
bd86298e | 4077 | group = ext4_get_group_number(sb, pa->pa_pstart); |
c9de560d | 4078 | |
9651e6b2 KK |
4079 | err = ext4_mb_load_buddy_gfp(sb, group, &e4b, |
4080 | GFP_NOFS|__GFP_NOFAIL); | |
ce89f46c | 4081 | if (err) { |
9651e6b2 KK |
4082 | ext4_error(sb, "Error %d loading buddy information for %u", |
4083 | err, group); | |
ce89f46c AK |
4084 | continue; |
4085 | } | |
c9de560d | 4086 | |
574ca174 | 4087 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
9008a58e DW |
4088 | if (IS_ERR(bitmap_bh)) { |
4089 | err = PTR_ERR(bitmap_bh); | |
4090 | ext4_error(sb, "Error %d reading block bitmap for %u", | |
4091 | err, group); | |
e39e07fd | 4092 | ext4_mb_unload_buddy(&e4b); |
ce89f46c | 4093 | continue; |
c9de560d AT |
4094 | } |
4095 | ||
4096 | ext4_lock_group(sb, group); | |
4097 | list_del(&pa->pa_group_list); | |
3e1e5f50 | 4098 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); |
c9de560d AT |
4099 | ext4_unlock_group(sb, group); |
4100 | ||
e39e07fd | 4101 | ext4_mb_unload_buddy(&e4b); |
c9de560d AT |
4102 | put_bh(bitmap_bh); |
4103 | ||
4104 | list_del(&pa->u.pa_tmp_list); | |
4105 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); | |
4106 | } | |
4107 | } | |
4108 | ||
6ba495e9 | 4109 | #ifdef CONFIG_EXT4_DEBUG |
c9de560d AT |
4110 | static void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
4111 | { | |
4112 | struct super_block *sb = ac->ac_sb; | |
8df9675f | 4113 | ext4_group_t ngroups, i; |
c9de560d | 4114 | |
a0b30c12 | 4115 | if (!ext4_mballoc_debug || |
4dd89fc6 | 4116 | (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) |
e3570639 ES |
4117 | return; |
4118 | ||
7f6a11e7 | 4119 | ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:" |
9d8b9ec4 | 4120 | " Allocation context details:"); |
7f6a11e7 | 4121 | ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d", |
c9de560d | 4122 | ac->ac_status, ac->ac_flags); |
7f6a11e7 | 4123 | ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, " |
9d8b9ec4 TT |
4124 | "goal %lu/%lu/%lu@%lu, " |
4125 | "best %lu/%lu/%lu@%lu cr %d", | |
c9de560d AT |
4126 | (unsigned long)ac->ac_o_ex.fe_group, |
4127 | (unsigned long)ac->ac_o_ex.fe_start, | |
4128 | (unsigned long)ac->ac_o_ex.fe_len, | |
4129 | (unsigned long)ac->ac_o_ex.fe_logical, | |
4130 | (unsigned long)ac->ac_g_ex.fe_group, | |
4131 | (unsigned long)ac->ac_g_ex.fe_start, | |
4132 | (unsigned long)ac->ac_g_ex.fe_len, | |
4133 | (unsigned long)ac->ac_g_ex.fe_logical, | |
4134 | (unsigned long)ac->ac_b_ex.fe_group, | |
4135 | (unsigned long)ac->ac_b_ex.fe_start, | |
4136 | (unsigned long)ac->ac_b_ex.fe_len, | |
4137 | (unsigned long)ac->ac_b_ex.fe_logical, | |
4138 | (int)ac->ac_criteria); | |
dc9ddd98 | 4139 | ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found); |
7f6a11e7 | 4140 | ext4_msg(ac->ac_sb, KERN_ERR, "groups: "); |
8df9675f TT |
4141 | ngroups = ext4_get_groups_count(sb); |
4142 | for (i = 0; i < ngroups; i++) { | |
c9de560d AT |
4143 | struct ext4_group_info *grp = ext4_get_group_info(sb, i); |
4144 | struct ext4_prealloc_space *pa; | |
4145 | ext4_grpblk_t start; | |
4146 | struct list_head *cur; | |
4147 | ext4_lock_group(sb, i); | |
4148 | list_for_each(cur, &grp->bb_prealloc_list) { | |
4149 | pa = list_entry(cur, struct ext4_prealloc_space, | |
4150 | pa_group_list); | |
4151 | spin_lock(&pa->pa_lock); | |
4152 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, | |
4153 | NULL, &start); | |
4154 | spin_unlock(&pa->pa_lock); | |
1c718505 AF |
4155 | printk(KERN_ERR "PA:%u:%d:%u \n", i, |
4156 | start, pa->pa_len); | |
c9de560d | 4157 | } |
60bd63d1 | 4158 | ext4_unlock_group(sb, i); |
c9de560d AT |
4159 | |
4160 | if (grp->bb_free == 0) | |
4161 | continue; | |
1c718505 | 4162 | printk(KERN_ERR "%u: %d/%d \n", |
c9de560d AT |
4163 | i, grp->bb_free, grp->bb_fragments); |
4164 | } | |
4165 | printk(KERN_ERR "\n"); | |
4166 | } | |
4167 | #else | |
4168 | static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac) | |
4169 | { | |
4170 | return; | |
4171 | } | |
4172 | #endif | |
4173 | ||
4174 | /* | |
4175 | * We use locality group preallocation for small size file. The size of the | |
4176 | * file is determined by the current size or the resulting size after | |
4177 | * allocation which ever is larger | |
4178 | * | |
b713a5ec | 4179 | * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req |
c9de560d AT |
4180 | */ |
4181 | static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) | |
4182 | { | |
4183 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); | |
4184 | int bsbits = ac->ac_sb->s_blocksize_bits; | |
4185 | loff_t size, isize; | |
4186 | ||
4187 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) | |
4188 | return; | |
4189 | ||
4ba74d00 TT |
4190 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
4191 | return; | |
4192 | ||
53accfa9 | 4193 | size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len); |
50797481 TT |
4194 | isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) |
4195 | >> bsbits; | |
c9de560d | 4196 | |
50797481 TT |
4197 | if ((size == isize) && |
4198 | !ext4_fs_is_busy(sbi) && | |
4199 | (atomic_read(&ac->ac_inode->i_writecount) == 0)) { | |
4200 | ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC; | |
4201 | return; | |
4202 | } | |
4203 | ||
ebbe0277 RD |
4204 | if (sbi->s_mb_group_prealloc <= 0) { |
4205 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; | |
4206 | return; | |
4207 | } | |
4208 | ||
c9de560d | 4209 | /* don't use group allocation for large files */ |
71780577 | 4210 | size = max(size, isize); |
cc483f10 | 4211 | if (size > sbi->s_mb_stream_request) { |
4ba74d00 | 4212 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
c9de560d | 4213 | return; |
4ba74d00 | 4214 | } |
c9de560d AT |
4215 | |
4216 | BUG_ON(ac->ac_lg != NULL); | |
4217 | /* | |
4218 | * locality group prealloc space are per cpu. The reason for having | |
4219 | * per cpu locality group is to reduce the contention between block | |
4220 | * request from multiple CPUs. | |
4221 | */ | |
a0b6bc63 | 4222 | ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups); |
c9de560d AT |
4223 | |
4224 | /* we're going to use group allocation */ | |
4225 | ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC; | |
4226 | ||
4227 | /* serialize all allocations in the group */ | |
4228 | mutex_lock(&ac->ac_lg->lg_mutex); | |
4229 | } | |
4230 | ||
4ddfef7b ES |
4231 | static noinline_for_stack int |
4232 | ext4_mb_initialize_context(struct ext4_allocation_context *ac, | |
c9de560d AT |
4233 | struct ext4_allocation_request *ar) |
4234 | { | |
4235 | struct super_block *sb = ar->inode->i_sb; | |
4236 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
4237 | struct ext4_super_block *es = sbi->s_es; | |
4238 | ext4_group_t group; | |
498e5f24 TT |
4239 | unsigned int len; |
4240 | ext4_fsblk_t goal; | |
c9de560d AT |
4241 | ext4_grpblk_t block; |
4242 | ||
4243 | /* we can't allocate > group size */ | |
4244 | len = ar->len; | |
4245 | ||
4246 | /* just a dirty hack to filter too big requests */ | |
40ae3487 TT |
4247 | if (len >= EXT4_CLUSTERS_PER_GROUP(sb)) |
4248 | len = EXT4_CLUSTERS_PER_GROUP(sb); | |
c9de560d AT |
4249 | |
4250 | /* start searching from the goal */ | |
4251 | goal = ar->goal; | |
4252 | if (goal < le32_to_cpu(es->s_first_data_block) || | |
4253 | goal >= ext4_blocks_count(es)) | |
4254 | goal = le32_to_cpu(es->s_first_data_block); | |
4255 | ext4_get_group_no_and_offset(sb, goal, &group, &block); | |
4256 | ||
4257 | /* set up allocation goals */ | |
f5a44db5 | 4258 | ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical); |
c9de560d | 4259 | ac->ac_status = AC_STATUS_CONTINUE; |
c9de560d AT |
4260 | ac->ac_sb = sb; |
4261 | ac->ac_inode = ar->inode; | |
53accfa9 | 4262 | ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical; |
c9de560d AT |
4263 | ac->ac_o_ex.fe_group = group; |
4264 | ac->ac_o_ex.fe_start = block; | |
4265 | ac->ac_o_ex.fe_len = len; | |
53accfa9 | 4266 | ac->ac_g_ex = ac->ac_o_ex; |
c9de560d | 4267 | ac->ac_flags = ar->flags; |
c9de560d AT |
4268 | |
4269 | /* we have to define context: we'll we work with a file or | |
4270 | * locality group. this is a policy, actually */ | |
4271 | ext4_mb_group_or_file(ac); | |
4272 | ||
6ba495e9 | 4273 | mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, " |
c9de560d AT |
4274 | "left: %u/%u, right %u/%u to %swritable\n", |
4275 | (unsigned) ar->len, (unsigned) ar->logical, | |
4276 | (unsigned) ar->goal, ac->ac_flags, ac->ac_2order, | |
4277 | (unsigned) ar->lleft, (unsigned) ar->pleft, | |
4278 | (unsigned) ar->lright, (unsigned) ar->pright, | |
4279 | atomic_read(&ar->inode->i_writecount) ? "" : "non-"); | |
4280 | return 0; | |
4281 | ||
4282 | } | |
4283 | ||
6be2ded1 AK |
4284 | static noinline_for_stack void |
4285 | ext4_mb_discard_lg_preallocations(struct super_block *sb, | |
4286 | struct ext4_locality_group *lg, | |
4287 | int order, int total_entries) | |
4288 | { | |
4289 | ext4_group_t group = 0; | |
4290 | struct ext4_buddy e4b; | |
4291 | struct list_head discard_list; | |
4292 | struct ext4_prealloc_space *pa, *tmp; | |
6be2ded1 | 4293 | |
6ba495e9 | 4294 | mb_debug(1, "discard locality group preallocation\n"); |
6be2ded1 AK |
4295 | |
4296 | INIT_LIST_HEAD(&discard_list); | |
6be2ded1 AK |
4297 | |
4298 | spin_lock(&lg->lg_prealloc_lock); | |
4299 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order], | |
4300 | pa_inode_list) { | |
4301 | spin_lock(&pa->pa_lock); | |
4302 | if (atomic_read(&pa->pa_count)) { | |
4303 | /* | |
4304 | * This is the pa that we just used | |
4305 | * for block allocation. So don't | |
4306 | * free that | |
4307 | */ | |
4308 | spin_unlock(&pa->pa_lock); | |
4309 | continue; | |
4310 | } | |
4311 | if (pa->pa_deleted) { | |
4312 | spin_unlock(&pa->pa_lock); | |
4313 | continue; | |
4314 | } | |
4315 | /* only lg prealloc space */ | |
cc0fb9ad | 4316 | BUG_ON(pa->pa_type != MB_GROUP_PA); |
6be2ded1 AK |
4317 | |
4318 | /* seems this one can be freed ... */ | |
4319 | pa->pa_deleted = 1; | |
4320 | spin_unlock(&pa->pa_lock); | |
4321 | ||
4322 | list_del_rcu(&pa->pa_inode_list); | |
4323 | list_add(&pa->u.pa_tmp_list, &discard_list); | |
4324 | ||
4325 | total_entries--; | |
4326 | if (total_entries <= 5) { | |
4327 | /* | |
4328 | * we want to keep only 5 entries | |
4329 | * allowing it to grow to 8. This | |
4330 | * mak sure we don't call discard | |
4331 | * soon for this list. | |
4332 | */ | |
4333 | break; | |
4334 | } | |
4335 | } | |
4336 | spin_unlock(&lg->lg_prealloc_lock); | |
4337 | ||
4338 | list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) { | |
9651e6b2 | 4339 | int err; |
6be2ded1 | 4340 | |
bd86298e | 4341 | group = ext4_get_group_number(sb, pa->pa_pstart); |
9651e6b2 KK |
4342 | err = ext4_mb_load_buddy_gfp(sb, group, &e4b, |
4343 | GFP_NOFS|__GFP_NOFAIL); | |
4344 | if (err) { | |
4345 | ext4_error(sb, "Error %d loading buddy information for %u", | |
4346 | err, group); | |
6be2ded1 AK |
4347 | continue; |
4348 | } | |
4349 | ext4_lock_group(sb, group); | |
4350 | list_del(&pa->pa_group_list); | |
3e1e5f50 | 4351 | ext4_mb_release_group_pa(&e4b, pa); |
6be2ded1 AK |
4352 | ext4_unlock_group(sb, group); |
4353 | ||
e39e07fd | 4354 | ext4_mb_unload_buddy(&e4b); |
6be2ded1 AK |
4355 | list_del(&pa->u.pa_tmp_list); |
4356 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); | |
4357 | } | |
6be2ded1 AK |
4358 | } |
4359 | ||
4360 | /* | |
4361 | * We have incremented pa_count. So it cannot be freed at this | |
4362 | * point. Also we hold lg_mutex. So no parallel allocation is | |
4363 | * possible from this lg. That means pa_free cannot be updated. | |
4364 | * | |
4365 | * A parallel ext4_mb_discard_group_preallocations is possible. | |
4366 | * which can cause the lg_prealloc_list to be updated. | |
4367 | */ | |
4368 | ||
4369 | static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac) | |
4370 | { | |
4371 | int order, added = 0, lg_prealloc_count = 1; | |
4372 | struct super_block *sb = ac->ac_sb; | |
4373 | struct ext4_locality_group *lg = ac->ac_lg; | |
4374 | struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa; | |
4375 | ||
4376 | order = fls(pa->pa_free) - 1; | |
4377 | if (order > PREALLOC_TB_SIZE - 1) | |
4378 | /* The max size of hash table is PREALLOC_TB_SIZE */ | |
4379 | order = PREALLOC_TB_SIZE - 1; | |
4380 | /* Add the prealloc space to lg */ | |
f1167009 | 4381 | spin_lock(&lg->lg_prealloc_lock); |
6be2ded1 AK |
4382 | list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order], |
4383 | pa_inode_list) { | |
4384 | spin_lock(&tmp_pa->pa_lock); | |
4385 | if (tmp_pa->pa_deleted) { | |
e7c9e3e9 | 4386 | spin_unlock(&tmp_pa->pa_lock); |
6be2ded1 AK |
4387 | continue; |
4388 | } | |
4389 | if (!added && pa->pa_free < tmp_pa->pa_free) { | |
4390 | /* Add to the tail of the previous entry */ | |
4391 | list_add_tail_rcu(&pa->pa_inode_list, | |
4392 | &tmp_pa->pa_inode_list); | |
4393 | added = 1; | |
4394 | /* | |
4395 | * we want to count the total | |
4396 | * number of entries in the list | |
4397 | */ | |
4398 | } | |
4399 | spin_unlock(&tmp_pa->pa_lock); | |
4400 | lg_prealloc_count++; | |
4401 | } | |
4402 | if (!added) | |
4403 | list_add_tail_rcu(&pa->pa_inode_list, | |
4404 | &lg->lg_prealloc_list[order]); | |
f1167009 | 4405 | spin_unlock(&lg->lg_prealloc_lock); |
6be2ded1 AK |
4406 | |
4407 | /* Now trim the list to be not more than 8 elements */ | |
4408 | if (lg_prealloc_count > 8) { | |
4409 | ext4_mb_discard_lg_preallocations(sb, lg, | |
f1167009 | 4410 | order, lg_prealloc_count); |
6be2ded1 AK |
4411 | return; |
4412 | } | |
4413 | return ; | |
4414 | } | |
4415 | ||
c9de560d AT |
4416 | /* |
4417 | * release all resource we used in allocation | |
4418 | */ | |
4419 | static int ext4_mb_release_context(struct ext4_allocation_context *ac) | |
4420 | { | |
53accfa9 | 4421 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
6be2ded1 AK |
4422 | struct ext4_prealloc_space *pa = ac->ac_pa; |
4423 | if (pa) { | |
cc0fb9ad | 4424 | if (pa->pa_type == MB_GROUP_PA) { |
c9de560d | 4425 | /* see comment in ext4_mb_use_group_pa() */ |
6be2ded1 | 4426 | spin_lock(&pa->pa_lock); |
53accfa9 TT |
4427 | pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
4428 | pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len); | |
6be2ded1 AK |
4429 | pa->pa_free -= ac->ac_b_ex.fe_len; |
4430 | pa->pa_len -= ac->ac_b_ex.fe_len; | |
4431 | spin_unlock(&pa->pa_lock); | |
c9de560d | 4432 | } |
c9de560d | 4433 | } |
ba443916 AK |
4434 | if (pa) { |
4435 | /* | |
4436 | * We want to add the pa to the right bucket. | |
4437 | * Remove it from the list and while adding | |
4438 | * make sure the list to which we are adding | |
44183d42 | 4439 | * doesn't grow big. |
ba443916 | 4440 | */ |
cc0fb9ad | 4441 | if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) { |
ba443916 AK |
4442 | spin_lock(pa->pa_obj_lock); |
4443 | list_del_rcu(&pa->pa_inode_list); | |
4444 | spin_unlock(pa->pa_obj_lock); | |
4445 | ext4_mb_add_n_trim(ac); | |
4446 | } | |
4447 | ext4_mb_put_pa(ac, ac->ac_sb, pa); | |
4448 | } | |
c9de560d | 4449 | if (ac->ac_bitmap_page) |
09cbfeaf | 4450 | put_page(ac->ac_bitmap_page); |
c9de560d | 4451 | if (ac->ac_buddy_page) |
09cbfeaf | 4452 | put_page(ac->ac_buddy_page); |
c9de560d AT |
4453 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
4454 | mutex_unlock(&ac->ac_lg->lg_mutex); | |
4455 | ext4_mb_collect_stats(ac); | |
4456 | return 0; | |
4457 | } | |
4458 | ||
4459 | static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) | |
4460 | { | |
8df9675f | 4461 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
c9de560d AT |
4462 | int ret; |
4463 | int freed = 0; | |
4464 | ||
9bffad1e | 4465 | trace_ext4_mb_discard_preallocations(sb, needed); |
8df9675f | 4466 | for (i = 0; i < ngroups && needed > 0; i++) { |
c9de560d AT |
4467 | ret = ext4_mb_discard_group_preallocations(sb, i, needed); |
4468 | freed += ret; | |
4469 | needed -= ret; | |
4470 | } | |
4471 | ||
4472 | return freed; | |
4473 | } | |
4474 | ||
4475 | /* | |
4476 | * Main entry point into mballoc to allocate blocks | |
4477 | * it tries to use preallocation first, then falls back | |
4478 | * to usual allocation | |
4479 | */ | |
4480 | ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, | |
6c7a120a | 4481 | struct ext4_allocation_request *ar, int *errp) |
c9de560d | 4482 | { |
6bc6e63f | 4483 | int freed; |
256bdb49 | 4484 | struct ext4_allocation_context *ac = NULL; |
c9de560d AT |
4485 | struct ext4_sb_info *sbi; |
4486 | struct super_block *sb; | |
4487 | ext4_fsblk_t block = 0; | |
60e58e0f | 4488 | unsigned int inquota = 0; |
53accfa9 | 4489 | unsigned int reserv_clstrs = 0; |
c9de560d | 4490 | |
b10a44c3 | 4491 | might_sleep(); |
c9de560d AT |
4492 | sb = ar->inode->i_sb; |
4493 | sbi = EXT4_SB(sb); | |
4494 | ||
9bffad1e | 4495 | trace_ext4_request_blocks(ar); |
ba80b101 | 4496 | |
45dc63e7 | 4497 | /* Allow to use superuser reservation for quota file */ |
02749a4c | 4498 | if (ext4_is_quota_file(ar->inode)) |
45dc63e7 DM |
4499 | ar->flags |= EXT4_MB_USE_ROOT_BLOCKS; |
4500 | ||
e3cf5d5d | 4501 | if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) { |
60e58e0f MC |
4502 | /* Without delayed allocation we need to verify |
4503 | * there is enough free blocks to do block allocation | |
4504 | * and verify allocation doesn't exceed the quota limits. | |
d2a17637 | 4505 | */ |
55f020db | 4506 | while (ar->len && |
e7d5f315 | 4507 | ext4_claim_free_clusters(sbi, ar->len, ar->flags)) { |
55f020db | 4508 | |
030ba6bc | 4509 | /* let others to free the space */ |
bb8b20ed | 4510 | cond_resched(); |
030ba6bc AK |
4511 | ar->len = ar->len >> 1; |
4512 | } | |
4513 | if (!ar->len) { | |
a30d542a AK |
4514 | *errp = -ENOSPC; |
4515 | return 0; | |
4516 | } | |
53accfa9 | 4517 | reserv_clstrs = ar->len; |
55f020db | 4518 | if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) { |
53accfa9 TT |
4519 | dquot_alloc_block_nofail(ar->inode, |
4520 | EXT4_C2B(sbi, ar->len)); | |
55f020db AH |
4521 | } else { |
4522 | while (ar->len && | |
53accfa9 TT |
4523 | dquot_alloc_block(ar->inode, |
4524 | EXT4_C2B(sbi, ar->len))) { | |
55f020db AH |
4525 | |
4526 | ar->flags |= EXT4_MB_HINT_NOPREALLOC; | |
4527 | ar->len--; | |
4528 | } | |
60e58e0f MC |
4529 | } |
4530 | inquota = ar->len; | |
4531 | if (ar->len == 0) { | |
4532 | *errp = -EDQUOT; | |
6c7a120a | 4533 | goto out; |
60e58e0f | 4534 | } |
07031431 | 4535 | } |
d2a17637 | 4536 | |
85556c9a | 4537 | ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS); |
833576b3 | 4538 | if (!ac) { |
363d4251 | 4539 | ar->len = 0; |
256bdb49 | 4540 | *errp = -ENOMEM; |
6c7a120a | 4541 | goto out; |
256bdb49 ES |
4542 | } |
4543 | ||
256bdb49 | 4544 | *errp = ext4_mb_initialize_context(ac, ar); |
c9de560d AT |
4545 | if (*errp) { |
4546 | ar->len = 0; | |
6c7a120a | 4547 | goto out; |
c9de560d AT |
4548 | } |
4549 | ||
256bdb49 ES |
4550 | ac->ac_op = EXT4_MB_HISTORY_PREALLOC; |
4551 | if (!ext4_mb_use_preallocated(ac)) { | |
256bdb49 ES |
4552 | ac->ac_op = EXT4_MB_HISTORY_ALLOC; |
4553 | ext4_mb_normalize_request(ac, ar); | |
c9de560d AT |
4554 | repeat: |
4555 | /* allocate space in core */ | |
6c7a120a | 4556 | *errp = ext4_mb_regular_allocator(ac); |
2c00ef3e AK |
4557 | if (*errp) |
4558 | goto discard_and_exit; | |
c9de560d AT |
4559 | |
4560 | /* as we've just preallocated more space than | |
2c00ef3e | 4561 | * user requested originally, we store allocated |
c9de560d | 4562 | * space in a special descriptor */ |
256bdb49 | 4563 | if (ac->ac_status == AC_STATUS_FOUND && |
2c00ef3e AK |
4564 | ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) |
4565 | *errp = ext4_mb_new_preallocation(ac); | |
4566 | if (*errp) { | |
4567 | discard_and_exit: | |
4568 | ext4_discard_allocated_blocks(ac); | |
4569 | goto errout; | |
4570 | } | |
c9de560d | 4571 | } |
256bdb49 | 4572 | if (likely(ac->ac_status == AC_STATUS_FOUND)) { |
53accfa9 | 4573 | *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs); |
554a5ccc | 4574 | if (*errp) { |
b844167e | 4575 | ext4_discard_allocated_blocks(ac); |
6d138ced ES |
4576 | goto errout; |
4577 | } else { | |
519deca0 AK |
4578 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
4579 | ar->len = ac->ac_b_ex.fe_len; | |
4580 | } | |
c9de560d | 4581 | } else { |
256bdb49 | 4582 | freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len); |
c9de560d AT |
4583 | if (freed) |
4584 | goto repeat; | |
4585 | *errp = -ENOSPC; | |
6c7a120a AK |
4586 | } |
4587 | ||
6d138ced | 4588 | errout: |
6c7a120a | 4589 | if (*errp) { |
256bdb49 | 4590 | ac->ac_b_ex.fe_len = 0; |
c9de560d | 4591 | ar->len = 0; |
256bdb49 | 4592 | ext4_mb_show_ac(ac); |
c9de560d | 4593 | } |
256bdb49 | 4594 | ext4_mb_release_context(ac); |
6c7a120a AK |
4595 | out: |
4596 | if (ac) | |
4597 | kmem_cache_free(ext4_ac_cachep, ac); | |
60e58e0f | 4598 | if (inquota && ar->len < inquota) |
53accfa9 | 4599 | dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len)); |
0087d9fb | 4600 | if (!ar->len) { |
e3cf5d5d | 4601 | if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) |
0087d9fb | 4602 | /* release all the reserved blocks if non delalloc */ |
57042651 | 4603 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
53accfa9 | 4604 | reserv_clstrs); |
0087d9fb | 4605 | } |
c9de560d | 4606 | |
9bffad1e | 4607 | trace_ext4_allocate_blocks(ar, (unsigned long long)block); |
ba80b101 | 4608 | |
c9de560d AT |
4609 | return block; |
4610 | } | |
c9de560d | 4611 | |
c894058d AK |
4612 | /* |
4613 | * We can merge two free data extents only if the physical blocks | |
4614 | * are contiguous, AND the extents were freed by the same transaction, | |
4615 | * AND the blocks are associated with the same group. | |
4616 | */ | |
a0154344 DJ |
4617 | static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi, |
4618 | struct ext4_free_data *entry, | |
4619 | struct ext4_free_data *new_entry, | |
4620 | struct rb_root *entry_rb_root) | |
c894058d | 4621 | { |
a0154344 DJ |
4622 | if ((entry->efd_tid != new_entry->efd_tid) || |
4623 | (entry->efd_group != new_entry->efd_group)) | |
4624 | return; | |
4625 | if (entry->efd_start_cluster + entry->efd_count == | |
4626 | new_entry->efd_start_cluster) { | |
4627 | new_entry->efd_start_cluster = entry->efd_start_cluster; | |
4628 | new_entry->efd_count += entry->efd_count; | |
4629 | } else if (new_entry->efd_start_cluster + new_entry->efd_count == | |
4630 | entry->efd_start_cluster) { | |
4631 | new_entry->efd_count += entry->efd_count; | |
4632 | } else | |
4633 | return; | |
4634 | spin_lock(&sbi->s_md_lock); | |
4635 | list_del(&entry->efd_list); | |
4636 | spin_unlock(&sbi->s_md_lock); | |
4637 | rb_erase(&entry->efd_node, entry_rb_root); | |
4638 | kmem_cache_free(ext4_free_data_cachep, entry); | |
c894058d AK |
4639 | } |
4640 | ||
4ddfef7b ES |
4641 | static noinline_for_stack int |
4642 | ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b, | |
7a2fcbf7 | 4643 | struct ext4_free_data *new_entry) |
c9de560d | 4644 | { |
e29136f8 | 4645 | ext4_group_t group = e4b->bd_group; |
84130193 | 4646 | ext4_grpblk_t cluster; |
d08854f5 | 4647 | ext4_grpblk_t clusters = new_entry->efd_count; |
7a2fcbf7 | 4648 | struct ext4_free_data *entry; |
c9de560d AT |
4649 | struct ext4_group_info *db = e4b->bd_info; |
4650 | struct super_block *sb = e4b->bd_sb; | |
4651 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
c894058d AK |
4652 | struct rb_node **n = &db->bb_free_root.rb_node, *node; |
4653 | struct rb_node *parent = NULL, *new_node; | |
4654 | ||
0390131b | 4655 | BUG_ON(!ext4_handle_valid(handle)); |
c9de560d AT |
4656 | BUG_ON(e4b->bd_bitmap_page == NULL); |
4657 | BUG_ON(e4b->bd_buddy_page == NULL); | |
4658 | ||
18aadd47 BJ |
4659 | new_node = &new_entry->efd_node; |
4660 | cluster = new_entry->efd_start_cluster; | |
c894058d | 4661 | |
c894058d AK |
4662 | if (!*n) { |
4663 | /* first free block exent. We need to | |
4664 | protect buddy cache from being freed, | |
4665 | * otherwise we'll refresh it from | |
4666 | * on-disk bitmap and lose not-yet-available | |
4667 | * blocks */ | |
09cbfeaf KS |
4668 | get_page(e4b->bd_buddy_page); |
4669 | get_page(e4b->bd_bitmap_page); | |
c894058d AK |
4670 | } |
4671 | while (*n) { | |
4672 | parent = *n; | |
18aadd47 BJ |
4673 | entry = rb_entry(parent, struct ext4_free_data, efd_node); |
4674 | if (cluster < entry->efd_start_cluster) | |
c894058d | 4675 | n = &(*n)->rb_left; |
18aadd47 | 4676 | else if (cluster >= (entry->efd_start_cluster + entry->efd_count)) |
c894058d AK |
4677 | n = &(*n)->rb_right; |
4678 | else { | |
e29136f8 | 4679 | ext4_grp_locked_error(sb, group, 0, |
84130193 TT |
4680 | ext4_group_first_block_no(sb, group) + |
4681 | EXT4_C2B(sbi, cluster), | |
e29136f8 | 4682 | "Block already on to-be-freed list"); |
c894058d | 4683 | return 0; |
c9de560d | 4684 | } |
c894058d | 4685 | } |
c9de560d | 4686 | |
c894058d AK |
4687 | rb_link_node(new_node, parent, n); |
4688 | rb_insert_color(new_node, &db->bb_free_root); | |
4689 | ||
4690 | /* Now try to see the extent can be merged to left and right */ | |
4691 | node = rb_prev(new_node); | |
4692 | if (node) { | |
18aadd47 | 4693 | entry = rb_entry(node, struct ext4_free_data, efd_node); |
a0154344 DJ |
4694 | ext4_try_merge_freed_extent(sbi, entry, new_entry, |
4695 | &(db->bb_free_root)); | |
c894058d | 4696 | } |
c9de560d | 4697 | |
c894058d AK |
4698 | node = rb_next(new_node); |
4699 | if (node) { | |
18aadd47 | 4700 | entry = rb_entry(node, struct ext4_free_data, efd_node); |
a0154344 DJ |
4701 | ext4_try_merge_freed_extent(sbi, entry, new_entry, |
4702 | &(db->bb_free_root)); | |
c9de560d | 4703 | } |
a0154344 | 4704 | |
d08854f5 | 4705 | spin_lock(&sbi->s_md_lock); |
a0154344 | 4706 | list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list); |
d08854f5 TT |
4707 | sbi->s_mb_free_pending += clusters; |
4708 | spin_unlock(&sbi->s_md_lock); | |
c9de560d AT |
4709 | return 0; |
4710 | } | |
4711 | ||
44338711 TT |
4712 | /** |
4713 | * ext4_free_blocks() -- Free given blocks and update quota | |
4714 | * @handle: handle for this transaction | |
4715 | * @inode: inode | |
4716 | * @block: start physical block to free | |
4717 | * @count: number of blocks to count | |
5def1360 | 4718 | * @flags: flags used by ext4_free_blocks |
c9de560d | 4719 | */ |
44338711 | 4720 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
e6362609 TT |
4721 | struct buffer_head *bh, ext4_fsblk_t block, |
4722 | unsigned long count, int flags) | |
c9de560d | 4723 | { |
26346ff6 | 4724 | struct buffer_head *bitmap_bh = NULL; |
c9de560d | 4725 | struct super_block *sb = inode->i_sb; |
c9de560d | 4726 | struct ext4_group_desc *gdp; |
498e5f24 | 4727 | unsigned int overflow; |
c9de560d AT |
4728 | ext4_grpblk_t bit; |
4729 | struct buffer_head *gd_bh; | |
4730 | ext4_group_t block_group; | |
4731 | struct ext4_sb_info *sbi; | |
4732 | struct ext4_buddy e4b; | |
84130193 | 4733 | unsigned int count_clusters; |
c9de560d AT |
4734 | int err = 0; |
4735 | int ret; | |
4736 | ||
b10a44c3 | 4737 | might_sleep(); |
e6362609 TT |
4738 | if (bh) { |
4739 | if (block) | |
4740 | BUG_ON(block != bh->b_blocknr); | |
4741 | else | |
4742 | block = bh->b_blocknr; | |
4743 | } | |
c9de560d | 4744 | |
c9de560d | 4745 | sbi = EXT4_SB(sb); |
1f2acb60 TT |
4746 | if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && |
4747 | !ext4_data_block_valid(sbi, block, count)) { | |
12062ddd | 4748 | ext4_error(sb, "Freeing blocks not in datazone - " |
1f2acb60 | 4749 | "block = %llu, count = %lu", block, count); |
c9de560d AT |
4750 | goto error_return; |
4751 | } | |
4752 | ||
0610b6e9 | 4753 | ext4_debug("freeing block %llu\n", block); |
e6362609 TT |
4754 | trace_ext4_free_blocks(inode, block, count, flags); |
4755 | ||
9c02ac97 DJ |
4756 | if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { |
4757 | BUG_ON(count > 1); | |
e6362609 | 4758 | |
9c02ac97 DJ |
4759 | ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA, |
4760 | inode, bh, block); | |
e6362609 TT |
4761 | } |
4762 | ||
84130193 TT |
4763 | /* |
4764 | * If the extent to be freed does not begin on a cluster | |
4765 | * boundary, we need to deal with partial clusters at the | |
4766 | * beginning and end of the extent. Normally we will free | |
4767 | * blocks at the beginning or the end unless we are explicitly | |
4768 | * requested to avoid doing so. | |
4769 | */ | |
f5a44db5 | 4770 | overflow = EXT4_PBLK_COFF(sbi, block); |
84130193 TT |
4771 | if (overflow) { |
4772 | if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) { | |
4773 | overflow = sbi->s_cluster_ratio - overflow; | |
4774 | block += overflow; | |
4775 | if (count > overflow) | |
4776 | count -= overflow; | |
4777 | else | |
4778 | return; | |
4779 | } else { | |
4780 | block -= overflow; | |
4781 | count += overflow; | |
4782 | } | |
4783 | } | |
f5a44db5 | 4784 | overflow = EXT4_LBLK_COFF(sbi, count); |
84130193 TT |
4785 | if (overflow) { |
4786 | if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) { | |
4787 | if (count > overflow) | |
4788 | count -= overflow; | |
4789 | else | |
4790 | return; | |
4791 | } else | |
4792 | count += sbi->s_cluster_ratio - overflow; | |
4793 | } | |
4794 | ||
9c02ac97 DJ |
4795 | if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { |
4796 | int i; | |
f96c450d | 4797 | int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA; |
9c02ac97 DJ |
4798 | |
4799 | for (i = 0; i < count; i++) { | |
4800 | cond_resched(); | |
f96c450d DJ |
4801 | if (is_metadata) |
4802 | bh = sb_find_get_block(inode->i_sb, block + i); | |
4803 | ext4_forget(handle, is_metadata, inode, bh, block + i); | |
9c02ac97 DJ |
4804 | } |
4805 | } | |
4806 | ||
c9de560d AT |
4807 | do_more: |
4808 | overflow = 0; | |
4809 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); | |
4810 | ||
163a203d DW |
4811 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT( |
4812 | ext4_get_group_info(sb, block_group)))) | |
4813 | return; | |
4814 | ||
c9de560d AT |
4815 | /* |
4816 | * Check to see if we are freeing blocks across a group | |
4817 | * boundary. | |
4818 | */ | |
84130193 TT |
4819 | if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
4820 | overflow = EXT4_C2B(sbi, bit) + count - | |
4821 | EXT4_BLOCKS_PER_GROUP(sb); | |
c9de560d AT |
4822 | count -= overflow; |
4823 | } | |
810da240 | 4824 | count_clusters = EXT4_NUM_B2C(sbi, count); |
574ca174 | 4825 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
9008a58e DW |
4826 | if (IS_ERR(bitmap_bh)) { |
4827 | err = PTR_ERR(bitmap_bh); | |
4828 | bitmap_bh = NULL; | |
c9de560d | 4829 | goto error_return; |
ce89f46c | 4830 | } |
c9de560d | 4831 | gdp = ext4_get_group_desc(sb, block_group, &gd_bh); |
ce89f46c AK |
4832 | if (!gdp) { |
4833 | err = -EIO; | |
c9de560d | 4834 | goto error_return; |
ce89f46c | 4835 | } |
c9de560d AT |
4836 | |
4837 | if (in_range(ext4_block_bitmap(sb, gdp), block, count) || | |
4838 | in_range(ext4_inode_bitmap(sb, gdp), block, count) || | |
4839 | in_range(block, ext4_inode_table(sb, gdp), | |
49598e04 | 4840 | sbi->s_itb_per_group) || |
c9de560d | 4841 | in_range(block + count - 1, ext4_inode_table(sb, gdp), |
49598e04 | 4842 | sbi->s_itb_per_group)) { |
c9de560d | 4843 | |
12062ddd | 4844 | ext4_error(sb, "Freeing blocks in system zone - " |
0610b6e9 | 4845 | "Block = %llu, count = %lu", block, count); |
519deca0 AK |
4846 | /* err = 0. ext4_std_error should be a no op */ |
4847 | goto error_return; | |
c9de560d AT |
4848 | } |
4849 | ||
4850 | BUFFER_TRACE(bitmap_bh, "getting write access"); | |
4851 | err = ext4_journal_get_write_access(handle, bitmap_bh); | |
4852 | if (err) | |
4853 | goto error_return; | |
4854 | ||
4855 | /* | |
4856 | * We are about to modify some metadata. Call the journal APIs | |
4857 | * to unshare ->b_data if a currently-committing transaction is | |
4858 | * using it | |
4859 | */ | |
4860 | BUFFER_TRACE(gd_bh, "get_write_access"); | |
4861 | err = ext4_journal_get_write_access(handle, gd_bh); | |
4862 | if (err) | |
4863 | goto error_return; | |
c9de560d AT |
4864 | #ifdef AGGRESSIVE_CHECK |
4865 | { | |
4866 | int i; | |
84130193 | 4867 | for (i = 0; i < count_clusters; i++) |
c9de560d AT |
4868 | BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data)); |
4869 | } | |
4870 | #endif | |
84130193 | 4871 | trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters); |
c9de560d | 4872 | |
adb7ef60 KK |
4873 | /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */ |
4874 | err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b, | |
4875 | GFP_NOFS|__GFP_NOFAIL); | |
920313a7 AK |
4876 | if (err) |
4877 | goto error_return; | |
e6362609 | 4878 | |
f96c450d DJ |
4879 | /* |
4880 | * We need to make sure we don't reuse the freed block until after the | |
4881 | * transaction is committed. We make an exception if the inode is to be | |
4882 | * written in writeback mode since writeback mode has weak data | |
4883 | * consistency guarantees. | |
4884 | */ | |
4885 | if (ext4_handle_valid(handle) && | |
4886 | ((flags & EXT4_FREE_BLOCKS_METADATA) || | |
4887 | !ext4_should_writeback_data(inode))) { | |
7a2fcbf7 AK |
4888 | struct ext4_free_data *new_entry; |
4889 | /* | |
7444a072 MH |
4890 | * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed |
4891 | * to fail. | |
7a2fcbf7 | 4892 | */ |
7444a072 MH |
4893 | new_entry = kmem_cache_alloc(ext4_free_data_cachep, |
4894 | GFP_NOFS|__GFP_NOFAIL); | |
18aadd47 BJ |
4895 | new_entry->efd_start_cluster = bit; |
4896 | new_entry->efd_group = block_group; | |
4897 | new_entry->efd_count = count_clusters; | |
4898 | new_entry->efd_tid = handle->h_transaction->t_tid; | |
955ce5f5 | 4899 | |
7a2fcbf7 | 4900 | ext4_lock_group(sb, block_group); |
84130193 | 4901 | mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); |
7a2fcbf7 | 4902 | ext4_mb_free_metadata(handle, &e4b, new_entry); |
c9de560d | 4903 | } else { |
7a2fcbf7 AK |
4904 | /* need to update group_info->bb_free and bitmap |
4905 | * with group lock held. generate_buddy look at | |
4906 | * them with group lock_held | |
4907 | */ | |
d71c1ae2 | 4908 | if (test_opt(sb, DISCARD)) { |
a0154344 DJ |
4909 | err = ext4_issue_discard(sb, block_group, bit, count, |
4910 | NULL); | |
d71c1ae2 LC |
4911 | if (err && err != -EOPNOTSUPP) |
4912 | ext4_msg(sb, KERN_WARNING, "discard request in" | |
4913 | " group:%d block:%d count:%lu failed" | |
4914 | " with %d", block_group, bit, count, | |
4915 | err); | |
8f9ff189 LC |
4916 | } else |
4917 | EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info); | |
d71c1ae2 | 4918 | |
955ce5f5 | 4919 | ext4_lock_group(sb, block_group); |
84130193 TT |
4920 | mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); |
4921 | mb_free_blocks(inode, &e4b, bit, count_clusters); | |
c9de560d AT |
4922 | } |
4923 | ||
021b65bb TT |
4924 | ret = ext4_free_group_clusters(sb, gdp) + count_clusters; |
4925 | ext4_free_group_clusters_set(sb, gdp, ret); | |
79f1ba49 | 4926 | ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh); |
feb0ab32 | 4927 | ext4_group_desc_csum_set(sb, block_group, gdp); |
955ce5f5 | 4928 | ext4_unlock_group(sb, block_group); |
c9de560d | 4929 | |
772cb7c8 JS |
4930 | if (sbi->s_log_groups_per_flex) { |
4931 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); | |
90ba983f TT |
4932 | atomic64_add(count_clusters, |
4933 | &sbi->s_flex_groups[flex_group].free_clusters); | |
772cb7c8 JS |
4934 | } |
4935 | ||
71d4f7d0 | 4936 | if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE)) |
7b415bf6 | 4937 | dquot_free_block(inode, EXT4_C2B(sbi, count_clusters)); |
7d734532 JK |
4938 | percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters); |
4939 | ||
4940 | ext4_mb_unload_buddy(&e4b); | |
7b415bf6 | 4941 | |
7a2fcbf7 AK |
4942 | /* We dirtied the bitmap block */ |
4943 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); | |
4944 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); | |
4945 | ||
c9de560d AT |
4946 | /* And the group descriptor block */ |
4947 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); | |
0390131b | 4948 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
c9de560d AT |
4949 | if (!err) |
4950 | err = ret; | |
4951 | ||
4952 | if (overflow && !err) { | |
4953 | block += count; | |
4954 | count = overflow; | |
4955 | put_bh(bitmap_bh); | |
4956 | goto do_more; | |
4957 | } | |
c9de560d AT |
4958 | error_return: |
4959 | brelse(bitmap_bh); | |
4960 | ext4_std_error(sb, err); | |
4961 | return; | |
4962 | } | |
7360d173 | 4963 | |
2846e820 | 4964 | /** |
0529155e | 4965 | * ext4_group_add_blocks() -- Add given blocks to an existing group |
2846e820 AG |
4966 | * @handle: handle to this transaction |
4967 | * @sb: super block | |
4907cb7b | 4968 | * @block: start physical block to add to the block group |
2846e820 AG |
4969 | * @count: number of blocks to free |
4970 | * | |
e73a347b | 4971 | * This marks the blocks as free in the bitmap and buddy. |
2846e820 | 4972 | */ |
cc7365df | 4973 | int ext4_group_add_blocks(handle_t *handle, struct super_block *sb, |
2846e820 AG |
4974 | ext4_fsblk_t block, unsigned long count) |
4975 | { | |
4976 | struct buffer_head *bitmap_bh = NULL; | |
4977 | struct buffer_head *gd_bh; | |
4978 | ext4_group_t block_group; | |
4979 | ext4_grpblk_t bit; | |
4980 | unsigned int i; | |
4981 | struct ext4_group_desc *desc; | |
4982 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
e73a347b | 4983 | struct ext4_buddy e4b; |
d77147ff | 4984 | int err = 0, ret, free_clusters_count; |
4985 | ext4_grpblk_t clusters_freed; | |
4986 | ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block); | |
4987 | ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1); | |
4988 | unsigned long cluster_count = last_cluster - first_cluster + 1; | |
2846e820 AG |
4989 | |
4990 | ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1); | |
4991 | ||
4740b830 YY |
4992 | if (count == 0) |
4993 | return 0; | |
4994 | ||
2846e820 | 4995 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
2846e820 AG |
4996 | /* |
4997 | * Check to see if we are freeing blocks across a group | |
4998 | * boundary. | |
4999 | */ | |
d77147ff | 5000 | if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) { |
5001 | ext4_warning(sb, "too many blocks added to group %u", | |
cc7365df YY |
5002 | block_group); |
5003 | err = -EINVAL; | |
2846e820 | 5004 | goto error_return; |
cc7365df | 5005 | } |
2cd05cc3 | 5006 | |
2846e820 | 5007 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
9008a58e DW |
5008 | if (IS_ERR(bitmap_bh)) { |
5009 | err = PTR_ERR(bitmap_bh); | |
5010 | bitmap_bh = NULL; | |
2846e820 | 5011 | goto error_return; |
cc7365df YY |
5012 | } |
5013 | ||
2846e820 | 5014 | desc = ext4_get_group_desc(sb, block_group, &gd_bh); |
cc7365df YY |
5015 | if (!desc) { |
5016 | err = -EIO; | |
2846e820 | 5017 | goto error_return; |
cc7365df | 5018 | } |
2846e820 AG |
5019 | |
5020 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || | |
5021 | in_range(ext4_inode_bitmap(sb, desc), block, count) || | |
5022 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || | |
5023 | in_range(block + count - 1, ext4_inode_table(sb, desc), | |
5024 | sbi->s_itb_per_group)) { | |
5025 | ext4_error(sb, "Adding blocks in system zones - " | |
5026 | "Block = %llu, count = %lu", | |
5027 | block, count); | |
cc7365df | 5028 | err = -EINVAL; |
2846e820 AG |
5029 | goto error_return; |
5030 | } | |
5031 | ||
2cd05cc3 TT |
5032 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
5033 | err = ext4_journal_get_write_access(handle, bitmap_bh); | |
2846e820 AG |
5034 | if (err) |
5035 | goto error_return; | |
5036 | ||
5037 | /* | |
5038 | * We are about to modify some metadata. Call the journal APIs | |
5039 | * to unshare ->b_data if a currently-committing transaction is | |
5040 | * using it | |
5041 | */ | |
5042 | BUFFER_TRACE(gd_bh, "get_write_access"); | |
5043 | err = ext4_journal_get_write_access(handle, gd_bh); | |
5044 | if (err) | |
5045 | goto error_return; | |
e73a347b | 5046 | |
d77147ff | 5047 | for (i = 0, clusters_freed = 0; i < cluster_count; i++) { |
2846e820 | 5048 | BUFFER_TRACE(bitmap_bh, "clear bit"); |
e73a347b | 5049 | if (!mb_test_bit(bit + i, bitmap_bh->b_data)) { |
2846e820 AG |
5050 | ext4_error(sb, "bit already cleared for block %llu", |
5051 | (ext4_fsblk_t)(block + i)); | |
5052 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); | |
5053 | } else { | |
d77147ff | 5054 | clusters_freed++; |
2846e820 AG |
5055 | } |
5056 | } | |
e73a347b AG |
5057 | |
5058 | err = ext4_mb_load_buddy(sb, block_group, &e4b); | |
5059 | if (err) | |
5060 | goto error_return; | |
5061 | ||
5062 | /* | |
5063 | * need to update group_info->bb_free and bitmap | |
5064 | * with group lock held. generate_buddy look at | |
5065 | * them with group lock_held | |
5066 | */ | |
2846e820 | 5067 | ext4_lock_group(sb, block_group); |
d77147ff | 5068 | mb_clear_bits(bitmap_bh->b_data, bit, cluster_count); |
5069 | mb_free_blocks(NULL, &e4b, bit, cluster_count); | |
5070 | free_clusters_count = clusters_freed + | |
5071 | ext4_free_group_clusters(sb, desc); | |
5072 | ext4_free_group_clusters_set(sb, desc, free_clusters_count); | |
79f1ba49 | 5073 | ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh); |
feb0ab32 | 5074 | ext4_group_desc_csum_set(sb, block_group, desc); |
2846e820 | 5075 | ext4_unlock_group(sb, block_group); |
57042651 | 5076 | percpu_counter_add(&sbi->s_freeclusters_counter, |
d77147ff | 5077 | clusters_freed); |
2846e820 AG |
5078 | |
5079 | if (sbi->s_log_groups_per_flex) { | |
5080 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); | |
d77147ff | 5081 | atomic64_add(clusters_freed, |
90ba983f | 5082 | &sbi->s_flex_groups[flex_group].free_clusters); |
2846e820 | 5083 | } |
e73a347b AG |
5084 | |
5085 | ext4_mb_unload_buddy(&e4b); | |
2846e820 AG |
5086 | |
5087 | /* We dirtied the bitmap block */ | |
5088 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); | |
5089 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); | |
5090 | ||
5091 | /* And the group descriptor block */ | |
5092 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); | |
5093 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); | |
5094 | if (!err) | |
5095 | err = ret; | |
5096 | ||
5097 | error_return: | |
5098 | brelse(bitmap_bh); | |
5099 | ext4_std_error(sb, err); | |
cc7365df | 5100 | return err; |
2846e820 AG |
5101 | } |
5102 | ||
7360d173 LC |
5103 | /** |
5104 | * ext4_trim_extent -- function to TRIM one single free extent in the group | |
5105 | * @sb: super block for the file system | |
5106 | * @start: starting block of the free extent in the alloc. group | |
5107 | * @count: number of blocks to TRIM | |
5108 | * @group: alloc. group we are working with | |
5109 | * @e4b: ext4 buddy for the group | |
5110 | * | |
5111 | * Trim "count" blocks starting at "start" in the "group". To assure that no | |
5112 | * one will allocate those blocks, mark it as used in buddy bitmap. This must | |
5113 | * be called with under the group lock. | |
5114 | */ | |
d71c1ae2 | 5115 | static int ext4_trim_extent(struct super_block *sb, int start, int count, |
d9f34504 | 5116 | ext4_group_t group, struct ext4_buddy *e4b) |
e2cbd587 | 5117 | __releases(bitlock) |
5118 | __acquires(bitlock) | |
7360d173 LC |
5119 | { |
5120 | struct ext4_free_extent ex; | |
d71c1ae2 | 5121 | int ret = 0; |
7360d173 | 5122 | |
b3d4c2b1 TM |
5123 | trace_ext4_trim_extent(sb, group, start, count); |
5124 | ||
7360d173 LC |
5125 | assert_spin_locked(ext4_group_lock_ptr(sb, group)); |
5126 | ||
5127 | ex.fe_start = start; | |
5128 | ex.fe_group = group; | |
5129 | ex.fe_len = count; | |
5130 | ||
5131 | /* | |
5132 | * Mark blocks used, so no one can reuse them while | |
5133 | * being trimmed. | |
5134 | */ | |
5135 | mb_mark_used(e4b, &ex); | |
5136 | ext4_unlock_group(sb, group); | |
a0154344 | 5137 | ret = ext4_issue_discard(sb, group, start, count, NULL); |
7360d173 LC |
5138 | ext4_lock_group(sb, group); |
5139 | mb_free_blocks(NULL, e4b, start, ex.fe_len); | |
d71c1ae2 | 5140 | return ret; |
7360d173 LC |
5141 | } |
5142 | ||
5143 | /** | |
5144 | * ext4_trim_all_free -- function to trim all free space in alloc. group | |
5145 | * @sb: super block for file system | |
22612283 | 5146 | * @group: group to be trimmed |
7360d173 LC |
5147 | * @start: first group block to examine |
5148 | * @max: last group block to examine | |
5149 | * @minblocks: minimum extent block count | |
5150 | * | |
5151 | * ext4_trim_all_free walks through group's buddy bitmap searching for free | |
5152 | * extents. When the free block is found, ext4_trim_extent is called to TRIM | |
5153 | * the extent. | |
5154 | * | |
5155 | * | |
5156 | * ext4_trim_all_free walks through group's block bitmap searching for free | |
5157 | * extents. When the free extent is found, mark it as used in group buddy | |
5158 | * bitmap. Then issue a TRIM command on this extent and free the extent in | |
5159 | * the group buddy bitmap. This is done until whole group is scanned. | |
5160 | */ | |
0b75a840 | 5161 | static ext4_grpblk_t |
78944086 LC |
5162 | ext4_trim_all_free(struct super_block *sb, ext4_group_t group, |
5163 | ext4_grpblk_t start, ext4_grpblk_t max, | |
5164 | ext4_grpblk_t minblocks) | |
7360d173 LC |
5165 | { |
5166 | void *bitmap; | |
169ddc3e | 5167 | ext4_grpblk_t next, count = 0, free_count = 0; |
78944086 | 5168 | struct ext4_buddy e4b; |
d71c1ae2 | 5169 | int ret = 0; |
7360d173 | 5170 | |
b3d4c2b1 TM |
5171 | trace_ext4_trim_all_free(sb, group, start, max); |
5172 | ||
78944086 LC |
5173 | ret = ext4_mb_load_buddy(sb, group, &e4b); |
5174 | if (ret) { | |
9651e6b2 KK |
5175 | ext4_warning(sb, "Error %d loading buddy information for %u", |
5176 | ret, group); | |
78944086 LC |
5177 | return ret; |
5178 | } | |
78944086 | 5179 | bitmap = e4b.bd_bitmap; |
28739eea LC |
5180 | |
5181 | ext4_lock_group(sb, group); | |
3d56b8d2 TM |
5182 | if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) && |
5183 | minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks)) | |
5184 | goto out; | |
5185 | ||
78944086 LC |
5186 | start = (e4b.bd_info->bb_first_free > start) ? |
5187 | e4b.bd_info->bb_first_free : start; | |
7360d173 | 5188 | |
913eed83 LC |
5189 | while (start <= max) { |
5190 | start = mb_find_next_zero_bit(bitmap, max + 1, start); | |
5191 | if (start > max) | |
7360d173 | 5192 | break; |
913eed83 | 5193 | next = mb_find_next_bit(bitmap, max + 1, start); |
7360d173 LC |
5194 | |
5195 | if ((next - start) >= minblocks) { | |
d71c1ae2 LC |
5196 | ret = ext4_trim_extent(sb, start, |
5197 | next - start, group, &e4b); | |
5198 | if (ret && ret != -EOPNOTSUPP) | |
5199 | break; | |
5200 | ret = 0; | |
7360d173 LC |
5201 | count += next - start; |
5202 | } | |
169ddc3e | 5203 | free_count += next - start; |
7360d173 LC |
5204 | start = next + 1; |
5205 | ||
5206 | if (fatal_signal_pending(current)) { | |
5207 | count = -ERESTARTSYS; | |
5208 | break; | |
5209 | } | |
5210 | ||
5211 | if (need_resched()) { | |
5212 | ext4_unlock_group(sb, group); | |
5213 | cond_resched(); | |
5214 | ext4_lock_group(sb, group); | |
5215 | } | |
5216 | ||
169ddc3e | 5217 | if ((e4b.bd_info->bb_free - free_count) < minblocks) |
7360d173 LC |
5218 | break; |
5219 | } | |
3d56b8d2 | 5220 | |
d71c1ae2 LC |
5221 | if (!ret) { |
5222 | ret = count; | |
3d56b8d2 | 5223 | EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); |
d71c1ae2 | 5224 | } |
3d56b8d2 | 5225 | out: |
7360d173 | 5226 | ext4_unlock_group(sb, group); |
78944086 | 5227 | ext4_mb_unload_buddy(&e4b); |
7360d173 LC |
5228 | |
5229 | ext4_debug("trimmed %d blocks in the group %d\n", | |
5230 | count, group); | |
5231 | ||
d71c1ae2 | 5232 | return ret; |
7360d173 LC |
5233 | } |
5234 | ||
5235 | /** | |
5236 | * ext4_trim_fs() -- trim ioctl handle function | |
5237 | * @sb: superblock for filesystem | |
5238 | * @range: fstrim_range structure | |
5239 | * | |
5240 | * start: First Byte to trim | |
5241 | * len: number of Bytes to trim from start | |
5242 | * minlen: minimum extent length in Bytes | |
5243 | * ext4_trim_fs goes through all allocation groups containing Bytes from | |
5244 | * start to start+len. For each such a group ext4_trim_all_free function | |
5245 | * is invoked to trim all free space. | |
5246 | */ | |
5247 | int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) | |
5248 | { | |
78944086 | 5249 | struct ext4_group_info *grp; |
913eed83 | 5250 | ext4_group_t group, first_group, last_group; |
7137d7a4 | 5251 | ext4_grpblk_t cnt = 0, first_cluster, last_cluster; |
913eed83 | 5252 | uint64_t start, end, minlen, trimmed = 0; |
0f0a25bf JK |
5253 | ext4_fsblk_t first_data_blk = |
5254 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); | |
913eed83 | 5255 | ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es); |
7360d173 LC |
5256 | int ret = 0; |
5257 | ||
5258 | start = range->start >> sb->s_blocksize_bits; | |
913eed83 | 5259 | end = start + (range->len >> sb->s_blocksize_bits) - 1; |
aaf7d73e LC |
5260 | minlen = EXT4_NUM_B2C(EXT4_SB(sb), |
5261 | range->minlen >> sb->s_blocksize_bits); | |
7360d173 | 5262 | |
5de35e8d LC |
5263 | if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) || |
5264 | start >= max_blks || | |
5265 | range->len < sb->s_blocksize) | |
7360d173 | 5266 | return -EINVAL; |
913eed83 LC |
5267 | if (end >= max_blks) |
5268 | end = max_blks - 1; | |
5269 | if (end <= first_data_blk) | |
22f10457 | 5270 | goto out; |
913eed83 | 5271 | if (start < first_data_blk) |
0f0a25bf | 5272 | start = first_data_blk; |
7360d173 | 5273 | |
913eed83 | 5274 | /* Determine first and last group to examine based on start and end */ |
7360d173 | 5275 | ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start, |
7137d7a4 | 5276 | &first_group, &first_cluster); |
913eed83 | 5277 | ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end, |
7137d7a4 | 5278 | &last_group, &last_cluster); |
7360d173 | 5279 | |
913eed83 LC |
5280 | /* end now represents the last cluster to discard in this group */ |
5281 | end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; | |
7360d173 LC |
5282 | |
5283 | for (group = first_group; group <= last_group; group++) { | |
78944086 LC |
5284 | grp = ext4_get_group_info(sb, group); |
5285 | /* We only do this if the grp has never been initialized */ | |
5286 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { | |
adb7ef60 | 5287 | ret = ext4_mb_init_group(sb, group, GFP_NOFS); |
78944086 LC |
5288 | if (ret) |
5289 | break; | |
7360d173 LC |
5290 | } |
5291 | ||
0ba08517 | 5292 | /* |
913eed83 LC |
5293 | * For all the groups except the last one, last cluster will |
5294 | * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to | |
5295 | * change it for the last group, note that last_cluster is | |
5296 | * already computed earlier by ext4_get_group_no_and_offset() | |
0ba08517 | 5297 | */ |
913eed83 LC |
5298 | if (group == last_group) |
5299 | end = last_cluster; | |
7360d173 | 5300 | |
78944086 | 5301 | if (grp->bb_free >= minlen) { |
7137d7a4 | 5302 | cnt = ext4_trim_all_free(sb, group, first_cluster, |
913eed83 | 5303 | end, minlen); |
7360d173 LC |
5304 | if (cnt < 0) { |
5305 | ret = cnt; | |
7360d173 LC |
5306 | break; |
5307 | } | |
21e7fd22 | 5308 | trimmed += cnt; |
7360d173 | 5309 | } |
913eed83 LC |
5310 | |
5311 | /* | |
5312 | * For every group except the first one, we are sure | |
5313 | * that the first cluster to discard will be cluster #0. | |
5314 | */ | |
7137d7a4 | 5315 | first_cluster = 0; |
7360d173 | 5316 | } |
7360d173 | 5317 | |
3d56b8d2 TM |
5318 | if (!ret) |
5319 | atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen); | |
5320 | ||
22f10457 | 5321 | out: |
aaf7d73e | 5322 | range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits; |
7360d173 LC |
5323 | return ret; |
5324 | } | |
0c9ec4be DW |
5325 | |
5326 | /* Iterate all the free extents in the group. */ | |
5327 | int | |
5328 | ext4_mballoc_query_range( | |
5329 | struct super_block *sb, | |
5330 | ext4_group_t group, | |
5331 | ext4_grpblk_t start, | |
5332 | ext4_grpblk_t end, | |
5333 | ext4_mballoc_query_range_fn formatter, | |
5334 | void *priv) | |
5335 | { | |
5336 | void *bitmap; | |
5337 | ext4_grpblk_t next; | |
5338 | struct ext4_buddy e4b; | |
5339 | int error; | |
5340 | ||
5341 | error = ext4_mb_load_buddy(sb, group, &e4b); | |
5342 | if (error) | |
5343 | return error; | |
5344 | bitmap = e4b.bd_bitmap; | |
5345 | ||
5346 | ext4_lock_group(sb, group); | |
5347 | ||
5348 | start = (e4b.bd_info->bb_first_free > start) ? | |
5349 | e4b.bd_info->bb_first_free : start; | |
5350 | if (end >= EXT4_CLUSTERS_PER_GROUP(sb)) | |
5351 | end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; | |
5352 | ||
5353 | while (start <= end) { | |
5354 | start = mb_find_next_zero_bit(bitmap, end + 1, start); | |
5355 | if (start > end) | |
5356 | break; | |
5357 | next = mb_find_next_bit(bitmap, end + 1, start); | |
5358 | ||
5359 | ext4_unlock_group(sb, group); | |
5360 | error = formatter(sb, group, start, next - start, priv); | |
5361 | if (error) | |
5362 | goto out_unload; | |
5363 | ext4_lock_group(sb, group); | |
5364 | ||
5365 | start = next + 1; | |
5366 | } | |
5367 | ||
5368 | ext4_unlock_group(sb, group); | |
5369 | out_unload: | |
5370 | ext4_mb_unload_buddy(&e4b); | |
5371 | ||
5372 | return error; | |
5373 | } |