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