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 <trace/events/ext4.h>
29 * - test ext4_ext_search_left() and ext4_ext_search_right()
30 * - search for metadata in few groups
33 * - normalization should take into account whether file is still open
34 * - discard preallocations if no free space left (policy?)
35 * - don't normalize tails
37 * - reservation for superuser
40 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
41 * - track min/max extents in each group for better group selection
42 * - mb_mark_used() may allocate chunk right after splitting buddy
43 * - tree of groups sorted by number of free blocks
48 * The allocation request involve request for multiple number of blocks
49 * near to the goal(block) value specified.
51 * During initialization phase of the allocator we decide to use the
52 * group preallocation or inode preallocation depending on the size of
53 * the file. The size of the file could be the resulting file size we
54 * would have after allocation, or the current file size, which ever
55 * is larger. If the size is less than sbi->s_mb_stream_request we
56 * select to use the group preallocation. The default value of
57 * s_mb_stream_request is 16 blocks. This can also be tuned via
58 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
59 * terms of number of blocks.
61 * The main motivation for having small file use group preallocation is to
62 * ensure that we have small files closer together on the disk.
64 * First stage the allocator looks at the inode prealloc list,
65 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
66 * spaces for this particular inode. The inode prealloc space is
69 * pa_lstart -> the logical start block for this prealloc space
70 * pa_pstart -> the physical start block for this prealloc space
71 * pa_len -> lenght for this prealloc space
72 * pa_free -> free space available in this prealloc space
74 * The inode preallocation space is used looking at the _logical_ start
75 * block. If only the logical file block falls within the range of prealloc
76 * space we will consume the particular prealloc space. This make sure that
77 * that the we have contiguous physical blocks representing the file blocks
79 * The important thing to be noted in case of inode prealloc space is that
80 * we don't modify the values associated to inode prealloc space except
83 * If we are not able to find blocks in the inode prealloc space and if we
84 * have the group allocation flag set then we look at the locality group
85 * prealloc space. These are per CPU prealloc list repreasented as
87 * ext4_sb_info.s_locality_groups[smp_processor_id()]
89 * The reason for having a per cpu locality group is to reduce the contention
90 * between CPUs. It is possible to get scheduled at this point.
92 * The locality group prealloc space is used looking at whether we have
93 * enough free space (pa_free) withing the prealloc space.
95 * If we can't allocate blocks via inode prealloc or/and locality group
96 * prealloc then we look at the buddy cache. The buddy cache is represented
97 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
98 * mapped to the buddy and bitmap information regarding different
99 * groups. The buddy information is attached to buddy cache inode so that
100 * we can access them through the page cache. The information regarding
101 * each group is loaded via ext4_mb_load_buddy. The information involve
102 * block bitmap and buddy information. The information are stored in the
106 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
109 * one block each for bitmap and buddy information. So for each group we
110 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
111 * blocksize) blocks. So it can have information regarding groups_per_page
112 * which is blocks_per_page/2
114 * The buddy cache inode is not stored on disk. The inode is thrown
115 * away when the filesystem is unmounted.
117 * We look for count number of blocks in the buddy cache. If we were able
118 * to locate that many free blocks we return with additional information
119 * regarding rest of the contiguous physical block available
121 * Before allocating blocks via buddy cache we normalize the request
122 * blocks. This ensure we ask for more blocks that we needed. The extra
123 * blocks that we get after allocation is added to the respective prealloc
124 * list. In case of inode preallocation we follow a list of heuristics
125 * based on file size. This can be found in ext4_mb_normalize_request. If
126 * we are doing a group prealloc we try to normalize the request to
127 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
128 * 512 blocks. This can be tuned via
129 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
130 * terms of number of blocks. If we have mounted the file system with -O
131 * stripe=<value> option the group prealloc request is normalized to the
132 * stripe value (sbi->s_stripe)
134 * The regular allocator(using the buddy cache) supports few tunables.
136 * /sys/fs/ext4/<partition>/mb_min_to_scan
137 * /sys/fs/ext4/<partition>/mb_max_to_scan
138 * /sys/fs/ext4/<partition>/mb_order2_req
140 * The regular allocator uses buddy scan only if the request len is power of
141 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
142 * value of s_mb_order2_reqs can be tuned via
143 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
144 * stripe size (sbi->s_stripe), we try to search for contigous block in
145 * stripe size. This should result in better allocation on RAID setups. If
146 * not, we search in the specific group using bitmap for best extents. The
147 * tunable min_to_scan and max_to_scan control the behaviour here.
148 * min_to_scan indicate how long the mballoc __must__ look for a best
149 * extent and max_to_scan indicates how long the mballoc __can__ look for a
150 * best extent in the found extents. Searching for the blocks starts with
151 * the group specified as the goal value in allocation context via
152 * ac_g_ex. Each group is first checked based on the criteria whether it
153 * can used for allocation. ext4_mb_good_group explains how the groups are
156 * Both the prealloc space are getting populated as above. So for the first
157 * request we will hit the buddy cache which will result in this prealloc
158 * space getting filled. The prealloc space is then later used for the
159 * subsequent request.
163 * mballoc operates on the following data:
165 * - in-core buddy (actually includes buddy and bitmap)
166 * - preallocation descriptors (PAs)
168 * there are two types of preallocations:
170 * assiged to specific inode and can be used for this inode only.
171 * it describes part of inode's space preallocated to specific
172 * physical blocks. any block from that preallocated can be used
173 * independent. the descriptor just tracks number of blocks left
174 * unused. so, before taking some block from descriptor, one must
175 * make sure corresponded logical block isn't allocated yet. this
176 * also means that freeing any block within descriptor's range
177 * must discard all preallocated blocks.
179 * assigned to specific locality group which does not translate to
180 * permanent set of inodes: inode can join and leave group. space
181 * from this type of preallocation can be used for any inode. thus
182 * it's consumed from the beginning to the end.
184 * relation between them can be expressed as:
185 * in-core buddy = on-disk bitmap + preallocation descriptors
187 * this mean blocks mballoc considers used are:
188 * - allocated blocks (persistent)
189 * - preallocated blocks (non-persistent)
191 * consistency in mballoc world means that at any time a block is either
192 * free or used in ALL structures. notice: "any time" should not be read
193 * literally -- time is discrete and delimited by locks.
195 * to keep it simple, we don't use block numbers, instead we count number of
196 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
198 * all operations can be expressed as:
199 * - init buddy: buddy = on-disk + PAs
200 * - new PA: buddy += N; PA = N
201 * - use inode PA: on-disk += N; PA -= N
202 * - discard inode PA buddy -= on-disk - PA; PA = 0
203 * - use locality group PA on-disk += N; PA -= N
204 * - discard locality group PA buddy -= PA; PA = 0
205 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
206 * is used in real operation because we can't know actual used
207 * bits from PA, only from on-disk bitmap
209 * if we follow this strict logic, then all operations above should be atomic.
210 * given some of them can block, we'd have to use something like semaphores
211 * killing performance on high-end SMP hardware. let's try to relax it using
212 * the following knowledge:
213 * 1) if buddy is referenced, it's already initialized
214 * 2) while block is used in buddy and the buddy is referenced,
215 * nobody can re-allocate that block
216 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
217 * bit set and PA claims same block, it's OK. IOW, one can set bit in
218 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * so, now we're building a concurrency table:
224 * blocks for PA are allocated in the buddy, buddy must be referenced
225 * until PA is linked to allocation group to avoid concurrent buddy init
227 * we need to make sure that either on-disk bitmap or PA has uptodate data
228 * given (3) we care that PA-=N operation doesn't interfere with init
230 * the simplest way would be to have buddy initialized by the discard
231 * - use locality group PA
232 * again PA-=N must be serialized with init
233 * - discard locality group PA
234 * the simplest way would be to have buddy initialized by the discard
237 * i_data_sem serializes them
239 * discard process must wait until PA isn't used by another process
240 * - use locality group PA
241 * some mutex should serialize them
242 * - discard locality group PA
243 * discard process must wait until PA isn't used by another process
246 * i_data_sem or another mutex should serializes them
248 * discard process must wait until PA isn't used by another process
249 * - use locality group PA
250 * nothing wrong here -- they're different PAs covering different blocks
251 * - discard locality group PA
252 * discard process must wait until PA isn't used by another process
254 * now we're ready to make few consequences:
255 * - PA is referenced and while it is no discard is possible
256 * - PA is referenced until block isn't marked in on-disk bitmap
257 * - PA changes only after on-disk bitmap
258 * - discard must not compete with init. either init is done before
259 * any discard or they're serialized somehow
260 * - buddy init as sum of on-disk bitmap and PAs is done atomically
262 * a special case when we've used PA to emptiness. no need to modify buddy
263 * in this case, but we should care about concurrent init
268 * Logic in few words:
273 * mark bits in on-disk bitmap
276 * - use preallocation:
277 * find proper PA (per-inode or group)
279 * mark bits in on-disk bitmap
285 * mark bits in on-disk bitmap
288 * - discard preallocations in group:
290 * move them onto local list
291 * load on-disk bitmap
293 * remove PA from object (inode or locality group)
294 * mark free blocks in-core
296 * - discard inode's preallocations:
303 * - bitlock on a group (group)
304 * - object (inode/locality) (object)
315 * - release consumed pa:
320 * - generate in-core bitmap:
324 * - discard all for given object (inode, locality group):
329 * - discard all for given group:
336 static struct kmem_cache
*ext4_pspace_cachep
;
337 static struct kmem_cache
*ext4_ac_cachep
;
338 static struct kmem_cache
*ext4_free_ext_cachep
;
339 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
341 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
343 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
345 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
347 #if BITS_PER_LONG == 64
348 *bit
+= ((unsigned long) addr
& 7UL) << 3;
349 addr
= (void *) ((unsigned long) addr
& ~7UL);
350 #elif BITS_PER_LONG == 32
351 *bit
+= ((unsigned long) addr
& 3UL) << 3;
352 addr
= (void *) ((unsigned long) addr
& ~3UL);
354 #error "how many bits you are?!"
359 static inline int mb_test_bit(int bit
, void *addr
)
362 * ext4_test_bit on architecture like powerpc
363 * needs unsigned long aligned address
365 addr
= mb_correct_addr_and_bit(&bit
, addr
);
366 return ext4_test_bit(bit
, addr
);
369 static inline void mb_set_bit(int bit
, void *addr
)
371 addr
= mb_correct_addr_and_bit(&bit
, addr
);
372 ext4_set_bit(bit
, addr
);
375 static inline void mb_clear_bit(int bit
, void *addr
)
377 addr
= mb_correct_addr_and_bit(&bit
, addr
);
378 ext4_clear_bit(bit
, addr
);
381 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
383 int fix
= 0, ret
, tmpmax
;
384 addr
= mb_correct_addr_and_bit(&fix
, addr
);
388 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
394 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
396 int fix
= 0, ret
, tmpmax
;
397 addr
= mb_correct_addr_and_bit(&fix
, addr
);
401 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
407 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
411 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
414 if (order
> e4b
->bd_blkbits
+ 1) {
419 /* at order 0 we see each particular block */
420 *max
= 1 << (e4b
->bd_blkbits
+ 3);
422 return EXT4_MB_BITMAP(e4b
);
424 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
425 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
431 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
432 int first
, int count
)
435 struct super_block
*sb
= e4b
->bd_sb
;
437 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
439 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
440 for (i
= 0; i
< count
; i
++) {
441 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
442 ext4_fsblk_t blocknr
;
443 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
444 blocknr
+= first
+ i
;
446 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
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
, unsigned first
, int len
,
626 struct ext4_group_info
*grp
)
628 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
631 unsigned short chunk
;
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 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
666 unsigned short i
= 0;
667 unsigned short first
;
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("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("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("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(unsigned short)*(sb
->s_blocksize_bits
+2));
872 * incore got set to the group block bitmap below
874 ext4_lock_group(sb
, group
);
875 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
876 ext4_unlock_group(sb
, group
);
879 /* this is block of bitmap */
880 BUG_ON(incore
!= NULL
);
881 mb_debug("put bitmap for group %u in page %lu/%x\n",
882 group
, page
->index
, i
* blocksize
);
884 /* see comments in ext4_mb_put_pa() */
885 ext4_lock_group(sb
, group
);
886 memcpy(data
, bitmap
, blocksize
);
888 /* mark all preallocated blks used in in-core bitmap */
889 ext4_mb_generate_from_pa(sb
, data
, group
);
890 ext4_mb_generate_from_freelist(sb
, data
, group
);
891 ext4_unlock_group(sb
, group
);
893 /* set incore so that the buddy information can be
894 * generated using this
899 SetPageUptodate(page
);
903 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
911 static noinline_for_stack
912 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
918 int block
, pnum
, poff
;
919 int num_grp_locked
= 0;
920 struct ext4_group_info
*this_grp
;
921 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
922 struct inode
*inode
= sbi
->s_buddy_cache
;
923 struct page
*page
= NULL
, *bitmap_page
= NULL
;
925 mb_debug("init group %lu\n", group
);
926 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
927 this_grp
= ext4_get_group_info(sb
, group
);
929 * This ensures we don't add group
930 * to this buddy cache via resize
932 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
933 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
935 * somebody initialized the group
936 * return without doing anything
942 * the buddy cache inode stores the block bitmap
943 * and buddy information in consecutive blocks.
944 * So for each group we need two blocks.
947 pnum
= block
/ blocks_per_page
;
948 poff
= block
% blocks_per_page
;
949 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
951 BUG_ON(page
->mapping
!= inode
->i_mapping
);
952 ret
= ext4_mb_init_cache(page
, NULL
);
959 if (page
== NULL
|| !PageUptodate(page
)) {
963 mark_page_accessed(page
);
965 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
967 /* init buddy cache */
969 pnum
= block
/ blocks_per_page
;
970 poff
= block
% blocks_per_page
;
971 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
972 if (page
== bitmap_page
) {
974 * If both the bitmap and buddy are in
975 * the same page we don't need to force
980 BUG_ON(page
->mapping
!= inode
->i_mapping
);
981 ret
= ext4_mb_init_cache(page
, bitmap
);
988 if (page
== NULL
|| !PageUptodate(page
)) {
992 mark_page_accessed(page
);
994 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
996 page_cache_release(bitmap_page
);
998 page_cache_release(page
);
1002 static noinline_for_stack
int
1003 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1004 struct ext4_buddy
*e4b
)
1006 int blocks_per_page
;
1012 struct ext4_group_info
*grp
;
1013 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1014 struct inode
*inode
= sbi
->s_buddy_cache
;
1016 mb_debug("load group %u\n", group
);
1018 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1019 grp
= ext4_get_group_info(sb
, group
);
1021 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1022 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
1024 e4b
->bd_group
= group
;
1025 e4b
->bd_buddy_page
= NULL
;
1026 e4b
->bd_bitmap_page
= NULL
;
1027 e4b
->alloc_semp
= &grp
->alloc_sem
;
1029 /* Take the read lock on the group alloc
1030 * sem. This would make sure a parallel
1031 * ext4_mb_init_group happening on other
1032 * groups mapped by the page is blocked
1033 * till we are done with allocation
1036 down_read(e4b
->alloc_semp
);
1038 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1039 /* we need to check for group need init flag
1040 * with alloc_semp held so that we can be sure
1041 * that new blocks didn't get added to the group
1042 * when we are loading the buddy cache
1044 up_read(e4b
->alloc_semp
);
1046 * we need full data about the group
1047 * to make a good selection
1049 ret
= ext4_mb_init_group(sb
, group
);
1052 goto repeat_load_buddy
;
1056 * the buddy cache inode stores the block bitmap
1057 * and buddy information in consecutive blocks.
1058 * So for each group we need two blocks.
1061 pnum
= block
/ blocks_per_page
;
1062 poff
= block
% blocks_per_page
;
1064 /* we could use find_or_create_page(), but it locks page
1065 * what we'd like to avoid in fast path ... */
1066 page
= find_get_page(inode
->i_mapping
, pnum
);
1067 if (page
== NULL
|| !PageUptodate(page
)) {
1070 * drop the page reference and try
1071 * to get the page with lock. If we
1072 * are not uptodate that implies
1073 * somebody just created the page but
1074 * is yet to initialize the same. So
1075 * wait for it to initialize.
1077 page_cache_release(page
);
1078 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1080 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1081 if (!PageUptodate(page
)) {
1082 ret
= ext4_mb_init_cache(page
, NULL
);
1087 mb_cmp_bitmaps(e4b
, page_address(page
) +
1088 (poff
* sb
->s_blocksize
));
1093 if (page
== NULL
|| !PageUptodate(page
)) {
1097 e4b
->bd_bitmap_page
= page
;
1098 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1099 mark_page_accessed(page
);
1102 pnum
= block
/ blocks_per_page
;
1103 poff
= block
% blocks_per_page
;
1105 page
= find_get_page(inode
->i_mapping
, pnum
);
1106 if (page
== NULL
|| !PageUptodate(page
)) {
1108 page_cache_release(page
);
1109 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1111 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1112 if (!PageUptodate(page
)) {
1113 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1122 if (page
== NULL
|| !PageUptodate(page
)) {
1126 e4b
->bd_buddy_page
= page
;
1127 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1128 mark_page_accessed(page
);
1130 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1131 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1136 if (e4b
->bd_bitmap_page
)
1137 page_cache_release(e4b
->bd_bitmap_page
);
1138 if (e4b
->bd_buddy_page
)
1139 page_cache_release(e4b
->bd_buddy_page
);
1140 e4b
->bd_buddy
= NULL
;
1141 e4b
->bd_bitmap
= NULL
;
1143 /* Done with the buddy cache */
1144 up_read(e4b
->alloc_semp
);
1148 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1150 if (e4b
->bd_bitmap_page
)
1151 page_cache_release(e4b
->bd_bitmap_page
);
1152 if (e4b
->bd_buddy_page
)
1153 page_cache_release(e4b
->bd_buddy_page
);
1154 /* Done with the buddy cache */
1155 if (e4b
->alloc_semp
)
1156 up_read(e4b
->alloc_semp
);
1160 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1165 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1166 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1168 bb
= EXT4_MB_BUDDY(e4b
);
1169 while (order
<= e4b
->bd_blkbits
+ 1) {
1171 if (!mb_test_bit(block
, bb
)) {
1172 /* this block is part of buddy of order 'order' */
1175 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1181 static void mb_clear_bits(void *bm
, int cur
, int len
)
1187 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1188 /* fast path: clear whole word at once */
1189 addr
= bm
+ (cur
>> 3);
1194 mb_clear_bit(cur
, bm
);
1199 static void mb_set_bits(void *bm
, int cur
, int len
)
1205 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1206 /* fast path: set whole word at once */
1207 addr
= bm
+ (cur
>> 3);
1212 mb_set_bit(cur
, bm
);
1217 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1218 int first
, int count
)
1225 struct super_block
*sb
= e4b
->bd_sb
;
1227 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1228 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1229 mb_check_buddy(e4b
);
1230 mb_free_blocks_double(inode
, e4b
, first
, count
);
1232 e4b
->bd_info
->bb_free
+= count
;
1233 if (first
< e4b
->bd_info
->bb_first_free
)
1234 e4b
->bd_info
->bb_first_free
= first
;
1236 /* let's maintain fragments counter */
1238 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1239 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1240 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1242 e4b
->bd_info
->bb_fragments
--;
1243 else if (!block
&& !max
)
1244 e4b
->bd_info
->bb_fragments
++;
1246 /* let's maintain buddy itself */
1247 while (count
-- > 0) {
1251 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1252 ext4_fsblk_t blocknr
;
1253 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1256 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1257 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1258 __func__
, "double-free of inode"
1259 " %lu's block %llu(bit %u in group %u)",
1260 inode
? inode
->i_ino
: 0, blocknr
, block
,
1263 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1264 e4b
->bd_info
->bb_counters
[order
]++;
1266 /* start of the buddy */
1267 buddy
= mb_find_buddy(e4b
, order
, &max
);
1271 if (mb_test_bit(block
, buddy
) ||
1272 mb_test_bit(block
+ 1, buddy
))
1275 /* both the buddies are free, try to coalesce them */
1276 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1282 /* for special purposes, we don't set
1283 * free bits in bitmap */
1284 mb_set_bit(block
, buddy
);
1285 mb_set_bit(block
+ 1, buddy
);
1287 e4b
->bd_info
->bb_counters
[order
]--;
1288 e4b
->bd_info
->bb_counters
[order
]--;
1292 e4b
->bd_info
->bb_counters
[order
]++;
1294 mb_clear_bit(block
, buddy2
);
1298 mb_check_buddy(e4b
);
1301 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1302 int needed
, struct ext4_free_extent
*ex
)
1309 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1312 buddy
= mb_find_buddy(e4b
, order
, &max
);
1313 BUG_ON(buddy
== NULL
);
1314 BUG_ON(block
>= max
);
1315 if (mb_test_bit(block
, buddy
)) {
1322 /* FIXME dorp order completely ? */
1323 if (likely(order
== 0)) {
1324 /* find actual order */
1325 order
= mb_find_order_for_block(e4b
, block
);
1326 block
= block
>> order
;
1329 ex
->fe_len
= 1 << order
;
1330 ex
->fe_start
= block
<< order
;
1331 ex
->fe_group
= e4b
->bd_group
;
1333 /* calc difference from given start */
1334 next
= next
- ex
->fe_start
;
1336 ex
->fe_start
+= next
;
1338 while (needed
> ex
->fe_len
&&
1339 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1341 if (block
+ 1 >= max
)
1344 next
= (block
+ 1) * (1 << order
);
1345 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1348 ord
= mb_find_order_for_block(e4b
, next
);
1351 block
= next
>> order
;
1352 ex
->fe_len
+= 1 << order
;
1355 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1359 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1365 int start
= ex
->fe_start
;
1366 int len
= ex
->fe_len
;
1371 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1372 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1373 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1374 mb_check_buddy(e4b
);
1375 mb_mark_used_double(e4b
, start
, len
);
1377 e4b
->bd_info
->bb_free
-= len
;
1378 if (e4b
->bd_info
->bb_first_free
== start
)
1379 e4b
->bd_info
->bb_first_free
+= len
;
1381 /* let's maintain fragments counter */
1383 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1384 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1385 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1387 e4b
->bd_info
->bb_fragments
++;
1388 else if (!mlen
&& !max
)
1389 e4b
->bd_info
->bb_fragments
--;
1391 /* let's maintain buddy itself */
1393 ord
= mb_find_order_for_block(e4b
, start
);
1395 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1396 /* the whole chunk may be allocated at once! */
1398 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1399 BUG_ON((start
>> ord
) >= max
);
1400 mb_set_bit(start
>> ord
, buddy
);
1401 e4b
->bd_info
->bb_counters
[ord
]--;
1408 /* store for history */
1410 ret
= len
| (ord
<< 16);
1412 /* we have to split large buddy */
1414 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1415 mb_set_bit(start
>> ord
, buddy
);
1416 e4b
->bd_info
->bb_counters
[ord
]--;
1419 cur
= (start
>> ord
) & ~1U;
1420 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1421 mb_clear_bit(cur
, buddy
);
1422 mb_clear_bit(cur
+ 1, buddy
);
1423 e4b
->bd_info
->bb_counters
[ord
]++;
1424 e4b
->bd_info
->bb_counters
[ord
]++;
1427 mb_set_bits(EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1428 mb_check_buddy(e4b
);
1434 * Must be called under group lock!
1436 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1437 struct ext4_buddy
*e4b
)
1439 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1442 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1443 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1445 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1446 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1447 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1449 /* preallocation can change ac_b_ex, thus we store actually
1450 * allocated blocks for history */
1451 ac
->ac_f_ex
= ac
->ac_b_ex
;
1453 ac
->ac_status
= AC_STATUS_FOUND
;
1454 ac
->ac_tail
= ret
& 0xffff;
1455 ac
->ac_buddy
= ret
>> 16;
1458 * take the page reference. We want the page to be pinned
1459 * so that we don't get a ext4_mb_init_cache_call for this
1460 * group until we update the bitmap. That would mean we
1461 * double allocate blocks. The reference is dropped
1462 * in ext4_mb_release_context
1464 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1465 get_page(ac
->ac_bitmap_page
);
1466 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1467 get_page(ac
->ac_buddy_page
);
1468 /* on allocation we use ac to track the held semaphore */
1469 ac
->alloc_semp
= e4b
->alloc_semp
;
1470 e4b
->alloc_semp
= NULL
;
1471 /* store last allocated for subsequent stream allocation */
1472 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1473 spin_lock(&sbi
->s_md_lock
);
1474 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1475 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1476 spin_unlock(&sbi
->s_md_lock
);
1481 * regular allocator, for general purposes allocation
1484 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1485 struct ext4_buddy
*e4b
,
1488 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1489 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1490 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1491 struct ext4_free_extent ex
;
1494 if (ac
->ac_status
== AC_STATUS_FOUND
)
1497 * We don't want to scan for a whole year
1499 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1500 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1501 ac
->ac_status
= AC_STATUS_BREAK
;
1506 * Haven't found good chunk so far, let's continue
1508 if (bex
->fe_len
< gex
->fe_len
)
1511 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1512 && bex
->fe_group
== e4b
->bd_group
) {
1513 /* recheck chunk's availability - we don't know
1514 * when it was found (within this lock-unlock
1516 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1517 if (max
>= gex
->fe_len
) {
1518 ext4_mb_use_best_found(ac
, e4b
);
1525 * The routine checks whether found extent is good enough. If it is,
1526 * then the extent gets marked used and flag is set to the context
1527 * to stop scanning. Otherwise, the extent is compared with the
1528 * previous found extent and if new one is better, then it's stored
1529 * in the context. Later, the best found extent will be used, if
1530 * mballoc can't find good enough extent.
1532 * FIXME: real allocation policy is to be designed yet!
1534 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1535 struct ext4_free_extent
*ex
,
1536 struct ext4_buddy
*e4b
)
1538 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1539 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1541 BUG_ON(ex
->fe_len
<= 0);
1542 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1543 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1544 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1549 * The special case - take what you catch first
1551 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1553 ext4_mb_use_best_found(ac
, e4b
);
1558 * Let's check whether the chuck is good enough
1560 if (ex
->fe_len
== gex
->fe_len
) {
1562 ext4_mb_use_best_found(ac
, e4b
);
1567 * If this is first found extent, just store it in the context
1569 if (bex
->fe_len
== 0) {
1575 * If new found extent is better, store it in the context
1577 if (bex
->fe_len
< gex
->fe_len
) {
1578 /* if the request isn't satisfied, any found extent
1579 * larger than previous best one is better */
1580 if (ex
->fe_len
> bex
->fe_len
)
1582 } else if (ex
->fe_len
> gex
->fe_len
) {
1583 /* if the request is satisfied, then we try to find
1584 * an extent that still satisfy the request, but is
1585 * smaller than previous one */
1586 if (ex
->fe_len
< bex
->fe_len
)
1590 ext4_mb_check_limits(ac
, e4b
, 0);
1593 static noinline_for_stack
1594 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1595 struct ext4_buddy
*e4b
)
1597 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1598 ext4_group_t group
= ex
.fe_group
;
1602 BUG_ON(ex
.fe_len
<= 0);
1603 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1607 ext4_lock_group(ac
->ac_sb
, group
);
1608 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1612 ext4_mb_use_best_found(ac
, e4b
);
1615 ext4_unlock_group(ac
->ac_sb
, group
);
1616 ext4_mb_release_desc(e4b
);
1621 static noinline_for_stack
1622 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1623 struct ext4_buddy
*e4b
)
1625 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1628 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1629 struct ext4_super_block
*es
= sbi
->s_es
;
1630 struct ext4_free_extent ex
;
1632 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1635 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1639 ext4_lock_group(ac
->ac_sb
, group
);
1640 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1641 ac
->ac_g_ex
.fe_len
, &ex
);
1643 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1646 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1647 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1648 /* use do_div to get remainder (would be 64-bit modulo) */
1649 if (do_div(start
, sbi
->s_stripe
) == 0) {
1652 ext4_mb_use_best_found(ac
, e4b
);
1654 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1655 BUG_ON(ex
.fe_len
<= 0);
1656 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1657 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1660 ext4_mb_use_best_found(ac
, e4b
);
1661 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1662 /* Sometimes, caller may want to merge even small
1663 * number of blocks to an existing extent */
1664 BUG_ON(ex
.fe_len
<= 0);
1665 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1666 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1669 ext4_mb_use_best_found(ac
, e4b
);
1671 ext4_unlock_group(ac
->ac_sb
, group
);
1672 ext4_mb_release_desc(e4b
);
1678 * The routine scans buddy structures (not bitmap!) from given order
1679 * to max order and tries to find big enough chunk to satisfy the req
1681 static noinline_for_stack
1682 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1683 struct ext4_buddy
*e4b
)
1685 struct super_block
*sb
= ac
->ac_sb
;
1686 struct ext4_group_info
*grp
= e4b
->bd_info
;
1692 BUG_ON(ac
->ac_2order
<= 0);
1693 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1694 if (grp
->bb_counters
[i
] == 0)
1697 buddy
= mb_find_buddy(e4b
, i
, &max
);
1698 BUG_ON(buddy
== NULL
);
1700 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1705 ac
->ac_b_ex
.fe_len
= 1 << i
;
1706 ac
->ac_b_ex
.fe_start
= k
<< i
;
1707 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1709 ext4_mb_use_best_found(ac
, e4b
);
1711 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1713 if (EXT4_SB(sb
)->s_mb_stats
)
1714 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1721 * The routine scans the group and measures all found extents.
1722 * In order to optimize scanning, caller must pass number of
1723 * free blocks in the group, so the routine can know upper limit.
1725 static noinline_for_stack
1726 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1727 struct ext4_buddy
*e4b
)
1729 struct super_block
*sb
= ac
->ac_sb
;
1730 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1731 struct ext4_free_extent ex
;
1735 free
= e4b
->bd_info
->bb_free
;
1738 i
= e4b
->bd_info
->bb_first_free
;
1740 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1741 i
= mb_find_next_zero_bit(bitmap
,
1742 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1743 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1745 * IF we have corrupt bitmap, we won't find any
1746 * free blocks even though group info says we
1747 * we have free blocks
1749 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1750 __func__
, "%d free blocks as per "
1751 "group info. But bitmap says 0",
1756 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1757 BUG_ON(ex
.fe_len
<= 0);
1758 if (free
< ex
.fe_len
) {
1759 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1760 __func__
, "%d free blocks as per "
1761 "group info. But got %d blocks",
1764 * The number of free blocks differs. This mostly
1765 * indicate that the bitmap is corrupt. So exit
1766 * without claiming the space.
1771 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1777 ext4_mb_check_limits(ac
, e4b
, 1);
1781 * This is a special case for storages like raid5
1782 * we try to find stripe-aligned chunks for stripe-size requests
1783 * XXX should do so at least for multiples of stripe size as well
1785 static noinline_for_stack
1786 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1787 struct ext4_buddy
*e4b
)
1789 struct super_block
*sb
= ac
->ac_sb
;
1790 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1791 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1792 struct ext4_free_extent ex
;
1793 ext4_fsblk_t first_group_block
;
1798 BUG_ON(sbi
->s_stripe
== 0);
1800 /* find first stripe-aligned block in group */
1801 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1802 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1803 a
= first_group_block
+ sbi
->s_stripe
- 1;
1804 do_div(a
, sbi
->s_stripe
);
1805 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1807 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1808 if (!mb_test_bit(i
, bitmap
)) {
1809 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1810 if (max
>= sbi
->s_stripe
) {
1813 ext4_mb_use_best_found(ac
, e4b
);
1821 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1822 ext4_group_t group
, int cr
)
1824 unsigned free
, fragments
;
1826 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1827 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1829 BUG_ON(cr
< 0 || cr
>= 4);
1830 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1832 free
= grp
->bb_free
;
1833 fragments
= grp
->bb_fragments
;
1841 BUG_ON(ac
->ac_2order
== 0);
1843 /* Avoid using the first bg of a flexgroup for data files */
1844 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1845 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1846 ((group
% flex_size
) == 0))
1849 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1850 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1851 if (grp
->bb_counters
[i
] > 0)
1855 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1859 if (free
>= ac
->ac_g_ex
.fe_len
)
1872 * lock the group_info alloc_sem of all the groups
1873 * belonging to the same buddy cache page. This
1874 * make sure other parallel operation on the buddy
1875 * cache doesn't happen whild holding the buddy cache
1878 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1882 int blocks_per_page
;
1883 int groups_per_page
;
1884 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
1885 ext4_group_t first_group
;
1886 struct ext4_group_info
*grp
;
1888 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1890 * the buddy cache inode stores the block bitmap
1891 * and buddy information in consecutive blocks.
1892 * So for each group we need two blocks.
1895 pnum
= block
/ blocks_per_page
;
1896 first_group
= pnum
* blocks_per_page
/ 2;
1898 groups_per_page
= blocks_per_page
>> 1;
1899 if (groups_per_page
== 0)
1900 groups_per_page
= 1;
1901 /* read all groups the page covers into the cache */
1902 for (i
= 0; i
< groups_per_page
; i
++) {
1904 if ((first_group
+ i
) >= ngroups
)
1906 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1907 /* take all groups write allocation
1908 * semaphore. This make sure there is
1909 * no block allocation going on in any
1912 down_write_nested(&grp
->alloc_sem
, i
);
1917 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1918 ext4_group_t group
, int locked_group
)
1922 int blocks_per_page
;
1923 ext4_group_t first_group
;
1924 struct ext4_group_info
*grp
;
1926 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1928 * the buddy cache inode stores the block bitmap
1929 * and buddy information in consecutive blocks.
1930 * So for each group we need two blocks.
1933 pnum
= block
/ blocks_per_page
;
1934 first_group
= pnum
* blocks_per_page
/ 2;
1935 /* release locks on all the groups */
1936 for (i
= 0; i
< locked_group
; i
++) {
1938 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1939 /* take all groups write allocation
1940 * semaphore. This make sure there is
1941 * no block allocation going on in any
1944 up_write(&grp
->alloc_sem
);
1949 static noinline_for_stack
int
1950 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1952 ext4_group_t ngroups
, group
, i
;
1956 struct ext4_sb_info
*sbi
;
1957 struct super_block
*sb
;
1958 struct ext4_buddy e4b
;
1962 ngroups
= ext4_get_groups_count(sb
);
1963 /* non-extent files are limited to low blocks/groups */
1964 if (!(EXT4_I(ac
->ac_inode
)->i_flags
& EXT4_EXTENTS_FL
))
1965 ngroups
= sbi
->s_blockfile_groups
;
1967 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1969 /* first, try the goal */
1970 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1971 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1974 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1978 * ac->ac2_order is set only if the fe_len is a power of 2
1979 * if ac2_order is set we also set criteria to 0 so that we
1980 * try exact allocation using buddy.
1982 i
= fls(ac
->ac_g_ex
.fe_len
);
1985 * We search using buddy data only if the order of the request
1986 * is greater than equal to the sbi_s_mb_order2_reqs
1987 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1989 if (i
>= sbi
->s_mb_order2_reqs
) {
1991 * This should tell if fe_len is exactly power of 2
1993 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1994 ac
->ac_2order
= i
- 1;
1997 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1999 /* if stream allocation is enabled, use global goal */
2000 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
2001 /* TBD: may be hot point */
2002 spin_lock(&sbi
->s_md_lock
);
2003 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2004 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2005 spin_unlock(&sbi
->s_md_lock
);
2008 /* Let's just scan groups to find more-less suitable blocks */
2009 cr
= ac
->ac_2order
? 0 : 1;
2011 * cr == 0 try to get exact allocation,
2012 * cr == 3 try to get anything
2015 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2016 ac
->ac_criteria
= cr
;
2018 * searching for the right group start
2019 * from the goal value specified
2021 group
= ac
->ac_g_ex
.fe_group
;
2023 for (i
= 0; i
< ngroups
; group
++, i
++) {
2024 struct ext4_group_info
*grp
;
2025 struct ext4_group_desc
*desc
;
2027 if (group
== ngroups
)
2030 /* quick check to skip empty groups */
2031 grp
= ext4_get_group_info(sb
, group
);
2032 if (grp
->bb_free
== 0)
2035 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2039 ext4_lock_group(sb
, group
);
2040 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2041 /* someone did allocation from this group */
2042 ext4_unlock_group(sb
, group
);
2043 ext4_mb_release_desc(&e4b
);
2047 ac
->ac_groups_scanned
++;
2048 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2050 ext4_mb_simple_scan_group(ac
, &e4b
);
2052 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2053 ext4_mb_scan_aligned(ac
, &e4b
);
2055 ext4_mb_complex_scan_group(ac
, &e4b
);
2057 ext4_unlock_group(sb
, group
);
2058 ext4_mb_release_desc(&e4b
);
2060 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2065 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2066 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2068 * We've been searching too long. Let's try to allocate
2069 * the best chunk we've found so far
2072 ext4_mb_try_best_found(ac
, &e4b
);
2073 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2075 * Someone more lucky has already allocated it.
2076 * The only thing we can do is just take first
2078 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2080 ac
->ac_b_ex
.fe_group
= 0;
2081 ac
->ac_b_ex
.fe_start
= 0;
2082 ac
->ac_b_ex
.fe_len
= 0;
2083 ac
->ac_status
= AC_STATUS_CONTINUE
;
2084 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2086 atomic_inc(&sbi
->s_mb_lost_chunks
);
2094 #ifdef EXT4_MB_HISTORY
2095 struct ext4_mb_proc_session
{
2096 struct ext4_mb_history
*history
;
2097 struct super_block
*sb
;
2102 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2103 struct ext4_mb_history
*hs
,
2106 if (hs
== s
->history
+ s
->max
)
2108 if (!first
&& hs
== s
->history
+ s
->start
)
2110 while (hs
->orig
.fe_len
== 0) {
2112 if (hs
== s
->history
+ s
->max
)
2114 if (hs
== s
->history
+ s
->start
)
2120 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2122 struct ext4_mb_proc_session
*s
= seq
->private;
2123 struct ext4_mb_history
*hs
;
2127 return SEQ_START_TOKEN
;
2128 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2131 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2135 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2138 struct ext4_mb_proc_session
*s
= seq
->private;
2139 struct ext4_mb_history
*hs
= v
;
2142 if (v
== SEQ_START_TOKEN
)
2143 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2145 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2148 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2150 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2151 struct ext4_mb_history
*hs
= v
;
2153 if (v
== SEQ_START_TOKEN
) {
2154 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2155 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2156 "pid", "inode", "original", "goal", "result", "found",
2157 "grps", "cr", "flags", "merge", "tail", "broken");
2161 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2162 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2163 "%-5u %-5s %-5u %-6u\n";
2164 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2165 hs
->result
.fe_start
, hs
->result
.fe_len
,
2166 hs
->result
.fe_logical
);
2167 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2168 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2169 hs
->orig
.fe_logical
);
2170 sprintf(buf3
, "%u/%d/%u@%u", hs
->goal
.fe_group
,
2171 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2172 hs
->goal
.fe_logical
);
2173 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2174 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2175 hs
->merged
? "M" : "", hs
->tail
,
2176 hs
->buddy
? 1 << hs
->buddy
: 0);
2177 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2178 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2179 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2180 hs
->result
.fe_start
, hs
->result
.fe_len
,
2181 hs
->result
.fe_logical
);
2182 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2183 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2184 hs
->orig
.fe_logical
);
2185 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2186 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2187 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2188 hs
->result
.fe_start
, hs
->result
.fe_len
);
2189 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2190 hs
->pid
, hs
->ino
, buf2
);
2191 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2192 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2193 hs
->result
.fe_start
, hs
->result
.fe_len
);
2194 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2195 hs
->pid
, hs
->ino
, buf2
);
2200 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2204 static struct seq_operations ext4_mb_seq_history_ops
= {
2205 .start
= ext4_mb_seq_history_start
,
2206 .next
= ext4_mb_seq_history_next
,
2207 .stop
= ext4_mb_seq_history_stop
,
2208 .show
= ext4_mb_seq_history_show
,
2211 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2213 struct super_block
*sb
= PDE(inode
)->data
;
2214 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2215 struct ext4_mb_proc_session
*s
;
2219 if (unlikely(sbi
->s_mb_history
== NULL
))
2221 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2225 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2226 s
->history
= kmalloc(size
, GFP_KERNEL
);
2227 if (s
->history
== NULL
) {
2232 spin_lock(&sbi
->s_mb_history_lock
);
2233 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2234 s
->max
= sbi
->s_mb_history_max
;
2235 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2236 spin_unlock(&sbi
->s_mb_history_lock
);
2238 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2240 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2250 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2252 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2253 struct ext4_mb_proc_session
*s
= seq
->private;
2256 return seq_release(inode
, file
);
2259 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2260 const char __user
*buffer
,
2261 size_t count
, loff_t
*ppos
)
2263 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2264 struct ext4_mb_proc_session
*s
= seq
->private;
2265 struct super_block
*sb
= s
->sb
;
2269 if (count
>= sizeof(str
)) {
2270 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2271 "mb_history", (int)sizeof(str
));
2275 if (copy_from_user(str
, buffer
, count
))
2278 value
= simple_strtol(str
, NULL
, 0);
2281 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2286 static struct file_operations ext4_mb_seq_history_fops
= {
2287 .owner
= THIS_MODULE
,
2288 .open
= ext4_mb_seq_history_open
,
2290 .write
= ext4_mb_seq_history_write
,
2291 .llseek
= seq_lseek
,
2292 .release
= ext4_mb_seq_history_release
,
2295 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2297 struct super_block
*sb
= seq
->private;
2300 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2303 return (void *) ((unsigned long) group
);
2306 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2308 struct super_block
*sb
= seq
->private;
2312 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2315 return (void *) ((unsigned long) group
);
2318 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2320 struct super_block
*sb
= seq
->private;
2321 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2324 struct ext4_buddy e4b
;
2326 struct ext4_group_info info
;
2327 unsigned short counters
[16];
2332 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2333 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2334 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2335 "group", "free", "frags", "first",
2336 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2337 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2339 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2340 sizeof(struct ext4_group_info
);
2341 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2343 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2346 ext4_lock_group(sb
, group
);
2347 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2348 ext4_unlock_group(sb
, group
);
2349 ext4_mb_release_desc(&e4b
);
2351 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2352 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2353 for (i
= 0; i
<= 13; i
++)
2354 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2355 sg
.info
.bb_counters
[i
] : 0);
2356 seq_printf(seq
, " ]\n");
2361 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2365 static struct seq_operations ext4_mb_seq_groups_ops
= {
2366 .start
= ext4_mb_seq_groups_start
,
2367 .next
= ext4_mb_seq_groups_next
,
2368 .stop
= ext4_mb_seq_groups_stop
,
2369 .show
= ext4_mb_seq_groups_show
,
2372 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2374 struct super_block
*sb
= PDE(inode
)->data
;
2377 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2379 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2386 static struct file_operations ext4_mb_seq_groups_fops
= {
2387 .owner
= THIS_MODULE
,
2388 .open
= ext4_mb_seq_groups_open
,
2390 .llseek
= seq_lseek
,
2391 .release
= seq_release
,
2394 static void ext4_mb_history_release(struct super_block
*sb
)
2396 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2398 if (sbi
->s_proc
!= NULL
) {
2399 remove_proc_entry("mb_groups", sbi
->s_proc
);
2400 if (sbi
->s_mb_history_max
)
2401 remove_proc_entry("mb_history", sbi
->s_proc
);
2403 kfree(sbi
->s_mb_history
);
2406 static void ext4_mb_history_init(struct super_block
*sb
)
2408 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2411 if (sbi
->s_proc
!= NULL
) {
2412 if (sbi
->s_mb_history_max
)
2413 proc_create_data("mb_history", S_IRUGO
, sbi
->s_proc
,
2414 &ext4_mb_seq_history_fops
, sb
);
2415 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2416 &ext4_mb_seq_groups_fops
, sb
);
2419 sbi
->s_mb_history_cur
= 0;
2420 spin_lock_init(&sbi
->s_mb_history_lock
);
2421 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2422 sbi
->s_mb_history
= i
? kzalloc(i
, GFP_KERNEL
) : NULL
;
2423 /* if we can't allocate history, then we simple won't use it */
2426 static noinline_for_stack
void
2427 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2429 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2430 struct ext4_mb_history h
;
2432 if (sbi
->s_mb_history
== NULL
)
2435 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2439 h
.pid
= current
->pid
;
2440 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2441 h
.orig
= ac
->ac_o_ex
;
2442 h
.result
= ac
->ac_b_ex
;
2443 h
.flags
= ac
->ac_flags
;
2444 h
.found
= ac
->ac_found
;
2445 h
.groups
= ac
->ac_groups_scanned
;
2446 h
.cr
= ac
->ac_criteria
;
2447 h
.tail
= ac
->ac_tail
;
2448 h
.buddy
= ac
->ac_buddy
;
2450 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2451 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2452 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2454 h
.goal
= ac
->ac_g_ex
;
2455 h
.result
= ac
->ac_f_ex
;
2458 spin_lock(&sbi
->s_mb_history_lock
);
2459 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2460 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2461 sbi
->s_mb_history_cur
= 0;
2462 spin_unlock(&sbi
->s_mb_history_lock
);
2466 #define ext4_mb_history_release(sb)
2467 #define ext4_mb_history_init(sb)
2471 /* Create and initialize ext4_group_info data for the given group. */
2472 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2473 struct ext4_group_desc
*desc
)
2477 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2478 struct ext4_group_info
**meta_group_info
;
2481 * First check if this group is the first of a reserved block.
2482 * If it's true, we have to allocate a new table of pointers
2483 * to ext4_group_info structures
2485 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2486 metalen
= sizeof(*meta_group_info
) <<
2487 EXT4_DESC_PER_BLOCK_BITS(sb
);
2488 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2489 if (meta_group_info
== NULL
) {
2490 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2492 goto exit_meta_group_info
;
2494 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2499 * calculate needed size. if change bb_counters size,
2500 * don't forget about ext4_mb_generate_buddy()
2502 len
= offsetof(typeof(**meta_group_info
),
2503 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2506 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2507 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2509 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2510 if (meta_group_info
[i
] == NULL
) {
2511 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2512 goto exit_group_info
;
2514 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2515 &(meta_group_info
[i
]->bb_state
));
2518 * initialize bb_free to be able to skip
2519 * empty groups without initialization
2521 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2522 meta_group_info
[i
]->bb_free
=
2523 ext4_free_blocks_after_init(sb
, group
, desc
);
2525 meta_group_info
[i
]->bb_free
=
2526 ext4_free_blks_count(sb
, desc
);
2529 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2530 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2531 meta_group_info
[i
]->bb_free_root
.rb_node
= NULL
;;
2535 struct buffer_head
*bh
;
2536 meta_group_info
[i
]->bb_bitmap
=
2537 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2538 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2539 bh
= ext4_read_block_bitmap(sb
, group
);
2541 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2550 /* If a meta_group_info table has been allocated, release it now */
2551 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2552 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2553 exit_meta_group_info
:
2555 } /* ext4_mb_add_groupinfo */
2558 * Update an existing group.
2559 * This function is used for online resize
2561 void ext4_mb_update_group_info(struct ext4_group_info
*grp
, ext4_grpblk_t add
)
2563 grp
->bb_free
+= add
;
2566 static int ext4_mb_init_backend(struct super_block
*sb
)
2568 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2570 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2571 struct ext4_super_block
*es
= sbi
->s_es
;
2572 int num_meta_group_infos
;
2573 int num_meta_group_infos_max
;
2575 struct ext4_group_desc
*desc
;
2577 /* This is the number of blocks used by GDT */
2578 num_meta_group_infos
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) -
2579 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2582 * This is the total number of blocks used by GDT including
2583 * the number of reserved blocks for GDT.
2584 * The s_group_info array is allocated with this value
2585 * to allow a clean online resize without a complex
2586 * manipulation of pointer.
2587 * The drawback is the unused memory when no resize
2588 * occurs but it's very low in terms of pages
2589 * (see comments below)
2590 * Need to handle this properly when META_BG resizing is allowed
2592 num_meta_group_infos_max
= num_meta_group_infos
+
2593 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2596 * array_size is the size of s_group_info array. We round it
2597 * to the next power of two because this approximation is done
2598 * internally by kmalloc so we can have some more memory
2599 * for free here (e.g. may be used for META_BG resize).
2602 while (array_size
< sizeof(*sbi
->s_group_info
) *
2603 num_meta_group_infos_max
)
2604 array_size
= array_size
<< 1;
2605 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2606 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2607 * So a two level scheme suffices for now. */
2608 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2609 if (sbi
->s_group_info
== NULL
) {
2610 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2613 sbi
->s_buddy_cache
= new_inode(sb
);
2614 if (sbi
->s_buddy_cache
== NULL
) {
2615 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2618 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2619 for (i
= 0; i
< ngroups
; i
++) {
2620 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2623 "EXT4-fs: can't read descriptor %u\n", i
);
2626 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2634 kfree(ext4_get_group_info(sb
, i
));
2635 i
= num_meta_group_infos
;
2637 kfree(sbi
->s_group_info
[i
]);
2638 iput(sbi
->s_buddy_cache
);
2640 kfree(sbi
->s_group_info
);
2644 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2646 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2652 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2654 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2655 if (sbi
->s_mb_offsets
== NULL
) {
2659 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned int);
2660 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2661 if (sbi
->s_mb_maxs
== NULL
) {
2662 kfree(sbi
->s_mb_offsets
);
2666 /* order 0 is regular bitmap */
2667 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2668 sbi
->s_mb_offsets
[0] = 0;
2672 max
= sb
->s_blocksize
<< 2;
2674 sbi
->s_mb_offsets
[i
] = offset
;
2675 sbi
->s_mb_maxs
[i
] = max
;
2676 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2679 } while (i
<= sb
->s_blocksize_bits
+ 1);
2681 /* init file for buddy data */
2682 ret
= ext4_mb_init_backend(sb
);
2684 kfree(sbi
->s_mb_offsets
);
2685 kfree(sbi
->s_mb_maxs
);
2689 spin_lock_init(&sbi
->s_md_lock
);
2690 spin_lock_init(&sbi
->s_bal_lock
);
2692 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2693 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2694 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2695 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2696 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2697 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2698 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2700 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2701 if (sbi
->s_locality_groups
== NULL
) {
2702 kfree(sbi
->s_mb_offsets
);
2703 kfree(sbi
->s_mb_maxs
);
2706 for_each_possible_cpu(i
) {
2707 struct ext4_locality_group
*lg
;
2708 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2709 mutex_init(&lg
->lg_mutex
);
2710 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2711 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2712 spin_lock_init(&lg
->lg_prealloc_lock
);
2715 ext4_mb_history_init(sb
);
2718 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2720 printk(KERN_INFO
"EXT4-fs: mballoc enabled\n");
2724 /* need to called with the ext4 group lock held */
2725 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2727 struct ext4_prealloc_space
*pa
;
2728 struct list_head
*cur
, *tmp
;
2731 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2732 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2733 list_del(&pa
->pa_group_list
);
2735 kmem_cache_free(ext4_pspace_cachep
, pa
);
2738 mb_debug("mballoc: %u PAs left\n", count
);
2742 int ext4_mb_release(struct super_block
*sb
)
2744 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2746 int num_meta_group_infos
;
2747 struct ext4_group_info
*grinfo
;
2748 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2750 if (sbi
->s_group_info
) {
2751 for (i
= 0; i
< ngroups
; i
++) {
2752 grinfo
= ext4_get_group_info(sb
, i
);
2754 kfree(grinfo
->bb_bitmap
);
2756 ext4_lock_group(sb
, i
);
2757 ext4_mb_cleanup_pa(grinfo
);
2758 ext4_unlock_group(sb
, i
);
2761 num_meta_group_infos
= (ngroups
+
2762 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2763 EXT4_DESC_PER_BLOCK_BITS(sb
);
2764 for (i
= 0; i
< num_meta_group_infos
; i
++)
2765 kfree(sbi
->s_group_info
[i
]);
2766 kfree(sbi
->s_group_info
);
2768 kfree(sbi
->s_mb_offsets
);
2769 kfree(sbi
->s_mb_maxs
);
2770 if (sbi
->s_buddy_cache
)
2771 iput(sbi
->s_buddy_cache
);
2772 if (sbi
->s_mb_stats
) {
2774 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2775 atomic_read(&sbi
->s_bal_allocated
),
2776 atomic_read(&sbi
->s_bal_reqs
),
2777 atomic_read(&sbi
->s_bal_success
));
2779 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2780 "%u 2^N hits, %u breaks, %u lost\n",
2781 atomic_read(&sbi
->s_bal_ex_scanned
),
2782 atomic_read(&sbi
->s_bal_goals
),
2783 atomic_read(&sbi
->s_bal_2orders
),
2784 atomic_read(&sbi
->s_bal_breaks
),
2785 atomic_read(&sbi
->s_mb_lost_chunks
));
2787 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2788 sbi
->s_mb_buddies_generated
++,
2789 sbi
->s_mb_generation_time
);
2791 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2792 atomic_read(&sbi
->s_mb_preallocated
),
2793 atomic_read(&sbi
->s_mb_discarded
));
2796 free_percpu(sbi
->s_locality_groups
);
2797 ext4_mb_history_release(sb
);
2803 * This function is called by the jbd2 layer once the commit has finished,
2804 * so we know we can free the blocks that were released with that commit.
2806 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2808 struct super_block
*sb
= journal
->j_private
;
2809 struct ext4_buddy e4b
;
2810 struct ext4_group_info
*db
;
2811 int err
, count
= 0, count2
= 0;
2812 struct ext4_free_data
*entry
;
2813 struct list_head
*l
, *ltmp
;
2815 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2816 entry
= list_entry(l
, struct ext4_free_data
, list
);
2818 mb_debug("gonna free %u blocks in group %u (0x%p):",
2819 entry
->count
, entry
->group
, entry
);
2821 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2822 /* we expect to find existing buddy because it's pinned */
2826 /* there are blocks to put in buddy to make them really free */
2827 count
+= entry
->count
;
2829 ext4_lock_group(sb
, entry
->group
);
2830 /* Take it out of per group rb tree */
2831 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2832 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2834 if (!db
->bb_free_root
.rb_node
) {
2835 /* No more items in the per group rb tree
2836 * balance refcounts from ext4_mb_free_metadata()
2838 page_cache_release(e4b
.bd_buddy_page
);
2839 page_cache_release(e4b
.bd_bitmap_page
);
2841 ext4_unlock_group(sb
, entry
->group
);
2842 if (test_opt(sb
, DISCARD
)) {
2843 ext4_fsblk_t discard_block
;
2844 struct ext4_super_block
*es
= EXT4_SB(sb
)->s_es
;
2846 discard_block
= (ext4_fsblk_t
)entry
->group
*
2847 EXT4_BLOCKS_PER_GROUP(sb
)
2849 + le32_to_cpu(es
->s_first_data_block
);
2850 trace_ext4_discard_blocks(sb
,
2851 (unsigned long long)discard_block
,
2853 sb_issue_discard(sb
, discard_block
, entry
->count
);
2855 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2856 ext4_mb_release_desc(&e4b
);
2859 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2862 int __init
init_ext4_mballoc(void)
2864 ext4_pspace_cachep
=
2865 kmem_cache_create("ext4_prealloc_space",
2866 sizeof(struct ext4_prealloc_space
),
2867 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2868 if (ext4_pspace_cachep
== NULL
)
2872 kmem_cache_create("ext4_alloc_context",
2873 sizeof(struct ext4_allocation_context
),
2874 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2875 if (ext4_ac_cachep
== NULL
) {
2876 kmem_cache_destroy(ext4_pspace_cachep
);
2880 ext4_free_ext_cachep
=
2881 kmem_cache_create("ext4_free_block_extents",
2882 sizeof(struct ext4_free_data
),
2883 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2884 if (ext4_free_ext_cachep
== NULL
) {
2885 kmem_cache_destroy(ext4_pspace_cachep
);
2886 kmem_cache_destroy(ext4_ac_cachep
);
2892 void exit_ext4_mballoc(void)
2895 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2896 * before destroying the slab cache.
2899 kmem_cache_destroy(ext4_pspace_cachep
);
2900 kmem_cache_destroy(ext4_ac_cachep
);
2901 kmem_cache_destroy(ext4_free_ext_cachep
);
2906 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2907 * Returns 0 if success or error code
2909 static noinline_for_stack
int
2910 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2911 handle_t
*handle
, unsigned int reserv_blks
)
2913 struct buffer_head
*bitmap_bh
= NULL
;
2914 struct ext4_super_block
*es
;
2915 struct ext4_group_desc
*gdp
;
2916 struct buffer_head
*gdp_bh
;
2917 struct ext4_sb_info
*sbi
;
2918 struct super_block
*sb
;
2922 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2923 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2931 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2935 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2940 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2944 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2945 ext4_free_blks_count(sb
, gdp
));
2947 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2951 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2952 + ac
->ac_b_ex
.fe_start
2953 + le32_to_cpu(es
->s_first_data_block
);
2955 len
= ac
->ac_b_ex
.fe_len
;
2956 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2957 ext4_error(sb
, __func__
,
2958 "Allocating blocks %llu-%llu which overlap "
2959 "fs metadata\n", block
, block
+len
);
2960 /* File system mounted not to panic on error
2961 * Fix the bitmap and repeat the block allocation
2962 * We leak some of the blocks here.
2964 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2965 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2966 ac
->ac_b_ex
.fe_len
);
2967 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2968 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2974 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2975 #ifdef AGGRESSIVE_CHECK
2978 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2979 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2980 bitmap_bh
->b_data
));
2984 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,ac
->ac_b_ex
.fe_len
);
2985 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2986 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2987 ext4_free_blks_set(sb
, gdp
,
2988 ext4_free_blocks_after_init(sb
,
2989 ac
->ac_b_ex
.fe_group
, gdp
));
2991 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2992 ext4_free_blks_set(sb
, gdp
, len
);
2993 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
2995 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2996 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
2998 * Now reduce the dirty block count also. Should not go negative
3000 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3001 /* release all the reserved blocks if non delalloc */
3002 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
3004 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
3005 ac
->ac_b_ex
.fe_len
);
3006 /* convert reserved quota blocks to real quota blocks */
3007 vfs_dq_claim_block(ac
->ac_inode
, ac
->ac_b_ex
.fe_len
);
3010 if (sbi
->s_log_groups_per_flex
) {
3011 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3012 ac
->ac_b_ex
.fe_group
);
3013 atomic_sub(ac
->ac_b_ex
.fe_len
,
3014 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
3017 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3020 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3029 * here we normalize request for locality group
3030 * Group request are normalized to s_strip size if we set the same via mount
3031 * option. If not we set it to s_mb_group_prealloc which can be configured via
3032 * /sys/fs/ext4/<partition>/mb_group_prealloc
3034 * XXX: should we try to preallocate more than the group has now?
3036 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3038 struct super_block
*sb
= ac
->ac_sb
;
3039 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3042 if (EXT4_SB(sb
)->s_stripe
)
3043 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3045 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3046 mb_debug("#%u: goal %u blocks for locality group\n",
3047 current
->pid
, ac
->ac_g_ex
.fe_len
);
3051 * Normalization means making request better in terms of
3052 * size and alignment
3054 static noinline_for_stack
void
3055 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3056 struct ext4_allocation_request
*ar
)
3060 loff_t size
, orig_size
, start_off
;
3061 ext4_lblk_t start
, orig_start
;
3062 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3063 struct ext4_prealloc_space
*pa
;
3065 /* do normalize only data requests, metadata requests
3066 do not need preallocation */
3067 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3070 /* sometime caller may want exact blocks */
3071 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3074 /* caller may indicate that preallocation isn't
3075 * required (it's a tail, for example) */
3076 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3079 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3080 ext4_mb_normalize_group_request(ac
);
3084 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3086 /* first, let's learn actual file size
3087 * given current request is allocated */
3088 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3089 size
= size
<< bsbits
;
3090 if (size
< i_size_read(ac
->ac_inode
))
3091 size
= i_size_read(ac
->ac_inode
);
3093 /* max size of free chunks */
3096 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3097 (req <= (size) || max <= (chunk_size))
3099 /* first, try to predict filesize */
3100 /* XXX: should this table be tunable? */
3102 if (size
<= 16 * 1024) {
3104 } else if (size
<= 32 * 1024) {
3106 } else if (size
<= 64 * 1024) {
3108 } else if (size
<= 128 * 1024) {
3110 } else if (size
<= 256 * 1024) {
3112 } else if (size
<= 512 * 1024) {
3114 } else if (size
<= 1024 * 1024) {
3116 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3117 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3118 (21 - bsbits
)) << 21;
3119 size
= 2 * 1024 * 1024;
3120 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3121 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3122 (22 - bsbits
)) << 22;
3123 size
= 4 * 1024 * 1024;
3124 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3125 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3126 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3127 (23 - bsbits
)) << 23;
3128 size
= 8 * 1024 * 1024;
3130 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3131 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3133 orig_size
= size
= size
>> bsbits
;
3134 orig_start
= start
= start_off
>> bsbits
;
3136 /* don't cover already allocated blocks in selected range */
3137 if (ar
->pleft
&& start
<= ar
->lleft
) {
3138 size
-= ar
->lleft
+ 1 - start
;
3139 start
= ar
->lleft
+ 1;
3141 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3142 size
-= start
+ size
- ar
->lright
;
3146 /* check we don't cross already preallocated blocks */
3148 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3153 spin_lock(&pa
->pa_lock
);
3154 if (pa
->pa_deleted
) {
3155 spin_unlock(&pa
->pa_lock
);
3159 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3161 /* PA must not overlap original request */
3162 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3163 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3165 /* skip PA normalized request doesn't overlap with */
3166 if (pa
->pa_lstart
>= end
) {
3167 spin_unlock(&pa
->pa_lock
);
3170 if (pa_end
<= start
) {
3171 spin_unlock(&pa
->pa_lock
);
3174 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3176 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3177 BUG_ON(pa_end
< start
);
3181 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3182 BUG_ON(pa
->pa_lstart
> end
);
3183 end
= pa
->pa_lstart
;
3185 spin_unlock(&pa
->pa_lock
);
3190 /* XXX: extra loop to check we really don't overlap preallocations */
3192 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3194 spin_lock(&pa
->pa_lock
);
3195 if (pa
->pa_deleted
== 0) {
3196 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3197 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3199 spin_unlock(&pa
->pa_lock
);
3203 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3204 start
> ac
->ac_o_ex
.fe_logical
) {
3205 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3206 (unsigned long) start
, (unsigned long) size
,
3207 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3209 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3210 start
> ac
->ac_o_ex
.fe_logical
);
3211 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3213 /* now prepare goal request */
3215 /* XXX: is it better to align blocks WRT to logical
3216 * placement or satisfy big request as is */
3217 ac
->ac_g_ex
.fe_logical
= start
;
3218 ac
->ac_g_ex
.fe_len
= size
;
3220 /* define goal start in order to merge */
3221 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3222 /* merge to the right */
3223 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3224 &ac
->ac_f_ex
.fe_group
,
3225 &ac
->ac_f_ex
.fe_start
);
3226 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3228 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3229 /* merge to the left */
3230 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3231 &ac
->ac_f_ex
.fe_group
,
3232 &ac
->ac_f_ex
.fe_start
);
3233 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3236 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3237 (unsigned) orig_size
, (unsigned) start
);
3240 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3242 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3244 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3245 atomic_inc(&sbi
->s_bal_reqs
);
3246 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3247 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3248 atomic_inc(&sbi
->s_bal_success
);
3249 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3250 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3251 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3252 atomic_inc(&sbi
->s_bal_goals
);
3253 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3254 atomic_inc(&sbi
->s_bal_breaks
);
3257 ext4_mb_store_history(ac
);
3261 * Called on failure; free up any blocks from the inode PA for this
3262 * context. We don't need this for MB_GROUP_PA because we only change
3263 * pa_free in ext4_mb_release_context(), but on failure, we've already
3264 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3266 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3268 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3271 if (pa
&& pa
->pa_type
== MB_INODE_PA
) {
3272 len
= ac
->ac_b_ex
.fe_len
;
3279 * use blocks preallocated to inode
3281 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3282 struct ext4_prealloc_space
*pa
)
3288 /* found preallocated blocks, use them */
3289 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3290 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3292 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3293 &ac
->ac_b_ex
.fe_start
);
3294 ac
->ac_b_ex
.fe_len
= len
;
3295 ac
->ac_status
= AC_STATUS_FOUND
;
3298 BUG_ON(start
< pa
->pa_pstart
);
3299 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3300 BUG_ON(pa
->pa_free
< len
);
3303 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3307 * use blocks preallocated to locality group
3309 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3310 struct ext4_prealloc_space
*pa
)
3312 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3314 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3315 &ac
->ac_b_ex
.fe_group
,
3316 &ac
->ac_b_ex
.fe_start
);
3317 ac
->ac_b_ex
.fe_len
= len
;
3318 ac
->ac_status
= AC_STATUS_FOUND
;
3321 /* we don't correct pa_pstart or pa_plen here to avoid
3322 * possible race when the group is being loaded concurrently
3323 * instead we correct pa later, after blocks are marked
3324 * in on-disk bitmap -- see ext4_mb_release_context()
3325 * Other CPUs are prevented from allocating from this pa by lg_mutex
3327 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3331 * Return the prealloc space that have minimal distance
3332 * from the goal block. @cpa is the prealloc
3333 * space that is having currently known minimal distance
3334 * from the goal block.
3336 static struct ext4_prealloc_space
*
3337 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3338 struct ext4_prealloc_space
*pa
,
3339 struct ext4_prealloc_space
*cpa
)
3341 ext4_fsblk_t cur_distance
, new_distance
;
3344 atomic_inc(&pa
->pa_count
);
3347 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3348 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3350 if (cur_distance
< new_distance
)
3353 /* drop the previous reference */
3354 atomic_dec(&cpa
->pa_count
);
3355 atomic_inc(&pa
->pa_count
);
3360 * search goal blocks in preallocated space
3362 static noinline_for_stack
int
3363 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3366 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3367 struct ext4_locality_group
*lg
;
3368 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3369 ext4_fsblk_t goal_block
;
3371 /* only data can be preallocated */
3372 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3375 /* first, try per-file preallocation */
3377 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3379 /* all fields in this condition don't change,
3380 * so we can skip locking for them */
3381 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3382 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3385 /* non-extent files can't have physical blocks past 2^32 */
3386 if (!(EXT4_I(ac
->ac_inode
)->i_flags
& EXT4_EXTENTS_FL
) &&
3387 pa
->pa_pstart
+ pa
->pa_len
> EXT4_MAX_BLOCK_FILE_PHYS
)
3390 /* found preallocated blocks, use them */
3391 spin_lock(&pa
->pa_lock
);
3392 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3393 atomic_inc(&pa
->pa_count
);
3394 ext4_mb_use_inode_pa(ac
, pa
);
3395 spin_unlock(&pa
->pa_lock
);
3396 ac
->ac_criteria
= 10;
3400 spin_unlock(&pa
->pa_lock
);
3404 /* can we use group allocation? */
3405 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3408 /* inode may have no locality group for some reason */
3412 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3413 if (order
> PREALLOC_TB_SIZE
- 1)
3414 /* The max size of hash table is PREALLOC_TB_SIZE */
3415 order
= PREALLOC_TB_SIZE
- 1;
3417 goal_block
= ac
->ac_g_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) +
3418 ac
->ac_g_ex
.fe_start
+
3419 le32_to_cpu(EXT4_SB(ac
->ac_sb
)->s_es
->s_first_data_block
);
3421 * search for the prealloc space that is having
3422 * minimal distance from the goal block.
3424 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3426 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3428 spin_lock(&pa
->pa_lock
);
3429 if (pa
->pa_deleted
== 0 &&
3430 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3432 cpa
= ext4_mb_check_group_pa(goal_block
,
3435 spin_unlock(&pa
->pa_lock
);
3440 ext4_mb_use_group_pa(ac
, cpa
);
3441 ac
->ac_criteria
= 20;
3448 * the function goes through all block freed in the group
3449 * but not yet committed and marks them used in in-core bitmap.
3450 * buddy must be generated from this bitmap
3451 * Need to be called with the ext4 group lock held
3453 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3457 struct ext4_group_info
*grp
;
3458 struct ext4_free_data
*entry
;
3460 grp
= ext4_get_group_info(sb
, group
);
3461 n
= rb_first(&(grp
->bb_free_root
));
3464 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3465 mb_set_bits(bitmap
, entry
->start_blk
, entry
->count
);
3472 * the function goes through all preallocation in this group and marks them
3473 * used in in-core bitmap. buddy must be generated from this bitmap
3474 * Need to be called with ext4 group lock held
3476 static noinline_for_stack
3477 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3480 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3481 struct ext4_prealloc_space
*pa
;
3482 struct list_head
*cur
;
3483 ext4_group_t groupnr
;
3484 ext4_grpblk_t start
;
3485 int preallocated
= 0;
3489 /* all form of preallocation discards first load group,
3490 * so the only competing code is preallocation use.
3491 * we don't need any locking here
3492 * notice we do NOT ignore preallocations with pa_deleted
3493 * otherwise we could leave used blocks available for
3494 * allocation in buddy when concurrent ext4_mb_put_pa()
3495 * is dropping preallocation
3497 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3498 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3499 spin_lock(&pa
->pa_lock
);
3500 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3503 spin_unlock(&pa
->pa_lock
);
3504 if (unlikely(len
== 0))
3506 BUG_ON(groupnr
!= group
);
3507 mb_set_bits(bitmap
, start
, len
);
3508 preallocated
+= len
;
3511 mb_debug("prellocated %u for group %u\n", preallocated
, group
);
3514 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3516 struct ext4_prealloc_space
*pa
;
3517 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3518 kmem_cache_free(ext4_pspace_cachep
, pa
);
3522 * drops a reference to preallocated space descriptor
3523 * if this was the last reference and the space is consumed
3525 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3526 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3529 ext4_fsblk_t grp_blk
;
3531 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3534 /* in this short window concurrent discard can set pa_deleted */
3535 spin_lock(&pa
->pa_lock
);
3536 if (pa
->pa_deleted
== 1) {
3537 spin_unlock(&pa
->pa_lock
);
3542 spin_unlock(&pa
->pa_lock
);
3544 grp_blk
= pa
->pa_pstart
;
3546 * If doing group-based preallocation, pa_pstart may be in the
3547 * next group when pa is used up
3549 if (pa
->pa_type
== MB_GROUP_PA
)
3552 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3557 * P1 (buddy init) P2 (regular allocation)
3558 * find block B in PA
3559 * copy on-disk bitmap to buddy
3560 * mark B in on-disk bitmap
3561 * drop PA from group
3562 * mark all PAs in buddy
3564 * thus, P1 initializes buddy with B available. to prevent this
3565 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3568 ext4_lock_group(sb
, grp
);
3569 list_del(&pa
->pa_group_list
);
3570 ext4_unlock_group(sb
, grp
);
3572 spin_lock(pa
->pa_obj_lock
);
3573 list_del_rcu(&pa
->pa_inode_list
);
3574 spin_unlock(pa
->pa_obj_lock
);
3576 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3580 * creates new preallocated space for given inode
3582 static noinline_for_stack
int
3583 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3585 struct super_block
*sb
= ac
->ac_sb
;
3586 struct ext4_prealloc_space
*pa
;
3587 struct ext4_group_info
*grp
;
3588 struct ext4_inode_info
*ei
;
3590 /* preallocate only when found space is larger then requested */
3591 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3592 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3593 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3595 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3599 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3605 /* we can't allocate as much as normalizer wants.
3606 * so, found space must get proper lstart
3607 * to cover original request */
3608 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3609 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3611 /* we're limited by original request in that
3612 * logical block must be covered any way
3613 * winl is window we can move our chunk within */
3614 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3616 /* also, we should cover whole original request */
3617 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3619 /* the smallest one defines real window */
3620 win
= min(winl
, wins
);
3622 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3623 if (offs
&& offs
< win
)
3626 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3627 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3628 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3631 /* preallocation can change ac_b_ex, thus we store actually
3632 * allocated blocks for history */
3633 ac
->ac_f_ex
= ac
->ac_b_ex
;
3635 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3636 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3637 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3638 pa
->pa_free
= pa
->pa_len
;
3639 atomic_set(&pa
->pa_count
, 1);
3640 spin_lock_init(&pa
->pa_lock
);
3641 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3642 INIT_LIST_HEAD(&pa
->pa_group_list
);
3644 pa
->pa_type
= MB_INODE_PA
;
3646 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3647 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3648 trace_ext4_mb_new_inode_pa(ac
, pa
);
3650 ext4_mb_use_inode_pa(ac
, pa
);
3651 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3653 ei
= EXT4_I(ac
->ac_inode
);
3654 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3656 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3657 pa
->pa_inode
= ac
->ac_inode
;
3659 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3660 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3661 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3663 spin_lock(pa
->pa_obj_lock
);
3664 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3665 spin_unlock(pa
->pa_obj_lock
);
3671 * creates new preallocated space for locality group inodes belongs to
3673 static noinline_for_stack
int
3674 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3676 struct super_block
*sb
= ac
->ac_sb
;
3677 struct ext4_locality_group
*lg
;
3678 struct ext4_prealloc_space
*pa
;
3679 struct ext4_group_info
*grp
;
3681 /* preallocate only when found space is larger then requested */
3682 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3683 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3684 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3686 BUG_ON(ext4_pspace_cachep
== NULL
);
3687 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3691 /* preallocation can change ac_b_ex, thus we store actually
3692 * allocated blocks for history */
3693 ac
->ac_f_ex
= ac
->ac_b_ex
;
3695 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3696 pa
->pa_lstart
= pa
->pa_pstart
;
3697 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3698 pa
->pa_free
= pa
->pa_len
;
3699 atomic_set(&pa
->pa_count
, 1);
3700 spin_lock_init(&pa
->pa_lock
);
3701 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3702 INIT_LIST_HEAD(&pa
->pa_group_list
);
3704 pa
->pa_type
= MB_GROUP_PA
;
3706 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3707 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3708 trace_ext4_mb_new_group_pa(ac
, pa
);
3710 ext4_mb_use_group_pa(ac
, pa
);
3711 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3713 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3717 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3718 pa
->pa_inode
= NULL
;
3720 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3721 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3722 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3725 * We will later add the new pa to the right bucket
3726 * after updating the pa_free in ext4_mb_release_context
3731 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3735 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3736 err
= ext4_mb_new_group_pa(ac
);
3738 err
= ext4_mb_new_inode_pa(ac
);
3743 * finds all unused blocks in on-disk bitmap, frees them in
3744 * in-core bitmap and buddy.
3745 * @pa must be unlinked from inode and group lists, so that
3746 * nobody else can find/use it.
3747 * the caller MUST hold group/inode locks.
3748 * TODO: optimize the case when there are no in-core structures yet
3750 static noinline_for_stack
int
3751 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3752 struct ext4_prealloc_space
*pa
,
3753 struct ext4_allocation_context
*ac
)
3755 struct super_block
*sb
= e4b
->bd_sb
;
3756 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3761 unsigned long long grp_blk_start
;
3766 BUG_ON(pa
->pa_deleted
== 0);
3767 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3768 grp_blk_start
= pa
->pa_pstart
- bit
;
3769 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3770 end
= bit
+ pa
->pa_len
;
3774 ac
->ac_inode
= pa
->pa_inode
;
3775 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3779 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3782 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3783 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3784 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3785 mb_debug(" free preallocated %u/%u in group %u\n",
3786 (unsigned) start
, (unsigned) next
- bit
,
3791 ac
->ac_b_ex
.fe_group
= group
;
3792 ac
->ac_b_ex
.fe_start
= bit
;
3793 ac
->ac_b_ex
.fe_len
= next
- bit
;
3794 ac
->ac_b_ex
.fe_logical
= 0;
3795 ext4_mb_store_history(ac
);
3798 trace_ext4_mb_release_inode_pa(ac
, pa
, grp_blk_start
+ bit
,
3800 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3803 if (free
!= pa
->pa_free
) {
3804 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3805 pa
, (unsigned long) pa
->pa_lstart
,
3806 (unsigned long) pa
->pa_pstart
,
3807 (unsigned long) pa
->pa_len
);
3808 ext4_grp_locked_error(sb
, group
,
3809 __func__
, "free %u, pa_free %u",
3812 * pa is already deleted so we use the value obtained
3813 * from the bitmap and continue.
3816 atomic_add(free
, &sbi
->s_mb_discarded
);
3821 static noinline_for_stack
int
3822 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3823 struct ext4_prealloc_space
*pa
,
3824 struct ext4_allocation_context
*ac
)
3826 struct super_block
*sb
= e4b
->bd_sb
;
3831 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3833 trace_ext4_mb_release_group_pa(ac
, pa
);
3834 BUG_ON(pa
->pa_deleted
== 0);
3835 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3836 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3837 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3838 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3842 ac
->ac_inode
= NULL
;
3843 ac
->ac_b_ex
.fe_group
= group
;
3844 ac
->ac_b_ex
.fe_start
= bit
;
3845 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3846 ac
->ac_b_ex
.fe_logical
= 0;
3847 ext4_mb_store_history(ac
);
3854 * releases all preallocations in given group
3856 * first, we need to decide discard policy:
3857 * - when do we discard
3859 * - how many do we discard
3860 * 1) how many requested
3862 static noinline_for_stack
int
3863 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3864 ext4_group_t group
, int needed
)
3866 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3867 struct buffer_head
*bitmap_bh
= NULL
;
3868 struct ext4_prealloc_space
*pa
, *tmp
;
3869 struct ext4_allocation_context
*ac
;
3870 struct list_head list
;
3871 struct ext4_buddy e4b
;
3876 mb_debug("discard preallocation for group %u\n", group
);
3878 if (list_empty(&grp
->bb_prealloc_list
))
3881 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3882 if (bitmap_bh
== NULL
) {
3883 ext4_error(sb
, __func__
, "Error in reading block "
3884 "bitmap for %u", group
);
3888 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3890 ext4_error(sb
, __func__
, "Error in loading buddy "
3891 "information for %u", group
);
3897 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3899 INIT_LIST_HEAD(&list
);
3900 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3904 ext4_lock_group(sb
, group
);
3905 list_for_each_entry_safe(pa
, tmp
,
3906 &grp
->bb_prealloc_list
, pa_group_list
) {
3907 spin_lock(&pa
->pa_lock
);
3908 if (atomic_read(&pa
->pa_count
)) {
3909 spin_unlock(&pa
->pa_lock
);
3913 if (pa
->pa_deleted
) {
3914 spin_unlock(&pa
->pa_lock
);
3918 /* seems this one can be freed ... */
3921 /* we can trust pa_free ... */
3922 free
+= pa
->pa_free
;
3924 spin_unlock(&pa
->pa_lock
);
3926 list_del(&pa
->pa_group_list
);
3927 list_add(&pa
->u
.pa_tmp_list
, &list
);
3930 /* if we still need more blocks and some PAs were used, try again */
3931 if (free
< needed
&& busy
) {
3933 ext4_unlock_group(sb
, group
);
3935 * Yield the CPU here so that we don't get soft lockup
3936 * in non preempt case.
3942 /* found anything to free? */
3943 if (list_empty(&list
)) {
3948 /* now free all selected PAs */
3949 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3951 /* remove from object (inode or locality group) */
3952 spin_lock(pa
->pa_obj_lock
);
3953 list_del_rcu(&pa
->pa_inode_list
);
3954 spin_unlock(pa
->pa_obj_lock
);
3956 if (pa
->pa_type
== MB_GROUP_PA
)
3957 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3959 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3961 list_del(&pa
->u
.pa_tmp_list
);
3962 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3966 ext4_unlock_group(sb
, group
);
3968 kmem_cache_free(ext4_ac_cachep
, ac
);
3969 ext4_mb_release_desc(&e4b
);
3975 * releases all non-used preallocated blocks for given inode
3977 * It's important to discard preallocations under i_data_sem
3978 * We don't want another block to be served from the prealloc
3979 * space when we are discarding the inode prealloc space.
3981 * FIXME!! Make sure it is valid at all the call sites
3983 void ext4_discard_preallocations(struct inode
*inode
)
3985 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3986 struct super_block
*sb
= inode
->i_sb
;
3987 struct buffer_head
*bitmap_bh
= NULL
;
3988 struct ext4_prealloc_space
*pa
, *tmp
;
3989 struct ext4_allocation_context
*ac
;
3990 ext4_group_t group
= 0;
3991 struct list_head list
;
3992 struct ext4_buddy e4b
;
3995 if (!S_ISREG(inode
->i_mode
)) {
3996 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4000 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
4001 trace_ext4_discard_preallocations(inode
);
4003 INIT_LIST_HEAD(&list
);
4005 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4008 ac
->ac_inode
= inode
;
4011 /* first, collect all pa's in the inode */
4012 spin_lock(&ei
->i_prealloc_lock
);
4013 while (!list_empty(&ei
->i_prealloc_list
)) {
4014 pa
= list_entry(ei
->i_prealloc_list
.next
,
4015 struct ext4_prealloc_space
, pa_inode_list
);
4016 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4017 spin_lock(&pa
->pa_lock
);
4018 if (atomic_read(&pa
->pa_count
)) {
4019 /* this shouldn't happen often - nobody should
4020 * use preallocation while we're discarding it */
4021 spin_unlock(&pa
->pa_lock
);
4022 spin_unlock(&ei
->i_prealloc_lock
);
4023 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
4025 schedule_timeout_uninterruptible(HZ
);
4029 if (pa
->pa_deleted
== 0) {
4031 spin_unlock(&pa
->pa_lock
);
4032 list_del_rcu(&pa
->pa_inode_list
);
4033 list_add(&pa
->u
.pa_tmp_list
, &list
);
4037 /* someone is deleting pa right now */
4038 spin_unlock(&pa
->pa_lock
);
4039 spin_unlock(&ei
->i_prealloc_lock
);
4041 /* we have to wait here because pa_deleted
4042 * doesn't mean pa is already unlinked from
4043 * the list. as we might be called from
4044 * ->clear_inode() the inode will get freed
4045 * and concurrent thread which is unlinking
4046 * pa from inode's list may access already
4047 * freed memory, bad-bad-bad */
4049 /* XXX: if this happens too often, we can
4050 * add a flag to force wait only in case
4051 * of ->clear_inode(), but not in case of
4052 * regular truncate */
4053 schedule_timeout_uninterruptible(HZ
);
4056 spin_unlock(&ei
->i_prealloc_lock
);
4058 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4059 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4060 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4062 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4064 ext4_error(sb
, __func__
, "Error in loading buddy "
4065 "information for %u", group
);
4069 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4070 if (bitmap_bh
== NULL
) {
4071 ext4_error(sb
, __func__
, "Error in reading block "
4072 "bitmap for %u", group
);
4073 ext4_mb_release_desc(&e4b
);
4077 ext4_lock_group(sb
, group
);
4078 list_del(&pa
->pa_group_list
);
4079 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
4080 ext4_unlock_group(sb
, group
);
4082 ext4_mb_release_desc(&e4b
);
4085 list_del(&pa
->u
.pa_tmp_list
);
4086 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4089 kmem_cache_free(ext4_ac_cachep
, ac
);
4093 * finds all preallocated spaces and return blocks being freed to them
4094 * if preallocated space becomes full (no block is used from the space)
4095 * then the function frees space in buddy
4096 * XXX: at the moment, truncate (which is the only way to free blocks)
4097 * discards all preallocations
4099 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4100 struct ext4_buddy
*e4b
,
4101 sector_t block
, int count
)
4103 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4106 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4108 struct super_block
*sb
= ac
->ac_sb
;
4109 ext4_group_t ngroups
, i
;
4111 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4112 " Allocation context details:\n");
4113 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4114 ac
->ac_status
, ac
->ac_flags
);
4115 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4116 "best %lu/%lu/%lu@%lu cr %d\n",
4117 (unsigned long)ac
->ac_o_ex
.fe_group
,
4118 (unsigned long)ac
->ac_o_ex
.fe_start
,
4119 (unsigned long)ac
->ac_o_ex
.fe_len
,
4120 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4121 (unsigned long)ac
->ac_g_ex
.fe_group
,
4122 (unsigned long)ac
->ac_g_ex
.fe_start
,
4123 (unsigned long)ac
->ac_g_ex
.fe_len
,
4124 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4125 (unsigned long)ac
->ac_b_ex
.fe_group
,
4126 (unsigned long)ac
->ac_b_ex
.fe_start
,
4127 (unsigned long)ac
->ac_b_ex
.fe_len
,
4128 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4129 (int)ac
->ac_criteria
);
4130 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4132 printk(KERN_ERR
"EXT4-fs: groups: \n");
4133 ngroups
= ext4_get_groups_count(sb
);
4134 for (i
= 0; i
< ngroups
; i
++) {
4135 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4136 struct ext4_prealloc_space
*pa
;
4137 ext4_grpblk_t start
;
4138 struct list_head
*cur
;
4139 ext4_lock_group(sb
, i
);
4140 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4141 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4143 spin_lock(&pa
->pa_lock
);
4144 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4146 spin_unlock(&pa
->pa_lock
);
4147 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4150 ext4_unlock_group(sb
, i
);
4152 if (grp
->bb_free
== 0)
4154 printk(KERN_ERR
"%lu: %d/%d \n",
4155 i
, grp
->bb_free
, grp
->bb_fragments
);
4157 printk(KERN_ERR
"\n");
4160 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4167 * We use locality group preallocation for small size file. The size of the
4168 * file is determined by the current size or the resulting size after
4169 * allocation which ever is larger
4171 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4173 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4175 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4176 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4179 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4182 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4185 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4186 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
4189 if ((size
== isize
) &&
4190 !ext4_fs_is_busy(sbi
) &&
4191 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
4192 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
4196 /* don't use group allocation for large files */
4197 size
= max(size
, isize
);
4198 if (size
>= sbi
->s_mb_stream_request
) {
4199 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4203 BUG_ON(ac
->ac_lg
!= NULL
);
4205 * locality group prealloc space are per cpu. The reason for having
4206 * per cpu locality group is to reduce the contention between block
4207 * request from multiple CPUs.
4209 ac
->ac_lg
= per_cpu_ptr(sbi
->s_locality_groups
, raw_smp_processor_id());
4211 /* we're going to use group allocation */
4212 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4214 /* serialize all allocations in the group */
4215 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4218 static noinline_for_stack
int
4219 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4220 struct ext4_allocation_request
*ar
)
4222 struct super_block
*sb
= ar
->inode
->i_sb
;
4223 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4224 struct ext4_super_block
*es
= sbi
->s_es
;
4228 ext4_grpblk_t block
;
4230 /* we can't allocate > group size */
4233 /* just a dirty hack to filter too big requests */
4234 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4235 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4237 /* start searching from the goal */
4239 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4240 goal
>= ext4_blocks_count(es
))
4241 goal
= le32_to_cpu(es
->s_first_data_block
);
4242 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4244 /* set up allocation goals */
4245 memset(ac
, 0, sizeof(struct ext4_allocation_context
));
4246 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4247 ac
->ac_status
= AC_STATUS_CONTINUE
;
4249 ac
->ac_inode
= ar
->inode
;
4250 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4251 ac
->ac_o_ex
.fe_group
= group
;
4252 ac
->ac_o_ex
.fe_start
= block
;
4253 ac
->ac_o_ex
.fe_len
= len
;
4254 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4255 ac
->ac_g_ex
.fe_group
= group
;
4256 ac
->ac_g_ex
.fe_start
= block
;
4257 ac
->ac_g_ex
.fe_len
= len
;
4258 ac
->ac_flags
= ar
->flags
;
4260 /* we have to define context: we'll we work with a file or
4261 * locality group. this is a policy, actually */
4262 ext4_mb_group_or_file(ac
);
4264 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4265 "left: %u/%u, right %u/%u to %swritable\n",
4266 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4267 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4268 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4269 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4270 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4275 static noinline_for_stack
void
4276 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4277 struct ext4_locality_group
*lg
,
4278 int order
, int total_entries
)
4280 ext4_group_t group
= 0;
4281 struct ext4_buddy e4b
;
4282 struct list_head discard_list
;
4283 struct ext4_prealloc_space
*pa
, *tmp
;
4284 struct ext4_allocation_context
*ac
;
4286 mb_debug("discard locality group preallocation\n");
4288 INIT_LIST_HEAD(&discard_list
);
4289 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4293 spin_lock(&lg
->lg_prealloc_lock
);
4294 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4296 spin_lock(&pa
->pa_lock
);
4297 if (atomic_read(&pa
->pa_count
)) {
4299 * This is the pa that we just used
4300 * for block allocation. So don't
4303 spin_unlock(&pa
->pa_lock
);
4306 if (pa
->pa_deleted
) {
4307 spin_unlock(&pa
->pa_lock
);
4310 /* only lg prealloc space */
4311 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4313 /* seems this one can be freed ... */
4315 spin_unlock(&pa
->pa_lock
);
4317 list_del_rcu(&pa
->pa_inode_list
);
4318 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4321 if (total_entries
<= 5) {
4323 * we want to keep only 5 entries
4324 * allowing it to grow to 8. This
4325 * mak sure we don't call discard
4326 * soon for this list.
4331 spin_unlock(&lg
->lg_prealloc_lock
);
4333 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4335 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4336 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4337 ext4_error(sb
, __func__
, "Error in loading buddy "
4338 "information for %u", group
);
4341 ext4_lock_group(sb
, group
);
4342 list_del(&pa
->pa_group_list
);
4343 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4344 ext4_unlock_group(sb
, group
);
4346 ext4_mb_release_desc(&e4b
);
4347 list_del(&pa
->u
.pa_tmp_list
);
4348 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4351 kmem_cache_free(ext4_ac_cachep
, ac
);
4355 * We have incremented pa_count. So it cannot be freed at this
4356 * point. Also we hold lg_mutex. So no parallel allocation is
4357 * possible from this lg. That means pa_free cannot be updated.
4359 * A parallel ext4_mb_discard_group_preallocations is possible.
4360 * which can cause the lg_prealloc_list to be updated.
4363 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4365 int order
, added
= 0, lg_prealloc_count
= 1;
4366 struct super_block
*sb
= ac
->ac_sb
;
4367 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4368 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4370 order
= fls(pa
->pa_free
) - 1;
4371 if (order
> PREALLOC_TB_SIZE
- 1)
4372 /* The max size of hash table is PREALLOC_TB_SIZE */
4373 order
= PREALLOC_TB_SIZE
- 1;
4374 /* Add the prealloc space to lg */
4376 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4378 spin_lock(&tmp_pa
->pa_lock
);
4379 if (tmp_pa
->pa_deleted
) {
4380 spin_unlock(&tmp_pa
->pa_lock
);
4383 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4384 /* Add to the tail of the previous entry */
4385 list_add_tail_rcu(&pa
->pa_inode_list
,
4386 &tmp_pa
->pa_inode_list
);
4389 * we want to count the total
4390 * number of entries in the list
4393 spin_unlock(&tmp_pa
->pa_lock
);
4394 lg_prealloc_count
++;
4397 list_add_tail_rcu(&pa
->pa_inode_list
,
4398 &lg
->lg_prealloc_list
[order
]);
4401 /* Now trim the list to be not more than 8 elements */
4402 if (lg_prealloc_count
> 8) {
4403 ext4_mb_discard_lg_preallocations(sb
, lg
,
4404 order
, lg_prealloc_count
);
4411 * release all resource we used in allocation
4413 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4415 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4417 if (pa
->pa_type
== MB_GROUP_PA
) {
4418 /* see comment in ext4_mb_use_group_pa() */
4419 spin_lock(&pa
->pa_lock
);
4420 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4421 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4422 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4423 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4424 spin_unlock(&pa
->pa_lock
);
4428 up_read(ac
->alloc_semp
);
4431 * We want to add the pa to the right bucket.
4432 * Remove it from the list and while adding
4433 * make sure the list to which we are adding
4434 * doesn't grow big. We need to release
4435 * alloc_semp before calling ext4_mb_add_n_trim()
4437 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4438 spin_lock(pa
->pa_obj_lock
);
4439 list_del_rcu(&pa
->pa_inode_list
);
4440 spin_unlock(pa
->pa_obj_lock
);
4441 ext4_mb_add_n_trim(ac
);
4443 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4445 if (ac
->ac_bitmap_page
)
4446 page_cache_release(ac
->ac_bitmap_page
);
4447 if (ac
->ac_buddy_page
)
4448 page_cache_release(ac
->ac_buddy_page
);
4449 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4450 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4451 ext4_mb_collect_stats(ac
);
4455 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4457 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4461 trace_ext4_mb_discard_preallocations(sb
, needed
);
4462 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4463 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4472 * Main entry point into mballoc to allocate blocks
4473 * it tries to use preallocation first, then falls back
4474 * to usual allocation
4476 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4477 struct ext4_allocation_request
*ar
, int *errp
)
4480 struct ext4_allocation_context
*ac
= NULL
;
4481 struct ext4_sb_info
*sbi
;
4482 struct super_block
*sb
;
4483 ext4_fsblk_t block
= 0;
4484 unsigned int inquota
= 0;
4485 unsigned int reserv_blks
= 0;
4487 sb
= ar
->inode
->i_sb
;
4490 trace_ext4_request_blocks(ar
);
4493 * For delayed allocation, we could skip the ENOSPC and
4494 * EDQUOT check, as blocks and quotas have been already
4495 * reserved when data being copied into pagecache.
4497 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4498 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4500 /* Without delayed allocation we need to verify
4501 * there is enough free blocks to do block allocation
4502 * and verify allocation doesn't exceed the quota limits.
4504 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4505 /* let others to free the space */
4507 ar
->len
= ar
->len
>> 1;
4513 reserv_blks
= ar
->len
;
4514 while (ar
->len
&& vfs_dq_alloc_block(ar
->inode
, ar
->len
)) {
4515 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4525 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4532 *errp
= ext4_mb_initialize_context(ac
, ar
);
4538 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4539 if (!ext4_mb_use_preallocated(ac
)) {
4540 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4541 ext4_mb_normalize_request(ac
, ar
);
4543 /* allocate space in core */
4544 ext4_mb_regular_allocator(ac
);
4546 /* as we've just preallocated more space than
4547 * user requested orinally, we store allocated
4548 * space in a special descriptor */
4549 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4550 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4551 ext4_mb_new_preallocation(ac
);
4553 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4554 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4555 if (*errp
== -EAGAIN
) {
4557 * drop the reference that we took
4558 * in ext4_mb_use_best_found
4560 ext4_mb_release_context(ac
);
4561 ac
->ac_b_ex
.fe_group
= 0;
4562 ac
->ac_b_ex
.fe_start
= 0;
4563 ac
->ac_b_ex
.fe_len
= 0;
4564 ac
->ac_status
= AC_STATUS_CONTINUE
;
4567 ext4_discard_allocated_blocks(ac
);
4568 ac
->ac_b_ex
.fe_len
= 0;
4570 ext4_mb_show_ac(ac
);
4572 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4573 ar
->len
= ac
->ac_b_ex
.fe_len
;
4576 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4580 ac
->ac_b_ex
.fe_len
= 0;
4582 ext4_mb_show_ac(ac
);
4585 ext4_mb_release_context(ac
);
4588 kmem_cache_free(ext4_ac_cachep
, ac
);
4590 if (inquota
&& ar
->len
< inquota
)
4591 vfs_dq_free_block(ar
->inode
, inquota
- ar
->len
);
4594 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4595 /* release all the reserved blocks if non delalloc */
4596 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4600 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4606 * We can merge two free data extents only if the physical blocks
4607 * are contiguous, AND the extents were freed by the same transaction,
4608 * AND the blocks are associated with the same group.
4610 static int can_merge(struct ext4_free_data
*entry1
,
4611 struct ext4_free_data
*entry2
)
4613 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4614 (entry1
->group
== entry2
->group
) &&
4615 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4620 static noinline_for_stack
int
4621 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4622 struct ext4_free_data
*new_entry
)
4624 ext4_grpblk_t block
;
4625 struct ext4_free_data
*entry
;
4626 struct ext4_group_info
*db
= e4b
->bd_info
;
4627 struct super_block
*sb
= e4b
->bd_sb
;
4628 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4629 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4630 struct rb_node
*parent
= NULL
, *new_node
;
4632 BUG_ON(!ext4_handle_valid(handle
));
4633 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4634 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4636 new_node
= &new_entry
->node
;
4637 block
= new_entry
->start_blk
;
4640 /* first free block exent. We need to
4641 protect buddy cache from being freed,
4642 * otherwise we'll refresh it from
4643 * on-disk bitmap and lose not-yet-available
4645 page_cache_get(e4b
->bd_buddy_page
);
4646 page_cache_get(e4b
->bd_bitmap_page
);
4650 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4651 if (block
< entry
->start_blk
)
4653 else if (block
>= (entry
->start_blk
+ entry
->count
))
4654 n
= &(*n
)->rb_right
;
4656 ext4_grp_locked_error(sb
, e4b
->bd_group
, __func__
,
4657 "Double free of blocks %d (%d %d)",
4658 block
, entry
->start_blk
, entry
->count
);
4663 rb_link_node(new_node
, parent
, n
);
4664 rb_insert_color(new_node
, &db
->bb_free_root
);
4666 /* Now try to see the extent can be merged to left and right */
4667 node
= rb_prev(new_node
);
4669 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4670 if (can_merge(entry
, new_entry
)) {
4671 new_entry
->start_blk
= entry
->start_blk
;
4672 new_entry
->count
+= entry
->count
;
4673 rb_erase(node
, &(db
->bb_free_root
));
4674 spin_lock(&sbi
->s_md_lock
);
4675 list_del(&entry
->list
);
4676 spin_unlock(&sbi
->s_md_lock
);
4677 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4681 node
= rb_next(new_node
);
4683 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4684 if (can_merge(new_entry
, entry
)) {
4685 new_entry
->count
+= entry
->count
;
4686 rb_erase(node
, &(db
->bb_free_root
));
4687 spin_lock(&sbi
->s_md_lock
);
4688 list_del(&entry
->list
);
4689 spin_unlock(&sbi
->s_md_lock
);
4690 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4693 /* Add the extent to transaction's private list */
4694 spin_lock(&sbi
->s_md_lock
);
4695 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4696 spin_unlock(&sbi
->s_md_lock
);
4701 * Main entry point into mballoc to free blocks
4703 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4704 ext4_fsblk_t block
, unsigned long count
,
4705 int metadata
, unsigned long *freed
)
4707 struct buffer_head
*bitmap_bh
= NULL
;
4708 struct super_block
*sb
= inode
->i_sb
;
4709 struct ext4_allocation_context
*ac
= NULL
;
4710 struct ext4_group_desc
*gdp
;
4711 struct ext4_super_block
*es
;
4712 unsigned int overflow
;
4714 struct buffer_head
*gd_bh
;
4715 ext4_group_t block_group
;
4716 struct ext4_sb_info
*sbi
;
4717 struct ext4_buddy e4b
;
4724 es
= EXT4_SB(sb
)->s_es
;
4725 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4726 block
+ count
< block
||
4727 block
+ count
> ext4_blocks_count(es
)) {
4728 ext4_error(sb
, __func__
,
4729 "Freeing blocks not in datazone - "
4730 "block = %llu, count = %lu", block
, count
);
4734 ext4_debug("freeing block %llu\n", block
);
4735 trace_ext4_free_blocks(inode
, block
, count
, metadata
);
4737 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4739 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4740 ac
->ac_inode
= inode
;
4746 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4749 * Check to see if we are freeing blocks across a group
4752 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4753 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4756 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4761 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4767 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4768 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4769 in_range(block
, ext4_inode_table(sb
, gdp
),
4770 EXT4_SB(sb
)->s_itb_per_group
) ||
4771 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4772 EXT4_SB(sb
)->s_itb_per_group
)) {
4774 ext4_error(sb
, __func__
,
4775 "Freeing blocks in system zone - "
4776 "Block = %llu, count = %lu", block
, count
);
4777 /* err = 0. ext4_std_error should be a no op */
4781 BUFFER_TRACE(bitmap_bh
, "getting write access");
4782 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4787 * We are about to modify some metadata. Call the journal APIs
4788 * to unshare ->b_data if a currently-committing transaction is
4791 BUFFER_TRACE(gd_bh
, "get_write_access");
4792 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4795 #ifdef AGGRESSIVE_CHECK
4798 for (i
= 0; i
< count
; i
++)
4799 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4803 ac
->ac_b_ex
.fe_group
= block_group
;
4804 ac
->ac_b_ex
.fe_start
= bit
;
4805 ac
->ac_b_ex
.fe_len
= count
;
4806 ext4_mb_store_history(ac
);
4809 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4812 if (metadata
&& ext4_handle_valid(handle
)) {
4813 struct ext4_free_data
*new_entry
;
4815 * blocks being freed are metadata. these blocks shouldn't
4816 * be used until this transaction is committed
4818 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4819 new_entry
->start_blk
= bit
;
4820 new_entry
->group
= block_group
;
4821 new_entry
->count
= count
;
4822 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4824 ext4_lock_group(sb
, block_group
);
4825 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4826 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4828 /* need to update group_info->bb_free and bitmap
4829 * with group lock held. generate_buddy look at
4830 * them with group lock_held
4832 ext4_lock_group(sb
, block_group
);
4833 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4834 mb_free_blocks(inode
, &e4b
, bit
, count
);
4835 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4838 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4839 ext4_free_blks_set(sb
, gdp
, ret
);
4840 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4841 ext4_unlock_group(sb
, block_group
);
4842 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4844 if (sbi
->s_log_groups_per_flex
) {
4845 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4846 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4849 ext4_mb_release_desc(&e4b
);
4853 /* We dirtied the bitmap block */
4854 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4855 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4857 /* And the group descriptor block */
4858 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4859 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4863 if (overflow
&& !err
) {
4872 ext4_std_error(sb
, err
);
4874 kmem_cache_free(ext4_ac_cachep
, ac
);