2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 * mballoc.c contains the multiblocks allocation routines
25 #include <linux/debugfs.h>
26 #include <linux/slab.h>
27 #include <trace/events/ext4.h>
31 * - test ext4_ext_search_left() and ext4_ext_search_right()
32 * - search for metadata in few groups
35 * - normalization should take into account whether file is still open
36 * - discard preallocations if no free space left (policy?)
37 * - don't normalize tails
39 * - reservation for superuser
42 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
43 * - track min/max extents in each group for better group selection
44 * - mb_mark_used() may allocate chunk right after splitting buddy
45 * - tree of groups sorted by number of free blocks
50 * The allocation request involve request for multiple number of blocks
51 * near to the goal(block) value specified.
53 * During initialization phase of the allocator we decide to use the
54 * group preallocation or inode preallocation depending on the size of
55 * the file. The size of the file could be the resulting file size we
56 * would have after allocation, or the current file size, which ever
57 * is larger. If the size is less than sbi->s_mb_stream_request we
58 * select to use the group preallocation. The default value of
59 * s_mb_stream_request is 16 blocks. This can also be tuned via
60 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
61 * terms of number of blocks.
63 * The main motivation for having small file use group preallocation is to
64 * ensure that we have small files closer together on the disk.
66 * First stage the allocator looks at the inode prealloc list,
67 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
68 * spaces for this particular inode. The inode prealloc space is
71 * pa_lstart -> the logical start block for this prealloc space
72 * pa_pstart -> the physical start block for this prealloc space
73 * pa_len -> length for this prealloc space
74 * pa_free -> free space available in this prealloc space
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
78 * space we will consume the particular prealloc space. This make sure that
79 * that the we have contiguous physical blocks representing the file blocks
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
87 * prealloc space. These are per CPU prealloc list repreasented as
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
94 * The locality group prealloc space is used looking at whether we have
95 * enough free space (pa_free) within the prealloc space.
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
129 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
130 * 512 blocks. This can be tuned via
131 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
132 * terms of number of blocks. If we have mounted the file system with -O
133 * stripe=<value> option the group prealloc request is normalized to the
134 * stripe value (sbi->s_stripe)
136 * The regular allocator(using the buddy cache) supports few tunables.
138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
142 * The regular allocator uses buddy scan only if the request len is power of
143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
150 * min_to_scan indicate how long the mballoc __must__ look for a best
151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
155 * can used for allocation. ext4_mb_good_group explains how the groups are
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
165 * mballoc operates on the following data:
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
170 * there are two types of preallocations:
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
223 * so, now we're building a concurrency table:
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
239 * i_data_sem serializes them
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
248 * i_data_sem or another mutex should serializes them
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
270 * Logic in few words:
275 * mark bits in on-disk bitmap
278 * - use preallocation:
279 * find proper PA (per-inode or group)
281 * mark bits in on-disk bitmap
287 * mark bits in on-disk bitmap
290 * - discard preallocations in group:
292 * move them onto local list
293 * load on-disk bitmap
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
298 * - discard inode's preallocations:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
317 * - release consumed pa:
322 * - generate in-core bitmap:
326 * - discard all for given object (inode, locality group):
331 * - discard all for given group:
338 static struct kmem_cache
*ext4_pspace_cachep
;
339 static struct kmem_cache
*ext4_ac_cachep
;
340 static struct kmem_cache
*ext4_free_ext_cachep
;
342 /* We create slab caches for groupinfo data structures based on the
343 * superblock block size. There will be one per mounted filesystem for
344 * each unique s_blocksize_bits */
345 #define NR_GRPINFO_CACHES 8
346 static struct kmem_cache
*ext4_groupinfo_caches
[NR_GRPINFO_CACHES
];
348 static const char *ext4_groupinfo_slab_names
[NR_GRPINFO_CACHES
] = {
349 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
350 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
351 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
354 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
356 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
358 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
360 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
362 #if BITS_PER_LONG == 64
363 *bit
+= ((unsigned long) addr
& 7UL) << 3;
364 addr
= (void *) ((unsigned long) addr
& ~7UL);
365 #elif BITS_PER_LONG == 32
366 *bit
+= ((unsigned long) addr
& 3UL) << 3;
367 addr
= (void *) ((unsigned long) addr
& ~3UL);
369 #error "how many bits you are?!"
374 static inline int mb_test_bit(int bit
, void *addr
)
377 * ext4_test_bit on architecture like powerpc
378 * needs unsigned long aligned address
380 addr
= mb_correct_addr_and_bit(&bit
, addr
);
381 return ext4_test_bit(bit
, addr
);
384 static inline void mb_set_bit(int bit
, void *addr
)
386 addr
= mb_correct_addr_and_bit(&bit
, addr
);
387 ext4_set_bit(bit
, addr
);
390 static inline void mb_clear_bit(int bit
, void *addr
)
392 addr
= mb_correct_addr_and_bit(&bit
, addr
);
393 ext4_clear_bit(bit
, addr
);
396 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
398 int fix
= 0, ret
, tmpmax
;
399 addr
= mb_correct_addr_and_bit(&fix
, addr
);
403 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
409 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
411 int fix
= 0, ret
, tmpmax
;
412 addr
= mb_correct_addr_and_bit(&fix
, addr
);
416 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
422 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
426 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
429 if (order
> e4b
->bd_blkbits
+ 1) {
434 /* at order 0 we see each particular block */
436 *max
= 1 << (e4b
->bd_blkbits
+ 3);
437 return EXT4_MB_BITMAP(e4b
);
440 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
441 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
447 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
448 int first
, int count
)
451 struct super_block
*sb
= e4b
->bd_sb
;
453 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
455 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
456 for (i
= 0; i
< count
; i
++) {
457 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
458 ext4_fsblk_t blocknr
;
460 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
461 blocknr
+= first
+ i
;
462 ext4_grp_locked_error(sb
, e4b
->bd_group
,
463 inode
? inode
->i_ino
: 0,
465 "freeing block already freed "
469 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
473 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
477 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
479 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
480 for (i
= 0; i
< count
; i
++) {
481 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
482 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
486 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
488 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
489 unsigned char *b1
, *b2
;
491 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
492 b2
= (unsigned char *) bitmap
;
493 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
494 if (b1
[i
] != b2
[i
]) {
495 printk(KERN_ERR
"corruption in group %u "
496 "at byte %u(%u): %x in copy != %x "
497 "on disk/prealloc\n",
498 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
506 static inline void mb_free_blocks_double(struct inode
*inode
,
507 struct ext4_buddy
*e4b
, int first
, int count
)
511 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
512 int first
, int count
)
516 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
522 #ifdef AGGRESSIVE_CHECK
524 #define MB_CHECK_ASSERT(assert) \
528 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
529 function, file, line, # assert); \
534 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
535 const char *function
, int line
)
537 struct super_block
*sb
= e4b
->bd_sb
;
538 int order
= e4b
->bd_blkbits
+ 1;
545 struct ext4_group_info
*grp
;
548 struct list_head
*cur
;
553 static int mb_check_counter
;
554 if (mb_check_counter
++ % 100 != 0)
559 buddy
= mb_find_buddy(e4b
, order
, &max
);
560 MB_CHECK_ASSERT(buddy
);
561 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
562 MB_CHECK_ASSERT(buddy2
);
563 MB_CHECK_ASSERT(buddy
!= buddy2
);
564 MB_CHECK_ASSERT(max
* 2 == max2
);
567 for (i
= 0; i
< max
; i
++) {
569 if (mb_test_bit(i
, buddy
)) {
570 /* only single bit in buddy2 may be 1 */
571 if (!mb_test_bit(i
<< 1, buddy2
)) {
573 mb_test_bit((i
<<1)+1, buddy2
));
574 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
576 mb_test_bit(i
<< 1, buddy2
));
581 /* both bits in buddy2 must be 0 */
582 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
583 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
585 for (j
= 0; j
< (1 << order
); j
++) {
586 k
= (i
* (1 << order
)) + j
;
588 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
592 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
597 buddy
= mb_find_buddy(e4b
, 0, &max
);
598 for (i
= 0; i
< max
; i
++) {
599 if (!mb_test_bit(i
, buddy
)) {
600 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
608 /* check used bits only */
609 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
610 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
612 MB_CHECK_ASSERT(k
< max2
);
613 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
616 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
617 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
619 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
620 list_for_each(cur
, &grp
->bb_prealloc_list
) {
621 ext4_group_t groupnr
;
622 struct ext4_prealloc_space
*pa
;
623 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
624 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
625 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
626 for (i
= 0; i
< pa
->pa_len
; i
++)
627 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
631 #undef MB_CHECK_ASSERT
632 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
633 __FILE__, __func__, __LINE__)
635 #define mb_check_buddy(e4b)
639 * Divide blocks started from @first with length @len into
640 * smaller chunks with power of 2 blocks.
641 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
642 * then increase bb_counters[] for corresponded chunk size.
644 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
645 void *buddy
, ext4_grpblk_t first
, ext4_grpblk_t len
,
646 struct ext4_group_info
*grp
)
648 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
652 unsigned short border
;
654 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
656 border
= 2 << sb
->s_blocksize_bits
;
659 /* find how many blocks can be covered since this position */
660 max
= ffs(first
| border
) - 1;
662 /* find how many blocks of power 2 we need to mark */
669 /* mark multiblock chunks only */
670 grp
->bb_counters
[min
]++;
672 mb_clear_bit(first
>> min
,
673 buddy
+ sbi
->s_mb_offsets
[min
]);
681 * Cache the order of the largest free extent we have available in this block
685 mb_set_largest_free_order(struct super_block
*sb
, struct ext4_group_info
*grp
)
690 grp
->bb_largest_free_order
= -1; /* uninit */
692 bits
= sb
->s_blocksize_bits
+ 1;
693 for (i
= bits
; i
>= 0; i
--) {
694 if (grp
->bb_counters
[i
] > 0) {
695 grp
->bb_largest_free_order
= i
;
701 static noinline_for_stack
702 void ext4_mb_generate_buddy(struct super_block
*sb
,
703 void *buddy
, void *bitmap
, ext4_group_t group
)
705 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
706 ext4_grpblk_t max
= EXT4_BLOCKS_PER_GROUP(sb
);
711 unsigned fragments
= 0;
712 unsigned long long period
= get_cycles();
714 /* initialize buddy from bitmap which is aggregation
715 * of on-disk bitmap and preallocations */
716 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
717 grp
->bb_first_free
= i
;
721 i
= mb_find_next_bit(bitmap
, max
, i
);
725 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
727 grp
->bb_counters
[0]++;
729 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
731 grp
->bb_fragments
= fragments
;
733 if (free
!= grp
->bb_free
) {
734 ext4_grp_locked_error(sb
, group
, 0, 0,
735 "%u blocks in bitmap, %u in gd",
738 * If we intent to continue, we consider group descritor
739 * corrupt and update bb_free using bitmap value
743 mb_set_largest_free_order(sb
, grp
);
745 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
747 period
= get_cycles() - period
;
748 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
749 EXT4_SB(sb
)->s_mb_buddies_generated
++;
750 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
751 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
754 /* The buddy information is attached the buddy cache inode
755 * for convenience. The information regarding each group
756 * is loaded via ext4_mb_load_buddy. The information involve
757 * block bitmap and buddy information. The information are
758 * stored in the inode as
761 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
764 * one block each for bitmap and buddy information.
765 * So for each group we take up 2 blocks. A page can
766 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
767 * So it can have information regarding groups_per_page which
768 * is blocks_per_page/2
770 * Locking note: This routine takes the block group lock of all groups
771 * for this page; do not hold this lock when calling this routine!
774 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
776 ext4_group_t ngroups
;
782 ext4_group_t first_group
;
784 struct super_block
*sb
;
785 struct buffer_head
*bhs
;
786 struct buffer_head
**bh
;
790 struct ext4_group_info
*grinfo
;
792 mb_debug(1, "init page %lu\n", page
->index
);
794 inode
= page
->mapping
->host
;
796 ngroups
= ext4_get_groups_count(sb
);
797 blocksize
= 1 << inode
->i_blkbits
;
798 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
800 groups_per_page
= blocks_per_page
>> 1;
801 if (groups_per_page
== 0)
804 /* allocate buffer_heads to read bitmaps */
805 if (groups_per_page
> 1) {
807 i
= sizeof(struct buffer_head
*) * groups_per_page
;
808 bh
= kzalloc(i
, GFP_NOFS
);
814 first_group
= page
->index
* blocks_per_page
/ 2;
816 /* read all groups the page covers into the cache */
817 for (i
= 0; i
< groups_per_page
; i
++) {
818 struct ext4_group_desc
*desc
;
820 if (first_group
+ i
>= ngroups
)
823 grinfo
= ext4_get_group_info(sb
, first_group
+ i
);
825 * If page is uptodate then we came here after online resize
826 * which added some new uninitialized group info structs, so
827 * we must skip all initialized uptodate buddies on the page,
828 * which may be currently in use by an allocating task.
830 if (PageUptodate(page
) && !EXT4_MB_GRP_NEED_INIT(grinfo
)) {
836 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
841 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
845 if (bitmap_uptodate(bh
[i
]))
849 if (bitmap_uptodate(bh
[i
])) {
850 unlock_buffer(bh
[i
]);
853 ext4_lock_group(sb
, first_group
+ i
);
854 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
855 ext4_init_block_bitmap(sb
, bh
[i
],
856 first_group
+ i
, desc
);
857 set_bitmap_uptodate(bh
[i
]);
858 set_buffer_uptodate(bh
[i
]);
859 ext4_unlock_group(sb
, first_group
+ i
);
860 unlock_buffer(bh
[i
]);
863 ext4_unlock_group(sb
, first_group
+ i
);
864 if (buffer_uptodate(bh
[i
])) {
866 * if not uninit if bh is uptodate,
867 * bitmap is also uptodate
869 set_bitmap_uptodate(bh
[i
]);
870 unlock_buffer(bh
[i
]);
875 * submit the buffer_head for read. We can
876 * safely mark the bitmap as uptodate now.
877 * We do it here so the bitmap uptodate bit
878 * get set with buffer lock held.
880 set_bitmap_uptodate(bh
[i
]);
881 bh
[i
]->b_end_io
= end_buffer_read_sync
;
882 submit_bh(READ
, bh
[i
]);
883 mb_debug(1, "read bitmap for group %u\n", first_group
+ i
);
886 /* wait for I/O completion */
887 for (i
= 0; i
< groups_per_page
; i
++)
889 wait_on_buffer(bh
[i
]);
892 for (i
= 0; i
< groups_per_page
; i
++)
893 if (bh
[i
] && !buffer_uptodate(bh
[i
]))
897 first_block
= page
->index
* blocks_per_page
;
898 for (i
= 0; i
< blocks_per_page
; i
++) {
901 group
= (first_block
+ i
) >> 1;
902 if (group
>= ngroups
)
905 if (!bh
[group
- first_group
])
906 /* skip initialized uptodate buddy */
910 * data carry information regarding this
911 * particular group in the format specified
915 data
= page_address(page
) + (i
* blocksize
);
916 bitmap
= bh
[group
- first_group
]->b_data
;
919 * We place the buddy block and bitmap block
922 if ((first_block
+ i
) & 1) {
923 /* this is block of buddy */
924 BUG_ON(incore
== NULL
);
925 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
926 group
, page
->index
, i
* blocksize
);
927 trace_ext4_mb_buddy_bitmap_load(sb
, group
);
928 grinfo
= ext4_get_group_info(sb
, group
);
929 grinfo
->bb_fragments
= 0;
930 memset(grinfo
->bb_counters
, 0,
931 sizeof(*grinfo
->bb_counters
) *
932 (sb
->s_blocksize_bits
+2));
934 * incore got set to the group block bitmap below
936 ext4_lock_group(sb
, group
);
938 memset(data
, 0xff, blocksize
);
939 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
940 ext4_unlock_group(sb
, group
);
943 /* this is block of bitmap */
944 BUG_ON(incore
!= NULL
);
945 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
946 group
, page
->index
, i
* blocksize
);
947 trace_ext4_mb_bitmap_load(sb
, group
);
949 /* see comments in ext4_mb_put_pa() */
950 ext4_lock_group(sb
, group
);
951 memcpy(data
, bitmap
, blocksize
);
953 /* mark all preallocated blks used in in-core bitmap */
954 ext4_mb_generate_from_pa(sb
, data
, group
);
955 ext4_mb_generate_from_freelist(sb
, data
, group
);
956 ext4_unlock_group(sb
, group
);
958 /* set incore so that the buddy information can be
959 * generated using this
964 SetPageUptodate(page
);
968 for (i
= 0; i
< groups_per_page
; i
++)
977 * Lock the buddy and bitmap pages. This make sure other parallel init_group
978 * on the same buddy page doesn't happen whild holding the buddy page lock.
979 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
980 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
982 static int ext4_mb_get_buddy_page_lock(struct super_block
*sb
,
983 ext4_group_t group
, struct ext4_buddy
*e4b
)
985 struct inode
*inode
= EXT4_SB(sb
)->s_buddy_cache
;
986 int block
, pnum
, poff
;
990 e4b
->bd_buddy_page
= NULL
;
991 e4b
->bd_bitmap_page
= NULL
;
993 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
995 * the buddy cache inode stores the block bitmap
996 * and buddy information in consecutive blocks.
997 * So for each group we need two blocks.
1000 pnum
= block
/ blocks_per_page
;
1001 poff
= block
% blocks_per_page
;
1002 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1005 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1006 e4b
->bd_bitmap_page
= page
;
1007 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1009 if (blocks_per_page
>= 2) {
1010 /* buddy and bitmap are on the same page */
1015 pnum
= block
/ blocks_per_page
;
1016 poff
= block
% blocks_per_page
;
1017 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1020 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1021 e4b
->bd_buddy_page
= page
;
1025 static void ext4_mb_put_buddy_page_lock(struct ext4_buddy
*e4b
)
1027 if (e4b
->bd_bitmap_page
) {
1028 unlock_page(e4b
->bd_bitmap_page
);
1029 page_cache_release(e4b
->bd_bitmap_page
);
1031 if (e4b
->bd_buddy_page
) {
1032 unlock_page(e4b
->bd_buddy_page
);
1033 page_cache_release(e4b
->bd_buddy_page
);
1038 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1039 * block group lock of all groups for this page; do not hold the BG lock when
1040 * calling this routine!
1042 static noinline_for_stack
1043 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1046 struct ext4_group_info
*this_grp
;
1047 struct ext4_buddy e4b
;
1051 mb_debug(1, "init group %u\n", group
);
1052 this_grp
= ext4_get_group_info(sb
, group
);
1054 * This ensures that we don't reinit the buddy cache
1055 * page which map to the group from which we are already
1056 * allocating. If we are looking at the buddy cache we would
1057 * have taken a reference using ext4_mb_load_buddy and that
1058 * would have pinned buddy page to page cache.
1060 ret
= ext4_mb_get_buddy_page_lock(sb
, group
, &e4b
);
1061 if (ret
|| !EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1063 * somebody initialized the group
1064 * return without doing anything
1069 page
= e4b
.bd_bitmap_page
;
1070 ret
= ext4_mb_init_cache(page
, NULL
);
1073 if (!PageUptodate(page
)) {
1077 mark_page_accessed(page
);
1079 if (e4b
.bd_buddy_page
== NULL
) {
1081 * If both the bitmap and buddy are in
1082 * the same page we don't need to force
1088 /* init buddy cache */
1089 page
= e4b
.bd_buddy_page
;
1090 ret
= ext4_mb_init_cache(page
, e4b
.bd_bitmap
);
1093 if (!PageUptodate(page
)) {
1097 mark_page_accessed(page
);
1099 ext4_mb_put_buddy_page_lock(&e4b
);
1104 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1105 * block group lock of all groups for this page; do not hold the BG lock when
1106 * calling this routine!
1108 static noinline_for_stack
int
1109 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1110 struct ext4_buddy
*e4b
)
1112 int blocks_per_page
;
1118 struct ext4_group_info
*grp
;
1119 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1120 struct inode
*inode
= sbi
->s_buddy_cache
;
1122 mb_debug(1, "load group %u\n", group
);
1124 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1125 grp
= ext4_get_group_info(sb
, group
);
1127 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1128 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
1130 e4b
->bd_group
= group
;
1131 e4b
->bd_buddy_page
= NULL
;
1132 e4b
->bd_bitmap_page
= NULL
;
1134 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1136 * we need full data about the group
1137 * to make a good selection
1139 ret
= ext4_mb_init_group(sb
, group
);
1145 * the buddy cache inode stores the block bitmap
1146 * and buddy information in consecutive blocks.
1147 * So for each group we need two blocks.
1150 pnum
= block
/ blocks_per_page
;
1151 poff
= block
% blocks_per_page
;
1153 /* we could use find_or_create_page(), but it locks page
1154 * what we'd like to avoid in fast path ... */
1155 page
= find_get_page(inode
->i_mapping
, pnum
);
1156 if (page
== NULL
|| !PageUptodate(page
)) {
1159 * drop the page reference and try
1160 * to get the page with lock. If we
1161 * are not uptodate that implies
1162 * somebody just created the page but
1163 * is yet to initialize the same. So
1164 * wait for it to initialize.
1166 page_cache_release(page
);
1167 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1169 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1170 if (!PageUptodate(page
)) {
1171 ret
= ext4_mb_init_cache(page
, NULL
);
1176 mb_cmp_bitmaps(e4b
, page_address(page
) +
1177 (poff
* sb
->s_blocksize
));
1182 if (page
== NULL
|| !PageUptodate(page
)) {
1186 e4b
->bd_bitmap_page
= page
;
1187 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1188 mark_page_accessed(page
);
1191 pnum
= block
/ blocks_per_page
;
1192 poff
= block
% blocks_per_page
;
1194 page
= find_get_page(inode
->i_mapping
, pnum
);
1195 if (page
== NULL
|| !PageUptodate(page
)) {
1197 page_cache_release(page
);
1198 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1200 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1201 if (!PageUptodate(page
)) {
1202 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1211 if (page
== NULL
|| !PageUptodate(page
)) {
1215 e4b
->bd_buddy_page
= page
;
1216 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1217 mark_page_accessed(page
);
1219 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1220 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1226 page_cache_release(page
);
1227 if (e4b
->bd_bitmap_page
)
1228 page_cache_release(e4b
->bd_bitmap_page
);
1229 if (e4b
->bd_buddy_page
)
1230 page_cache_release(e4b
->bd_buddy_page
);
1231 e4b
->bd_buddy
= NULL
;
1232 e4b
->bd_bitmap
= NULL
;
1236 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1238 if (e4b
->bd_bitmap_page
)
1239 page_cache_release(e4b
->bd_bitmap_page
);
1240 if (e4b
->bd_buddy_page
)
1241 page_cache_release(e4b
->bd_buddy_page
);
1245 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1250 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1251 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1253 bb
= EXT4_MB_BUDDY(e4b
);
1254 while (order
<= e4b
->bd_blkbits
+ 1) {
1256 if (!mb_test_bit(block
, bb
)) {
1257 /* this block is part of buddy of order 'order' */
1260 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1266 static void mb_clear_bits(void *bm
, int cur
, int len
)
1272 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1273 /* fast path: clear whole word at once */
1274 addr
= bm
+ (cur
>> 3);
1279 mb_clear_bit(cur
, bm
);
1284 static void mb_set_bits(void *bm
, int cur
, int len
)
1290 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1291 /* fast path: set whole word at once */
1292 addr
= bm
+ (cur
>> 3);
1297 mb_set_bit(cur
, bm
);
1302 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1303 int first
, int count
)
1310 struct super_block
*sb
= e4b
->bd_sb
;
1312 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1313 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1314 mb_check_buddy(e4b
);
1315 mb_free_blocks_double(inode
, e4b
, first
, count
);
1317 e4b
->bd_info
->bb_free
+= count
;
1318 if (first
< e4b
->bd_info
->bb_first_free
)
1319 e4b
->bd_info
->bb_first_free
= first
;
1321 /* let's maintain fragments counter */
1323 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1324 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1325 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1327 e4b
->bd_info
->bb_fragments
--;
1328 else if (!block
&& !max
)
1329 e4b
->bd_info
->bb_fragments
++;
1331 /* let's maintain buddy itself */
1332 while (count
-- > 0) {
1336 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1337 ext4_fsblk_t blocknr
;
1339 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1341 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1342 inode
? inode
->i_ino
: 0,
1344 "freeing already freed block "
1347 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1348 e4b
->bd_info
->bb_counters
[order
]++;
1350 /* start of the buddy */
1351 buddy
= mb_find_buddy(e4b
, order
, &max
);
1355 if (mb_test_bit(block
, buddy
) ||
1356 mb_test_bit(block
+ 1, buddy
))
1359 /* both the buddies are free, try to coalesce them */
1360 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1366 /* for special purposes, we don't set
1367 * free bits in bitmap */
1368 mb_set_bit(block
, buddy
);
1369 mb_set_bit(block
+ 1, buddy
);
1371 e4b
->bd_info
->bb_counters
[order
]--;
1372 e4b
->bd_info
->bb_counters
[order
]--;
1376 e4b
->bd_info
->bb_counters
[order
]++;
1378 mb_clear_bit(block
, buddy2
);
1382 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1383 mb_check_buddy(e4b
);
1386 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1387 int needed
, struct ext4_free_extent
*ex
)
1394 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1397 buddy
= mb_find_buddy(e4b
, order
, &max
);
1398 BUG_ON(buddy
== NULL
);
1399 BUG_ON(block
>= max
);
1400 if (mb_test_bit(block
, buddy
)) {
1407 /* FIXME dorp order completely ? */
1408 if (likely(order
== 0)) {
1409 /* find actual order */
1410 order
= mb_find_order_for_block(e4b
, block
);
1411 block
= block
>> order
;
1414 ex
->fe_len
= 1 << order
;
1415 ex
->fe_start
= block
<< order
;
1416 ex
->fe_group
= e4b
->bd_group
;
1418 /* calc difference from given start */
1419 next
= next
- ex
->fe_start
;
1421 ex
->fe_start
+= next
;
1423 while (needed
> ex
->fe_len
&&
1424 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1426 if (block
+ 1 >= max
)
1429 next
= (block
+ 1) * (1 << order
);
1430 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1433 ord
= mb_find_order_for_block(e4b
, next
);
1436 block
= next
>> order
;
1437 ex
->fe_len
+= 1 << order
;
1440 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1444 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1450 int start
= ex
->fe_start
;
1451 int len
= ex
->fe_len
;
1456 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1457 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1458 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1459 mb_check_buddy(e4b
);
1460 mb_mark_used_double(e4b
, start
, len
);
1462 e4b
->bd_info
->bb_free
-= len
;
1463 if (e4b
->bd_info
->bb_first_free
== start
)
1464 e4b
->bd_info
->bb_first_free
+= len
;
1466 /* let's maintain fragments counter */
1468 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1469 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1470 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1472 e4b
->bd_info
->bb_fragments
++;
1473 else if (!mlen
&& !max
)
1474 e4b
->bd_info
->bb_fragments
--;
1476 /* let's maintain buddy itself */
1478 ord
= mb_find_order_for_block(e4b
, start
);
1480 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1481 /* the whole chunk may be allocated at once! */
1483 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1484 BUG_ON((start
>> ord
) >= max
);
1485 mb_set_bit(start
>> ord
, buddy
);
1486 e4b
->bd_info
->bb_counters
[ord
]--;
1493 /* store for history */
1495 ret
= len
| (ord
<< 16);
1497 /* we have to split large buddy */
1499 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1500 mb_set_bit(start
>> ord
, buddy
);
1501 e4b
->bd_info
->bb_counters
[ord
]--;
1504 cur
= (start
>> ord
) & ~1U;
1505 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1506 mb_clear_bit(cur
, buddy
);
1507 mb_clear_bit(cur
+ 1, buddy
);
1508 e4b
->bd_info
->bb_counters
[ord
]++;
1509 e4b
->bd_info
->bb_counters
[ord
]++;
1511 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1513 mb_set_bits(EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1514 mb_check_buddy(e4b
);
1520 * Must be called under group lock!
1522 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1523 struct ext4_buddy
*e4b
)
1525 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1528 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1529 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1531 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1532 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1533 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1535 /* preallocation can change ac_b_ex, thus we store actually
1536 * allocated blocks for history */
1537 ac
->ac_f_ex
= ac
->ac_b_ex
;
1539 ac
->ac_status
= AC_STATUS_FOUND
;
1540 ac
->ac_tail
= ret
& 0xffff;
1541 ac
->ac_buddy
= ret
>> 16;
1544 * take the page reference. We want the page to be pinned
1545 * so that we don't get a ext4_mb_init_cache_call for this
1546 * group until we update the bitmap. That would mean we
1547 * double allocate blocks. The reference is dropped
1548 * in ext4_mb_release_context
1550 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1551 get_page(ac
->ac_bitmap_page
);
1552 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1553 get_page(ac
->ac_buddy_page
);
1554 /* store last allocated for subsequent stream allocation */
1555 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1556 spin_lock(&sbi
->s_md_lock
);
1557 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1558 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1559 spin_unlock(&sbi
->s_md_lock
);
1564 * regular allocator, for general purposes allocation
1567 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1568 struct ext4_buddy
*e4b
,
1571 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1572 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1573 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1574 struct ext4_free_extent ex
;
1577 if (ac
->ac_status
== AC_STATUS_FOUND
)
1580 * We don't want to scan for a whole year
1582 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1583 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1584 ac
->ac_status
= AC_STATUS_BREAK
;
1589 * Haven't found good chunk so far, let's continue
1591 if (bex
->fe_len
< gex
->fe_len
)
1594 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1595 && bex
->fe_group
== e4b
->bd_group
) {
1596 /* recheck chunk's availability - we don't know
1597 * when it was found (within this lock-unlock
1599 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1600 if (max
>= gex
->fe_len
) {
1601 ext4_mb_use_best_found(ac
, e4b
);
1608 * The routine checks whether found extent is good enough. If it is,
1609 * then the extent gets marked used and flag is set to the context
1610 * to stop scanning. Otherwise, the extent is compared with the
1611 * previous found extent and if new one is better, then it's stored
1612 * in the context. Later, the best found extent will be used, if
1613 * mballoc can't find good enough extent.
1615 * FIXME: real allocation policy is to be designed yet!
1617 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1618 struct ext4_free_extent
*ex
,
1619 struct ext4_buddy
*e4b
)
1621 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1622 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1624 BUG_ON(ex
->fe_len
<= 0);
1625 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1626 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1627 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1632 * The special case - take what you catch first
1634 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1636 ext4_mb_use_best_found(ac
, e4b
);
1641 * Let's check whether the chuck is good enough
1643 if (ex
->fe_len
== gex
->fe_len
) {
1645 ext4_mb_use_best_found(ac
, e4b
);
1650 * If this is first found extent, just store it in the context
1652 if (bex
->fe_len
== 0) {
1658 * If new found extent is better, store it in the context
1660 if (bex
->fe_len
< gex
->fe_len
) {
1661 /* if the request isn't satisfied, any found extent
1662 * larger than previous best one is better */
1663 if (ex
->fe_len
> bex
->fe_len
)
1665 } else if (ex
->fe_len
> gex
->fe_len
) {
1666 /* if the request is satisfied, then we try to find
1667 * an extent that still satisfy the request, but is
1668 * smaller than previous one */
1669 if (ex
->fe_len
< bex
->fe_len
)
1673 ext4_mb_check_limits(ac
, e4b
, 0);
1676 static noinline_for_stack
1677 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1678 struct ext4_buddy
*e4b
)
1680 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1681 ext4_group_t group
= ex
.fe_group
;
1685 BUG_ON(ex
.fe_len
<= 0);
1686 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1690 ext4_lock_group(ac
->ac_sb
, group
);
1691 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1695 ext4_mb_use_best_found(ac
, e4b
);
1698 ext4_unlock_group(ac
->ac_sb
, group
);
1699 ext4_mb_unload_buddy(e4b
);
1704 static noinline_for_stack
1705 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1706 struct ext4_buddy
*e4b
)
1708 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1711 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1712 struct ext4_free_extent ex
;
1714 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1717 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1721 ext4_lock_group(ac
->ac_sb
, group
);
1722 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1723 ac
->ac_g_ex
.fe_len
, &ex
);
1725 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1728 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1730 /* use do_div to get remainder (would be 64-bit modulo) */
1731 if (do_div(start
, sbi
->s_stripe
) == 0) {
1734 ext4_mb_use_best_found(ac
, e4b
);
1736 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1737 BUG_ON(ex
.fe_len
<= 0);
1738 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1739 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1742 ext4_mb_use_best_found(ac
, e4b
);
1743 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1744 /* Sometimes, caller may want to merge even small
1745 * number of blocks to an existing extent */
1746 BUG_ON(ex
.fe_len
<= 0);
1747 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1748 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1751 ext4_mb_use_best_found(ac
, e4b
);
1753 ext4_unlock_group(ac
->ac_sb
, group
);
1754 ext4_mb_unload_buddy(e4b
);
1760 * The routine scans buddy structures (not bitmap!) from given order
1761 * to max order and tries to find big enough chunk to satisfy the req
1763 static noinline_for_stack
1764 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1765 struct ext4_buddy
*e4b
)
1767 struct super_block
*sb
= ac
->ac_sb
;
1768 struct ext4_group_info
*grp
= e4b
->bd_info
;
1774 BUG_ON(ac
->ac_2order
<= 0);
1775 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1776 if (grp
->bb_counters
[i
] == 0)
1779 buddy
= mb_find_buddy(e4b
, i
, &max
);
1780 BUG_ON(buddy
== NULL
);
1782 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1787 ac
->ac_b_ex
.fe_len
= 1 << i
;
1788 ac
->ac_b_ex
.fe_start
= k
<< i
;
1789 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1791 ext4_mb_use_best_found(ac
, e4b
);
1793 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1795 if (EXT4_SB(sb
)->s_mb_stats
)
1796 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1803 * The routine scans the group and measures all found extents.
1804 * In order to optimize scanning, caller must pass number of
1805 * free blocks in the group, so the routine can know upper limit.
1807 static noinline_for_stack
1808 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1809 struct ext4_buddy
*e4b
)
1811 struct super_block
*sb
= ac
->ac_sb
;
1812 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1813 struct ext4_free_extent ex
;
1817 free
= e4b
->bd_info
->bb_free
;
1820 i
= e4b
->bd_info
->bb_first_free
;
1822 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1823 i
= mb_find_next_zero_bit(bitmap
,
1824 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1825 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1827 * IF we have corrupt bitmap, we won't find any
1828 * free blocks even though group info says we
1829 * we have free blocks
1831 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1832 "%d free blocks as per "
1833 "group info. But bitmap says 0",
1838 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1839 BUG_ON(ex
.fe_len
<= 0);
1840 if (free
< ex
.fe_len
) {
1841 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1842 "%d free blocks as per "
1843 "group info. But got %d blocks",
1846 * The number of free blocks differs. This mostly
1847 * indicate that the bitmap is corrupt. So exit
1848 * without claiming the space.
1853 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1859 ext4_mb_check_limits(ac
, e4b
, 1);
1863 * This is a special case for storages like raid5
1864 * we try to find stripe-aligned chunks for stripe-size-multiple requests
1866 static noinline_for_stack
1867 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1868 struct ext4_buddy
*e4b
)
1870 struct super_block
*sb
= ac
->ac_sb
;
1871 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1872 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1873 struct ext4_free_extent ex
;
1874 ext4_fsblk_t first_group_block
;
1879 BUG_ON(sbi
->s_stripe
== 0);
1881 /* find first stripe-aligned block in group */
1882 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1884 a
= first_group_block
+ sbi
->s_stripe
- 1;
1885 do_div(a
, sbi
->s_stripe
);
1886 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1888 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1889 if (!mb_test_bit(i
, bitmap
)) {
1890 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1891 if (max
>= sbi
->s_stripe
) {
1894 ext4_mb_use_best_found(ac
, e4b
);
1902 /* This is now called BEFORE we load the buddy bitmap. */
1903 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1904 ext4_group_t group
, int cr
)
1906 unsigned free
, fragments
;
1907 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1908 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1910 BUG_ON(cr
< 0 || cr
>= 4);
1912 /* We only do this if the grp has never been initialized */
1913 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1914 int ret
= ext4_mb_init_group(ac
->ac_sb
, group
);
1919 free
= grp
->bb_free
;
1920 fragments
= grp
->bb_fragments
;
1928 BUG_ON(ac
->ac_2order
== 0);
1930 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
1933 /* Avoid using the first bg of a flexgroup for data files */
1934 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1935 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1936 ((group
% flex_size
) == 0))
1941 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1945 if (free
>= ac
->ac_g_ex
.fe_len
)
1957 static noinline_for_stack
int
1958 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1960 ext4_group_t ngroups
, group
, i
;
1963 struct ext4_sb_info
*sbi
;
1964 struct super_block
*sb
;
1965 struct ext4_buddy e4b
;
1969 ngroups
= ext4_get_groups_count(sb
);
1970 /* non-extent files are limited to low blocks/groups */
1971 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
1972 ngroups
= sbi
->s_blockfile_groups
;
1974 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1976 /* first, try the goal */
1977 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1978 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1981 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1985 * ac->ac2_order is set only if the fe_len is a power of 2
1986 * if ac2_order is set we also set criteria to 0 so that we
1987 * try exact allocation using buddy.
1989 i
= fls(ac
->ac_g_ex
.fe_len
);
1992 * We search using buddy data only if the order of the request
1993 * is greater than equal to the sbi_s_mb_order2_reqs
1994 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1996 if (i
>= sbi
->s_mb_order2_reqs
) {
1998 * This should tell if fe_len is exactly power of 2
2000 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
2001 ac
->ac_2order
= i
- 1;
2004 /* if stream allocation is enabled, use global goal */
2005 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
2006 /* TBD: may be hot point */
2007 spin_lock(&sbi
->s_md_lock
);
2008 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2009 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2010 spin_unlock(&sbi
->s_md_lock
);
2013 /* Let's just scan groups to find more-less suitable blocks */
2014 cr
= ac
->ac_2order
? 0 : 1;
2016 * cr == 0 try to get exact allocation,
2017 * cr == 3 try to get anything
2020 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2021 ac
->ac_criteria
= cr
;
2023 * searching for the right group start
2024 * from the goal value specified
2026 group
= ac
->ac_g_ex
.fe_group
;
2028 for (i
= 0; i
< ngroups
; group
++, i
++) {
2029 if (group
== ngroups
)
2032 /* This now checks without needing the buddy page */
2033 if (!ext4_mb_good_group(ac
, group
, cr
))
2036 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2040 ext4_lock_group(sb
, group
);
2043 * We need to check again after locking the
2046 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2047 ext4_unlock_group(sb
, group
);
2048 ext4_mb_unload_buddy(&e4b
);
2052 ac
->ac_groups_scanned
++;
2054 ext4_mb_simple_scan_group(ac
, &e4b
);
2055 else if (cr
== 1 && sbi
->s_stripe
&&
2056 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2057 ext4_mb_scan_aligned(ac
, &e4b
);
2059 ext4_mb_complex_scan_group(ac
, &e4b
);
2061 ext4_unlock_group(sb
, group
);
2062 ext4_mb_unload_buddy(&e4b
);
2064 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2069 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2070 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2072 * We've been searching too long. Let's try to allocate
2073 * the best chunk we've found so far
2076 ext4_mb_try_best_found(ac
, &e4b
);
2077 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2079 * Someone more lucky has already allocated it.
2080 * The only thing we can do is just take first
2082 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2084 ac
->ac_b_ex
.fe_group
= 0;
2085 ac
->ac_b_ex
.fe_start
= 0;
2086 ac
->ac_b_ex
.fe_len
= 0;
2087 ac
->ac_status
= AC_STATUS_CONTINUE
;
2088 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2090 atomic_inc(&sbi
->s_mb_lost_chunks
);
2098 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2100 struct super_block
*sb
= seq
->private;
2103 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2106 return (void *) ((unsigned long) group
);
2109 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2111 struct super_block
*sb
= seq
->private;
2115 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2118 return (void *) ((unsigned long) group
);
2121 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2123 struct super_block
*sb
= seq
->private;
2124 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2127 struct ext4_buddy e4b
;
2129 struct ext4_group_info info
;
2130 ext4_grpblk_t counters
[16];
2135 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2136 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2137 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2138 "group", "free", "frags", "first",
2139 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2140 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2142 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2143 sizeof(struct ext4_group_info
);
2144 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2146 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2149 ext4_lock_group(sb
, group
);
2150 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2151 ext4_unlock_group(sb
, group
);
2152 ext4_mb_unload_buddy(&e4b
);
2154 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2155 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2156 for (i
= 0; i
<= 13; i
++)
2157 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2158 sg
.info
.bb_counters
[i
] : 0);
2159 seq_printf(seq
, " ]\n");
2164 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2168 static const struct seq_operations ext4_mb_seq_groups_ops
= {
2169 .start
= ext4_mb_seq_groups_start
,
2170 .next
= ext4_mb_seq_groups_next
,
2171 .stop
= ext4_mb_seq_groups_stop
,
2172 .show
= ext4_mb_seq_groups_show
,
2175 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2177 struct super_block
*sb
= PDE(inode
)->data
;
2180 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2182 struct seq_file
*m
= file
->private_data
;
2189 static const struct file_operations ext4_mb_seq_groups_fops
= {
2190 .owner
= THIS_MODULE
,
2191 .open
= ext4_mb_seq_groups_open
,
2193 .llseek
= seq_lseek
,
2194 .release
= seq_release
,
2197 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2199 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2200 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2206 /* Create and initialize ext4_group_info data for the given group. */
2207 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2208 struct ext4_group_desc
*desc
)
2212 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2213 struct ext4_group_info
**meta_group_info
;
2214 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2217 * First check if this group is the first of a reserved block.
2218 * If it's true, we have to allocate a new table of pointers
2219 * to ext4_group_info structures
2221 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2222 metalen
= sizeof(*meta_group_info
) <<
2223 EXT4_DESC_PER_BLOCK_BITS(sb
);
2224 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2225 if (meta_group_info
== NULL
) {
2226 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2228 goto exit_meta_group_info
;
2230 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2235 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2236 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2238 meta_group_info
[i
] = kmem_cache_alloc(cachep
, GFP_KERNEL
);
2239 if (meta_group_info
[i
] == NULL
) {
2240 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2241 goto exit_group_info
;
2243 memset(meta_group_info
[i
], 0, kmem_cache_size(cachep
));
2244 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2245 &(meta_group_info
[i
]->bb_state
));
2248 * initialize bb_free to be able to skip
2249 * empty groups without initialization
2251 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2252 meta_group_info
[i
]->bb_free
=
2253 ext4_free_blocks_after_init(sb
, group
, desc
);
2255 meta_group_info
[i
]->bb_free
=
2256 ext4_free_blks_count(sb
, desc
);
2259 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2260 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2261 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2262 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2266 struct buffer_head
*bh
;
2267 meta_group_info
[i
]->bb_bitmap
=
2268 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2269 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2270 bh
= ext4_read_block_bitmap(sb
, group
);
2272 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2281 /* If a meta_group_info table has been allocated, release it now */
2282 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2283 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2284 exit_meta_group_info
:
2286 } /* ext4_mb_add_groupinfo */
2288 static int ext4_mb_init_backend(struct super_block
*sb
)
2290 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2292 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2293 struct ext4_super_block
*es
= sbi
->s_es
;
2294 int num_meta_group_infos
;
2295 int num_meta_group_infos_max
;
2297 struct ext4_group_desc
*desc
;
2298 struct kmem_cache
*cachep
;
2300 /* This is the number of blocks used by GDT */
2301 num_meta_group_infos
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) -
2302 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2305 * This is the total number of blocks used by GDT including
2306 * the number of reserved blocks for GDT.
2307 * The s_group_info array is allocated with this value
2308 * to allow a clean online resize without a complex
2309 * manipulation of pointer.
2310 * The drawback is the unused memory when no resize
2311 * occurs but it's very low in terms of pages
2312 * (see comments below)
2313 * Need to handle this properly when META_BG resizing is allowed
2315 num_meta_group_infos_max
= num_meta_group_infos
+
2316 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2319 * array_size is the size of s_group_info array. We round it
2320 * to the next power of two because this approximation is done
2321 * internally by kmalloc so we can have some more memory
2322 * for free here (e.g. may be used for META_BG resize).
2325 while (array_size
< sizeof(*sbi
->s_group_info
) *
2326 num_meta_group_infos_max
)
2327 array_size
= array_size
<< 1;
2328 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2329 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2330 * So a two level scheme suffices for now. */
2331 sbi
->s_group_info
= kzalloc(array_size
, GFP_KERNEL
);
2332 if (sbi
->s_group_info
== NULL
) {
2333 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2336 sbi
->s_buddy_cache
= new_inode(sb
);
2337 if (sbi
->s_buddy_cache
== NULL
) {
2338 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2341 sbi
->s_buddy_cache
->i_ino
= get_next_ino();
2342 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2343 for (i
= 0; i
< ngroups
; i
++) {
2344 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2347 "EXT4-fs: can't read descriptor %u\n", i
);
2350 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2357 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2359 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2360 i
= num_meta_group_infos
;
2362 kfree(sbi
->s_group_info
[i
]);
2363 iput(sbi
->s_buddy_cache
);
2365 kfree(sbi
->s_group_info
);
2369 static void ext4_groupinfo_destroy_slabs(void)
2373 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2374 if (ext4_groupinfo_caches
[i
])
2375 kmem_cache_destroy(ext4_groupinfo_caches
[i
]);
2376 ext4_groupinfo_caches
[i
] = NULL
;
2380 static int ext4_groupinfo_create_slab(size_t size
)
2382 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex
);
2384 int blocksize_bits
= order_base_2(size
);
2385 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2386 struct kmem_cache
*cachep
;
2388 if (cache_index
>= NR_GRPINFO_CACHES
)
2391 if (unlikely(cache_index
< 0))
2394 mutex_lock(&ext4_grpinfo_slab_create_mutex
);
2395 if (ext4_groupinfo_caches
[cache_index
]) {
2396 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2397 return 0; /* Already created */
2400 slab_size
= offsetof(struct ext4_group_info
,
2401 bb_counters
[blocksize_bits
+ 2]);
2403 cachep
= kmem_cache_create(ext4_groupinfo_slab_names
[cache_index
],
2404 slab_size
, 0, SLAB_RECLAIM_ACCOUNT
,
2407 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2409 printk(KERN_EMERG
"EXT4: no memory for groupinfo slab cache\n");
2413 ext4_groupinfo_caches
[cache_index
] = cachep
;
2418 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2420 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2426 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2428 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2429 if (sbi
->s_mb_offsets
== NULL
) {
2434 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2435 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2436 if (sbi
->s_mb_maxs
== NULL
) {
2441 ret
= ext4_groupinfo_create_slab(sb
->s_blocksize
);
2445 /* order 0 is regular bitmap */
2446 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2447 sbi
->s_mb_offsets
[0] = 0;
2451 max
= sb
->s_blocksize
<< 2;
2453 sbi
->s_mb_offsets
[i
] = offset
;
2454 sbi
->s_mb_maxs
[i
] = max
;
2455 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2458 } while (i
<= sb
->s_blocksize_bits
+ 1);
2460 /* init file for buddy data */
2461 ret
= ext4_mb_init_backend(sb
);
2466 spin_lock_init(&sbi
->s_md_lock
);
2467 spin_lock_init(&sbi
->s_bal_lock
);
2469 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2470 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2471 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2472 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2473 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2474 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2476 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2477 if (sbi
->s_locality_groups
== NULL
) {
2481 for_each_possible_cpu(i
) {
2482 struct ext4_locality_group
*lg
;
2483 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2484 mutex_init(&lg
->lg_mutex
);
2485 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2486 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2487 spin_lock_init(&lg
->lg_prealloc_lock
);
2491 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2492 &ext4_mb_seq_groups_fops
, sb
);
2495 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2498 kfree(sbi
->s_mb_offsets
);
2499 kfree(sbi
->s_mb_maxs
);
2504 /* need to called with the ext4 group lock held */
2505 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2507 struct ext4_prealloc_space
*pa
;
2508 struct list_head
*cur
, *tmp
;
2511 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2512 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2513 list_del(&pa
->pa_group_list
);
2515 kmem_cache_free(ext4_pspace_cachep
, pa
);
2518 mb_debug(1, "mballoc: %u PAs left\n", count
);
2522 int ext4_mb_release(struct super_block
*sb
)
2524 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2526 int num_meta_group_infos
;
2527 struct ext4_group_info
*grinfo
;
2528 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2529 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2532 remove_proc_entry("mb_groups", sbi
->s_proc
);
2534 if (sbi
->s_group_info
) {
2535 for (i
= 0; i
< ngroups
; i
++) {
2536 grinfo
= ext4_get_group_info(sb
, i
);
2538 kfree(grinfo
->bb_bitmap
);
2540 ext4_lock_group(sb
, i
);
2541 ext4_mb_cleanup_pa(grinfo
);
2542 ext4_unlock_group(sb
, i
);
2543 kmem_cache_free(cachep
, grinfo
);
2545 num_meta_group_infos
= (ngroups
+
2546 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2547 EXT4_DESC_PER_BLOCK_BITS(sb
);
2548 for (i
= 0; i
< num_meta_group_infos
; i
++)
2549 kfree(sbi
->s_group_info
[i
]);
2550 kfree(sbi
->s_group_info
);
2552 kfree(sbi
->s_mb_offsets
);
2553 kfree(sbi
->s_mb_maxs
);
2554 if (sbi
->s_buddy_cache
)
2555 iput(sbi
->s_buddy_cache
);
2556 if (sbi
->s_mb_stats
) {
2558 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2559 atomic_read(&sbi
->s_bal_allocated
),
2560 atomic_read(&sbi
->s_bal_reqs
),
2561 atomic_read(&sbi
->s_bal_success
));
2563 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2564 "%u 2^N hits, %u breaks, %u lost\n",
2565 atomic_read(&sbi
->s_bal_ex_scanned
),
2566 atomic_read(&sbi
->s_bal_goals
),
2567 atomic_read(&sbi
->s_bal_2orders
),
2568 atomic_read(&sbi
->s_bal_breaks
),
2569 atomic_read(&sbi
->s_mb_lost_chunks
));
2571 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2572 sbi
->s_mb_buddies_generated
++,
2573 sbi
->s_mb_generation_time
);
2575 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2576 atomic_read(&sbi
->s_mb_preallocated
),
2577 atomic_read(&sbi
->s_mb_discarded
));
2580 free_percpu(sbi
->s_locality_groups
);
2585 static inline int ext4_issue_discard(struct super_block
*sb
,
2586 ext4_group_t block_group
, ext4_grpblk_t block
, int count
)
2588 ext4_fsblk_t discard_block
;
2590 discard_block
= block
+ ext4_group_first_block_no(sb
, block_group
);
2591 trace_ext4_discard_blocks(sb
,
2592 (unsigned long long) discard_block
, count
);
2593 return sb_issue_discard(sb
, discard_block
, count
, GFP_NOFS
, 0);
2597 * This function is called by the jbd2 layer once the commit has finished,
2598 * so we know we can free the blocks that were released with that commit.
2600 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2602 struct super_block
*sb
= journal
->j_private
;
2603 struct ext4_buddy e4b
;
2604 struct ext4_group_info
*db
;
2605 int err
, count
= 0, count2
= 0;
2606 struct ext4_free_data
*entry
;
2607 struct list_head
*l
, *ltmp
;
2609 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2610 entry
= list_entry(l
, struct ext4_free_data
, list
);
2612 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2613 entry
->count
, entry
->group
, entry
);
2615 if (test_opt(sb
, DISCARD
))
2616 ext4_issue_discard(sb
, entry
->group
,
2617 entry
->start_blk
, entry
->count
);
2619 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2620 /* we expect to find existing buddy because it's pinned */
2624 /* there are blocks to put in buddy to make them really free */
2625 count
+= entry
->count
;
2627 ext4_lock_group(sb
, entry
->group
);
2628 /* Take it out of per group rb tree */
2629 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2630 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2632 if (!db
->bb_free_root
.rb_node
) {
2633 /* No more items in the per group rb tree
2634 * balance refcounts from ext4_mb_free_metadata()
2636 page_cache_release(e4b
.bd_buddy_page
);
2637 page_cache_release(e4b
.bd_bitmap_page
);
2639 ext4_unlock_group(sb
, entry
->group
);
2640 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2641 ext4_mb_unload_buddy(&e4b
);
2644 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2647 #ifdef CONFIG_EXT4_DEBUG
2648 u8 mb_enable_debug __read_mostly
;
2650 static struct dentry
*debugfs_dir
;
2651 static struct dentry
*debugfs_debug
;
2653 static void __init
ext4_create_debugfs_entry(void)
2655 debugfs_dir
= debugfs_create_dir("ext4", NULL
);
2657 debugfs_debug
= debugfs_create_u8("mballoc-debug",
2663 static void ext4_remove_debugfs_entry(void)
2665 debugfs_remove(debugfs_debug
);
2666 debugfs_remove(debugfs_dir
);
2671 static void __init
ext4_create_debugfs_entry(void)
2675 static void ext4_remove_debugfs_entry(void)
2681 int __init
ext4_init_mballoc(void)
2683 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
2684 SLAB_RECLAIM_ACCOUNT
);
2685 if (ext4_pspace_cachep
== NULL
)
2688 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
2689 SLAB_RECLAIM_ACCOUNT
);
2690 if (ext4_ac_cachep
== NULL
) {
2691 kmem_cache_destroy(ext4_pspace_cachep
);
2695 ext4_free_ext_cachep
= KMEM_CACHE(ext4_free_data
,
2696 SLAB_RECLAIM_ACCOUNT
);
2697 if (ext4_free_ext_cachep
== NULL
) {
2698 kmem_cache_destroy(ext4_pspace_cachep
);
2699 kmem_cache_destroy(ext4_ac_cachep
);
2702 ext4_create_debugfs_entry();
2706 void ext4_exit_mballoc(void)
2709 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2710 * before destroying the slab cache.
2713 kmem_cache_destroy(ext4_pspace_cachep
);
2714 kmem_cache_destroy(ext4_ac_cachep
);
2715 kmem_cache_destroy(ext4_free_ext_cachep
);
2716 ext4_groupinfo_destroy_slabs();
2717 ext4_remove_debugfs_entry();
2722 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
2723 * Returns 0 if success or error code
2725 static noinline_for_stack
int
2726 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2727 handle_t
*handle
, unsigned int reserv_blks
)
2729 struct buffer_head
*bitmap_bh
= NULL
;
2730 struct ext4_group_desc
*gdp
;
2731 struct buffer_head
*gdp_bh
;
2732 struct ext4_sb_info
*sbi
;
2733 struct super_block
*sb
;
2737 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2738 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2744 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2748 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2753 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2757 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2758 ext4_free_blks_count(sb
, gdp
));
2760 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2764 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
2766 len
= ac
->ac_b_ex
.fe_len
;
2767 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2768 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
2769 "fs metadata\n", block
, block
+len
);
2770 /* File system mounted not to panic on error
2771 * Fix the bitmap and repeat the block allocation
2772 * We leak some of the blocks here.
2774 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2775 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2776 ac
->ac_b_ex
.fe_len
);
2777 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2778 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2784 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2785 #ifdef AGGRESSIVE_CHECK
2788 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2789 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2790 bitmap_bh
->b_data
));
2794 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,ac
->ac_b_ex
.fe_len
);
2795 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2796 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2797 ext4_free_blks_set(sb
, gdp
,
2798 ext4_free_blocks_after_init(sb
,
2799 ac
->ac_b_ex
.fe_group
, gdp
));
2801 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2802 ext4_free_blks_set(sb
, gdp
, len
);
2803 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
2805 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2806 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
2808 * Now reduce the dirty block count also. Should not go negative
2810 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
2811 /* release all the reserved blocks if non delalloc */
2812 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
2814 if (sbi
->s_log_groups_per_flex
) {
2815 ext4_group_t flex_group
= ext4_flex_group(sbi
,
2816 ac
->ac_b_ex
.fe_group
);
2817 atomic_sub(ac
->ac_b_ex
.fe_len
,
2818 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
2821 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2824 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
2827 ext4_mark_super_dirty(sb
);
2833 * here we normalize request for locality group
2834 * Group request are normalized to s_strip size if we set the same via mount
2835 * option. If not we set it to s_mb_group_prealloc which can be configured via
2836 * /sys/fs/ext4/<partition>/mb_group_prealloc
2838 * XXX: should we try to preallocate more than the group has now?
2840 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
2842 struct super_block
*sb
= ac
->ac_sb
;
2843 struct ext4_locality_group
*lg
= ac
->ac_lg
;
2846 if (EXT4_SB(sb
)->s_stripe
)
2847 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
2849 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
2850 mb_debug(1, "#%u: goal %u blocks for locality group\n",
2851 current
->pid
, ac
->ac_g_ex
.fe_len
);
2855 * Normalization means making request better in terms of
2856 * size and alignment
2858 static noinline_for_stack
void
2859 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
2860 struct ext4_allocation_request
*ar
)
2864 loff_t size
, orig_size
, start_off
;
2866 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
2867 struct ext4_prealloc_space
*pa
;
2869 /* do normalize only data requests, metadata requests
2870 do not need preallocation */
2871 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
2874 /* sometime caller may want exact blocks */
2875 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2878 /* caller may indicate that preallocation isn't
2879 * required (it's a tail, for example) */
2880 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
2883 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
2884 ext4_mb_normalize_group_request(ac
);
2888 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
2890 /* first, let's learn actual file size
2891 * given current request is allocated */
2892 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
2893 size
= size
<< bsbits
;
2894 if (size
< i_size_read(ac
->ac_inode
))
2895 size
= i_size_read(ac
->ac_inode
);
2898 /* max size of free chunks */
2901 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2902 (req <= (size) || max <= (chunk_size))
2904 /* first, try to predict filesize */
2905 /* XXX: should this table be tunable? */
2907 if (size
<= 16 * 1024) {
2909 } else if (size
<= 32 * 1024) {
2911 } else if (size
<= 64 * 1024) {
2913 } else if (size
<= 128 * 1024) {
2915 } else if (size
<= 256 * 1024) {
2917 } else if (size
<= 512 * 1024) {
2919 } else if (size
<= 1024 * 1024) {
2921 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
2922 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2923 (21 - bsbits
)) << 21;
2924 size
= 2 * 1024 * 1024;
2925 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
2926 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2927 (22 - bsbits
)) << 22;
2928 size
= 4 * 1024 * 1024;
2929 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
2930 (8<<20)>>bsbits
, max
, 8 * 1024)) {
2931 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2932 (23 - bsbits
)) << 23;
2933 size
= 8 * 1024 * 1024;
2935 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
2936 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
2938 size
= size
>> bsbits
;
2939 start
= start_off
>> bsbits
;
2941 /* don't cover already allocated blocks in selected range */
2942 if (ar
->pleft
&& start
<= ar
->lleft
) {
2943 size
-= ar
->lleft
+ 1 - start
;
2944 start
= ar
->lleft
+ 1;
2946 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
2947 size
-= start
+ size
- ar
->lright
;
2951 /* check we don't cross already preallocated blocks */
2953 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2958 spin_lock(&pa
->pa_lock
);
2959 if (pa
->pa_deleted
) {
2960 spin_unlock(&pa
->pa_lock
);
2964 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
2966 /* PA must not overlap original request */
2967 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
2968 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
2970 /* skip PAs this normalized request doesn't overlap with */
2971 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
2972 spin_unlock(&pa
->pa_lock
);
2975 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
2977 /* adjust start or end to be adjacent to this pa */
2978 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
2979 BUG_ON(pa_end
< start
);
2981 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
2982 BUG_ON(pa
->pa_lstart
> end
);
2983 end
= pa
->pa_lstart
;
2985 spin_unlock(&pa
->pa_lock
);
2990 /* XXX: extra loop to check we really don't overlap preallocations */
2992 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2994 spin_lock(&pa
->pa_lock
);
2995 if (pa
->pa_deleted
== 0) {
2996 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
2997 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
2999 spin_unlock(&pa
->pa_lock
);
3003 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3004 start
> ac
->ac_o_ex
.fe_logical
) {
3005 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3006 (unsigned long) start
, (unsigned long) size
,
3007 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3009 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3010 start
> ac
->ac_o_ex
.fe_logical
);
3011 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3013 /* now prepare goal request */
3015 /* XXX: is it better to align blocks WRT to logical
3016 * placement or satisfy big request as is */
3017 ac
->ac_g_ex
.fe_logical
= start
;
3018 ac
->ac_g_ex
.fe_len
= size
;
3020 /* define goal start in order to merge */
3021 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3022 /* merge to the right */
3023 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3024 &ac
->ac_f_ex
.fe_group
,
3025 &ac
->ac_f_ex
.fe_start
);
3026 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3028 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3029 /* merge to the left */
3030 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3031 &ac
->ac_f_ex
.fe_group
,
3032 &ac
->ac_f_ex
.fe_start
);
3033 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3036 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3037 (unsigned) orig_size
, (unsigned) start
);
3040 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3042 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3044 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3045 atomic_inc(&sbi
->s_bal_reqs
);
3046 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3047 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3048 atomic_inc(&sbi
->s_bal_success
);
3049 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3050 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3051 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3052 atomic_inc(&sbi
->s_bal_goals
);
3053 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3054 atomic_inc(&sbi
->s_bal_breaks
);
3057 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3058 trace_ext4_mballoc_alloc(ac
);
3060 trace_ext4_mballoc_prealloc(ac
);
3064 * Called on failure; free up any blocks from the inode PA for this
3065 * context. We don't need this for MB_GROUP_PA because we only change
3066 * pa_free in ext4_mb_release_context(), but on failure, we've already
3067 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3069 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3071 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3074 if (pa
&& pa
->pa_type
== MB_INODE_PA
) {
3075 len
= ac
->ac_b_ex
.fe_len
;
3082 * use blocks preallocated to inode
3084 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3085 struct ext4_prealloc_space
*pa
)
3091 /* found preallocated blocks, use them */
3092 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3093 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3095 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3096 &ac
->ac_b_ex
.fe_start
);
3097 ac
->ac_b_ex
.fe_len
= len
;
3098 ac
->ac_status
= AC_STATUS_FOUND
;
3101 BUG_ON(start
< pa
->pa_pstart
);
3102 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3103 BUG_ON(pa
->pa_free
< len
);
3106 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3110 * use blocks preallocated to locality group
3112 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3113 struct ext4_prealloc_space
*pa
)
3115 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3117 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3118 &ac
->ac_b_ex
.fe_group
,
3119 &ac
->ac_b_ex
.fe_start
);
3120 ac
->ac_b_ex
.fe_len
= len
;
3121 ac
->ac_status
= AC_STATUS_FOUND
;
3124 /* we don't correct pa_pstart or pa_plen here to avoid
3125 * possible race when the group is being loaded concurrently
3126 * instead we correct pa later, after blocks are marked
3127 * in on-disk bitmap -- see ext4_mb_release_context()
3128 * Other CPUs are prevented from allocating from this pa by lg_mutex
3130 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3134 * Return the prealloc space that have minimal distance
3135 * from the goal block. @cpa is the prealloc
3136 * space that is having currently known minimal distance
3137 * from the goal block.
3139 static struct ext4_prealloc_space
*
3140 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3141 struct ext4_prealloc_space
*pa
,
3142 struct ext4_prealloc_space
*cpa
)
3144 ext4_fsblk_t cur_distance
, new_distance
;
3147 atomic_inc(&pa
->pa_count
);
3150 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3151 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3153 if (cur_distance
<= new_distance
)
3156 /* drop the previous reference */
3157 atomic_dec(&cpa
->pa_count
);
3158 atomic_inc(&pa
->pa_count
);
3163 * search goal blocks in preallocated space
3165 static noinline_for_stack
int
3166 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3169 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3170 struct ext4_locality_group
*lg
;
3171 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3172 ext4_fsblk_t goal_block
;
3174 /* only data can be preallocated */
3175 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3178 /* first, try per-file preallocation */
3180 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3182 /* all fields in this condition don't change,
3183 * so we can skip locking for them */
3184 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3185 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3188 /* non-extent files can't have physical blocks past 2^32 */
3189 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3190 pa
->pa_pstart
+ pa
->pa_len
> EXT4_MAX_BLOCK_FILE_PHYS
)
3193 /* found preallocated blocks, use them */
3194 spin_lock(&pa
->pa_lock
);
3195 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3196 atomic_inc(&pa
->pa_count
);
3197 ext4_mb_use_inode_pa(ac
, pa
);
3198 spin_unlock(&pa
->pa_lock
);
3199 ac
->ac_criteria
= 10;
3203 spin_unlock(&pa
->pa_lock
);
3207 /* can we use group allocation? */
3208 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3211 /* inode may have no locality group for some reason */
3215 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3216 if (order
> PREALLOC_TB_SIZE
- 1)
3217 /* The max size of hash table is PREALLOC_TB_SIZE */
3218 order
= PREALLOC_TB_SIZE
- 1;
3220 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3222 * search for the prealloc space that is having
3223 * minimal distance from the goal block.
3225 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3227 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3229 spin_lock(&pa
->pa_lock
);
3230 if (pa
->pa_deleted
== 0 &&
3231 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3233 cpa
= ext4_mb_check_group_pa(goal_block
,
3236 spin_unlock(&pa
->pa_lock
);
3241 ext4_mb_use_group_pa(ac
, cpa
);
3242 ac
->ac_criteria
= 20;
3249 * the function goes through all block freed in the group
3250 * but not yet committed and marks them used in in-core bitmap.
3251 * buddy must be generated from this bitmap
3252 * Need to be called with the ext4 group lock held
3254 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3258 struct ext4_group_info
*grp
;
3259 struct ext4_free_data
*entry
;
3261 grp
= ext4_get_group_info(sb
, group
);
3262 n
= rb_first(&(grp
->bb_free_root
));
3265 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3266 mb_set_bits(bitmap
, entry
->start_blk
, entry
->count
);
3273 * the function goes through all preallocation in this group and marks them
3274 * used in in-core bitmap. buddy must be generated from this bitmap
3275 * Need to be called with ext4 group lock held
3277 static noinline_for_stack
3278 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3281 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3282 struct ext4_prealloc_space
*pa
;
3283 struct list_head
*cur
;
3284 ext4_group_t groupnr
;
3285 ext4_grpblk_t start
;
3286 int preallocated
= 0;
3290 /* all form of preallocation discards first load group,
3291 * so the only competing code is preallocation use.
3292 * we don't need any locking here
3293 * notice we do NOT ignore preallocations with pa_deleted
3294 * otherwise we could leave used blocks available for
3295 * allocation in buddy when concurrent ext4_mb_put_pa()
3296 * is dropping preallocation
3298 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3299 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3300 spin_lock(&pa
->pa_lock
);
3301 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3304 spin_unlock(&pa
->pa_lock
);
3305 if (unlikely(len
== 0))
3307 BUG_ON(groupnr
!= group
);
3308 mb_set_bits(bitmap
, start
, len
);
3309 preallocated
+= len
;
3312 mb_debug(1, "prellocated %u for group %u\n", preallocated
, group
);
3315 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3317 struct ext4_prealloc_space
*pa
;
3318 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3319 kmem_cache_free(ext4_pspace_cachep
, pa
);
3323 * drops a reference to preallocated space descriptor
3324 * if this was the last reference and the space is consumed
3326 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3327 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3330 ext4_fsblk_t grp_blk
;
3332 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3335 /* in this short window concurrent discard can set pa_deleted */
3336 spin_lock(&pa
->pa_lock
);
3337 if (pa
->pa_deleted
== 1) {
3338 spin_unlock(&pa
->pa_lock
);
3343 spin_unlock(&pa
->pa_lock
);
3345 grp_blk
= pa
->pa_pstart
;
3347 * If doing group-based preallocation, pa_pstart may be in the
3348 * next group when pa is used up
3350 if (pa
->pa_type
== MB_GROUP_PA
)
3353 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3358 * P1 (buddy init) P2 (regular allocation)
3359 * find block B in PA
3360 * copy on-disk bitmap to buddy
3361 * mark B in on-disk bitmap
3362 * drop PA from group
3363 * mark all PAs in buddy
3365 * thus, P1 initializes buddy with B available. to prevent this
3366 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3369 ext4_lock_group(sb
, grp
);
3370 list_del(&pa
->pa_group_list
);
3371 ext4_unlock_group(sb
, grp
);
3373 spin_lock(pa
->pa_obj_lock
);
3374 list_del_rcu(&pa
->pa_inode_list
);
3375 spin_unlock(pa
->pa_obj_lock
);
3377 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3381 * creates new preallocated space for given inode
3383 static noinline_for_stack
int
3384 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3386 struct super_block
*sb
= ac
->ac_sb
;
3387 struct ext4_prealloc_space
*pa
;
3388 struct ext4_group_info
*grp
;
3389 struct ext4_inode_info
*ei
;
3391 /* preallocate only when found space is larger then requested */
3392 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3393 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3394 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3396 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3400 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3406 /* we can't allocate as much as normalizer wants.
3407 * so, found space must get proper lstart
3408 * to cover original request */
3409 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3410 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3412 /* we're limited by original request in that
3413 * logical block must be covered any way
3414 * winl is window we can move our chunk within */
3415 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3417 /* also, we should cover whole original request */
3418 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3420 /* the smallest one defines real window */
3421 win
= min(winl
, wins
);
3423 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3424 if (offs
&& offs
< win
)
3427 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3428 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3429 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3432 /* preallocation can change ac_b_ex, thus we store actually
3433 * allocated blocks for history */
3434 ac
->ac_f_ex
= ac
->ac_b_ex
;
3436 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3437 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3438 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3439 pa
->pa_free
= pa
->pa_len
;
3440 atomic_set(&pa
->pa_count
, 1);
3441 spin_lock_init(&pa
->pa_lock
);
3442 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3443 INIT_LIST_HEAD(&pa
->pa_group_list
);
3445 pa
->pa_type
= MB_INODE_PA
;
3447 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3448 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3449 trace_ext4_mb_new_inode_pa(ac
, pa
);
3451 ext4_mb_use_inode_pa(ac
, pa
);
3452 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3454 ei
= EXT4_I(ac
->ac_inode
);
3455 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3457 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3458 pa
->pa_inode
= ac
->ac_inode
;
3460 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3461 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3462 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3464 spin_lock(pa
->pa_obj_lock
);
3465 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3466 spin_unlock(pa
->pa_obj_lock
);
3472 * creates new preallocated space for locality group inodes belongs to
3474 static noinline_for_stack
int
3475 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3477 struct super_block
*sb
= ac
->ac_sb
;
3478 struct ext4_locality_group
*lg
;
3479 struct ext4_prealloc_space
*pa
;
3480 struct ext4_group_info
*grp
;
3482 /* preallocate only when found space is larger then requested */
3483 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3484 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3485 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3487 BUG_ON(ext4_pspace_cachep
== NULL
);
3488 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3492 /* preallocation can change ac_b_ex, thus we store actually
3493 * allocated blocks for history */
3494 ac
->ac_f_ex
= ac
->ac_b_ex
;
3496 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3497 pa
->pa_lstart
= pa
->pa_pstart
;
3498 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3499 pa
->pa_free
= pa
->pa_len
;
3500 atomic_set(&pa
->pa_count
, 1);
3501 spin_lock_init(&pa
->pa_lock
);
3502 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3503 INIT_LIST_HEAD(&pa
->pa_group_list
);
3505 pa
->pa_type
= MB_GROUP_PA
;
3507 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3508 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3509 trace_ext4_mb_new_group_pa(ac
, pa
);
3511 ext4_mb_use_group_pa(ac
, pa
);
3512 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3514 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3518 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3519 pa
->pa_inode
= NULL
;
3521 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3522 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3523 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3526 * We will later add the new pa to the right bucket
3527 * after updating the pa_free in ext4_mb_release_context
3532 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3536 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3537 err
= ext4_mb_new_group_pa(ac
);
3539 err
= ext4_mb_new_inode_pa(ac
);
3544 * finds all unused blocks in on-disk bitmap, frees them in
3545 * in-core bitmap and buddy.
3546 * @pa must be unlinked from inode and group lists, so that
3547 * nobody else can find/use it.
3548 * the caller MUST hold group/inode locks.
3549 * TODO: optimize the case when there are no in-core structures yet
3551 static noinline_for_stack
int
3552 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3553 struct ext4_prealloc_space
*pa
)
3555 struct super_block
*sb
= e4b
->bd_sb
;
3556 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3561 unsigned long long grp_blk_start
;
3565 BUG_ON(pa
->pa_deleted
== 0);
3566 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3567 grp_blk_start
= pa
->pa_pstart
- bit
;
3568 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3569 end
= bit
+ pa
->pa_len
;
3572 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3575 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3576 mb_debug(1, " free preallocated %u/%u in group %u\n",
3577 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3578 (unsigned) next
- bit
, (unsigned) group
);
3581 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3582 trace_ext4_mb_release_inode_pa(pa
, grp_blk_start
+ bit
,
3584 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3587 if (free
!= pa
->pa_free
) {
3588 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3589 pa
, (unsigned long) pa
->pa_lstart
,
3590 (unsigned long) pa
->pa_pstart
,
3591 (unsigned long) pa
->pa_len
);
3592 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3595 * pa is already deleted so we use the value obtained
3596 * from the bitmap and continue.
3599 atomic_add(free
, &sbi
->s_mb_discarded
);
3604 static noinline_for_stack
int
3605 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3606 struct ext4_prealloc_space
*pa
)
3608 struct super_block
*sb
= e4b
->bd_sb
;
3612 trace_ext4_mb_release_group_pa(pa
);
3613 BUG_ON(pa
->pa_deleted
== 0);
3614 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3615 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3616 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3617 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3618 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3624 * releases all preallocations in given group
3626 * first, we need to decide discard policy:
3627 * - when do we discard
3629 * - how many do we discard
3630 * 1) how many requested
3632 static noinline_for_stack
int
3633 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3634 ext4_group_t group
, int needed
)
3636 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3637 struct buffer_head
*bitmap_bh
= NULL
;
3638 struct ext4_prealloc_space
*pa
, *tmp
;
3639 struct list_head list
;
3640 struct ext4_buddy e4b
;
3645 mb_debug(1, "discard preallocation for group %u\n", group
);
3647 if (list_empty(&grp
->bb_prealloc_list
))
3650 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3651 if (bitmap_bh
== NULL
) {
3652 ext4_error(sb
, "Error reading block bitmap for %u", group
);
3656 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3658 ext4_error(sb
, "Error loading buddy information for %u", group
);
3664 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3666 INIT_LIST_HEAD(&list
);
3668 ext4_lock_group(sb
, group
);
3669 list_for_each_entry_safe(pa
, tmp
,
3670 &grp
->bb_prealloc_list
, pa_group_list
) {
3671 spin_lock(&pa
->pa_lock
);
3672 if (atomic_read(&pa
->pa_count
)) {
3673 spin_unlock(&pa
->pa_lock
);
3677 if (pa
->pa_deleted
) {
3678 spin_unlock(&pa
->pa_lock
);
3682 /* seems this one can be freed ... */
3685 /* we can trust pa_free ... */
3686 free
+= pa
->pa_free
;
3688 spin_unlock(&pa
->pa_lock
);
3690 list_del(&pa
->pa_group_list
);
3691 list_add(&pa
->u
.pa_tmp_list
, &list
);
3694 /* if we still need more blocks and some PAs were used, try again */
3695 if (free
< needed
&& busy
) {
3697 ext4_unlock_group(sb
, group
);
3699 * Yield the CPU here so that we don't get soft lockup
3700 * in non preempt case.
3706 /* found anything to free? */
3707 if (list_empty(&list
)) {
3712 /* now free all selected PAs */
3713 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3715 /* remove from object (inode or locality group) */
3716 spin_lock(pa
->pa_obj_lock
);
3717 list_del_rcu(&pa
->pa_inode_list
);
3718 spin_unlock(pa
->pa_obj_lock
);
3720 if (pa
->pa_type
== MB_GROUP_PA
)
3721 ext4_mb_release_group_pa(&e4b
, pa
);
3723 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3725 list_del(&pa
->u
.pa_tmp_list
);
3726 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3730 ext4_unlock_group(sb
, group
);
3731 ext4_mb_unload_buddy(&e4b
);
3737 * releases all non-used preallocated blocks for given inode
3739 * It's important to discard preallocations under i_data_sem
3740 * We don't want another block to be served from the prealloc
3741 * space when we are discarding the inode prealloc space.
3743 * FIXME!! Make sure it is valid at all the call sites
3745 void ext4_discard_preallocations(struct inode
*inode
)
3747 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3748 struct super_block
*sb
= inode
->i_sb
;
3749 struct buffer_head
*bitmap_bh
= NULL
;
3750 struct ext4_prealloc_space
*pa
, *tmp
;
3751 ext4_group_t group
= 0;
3752 struct list_head list
;
3753 struct ext4_buddy e4b
;
3756 if (!S_ISREG(inode
->i_mode
)) {
3757 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3761 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
3762 trace_ext4_discard_preallocations(inode
);
3764 INIT_LIST_HEAD(&list
);
3767 /* first, collect all pa's in the inode */
3768 spin_lock(&ei
->i_prealloc_lock
);
3769 while (!list_empty(&ei
->i_prealloc_list
)) {
3770 pa
= list_entry(ei
->i_prealloc_list
.next
,
3771 struct ext4_prealloc_space
, pa_inode_list
);
3772 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3773 spin_lock(&pa
->pa_lock
);
3774 if (atomic_read(&pa
->pa_count
)) {
3775 /* this shouldn't happen often - nobody should
3776 * use preallocation while we're discarding it */
3777 spin_unlock(&pa
->pa_lock
);
3778 spin_unlock(&ei
->i_prealloc_lock
);
3779 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
3781 schedule_timeout_uninterruptible(HZ
);
3785 if (pa
->pa_deleted
== 0) {
3787 spin_unlock(&pa
->pa_lock
);
3788 list_del_rcu(&pa
->pa_inode_list
);
3789 list_add(&pa
->u
.pa_tmp_list
, &list
);
3793 /* someone is deleting pa right now */
3794 spin_unlock(&pa
->pa_lock
);
3795 spin_unlock(&ei
->i_prealloc_lock
);
3797 /* we have to wait here because pa_deleted
3798 * doesn't mean pa is already unlinked from
3799 * the list. as we might be called from
3800 * ->clear_inode() the inode will get freed
3801 * and concurrent thread which is unlinking
3802 * pa from inode's list may access already
3803 * freed memory, bad-bad-bad */
3805 /* XXX: if this happens too often, we can
3806 * add a flag to force wait only in case
3807 * of ->clear_inode(), but not in case of
3808 * regular truncate */
3809 schedule_timeout_uninterruptible(HZ
);
3812 spin_unlock(&ei
->i_prealloc_lock
);
3814 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3815 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
3816 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3818 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3820 ext4_error(sb
, "Error loading buddy information for %u",
3825 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3826 if (bitmap_bh
== NULL
) {
3827 ext4_error(sb
, "Error reading block bitmap for %u",
3829 ext4_mb_unload_buddy(&e4b
);
3833 ext4_lock_group(sb
, group
);
3834 list_del(&pa
->pa_group_list
);
3835 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3836 ext4_unlock_group(sb
, group
);
3838 ext4_mb_unload_buddy(&e4b
);
3841 list_del(&pa
->u
.pa_tmp_list
);
3842 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3846 #ifdef CONFIG_EXT4_DEBUG
3847 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3849 struct super_block
*sb
= ac
->ac_sb
;
3850 ext4_group_t ngroups
, i
;
3852 if (!mb_enable_debug
||
3853 (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
))
3856 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
3857 " Allocation context details:\n");
3858 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
3859 ac
->ac_status
, ac
->ac_flags
);
3860 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3861 "best %lu/%lu/%lu@%lu cr %d\n",
3862 (unsigned long)ac
->ac_o_ex
.fe_group
,
3863 (unsigned long)ac
->ac_o_ex
.fe_start
,
3864 (unsigned long)ac
->ac_o_ex
.fe_len
,
3865 (unsigned long)ac
->ac_o_ex
.fe_logical
,
3866 (unsigned long)ac
->ac_g_ex
.fe_group
,
3867 (unsigned long)ac
->ac_g_ex
.fe_start
,
3868 (unsigned long)ac
->ac_g_ex
.fe_len
,
3869 (unsigned long)ac
->ac_g_ex
.fe_logical
,
3870 (unsigned long)ac
->ac_b_ex
.fe_group
,
3871 (unsigned long)ac
->ac_b_ex
.fe_start
,
3872 (unsigned long)ac
->ac_b_ex
.fe_len
,
3873 (unsigned long)ac
->ac_b_ex
.fe_logical
,
3874 (int)ac
->ac_criteria
);
3875 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
3877 printk(KERN_ERR
"EXT4-fs: groups: \n");
3878 ngroups
= ext4_get_groups_count(sb
);
3879 for (i
= 0; i
< ngroups
; i
++) {
3880 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
3881 struct ext4_prealloc_space
*pa
;
3882 ext4_grpblk_t start
;
3883 struct list_head
*cur
;
3884 ext4_lock_group(sb
, i
);
3885 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3886 pa
= list_entry(cur
, struct ext4_prealloc_space
,
3888 spin_lock(&pa
->pa_lock
);
3889 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3891 spin_unlock(&pa
->pa_lock
);
3892 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
3895 ext4_unlock_group(sb
, i
);
3897 if (grp
->bb_free
== 0)
3899 printk(KERN_ERR
"%u: %d/%d \n",
3900 i
, grp
->bb_free
, grp
->bb_fragments
);
3902 printk(KERN_ERR
"\n");
3905 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3912 * We use locality group preallocation for small size file. The size of the
3913 * file is determined by the current size or the resulting size after
3914 * allocation which ever is larger
3916 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
3918 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
3920 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3921 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3924 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3927 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3930 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3931 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
3934 if ((size
== isize
) &&
3935 !ext4_fs_is_busy(sbi
) &&
3936 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
3937 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
3941 /* don't use group allocation for large files */
3942 size
= max(size
, isize
);
3943 if (size
> sbi
->s_mb_stream_request
) {
3944 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
3948 BUG_ON(ac
->ac_lg
!= NULL
);
3950 * locality group prealloc space are per cpu. The reason for having
3951 * per cpu locality group is to reduce the contention between block
3952 * request from multiple CPUs.
3954 ac
->ac_lg
= __this_cpu_ptr(sbi
->s_locality_groups
);
3956 /* we're going to use group allocation */
3957 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
3959 /* serialize all allocations in the group */
3960 mutex_lock(&ac
->ac_lg
->lg_mutex
);
3963 static noinline_for_stack
int
3964 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
3965 struct ext4_allocation_request
*ar
)
3967 struct super_block
*sb
= ar
->inode
->i_sb
;
3968 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3969 struct ext4_super_block
*es
= sbi
->s_es
;
3973 ext4_grpblk_t block
;
3975 /* we can't allocate > group size */
3978 /* just a dirty hack to filter too big requests */
3979 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
3980 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
3982 /* start searching from the goal */
3984 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
3985 goal
>= ext4_blocks_count(es
))
3986 goal
= le32_to_cpu(es
->s_first_data_block
);
3987 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
3989 /* set up allocation goals */
3990 memset(ac
, 0, sizeof(struct ext4_allocation_context
));
3991 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
3992 ac
->ac_status
= AC_STATUS_CONTINUE
;
3994 ac
->ac_inode
= ar
->inode
;
3995 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
3996 ac
->ac_o_ex
.fe_group
= group
;
3997 ac
->ac_o_ex
.fe_start
= block
;
3998 ac
->ac_o_ex
.fe_len
= len
;
3999 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4000 ac
->ac_g_ex
.fe_group
= group
;
4001 ac
->ac_g_ex
.fe_start
= block
;
4002 ac
->ac_g_ex
.fe_len
= len
;
4003 ac
->ac_flags
= ar
->flags
;
4005 /* we have to define context: we'll we work with a file or
4006 * locality group. this is a policy, actually */
4007 ext4_mb_group_or_file(ac
);
4009 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4010 "left: %u/%u, right %u/%u to %swritable\n",
4011 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4012 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4013 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4014 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4015 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4020 static noinline_for_stack
void
4021 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4022 struct ext4_locality_group
*lg
,
4023 int order
, int total_entries
)
4025 ext4_group_t group
= 0;
4026 struct ext4_buddy e4b
;
4027 struct list_head discard_list
;
4028 struct ext4_prealloc_space
*pa
, *tmp
;
4030 mb_debug(1, "discard locality group preallocation\n");
4032 INIT_LIST_HEAD(&discard_list
);
4034 spin_lock(&lg
->lg_prealloc_lock
);
4035 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4037 spin_lock(&pa
->pa_lock
);
4038 if (atomic_read(&pa
->pa_count
)) {
4040 * This is the pa that we just used
4041 * for block allocation. So don't
4044 spin_unlock(&pa
->pa_lock
);
4047 if (pa
->pa_deleted
) {
4048 spin_unlock(&pa
->pa_lock
);
4051 /* only lg prealloc space */
4052 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4054 /* seems this one can be freed ... */
4056 spin_unlock(&pa
->pa_lock
);
4058 list_del_rcu(&pa
->pa_inode_list
);
4059 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4062 if (total_entries
<= 5) {
4064 * we want to keep only 5 entries
4065 * allowing it to grow to 8. This
4066 * mak sure we don't call discard
4067 * soon for this list.
4072 spin_unlock(&lg
->lg_prealloc_lock
);
4074 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4076 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4077 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4078 ext4_error(sb
, "Error loading buddy information for %u",
4082 ext4_lock_group(sb
, group
);
4083 list_del(&pa
->pa_group_list
);
4084 ext4_mb_release_group_pa(&e4b
, pa
);
4085 ext4_unlock_group(sb
, group
);
4087 ext4_mb_unload_buddy(&e4b
);
4088 list_del(&pa
->u
.pa_tmp_list
);
4089 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4094 * We have incremented pa_count. So it cannot be freed at this
4095 * point. Also we hold lg_mutex. So no parallel allocation is
4096 * possible from this lg. That means pa_free cannot be updated.
4098 * A parallel ext4_mb_discard_group_preallocations is possible.
4099 * which can cause the lg_prealloc_list to be updated.
4102 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4104 int order
, added
= 0, lg_prealloc_count
= 1;
4105 struct super_block
*sb
= ac
->ac_sb
;
4106 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4107 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4109 order
= fls(pa
->pa_free
) - 1;
4110 if (order
> PREALLOC_TB_SIZE
- 1)
4111 /* The max size of hash table is PREALLOC_TB_SIZE */
4112 order
= PREALLOC_TB_SIZE
- 1;
4113 /* Add the prealloc space to lg */
4115 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4117 spin_lock(&tmp_pa
->pa_lock
);
4118 if (tmp_pa
->pa_deleted
) {
4119 spin_unlock(&tmp_pa
->pa_lock
);
4122 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4123 /* Add to the tail of the previous entry */
4124 list_add_tail_rcu(&pa
->pa_inode_list
,
4125 &tmp_pa
->pa_inode_list
);
4128 * we want to count the total
4129 * number of entries in the list
4132 spin_unlock(&tmp_pa
->pa_lock
);
4133 lg_prealloc_count
++;
4136 list_add_tail_rcu(&pa
->pa_inode_list
,
4137 &lg
->lg_prealloc_list
[order
]);
4140 /* Now trim the list to be not more than 8 elements */
4141 if (lg_prealloc_count
> 8) {
4142 ext4_mb_discard_lg_preallocations(sb
, lg
,
4143 order
, lg_prealloc_count
);
4150 * release all resource we used in allocation
4152 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4154 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4156 if (pa
->pa_type
== MB_GROUP_PA
) {
4157 /* see comment in ext4_mb_use_group_pa() */
4158 spin_lock(&pa
->pa_lock
);
4159 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4160 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4161 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4162 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4163 spin_unlock(&pa
->pa_lock
);
4168 * We want to add the pa to the right bucket.
4169 * Remove it from the list and while adding
4170 * make sure the list to which we are adding
4173 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4174 spin_lock(pa
->pa_obj_lock
);
4175 list_del_rcu(&pa
->pa_inode_list
);
4176 spin_unlock(pa
->pa_obj_lock
);
4177 ext4_mb_add_n_trim(ac
);
4179 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4181 if (ac
->ac_bitmap_page
)
4182 page_cache_release(ac
->ac_bitmap_page
);
4183 if (ac
->ac_buddy_page
)
4184 page_cache_release(ac
->ac_buddy_page
);
4185 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4186 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4187 ext4_mb_collect_stats(ac
);
4191 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4193 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4197 trace_ext4_mb_discard_preallocations(sb
, needed
);
4198 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4199 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4208 * Main entry point into mballoc to allocate blocks
4209 * it tries to use preallocation first, then falls back
4210 * to usual allocation
4212 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4213 struct ext4_allocation_request
*ar
, int *errp
)
4216 struct ext4_allocation_context
*ac
= NULL
;
4217 struct ext4_sb_info
*sbi
;
4218 struct super_block
*sb
;
4219 ext4_fsblk_t block
= 0;
4220 unsigned int inquota
= 0;
4221 unsigned int reserv_blks
= 0;
4223 sb
= ar
->inode
->i_sb
;
4226 trace_ext4_request_blocks(ar
);
4229 * For delayed allocation, we could skip the ENOSPC and
4230 * EDQUOT check, as blocks and quotas have been already
4231 * reserved when data being copied into pagecache.
4233 if (ext4_test_inode_state(ar
->inode
, EXT4_STATE_DELALLOC_RESERVED
))
4234 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4236 /* Without delayed allocation we need to verify
4237 * there is enough free blocks to do block allocation
4238 * and verify allocation doesn't exceed the quota limits.
4241 ext4_claim_free_blocks(sbi
, ar
->len
, ar
->flags
)) {
4243 /* let others to free the space */
4245 ar
->len
= ar
->len
>> 1;
4251 reserv_blks
= ar
->len
;
4252 if (ar
->flags
& EXT4_MB_USE_ROOT_BLOCKS
) {
4253 dquot_alloc_block_nofail(ar
->inode
, ar
->len
);
4256 dquot_alloc_block(ar
->inode
, ar
->len
)) {
4258 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4269 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4276 *errp
= ext4_mb_initialize_context(ac
, ar
);
4282 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4283 if (!ext4_mb_use_preallocated(ac
)) {
4284 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4285 ext4_mb_normalize_request(ac
, ar
);
4287 /* allocate space in core */
4288 *errp
= ext4_mb_regular_allocator(ac
);
4292 /* as we've just preallocated more space than
4293 * user requested orinally, we store allocated
4294 * space in a special descriptor */
4295 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4296 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4297 ext4_mb_new_preallocation(ac
);
4299 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4300 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4301 if (*errp
== -EAGAIN
) {
4303 * drop the reference that we took
4304 * in ext4_mb_use_best_found
4306 ext4_mb_release_context(ac
);
4307 ac
->ac_b_ex
.fe_group
= 0;
4308 ac
->ac_b_ex
.fe_start
= 0;
4309 ac
->ac_b_ex
.fe_len
= 0;
4310 ac
->ac_status
= AC_STATUS_CONTINUE
;
4314 ext4_discard_allocated_blocks(ac
);
4316 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4317 ar
->len
= ac
->ac_b_ex
.fe_len
;
4320 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4327 ac
->ac_b_ex
.fe_len
= 0;
4329 ext4_mb_show_ac(ac
);
4331 ext4_mb_release_context(ac
);
4334 kmem_cache_free(ext4_ac_cachep
, ac
);
4335 if (inquota
&& ar
->len
< inquota
)
4336 dquot_free_block(ar
->inode
, inquota
- ar
->len
);
4338 if (!ext4_test_inode_state(ar
->inode
,
4339 EXT4_STATE_DELALLOC_RESERVED
))
4340 /* release all the reserved blocks if non delalloc */
4341 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4345 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4351 * We can merge two free data extents only if the physical blocks
4352 * are contiguous, AND the extents were freed by the same transaction,
4353 * AND the blocks are associated with the same group.
4355 static int can_merge(struct ext4_free_data
*entry1
,
4356 struct ext4_free_data
*entry2
)
4358 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4359 (entry1
->group
== entry2
->group
) &&
4360 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4365 static noinline_for_stack
int
4366 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4367 struct ext4_free_data
*new_entry
)
4369 ext4_group_t group
= e4b
->bd_group
;
4370 ext4_grpblk_t block
;
4371 struct ext4_free_data
*entry
;
4372 struct ext4_group_info
*db
= e4b
->bd_info
;
4373 struct super_block
*sb
= e4b
->bd_sb
;
4374 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4375 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4376 struct rb_node
*parent
= NULL
, *new_node
;
4378 BUG_ON(!ext4_handle_valid(handle
));
4379 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4380 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4382 new_node
= &new_entry
->node
;
4383 block
= new_entry
->start_blk
;
4386 /* first free block exent. We need to
4387 protect buddy cache from being freed,
4388 * otherwise we'll refresh it from
4389 * on-disk bitmap and lose not-yet-available
4391 page_cache_get(e4b
->bd_buddy_page
);
4392 page_cache_get(e4b
->bd_bitmap_page
);
4396 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4397 if (block
< entry
->start_blk
)
4399 else if (block
>= (entry
->start_blk
+ entry
->count
))
4400 n
= &(*n
)->rb_right
;
4402 ext4_grp_locked_error(sb
, group
, 0,
4403 ext4_group_first_block_no(sb
, group
) + block
,
4404 "Block already on to-be-freed list");
4409 rb_link_node(new_node
, parent
, n
);
4410 rb_insert_color(new_node
, &db
->bb_free_root
);
4412 /* Now try to see the extent can be merged to left and right */
4413 node
= rb_prev(new_node
);
4415 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4416 if (can_merge(entry
, new_entry
)) {
4417 new_entry
->start_blk
= entry
->start_blk
;
4418 new_entry
->count
+= entry
->count
;
4419 rb_erase(node
, &(db
->bb_free_root
));
4420 spin_lock(&sbi
->s_md_lock
);
4421 list_del(&entry
->list
);
4422 spin_unlock(&sbi
->s_md_lock
);
4423 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4427 node
= rb_next(new_node
);
4429 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4430 if (can_merge(new_entry
, entry
)) {
4431 new_entry
->count
+= entry
->count
;
4432 rb_erase(node
, &(db
->bb_free_root
));
4433 spin_lock(&sbi
->s_md_lock
);
4434 list_del(&entry
->list
);
4435 spin_unlock(&sbi
->s_md_lock
);
4436 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4439 /* Add the extent to transaction's private list */
4440 spin_lock(&sbi
->s_md_lock
);
4441 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4442 spin_unlock(&sbi
->s_md_lock
);
4447 * ext4_free_blocks() -- Free given blocks and update quota
4448 * @handle: handle for this transaction
4450 * @block: start physical block to free
4451 * @count: number of blocks to count
4452 * @flags: flags used by ext4_free_blocks
4454 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4455 struct buffer_head
*bh
, ext4_fsblk_t block
,
4456 unsigned long count
, int flags
)
4458 struct buffer_head
*bitmap_bh
= NULL
;
4459 struct super_block
*sb
= inode
->i_sb
;
4460 struct ext4_group_desc
*gdp
;
4461 unsigned long freed
= 0;
4462 unsigned int overflow
;
4464 struct buffer_head
*gd_bh
;
4465 ext4_group_t block_group
;
4466 struct ext4_sb_info
*sbi
;
4467 struct ext4_buddy e4b
;
4473 BUG_ON(block
!= bh
->b_blocknr
);
4475 block
= bh
->b_blocknr
;
4479 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4480 !ext4_data_block_valid(sbi
, block
, count
)) {
4481 ext4_error(sb
, "Freeing blocks not in datazone - "
4482 "block = %llu, count = %lu", block
, count
);
4486 ext4_debug("freeing block %llu\n", block
);
4487 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4489 if (flags
& EXT4_FREE_BLOCKS_FORGET
) {
4490 struct buffer_head
*tbh
= bh
;
4493 BUG_ON(bh
&& (count
> 1));
4495 for (i
= 0; i
< count
; i
++) {
4497 tbh
= sb_find_get_block(inode
->i_sb
,
4501 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4502 inode
, tbh
, block
+ i
);
4507 * We need to make sure we don't reuse the freed block until
4508 * after the transaction is committed, which we can do by
4509 * treating the block as metadata, below. We make an
4510 * exception if the inode is to be written in writeback mode
4511 * since writeback mode has weak data consistency guarantees.
4513 if (!ext4_should_writeback_data(inode
))
4514 flags
|= EXT4_FREE_BLOCKS_METADATA
;
4518 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4521 * Check to see if we are freeing blocks across a group
4524 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4525 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4528 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4533 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4539 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4540 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4541 in_range(block
, ext4_inode_table(sb
, gdp
),
4542 EXT4_SB(sb
)->s_itb_per_group
) ||
4543 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4544 EXT4_SB(sb
)->s_itb_per_group
)) {
4546 ext4_error(sb
, "Freeing blocks in system zone - "
4547 "Block = %llu, count = %lu", block
, count
);
4548 /* err = 0. ext4_std_error should be a no op */
4552 BUFFER_TRACE(bitmap_bh
, "getting write access");
4553 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4558 * We are about to modify some metadata. Call the journal APIs
4559 * to unshare ->b_data if a currently-committing transaction is
4562 BUFFER_TRACE(gd_bh
, "get_write_access");
4563 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4566 #ifdef AGGRESSIVE_CHECK
4569 for (i
= 0; i
< count
; i
++)
4570 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4573 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count
);
4575 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4579 if ((flags
& EXT4_FREE_BLOCKS_METADATA
) && ext4_handle_valid(handle
)) {
4580 struct ext4_free_data
*new_entry
;
4582 * blocks being freed are metadata. these blocks shouldn't
4583 * be used until this transaction is committed
4585 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4587 ext4_mb_unload_buddy(&e4b
);
4591 new_entry
->start_blk
= bit
;
4592 new_entry
->group
= block_group
;
4593 new_entry
->count
= count
;
4594 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4596 ext4_lock_group(sb
, block_group
);
4597 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4598 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4600 /* need to update group_info->bb_free and bitmap
4601 * with group lock held. generate_buddy look at
4602 * them with group lock_held
4604 ext4_lock_group(sb
, block_group
);
4605 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4606 mb_free_blocks(inode
, &e4b
, bit
, count
);
4609 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4610 ext4_free_blks_set(sb
, gdp
, ret
);
4611 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4612 ext4_unlock_group(sb
, block_group
);
4613 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4615 if (sbi
->s_log_groups_per_flex
) {
4616 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4617 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4620 ext4_mb_unload_buddy(&e4b
);
4624 /* We dirtied the bitmap block */
4625 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4626 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4628 /* And the group descriptor block */
4629 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4630 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4634 if (overflow
&& !err
) {
4640 ext4_mark_super_dirty(sb
);
4642 if (freed
&& !(flags
& EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
))
4643 dquot_free_block(inode
, freed
);
4645 ext4_std_error(sb
, err
);
4650 * ext4_add_groupblocks() -- Add given blocks to an existing group
4651 * @handle: handle to this transaction
4653 * @block: start physcial block to add to the block group
4654 * @count: number of blocks to free
4656 * This marks the blocks as free in the bitmap and buddy.
4658 void ext4_add_groupblocks(handle_t
*handle
, struct super_block
*sb
,
4659 ext4_fsblk_t block
, unsigned long count
)
4661 struct buffer_head
*bitmap_bh
= NULL
;
4662 struct buffer_head
*gd_bh
;
4663 ext4_group_t block_group
;
4666 struct ext4_group_desc
*desc
;
4667 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4668 struct ext4_buddy e4b
;
4669 int err
= 0, ret
, blk_free_count
;
4670 ext4_grpblk_t blocks_freed
;
4671 struct ext4_group_info
*grp
;
4673 ext4_debug("Adding block(s) %llu-%llu\n", block
, block
+ count
- 1);
4675 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4676 grp
= ext4_get_group_info(sb
, block_group
);
4678 * Check to see if we are freeing blocks across a group
4681 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
))
4684 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4687 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4691 if (in_range(ext4_block_bitmap(sb
, desc
), block
, count
) ||
4692 in_range(ext4_inode_bitmap(sb
, desc
), block
, count
) ||
4693 in_range(block
, ext4_inode_table(sb
, desc
), sbi
->s_itb_per_group
) ||
4694 in_range(block
+ count
- 1, ext4_inode_table(sb
, desc
),
4695 sbi
->s_itb_per_group
)) {
4696 ext4_error(sb
, "Adding blocks in system zones - "
4697 "Block = %llu, count = %lu",
4702 BUFFER_TRACE(bitmap_bh
, "getting write access");
4703 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4708 * We are about to modify some metadata. Call the journal APIs
4709 * to unshare ->b_data if a currently-committing transaction is
4712 BUFFER_TRACE(gd_bh
, "get_write_access");
4713 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4717 for (i
= 0, blocks_freed
= 0; i
< count
; i
++) {
4718 BUFFER_TRACE(bitmap_bh
, "clear bit");
4719 if (!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
)) {
4720 ext4_error(sb
, "bit already cleared for block %llu",
4721 (ext4_fsblk_t
)(block
+ i
));
4722 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
4728 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4733 * need to update group_info->bb_free and bitmap
4734 * with group lock held. generate_buddy look at
4735 * them with group lock_held
4737 ext4_lock_group(sb
, block_group
);
4738 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4739 mb_free_blocks(NULL
, &e4b
, bit
, count
);
4740 blk_free_count
= blocks_freed
+ ext4_free_blks_count(sb
, desc
);
4741 ext4_free_blks_set(sb
, desc
, blk_free_count
);
4742 desc
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, desc
);
4743 ext4_unlock_group(sb
, block_group
);
4744 percpu_counter_add(&sbi
->s_freeblocks_counter
, blocks_freed
);
4746 if (sbi
->s_log_groups_per_flex
) {
4747 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4748 atomic_add(blocks_freed
,
4749 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4752 ext4_mb_unload_buddy(&e4b
);
4754 /* We dirtied the bitmap block */
4755 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4756 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4758 /* And the group descriptor block */
4759 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4760 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4766 ext4_std_error(sb
, err
);
4771 * ext4_trim_extent -- function to TRIM one single free extent in the group
4772 * @sb: super block for the file system
4773 * @start: starting block of the free extent in the alloc. group
4774 * @count: number of blocks to TRIM
4775 * @group: alloc. group we are working with
4776 * @e4b: ext4 buddy for the group
4778 * Trim "count" blocks starting at "start" in the "group". To assure that no
4779 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4780 * be called with under the group lock.
4782 static void ext4_trim_extent(struct super_block
*sb
, int start
, int count
,
4783 ext4_group_t group
, struct ext4_buddy
*e4b
)
4785 struct ext4_free_extent ex
;
4787 assert_spin_locked(ext4_group_lock_ptr(sb
, group
));
4789 ex
.fe_start
= start
;
4790 ex
.fe_group
= group
;
4794 * Mark blocks used, so no one can reuse them while
4797 mb_mark_used(e4b
, &ex
);
4798 ext4_unlock_group(sb
, group
);
4799 ext4_issue_discard(sb
, group
, start
, count
);
4800 ext4_lock_group(sb
, group
);
4801 mb_free_blocks(NULL
, e4b
, start
, ex
.fe_len
);
4805 * ext4_trim_all_free -- function to trim all free space in alloc. group
4806 * @sb: super block for file system
4808 * @start: first group block to examine
4809 * @max: last group block to examine
4810 * @minblocks: minimum extent block count
4812 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4813 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4817 * ext4_trim_all_free walks through group's block bitmap searching for free
4818 * extents. When the free extent is found, mark it as used in group buddy
4819 * bitmap. Then issue a TRIM command on this extent and free the extent in
4820 * the group buddy bitmap. This is done until whole group is scanned.
4822 static ext4_grpblk_t
4823 ext4_trim_all_free(struct super_block
*sb
, ext4_group_t group
,
4824 ext4_grpblk_t start
, ext4_grpblk_t max
,
4825 ext4_grpblk_t minblocks
)
4828 ext4_grpblk_t next
, count
= 0;
4829 struct ext4_buddy e4b
;
4832 ret
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4834 ext4_error(sb
, "Error in loading buddy "
4835 "information for %u", group
);
4838 bitmap
= e4b
.bd_bitmap
;
4840 ext4_lock_group(sb
, group
);
4841 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
4842 e4b
.bd_info
->bb_first_free
: start
;
4844 while (start
< max
) {
4845 start
= mb_find_next_zero_bit(bitmap
, max
, start
);
4848 next
= mb_find_next_bit(bitmap
, max
, start
);
4850 if ((next
- start
) >= minblocks
) {
4851 ext4_trim_extent(sb
, start
,
4852 next
- start
, group
, &e4b
);
4853 count
+= next
- start
;
4857 if (fatal_signal_pending(current
)) {
4858 count
= -ERESTARTSYS
;
4862 if (need_resched()) {
4863 ext4_unlock_group(sb
, group
);
4865 ext4_lock_group(sb
, group
);
4868 if ((e4b
.bd_info
->bb_free
- count
) < minblocks
)
4871 ext4_unlock_group(sb
, group
);
4872 ext4_mb_unload_buddy(&e4b
);
4874 ext4_debug("trimmed %d blocks in the group %d\n",
4881 * ext4_trim_fs() -- trim ioctl handle function
4882 * @sb: superblock for filesystem
4883 * @range: fstrim_range structure
4885 * start: First Byte to trim
4886 * len: number of Bytes to trim from start
4887 * minlen: minimum extent length in Bytes
4888 * ext4_trim_fs goes through all allocation groups containing Bytes from
4889 * start to start+len. For each such a group ext4_trim_all_free function
4890 * is invoked to trim all free space.
4892 int ext4_trim_fs(struct super_block
*sb
, struct fstrim_range
*range
)
4894 struct ext4_group_info
*grp
;
4895 ext4_group_t first_group
, last_group
;
4896 ext4_group_t group
, ngroups
= ext4_get_groups_count(sb
);
4897 ext4_grpblk_t cnt
= 0, first_block
, last_block
;
4898 uint64_t start
, len
, minlen
, trimmed
= 0;
4899 ext4_fsblk_t first_data_blk
=
4900 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
4903 start
= range
->start
>> sb
->s_blocksize_bits
;
4904 len
= range
->len
>> sb
->s_blocksize_bits
;
4905 minlen
= range
->minlen
>> sb
->s_blocksize_bits
;
4907 if (unlikely(minlen
> EXT4_BLOCKS_PER_GROUP(sb
)))
4909 if (start
< first_data_blk
) {
4910 len
-= first_data_blk
- start
;
4911 start
= first_data_blk
;
4914 /* Determine first and last group to examine based on start and len */
4915 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) start
,
4916 &first_group
, &first_block
);
4917 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) (start
+ len
),
4918 &last_group
, &last_block
);
4919 last_group
= (last_group
> ngroups
- 1) ? ngroups
- 1 : last_group
;
4920 last_block
= EXT4_BLOCKS_PER_GROUP(sb
);
4922 if (first_group
> last_group
)
4925 for (group
= first_group
; group
<= last_group
; group
++) {
4926 grp
= ext4_get_group_info(sb
, group
);
4927 /* We only do this if the grp has never been initialized */
4928 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
4929 ret
= ext4_mb_init_group(sb
, group
);
4935 * For all the groups except the last one, last block will
4936 * always be EXT4_BLOCKS_PER_GROUP(sb), so we only need to
4937 * change it for the last group in which case start +
4938 * len < EXT4_BLOCKS_PER_GROUP(sb).
4940 if (first_block
+ len
< EXT4_BLOCKS_PER_GROUP(sb
))
4941 last_block
= first_block
+ len
;
4942 len
-= last_block
- first_block
;
4944 if (grp
->bb_free
>= minlen
) {
4945 cnt
= ext4_trim_all_free(sb
, group
, first_block
,
4946 last_block
, minlen
);
4955 range
->len
= trimmed
* sb
->s_blocksize
;