2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
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.
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.
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-
21 * mballoc.c contains the multiblocks allocation routines
25 #include <linux/debugfs.h>
26 #include <trace/events/ext4.h>
30 * - test ext4_ext_search_left() and ext4_ext_search_right()
31 * - search for metadata in few groups
34 * - normalization should take into account whether file is still open
35 * - discard preallocations if no free space left (policy?)
36 * - don't normalize tails
38 * - reservation for superuser
41 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
42 * - track min/max extents in each group for better group selection
43 * - mb_mark_used() may allocate chunk right after splitting buddy
44 * - tree of groups sorted by number of free blocks
49 * The allocation request involve request for multiple number of blocks
50 * near to the goal(block) value specified.
52 * During initialization phase of the allocator we decide to use the
53 * group preallocation or inode preallocation depending on the size of
54 * the file. The size of the file could be the resulting file size we
55 * would have after allocation, or the current file size, which ever
56 * is larger. If the size is less than sbi->s_mb_stream_request we
57 * select to use the group preallocation. The default value of
58 * s_mb_stream_request is 16 blocks. This can also be tuned via
59 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
60 * terms of number of blocks.
62 * The main motivation for having small file use group preallocation is to
63 * ensure that we have small files closer together on the disk.
65 * First stage the allocator looks at the inode prealloc list,
66 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
67 * spaces for this particular inode. The inode prealloc space is
70 * pa_lstart -> the logical start block for this prealloc space
71 * pa_pstart -> the physical start block for this prealloc space
72 * pa_len -> lenght for this prealloc space
73 * pa_free -> free space available in this prealloc space
75 * The inode preallocation space is used looking at the _logical_ start
76 * block. If only the logical file block falls within the range of prealloc
77 * space we will consume the particular prealloc space. This make sure that
78 * that the we have contiguous physical blocks representing the file blocks
80 * The important thing to be noted in case of inode prealloc space is that
81 * we don't modify the values associated to inode prealloc space except
84 * If we are not able to find blocks in the inode prealloc space and if we
85 * have the group allocation flag set then we look at the locality group
86 * prealloc space. These are per CPU prealloc list repreasented as
88 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 * The reason for having a per cpu locality group is to reduce the contention
91 * between CPUs. It is possible to get scheduled at this point.
93 * The locality group prealloc space is used looking at whether we have
94 * enough free space (pa_free) withing the prealloc space.
96 * If we can't allocate blocks via inode prealloc or/and locality group
97 * prealloc then we look at the buddy cache. The buddy cache is represented
98 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
99 * mapped to the buddy and bitmap information regarding different
100 * groups. The buddy information is attached to buddy cache inode so that
101 * we can access them through the page cache. The information regarding
102 * each group is loaded via ext4_mb_load_buddy. The information involve
103 * block bitmap and buddy information. The information are stored in the
107 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
110 * one block each for bitmap and buddy information. So for each group we
111 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
112 * blocksize) blocks. So it can have information regarding groups_per_page
113 * which is blocks_per_page/2
115 * The buddy cache inode is not stored on disk. The inode is thrown
116 * away when the filesystem is unmounted.
118 * We look for count number of blocks in the buddy cache. If we were able
119 * to locate that many free blocks we return with additional information
120 * regarding rest of the contiguous physical block available
122 * Before allocating blocks via buddy cache we normalize the request
123 * blocks. This ensure we ask for more blocks that we needed. The extra
124 * blocks that we get after allocation is added to the respective prealloc
125 * list. In case of inode preallocation we follow a list of heuristics
126 * based on file size. This can be found in ext4_mb_normalize_request. If
127 * we are doing a group prealloc we try to normalize the request to
128 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
129 * 512 blocks. This can be tuned via
130 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
131 * terms of number of blocks. If we have mounted the file system with -O
132 * stripe=<value> option the group prealloc request is normalized to the
133 * stripe value (sbi->s_stripe)
135 * The regular allocator(using the buddy cache) supports few tunables.
137 * /sys/fs/ext4/<partition>/mb_min_to_scan
138 * /sys/fs/ext4/<partition>/mb_max_to_scan
139 * /sys/fs/ext4/<partition>/mb_order2_req
141 * The regular allocator uses buddy scan only if the request len is power of
142 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
143 * value of s_mb_order2_reqs can be tuned via
144 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
145 * stripe size (sbi->s_stripe), we try to search for contiguous block in
146 * stripe size. This should result in better allocation on RAID setups. If
147 * not, we search in the specific group using bitmap for best extents. The
148 * tunable min_to_scan and max_to_scan control the behaviour here.
149 * min_to_scan indicate how long the mballoc __must__ look for a best
150 * extent and max_to_scan indicates how long the mballoc __can__ look for a
151 * best extent in the found extents. Searching for the blocks starts with
152 * the group specified as the goal value in allocation context via
153 * ac_g_ex. Each group is first checked based on the criteria whether it
154 * can used for allocation. ext4_mb_good_group explains how the groups are
157 * Both the prealloc space are getting populated as above. So for the first
158 * request we will hit the buddy cache which will result in this prealloc
159 * space getting filled. The prealloc space is then later used for the
160 * subsequent request.
164 * mballoc operates on the following data:
166 * - in-core buddy (actually includes buddy and bitmap)
167 * - preallocation descriptors (PAs)
169 * there are two types of preallocations:
171 * assiged to specific inode and can be used for this inode only.
172 * it describes part of inode's space preallocated to specific
173 * physical blocks. any block from that preallocated can be used
174 * independent. the descriptor just tracks number of blocks left
175 * unused. so, before taking some block from descriptor, one must
176 * make sure corresponded logical block isn't allocated yet. this
177 * also means that freeing any block within descriptor's range
178 * must discard all preallocated blocks.
180 * assigned to specific locality group which does not translate to
181 * permanent set of inodes: inode can join and leave group. space
182 * from this type of preallocation can be used for any inode. thus
183 * it's consumed from the beginning to the end.
185 * relation between them can be expressed as:
186 * in-core buddy = on-disk bitmap + preallocation descriptors
188 * this mean blocks mballoc considers used are:
189 * - allocated blocks (persistent)
190 * - preallocated blocks (non-persistent)
192 * consistency in mballoc world means that at any time a block is either
193 * free or used in ALL structures. notice: "any time" should not be read
194 * literally -- time is discrete and delimited by locks.
196 * to keep it simple, we don't use block numbers, instead we count number of
197 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 * all operations can be expressed as:
200 * - init buddy: buddy = on-disk + PAs
201 * - new PA: buddy += N; PA = N
202 * - use inode PA: on-disk += N; PA -= N
203 * - discard inode PA buddy -= on-disk - PA; PA = 0
204 * - use locality group PA on-disk += N; PA -= N
205 * - discard locality group PA buddy -= PA; PA = 0
206 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
207 * is used in real operation because we can't know actual used
208 * bits from PA, only from on-disk bitmap
210 * if we follow this strict logic, then all operations above should be atomic.
211 * given some of them can block, we'd have to use something like semaphores
212 * killing performance on high-end SMP hardware. let's try to relax it using
213 * the following knowledge:
214 * 1) if buddy is referenced, it's already initialized
215 * 2) while block is used in buddy and the buddy is referenced,
216 * nobody can re-allocate that block
217 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
218 * bit set and PA claims same block, it's OK. IOW, one can set bit in
219 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
222 * so, now we're building a concurrency table:
225 * blocks for PA are allocated in the buddy, buddy must be referenced
226 * until PA is linked to allocation group to avoid concurrent buddy init
228 * we need to make sure that either on-disk bitmap or PA has uptodate data
229 * given (3) we care that PA-=N operation doesn't interfere with init
231 * the simplest way would be to have buddy initialized by the discard
232 * - use locality group PA
233 * again PA-=N must be serialized with init
234 * - discard locality group PA
235 * the simplest way would be to have buddy initialized by the discard
238 * i_data_sem serializes them
240 * discard process must wait until PA isn't used by another process
241 * - use locality group PA
242 * some mutex should serialize them
243 * - discard locality group PA
244 * discard process must wait until PA isn't used by another process
247 * i_data_sem or another mutex should serializes them
249 * discard process must wait until PA isn't used by another process
250 * - use locality group PA
251 * nothing wrong here -- they're different PAs covering different blocks
252 * - discard locality group PA
253 * discard process must wait until PA isn't used by another process
255 * now we're ready to make few consequences:
256 * - PA is referenced and while it is no discard is possible
257 * - PA is referenced until block isn't marked in on-disk bitmap
258 * - PA changes only after on-disk bitmap
259 * - discard must not compete with init. either init is done before
260 * any discard or they're serialized somehow
261 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 * a special case when we've used PA to emptiness. no need to modify buddy
264 * in this case, but we should care about concurrent init
269 * Logic in few words:
274 * mark bits in on-disk bitmap
277 * - use preallocation:
278 * find proper PA (per-inode or group)
280 * mark bits in on-disk bitmap
286 * mark bits in on-disk bitmap
289 * - discard preallocations in group:
291 * move them onto local list
292 * load on-disk bitmap
294 * remove PA from object (inode or locality group)
295 * mark free blocks in-core
297 * - discard inode's preallocations:
304 * - bitlock on a group (group)
305 * - object (inode/locality) (object)
316 * - release consumed pa:
321 * - generate in-core bitmap:
325 * - discard all for given object (inode, locality group):
330 * - discard all for given group:
337 static struct kmem_cache *ext4_pspace_cachep;
338 static struct kmem_cache *ext4_ac_cachep;
339 static struct kmem_cache *ext4_free_ext_cachep;
340 static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
342 static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
344 static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
346 static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
348 #if BITS_PER_LONG == 64
349 *bit += ((unsigned long) addr & 7UL) << 3;
350 addr = (void *) ((unsigned long) addr & ~7UL);
351 #elif BITS_PER_LONG == 32
352 *bit += ((unsigned long) addr & 3UL) << 3;
353 addr = (void *) ((unsigned long) addr & ~3UL);
355 #error "how many bits you are?!"
360 static inline int mb_test_bit(int bit, void *addr)
363 * ext4_test_bit on architecture like powerpc
364 * needs unsigned long aligned address
366 addr = mb_correct_addr_and_bit(&bit, addr);
367 return ext4_test_bit(bit, addr);
370 static inline void mb_set_bit(int bit, void *addr)
372 addr = mb_correct_addr_and_bit(&bit, addr);
373 ext4_set_bit(bit, addr);
376 static inline void mb_clear_bit(int bit, void *addr)
378 addr = mb_correct_addr_and_bit(&bit, addr);
379 ext4_clear_bit(bit, addr);
382 static inline int mb_find_next_zero_bit(void *addr, int max, int start)
384 int fix = 0, ret, tmpmax;
385 addr = mb_correct_addr_and_bit(&fix, addr);
389 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
395 static inline int mb_find_next_bit(void *addr, int max, int start)
397 int fix = 0, ret, tmpmax;
398 addr = mb_correct_addr_and_bit(&fix, addr);
402 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
408 static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
412 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
415 if (order > e4b->bd_blkbits + 1) {
420 /* at order 0 we see each particular block */
421 *max = 1 << (e4b->bd_blkbits + 3);
423 return EXT4_MB_BITMAP(e4b);
425 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
426 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
432 static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
433 int first, int count)
436 struct super_block *sb = e4b->bd_sb;
438 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
440 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
441 for (i = 0; i < count; i++) {
442 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
443 ext4_fsblk_t blocknr;
445 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
446 blocknr += first + i;
447 ext4_grp_locked_error(sb, e4b->bd_group,
448 __func__, "double-free of inode"
449 " %lu's block %llu(bit %u in group %u)",
450 inode ? inode->i_ino : 0, blocknr,
451 first + i, e4b->bd_group);
453 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
457 static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
461 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
463 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
464 for (i = 0; i < count; i++) {
465 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
466 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
470 static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
472 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
473 unsigned char *b1, *b2;
475 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
476 b2 = (unsigned char *) bitmap;
477 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
478 if (b1[i] != b2[i]) {
479 printk(KERN_ERR "corruption in group %u "
480 "at byte %u(%u): %x in copy != %x "
481 "on disk/prealloc\n",
482 e4b->bd_group, i, i * 8, b1[i], b2[i]);
490 static inline void mb_free_blocks_double(struct inode *inode,
491 struct ext4_buddy *e4b, int first, int count)
495 static inline void mb_mark_used_double(struct ext4_buddy *e4b,
496 int first, int count)
500 static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
506 #ifdef AGGRESSIVE_CHECK
508 #define MB_CHECK_ASSERT(assert) \
512 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
513 function, file, line, # assert); \
518 static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
519 const char *function, int line)
521 struct super_block *sb = e4b->bd_sb;
522 int order = e4b->bd_blkbits + 1;
529 struct ext4_group_info *grp;
532 struct list_head *cur;
537 static int mb_check_counter;
538 if (mb_check_counter++ % 100 != 0)
543 buddy = mb_find_buddy(e4b, order, &max);
544 MB_CHECK_ASSERT(buddy);
545 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
546 MB_CHECK_ASSERT(buddy2);
547 MB_CHECK_ASSERT(buddy != buddy2);
548 MB_CHECK_ASSERT(max * 2 == max2);
551 for (i = 0; i < max; i++) {
553 if (mb_test_bit(i, buddy)) {
554 /* only single bit in buddy2 may be 1 */
555 if (!mb_test_bit(i << 1, buddy2)) {
557 mb_test_bit((i<<1)+1, buddy2));
558 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
560 mb_test_bit(i << 1, buddy2));
565 /* both bits in buddy2 must be 0 */
566 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
567 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
569 for (j = 0; j < (1 << order); j++) {
570 k = (i * (1 << order)) + j;
572 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
576 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
581 buddy = mb_find_buddy(e4b, 0, &max);
582 for (i = 0; i < max; i++) {
583 if (!mb_test_bit(i, buddy)) {
584 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
592 /* check used bits only */
593 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
594 buddy2 = mb_find_buddy(e4b, j, &max2);
596 MB_CHECK_ASSERT(k < max2);
597 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
600 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
601 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
603 grp = ext4_get_group_info(sb, e4b->bd_group);
604 buddy = mb_find_buddy(e4b, 0, &max);
605 list_for_each(cur, &grp->bb_prealloc_list) {
606 ext4_group_t groupnr;
607 struct ext4_prealloc_space *pa;
608 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
609 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
610 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
611 for (i = 0; i < pa->pa_len; i++)
612 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
616 #undef MB_CHECK_ASSERT
617 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
618 __FILE__, __func__, __LINE__)
620 #define mb_check_buddy(e4b)
623 /* FIXME!! need more doc */
624 static void ext4_mb_mark_free_simple(struct super_block *sb,
625 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
626 struct ext4_group_info *grp)
628 struct ext4_sb_info *sbi = EXT4_SB(sb);
632 unsigned short border;
634 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
636 border = 2 << sb->s_blocksize_bits;
639 /* find how many blocks can be covered since this position */
640 max = ffs(first | border) - 1;
642 /* find how many blocks of power 2 we need to mark */
649 /* mark multiblock chunks only */
650 grp->bb_counters[min]++;
652 mb_clear_bit(first >> min,
653 buddy + sbi->s_mb_offsets[min]);
660 static noinline_for_stack
661 void ext4_mb_generate_buddy(struct super_block *sb,
662 void *buddy, void *bitmap, ext4_group_t group)
664 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
665 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
670 unsigned fragments = 0;
671 unsigned long long period = get_cycles();
673 /* initialize buddy from bitmap which is aggregation
674 * of on-disk bitmap and preallocations */
675 i = mb_find_next_zero_bit(bitmap, max, 0);
676 grp->bb_first_free = i;
680 i = mb_find_next_bit(bitmap, max, i);
684 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
686 grp->bb_counters[0]++;
688 i = mb_find_next_zero_bit(bitmap, max, i);
690 grp->bb_fragments = fragments;
692 if (free != grp->bb_free) {
693 ext4_grp_locked_error(sb, group, __func__,
694 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
695 group, free, grp->bb_free);
697 * If we intent to continue, we consider group descritor
698 * corrupt and update bb_free using bitmap value
703 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
705 period = get_cycles() - period;
706 spin_lock(&EXT4_SB(sb)->s_bal_lock);
707 EXT4_SB(sb)->s_mb_buddies_generated++;
708 EXT4_SB(sb)->s_mb_generation_time += period;
709 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
712 /* The buddy information is attached the buddy cache inode
713 * for convenience. The information regarding each group
714 * is loaded via ext4_mb_load_buddy. The information involve
715 * block bitmap and buddy information. The information are
716 * stored in the inode as
719 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
722 * one block each for bitmap and buddy information.
723 * So for each group we take up 2 blocks. A page can
724 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
725 * So it can have information regarding groups_per_page which
726 * is blocks_per_page/2
729 static int ext4_mb_init_cache(struct page *page, char *incore)
731 ext4_group_t ngroups;
737 ext4_group_t first_group;
739 struct super_block *sb;
740 struct buffer_head *bhs;
741 struct buffer_head **bh;
746 mb_debug(1, "init page %lu\n", page->index);
748 inode = page->mapping->host;
750 ngroups = ext4_get_groups_count(sb);
751 blocksize = 1 << inode->i_blkbits;
752 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
754 groups_per_page = blocks_per_page >> 1;
755 if (groups_per_page == 0)
758 /* allocate buffer_heads to read bitmaps */
759 if (groups_per_page > 1) {
761 i = sizeof(struct buffer_head *) * groups_per_page;
762 bh = kzalloc(i, GFP_NOFS);
768 first_group = page->index * blocks_per_page / 2;
770 /* read all groups the page covers into the cache */
771 for (i = 0; i < groups_per_page; i++) {
772 struct ext4_group_desc *desc;
774 if (first_group + i >= ngroups)
778 desc = ext4_get_group_desc(sb, first_group + i, NULL);
783 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
787 if (bitmap_uptodate(bh[i]))
791 if (bitmap_uptodate(bh[i])) {
792 unlock_buffer(bh[i]);
795 ext4_lock_group(sb, first_group + i);
796 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
797 ext4_init_block_bitmap(sb, bh[i],
798 first_group + i, desc);
799 set_bitmap_uptodate(bh[i]);
800 set_buffer_uptodate(bh[i]);
801 ext4_unlock_group(sb, first_group + i);
802 unlock_buffer(bh[i]);
805 ext4_unlock_group(sb, first_group + i);
806 if (buffer_uptodate(bh[i])) {
808 * if not uninit if bh is uptodate,
809 * bitmap is also uptodate
811 set_bitmap_uptodate(bh[i]);
812 unlock_buffer(bh[i]);
817 * submit the buffer_head for read. We can
818 * safely mark the bitmap as uptodate now.
819 * We do it here so the bitmap uptodate bit
820 * get set with buffer lock held.
822 set_bitmap_uptodate(bh[i]);
823 bh[i]->b_end_io = end_buffer_read_sync;
824 submit_bh(READ, bh[i]);
825 mb_debug(1, "read bitmap for group %u\n", first_group + i);
828 /* wait for I/O completion */
829 for (i = 0; i < groups_per_page && bh[i]; i++)
830 wait_on_buffer(bh[i]);
833 for (i = 0; i < groups_per_page && bh[i]; i++)
834 if (!buffer_uptodate(bh[i]))
838 first_block = page->index * blocks_per_page;
840 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
841 for (i = 0; i < blocks_per_page; i++) {
843 struct ext4_group_info *grinfo;
845 group = (first_block + i) >> 1;
846 if (group >= ngroups)
850 * data carry information regarding this
851 * particular group in the format specified
855 data = page_address(page) + (i * blocksize);
856 bitmap = bh[group - first_group]->b_data;
859 * We place the buddy block and bitmap block
862 if ((first_block + i) & 1) {
863 /* this is block of buddy */
864 BUG_ON(incore == NULL);
865 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
866 group, page->index, i * blocksize);
867 grinfo = ext4_get_group_info(sb, group);
868 grinfo->bb_fragments = 0;
869 memset(grinfo->bb_counters, 0,
870 sizeof(*grinfo->bb_counters) *
871 (sb->s_blocksize_bits+2));
873 * incore got set to the group block bitmap below
875 ext4_lock_group(sb, group);
876 ext4_mb_generate_buddy(sb, data, incore, group);
877 ext4_unlock_group(sb, group);
880 /* this is block of bitmap */
881 BUG_ON(incore != NULL);
882 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
883 group, page->index, i * blocksize);
885 /* see comments in ext4_mb_put_pa() */
886 ext4_lock_group(sb, group);
887 memcpy(data, bitmap, blocksize);
889 /* mark all preallocated blks used in in-core bitmap */
890 ext4_mb_generate_from_pa(sb, data, group);
891 ext4_mb_generate_from_freelist(sb, data, group);
892 ext4_unlock_group(sb, group);
894 /* set incore so that the buddy information can be
895 * generated using this
900 SetPageUptodate(page);
904 for (i = 0; i < groups_per_page && bh[i]; i++)
912 static noinline_for_stack
913 int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
919 int block, pnum, poff;
920 int num_grp_locked = 0;
921 struct ext4_group_info *this_grp;
922 struct ext4_sb_info *sbi = EXT4_SB(sb);
923 struct inode *inode = sbi->s_buddy_cache;
924 struct page *page = NULL, *bitmap_page = NULL;
926 mb_debug(1, "init group %u\n", group);
927 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
928 this_grp = ext4_get_group_info(sb, group);
930 * This ensures that we don't reinit the buddy cache
931 * page which map to the group from which we are already
932 * allocating. If we are looking at the buddy cache we would
933 * have taken a reference using ext4_mb_load_buddy and that
934 * would have taken the alloc_sem lock.
936 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
937 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
939 * somebody initialized the group
940 * return without doing anything
946 * the buddy cache inode stores the block bitmap
947 * and buddy information in consecutive blocks.
948 * So for each group we need two blocks.
951 pnum = block / blocks_per_page;
952 poff = block % blocks_per_page;
953 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
955 BUG_ON(page->mapping != inode->i_mapping);
956 ret = ext4_mb_init_cache(page, NULL);
963 if (page == NULL || !PageUptodate(page)) {
967 mark_page_accessed(page);
969 bitmap = page_address(page) + (poff * sb->s_blocksize);
971 /* init buddy cache */
973 pnum = block / blocks_per_page;
974 poff = block % blocks_per_page;
975 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
976 if (page == bitmap_page) {
978 * If both the bitmap and buddy are in
979 * the same page we don't need to force
984 BUG_ON(page->mapping != inode->i_mapping);
985 ret = ext4_mb_init_cache(page, bitmap);
992 if (page == NULL || !PageUptodate(page)) {
996 mark_page_accessed(page);
998 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1000 page_cache_release(bitmap_page);
1002 page_cache_release(page);
1006 static noinline_for_stack int
1007 ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1008 struct ext4_buddy *e4b)
1010 int blocks_per_page;
1016 struct ext4_group_info *grp;
1017 struct ext4_sb_info *sbi = EXT4_SB(sb);
1018 struct inode *inode = sbi->s_buddy_cache;
1020 mb_debug(1, "load group %u\n", group);
1022 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1023 grp = ext4_get_group_info(sb, group);
1025 e4b->bd_blkbits = sb->s_blocksize_bits;
1026 e4b->bd_info = ext4_get_group_info(sb, group);
1028 e4b->bd_group = group;
1029 e4b->bd_buddy_page = NULL;
1030 e4b->bd_bitmap_page = NULL;
1031 e4b->alloc_semp = &grp->alloc_sem;
1033 /* Take the read lock on the group alloc
1034 * sem. This would make sure a parallel
1035 * ext4_mb_init_group happening on other
1036 * groups mapped by the page is blocked
1037 * till we are done with allocation
1040 down_read(e4b->alloc_semp);
1042 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1043 /* we need to check for group need init flag
1044 * with alloc_semp held so that we can be sure
1045 * that new blocks didn't get added to the group
1046 * when we are loading the buddy cache
1048 up_read(e4b->alloc_semp);
1050 * we need full data about the group
1051 * to make a good selection
1053 ret = ext4_mb_init_group(sb, group);
1056 goto repeat_load_buddy;
1060 * the buddy cache inode stores the block bitmap
1061 * and buddy information in consecutive blocks.
1062 * So for each group we need two blocks.
1065 pnum = block / blocks_per_page;
1066 poff = block % blocks_per_page;
1068 /* we could use find_or_create_page(), but it locks page
1069 * what we'd like to avoid in fast path ... */
1070 page = find_get_page(inode->i_mapping, pnum);
1071 if (page == NULL || !PageUptodate(page)) {
1074 * drop the page reference and try
1075 * to get the page with lock. If we
1076 * are not uptodate that implies
1077 * somebody just created the page but
1078 * is yet to initialize the same. So
1079 * wait for it to initialize.
1081 page_cache_release(page);
1082 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1084 BUG_ON(page->mapping != inode->i_mapping);
1085 if (!PageUptodate(page)) {
1086 ret = ext4_mb_init_cache(page, NULL);
1091 mb_cmp_bitmaps(e4b, page_address(page) +
1092 (poff * sb->s_blocksize));
1097 if (page == NULL || !PageUptodate(page)) {
1101 e4b->bd_bitmap_page = page;
1102 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1103 mark_page_accessed(page);
1106 pnum = block / blocks_per_page;
1107 poff = block % blocks_per_page;
1109 page = find_get_page(inode->i_mapping, pnum);
1110 if (page == NULL || !PageUptodate(page)) {
1112 page_cache_release(page);
1113 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1115 BUG_ON(page->mapping != inode->i_mapping);
1116 if (!PageUptodate(page)) {
1117 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1126 if (page == NULL || !PageUptodate(page)) {
1130 e4b->bd_buddy_page = page;
1131 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1132 mark_page_accessed(page);
1134 BUG_ON(e4b->bd_bitmap_page == NULL);
1135 BUG_ON(e4b->bd_buddy_page == NULL);
1140 if (e4b->bd_bitmap_page)
1141 page_cache_release(e4b->bd_bitmap_page);
1142 if (e4b->bd_buddy_page)
1143 page_cache_release(e4b->bd_buddy_page);
1144 e4b->bd_buddy = NULL;
1145 e4b->bd_bitmap = NULL;
1147 /* Done with the buddy cache */
1148 up_read(e4b->alloc_semp);
1152 static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1154 if (e4b->bd_bitmap_page)
1155 page_cache_release(e4b->bd_bitmap_page);
1156 if (e4b->bd_buddy_page)
1157 page_cache_release(e4b->bd_buddy_page);
1158 /* Done with the buddy cache */
1159 if (e4b->alloc_semp)
1160 up_read(e4b->alloc_semp);
1164 static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1169 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1170 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1172 bb = EXT4_MB_BUDDY(e4b);
1173 while (order <= e4b->bd_blkbits + 1) {
1175 if (!mb_test_bit(block, bb)) {
1176 /* this block is part of buddy of order 'order' */
1179 bb += 1 << (e4b->bd_blkbits - order);
1185 static void mb_clear_bits(void *bm, int cur, int len)
1191 if ((cur & 31) == 0 && (len - cur) >= 32) {
1192 /* fast path: clear whole word at once */
1193 addr = bm + (cur >> 3);
1198 mb_clear_bit(cur, bm);
1203 static void mb_set_bits(void *bm, int cur, int len)
1209 if ((cur & 31) == 0 && (len - cur) >= 32) {
1210 /* fast path: set whole word at once */
1211 addr = bm + (cur >> 3);
1216 mb_set_bit(cur, bm);
1221 static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1222 int first, int count)
1229 struct super_block *sb = e4b->bd_sb;
1231 BUG_ON(first + count > (sb->s_blocksize << 3));
1232 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
1233 mb_check_buddy(e4b);
1234 mb_free_blocks_double(inode, e4b, first, count);
1236 e4b->bd_info->bb_free += count;
1237 if (first < e4b->bd_info->bb_first_free)
1238 e4b->bd_info->bb_first_free = first;
1240 /* let's maintain fragments counter */
1242 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1243 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1244 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1246 e4b->bd_info->bb_fragments--;
1247 else if (!block && !max)
1248 e4b->bd_info->bb_fragments++;
1250 /* let's maintain buddy itself */
1251 while (count-- > 0) {
1255 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1256 ext4_fsblk_t blocknr;
1258 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
1260 ext4_grp_locked_error(sb, e4b->bd_group,
1261 __func__, "double-free of inode"
1262 " %lu's block %llu(bit %u in group %u)",
1263 inode ? inode->i_ino : 0, blocknr, block,
1266 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1267 e4b->bd_info->bb_counters[order]++;
1269 /* start of the buddy */
1270 buddy = mb_find_buddy(e4b, order, &max);
1274 if (mb_test_bit(block, buddy) ||
1275 mb_test_bit(block + 1, buddy))
1278 /* both the buddies are free, try to coalesce them */
1279 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1285 /* for special purposes, we don't set
1286 * free bits in bitmap */
1287 mb_set_bit(block, buddy);
1288 mb_set_bit(block + 1, buddy);
1290 e4b->bd_info->bb_counters[order]--;
1291 e4b->bd_info->bb_counters[order]--;
1295 e4b->bd_info->bb_counters[order]++;
1297 mb_clear_bit(block, buddy2);
1301 mb_check_buddy(e4b);
1304 static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1305 int needed, struct ext4_free_extent *ex)
1312 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
1315 buddy = mb_find_buddy(e4b, order, &max);
1316 BUG_ON(buddy == NULL);
1317 BUG_ON(block >= max);
1318 if (mb_test_bit(block, buddy)) {
1325 /* FIXME dorp order completely ? */
1326 if (likely(order == 0)) {
1327 /* find actual order */
1328 order = mb_find_order_for_block(e4b, block);
1329 block = block >> order;
1332 ex->fe_len = 1 << order;
1333 ex->fe_start = block << order;
1334 ex->fe_group = e4b->bd_group;
1336 /* calc difference from given start */
1337 next = next - ex->fe_start;
1339 ex->fe_start += next;
1341 while (needed > ex->fe_len &&
1342 (buddy = mb_find_buddy(e4b, order, &max))) {
1344 if (block + 1 >= max)
1347 next = (block + 1) * (1 << order);
1348 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1351 ord = mb_find_order_for_block(e4b, next);
1354 block = next >> order;
1355 ex->fe_len += 1 << order;
1358 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1362 static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1368 int start = ex->fe_start;
1369 int len = ex->fe_len;
1374 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1375 BUG_ON(e4b->bd_group != ex->fe_group);
1376 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
1377 mb_check_buddy(e4b);
1378 mb_mark_used_double(e4b, start, len);
1380 e4b->bd_info->bb_free -= len;
1381 if (e4b->bd_info->bb_first_free == start)
1382 e4b->bd_info->bb_first_free += len;
1384 /* let's maintain fragments counter */
1386 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1387 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1388 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1390 e4b->bd_info->bb_fragments++;
1391 else if (!mlen && !max)
1392 e4b->bd_info->bb_fragments--;
1394 /* let's maintain buddy itself */
1396 ord = mb_find_order_for_block(e4b, start);
1398 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1399 /* the whole chunk may be allocated at once! */
1401 buddy = mb_find_buddy(e4b, ord, &max);
1402 BUG_ON((start >> ord) >= max);
1403 mb_set_bit(start >> ord, buddy);
1404 e4b->bd_info->bb_counters[ord]--;
1411 /* store for history */
1413 ret = len | (ord << 16);
1415 /* we have to split large buddy */
1417 buddy = mb_find_buddy(e4b, ord, &max);
1418 mb_set_bit(start >> ord, buddy);
1419 e4b->bd_info->bb_counters[ord]--;
1422 cur = (start >> ord) & ~1U;
1423 buddy = mb_find_buddy(e4b, ord, &max);
1424 mb_clear_bit(cur, buddy);
1425 mb_clear_bit(cur + 1, buddy);
1426 e4b->bd_info->bb_counters[ord]++;
1427 e4b->bd_info->bb_counters[ord]++;
1430 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1431 mb_check_buddy(e4b);
1437 * Must be called under group lock!
1439 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1440 struct ext4_buddy *e4b)
1442 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1445 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1446 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1448 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1449 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1450 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1452 /* preallocation can change ac_b_ex, thus we store actually
1453 * allocated blocks for history */
1454 ac->ac_f_ex = ac->ac_b_ex;
1456 ac->ac_status = AC_STATUS_FOUND;
1457 ac->ac_tail = ret & 0xffff;
1458 ac->ac_buddy = ret >> 16;
1461 * take the page reference. We want the page to be pinned
1462 * so that we don't get a ext4_mb_init_cache_call for this
1463 * group until we update the bitmap. That would mean we
1464 * double allocate blocks. The reference is dropped
1465 * in ext4_mb_release_context
1467 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1468 get_page(ac->ac_bitmap_page);
1469 ac->ac_buddy_page = e4b->bd_buddy_page;
1470 get_page(ac->ac_buddy_page);
1471 /* on allocation we use ac to track the held semaphore */
1472 ac->alloc_semp = e4b->alloc_semp;
1473 e4b->alloc_semp = NULL;
1474 /* store last allocated for subsequent stream allocation */
1475 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
1476 spin_lock(&sbi->s_md_lock);
1477 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1478 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1479 spin_unlock(&sbi->s_md_lock);
1484 * regular allocator, for general purposes allocation
1487 static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1488 struct ext4_buddy *e4b,
1491 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1492 struct ext4_free_extent *bex = &ac->ac_b_ex;
1493 struct ext4_free_extent *gex = &ac->ac_g_ex;
1494 struct ext4_free_extent ex;
1497 if (ac->ac_status == AC_STATUS_FOUND)
1500 * We don't want to scan for a whole year
1502 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1503 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1504 ac->ac_status = AC_STATUS_BREAK;
1509 * Haven't found good chunk so far, let's continue
1511 if (bex->fe_len < gex->fe_len)
1514 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1515 && bex->fe_group == e4b->bd_group) {
1516 /* recheck chunk's availability - we don't know
1517 * when it was found (within this lock-unlock
1519 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1520 if (max >= gex->fe_len) {
1521 ext4_mb_use_best_found(ac, e4b);
1528 * The routine checks whether found extent is good enough. If it is,
1529 * then the extent gets marked used and flag is set to the context
1530 * to stop scanning. Otherwise, the extent is compared with the
1531 * previous found extent and if new one is better, then it's stored
1532 * in the context. Later, the best found extent will be used, if
1533 * mballoc can't find good enough extent.
1535 * FIXME: real allocation policy is to be designed yet!
1537 static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1538 struct ext4_free_extent *ex,
1539 struct ext4_buddy *e4b)
1541 struct ext4_free_extent *bex = &ac->ac_b_ex;
1542 struct ext4_free_extent *gex = &ac->ac_g_ex;
1544 BUG_ON(ex->fe_len <= 0);
1545 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1546 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1547 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1552 * The special case - take what you catch first
1554 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1556 ext4_mb_use_best_found(ac, e4b);
1561 * Let's check whether the chuck is good enough
1563 if (ex->fe_len == gex->fe_len) {
1565 ext4_mb_use_best_found(ac, e4b);
1570 * If this is first found extent, just store it in the context
1572 if (bex->fe_len == 0) {
1578 * If new found extent is better, store it in the context
1580 if (bex->fe_len < gex->fe_len) {
1581 /* if the request isn't satisfied, any found extent
1582 * larger than previous best one is better */
1583 if (ex->fe_len > bex->fe_len)
1585 } else if (ex->fe_len > gex->fe_len) {
1586 /* if the request is satisfied, then we try to find
1587 * an extent that still satisfy the request, but is
1588 * smaller than previous one */
1589 if (ex->fe_len < bex->fe_len)
1593 ext4_mb_check_limits(ac, e4b, 0);
1596 static noinline_for_stack
1597 int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1598 struct ext4_buddy *e4b)
1600 struct ext4_free_extent ex = ac->ac_b_ex;
1601 ext4_group_t group = ex.fe_group;
1605 BUG_ON(ex.fe_len <= 0);
1606 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1610 ext4_lock_group(ac->ac_sb, group);
1611 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1615 ext4_mb_use_best_found(ac, e4b);
1618 ext4_unlock_group(ac->ac_sb, group);
1619 ext4_mb_release_desc(e4b);
1624 static noinline_for_stack
1625 int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1626 struct ext4_buddy *e4b)
1628 ext4_group_t group = ac->ac_g_ex.fe_group;
1631 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1632 struct ext4_free_extent ex;
1634 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1637 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1641 ext4_lock_group(ac->ac_sb, group);
1642 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1643 ac->ac_g_ex.fe_len, &ex);
1645 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1648 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1650 /* use do_div to get remainder (would be 64-bit modulo) */
1651 if (do_div(start, sbi->s_stripe) == 0) {
1654 ext4_mb_use_best_found(ac, e4b);
1656 } else if (max >= ac->ac_g_ex.fe_len) {
1657 BUG_ON(ex.fe_len <= 0);
1658 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1659 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1662 ext4_mb_use_best_found(ac, e4b);
1663 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1664 /* Sometimes, caller may want to merge even small
1665 * number of blocks to an existing extent */
1666 BUG_ON(ex.fe_len <= 0);
1667 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1668 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1671 ext4_mb_use_best_found(ac, e4b);
1673 ext4_unlock_group(ac->ac_sb, group);
1674 ext4_mb_release_desc(e4b);
1680 * The routine scans buddy structures (not bitmap!) from given order
1681 * to max order and tries to find big enough chunk to satisfy the req
1683 static noinline_for_stack
1684 void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1685 struct ext4_buddy *e4b)
1687 struct super_block *sb = ac->ac_sb;
1688 struct ext4_group_info *grp = e4b->bd_info;
1694 BUG_ON(ac->ac_2order <= 0);
1695 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1696 if (grp->bb_counters[i] == 0)
1699 buddy = mb_find_buddy(e4b, i, &max);
1700 BUG_ON(buddy == NULL);
1702 k = mb_find_next_zero_bit(buddy, max, 0);
1707 ac->ac_b_ex.fe_len = 1 << i;
1708 ac->ac_b_ex.fe_start = k << i;
1709 ac->ac_b_ex.fe_group = e4b->bd_group;
1711 ext4_mb_use_best_found(ac, e4b);
1713 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1715 if (EXT4_SB(sb)->s_mb_stats)
1716 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1723 * The routine scans the group and measures all found extents.
1724 * In order to optimize scanning, caller must pass number of
1725 * free blocks in the group, so the routine can know upper limit.
1727 static noinline_for_stack
1728 void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1729 struct ext4_buddy *e4b)
1731 struct super_block *sb = ac->ac_sb;
1732 void *bitmap = EXT4_MB_BITMAP(e4b);
1733 struct ext4_free_extent ex;
1737 free = e4b->bd_info->bb_free;
1740 i = e4b->bd_info->bb_first_free;
1742 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1743 i = mb_find_next_zero_bit(bitmap,
1744 EXT4_BLOCKS_PER_GROUP(sb), i);
1745 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
1747 * IF we have corrupt bitmap, we won't find any
1748 * free blocks even though group info says we
1749 * we have free blocks
1751 ext4_grp_locked_error(sb, e4b->bd_group,
1752 __func__, "%d free blocks as per "
1753 "group info. But bitmap says 0",
1758 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1759 BUG_ON(ex.fe_len <= 0);
1760 if (free < ex.fe_len) {
1761 ext4_grp_locked_error(sb, e4b->bd_group,
1762 __func__, "%d free blocks as per "
1763 "group info. But got %d blocks",
1766 * The number of free blocks differs. This mostly
1767 * indicate that the bitmap is corrupt. So exit
1768 * without claiming the space.
1773 ext4_mb_measure_extent(ac, &ex, e4b);
1779 ext4_mb_check_limits(ac, e4b, 1);
1783 * This is a special case for storages like raid5
1784 * we try to find stripe-aligned chunks for stripe-size requests
1785 * XXX should do so at least for multiples of stripe size as well
1787 static noinline_for_stack
1788 void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1789 struct ext4_buddy *e4b)
1791 struct super_block *sb = ac->ac_sb;
1792 struct ext4_sb_info *sbi = EXT4_SB(sb);
1793 void *bitmap = EXT4_MB_BITMAP(e4b);
1794 struct ext4_free_extent ex;
1795 ext4_fsblk_t first_group_block;
1800 BUG_ON(sbi->s_stripe == 0);
1802 /* find first stripe-aligned block in group */
1803 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1805 a = first_group_block + sbi->s_stripe - 1;
1806 do_div(a, sbi->s_stripe);
1807 i = (a * sbi->s_stripe) - first_group_block;
1809 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1810 if (!mb_test_bit(i, bitmap)) {
1811 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1812 if (max >= sbi->s_stripe) {
1815 ext4_mb_use_best_found(ac, e4b);
1823 static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1824 ext4_group_t group, int cr)
1826 unsigned free, fragments;
1828 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
1829 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1831 BUG_ON(cr < 0 || cr >= 4);
1832 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1834 free = grp->bb_free;
1835 fragments = grp->bb_fragments;
1843 BUG_ON(ac->ac_2order == 0);
1845 /* Avoid using the first bg of a flexgroup for data files */
1846 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1847 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1848 ((group % flex_size) == 0))
1851 bits = ac->ac_sb->s_blocksize_bits + 1;
1852 for (i = ac->ac_2order; i <= bits; i++)
1853 if (grp->bb_counters[i] > 0)
1857 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1861 if (free >= ac->ac_g_ex.fe_len)
1874 * lock the group_info alloc_sem of all the groups
1875 * belonging to the same buddy cache page. This
1876 * make sure other parallel operation on the buddy
1877 * cache doesn't happen whild holding the buddy cache
1880 int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1884 int blocks_per_page;
1885 int groups_per_page;
1886 ext4_group_t ngroups = ext4_get_groups_count(sb);
1887 ext4_group_t first_group;
1888 struct ext4_group_info *grp;
1890 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1892 * the buddy cache inode stores the block bitmap
1893 * and buddy information in consecutive blocks.
1894 * So for each group we need two blocks.
1897 pnum = block / blocks_per_page;
1898 first_group = pnum * blocks_per_page / 2;
1900 groups_per_page = blocks_per_page >> 1;
1901 if (groups_per_page == 0)
1902 groups_per_page = 1;
1903 /* read all groups the page covers into the cache */
1904 for (i = 0; i < groups_per_page; i++) {
1906 if ((first_group + i) >= ngroups)
1908 grp = ext4_get_group_info(sb, first_group + i);
1909 /* take all groups write allocation
1910 * semaphore. This make sure there is
1911 * no block allocation going on in any
1914 down_write_nested(&grp->alloc_sem, i);
1919 void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1920 ext4_group_t group, int locked_group)
1924 int blocks_per_page;
1925 ext4_group_t first_group;
1926 struct ext4_group_info *grp;
1928 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1930 * the buddy cache inode stores the block bitmap
1931 * and buddy information in consecutive blocks.
1932 * So for each group we need two blocks.
1935 pnum = block / blocks_per_page;
1936 first_group = pnum * blocks_per_page / 2;
1937 /* release locks on all the groups */
1938 for (i = 0; i < locked_group; i++) {
1940 grp = ext4_get_group_info(sb, first_group + i);
1941 /* take all groups write allocation
1942 * semaphore. This make sure there is
1943 * no block allocation going on in any
1946 up_write(&grp->alloc_sem);
1951 static noinline_for_stack int
1952 ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
1954 ext4_group_t ngroups, group, i;
1958 struct ext4_sb_info *sbi;
1959 struct super_block *sb;
1960 struct ext4_buddy e4b;
1964 ngroups = ext4_get_groups_count(sb);
1965 /* non-extent files are limited to low blocks/groups */
1966 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL))
1967 ngroups = sbi->s_blockfile_groups;
1969 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1971 /* first, try the goal */
1972 err = ext4_mb_find_by_goal(ac, &e4b);
1973 if (err || ac->ac_status == AC_STATUS_FOUND)
1976 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1980 * ac->ac2_order is set only if the fe_len is a power of 2
1981 * if ac2_order is set we also set criteria to 0 so that we
1982 * try exact allocation using buddy.
1984 i = fls(ac->ac_g_ex.fe_len);
1987 * We search using buddy data only if the order of the request
1988 * is greater than equal to the sbi_s_mb_order2_reqs
1989 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1991 if (i >= sbi->s_mb_order2_reqs) {
1993 * This should tell if fe_len is exactly power of 2
1995 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1996 ac->ac_2order = i - 1;
1999 bsbits = ac->ac_sb->s_blocksize_bits;
2001 /* if stream allocation is enabled, use global goal */
2002 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
2003 /* TBD: may be hot point */
2004 spin_lock(&sbi->s_md_lock);
2005 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2006 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2007 spin_unlock(&sbi->s_md_lock);
2010 /* Let's just scan groups to find more-less suitable blocks */
2011 cr = ac->ac_2order ? 0 : 1;
2013 * cr == 0 try to get exact allocation,
2014 * cr == 3 try to get anything
2017 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2018 ac->ac_criteria = cr;
2020 * searching for the right group start
2021 * from the goal value specified
2023 group = ac->ac_g_ex.fe_group;
2025 for (i = 0; i < ngroups; group++, i++) {
2026 struct ext4_group_info *grp;
2027 struct ext4_group_desc *desc;
2029 if (group == ngroups)
2032 /* quick check to skip empty groups */
2033 grp = ext4_get_group_info(sb, group);
2034 if (grp->bb_free == 0)
2037 err = ext4_mb_load_buddy(sb, group, &e4b);
2041 ext4_lock_group(sb, group);
2042 if (!ext4_mb_good_group(ac, group, cr)) {
2043 /* someone did allocation from this group */
2044 ext4_unlock_group(sb, group);
2045 ext4_mb_release_desc(&e4b);
2049 ac->ac_groups_scanned++;
2050 desc = ext4_get_group_desc(sb, group, NULL);
2052 ext4_mb_simple_scan_group(ac, &e4b);
2054 ac->ac_g_ex.fe_len == sbi->s_stripe)
2055 ext4_mb_scan_aligned(ac, &e4b);
2057 ext4_mb_complex_scan_group(ac, &e4b);
2059 ext4_unlock_group(sb, group);
2060 ext4_mb_release_desc(&e4b);
2062 if (ac->ac_status != AC_STATUS_CONTINUE)
2067 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2068 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2070 * We've been searching too long. Let's try to allocate
2071 * the best chunk we've found so far
2074 ext4_mb_try_best_found(ac, &e4b);
2075 if (ac->ac_status != AC_STATUS_FOUND) {
2077 * Someone more lucky has already allocated it.
2078 * The only thing we can do is just take first
2080 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2082 ac->ac_b_ex.fe_group = 0;
2083 ac->ac_b_ex.fe_start = 0;
2084 ac->ac_b_ex.fe_len = 0;
2085 ac->ac_status = AC_STATUS_CONTINUE;
2086 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2088 atomic_inc(&sbi->s_mb_lost_chunks);
2096 static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2098 struct super_block *sb = seq->private;
2101 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
2104 return (void *) ((unsigned long) group);
2107 static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2109 struct super_block *sb = seq->private;
2113 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
2116 return (void *) ((unsigned long) group);
2119 static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2121 struct super_block *sb = seq->private;
2122 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
2125 struct ext4_buddy e4b;
2127 struct ext4_group_info info;
2128 ext4_grpblk_t counters[16];
2133 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2134 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2135 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2136 "group", "free", "frags", "first",
2137 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2138 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2140 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2141 sizeof(struct ext4_group_info);
2142 err = ext4_mb_load_buddy(sb, group, &e4b);
2144 seq_printf(seq, "#%-5u: I/O error\n", group);
2147 ext4_lock_group(sb, group);
2148 memcpy(&sg, ext4_get_group_info(sb, group), i);
2149 ext4_unlock_group(sb, group);
2150 ext4_mb_release_desc(&e4b);
2152 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
2153 sg.info.bb_fragments, sg.info.bb_first_free);
2154 for (i = 0; i <= 13; i++)
2155 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2156 sg.info.bb_counters[i] : 0);
2157 seq_printf(seq, " ]\n");
2162 static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2166 static const struct seq_operations ext4_mb_seq_groups_ops = {
2167 .start = ext4_mb_seq_groups_start,
2168 .next = ext4_mb_seq_groups_next,
2169 .stop = ext4_mb_seq_groups_stop,
2170 .show = ext4_mb_seq_groups_show,
2173 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2175 struct super_block *sb = PDE(inode)->data;
2178 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2180 struct seq_file *m = (struct seq_file *)file->private_data;
2187 static const struct file_operations ext4_mb_seq_groups_fops = {
2188 .owner = THIS_MODULE,
2189 .open = ext4_mb_seq_groups_open,
2191 .llseek = seq_lseek,
2192 .release = seq_release,
2196 /* Create and initialize ext4_group_info data for the given group. */
2197 int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
2198 struct ext4_group_desc *desc)
2202 struct ext4_sb_info *sbi = EXT4_SB(sb);
2203 struct ext4_group_info **meta_group_info;
2206 * First check if this group is the first of a reserved block.
2207 * If it's true, we have to allocate a new table of pointers
2208 * to ext4_group_info structures
2210 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2211 metalen = sizeof(*meta_group_info) <<
2212 EXT4_DESC_PER_BLOCK_BITS(sb);
2213 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2214 if (meta_group_info == NULL) {
2215 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2217 goto exit_meta_group_info;
2219 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2224 * calculate needed size. if change bb_counters size,
2225 * don't forget about ext4_mb_generate_buddy()
2227 len = offsetof(typeof(**meta_group_info),
2228 bb_counters[sb->s_blocksize_bits + 2]);
2231 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2232 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2234 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2235 if (meta_group_info[i] == NULL) {
2236 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2237 goto exit_group_info;
2239 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2240 &(meta_group_info[i]->bb_state));
2243 * initialize bb_free to be able to skip
2244 * empty groups without initialization
2246 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2247 meta_group_info[i]->bb_free =
2248 ext4_free_blocks_after_init(sb, group, desc);
2250 meta_group_info[i]->bb_free =
2251 ext4_free_blks_count(sb, desc);
2254 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
2255 init_rwsem(&meta_group_info[i]->alloc_sem);
2256 meta_group_info[i]->bb_free_root = RB_ROOT;
2260 struct buffer_head *bh;
2261 meta_group_info[i]->bb_bitmap =
2262 kmalloc(sb->s_blocksize, GFP_KERNEL);
2263 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2264 bh = ext4_read_block_bitmap(sb, group);
2266 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2275 /* If a meta_group_info table has been allocated, release it now */
2276 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2277 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2278 exit_meta_group_info:
2280 } /* ext4_mb_add_groupinfo */
2282 static int ext4_mb_init_backend(struct super_block *sb)
2284 ext4_group_t ngroups = ext4_get_groups_count(sb);
2286 struct ext4_sb_info *sbi = EXT4_SB(sb);
2287 struct ext4_super_block *es = sbi->s_es;
2288 int num_meta_group_infos;
2289 int num_meta_group_infos_max;
2291 struct ext4_group_desc *desc;
2293 /* This is the number of blocks used by GDT */
2294 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
2295 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2298 * This is the total number of blocks used by GDT including
2299 * the number of reserved blocks for GDT.
2300 * The s_group_info array is allocated with this value
2301 * to allow a clean online resize without a complex
2302 * manipulation of pointer.
2303 * The drawback is the unused memory when no resize
2304 * occurs but it's very low in terms of pages
2305 * (see comments below)
2306 * Need to handle this properly when META_BG resizing is allowed
2308 num_meta_group_infos_max = num_meta_group_infos +
2309 le16_to_cpu(es->s_reserved_gdt_blocks);
2312 * array_size is the size of s_group_info array. We round it
2313 * to the next power of two because this approximation is done
2314 * internally by kmalloc so we can have some more memory
2315 * for free here (e.g. may be used for META_BG resize).
2318 while (array_size < sizeof(*sbi->s_group_info) *
2319 num_meta_group_infos_max)
2320 array_size = array_size << 1;
2321 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2322 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2323 * So a two level scheme suffices for now. */
2324 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
2325 if (sbi->s_group_info == NULL) {
2326 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2329 sbi->s_buddy_cache = new_inode(sb);
2330 if (sbi->s_buddy_cache == NULL) {
2331 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2334 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
2335 for (i = 0; i < ngroups; i++) {
2336 desc = ext4_get_group_desc(sb, i, NULL);
2339 "EXT4-fs: can't read descriptor %u\n", i);
2342 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2350 kfree(ext4_get_group_info(sb, i));
2351 i = num_meta_group_infos;
2353 kfree(sbi->s_group_info[i]);
2354 iput(sbi->s_buddy_cache);
2356 kfree(sbi->s_group_info);
2360 int ext4_mb_init(struct super_block *sb, int needs_recovery)
2362 struct ext4_sb_info *sbi = EXT4_SB(sb);
2368 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
2370 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2371 if (sbi->s_mb_offsets == NULL) {
2375 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
2376 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2377 if (sbi->s_mb_maxs == NULL) {
2378 kfree(sbi->s_mb_offsets);
2382 /* order 0 is regular bitmap */
2383 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2384 sbi->s_mb_offsets[0] = 0;
2388 max = sb->s_blocksize << 2;
2390 sbi->s_mb_offsets[i] = offset;
2391 sbi->s_mb_maxs[i] = max;
2392 offset += 1 << (sb->s_blocksize_bits - i);
2395 } while (i <= sb->s_blocksize_bits + 1);
2397 /* init file for buddy data */
2398 ret = ext4_mb_init_backend(sb);
2400 kfree(sbi->s_mb_offsets);
2401 kfree(sbi->s_mb_maxs);
2405 spin_lock_init(&sbi->s_md_lock);
2406 spin_lock_init(&sbi->s_bal_lock);
2408 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2409 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2410 sbi->s_mb_stats = MB_DEFAULT_STATS;
2411 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2412 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2413 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2415 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
2416 if (sbi->s_locality_groups == NULL) {
2417 kfree(sbi->s_mb_offsets);
2418 kfree(sbi->s_mb_maxs);
2421 for_each_possible_cpu(i) {
2422 struct ext4_locality_group *lg;
2423 lg = per_cpu_ptr(sbi->s_locality_groups, i);
2424 mutex_init(&lg->lg_mutex);
2425 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2426 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
2427 spin_lock_init(&lg->lg_prealloc_lock);
2431 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2432 &ext4_mb_seq_groups_fops, sb);
2435 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
2439 /* need to called with the ext4 group lock held */
2440 static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2442 struct ext4_prealloc_space *pa;
2443 struct list_head *cur, *tmp;
2446 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2447 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2448 list_del(&pa->pa_group_list);
2450 kmem_cache_free(ext4_pspace_cachep, pa);
2453 mb_debug(1, "mballoc: %u PAs left\n", count);
2457 int ext4_mb_release(struct super_block *sb)
2459 ext4_group_t ngroups = ext4_get_groups_count(sb);
2461 int num_meta_group_infos;
2462 struct ext4_group_info *grinfo;
2463 struct ext4_sb_info *sbi = EXT4_SB(sb);
2465 if (sbi->s_group_info) {
2466 for (i = 0; i < ngroups; i++) {
2467 grinfo = ext4_get_group_info(sb, i);
2469 kfree(grinfo->bb_bitmap);
2471 ext4_lock_group(sb, i);
2472 ext4_mb_cleanup_pa(grinfo);
2473 ext4_unlock_group(sb, i);
2476 num_meta_group_infos = (ngroups +
2477 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2478 EXT4_DESC_PER_BLOCK_BITS(sb);
2479 for (i = 0; i < num_meta_group_infos; i++)
2480 kfree(sbi->s_group_info[i]);
2481 kfree(sbi->s_group_info);
2483 kfree(sbi->s_mb_offsets);
2484 kfree(sbi->s_mb_maxs);
2485 if (sbi->s_buddy_cache)
2486 iput(sbi->s_buddy_cache);
2487 if (sbi->s_mb_stats) {
2489 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2490 atomic_read(&sbi->s_bal_allocated),
2491 atomic_read(&sbi->s_bal_reqs),
2492 atomic_read(&sbi->s_bal_success));
2494 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2495 "%u 2^N hits, %u breaks, %u lost\n",
2496 atomic_read(&sbi->s_bal_ex_scanned),
2497 atomic_read(&sbi->s_bal_goals),
2498 atomic_read(&sbi->s_bal_2orders),
2499 atomic_read(&sbi->s_bal_breaks),
2500 atomic_read(&sbi->s_mb_lost_chunks));
2502 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2503 sbi->s_mb_buddies_generated++,
2504 sbi->s_mb_generation_time);
2506 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2507 atomic_read(&sbi->s_mb_preallocated),
2508 atomic_read(&sbi->s_mb_discarded));
2511 free_percpu(sbi->s_locality_groups);
2513 remove_proc_entry("mb_groups", sbi->s_proc);
2519 * This function is called by the jbd2 layer once the commit has finished,
2520 * so we know we can free the blocks that were released with that commit.
2522 static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
2524 struct super_block *sb = journal->j_private;
2525 struct ext4_buddy e4b;
2526 struct ext4_group_info *db;
2527 int err, count = 0, count2 = 0;
2528 struct ext4_free_data *entry;
2529 struct list_head *l, *ltmp;
2531 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2532 entry = list_entry(l, struct ext4_free_data, list);
2534 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2535 entry->count, entry->group, entry);
2537 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
2538 /* we expect to find existing buddy because it's pinned */
2542 /* there are blocks to put in buddy to make them really free */
2543 count += entry->count;
2545 ext4_lock_group(sb, entry->group);
2546 /* Take it out of per group rb tree */
2547 rb_erase(&entry->node, &(db->bb_free_root));
2548 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2550 if (!db->bb_free_root.rb_node) {
2551 /* No more items in the per group rb tree
2552 * balance refcounts from ext4_mb_free_metadata()
2554 page_cache_release(e4b.bd_buddy_page);
2555 page_cache_release(e4b.bd_bitmap_page);
2557 ext4_unlock_group(sb, entry->group);
2558 if (test_opt(sb, DISCARD)) {
2559 ext4_fsblk_t discard_block;
2561 discard_block = entry->start_blk +
2562 ext4_group_first_block_no(sb, entry->group);
2563 trace_ext4_discard_blocks(sb,
2564 (unsigned long long)discard_block,
2566 sb_issue_discard(sb, discard_block, entry->count);
2568 kmem_cache_free(ext4_free_ext_cachep, entry);
2569 ext4_mb_release_desc(&e4b);
2572 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
2575 #ifdef CONFIG_EXT4_DEBUG
2576 u8 mb_enable_debug __read_mostly;
2578 static struct dentry *debugfs_dir;
2579 static struct dentry *debugfs_debug;
2581 static void __init ext4_create_debugfs_entry(void)
2583 debugfs_dir = debugfs_create_dir("ext4", NULL);
2585 debugfs_debug = debugfs_create_u8("mballoc-debug",
2591 static void ext4_remove_debugfs_entry(void)
2593 debugfs_remove(debugfs_debug);
2594 debugfs_remove(debugfs_dir);
2599 static void __init ext4_create_debugfs_entry(void)
2603 static void ext4_remove_debugfs_entry(void)
2609 int __init init_ext4_mballoc(void)
2611 ext4_pspace_cachep =
2612 kmem_cache_create("ext4_prealloc_space",
2613 sizeof(struct ext4_prealloc_space),
2614 0, SLAB_RECLAIM_ACCOUNT, NULL);
2615 if (ext4_pspace_cachep == NULL)
2619 kmem_cache_create("ext4_alloc_context",
2620 sizeof(struct ext4_allocation_context),
2621 0, SLAB_RECLAIM_ACCOUNT, NULL);
2622 if (ext4_ac_cachep == NULL) {
2623 kmem_cache_destroy(ext4_pspace_cachep);
2627 ext4_free_ext_cachep =
2628 kmem_cache_create("ext4_free_block_extents",
2629 sizeof(struct ext4_free_data),
2630 0, SLAB_RECLAIM_ACCOUNT, NULL);
2631 if (ext4_free_ext_cachep == NULL) {
2632 kmem_cache_destroy(ext4_pspace_cachep);
2633 kmem_cache_destroy(ext4_ac_cachep);
2636 ext4_create_debugfs_entry();
2640 void exit_ext4_mballoc(void)
2643 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2644 * before destroying the slab cache.
2647 kmem_cache_destroy(ext4_pspace_cachep);
2648 kmem_cache_destroy(ext4_ac_cachep);
2649 kmem_cache_destroy(ext4_free_ext_cachep);
2650 ext4_remove_debugfs_entry();
2655 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2656 * Returns 0 if success or error code
2658 static noinline_for_stack int
2659 ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
2660 handle_t *handle, unsigned int reserv_blks)
2662 struct buffer_head *bitmap_bh = NULL;
2663 struct ext4_super_block *es;
2664 struct ext4_group_desc *gdp;
2665 struct buffer_head *gdp_bh;
2666 struct ext4_sb_info *sbi;
2667 struct super_block *sb;
2671 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2672 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2680 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
2684 err = ext4_journal_get_write_access(handle, bitmap_bh);
2689 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2693 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
2694 ext4_free_blks_count(sb, gdp));
2696 err = ext4_journal_get_write_access(handle, gdp_bh);
2700 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
2702 len = ac->ac_b_ex.fe_len;
2703 if (!ext4_data_block_valid(sbi, block, len)) {
2704 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
2705 "fs metadata\n", block, block+len);
2706 /* File system mounted not to panic on error
2707 * Fix the bitmap and repeat the block allocation
2708 * We leak some of the blocks here.
2710 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2711 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2712 ac->ac_b_ex.fe_len);
2713 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
2714 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
2720 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2721 #ifdef AGGRESSIVE_CHECK
2724 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2725 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2726 bitmap_bh->b_data));
2730 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
2731 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2732 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
2733 ext4_free_blks_set(sb, gdp,
2734 ext4_free_blocks_after_init(sb,
2735 ac->ac_b_ex.fe_group, gdp));
2737 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2738 ext4_free_blks_set(sb, gdp, len);
2739 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
2741 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
2742 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
2744 * Now reduce the dirty block count also. Should not go negative
2746 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2747 /* release all the reserved blocks if non delalloc */
2748 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
2750 if (sbi->s_log_groups_per_flex) {
2751 ext4_group_t flex_group = ext4_flex_group(sbi,
2752 ac->ac_b_ex.fe_group);
2753 atomic_sub(ac->ac_b_ex.fe_len,
2754 &sbi->s_flex_groups[flex_group].free_blocks);
2757 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
2760 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
2769 * here we normalize request for locality group
2770 * Group request are normalized to s_strip size if we set the same via mount
2771 * option. If not we set it to s_mb_group_prealloc which can be configured via
2772 * /sys/fs/ext4/<partition>/mb_group_prealloc
2774 * XXX: should we try to preallocate more than the group has now?
2776 static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2778 struct super_block *sb = ac->ac_sb;
2779 struct ext4_locality_group *lg = ac->ac_lg;
2782 if (EXT4_SB(sb)->s_stripe)
2783 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2785 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
2786 mb_debug(1, "#%u: goal %u blocks for locality group\n",
2787 current->pid, ac->ac_g_ex.fe_len);
2791 * Normalization means making request better in terms of
2792 * size and alignment
2794 static noinline_for_stack void
2795 ext4_mb_normalize_request(struct ext4_allocation_context *ac,
2796 struct ext4_allocation_request *ar)
2800 loff_t size, orig_size, start_off;
2801 ext4_lblk_t start, orig_start;
2802 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
2803 struct ext4_prealloc_space *pa;
2805 /* do normalize only data requests, metadata requests
2806 do not need preallocation */
2807 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2810 /* sometime caller may want exact blocks */
2811 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2814 /* caller may indicate that preallocation isn't
2815 * required (it's a tail, for example) */
2816 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2819 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2820 ext4_mb_normalize_group_request(ac);
2824 bsbits = ac->ac_sb->s_blocksize_bits;
2826 /* first, let's learn actual file size
2827 * given current request is allocated */
2828 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2829 size = size << bsbits;
2830 if (size < i_size_read(ac->ac_inode))
2831 size = i_size_read(ac->ac_inode);
2833 /* max size of free chunks */
2836 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2837 (req <= (size) || max <= (chunk_size))
2839 /* first, try to predict filesize */
2840 /* XXX: should this table be tunable? */
2842 if (size <= 16 * 1024) {
2844 } else if (size <= 32 * 1024) {
2846 } else if (size <= 64 * 1024) {
2848 } else if (size <= 128 * 1024) {
2850 } else if (size <= 256 * 1024) {
2852 } else if (size <= 512 * 1024) {
2854 } else if (size <= 1024 * 1024) {
2856 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
2857 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2858 (21 - bsbits)) << 21;
2859 size = 2 * 1024 * 1024;
2860 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
2861 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2862 (22 - bsbits)) << 22;
2863 size = 4 * 1024 * 1024;
2864 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
2865 (8<<20)>>bsbits, max, 8 * 1024)) {
2866 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2867 (23 - bsbits)) << 23;
2868 size = 8 * 1024 * 1024;
2870 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2871 size = ac->ac_o_ex.fe_len << bsbits;
2873 orig_size = size = size >> bsbits;
2874 orig_start = start = start_off >> bsbits;
2876 /* don't cover already allocated blocks in selected range */
2877 if (ar->pleft && start <= ar->lleft) {
2878 size -= ar->lleft + 1 - start;
2879 start = ar->lleft + 1;
2881 if (ar->pright && start + size - 1 >= ar->lright)
2882 size -= start + size - ar->lright;
2886 /* check we don't cross already preallocated blocks */
2888 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
2893 spin_lock(&pa->pa_lock);
2894 if (pa->pa_deleted) {
2895 spin_unlock(&pa->pa_lock);
2899 pa_end = pa->pa_lstart + pa->pa_len;
2901 /* PA must not overlap original request */
2902 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2903 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2905 /* skip PAs this normalized request doesn't overlap with */
2906 if (pa->pa_lstart >= end || pa_end <= start) {
2907 spin_unlock(&pa->pa_lock);
2910 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2912 /* adjust start or end to be adjacent to this pa */
2913 if (pa_end <= ac->ac_o_ex.fe_logical) {
2914 BUG_ON(pa_end < start);
2916 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
2917 BUG_ON(pa->pa_lstart > end);
2918 end = pa->pa_lstart;
2920 spin_unlock(&pa->pa_lock);
2925 /* XXX: extra loop to check we really don't overlap preallocations */
2927 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
2929 spin_lock(&pa->pa_lock);
2930 if (pa->pa_deleted == 0) {
2931 pa_end = pa->pa_lstart + pa->pa_len;
2932 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2934 spin_unlock(&pa->pa_lock);
2938 if (start + size <= ac->ac_o_ex.fe_logical &&
2939 start > ac->ac_o_ex.fe_logical) {
2940 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
2941 (unsigned long) start, (unsigned long) size,
2942 (unsigned long) ac->ac_o_ex.fe_logical);
2944 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
2945 start > ac->ac_o_ex.fe_logical);
2946 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
2948 /* now prepare goal request */
2950 /* XXX: is it better to align blocks WRT to logical
2951 * placement or satisfy big request as is */
2952 ac->ac_g_ex.fe_logical = start;
2953 ac->ac_g_ex.fe_len = size;
2955 /* define goal start in order to merge */
2956 if (ar->pright && (ar->lright == (start + size))) {
2957 /* merge to the right */
2958 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
2959 &ac->ac_f_ex.fe_group,
2960 &ac->ac_f_ex.fe_start);
2961 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2963 if (ar->pleft && (ar->lleft + 1 == start)) {
2964 /* merge to the left */
2965 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
2966 &ac->ac_f_ex.fe_group,
2967 &ac->ac_f_ex.fe_start);
2968 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2971 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
2972 (unsigned) orig_size, (unsigned) start);
2975 static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
2977 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2979 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
2980 atomic_inc(&sbi->s_bal_reqs);
2981 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
2982 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
2983 atomic_inc(&sbi->s_bal_success);
2984 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
2985 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2986 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2987 atomic_inc(&sbi->s_bal_goals);
2988 if (ac->ac_found > sbi->s_mb_max_to_scan)
2989 atomic_inc(&sbi->s_bal_breaks);
2992 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
2993 trace_ext4_mballoc_alloc(ac);
2995 trace_ext4_mballoc_prealloc(ac);
2999 * Called on failure; free up any blocks from the inode PA for this
3000 * context. We don't need this for MB_GROUP_PA because we only change
3001 * pa_free in ext4_mb_release_context(), but on failure, we've already
3002 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3004 static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3006 struct ext4_prealloc_space *pa = ac->ac_pa;
3009 if (pa && pa->pa_type == MB_INODE_PA) {
3010 len = ac->ac_b_ex.fe_len;
3017 * use blocks preallocated to inode
3019 static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3020 struct ext4_prealloc_space *pa)
3026 /* found preallocated blocks, use them */
3027 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3028 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3030 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3031 &ac->ac_b_ex.fe_start);
3032 ac->ac_b_ex.fe_len = len;
3033 ac->ac_status = AC_STATUS_FOUND;
3036 BUG_ON(start < pa->pa_pstart);
3037 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3038 BUG_ON(pa->pa_free < len);
3041 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
3045 * use blocks preallocated to locality group
3047 static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3048 struct ext4_prealloc_space *pa)
3050 unsigned int len = ac->ac_o_ex.fe_len;
3052 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3053 &ac->ac_b_ex.fe_group,
3054 &ac->ac_b_ex.fe_start);
3055 ac->ac_b_ex.fe_len = len;
3056 ac->ac_status = AC_STATUS_FOUND;
3059 /* we don't correct pa_pstart or pa_plen here to avoid
3060 * possible race when the group is being loaded concurrently
3061 * instead we correct pa later, after blocks are marked
3062 * in on-disk bitmap -- see ext4_mb_release_context()
3063 * Other CPUs are prevented from allocating from this pa by lg_mutex
3065 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3069 * Return the prealloc space that have minimal distance
3070 * from the goal block. @cpa is the prealloc
3071 * space that is having currently known minimal distance
3072 * from the goal block.
3074 static struct ext4_prealloc_space *
3075 ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3076 struct ext4_prealloc_space *pa,
3077 struct ext4_prealloc_space *cpa)
3079 ext4_fsblk_t cur_distance, new_distance;
3082 atomic_inc(&pa->pa_count);
3085 cur_distance = abs(goal_block - cpa->pa_pstart);
3086 new_distance = abs(goal_block - pa->pa_pstart);
3088 if (cur_distance < new_distance)
3091 /* drop the previous reference */
3092 atomic_dec(&cpa->pa_count);
3093 atomic_inc(&pa->pa_count);
3098 * search goal blocks in preallocated space
3100 static noinline_for_stack int
3101 ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
3104 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3105 struct ext4_locality_group *lg;
3106 struct ext4_prealloc_space *pa, *cpa = NULL;
3107 ext4_fsblk_t goal_block;
3109 /* only data can be preallocated */
3110 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3113 /* first, try per-file preallocation */
3115 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
3117 /* all fields in this condition don't change,
3118 * so we can skip locking for them */
3119 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3120 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3123 /* non-extent files can't have physical blocks past 2^32 */
3124 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) &&
3125 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3128 /* found preallocated blocks, use them */
3129 spin_lock(&pa->pa_lock);
3130 if (pa->pa_deleted == 0 && pa->pa_free) {
3131 atomic_inc(&pa->pa_count);
3132 ext4_mb_use_inode_pa(ac, pa);
3133 spin_unlock(&pa->pa_lock);
3134 ac->ac_criteria = 10;
3138 spin_unlock(&pa->pa_lock);
3142 /* can we use group allocation? */
3143 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3146 /* inode may have no locality group for some reason */
3150 order = fls(ac->ac_o_ex.fe_len) - 1;
3151 if (order > PREALLOC_TB_SIZE - 1)
3152 /* The max size of hash table is PREALLOC_TB_SIZE */
3153 order = PREALLOC_TB_SIZE - 1;
3155 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
3157 * search for the prealloc space that is having
3158 * minimal distance from the goal block.
3160 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3162 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3164 spin_lock(&pa->pa_lock);
3165 if (pa->pa_deleted == 0 &&
3166 pa->pa_free >= ac->ac_o_ex.fe_len) {
3168 cpa = ext4_mb_check_group_pa(goal_block,
3171 spin_unlock(&pa->pa_lock);
3176 ext4_mb_use_group_pa(ac, cpa);
3177 ac->ac_criteria = 20;
3184 * the function goes through all block freed in the group
3185 * but not yet committed and marks them used in in-core bitmap.
3186 * buddy must be generated from this bitmap
3187 * Need to be called with the ext4 group lock held
3189 static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3193 struct ext4_group_info *grp;
3194 struct ext4_free_data *entry;
3196 grp = ext4_get_group_info(sb, group);
3197 n = rb_first(&(grp->bb_free_root));
3200 entry = rb_entry(n, struct ext4_free_data, node);
3201 mb_set_bits(bitmap, entry->start_blk, entry->count);
3208 * the function goes through all preallocation in this group and marks them
3209 * used in in-core bitmap. buddy must be generated from this bitmap
3210 * Need to be called with ext4 group lock held
3212 static noinline_for_stack
3213 void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3216 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3217 struct ext4_prealloc_space *pa;
3218 struct list_head *cur;
3219 ext4_group_t groupnr;
3220 ext4_grpblk_t start;
3221 int preallocated = 0;
3225 /* all form of preallocation discards first load group,
3226 * so the only competing code is preallocation use.
3227 * we don't need any locking here
3228 * notice we do NOT ignore preallocations with pa_deleted
3229 * otherwise we could leave used blocks available for
3230 * allocation in buddy when concurrent ext4_mb_put_pa()
3231 * is dropping preallocation
3233 list_for_each(cur, &grp->bb_prealloc_list) {
3234 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3235 spin_lock(&pa->pa_lock);
3236 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3239 spin_unlock(&pa->pa_lock);
3240 if (unlikely(len == 0))
3242 BUG_ON(groupnr != group);
3243 mb_set_bits(bitmap, start, len);
3244 preallocated += len;
3247 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
3250 static void ext4_mb_pa_callback(struct rcu_head *head)
3252 struct ext4_prealloc_space *pa;
3253 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3254 kmem_cache_free(ext4_pspace_cachep, pa);
3258 * drops a reference to preallocated space descriptor
3259 * if this was the last reference and the space is consumed
3261 static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3262 struct super_block *sb, struct ext4_prealloc_space *pa)
3265 ext4_fsblk_t grp_blk;
3267 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3270 /* in this short window concurrent discard can set pa_deleted */
3271 spin_lock(&pa->pa_lock);
3272 if (pa->pa_deleted == 1) {
3273 spin_unlock(&pa->pa_lock);
3278 spin_unlock(&pa->pa_lock);
3280 grp_blk = pa->pa_pstart;
3282 * If doing group-based preallocation, pa_pstart may be in the
3283 * next group when pa is used up
3285 if (pa->pa_type == MB_GROUP_PA)
3288 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
3293 * P1 (buddy init) P2 (regular allocation)
3294 * find block B in PA
3295 * copy on-disk bitmap to buddy
3296 * mark B in on-disk bitmap
3297 * drop PA from group
3298 * mark all PAs in buddy
3300 * thus, P1 initializes buddy with B available. to prevent this
3301 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3304 ext4_lock_group(sb, grp);
3305 list_del(&pa->pa_group_list);
3306 ext4_unlock_group(sb, grp);
3308 spin_lock(pa->pa_obj_lock);
3309 list_del_rcu(&pa->pa_inode_list);
3310 spin_unlock(pa->pa_obj_lock);
3312 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3316 * creates new preallocated space for given inode
3318 static noinline_for_stack int
3319 ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
3321 struct super_block *sb = ac->ac_sb;
3322 struct ext4_prealloc_space *pa;
3323 struct ext4_group_info *grp;
3324 struct ext4_inode_info *ei;
3326 /* preallocate only when found space is larger then requested */
3327 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3328 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3329 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3331 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3335 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3341 /* we can't allocate as much as normalizer wants.
3342 * so, found space must get proper lstart
3343 * to cover original request */
3344 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3345 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3347 /* we're limited by original request in that
3348 * logical block must be covered any way
3349 * winl is window we can move our chunk within */
3350 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3352 /* also, we should cover whole original request */
3353 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3355 /* the smallest one defines real window */
3356 win = min(winl, wins);
3358 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3359 if (offs && offs < win)
3362 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3363 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3364 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3367 /* preallocation can change ac_b_ex, thus we store actually
3368 * allocated blocks for history */
3369 ac->ac_f_ex = ac->ac_b_ex;
3371 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3372 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3373 pa->pa_len = ac->ac_b_ex.fe_len;
3374 pa->pa_free = pa->pa_len;
3375 atomic_set(&pa->pa_count, 1);
3376 spin_lock_init(&pa->pa_lock);
3377 INIT_LIST_HEAD(&pa->pa_inode_list);
3378 INIT_LIST_HEAD(&pa->pa_group_list);
3380 pa->pa_type = MB_INODE_PA;
3382 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
3383 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3384 trace_ext4_mb_new_inode_pa(ac, pa);
3386 ext4_mb_use_inode_pa(ac, pa);
3387 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3389 ei = EXT4_I(ac->ac_inode);
3390 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3392 pa->pa_obj_lock = &ei->i_prealloc_lock;
3393 pa->pa_inode = ac->ac_inode;
3395 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3396 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3397 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3399 spin_lock(pa->pa_obj_lock);
3400 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3401 spin_unlock(pa->pa_obj_lock);
3407 * creates new preallocated space for locality group inodes belongs to
3409 static noinline_for_stack int
3410 ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
3412 struct super_block *sb = ac->ac_sb;
3413 struct ext4_locality_group *lg;
3414 struct ext4_prealloc_space *pa;
3415 struct ext4_group_info *grp;
3417 /* preallocate only when found space is larger then requested */
3418 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3419 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3420 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3422 BUG_ON(ext4_pspace_cachep == NULL);
3423 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3427 /* preallocation can change ac_b_ex, thus we store actually
3428 * allocated blocks for history */
3429 ac->ac_f_ex = ac->ac_b_ex;
3431 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3432 pa->pa_lstart = pa->pa_pstart;
3433 pa->pa_len = ac->ac_b_ex.fe_len;
3434 pa->pa_free = pa->pa_len;
3435 atomic_set(&pa->pa_count, 1);
3436 spin_lock_init(&pa->pa_lock);
3437 INIT_LIST_HEAD(&pa->pa_inode_list);
3438 INIT_LIST_HEAD(&pa->pa_group_list);
3440 pa->pa_type = MB_GROUP_PA;
3442 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
3443 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3444 trace_ext4_mb_new_group_pa(ac, pa);
3446 ext4_mb_use_group_pa(ac, pa);
3447 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3449 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3453 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3454 pa->pa_inode = NULL;
3456 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3457 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3458 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3461 * We will later add the new pa to the right bucket
3462 * after updating the pa_free in ext4_mb_release_context
3467 static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3471 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3472 err = ext4_mb_new_group_pa(ac);
3474 err = ext4_mb_new_inode_pa(ac);
3479 * finds all unused blocks in on-disk bitmap, frees them in
3480 * in-core bitmap and buddy.
3481 * @pa must be unlinked from inode and group lists, so that
3482 * nobody else can find/use it.
3483 * the caller MUST hold group/inode locks.
3484 * TODO: optimize the case when there are no in-core structures yet
3486 static noinline_for_stack int
3487 ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
3488 struct ext4_prealloc_space *pa,
3489 struct ext4_allocation_context *ac)
3491 struct super_block *sb = e4b->bd_sb;
3492 struct ext4_sb_info *sbi = EXT4_SB(sb);
3497 unsigned long long grp_blk_start;
3502 BUG_ON(pa->pa_deleted == 0);
3503 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3504 grp_blk_start = pa->pa_pstart - bit;
3505 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3506 end = bit + pa->pa_len;
3510 ac->ac_inode = pa->pa_inode;
3514 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3517 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3518 start = ext4_group_first_block_no(sb, group) + bit;
3519 mb_debug(1, " free preallocated %u/%u in group %u\n",
3520 (unsigned) start, (unsigned) next - bit,
3525 ac->ac_b_ex.fe_group = group;
3526 ac->ac_b_ex.fe_start = bit;
3527 ac->ac_b_ex.fe_len = next - bit;
3528 ac->ac_b_ex.fe_logical = 0;
3529 trace_ext4_mballoc_discard(ac);
3532 trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
3534 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3537 if (free != pa->pa_free) {
3538 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
3539 pa, (unsigned long) pa->pa_lstart,
3540 (unsigned long) pa->pa_pstart,
3541 (unsigned long) pa->pa_len);
3542 ext4_grp_locked_error(sb, group,
3543 __func__, "free %u, pa_free %u",
3546 * pa is already deleted so we use the value obtained
3547 * from the bitmap and continue.
3550 atomic_add(free, &sbi->s_mb_discarded);
3555 static noinline_for_stack int
3556 ext4_mb_release_group_pa(struct ext4_buddy *e4b,
3557 struct ext4_prealloc_space *pa,
3558 struct ext4_allocation_context *ac)
3560 struct super_block *sb = e4b->bd_sb;
3564 trace_ext4_mb_release_group_pa(ac, pa);
3565 BUG_ON(pa->pa_deleted == 0);
3566 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3567 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3568 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3569 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3573 ac->ac_inode = NULL;
3574 ac->ac_b_ex.fe_group = group;
3575 ac->ac_b_ex.fe_start = bit;
3576 ac->ac_b_ex.fe_len = pa->pa_len;
3577 ac->ac_b_ex.fe_logical = 0;
3578 trace_ext4_mballoc_discard(ac);
3585 * releases all preallocations in given group
3587 * first, we need to decide discard policy:
3588 * - when do we discard
3590 * - how many do we discard
3591 * 1) how many requested
3593 static noinline_for_stack int
3594 ext4_mb_discard_group_preallocations(struct super_block *sb,
3595 ext4_group_t group, int needed)
3597 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3598 struct buffer_head *bitmap_bh = NULL;
3599 struct ext4_prealloc_space *pa, *tmp;
3600 struct ext4_allocation_context *ac;
3601 struct list_head list;
3602 struct ext4_buddy e4b;
3607 mb_debug(1, "discard preallocation for group %u\n", group);
3609 if (list_empty(&grp->bb_prealloc_list))
3612 bitmap_bh = ext4_read_block_bitmap(sb, group);
3613 if (bitmap_bh == NULL) {
3614 ext4_error(sb, "Error reading block bitmap for %u", group);
3618 err = ext4_mb_load_buddy(sb, group, &e4b);
3620 ext4_error(sb, "Error loading buddy information for %u", group);
3626 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3628 INIT_LIST_HEAD(&list);
3629 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
3633 ext4_lock_group(sb, group);
3634 list_for_each_entry_safe(pa, tmp,
3635 &grp->bb_prealloc_list, pa_group_list) {
3636 spin_lock(&pa->pa_lock);
3637 if (atomic_read(&pa->pa_count)) {
3638 spin_unlock(&pa->pa_lock);
3642 if (pa->pa_deleted) {
3643 spin_unlock(&pa->pa_lock);
3647 /* seems this one can be freed ... */
3650 /* we can trust pa_free ... */
3651 free += pa->pa_free;
3653 spin_unlock(&pa->pa_lock);
3655 list_del(&pa->pa_group_list);
3656 list_add(&pa->u.pa_tmp_list, &list);
3659 /* if we still need more blocks and some PAs were used, try again */
3660 if (free < needed && busy) {
3662 ext4_unlock_group(sb, group);
3664 * Yield the CPU here so that we don't get soft lockup
3665 * in non preempt case.
3671 /* found anything to free? */
3672 if (list_empty(&list)) {
3677 /* now free all selected PAs */
3678 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3680 /* remove from object (inode or locality group) */
3681 spin_lock(pa->pa_obj_lock);
3682 list_del_rcu(&pa->pa_inode_list);
3683 spin_unlock(pa->pa_obj_lock);
3685 if (pa->pa_type == MB_GROUP_PA)
3686 ext4_mb_release_group_pa(&e4b, pa, ac);
3688 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
3690 list_del(&pa->u.pa_tmp_list);
3691 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3695 ext4_unlock_group(sb, group);
3697 kmem_cache_free(ext4_ac_cachep, ac);
3698 ext4_mb_release_desc(&e4b);
3704 * releases all non-used preallocated blocks for given inode
3706 * It's important to discard preallocations under i_data_sem
3707 * We don't want another block to be served from the prealloc
3708 * space when we are discarding the inode prealloc space.
3710 * FIXME!! Make sure it is valid at all the call sites
3712 void ext4_discard_preallocations(struct inode *inode)
3714 struct ext4_inode_info *ei = EXT4_I(inode);
3715 struct super_block *sb = inode->i_sb;
3716 struct buffer_head *bitmap_bh = NULL;
3717 struct ext4_prealloc_space *pa, *tmp;
3718 struct ext4_allocation_context *ac;
3719 ext4_group_t group = 0;
3720 struct list_head list;
3721 struct ext4_buddy e4b;
3724 if (!S_ISREG(inode->i_mode)) {
3725 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3729 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
3730 trace_ext4_discard_preallocations(inode);
3732 INIT_LIST_HEAD(&list);
3734 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
3737 ac->ac_inode = inode;
3740 /* first, collect all pa's in the inode */
3741 spin_lock(&ei->i_prealloc_lock);
3742 while (!list_empty(&ei->i_prealloc_list)) {
3743 pa = list_entry(ei->i_prealloc_list.next,
3744 struct ext4_prealloc_space, pa_inode_list);
3745 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3746 spin_lock(&pa->pa_lock);
3747 if (atomic_read(&pa->pa_count)) {
3748 /* this shouldn't happen often - nobody should
3749 * use preallocation while we're discarding it */
3750 spin_unlock(&pa->pa_lock);
3751 spin_unlock(&ei->i_prealloc_lock);
3752 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3754 schedule_timeout_uninterruptible(HZ);
3758 if (pa->pa_deleted == 0) {
3760 spin_unlock(&pa->pa_lock);
3761 list_del_rcu(&pa->pa_inode_list);
3762 list_add(&pa->u.pa_tmp_list, &list);
3766 /* someone is deleting pa right now */
3767 spin_unlock(&pa->pa_lock);
3768 spin_unlock(&ei->i_prealloc_lock);
3770 /* we have to wait here because pa_deleted
3771 * doesn't mean pa is already unlinked from
3772 * the list. as we might be called from
3773 * ->clear_inode() the inode will get freed
3774 * and concurrent thread which is unlinking
3775 * pa from inode's list may access already
3776 * freed memory, bad-bad-bad */
3778 /* XXX: if this happens too often, we can
3779 * add a flag to force wait only in case
3780 * of ->clear_inode(), but not in case of
3781 * regular truncate */
3782 schedule_timeout_uninterruptible(HZ);
3785 spin_unlock(&ei->i_prealloc_lock);
3787 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3788 BUG_ON(pa->pa_type != MB_INODE_PA);
3789 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3791 err = ext4_mb_load_buddy(sb, group, &e4b);
3793 ext4_error(sb, "Error loading buddy information for %u",
3798 bitmap_bh = ext4_read_block_bitmap(sb, group);
3799 if (bitmap_bh == NULL) {
3800 ext4_error(sb, "Error reading block bitmap for %u",
3802 ext4_mb_release_desc(&e4b);
3806 ext4_lock_group(sb, group);
3807 list_del(&pa->pa_group_list);
3808 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
3809 ext4_unlock_group(sb, group);
3811 ext4_mb_release_desc(&e4b);
3814 list_del(&pa->u.pa_tmp_list);
3815 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3818 kmem_cache_free(ext4_ac_cachep, ac);
3822 * finds all preallocated spaces and return blocks being freed to them
3823 * if preallocated space becomes full (no block is used from the space)
3824 * then the function frees space in buddy
3825 * XXX: at the moment, truncate (which is the only way to free blocks)
3826 * discards all preallocations
3828 static void ext4_mb_return_to_preallocation(struct inode *inode,
3829 struct ext4_buddy *e4b,
3830 sector_t block, int count)
3832 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
3834 #ifdef CONFIG_EXT4_DEBUG
3835 static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3837 struct super_block *sb = ac->ac_sb;
3838 ext4_group_t ngroups, i;
3840 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3841 " Allocation context details:\n");
3842 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3843 ac->ac_status, ac->ac_flags);
3844 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3845 "best %lu/%lu/%lu@%lu cr %d\n",
3846 (unsigned long)ac->ac_o_ex.fe_group,
3847 (unsigned long)ac->ac_o_ex.fe_start,
3848 (unsigned long)ac->ac_o_ex.fe_len,
3849 (unsigned long)ac->ac_o_ex.fe_logical,
3850 (unsigned long)ac->ac_g_ex.fe_group,
3851 (unsigned long)ac->ac_g_ex.fe_start,
3852 (unsigned long)ac->ac_g_ex.fe_len,
3853 (unsigned long)ac->ac_g_ex.fe_logical,
3854 (unsigned long)ac->ac_b_ex.fe_group,
3855 (unsigned long)ac->ac_b_ex.fe_start,
3856 (unsigned long)ac->ac_b_ex.fe_len,
3857 (unsigned long)ac->ac_b_ex.fe_logical,
3858 (int)ac->ac_criteria);
3859 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3861 printk(KERN_ERR "EXT4-fs: groups: \n");
3862 ngroups = ext4_get_groups_count(sb);
3863 for (i = 0; i < ngroups; i++) {
3864 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3865 struct ext4_prealloc_space *pa;
3866 ext4_grpblk_t start;
3867 struct list_head *cur;
3868 ext4_lock_group(sb, i);
3869 list_for_each(cur, &grp->bb_prealloc_list) {
3870 pa = list_entry(cur, struct ext4_prealloc_space,
3872 spin_lock(&pa->pa_lock);
3873 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3875 spin_unlock(&pa->pa_lock);
3876 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3879 ext4_unlock_group(sb, i);
3881 if (grp->bb_free == 0)
3883 printk(KERN_ERR "%u: %d/%d \n",
3884 i, grp->bb_free, grp->bb_fragments);
3886 printk(KERN_ERR "\n");
3889 static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3896 * We use locality group preallocation for small size file. The size of the
3897 * file is determined by the current size or the resulting size after
3898 * allocation which ever is larger
3900 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
3902 static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3904 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3905 int bsbits = ac->ac_sb->s_blocksize_bits;
3908 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3911 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3914 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3915 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3918 if ((size == isize) &&
3919 !ext4_fs_is_busy(sbi) &&
3920 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3921 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3925 /* don't use group allocation for large files */
3926 size = max(size, isize);
3927 if (size > sbi->s_mb_stream_request) {
3928 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
3932 BUG_ON(ac->ac_lg != NULL);
3934 * locality group prealloc space are per cpu. The reason for having
3935 * per cpu locality group is to reduce the contention between block
3936 * request from multiple CPUs.
3938 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
3940 /* we're going to use group allocation */
3941 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3943 /* serialize all allocations in the group */
3944 mutex_lock(&ac->ac_lg->lg_mutex);
3947 static noinline_for_stack int
3948 ext4_mb_initialize_context(struct ext4_allocation_context *ac,
3949 struct ext4_allocation_request *ar)
3951 struct super_block *sb = ar->inode->i_sb;
3952 struct ext4_sb_info *sbi = EXT4_SB(sb);
3953 struct ext4_super_block *es = sbi->s_es;
3957 ext4_grpblk_t block;
3959 /* we can't allocate > group size */
3962 /* just a dirty hack to filter too big requests */
3963 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
3964 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
3966 /* start searching from the goal */
3968 if (goal < le32_to_cpu(es->s_first_data_block) ||
3969 goal >= ext4_blocks_count(es))
3970 goal = le32_to_cpu(es->s_first_data_block);
3971 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3973 /* set up allocation goals */
3974 memset(ac, 0, sizeof(struct ext4_allocation_context));
3975 ac->ac_b_ex.fe_logical = ar->logical;
3976 ac->ac_status = AC_STATUS_CONTINUE;
3978 ac->ac_inode = ar->inode;
3979 ac->ac_o_ex.fe_logical = ar->logical;
3980 ac->ac_o_ex.fe_group = group;
3981 ac->ac_o_ex.fe_start = block;
3982 ac->ac_o_ex.fe_len = len;
3983 ac->ac_g_ex.fe_logical = ar->logical;
3984 ac->ac_g_ex.fe_group = group;
3985 ac->ac_g_ex.fe_start = block;
3986 ac->ac_g_ex.fe_len = len;
3987 ac->ac_flags = ar->flags;
3989 /* we have to define context: we'll we work with a file or
3990 * locality group. this is a policy, actually */
3991 ext4_mb_group_or_file(ac);
3993 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
3994 "left: %u/%u, right %u/%u to %swritable\n",
3995 (unsigned) ar->len, (unsigned) ar->logical,
3996 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
3997 (unsigned) ar->lleft, (unsigned) ar->pleft,
3998 (unsigned) ar->lright, (unsigned) ar->pright,
3999 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4004 static noinline_for_stack void
4005 ext4_mb_discard_lg_preallocations(struct super_block *sb,
4006 struct ext4_locality_group *lg,
4007 int order, int total_entries)
4009 ext4_group_t group = 0;
4010 struct ext4_buddy e4b;
4011 struct list_head discard_list;
4012 struct ext4_prealloc_space *pa, *tmp;
4013 struct ext4_allocation_context *ac;
4015 mb_debug(1, "discard locality group preallocation\n");
4017 INIT_LIST_HEAD(&discard_list);
4018 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4022 spin_lock(&lg->lg_prealloc_lock);
4023 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4025 spin_lock(&pa->pa_lock);
4026 if (atomic_read(&pa->pa_count)) {
4028 * This is the pa that we just used
4029 * for block allocation. So don't
4032 spin_unlock(&pa->pa_lock);
4035 if (pa->pa_deleted) {
4036 spin_unlock(&pa->pa_lock);
4039 /* only lg prealloc space */
4040 BUG_ON(pa->pa_type != MB_GROUP_PA);
4042 /* seems this one can be freed ... */
4044 spin_unlock(&pa->pa_lock);
4046 list_del_rcu(&pa->pa_inode_list);
4047 list_add(&pa->u.pa_tmp_list, &discard_list);
4050 if (total_entries <= 5) {
4052 * we want to keep only 5 entries
4053 * allowing it to grow to 8. This
4054 * mak sure we don't call discard
4055 * soon for this list.
4060 spin_unlock(&lg->lg_prealloc_lock);
4062 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4064 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4065 if (ext4_mb_load_buddy(sb, group, &e4b)) {
4066 ext4_error(sb, "Error loading buddy information for %u",
4070 ext4_lock_group(sb, group);
4071 list_del(&pa->pa_group_list);
4072 ext4_mb_release_group_pa(&e4b, pa, ac);
4073 ext4_unlock_group(sb, group);
4075 ext4_mb_release_desc(&e4b);
4076 list_del(&pa->u.pa_tmp_list);
4077 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4080 kmem_cache_free(ext4_ac_cachep, ac);
4084 * We have incremented pa_count. So it cannot be freed at this
4085 * point. Also we hold lg_mutex. So no parallel allocation is
4086 * possible from this lg. That means pa_free cannot be updated.
4088 * A parallel ext4_mb_discard_group_preallocations is possible.
4089 * which can cause the lg_prealloc_list to be updated.
4092 static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4094 int order, added = 0, lg_prealloc_count = 1;
4095 struct super_block *sb = ac->ac_sb;
4096 struct ext4_locality_group *lg = ac->ac_lg;
4097 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4099 order = fls(pa->pa_free) - 1;
4100 if (order > PREALLOC_TB_SIZE - 1)
4101 /* The max size of hash table is PREALLOC_TB_SIZE */
4102 order = PREALLOC_TB_SIZE - 1;
4103 /* Add the prealloc space to lg */
4105 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4107 spin_lock(&tmp_pa->pa_lock);
4108 if (tmp_pa->pa_deleted) {
4109 spin_unlock(&tmp_pa->pa_lock);
4112 if (!added && pa->pa_free < tmp_pa->pa_free) {
4113 /* Add to the tail of the previous entry */
4114 list_add_tail_rcu(&pa->pa_inode_list,
4115 &tmp_pa->pa_inode_list);
4118 * we want to count the total
4119 * number of entries in the list
4122 spin_unlock(&tmp_pa->pa_lock);
4123 lg_prealloc_count++;
4126 list_add_tail_rcu(&pa->pa_inode_list,
4127 &lg->lg_prealloc_list[order]);
4130 /* Now trim the list to be not more than 8 elements */
4131 if (lg_prealloc_count > 8) {
4132 ext4_mb_discard_lg_preallocations(sb, lg,
4133 order, lg_prealloc_count);
4140 * release all resource we used in allocation
4142 static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4144 struct ext4_prealloc_space *pa = ac->ac_pa;
4146 if (pa->pa_type == MB_GROUP_PA) {
4147 /* see comment in ext4_mb_use_group_pa() */
4148 spin_lock(&pa->pa_lock);
4149 pa->pa_pstart += ac->ac_b_ex.fe_len;
4150 pa->pa_lstart += ac->ac_b_ex.fe_len;
4151 pa->pa_free -= ac->ac_b_ex.fe_len;
4152 pa->pa_len -= ac->ac_b_ex.fe_len;
4153 spin_unlock(&pa->pa_lock);
4157 up_read(ac->alloc_semp);
4160 * We want to add the pa to the right bucket.
4161 * Remove it from the list and while adding
4162 * make sure the list to which we are adding
4163 * doesn't grow big. We need to release
4164 * alloc_semp before calling ext4_mb_add_n_trim()
4166 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
4167 spin_lock(pa->pa_obj_lock);
4168 list_del_rcu(&pa->pa_inode_list);
4169 spin_unlock(pa->pa_obj_lock);
4170 ext4_mb_add_n_trim(ac);
4172 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4174 if (ac->ac_bitmap_page)
4175 page_cache_release(ac->ac_bitmap_page);
4176 if (ac->ac_buddy_page)
4177 page_cache_release(ac->ac_buddy_page);
4178 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4179 mutex_unlock(&ac->ac_lg->lg_mutex);
4180 ext4_mb_collect_stats(ac);
4184 static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4186 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4190 trace_ext4_mb_discard_preallocations(sb, needed);
4191 for (i = 0; i < ngroups && needed > 0; i++) {
4192 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4201 * Main entry point into mballoc to allocate blocks
4202 * it tries to use preallocation first, then falls back
4203 * to usual allocation
4205 ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4206 struct ext4_allocation_request *ar, int *errp)
4209 struct ext4_allocation_context *ac = NULL;
4210 struct ext4_sb_info *sbi;
4211 struct super_block *sb;
4212 ext4_fsblk_t block = 0;
4213 unsigned int inquota = 0;
4214 unsigned int reserv_blks = 0;
4216 sb = ar->inode->i_sb;
4219 trace_ext4_request_blocks(ar);
4222 * For delayed allocation, we could skip the ENOSPC and
4223 * EDQUOT check, as blocks and quotas have been already
4224 * reserved when data being copied into pagecache.
4226 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4227 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4229 /* Without delayed allocation we need to verify
4230 * there is enough free blocks to do block allocation
4231 * and verify allocation doesn't exceed the quota limits.
4233 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4234 /* let others to free the space */
4236 ar->len = ar->len >> 1;
4242 reserv_blks = ar->len;
4243 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
4244 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4254 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4261 *errp = ext4_mb_initialize_context(ac, ar);
4267 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4268 if (!ext4_mb_use_preallocated(ac)) {
4269 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4270 ext4_mb_normalize_request(ac, ar);
4272 /* allocate space in core */
4273 ext4_mb_regular_allocator(ac);
4275 /* as we've just preallocated more space than
4276 * user requested orinally, we store allocated
4277 * space in a special descriptor */
4278 if (ac->ac_status == AC_STATUS_FOUND &&
4279 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4280 ext4_mb_new_preallocation(ac);
4282 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
4283 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
4284 if (*errp == -EAGAIN) {
4286 * drop the reference that we took
4287 * in ext4_mb_use_best_found
4289 ext4_mb_release_context(ac);
4290 ac->ac_b_ex.fe_group = 0;
4291 ac->ac_b_ex.fe_start = 0;
4292 ac->ac_b_ex.fe_len = 0;
4293 ac->ac_status = AC_STATUS_CONTINUE;
4296 ext4_discard_allocated_blocks(ac);
4297 ac->ac_b_ex.fe_len = 0;
4299 ext4_mb_show_ac(ac);
4301 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4302 ar->len = ac->ac_b_ex.fe_len;
4305 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
4309 ac->ac_b_ex.fe_len = 0;
4311 ext4_mb_show_ac(ac);
4314 ext4_mb_release_context(ac);
4317 kmem_cache_free(ext4_ac_cachep, ac);
4319 if (inquota && ar->len < inquota)
4320 dquot_free_block(ar->inode, inquota - ar->len);
4323 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4324 /* release all the reserved blocks if non delalloc */
4325 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4329 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
4335 * We can merge two free data extents only if the physical blocks
4336 * are contiguous, AND the extents were freed by the same transaction,
4337 * AND the blocks are associated with the same group.
4339 static int can_merge(struct ext4_free_data *entry1,
4340 struct ext4_free_data *entry2)
4342 if ((entry1->t_tid == entry2->t_tid) &&
4343 (entry1->group == entry2->group) &&
4344 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4349 static noinline_for_stack int
4350 ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
4351 struct ext4_free_data *new_entry)
4353 ext4_grpblk_t block;
4354 struct ext4_free_data *entry;
4355 struct ext4_group_info *db = e4b->bd_info;
4356 struct super_block *sb = e4b->bd_sb;
4357 struct ext4_sb_info *sbi = EXT4_SB(sb);
4358 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4359 struct rb_node *parent = NULL, *new_node;
4361 BUG_ON(!ext4_handle_valid(handle));
4362 BUG_ON(e4b->bd_bitmap_page == NULL);
4363 BUG_ON(e4b->bd_buddy_page == NULL);
4365 new_node = &new_entry->node;
4366 block = new_entry->start_blk;
4369 /* first free block exent. We need to
4370 protect buddy cache from being freed,
4371 * otherwise we'll refresh it from
4372 * on-disk bitmap and lose not-yet-available
4374 page_cache_get(e4b->bd_buddy_page);
4375 page_cache_get(e4b->bd_bitmap_page);
4379 entry = rb_entry(parent, struct ext4_free_data, node);
4380 if (block < entry->start_blk)
4382 else if (block >= (entry->start_blk + entry->count))
4383 n = &(*n)->rb_right;
4385 ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4386 "Double free of blocks %d (%d %d)",
4387 block, entry->start_blk, entry->count);
4392 rb_link_node(new_node, parent, n);
4393 rb_insert_color(new_node, &db->bb_free_root);
4395 /* Now try to see the extent can be merged to left and right */
4396 node = rb_prev(new_node);
4398 entry = rb_entry(node, struct ext4_free_data, node);
4399 if (can_merge(entry, new_entry)) {
4400 new_entry->start_blk = entry->start_blk;
4401 new_entry->count += entry->count;
4402 rb_erase(node, &(db->bb_free_root));
4403 spin_lock(&sbi->s_md_lock);
4404 list_del(&entry->list);
4405 spin_unlock(&sbi->s_md_lock);
4406 kmem_cache_free(ext4_free_ext_cachep, entry);
4410 node = rb_next(new_node);
4412 entry = rb_entry(node, struct ext4_free_data, node);
4413 if (can_merge(new_entry, entry)) {
4414 new_entry->count += entry->count;
4415 rb_erase(node, &(db->bb_free_root));
4416 spin_lock(&sbi->s_md_lock);
4417 list_del(&entry->list);
4418 spin_unlock(&sbi->s_md_lock);
4419 kmem_cache_free(ext4_free_ext_cachep, entry);
4422 /* Add the extent to transaction's private list */
4423 spin_lock(&sbi->s_md_lock);
4424 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
4425 spin_unlock(&sbi->s_md_lock);
4430 * ext4_free_blocks() -- Free given blocks and update quota
4431 * @handle: handle for this transaction
4433 * @block: start physical block to free
4434 * @count: number of blocks to count
4435 * @metadata: Are these metadata blocks
4437 void ext4_free_blocks(handle_t *handle, struct inode *inode,
4438 struct buffer_head *bh, ext4_fsblk_t block,
4439 unsigned long count, int flags)
4441 struct buffer_head *bitmap_bh = NULL;
4442 struct super_block *sb = inode->i_sb;
4443 struct ext4_allocation_context *ac = NULL;
4444 struct ext4_group_desc *gdp;
4445 struct ext4_super_block *es;
4446 unsigned long freed = 0;
4447 unsigned int overflow;
4449 struct buffer_head *gd_bh;
4450 ext4_group_t block_group;
4451 struct ext4_sb_info *sbi;
4452 struct ext4_buddy e4b;
4458 BUG_ON(block != bh->b_blocknr);
4460 block = bh->b_blocknr;
4464 es = EXT4_SB(sb)->s_es;
4465 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4466 !ext4_data_block_valid(sbi, block, count)) {
4467 ext4_error(sb, "Freeing blocks not in datazone - "
4468 "block = %llu, count = %lu", block, count);
4472 ext4_debug("freeing block %llu\n", block);
4473 trace_ext4_free_blocks(inode, block, count, flags);
4475 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4476 struct buffer_head *tbh = bh;
4479 BUG_ON(bh && (count > 1));
4481 for (i = 0; i < count; i++) {
4483 tbh = sb_find_get_block(inode->i_sb,
4485 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4486 inode, tbh, block + i);
4491 * We need to make sure we don't reuse the freed block until
4492 * after the transaction is committed, which we can do by
4493 * treating the block as metadata, below. We make an
4494 * exception if the inode is to be written in writeback mode
4495 * since writeback mode has weak data consistency guarantees.
4497 if (!ext4_should_writeback_data(inode))
4498 flags |= EXT4_FREE_BLOCKS_METADATA;
4500 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4502 ac->ac_inode = inode;
4508 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4511 * Check to see if we are freeing blocks across a group
4514 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4515 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4518 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
4523 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
4529 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4530 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4531 in_range(block, ext4_inode_table(sb, gdp),
4532 EXT4_SB(sb)->s_itb_per_group) ||
4533 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4534 EXT4_SB(sb)->s_itb_per_group)) {
4536 ext4_error(sb, "Freeing blocks in system zone - "
4537 "Block = %llu, count = %lu", block, count);
4538 /* err = 0. ext4_std_error should be a no op */
4542 BUFFER_TRACE(bitmap_bh, "getting write access");
4543 err = ext4_journal_get_write_access(handle, bitmap_bh);
4548 * We are about to modify some metadata. Call the journal APIs
4549 * to unshare ->b_data if a currently-committing transaction is
4552 BUFFER_TRACE(gd_bh, "get_write_access");
4553 err = ext4_journal_get_write_access(handle, gd_bh);
4556 #ifdef AGGRESSIVE_CHECK
4559 for (i = 0; i < count; i++)
4560 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4564 ac->ac_b_ex.fe_group = block_group;
4565 ac->ac_b_ex.fe_start = bit;
4566 ac->ac_b_ex.fe_len = count;
4567 trace_ext4_mballoc_free(ac);
4570 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4574 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
4575 struct ext4_free_data *new_entry;
4577 * blocks being freed are metadata. these blocks shouldn't
4578 * be used until this transaction is committed
4580 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4581 new_entry->start_blk = bit;
4582 new_entry->group = block_group;
4583 new_entry->count = count;
4584 new_entry->t_tid = handle->h_transaction->t_tid;
4586 ext4_lock_group(sb, block_group);
4587 mb_clear_bits(bitmap_bh->b_data, bit, count);
4588 ext4_mb_free_metadata(handle, &e4b, new_entry);
4590 /* need to update group_info->bb_free and bitmap
4591 * with group lock held. generate_buddy look at
4592 * them with group lock_held
4594 ext4_lock_group(sb, block_group);
4595 mb_clear_bits(bitmap_bh->b_data, bit, count);
4596 mb_free_blocks(inode, &e4b, bit, count);
4597 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4600 ret = ext4_free_blks_count(sb, gdp) + count;
4601 ext4_free_blks_set(sb, gdp, ret);
4602 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4603 ext4_unlock_group(sb, block_group);
4604 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4606 if (sbi->s_log_groups_per_flex) {
4607 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4608 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
4611 ext4_mb_release_desc(&e4b);
4615 /* We dirtied the bitmap block */
4616 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4617 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4619 /* And the group descriptor block */
4620 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4621 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4625 if (overflow && !err) {
4634 dquot_free_block(inode, freed);
4636 ext4_std_error(sb, err);
4638 kmem_cache_free(ext4_ac_cachep, ac);