blob: c17063ddb3079082a2b9162ace2789316528fa4b [file] [log] [blame]
Alex Tomasc9de5602008-01-29 00:19:52 -05001/*
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
Mingming Cao8f6e39a2008-04-29 22:01:31 -040024#include "mballoc.h"
Alex Tomasc9de5602008-01-29 00:19:52 -050025/*
26 * MUSTDO:
27 * - test ext4_ext_search_left() and ext4_ext_search_right()
28 * - search for metadata in few groups
29 *
30 * TODO v4:
31 * - normalization should take into account whether file is still open
32 * - discard preallocations if no free space left (policy?)
33 * - don't normalize tails
34 * - quota
35 * - reservation for superuser
36 *
37 * TODO v3:
38 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
39 * - track min/max extents in each group for better group selection
40 * - mb_mark_used() may allocate chunk right after splitting buddy
41 * - tree of groups sorted by number of free blocks
42 * - error handling
43 */
44
45/*
46 * The allocation request involve request for multiple number of blocks
47 * near to the goal(block) value specified.
48 *
49 * During initialization phase of the allocator we decide to use the group
50 * preallocation or inode preallocation depending on the size file. The
51 * size of the file could be the resulting file size we would have after
52 * allocation or the current file size which ever is larger. If the size is
53 * less that sbi->s_mb_stream_request we select the group
54 * preallocation. The default value of s_mb_stream_request is 16
55 * blocks. This can also be tuned via
56 * /proc/fs/ext4/<partition>/stream_req. The value is represented in terms
57 * of number of blocks.
58 *
59 * The main motivation for having small file use group preallocation is to
60 * ensure that we have small file closer in the disk.
61 *
62 * First stage the allocator looks at the inode prealloc list
63 * ext4_inode_info->i_prealloc_list contain list of prealloc spaces for
64 * this particular inode. The inode prealloc space is represented as:
65 *
66 * pa_lstart -> the logical start block for this prealloc space
67 * pa_pstart -> the physical start block for this prealloc space
68 * pa_len -> lenght for this prealloc space
69 * pa_free -> free space available in this prealloc space
70 *
71 * The inode preallocation space is used looking at the _logical_ start
72 * block. If only the logical file block falls within the range of prealloc
73 * space we will consume the particular prealloc space. This make sure that
74 * that the we have contiguous physical blocks representing the file blocks
75 *
76 * The important thing to be noted in case of inode prealloc space is that
77 * we don't modify the values associated to inode prealloc space except
78 * pa_free.
79 *
80 * If we are not able to find blocks in the inode prealloc space and if we
81 * have the group allocation flag set then we look at the locality group
82 * prealloc space. These are per CPU prealloc list repreasented as
83 *
84 * ext4_sb_info.s_locality_groups[smp_processor_id()]
85 *
86 * The reason for having a per cpu locality group is to reduce the contention
87 * between CPUs. It is possible to get scheduled at this point.
88 *
89 * The locality group prealloc space is used looking at whether we have
90 * enough free space (pa_free) withing the prealloc space.
91 *
92 * If we can't allocate blocks via inode prealloc or/and locality group
93 * prealloc then we look at the buddy cache. The buddy cache is represented
94 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
95 * mapped to the buddy and bitmap information regarding different
96 * groups. The buddy information is attached to buddy cache inode so that
97 * we can access them through the page cache. The information regarding
98 * each group is loaded via ext4_mb_load_buddy. The information involve
99 * block bitmap and buddy information. The information are stored in the
100 * inode as:
101 *
102 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500103 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500104 *
105 *
106 * one block each for bitmap and buddy information. So for each group we
107 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
108 * blocksize) blocks. So it can have information regarding groups_per_page
109 * which is blocks_per_page/2
110 *
111 * The buddy cache inode is not stored on disk. The inode is thrown
112 * away when the filesystem is unmounted.
113 *
114 * We look for count number of blocks in the buddy cache. If we were able
115 * to locate that many free blocks we return with additional information
116 * regarding rest of the contiguous physical block available
117 *
118 * Before allocating blocks via buddy cache we normalize the request
119 * blocks. This ensure we ask for more blocks that we needed. The extra
120 * blocks that we get after allocation is added to the respective prealloc
121 * list. In case of inode preallocation we follow a list of heuristics
122 * based on file size. This can be found in ext4_mb_normalize_request. If
123 * we are doing a group prealloc we try to normalize the request to
124 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is set to
125 * 512 blocks. This can be tuned via
126 * /proc/fs/ext4/<partition/group_prealloc. The value is represented in
127 * terms of number of blocks. If we have mounted the file system with -O
128 * stripe=<value> option the group prealloc request is normalized to the
129 * stripe value (sbi->s_stripe)
130 *
131 * The regular allocator(using the buddy cache) support few tunables.
132 *
133 * /proc/fs/ext4/<partition>/min_to_scan
134 * /proc/fs/ext4/<partition>/max_to_scan
135 * /proc/fs/ext4/<partition>/order2_req
136 *
137 * The regular allocator use buddy scan only if the request len is power of
138 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
139 * value of s_mb_order2_reqs can be tuned via
140 * /proc/fs/ext4/<partition>/order2_req. If the request len is equal to
141 * stripe size (sbi->s_stripe), we try to search for contigous block in
142 * stripe size. This should result in better allocation on RAID setup. If
143 * not we search in the specific group using bitmap for best extents. The
144 * tunable min_to_scan and max_to_scan controll the behaviour here.
145 * min_to_scan indicate how long the mballoc __must__ look for a best
146 * extent and max_to_scanindicate how long the mballoc __can__ look for a
147 * best extent in the found extents. Searching for the blocks starts with
148 * the group specified as the goal value in allocation context via
149 * ac_g_ex. Each group is first checked based on the criteria whether it
150 * can used for allocation. ext4_mb_good_group explains how the groups are
151 * checked.
152 *
153 * Both the prealloc space are getting populated as above. So for the first
154 * request we will hit the buddy cache which will result in this prealloc
155 * space getting filled. The prealloc space is then later used for the
156 * subsequent request.
157 */
158
159/*
160 * mballoc operates on the following data:
161 * - on-disk bitmap
162 * - in-core buddy (actually includes buddy and bitmap)
163 * - preallocation descriptors (PAs)
164 *
165 * there are two types of preallocations:
166 * - inode
167 * assiged to specific inode and can be used for this inode only.
168 * it describes part of inode's space preallocated to specific
169 * physical blocks. any block from that preallocated can be used
170 * independent. the descriptor just tracks number of blocks left
171 * unused. so, before taking some block from descriptor, one must
172 * make sure corresponded logical block isn't allocated yet. this
173 * also means that freeing any block within descriptor's range
174 * must discard all preallocated blocks.
175 * - locality group
176 * assigned to specific locality group which does not translate to
177 * permanent set of inodes: inode can join and leave group. space
178 * from this type of preallocation can be used for any inode. thus
179 * it's consumed from the beginning to the end.
180 *
181 * relation between them can be expressed as:
182 * in-core buddy = on-disk bitmap + preallocation descriptors
183 *
184 * this mean blocks mballoc considers used are:
185 * - allocated blocks (persistent)
186 * - preallocated blocks (non-persistent)
187 *
188 * consistency in mballoc world means that at any time a block is either
189 * free or used in ALL structures. notice: "any time" should not be read
190 * literally -- time is discrete and delimited by locks.
191 *
192 * to keep it simple, we don't use block numbers, instead we count number of
193 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
194 *
195 * all operations can be expressed as:
196 * - init buddy: buddy = on-disk + PAs
197 * - new PA: buddy += N; PA = N
198 * - use inode PA: on-disk += N; PA -= N
199 * - discard inode PA buddy -= on-disk - PA; PA = 0
200 * - use locality group PA on-disk += N; PA -= N
201 * - discard locality group PA buddy -= PA; PA = 0
202 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
203 * is used in real operation because we can't know actual used
204 * bits from PA, only from on-disk bitmap
205 *
206 * if we follow this strict logic, then all operations above should be atomic.
207 * given some of them can block, we'd have to use something like semaphores
208 * killing performance on high-end SMP hardware. let's try to relax it using
209 * the following knowledge:
210 * 1) if buddy is referenced, it's already initialized
211 * 2) while block is used in buddy and the buddy is referenced,
212 * nobody can re-allocate that block
213 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
214 * bit set and PA claims same block, it's OK. IOW, one can set bit in
215 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
216 * block
217 *
218 * so, now we're building a concurrency table:
219 * - init buddy vs.
220 * - new PA
221 * blocks for PA are allocated in the buddy, buddy must be referenced
222 * until PA is linked to allocation group to avoid concurrent buddy init
223 * - use inode PA
224 * we need to make sure that either on-disk bitmap or PA has uptodate data
225 * given (3) we care that PA-=N operation doesn't interfere with init
226 * - discard inode PA
227 * the simplest way would be to have buddy initialized by the discard
228 * - use locality group PA
229 * again PA-=N must be serialized with init
230 * - discard locality group PA
231 * the simplest way would be to have buddy initialized by the discard
232 * - new PA vs.
233 * - use inode PA
234 * i_data_sem serializes them
235 * - discard inode PA
236 * discard process must wait until PA isn't used by another process
237 * - use locality group PA
238 * some mutex should serialize them
239 * - discard locality group PA
240 * discard process must wait until PA isn't used by another process
241 * - use inode PA
242 * - use inode PA
243 * i_data_sem or another mutex should serializes them
244 * - discard inode PA
245 * discard process must wait until PA isn't used by another process
246 * - use locality group PA
247 * nothing wrong here -- they're different PAs covering different blocks
248 * - discard locality group PA
249 * discard process must wait until PA isn't used by another process
250 *
251 * now we're ready to make few consequences:
252 * - PA is referenced and while it is no discard is possible
253 * - PA is referenced until block isn't marked in on-disk bitmap
254 * - PA changes only after on-disk bitmap
255 * - discard must not compete with init. either init is done before
256 * any discard or they're serialized somehow
257 * - buddy init as sum of on-disk bitmap and PAs is done atomically
258 *
259 * a special case when we've used PA to emptiness. no need to modify buddy
260 * in this case, but we should care about concurrent init
261 *
262 */
263
264 /*
265 * Logic in few words:
266 *
267 * - allocation:
268 * load group
269 * find blocks
270 * mark bits in on-disk bitmap
271 * release group
272 *
273 * - use preallocation:
274 * find proper PA (per-inode or group)
275 * load group
276 * mark bits in on-disk bitmap
277 * release group
278 * release PA
279 *
280 * - free:
281 * load group
282 * mark bits in on-disk bitmap
283 * release group
284 *
285 * - discard preallocations in group:
286 * mark PAs deleted
287 * move them onto local list
288 * load on-disk bitmap
289 * load group
290 * remove PA from object (inode or locality group)
291 * mark free blocks in-core
292 *
293 * - discard inode's preallocations:
294 */
295
296/*
297 * Locking rules
298 *
299 * Locks:
300 * - bitlock on a group (group)
301 * - object (inode/locality) (object)
302 * - per-pa lock (pa)
303 *
304 * Paths:
305 * - new pa
306 * object
307 * group
308 *
309 * - find and use pa:
310 * pa
311 *
312 * - release consumed pa:
313 * pa
314 * group
315 * object
316 *
317 * - generate in-core bitmap:
318 * group
319 * pa
320 *
321 * - discard all for given object (inode, locality group):
322 * object
323 * pa
324 * group
325 *
326 * - discard all for given group:
327 * group
328 * pa
329 * group
330 * object
331 *
332 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500333static struct kmem_cache *ext4_pspace_cachep;
334static struct kmem_cache *ext4_ac_cachep;
335static struct kmem_cache *ext4_free_ext_cachep;
336static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
337 ext4_group_t group);
338static int ext4_mb_init_per_dev_proc(struct super_block *sb);
339static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
340static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
341
342
Alex Tomasc9de5602008-01-29 00:19:52 -0500343
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500344static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
345{
Alex Tomasc9de5602008-01-29 00:19:52 -0500346#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500347 *bit += ((unsigned long) addr & 7UL) << 3;
348 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500349#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500350 *bit += ((unsigned long) addr & 3UL) << 3;
351 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500352#else
353#error "how many bits you are?!"
354#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500355 return addr;
356}
Alex Tomasc9de5602008-01-29 00:19:52 -0500357
358static inline int mb_test_bit(int bit, void *addr)
359{
360 /*
361 * ext4_test_bit on architecture like powerpc
362 * needs unsigned long aligned address
363 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500364 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500365 return ext4_test_bit(bit, addr);
366}
367
368static inline void mb_set_bit(int bit, void *addr)
369{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500370 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500371 ext4_set_bit(bit, addr);
372}
373
374static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
375{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500376 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500377 ext4_set_bit_atomic(lock, bit, addr);
378}
379
380static inline void mb_clear_bit(int bit, void *addr)
381{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500382 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500383 ext4_clear_bit(bit, addr);
384}
385
386static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
387{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500388 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500389 ext4_clear_bit_atomic(lock, bit, addr);
390}
391
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500392static inline int mb_find_next_zero_bit(void *addr, int max, int start)
393{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400394 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500395 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400396 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500397 start += fix;
398
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400399 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
400 if (ret > max)
401 return max;
402 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500403}
404
405static inline int mb_find_next_bit(void *addr, int max, int start)
406{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400407 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500408 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400409 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500410 start += fix;
411
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400412 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
413 if (ret > max)
414 return max;
415 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500416}
417
Alex Tomasc9de5602008-01-29 00:19:52 -0500418static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
419{
420 char *bb;
421
Alex Tomasc9de5602008-01-29 00:19:52 -0500422 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
423 BUG_ON(max == NULL);
424
425 if (order > e4b->bd_blkbits + 1) {
426 *max = 0;
427 return NULL;
428 }
429
430 /* at order 0 we see each particular block */
431 *max = 1 << (e4b->bd_blkbits + 3);
432 if (order == 0)
433 return EXT4_MB_BITMAP(e4b);
434
435 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
436 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
437
438 return bb;
439}
440
441#ifdef DOUBLE_CHECK
442static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
443 int first, int count)
444{
445 int i;
446 struct super_block *sb = e4b->bd_sb;
447
448 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
449 return;
450 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
451 for (i = 0; i < count; i++) {
452 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
453 ext4_fsblk_t blocknr;
454 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
455 blocknr += first + i;
456 blocknr +=
457 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
458
Harvey Harrison46e665e2008-04-17 10:38:59 -0400459 ext4_error(sb, __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500460 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -0500461 inode ? inode->i_ino : 0, blocknr,
462 first + i, e4b->bd_group);
463 }
464 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
465 }
466}
467
468static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
469{
470 int i;
471
472 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
473 return;
474 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
475 for (i = 0; i < count; i++) {
476 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
477 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
478 }
479}
480
481static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
482{
483 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
484 unsigned char *b1, *b2;
485 int i;
486 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
487 b2 = (unsigned char *) bitmap;
488 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
489 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500490 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400491 "at byte %u(%u): %x in copy != %x "
492 "on disk/prealloc\n",
493 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500494 BUG();
495 }
496 }
497 }
498}
499
500#else
501static inline void mb_free_blocks_double(struct inode *inode,
502 struct ext4_buddy *e4b, int first, int count)
503{
504 return;
505}
506static inline void mb_mark_used_double(struct ext4_buddy *e4b,
507 int first, int count)
508{
509 return;
510}
511static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
512{
513 return;
514}
515#endif
516
517#ifdef AGGRESSIVE_CHECK
518
519#define MB_CHECK_ASSERT(assert) \
520do { \
521 if (!(assert)) { \
522 printk(KERN_EMERG \
523 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
524 function, file, line, # assert); \
525 BUG(); \
526 } \
527} while (0)
528
529static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
530 const char *function, int line)
531{
532 struct super_block *sb = e4b->bd_sb;
533 int order = e4b->bd_blkbits + 1;
534 int max;
535 int max2;
536 int i;
537 int j;
538 int k;
539 int count;
540 struct ext4_group_info *grp;
541 int fragments = 0;
542 int fstart;
543 struct list_head *cur;
544 void *buddy;
545 void *buddy2;
546
Alex Tomasc9de5602008-01-29 00:19:52 -0500547 {
548 static int mb_check_counter;
549 if (mb_check_counter++ % 100 != 0)
550 return 0;
551 }
552
553 while (order > 1) {
554 buddy = mb_find_buddy(e4b, order, &max);
555 MB_CHECK_ASSERT(buddy);
556 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
557 MB_CHECK_ASSERT(buddy2);
558 MB_CHECK_ASSERT(buddy != buddy2);
559 MB_CHECK_ASSERT(max * 2 == max2);
560
561 count = 0;
562 for (i = 0; i < max; i++) {
563
564 if (mb_test_bit(i, buddy)) {
565 /* only single bit in buddy2 may be 1 */
566 if (!mb_test_bit(i << 1, buddy2)) {
567 MB_CHECK_ASSERT(
568 mb_test_bit((i<<1)+1, buddy2));
569 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
570 MB_CHECK_ASSERT(
571 mb_test_bit(i << 1, buddy2));
572 }
573 continue;
574 }
575
576 /* both bits in buddy2 must be 0 */
577 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
578 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
579
580 for (j = 0; j < (1 << order); j++) {
581 k = (i * (1 << order)) + j;
582 MB_CHECK_ASSERT(
583 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
584 }
585 count++;
586 }
587 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
588 order--;
589 }
590
591 fstart = -1;
592 buddy = mb_find_buddy(e4b, 0, &max);
593 for (i = 0; i < max; i++) {
594 if (!mb_test_bit(i, buddy)) {
595 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
596 if (fstart == -1) {
597 fragments++;
598 fstart = i;
599 }
600 continue;
601 }
602 fstart = -1;
603 /* check used bits only */
604 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
605 buddy2 = mb_find_buddy(e4b, j, &max2);
606 k = i >> j;
607 MB_CHECK_ASSERT(k < max2);
608 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
609 }
610 }
611 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
612 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
613
614 grp = ext4_get_group_info(sb, e4b->bd_group);
615 buddy = mb_find_buddy(e4b, 0, &max);
616 list_for_each(cur, &grp->bb_prealloc_list) {
617 ext4_group_t groupnr;
618 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400619 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
620 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500621 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400622 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500623 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
624 }
625 return 0;
626}
627#undef MB_CHECK_ASSERT
628#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400629 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500630#else
631#define mb_check_buddy(e4b)
632#endif
633
634/* FIXME!! need more doc */
635static void ext4_mb_mark_free_simple(struct super_block *sb,
636 void *buddy, unsigned first, int len,
637 struct ext4_group_info *grp)
638{
639 struct ext4_sb_info *sbi = EXT4_SB(sb);
640 unsigned short min;
641 unsigned short max;
642 unsigned short chunk;
643 unsigned short border;
644
Valerie Clementb73fce62008-02-15 13:48:51 -0500645 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500646
647 border = 2 << sb->s_blocksize_bits;
648
649 while (len > 0) {
650 /* find how many blocks can be covered since this position */
651 max = ffs(first | border) - 1;
652
653 /* find how many blocks of power 2 we need to mark */
654 min = fls(len) - 1;
655
656 if (max < min)
657 min = max;
658 chunk = 1 << min;
659
660 /* mark multiblock chunks only */
661 grp->bb_counters[min]++;
662 if (min > 0)
663 mb_clear_bit(first >> min,
664 buddy + sbi->s_mb_offsets[min]);
665
666 len -= chunk;
667 first += chunk;
668 }
669}
670
671static void ext4_mb_generate_buddy(struct super_block *sb,
672 void *buddy, void *bitmap, ext4_group_t group)
673{
674 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
675 unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
676 unsigned short i = 0;
677 unsigned short first;
678 unsigned short len;
679 unsigned free = 0;
680 unsigned fragments = 0;
681 unsigned long long period = get_cycles();
682
683 /* initialize buddy from bitmap which is aggregation
684 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500685 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500686 grp->bb_first_free = i;
687 while (i < max) {
688 fragments++;
689 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500690 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500691 len = i - first;
692 free += len;
693 if (len > 1)
694 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
695 else
696 grp->bb_counters[0]++;
697 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500698 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500699 }
700 grp->bb_fragments = fragments;
701
702 if (free != grp->bb_free) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400703 ext4_error(sb, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500704 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
Alex Tomasc9de5602008-01-29 00:19:52 -0500705 group, free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500706 /*
707 * If we intent to continue, we consider group descritor
708 * corrupt and update bb_free using bitmap value
709 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500710 grp->bb_free = free;
711 }
712
713 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
714
715 period = get_cycles() - period;
716 spin_lock(&EXT4_SB(sb)->s_bal_lock);
717 EXT4_SB(sb)->s_mb_buddies_generated++;
718 EXT4_SB(sb)->s_mb_generation_time += period;
719 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
720}
721
722/* The buddy information is attached the buddy cache inode
723 * for convenience. The information regarding each group
724 * is loaded via ext4_mb_load_buddy. The information involve
725 * block bitmap and buddy information. The information are
726 * stored in the inode as
727 *
728 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500729 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500730 *
731 *
732 * one block each for bitmap and buddy information.
733 * So for each group we take up 2 blocks. A page can
734 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
735 * So it can have information regarding groups_per_page which
736 * is blocks_per_page/2
737 */
738
739static int ext4_mb_init_cache(struct page *page, char *incore)
740{
741 int blocksize;
742 int blocks_per_page;
743 int groups_per_page;
744 int err = 0;
745 int i;
746 ext4_group_t first_group;
747 int first_block;
748 struct super_block *sb;
749 struct buffer_head *bhs;
750 struct buffer_head **bh;
751 struct inode *inode;
752 char *data;
753 char *bitmap;
754
755 mb_debug("init page %lu\n", page->index);
756
757 inode = page->mapping->host;
758 sb = inode->i_sb;
759 blocksize = 1 << inode->i_blkbits;
760 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
761
762 groups_per_page = blocks_per_page >> 1;
763 if (groups_per_page == 0)
764 groups_per_page = 1;
765
766 /* allocate buffer_heads to read bitmaps */
767 if (groups_per_page > 1) {
768 err = -ENOMEM;
769 i = sizeof(struct buffer_head *) * groups_per_page;
770 bh = kzalloc(i, GFP_NOFS);
771 if (bh == NULL)
772 goto out;
773 } else
774 bh = &bhs;
775
776 first_group = page->index * blocks_per_page / 2;
777
778 /* read all groups the page covers into the cache */
779 for (i = 0; i < groups_per_page; i++) {
780 struct ext4_group_desc *desc;
781
782 if (first_group + i >= EXT4_SB(sb)->s_groups_count)
783 break;
784
785 err = -EIO;
786 desc = ext4_get_group_desc(sb, first_group + i, NULL);
787 if (desc == NULL)
788 goto out;
789
790 err = -ENOMEM;
791 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
792 if (bh[i] == NULL)
793 goto out;
794
Frederic Bohec806e682008-10-10 08:09:18 -0400795 if (buffer_uptodate(bh[i]) &&
796 !(desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))
Alex Tomasc9de5602008-01-29 00:19:52 -0500797 continue;
798
Frederic Bohec806e682008-10-10 08:09:18 -0400799 lock_buffer(bh[i]);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400800 spin_lock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500801 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
802 ext4_init_block_bitmap(sb, bh[i],
803 first_group + i, desc);
804 set_buffer_uptodate(bh[i]);
805 unlock_buffer(bh[i]);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400806 spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500807 continue;
808 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400809 spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 get_bh(bh[i]);
811 bh[i]->b_end_io = end_buffer_read_sync;
812 submit_bh(READ, bh[i]);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500813 mb_debug("read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500814 }
815
816 /* wait for I/O completion */
817 for (i = 0; i < groups_per_page && bh[i]; i++)
818 wait_on_buffer(bh[i]);
819
820 err = -EIO;
821 for (i = 0; i < groups_per_page && bh[i]; i++)
822 if (!buffer_uptodate(bh[i]))
823 goto out;
824
Mingming Cao31b481d2008-07-11 19:27:31 -0400825 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500826 first_block = page->index * blocks_per_page;
827 for (i = 0; i < blocks_per_page; i++) {
828 int group;
829 struct ext4_group_info *grinfo;
830
831 group = (first_block + i) >> 1;
832 if (group >= EXT4_SB(sb)->s_groups_count)
833 break;
834
835 /*
836 * data carry information regarding this
837 * particular group in the format specified
838 * above
839 *
840 */
841 data = page_address(page) + (i * blocksize);
842 bitmap = bh[group - first_group]->b_data;
843
844 /*
845 * We place the buddy block and bitmap block
846 * close together
847 */
848 if ((first_block + i) & 1) {
849 /* this is block of buddy */
850 BUG_ON(incore == NULL);
851 mb_debug("put buddy for group %u in page %lu/%x\n",
852 group, page->index, i * blocksize);
853 memset(data, 0xff, blocksize);
854 grinfo = ext4_get_group_info(sb, group);
855 grinfo->bb_fragments = 0;
856 memset(grinfo->bb_counters, 0,
857 sizeof(unsigned short)*(sb->s_blocksize_bits+2));
858 /*
859 * incore got set to the group block bitmap below
860 */
861 ext4_mb_generate_buddy(sb, data, incore, group);
862 incore = NULL;
863 } else {
864 /* this is block of bitmap */
865 BUG_ON(incore != NULL);
866 mb_debug("put bitmap for group %u in page %lu/%x\n",
867 group, page->index, i * blocksize);
868
869 /* see comments in ext4_mb_put_pa() */
870 ext4_lock_group(sb, group);
871 memcpy(data, bitmap, blocksize);
872
873 /* mark all preallocated blks used in in-core bitmap */
874 ext4_mb_generate_from_pa(sb, data, group);
875 ext4_unlock_group(sb, group);
876
877 /* set incore so that the buddy information can be
878 * generated using this
879 */
880 incore = data;
881 }
882 }
883 SetPageUptodate(page);
884
885out:
886 if (bh) {
887 for (i = 0; i < groups_per_page && bh[i]; i++)
888 brelse(bh[i]);
889 if (bh != &bhs)
890 kfree(bh);
891 }
892 return err;
893}
894
Eric Sandeen4ddfef72008-04-29 08:11:12 -0400895static noinline_for_stack int
896ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
897 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -0500898{
Alex Tomasc9de5602008-01-29 00:19:52 -0500899 int blocks_per_page;
900 int block;
901 int pnum;
902 int poff;
903 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400904 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500905 struct ext4_group_info *grp;
906 struct ext4_sb_info *sbi = EXT4_SB(sb);
907 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -0500908
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500909 mb_debug("load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500910
911 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500912 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500913
914 e4b->bd_blkbits = sb->s_blocksize_bits;
915 e4b->bd_info = ext4_get_group_info(sb, group);
916 e4b->bd_sb = sb;
917 e4b->bd_group = group;
918 e4b->bd_buddy_page = NULL;
919 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500920 e4b->alloc_semp = &grp->alloc_sem;
921
922 /* Take the read lock on the group alloc
923 * sem. This would make sure a parallel
924 * ext4_mb_init_group happening on other
925 * groups mapped by the page is blocked
926 * till we are done with allocation
927 */
928 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500929
930 /*
931 * the buddy cache inode stores the block bitmap
932 * and buddy information in consecutive blocks.
933 * So for each group we need two blocks.
934 */
935 block = group * 2;
936 pnum = block / blocks_per_page;
937 poff = block % blocks_per_page;
938
939 /* we could use find_or_create_page(), but it locks page
940 * what we'd like to avoid in fast path ... */
941 page = find_get_page(inode->i_mapping, pnum);
942 if (page == NULL || !PageUptodate(page)) {
943 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500944 /*
945 * drop the page reference and try
946 * to get the page with lock. If we
947 * are not uptodate that implies
948 * somebody just created the page but
949 * is yet to initialize the same. So
950 * wait for it to initialize.
951 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500952 page_cache_release(page);
953 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
954 if (page) {
955 BUG_ON(page->mapping != inode->i_mapping);
956 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400957 ret = ext4_mb_init_cache(page, NULL);
958 if (ret) {
959 unlock_page(page);
960 goto err;
961 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500962 mb_cmp_bitmaps(e4b, page_address(page) +
963 (poff * sb->s_blocksize));
964 }
965 unlock_page(page);
966 }
967 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400968 if (page == NULL || !PageUptodate(page)) {
969 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500970 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400971 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500972 e4b->bd_bitmap_page = page;
973 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
974 mark_page_accessed(page);
975
976 block++;
977 pnum = block / blocks_per_page;
978 poff = block % blocks_per_page;
979
980 page = find_get_page(inode->i_mapping, pnum);
981 if (page == NULL || !PageUptodate(page)) {
982 if (page)
983 page_cache_release(page);
984 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
985 if (page) {
986 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400987 if (!PageUptodate(page)) {
988 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
989 if (ret) {
990 unlock_page(page);
991 goto err;
992 }
993 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500994 unlock_page(page);
995 }
996 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400997 if (page == NULL || !PageUptodate(page)) {
998 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500999 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001000 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001001 e4b->bd_buddy_page = page;
1002 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1003 mark_page_accessed(page);
1004
1005 BUG_ON(e4b->bd_bitmap_page == NULL);
1006 BUG_ON(e4b->bd_buddy_page == NULL);
1007
1008 return 0;
1009
1010err:
1011 if (e4b->bd_bitmap_page)
1012 page_cache_release(e4b->bd_bitmap_page);
1013 if (e4b->bd_buddy_page)
1014 page_cache_release(e4b->bd_buddy_page);
1015 e4b->bd_buddy = NULL;
1016 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001017
1018 /* Done with the buddy cache */
1019 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001020 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001021}
1022
1023static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1024{
1025 if (e4b->bd_bitmap_page)
1026 page_cache_release(e4b->bd_bitmap_page);
1027 if (e4b->bd_buddy_page)
1028 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001029 /* Done with the buddy cache */
1030 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001031}
1032
1033
1034static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1035{
1036 int order = 1;
1037 void *bb;
1038
1039 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1040 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1041
1042 bb = EXT4_MB_BUDDY(e4b);
1043 while (order <= e4b->bd_blkbits + 1) {
1044 block = block >> 1;
1045 if (!mb_test_bit(block, bb)) {
1046 /* this block is part of buddy of order 'order' */
1047 return order;
1048 }
1049 bb += 1 << (e4b->bd_blkbits - order);
1050 order++;
1051 }
1052 return 0;
1053}
1054
1055static void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1056{
1057 __u32 *addr;
1058
1059 len = cur + len;
1060 while (cur < len) {
1061 if ((cur & 31) == 0 && (len - cur) >= 32) {
1062 /* fast path: clear whole word at once */
1063 addr = bm + (cur >> 3);
1064 *addr = 0;
1065 cur += 32;
1066 continue;
1067 }
1068 mb_clear_bit_atomic(lock, cur, bm);
1069 cur++;
1070 }
1071}
1072
1073static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1074{
1075 __u32 *addr;
1076
1077 len = cur + len;
1078 while (cur < len) {
1079 if ((cur & 31) == 0 && (len - cur) >= 32) {
1080 /* fast path: set whole word at once */
1081 addr = bm + (cur >> 3);
1082 *addr = 0xffffffff;
1083 cur += 32;
1084 continue;
1085 }
1086 mb_set_bit_atomic(lock, cur, bm);
1087 cur++;
1088 }
1089}
1090
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001091static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001092 int first, int count)
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05001093__releases(bitlock)
1094__acquires(bitlock)
Alex Tomasc9de5602008-01-29 00:19:52 -05001095{
1096 int block = 0;
1097 int max = 0;
1098 int order;
1099 void *buddy;
1100 void *buddy2;
1101 struct super_block *sb = e4b->bd_sb;
1102
1103 BUG_ON(first + count > (sb->s_blocksize << 3));
1104 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
1105 mb_check_buddy(e4b);
1106 mb_free_blocks_double(inode, e4b, first, count);
1107
1108 e4b->bd_info->bb_free += count;
1109 if (first < e4b->bd_info->bb_first_free)
1110 e4b->bd_info->bb_first_free = first;
1111
1112 /* let's maintain fragments counter */
1113 if (first != 0)
1114 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1115 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1116 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1117 if (block && max)
1118 e4b->bd_info->bb_fragments--;
1119 else if (!block && !max)
1120 e4b->bd_info->bb_fragments++;
1121
1122 /* let's maintain buddy itself */
1123 while (count-- > 0) {
1124 block = first++;
1125 order = 0;
1126
1127 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1128 ext4_fsblk_t blocknr;
1129 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1130 blocknr += block;
1131 blocknr +=
1132 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001133 ext4_unlock_group(sb, e4b->bd_group);
Harvey Harrison46e665e2008-04-17 10:38:59 -04001134 ext4_error(sb, __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001135 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -05001136 inode ? inode->i_ino : 0, blocknr, block,
1137 e4b->bd_group);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001138 ext4_lock_group(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001139 }
1140 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1141 e4b->bd_info->bb_counters[order]++;
1142
1143 /* start of the buddy */
1144 buddy = mb_find_buddy(e4b, order, &max);
1145
1146 do {
1147 block &= ~1UL;
1148 if (mb_test_bit(block, buddy) ||
1149 mb_test_bit(block + 1, buddy))
1150 break;
1151
1152 /* both the buddies are free, try to coalesce them */
1153 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1154
1155 if (!buddy2)
1156 break;
1157
1158 if (order > 0) {
1159 /* for special purposes, we don't set
1160 * free bits in bitmap */
1161 mb_set_bit(block, buddy);
1162 mb_set_bit(block + 1, buddy);
1163 }
1164 e4b->bd_info->bb_counters[order]--;
1165 e4b->bd_info->bb_counters[order]--;
1166
1167 block = block >> 1;
1168 order++;
1169 e4b->bd_info->bb_counters[order]++;
1170
1171 mb_clear_bit(block, buddy2);
1172 buddy = buddy2;
1173 } while (1);
1174 }
1175 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001176}
1177
1178static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1179 int needed, struct ext4_free_extent *ex)
1180{
1181 int next = block;
1182 int max;
1183 int ord;
1184 void *buddy;
1185
1186 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1187 BUG_ON(ex == NULL);
1188
1189 buddy = mb_find_buddy(e4b, order, &max);
1190 BUG_ON(buddy == NULL);
1191 BUG_ON(block >= max);
1192 if (mb_test_bit(block, buddy)) {
1193 ex->fe_len = 0;
1194 ex->fe_start = 0;
1195 ex->fe_group = 0;
1196 return 0;
1197 }
1198
1199 /* FIXME dorp order completely ? */
1200 if (likely(order == 0)) {
1201 /* find actual order */
1202 order = mb_find_order_for_block(e4b, block);
1203 block = block >> order;
1204 }
1205
1206 ex->fe_len = 1 << order;
1207 ex->fe_start = block << order;
1208 ex->fe_group = e4b->bd_group;
1209
1210 /* calc difference from given start */
1211 next = next - ex->fe_start;
1212 ex->fe_len -= next;
1213 ex->fe_start += next;
1214
1215 while (needed > ex->fe_len &&
1216 (buddy = mb_find_buddy(e4b, order, &max))) {
1217
1218 if (block + 1 >= max)
1219 break;
1220
1221 next = (block + 1) * (1 << order);
1222 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1223 break;
1224
1225 ord = mb_find_order_for_block(e4b, next);
1226
1227 order = ord;
1228 block = next >> order;
1229 ex->fe_len += 1 << order;
1230 }
1231
1232 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1233 return ex->fe_len;
1234}
1235
1236static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1237{
1238 int ord;
1239 int mlen = 0;
1240 int max = 0;
1241 int cur;
1242 int start = ex->fe_start;
1243 int len = ex->fe_len;
1244 unsigned ret = 0;
1245 int len0 = len;
1246 void *buddy;
1247
1248 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1249 BUG_ON(e4b->bd_group != ex->fe_group);
1250 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1251 mb_check_buddy(e4b);
1252 mb_mark_used_double(e4b, start, len);
1253
1254 e4b->bd_info->bb_free -= len;
1255 if (e4b->bd_info->bb_first_free == start)
1256 e4b->bd_info->bb_first_free += len;
1257
1258 /* let's maintain fragments counter */
1259 if (start != 0)
1260 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1261 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1262 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1263 if (mlen && max)
1264 e4b->bd_info->bb_fragments++;
1265 else if (!mlen && !max)
1266 e4b->bd_info->bb_fragments--;
1267
1268 /* let's maintain buddy itself */
1269 while (len) {
1270 ord = mb_find_order_for_block(e4b, start);
1271
1272 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1273 /* the whole chunk may be allocated at once! */
1274 mlen = 1 << ord;
1275 buddy = mb_find_buddy(e4b, ord, &max);
1276 BUG_ON((start >> ord) >= max);
1277 mb_set_bit(start >> ord, buddy);
1278 e4b->bd_info->bb_counters[ord]--;
1279 start += mlen;
1280 len -= mlen;
1281 BUG_ON(len < 0);
1282 continue;
1283 }
1284
1285 /* store for history */
1286 if (ret == 0)
1287 ret = len | (ord << 16);
1288
1289 /* we have to split large buddy */
1290 BUG_ON(ord <= 0);
1291 buddy = mb_find_buddy(e4b, ord, &max);
1292 mb_set_bit(start >> ord, buddy);
1293 e4b->bd_info->bb_counters[ord]--;
1294
1295 ord--;
1296 cur = (start >> ord) & ~1U;
1297 buddy = mb_find_buddy(e4b, ord, &max);
1298 mb_clear_bit(cur, buddy);
1299 mb_clear_bit(cur + 1, buddy);
1300 e4b->bd_info->bb_counters[ord]++;
1301 e4b->bd_info->bb_counters[ord]++;
1302 }
1303
1304 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
1305 EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1306 mb_check_buddy(e4b);
1307
1308 return ret;
1309}
1310
1311/*
1312 * Must be called under group lock!
1313 */
1314static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1315 struct ext4_buddy *e4b)
1316{
1317 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1318 int ret;
1319
1320 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1321 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1322
1323 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1324 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1325 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1326
1327 /* preallocation can change ac_b_ex, thus we store actually
1328 * allocated blocks for history */
1329 ac->ac_f_ex = ac->ac_b_ex;
1330
1331 ac->ac_status = AC_STATUS_FOUND;
1332 ac->ac_tail = ret & 0xffff;
1333 ac->ac_buddy = ret >> 16;
1334
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001335 /*
1336 * take the page reference. We want the page to be pinned
1337 * so that we don't get a ext4_mb_init_cache_call for this
1338 * group until we update the bitmap. That would mean we
1339 * double allocate blocks. The reference is dropped
1340 * in ext4_mb_release_context
1341 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001342 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1343 get_page(ac->ac_bitmap_page);
1344 ac->ac_buddy_page = e4b->bd_buddy_page;
1345 get_page(ac->ac_buddy_page);
1346
1347 /* store last allocated for subsequent stream allocation */
1348 if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
1349 spin_lock(&sbi->s_md_lock);
1350 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1351 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1352 spin_unlock(&sbi->s_md_lock);
1353 }
1354}
1355
1356/*
1357 * regular allocator, for general purposes allocation
1358 */
1359
1360static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1361 struct ext4_buddy *e4b,
1362 int finish_group)
1363{
1364 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1365 struct ext4_free_extent *bex = &ac->ac_b_ex;
1366 struct ext4_free_extent *gex = &ac->ac_g_ex;
1367 struct ext4_free_extent ex;
1368 int max;
1369
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001370 if (ac->ac_status == AC_STATUS_FOUND)
1371 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001372 /*
1373 * We don't want to scan for a whole year
1374 */
1375 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1376 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1377 ac->ac_status = AC_STATUS_BREAK;
1378 return;
1379 }
1380
1381 /*
1382 * Haven't found good chunk so far, let's continue
1383 */
1384 if (bex->fe_len < gex->fe_len)
1385 return;
1386
1387 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1388 && bex->fe_group == e4b->bd_group) {
1389 /* recheck chunk's availability - we don't know
1390 * when it was found (within this lock-unlock
1391 * period or not) */
1392 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1393 if (max >= gex->fe_len) {
1394 ext4_mb_use_best_found(ac, e4b);
1395 return;
1396 }
1397 }
1398}
1399
1400/*
1401 * The routine checks whether found extent is good enough. If it is,
1402 * then the extent gets marked used and flag is set to the context
1403 * to stop scanning. Otherwise, the extent is compared with the
1404 * previous found extent and if new one is better, then it's stored
1405 * in the context. Later, the best found extent will be used, if
1406 * mballoc can't find good enough extent.
1407 *
1408 * FIXME: real allocation policy is to be designed yet!
1409 */
1410static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1411 struct ext4_free_extent *ex,
1412 struct ext4_buddy *e4b)
1413{
1414 struct ext4_free_extent *bex = &ac->ac_b_ex;
1415 struct ext4_free_extent *gex = &ac->ac_g_ex;
1416
1417 BUG_ON(ex->fe_len <= 0);
1418 BUG_ON(ex->fe_len >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1419 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1420 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1421
1422 ac->ac_found++;
1423
1424 /*
1425 * The special case - take what you catch first
1426 */
1427 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1428 *bex = *ex;
1429 ext4_mb_use_best_found(ac, e4b);
1430 return;
1431 }
1432
1433 /*
1434 * Let's check whether the chuck is good enough
1435 */
1436 if (ex->fe_len == gex->fe_len) {
1437 *bex = *ex;
1438 ext4_mb_use_best_found(ac, e4b);
1439 return;
1440 }
1441
1442 /*
1443 * If this is first found extent, just store it in the context
1444 */
1445 if (bex->fe_len == 0) {
1446 *bex = *ex;
1447 return;
1448 }
1449
1450 /*
1451 * If new found extent is better, store it in the context
1452 */
1453 if (bex->fe_len < gex->fe_len) {
1454 /* if the request isn't satisfied, any found extent
1455 * larger than previous best one is better */
1456 if (ex->fe_len > bex->fe_len)
1457 *bex = *ex;
1458 } else if (ex->fe_len > gex->fe_len) {
1459 /* if the request is satisfied, then we try to find
1460 * an extent that still satisfy the request, but is
1461 * smaller than previous one */
1462 if (ex->fe_len < bex->fe_len)
1463 *bex = *ex;
1464 }
1465
1466 ext4_mb_check_limits(ac, e4b, 0);
1467}
1468
1469static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1470 struct ext4_buddy *e4b)
1471{
1472 struct ext4_free_extent ex = ac->ac_b_ex;
1473 ext4_group_t group = ex.fe_group;
1474 int max;
1475 int err;
1476
1477 BUG_ON(ex.fe_len <= 0);
1478 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1479 if (err)
1480 return err;
1481
1482 ext4_lock_group(ac->ac_sb, group);
1483 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1484
1485 if (max > 0) {
1486 ac->ac_b_ex = ex;
1487 ext4_mb_use_best_found(ac, e4b);
1488 }
1489
1490 ext4_unlock_group(ac->ac_sb, group);
1491 ext4_mb_release_desc(e4b);
1492
1493 return 0;
1494}
1495
1496static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1497 struct ext4_buddy *e4b)
1498{
1499 ext4_group_t group = ac->ac_g_ex.fe_group;
1500 int max;
1501 int err;
1502 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1503 struct ext4_super_block *es = sbi->s_es;
1504 struct ext4_free_extent ex;
1505
1506 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1507 return 0;
1508
1509 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1510 if (err)
1511 return err;
1512
1513 ext4_lock_group(ac->ac_sb, group);
1514 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1515 ac->ac_g_ex.fe_len, &ex);
1516
1517 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1518 ext4_fsblk_t start;
1519
1520 start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1521 ex.fe_start + le32_to_cpu(es->s_first_data_block);
1522 /* use do_div to get remainder (would be 64-bit modulo) */
1523 if (do_div(start, sbi->s_stripe) == 0) {
1524 ac->ac_found++;
1525 ac->ac_b_ex = ex;
1526 ext4_mb_use_best_found(ac, e4b);
1527 }
1528 } else if (max >= ac->ac_g_ex.fe_len) {
1529 BUG_ON(ex.fe_len <= 0);
1530 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1531 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1532 ac->ac_found++;
1533 ac->ac_b_ex = ex;
1534 ext4_mb_use_best_found(ac, e4b);
1535 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1536 /* Sometimes, caller may want to merge even small
1537 * number of blocks to an existing extent */
1538 BUG_ON(ex.fe_len <= 0);
1539 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1540 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1541 ac->ac_found++;
1542 ac->ac_b_ex = ex;
1543 ext4_mb_use_best_found(ac, e4b);
1544 }
1545 ext4_unlock_group(ac->ac_sb, group);
1546 ext4_mb_release_desc(e4b);
1547
1548 return 0;
1549}
1550
1551/*
1552 * The routine scans buddy structures (not bitmap!) from given order
1553 * to max order and tries to find big enough chunk to satisfy the req
1554 */
1555static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1556 struct ext4_buddy *e4b)
1557{
1558 struct super_block *sb = ac->ac_sb;
1559 struct ext4_group_info *grp = e4b->bd_info;
1560 void *buddy;
1561 int i;
1562 int k;
1563 int max;
1564
1565 BUG_ON(ac->ac_2order <= 0);
1566 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1567 if (grp->bb_counters[i] == 0)
1568 continue;
1569
1570 buddy = mb_find_buddy(e4b, i, &max);
1571 BUG_ON(buddy == NULL);
1572
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001573 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001574 BUG_ON(k >= max);
1575
1576 ac->ac_found++;
1577
1578 ac->ac_b_ex.fe_len = 1 << i;
1579 ac->ac_b_ex.fe_start = k << i;
1580 ac->ac_b_ex.fe_group = e4b->bd_group;
1581
1582 ext4_mb_use_best_found(ac, e4b);
1583
1584 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1585
1586 if (EXT4_SB(sb)->s_mb_stats)
1587 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1588
1589 break;
1590 }
1591}
1592
1593/*
1594 * The routine scans the group and measures all found extents.
1595 * In order to optimize scanning, caller must pass number of
1596 * free blocks in the group, so the routine can know upper limit.
1597 */
1598static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1599 struct ext4_buddy *e4b)
1600{
1601 struct super_block *sb = ac->ac_sb;
1602 void *bitmap = EXT4_MB_BITMAP(e4b);
1603 struct ext4_free_extent ex;
1604 int i;
1605 int free;
1606
1607 free = e4b->bd_info->bb_free;
1608 BUG_ON(free <= 0);
1609
1610 i = e4b->bd_info->bb_first_free;
1611
1612 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001613 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001614 EXT4_BLOCKS_PER_GROUP(sb), i);
1615 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001616 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001617 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001618 * free blocks even though group info says we
1619 * we have free blocks
1620 */
Harvey Harrison46e665e2008-04-17 10:38:59 -04001621 ext4_error(sb, __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001622 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001623 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001624 break;
1625 }
1626
1627 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1628 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001629 if (free < ex.fe_len) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001630 ext4_error(sb, __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001631 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001632 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001633 /*
1634 * The number of free blocks differs. This mostly
1635 * indicate that the bitmap is corrupt. So exit
1636 * without claiming the space.
1637 */
1638 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001639 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001640
1641 ext4_mb_measure_extent(ac, &ex, e4b);
1642
1643 i += ex.fe_len;
1644 free -= ex.fe_len;
1645 }
1646
1647 ext4_mb_check_limits(ac, e4b, 1);
1648}
1649
1650/*
1651 * This is a special case for storages like raid5
1652 * we try to find stripe-aligned chunks for stripe-size requests
1653 * XXX should do so at least for multiples of stripe size as well
1654 */
1655static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1656 struct ext4_buddy *e4b)
1657{
1658 struct super_block *sb = ac->ac_sb;
1659 struct ext4_sb_info *sbi = EXT4_SB(sb);
1660 void *bitmap = EXT4_MB_BITMAP(e4b);
1661 struct ext4_free_extent ex;
1662 ext4_fsblk_t first_group_block;
1663 ext4_fsblk_t a;
1664 ext4_grpblk_t i;
1665 int max;
1666
1667 BUG_ON(sbi->s_stripe == 0);
1668
1669 /* find first stripe-aligned block in group */
1670 first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1671 + le32_to_cpu(sbi->s_es->s_first_data_block);
1672 a = first_group_block + sbi->s_stripe - 1;
1673 do_div(a, sbi->s_stripe);
1674 i = (a * sbi->s_stripe) - first_group_block;
1675
1676 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1677 if (!mb_test_bit(i, bitmap)) {
1678 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1679 if (max >= sbi->s_stripe) {
1680 ac->ac_found++;
1681 ac->ac_b_ex = ex;
1682 ext4_mb_use_best_found(ac, e4b);
1683 break;
1684 }
1685 }
1686 i += sbi->s_stripe;
1687 }
1688}
1689
1690static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1691 ext4_group_t group, int cr)
1692{
1693 unsigned free, fragments;
1694 unsigned i, bits;
1695 struct ext4_group_desc *desc;
1696 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1697
1698 BUG_ON(cr < 0 || cr >= 4);
1699 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1700
1701 free = grp->bb_free;
1702 fragments = grp->bb_fragments;
1703 if (free == 0)
1704 return 0;
1705 if (fragments == 0)
1706 return 0;
1707
1708 switch (cr) {
1709 case 0:
1710 BUG_ON(ac->ac_2order == 0);
1711 /* If this group is uninitialized, skip it initially */
1712 desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
1713 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
1714 return 0;
1715
1716 bits = ac->ac_sb->s_blocksize_bits + 1;
1717 for (i = ac->ac_2order; i <= bits; i++)
1718 if (grp->bb_counters[i] > 0)
1719 return 1;
1720 break;
1721 case 1:
1722 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1723 return 1;
1724 break;
1725 case 2:
1726 if (free >= ac->ac_g_ex.fe_len)
1727 return 1;
1728 break;
1729 case 3:
1730 return 1;
1731 default:
1732 BUG();
1733 }
1734
1735 return 0;
1736}
1737
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001738/*
1739 * lock the group_info alloc_sem of all the groups
1740 * belonging to the same buddy cache page. This
1741 * make sure other parallel operation on the buddy
1742 * cache doesn't happen whild holding the buddy cache
1743 * lock
1744 */
1745int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1746{
1747 int i;
1748 int block, pnum;
1749 int blocks_per_page;
1750 int groups_per_page;
1751 ext4_group_t first_group;
1752 struct ext4_group_info *grp;
1753
1754 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1755 /*
1756 * the buddy cache inode stores the block bitmap
1757 * and buddy information in consecutive blocks.
1758 * So for each group we need two blocks.
1759 */
1760 block = group * 2;
1761 pnum = block / blocks_per_page;
1762 first_group = pnum * blocks_per_page / 2;
1763
1764 groups_per_page = blocks_per_page >> 1;
1765 if (groups_per_page == 0)
1766 groups_per_page = 1;
1767 /* read all groups the page covers into the cache */
1768 for (i = 0; i < groups_per_page; i++) {
1769
1770 if ((first_group + i) >= EXT4_SB(sb)->s_groups_count)
1771 break;
1772 grp = ext4_get_group_info(sb, first_group + i);
1773 /* take all groups write allocation
1774 * semaphore. This make sure there is
1775 * no block allocation going on in any
1776 * of that groups
1777 */
1778 down_write(&grp->alloc_sem);
1779 }
1780 return i;
1781}
1782
1783void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1784 ext4_group_t group, int locked_group)
1785{
1786 int i;
1787 int block, pnum;
1788 int blocks_per_page;
1789 ext4_group_t first_group;
1790 struct ext4_group_info *grp;
1791
1792 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1793 /*
1794 * the buddy cache inode stores the block bitmap
1795 * and buddy information in consecutive blocks.
1796 * So for each group we need two blocks.
1797 */
1798 block = group * 2;
1799 pnum = block / blocks_per_page;
1800 first_group = pnum * blocks_per_page / 2;
1801 /* release locks on all the groups */
1802 for (i = 0; i < locked_group; i++) {
1803
1804 grp = ext4_get_group_info(sb, first_group + i);
1805 /* take all groups write allocation
1806 * semaphore. This make sure there is
1807 * no block allocation going on in any
1808 * of that groups
1809 */
1810 up_write(&grp->alloc_sem);
1811 }
1812
1813}
1814
1815static int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1816{
1817
1818 int ret;
1819 void *bitmap;
1820 int blocks_per_page;
1821 int block, pnum, poff;
1822 int num_grp_locked = 0;
1823 struct ext4_group_info *this_grp;
1824 struct ext4_sb_info *sbi = EXT4_SB(sb);
1825 struct inode *inode = sbi->s_buddy_cache;
1826 struct page *page = NULL, *bitmap_page = NULL;
1827
1828 mb_debug("init group %lu\n", group);
1829 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1830 this_grp = ext4_get_group_info(sb, group);
1831 /*
1832 * This ensures we don't add group
1833 * to this buddy cache via resize
1834 */
1835 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
1836 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
1837 /*
1838 * somebody initialized the group
1839 * return without doing anything
1840 */
1841 ret = 0;
1842 goto err;
1843 }
1844 /*
1845 * the buddy cache inode stores the block bitmap
1846 * and buddy information in consecutive blocks.
1847 * So for each group we need two blocks.
1848 */
1849 block = group * 2;
1850 pnum = block / blocks_per_page;
1851 poff = block % blocks_per_page;
1852 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1853 if (page) {
1854 BUG_ON(page->mapping != inode->i_mapping);
1855 ret = ext4_mb_init_cache(page, NULL);
1856 if (ret) {
1857 unlock_page(page);
1858 goto err;
1859 }
1860 unlock_page(page);
1861 }
1862 if (page == NULL || !PageUptodate(page)) {
1863 ret = -EIO;
1864 goto err;
1865 }
1866 mark_page_accessed(page);
1867 bitmap_page = page;
1868 bitmap = page_address(page) + (poff * sb->s_blocksize);
1869
1870 /* init buddy cache */
1871 block++;
1872 pnum = block / blocks_per_page;
1873 poff = block % blocks_per_page;
1874 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1875 if (page == bitmap_page) {
1876 /*
1877 * If both the bitmap and buddy are in
1878 * the same page we don't need to force
1879 * init the buddy
1880 */
1881 unlock_page(page);
1882 } else if (page) {
1883 BUG_ON(page->mapping != inode->i_mapping);
1884 ret = ext4_mb_init_cache(page, bitmap);
1885 if (ret) {
1886 unlock_page(page);
1887 goto err;
1888 }
1889 unlock_page(page);
1890 }
1891 if (page == NULL || !PageUptodate(page)) {
1892 ret = -EIO;
1893 goto err;
1894 }
1895 mark_page_accessed(page);
1896err:
1897 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1898 if (bitmap_page)
1899 page_cache_release(bitmap_page);
1900 if (page)
1901 page_cache_release(page);
1902 return ret;
1903}
1904
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001905static noinline_for_stack int
1906ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001907{
1908 ext4_group_t group;
1909 ext4_group_t i;
1910 int cr;
1911 int err = 0;
1912 int bsbits;
1913 struct ext4_sb_info *sbi;
1914 struct super_block *sb;
1915 struct ext4_buddy e4b;
1916 loff_t size, isize;
1917
1918 sb = ac->ac_sb;
1919 sbi = EXT4_SB(sb);
1920 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1921
1922 /* first, try the goal */
1923 err = ext4_mb_find_by_goal(ac, &e4b);
1924 if (err || ac->ac_status == AC_STATUS_FOUND)
1925 goto out;
1926
1927 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1928 goto out;
1929
1930 /*
1931 * ac->ac2_order is set only if the fe_len is a power of 2
1932 * if ac2_order is set we also set criteria to 0 so that we
1933 * try exact allocation using buddy.
1934 */
1935 i = fls(ac->ac_g_ex.fe_len);
1936 ac->ac_2order = 0;
1937 /*
1938 * We search using buddy data only if the order of the request
1939 * is greater than equal to the sbi_s_mb_order2_reqs
1940 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1941 */
1942 if (i >= sbi->s_mb_order2_reqs) {
1943 /*
1944 * This should tell if fe_len is exactly power of 2
1945 */
1946 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1947 ac->ac_2order = i - 1;
1948 }
1949
1950 bsbits = ac->ac_sb->s_blocksize_bits;
1951 /* if stream allocation is enabled, use global goal */
1952 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
1953 isize = i_size_read(ac->ac_inode) >> bsbits;
1954 if (size < isize)
1955 size = isize;
1956
1957 if (size < sbi->s_mb_stream_request &&
1958 (ac->ac_flags & EXT4_MB_HINT_DATA)) {
1959 /* TBD: may be hot point */
1960 spin_lock(&sbi->s_md_lock);
1961 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
1962 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
1963 spin_unlock(&sbi->s_md_lock);
1964 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001965 /* Let's just scan groups to find more-less suitable blocks */
1966 cr = ac->ac_2order ? 0 : 1;
1967 /*
1968 * cr == 0 try to get exact allocation,
1969 * cr == 3 try to get anything
1970 */
1971repeat:
1972 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
1973 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04001974 /*
1975 * searching for the right group start
1976 * from the goal value specified
1977 */
1978 group = ac->ac_g_ex.fe_group;
1979
Alex Tomasc9de5602008-01-29 00:19:52 -05001980 for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
1981 struct ext4_group_info *grp;
1982 struct ext4_group_desc *desc;
1983
1984 if (group == EXT4_SB(sb)->s_groups_count)
1985 group = 0;
1986
1987 /* quick check to skip empty groups */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001988 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001989 if (grp->bb_free == 0)
1990 continue;
1991
1992 /*
1993 * if the group is already init we check whether it is
1994 * a good group and if not we don't load the buddy
1995 */
1996 if (EXT4_MB_GRP_NEED_INIT(grp)) {
1997 /*
1998 * we need full data about the group
1999 * to make a good selection
2000 */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002001 err = ext4_mb_init_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002002 if (err)
2003 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002004 }
2005
2006 /*
2007 * If the particular group doesn't satisfy our
2008 * criteria we continue with the next group
2009 */
2010 if (!ext4_mb_good_group(ac, group, cr))
2011 continue;
2012
2013 err = ext4_mb_load_buddy(sb, group, &e4b);
2014 if (err)
2015 goto out;
2016
2017 ext4_lock_group(sb, group);
2018 if (!ext4_mb_good_group(ac, group, cr)) {
2019 /* someone did allocation from this group */
2020 ext4_unlock_group(sb, group);
2021 ext4_mb_release_desc(&e4b);
2022 continue;
2023 }
2024
2025 ac->ac_groups_scanned++;
2026 desc = ext4_get_group_desc(sb, group, NULL);
2027 if (cr == 0 || (desc->bg_flags &
2028 cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
2029 ac->ac_2order != 0))
2030 ext4_mb_simple_scan_group(ac, &e4b);
2031 else if (cr == 1 &&
2032 ac->ac_g_ex.fe_len == sbi->s_stripe)
2033 ext4_mb_scan_aligned(ac, &e4b);
2034 else
2035 ext4_mb_complex_scan_group(ac, &e4b);
2036
2037 ext4_unlock_group(sb, group);
2038 ext4_mb_release_desc(&e4b);
2039
2040 if (ac->ac_status != AC_STATUS_CONTINUE)
2041 break;
2042 }
2043 }
2044
2045 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2046 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2047 /*
2048 * We've been searching too long. Let's try to allocate
2049 * the best chunk we've found so far
2050 */
2051
2052 ext4_mb_try_best_found(ac, &e4b);
2053 if (ac->ac_status != AC_STATUS_FOUND) {
2054 /*
2055 * Someone more lucky has already allocated it.
2056 * The only thing we can do is just take first
2057 * found block(s)
2058 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2059 */
2060 ac->ac_b_ex.fe_group = 0;
2061 ac->ac_b_ex.fe_start = 0;
2062 ac->ac_b_ex.fe_len = 0;
2063 ac->ac_status = AC_STATUS_CONTINUE;
2064 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2065 cr = 3;
2066 atomic_inc(&sbi->s_mb_lost_chunks);
2067 goto repeat;
2068 }
2069 }
2070out:
2071 return err;
2072}
2073
2074#ifdef EXT4_MB_HISTORY
2075struct ext4_mb_proc_session {
2076 struct ext4_mb_history *history;
2077 struct super_block *sb;
2078 int start;
2079 int max;
2080};
2081
2082static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
2083 struct ext4_mb_history *hs,
2084 int first)
2085{
2086 if (hs == s->history + s->max)
2087 hs = s->history;
2088 if (!first && hs == s->history + s->start)
2089 return NULL;
2090 while (hs->orig.fe_len == 0) {
2091 hs++;
2092 if (hs == s->history + s->max)
2093 hs = s->history;
2094 if (hs == s->history + s->start)
2095 return NULL;
2096 }
2097 return hs;
2098}
2099
2100static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2101{
2102 struct ext4_mb_proc_session *s = seq->private;
2103 struct ext4_mb_history *hs;
2104 int l = *pos;
2105
2106 if (l == 0)
2107 return SEQ_START_TOKEN;
2108 hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2109 if (!hs)
2110 return NULL;
2111 while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2112 return hs;
2113}
2114
2115static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
2116 loff_t *pos)
2117{
2118 struct ext4_mb_proc_session *s = seq->private;
2119 struct ext4_mb_history *hs = v;
2120
2121 ++*pos;
2122 if (v == SEQ_START_TOKEN)
2123 return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2124 else
2125 return ext4_mb_history_skip_empty(s, ++hs, 0);
2126}
2127
2128static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
2129{
2130 char buf[25], buf2[25], buf3[25], *fmt;
2131 struct ext4_mb_history *hs = v;
2132
2133 if (v == SEQ_START_TOKEN) {
2134 seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2135 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2136 "pid", "inode", "original", "goal", "result", "found",
2137 "grps", "cr", "flags", "merge", "tail", "broken");
2138 return 0;
2139 }
2140
2141 if (hs->op == EXT4_MB_HISTORY_ALLOC) {
2142 fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2143 "%-5u %-5s %-5u %-6u\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002144 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002145 hs->result.fe_start, hs->result.fe_len,
2146 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002147 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002148 hs->orig.fe_start, hs->orig.fe_len,
2149 hs->orig.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002150 sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002151 hs->goal.fe_start, hs->goal.fe_len,
2152 hs->goal.fe_logical);
2153 seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2154 hs->found, hs->groups, hs->cr, hs->flags,
2155 hs->merged ? "M" : "", hs->tail,
2156 hs->buddy ? 1 << hs->buddy : 0);
2157 } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
2158 fmt = "%-5u %-8u %-23s %-23s %-23s\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002159 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002160 hs->result.fe_start, hs->result.fe_len,
2161 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002162 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002163 hs->orig.fe_start, hs->orig.fe_len,
2164 hs->orig.fe_logical);
2165 seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2166 } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002167 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002168 hs->result.fe_start, hs->result.fe_len);
2169 seq_printf(seq, "%-5u %-8u %-23s discard\n",
2170 hs->pid, hs->ino, buf2);
2171 } else if (hs->op == EXT4_MB_HISTORY_FREE) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002172 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002173 hs->result.fe_start, hs->result.fe_len);
2174 seq_printf(seq, "%-5u %-8u %-23s free\n",
2175 hs->pid, hs->ino, buf2);
2176 }
2177 return 0;
2178}
2179
2180static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
2181{
2182}
2183
2184static struct seq_operations ext4_mb_seq_history_ops = {
2185 .start = ext4_mb_seq_history_start,
2186 .next = ext4_mb_seq_history_next,
2187 .stop = ext4_mb_seq_history_stop,
2188 .show = ext4_mb_seq_history_show,
2189};
2190
2191static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
2192{
2193 struct super_block *sb = PDE(inode)->data;
2194 struct ext4_sb_info *sbi = EXT4_SB(sb);
2195 struct ext4_mb_proc_session *s;
2196 int rc;
2197 int size;
2198
Shen Feng74767c52008-07-11 19:27:31 -04002199 if (unlikely(sbi->s_mb_history == NULL))
2200 return -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -05002201 s = kmalloc(sizeof(*s), GFP_KERNEL);
2202 if (s == NULL)
2203 return -ENOMEM;
2204 s->sb = sb;
2205 size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
2206 s->history = kmalloc(size, GFP_KERNEL);
2207 if (s->history == NULL) {
2208 kfree(s);
2209 return -ENOMEM;
2210 }
2211
2212 spin_lock(&sbi->s_mb_history_lock);
2213 memcpy(s->history, sbi->s_mb_history, size);
2214 s->max = sbi->s_mb_history_max;
2215 s->start = sbi->s_mb_history_cur % s->max;
2216 spin_unlock(&sbi->s_mb_history_lock);
2217
2218 rc = seq_open(file, &ext4_mb_seq_history_ops);
2219 if (rc == 0) {
2220 struct seq_file *m = (struct seq_file *)file->private_data;
2221 m->private = s;
2222 } else {
2223 kfree(s->history);
2224 kfree(s);
2225 }
2226 return rc;
2227
2228}
2229
2230static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
2231{
2232 struct seq_file *seq = (struct seq_file *)file->private_data;
2233 struct ext4_mb_proc_session *s = seq->private;
2234 kfree(s->history);
2235 kfree(s);
2236 return seq_release(inode, file);
2237}
2238
2239static ssize_t ext4_mb_seq_history_write(struct file *file,
2240 const char __user *buffer,
2241 size_t count, loff_t *ppos)
2242{
2243 struct seq_file *seq = (struct seq_file *)file->private_data;
2244 struct ext4_mb_proc_session *s = seq->private;
2245 struct super_block *sb = s->sb;
2246 char str[32];
2247 int value;
2248
2249 if (count >= sizeof(str)) {
2250 printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
2251 "mb_history", (int)sizeof(str));
2252 return -EOVERFLOW;
2253 }
2254
2255 if (copy_from_user(str, buffer, count))
2256 return -EFAULT;
2257
2258 value = simple_strtol(str, NULL, 0);
2259 if (value < 0)
2260 return -ERANGE;
2261 EXT4_SB(sb)->s_mb_history_filter = value;
2262
2263 return count;
2264}
2265
2266static struct file_operations ext4_mb_seq_history_fops = {
2267 .owner = THIS_MODULE,
2268 .open = ext4_mb_seq_history_open,
2269 .read = seq_read,
2270 .write = ext4_mb_seq_history_write,
2271 .llseek = seq_lseek,
2272 .release = ext4_mb_seq_history_release,
2273};
2274
2275static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2276{
2277 struct super_block *sb = seq->private;
2278 struct ext4_sb_info *sbi = EXT4_SB(sb);
2279 ext4_group_t group;
2280
2281 if (*pos < 0 || *pos >= sbi->s_groups_count)
2282 return NULL;
2283
2284 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002285 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002286}
2287
2288static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2289{
2290 struct super_block *sb = seq->private;
2291 struct ext4_sb_info *sbi = EXT4_SB(sb);
2292 ext4_group_t group;
2293
2294 ++*pos;
2295 if (*pos < 0 || *pos >= sbi->s_groups_count)
2296 return NULL;
2297 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002298 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002299}
2300
2301static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2302{
2303 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002304 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002305 int i;
2306 int err;
2307 struct ext4_buddy e4b;
2308 struct sg {
2309 struct ext4_group_info info;
2310 unsigned short counters[16];
2311 } sg;
2312
2313 group--;
2314 if (group == 0)
2315 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2316 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2317 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2318 "group", "free", "frags", "first",
2319 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2320 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2321
2322 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2323 sizeof(struct ext4_group_info);
2324 err = ext4_mb_load_buddy(sb, group, &e4b);
2325 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002326 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002327 return 0;
2328 }
2329 ext4_lock_group(sb, group);
2330 memcpy(&sg, ext4_get_group_info(sb, group), i);
2331 ext4_unlock_group(sb, group);
2332 ext4_mb_release_desc(&e4b);
2333
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002334 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002335 sg.info.bb_fragments, sg.info.bb_first_free);
2336 for (i = 0; i <= 13; i++)
2337 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2338 sg.info.bb_counters[i] : 0);
2339 seq_printf(seq, " ]\n");
2340
2341 return 0;
2342}
2343
2344static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2345{
2346}
2347
2348static struct seq_operations ext4_mb_seq_groups_ops = {
2349 .start = ext4_mb_seq_groups_start,
2350 .next = ext4_mb_seq_groups_next,
2351 .stop = ext4_mb_seq_groups_stop,
2352 .show = ext4_mb_seq_groups_show,
2353};
2354
2355static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2356{
2357 struct super_block *sb = PDE(inode)->data;
2358 int rc;
2359
2360 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2361 if (rc == 0) {
2362 struct seq_file *m = (struct seq_file *)file->private_data;
2363 m->private = sb;
2364 }
2365 return rc;
2366
2367}
2368
2369static struct file_operations ext4_mb_seq_groups_fops = {
2370 .owner = THIS_MODULE,
2371 .open = ext4_mb_seq_groups_open,
2372 .read = seq_read,
2373 .llseek = seq_lseek,
2374 .release = seq_release,
2375};
2376
2377static void ext4_mb_history_release(struct super_block *sb)
2378{
2379 struct ext4_sb_info *sbi = EXT4_SB(sb);
2380
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002381 if (sbi->s_proc != NULL) {
2382 remove_proc_entry("mb_groups", sbi->s_proc);
2383 remove_proc_entry("mb_history", sbi->s_proc);
2384 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002385 kfree(sbi->s_mb_history);
2386}
2387
2388static void ext4_mb_history_init(struct super_block *sb)
2389{
2390 struct ext4_sb_info *sbi = EXT4_SB(sb);
2391 int i;
2392
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002393 if (sbi->s_proc != NULL) {
2394 proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002395 &ext4_mb_seq_history_fops, sb);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002396 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002397 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002398 }
2399
2400 sbi->s_mb_history_max = 1000;
2401 sbi->s_mb_history_cur = 0;
2402 spin_lock_init(&sbi->s_mb_history_lock);
2403 i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
Shen Feng74767c52008-07-11 19:27:31 -04002404 sbi->s_mb_history = kzalloc(i, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002405 /* if we can't allocate history, then we simple won't use it */
2406}
2407
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002408static noinline_for_stack void
2409ext4_mb_store_history(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002410{
2411 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2412 struct ext4_mb_history h;
2413
2414 if (unlikely(sbi->s_mb_history == NULL))
2415 return;
2416
2417 if (!(ac->ac_op & sbi->s_mb_history_filter))
2418 return;
2419
2420 h.op = ac->ac_op;
2421 h.pid = current->pid;
2422 h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2423 h.orig = ac->ac_o_ex;
2424 h.result = ac->ac_b_ex;
2425 h.flags = ac->ac_flags;
2426 h.found = ac->ac_found;
2427 h.groups = ac->ac_groups_scanned;
2428 h.cr = ac->ac_criteria;
2429 h.tail = ac->ac_tail;
2430 h.buddy = ac->ac_buddy;
2431 h.merged = 0;
2432 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
2433 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2434 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2435 h.merged = 1;
2436 h.goal = ac->ac_g_ex;
2437 h.result = ac->ac_f_ex;
2438 }
2439
2440 spin_lock(&sbi->s_mb_history_lock);
2441 memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2442 if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2443 sbi->s_mb_history_cur = 0;
2444 spin_unlock(&sbi->s_mb_history_lock);
2445}
2446
2447#else
2448#define ext4_mb_history_release(sb)
2449#define ext4_mb_history_init(sb)
2450#endif
2451
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002452
2453/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002454int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002455 struct ext4_group_desc *desc)
2456{
2457 int i, len;
2458 int metalen = 0;
2459 struct ext4_sb_info *sbi = EXT4_SB(sb);
2460 struct ext4_group_info **meta_group_info;
2461
2462 /*
2463 * First check if this group is the first of a reserved block.
2464 * If it's true, we have to allocate a new table of pointers
2465 * to ext4_group_info structures
2466 */
2467 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2468 metalen = sizeof(*meta_group_info) <<
2469 EXT4_DESC_PER_BLOCK_BITS(sb);
2470 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2471 if (meta_group_info == NULL) {
2472 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2473 "buddy group\n");
2474 goto exit_meta_group_info;
2475 }
2476 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2477 meta_group_info;
2478 }
2479
2480 /*
2481 * calculate needed size. if change bb_counters size,
2482 * don't forget about ext4_mb_generate_buddy()
2483 */
2484 len = offsetof(typeof(**meta_group_info),
2485 bb_counters[sb->s_blocksize_bits + 2]);
2486
2487 meta_group_info =
2488 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2489 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2490
2491 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2492 if (meta_group_info[i] == NULL) {
2493 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2494 goto exit_group_info;
2495 }
2496 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2497 &(meta_group_info[i]->bb_state));
2498
2499 /*
2500 * initialize bb_free to be able to skip
2501 * empty groups without initialization
2502 */
2503 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2504 meta_group_info[i]->bb_free =
2505 ext4_free_blocks_after_init(sb, group, desc);
2506 } else {
2507 meta_group_info[i]->bb_free =
2508 le16_to_cpu(desc->bg_free_blocks_count);
2509 }
2510
2511 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002512 init_rwsem(&meta_group_info[i]->alloc_sem);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002513 meta_group_info[i]->bb_free_root.rb_node = NULL;;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002514
2515#ifdef DOUBLE_CHECK
2516 {
2517 struct buffer_head *bh;
2518 meta_group_info[i]->bb_bitmap =
2519 kmalloc(sb->s_blocksize, GFP_KERNEL);
2520 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2521 bh = ext4_read_block_bitmap(sb, group);
2522 BUG_ON(bh == NULL);
2523 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2524 sb->s_blocksize);
2525 put_bh(bh);
2526 }
2527#endif
2528
2529 return 0;
2530
2531exit_group_info:
2532 /* If a meta_group_info table has been allocated, release it now */
2533 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2534 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2535exit_meta_group_info:
2536 return -ENOMEM;
2537} /* ext4_mb_add_groupinfo */
2538
2539/*
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002540 * Update an existing group.
2541 * This function is used for online resize
2542 */
2543void ext4_mb_update_group_info(struct ext4_group_info *grp, ext4_grpblk_t add)
2544{
2545 grp->bb_free += add;
2546}
2547
Alex Tomasc9de5602008-01-29 00:19:52 -05002548static int ext4_mb_init_backend(struct super_block *sb)
2549{
2550 ext4_group_t i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002551 int metalen;
Alex Tomasc9de5602008-01-29 00:19:52 -05002552 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002553 struct ext4_super_block *es = sbi->s_es;
2554 int num_meta_group_infos;
2555 int num_meta_group_infos_max;
2556 int array_size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002557 struct ext4_group_info **meta_group_info;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002558 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002559
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002560 /* This is the number of blocks used by GDT */
2561 num_meta_group_infos = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) -
2562 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2563
2564 /*
2565 * This is the total number of blocks used by GDT including
2566 * the number of reserved blocks for GDT.
2567 * The s_group_info array is allocated with this value
2568 * to allow a clean online resize without a complex
2569 * manipulation of pointer.
2570 * The drawback is the unused memory when no resize
2571 * occurs but it's very low in terms of pages
2572 * (see comments below)
2573 * Need to handle this properly when META_BG resizing is allowed
2574 */
2575 num_meta_group_infos_max = num_meta_group_infos +
2576 le16_to_cpu(es->s_reserved_gdt_blocks);
2577
2578 /*
2579 * array_size is the size of s_group_info array. We round it
2580 * to the next power of two because this approximation is done
2581 * internally by kmalloc so we can have some more memory
2582 * for free here (e.g. may be used for META_BG resize).
2583 */
2584 array_size = 1;
2585 while (array_size < sizeof(*sbi->s_group_info) *
2586 num_meta_group_infos_max)
2587 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002588 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2589 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2590 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002591 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002592 if (sbi->s_group_info == NULL) {
2593 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2594 return -ENOMEM;
2595 }
2596 sbi->s_buddy_cache = new_inode(sb);
2597 if (sbi->s_buddy_cache == NULL) {
2598 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2599 goto err_freesgi;
2600 }
2601 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
2602
2603 metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
2604 for (i = 0; i < num_meta_group_infos; i++) {
2605 if ((i + 1) == num_meta_group_infos)
2606 metalen = sizeof(*meta_group_info) *
2607 (sbi->s_groups_count -
2608 (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
2609 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2610 if (meta_group_info == NULL) {
2611 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2612 "buddy group\n");
2613 goto err_freemeta;
2614 }
2615 sbi->s_group_info[i] = meta_group_info;
2616 }
2617
Alex Tomasc9de5602008-01-29 00:19:52 -05002618 for (i = 0; i < sbi->s_groups_count; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002619 desc = ext4_get_group_desc(sb, i, NULL);
2620 if (desc == NULL) {
2621 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002622 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002623 goto err_freebuddy;
2624 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002625 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2626 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002627 }
2628
2629 return 0;
2630
2631err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002632 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002633 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002634 i = num_meta_group_infos;
2635err_freemeta:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002636 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002637 kfree(sbi->s_group_info[i]);
2638 iput(sbi->s_buddy_cache);
2639err_freesgi:
2640 kfree(sbi->s_group_info);
2641 return -ENOMEM;
2642}
2643
2644int ext4_mb_init(struct super_block *sb, int needs_recovery)
2645{
2646 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002647 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002648 unsigned offset;
2649 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002650 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002651
Alex Tomasc9de5602008-01-29 00:19:52 -05002652 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2653
2654 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2655 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002656 return -ENOMEM;
2657 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002658
2659 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned int);
Alex Tomasc9de5602008-01-29 00:19:52 -05002660 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2661 if (sbi->s_mb_maxs == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002662 kfree(sbi->s_mb_maxs);
2663 return -ENOMEM;
2664 }
2665
2666 /* order 0 is regular bitmap */
2667 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2668 sbi->s_mb_offsets[0] = 0;
2669
2670 i = 1;
2671 offset = 0;
2672 max = sb->s_blocksize << 2;
2673 do {
2674 sbi->s_mb_offsets[i] = offset;
2675 sbi->s_mb_maxs[i] = max;
2676 offset += 1 << (sb->s_blocksize_bits - i);
2677 max = max >> 1;
2678 i++;
2679 } while (i <= sb->s_blocksize_bits + 1);
2680
2681 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002682 ret = ext4_mb_init_backend(sb);
2683 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002684 kfree(sbi->s_mb_offsets);
2685 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002686 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 }
2688
2689 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002690 spin_lock_init(&sbi->s_bal_lock);
2691
2692 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2693 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2694 sbi->s_mb_stats = MB_DEFAULT_STATS;
2695 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2696 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2697 sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
2698 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2699
Eric Sandeen730c2132008-09-13 15:23:29 -04002700 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002701 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002702 kfree(sbi->s_mb_offsets);
2703 kfree(sbi->s_mb_maxs);
2704 return -ENOMEM;
2705 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002706 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002707 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002708 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002709 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002710 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2711 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002712 spin_lock_init(&lg->lg_prealloc_lock);
2713 }
2714
2715 ext4_mb_init_per_dev_proc(sb);
2716 ext4_mb_history_init(sb);
2717
Frank Mayhar03901312009-01-07 00:06:22 -05002718 if (sbi->s_journal)
2719 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002720
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002721 printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002722 return 0;
2723}
2724
2725/* need to called with ext4 group lock (ext4_lock_group) */
2726static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2727{
2728 struct ext4_prealloc_space *pa;
2729 struct list_head *cur, *tmp;
2730 int count = 0;
2731
2732 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2733 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2734 list_del(&pa->pa_group_list);
2735 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002736 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002737 }
2738 if (count)
2739 mb_debug("mballoc: %u PAs left\n", count);
2740
2741}
2742
2743int ext4_mb_release(struct super_block *sb)
2744{
2745 ext4_group_t i;
2746 int num_meta_group_infos;
2747 struct ext4_group_info *grinfo;
2748 struct ext4_sb_info *sbi = EXT4_SB(sb);
2749
Alex Tomasc9de5602008-01-29 00:19:52 -05002750 if (sbi->s_group_info) {
2751 for (i = 0; i < sbi->s_groups_count; i++) {
2752 grinfo = ext4_get_group_info(sb, i);
2753#ifdef DOUBLE_CHECK
2754 kfree(grinfo->bb_bitmap);
2755#endif
2756 ext4_lock_group(sb, i);
2757 ext4_mb_cleanup_pa(grinfo);
2758 ext4_unlock_group(sb, i);
2759 kfree(grinfo);
2760 }
2761 num_meta_group_infos = (sbi->s_groups_count +
2762 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2763 EXT4_DESC_PER_BLOCK_BITS(sb);
2764 for (i = 0; i < num_meta_group_infos; i++)
2765 kfree(sbi->s_group_info[i]);
2766 kfree(sbi->s_group_info);
2767 }
2768 kfree(sbi->s_mb_offsets);
2769 kfree(sbi->s_mb_maxs);
2770 if (sbi->s_buddy_cache)
2771 iput(sbi->s_buddy_cache);
2772 if (sbi->s_mb_stats) {
2773 printk(KERN_INFO
2774 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2775 atomic_read(&sbi->s_bal_allocated),
2776 atomic_read(&sbi->s_bal_reqs),
2777 atomic_read(&sbi->s_bal_success));
2778 printk(KERN_INFO
2779 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2780 "%u 2^N hits, %u breaks, %u lost\n",
2781 atomic_read(&sbi->s_bal_ex_scanned),
2782 atomic_read(&sbi->s_bal_goals),
2783 atomic_read(&sbi->s_bal_2orders),
2784 atomic_read(&sbi->s_bal_breaks),
2785 atomic_read(&sbi->s_mb_lost_chunks));
2786 printk(KERN_INFO
2787 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2788 sbi->s_mb_buddies_generated++,
2789 sbi->s_mb_generation_time);
2790 printk(KERN_INFO
2791 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2792 atomic_read(&sbi->s_mb_preallocated),
2793 atomic_read(&sbi->s_mb_discarded));
2794 }
2795
Eric Sandeen730c2132008-09-13 15:23:29 -04002796 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002797 ext4_mb_history_release(sb);
2798 ext4_mb_destroy_per_dev_proc(sb);
2799
2800 return 0;
2801}
2802
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002803/*
2804 * This function is called by the jbd2 layer once the commit has finished,
2805 * so we know we can free the blocks that were released with that commit.
2806 */
2807static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002808{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002809 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002810 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002811 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002812 int err, count = 0, count2 = 0;
2813 struct ext4_free_data *entry;
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002814 ext4_fsblk_t discard_block;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002815 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002816
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002817 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2818 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002819
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002820 mb_debug("gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002821 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002822
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002823 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002824 /* we expect to find existing buddy because it's pinned */
2825 BUG_ON(err != 0);
2826
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002827 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002828 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002829 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002830 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002831 ext4_lock_group(sb, entry->group);
2832 /* Take it out of per group rb tree */
2833 rb_erase(&entry->node, &(db->bb_free_root));
2834 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2835
2836 if (!db->bb_free_root.rb_node) {
2837 /* No more items in the per group rb tree
2838 * balance refcounts from ext4_mb_free_metadata()
2839 */
2840 page_cache_release(e4b.bd_buddy_page);
2841 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002842 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002843 ext4_unlock_group(sb, entry->group);
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002844 discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
2845 + entry->start_blk
2846 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
2847 trace_mark(ext4_discard_blocks, "dev %s blk %llu count %u", sb->s_id,
2848 (unsigned long long) discard_block, entry->count);
2849 sb_issue_discard(sb, discard_block, entry->count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002850
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002851 kmem_cache_free(ext4_free_ext_cachep, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002852 ext4_mb_release_desc(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002853 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002854
2855 mb_debug("freed %u blocks in %u structures\n", count, count2);
2856}
2857
Alex Tomasc9de5602008-01-29 00:19:52 -05002858#define EXT4_MB_STATS_NAME "stats"
2859#define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2860#define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2861#define EXT4_MB_ORDER2_REQ "order2_req"
2862#define EXT4_MB_STREAM_REQ "stream_req"
2863#define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2864
Alex Tomasc9de5602008-01-29 00:19:52 -05002865static int ext4_mb_init_per_dev_proc(struct super_block *sb)
2866{
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002867#ifdef CONFIG_PROC_FS
Alex Tomasc9de5602008-01-29 00:19:52 -05002868 mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
2869 struct ext4_sb_info *sbi = EXT4_SB(sb);
2870 struct proc_dir_entry *proc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002871
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002872 if (sbi->s_proc == NULL)
Shen Fengcfbe7e42008-07-13 21:03:31 -04002873 return -EINVAL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002874
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04002875 EXT4_PROC_HANDLER(EXT4_MB_STATS_NAME, mb_stats);
2876 EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, mb_max_to_scan);
2877 EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, mb_min_to_scan);
2878 EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ, mb_order2_reqs);
2879 EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ, mb_stream_request);
2880 EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, mb_group_prealloc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002881 return 0;
2882
2883err_out:
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002884 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
2885 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
2886 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
2887 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
2888 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
2889 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002890 return -ENOMEM;
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002891#else
2892 return 0;
2893#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05002894}
2895
2896static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
2897{
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002898#ifdef CONFIG_PROC_FS
Alex Tomasc9de5602008-01-29 00:19:52 -05002899 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002900
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002901 if (sbi->s_proc == NULL)
Alex Tomasc9de5602008-01-29 00:19:52 -05002902 return -EINVAL;
2903
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002904 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
2905 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
2906 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
2907 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
2908 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
2909 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002910#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05002911 return 0;
2912}
2913
2914int __init init_ext4_mballoc(void)
2915{
2916 ext4_pspace_cachep =
2917 kmem_cache_create("ext4_prealloc_space",
2918 sizeof(struct ext4_prealloc_space),
2919 0, SLAB_RECLAIM_ACCOUNT, NULL);
2920 if (ext4_pspace_cachep == NULL)
2921 return -ENOMEM;
2922
Eric Sandeen256bdb42008-02-10 01:13:33 -05002923 ext4_ac_cachep =
2924 kmem_cache_create("ext4_alloc_context",
2925 sizeof(struct ext4_allocation_context),
2926 0, SLAB_RECLAIM_ACCOUNT, NULL);
2927 if (ext4_ac_cachep == NULL) {
2928 kmem_cache_destroy(ext4_pspace_cachep);
2929 return -ENOMEM;
2930 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002931
2932 ext4_free_ext_cachep =
2933 kmem_cache_create("ext4_free_block_extents",
2934 sizeof(struct ext4_free_data),
2935 0, SLAB_RECLAIM_ACCOUNT, NULL);
2936 if (ext4_free_ext_cachep == NULL) {
2937 kmem_cache_destroy(ext4_pspace_cachep);
2938 kmem_cache_destroy(ext4_ac_cachep);
2939 return -ENOMEM;
2940 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002941 return 0;
2942}
2943
2944void exit_ext4_mballoc(void)
2945{
2946 /* XXX: synchronize_rcu(); */
2947 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002948 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002949 kmem_cache_destroy(ext4_free_ext_cachep);
Alex Tomasc9de5602008-01-29 00:19:52 -05002950}
2951
2952
2953/*
2954 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2955 * Returns 0 if success or error code
2956 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002957static noinline_for_stack int
2958ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002959 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002960{
2961 struct buffer_head *bitmap_bh = NULL;
2962 struct ext4_super_block *es;
2963 struct ext4_group_desc *gdp;
2964 struct buffer_head *gdp_bh;
2965 struct ext4_sb_info *sbi;
2966 struct super_block *sb;
2967 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002968 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002969
2970 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2971 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2972
2973 sb = ac->ac_sb;
2974 sbi = EXT4_SB(sb);
2975 es = sbi->s_es;
2976
Alex Tomasc9de5602008-01-29 00:19:52 -05002977
2978 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002979 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002980 if (!bitmap_bh)
2981 goto out_err;
2982
2983 err = ext4_journal_get_write_access(handle, bitmap_bh);
2984 if (err)
2985 goto out_err;
2986
2987 err = -EIO;
2988 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2989 if (!gdp)
2990 goto out_err;
2991
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002992 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002993 gdp->bg_free_blocks_count);
2994
Alex Tomasc9de5602008-01-29 00:19:52 -05002995 err = ext4_journal_get_write_access(handle, gdp_bh);
2996 if (err)
2997 goto out_err;
2998
2999 block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
3000 + ac->ac_b_ex.fe_start
3001 + le32_to_cpu(es->s_first_data_block);
3002
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003003 len = ac->ac_b_ex.fe_len;
3004 if (in_range(ext4_block_bitmap(sb, gdp), block, len) ||
3005 in_range(ext4_inode_bitmap(sb, gdp), block, len) ||
3006 in_range(block, ext4_inode_table(sb, gdp),
3007 EXT4_SB(sb)->s_itb_per_group) ||
3008 in_range(block + len - 1, ext4_inode_table(sb, gdp),
3009 EXT4_SB(sb)->s_itb_per_group)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04003010 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05003011 "Allocating block in system zone - block = %llu",
3012 block);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003013 /* File system mounted not to panic on error
3014 * Fix the bitmap and repeat the block allocation
3015 * We leak some of the blocks here.
3016 */
3017 mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group),
3018 bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3019 ac->ac_b_ex.fe_len);
Frank Mayhar03901312009-01-07 00:06:22 -05003020 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003021 if (!err)
3022 err = -EAGAIN;
3023 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003024 }
3025#ifdef AGGRESSIVE_CHECK
3026 {
3027 int i;
3028 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3029 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3030 bitmap_bh->b_data));
3031 }
3032 }
3033#endif
3034 mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
3035 ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3036
3037 spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3038 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3039 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
3040 gdp->bg_free_blocks_count =
3041 cpu_to_le16(ext4_free_blocks_after_init(sb,
3042 ac->ac_b_ex.fe_group,
3043 gdp));
3044 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04003045 le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003046 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
3047 spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003048 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003049 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003050 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003051 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003052 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3053 /* release all the reserved blocks if non delalloc */
3054 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
3055 else
3056 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
3057 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003058
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003059 if (sbi->s_log_groups_per_flex) {
3060 ext4_group_t flex_group = ext4_flex_group(sbi,
3061 ac->ac_b_ex.fe_group);
3062 spin_lock(sb_bgl_lock(sbi, flex_group));
3063 sbi->s_flex_groups[flex_group].free_blocks -= ac->ac_b_ex.fe_len;
3064 spin_unlock(sb_bgl_lock(sbi, flex_group));
3065 }
3066
Frank Mayhar03901312009-01-07 00:06:22 -05003067 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003068 if (err)
3069 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003070 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003071
3072out_err:
3073 sb->s_dirt = 1;
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003074 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003075 return err;
3076}
3077
3078/*
3079 * here we normalize request for locality group
3080 * Group request are normalized to s_strip size if we set the same via mount
3081 * option. If not we set it to s_mb_group_prealloc which can be configured via
3082 * /proc/fs/ext4/<partition>/group_prealloc
3083 *
3084 * XXX: should we try to preallocate more than the group has now?
3085 */
3086static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3087{
3088 struct super_block *sb = ac->ac_sb;
3089 struct ext4_locality_group *lg = ac->ac_lg;
3090
3091 BUG_ON(lg == NULL);
3092 if (EXT4_SB(sb)->s_stripe)
3093 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
3094 else
3095 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003096 mb_debug("#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003097 current->pid, ac->ac_g_ex.fe_len);
3098}
3099
3100/*
3101 * Normalization means making request better in terms of
3102 * size and alignment
3103 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003104static noinline_for_stack void
3105ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003106 struct ext4_allocation_request *ar)
3107{
3108 int bsbits, max;
3109 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003110 loff_t size, orig_size, start_off;
3111 ext4_lblk_t start, orig_start;
3112 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003113 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003114
3115 /* do normalize only data requests, metadata requests
3116 do not need preallocation */
3117 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3118 return;
3119
3120 /* sometime caller may want exact blocks */
3121 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3122 return;
3123
3124 /* caller may indicate that preallocation isn't
3125 * required (it's a tail, for example) */
3126 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3127 return;
3128
3129 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3130 ext4_mb_normalize_group_request(ac);
3131 return ;
3132 }
3133
3134 bsbits = ac->ac_sb->s_blocksize_bits;
3135
3136 /* first, let's learn actual file size
3137 * given current request is allocated */
3138 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3139 size = size << bsbits;
3140 if (size < i_size_read(ac->ac_inode))
3141 size = i_size_read(ac->ac_inode);
3142
Valerie Clement19304792008-05-13 19:31:14 -04003143 /* max size of free chunks */
3144 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003145
Valerie Clement19304792008-05-13 19:31:14 -04003146#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3147 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003148
3149 /* first, try to predict filesize */
3150 /* XXX: should this table be tunable? */
3151 start_off = 0;
3152 if (size <= 16 * 1024) {
3153 size = 16 * 1024;
3154 } else if (size <= 32 * 1024) {
3155 size = 32 * 1024;
3156 } else if (size <= 64 * 1024) {
3157 size = 64 * 1024;
3158 } else if (size <= 128 * 1024) {
3159 size = 128 * 1024;
3160 } else if (size <= 256 * 1024) {
3161 size = 256 * 1024;
3162 } else if (size <= 512 * 1024) {
3163 size = 512 * 1024;
3164 } else if (size <= 1024 * 1024) {
3165 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003166 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003167 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003168 (21 - bsbits)) << 21;
3169 size = 2 * 1024 * 1024;
3170 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003171 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3172 (22 - bsbits)) << 22;
3173 size = 4 * 1024 * 1024;
3174 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003175 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003176 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3177 (23 - bsbits)) << 23;
3178 size = 8 * 1024 * 1024;
3179 } else {
3180 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
3181 size = ac->ac_o_ex.fe_len << bsbits;
3182 }
3183 orig_size = size = size >> bsbits;
3184 orig_start = start = start_off >> bsbits;
3185
3186 /* don't cover already allocated blocks in selected range */
3187 if (ar->pleft && start <= ar->lleft) {
3188 size -= ar->lleft + 1 - start;
3189 start = ar->lleft + 1;
3190 }
3191 if (ar->pright && start + size - 1 >= ar->lright)
3192 size -= start + size - ar->lright;
3193
3194 end = start + size;
3195
3196 /* check we don't cross already preallocated blocks */
3197 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003198 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003199 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003200
Alex Tomasc9de5602008-01-29 00:19:52 -05003201 if (pa->pa_deleted)
3202 continue;
3203 spin_lock(&pa->pa_lock);
3204 if (pa->pa_deleted) {
3205 spin_unlock(&pa->pa_lock);
3206 continue;
3207 }
3208
3209 pa_end = pa->pa_lstart + pa->pa_len;
3210
3211 /* PA must not overlap original request */
3212 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3213 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3214
3215 /* skip PA normalized request doesn't overlap with */
3216 if (pa->pa_lstart >= end) {
3217 spin_unlock(&pa->pa_lock);
3218 continue;
3219 }
3220 if (pa_end <= start) {
3221 spin_unlock(&pa->pa_lock);
3222 continue;
3223 }
3224 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3225
3226 if (pa_end <= ac->ac_o_ex.fe_logical) {
3227 BUG_ON(pa_end < start);
3228 start = pa_end;
3229 }
3230
3231 if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3232 BUG_ON(pa->pa_lstart > end);
3233 end = pa->pa_lstart;
3234 }
3235 spin_unlock(&pa->pa_lock);
3236 }
3237 rcu_read_unlock();
3238 size = end - start;
3239
3240 /* XXX: extra loop to check we really don't overlap preallocations */
3241 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003242 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003243 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003244 spin_lock(&pa->pa_lock);
3245 if (pa->pa_deleted == 0) {
3246 pa_end = pa->pa_lstart + pa->pa_len;
3247 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3248 }
3249 spin_unlock(&pa->pa_lock);
3250 }
3251 rcu_read_unlock();
3252
3253 if (start + size <= ac->ac_o_ex.fe_logical &&
3254 start > ac->ac_o_ex.fe_logical) {
3255 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3256 (unsigned long) start, (unsigned long) size,
3257 (unsigned long) ac->ac_o_ex.fe_logical);
3258 }
3259 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3260 start > ac->ac_o_ex.fe_logical);
3261 BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
3262
3263 /* now prepare goal request */
3264
3265 /* XXX: is it better to align blocks WRT to logical
3266 * placement or satisfy big request as is */
3267 ac->ac_g_ex.fe_logical = start;
3268 ac->ac_g_ex.fe_len = size;
3269
3270 /* define goal start in order to merge */
3271 if (ar->pright && (ar->lright == (start + size))) {
3272 /* merge to the right */
3273 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3274 &ac->ac_f_ex.fe_group,
3275 &ac->ac_f_ex.fe_start);
3276 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3277 }
3278 if (ar->pleft && (ar->lleft + 1 == start)) {
3279 /* merge to the left */
3280 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3281 &ac->ac_f_ex.fe_group,
3282 &ac->ac_f_ex.fe_start);
3283 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3284 }
3285
3286 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3287 (unsigned) orig_size, (unsigned) start);
3288}
3289
3290static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3291{
3292 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3293
3294 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3295 atomic_inc(&sbi->s_bal_reqs);
3296 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3297 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3298 atomic_inc(&sbi->s_bal_success);
3299 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3300 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3301 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3302 atomic_inc(&sbi->s_bal_goals);
3303 if (ac->ac_found > sbi->s_mb_max_to_scan)
3304 atomic_inc(&sbi->s_bal_breaks);
3305 }
3306
3307 ext4_mb_store_history(ac);
3308}
3309
3310/*
3311 * use blocks preallocated to inode
3312 */
3313static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3314 struct ext4_prealloc_space *pa)
3315{
3316 ext4_fsblk_t start;
3317 ext4_fsblk_t end;
3318 int len;
3319
3320 /* found preallocated blocks, use them */
3321 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3322 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3323 len = end - start;
3324 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3325 &ac->ac_b_ex.fe_start);
3326 ac->ac_b_ex.fe_len = len;
3327 ac->ac_status = AC_STATUS_FOUND;
3328 ac->ac_pa = pa;
3329
3330 BUG_ON(start < pa->pa_pstart);
3331 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3332 BUG_ON(pa->pa_free < len);
3333 pa->pa_free -= len;
3334
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003335 mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003336}
3337
3338/*
3339 * use blocks preallocated to locality group
3340 */
3341static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3342 struct ext4_prealloc_space *pa)
3343{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003344 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003345
Alex Tomasc9de5602008-01-29 00:19:52 -05003346 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3347 &ac->ac_b_ex.fe_group,
3348 &ac->ac_b_ex.fe_start);
3349 ac->ac_b_ex.fe_len = len;
3350 ac->ac_status = AC_STATUS_FOUND;
3351 ac->ac_pa = pa;
3352
3353 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003354 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003355 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003356 * in on-disk bitmap -- see ext4_mb_release_context()
3357 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003358 */
3359 mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3360}
3361
3362/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003363 * Return the prealloc space that have minimal distance
3364 * from the goal block. @cpa is the prealloc
3365 * space that is having currently known minimal distance
3366 * from the goal block.
3367 */
3368static struct ext4_prealloc_space *
3369ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3370 struct ext4_prealloc_space *pa,
3371 struct ext4_prealloc_space *cpa)
3372{
3373 ext4_fsblk_t cur_distance, new_distance;
3374
3375 if (cpa == NULL) {
3376 atomic_inc(&pa->pa_count);
3377 return pa;
3378 }
3379 cur_distance = abs(goal_block - cpa->pa_pstart);
3380 new_distance = abs(goal_block - pa->pa_pstart);
3381
3382 if (cur_distance < new_distance)
3383 return cpa;
3384
3385 /* drop the previous reference */
3386 atomic_dec(&cpa->pa_count);
3387 atomic_inc(&pa->pa_count);
3388 return pa;
3389}
3390
3391/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003392 * search goal blocks in preallocated space
3393 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003394static noinline_for_stack int
3395ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003396{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003397 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003398 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3399 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003400 struct ext4_prealloc_space *pa, *cpa = NULL;
3401 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003402
3403 /* only data can be preallocated */
3404 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3405 return 0;
3406
3407 /* first, try per-file preallocation */
3408 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003409 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003410
3411 /* all fields in this condition don't change,
3412 * so we can skip locking for them */
3413 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3414 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3415 continue;
3416
3417 /* found preallocated blocks, use them */
3418 spin_lock(&pa->pa_lock);
3419 if (pa->pa_deleted == 0 && pa->pa_free) {
3420 atomic_inc(&pa->pa_count);
3421 ext4_mb_use_inode_pa(ac, pa);
3422 spin_unlock(&pa->pa_lock);
3423 ac->ac_criteria = 10;
3424 rcu_read_unlock();
3425 return 1;
3426 }
3427 spin_unlock(&pa->pa_lock);
3428 }
3429 rcu_read_unlock();
3430
3431 /* can we use group allocation? */
3432 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3433 return 0;
3434
3435 /* inode may have no locality group for some reason */
3436 lg = ac->ac_lg;
3437 if (lg == NULL)
3438 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003439 order = fls(ac->ac_o_ex.fe_len) - 1;
3440 if (order > PREALLOC_TB_SIZE - 1)
3441 /* The max size of hash table is PREALLOC_TB_SIZE */
3442 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003443
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003444 goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
3445 ac->ac_g_ex.fe_start +
3446 le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
3447 /*
3448 * search for the prealloc space that is having
3449 * minimal distance from the goal block.
3450 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003451 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3452 rcu_read_lock();
3453 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3454 pa_inode_list) {
3455 spin_lock(&pa->pa_lock);
3456 if (pa->pa_deleted == 0 &&
3457 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003458
3459 cpa = ext4_mb_check_group_pa(goal_block,
3460 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003461 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003462 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003463 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003464 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003465 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003466 if (cpa) {
3467 ext4_mb_use_group_pa(ac, cpa);
3468 ac->ac_criteria = 20;
3469 return 1;
3470 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003471 return 0;
3472}
3473
3474/*
3475 * the function goes through all preallocation in this group and marks them
3476 * used in in-core bitmap. buddy must be generated from this bitmap
3477 * Need to be called with ext4 group lock (ext4_lock_group)
3478 */
3479static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3480 ext4_group_t group)
3481{
3482 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3483 struct ext4_prealloc_space *pa;
3484 struct list_head *cur;
3485 ext4_group_t groupnr;
3486 ext4_grpblk_t start;
3487 int preallocated = 0;
3488 int count = 0;
3489 int len;
3490
3491 /* all form of preallocation discards first load group,
3492 * so the only competing code is preallocation use.
3493 * we don't need any locking here
3494 * notice we do NOT ignore preallocations with pa_deleted
3495 * otherwise we could leave used blocks available for
3496 * allocation in buddy when concurrent ext4_mb_put_pa()
3497 * is dropping preallocation
3498 */
3499 list_for_each(cur, &grp->bb_prealloc_list) {
3500 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3501 spin_lock(&pa->pa_lock);
3502 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3503 &groupnr, &start);
3504 len = pa->pa_len;
3505 spin_unlock(&pa->pa_lock);
3506 if (unlikely(len == 0))
3507 continue;
3508 BUG_ON(groupnr != group);
3509 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
3510 bitmap, start, len);
3511 preallocated += len;
3512 count++;
3513 }
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003514 mb_debug("prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003515}
3516
3517static void ext4_mb_pa_callback(struct rcu_head *head)
3518{
3519 struct ext4_prealloc_space *pa;
3520 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3521 kmem_cache_free(ext4_pspace_cachep, pa);
3522}
3523
3524/*
3525 * drops a reference to preallocated space descriptor
3526 * if this was the last reference and the space is consumed
3527 */
3528static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3529 struct super_block *sb, struct ext4_prealloc_space *pa)
3530{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003531 ext4_group_t grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05003532
3533 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3534 return;
3535
3536 /* in this short window concurrent discard can set pa_deleted */
3537 spin_lock(&pa->pa_lock);
3538 if (pa->pa_deleted == 1) {
3539 spin_unlock(&pa->pa_lock);
3540 return;
3541 }
3542
3543 pa->pa_deleted = 1;
3544 spin_unlock(&pa->pa_lock);
3545
3546 /* -1 is to protect from crossing allocation group */
3547 ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
3548
3549 /*
3550 * possible race:
3551 *
3552 * P1 (buddy init) P2 (regular allocation)
3553 * find block B in PA
3554 * copy on-disk bitmap to buddy
3555 * mark B in on-disk bitmap
3556 * drop PA from group
3557 * mark all PAs in buddy
3558 *
3559 * thus, P1 initializes buddy with B available. to prevent this
3560 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3561 * against that pair
3562 */
3563 ext4_lock_group(sb, grp);
3564 list_del(&pa->pa_group_list);
3565 ext4_unlock_group(sb, grp);
3566
3567 spin_lock(pa->pa_obj_lock);
3568 list_del_rcu(&pa->pa_inode_list);
3569 spin_unlock(pa->pa_obj_lock);
3570
3571 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3572}
3573
3574/*
3575 * creates new preallocated space for given inode
3576 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003577static noinline_for_stack int
3578ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003579{
3580 struct super_block *sb = ac->ac_sb;
3581 struct ext4_prealloc_space *pa;
3582 struct ext4_group_info *grp;
3583 struct ext4_inode_info *ei;
3584
3585 /* preallocate only when found space is larger then requested */
3586 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3587 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3588 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3589
3590 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3591 if (pa == NULL)
3592 return -ENOMEM;
3593
3594 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3595 int winl;
3596 int wins;
3597 int win;
3598 int offs;
3599
3600 /* we can't allocate as much as normalizer wants.
3601 * so, found space must get proper lstart
3602 * to cover original request */
3603 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3604 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3605
3606 /* we're limited by original request in that
3607 * logical block must be covered any way
3608 * winl is window we can move our chunk within */
3609 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3610
3611 /* also, we should cover whole original request */
3612 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3613
3614 /* the smallest one defines real window */
3615 win = min(winl, wins);
3616
3617 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3618 if (offs && offs < win)
3619 win = offs;
3620
3621 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3622 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3623 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3624 }
3625
3626 /* preallocation can change ac_b_ex, thus we store actually
3627 * allocated blocks for history */
3628 ac->ac_f_ex = ac->ac_b_ex;
3629
3630 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3631 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3632 pa->pa_len = ac->ac_b_ex.fe_len;
3633 pa->pa_free = pa->pa_len;
3634 atomic_set(&pa->pa_count, 1);
3635 spin_lock_init(&pa->pa_lock);
3636 pa->pa_deleted = 0;
3637 pa->pa_linear = 0;
3638
3639 mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
3640 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3641
3642 ext4_mb_use_inode_pa(ac, pa);
3643 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3644
3645 ei = EXT4_I(ac->ac_inode);
3646 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3647
3648 pa->pa_obj_lock = &ei->i_prealloc_lock;
3649 pa->pa_inode = ac->ac_inode;
3650
3651 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3652 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3653 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3654
3655 spin_lock(pa->pa_obj_lock);
3656 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3657 spin_unlock(pa->pa_obj_lock);
3658
3659 return 0;
3660}
3661
3662/*
3663 * creates new preallocated space for locality group inodes belongs to
3664 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003665static noinline_for_stack int
3666ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003667{
3668 struct super_block *sb = ac->ac_sb;
3669 struct ext4_locality_group *lg;
3670 struct ext4_prealloc_space *pa;
3671 struct ext4_group_info *grp;
3672
3673 /* preallocate only when found space is larger then requested */
3674 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3675 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3676 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3677
3678 BUG_ON(ext4_pspace_cachep == NULL);
3679 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3680 if (pa == NULL)
3681 return -ENOMEM;
3682
3683 /* preallocation can change ac_b_ex, thus we store actually
3684 * allocated blocks for history */
3685 ac->ac_f_ex = ac->ac_b_ex;
3686
3687 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3688 pa->pa_lstart = pa->pa_pstart;
3689 pa->pa_len = ac->ac_b_ex.fe_len;
3690 pa->pa_free = pa->pa_len;
3691 atomic_set(&pa->pa_count, 1);
3692 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003693 INIT_LIST_HEAD(&pa->pa_inode_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003694 pa->pa_deleted = 0;
3695 pa->pa_linear = 1;
3696
3697 mb_debug("new group pa %p: %llu/%u for %u\n", pa,
3698 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3699
3700 ext4_mb_use_group_pa(ac, pa);
3701 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3702
3703 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3704 lg = ac->ac_lg;
3705 BUG_ON(lg == NULL);
3706
3707 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3708 pa->pa_inode = NULL;
3709
3710 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3711 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3712 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3713
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003714 /*
3715 * We will later add the new pa to the right bucket
3716 * after updating the pa_free in ext4_mb_release_context
3717 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003718 return 0;
3719}
3720
3721static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3722{
3723 int err;
3724
3725 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3726 err = ext4_mb_new_group_pa(ac);
3727 else
3728 err = ext4_mb_new_inode_pa(ac);
3729 return err;
3730}
3731
3732/*
3733 * finds all unused blocks in on-disk bitmap, frees them in
3734 * in-core bitmap and buddy.
3735 * @pa must be unlinked from inode and group lists, so that
3736 * nobody else can find/use it.
3737 * the caller MUST hold group/inode locks.
3738 * TODO: optimize the case when there are no in-core structures yet
3739 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003740static noinline_for_stack int
3741ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003742 struct ext4_prealloc_space *pa,
3743 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003744{
Alex Tomasc9de5602008-01-29 00:19:52 -05003745 struct super_block *sb = e4b->bd_sb;
3746 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003747 unsigned int end;
3748 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003749 ext4_group_t group;
3750 ext4_grpblk_t bit;
3751 sector_t start;
3752 int err = 0;
3753 int free = 0;
3754
3755 BUG_ON(pa->pa_deleted == 0);
3756 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3757 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3758 end = bit + pa->pa_len;
3759
Eric Sandeen256bdb42008-02-10 01:13:33 -05003760 if (ac) {
3761 ac->ac_sb = sb;
3762 ac->ac_inode = pa->pa_inode;
3763 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3764 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003765
3766 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003767 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003768 if (bit >= end)
3769 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003770 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003771 start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3772 le32_to_cpu(sbi->s_es->s_first_data_block);
3773 mb_debug(" free preallocated %u/%u in group %u\n",
3774 (unsigned) start, (unsigned) next - bit,
3775 (unsigned) group);
3776 free += next - bit;
3777
Eric Sandeen256bdb42008-02-10 01:13:33 -05003778 if (ac) {
3779 ac->ac_b_ex.fe_group = group;
3780 ac->ac_b_ex.fe_start = bit;
3781 ac->ac_b_ex.fe_len = next - bit;
3782 ac->ac_b_ex.fe_logical = 0;
3783 ext4_mb_store_history(ac);
3784 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003785
3786 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3787 bit = next + 1;
3788 }
3789 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003790 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003791 pa, (unsigned long) pa->pa_lstart,
3792 (unsigned long) pa->pa_pstart,
3793 (unsigned long) pa->pa_len);
Theodore Ts'ofde4d952009-01-05 22:17:35 -05003794 ext4_error(sb, __func__, "free %u, pa_free %u",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003795 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003796 /*
3797 * pa is already deleted so we use the value obtained
3798 * from the bitmap and continue.
3799 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003800 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003801 atomic_add(free, &sbi->s_mb_discarded);
3802
3803 return err;
3804}
3805
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003806static noinline_for_stack int
3807ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003808 struct ext4_prealloc_space *pa,
3809 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003810{
Alex Tomasc9de5602008-01-29 00:19:52 -05003811 struct super_block *sb = e4b->bd_sb;
3812 ext4_group_t group;
3813 ext4_grpblk_t bit;
3814
Eric Sandeen256bdb42008-02-10 01:13:33 -05003815 if (ac)
3816 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
Alex Tomasc9de5602008-01-29 00:19:52 -05003817
3818 BUG_ON(pa->pa_deleted == 0);
3819 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3820 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3821 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3822 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3823
Eric Sandeen256bdb42008-02-10 01:13:33 -05003824 if (ac) {
3825 ac->ac_sb = sb;
3826 ac->ac_inode = NULL;
3827 ac->ac_b_ex.fe_group = group;
3828 ac->ac_b_ex.fe_start = bit;
3829 ac->ac_b_ex.fe_len = pa->pa_len;
3830 ac->ac_b_ex.fe_logical = 0;
3831 ext4_mb_store_history(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003832 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003833
3834 return 0;
3835}
3836
3837/*
3838 * releases all preallocations in given group
3839 *
3840 * first, we need to decide discard policy:
3841 * - when do we discard
3842 * 1) ENOSPC
3843 * - how many do we discard
3844 * 1) how many requested
3845 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003846static noinline_for_stack int
3847ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003848 ext4_group_t group, int needed)
3849{
3850 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3851 struct buffer_head *bitmap_bh = NULL;
3852 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003853 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003854 struct list_head list;
3855 struct ext4_buddy e4b;
3856 int err;
3857 int busy = 0;
3858 int free = 0;
3859
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003860 mb_debug("discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003861
3862 if (list_empty(&grp->bb_prealloc_list))
3863 return 0;
3864
Theodore Ts'o574ca172008-07-11 19:27:31 -04003865 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003866 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003867 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003868 "bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003869 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003870 }
3871
3872 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003873 if (err) {
3874 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003875 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003876 put_bh(bitmap_bh);
3877 return 0;
3878 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003879
3880 if (needed == 0)
3881 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3882
Alex Tomasc9de5602008-01-29 00:19:52 -05003883 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003884 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Alex Tomasc9de5602008-01-29 00:19:52 -05003885repeat:
3886 ext4_lock_group(sb, group);
3887 list_for_each_entry_safe(pa, tmp,
3888 &grp->bb_prealloc_list, pa_group_list) {
3889 spin_lock(&pa->pa_lock);
3890 if (atomic_read(&pa->pa_count)) {
3891 spin_unlock(&pa->pa_lock);
3892 busy = 1;
3893 continue;
3894 }
3895 if (pa->pa_deleted) {
3896 spin_unlock(&pa->pa_lock);
3897 continue;
3898 }
3899
3900 /* seems this one can be freed ... */
3901 pa->pa_deleted = 1;
3902
3903 /* we can trust pa_free ... */
3904 free += pa->pa_free;
3905
3906 spin_unlock(&pa->pa_lock);
3907
3908 list_del(&pa->pa_group_list);
3909 list_add(&pa->u.pa_tmp_list, &list);
3910 }
3911
3912 /* if we still need more blocks and some PAs were used, try again */
3913 if (free < needed && busy) {
3914 busy = 0;
3915 ext4_unlock_group(sb, group);
3916 /*
3917 * Yield the CPU here so that we don't get soft lockup
3918 * in non preempt case.
3919 */
3920 yield();
3921 goto repeat;
3922 }
3923
3924 /* found anything to free? */
3925 if (list_empty(&list)) {
3926 BUG_ON(free != 0);
3927 goto out;
3928 }
3929
3930 /* now free all selected PAs */
3931 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3932
3933 /* remove from object (inode or locality group) */
3934 spin_lock(pa->pa_obj_lock);
3935 list_del_rcu(&pa->pa_inode_list);
3936 spin_unlock(pa->pa_obj_lock);
3937
3938 if (pa->pa_linear)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003939 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003940 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003941 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003942
3943 list_del(&pa->u.pa_tmp_list);
3944 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3945 }
3946
3947out:
3948 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003949 if (ac)
3950 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003951 ext4_mb_release_desc(&e4b);
3952 put_bh(bitmap_bh);
3953 return free;
3954}
3955
3956/*
3957 * releases all non-used preallocated blocks for given inode
3958 *
3959 * It's important to discard preallocations under i_data_sem
3960 * We don't want another block to be served from the prealloc
3961 * space when we are discarding the inode prealloc space.
3962 *
3963 * FIXME!! Make sure it is valid at all the call sites
3964 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003965void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003966{
3967 struct ext4_inode_info *ei = EXT4_I(inode);
3968 struct super_block *sb = inode->i_sb;
3969 struct buffer_head *bitmap_bh = NULL;
3970 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003971 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003972 ext4_group_t group = 0;
3973 struct list_head list;
3974 struct ext4_buddy e4b;
3975 int err;
3976
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003977 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003978 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3979 return;
3980 }
3981
3982 mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
3983
3984 INIT_LIST_HEAD(&list);
3985
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003986 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Alex Tomasc9de5602008-01-29 00:19:52 -05003987repeat:
3988 /* first, collect all pa's in the inode */
3989 spin_lock(&ei->i_prealloc_lock);
3990 while (!list_empty(&ei->i_prealloc_list)) {
3991 pa = list_entry(ei->i_prealloc_list.next,
3992 struct ext4_prealloc_space, pa_inode_list);
3993 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3994 spin_lock(&pa->pa_lock);
3995 if (atomic_read(&pa->pa_count)) {
3996 /* this shouldn't happen often - nobody should
3997 * use preallocation while we're discarding it */
3998 spin_unlock(&pa->pa_lock);
3999 spin_unlock(&ei->i_prealloc_lock);
4000 printk(KERN_ERR "uh-oh! used pa while discarding\n");
4001 WARN_ON(1);
4002 schedule_timeout_uninterruptible(HZ);
4003 goto repeat;
4004
4005 }
4006 if (pa->pa_deleted == 0) {
4007 pa->pa_deleted = 1;
4008 spin_unlock(&pa->pa_lock);
4009 list_del_rcu(&pa->pa_inode_list);
4010 list_add(&pa->u.pa_tmp_list, &list);
4011 continue;
4012 }
4013
4014 /* someone is deleting pa right now */
4015 spin_unlock(&pa->pa_lock);
4016 spin_unlock(&ei->i_prealloc_lock);
4017
4018 /* we have to wait here because pa_deleted
4019 * doesn't mean pa is already unlinked from
4020 * the list. as we might be called from
4021 * ->clear_inode() the inode will get freed
4022 * and concurrent thread which is unlinking
4023 * pa from inode's list may access already
4024 * freed memory, bad-bad-bad */
4025
4026 /* XXX: if this happens too often, we can
4027 * add a flag to force wait only in case
4028 * of ->clear_inode(), but not in case of
4029 * regular truncate */
4030 schedule_timeout_uninterruptible(HZ);
4031 goto repeat;
4032 }
4033 spin_unlock(&ei->i_prealloc_lock);
4034
4035 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4036 BUG_ON(pa->pa_linear != 0);
4037 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4038
4039 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004040 if (err) {
4041 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004042 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004043 continue;
4044 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004045
Theodore Ts'o574ca172008-07-11 19:27:31 -04004046 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004047 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004048 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004049 "bitmap for %u", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004050 ext4_mb_release_desc(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004051 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004052 }
4053
4054 ext4_lock_group(sb, group);
4055 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004056 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004057 ext4_unlock_group(sb, group);
4058
4059 ext4_mb_release_desc(&e4b);
4060 put_bh(bitmap_bh);
4061
4062 list_del(&pa->u.pa_tmp_list);
4063 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4064 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004065 if (ac)
4066 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004067}
4068
4069/*
4070 * finds all preallocated spaces and return blocks being freed to them
4071 * if preallocated space becomes full (no block is used from the space)
4072 * then the function frees space in buddy
4073 * XXX: at the moment, truncate (which is the only way to free blocks)
4074 * discards all preallocations
4075 */
4076static void ext4_mb_return_to_preallocation(struct inode *inode,
4077 struct ext4_buddy *e4b,
4078 sector_t block, int count)
4079{
4080 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
4081}
4082#ifdef MB_DEBUG
4083static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4084{
4085 struct super_block *sb = ac->ac_sb;
4086 ext4_group_t i;
4087
4088 printk(KERN_ERR "EXT4-fs: Can't allocate:"
4089 " Allocation context details:\n");
4090 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
4091 ac->ac_status, ac->ac_flags);
4092 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4093 "best %lu/%lu/%lu@%lu cr %d\n",
4094 (unsigned long)ac->ac_o_ex.fe_group,
4095 (unsigned long)ac->ac_o_ex.fe_start,
4096 (unsigned long)ac->ac_o_ex.fe_len,
4097 (unsigned long)ac->ac_o_ex.fe_logical,
4098 (unsigned long)ac->ac_g_ex.fe_group,
4099 (unsigned long)ac->ac_g_ex.fe_start,
4100 (unsigned long)ac->ac_g_ex.fe_len,
4101 (unsigned long)ac->ac_g_ex.fe_logical,
4102 (unsigned long)ac->ac_b_ex.fe_group,
4103 (unsigned long)ac->ac_b_ex.fe_start,
4104 (unsigned long)ac->ac_b_ex.fe_len,
4105 (unsigned long)ac->ac_b_ex.fe_logical,
4106 (int)ac->ac_criteria);
4107 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4108 ac->ac_found);
4109 printk(KERN_ERR "EXT4-fs: groups: \n");
4110 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
4111 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4112 struct ext4_prealloc_space *pa;
4113 ext4_grpblk_t start;
4114 struct list_head *cur;
4115 ext4_lock_group(sb, i);
4116 list_for_each(cur, &grp->bb_prealloc_list) {
4117 pa = list_entry(cur, struct ext4_prealloc_space,
4118 pa_group_list);
4119 spin_lock(&pa->pa_lock);
4120 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4121 NULL, &start);
4122 spin_unlock(&pa->pa_lock);
4123 printk(KERN_ERR "PA:%lu:%d:%u \n", i,
4124 start, pa->pa_len);
4125 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004126 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004127
4128 if (grp->bb_free == 0)
4129 continue;
4130 printk(KERN_ERR "%lu: %d/%d \n",
4131 i, grp->bb_free, grp->bb_fragments);
4132 }
4133 printk(KERN_ERR "\n");
4134}
4135#else
4136static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4137{
4138 return;
4139}
4140#endif
4141
4142/*
4143 * We use locality group preallocation for small size file. The size of the
4144 * file is determined by the current size or the resulting size after
4145 * allocation which ever is larger
4146 *
4147 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4148 */
4149static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4150{
4151 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4152 int bsbits = ac->ac_sb->s_blocksize_bits;
4153 loff_t size, isize;
4154
4155 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4156 return;
4157
4158 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4159 isize = i_size_read(ac->ac_inode) >> bsbits;
4160 size = max(size, isize);
4161
4162 /* don't use group allocation for large files */
4163 if (size >= sbi->s_mb_stream_request)
4164 return;
4165
4166 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4167 return;
4168
4169 BUG_ON(ac->ac_lg != NULL);
4170 /*
4171 * locality group prealloc space are per cpu. The reason for having
4172 * per cpu locality group is to reduce the contention between block
4173 * request from multiple CPUs.
4174 */
Eric Sandeen730c2132008-09-13 15:23:29 -04004175 ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
Alex Tomasc9de5602008-01-29 00:19:52 -05004176
4177 /* we're going to use group allocation */
4178 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4179
4180 /* serialize all allocations in the group */
4181 mutex_lock(&ac->ac_lg->lg_mutex);
4182}
4183
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004184static noinline_for_stack int
4185ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004186 struct ext4_allocation_request *ar)
4187{
4188 struct super_block *sb = ar->inode->i_sb;
4189 struct ext4_sb_info *sbi = EXT4_SB(sb);
4190 struct ext4_super_block *es = sbi->s_es;
4191 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004192 unsigned int len;
4193 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004194 ext4_grpblk_t block;
4195
4196 /* we can't allocate > group size */
4197 len = ar->len;
4198
4199 /* just a dirty hack to filter too big requests */
4200 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4201 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4202
4203 /* start searching from the goal */
4204 goal = ar->goal;
4205 if (goal < le32_to_cpu(es->s_first_data_block) ||
4206 goal >= ext4_blocks_count(es))
4207 goal = le32_to_cpu(es->s_first_data_block);
4208 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4209
4210 /* set up allocation goals */
4211 ac->ac_b_ex.fe_logical = ar->logical;
4212 ac->ac_b_ex.fe_group = 0;
4213 ac->ac_b_ex.fe_start = 0;
4214 ac->ac_b_ex.fe_len = 0;
4215 ac->ac_status = AC_STATUS_CONTINUE;
4216 ac->ac_groups_scanned = 0;
4217 ac->ac_ex_scanned = 0;
4218 ac->ac_found = 0;
4219 ac->ac_sb = sb;
4220 ac->ac_inode = ar->inode;
4221 ac->ac_o_ex.fe_logical = ar->logical;
4222 ac->ac_o_ex.fe_group = group;
4223 ac->ac_o_ex.fe_start = block;
4224 ac->ac_o_ex.fe_len = len;
4225 ac->ac_g_ex.fe_logical = ar->logical;
4226 ac->ac_g_ex.fe_group = group;
4227 ac->ac_g_ex.fe_start = block;
4228 ac->ac_g_ex.fe_len = len;
4229 ac->ac_f_ex.fe_len = 0;
4230 ac->ac_flags = ar->flags;
4231 ac->ac_2order = 0;
4232 ac->ac_criteria = 0;
4233 ac->ac_pa = NULL;
4234 ac->ac_bitmap_page = NULL;
4235 ac->ac_buddy_page = NULL;
4236 ac->ac_lg = NULL;
4237
4238 /* we have to define context: we'll we work with a file or
4239 * locality group. this is a policy, actually */
4240 ext4_mb_group_or_file(ac);
4241
4242 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4243 "left: %u/%u, right %u/%u to %swritable\n",
4244 (unsigned) ar->len, (unsigned) ar->logical,
4245 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4246 (unsigned) ar->lleft, (unsigned) ar->pleft,
4247 (unsigned) ar->lright, (unsigned) ar->pright,
4248 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4249 return 0;
4250
4251}
4252
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004253static noinline_for_stack void
4254ext4_mb_discard_lg_preallocations(struct super_block *sb,
4255 struct ext4_locality_group *lg,
4256 int order, int total_entries)
4257{
4258 ext4_group_t group = 0;
4259 struct ext4_buddy e4b;
4260 struct list_head discard_list;
4261 struct ext4_prealloc_space *pa, *tmp;
4262 struct ext4_allocation_context *ac;
4263
4264 mb_debug("discard locality group preallocation\n");
4265
4266 INIT_LIST_HEAD(&discard_list);
4267 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4268
4269 spin_lock(&lg->lg_prealloc_lock);
4270 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4271 pa_inode_list) {
4272 spin_lock(&pa->pa_lock);
4273 if (atomic_read(&pa->pa_count)) {
4274 /*
4275 * This is the pa that we just used
4276 * for block allocation. So don't
4277 * free that
4278 */
4279 spin_unlock(&pa->pa_lock);
4280 continue;
4281 }
4282 if (pa->pa_deleted) {
4283 spin_unlock(&pa->pa_lock);
4284 continue;
4285 }
4286 /* only lg prealloc space */
4287 BUG_ON(!pa->pa_linear);
4288
4289 /* seems this one can be freed ... */
4290 pa->pa_deleted = 1;
4291 spin_unlock(&pa->pa_lock);
4292
4293 list_del_rcu(&pa->pa_inode_list);
4294 list_add(&pa->u.pa_tmp_list, &discard_list);
4295
4296 total_entries--;
4297 if (total_entries <= 5) {
4298 /*
4299 * we want to keep only 5 entries
4300 * allowing it to grow to 8. This
4301 * mak sure we don't call discard
4302 * soon for this list.
4303 */
4304 break;
4305 }
4306 }
4307 spin_unlock(&lg->lg_prealloc_lock);
4308
4309 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4310
4311 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4312 if (ext4_mb_load_buddy(sb, group, &e4b)) {
4313 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004314 "information for %u", group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004315 continue;
4316 }
4317 ext4_lock_group(sb, group);
4318 list_del(&pa->pa_group_list);
4319 ext4_mb_release_group_pa(&e4b, pa, ac);
4320 ext4_unlock_group(sb, group);
4321
4322 ext4_mb_release_desc(&e4b);
4323 list_del(&pa->u.pa_tmp_list);
4324 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4325 }
4326 if (ac)
4327 kmem_cache_free(ext4_ac_cachep, ac);
4328}
4329
4330/*
4331 * We have incremented pa_count. So it cannot be freed at this
4332 * point. Also we hold lg_mutex. So no parallel allocation is
4333 * possible from this lg. That means pa_free cannot be updated.
4334 *
4335 * A parallel ext4_mb_discard_group_preallocations is possible.
4336 * which can cause the lg_prealloc_list to be updated.
4337 */
4338
4339static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4340{
4341 int order, added = 0, lg_prealloc_count = 1;
4342 struct super_block *sb = ac->ac_sb;
4343 struct ext4_locality_group *lg = ac->ac_lg;
4344 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4345
4346 order = fls(pa->pa_free) - 1;
4347 if (order > PREALLOC_TB_SIZE - 1)
4348 /* The max size of hash table is PREALLOC_TB_SIZE */
4349 order = PREALLOC_TB_SIZE - 1;
4350 /* Add the prealloc space to lg */
4351 rcu_read_lock();
4352 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4353 pa_inode_list) {
4354 spin_lock(&tmp_pa->pa_lock);
4355 if (tmp_pa->pa_deleted) {
4356 spin_unlock(&pa->pa_lock);
4357 continue;
4358 }
4359 if (!added && pa->pa_free < tmp_pa->pa_free) {
4360 /* Add to the tail of the previous entry */
4361 list_add_tail_rcu(&pa->pa_inode_list,
4362 &tmp_pa->pa_inode_list);
4363 added = 1;
4364 /*
4365 * we want to count the total
4366 * number of entries in the list
4367 */
4368 }
4369 spin_unlock(&tmp_pa->pa_lock);
4370 lg_prealloc_count++;
4371 }
4372 if (!added)
4373 list_add_tail_rcu(&pa->pa_inode_list,
4374 &lg->lg_prealloc_list[order]);
4375 rcu_read_unlock();
4376
4377 /* Now trim the list to be not more than 8 elements */
4378 if (lg_prealloc_count > 8) {
4379 ext4_mb_discard_lg_preallocations(sb, lg,
4380 order, lg_prealloc_count);
4381 return;
4382 }
4383 return ;
4384}
4385
Alex Tomasc9de5602008-01-29 00:19:52 -05004386/*
4387 * release all resource we used in allocation
4388 */
4389static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4390{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004391 struct ext4_prealloc_space *pa = ac->ac_pa;
4392 if (pa) {
4393 if (pa->pa_linear) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004394 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004395 spin_lock(&pa->pa_lock);
4396 pa->pa_pstart += ac->ac_b_ex.fe_len;
4397 pa->pa_lstart += ac->ac_b_ex.fe_len;
4398 pa->pa_free -= ac->ac_b_ex.fe_len;
4399 pa->pa_len -= ac->ac_b_ex.fe_len;
4400 spin_unlock(&pa->pa_lock);
4401 /*
4402 * We want to add the pa to the right bucket.
4403 * Remove it from the list and while adding
4404 * make sure the list to which we are adding
4405 * doesn't grow big.
4406 */
4407 if (likely(pa->pa_free)) {
4408 spin_lock(pa->pa_obj_lock);
4409 list_del_rcu(&pa->pa_inode_list);
4410 spin_unlock(pa->pa_obj_lock);
4411 ext4_mb_add_n_trim(ac);
4412 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004413 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004414 ext4_mb_put_pa(ac, ac->ac_sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004415 }
4416 if (ac->ac_bitmap_page)
4417 page_cache_release(ac->ac_bitmap_page);
4418 if (ac->ac_buddy_page)
4419 page_cache_release(ac->ac_buddy_page);
4420 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4421 mutex_unlock(&ac->ac_lg->lg_mutex);
4422 ext4_mb_collect_stats(ac);
4423 return 0;
4424}
4425
4426static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4427{
4428 ext4_group_t i;
4429 int ret;
4430 int freed = 0;
4431
4432 for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
4433 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4434 freed += ret;
4435 needed -= ret;
4436 }
4437
4438 return freed;
4439}
4440
4441/*
4442 * Main entry point into mballoc to allocate blocks
4443 * it tries to use preallocation first, then falls back
4444 * to usual allocation
4445 */
4446ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4447 struct ext4_allocation_request *ar, int *errp)
4448{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004449 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004450 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004451 struct ext4_sb_info *sbi;
4452 struct super_block *sb;
4453 ext4_fsblk_t block = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004454 unsigned int inquota;
4455 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004456
4457 sb = ar->inode->i_sb;
4458 sbi = EXT4_SB(sb);
4459
Mingming Caod2a17632008-07-14 17:52:37 -04004460 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) {
4461 /*
4462 * With delalloc we already reserved the blocks
4463 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004464 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4465 /* let others to free the space */
4466 yield();
4467 ar->len = ar->len >> 1;
4468 }
4469 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004470 *errp = -ENOSPC;
4471 return 0;
4472 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004473 reserv_blks = ar->len;
Mingming Caod2a17632008-07-14 17:52:37 -04004474 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004475 while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
4476 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4477 ar->len--;
4478 }
4479 if (ar->len == 0) {
4480 *errp = -EDQUOT;
4481 return 0;
4482 }
4483 inquota = ar->len;
4484
Mingming Caod2a17632008-07-14 17:52:37 -04004485 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4486 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4487
Eric Sandeen256bdb42008-02-10 01:13:33 -05004488 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4489 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004490 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004491 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004492 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004493 }
4494
Eric Sandeen256bdb42008-02-10 01:13:33 -05004495 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004496 if (*errp) {
4497 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004498 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004499 }
4500
Eric Sandeen256bdb42008-02-10 01:13:33 -05004501 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4502 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004503 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4504 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004505repeat:
4506 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004507 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004508
4509 /* as we've just preallocated more space than
4510 * user requested orinally, we store allocated
4511 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004512 if (ac->ac_status == AC_STATUS_FOUND &&
4513 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4514 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004515 }
4516
Eric Sandeen256bdb42008-02-10 01:13:33 -05004517 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004518 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004519 if (*errp == -EAGAIN) {
4520 ac->ac_b_ex.fe_group = 0;
4521 ac->ac_b_ex.fe_start = 0;
4522 ac->ac_b_ex.fe_len = 0;
4523 ac->ac_status = AC_STATUS_CONTINUE;
4524 goto repeat;
4525 } else if (*errp) {
4526 ac->ac_b_ex.fe_len = 0;
4527 ar->len = 0;
4528 ext4_mb_show_ac(ac);
4529 } else {
4530 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4531 ar->len = ac->ac_b_ex.fe_len;
4532 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004533 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004534 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004535 if (freed)
4536 goto repeat;
4537 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004538 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004539 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004540 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004541 }
4542
Eric Sandeen256bdb42008-02-10 01:13:33 -05004543 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004544
Shen Feng363d4252008-07-11 19:27:31 -04004545out2:
4546 kmem_cache_free(ext4_ac_cachep, ac);
4547out1:
Alex Tomasc9de5602008-01-29 00:19:52 -05004548 if (ar->len < inquota)
4549 DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
4550
4551 return block;
4552}
Alex Tomasc9de5602008-01-29 00:19:52 -05004553
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004554/*
4555 * We can merge two free data extents only if the physical blocks
4556 * are contiguous, AND the extents were freed by the same transaction,
4557 * AND the blocks are associated with the same group.
4558 */
4559static int can_merge(struct ext4_free_data *entry1,
4560 struct ext4_free_data *entry2)
4561{
4562 if ((entry1->t_tid == entry2->t_tid) &&
4563 (entry1->group == entry2->group) &&
4564 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4565 return 1;
4566 return 0;
4567}
4568
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004569static noinline_for_stack int
4570ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05004571 ext4_group_t group, ext4_grpblk_t block, int count)
4572{
4573 struct ext4_group_info *db = e4b->bd_info;
4574 struct super_block *sb = e4b->bd_sb;
4575 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004576 struct ext4_free_data *entry, *new_entry;
4577 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4578 struct rb_node *parent = NULL, *new_node;
4579
Frank Mayhar03901312009-01-07 00:06:22 -05004580 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004581 BUG_ON(e4b->bd_bitmap_page == NULL);
4582 BUG_ON(e4b->bd_buddy_page == NULL);
4583
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004584 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4585 new_entry->start_blk = block;
4586 new_entry->group = group;
4587 new_entry->count = count;
4588 new_entry->t_tid = handle->h_transaction->t_tid;
4589 new_node = &new_entry->node;
4590
Alex Tomasc9de5602008-01-29 00:19:52 -05004591 ext4_lock_group(sb, group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004592 if (!*n) {
4593 /* first free block exent. We need to
4594 protect buddy cache from being freed,
4595 * otherwise we'll refresh it from
4596 * on-disk bitmap and lose not-yet-available
4597 * blocks */
4598 page_cache_get(e4b->bd_buddy_page);
4599 page_cache_get(e4b->bd_bitmap_page);
4600 }
4601 while (*n) {
4602 parent = *n;
4603 entry = rb_entry(parent, struct ext4_free_data, node);
4604 if (block < entry->start_blk)
4605 n = &(*n)->rb_left;
4606 else if (block >= (entry->start_blk + entry->count))
4607 n = &(*n)->rb_right;
4608 else {
Aneesh Kumar K.Vae2d9fb2008-11-04 09:10:50 -05004609 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004610 ext4_error(sb, __func__,
Theodore Ts'ofde4d952009-01-05 22:17:35 -05004611 "Double free of blocks %d (%d %d)",
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004612 block, entry->start_blk, entry->count);
4613 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004614 }
4615 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004616
4617 rb_link_node(new_node, parent, n);
4618 rb_insert_color(new_node, &db->bb_free_root);
4619
4620 /* Now try to see the extent can be merged to left and right */
4621 node = rb_prev(new_node);
4622 if (node) {
4623 entry = rb_entry(node, struct ext4_free_data, node);
4624 if (can_merge(entry, new_entry)) {
4625 new_entry->start_blk = entry->start_blk;
4626 new_entry->count += entry->count;
4627 rb_erase(node, &(db->bb_free_root));
4628 spin_lock(&sbi->s_md_lock);
4629 list_del(&entry->list);
4630 spin_unlock(&sbi->s_md_lock);
4631 kmem_cache_free(ext4_free_ext_cachep, entry);
4632 }
4633 }
4634
4635 node = rb_next(new_node);
4636 if (node) {
4637 entry = rb_entry(node, struct ext4_free_data, node);
4638 if (can_merge(new_entry, entry)) {
4639 new_entry->count += entry->count;
4640 rb_erase(node, &(db->bb_free_root));
4641 spin_lock(&sbi->s_md_lock);
4642 list_del(&entry->list);
4643 spin_unlock(&sbi->s_md_lock);
4644 kmem_cache_free(ext4_free_ext_cachep, entry);
4645 }
4646 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004647 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004648 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004649 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004650 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004651 ext4_unlock_group(sb, group);
4652 return 0;
4653}
4654
4655/*
4656 * Main entry point into mballoc to free blocks
4657 */
4658void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
4659 unsigned long block, unsigned long count,
4660 int metadata, unsigned long *freed)
4661{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004662 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004664 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004665 struct ext4_group_desc *gdp;
4666 struct ext4_super_block *es;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004667 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004668 ext4_grpblk_t bit;
4669 struct buffer_head *gd_bh;
4670 ext4_group_t block_group;
4671 struct ext4_sb_info *sbi;
4672 struct ext4_buddy e4b;
4673 int err = 0;
4674 int ret;
4675
4676 *freed = 0;
4677
Alex Tomasc9de5602008-01-29 00:19:52 -05004678 sbi = EXT4_SB(sb);
4679 es = EXT4_SB(sb)->s_es;
4680 if (block < le32_to_cpu(es->s_first_data_block) ||
4681 block + count < block ||
4682 block + count > ext4_blocks_count(es)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004683 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004684 "Freeing blocks not in datazone - "
4685 "block = %lu, count = %lu", block, count);
4686 goto error_return;
4687 }
4688
4689 ext4_debug("freeing block %lu\n", block);
4690
Eric Sandeen256bdb42008-02-10 01:13:33 -05004691 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4692 if (ac) {
4693 ac->ac_op = EXT4_MB_HISTORY_FREE;
4694 ac->ac_inode = inode;
4695 ac->ac_sb = sb;
4696 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004697
4698do_more:
4699 overflow = 0;
4700 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4701
4702 /*
4703 * Check to see if we are freeing blocks across a group
4704 * boundary.
4705 */
4706 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4707 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4708 count -= overflow;
4709 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004710 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004711 if (!bitmap_bh) {
4712 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004713 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004714 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004715 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004716 if (!gdp) {
4717 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004718 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004719 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004720
4721 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4722 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4723 in_range(block, ext4_inode_table(sb, gdp),
4724 EXT4_SB(sb)->s_itb_per_group) ||
4725 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4726 EXT4_SB(sb)->s_itb_per_group)) {
4727
Harvey Harrison46e665e2008-04-17 10:38:59 -04004728 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004729 "Freeing blocks in system zone - "
4730 "Block = %lu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004731 /* err = 0. ext4_std_error should be a no op */
4732 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004733 }
4734
4735 BUFFER_TRACE(bitmap_bh, "getting write access");
4736 err = ext4_journal_get_write_access(handle, bitmap_bh);
4737 if (err)
4738 goto error_return;
4739
4740 /*
4741 * We are about to modify some metadata. Call the journal APIs
4742 * to unshare ->b_data if a currently-committing transaction is
4743 * using it
4744 */
4745 BUFFER_TRACE(gd_bh, "get_write_access");
4746 err = ext4_journal_get_write_access(handle, gd_bh);
4747 if (err)
4748 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004749#ifdef AGGRESSIVE_CHECK
4750 {
4751 int i;
4752 for (i = 0; i < count; i++)
4753 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4754 }
4755#endif
4756 mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
4757 bit, count);
4758
4759 /* We dirtied the bitmap block */
4760 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
Frank Mayhar03901312009-01-07 00:06:22 -05004761 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004762 if (err)
4763 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004764
Eric Sandeen256bdb42008-02-10 01:13:33 -05004765 if (ac) {
4766 ac->ac_b_ex.fe_group = block_group;
4767 ac->ac_b_ex.fe_start = bit;
4768 ac->ac_b_ex.fe_len = count;
4769 ext4_mb_store_history(ac);
4770 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004771
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004772 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4773 if (err)
4774 goto error_return;
Frank Mayhar03901312009-01-07 00:06:22 -05004775 if (metadata && ext4_handle_valid(handle)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004776 /* blocks being freed are metadata. these blocks shouldn't
4777 * be used until this transaction is committed */
4778 ext4_mb_free_metadata(handle, &e4b, block_group, bit, count);
4779 } else {
4780 ext4_lock_group(sb, block_group);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004781 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004782 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4783 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004784 }
4785
4786 spin_lock(sb_bgl_lock(sbi, block_group));
Marcin Slusarze8546d02008-04-17 10:38:59 -04004787 le16_add_cpu(&gdp->bg_free_blocks_count, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004788 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4789 spin_unlock(sb_bgl_lock(sbi, block_group));
4790 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4791
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004792 if (sbi->s_log_groups_per_flex) {
4793 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4794 spin_lock(sb_bgl_lock(sbi, flex_group));
4795 sbi->s_flex_groups[flex_group].free_blocks += count;
4796 spin_unlock(sb_bgl_lock(sbi, flex_group));
4797 }
4798
Alex Tomasc9de5602008-01-29 00:19:52 -05004799 ext4_mb_release_desc(&e4b);
4800
4801 *freed += count;
4802
4803 /* And the group descriptor block */
4804 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004805 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004806 if (!err)
4807 err = ret;
4808
4809 if (overflow && !err) {
4810 block += count;
4811 count = overflow;
4812 put_bh(bitmap_bh);
4813 goto do_more;
4814 }
4815 sb->s_dirt = 1;
4816error_return:
4817 brelse(bitmap_bh);
4818 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004819 if (ac)
4820 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004821 return;
4822}