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